| 1 | /** |
|---|
| 2 | * Loads external SWF skins and plugins. |
|---|
| 3 | **/ |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | package com.jeroenwijering.player { |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | import com.jeroenwijering.events.SPLoaderEvent; |
|---|
| 10 | import com.jeroenwijering.utils.Draw; |
|---|
| 11 | import com.jeroenwijering.utils.Strings; |
|---|
| 12 | |
|---|
| 13 | import flash.display.Loader; |
|---|
| 14 | import flash.display.MovieClip; |
|---|
| 15 | import flash.events.Event; |
|---|
| 16 | import flash.events.EventDispatcher; |
|---|
| 17 | import flash.events.IOErrorEvent; |
|---|
| 18 | import flash.net.URLRequest; |
|---|
| 19 | import flash.system.*; |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | public class SPLoader extends EventDispatcher { |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | /** Reference to the player itself. **/ |
|---|
| 26 | private var player:MovieClip; |
|---|
| 27 | /** SWF loader reference **/ |
|---|
| 28 | private var loader:Loader; |
|---|
| 29 | /** Number of plugns that are done loading. **/ |
|---|
| 30 | private var done:Number; |
|---|
| 31 | /** Reference to all the plugin objects. **/ |
|---|
| 32 | private var plugins:Array; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /** Constructor, references player and gets plugin basedir. **/ |
|---|
| 36 | public function SPLoader(ply:MovieClip):void { |
|---|
| 37 | player = ply; |
|---|
| 38 | plugins = new Array(); |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /** Add an already inited plugin to the list. **/ |
|---|
| 43 | public function addPlugin(pgi:Object,nam:String) { |
|---|
| 44 | var obj:Object = {reference:pgi,name:nam,position:'over',size:50}; |
|---|
| 45 | // hack for the playlist/controlbar flashvars |
|---|
| 46 | if(nam == 'controlbar') { |
|---|
| 47 | obj['position'] = player.config['controlbar']; |
|---|
| 48 | obj['size'] = player.skin.controlbar.height; |
|---|
| 49 | obj['margin'] = player.skin.controlbar.x; |
|---|
| 50 | } else if (nam == 'playlist') { |
|---|
| 51 | obj['position'] = player.config['playlist']; |
|---|
| 52 | obj['size'] = player.config['playlistsize']; |
|---|
| 53 | } |
|---|
| 54 | for(var str:String in player.config) { |
|---|
| 55 | if(player.config[nam+'.'+str]) { |
|---|
| 56 | obj[str] = player.config[nam+'.'+str]; |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | plugins.push(obj); |
|---|
| 60 | pgi.initializePlugin(player.view); |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | /** Get a reference to a specific plugin. **/ |
|---|
| 65 | public function getPlugin(nam:String):Object { |
|---|
| 66 | for(var i:Number=0; i<plugins.length; i++) { |
|---|
| 67 | if(plugins[i]['name'] == nam) { |
|---|
| 68 | return plugins[i]['reference']; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | return null; |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /** Return the configuration data of a specific plugin. **/ |
|---|
| 76 | public function getPluginConfig(plg:Object):Object { |
|---|
| 77 | for(var i:Number=0; i<plugins.length; i++) { |
|---|
| 78 | if(plugins[i]['reference'] == plg) { |
|---|
| 79 | return plugins[i]; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | return null; |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | /** Load a single plugin into the stack (after initialization). **/ |
|---|
| 87 | public function loadPlugin(url:String,str:String=null) { |
|---|
| 88 | if(str != null && str != '') { |
|---|
| 89 | var ar1:Array = str.split('&'); |
|---|
| 90 | for(var i:String in ar1) { |
|---|
| 91 | var ar2:Array = ar1[i].split('='); |
|---|
| 92 | player.config[ar2[0]] = Strings.serialize(ar2[1]); } |
|---|
| 93 | } |
|---|
| 94 | loadSWF(url,false); |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | /** Start loading the SWF plugins, or broadcast if there's none. **/ |
|---|
| 99 | public function loadPlugins():void { |
|---|
| 100 | if(player.config['plugins']) { |
|---|
| 101 | var arr:Array = player.config['plugins'].split(','); |
|---|
| 102 | done = arr.length; |
|---|
| 103 | for(var i:Number=0; i<arr.length; i++) { |
|---|
| 104 | loadSWF(arr[i],false); |
|---|
| 105 | } |
|---|
| 106 | } else { |
|---|
| 107 | dispatchEvent(new SPLoaderEvent(SPLoaderEvent.PLUGINS)); |
|---|
| 108 | } |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** Start loading the skin, or broadcast if there's none. **/ |
|---|
| 113 | public function loadSkin():void { |
|---|
| 114 | if(player.config['skin']) { |
|---|
| 115 | loadSWF(player.config['skin'],true); |
|---|
| 116 | } else { |
|---|
| 117 | dispatchEvent(new SPLoaderEvent(SPLoaderEvent.SKIN)); |
|---|
| 118 | } |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /** Load a particular SWF file. **/ |
|---|
| 123 | private function loadSWF(str:String,skn:Boolean):void { |
|---|
| 124 | if(str.substr(-4) == '.swf') { str = str.substr(0, str.length-4); } |
|---|
| 125 | var ldr:Loader = new Loader(); |
|---|
| 126 | if(skn) { |
|---|
| 127 | ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,skinError); |
|---|
| 128 | ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,skinHandler); |
|---|
| 129 | } else { |
|---|
| 130 | ldr.visible = false; |
|---|
| 131 | player.skin.addChild(ldr); |
|---|
| 132 | ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,pluginError); |
|---|
| 133 | ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,pluginHandler); |
|---|
| 134 | } |
|---|
| 135 | str += '.swf'; |
|---|
| 136 | if(player.loaderInfo.url.indexOf('http') == 0) { |
|---|
| 137 | var ctx:LoaderContext = new LoaderContext(true,ApplicationDomain.currentDomain,SecurityDomain.currentDomain); |
|---|
| 138 | if(skn) { |
|---|
| 139 | ldr.load(new URLRequest(str),ctx); |
|---|
| 140 | } else { |
|---|
| 141 | ldr.load(new URLRequest(player.basedir+str),ctx); |
|---|
| 142 | } |
|---|
| 143 | } else { |
|---|
| 144 | ldr.load(new URLRequest(str)); |
|---|
| 145 | } |
|---|
| 146 | }; |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | /** Plugin loading failed. **/ |
|---|
| 150 | private function pluginError(evt:IOErrorEvent):void { |
|---|
| 151 | player.view.sendEvent('TRACE',' plugin: '+evt.text); |
|---|
| 152 | done--; |
|---|
| 153 | if(done == 0) { |
|---|
| 154 | dispatchEvent(new SPLoaderEvent(SPLoaderEvent.PLUGINS)); |
|---|
| 155 | } |
|---|
| 156 | }; |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | /** Plugin loading completed; add to stage and populate. **/ |
|---|
| 160 | private function pluginHandler(evt:Event):void { |
|---|
| 161 | try { |
|---|
| 162 | var idx:Number = evt.target.url.lastIndexOf('/'); |
|---|
| 163 | var nam:String = evt.target.url.substr(idx+1,evt.target.url.indexOf('.swf.')-3); |
|---|
| 164 | addPlugin(evt.target.content,nam); |
|---|
| 165 | evt.target.loader.visible = true; |
|---|
| 166 | } catch(err:Error) { |
|---|
| 167 | player.view.sendEvent('TRACE',' plugin: '+err.message); |
|---|
| 168 | } |
|---|
| 169 | done--; |
|---|
| 170 | if(done == 0) { |
|---|
| 171 | dispatchEvent(new SPLoaderEvent(SPLoaderEvent.PLUGINS)); |
|---|
| 172 | } |
|---|
| 173 | }; |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | /** Layout all plugins for a normal resize. **/ |
|---|
| 177 | public function layoutNormal():void { |
|---|
| 178 | var bounds:Object = {x:0,y:0,width:player.config['width'],height:player.config['height']}; |
|---|
| 179 | var overs:Array = new Array(); |
|---|
| 180 | for(var i:Number = plugins.length-1; i>=0; i--) { |
|---|
| 181 | switch(plugins[i]['position']) { |
|---|
| 182 | case "left": |
|---|
| 183 | plugins[i]['x'] = bounds.x; |
|---|
| 184 | plugins[i]['y'] = bounds.y; |
|---|
| 185 | plugins[i]['width'] = plugins[i]['size']; |
|---|
| 186 | plugins[i]['height'] = bounds.height; |
|---|
| 187 | plugins[i]['visible'] = true; |
|---|
| 188 | bounds.x += plugins[i]['size']; |
|---|
| 189 | bounds.width -= plugins[i]['size']; |
|---|
| 190 | break; |
|---|
| 191 | case "top": |
|---|
| 192 | plugins[i]['x'] = bounds.x; |
|---|
| 193 | plugins[i]['y'] = bounds.y; |
|---|
| 194 | plugins[i]['width'] = bounds.width; |
|---|
| 195 | plugins[i]['height'] = plugins[i]['size']; |
|---|
| 196 | plugins[i]['visible'] = true; |
|---|
| 197 | bounds.y += plugins[i]['size']; |
|---|
| 198 | bounds.height -= plugins[i]['size']; |
|---|
| 199 | break; |
|---|
| 200 | case "right": |
|---|
| 201 | plugins[i]['x'] = bounds.x + bounds.width - plugins[i]['size']; |
|---|
| 202 | plugins[i]['y'] = bounds.y; |
|---|
| 203 | plugins[i]['width'] = plugins[i]['size']; |
|---|
| 204 | plugins[i]['height'] = bounds.height; |
|---|
| 205 | plugins[i]['visible'] = true; |
|---|
| 206 | bounds.width -= plugins[i]['size']; |
|---|
| 207 | break; |
|---|
| 208 | case "bottom": |
|---|
| 209 | plugins[i]['x'] = bounds.x; |
|---|
| 210 | plugins[i]['y'] = bounds.y+bounds.height-plugins[i]['size']; |
|---|
| 211 | plugins[i]['width'] = bounds.width; |
|---|
| 212 | plugins[i]['height'] = plugins[i]['size']; |
|---|
| 213 | plugins[i]['visible'] = true; |
|---|
| 214 | bounds.height -= plugins[i]['size']; |
|---|
| 215 | break; |
|---|
| 216 | case "over": |
|---|
| 217 | overs.push(i); |
|---|
| 218 | break; |
|---|
| 219 | default: |
|---|
| 220 | plugins[i]['visible'] = false; |
|---|
| 221 | break; |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | for(var j:Number=0; j<overs.length; j++) { |
|---|
| 225 | plugins[overs[j]]['x'] = bounds.x; |
|---|
| 226 | plugins[overs[j]]['y'] = bounds.y; |
|---|
| 227 | plugins[overs[j]]['width'] = bounds.width; |
|---|
| 228 | plugins[overs[j]]['height'] = bounds.height; |
|---|
| 229 | plugins[overs[j]]['visible'] = true; |
|---|
| 230 | } |
|---|
| 231 | if(player.config['resizing']) { |
|---|
| 232 | player.config['width'] = bounds.width; |
|---|
| 233 | player.config['height'] = bounds.height; |
|---|
| 234 | } |
|---|
| 235 | }; |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | /** Layout all plugins in case of a fullscreen resize. **/ |
|---|
| 239 | public function layoutFullscreen() { |
|---|
| 240 | for(var i:Number=0; i<plugins.length; i++) { |
|---|
| 241 | if (plugins[i]['position'] == 'over' || plugins[i]['name'] == 'controlbar') { |
|---|
| 242 | plugins[i]['x'] = 0; |
|---|
| 243 | plugins[i]['y'] = 0; |
|---|
| 244 | plugins[i]['width'] = player.skin.stage.stageWidth; |
|---|
| 245 | plugins[i]['height'] = player.skin.stage.stageHeight; |
|---|
| 246 | plugins[i]['visible'] = true; |
|---|
| 247 | } else { |
|---|
| 248 | plugins[i]['visible'] = false; |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | }; |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | /** Skin loading failed; use default skin. **/ |
|---|
| 255 | private function skinError(evt:IOErrorEvent=null):void { |
|---|
| 256 | dispatchEvent(new SPLoaderEvent(SPLoaderEvent.SKIN)); |
|---|
| 257 | }; |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | /** Skin loading completed; add to stage and populate. **/ |
|---|
| 261 | private function skinHandler(evt:Event):void { |
|---|
| 262 | if(evt.target.content['player']) { |
|---|
| 263 | player.skin = MovieClip(evt.target.content['player']); |
|---|
| 264 | Draw.clear(player); |
|---|
| 265 | player.addChild(player.skin); |
|---|
| 266 | } |
|---|
| 267 | dispatchEvent(new SPLoaderEvent(SPLoaderEvent.SKIN)); |
|---|
| 268 | }; |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | } |
|---|