| 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.model.Color; |
|---|
| 8 | import com.longtailvideo.jwplayer.player.IPlayer; |
|---|
| 9 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 10 | import com.longtailvideo.jwplayer.plugins.PluginConfig; |
|---|
| 11 | import com.longtailvideo.jwplayer.utils.Animations; |
|---|
| 12 | import com.longtailvideo.jwplayer.utils.Logger; |
|---|
| 13 | import com.longtailvideo.jwplayer.utils.RootReference; |
|---|
| 14 | import com.longtailvideo.jwplayer.utils.Strings; |
|---|
| 15 | import com.longtailvideo.jwplayer.view.interfaces.IControlbarComponent; |
|---|
| 16 | |
|---|
| 17 | import flash.display.DisplayObject; |
|---|
| 18 | import flash.display.MovieClip; |
|---|
| 19 | import flash.display.Sprite; |
|---|
| 20 | import flash.events.Event; |
|---|
| 21 | import flash.events.MouseEvent; |
|---|
| 22 | import flash.geom.ColorTransform; |
|---|
| 23 | import flash.text.StyleSheet; |
|---|
| 24 | import flash.text.TextField; |
|---|
| 25 | import flash.text.TextFieldAutoSize; |
|---|
| 26 | import flash.text.TextFormat; |
|---|
| 27 | import flash.ui.Mouse; |
|---|
| 28 | import flash.utils.clearTimeout; |
|---|
| 29 | import flash.utils.setTimeout; |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * Sent when the user interface requests that the player play the currently loaded media |
|---|
| 34 | * |
|---|
| 35 | * @eventType com.longtailvideo.jwplayer.events.ViewEvent.JWPLAYER_VIEW_PLAY |
|---|
| 36 | */ |
|---|
| 37 | [Event(name="jwPlayerViewPlay", type="com.longtailvideo.jwplayer.events.ViewEvent")] |
|---|
| 38 | /** |
|---|
| 39 | * Sent when the user interface requests that the player pause the currently playing media |
|---|
| 40 | * |
|---|
| 41 | * @eventType com.longtailvideo.jwplayer.events.ViewEvent.JWPLAYER_VIEW_PAUSE |
|---|
| 42 | */ |
|---|
| 43 | [Event(name="jwPlayerViewPause", type="com.longtailvideo.jwplayer.events.ViewEvent")] |
|---|
| 44 | /** |
|---|
| 45 | * Sent when the user interface requests that the player stop the currently playing media |
|---|
| 46 | * |
|---|
| 47 | * @eventType com.longtailvideo.jwplayer.events.ViewEvent.JWPLAYER_VIEW_STOP |
|---|
| 48 | */ |
|---|
| 49 | [Event(name="jwPlayerViewStop", type="com.longtailvideo.jwplayer.events.ViewEvent")] |
|---|
| 50 | /** |
|---|
| 51 | * Sent when the user interface requests that the player play the next item in its playlist |
|---|
| 52 | * |
|---|
| 53 | * @eventType com.longtailvideo.jwplayer.events.ViewEvent.JWPLAYER_VIEW_NEXT |
|---|
| 54 | */ |
|---|
| 55 | [Event(name="jwPlayerViewNext", type="com.longtailvideo.jwplayer.events.ViewEvent")] |
|---|
| 56 | /** |
|---|
| 57 | * Sent when the user interface requests that the player play the previous item in its playlist |
|---|
| 58 | * |
|---|
| 59 | * @eventType com.longtailvideo.jwplayer.events.ViewEvent.JWPLAYER_VIEW_PREV |
|---|
| 60 | */ |
|---|
| 61 | [Event(name="jwPlayerViewPrev", type="com.longtailvideo.jwplayer.events.ViewEvent")] |
|---|
| 62 | /** |
|---|
| 63 | * Sent when the user interface requests that the player navigate to the playlist item's <code>link</code> property |
|---|
| 64 | * |
|---|
| 65 | * @eventType com.longtailvideo.jwplayer.events.ViewEvent.JWPLAYER_VIEW_LINK |
|---|
| 66 | */ |
|---|
| 67 | [Event(name="jwPlayerViewLink", type="com.longtailvideo.jwplayer.events.ViewEvent")] |
|---|
| 68 | /** |
|---|
| 69 | * |
|---|
| 70 | * |
|---|
| 71 | * @eventType com.longtailvideo.jwplayer.events.ViewEvent.JWPLAYER_VIEW_MUTE |
|---|
| 72 | */ |
|---|
| 73 | [Event(name="jwPlayerViewMute", type="com.longtailvideo.jwplayer.events.ViewEvent")] |
|---|
| 74 | /** |
|---|
| 75 | * |
|---|
| 76 | * |
|---|
| 77 | * @eventType com.longtailvideo.jwplayer.events.ViewEvent.JWPLAYER_VIEW_FULLSCREEN |
|---|
| 78 | */ |
|---|
| 79 | [Event(name="jwPlayerViewFullscreen", type="com.longtailvideo.jwplayer.events.ViewEvent")] |
|---|
| 80 | /** |
|---|
| 81 | * |
|---|
| 82 | * |
|---|
| 83 | * @eventType com.longtailvideo.jwplayer.events.ViewEvent.JWPLAYER_VIEW_VOLUME |
|---|
| 84 | */ |
|---|
| 85 | [Event(name="jwPlayerViewVolume", type="com.longtailvideo.jwplayer.events.ViewEvent")] |
|---|
| 86 | /** |
|---|
| 87 | * |
|---|
| 88 | * |
|---|
| 89 | * @eventType com.longtailvideo.jwplayer.events.ViewEvent.JWPLAYER_VIEW_SEEK |
|---|
| 90 | */ |
|---|
| 91 | [Event(name="jwPlayerViewSeek", type="com.longtailvideo.jwplayer.events.ViewEvent")] |
|---|
| 92 | public class ControlbarComponent extends CoreComponent implements IControlbarComponent { |
|---|
| 93 | protected var _buttons:Object = {}; |
|---|
| 94 | protected var _customButtons:Array = []; |
|---|
| 95 | protected var _removedButtons:Array = []; |
|---|
| 96 | protected var _dividers:Array; |
|---|
| 97 | protected var _dividerElements:Object; |
|---|
| 98 | protected var _defaultLayout:String = "[play|stop|prev|next|elapsed][time][duration|blank|fullscreen|mute volume]"; |
|---|
| 99 | protected var _currentLayout:String; |
|---|
| 100 | protected var _layoutManager:ControlbarLayoutManager; |
|---|
| 101 | protected var _width:Number; |
|---|
| 102 | protected var _height:Number; |
|---|
| 103 | protected var _timeSlider:Slider; |
|---|
| 104 | protected var _volSlider:Slider; |
|---|
| 105 | |
|---|
| 106 | protected var _bgColorSheet:Sprite; |
|---|
| 107 | |
|---|
| 108 | protected var _fullscreen:Boolean = false; |
|---|
| 109 | |
|---|
| 110 | protected var controlbarConfig:PluginConfig; |
|---|
| 111 | protected var animations:Animations; |
|---|
| 112 | protected var hiding:Number; |
|---|
| 113 | |
|---|
| 114 | public function ControlbarComponent(player:IPlayer) { |
|---|
| 115 | super(player, "controlbar"); |
|---|
| 116 | controlbarConfig = _player.config.pluginConfig(_name); |
|---|
| 117 | animations = new Animations(this); |
|---|
| 118 | if (controlbarConfig['position'] == "over" && hideOnIdle) { |
|---|
| 119 | alpha = 0; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | _layoutManager = new ControlbarLayoutManager(this); |
|---|
| 123 | _dividers = []; |
|---|
| 124 | _dividerElements = {'divider': true}; |
|---|
| 125 | setupBackground(); |
|---|
| 126 | setupDefaultButtons(); |
|---|
| 127 | addEventListeners(); |
|---|
| 128 | updateControlbarState(); |
|---|
| 129 | setTime(0, 0); |
|---|
| 130 | updateVolumeSlider(); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | private function addEventListeners():void { |
|---|
| 134 | player.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler); |
|---|
| 135 | player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_LOADED, playlistHandler); |
|---|
| 136 | player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, playlistHandler); |
|---|
| 137 | player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, playlistHandler); |
|---|
| 138 | player.addEventListener(MediaEvent.JWPLAYER_MEDIA_MUTE, stateHandler); |
|---|
| 139 | player.addEventListener(MediaEvent.JWPLAYER_MEDIA_VOLUME, updateVolumeSlider); |
|---|
| 140 | player.addEventListener(MediaEvent.JWPLAYER_MEDIA_BUFFER, mediaHandler); |
|---|
| 141 | player.addEventListener(MediaEvent.JWPLAYER_MEDIA_TIME, mediaHandler); |
|---|
| 142 | player.addEventListener(PlayerEvent.JWPLAYER_LOCKED, lockHandler); |
|---|
| 143 | player.addEventListener(PlayerEvent.JWPLAYER_UNLOCKED, lockHandler); |
|---|
| 144 | RootReference.stage.addEventListener(Event.MOUSE_LEAVE, mouseLeftStage); |
|---|
| 145 | RootReference.stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | private function lockHandler(evt:PlayerEvent):void { |
|---|
| 150 | if (_player.locked) { |
|---|
| 151 | if (_timeSlider) _timeSlider.lock(); |
|---|
| 152 | if (_volSlider) _volSlider.lock(); |
|---|
| 153 | } else { |
|---|
| 154 | if (_timeSlider) _timeSlider.unlock(); |
|---|
| 155 | if (_volSlider) _volSlider.unlock(); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | private function playlistHandler(evt:PlaylistEvent):void { |
|---|
| 161 | if (_timeSlider) _timeSlider.reset(); |
|---|
| 162 | updateControlbarState(); |
|---|
| 163 | redraw(); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | private function get fadeOnTimeout():Boolean { |
|---|
| 167 | return controlbarConfig['position'] == 'over' || (_player.config.fullscreen && controlbarConfig['position'] != 'none'); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | private function get hideOnIdle():Boolean { |
|---|
| 171 | return String(controlbarConfig['idlehide']) == "true"; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | private function startFader():void { |
|---|
| 175 | if (fadeOnTimeout) { |
|---|
| 176 | if (!isNaN(hiding)) { |
|---|
| 177 | clearTimeout(hiding); |
|---|
| 178 | } |
|---|
| 179 | hiding = setTimeout(moveTimeout, 2000); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | private function stopFader():void { |
|---|
| 184 | if (alpha == 0) { |
|---|
| 185 | animations.fade(1, 0.5); |
|---|
| 186 | } |
|---|
| 187 | if (!isNaN(hiding)) { |
|---|
| 188 | clearTimeout(hiding); |
|---|
| 189 | Mouse.show(); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | /** Show above controlbar on mousemove and restart the countdown. **/ |
|---|
| 194 | private function moveHandler(evt:MouseEvent=null):void { |
|---|
| 195 | stopFader(); |
|---|
| 196 | if (_player.state == PlayerState.BUFFERING || _player.state == PlayerState.PLAYING || hideOnIdle) { |
|---|
| 197 | startFader(); |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | /** Hide above controlbar again when move has timed out. **/ |
|---|
| 202 | private function moveTimeout(evt:Event=null):void { |
|---|
| 203 | animations.fade(0, 0.5); |
|---|
| 204 | Mouse.hide(); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /** If the mouse leaves the stage, hide the controlbar if position is 'over' **/ |
|---|
| 208 | private function mouseLeftStage(evt:Event=null):void { |
|---|
| 209 | if (fadeOnTimeout) { |
|---|
| 210 | if (_player.state == PlayerState.BUFFERING || _player.state == PlayerState.PLAYING || hideOnIdle) { |
|---|
| 211 | animations.fade(0); |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | private function stateHandler(evt:PlayerEvent=null):void { |
|---|
| 217 | switch(_player.state) { |
|---|
| 218 | case PlayerState.BUFFERING: |
|---|
| 219 | case PlayerState.PLAYING: |
|---|
| 220 | startFader(); |
|---|
| 221 | break; |
|---|
| 222 | case PlayerState.PAUSED: |
|---|
| 223 | case PlayerState.IDLE: |
|---|
| 224 | if (hideOnIdle) { |
|---|
| 225 | mouseLeftStage(); |
|---|
| 226 | } else { |
|---|
| 227 | stopFader(); |
|---|
| 228 | } |
|---|
| 229 | break; |
|---|
| 230 | } |
|---|
| 231 | updateControlbarState(); |
|---|
| 232 | redraw(); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | private function parseStructuredLayout(structuredLayout:Object):String { |
|---|
| 237 | var layoutString:String = ""; |
|---|
| 238 | getTextField('elapsed').visible = false; |
|---|
| 239 | getTextField('duration').visible = false; |
|---|
| 240 | for each (var position:String in ['left','center','right']) { |
|---|
| 241 | layoutString += "["; |
|---|
| 242 | var layout:Array = structuredLayout[position] as Array; |
|---|
| 243 | if (layout) { |
|---|
| 244 | var lastWasDivider:Boolean = true; |
|---|
| 245 | for each (var item:Object in layout) { |
|---|
| 246 | if (item['type'] == "divider") { |
|---|
| 247 | if (item['element']) { |
|---|
| 248 | layoutString += "<" + item['element'] + ">"; |
|---|
| 249 | _dividerElements[item['element']] = true; |
|---|
| 250 | } else if (item['width'] > 0) { |
|---|
| 251 | layoutString += "<"+item['width']+">"; |
|---|
| 252 | } else { |
|---|
| 253 | layoutString += "|"; |
|---|
| 254 | } |
|---|
| 255 | lastWasDivider = true; |
|---|
| 256 | } else { |
|---|
| 257 | if (item['type'] == "text") { |
|---|
| 258 | getTextField(item['name']).visible = true; |
|---|
| 259 | } |
|---|
| 260 | if (!lastWasDivider) layoutString += " "; |
|---|
| 261 | layoutString += item['name']; |
|---|
| 262 | lastWasDivider = false; |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | layoutString += "]"; |
|---|
| 267 | } |
|---|
| 268 | return layoutString; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | private function updateControlbarState():void { |
|---|
| 273 | var newLayout:String = _defaultLayout; |
|---|
| 274 | var controlbarLayout:Object = _player.skin.getSkinProperties().layout['controlbar']; |
|---|
| 275 | if (controlbarLayout) { |
|---|
| 276 | newLayout = parseStructuredLayout(controlbarLayout); |
|---|
| 277 | } |
|---|
| 278 | removeInactive(newLayout); |
|---|
| 279 | newLayout = newLayout.replace("blank", _customButtons.join("|")); |
|---|
| 280 | newLayout = removeButtonFromLayout("blank", newLayout); |
|---|
| 281 | for each (var removed:String in _removedButtons) { |
|---|
| 282 | newLayout = removeButtonFromLayout(removed, newLayout); |
|---|
| 283 | } |
|---|
| 284 | if (player.state == PlayerState.PLAYING) { |
|---|
| 285 | newLayout = newLayout.replace('play', 'pause'); |
|---|
| 286 | hideButton('play'); |
|---|
| 287 | } else if (player.state == PlayerState.IDLE) { |
|---|
| 288 | if (_timeSlider) { |
|---|
| 289 | _timeSlider.reset(); |
|---|
| 290 | _timeSlider.thumbVisible = false; |
|---|
| 291 | if (_player.playlist.currentItem) { |
|---|
| 292 | setTime(0, _player.playlist.currentItem.duration); |
|---|
| 293 | } |
|---|
| 294 | } |
|---|
| 295 | hideButton('pause'); |
|---|
| 296 | } else { |
|---|
| 297 | hideButton('pause'); |
|---|
| 298 | } |
|---|
| 299 | if (player.playlist.length <= 1) { |
|---|
| 300 | newLayout = newLayout.replace("|prev|next", ""); |
|---|
| 301 | hideButton('prev'); |
|---|
| 302 | hideButton('next'); |
|---|
| 303 | } |
|---|
| 304 | if (player.config.mute) { |
|---|
| 305 | newLayout = newLayout.replace("mute", "unmute"); |
|---|
| 306 | hideButton("mute"); |
|---|
| 307 | } else { |
|---|
| 308 | hideButton("unmute"); |
|---|
| 309 | } |
|---|
| 310 | if (player.config.fullscreen) { |
|---|
| 311 | newLayout = newLayout.replace("fullscreen", "normalscreen"); |
|---|
| 312 | hideButton("fullscreen"); |
|---|
| 313 | } else { |
|---|
| 314 | hideButton("normalscreen"); |
|---|
| 315 | } |
|---|
| 316 | _currentLayout = removeInactive(newLayout); |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | private function removeInactive(layout:String):String { |
|---|
| 321 | var buttons:Array = _defaultLayout.match(/\W*([A-Za-z0-9]+?)\W/g); |
|---|
| 322 | for (var i:Number = 0; i < buttons.length; i++) { |
|---|
| 323 | var button:String = (buttons[i] as String).replace(/\W/g, ""); |
|---|
| 324 | if (!_buttons[button]) { |
|---|
| 325 | layout = removeButtonFromLayout(button, layout); |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | return layout; |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | private function removeButtonFromLayout(button:String, layout:String):String { |
|---|
| 333 | layout = layout.replace(button, ""); |
|---|
| 334 | layout = layout.replace(/\|\|/g, "|"); |
|---|
| 335 | layout = layout.replace(/\[\|/g, "["); |
|---|
| 336 | layout = layout.replace(/\|\]/g, "]"); |
|---|
| 337 | layout = layout.replace(/\[\]/g, ""); |
|---|
| 338 | return layout; |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | private function mediaHandler(evt:MediaEvent):void { |
|---|
| 343 | var scrubber:Slider = _timeSlider; |
|---|
| 344 | switch (evt.type) { |
|---|
| 345 | case MediaEvent.JWPLAYER_MEDIA_BUFFER: |
|---|
| 346 | case MediaEvent.JWPLAYER_MEDIA_TIME: |
|---|
| 347 | if (scrubber) { |
|---|
| 348 | scrubber.setProgress(evt.position / evt.duration *100); |
|---|
| 349 | scrubber.thumbVisible = (evt.duration > 0); |
|---|
| 350 | if (evt.bufferPercent > 0) { |
|---|
| 351 | scrubber.setBuffer(evt.bufferPercent); |
|---|
| 352 | scrubber.setBufferOffset(evt.offset / evt.duration * 100); |
|---|
| 353 | } |
|---|
| 354 | if (evt.position > 0) { setTime(evt.position, evt.duration); } |
|---|
| 355 | } |
|---|
| 356 | break; |
|---|
| 357 | default: |
|---|
| 358 | scrubber.reset(); |
|---|
| 359 | break; |
|---|
| 360 | } |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | private function updateVolumeSlider(evt:MediaEvent=null):void { |
|---|
| 365 | var volume:Slider = _volSlider; |
|---|
| 366 | if (volume) { |
|---|
| 367 | var volumeWidth:Number = getSkinElement("volumeSliderRail").width + volume.capsWidth; |
|---|
| 368 | |
|---|
| 369 | if (!_player.config.mute) { |
|---|
| 370 | volume.setBuffer(100); |
|---|
| 371 | volume.setProgress(_player.config.volume); |
|---|
| 372 | volume.thumbVisible = true; |
|---|
| 373 | volume.resize(volumeWidth, volume.height); |
|---|
| 374 | } else { |
|---|
| 375 | volume.reset(); |
|---|
| 376 | volume.thumbVisible = false; |
|---|
| 377 | volume.resize(volumeWidth, volume.height); |
|---|
| 378 | } |
|---|
| 379 | } |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | private function setTime(position:Number, duration:Number):void { |
|---|
| 384 | if (position < 0) { |
|---|
| 385 | position = 0; |
|---|
| 386 | } |
|---|
| 387 | if (duration < 0) { |
|---|
| 388 | duration = 0; |
|---|
| 389 | } |
|---|
| 390 | var elapsedText:TextField = getTextField('elapsed'); |
|---|
| 391 | if (elapsedText) elapsedText.text = Strings.digits(position); |
|---|
| 392 | var durationField:TextField = getTextField('duration'); |
|---|
| 393 | if (durationField) durationField.text = Strings.digits(duration); |
|---|
| 394 | redraw(); |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | private function setupBackground():void { |
|---|
| 399 | var back:DisplayObject = getSkinElement("background"); |
|---|
| 400 | var capLeft:DisplayObject = getSkinElement("capLeft"); |
|---|
| 401 | var capRight:DisplayObject = getSkinElement("capRight"); |
|---|
| 402 | //var shade:DisplayObject = getSkinElement("shade"); |
|---|
| 403 | |
|---|
| 404 | if (!back) { |
|---|
| 405 | var newBackground:Sprite = new Sprite(); |
|---|
| 406 | newBackground.name = "background"; |
|---|
| 407 | newBackground.graphics.beginFill(0, 0); |
|---|
| 408 | newBackground.graphics.drawRect(0, 0, 1, 1); |
|---|
| 409 | newBackground.graphics.endFill(); |
|---|
| 410 | back = newBackground as DisplayObject; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | if (!capLeft) { capLeft = new Sprite(); } |
|---|
| 414 | if (!capRight) { capRight = new Sprite(); } |
|---|
| 415 | |
|---|
| 416 | _bgColorSheet = new Sprite(); |
|---|
| 417 | if (backgroundColor) { |
|---|
| 418 | _bgColorSheet.graphics.beginFill(backgroundColor.color, 1); |
|---|
| 419 | _bgColorSheet.graphics.drawRect(0, 0, 1, 1); |
|---|
| 420 | _bgColorSheet.graphics.endFill(); |
|---|
| 421 | } |
|---|
| 422 | addChildAt(_bgColorSheet, 0); |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | _buttons['background'] = back; |
|---|
| 426 | addChild(back); |
|---|
| 427 | _height = back.height; |
|---|
| 428 | player.config.pluginConfig("controlbar")['size'] = back.height; |
|---|
| 429 | |
|---|
| 430 | if (capLeft) { |
|---|
| 431 | _buttons['capLeft'] = capLeft; |
|---|
| 432 | addChild(capLeft); |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | if (capRight) { |
|---|
| 436 | _buttons['capRight'] = capRight; |
|---|
| 437 | addChild(capRight); |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | /*if (shade) { |
|---|
| 441 | _buttons['shade'] = shade; |
|---|
| 442 | addChild(shade); |
|---|
| 443 | }*/ |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | private function setupDefaultButtons():void { |
|---|
| 448 | addComponentButton('play', ViewEvent.JWPLAYER_VIEW_PLAY); |
|---|
| 449 | addComponentButton('pause', ViewEvent.JWPLAYER_VIEW_PAUSE); |
|---|
| 450 | addComponentButton('prev', ViewEvent.JWPLAYER_VIEW_PREV); |
|---|
| 451 | addComponentButton('next', ViewEvent.JWPLAYER_VIEW_NEXT); |
|---|
| 452 | addComponentButton('stop', ViewEvent.JWPLAYER_VIEW_STOP); |
|---|
| 453 | addComponentButton('fullscreen', ViewEvent.JWPLAYER_VIEW_FULLSCREEN, true); |
|---|
| 454 | addComponentButton('normalscreen', ViewEvent.JWPLAYER_VIEW_FULLSCREEN, false); |
|---|
| 455 | addComponentButton('unmute', ViewEvent.JWPLAYER_VIEW_MUTE, false); |
|---|
| 456 | addComponentButton('mute', ViewEvent.JWPLAYER_VIEW_MUTE, true); |
|---|
| 457 | addTextField('elapsed'); |
|---|
| 458 | addTextField('duration'); |
|---|
| 459 | addSlider('time', ViewEvent.JWPLAYER_VIEW_CLICK, seekHandler); |
|---|
| 460 | _timeSlider = getSlider('time'); |
|---|
| 461 | addSlider('volume', ViewEvent.JWPLAYER_VIEW_CLICK, volumeHandler); |
|---|
| 462 | _volSlider = getSlider('volume'); |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | private function addComponentButton(name:String, event:String, eventData:*=null):void { |
|---|
| 467 | var button:ComponentButton = new ComponentButton(); |
|---|
| 468 | button.name = name; |
|---|
| 469 | button.setOutIcon(getSkinElement(name + "Button")); |
|---|
| 470 | button.setOverIcon(getSkinElement(name + "ButtonOver")); |
|---|
| 471 | button.setBackground(getSkinElement(name + "ButtonBack")); |
|---|
| 472 | button.tabEnabled = true; |
|---|
| 473 | button.clickFunction = function():void { |
|---|
| 474 | forward(new ViewEvent(event, eventData)); |
|---|
| 475 | } |
|---|
| 476 | if (getSkinElement(name + "Button") || getSkinElement(name + "ButtonOver") || getSkinElement(name + "ButtonBack")) { |
|---|
| 477 | button.init(); |
|---|
| 478 | addButtonDisplayObject(button, name); |
|---|
| 479 | } |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | private function addSlider(name:String, event:String, callback:Function, margin:Number=0):void { |
|---|
| 484 | try { |
|---|
| 485 | var slider:Slider = new Slider( |
|---|
| 486 | getSkinElement(name + "SliderRail"), |
|---|
| 487 | getSkinElement(name + "SliderBuffer"), |
|---|
| 488 | getSkinElement(name + "SliderProgress"), |
|---|
| 489 | getSkinElement(name + "SliderThumb"), |
|---|
| 490 | getSkinElement(name + "SliderCapLeft"), |
|---|
| 491 | getSkinElement(name + "SliderCapRight") |
|---|
| 492 | ); |
|---|
| 493 | slider.addEventListener(event, callback); |
|---|
| 494 | slider.name = name; |
|---|
| 495 | slider.tabEnabled = false; |
|---|
| 496 | _buttons[name] = slider; |
|---|
| 497 | } catch (e:Error) { |
|---|
| 498 | Logger.log("Could not create " + name + "slider"); |
|---|
| 499 | } |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | private function addTextField(name:String):void { |
|---|
| 504 | var textFormat:TextFormat = new TextFormat(); |
|---|
| 505 | |
|---|
| 506 | if (fontColor) { |
|---|
| 507 | textFormat.color = fontColor.color; |
|---|
| 508 | } else if (_player.config.frontcolor) { |
|---|
| 509 | textFormat.color = _player.config.frontcolor.color; |
|---|
| 510 | } |
|---|
| 511 | |
|---|
| 512 | textFormat.size = fontSize ? fontSize : 10; |
|---|
| 513 | textFormat.font = fontFace ? fontFace : "_sans"; |
|---|
| 514 | textFormat.bold = (!fontWeight || fontWeight == "bold"); |
|---|
| 515 | textFormat.italic = (fontStyle && fontStyle == "italic"); |
|---|
| 516 | |
|---|
| 517 | var textField:TextField = new TextField(); |
|---|
| 518 | textField.defaultTextFormat = textFormat; |
|---|
| 519 | textField.selectable = false; |
|---|
| 520 | textField.autoSize = TextFieldAutoSize.LEFT; |
|---|
| 521 | textField.name = 'text'; |
|---|
| 522 | |
|---|
| 523 | var textContainer:Sprite = new Sprite(); |
|---|
| 524 | textContainer.name = name; |
|---|
| 525 | |
|---|
| 526 | var textBackground:DisplayObject = getSkinElement(name + 'Background'); |
|---|
| 527 | if (textBackground) { |
|---|
| 528 | textBackground.name = 'back'; |
|---|
| 529 | textBackground.x = textBackground.y = 0; |
|---|
| 530 | textContainer.addChild(textBackground); |
|---|
| 531 | } |
|---|
| 532 | textContainer.addChild(textField); |
|---|
| 533 | addChild(textContainer); |
|---|
| 534 | _buttons[name] = textContainer; |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | private function forward(evt:ViewEvent):void { |
|---|
| 539 | dispatchEvent(evt); |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | private function volumeHandler(evt:ViewEvent):void { |
|---|
| 544 | var volume:Number = Math.round(evt.data * 100); |
|---|
| 545 | if (!_player.locked) { |
|---|
| 546 | var volumeEvent:MediaEvent = new MediaEvent(MediaEvent.JWPLAYER_MEDIA_VOLUME); |
|---|
| 547 | volumeEvent.volume = volume; |
|---|
| 548 | updateVolumeSlider(volumeEvent); |
|---|
| 549 | } |
|---|
| 550 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_VOLUME, volume)); |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | private function seekHandler(evt:ViewEvent):void { |
|---|
| 555 | var duration:Number = 0; |
|---|
| 556 | try { |
|---|
| 557 | duration = player.playlist.currentItem.duration; |
|---|
| 558 | } catch (err:Error) { |
|---|
| 559 | } |
|---|
| 560 | var percent:Number = Math.round(duration * evt.data); |
|---|
| 561 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_SEEK, percent)); |
|---|
| 562 | } |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | private function addButtonDisplayObject(icon:DisplayObject, name:String, handler:Function=null):MovieClip { |
|---|
| 566 | if (icon is ComponentButton) { |
|---|
| 567 | icon.name = name; |
|---|
| 568 | _buttons[name] = icon; |
|---|
| 569 | return icon as ComponentButton; |
|---|
| 570 | } else if (icon) { |
|---|
| 571 | var clipMC:MovieClip = new MovieClip(); |
|---|
| 572 | if (handler != null) { |
|---|
| 573 | clipMC.addEventListener(MouseEvent.CLICK, handler); |
|---|
| 574 | } |
|---|
| 575 | clipMC.name = name; |
|---|
| 576 | clipMC.addChild(icon); |
|---|
| 577 | _buttons[name] = clipMC; |
|---|
| 578 | return clipMC; |
|---|
| 579 | } |
|---|
| 580 | return null; |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | protected function get buttonColor():Color { |
|---|
| 584 | return getConfigParam("buttoncolor") ? new Color(String(getConfigParam("buttoncolor"))) : null; |
|---|
| 585 | } |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | public function addButton(icon:DisplayObject, name:String, handler:Function=null):MovieClip { |
|---|
| 589 | if (_customButtons.indexOf(name) < 0) { |
|---|
| 590 | _customButtons.push(name); |
|---|
| 591 | } |
|---|
| 592 | if (_removedButtons.indexOf(name) >= 0) { |
|---|
| 593 | _removedButtons.splice(_removedButtons.indexOf(name), 1); |
|---|
| 594 | } |
|---|
| 595 | icon.x = icon.y = 0; |
|---|
| 596 | var button:ComponentButton = new ComponentButton(); |
|---|
| 597 | button.name = name; |
|---|
| 598 | button.clickFunction = handler; |
|---|
| 599 | var outBackground:DisplayObject = getSkinElement("blankButton"); |
|---|
| 600 | if (outBackground) { |
|---|
| 601 | var outImage:Sprite = new Sprite(); |
|---|
| 602 | var outIcon:DisplayObject = icon; |
|---|
| 603 | if (buttonColor || _player.config.frontcolor){ |
|---|
| 604 | var outTransform:ColorTransform = new ColorTransform(); |
|---|
| 605 | outTransform.color = buttonColor ? buttonColor.color : _player.config.frontcolor.color; |
|---|
| 606 | outIcon.transform.colorTransform = outTransform; |
|---|
| 607 | } |
|---|
| 608 | var outOffset:Number = Math.round((outBackground.height - outIcon.height) / 2); |
|---|
| 609 | outBackground.width = outIcon.width + 2 * outOffset; |
|---|
| 610 | outImage.addChild(outBackground); |
|---|
| 611 | outImage.addChild(outIcon); |
|---|
| 612 | outIcon.x = outIcon.y = outOffset; |
|---|
| 613 | button.setOutIcon(outImage); |
|---|
| 614 | |
|---|
| 615 | button.init(); |
|---|
| 616 | return addButtonDisplayObject(button, name); |
|---|
| 617 | } |
|---|
| 618 | return null; |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | public function removeButton(name:String):void { |
|---|
| 623 | if (_buttons[name] is DisplayObject && this.contains(_buttons[name] as DisplayObject)) { |
|---|
| 624 | removeChild(_buttons[name]); |
|---|
| 625 | _buttons[name] = null; |
|---|
| 626 | _defaultLayout = removeButtonFromLayout(name, _defaultLayout); |
|---|
| 627 | _currentLayout = removeButtonFromLayout(name, _currentLayout); |
|---|
| 628 | if (_removedButtons.indexOf(name) < 0) { |
|---|
| 629 | _removedButtons.push(name); |
|---|
| 630 | } |
|---|
| 631 | redraw(); |
|---|
| 632 | } |
|---|
| 633 | } |
|---|
| 634 | |
|---|
| 635 | |
|---|
| 636 | private function hideButton(name:String):void { |
|---|
| 637 | if (_buttons[name]) { |
|---|
| 638 | _buttons[name].visible = false; |
|---|
| 639 | } |
|---|
| 640 | } |
|---|
| 641 | |
|---|
| 642 | |
|---|
| 643 | public function getButton(buttonName:String):DisplayObject { |
|---|
| 644 | if (_dividerElements[buttonName]) { |
|---|
| 645 | var divider:DisplayObject = getSkinElement(buttonName); |
|---|
| 646 | if (divider) { |
|---|
| 647 | _dividers.push(divider); |
|---|
| 648 | } |
|---|
| 649 | return divider; |
|---|
| 650 | } |
|---|
| 651 | return _buttons[buttonName]; |
|---|
| 652 | } |
|---|
| 653 | |
|---|
| 654 | private function getTextField(textName:String):TextField { |
|---|
| 655 | var textContainer:Sprite = getButton(textName) as Sprite; |
|---|
| 656 | if (textContainer) { |
|---|
| 657 | return textContainer.getChildByName('text') as TextField; |
|---|
| 658 | } |
|---|
| 659 | return null; |
|---|
| 660 | } |
|---|
| 661 | |
|---|
| 662 | |
|---|
| 663 | public function getSlider(sliderName:String):Slider { |
|---|
| 664 | return getButton(sliderName) as Slider; |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | public function resize(width:Number, height:Number):void { |
|---|
| 669 | if (getConfigParam('position') == "none") { |
|---|
| 670 | visible = false; |
|---|
| 671 | return; |
|---|
| 672 | } |
|---|
| 673 | |
|---|
| 674 | _width = width; |
|---|
| 675 | |
|---|
| 676 | if (getConfigParam('position') == 'over' || _player.config.fullscreen == true) { |
|---|
| 677 | x = getConfigParam('margin') + player.config.pluginConfig('display')['x']; |
|---|
| 678 | y = height - background.height - getConfigParam('margin') + player.config.pluginConfig('display')['y']; |
|---|
| 679 | _width = width - 2 * getConfigParam('margin'); |
|---|
| 680 | } |
|---|
| 681 | |
|---|
| 682 | //shade.width = _width; |
|---|
| 683 | |
|---|
| 684 | var backgroundWidth:Number = _width; |
|---|
| 685 | |
|---|
| 686 | backgroundWidth -= capLeft.width; |
|---|
| 687 | capLeft.x = 0; |
|---|
| 688 | |
|---|
| 689 | backgroundWidth -= capRight.width; |
|---|
| 690 | capRight.x = _width - capRight.width; |
|---|
| 691 | |
|---|
| 692 | background.width = backgroundWidth; |
|---|
| 693 | background.x = capLeft.width; |
|---|
| 694 | setChildIndex(capLeft, numChildren - 1); |
|---|
| 695 | setChildIndex(capRight, numChildren - 1); |
|---|
| 696 | |
|---|
| 697 | _bgColorSheet.width = _width; |
|---|
| 698 | _bgColorSheet.height = background.height; |
|---|
| 699 | |
|---|
| 700 | if (_fullscreen && !_player.config.fullscreen) { |
|---|
| 701 | stopFader(); |
|---|
| 702 | } |
|---|
| 703 | _fullscreen = _player.config.fullscreen; |
|---|
| 704 | stateHandler(); |
|---|
| 705 | redraw(); |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | private function redraw():void { |
|---|
| 710 | clearDividers(); |
|---|
| 711 | alignTextFields(); |
|---|
| 712 | _layoutManager.resize(_width, _height); |
|---|
| 713 | } |
|---|
| 714 | |
|---|
| 715 | |
|---|
| 716 | private function clearDividers():void { |
|---|
| 717 | for (var i:Number = 0; i < _dividers.length; i++) { |
|---|
| 718 | _dividers[i].visible = false; |
|---|
| 719 | _dividers[i] = null; |
|---|
| 720 | } |
|---|
| 721 | _dividers = []; |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | private function alignTextFields():void { |
|---|
| 725 | for each(var fieldName:String in ['elapsed','duration']) { |
|---|
| 726 | var textContainer:Sprite = getButton(fieldName) as Sprite; |
|---|
| 727 | var textField:DisplayObject = textContainer.getChildByName('text'); |
|---|
| 728 | var textBackground:DisplayObject = textContainer.getChildByName('back'); |
|---|
| 729 | |
|---|
| 730 | if (textField && textBackground) { |
|---|
| 731 | if (textField.width > textBackground.width) { |
|---|
| 732 | textField.x = 0; |
|---|
| 733 | textBackground.x = (textField.width - textBackground.width) / 2; |
|---|
| 734 | } else { |
|---|
| 735 | textBackground.x = 0; |
|---|
| 736 | textField.x = (textBackground.width - textField.width) / 2; |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | if (textField.height > textBackground.height) { |
|---|
| 740 | textField.y = 0; |
|---|
| 741 | textBackground.y = (textField.height - textBackground.height) / 2; |
|---|
| 742 | } else { |
|---|
| 743 | textBackground.y = 0; |
|---|
| 744 | textField.y = (textBackground.height - textField.height) / 2; |
|---|
| 745 | } |
|---|
| 746 | } |
|---|
| 747 | } |
|---|
| 748 | } |
|---|
| 749 | |
|---|
| 750 | |
|---|
| 751 | public function get layout():String { |
|---|
| 752 | return _currentLayout.replace(/\|/g, "<divider>"); |
|---|
| 753 | } |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | private function get background():DisplayObject { |
|---|
| 757 | if (_buttons['background']) { |
|---|
| 758 | return _buttons['background']; |
|---|
| 759 | } |
|---|
| 760 | return (new Sprite()); |
|---|
| 761 | } |
|---|
| 762 | |
|---|
| 763 | |
|---|
| 764 | private function get capLeft():DisplayObject { |
|---|
| 765 | if (_buttons['capLeft']) { |
|---|
| 766 | return _buttons['capLeft']; |
|---|
| 767 | } |
|---|
| 768 | return (new Sprite()); |
|---|
| 769 | } |
|---|
| 770 | |
|---|
| 771 | |
|---|
| 772 | private function get capRight():DisplayObject { |
|---|
| 773 | if (_buttons['capRight']) { |
|---|
| 774 | return _buttons['capRight']; |
|---|
| 775 | } |
|---|
| 776 | return (new Sprite()); |
|---|
| 777 | } |
|---|
| 778 | } |
|---|
| 779 | } |
|---|