| 1 | package com.longtailvideo.jwplayer.view.components { |
|---|
| 2 | import com.longtailvideo.jwplayer.model.Color; |
|---|
| 3 | import com.longtailvideo.jwplayer.utils.Logger; |
|---|
| 4 | import flash.display.DisplayObject; |
|---|
| 5 | import flash.display.MovieClip; |
|---|
| 6 | import flash.display.Sprite; |
|---|
| 7 | import flash.events.MouseEvent; |
|---|
| 8 | |
|---|
| 9 | public class ComponentButton extends MovieClip { |
|---|
| 10 | protected var _background:DisplayObject; |
|---|
| 11 | protected var _clickFunction:Function; |
|---|
| 12 | protected var _clickLayer:Sprite; |
|---|
| 13 | protected var _imageLayer:Sprite; |
|---|
| 14 | protected var _outColor:Color; |
|---|
| 15 | protected var _outIcon:DisplayObject; |
|---|
| 16 | protected var _overColor:Color; |
|---|
| 17 | protected var _overIcon:DisplayObject; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | public function ComponentButton () { |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | public function init ():void { |
|---|
| 25 | if (_background) { |
|---|
| 26 | nameDisplayObject("backgroundLayer", _background); |
|---|
| 27 | addChild(_background); |
|---|
| 28 | _background.x = 0; |
|---|
| 29 | _background.y = 0; |
|---|
| 30 | } |
|---|
| 31 | _imageLayer = new Sprite(); |
|---|
| 32 | nameDisplayObject("imageLayer", _imageLayer); |
|---|
| 33 | addChild(_imageLayer); |
|---|
| 34 | _imageLayer.x = 0; |
|---|
| 35 | _imageLayer.y = 0; |
|---|
| 36 | setImage(_outIcon); |
|---|
| 37 | addEventListener(MouseEvent.MOUSE_OVER, overHandler); |
|---|
| 38 | addEventListener(MouseEvent.MOUSE_OUT, outHandler); |
|---|
| 39 | addEventListener(MouseEvent.CLICK, clickHandler); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | protected function outHandler (event:MouseEvent):void { |
|---|
| 44 | if (_overIcon) { |
|---|
| 45 | if (_imageLayer.contains(_overIcon)) { |
|---|
| 46 | _imageLayer.removeChild(_overIcon); |
|---|
| 47 | } |
|---|
| 48 | setImage(_outIcon); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | protected function overHandler (event:MouseEvent):void { |
|---|
| 54 | if (_overIcon) { |
|---|
| 55 | if (_imageLayer.contains(_outIcon)) { |
|---|
| 56 | _imageLayer.removeChild(_outIcon); |
|---|
| 57 | } |
|---|
| 58 | setImage(_overIcon); |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /** Handles mouse clicks **/ |
|---|
| 64 | protected function clickHandler (event:MouseEvent):void { |
|---|
| 65 | try { |
|---|
| 66 | _clickFunction(); |
|---|
| 67 | } catch (error:Error) { |
|---|
| 68 | Logger.log(error.message); |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * Change the image in the button. |
|---|
| 75 | * |
|---|
| 76 | * @param dpo The new caption for the button. |
|---|
| 77 | **/ |
|---|
| 78 | protected function setImage (dpo:DisplayObject):void { |
|---|
| 79 | if (dpo) { |
|---|
| 80 | if (_imageLayer.contains(dpo)) { |
|---|
| 81 | _imageLayer.removeChild(dpo); |
|---|
| 82 | } |
|---|
| 83 | _imageLayer.addChild(dpo); |
|---|
| 84 | centerIcon(dpo); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | public function setBackground (background:DisplayObject = null):void { |
|---|
| 90 | if (background) { |
|---|
| 91 | _background = background; |
|---|
| 92 | updateClickLayer(); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | public function setOutIcon (outIcon:DisplayObject = null):void { |
|---|
| 98 | if (outIcon) { |
|---|
| 99 | _outIcon = outIcon; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | public function setOverIcon (overIcon:DisplayObject = null):void { |
|---|
| 105 | if (overIcon) { |
|---|
| 106 | _overIcon = overIcon; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | public function resize (width:Number, height:Number):void { |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | protected function centerIcon (icon:DisplayObject):void { |
|---|
| 114 | if (icon) { |
|---|
| 115 | if (_background) { |
|---|
| 116 | icon.x = (_background.width - icon.width) / 2; |
|---|
| 117 | icon.y = (_background.height - icon.height) / 2; |
|---|
| 118 | } else { |
|---|
| 119 | icon.x = 0; |
|---|
| 120 | icon.y = 0; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | protected function updateClickLayer ():void { |
|---|
| 127 | if (_clickLayer && contains(_clickLayer)) { |
|---|
| 128 | removeChild(_clickLayer); |
|---|
| 129 | } |
|---|
| 130 | /*_clickLayer = Draw.clone(_background as Sprite) as Sprite; |
|---|
| 131 | var overTransform:ColorTransform = new ColorTransform() |
|---|
| 132 | overTransform.color = 0; |
|---|
| 133 | _clickLayer.transform.colorTransform = overTransform; |
|---|
| 134 | _clickLayer.alpha = 1; |
|---|
| 135 | _clickLayer.name = "clickLayer"; |
|---|
| 136 | addChild(_clickLayer); |
|---|
| 137 | _clickLayer.addEventListener(MouseEvent.MOUSE_OVER, tempHandler);*/ |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | public function set outColor (outColor:Color):void { |
|---|
| 142 | _outColor = outColor; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | public function set overColor (overColor:Color):void { |
|---|
| 147 | _overColor = overColor; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | public function set clickFunction (clickFunction:Function):void { |
|---|
| 152 | _clickFunction = clickFunction |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | private function nameDisplayObject (name:String, displayObject:DisplayObject):void { |
|---|
| 157 | try { |
|---|
| 158 | displayObject.name = name; |
|---|
| 159 | } catch (error:Error) { |
|---|
| 160 | |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | } |
|---|