package com.exanimo.controls { import com.exanimo.controls.Drawer; import com.exanimo.controls.DrawerDirection; import com.exanimo.transitions.GCSafeTween; import com.exanimo.transitions.TweenParams; import fl.transitions.TweenEvent; import fl.transitions.easing.*; import flash.events.Event; import flash.geom.Point; import flash.geom.Rectangle; import flash.utils.Dictionary; public class DrawerGroup { private static var _groups:Array = []; private var _direction:String; private var _drawers:Array; private var _homePositions:Dictionary; private var _overhang:Number; private var _topDrawer:Drawer; public var isOpen:Boolean; public var name:String; public var openTweenParams:TweenParams; public var closeTweenParams:TweenParams; /** * * * */ public function DrawerGroup(name:String) { this.openTweenParams = new TweenParams({duration: 1}); this.closeTweenParams = new TweenParams({duration: 1}); this.name = name; this.isOpen = false; this._drawers = []; this.direction = DrawerDirection.LEFT; DrawerGroup._groups[name] = this; this._homePositions = new Dictionary(); } // // public static // /** * * * */ public static function getGroup(name:String):DrawerGroup { return DrawerGroup._groups[name] || new DrawerGroup(name); } // // get/set // /** * * * */ public function get direction():String { return this._direction; } public function set direction(direction:String):void { this._direction = direction; } /** * * * */ public function get overhang():Number { return this._overhang || this._drawers[0].tab[this._getWidthOrHeight()]; } public function set overhang(overhang:Number):void { this._overhang = overhang; } // // public // /** * * * */ public function addDrawer(drawer:Drawer):void { this._drawers.push(drawer); this._homePositions[drawer] = new Point(drawer.obj.x, drawer.obj.y); this._bringToTop(drawer); } /** * * * */ public function close():void { if (this.isOpen) { this.isOpen = false; // Animate the group closed. this._animateClosed(); } } /** * * * */ public function open(drawer:Drawer = null):void { this._topDrawer.isOpen = false; if (drawer) { this._bringToTop(drawer); } if (!this.isOpen) { this._animateOpen(); } this.isOpen = true; } /** * * * */ public function removeDrawer(drawer:Drawer):void { for (var i:Number = 0; i < this._drawers.length; i++) { if (this._drawers[i] == drawer) { this._drawers.splice(i, 1); delete this._homePositions[drawer]; break; } } } // // private // /** * * * */ private function _animateOpen():void { var drawer:Drawer; var widthOrHeight:String = this._getWidthOrHeight(); var xOrY:String = this._getXorY(); var t:GCSafeTween; var offset:Number = 0; var i:uint; for (i = 0; i < this._drawers.length; i++) { drawer = this._drawers[i]; var bounds:Rectangle = drawer.obj.getRect(drawer.obj); switch(this.direction) { case DrawerDirection.UP: case DrawerDirection.DOWN: offset = Math.max(offset, bounds.height - drawer._tab.height); break; case DrawerDirection.RIGHT: case DrawerDirection.LEFT: offset = Math.max(offset, bounds.width - drawer._tab.width); break; } } switch(this.direction) { case DrawerDirection.RIGHT: case DrawerDirection.DOWN: offset *= -1; break; } for (i = 0; i < this._drawers.length; i++) { drawer = this._drawers[i]; t = new GCSafeTween(drawer.obj, xOrY, this.openTweenParams.func, drawer.obj[xOrY], this._homePositions[drawer][xOrY] - offset, this.openTweenParams.duration, this.openTweenParams.useSeconds); } } /** * * * */ private function _animateClosed():void { var drawer:Drawer; var xOrY:String = this._getXorY(); var t:GCSafeTween; for (var i:Number = 0; i < this._drawers.length; i++) { drawer = this._drawers[i]; t = new GCSafeTween(drawer.obj, xOrY, this.closeTweenParams.func, drawer.obj[xOrY], this._homePositions[drawer][xOrY], this.closeTweenParams.duration, this.closeTweenParams.useSeconds); } } /** * * * */ private function _bringToTop(drawer:Drawer):void { if (this._topDrawer) { var topDrawerDepth:int = this._topDrawer.obj.parent.getChildIndex(this._topDrawer.obj); var newDrawerDepth:int = drawer.obj.parent.getChildIndex(drawer.obj); if (topDrawerDepth > newDrawerDepth) { drawer.obj.parent.swapChildren(drawer.obj, this._topDrawer.obj); } } this._topDrawer = drawer; } /** * * * */ private function _getXorY():String { switch(this.direction) { case DrawerDirection.UP: case DrawerDirection.DOWN: return 'y'; case DrawerDirection.LEFT: case DrawerDirection.RIGHT: return 'x'; default: throw new Error('Invalid Drawer direction value'); } } /** * * * */ private function _getWidthOrHeight():String { switch(this.direction) { case DrawerDirection.UP: case DrawerDirection.DOWN: return 'height'; case DrawerDirection.LEFT: case DrawerDirection.RIGHT: return 'width'; default: throw new Error('Invalid Drawer direction value'); } } } }