| 1 | package com.longtailvideo.jwplayer.view.components { |
|---|
| 2 | import com.longtailvideo.jwplayer.events.MediaEvent; |
|---|
| 3 | import com.longtailvideo.jwplayer.events.PlayerEvent; |
|---|
| 4 | import com.longtailvideo.jwplayer.events.PlayerStateEvent; |
|---|
| 5 | import com.longtailvideo.jwplayer.events.PlaylistEvent; |
|---|
| 6 | import com.longtailvideo.jwplayer.events.ViewEvent; |
|---|
| 7 | import com.longtailvideo.jwplayer.player.IPlayer; |
|---|
| 8 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 9 | import com.longtailvideo.jwplayer.utils.Draw; |
|---|
| 10 | import com.longtailvideo.jwplayer.view.interfaces.IDisplayComponent; |
|---|
| 11 | import com.longtailvideo.jwplayer.view.skins.PNGSkin; |
|---|
| 12 | |
|---|
| 13 | import flash.display.Bitmap; |
|---|
| 14 | import flash.display.DisplayObject; |
|---|
| 15 | import flash.display.MovieClip; |
|---|
| 16 | import flash.display.Sprite; |
|---|
| 17 | import flash.events.MouseEvent; |
|---|
| 18 | import flash.geom.ColorTransform; |
|---|
| 19 | import flash.text.GridFitType; |
|---|
| 20 | import flash.text.TextField; |
|---|
| 21 | import flash.text.TextFormatAlign; |
|---|
| 22 | import flash.utils.clearInterval; |
|---|
| 23 | import flash.utils.setInterval; |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | public class DisplayComponent extends CoreComponent implements IDisplayComponent { |
|---|
| 27 | protected var _icon:DisplayObject; |
|---|
| 28 | protected var _background:MovieClip; |
|---|
| 29 | protected var _text:TextField; |
|---|
| 30 | protected var _icons:Object; |
|---|
| 31 | protected var _rotateInterval:Number; |
|---|
| 32 | protected var _bufferIcon:Sprite; |
|---|
| 33 | protected var _rotate:Boolean = true; |
|---|
| 34 | protected var _youtubeMask:MovieClip; |
|---|
| 35 | |
|---|
| 36 | protected var _bufferRotationTime:Number = 100; |
|---|
| 37 | protected var _bufferRotationAngle:Number = 15; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | public function DisplayComponent(player:IPlayer) { |
|---|
| 41 | super(player, "display"); |
|---|
| 42 | addListeners(); |
|---|
| 43 | setupDisplayObjects(); |
|---|
| 44 | setupIcons(); |
|---|
| 45 | if (!isNaN(getConfigParam('bufferrotation'))) _bufferRotationAngle = Number(getConfigParam('bufferrotation')); |
|---|
| 46 | if (!isNaN(getConfigParam('bufferinterval'))) _bufferRotationTime = Number(getConfigParam('bufferinterval')); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | private function itemHandler(evt:PlaylistEvent):void { |
|---|
| 51 | setDisplay(_icons['play'], ''); |
|---|
| 52 | if (_player.playlist.currentItem && _player.playlist.currentItem.provider == "youtube") { |
|---|
| 53 | this.mask = _youtubeMask; |
|---|
| 54 | } else { |
|---|
| 55 | this.mask = null; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | private function addListeners():void { |
|---|
| 61 | player.addEventListener(MediaEvent.JWPLAYER_MEDIA_MUTE, stateHandler); |
|---|
| 62 | player.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler); |
|---|
| 63 | player.addEventListener(PlayerEvent.JWPLAYER_ERROR, errorHandler); |
|---|
| 64 | player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, itemHandler); |
|---|
| 65 | addEventListener(MouseEvent.CLICK, clickHandler); |
|---|
| 66 | this.buttonMode = true; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | private function setupDisplayObjects():void { |
|---|
| 71 | _background = new MovieClip(); |
|---|
| 72 | background.name = "background"; |
|---|
| 73 | addChildAt(background, 0); |
|---|
| 74 | background.graphics.beginFill(0, 0); |
|---|
| 75 | background.graphics.drawRect(0, 0, 1, 1); |
|---|
| 76 | background.graphics.endFill(); |
|---|
| 77 | |
|---|
| 78 | _icon = new MovieClip(); |
|---|
| 79 | addChildAt(icon, 1); |
|---|
| 80 | |
|---|
| 81 | _text = new TextField(); |
|---|
| 82 | var textColorTransform:ColorTransform = new ColorTransform(); |
|---|
| 83 | textColorTransform.color = player.config.frontcolor ? player.config.frontcolor.color : 0x999999; |
|---|
| 84 | text.transform.colorTransform = textColorTransform; |
|---|
| 85 | text.gridFitType = GridFitType.NONE; |
|---|
| 86 | addChildAt(text, 2); |
|---|
| 87 | |
|---|
| 88 | _youtubeMask = new MovieClip(); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | protected function setupIcons():void { |
|---|
| 93 | _icons = {}; |
|---|
| 94 | setupIcon('buffer'); |
|---|
| 95 | setupIcon('play'); |
|---|
| 96 | setupIcon('mute'); |
|---|
| 97 | setupIcon('error'); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 102 | * Takes in an icon from a PNG skin and rearranges its children so that it's centered around 0, 0 |
|---|
| 103 | */ |
|---|
| 104 | protected function centerIcon(icon:Sprite):void { |
|---|
| 105 | if (icon) { |
|---|
| 106 | for (var i:Number=0; i < icon.numChildren; i++) { |
|---|
| 107 | icon.getChildAt(i).x = -Math.round(icon.getChildAt(i).width)/2; |
|---|
| 108 | icon.getChildAt(i).y = -Math.round(icon.getChildAt(i).height)/2; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | protected function setupIcon(name:String):void { |
|---|
| 114 | var icon:Sprite = getSkinElement(name + 'Icon') as Sprite; |
|---|
| 115 | var iconOver:Sprite = getSkinElement(name + 'IconOver') as Sprite; |
|---|
| 116 | |
|---|
| 117 | if (!icon) { return; } |
|---|
| 118 | |
|---|
| 119 | if (_player.skin is PNGSkin) { |
|---|
| 120 | if (icon.getChildByName("bitmap")) { |
|---|
| 121 | centerIcon(icon); |
|---|
| 122 | icon.name = 'out'; |
|---|
| 123 | } |
|---|
| 124 | if (iconOver && iconOver.getChildByName("bitmap")) { |
|---|
| 125 | centerIcon(iconOver); |
|---|
| 126 | iconOver.name = 'over'; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | if (name == "buffer") { |
|---|
| 131 | if (player.skin is PNGSkin) { |
|---|
| 132 | if (icon is MovieClip && (icon as MovieClip).totalFrames > 1) { |
|---|
| 133 | // Buffer is already animated; no need to rotate. |
|---|
| 134 | _rotate = false; |
|---|
| 135 | } else { |
|---|
| 136 | try { |
|---|
| 137 | _bufferIcon = icon; |
|---|
| 138 | var bufferBitmap:Bitmap = _bufferIcon.getChildByName('bitmap') as Bitmap; |
|---|
| 139 | if (bufferBitmap) { |
|---|
| 140 | Draw.smooth(bufferBitmap); |
|---|
| 141 | } else { |
|---|
| 142 | centerIcon(icon); |
|---|
| 143 | } |
|---|
| 144 | } catch (e:Error) { |
|---|
| 145 | _rotate = false; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } else { |
|---|
| 149 | _rotate = false; |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | var back:Sprite = getSkinElement('background') as Sprite; |
|---|
| 154 | if (back) { |
|---|
| 155 | if (_player.skin is PNGSkin) centerIcon(back); |
|---|
| 156 | } else { |
|---|
| 157 | back = new Sprite(); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | if (iconOver && player.skin is PNGSkin && name != "buffer") { |
|---|
| 161 | iconOver.visible = false; |
|---|
| 162 | back.addChild(iconOver); |
|---|
| 163 | back.addEventListener(MouseEvent.MOUSE_OVER, overHandler); |
|---|
| 164 | back.addEventListener(MouseEvent.MOUSE_OUT, outHandler); |
|---|
| 165 | } |
|---|
| 166 | back.addChild(icon); |
|---|
| 167 | if (player.skin is PNGSkin && !icon.getChildByName("bitmap")) { |
|---|
| 168 | if (name != "buffer" || !_rotate) { |
|---|
| 169 | centerIcon(back); |
|---|
| 170 | } |
|---|
| 171 | } else { |
|---|
| 172 | back.x = back.y = icon.x = icon.y = 0; |
|---|
| 173 | } |
|---|
| 174 | _icons[name] = back; |
|---|
| 175 | |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | protected function overHandler(evt:MouseEvent):void { |
|---|
| 179 | var button:Sprite = _icon as Sprite; |
|---|
| 180 | if (button) { |
|---|
| 181 | setIconHover(button, true); |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | protected function outHandler(evt:MouseEvent):void { |
|---|
| 186 | var button:Sprite = _icon as Sprite; |
|---|
| 187 | if (button) { |
|---|
| 188 | setIconHover(button, false); |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | protected function setIconHover(icon:Sprite, state:Boolean):void { |
|---|
| 193 | var over:DisplayObject = icon.getChildByName('over'); |
|---|
| 194 | var out:DisplayObject = icon.getChildByName('out'); |
|---|
| 195 | |
|---|
| 196 | if (over && out) { |
|---|
| 197 | over.visible = state; |
|---|
| 198 | out.visible = !state; |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | public function resize(width:Number, height:Number):void { |
|---|
| 203 | _background.width = width; |
|---|
| 204 | _background.height = height; |
|---|
| 205 | |
|---|
| 206 | _youtubeMask.graphics.clear(); |
|---|
| 207 | _youtubeMask.graphics.beginFill(0x00AA00, 1); |
|---|
| 208 | _youtubeMask.graphics.drawRect(0, 0, width-120, height); |
|---|
| 209 | _youtubeMask.graphics.endFill(); |
|---|
| 210 | _youtubeMask.graphics.beginFill(0x00AA00, 1); |
|---|
| 211 | _youtubeMask.graphics.drawRect(0, 0, width, height-60); |
|---|
| 212 | _youtubeMask.graphics.endFill(); |
|---|
| 213 | |
|---|
| 214 | positionIcon(); |
|---|
| 215 | positionText(); |
|---|
| 216 | stateHandler(); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | public function setIcon(displayIcon:DisplayObject):void { |
|---|
| 221 | try { |
|---|
| 222 | removeChild(icon); |
|---|
| 223 | } catch (err:Error) { |
|---|
| 224 | } |
|---|
| 225 | if (displayIcon && _player.config.icons) { |
|---|
| 226 | if (displayIcon is Sprite) { |
|---|
| 227 | setIconHover(displayIcon as Sprite, false); |
|---|
| 228 | } |
|---|
| 229 | _icon = displayIcon; |
|---|
| 230 | addChild(icon); |
|---|
| 231 | positionIcon(); |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | private function positionIcon():void { |
|---|
| 237 | icon.x = background.scaleX / 2; |
|---|
| 238 | icon.y = background.scaleY / 2; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | public function setText(displayText:String):void { |
|---|
| 243 | if (_icon is Sprite && (_icon as Sprite).getChildByName('txt') is TextField) { |
|---|
| 244 | ((_icon as Sprite).getChildByName('txt') as TextField).text = displayText ? displayText : ''; |
|---|
| 245 | text.text = ''; |
|---|
| 246 | } else { |
|---|
| 247 | text.text = displayText ? displayText : ''; |
|---|
| 248 | } |
|---|
| 249 | positionText(); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | private function positionText():void { |
|---|
| 254 | if (text.text) { |
|---|
| 255 | text.visible = true; |
|---|
| 256 | if (text.width > background.scaleX * .75) { |
|---|
| 257 | text.width = background.scaleX * .75; |
|---|
| 258 | text.wordWrap = true; |
|---|
| 259 | } else { |
|---|
| 260 | text.autoSize = TextFormatAlign.CENTER; |
|---|
| 261 | } |
|---|
| 262 | text.x = (background.scaleX - text.textWidth) / 2; |
|---|
| 263 | if (contains(icon)) { |
|---|
| 264 | text.y = icon.y + (icon.height/2) + 10; |
|---|
| 265 | } else { |
|---|
| 266 | text.y = (background.scaleY - text.textHeight) / 2; |
|---|
| 267 | } |
|---|
| 268 | } else { |
|---|
| 269 | text.visible = false; |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | protected function setDisplay(displayIcon:DisplayObject, displayText:String = null):void { |
|---|
| 275 | setIcon(displayIcon); |
|---|
| 276 | setText(displayText != null ? displayText : text.text); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | protected function clearDisplay():void { |
|---|
| 281 | setDisplay(null, ''); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | protected function stateHandler(event:PlayerEvent = null):void { |
|---|
| 286 | //TODO: Handle mute button in error state |
|---|
| 287 | clearRotation(); |
|---|
| 288 | switch (player.state) { |
|---|
| 289 | case PlayerState.BUFFERING: |
|---|
| 290 | setDisplay(_icons['buffer'], ''); |
|---|
| 291 | if (_rotate){ |
|---|
| 292 | startRotation(); |
|---|
| 293 | } |
|---|
| 294 | break; |
|---|
| 295 | case PlayerState.PAUSED: |
|---|
| 296 | setDisplay(_icons['play']); |
|---|
| 297 | break; |
|---|
| 298 | case PlayerState.IDLE: |
|---|
| 299 | setDisplay(_icons['play']); |
|---|
| 300 | break; |
|---|
| 301 | default: |
|---|
| 302 | if (player.config.mute) { |
|---|
| 303 | setDisplay(_icons['mute']); |
|---|
| 304 | } else { |
|---|
| 305 | clearDisplay(); |
|---|
| 306 | } |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | protected function startRotation():void { |
|---|
| 312 | if (!_rotateInterval && _bufferRotationAngle > 0) { |
|---|
| 313 | _rotateInterval = setInterval(updateRotation, _bufferRotationTime); |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | protected function updateRotation():void { |
|---|
| 319 | if (_bufferIcon) _bufferIcon.rotation += _bufferRotationAngle; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | protected function clearRotation():void { |
|---|
| 324 | if (_bufferIcon) _bufferIcon.rotation = 0; |
|---|
| 325 | if (_rotateInterval) { |
|---|
| 326 | clearInterval(_rotateInterval); |
|---|
| 327 | _rotateInterval = undefined; |
|---|
| 328 | } |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | protected function errorHandler(event:PlayerEvent):void { |
|---|
| 333 | setDisplay(_icons['error'], event.message); |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | protected function clickHandler(event:MouseEvent):void { |
|---|
| 338 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_CLICK)); |
|---|
| 339 | if (player.state == PlayerState.PLAYING || player.state == PlayerState.BUFFERING) { |
|---|
| 340 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_PAUSE)); |
|---|
| 341 | } else { |
|---|
| 342 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_PLAY)); |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | protected function get icon():DisplayObject { |
|---|
| 348 | return _icon; |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | protected function get text():TextField { |
|---|
| 353 | return _text; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | protected function get background():MovieClip { |
|---|
| 358 | return _background; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | } |
|---|
| 362 | } |
|---|