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