1. package punk.transition.effects
  2. {
  3.     import flash.display.BlendMode;
  4.     import flash.events.IMEEvent;
  5.     import flash.utils.getTimer;
  6.     import net.flashpunk.FP;
  7.     import net.flashpunk.Graphic;
  8.     import net.flashpunk.graphics.Graphiclist;
  9.     import net.flashpunk.graphics.Image;
  10.     import net.flashpunk.utils.Ease;
  11.     /**
  12.      * ...
  13.      * @author azrafe7
  14.      */
  15.     public class StripeFade extends Effect
  16.     {
  17.         protected var _startTime:int;
  18.        
  19.         protected var _fadeImages:Vector.<Image> = new Vector.<Image>;
  20.  
  21.         // main options
  22.         protected var _fadeIn:Boolean;
  23.         protected var _fadeFrom:int;
  24.        
  25.         // extra options (with default values)
  26.         protected var _duration:Number = 2;
  27.         protected var _ease:Function = linear;
  28.         protected var _numStripes:int = 18;
  29.         protected var _color:int = 0xFF000000;
  30.         protected var _stripeEase:Function = Ease.sineIn;
  31.         protected var _stripeDuration:Number = .8;
  32.  
  33.         // starting side constants
  34.         public static const LEFT:int = 0;
  35.         public static const TOP:int = 1;
  36.         public static const RIGHT:int = 2;
  37.         public static const BOTTOM:int = 3;
  38.        
  39.         // linear ease function
  40.         public static function linear(t:Number):Number { return t; };
  41.        
  42.        
  43.         /**
  44.          * StripeFade effect constructor.
  45.          * @param   fadeIn      If true the stripes will fade In. DefaultS to false.
  46.          * @param   fadeFrom    On which side the fade will start. Possible values are StripeFade.LEFT, StripeFade.TOP, StripeFade.RIGHT, StripeFade.BOTTOM. Defaults to LEFT.
  47.          * @param   options     An object containing key/value pairs of the following optional parameters:
  48.          *                      duration        Optional number indicating the time (in seconds) the effect will last (approximately). Defaults to 2.
  49.          *                      ease            Optional easer function. Defaults to linear.
  50.          *                      numStripes      Optional number of stripes composing the effect. Defaults to 18.
  51.          *                      color           Optional color of stripes. Defaults to black.
  52.          *                      stripeDuration  Optional number indicating the time (in seconds) each stripe will last (approximately). Defaults to 0.8.
  53.          *                      stripeEase      Optional easer function for the single stripes. Defaults to Ease.sineIn.
  54.          *
  55.          * Example: new StripeFade(true, StripeFade.BOTTOM, { ease:Ease.bounceIn, numStripes:32, stripeDuration:1.5, color:0xFF3366});
  56.          */
  57.         public function StripeFade(fadeIn:Boolean=false, fadeFrom:int=LEFT, options:Object=null)
  58.         {
  59.             super();
  60.            
  61.             if (options) {
  62.                 if (options.hasOwnProperty("duration")) _duration = options.duration;
  63.                 if (options.hasOwnProperty("ease")) _ease = options.ease || linear;
  64.                 if (options.hasOwnProperty("numStripes")) _numStripes = options.numStripes;
  65.                 if (options.hasOwnProperty("color")) _color = options.color;
  66.                 if (options.hasOwnProperty("stripeDuration")) _stripeDuration = options.stripeDuration;
  67.                 if (options.hasOwnProperty("stripeEase")) _stripeEase = options.stripeEase || linear;
  68.             }
  69.            
  70.             _fadeIn = fadeIn;
  71.             _fadeFrom = fadeFrom;
  72.             _duration -= _stripeDuration;
  73.            
  74.             var vertStripes:Boolean = (_fadeFrom % 2 == 0);
  75.             var stripeW:Number = vertStripes ? Math.ceil(FP.width/_numStripes) : FP.screen.width;
  76.             var stripeH:Number = vertStripes ? FP.height : Math.ceil(FP.height/_numStripes);
  77.            
  78.             for (var i:int = 0; i < _numStripes; i++) {
  79.                 var img:Image = Image.createRect(stripeW, stripeH, _color);
  80.                
  81.                 img.alpha = fadeIn ? 1 : 0;
  82.                 img.x = vertStripes ? i * stripeW : 0;
  83.                 img.y = vertStripes ? 0 : i * stripeH;
  84.                 img.scrollX = img.scrollY = 0;
  85.                 addGraphic(img);
  86.             }
  87.         }
  88.        
  89.         // called once the effect gets added to the world
  90.         override public function added():void
  91.         {
  92.             super.added();
  93.            
  94.             var t:Number = 1 / _numStripes;
  95.             var stripeTime:Number = _stripeDuration;
  96.             var destAlpha:Number = _fadeIn ? 0 : 1;
  97.             var images:Vector.<Graphic> = (graphic as Graphiclist).children;
  98.            
  99.             for (var i:int = 0; i < _numStripes; i++) {
  100.                 var idx:int = _fadeFrom < 2 ? i : _numStripes - i - 1;
  101.                 var img:Image = images[idx] as Image;
  102.                 var options:Object = new Object();
  103.                 if (i > 0) {
  104.                     options.delay = _duration * _ease(i * t);
  105.                 }
  106.                 options.ease = _stripeEase;
  107.                 if (i == _numStripes - 1) options.complete = _onComplete;
  108.                 FP.tween(img, { alpha:destAlpha }, stripeTime, options);
  109.             }
  110.            
  111.             //_startTime = getTimer();         
  112.         }
  113.        
  114.         /*
  115.         public function time():Function
  116.         {
  117.             var elapsed:Number = (getTimer()-_startTime)/1000;
  118.             trace(elapsed.toFixed(2), "secs /", (_duration + _stripeDuration).toFixed(2), "secs");
  119.             return super._onComplete();
  120.         }*/
  121.        
  122.     }
  123.  
  124. }