| 1 | /** |
|---|
| 2 | * A button from within the dock. |
|---|
| 3 | **/ |
|---|
| 4 | package com.longtailvideo.jwplayer.view.components { |
|---|
| 5 | import com.longtailvideo.jwplayer.model.Color; |
|---|
| 6 | |
|---|
| 7 | import flash.display.*; |
|---|
| 8 | import flash.events.*; |
|---|
| 9 | import flash.geom.ColorTransform; |
|---|
| 10 | import flash.text.TextField; |
|---|
| 11 | import flash.text.TextFormat; |
|---|
| 12 | import flash.text.TextFormatAlign; |
|---|
| 13 | |
|---|
| 14 | public class DockButton extends ComponentButton { |
|---|
| 15 | /** Asset color **/ |
|---|
| 16 | protected var _assetColor:Color; |
|---|
| 17 | /** Reference to the text field **/ |
|---|
| 18 | protected var _text:TextField; |
|---|
| 19 | /** Background colorization **/ |
|---|
| 20 | private var _colorize:Boolean; |
|---|
| 21 | /** Background over state **/ |
|---|
| 22 | private var _outBackground:Sprite; |
|---|
| 23 | /** Background over state **/ |
|---|
| 24 | private var _overBackground:Sprite; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | /** Constructor **/ |
|---|
| 28 | public function DockButton():void { |
|---|
| 29 | var textFormat:TextFormat = new TextFormat(); |
|---|
| 30 | textFormat.align = TextFormatAlign.CENTER; |
|---|
| 31 | textFormat.font = "_sans"; |
|---|
| 32 | textFormat.size = 11; |
|---|
| 33 | _text = new TextField(); |
|---|
| 34 | _text.defaultTextFormat = textFormat; |
|---|
| 35 | _text.x = 0; |
|---|
| 36 | _text.y = 30; |
|---|
| 37 | _text.width = 50; |
|---|
| 38 | _text.height = 20; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /** Sets up the button **/ |
|---|
| 43 | override public function init():void { |
|---|
| 44 | if (!_overBackground && !_outBackground) { |
|---|
| 45 | setupBackground(); |
|---|
| 46 | setBackground(_outBackground); |
|---|
| 47 | } else if (_outBackground){ |
|---|
| 48 | if (_colorize && _outColor) { |
|---|
| 49 | _outBackground.transform.colorTransform = createColorTransform(_outColor); |
|---|
| 50 | } |
|---|
| 51 | setBackground(_outBackground); |
|---|
| 52 | } else if (!_background) { |
|---|
| 53 | var backgroundSprite:Sprite = new Sprite(); |
|---|
| 54 | backgroundSprite.graphics.clear(); |
|---|
| 55 | backgroundSprite.graphics.beginFill(_outColor ? _outColor.color : 0x000000, 0.55); |
|---|
| 56 | backgroundSprite.graphics.drawRect(0, 0, 50, 50); |
|---|
| 57 | backgroundSprite.graphics.endFill(); |
|---|
| 58 | setBackground(backgroundSprite); |
|---|
| 59 | } |
|---|
| 60 | super.init(); |
|---|
| 61 | _imageLayer.addChild(_text); |
|---|
| 62 | if (_assetColor) { |
|---|
| 63 | _text.textColor = _assetColor.color; |
|---|
| 64 | } else { |
|---|
| 65 | _text.textColor = 0xFFFFFF; |
|---|
| 66 | } |
|---|
| 67 | mouseChildren = false; |
|---|
| 68 | buttonMode = true; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | protected function createColorTransform (color:Color):ColorTransform { |
|---|
| 73 | var colorTransform:ColorTransform = new ColorTransform(); |
|---|
| 74 | if (color) |
|---|
| 75 | colorTransform.color = color.color; |
|---|
| 76 | return colorTransform; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | /** Draws the dock icon background **/ |
|---|
| 81 | private function setupBackground ():void { |
|---|
| 82 | _outBackground = new Sprite(); |
|---|
| 83 | _outBackground.graphics.clear(); |
|---|
| 84 | _outBackground.graphics.beginFill(_outColor ? _outColor.color : 0x000000, 0.55); |
|---|
| 85 | _outBackground.graphics.drawRect(0, 0, 50, 50); |
|---|
| 86 | _outBackground.graphics.endFill(); |
|---|
| 87 | |
|---|
| 88 | _overBackground = new Sprite(); |
|---|
| 89 | _overBackground.graphics.clear(); |
|---|
| 90 | _overBackground.graphics.beginFill(_overColor ? _overColor.color : 0x000000, 0.55); |
|---|
| 91 | _overBackground.graphics.drawRect(0, 0, 50, 50); |
|---|
| 92 | _overBackground.graphics.endFill(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | public function centerText ():void { |
|---|
| 97 | _text.width = _background.width; |
|---|
| 98 | _text.y = _background.height / 2 + (_background.height / 2 - _text.height) / 2; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | override protected function centerIcon (icon:DisplayObject):void { |
|---|
| 103 | if (icon) { |
|---|
| 104 | if (_background) { |
|---|
| 105 | icon.x = (_background.width - icon.width) / 2; |
|---|
| 106 | icon.y = (_background.height - icon.height * 1.5) / 2; |
|---|
| 107 | } else { |
|---|
| 108 | icon.x = 0; |
|---|
| 109 | icon.y = 0; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | /** When rolling over, the background is color changed. **/ |
|---|
| 116 | override protected function outHandler (evt:MouseEvent):void { |
|---|
| 117 | if (_outBackground) { |
|---|
| 118 | setBackground(_outBackground); |
|---|
| 119 | } else if (_colorize) { |
|---|
| 120 | _background.transform.colorTransform = createColorTransform(_outColor); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | /** When rolling over, the background is color changed. **/ |
|---|
| 126 | override protected function overHandler (evt:MouseEvent):void { |
|---|
| 127 | if (_overBackground) { |
|---|
| 128 | setBackground(_overBackground); |
|---|
| 129 | } else if (_colorize) { |
|---|
| 130 | _background.transform.colorTransform = createColorTransform(_overColor); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | override protected function clickHandler (evt:MouseEvent):void { |
|---|
| 136 | super.clickHandler(evt); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | public function set assetColor (assetColor:Color):void { |
|---|
| 141 | _assetColor = assetColor; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | public function set colorize (value:Boolean):void { |
|---|
| 146 | _colorize = value; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | public function set outBackground (outBackground:Sprite):void { |
|---|
| 151 | _outBackground = outBackground; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | public function set overBackground (overBackground:Sprite):void { |
|---|
| 156 | _overBackground = overBackground; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | public function set text (text:String):void { |
|---|
| 161 | _text.text = text; |
|---|
| 162 | centerText(); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | /** Legacy support**/ |
|---|
| 167 | public function get field ():TextField { |
|---|
| 168 | return _text; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|