home *** CD-ROM | disk | FTP | other *** search
- package qs.controls
- {
- import flash.display.DisplayObject;
- import flash.display.DisplayObjectContainer;
- import flash.events.TimerEvent;
- import flash.geom.Matrix;
- import flash.utils.Dictionary;
- import flash.utils.Timer;
- import mx.core.IFlexDisplayObject;
-
- public class LayoutAnimator
- {
- private static var running:Number = 0;
-
- public var updateFunction:Function;
-
- private var _targets:Dictionary;
-
- public var initializeFunction:Function;
-
- private var _timer:Timer;
-
- public var autoPrioritize:Boolean = true;
-
- private var _animationSpeed:Number = 0.25;
-
- private const TRANSLATE_TOLERANCE:Number = 0.5;
-
- private const SCALE_TOLERANCE:Number = 0.01;
-
- private var _removedTargets:Dictionary;
-
- public var layoutFunction:Function;
-
- private const ALPHA_TOLERANCE:Number = 0.01;
-
- private const DEFAULT_ANIMATION_SPEED:Number = 0.25;
-
- private const SIZE_TOLERANCE:Number = 0.5;
-
- private var _priorityInvalid:Boolean = true;
-
- private var _layoutInvalid:Boolean = false;
-
- private var _orderedTargets:Array;
-
- public var releaseFunction:Function;
-
- public function LayoutAnimator()
- {
- super();
- this._timer = new Timer(10);
- this._timer.addEventListener(TimerEvent.TIMER,this.timerHandler);
- this._targets = new Dictionary(true);
- }
-
- public function set items(param1:Object) : void
- {
- var _loc2_:Array = null;
- var _loc3_:int = 0;
- var _loc4_:DisplayObjectContainer = null;
- this._targets = new Dictionary(true);
- if(param1 is Array)
- {
- _loc2_ = param1 as Array;
- _loc3_ = 0;
- while(_loc3_ < _loc2_.length)
- {
- this._targets[_loc2_[_loc3_]] = new LayoutTarget(_loc2_[_loc3_]);
- _loc3_++;
- }
- }
- else if(param1 is DisplayObjectContainer)
- {
- _loc4_ = DisplayObjectContainer(param1);
- _loc3_ = 0;
- while(_loc3_ < _loc4_.numChildren)
- {
- this._targets[_loc4_.getChildAt(_loc3_)] = new LayoutTarget(IFlexDisplayObject(_loc4_.getChildAt(_loc3_)));
- _loc3_++;
- }
- }
- }
-
- public function targetFor(param1:IFlexDisplayObject) : LayoutTarget
- {
- var _loc2_:LayoutTarget = this._targets[param1];
- if(_loc2_ == null)
- {
- _loc2_ = this._targets[param1] = new LayoutTarget(param1);
- }
- return _loc2_;
- }
-
- private function startTimer() : void
- {
- if(this._timer.running == false)
- {
- this._timer.reset();
- this._timer.start();
- ++running;
- }
- }
-
- public function updateLayoutWithoutAnimation() : void
- {
- var _loc1_:* = undefined;
- var _loc2_:LayoutTarget = null;
- var _loc3_:IFlexDisplayObject = null;
- var _loc4_:Matrix = null;
- if(this._layoutInvalid == true && this.layoutFunction != null)
- {
- this._layoutInvalid = false;
- this.layoutFunction();
- }
- for(_loc1_ in this._targets)
- {
- _loc2_ = this._targets[_loc1_];
- _loc3_ = _loc2_.item;
- _loc4_ = DisplayObject(_loc3_).transform.matrix;
- _loc4_.tx = _loc2_.x;
- _loc4_.ty = _loc2_.y;
- _loc4_.a = _loc2_.scaleX;
- _loc4_.d = _loc2_.scaleY;
- DisplayObject(_loc3_).alpha = _loc2_.alpha;
- DisplayObject(_loc3_).transform.matrix = _loc4_;
- _loc3_.setActualSize(_loc2_.unscaledWidth,_loc2_.unscaledHeight);
- if(_loc2_.state == "removed")
- {
- delete this._targets[_loc2_.item];
- }
- else
- {
- _loc2_.state = "positioned";
- }
- }
- this.stopTimer();
- }
-
- public function updateLayout() : void
- {
- var _loc4_:* = undefined;
- var _loc5_:LayoutTarget = null;
- var _loc6_:IFlexDisplayObject = null;
- var _loc7_:Boolean = false;
- var _loc8_:Matrix = null;
- var _loc9_:Number = NaN;
- var _loc10_:Number = NaN;
- if(this._layoutInvalid == true && this.layoutFunction != null)
- {
- this._layoutInvalid = false;
- this.layoutFunction();
- this._priorityInvalid = true;
- }
- var _loc1_:Boolean = false;
- if(this._priorityInvalid)
- {
- this._priorityInvalid = false;
- this._orderedTargets = [];
- for(_loc4_ in this._targets)
- {
- _loc5_ = this._targets[_loc4_];
- if(this.autoPrioritize)
- {
- _loc5_.priority = _loc5_.state == "removed" ? 0 : (_loc5_.state == "added" ? 2 : 1);
- }
- this._orderedTargets.push(this._targets[_loc4_]);
- }
- this._orderedTargets.sortOn("priority",Array.NUMERIC | Array.DESCENDING);
- }
- var _loc2_:int = -1;
- var _loc3_:int = int(this._orderedTargets.length - 1);
- while(_loc3_ >= 0)
- {
- _loc5_ = this._orderedTargets[_loc3_];
- _loc6_ = _loc5_.item;
- _loc7_ = false;
- _loc8_ = DisplayObject(_loc6_).transform.matrix;
- if(_loc2_ >= 0 && _loc5_.priority > _loc2_)
- {
- break;
- }
- _loc9_ = Number(_loc6_.width);
- _loc10_ = Number(_loc6_.height);
- if(_loc5_.state == "added")
- {
- _loc1_ = true;
- if(_loc5_.initializeFunction != null)
- {
- _loc5_.initializeFunction(_loc5_);
- _loc8_ = DisplayObject(_loc6_).transform.matrix;
- _loc9_ = Number(_loc6_.width);
- _loc10_ = Number(_loc6_.height);
- }
- else if(this.initializeFunction != null)
- {
- this.initializeFunction(_loc5_);
- _loc8_ = DisplayObject(_loc6_).transform.matrix;
- _loc9_ = Number(_loc6_.width);
- _loc10_ = Number(_loc6_.height);
- }
- else
- {
- _loc8_.tx = _loc5_.x + _loc5_.scaleX * _loc5_.unscaledWidth / 2;
- _loc8_.ty = _loc5_.y + _loc5_.scaleY * _loc5_.unscaledHeight / 2;
- _loc8_.a = 0;
- _loc8_.d = 0;
- }
- _loc2_ = _loc5_.priority;
- _loc5_.state = "positioned";
- }
- else if(_loc5_.animate == false || Math.abs(_loc8_.tx - _loc5_.x) < this.TRANSLATE_TOLERANCE && Math.abs(_loc8_.ty - _loc5_.y) < this.TRANSLATE_TOLERANCE && Math.abs(_loc8_.a - _loc5_.scaleX) < this.SCALE_TOLERANCE && Math.abs(_loc8_.d - _loc5_.scaleY) < this.SCALE_TOLERANCE && Math.abs(_loc6_.width - _loc5_.unscaledWidth) < this.SIZE_TOLERANCE && Math.abs(_loc6_.alpha - _loc5_.alpha) < this.ALPHA_TOLERANCE && Math.abs(_loc6_.height - _loc5_.unscaledHeight) < this.SIZE_TOLERANCE)
- {
- _loc8_.tx = _loc5_.x;
- _loc8_.ty = _loc5_.y;
- _loc8_.a = _loc5_.scaleX;
- _loc8_.d = _loc5_.scaleY;
- _loc6_.alpha = _loc5_.alpha;
- _loc6_.setActualSize(_loc5_.unscaledWidth,_loc5_.unscaledHeight);
- _loc7_ = true;
- if(_loc5_.state == "removed")
- {
- delete this._targets[_loc5_.item];
- }
- this._orderedTargets.splice(_loc3_,1);
- }
- else
- {
- _loc1_ = true;
- _loc8_.tx += this._animationSpeed * (_loc5_.x - _loc8_.tx);
- _loc8_.ty += this._animationSpeed * (_loc5_.y - _loc8_.ty);
- _loc8_.a += this._animationSpeed * (_loc5_.scaleX - _loc8_.a);
- _loc8_.d += this._animationSpeed * (_loc5_.scaleY - _loc8_.d);
- _loc6_.alpha += this._animationSpeed * (_loc5_.alpha - _loc6_.alpha);
- _loc2_ = _loc5_.priority;
- }
- DisplayObject(_loc6_).transform.matrix = _loc8_;
- if(_loc7_)
- {
- _loc6_.setActualSize(_loc5_.unscaledWidth,_loc5_.unscaledHeight);
- }
- else if(_loc9_ != _loc5_.unscaledWidth || _loc10_ != _loc5_.unscaledHeight)
- {
- _loc6_.setActualSize(_loc9_ + this._animationSpeed * (_loc5_.unscaledWidth - _loc9_),_loc10_ + this._animationSpeed * (_loc5_.unscaledHeight - _loc10_));
- }
- _loc3_--;
- }
- if(this.updateFunction != null)
- {
- this.updateFunction();
- }
- if(_loc1_ == false)
- {
- this.stopTimer();
- }
- }
-
- public function invalidateLayout(param1:Boolean = false) : void
- {
- this._layoutInvalid = true;
- this.startTimer();
- if(param1)
- {
- this.updateLayout();
- }
- }
-
- public function set animationSpeed(param1:Number) : void
- {
- this._animationSpeed = isNaN(param1) ? this.DEFAULT_ANIMATION_SPEED : param1;
- }
-
- public function releaseTarget(param1:IFlexDisplayObject) : LayoutTarget
- {
- var _loc2_:LayoutTarget = this._targets[param1];
- if(_loc2_ == null)
- {
- return null;
- }
- _loc2_.state = "removed";
- if(_loc2_.releaseFunction != null)
- {
- _loc2_.releaseFunction(_loc2_);
- }
- else if(this.releaseFunction != null)
- {
- this.releaseFunction(_loc2_);
- }
- else
- {
- _loc2_.x += _loc2_.unscaledWidth * _loc2_.scaleX / 2;
- _loc2_.y += _loc2_.unscaledHeight * _loc2_.scaleY / 2;
- _loc2_.scaleX = 0;
- _loc2_.scaleY = 0;
- }
- this.invalidateLayout();
- return _loc2_;
- }
-
- private function timerHandler(param1:TimerEvent) : void
- {
- this.updateLayout();
- param1.updateAfterEvent();
- }
-
- public function get targets() : Dictionary
- {
- return this._targets;
- }
-
- public function get animating() : Boolean
- {
- return this._timer.running;
- }
-
- public function get animationSpeed() : Number
- {
- return this._animationSpeed;
- }
-
- public function layout(param1:Boolean = false) : void
- {
- this._layoutInvalid = false;
- this.startTimer();
- if(param1)
- {
- this.updateLayout();
- }
- }
-
- private function stopTimer() : void
- {
- if(this._timer.running)
- {
- --running;
- this._timer.stop();
- }
- }
- }
- }
-
-