package com.mindfock.ui { import flash.display.DisplayObject; import flash.display.Sprite; import flash.events.*; import gs.TweenLite; /** * ... * @author John del Rosario */ public class Shuffler extends Sprite{ private var _items:Array; private var _width:Number; private var _height:Number; private var _max:int; private var _focusedItem:DisplayObject; private var _stacked:Boolean; /** * constructor * @param width = width of shuffler. limits shuffling distance * @param height = height of shuffler. limits shuffling distance * @param max = maximum number of items allowed. 0 sets no limit. * @param items = optional array of DisplayObjects to be intially added. max still apply. */ public function Shuffler(width:Number, height:Number, max:int = 0, items:Array = null) { _width = width; _height = height; _items = new Array(); _max = max; _stacked = false; init(); } private function init():void { } /** * adds individual items to the shuffler, and puts it on top in focus * * @param item = item to add * * */ public function addItem(item:DisplayObject):void { _items.push(item); focusedItem = item; newItem(item); addChild(item); item.addEventListener(MouseEvent.CLICK, focusItem); if(_items.length > _max && _max > 0){ deleteItem(0); } } /** * sets focus on item and aligns it to center * * @param item = item to set focus on * * */ public function setFocusOn(item:DisplayObject):void { for (var i:int = 0; i < _items.length; i++) { if (item === _items[i]) { if (focusedItem != item) { focusedItem = item; moveItemTop(i); focusing(_items[_items.length -1]); update(); } break; } } } /** * deletes item at index; remove from array, and remove from display list * * @param index = index of item to be deleted * */ public function deleteItem(index:int):DisplayObject{ var temp:DisplayObject; if(index < _items.length && index >= 0){ temp = _items[index]; for(var i:int = 0; i < _items.length; i++){ if(_items[i] == temp){ removeChild(_items[i]); } } _items.splice(index, 1); update(); } return temp; } /** * shuffles the items randomly * * */ public function shuffle():void { for (var i:int = 0; i < _items.length; i++) { TweenLite.to(_items[i], 1, {x: Math.random() * _width, y:Math.random() *_height, rotation: Math.random() * 360 } ); } _stacked = false; } /** * stacks the items in order * * */ public function stack():void { var j:int = 0; for (var i:int = _items.length - 1; i >= 0; i--) { var offset:int = j * 10; setChildIndex(_items[i], i); TweenLite.to(_items[i], .5, { x: _width / 2 + offset, y:_height / 2 + offset, rotation: 0 } ); j++; } _stacked = true; } /** * Function to call when item is currently set to focus * * @param item = item getting the focus * * */ private function focusing(item:DisplayObject):void { TweenLite.to(item, 1, {x: _width /2, y:_height /2, rotation:0 } ); } private function newItem(item:DisplayObject):void { item.rotation = Math.random() * 360; TweenLite.to(item, 1, {x: Math.random() * _width, y:Math.random() *_height, rotation: 0 } ); } /** * moves item to last of array, and puts it on top of display list * * @param index = index of item to move * * */ private function moveItemTop(index:int):void{ var temp:DisplayObject = _items[index] as DisplayObject; _items.splice(index, 1); _items.push(temp); setChildIndex(temp, _items.length - 1); } private function update():void{ if(_stacked){ stack(); } } private function focusItem(event:MouseEvent):void { setFocusOn(event.target as DisplayObject); } /** * * Getters and Setters * */ public function get max():int { return _max; } public function set max(value:int):void { _max = value; } public function get focusedItem():DisplayObject { return _focusedItem; } public function set focusedItem(value:DisplayObject):void { _focusedItem = value; } } }