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