home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / qs / controls / LayoutAnimator.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  11.2 KB  |  342 lines

  1. package qs.controls
  2. {
  3.    import flash.display.DisplayObject;
  4.    import flash.display.DisplayObjectContainer;
  5.    import flash.events.TimerEvent;
  6.    import flash.geom.Matrix;
  7.    import flash.utils.Dictionary;
  8.    import flash.utils.Timer;
  9.    import mx.core.IFlexDisplayObject;
  10.    
  11.    public class LayoutAnimator
  12.    {
  13.       private static var running:Number = 0;
  14.       
  15.       public var updateFunction:Function;
  16.       
  17.       private var _targets:Dictionary;
  18.       
  19.       public var initializeFunction:Function;
  20.       
  21.       private var _timer:Timer;
  22.       
  23.       public var autoPrioritize:Boolean = true;
  24.       
  25.       private var _animationSpeed:Number = 0.25;
  26.       
  27.       private const TRANSLATE_TOLERANCE:Number = 0.5;
  28.       
  29.       private const SCALE_TOLERANCE:Number = 0.01;
  30.       
  31.       private var _removedTargets:Dictionary;
  32.       
  33.       public var layoutFunction:Function;
  34.       
  35.       private const ALPHA_TOLERANCE:Number = 0.01;
  36.       
  37.       private const DEFAULT_ANIMATION_SPEED:Number = 0.25;
  38.       
  39.       private const SIZE_TOLERANCE:Number = 0.5;
  40.       
  41.       private var _priorityInvalid:Boolean = true;
  42.       
  43.       private var _layoutInvalid:Boolean = false;
  44.       
  45.       private var _orderedTargets:Array;
  46.       
  47.       public var releaseFunction:Function;
  48.       
  49.       public function LayoutAnimator()
  50.       {
  51.          super();
  52.          this._timer = new Timer(10);
  53.          this._timer.addEventListener(TimerEvent.TIMER,this.timerHandler);
  54.          this._targets = new Dictionary(true);
  55.       }
  56.       
  57.       public function set items(param1:Object) : void
  58.       {
  59.          var _loc2_:Array = null;
  60.          var _loc3_:int = 0;
  61.          var _loc4_:DisplayObjectContainer = null;
  62.          this._targets = new Dictionary(true);
  63.          if(param1 is Array)
  64.          {
  65.             _loc2_ = param1 as Array;
  66.             _loc3_ = 0;
  67.             while(_loc3_ < _loc2_.length)
  68.             {
  69.                this._targets[_loc2_[_loc3_]] = new LayoutTarget(_loc2_[_loc3_]);
  70.                _loc3_++;
  71.             }
  72.          }
  73.          else if(param1 is DisplayObjectContainer)
  74.          {
  75.             _loc4_ = DisplayObjectContainer(param1);
  76.             _loc3_ = 0;
  77.             while(_loc3_ < _loc4_.numChildren)
  78.             {
  79.                this._targets[_loc4_.getChildAt(_loc3_)] = new LayoutTarget(IFlexDisplayObject(_loc4_.getChildAt(_loc3_)));
  80.                _loc3_++;
  81.             }
  82.          }
  83.       }
  84.       
  85.       public function targetFor(param1:IFlexDisplayObject) : LayoutTarget
  86.       {
  87.          var _loc2_:LayoutTarget = this._targets[param1];
  88.          if(_loc2_ == null)
  89.          {
  90.             _loc2_ = this._targets[param1] = new LayoutTarget(param1);
  91.          }
  92.          return _loc2_;
  93.       }
  94.       
  95.       private function startTimer() : void
  96.       {
  97.          if(this._timer.running == false)
  98.          {
  99.             this._timer.reset();
  100.             this._timer.start();
  101.             ++running;
  102.          }
  103.       }
  104.       
  105.       public function updateLayoutWithoutAnimation() : void
  106.       {
  107.          var _loc1_:* = undefined;
  108.          var _loc2_:LayoutTarget = null;
  109.          var _loc3_:IFlexDisplayObject = null;
  110.          var _loc4_:Matrix = null;
  111.          if(this._layoutInvalid == true && this.layoutFunction != null)
  112.          {
  113.             this._layoutInvalid = false;
  114.             this.layoutFunction();
  115.          }
  116.          for(_loc1_ in this._targets)
  117.          {
  118.             _loc2_ = this._targets[_loc1_];
  119.             _loc3_ = _loc2_.item;
  120.             _loc4_ = DisplayObject(_loc3_).transform.matrix;
  121.             _loc4_.tx = _loc2_.x;
  122.             _loc4_.ty = _loc2_.y;
  123.             _loc4_.a = _loc2_.scaleX;
  124.             _loc4_.d = _loc2_.scaleY;
  125.             DisplayObject(_loc3_).alpha = _loc2_.alpha;
  126.             DisplayObject(_loc3_).transform.matrix = _loc4_;
  127.             _loc3_.setActualSize(_loc2_.unscaledWidth,_loc2_.unscaledHeight);
  128.             if(_loc2_.state == "removed")
  129.             {
  130.                delete this._targets[_loc2_.item];
  131.             }
  132.             else
  133.             {
  134.                _loc2_.state = "positioned";
  135.             }
  136.          }
  137.          this.stopTimer();
  138.       }
  139.       
  140.       public function updateLayout() : void
  141.       {
  142.          var _loc4_:* = undefined;
  143.          var _loc5_:LayoutTarget = null;
  144.          var _loc6_:IFlexDisplayObject = null;
  145.          var _loc7_:Boolean = false;
  146.          var _loc8_:Matrix = null;
  147.          var _loc9_:Number = NaN;
  148.          var _loc10_:Number = NaN;
  149.          if(this._layoutInvalid == true && this.layoutFunction != null)
  150.          {
  151.             this._layoutInvalid = false;
  152.             this.layoutFunction();
  153.             this._priorityInvalid = true;
  154.          }
  155.          var _loc1_:Boolean = false;
  156.          if(this._priorityInvalid)
  157.          {
  158.             this._priorityInvalid = false;
  159.             this._orderedTargets = [];
  160.             for(_loc4_ in this._targets)
  161.             {
  162.                _loc5_ = this._targets[_loc4_];
  163.                if(this.autoPrioritize)
  164.                {
  165.                   _loc5_.priority = _loc5_.state == "removed" ? 0 : (_loc5_.state == "added" ? 2 : 1);
  166.                }
  167.                this._orderedTargets.push(this._targets[_loc4_]);
  168.             }
  169.             this._orderedTargets.sortOn("priority",Array.NUMERIC | Array.DESCENDING);
  170.          }
  171.          var _loc2_:int = -1;
  172.          var _loc3_:int = int(this._orderedTargets.length - 1);
  173.          while(_loc3_ >= 0)
  174.          {
  175.             _loc5_ = this._orderedTargets[_loc3_];
  176.             _loc6_ = _loc5_.item;
  177.             _loc7_ = false;
  178.             _loc8_ = DisplayObject(_loc6_).transform.matrix;
  179.             if(_loc2_ >= 0 && _loc5_.priority > _loc2_)
  180.             {
  181.                break;
  182.             }
  183.             _loc9_ = Number(_loc6_.width);
  184.             _loc10_ = Number(_loc6_.height);
  185.             if(_loc5_.state == "added")
  186.             {
  187.                _loc1_ = true;
  188.                if(_loc5_.initializeFunction != null)
  189.                {
  190.                   _loc5_.initializeFunction(_loc5_);
  191.                   _loc8_ = DisplayObject(_loc6_).transform.matrix;
  192.                   _loc9_ = Number(_loc6_.width);
  193.                   _loc10_ = Number(_loc6_.height);
  194.                }
  195.                else if(this.initializeFunction != null)
  196.                {
  197.                   this.initializeFunction(_loc5_);
  198.                   _loc8_ = DisplayObject(_loc6_).transform.matrix;
  199.                   _loc9_ = Number(_loc6_.width);
  200.                   _loc10_ = Number(_loc6_.height);
  201.                }
  202.                else
  203.                {
  204.                   _loc8_.tx = _loc5_.x + _loc5_.scaleX * _loc5_.unscaledWidth / 2;
  205.                   _loc8_.ty = _loc5_.y + _loc5_.scaleY * _loc5_.unscaledHeight / 2;
  206.                   _loc8_.a = 0;
  207.                   _loc8_.d = 0;
  208.                }
  209.                _loc2_ = _loc5_.priority;
  210.                _loc5_.state = "positioned";
  211.             }
  212.             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)
  213.             {
  214.                _loc8_.tx = _loc5_.x;
  215.                _loc8_.ty = _loc5_.y;
  216.                _loc8_.a = _loc5_.scaleX;
  217.                _loc8_.d = _loc5_.scaleY;
  218.                _loc6_.alpha = _loc5_.alpha;
  219.                _loc6_.setActualSize(_loc5_.unscaledWidth,_loc5_.unscaledHeight);
  220.                _loc7_ = true;
  221.                if(_loc5_.state == "removed")
  222.                {
  223.                   delete this._targets[_loc5_.item];
  224.                }
  225.                this._orderedTargets.splice(_loc3_,1);
  226.             }
  227.             else
  228.             {
  229.                _loc1_ = true;
  230.                _loc8_.tx += this._animationSpeed * (_loc5_.x - _loc8_.tx);
  231.                _loc8_.ty += this._animationSpeed * (_loc5_.y - _loc8_.ty);
  232.                _loc8_.a += this._animationSpeed * (_loc5_.scaleX - _loc8_.a);
  233.                _loc8_.d += this._animationSpeed * (_loc5_.scaleY - _loc8_.d);
  234.                _loc6_.alpha += this._animationSpeed * (_loc5_.alpha - _loc6_.alpha);
  235.                _loc2_ = _loc5_.priority;
  236.             }
  237.             DisplayObject(_loc6_).transform.matrix = _loc8_;
  238.             if(_loc7_)
  239.             {
  240.                _loc6_.setActualSize(_loc5_.unscaledWidth,_loc5_.unscaledHeight);
  241.             }
  242.             else if(_loc9_ != _loc5_.unscaledWidth || _loc10_ != _loc5_.unscaledHeight)
  243.             {
  244.                _loc6_.setActualSize(_loc9_ + this._animationSpeed * (_loc5_.unscaledWidth - _loc9_),_loc10_ + this._animationSpeed * (_loc5_.unscaledHeight - _loc10_));
  245.             }
  246.             _loc3_--;
  247.          }
  248.          if(this.updateFunction != null)
  249.          {
  250.             this.updateFunction();
  251.          }
  252.          if(_loc1_ == false)
  253.          {
  254.             this.stopTimer();
  255.          }
  256.       }
  257.       
  258.       public function invalidateLayout(param1:Boolean = false) : void
  259.       {
  260.          this._layoutInvalid = true;
  261.          this.startTimer();
  262.          if(param1)
  263.          {
  264.             this.updateLayout();
  265.          }
  266.       }
  267.       
  268.       public function set animationSpeed(param1:Number) : void
  269.       {
  270.          this._animationSpeed = isNaN(param1) ? this.DEFAULT_ANIMATION_SPEED : param1;
  271.       }
  272.       
  273.       public function releaseTarget(param1:IFlexDisplayObject) : LayoutTarget
  274.       {
  275.          var _loc2_:LayoutTarget = this._targets[param1];
  276.          if(_loc2_ == null)
  277.          {
  278.             return null;
  279.          }
  280.          _loc2_.state = "removed";
  281.          if(_loc2_.releaseFunction != null)
  282.          {
  283.             _loc2_.releaseFunction(_loc2_);
  284.          }
  285.          else if(this.releaseFunction != null)
  286.          {
  287.             this.releaseFunction(_loc2_);
  288.          }
  289.          else
  290.          {
  291.             _loc2_.x += _loc2_.unscaledWidth * _loc2_.scaleX / 2;
  292.             _loc2_.y += _loc2_.unscaledHeight * _loc2_.scaleY / 2;
  293.             _loc2_.scaleX = 0;
  294.             _loc2_.scaleY = 0;
  295.          }
  296.          this.invalidateLayout();
  297.          return _loc2_;
  298.       }
  299.       
  300.       private function timerHandler(param1:TimerEvent) : void
  301.       {
  302.          this.updateLayout();
  303.          param1.updateAfterEvent();
  304.       }
  305.       
  306.       public function get targets() : Dictionary
  307.       {
  308.          return this._targets;
  309.       }
  310.       
  311.       public function get animating() : Boolean
  312.       {
  313.          return this._timer.running;
  314.       }
  315.       
  316.       public function get animationSpeed() : Number
  317.       {
  318.          return this._animationSpeed;
  319.       }
  320.       
  321.       public function layout(param1:Boolean = false) : void
  322.       {
  323.          this._layoutInvalid = false;
  324.          this.startTimer();
  325.          if(param1)
  326.          {
  327.             this.updateLayout();
  328.          }
  329.       }
  330.       
  331.       private function stopTimer() : void
  332.       {
  333.          if(this._timer.running)
  334.          {
  335.             --running;
  336.             this._timer.stop();
  337.          }
  338.       }
  339.    }
  340. }
  341.  
  342.