| [517] | 1 | package com.longtailvideo.jwplayer.view.components { |
|---|
| [400] | 2 | import com.longtailvideo.jwplayer.events.MediaEvent; |
|---|
| 3 | import com.longtailvideo.jwplayer.events.PlayerEvent; |
|---|
| [413] | 4 | import com.longtailvideo.jwplayer.events.PlayerStateEvent; |
|---|
| [400] | 5 | import com.longtailvideo.jwplayer.events.ViewEvent; |
|---|
| [548] | 6 | import com.longtailvideo.jwplayer.player.IPlayer; |
|---|
| [400] | 7 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 8 | import com.longtailvideo.jwplayer.view.interfaces.IDisplayComponent; |
|---|
| 9 | import flash.display.DisplayObject; |
|---|
| 10 | import flash.display.MovieClip; |
|---|
| [578] | 11 | import flash.display.Sprite; |
|---|
| [400] | 12 | import flash.events.MouseEvent; |
|---|
| 13 | import flash.geom.ColorTransform; |
|---|
| 14 | import flash.text.GridFitType; |
|---|
| 15 | import flash.text.TextField; |
|---|
| [476] | 16 | import flash.text.TextFormatAlign; |
|---|
| [400] | 17 | |
|---|
| [578] | 18 | |
|---|
| [400] | 19 | public class DisplayComponent extends CoreComponent implements IDisplayComponent { |
|---|
| 20 | protected var _icon:DisplayObject; |
|---|
| 21 | protected var _background:MovieClip; |
|---|
| 22 | protected var _text:TextField; |
|---|
| [578] | 23 | protected var _icons:Object; |
|---|
| [400] | 24 | |
|---|
| [578] | 25 | |
|---|
| [548] | 26 | public function DisplayComponent(player:IPlayer) { |
|---|
| [576] | 27 | super(player, "display"); |
|---|
| [400] | 28 | addListeners(); |
|---|
| 29 | setupDisplayObjects(); |
|---|
| [578] | 30 | setupIcons(); |
|---|
| [400] | 31 | } |
|---|
| 32 | |
|---|
| [578] | 33 | |
|---|
| [400] | 34 | private function addListeners():void { |
|---|
| [422] | 35 | player.addEventListener(MediaEvent.JWPLAYER_MEDIA_MUTE, stateHandler); |
|---|
| [413] | 36 | player.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler); |
|---|
| [400] | 37 | player.addEventListener(PlayerEvent.JWPLAYER_ERROR, errorHandler); |
|---|
| 38 | addEventListener(MouseEvent.CLICK, clickHandler); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| [578] | 41 | |
|---|
| [400] | 42 | private function setupDisplayObjects():void { |
|---|
| 43 | _background = new MovieClip(); |
|---|
| 44 | background.name = "background"; |
|---|
| 45 | addChildAt(background, 0); |
|---|
| [578] | 46 | background.graphics.beginFill(0, 0); |
|---|
| 47 | background.graphics.drawRect(0, 0, 1, 1); |
|---|
| [400] | 48 | background.graphics.endFill(); |
|---|
| [505] | 49 | if (player.config.screencolor) { |
|---|
| 50 | var colorTransform:ColorTransform = new ColorTransform(); |
|---|
| 51 | colorTransform.color = player.config.screencolor.color; |
|---|
| 52 | background.transform.colorTransform = colorTransform; |
|---|
| 53 | } |
|---|
| [400] | 54 | _icon = new MovieClip(); |
|---|
| [578] | 55 | addChildAt(icon, 1); |
|---|
| [400] | 56 | _text = new TextField(); |
|---|
| [518] | 57 | var textColorTransform:ColorTransform = new ColorTransform(); |
|---|
| 58 | textColorTransform.color = player.config.frontcolor ? player.config.frontcolor.color : 0xFFFFFF; |
|---|
| 59 | text.transform.colorTransform = textColorTransform; |
|---|
| [400] | 60 | text.gridFitType = GridFitType.NONE; |
|---|
| [578] | 61 | addChildAt(text, 2); |
|---|
| [400] | 62 | } |
|---|
| 63 | |
|---|
| [578] | 64 | |
|---|
| 65 | protected function setupIcons():void { |
|---|
| 66 | _icons = {}; |
|---|
| 67 | setupIcon('buffer'); |
|---|
| 68 | setupIcon('play'); |
|---|
| 69 | setupIcon('mute'); |
|---|
| 70 | setupIcon('error'); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | protected function setupIcon(name:String):void { |
|---|
| 75 | var back:Sprite = getSkinElement('background') as Sprite; |
|---|
| 76 | var icon:Sprite = getSkinElement(name + 'Icon') as Sprite; |
|---|
| 77 | if (back) { |
|---|
| 78 | back.x = 0; |
|---|
| 79 | back.y = 0; |
|---|
| 80 | back.addChild(icon); |
|---|
| 81 | icon.x = (back.width - icon.width) / 2; |
|---|
| 82 | icon.y = (back.height - icon.height) / 2; |
|---|
| 83 | _icons[name] = back; |
|---|
| 84 | } else { |
|---|
| 85 | _icons[name] = icon; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| [400] | 90 | public function resize(width:Number, height:Number):void { |
|---|
| 91 | background.width = width; |
|---|
| 92 | background.height = height; |
|---|
| 93 | positionIcon(); |
|---|
| 94 | positionText(); |
|---|
| [422] | 95 | stateHandler(); |
|---|
| [400] | 96 | } |
|---|
| 97 | |
|---|
| [578] | 98 | |
|---|
| [400] | 99 | public function setIcon(displayIcon:DisplayObject):void { |
|---|
| [422] | 100 | try { |
|---|
| 101 | removeChild(icon); |
|---|
| 102 | } catch (err:Error) { |
|---|
| 103 | } |
|---|
| [578] | 104 | if (displayIcon) { |
|---|
| [422] | 105 | _icon = displayIcon; |
|---|
| 106 | addChild(icon); |
|---|
| 107 | positionIcon(); |
|---|
| 108 | } |
|---|
| [400] | 109 | } |
|---|
| [578] | 110 | |
|---|
| 111 | |
|---|
| [400] | 112 | private function positionIcon():void { |
|---|
| [578] | 113 | icon.x = (background.scaleX - icon.width) / 2; |
|---|
| 114 | icon.y = (background.scaleY - icon.height) / 2; |
|---|
| [400] | 115 | } |
|---|
| 116 | |
|---|
| [578] | 117 | |
|---|
| [400] | 118 | public function setText(displayText:String):void { |
|---|
| [476] | 119 | text.text = displayText ? displayText : ''; |
|---|
| [400] | 120 | positionText(); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| [578] | 123 | |
|---|
| [400] | 124 | private function positionText():void { |
|---|
| [578] | 125 | if (text.width > background.scaleX * .75) { |
|---|
| [476] | 126 | text.width = background.scaleX * .75; |
|---|
| 127 | text.wordWrap = true; |
|---|
| 128 | } else { |
|---|
| 129 | text.autoSize = TextFormatAlign.CENTER; |
|---|
| 130 | } |
|---|
| 131 | text.x = (background.scaleX - text.textWidth) / 2; |
|---|
| [482] | 132 | text.y = icon.y + (icon.height / 2) + 10; |
|---|
| [400] | 133 | } |
|---|
| 134 | |
|---|
| [578] | 135 | |
|---|
| [400] | 136 | protected function setDisplay(displayIcon:DisplayObject, displayText:String = null):void { |
|---|
| [422] | 137 | setIcon(displayIcon); |
|---|
| [482] | 138 | setText(displayText); |
|---|
| [400] | 139 | } |
|---|
| 140 | |
|---|
| [578] | 141 | |
|---|
| [400] | 142 | protected function clearDisplay():void { |
|---|
| [578] | 143 | setDisplay(null, null); |
|---|
| [400] | 144 | } |
|---|
| 145 | |
|---|
| [578] | 146 | |
|---|
| [422] | 147 | protected function stateHandler(event:PlayerEvent = null):void { |
|---|
| [482] | 148 | //TODO: Handle mute button in error state |
|---|
| [422] | 149 | switch (player.state) { |
|---|
| [400] | 150 | case PlayerState.BUFFERING: |
|---|
| [578] | 151 | setDisplay(_icons['buffer']); |
|---|
| [400] | 152 | break; |
|---|
| 153 | case PlayerState.PAUSED: |
|---|
| [578] | 154 | setDisplay(_icons['play']); |
|---|
| [400] | 155 | break; |
|---|
| 156 | case PlayerState.IDLE: |
|---|
| [578] | 157 | setDisplay(_icons['play']); |
|---|
| [400] | 158 | break; |
|---|
| 159 | default: |
|---|
| [578] | 160 | if (player.mute) { |
|---|
| 161 | setDisplay(_icons['mute']); |
|---|
| [422] | 162 | } else { |
|---|
| 163 | clearDisplay(); |
|---|
| 164 | } |
|---|
| [400] | 165 | } |
|---|
| 166 | } |
|---|
| [578] | 167 | |
|---|
| 168 | |
|---|
| [400] | 169 | protected function errorHandler(event:PlayerEvent):void { |
|---|
| [578] | 170 | setDisplay(_icons['error'], event.message); |
|---|
| [400] | 171 | } |
|---|
| 172 | |
|---|
| [578] | 173 | |
|---|
| [400] | 174 | protected function clickHandler(event:MouseEvent):void { |
|---|
| [422] | 175 | var clickEvent:String = player.state == PlayerState.PLAYING ? ViewEvent.JWPLAYER_VIEW_PAUSE : ViewEvent.JWPLAYER_VIEW_PLAY; |
|---|
| 176 | dispatchEvent(new ViewEvent(clickEvent)); |
|---|
| [400] | 177 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_CLICK)); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| [578] | 180 | |
|---|
| [400] | 181 | protected function get icon():DisplayObject { |
|---|
| 182 | return _icon; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| [578] | 185 | |
|---|
| [400] | 186 | protected function get text():TextField { |
|---|
| 187 | return _text; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| [578] | 190 | |
|---|
| [400] | 191 | protected function get background():MovieClip { |
|---|
| 192 | return _background; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | } |
|---|