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 / LabelButton.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  9.6 KB  |  320 lines

  1. package fl.controls
  2. {
  3.    import fl.core.InvalidationType;
  4.    import fl.core.UIComponent;
  5.    import fl.events.ComponentEvent;
  6.    import fl.managers.IFocusManagerComponent;
  7.    import flash.display.DisplayObject;
  8.    import flash.events.Event;
  9.    import flash.events.KeyboardEvent;
  10.    import flash.events.MouseEvent;
  11.    import flash.text.TextField;
  12.    import flash.text.TextFieldType;
  13.    import flash.text.TextFormat;
  14.    import flash.ui.Keyboard;
  15.    
  16.    public class LabelButton extends BaseButton implements IFocusManagerComponent
  17.    {
  18.       public static var createAccessibilityImplementation:Function;
  19.       
  20.       private static var defaultStyles:Object = {
  21.          "icon":null,
  22.          "upIcon":null,
  23.          "downIcon":null,
  24.          "overIcon":null,
  25.          "disabledIcon":null,
  26.          "selectedDisabledIcon":null,
  27.          "selectedUpIcon":null,
  28.          "selectedDownIcon":null,
  29.          "selectedOverIcon":null,
  30.          "textFormat":null,
  31.          "disabledTextFormat":null,
  32.          "textPadding":5,
  33.          "embedFonts":false
  34.       };
  35.       
  36.       protected var _toggle:Boolean = false;
  37.       
  38.       public var textField:TextField;
  39.       
  40.       protected var mode:String = "center";
  41.       
  42.       protected var _labelPlacement:String = "right";
  43.       
  44.       protected var oldMouseState:String;
  45.       
  46.       protected var _label:String = "Label";
  47.       
  48.       protected var icon:DisplayObject;
  49.       
  50.       public function LabelButton()
  51.       {
  52.          super();
  53.       }
  54.       
  55.       public static function getStyleDefinition() : Object
  56.       {
  57.          return mergeStyles(defaultStyles,BaseButton.getStyleDefinition());
  58.       }
  59.       
  60.       override protected function draw() : void
  61.       {
  62.          if(textField.text != _label)
  63.          {
  64.             label = _label;
  65.          }
  66.          if(isInvalid(InvalidationType.STYLES,InvalidationType.STATE))
  67.          {
  68.             drawBackground();
  69.             drawIcon();
  70.             drawTextFormat();
  71.             invalidate(InvalidationType.SIZE,false);
  72.          }
  73.          if(isInvalid(InvalidationType.SIZE))
  74.          {
  75.             drawLayout();
  76.          }
  77.          if(isInvalid(InvalidationType.SIZE,InvalidationType.STYLES))
  78.          {
  79.             if(isFocused && focusManager.showFocusIndicator)
  80.             {
  81.                drawFocus(true);
  82.             }
  83.          }
  84.          validate();
  85.       }
  86.       
  87.       override protected function drawLayout() : void
  88.       {
  89.          var _loc7_:Number = NaN;
  90.          var _loc8_:Number = NaN;
  91.          var _loc1_:Number = Number(getStyleValue("textPadding"));
  92.          var _loc2_:String = icon == null && mode == "center" ? ButtonLabelPlacement.TOP : _labelPlacement;
  93.          textField.height = textField.textHeight + 4;
  94.          var _loc3_:Number = textField.textWidth + 4;
  95.          var _loc4_:Number = textField.textHeight + 4;
  96.          var _loc5_:Number = icon == null ? 0 : icon.width + _loc1_;
  97.          var _loc6_:Number = icon == null ? 0 : icon.height + _loc1_;
  98.          textField.visible = label.length > 0;
  99.          if(icon != null)
  100.          {
  101.             icon.x = Math.round((width - icon.width) / 2);
  102.             icon.y = Math.round((height - icon.height) / 2);
  103.          }
  104.          if(textField.visible == false)
  105.          {
  106.             textField.width = 0;
  107.             textField.height = 0;
  108.          }
  109.          else if(_loc2_ == ButtonLabelPlacement.BOTTOM || _loc2_ == ButtonLabelPlacement.TOP)
  110.          {
  111.             _loc7_ = Math.max(0,Math.min(_loc3_,width - 2 * _loc1_));
  112.             if(height - 2 > _loc4_)
  113.             {
  114.                _loc8_ = _loc4_;
  115.             }
  116.             else
  117.             {
  118.                _loc8_ = height - 2;
  119.             }
  120.             textField.width = _loc3_ = _loc7_;
  121.             textField.height = _loc4_ = _loc8_;
  122.             textField.x = Math.round((width - _loc3_) / 2);
  123.             textField.y = Math.round((height - textField.height - _loc6_) / 2 + (_loc2_ == ButtonLabelPlacement.BOTTOM ? _loc6_ : 0));
  124.             if(icon != null)
  125.             {
  126.                icon.y = Math.round(_loc2_ == ButtonLabelPlacement.BOTTOM ? textField.y - _loc6_ : textField.y + textField.height + _loc1_);
  127.             }
  128.          }
  129.          else
  130.          {
  131.             _loc7_ = Math.max(0,Math.min(_loc3_,width - _loc5_ - 2 * _loc1_));
  132.             textField.width = _loc3_ = _loc7_;
  133.             textField.x = Math.round((width - _loc3_ - _loc5_) / 2 + (_loc2_ != ButtonLabelPlacement.LEFT ? _loc5_ : 0));
  134.             textField.y = Math.round((height - textField.height) / 2);
  135.             if(icon != null)
  136.             {
  137.                icon.x = Math.round(_loc2_ != ButtonLabelPlacement.LEFT ? textField.x - _loc5_ : textField.x + _loc3_ + _loc1_);
  138.             }
  139.          }
  140.          super.drawLayout();
  141.       }
  142.       
  143.       protected function toggleSelected(param1:MouseEvent) : void
  144.       {
  145.          selected = !selected;
  146.          dispatchEvent(new Event(Event.CHANGE,true));
  147.       }
  148.       
  149.       override protected function keyUpHandler(param1:KeyboardEvent) : void
  150.       {
  151.          if(!enabled)
  152.          {
  153.             return;
  154.          }
  155.          if(param1.keyCode == Keyboard.SPACE)
  156.          {
  157.             setMouseState(oldMouseState);
  158.             oldMouseState = null;
  159.             endPress();
  160.             dispatchEvent(new MouseEvent(MouseEvent.CLICK));
  161.          }
  162.       }
  163.       
  164.       public function get labelPlacement() : String
  165.       {
  166.          return _labelPlacement;
  167.       }
  168.       
  169.       public function get toggle() : Boolean
  170.       {
  171.          return _toggle;
  172.       }
  173.       
  174.       protected function setEmbedFont() : *
  175.       {
  176.          var _loc1_:Object = getStyleValue("embedFonts");
  177.          if(_loc1_ != null)
  178.          {
  179.             textField.embedFonts = _loc1_;
  180.          }
  181.       }
  182.       
  183.       override public function get selected() : Boolean
  184.       {
  185.          return _toggle ? _selected : false;
  186.       }
  187.       
  188.       override protected function configUI() : void
  189.       {
  190.          super.configUI();
  191.          textField = new TextField();
  192.          textField.type = TextFieldType.DYNAMIC;
  193.          textField.selectable = false;
  194.          addChild(textField);
  195.       }
  196.       
  197.       override protected function initializeAccessibility() : void
  198.       {
  199.          if(LabelButton.createAccessibilityImplementation != null)
  200.          {
  201.             LabelButton.createAccessibilityImplementation(this);
  202.          }
  203.       }
  204.       
  205.       public function set labelPlacement(param1:String) : void
  206.       {
  207.          _labelPlacement = param1;
  208.          invalidate(InvalidationType.SIZE);
  209.       }
  210.       
  211.       protected function drawIcon() : void
  212.       {
  213.          var _loc1_:DisplayObject = icon;
  214.          var _loc2_:* = enabled ? mouseState : "disabled";
  215.          if(selected)
  216.          {
  217.             _loc2_ = "selected" + _loc2_.substr(0,1).toUpperCase() + _loc2_.substr(1);
  218.          }
  219.          _loc2_ += "Icon";
  220.          var _loc3_:Object = getStyleValue(_loc2_);
  221.          if(_loc3_ == null)
  222.          {
  223.             _loc3_ = getStyleValue("icon");
  224.          }
  225.          if(_loc3_ != null)
  226.          {
  227.             icon = getDisplayObjectInstance(_loc3_);
  228.          }
  229.          if(icon != null)
  230.          {
  231.             addChildAt(icon,1);
  232.          }
  233.          if(_loc1_ != null && _loc1_ != icon)
  234.          {
  235.             removeChild(_loc1_);
  236.          }
  237.       }
  238.       
  239.       public function set label(param1:String) : void
  240.       {
  241.          _label = param1;
  242.          if(textField.text != _label)
  243.          {
  244.             textField.text = _label;
  245.             dispatchEvent(new ComponentEvent(ComponentEvent.LABEL_CHANGE));
  246.          }
  247.          invalidate(InvalidationType.SIZE);
  248.          invalidate(InvalidationType.STYLES);
  249.       }
  250.       
  251.       override protected function keyDownHandler(param1:KeyboardEvent) : void
  252.       {
  253.          if(!enabled)
  254.          {
  255.             return;
  256.          }
  257.          if(param1.keyCode == Keyboard.SPACE)
  258.          {
  259.             if(oldMouseState == null)
  260.             {
  261.                oldMouseState = mouseState;
  262.             }
  263.             setMouseState("down");
  264.             startPress();
  265.          }
  266.       }
  267.       
  268.       public function set toggle(param1:Boolean) : void
  269.       {
  270.          if(!param1 && super.selected)
  271.          {
  272.             selected = false;
  273.          }
  274.          _toggle = param1;
  275.          if(_toggle)
  276.          {
  277.             addEventListener(MouseEvent.CLICK,toggleSelected,false,0,true);
  278.          }
  279.          else
  280.          {
  281.             removeEventListener(MouseEvent.CLICK,toggleSelected);
  282.          }
  283.          invalidate(InvalidationType.STATE);
  284.       }
  285.       
  286.       override public function set selected(param1:Boolean) : void
  287.       {
  288.          _selected = param1;
  289.          if(_toggle)
  290.          {
  291.             invalidate(InvalidationType.STATE);
  292.          }
  293.       }
  294.       
  295.       protected function drawTextFormat() : void
  296.       {
  297.          var _loc1_:Object = UIComponent.getStyleDefinition();
  298.          var _loc2_:TextFormat = enabled ? _loc1_.defaultTextFormat as TextFormat : _loc1_.defaultDisabledTextFormat as TextFormat;
  299.          textField.setTextFormat(_loc2_);
  300.          var _loc3_:TextFormat = getStyleValue(enabled ? "textFormat" : "disabledTextFormat") as TextFormat;
  301.          if(_loc3_ != null)
  302.          {
  303.             textField.setTextFormat(_loc3_);
  304.          }
  305.          else
  306.          {
  307.             _loc3_ = _loc2_;
  308.          }
  309.          textField.defaultTextFormat = _loc3_;
  310.          setEmbedFont();
  311.       }
  312.       
  313.       public function get label() : String
  314.       {
  315.          return _label;
  316.       }
  317.    }
  318. }
  319.  
  320.