| 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 _pluginsLayer:MovieClip; |
|---|
| 50 | |
|---|
| 51 | private var _image:Loader; |
|---|
| 52 | |
|---|
| 53 | public function View(player:Player, model:Model) { |
|---|
| 54 | _player = player; |
|---|
| 55 | _model = model; |
|---|
| 56 | |
|---|
| 57 | _root = new MovieClip(); |
|---|
| 58 | RootReference.stage.addChildAt(_root, 0); |
|---|
| 59 | |
|---|
| 60 | setupLayers(); |
|---|
| 61 | RootReference.stage.scaleMode = StageScaleMode.NO_SCALE; |
|---|
| 62 | RootReference.stage.stage.align = StageAlign.TOP_LEFT; |
|---|
| 63 | RootReference.stage.addEventListener(Event.FULLSCREEN, resizeHandler); |
|---|
| 64 | RootReference.stage.addEventListener(Event.RESIZE, resizeHandler); |
|---|
| 65 | _model.addEventListener(MediaEvent.JWPLAYER_MEDIA_LOADED, mediaLoaded); |
|---|
| 66 | _model.playlist.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, itemHandler); |
|---|
| 67 | _model.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler); |
|---|
| 68 | |
|---|
| 69 | var menu:RightclickMenu = new RightclickMenu(model, _root); |
|---|
| 70 | menu.addGlobalListener(forward); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | private function setupLayers():void { |
|---|
| 74 | _backgroundLayer = setupLayer("background", 0); |
|---|
| 75 | var background:MovieClip = new MovieClip(); |
|---|
| 76 | background.name = "background"; |
|---|
| 77 | _backgroundLayer.addChildAt(background, 0); |
|---|
| 78 | background.graphics.beginFill(_player.config.backcolor, 1); |
|---|
| 79 | background.graphics.drawRect(0, 0, 1, 1); |
|---|
| 80 | background.graphics.endFill(); |
|---|
| 81 | |
|---|
| 82 | _mediaLayer = setupLayer("media", 1); |
|---|
| 83 | |
|---|
| 84 | _imageLayer = setupLayer("image", 2); |
|---|
| 85 | _image = new Loader(); |
|---|
| 86 | _image.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, imageError); |
|---|
| 87 | _image.contentLoaderInfo.addEventListener(Event.COMPLETE, imageComplete); |
|---|
| 88 | |
|---|
| 89 | _componentsLayer = setupLayer("components", 3); |
|---|
| 90 | _pluginsLayer = setupLayer("plugins", 4); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | private function setupLayer(name:String, index:Number):MovieClip { |
|---|
| 94 | var layer:MovieClip = new MovieClip(); |
|---|
| 95 | _root.addChildAt(layer, index); |
|---|
| 96 | layer.name = name; |
|---|
| 97 | layer.x = 0; |
|---|
| 98 | layer.y = 0; |
|---|
| 99 | return layer; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | private function resizeHandler(event:Event):void { |
|---|
| 103 | var width:Number = RootReference.stage.stageWidth; |
|---|
| 104 | var height:Number = RootReference.stage.stageHeight; |
|---|
| 105 | _backgroundLayer.getChildByName("background").width = width; |
|---|
| 106 | _backgroundLayer.getChildByName("background").height = height; |
|---|
| 107 | |
|---|
| 108 | var layoutManager:PlayerLayoutManager = new PlayerLayoutManager(_player); |
|---|
| 109 | layoutManager.resize(width, height); |
|---|
| 110 | |
|---|
| 111 | redraw(); |
|---|
| 112 | |
|---|
| 113 | var currentFSMode:Boolean = (RootReference.stage.displayState == StageDisplayState.FULL_SCREEN); |
|---|
| 114 | if (_model.fullscreen != currentFSMode) { |
|---|
| 115 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_FULLSCREEN, currentFSMode)); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | public function set skin(skn:ISkin):void { |
|---|
| 120 | _skin = skn; |
|---|
| 121 | if (!_components) { |
|---|
| 122 | setupComponents(); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | //TODO: I think plugins and components have to go on the same level, otherwise the component layer will simply go over |
|---|
| 127 | private function setupComponents():void { |
|---|
| 128 | _components = new PlayerComponents(_player); |
|---|
| 129 | |
|---|
| 130 | setupComponent(_components.display, 0); |
|---|
| 131 | setupComponent(_components.controlbar, 1); |
|---|
| 132 | setupComponent(_components.playlist, 2); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | private function setupComponent(component:IPlayerComponent, index:Number):void { |
|---|
| 136 | component.addGlobalListener(forward); |
|---|
| 137 | _componentsLayer.addChildAt(component as DisplayObject, index); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | public function get skin():ISkin { |
|---|
| 141 | return _skin; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | public function fullscreen(mode:Boolean=true):void { |
|---|
| 145 | RootReference.stage.displayState = mode ? StageDisplayState.FULL_SCREEN : StageDisplayState.NORMAL; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /** Redraws the plugins **/ |
|---|
| 149 | public function redraw():void { |
|---|
| 150 | _components.resize(_player.config.width, _player.config.height); |
|---|
| 151 | |
|---|
| 152 | if (_imageLayer.numChildren) { |
|---|
| 153 | _imageLayer.x = _components.display.x; |
|---|
| 154 | _imageLayer.y = _components.display.y; |
|---|
| 155 | Stretcher.stretch(_image, _player.config.width, _player.config.height, _player.config.stretching); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | if (_mediaLayer.numChildren) { |
|---|
| 159 | _mediaLayer.x = _components.display.x; |
|---|
| 160 | _mediaLayer.y = _components.display.y; |
|---|
| 161 | _model.media.resize(_player.config.width, _player.config.height); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | for (var i:Number = 0; i < _pluginsLayer.numChildren; i++) { |
|---|
| 165 | var plug:IPlugin = _pluginsLayer.getChildAt(i) as IPlugin; |
|---|
| 166 | if (plug) { |
|---|
| 167 | var cfg:PluginConfig = _player.config.pluginConfig((plug as DisplayObject).name); |
|---|
| 168 | plug.resize(cfg.width, cfg.height); |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | PlayerV4Emulation.getInstance().resize(_player.config.width, _player.config.height); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | public function get components():PlayerComponents { |
|---|
| 175 | return _components; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | public function overrideComponent(newComponent:IPlayerComponent):void { |
|---|
| 179 | if (newComponent is IControlbarComponent) { |
|---|
| 180 | // Replace controlbar |
|---|
| 181 | } else if (newComponent is IDisplayComponent) { |
|---|
| 182 | // Replace display |
|---|
| 183 | } else if (newComponent is IDockComponent) { |
|---|
| 184 | // Replace dock |
|---|
| 185 | } else if (newComponent is IPlaylistComponent) { |
|---|
| 186 | // Replace playlist |
|---|
| 187 | } else { |
|---|
| 188 | throw(new Error("Component must implement a component interface")); |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | public function addPlugin(name:String, plugin:IPlugin):void { |
|---|
| 193 | try { |
|---|
| 194 | var plugDO:DisplayObject = plugin as DisplayObject; |
|---|
| 195 | if (_pluginsLayer.getChildByName(name) == null && plugDO != null) { |
|---|
| 196 | plugDO.name = name; |
|---|
| 197 | _pluginsLayer.addChild(plugDO); |
|---|
| 198 | _pluginsLayer[name] = plugDO; |
|---|
| 199 | } |
|---|
| 200 | } catch (e:Error) { |
|---|
| 201 | dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, e.message)); |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | public function loadedPlugins():Array { |
|---|
| 206 | var list:Array = []; |
|---|
| 207 | for each (var plugin:DisplayObject in _pluginsLayer) { |
|---|
| 208 | if (plugin is IPlugin) { |
|---|
| 209 | list.push(plugin.name); |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | return list; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | public function getPlugin(name:String):IPlugin { |
|---|
| 216 | return _pluginsLayer.getChildByName(name) as IPlugin; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | private function mediaLoaded(evt:MediaEvent):void { |
|---|
| 220 | while (_mediaLayer.numChildren) { |
|---|
| 221 | _mediaLayer.removeChildAt(0); |
|---|
| 222 | } |
|---|
| 223 | _model.media.resize(_player.config.width, _player.config.height); |
|---|
| 224 | |
|---|
| 225 | _mediaLayer.x = _components.display.x; |
|---|
| 226 | _mediaLayer.y = _components.display.y; |
|---|
| 227 | _mediaLayer.addChild(_model.media.display); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | private function itemHandler(evt:PlaylistEvent):void { |
|---|
| 231 | if (_model.playlist.currentItem && _model.playlist.currentItem.image) { |
|---|
| 232 | loadImage(_model.playlist.currentItem.image); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | private function loadImage(url:String):void { |
|---|
| 237 | _image.load(new URLRequest(url)); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | private function imageComplete(evt:Event):void { |
|---|
| 241 | while (_imageLayer.numChildren) { |
|---|
| 242 | _imageLayer.removeChildAt(0); |
|---|
| 243 | } |
|---|
| 244 | _imageLayer.addChild(_image); |
|---|
| 245 | _imageLayer.x = _components.display.x; |
|---|
| 246 | _imageLayer.y = _components.display.y; |
|---|
| 247 | Stretcher.stretch(_image, _player.config.width, _player.config.height, _player.config.stretching); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | private function imageError(evt:IOErrorEvent):void { |
|---|
| 251 | _image = null; |
|---|
| 252 | dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, evt.text)); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | private function stateHandler(evt:PlayerStateEvent):void { |
|---|
| 256 | switch (evt.newstate) { |
|---|
| 257 | case PlayerState.IDLE: |
|---|
| 258 | _mediaLayer.visible = false; |
|---|
| 259 | _imageLayer.visible = true; |
|---|
| 260 | break; |
|---|
| 261 | case PlayerState.PLAYING: |
|---|
| 262 | _mediaLayer.visible = true; |
|---|
| 263 | _imageLayer.visible = false; |
|---|
| 264 | break; |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | private function forward(evt:Event):void { |
|---|
| 269 | if (evt is PlayerEvent) dispatchEvent(evt); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | } |
|---|
| 273 | } |
|---|