| 1 | package com.longtailvideo.jwplayer.view { |
|---|
| 2 | import com.longtailvideo.jwplayer.events.GlobalEventDispatcher; |
|---|
| 3 | import com.longtailvideo.jwplayer.events.MediaEvent; |
|---|
| 4 | import com.longtailvideo.jwplayer.events.PlayerEvent; |
|---|
| 5 | import com.longtailvideo.jwplayer.events.PlayerStateEvent; |
|---|
| 6 | import com.longtailvideo.jwplayer.events.PlaylistEvent; |
|---|
| 7 | import com.longtailvideo.jwplayer.events.ViewEvent; |
|---|
| 8 | import com.longtailvideo.jwplayer.model.Model; |
|---|
| 9 | import com.longtailvideo.jwplayer.player.IPlayer; |
|---|
| 10 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 11 | import com.longtailvideo.jwplayer.player.PlayerV4Emulation; |
|---|
| 12 | import com.longtailvideo.jwplayer.plugins.IPlugin; |
|---|
| 13 | import com.longtailvideo.jwplayer.plugins.PluginConfig; |
|---|
| 14 | import com.longtailvideo.jwplayer.utils.RootReference; |
|---|
| 15 | import com.longtailvideo.jwplayer.utils.Stretcher; |
|---|
| 16 | import com.longtailvideo.jwplayer.view.interfaces.IControlbarComponent; |
|---|
| 17 | import com.longtailvideo.jwplayer.view.interfaces.IDisplayComponent; |
|---|
| 18 | import com.longtailvideo.jwplayer.view.interfaces.IDockComponent; |
|---|
| 19 | import com.longtailvideo.jwplayer.view.interfaces.IPlayerComponent; |
|---|
| 20 | import com.longtailvideo.jwplayer.view.interfaces.IPlaylistComponent; |
|---|
| 21 | import com.longtailvideo.jwplayer.view.interfaces.ISkin; |
|---|
| 22 | |
|---|
| 23 | import flash.display.DisplayObject; |
|---|
| 24 | import flash.display.Loader; |
|---|
| 25 | import flash.display.MovieClip; |
|---|
| 26 | import flash.display.Sprite; |
|---|
| 27 | import flash.display.Stage; |
|---|
| 28 | import flash.display.StageAlign; |
|---|
| 29 | import flash.display.StageDisplayState; |
|---|
| 30 | import flash.display.StageScaleMode; |
|---|
| 31 | import flash.events.ErrorEvent; |
|---|
| 32 | import flash.events.Event; |
|---|
| 33 | import flash.events.IOErrorEvent; |
|---|
| 34 | import flash.net.URLRequest; |
|---|
| 35 | |
|---|
| 36 | public class View extends GlobalEventDispatcher { |
|---|
| 37 | private var _player:IPlayer; |
|---|
| 38 | private var _model:Model; |
|---|
| 39 | private var _skin:ISkin; |
|---|
| 40 | private var _components:IPlayerComponents; |
|---|
| 41 | private var _fullscreen:Boolean = false; |
|---|
| 42 | private var stage:Stage; |
|---|
| 43 | |
|---|
| 44 | private var _root:MovieClip; |
|---|
| 45 | |
|---|
| 46 | private var _backgroundLayer:MovieClip; |
|---|
| 47 | private var _mediaLayer:MovieClip; |
|---|
| 48 | private var _imageLayer:MovieClip; |
|---|
| 49 | private var _componentsLayer:MovieClip; |
|---|
| 50 | private var _logoLayer:MovieClip; |
|---|
| 51 | private var _pluginsLayer:MovieClip; |
|---|
| 52 | private var _plugins:Object; |
|---|
| 53 | |
|---|
| 54 | private var _displayMasker:MovieClip; |
|---|
| 55 | |
|---|
| 56 | private var _image:Loader; |
|---|
| 57 | private var _logo:Logo; |
|---|
| 58 | |
|---|
| 59 | private var layoutManager:PlayerLayoutManager; |
|---|
| 60 | |
|---|
| 61 | [Embed(source="../../../../../assets/flash/loader/loader.swf")] |
|---|
| 62 | private var LoadingScreen:Class; |
|---|
| 63 | |
|---|
| 64 | [Embed(source="../../../../../assets/flash/loader/error.swf")] |
|---|
| 65 | private var ErrorScreen:Class; |
|---|
| 66 | |
|---|
| 67 | private var loaderScreen:Sprite; |
|---|
| 68 | private var loaderAnim:DisplayObject; |
|---|
| 69 | |
|---|
| 70 | public function View(player:IPlayer, model:Model) { |
|---|
| 71 | _player = player; |
|---|
| 72 | _model = model; |
|---|
| 73 | |
|---|
| 74 | RootReference.stage.scaleMode = StageScaleMode.NO_SCALE; |
|---|
| 75 | RootReference.stage.stage.align = StageAlign.TOP_LEFT; |
|---|
| 76 | |
|---|
| 77 | loaderScreen = new Sprite(); |
|---|
| 78 | loaderScreen.name = 'loaderScreen'; |
|---|
| 79 | |
|---|
| 80 | loaderAnim = new LoadingScreen() as DisplayObject; |
|---|
| 81 | loaderScreen.addChild(loaderAnim); |
|---|
| 82 | |
|---|
| 83 | RootReference.stage.addChildAt(loaderScreen, 0); |
|---|
| 84 | |
|---|
| 85 | if (RootReference.stage.stageWidth > 0) { |
|---|
| 86 | resizeStage(); |
|---|
| 87 | } else { |
|---|
| 88 | RootReference.stage.addEventListener(Event.RESIZE, resizeStage); |
|---|
| 89 | RootReference.stage.addEventListener(Event.ADDED_TO_STAGE, resizeStage); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | _root = new MovieClip(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | private function resizeStage(evt:Event=null):void { |
|---|
| 96 | RootReference.stage.removeEventListener(Event.RESIZE, resizeStage); |
|---|
| 97 | RootReference.stage.removeEventListener(Event.ADDED_TO_STAGE, resizeStage); |
|---|
| 98 | |
|---|
| 99 | loaderScreen.graphics.clear(); |
|---|
| 100 | loaderScreen.graphics.beginFill(0, 1); |
|---|
| 101 | loaderScreen.graphics.drawRect(0, 0, RootReference.stage.stageWidth, RootReference.stage.stageHeight); |
|---|
| 102 | loaderScreen.graphics.endFill(); |
|---|
| 103 | |
|---|
| 104 | loaderAnim.x = (RootReference.stage.stageWidth - loaderAnim.width) / 2; |
|---|
| 105 | loaderAnim.y = (RootReference.stage.stageHeight - loaderAnim.height) / 2; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | public function get skin():ISkin { |
|---|
| 109 | return _skin; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | public function set skin(skn:ISkin):void { |
|---|
| 114 | _skin = skn; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | public function setupView():void { |
|---|
| 118 | setupLayers(); |
|---|
| 119 | setupComponents(); |
|---|
| 120 | |
|---|
| 121 | RootReference.stage.addEventListener(Event.FULLSCREEN, resizeHandler); |
|---|
| 122 | RootReference.stage.addEventListener(Event.RESIZE, resizeHandler); |
|---|
| 123 | |
|---|
| 124 | _model.addEventListener(MediaEvent.JWPLAYER_MEDIA_LOADED, mediaLoaded); |
|---|
| 125 | _model.playlist.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, itemHandler); |
|---|
| 126 | _model.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler); |
|---|
| 127 | |
|---|
| 128 | layoutManager = new PlayerLayoutManager(_player); |
|---|
| 129 | var menu:RightclickMenu = new RightclickMenu(_model, _root); |
|---|
| 130 | menu.addGlobalListener(forward); |
|---|
| 131 | |
|---|
| 132 | redraw(); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | public function completeView(isError:Boolean=false, errorMsg:String=""):void { |
|---|
| 136 | if (!isError) { |
|---|
| 137 | RootReference.stage.removeChild(loaderScreen); |
|---|
| 138 | RootReference.stage.addChildAt(_root, 0); |
|---|
| 139 | } else { |
|---|
| 140 | loaderScreen.removeChild(loaderAnim); |
|---|
| 141 | var errorScreen:DisplayObject = new ErrorScreen() as DisplayObject; |
|---|
| 142 | errorScreen.x = (loaderScreen.width - errorScreen.width) / 2; |
|---|
| 143 | errorScreen.y = (loaderScreen.height - errorScreen.height) / 2; |
|---|
| 144 | loaderScreen.addChild(errorScreen); |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | private function setupLayers():void { |
|---|
| 149 | _backgroundLayer = setupLayer("background", 0); |
|---|
| 150 | setupBackground(); |
|---|
| 151 | |
|---|
| 152 | _mediaLayer = setupLayer("media", 1); |
|---|
| 153 | _mediaLayer.visible = false; |
|---|
| 154 | |
|---|
| 155 | _imageLayer = setupLayer("image", 2); |
|---|
| 156 | _image = new Loader(); |
|---|
| 157 | |
|---|
| 158 | _componentsLayer = setupLayer("components", 3); |
|---|
| 159 | |
|---|
| 160 | _pluginsLayer = setupLayer("plugins", 4); |
|---|
| 161 | _plugins = {}; |
|---|
| 162 | |
|---|
| 163 | _logoLayer = setupLayer("logo", 5); |
|---|
| 164 | _logo = new Logo(_player); |
|---|
| 165 | _logoLayer.addChild(_logo); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | private function setupLayer(name:String, index:Number):MovieClip { |
|---|
| 169 | var layer:MovieClip = new MovieClip(); |
|---|
| 170 | _root.addChildAt(layer, index); |
|---|
| 171 | layer.name = name; |
|---|
| 172 | layer.x = 0; |
|---|
| 173 | layer.y = 0; |
|---|
| 174 | return layer; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | private function setupBackground():void { |
|---|
| 178 | var background:MovieClip = new MovieClip(); |
|---|
| 179 | background.name = "background"; |
|---|
| 180 | _backgroundLayer.addChild(background); |
|---|
| 181 | background.graphics.beginFill(_player.config.screencolor ? _player.config.screencolor.color : 0x000000, 1); |
|---|
| 182 | background.graphics.drawRect(0, 0, 1, 1); |
|---|
| 183 | background.graphics.endFill(); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | private function setupDisplayMask():void { |
|---|
| 187 | _displayMasker = new MovieClip(); |
|---|
| 188 | _displayMasker.graphics.beginFill(0x000000, 1); |
|---|
| 189 | _displayMasker.graphics.drawRect(0, 0, _player.config.width, _player.config.height); |
|---|
| 190 | _displayMasker.graphics.endFill(); |
|---|
| 191 | |
|---|
| 192 | _backgroundLayer.mask = _displayMasker; |
|---|
| 193 | _imageLayer.mask = _displayMasker; |
|---|
| 194 | _mediaLayer.mask = _displayMasker; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | private function setupComponents():void { |
|---|
| 198 | _components = new PlayerComponents(_player); |
|---|
| 199 | |
|---|
| 200 | setupComponent(_components.display, 0); |
|---|
| 201 | setupComponent(_components.playlist, 1); |
|---|
| 202 | setupComponent(_components.controlbar, 2); |
|---|
| 203 | setupComponent(_components.dock, 3); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | private function setupComponent(component:IPlayerComponent, index:Number):void { |
|---|
| 207 | component.addGlobalListener(forward); |
|---|
| 208 | _componentsLayer.addChildAt(component as DisplayObject, index); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | private function resizeHandler(event:Event):void { |
|---|
| 213 | redraw(); |
|---|
| 214 | |
|---|
| 215 | var currentFSMode:Boolean = (RootReference.stage.displayState == StageDisplayState.FULL_SCREEN); |
|---|
| 216 | if (_model.fullscreen != currentFSMode) { |
|---|
| 217 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_FULLSCREEN, currentFSMode)); |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | public function fullscreen(mode:Boolean=true):void { |
|---|
| 223 | RootReference.stage.displayState = mode ? StageDisplayState.FULL_SCREEN : StageDisplayState.NORMAL; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | /** Redraws the plugins and player components **/ |
|---|
| 227 | public function redraw():void { |
|---|
| 228 | layoutManager.resize(RootReference.stage.stageWidth, RootReference.stage.stageHeight); |
|---|
| 229 | |
|---|
| 230 | _components.resize(_player.config.width, _player.config.height); |
|---|
| 231 | |
|---|
| 232 | resizeBackground(); |
|---|
| 233 | resizeMasker(); |
|---|
| 234 | |
|---|
| 235 | if (_imageLayer.numChildren) { |
|---|
| 236 | _imageLayer.x = _components.display.x; |
|---|
| 237 | _imageLayer.y = _components.display.y; |
|---|
| 238 | Stretcher.stretch(_image, _player.config.width, _player.config.height, _player.config.stretching); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | if (_mediaLayer.numChildren && _model.media.display) { |
|---|
| 242 | _mediaLayer.x = _components.display.x; |
|---|
| 243 | _mediaLayer.y = _components.display.y; |
|---|
| 244 | _model.media.resize(_player.config.width, _player.config.height); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | if (_logoLayer.numChildren) { |
|---|
| 248 | _logoLayer.x = _components.display.x; |
|---|
| 249 | _logoLayer.y = _components.display.y; |
|---|
| 250 | _logo.resize(_player.config.width, _player.config.height); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | for (var i:Number = 0; i < _pluginsLayer.numChildren; i++) { |
|---|
| 254 | var plug:IPlugin = _pluginsLayer.getChildAt(i) as IPlugin; |
|---|
| 255 | var plugDisplay:DisplayObject = plug as DisplayObject; |
|---|
| 256 | if (plug && plugDisplay) { |
|---|
| 257 | var cfg:PluginConfig = _player.config.pluginConfig(plug.id); |
|---|
| 258 | if (cfg['visible']) { |
|---|
| 259 | plugDisplay.visible = true; |
|---|
| 260 | plugDisplay.x = cfg['x']; |
|---|
| 261 | plugDisplay.y = cfg['y']; |
|---|
| 262 | plug.resize(cfg.width, cfg.height); |
|---|
| 263 | } else { |
|---|
| 264 | plugDisplay.visible = false; |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | PlayerV4Emulation.getInstance(_player).resize(_player.config.width, _player.config.height); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | private function resizeBackground():void { |
|---|
| 272 | var bg:DisplayObject = _backgroundLayer.getChildByName("background"); |
|---|
| 273 | bg.width = _player.config.width; |
|---|
| 274 | bg.height = _player.config.height; |
|---|
| 275 | bg.x = _components.display.x; |
|---|
| 276 | bg.y = _components.display.y; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | private function resizeMasker():void { |
|---|
| 280 | if (_displayMasker == null) setupDisplayMask(); |
|---|
| 281 | |
|---|
| 282 | _displayMasker.graphics.clear(); |
|---|
| 283 | _displayMasker.graphics.beginFill(0, 1); |
|---|
| 284 | _displayMasker.graphics.drawRect(_components.display.x, _components.display.y, _player.config.width, _player.config.height); |
|---|
| 285 | _displayMasker.graphics.endFill(); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | public function get components():IPlayerComponents { |
|---|
| 289 | return _components; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | public function overrideComponent(newComponent:IPlayerComponent):void { |
|---|
| 293 | if (newComponent is IControlbarComponent) { |
|---|
| 294 | // Replace controlbar |
|---|
| 295 | } else if (newComponent is IDisplayComponent) { |
|---|
| 296 | // Replace display |
|---|
| 297 | } else if (newComponent is IDockComponent) { |
|---|
| 298 | // Replace dock |
|---|
| 299 | } else if (newComponent is IPlaylistComponent) { |
|---|
| 300 | // Replace playlist |
|---|
| 301 | } else { |
|---|
| 302 | dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, "Component must implement a component interface")); |
|---|
| 303 | } |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | public function addPlugin(id:String, plugin:IPlugin):void { |
|---|
| 307 | try { |
|---|
| 308 | var plugDO:DisplayObject = plugin as DisplayObject; |
|---|
| 309 | if (!_plugins[id] && plugDO != null) { |
|---|
| 310 | _plugins[id] = plugDO; |
|---|
| 311 | _pluginsLayer.addChild(plugDO); |
|---|
| 312 | //_pluginsLayer[id] = plugDO; |
|---|
| 313 | } |
|---|
| 314 | } catch (e:Error) { |
|---|
| 315 | dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, e.message)); |
|---|
| 316 | } |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | public function removePlugin(plugin:IPlugin):void { |
|---|
| 320 | var id:String = plugin.id; |
|---|
| 321 | if (id && _plugins[id] is IPlugin) { |
|---|
| 322 | _pluginsLayer.removeChild(_plugins[id]); |
|---|
| 323 | delete _plugins[id]; |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | public function loadedPlugins():Array { |
|---|
| 329 | var list:Array = []; |
|---|
| 330 | for (var pluginId:String in _plugins) { |
|---|
| 331 | if (_plugins[pluginId] is IPlugin) { |
|---|
| 332 | list.push(pluginId); |
|---|
| 333 | } |
|---|
| 334 | } |
|---|
| 335 | return list; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | public function getPlugin(id:String):IPlugin { |
|---|
| 339 | return _plugins[id] as IPlugin; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | public function bringPluginToFront(id:String):void { |
|---|
| 343 | var plugin:IPlugin = getPlugin(id); |
|---|
| 344 | _pluginsLayer.setChildIndex(plugin as DisplayObject, _pluginsLayer.numChildren - 1); |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | private function mediaLoaded(evt:MediaEvent):void { |
|---|
| 348 | _mediaLayer.x = _components.display.x; |
|---|
| 349 | _mediaLayer.y = _components.display.y; |
|---|
| 350 | if (_model.media.display) { |
|---|
| 351 | _model.media.resize(_player.config.width, _player.config.height); |
|---|
| 352 | _mediaLayer.addChild(_model.media.display); |
|---|
| 353 | } |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | private function itemHandler(evt:PlaylistEvent):void { |
|---|
| 357 | while (_mediaLayer.numChildren) { |
|---|
| 358 | _mediaLayer.removeChildAt(0); |
|---|
| 359 | } |
|---|
| 360 | if (_model.playlist.currentItem && _model.playlist.currentItem.image) { |
|---|
| 361 | loadImage(_model.playlist.currentItem.image); |
|---|
| 362 | |
|---|
| 363 | } |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | private function loadImage(url:String):void { |
|---|
| 367 | while (_imageLayer.numChildren) { |
|---|
| 368 | _imageLayer.removeChildAt(0); |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | _image = new Loader(); |
|---|
| 372 | _image.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, imageError); |
|---|
| 373 | _image.contentLoaderInfo.addEventListener(Event.COMPLETE, imageComplete); |
|---|
| 374 | _image.load(new URLRequest(url)); |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | private function imageComplete(evt:Event):void { |
|---|
| 378 | _imageLayer.addChild(_image); |
|---|
| 379 | _imageLayer.x = _components.display.x; |
|---|
| 380 | _imageLayer.y = _components.display.y; |
|---|
| 381 | Stretcher.stretch(_image, _player.config.width, _player.config.height, _player.config.stretching); |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | private function imageError(evt:IOErrorEvent):void { |
|---|
| 385 | _image = null; |
|---|
| 386 | dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, evt.text)); |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | private function stateHandler(evt:PlayerStateEvent):void { |
|---|
| 390 | switch (evt.newstate) { |
|---|
| 391 | case PlayerState.IDLE: |
|---|
| 392 | _imageLayer.visible = true; |
|---|
| 393 | _mediaLayer.visible = false; |
|---|
| 394 | _logoLayer.visible = false; |
|---|
| 395 | break; |
|---|
| 396 | case PlayerState.PLAYING: |
|---|
| 397 | if (_model.media.display) { |
|---|
| 398 | _imageLayer.visible = false; |
|---|
| 399 | _mediaLayer.visible = true; |
|---|
| 400 | } |
|---|
| 401 | _logoLayer.visible = true; |
|---|
| 402 | break; |
|---|
| 403 | } |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | private function forward(evt:Event):void { |
|---|
| 407 | if (evt is PlayerEvent) dispatchEvent(evt); |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | } |
|---|
| 411 | } |
|---|