| 1 | /** |
|---|
| 2 | * Interface for all display elements. |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.plugins { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import com.jeroenwijering.events.*; |
|---|
| 8 | import com.jeroenwijering.utils.Draw; |
|---|
| 9 | import com.jeroenwijering.utils.Strings; |
|---|
| 10 | import flash.display.Loader; |
|---|
| 11 | import flash.display.MovieClip; |
|---|
| 12 | import flash.display.Sprite; |
|---|
| 13 | import flash.events.Event; |
|---|
| 14 | import flash.events.MouseEvent; |
|---|
| 15 | import flash.geom.ColorTransform; |
|---|
| 16 | import flash.net.URLRequest; |
|---|
| 17 | import flash.utils.clearTimeout; |
|---|
| 18 | import flash.utils.setTimeout; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | public class Display implements PluginInterface { |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | /** Reference to the MVC view. **/ |
|---|
| 25 | private var view:AbstractView; |
|---|
| 26 | /** Reference to the display MC. **/ |
|---|
| 27 | private var display:MovieClip; |
|---|
| 28 | /** Loader object for loading a logo. **/ |
|---|
| 29 | private var loader:Loader; |
|---|
| 30 | /** The margins of the logo. **/ |
|---|
| 31 | private var margins:Array; |
|---|
| 32 | /** The latest playback state **/ |
|---|
| 33 | private var state:String; |
|---|
| 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 | ); |
|---|
| 44 | /** ID for the buffer showing tiomeout. **/ |
|---|
| 45 | private var timeout:Number; |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | /** Constructor; add all needed listeners. **/ |
|---|
| 49 | public function Display():void {}; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | /** Initialize the plugin. **/ |
|---|
| 53 | public function initializePlugin(vie:AbstractView):void { |
|---|
| 54 | view = vie; |
|---|
| 55 | view.addControllerListener(ControllerEvent.ERROR,errorHandler); |
|---|
| 56 | view.addControllerListener(ControllerEvent.RESIZE,resizeHandler); |
|---|
| 57 | view.addControllerListener(ControllerEvent.PLAYLIST,stateHandler); |
|---|
| 58 | view.addModelListener(ModelEvent.BUFFER,bufferHandler); |
|---|
| 59 | view.addModelListener(ModelEvent.ERROR,errorHandler); |
|---|
| 60 | view.addModelListener(ModelEvent.STATE,stateHandler); |
|---|
| 61 | display = view.skin['display']; |
|---|
| 62 | display.media.mask = display.masker; |
|---|
| 63 | if(view.config['screencolor']) { |
|---|
| 64 | var clr:ColorTransform = new ColorTransform(); |
|---|
| 65 | clr.color = uint('0x'+view.config['screencolor'].substr(-6)); |
|---|
| 66 | display.back.transform.colorTransform = clr; |
|---|
| 67 | } |
|---|
| 68 | if(view.config['screenalpha']) { |
|---|
| 69 | if(view.config['screenalpha'] < 1) { |
|---|
| 70 | display.back.alpha = view.config['screenalpha']; |
|---|
| 71 | } else if(view.config['screenalpha'] <= 100) { |
|---|
| 72 | display.back.alpha = Number(view.config['screenalpha'])/100; |
|---|
| 73 | } else { |
|---|
| 74 | display.back.alpha = 1; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | if(view.config['displayclick'] != 'none') { |
|---|
| 78 | display.addEventListener(MouseEvent.CLICK,clickHandler); |
|---|
| 79 | display.buttonMode = true; |
|---|
| 80 | display.mouseChildren = false; |
|---|
| 81 | } |
|---|
| 82 | try { |
|---|
| 83 | Draw.clear(display.logo); |
|---|
| 84 | if(view.config['logo']) { setLogo(); } |
|---|
| 85 | } catch (err:Error) {} |
|---|
| 86 | stateHandler(); |
|---|
| 87 | resizeHandler(); |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | /** Receive buffer updates. **/ |
|---|
| 92 | private function bufferHandler(evt:ModelEvent):void { |
|---|
| 93 | var pct:String = ''; |
|---|
| 94 | if(evt.data.percentage > 0) { |
|---|
| 95 | pct = Strings.zero(evt.data.percentage); |
|---|
| 96 | } |
|---|
| 97 | try { |
|---|
| 98 | display.bufferIcon.txt.text = pct; |
|---|
| 99 | } catch (err:Error) {} |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | /** Process a click on the display. **/ |
|---|
| 104 | private function clickHandler(evt:MouseEvent):void { |
|---|
| 105 | view.sendEvent(view.config['displayclick']); |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | /** Receive and print errors. **/ |
|---|
| 110 | private function errorHandler(evt:Object):void { |
|---|
| 111 | if(view.config['icons'] == true) { |
|---|
| 112 | try { |
|---|
| 113 | setIcon('errorIcon'); |
|---|
| 114 | display.errorIcon.txt.text = evt.data.message; |
|---|
| 115 | } catch (err:Error) {} |
|---|
| 116 | } |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | /** Logo loaded; now position it. **/ |
|---|
| 121 | private function logoHandler(evt:Event):void { |
|---|
| 122 | if(margins[0] > margins[2]) { |
|---|
| 123 | display.logo.x = display.back.width- margins[2]-display.logo.width; |
|---|
| 124 | } else { |
|---|
| 125 | display.logo.x = margins[0]; |
|---|
| 126 | } |
|---|
| 127 | if(margins[1] > margins[3]) { |
|---|
| 128 | display.logo.y = display.back.height- margins[3]-display.logo.height; |
|---|
| 129 | } else { |
|---|
| 130 | display.logo.y = margins[1]; |
|---|
| 131 | } |
|---|
| 132 | }; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | /** Receive resizing requests **/ |
|---|
| 136 | private function resizeHandler(evt:ControllerEvent=null):void { |
|---|
| 137 | if(view.config['height'] > 0) { |
|---|
| 138 | display.visible = true; |
|---|
| 139 | display.x = view.config['x']; |
|---|
| 140 | display.y = view.config['y']; |
|---|
| 141 | } else { |
|---|
| 142 | display.visible = false; |
|---|
| 143 | } |
|---|
| 144 | display.back.width = view.config['width']; |
|---|
| 145 | display.back.height = view.config['height']; |
|---|
| 146 | try { |
|---|
| 147 | display.masker.width = view.config['width']; |
|---|
| 148 | display.masker.height = view.config['height']; |
|---|
| 149 | } catch (err:Error) {} |
|---|
| 150 | for(var i:String in ICONS) { |
|---|
| 151 | try { |
|---|
| 152 | display[ICONS[i]].x = Math.round(view.config['width']/2); |
|---|
| 153 | display[ICONS[i]].y = Math.round(view.config['height']/2); |
|---|
| 154 | } catch (err:Error) {} |
|---|
| 155 | } |
|---|
| 156 | if(view.config['logo']) { |
|---|
| 157 | logoHandler(new Event(Event.COMPLETE)); |
|---|
| 158 | } |
|---|
| 159 | }; |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | /** Set a specific icon in the display. **/ |
|---|
| 163 | private function setIcon(icn:String=undefined):void { |
|---|
| 164 | for(var i:String in ICONS) { |
|---|
| 165 | if(display[ICONS[i]]) { |
|---|
| 166 | if(icn == ICONS[i]) { |
|---|
| 167 | display[ICONS[i]].visible = true; |
|---|
| 168 | } else { |
|---|
| 169 | display[ICONS[i]].visible = false; |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | }; |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | /** Setup the logo loading. **/ |
|---|
| 177 | private function setLogo():void { |
|---|
| 178 | margins = new Array( |
|---|
| 179 | display.logo.x, |
|---|
| 180 | display.logo.y, |
|---|
| 181 | display.back.width-display.logo.x-display.logo.width, |
|---|
| 182 | display.back.height-display.logo.y-display.logo.height |
|---|
| 183 | ); |
|---|
| 184 | loader = new Loader(); |
|---|
| 185 | loader.contentLoaderInfo.addEventListener(Event.COMPLETE,logoHandler); |
|---|
| 186 | display.logo.addChild(loader); |
|---|
| 187 | loader.load(new URLRequest(view.config['logo'])); |
|---|
| 188 | }; |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | /** Handle a change in playback state. **/ |
|---|
| 192 | private function stateHandler(evt:Event=null):void { |
|---|
| 193 | state = view.config['state']; |
|---|
| 194 | clearTimeout(timeout); |
|---|
| 195 | if(state == ModelStates.PLAYING) { |
|---|
| 196 | setIcon(); |
|---|
| 197 | } else if (state == ModelStates.BUFFERING && view.config['icons'] == true) { |
|---|
| 198 | setIcon(); |
|---|
| 199 | timeout = setTimeout(setIcon,1500,'bufferIcon'); |
|---|
| 200 | } else { |
|---|
| 201 | switch(view.config.displayclick) { |
|---|
| 202 | case 'none': |
|---|
| 203 | setIcon(); |
|---|
| 204 | break; |
|---|
| 205 | default: |
|---|
| 206 | if(view.config['icons'] == true && view.playlist) { |
|---|
| 207 | setIcon(view.config.displayclick+'Icon'); |
|---|
| 208 | } else { |
|---|
| 209 | setIcon(); |
|---|
| 210 | } |
|---|
| 211 | break; |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | }; |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | }; |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | } |
|---|