| 1 | /** |
|---|
| 2 | * Display a searchbar and direct the search externally. |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.views { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import com.jeroenwijering.events.*; |
|---|
| 8 | import com.jeroenwijering.player.View; |
|---|
| 9 | import com.jeroenwijering.utils.*; |
|---|
| 10 | import flash.display.Loader; |
|---|
| 11 | import flash.display.MovieClip; |
|---|
| 12 | import flash.events.Event; |
|---|
| 13 | import flash.events.MouseEvent; |
|---|
| 14 | import flash.geom.Rectangle; |
|---|
| 15 | import flash.net.URLRequest; |
|---|
| 16 | import flash.utils.setInterval; |
|---|
| 17 | import flash.utils.clearInterval; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | public class PlaylistView { |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** Reference to the view. **/ |
|---|
| 24 | private var view:View; |
|---|
| 25 | /** Reference to the playlist MC. **/ |
|---|
| 26 | private var clip:MovieClip; |
|---|
| 27 | /** Array with all button instances **/ |
|---|
| 28 | private var buttons:Array; |
|---|
| 29 | /** Height of a button (to calculate scrolling) **/ |
|---|
| 30 | private var buttonheight:Number; |
|---|
| 31 | /** Currently active button. **/ |
|---|
| 32 | private var active:Number; |
|---|
| 33 | /** Proportion between clip and mask. **/ |
|---|
| 34 | private var proportion:Number; |
|---|
| 35 | /** Interval ID for scrolling **/ |
|---|
| 36 | private var scrollInterval:Number; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | public function PlaylistView(vie:View) { |
|---|
| 40 | view = vie; |
|---|
| 41 | view.addControllerListener(ControllerEvent.ITEM,itemHandler); |
|---|
| 42 | view.addControllerListener(ControllerEvent.PLAYLIST,playlistHandler); |
|---|
| 43 | view.addControllerListener(ControllerEvent.RESIZE,resizeHandler); |
|---|
| 44 | view.addModelListener(ModelEvent.STATE,stateHandler); |
|---|
| 45 | clip = view.skin['playlist']; |
|---|
| 46 | buttonheight = clip.list.button.height; |
|---|
| 47 | clip.list.button.visible = false; |
|---|
| 48 | clip.list.mask = clip.masker; |
|---|
| 49 | clip.slider.buttonMode = true; |
|---|
| 50 | clip.slider.mouseChildren = false; |
|---|
| 51 | clip.list.addEventListener(MouseEvent.CLICK,clickHandler); |
|---|
| 52 | clip.list.addEventListener(MouseEvent.MOUSE_OVER,overHandler); |
|---|
| 53 | clip.list.addEventListener(MouseEvent.MOUSE_OUT,outHandler); |
|---|
| 54 | clip.list.addEventListener(MouseEvent.MOUSE_UP,stopHandler); |
|---|
| 55 | clip.slider.addEventListener(MouseEvent.MOUSE_DOWN,startHandler); |
|---|
| 56 | clip.visible = false; |
|---|
| 57 | trace(clip); |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /** Setup all buttons in the playlist **/ |
|---|
| 62 | private function buildList(clr:Boolean) { |
|---|
| 63 | var wid = clip.back.width; |
|---|
| 64 | var hei = clip.back.height; |
|---|
| 65 | proportion = view.playlist.length*buttonheight/hei; |
|---|
| 66 | if (proportion > 1) { |
|---|
| 67 | wid -=20; |
|---|
| 68 | buildSlider(); |
|---|
| 69 | } else { |
|---|
| 70 | clip.slider.visible = false; |
|---|
| 71 | } |
|---|
| 72 | clip.masker.height = hei; |
|---|
| 73 | clip.masker.width = wid; |
|---|
| 74 | if(clr) { |
|---|
| 75 | clip.list.y = 0; |
|---|
| 76 | Draw.clear(clip.list); |
|---|
| 77 | buttons = new Array(); |
|---|
| 78 | clip.visible= true; |
|---|
| 79 | } else { |
|---|
| 80 | if(proportion > 1) { scrollCheck(); } |
|---|
| 81 | } |
|---|
| 82 | for(var i=0; i<view.playlist.length; i++) { |
|---|
| 83 | if(clr) { |
|---|
| 84 | var btn = Draw.clone(clip.list.button); |
|---|
| 85 | clip.list.addChild(btn); |
|---|
| 86 | var stc = new Stacker(btn); |
|---|
| 87 | btn.y = i*buttonheight; |
|---|
| 88 | btn.buttonMode = true; |
|---|
| 89 | btn.mouseChildren =false; |
|---|
| 90 | btn.name = i; |
|---|
| 91 | buttons.push({c:btn,s:stc}); |
|---|
| 92 | setContents(i); |
|---|
| 93 | } |
|---|
| 94 | buttons[i].s.rearrange(wid); |
|---|
| 95 | } |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** Setup the scrollbar component **/ |
|---|
| 100 | private function buildSlider() { |
|---|
| 101 | var scr = clip.slider; |
|---|
| 102 | scr.visible = true; |
|---|
| 103 | scr.x = clip.back.width-scr.width; |
|---|
| 104 | var dif = clip.back.height-scr.height; |
|---|
| 105 | scr.back.height += dif; |
|---|
| 106 | scr.rail.height += dif; |
|---|
| 107 | scr.icon.height = Math.round(scr.rail.height/proportion); |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | /** Handle a click on a button. **/ |
|---|
| 112 | private function clickHandler(evt:MouseEvent) { |
|---|
| 113 | view.sendEvent('item',Number(evt.target.name)); |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | /** Switch the currently active item */ |
|---|
| 118 | private function itemHandler(evt:ControllerEvent) { |
|---|
| 119 | var idx = evt.data.index; |
|---|
| 120 | if(!isNaN(active)) { |
|---|
| 121 | buttons[active].c.gotoAndStop('out'); |
|---|
| 122 | } |
|---|
| 123 | buttons[idx].c.gotoAndStop('active'); |
|---|
| 124 | active = idx; |
|---|
| 125 | }; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | /** Loading of image completed; resume loading **/ |
|---|
| 129 | private function loaderHandler(evt:Event) { |
|---|
| 130 | var ldr = Loader(evt.target.loader); |
|---|
| 131 | Stretcher.stretch(ldr,ldr.mask.width,ldr.mask.height,Stretcher.FILL); |
|---|
| 132 | }; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | /** Handle a button rollover. **/ |
|---|
| 136 | private function overHandler(evt:MouseEvent) { |
|---|
| 137 | var idx = Number(evt.target.name); |
|---|
| 138 | buttons[idx].c.gotoAndStop('over'); |
|---|
| 139 | }; |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | /** Handle a button rollover. **/ |
|---|
| 143 | private function outHandler(evt:MouseEvent) { |
|---|
| 144 | var idx = Number(evt.target.name); |
|---|
| 145 | if(idx == active) { |
|---|
| 146 | buttons[idx].c.gotoAndStop('active'); |
|---|
| 147 | } else { |
|---|
| 148 | buttons[idx].c.gotoAndStop('out'); |
|---|
| 149 | } |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | /** New playlist loaded: rebuild the playclip. **/ |
|---|
| 154 | private function playlistHandler(evt:ControllerEvent) { |
|---|
| 155 | if(view.config['playlist'] != 'none') { |
|---|
| 156 | buildList(true); |
|---|
| 157 | } |
|---|
| 158 | }; |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | /** Process resizing requests **/ |
|---|
| 162 | private function resizeHandler(evt:ControllerEvent) { |
|---|
| 163 | if(view.config['playlist'] == 'right') { |
|---|
| 164 | clip.x = evt.data.width; |
|---|
| 165 | clip.y = 0; |
|---|
| 166 | clip.back.width = view.config['playlistsize']; |
|---|
| 167 | clip.back.height = evt.data.height; |
|---|
| 168 | } else if (view.config['playlist'] == 'bottom') { |
|---|
| 169 | clip.x = 0; |
|---|
| 170 | clip.y = evt.data.height; |
|---|
| 171 | if (view.config['controlbar'] == 'bottom') { |
|---|
| 172 | clip.y += view.config['controlbarsize']; |
|---|
| 173 | } |
|---|
| 174 | clip.back.height = view.config['playlistsize']; |
|---|
| 175 | clip.back.width = evt.data.width; |
|---|
| 176 | } else if (view.config['playlist'] == 'over') { |
|---|
| 177 | clip.x = clip.y = 0; |
|---|
| 178 | clip.back.height = evt.data.height; |
|---|
| 179 | clip.back.width = evt.data.width; |
|---|
| 180 | } |
|---|
| 181 | buildList(false); |
|---|
| 182 | }; |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | /** Make sure the playlist is not out of range. **/ |
|---|
| 186 | private function scrollCheck() { |
|---|
| 187 | var scr = clip.slider; |
|---|
| 188 | if(clip.list.y > 0) { |
|---|
| 189 | clip.list.y = 0; |
|---|
| 190 | scr.icon.y = scr.rail.y; |
|---|
| 191 | } else if (clip.list.y < clip.masker.height-clip.list.height) { |
|---|
| 192 | scr.icon.y = scr.rail.y+scr.rail.height-scr.icon.height; |
|---|
| 193 | clip.list.y = clip.masker.height-clip.list.height; |
|---|
| 194 | } |
|---|
| 195 | }; |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | /** Scrolling handler. **/ |
|---|
| 199 | private function scrollHandler() { |
|---|
| 200 | var scr = clip.slider; |
|---|
| 201 | var yps = scr.mouseY; |
|---|
| 202 | var ips = yps - scr.icon.height/2; |
|---|
| 203 | var cps = clip.masker.y+clip.masker.height/2-proportion*yps; |
|---|
| 204 | scr.icon.y = Math.round(ips - (ips-scr.icon.y)/1.5); |
|---|
| 205 | clip.list.y = Math.round((cps - (cps-clip.list.y)/1.5)); |
|---|
| 206 | scrollCheck(); |
|---|
| 207 | }; |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | /** Setup button elements **/ |
|---|
| 211 | private function setContents(idx:Number) { |
|---|
| 212 | for (var itm in view.playlist[idx]) { |
|---|
| 213 | if(!buttons[idx].c[itm]) { |
|---|
| 214 | continue; |
|---|
| 215 | } else if(itm == 'image') { |
|---|
| 216 | var ldr = new Loader(); |
|---|
| 217 | buttons[idx].c.addChild(ldr); |
|---|
| 218 | ldr.x = buttons[idx].c.image.x; |
|---|
| 219 | ldr.y = buttons[idx].c.image.y; |
|---|
| 220 | ldr.mask = buttons[idx].c.image; |
|---|
| 221 | ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderHandler); |
|---|
| 222 | ldr.load(new URLRequest(view.playlist[idx]['image'])); |
|---|
| 223 | } else if(itm == 'duration') { |
|---|
| 224 | if(view.playlist[idx][itm] > 0) { |
|---|
| 225 | buttons[idx].c[itm].field.text = Strings.digits(view.playlist[idx][itm]); |
|---|
| 226 | } |
|---|
| 227 | } else { |
|---|
| 228 | buttons[idx].c[itm].field.text = view.playlist[idx][itm]; |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | if(!view.playlist[idx]['image'] && buttons[idx].c['image']) { |
|---|
| 232 | buttons[idx].c['image'].visible = false; |
|---|
| 233 | } |
|---|
| 234 | }; |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | /** Start scrolling the playlist. **/ |
|---|
| 238 | private function startHandler(evt:MouseEvent) { |
|---|
| 239 | clearInterval(scrollInterval); |
|---|
| 240 | scrollHandler(); |
|---|
| 241 | scrollInterval = setInterval(scrollHandler,50); |
|---|
| 242 | }; |
|---|
| 243 | |
|---|
| 244 | /** Process state changes **/ |
|---|
| 245 | private function stateHandler(evt:ModelEvent) { |
|---|
| 246 | if(view.config['playlist'] == 'over') { |
|---|
| 247 | if(evt.data.newstate == ModelStates.PLAYING || evt.data.newstate == ModelStates.BUFFERING) { |
|---|
| 248 | clip.visible = false; |
|---|
| 249 | } else { |
|---|
| 250 | clip.visible = true; |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | }; |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | /** Stop scrolling the playlist. **/ |
|---|
| 257 | private function stopHandler(evt:MouseEvent) { |
|---|
| 258 | clearInterval(scrollInterval); |
|---|
| 259 | }; |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | }; |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | } |
|---|