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 / Slider.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  12.5 KB  |  407 lines

  1. package fl.controls
  2. {
  3.    import fl.core.InvalidationType;
  4.    import fl.core.UIComponent;
  5.    import fl.events.InteractionInputType;
  6.    import fl.events.SliderEvent;
  7.    import fl.events.SliderEventClickTarget;
  8.    import fl.managers.IFocusManagerComponent;
  9.    import flash.display.DisplayObject;
  10.    import flash.display.Sprite;
  11.    import flash.events.KeyboardEvent;
  12.    import flash.events.MouseEvent;
  13.    import flash.ui.Keyboard;
  14.    
  15.    [Embed(source="/_assets/assets.swf", symbol="symbol399")]
  16.    public class Slider extends UIComponent implements IFocusManagerComponent
  17.    {
  18.       protected static var defaultStyles:Object = {
  19.          "thumbUpSkin":"SliderThumb_upSkin",
  20.          "thumbOverSkin":"SliderThumb_overSkin",
  21.          "thumbDownSkin":"SliderThumb_downSkin",
  22.          "thumbDisabledSkin":"SliderThumb_disabledSkin",
  23.          "sliderTrackSkin":"SliderTrack_skin",
  24.          "sliderTrackDisabledSkin":"SliderTrack_disabledSkin",
  25.          "tickSkin":"SliderTick_skin",
  26.          "focusRectSkin":null,
  27.          "focusRectPadding":null
  28.       };
  29.       
  30.       protected static const TRACK_STYLES:Object = {
  31.          "upSkin":"sliderTrackSkin",
  32.          "overSkin":"sliderTrackSkin",
  33.          "downSkin":"sliderTrackSkin",
  34.          "disabledSkin":"sliderTrackDisabledSkin"
  35.       };
  36.       
  37.       protected static const THUMB_STYLES:Object = {
  38.          "upSkin":"thumbUpSkin",
  39.          "overSkin":"thumbOverSkin",
  40.          "downSkin":"thumbDownSkin",
  41.          "disabledSkin":"thumbDisabledSkin"
  42.       };
  43.       
  44.       protected static const TICK_STYLES:Object = {"upSkin":"tickSkin"};
  45.       
  46.       protected var _direction:String;
  47.       
  48.       protected var _snapInterval:Number = 0;
  49.       
  50.       protected var _liveDragging:Boolean = false;
  51.       
  52.       protected var track:BaseButton;
  53.       
  54.       protected var _minimum:Number = 0;
  55.       
  56.       protected var thumb:BaseButton;
  57.       
  58.       protected var _maximum:Number = 10;
  59.       
  60.       protected var _tickInterval:Number = 0;
  61.       
  62.       protected var tickContainer:Sprite;
  63.       
  64.       protected var _value:Number = 0;
  65.       
  66.       public function Slider()
  67.       {
  68.          _direction = SliderDirection.HORIZONTAL;
  69.          super();
  70.          setStyles();
  71.       }
  72.       
  73.       public static function getStyleDefinition() : Object
  74.       {
  75.          return defaultStyles;
  76.       }
  77.       
  78.       public function get tickInterval() : Number
  79.       {
  80.          return _tickInterval;
  81.       }
  82.       
  83.       override public function setSize(param1:Number, param2:Number) : void
  84.       {
  85.          if(_direction == SliderDirection.VERTICAL && !isLivePreview)
  86.          {
  87.             super.setSize(param2,param1);
  88.          }
  89.          else
  90.          {
  91.             super.setSize(param1,param2);
  92.          }
  93.          invalidate(InvalidationType.SIZE);
  94.       }
  95.       
  96.       public function set tickInterval(param1:Number) : void
  97.       {
  98.          _tickInterval = param1;
  99.          invalidate(InvalidationType.SIZE);
  100.       }
  101.       
  102.       override public function set enabled(param1:Boolean) : void
  103.       {
  104.          if(enabled == param1)
  105.          {
  106.             return;
  107.          }
  108.          super.enabled = param1;
  109.          track.enabled = thumb.enabled = param1;
  110.       }
  111.       
  112.       protected function drawTicks() : void
  113.       {
  114.          var _loc5_:DisplayObject = null;
  115.          clearTicks();
  116.          tickContainer = new Sprite();
  117.          var _loc1_:Number = maximum < 1 ? tickInterval / 100 : tickInterval;
  118.          var _loc2_:Number = (maximum - minimum) / _loc1_;
  119.          var _loc3_:Number = _width / _loc2_;
  120.          var _loc4_:uint = 0;
  121.          while(_loc4_ <= _loc2_)
  122.          {
  123.             _loc5_ = getDisplayObjectInstance(getStyleValue("tickSkin"));
  124.             _loc5_.x = _loc3_ * _loc4_;
  125.             _loc5_.y = track.y - _loc5_.height - 2;
  126.             tickContainer.addChild(_loc5_);
  127.             _loc4_++;
  128.          }
  129.          addChild(tickContainer);
  130.       }
  131.       
  132.       public function get maximum() : Number
  133.       {
  134.          return _maximum;
  135.       }
  136.       
  137.       public function set minimum(param1:Number) : void
  138.       {
  139.          _minimum = param1;
  140.          this.value = Math.max(param1,this.value);
  141.          invalidate(InvalidationType.DATA);
  142.       }
  143.       
  144.       public function get minimum() : Number
  145.       {
  146.          return _minimum;
  147.       }
  148.       
  149.       protected function clearTicks() : void
  150.       {
  151.          if(!tickContainer || !tickContainer.parent)
  152.          {
  153.             return;
  154.          }
  155.          removeChild(tickContainer);
  156.       }
  157.       
  158.       protected function calculateValue(param1:Number, param2:String, param3:String, param4:int = undefined) : void
  159.       {
  160.          var _loc5_:Number = param1 / _width * (maximum - minimum);
  161.          if(_direction == SliderDirection.VERTICAL)
  162.          {
  163.             _loc5_ = maximum - _loc5_;
  164.          }
  165.          else
  166.          {
  167.             _loc5_ = minimum + _loc5_;
  168.          }
  169.          doSetValue(_loc5_,param2,param3,param4);
  170.       }
  171.       
  172.       protected function positionThumb() : void
  173.       {
  174.          thumb.x = (_direction == SliderDirection.VERTICAL ? maximum - value : value - minimum) / (maximum - minimum) * _width;
  175.       }
  176.       
  177.       public function get snapInterval() : Number
  178.       {
  179.          return _snapInterval;
  180.       }
  181.       
  182.       public function set liveDragging(param1:Boolean) : void
  183.       {
  184.          _liveDragging = param1;
  185.       }
  186.       
  187.       protected function thumbReleaseHandler(param1:MouseEvent) : void
  188.       {
  189.          stage.removeEventListener(MouseEvent.MOUSE_MOVE,doDrag);
  190.          stage.removeEventListener(MouseEvent.MOUSE_UP,thumbReleaseHandler);
  191.          dispatchEvent(new SliderEvent(SliderEvent.THUMB_RELEASE,value,SliderEventClickTarget.THUMB,InteractionInputType.MOUSE));
  192.          dispatchEvent(new SliderEvent(SliderEvent.CHANGE,value,SliderEventClickTarget.THUMB,InteractionInputType.MOUSE));
  193.       }
  194.       
  195.       protected function onTrackClick(param1:MouseEvent) : void
  196.       {
  197.          calculateValue(track.mouseX,InteractionInputType.MOUSE,SliderEventClickTarget.TRACK);
  198.          if(!liveDragging)
  199.          {
  200.             dispatchEvent(new SliderEvent(SliderEvent.CHANGE,value,SliderEventClickTarget.TRACK,InteractionInputType.MOUSE));
  201.          }
  202.       }
  203.       
  204.       public function set maximum(param1:Number) : void
  205.       {
  206.          _maximum = param1;
  207.          this.value = Math.min(param1,this.value);
  208.          invalidate(InvalidationType.DATA);
  209.       }
  210.       
  211.       override public function get enabled() : Boolean
  212.       {
  213.          return super.enabled;
  214.       }
  215.       
  216.       override protected function draw() : void
  217.       {
  218.          if(isInvalid(InvalidationType.STYLES))
  219.          {
  220.             setStyles();
  221.             invalidate(InvalidationType.SIZE,false);
  222.          }
  223.          if(isInvalid(InvalidationType.SIZE))
  224.          {
  225.             track.setSize(_width,track.height);
  226.             track.drawNow();
  227.             thumb.drawNow();
  228.          }
  229.          if(tickInterval > 0)
  230.          {
  231.             drawTicks();
  232.          }
  233.          else
  234.          {
  235.             clearTicks();
  236.          }
  237.          positionThumb();
  238.          super.draw();
  239.       }
  240.       
  241.       protected function getPrecision(param1:Number) : Number
  242.       {
  243.          var _loc2_:String = param1.toString();
  244.          if(_loc2_.indexOf(".") == -1)
  245.          {
  246.             return 0;
  247.          }
  248.          return _loc2_.split(".").pop().length;
  249.       }
  250.       
  251.       protected function doSetValue(param1:Number, param2:String = null, param3:String = null, param4:int = undefined) : void
  252.       {
  253.          var _loc6_:Number = NaN;
  254.          var _loc7_:Number = NaN;
  255.          var _loc8_:Number = NaN;
  256.          var _loc9_:Number = NaN;
  257.          var _loc5_:Number = _value;
  258.          if(_snapInterval != 0 && _snapInterval != 1)
  259.          {
  260.             _loc6_ = Math.pow(10,getPrecision(snapInterval));
  261.             _loc7_ = _snapInterval * _loc6_;
  262.             _loc8_ = Math.round(param1 * _loc6_);
  263.             _loc9_ = Math.round(_loc8_ / _loc7_) * _loc7_;
  264.             param1 = _loc9_ / _loc6_;
  265.             _value = Math.max(minimum,Math.min(maximum,param1));
  266.          }
  267.          else
  268.          {
  269.             _value = Math.max(minimum,Math.min(maximum,Math.round(param1)));
  270.          }
  271.          if(_loc5_ != _value && (liveDragging && param3 != null || param2 == InteractionInputType.KEYBOARD))
  272.          {
  273.             dispatchEvent(new SliderEvent(SliderEvent.CHANGE,value,param3,param2,param4));
  274.          }
  275.          positionThumb();
  276.       }
  277.       
  278.       public function get liveDragging() : Boolean
  279.       {
  280.          return _liveDragging;
  281.       }
  282.       
  283.       override protected function configUI() : void
  284.       {
  285.          super.configUI();
  286.          thumb = new BaseButton();
  287.          thumb.setSize(13,13);
  288.          thumb.autoRepeat = false;
  289.          addChild(thumb);
  290.          thumb.addEventListener(MouseEvent.MOUSE_DOWN,thumbPressHandler,false,0,true);
  291.          track = new BaseButton();
  292.          track.move(0,0);
  293.          track.setSize(80,4);
  294.          track.autoRepeat = false;
  295.          track.useHandCursor = false;
  296.          track.addEventListener(MouseEvent.CLICK,onTrackClick,false,0,true);
  297.          addChildAt(track,0);
  298.       }
  299.       
  300.       public function set snapInterval(param1:Number) : void
  301.       {
  302.          _snapInterval = param1;
  303.       }
  304.       
  305.       protected function doDrag(param1:MouseEvent) : void
  306.       {
  307.          var _loc2_:Number = _width / snapInterval;
  308.          var _loc3_:Number = track.mouseX;
  309.          calculateValue(_loc3_,InteractionInputType.MOUSE,SliderEventClickTarget.THUMB);
  310.          dispatchEvent(new SliderEvent(SliderEvent.THUMB_DRAG,value,SliderEventClickTarget.THUMB,InteractionInputType.MOUSE));
  311.       }
  312.       
  313.       public function set value(param1:Number) : void
  314.       {
  315.          doSetValue(param1);
  316.       }
  317.       
  318.       override protected function keyDownHandler(param1:KeyboardEvent) : void
  319.       {
  320.          var _loc3_:Number = NaN;
  321.          if(!enabled)
  322.          {
  323.             return;
  324.          }
  325.          var _loc2_:Number = snapInterval > 0 ? snapInterval : 1;
  326.          var _loc4_:* = direction == SliderDirection.HORIZONTAL;
  327.          if(param1.keyCode == Keyboard.DOWN && !_loc4_ || param1.keyCode == Keyboard.LEFT && _loc4_)
  328.          {
  329.             _loc3_ = value - _loc2_;
  330.          }
  331.          else if(param1.keyCode == Keyboard.UP && !_loc4_ || param1.keyCode == Keyboard.RIGHT && _loc4_)
  332.          {
  333.             _loc3_ = value + _loc2_;
  334.          }
  335.          else if(param1.keyCode == Keyboard.PAGE_DOWN && !_loc4_ || param1.keyCode == Keyboard.HOME && _loc4_)
  336.          {
  337.             _loc3_ = minimum;
  338.          }
  339.          else if(param1.keyCode == Keyboard.PAGE_UP && !_loc4_ || param1.keyCode == Keyboard.END && _loc4_)
  340.          {
  341.             _loc3_ = maximum;
  342.          }
  343.          if(!isNaN(_loc3_))
  344.          {
  345.             param1.stopPropagation();
  346.             doSetValue(_loc3_,InteractionInputType.KEYBOARD,null,param1.keyCode);
  347.          }
  348.       }
  349.       
  350.       public function get value() : Number
  351.       {
  352.          return _value;
  353.       }
  354.       
  355.       protected function setStyles() : void
  356.       {
  357.          copyStylesToChild(thumb,THUMB_STYLES);
  358.          copyStylesToChild(track,TRACK_STYLES);
  359.       }
  360.       
  361.       protected function thumbPressHandler(param1:MouseEvent) : void
  362.       {
  363.          stage.addEventListener(MouseEvent.MOUSE_MOVE,doDrag,false,0,true);
  364.          stage.addEventListener(MouseEvent.MOUSE_UP,thumbReleaseHandler,false,0,true);
  365.          dispatchEvent(new SliderEvent(SliderEvent.THUMB_PRESS,value,SliderEventClickTarget.THUMB,InteractionInputType.MOUSE));
  366.       }
  367.       
  368.       public function set direction(param1:String) : void
  369.       {
  370.          _direction = param1;
  371.          var _loc2_:* = _direction == SliderDirection.VERTICAL;
  372.          if(isLivePreview)
  373.          {
  374.             if(_loc2_)
  375.             {
  376.                setScaleY(-1);
  377.                y = track.height;
  378.             }
  379.             else
  380.             {
  381.                setScaleY(1);
  382.                y = 0;
  383.             }
  384.             positionThumb();
  385.             return;
  386.          }
  387.          if(_loc2_ && componentInspectorSetting)
  388.          {
  389.             if(rotation % 90 == 0)
  390.             {
  391.                setScaleY(-1);
  392.             }
  393.          }
  394.          if(!componentInspectorSetting)
  395.          {
  396.             rotation = _loc2_ ? 90 : 0;
  397.          }
  398.       }
  399.       
  400.       public function get direction() : String
  401.       {
  402.          return _direction;
  403.       }
  404.    }
  405. }
  406.  
  407.