| 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.ViewEvent; |
|---|
| 6 | import com.longtailvideo.jwplayer.player.IPlayer; |
|---|
| 7 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 8 | import com.longtailvideo.jwplayer.view.interfaces.IDisplayComponent; |
|---|
| 9 | |
|---|
| 10 | import flash.display.DisplayObject; |
|---|
| 11 | import flash.display.MovieClip; |
|---|
| 12 | import flash.events.MouseEvent; |
|---|
| 13 | import flash.geom.ColorTransform; |
|---|
| 14 | import flash.text.GridFitType; |
|---|
| 15 | import flash.text.TextField; |
|---|
| 16 | import flash.text.TextFieldAutoSize; |
|---|
| 17 | import flash.text.TextFormatAlign; |
|---|
| 18 | import flash.text.TextFormat; |
|---|
| 19 | |
|---|
| 20 | public class DisplayComponent extends CoreComponent implements IDisplayComponent { |
|---|
| 21 | protected var _icon:DisplayObject; |
|---|
| 22 | protected var _background:MovieClip; |
|---|
| 23 | protected var _text:TextField; |
|---|
| 24 | |
|---|
| 25 | public function DisplayComponent(player:IPlayer) { |
|---|
| 26 | super(player); |
|---|
| 27 | addListeners(); |
|---|
| 28 | setupDisplayObjects(); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | private function addListeners():void { |
|---|
| 32 | player.addEventListener(MediaEvent.JWPLAYER_MEDIA_MUTE, stateHandler); |
|---|
| 33 | player.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler); |
|---|
| 34 | player.addEventListener(PlayerEvent.JWPLAYER_ERROR, errorHandler); |
|---|
| 35 | addEventListener(MouseEvent.CLICK, clickHandler); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | private function setupDisplayObjects():void { |
|---|
| 39 | _background = new MovieClip(); |
|---|
| 40 | background.name = "background"; |
|---|
| 41 | addChildAt(background, 0); |
|---|
| 42 | background.graphics.beginFill(0,0); |
|---|
| 43 | background.graphics.drawRect(0,0,1,1); |
|---|
| 44 | background.graphics.endFill(); |
|---|
| 45 | if (player.config.screencolor) { |
|---|
| 46 | var colorTransform:ColorTransform = new ColorTransform(); |
|---|
| 47 | colorTransform.color = player.config.screencolor.color; |
|---|
| 48 | background.transform.colorTransform = colorTransform; |
|---|
| 49 | } |
|---|
| 50 | _icon = new MovieClip(); |
|---|
| 51 | addChildAt(icon,1); |
|---|
| 52 | _text = new TextField(); |
|---|
| 53 | var textColorTransform:ColorTransform = new ColorTransform(); |
|---|
| 54 | textColorTransform.color = player.config.frontcolor ? player.config.frontcolor.color : 0xFFFFFF; |
|---|
| 55 | text.transform.colorTransform = textColorTransform; |
|---|
| 56 | text.gridFitType = GridFitType.NONE; |
|---|
| 57 | addChildAt(text,2); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | public function resize(width:Number, height:Number):void { |
|---|
| 61 | background.width = width; |
|---|
| 62 | background.height = height; |
|---|
| 63 | positionIcon(); |
|---|
| 64 | positionText(); |
|---|
| 65 | stateHandler(); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public function setIcon(displayIcon:DisplayObject):void { |
|---|
| 69 | try { |
|---|
| 70 | removeChild(icon); |
|---|
| 71 | } catch (err:Error) { |
|---|
| 72 | |
|---|
| 73 | } |
|---|
| 74 | if (displayIcon){ |
|---|
| 75 | _icon = displayIcon; |
|---|
| 76 | addChild(icon); |
|---|
| 77 | positionIcon(); |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | private function positionIcon():void { |
|---|
| 82 | icon.x = background.scaleX / 2; |
|---|
| 83 | icon.y = background.scaleY / 2 ; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | public function setText(displayText:String):void { |
|---|
| 87 | text.text = displayText ? displayText : ''; |
|---|
| 88 | positionText(); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | private function positionText():void { |
|---|
| 92 | if (text.width > background.scaleX * .75){ |
|---|
| 93 | text.width = background.scaleX * .75; |
|---|
| 94 | text.wordWrap = true; |
|---|
| 95 | } else { |
|---|
| 96 | text.autoSize = TextFormatAlign.CENTER; |
|---|
| 97 | } |
|---|
| 98 | text.x = (background.scaleX - text.textWidth) / 2; |
|---|
| 99 | text.y = icon.y + (icon.height / 2) + 10; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | protected function setDisplay(displayIcon:DisplayObject, displayText:String = null):void { |
|---|
| 103 | setIcon(displayIcon); |
|---|
| 104 | setText(displayText); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | protected function clearDisplay():void { |
|---|
| 108 | setDisplay(null,null); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | protected function stateHandler(event:PlayerEvent = null):void { |
|---|
| 112 | //TODO: Handle mute button in error state |
|---|
| 113 | switch (player.state) { |
|---|
| 114 | case PlayerState.BUFFERING: |
|---|
| 115 | setDisplay(getSkinElement('display', 'bufferIcon')); |
|---|
| 116 | break; |
|---|
| 117 | case PlayerState.PAUSED: |
|---|
| 118 | setDisplay(getSkinElement('display', 'playIcon')); |
|---|
| 119 | break; |
|---|
| 120 | case PlayerState.IDLE: |
|---|
| 121 | setDisplay(getSkinElement('display', 'playIcon')); |
|---|
| 122 | break; |
|---|
| 123 | default: |
|---|
| 124 | if (player.mute){ |
|---|
| 125 | setDisplay(getSkinElement('display', 'muteIcon')); |
|---|
| 126 | } else { |
|---|
| 127 | clearDisplay(); |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | protected function errorHandler(event:PlayerEvent):void { |
|---|
| 133 | setDisplay(getSkinElement('display', 'errorIcon'), event.message); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | protected function clickHandler(event:MouseEvent):void { |
|---|
| 137 | var clickEvent:String = player.state == PlayerState.PLAYING ? ViewEvent.JWPLAYER_VIEW_PAUSE : ViewEvent.JWPLAYER_VIEW_PLAY; |
|---|
| 138 | dispatchEvent(new ViewEvent(clickEvent)); |
|---|
| 139 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_CLICK)); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | protected function get icon():DisplayObject { |
|---|
| 143 | return _icon; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | protected function get text():TextField { |
|---|
| 147 | return _text; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | protected function get background():MovieClip { |
|---|
| 151 | return _background; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | private function getSkinElement(component:String, element:String):DisplayObject { |
|---|
| 155 | return player.skin.getSkinElement(component,element); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | } |
|---|