home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 2010 Software/Programs / PCGuia_programas.iso / Software / Utils / Livebrush / Install-LivebrushLite.air / livebrush.swf / scripts / fl / controls / BaseButton.as next >
Encoding:
Text File  |  2009-10-26  |  6.0 KB  |  218 lines

  1. package fl.controls
  2. {
  3.    import fl.core.InvalidationType;
  4.    import fl.core.UIComponent;
  5.    import fl.events.ComponentEvent;
  6.    import flash.display.DisplayObject;
  7.    import flash.events.MouseEvent;
  8.    import flash.events.TimerEvent;
  9.    import flash.utils.Timer;
  10.    
  11.    public class BaseButton extends UIComponent
  12.    {
  13.       private static var defaultStyles:Object = {
  14.          "upSkin":"Button_upSkin",
  15.          "downSkin":"Button_downSkin",
  16.          "overSkin":"Button_overSkin",
  17.          "disabledSkin":"Button_disabledSkin",
  18.          "selectedDisabledSkin":"Button_selectedDisabledSkin",
  19.          "selectedUpSkin":"Button_selectedUpSkin",
  20.          "selectedDownSkin":"Button_selectedDownSkin",
  21.          "selectedOverSkin":"Button_selectedOverSkin",
  22.          "focusRectSkin":null,
  23.          "focusRectPadding":null,
  24.          "repeatDelay":500,
  25.          "repeatInterval":35
  26.       };
  27.       
  28.       protected var pressTimer:Timer;
  29.       
  30.       protected var _autoRepeat:Boolean = false;
  31.       
  32.       protected var _selected:Boolean = false;
  33.       
  34.       protected var background:DisplayObject;
  35.       
  36.       private var unlockedMouseState:String;
  37.       
  38.       protected var mouseState:String;
  39.       
  40.       private var _mouseStateLocked:Boolean = false;
  41.       
  42.       public function BaseButton()
  43.       {
  44.          super();
  45.          buttonMode = true;
  46.          mouseChildren = false;
  47.          useHandCursor = false;
  48.          setupMouseEvents();
  49.          setMouseState("up");
  50.          pressTimer = new Timer(1,0);
  51.          pressTimer.addEventListener(TimerEvent.TIMER,buttonDown,false,0,true);
  52.       }
  53.       
  54.       public static function getStyleDefinition() : Object
  55.       {
  56.          return defaultStyles;
  57.       }
  58.       
  59.       override public function get enabled() : Boolean
  60.       {
  61.          return super.enabled;
  62.       }
  63.       
  64.       protected function startPress() : void
  65.       {
  66.          if(_autoRepeat)
  67.          {
  68.             pressTimer.delay = Number(getStyleValue("repeatDelay"));
  69.             pressTimer.start();
  70.          }
  71.          dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN,true));
  72.       }
  73.       
  74.       override protected function draw() : void
  75.       {
  76.          if(isInvalid(InvalidationType.STYLES,InvalidationType.STATE))
  77.          {
  78.             drawBackground();
  79.             invalidate(InvalidationType.SIZE,false);
  80.          }
  81.          if(isInvalid(InvalidationType.SIZE))
  82.          {
  83.             drawLayout();
  84.          }
  85.          super.draw();
  86.       }
  87.       
  88.       protected function drawLayout() : void
  89.       {
  90.          background.width = width;
  91.          background.height = height;
  92.       }
  93.       
  94.       override public function set enabled(param1:Boolean) : void
  95.       {
  96.          super.enabled = param1;
  97.          mouseEnabled = param1;
  98.       }
  99.       
  100.       public function set autoRepeat(param1:Boolean) : void
  101.       {
  102.          _autoRepeat = param1;
  103.       }
  104.       
  105.       protected function mouseEventHandler(param1:MouseEvent) : void
  106.       {
  107.          if(param1.type == MouseEvent.MOUSE_DOWN)
  108.          {
  109.             setMouseState("down");
  110.             startPress();
  111.          }
  112.          else if(param1.type == MouseEvent.ROLL_OVER || param1.type == MouseEvent.MOUSE_UP)
  113.          {
  114.             setMouseState("over");
  115.             endPress();
  116.          }
  117.          else if(param1.type == MouseEvent.ROLL_OUT)
  118.          {
  119.             setMouseState("up");
  120.             endPress();
  121.          }
  122.       }
  123.       
  124.       protected function drawBackground() : void
  125.       {
  126.          var _loc1_:* = enabled ? mouseState : "disabled";
  127.          if(selected)
  128.          {
  129.             _loc1_ = "selected" + _loc1_.substr(0,1).toUpperCase() + _loc1_.substr(1);
  130.          }
  131.          _loc1_ += "Skin";
  132.          var _loc2_:DisplayObject = background;
  133.          background = getDisplayObjectInstance(getStyleValue(_loc1_));
  134.          addChildAt(background,0);
  135.          if(_loc2_ != null && _loc2_ != background)
  136.          {
  137.             removeChild(_loc2_);
  138.          }
  139.       }
  140.       
  141.       public function get selected() : Boolean
  142.       {
  143.          return _selected;
  144.       }
  145.       
  146.       protected function setupMouseEvents() : void
  147.       {
  148.          addEventListener(MouseEvent.ROLL_OVER,mouseEventHandler,false,0,true);
  149.          addEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler,false,0,true);
  150.          addEventListener(MouseEvent.MOUSE_UP,mouseEventHandler,false,0,true);
  151.          addEventListener(MouseEvent.ROLL_OUT,mouseEventHandler,false,0,true);
  152.       }
  153.       
  154.       protected function endPress() : void
  155.       {
  156.          pressTimer.reset();
  157.       }
  158.       
  159.       public function set mouseStateLocked(param1:Boolean) : void
  160.       {
  161.          _mouseStateLocked = param1;
  162.          if(param1 == false)
  163.          {
  164.             setMouseState(unlockedMouseState);
  165.          }
  166.          else
  167.          {
  168.             unlockedMouseState = mouseState;
  169.          }
  170.       }
  171.       
  172.       public function get autoRepeat() : Boolean
  173.       {
  174.          return _autoRepeat;
  175.       }
  176.       
  177.       public function set selected(param1:Boolean) : void
  178.       {
  179.          if(_selected == param1)
  180.          {
  181.             return;
  182.          }
  183.          _selected = param1;
  184.          invalidate(InvalidationType.STATE);
  185.       }
  186.       
  187.       protected function buttonDown(param1:TimerEvent) : void
  188.       {
  189.          if(!_autoRepeat)
  190.          {
  191.             endPress();
  192.             return;
  193.          }
  194.          if(pressTimer.currentCount == 1)
  195.          {
  196.             pressTimer.delay = Number(getStyleValue("repeatInterval"));
  197.          }
  198.          dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN,true));
  199.       }
  200.       
  201.       public function setMouseState(param1:String) : void
  202.       {
  203.          if(_mouseStateLocked)
  204.          {
  205.             unlockedMouseState = param1;
  206.             return;
  207.          }
  208.          if(mouseState == param1)
  209.          {
  210.             return;
  211.          }
  212.          mouseState = param1;
  213.          invalidate(InvalidationType.STATE);
  214.       }
  215.    }
  216. }
  217.  
  218.