home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / com / gskinner / utils / FramerateThrottler.as
Encoding:
Text File  |  2010-06-23  |  3.6 KB  |  126 lines

  1. package com.gskinner.utils
  2. {
  3.    import flash.desktop.NativeApplication;
  4.    import flash.events.Event;
  5.    import flash.system.Capabilities;
  6.    
  7.    public class FramerateThrottler
  8.    {
  9.       protected static var _backgroundFramerate:Number;
  10.       
  11.       protected static var _activeFramerate:Number;
  12.       
  13.       protected static var mac:Boolean;
  14.       
  15.       public static var onlyThrottleOnMac:Boolean = false;
  16.       
  17.       protected static var _active:Boolean = false;
  18.       
  19.       protected static var _enabled:Boolean = true;
  20.       
  21.       public function FramerateThrottler()
  22.       {
  23.          super();
  24.       }
  25.       
  26.       public static function get enabled() : Boolean
  27.       {
  28.          return _enabled;
  29.       }
  30.       
  31.       protected static function throttleFramerate() : void
  32.       {
  33.          if(onlyThrottleOnMac && !mac)
  34.          {
  35.             return;
  36.          }
  37.          var _loc1_:NativeApplication = NativeApplication.nativeApplication;
  38.          if(_loc1_.openedWindows.length > 0)
  39.          {
  40.             _activeFramerate = _loc1_.openedWindows[0].stage.frameRate;
  41.             _loc1_.openedWindows[0].stage.frameRate = _backgroundFramerate;
  42.          }
  43.       }
  44.       
  45.       public static function get active() : Boolean
  46.       {
  47.          return _active;
  48.       }
  49.       
  50.       public static function set enabled(param1:Boolean) : void
  51.       {
  52.          if(param1 == _enabled)
  53.          {
  54.             return;
  55.          }
  56.          _enabled = param1;
  57.          if(!_active && !_enabled)
  58.          {
  59.             restoreFramerate();
  60.          }
  61.          else if(!_active && _enabled)
  62.          {
  63.             throttleFramerate();
  64.          }
  65.       }
  66.       
  67.       public static function initialize(param1:Number = 1, param2:Number = NaN) : void
  68.       {
  69.          var _loc3_:NativeApplication = NativeApplication.nativeApplication;
  70.          if(!isNaN(param2) && param2 > 0)
  71.          {
  72.             _activeFramerate = param2;
  73.          }
  74.          else if(_loc3_.openedWindows.length > 0)
  75.          {
  76.             _activeFramerate = _loc3_.openedWindows[0].stage.frameRate;
  77.          }
  78.          else
  79.          {
  80.             _activeFramerate = 20;
  81.          }
  82.          _backgroundFramerate = param1;
  83.          mac = Capabilities.version.split(" ")[0].toUpperCase() == "MAC";
  84.          _loc3_.addEventListener(Event.DEACTIVATE,handleDeactivate);
  85.       }
  86.       
  87.       protected static function handleActivate(param1:Event) : void
  88.       {
  89.          _active = true;
  90.          if(_enabled)
  91.          {
  92.             restoreFramerate();
  93.          }
  94.          var _loc2_:NativeApplication = NativeApplication.nativeApplication;
  95.          _loc2_.removeEventListener(Event.ACTIVATE,handleActivate);
  96.          _loc2_.addEventListener(Event.DEACTIVATE,handleDeactivate);
  97.       }
  98.       
  99.       protected static function handleDeactivate(param1:Event) : void
  100.       {
  101.          _active = false;
  102.          if(_enabled)
  103.          {
  104.             throttleFramerate();
  105.          }
  106.          var _loc2_:NativeApplication = NativeApplication.nativeApplication;
  107.          _loc2_.removeEventListener(Event.DEACTIVATE,handleDeactivate);
  108.          _loc2_.addEventListener(Event.ACTIVATE,handleActivate);
  109.       }
  110.       
  111.       protected static function restoreFramerate() : void
  112.       {
  113.          if(onlyThrottleOnMac && !mac)
  114.          {
  115.             return;
  116.          }
  117.          var _loc1_:NativeApplication = NativeApplication.nativeApplication;
  118.          if(_loc1_.openedWindows.length > 0)
  119.          {
  120.             _loc1_.openedWindows[0].stage.frameRate = _activeFramerate;
  121.          }
  122.       }
  123.    }
  124. }
  125.  
  126.