| 1 | /** |
|---|
| 2 | * Plugin that skins the actual mediafiles, overlay icons and the logo. |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.plugins { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import com.jeroenwijering.events.*; |
|---|
| 8 | import com.jeroenwijering.utils.*; |
|---|
| 9 | |
|---|
| 10 | import flash.display.*; |
|---|
| 11 | import flash.events.*; |
|---|
| 12 | import flash.geom.ColorTransform; |
|---|
| 13 | import flash.net.*; |
|---|
| 14 | import flash.utils.*; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | public class Display implements PluginInterface { |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | /** Configuration vars for this plugin. **/ |
|---|
| 21 | public var config:Object = {}; |
|---|
| 22 | /** Reference to the skin MC. **/ |
|---|
| 23 | public var clip:MovieClip; |
|---|
| 24 | /** Reference to the MVC view. **/ |
|---|
| 25 | private var view:AbstractView; |
|---|
| 26 | /** Loader object for loading a logo. **/ |
|---|
| 27 | private var loader:Loader; |
|---|
| 28 | /** The margins of the logo. **/ |
|---|
| 29 | private var margins:Array; |
|---|
| 30 | /** The latest playback state **/ |
|---|
| 31 | private var state:String; |
|---|
| 32 | /** Map with color transformation objects. **/ |
|---|
| 33 | private var colors:Object; |
|---|
| 34 | /** A list of all the icons. **/ |
|---|
| 35 | private var ICONS:Array = new Array( |
|---|
| 36 | 'playIcon', |
|---|
| 37 | 'errorIcon', |
|---|
| 38 | 'bufferIcon', |
|---|
| 39 | 'linkIcon', |
|---|
| 40 | 'muteIcon', |
|---|
| 41 | 'fullscreenIcon', |
|---|
| 42 | 'nextIcon', |
|---|
| 43 | 'titleIcon' |
|---|
| 44 | ); |
|---|
| 45 | /** Timeout for hiding the buffericon. **/ |
|---|
| 46 | private var timeout:Number; |
|---|
| 47 | /** Is there an error sent. **/ |
|---|
| 48 | private var errored:Boolean; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | /** Constructor; add all needed listeners. **/ |
|---|
| 52 | public function Display():void {}; |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | /** Initialize the plugin. **/ |
|---|
| 56 | public function initializePlugin(vie:AbstractView):void { |
|---|
| 57 | view = vie; |
|---|
| 58 | view.addControllerListener(ControllerEvent.ERROR,errorHandler); |
|---|
| 59 | view.addControllerListener(ControllerEvent.MUTE,stateHandler); |
|---|
| 60 | view.addControllerListener(ControllerEvent.PLAYLIST,stateHandler); |
|---|
| 61 | view.addControllerListener(ControllerEvent.RESIZE,resizeHandler); |
|---|
| 62 | view.addModelListener(ModelEvent.BUFFER,bufferHandler); |
|---|
| 63 | view.addModelListener(ModelEvent.ERROR,errorHandler); |
|---|
| 64 | view.addModelListener(ModelEvent.STATE,stateHandler); |
|---|
| 65 | if(view.config['backcolor'] && view.config['frontcolor'] && view.config['screencolor']) { |
|---|
| 66 | setColors(); |
|---|
| 67 | } |
|---|
| 68 | if(view.config['displayclick'] != 'none') { |
|---|
| 69 | clip.addEventListener(MouseEvent.CLICK,clickHandler); |
|---|
| 70 | clip.buttonMode = true; |
|---|
| 71 | clip.mouseChildren = false; |
|---|
| 72 | } |
|---|
| 73 | if(clip.logo) { |
|---|
| 74 | logoSetter(); |
|---|
| 75 | } |
|---|
| 76 | stateHandler(); |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | /** Receive buffer updates. **/ |
|---|
| 81 | private function bufferHandler(evt:ModelEvent):void { |
|---|
| 82 | if(evt.data.percentage > 0) { |
|---|
| 83 | Draw.set(clip.bufferIcon.txt,'text',Strings.zero(evt.data.percentage)); |
|---|
| 84 | } else { |
|---|
| 85 | Draw.set(clip.bufferIcon.txt,'text',''); |
|---|
| 86 | } |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | /** Process a click on the clip. **/ |
|---|
| 91 | private function clickHandler(evt:MouseEvent):void { |
|---|
| 92 | if(view.config['state'] == ModelStates.IDLE) { |
|---|
| 93 | view.sendEvent('PLAY'); |
|---|
| 94 | } else if (view.config['state'] == ModelStates.PLAYING && view.config['mute'] == true) { |
|---|
| 95 | view.sendEvent('MUTE'); |
|---|
| 96 | } else { |
|---|
| 97 | view.sendEvent(view.config['displayclick']); |
|---|
| 98 | } |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /** Receive and print errors. **/ |
|---|
| 103 | private function errorHandler(evt:Object):void { |
|---|
| 104 | if(view.config['icons'] == true) { |
|---|
| 105 | errored = true; |
|---|
| 106 | setIcon('errorIcon'); |
|---|
| 107 | Draw.set(clip.errorIcon.txt,'text',evt.data.message); |
|---|
| 108 | } |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** Logo loaded; now position it. **/ |
|---|
| 113 | private function loaderHandler(evt:Event=null):void { |
|---|
| 114 | if(margins[0] > margins[2]) { |
|---|
| 115 | clip.logo.x = clip.back.width- margins[2]-clip.logo.width; |
|---|
| 116 | } else { |
|---|
| 117 | clip.logo.x = margins[0]; |
|---|
| 118 | } |
|---|
| 119 | if(margins[1] > margins[3]) { |
|---|
| 120 | clip.logo.y = clip.back.height- margins[3]-clip.logo.height; |
|---|
| 121 | } else { |
|---|
| 122 | clip.logo.y = margins[1]; |
|---|
| 123 | } |
|---|
| 124 | }; |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | /** Setup the logo loading. **/ |
|---|
| 128 | private function logoSetter():void { |
|---|
| 129 | margins = new Array( |
|---|
| 130 | clip.logo.x, |
|---|
| 131 | clip.logo.y, |
|---|
| 132 | clip.back.width-clip.logo.x-clip.logo.width, |
|---|
| 133 | clip.back.height-clip.logo.y-clip.logo.height |
|---|
| 134 | ); |
|---|
| 135 | if(clip.logo.width == 10) { |
|---|
| 136 | Draw.clear(clip.logo); |
|---|
| 137 | } |
|---|
| 138 | if(view.config['logo']) { |
|---|
| 139 | Draw.clear(clip.logo); |
|---|
| 140 | loader = new Loader(); |
|---|
| 141 | loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderHandler); |
|---|
| 142 | loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loaderHandler); |
|---|
| 143 | clip.logo.addChild(loader); |
|---|
| 144 | loader.load(new URLRequest(view.config['logo'])); |
|---|
| 145 | } |
|---|
| 146 | }; |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | /** Receive resizing requests **/ |
|---|
| 151 | private function resizeHandler(evt:ControllerEvent):void { |
|---|
| 152 | if(config['height'] > 11) { |
|---|
| 153 | clip.visible = true; |
|---|
| 154 | } else { |
|---|
| 155 | clip.visible = false; |
|---|
| 156 | } |
|---|
| 157 | Draw.pos(clip,config['x'],config['y']); |
|---|
| 158 | Draw.size(clip.back,config['width'],config['height']); |
|---|
| 159 | Draw.size(clip.masker,config['width'],config['height']); |
|---|
| 160 | for(var i:String in ICONS) { |
|---|
| 161 | Draw.pos(clip[ICONS[i]],config['width']/2,config['height']/2); |
|---|
| 162 | } |
|---|
| 163 | if(clip.logo) { |
|---|
| 164 | loaderHandler(); |
|---|
| 165 | } |
|---|
| 166 | }; |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | /** Set color tranformation objects so the buttons can be colorized. **/ |
|---|
| 170 | private function setColors():void { |
|---|
| 171 | var scr:ColorTransform = new ColorTransform(); |
|---|
| 172 | scr.color = uint('0x'+view.config['screencolor']); |
|---|
| 173 | var frt:ColorTransform = new ColorTransform(); |
|---|
| 174 | frt.color = uint('0x'+view.config['frontcolor']); |
|---|
| 175 | var bck:ColorTransform = new ColorTransform(); |
|---|
| 176 | bck.color = uint('0x'+view.config['backcolor']); |
|---|
| 177 | colors = {screen:scr,front:frt,back:bck}; |
|---|
| 178 | clip.back.transform.colorTransform = colors['screen']; |
|---|
| 179 | for(var i:String in ICONS) { |
|---|
| 180 | try { |
|---|
| 181 | clip[ICONS[i]].bck.transform.colorTransform = colors['front']; |
|---|
| 182 | clip[ICONS[i]].icn.transform.colorTransform = colors['back']; |
|---|
| 183 | clip[ICONS[i]].txt.textColor = colors['back'].color; |
|---|
| 184 | } catch (err:Error) {} |
|---|
| 185 | } |
|---|
| 186 | }; |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | /** Set a specific icon in the clip. **/ |
|---|
| 190 | private function setIcon(icn:String=undefined):void { |
|---|
| 191 | clearTimeout(timeout); |
|---|
| 192 | for(var i:String in ICONS) { |
|---|
| 193 | if(clip[ICONS[i]]) { |
|---|
| 194 | if(icn == ICONS[i] && view.config['icons'] == true) { |
|---|
| 195 | clip[ICONS[i]].visible = true; |
|---|
| 196 | } else { |
|---|
| 197 | clip[ICONS[i]].visible = false; |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | }; |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | /** Place the title in the titleIcon. **/ |
|---|
| 205 | private function setTitle() { |
|---|
| 206 | var icn:MovieClip = clip.titleIcon; |
|---|
| 207 | icn.txt.autoSize = 'left'; |
|---|
| 208 | icn.txt.text = view.playlist[view.config['item']]['title']; |
|---|
| 209 | if(icn.txt.width+icn.icn.width + 60 > config['width']) { |
|---|
| 210 | icn.bck.width = config['width'] - 60; |
|---|
| 211 | icn.txt.autoSize = 'none'; |
|---|
| 212 | icn.txt.width = icn.bck.width - icn.icn.width - 20; |
|---|
| 213 | } else { |
|---|
| 214 | icn.bck.width = icn.txt.width + icn.icn.width + 20; |
|---|
| 215 | } |
|---|
| 216 | icn.bck.x = -icn.bck.width/2; |
|---|
| 217 | icn.icn.x = icn.bck.x; |
|---|
| 218 | icn.txt.x = icn.icn.x + icn.icn.width; |
|---|
| 219 | }; |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | /** Handle a change in playback state. **/ |
|---|
| 223 | private function stateHandler(evt:Event=null):void { |
|---|
| 224 | switch (view.config['state']) { |
|---|
| 225 | case ModelStates.PLAYING: |
|---|
| 226 | if(view.config['mute'] == true) { |
|---|
| 227 | setIcon('muteIcon'); |
|---|
| 228 | } else { |
|---|
| 229 | setIcon(); |
|---|
| 230 | } |
|---|
| 231 | break; |
|---|
| 232 | case ModelStates.BUFFERING: |
|---|
| 233 | if(evt && evt['data'].oldstate == ModelStates.PLAYING) { |
|---|
| 234 | timeout = setTimeout(setIcon,1500,'bufferIcon'); |
|---|
| 235 | } else { |
|---|
| 236 | setIcon('bufferIcon'); |
|---|
| 237 | } |
|---|
| 238 | break; |
|---|
| 239 | case ModelStates.IDLE: |
|---|
| 240 | case ModelStates.COMPLETED: |
|---|
| 241 | if(view.config.displayclick == 'none' || !view.playlist) { |
|---|
| 242 | setIcon(); |
|---|
| 243 | } else if (errored) { |
|---|
| 244 | errored = false; |
|---|
| 245 | } else if (clip.titleIcon && view.config['displaytitle']) { |
|---|
| 246 | setTitle(); |
|---|
| 247 | setIcon('titleIcon'); |
|---|
| 248 | } else { |
|---|
| 249 | setIcon('playIcon'); |
|---|
| 250 | } |
|---|
| 251 | break; |
|---|
| 252 | default: |
|---|
| 253 | setIcon(view.config.displayclick+'Icon'); |
|---|
| 254 | break; |
|---|
| 255 | } |
|---|
| 256 | }; |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | }; |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | } |
|---|