topical media & game development

talk show tell print

actionscript-video-com-flashconnections-ui-component-mediaPlayer-MediaPlayerButton.ax

actionscript-video-com-flashconnections-ui-component-mediaPlayer-MediaPlayerButton.ax [swf] flex


  package com.flashconnections.ui.component.mediaPlayer
  {
          import flash.display.SimpleButton;
          import flash.display.DisplayObject;
          import flash.display.Shape;
          import flash.display.Sprite;
          import flash.text.TextFormat;
          import flash.text.TextFormatAlign;
          import flash.text.TextField;
          import flash.filters.DropShadowFilter;
  
          public class @ax-actionscript-video-com-flashconnections-ui-component-mediaPlayer-MediaPlayerButton extends SimpleButton
          {
                  private static var BUTTON_WIDTH:uint = 100;
                  private static var BUTTON_HEIGHT:uint = 30;
                  
                  private var _label:String;
                  public function get Label ():String {
                          return _label;
                  }
                  public function set Label (val:String):void {
                          _label = val;
                          upState = createState(0xAAAAAA,false);
                          overState = createState(0xCCCCCC,false);
                          downState = createState(0x666666,true);
                          hitTestState = createState(0x000000,false);
                  }
                  public function @ax-actionscript-video-com-flashconnections-ui-component-mediaPlayer-MediaPlayerButton(label:String)        {
                          Label = label;
                  }
                  private function createState(color:uint,isDownState:Boolean):Sprite {
                          var result:Sprite = new Sprite();
                          var background:Shape = createButtonBg(color);
                          background.filters = [new DropShadowFilter(4,45,0,.5)];
                          result.addChild(background);
                          var textField:TextField = createTextField(isDownState);
                          result.addChild(textField);
                          return result;
                  }
                  // Create a rounded rectangle with a specific fill color
                  private function createButtonBg(color:uint):Shape {
                          var result:Shape = new Shape();
                          result.graphics.lineStyle(2,0x000000);
                          result.graphics.beginFill(color);
                          result.graphics.drawRoundRect(0,0,BUTTON_WIDTH,BUTTON_HEIGHT,25);
                          result.graphics.endFill();
                          return result;
                  }
                  //Create the text field to display the text of the button
                  private function createTextField(isDownState:Boolean):TextField {
                          var result:TextField = new TextField();
                          result.text = _label;
                          result.width = BUTTON_WIDTH;
                          result.height = BUTTON_HEIGHT;
                          // Center the text horizontally
                          var format:TextFormat = new TextFormat();
                          format.align = TextFormatAlign.CENTER
                          format.color = 0xFFFFFF;
                          format.font = 'Arial';
                          format.bold = true;
                          result.setTextFormat(format);
                          // Center the text vertically
                          result.y = ((BUTTON_HEIGHT-result.textHeight)/2)-2;
                          // The down state places the text down and to the right
                          // further than the other states
                          if (isDownState)  {
                                  result.x += 1;
                                  result.y += 1;
                          }
                          return result;
                  }
          }
  }


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.