source: branches/4.2/com/jeroenwijering/plugins/Playlist.as @ 68

Revision 68, 10.4 KB checked in by jeroen, 5 years ago (diff)

added new feeditems, polished simple/thin skins and made sure large YT images are loaded by default

  • Property svn:executable set to *
Line 
1/**
2* Display a searchbar and direct the search externally.
3**/
4package com.jeroenwijering.plugins {
5
6
7import com.jeroenwijering.events.*;
8import com.jeroenwijering.utils.*;
9import flash.display.Loader;
10import flash.display.MovieClip;
11import flash.events.Event;
12import flash.events.MouseEvent;
13import flash.geom.ColorTransform;
14import flash.geom.Rectangle;
15import flash.net.URLRequest;
16import flash.utils.setInterval;
17import flash.utils.clearInterval;
18
19
20public class Playlist implements PluginInterface {
21
22
23        /** Reference to the view. **/
24        private var view:AbstractView;
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        /** Image dimensions. **/
38        private var image:Array;
39        /** Color object for backcolor. **/
40        private var back:ColorTransform;
41        /** Color object for frontcolor. **/
42        private var front:ColorTransform;
43        /** Color object for lightcolor. **/
44        private var light:ColorTransform;
45
46
47        public function Playlist():void {};
48
49
50        /** Initialize the communication with the player. **/
51        public function initializePlugin(vie:AbstractView):void {
52                view = vie;
53                view.addControllerListener(ControllerEvent.ITEM,itemHandler);
54                view.addControllerListener(ControllerEvent.PLAYLIST,playlistHandler);
55                view.addControllerListener(ControllerEvent.RESIZE,resizeHandler);
56                view.addModelListener(ModelEvent.STATE,stateHandler);
57                clip = view.skin['playlist'];
58                buttonheight = clip.list.button.height;
59                clip.list.button.visible = false;
60                clip.list.mask = clip.masker;
61                clip.list.addEventListener(MouseEvent.CLICK,clickHandler);
62                clip.list.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
63                clip.list.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
64                clip.slider.buttonMode = true;
65                clip.slider.mouseChildren = false;
66                clip.slider.addEventListener(MouseEvent.MOUSE_DOWN,sdownHandler);
67                clip.slider.addEventListener(MouseEvent.MOUSE_OVER,soverHandler);
68                clip.slider.addEventListener(MouseEvent.MOUSE_OUT,soutHandler);
69                clip.slider.addEventListener(MouseEvent.MOUSE_WHEEL,wheelHandler);
70                clip.visible = false;
71                buttons = new Array();
72                try {
73                        image = new Array(clip.list.button.image.width,clip.list.button.image.height);
74                } catch (err:Error) {}
75                setColors();
76                playlistHandler();
77                resizeHandler();
78        };
79
80
81        /** Setup all buttons in the playlist **/
82        private function buildList(clr:Boolean):void {
83                if(!view.playlist) { return; }
84                var wid = clip.back.width;
85                var hei = clip.back.height;
86                proportion = view.playlist.length*buttonheight/hei;
87                if (proportion > 1.01) {
88                        wid -=clip.slider.width;
89                        buildSlider();
90                } else {
91                        clip.slider.visible = false;
92                }
93                clip.masker.height = hei;
94                clip.masker.width = wid;
95                if(clr) {
96                        clip.list.y = 0;
97                        for(var j=0; j<buttons.length; j++) {
98                                clip.list.removeChild(buttons[j].c);
99                        }
100                        buttons = new Array();
101                        clip.visible= true;
102                } else {
103                        if(proportion > 1) { scrollCheck(); }
104                }
105                for(var i=0; i<view.playlist.length; i++) {
106                        if(clr) {
107                                var btn = Draw.clone(clip.list.button,true);
108                                var stc = new Stacker(btn);
109                                btn.y = i*buttonheight;
110                                btn.buttonMode = true;
111                                btn.mouseChildren =false;
112                                btn.name = i;
113                                buttons.push({c:btn,s:stc});
114                                setContents(i);
115                        }
116                        buttons[i].s.rearrange(wid);
117                }
118        };
119
120
121        /** Setup the scrollbar component **/
122        private function buildSlider():void {
123                var scr = clip.slider;
124                scr.visible = true;
125                scr.x = clip.back.width-scr.width;
126                var dif = clip.back.height-scr.height;
127                scr.back.height += dif;
128                scr.rail.height += dif;
129                scr.icon.height = Math.round(scr.rail.height/proportion);
130        };
131
132
133        /** Handle a click on a button. **/
134        private function clickHandler(evt:MouseEvent):void {
135                trace(evt.target.name);
136                view.sendEvent('item',Number(evt.target.name));
137        };
138
139
140        /** Switch the currently active item */
141        private function itemHandler(evt:ControllerEvent):void {
142                var idx = view.config['item'];
143                if(buttons[idx]) {
144                        if(buttons[idx].c['back']) {
145                                if(!isNaN(active)) {
146                                        buttons[active].c['back'].visible = false;
147                                }
148                                buttons[idx].c['back'].visible = true;
149                        }
150                        buttons[idx].c.gotoAndPlay('active');
151                        if(!isNaN(active)) {
152                                buttons[active].c.gotoAndPlay('out');
153                        }
154                }
155                active = idx;
156        };
157
158
159        /** Loading of image completed; resume loading **/
160        private function loaderHandler(evt:Event):void {
161                var ldr = Loader(evt.target.loader);
162                Stretcher.stretch(ldr,image[0],image[1],Stretcher.FILL);
163        };
164
165
166        /** Handle a button rollover. **/
167        private function overHandler(evt:MouseEvent):void {
168                var idx = Number(evt.target.name);
169                if(light) {
170                        for (var itm in view.playlist[idx]) {
171                                if(buttons[idx].c[itm]) {
172                                        buttons[idx].c[itm].textColor = light.color;
173                                }
174                        }
175                } else {
176                        buttons[idx].c.gotoAndPlay('over');
177                }
178        };
179
180
181        /** Handle a button rollover. **/
182        private function outHandler(evt:MouseEvent):void {
183                var idx = Number(evt.target.name);
184                if(light) {
185                        for (var itm in view.playlist[idx]) {
186                                if(buttons[idx].c[itm]) {
187                                        buttons[idx].c[itm].textColor = front.color;
188                                }
189                        }
190                } else if(idx == active) {
191                        buttons[idx].c.gotoAndPlay('active');
192                } else {
193                        buttons[idx].c.gotoAndPlay('out');
194                }
195        };
196
197
198        /** New playlist loaded: rebuild the playclip. **/
199        private function playlistHandler(evt:ControllerEvent=null):void {
200                active = undefined;
201                if(view.config['playlist'] != 'none') {
202                        buildList(true);
203                }
204        };
205
206
207        /** Process resizing requests **/
208        private function resizeHandler(evt:ControllerEvent=null):void {
209                if(view.config['playlist'] == 'right') {
210                        clip.x = view.config['width'];
211                        clip.y = 0;
212                        clip.back.width = view.config['playlistsize'];
213                        clip.back.height = view.config['height'];;
214                        buildList(false);
215                } else if (view.config['playlist'] == 'bottom') {
216                        clip.x = 0;
217                        clip.y = view.config['height'];
218                        if (view.config['controlbar'] == 'bottom') {
219                                clip.y += view.config['controlbarsize'];
220                        }
221                        clip.back.height = view.config['playlistsize'];
222                        clip.back.width = view.config['width'];
223                        buildList(false);
224                } else if (view.config['playlist'] == 'over') {
225                        clip.x = clip.y = 0;
226                        clip.back.width = view.config['width'];
227                        clip.back.height = view.config['height'];
228                        buildList(false);
229                } else {
230                        clip.visible = false;
231                }
232        };
233
234
235        /** Make sure the playlist is not out of range. **/
236        private function scrollCheck():void {
237                var scr = clip.slider;
238                if(clip.list.y > 0 || scr.icon.y < scr.rail.y) {
239                        clip.list.y = 0;
240                        scr.icon.y = scr.rail.y;
241                } else if (clip.list.y < clip.masker.height-clip.list.height ||
242                        scr.icon.y > scr.rail.y+scr.rail.height-scr.icon.height) {
243                        scr.icon.y = scr.rail.y+scr.rail.height-scr.icon.height;
244                        clip.list.y = clip.masker.height-clip.list.height;
245                }
246        };
247
248
249        /** Scrolling handler. **/
250        private function scrollHandler():void {
251                var scr = clip.slider;
252                var yps = scr.mouseY;
253                var ips = yps - scr.icon.height/2;
254                var cps = clip.masker.y+clip.masker.height/2-proportion*yps;
255                scr.icon.y = Math.round(ips-(ips-scr.icon.y)/1.5);
256                clip.list.y = Math.round((cps - (cps-clip.list.y)/1.5));
257                scrollCheck();
258        };
259
260
261        /** Init the colors. **/
262        private function setColors():void {
263                if(view.config['backcolor']) {
264                        back = new ColorTransform();
265                        back.color = uint('0x'+view.config['backcolor'].substr(-6));
266                        clip.back.transform.colorTransform = back;
267                }
268                if(view.config['frontcolor']) {
269                        front = new ColorTransform();
270                        front.color = uint('0x'+view.config['frontcolor'].substr(-6));
271                        try {
272                                clip.slider.icon.transform.colorTransform = front;
273                                clip.slider.rail.transform.colorTransform = front;
274                        } catch (err:Error) {}
275                }
276                if(view.config['lightcolor']) {
277                        light = new ColorTransform();
278                        light.color = uint('0x'+view.config['lightcolor'].substr(-6));
279                }
280        };
281
282
283        /** Setup button elements **/
284        private function setContents(idx:Number):void {
285                for (var itm in view.playlist[idx]) {
286                        if(!buttons[idx].c[itm]) {
287                                continue;
288                        } else if(itm == 'image') {
289                                var img = buttons[idx].c.image;
290                                var msk = Draw.rect(buttons[idx].c,'0xFF0000',img.width,img.height,img.x,img.y);
291                                var ldr = new Loader();
292                                img.mask = msk;
293                                img.addChild(ldr);
294                                ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderHandler);
295                                ldr.load(new URLRequest(view.playlist[idx]['image']));
296                        } else if(itm == 'duration') {
297                                if(view.playlist[idx][itm] > 0) {
298                                        buttons[idx].c[itm].text = Strings.digits(view.playlist[idx][itm]);
299                                        if(front) {
300                                                buttons[idx].c[itm].textColor = front.color;
301                                        }
302                                }
303                        } else {
304                                if(itm == 'description') {
305                                        buttons[idx].c[itm].htmlText = view.playlist[idx][itm];
306                                } else {
307                                        buttons[idx].c[itm].text = view.playlist[idx][itm];
308                                }
309                                if(front) {
310                                        buttons[idx].c[itm].textColor = front.color;
311                                }
312                        }
313                }
314                if(buttons[idx].c['back']) {
315                        if(light) {
316                                buttons[idx].c['back'].transform.colorTransform = light;
317                        }
318                        buttons[idx].c['back'].visible = false;
319                }
320                if(!view.playlist[idx]['image'] && buttons[idx].c['image']) {
321                        buttons[idx].c['image'].visible = false;
322                }
323        };
324
325
326        /** Start scrolling the playlist on mousedown. **/
327        private function sdownHandler(evt:MouseEvent):void {
328                clearInterval(scrollInterval);
329        clip.stage.addEventListener(MouseEvent.MOUSE_UP,supHandler);
330                scrollHandler();
331                scrollInterval = setInterval(scrollHandler,50);
332        };
333
334
335        /** Revert the highlight on mouseout. **/
336        private function soutHandler(evt:MouseEvent):void {
337                if(front) {
338                        clip.slider.icon.transform.colorTransform = front;
339                } else {
340                        clip.slider.icon.gotoAndPlay('out');
341                }
342        };
343
344
345        /** Highlight the icon on rollover. **/
346        private function soverHandler(evt:MouseEvent):void {
347                if(light) {
348                        clip.slider.icon.transform.colorTransform = light;
349                } else {
350                        clip.slider.icon.gotoAndPlay('over');
351                }
352        };
353
354
355        /** Stop scrolling the playlist on mouseout. **/
356        private function supHandler(evt:MouseEvent):void {
357                clearInterval(scrollInterval);
358        clip.stage.removeEventListener(MouseEvent.MOUSE_UP,supHandler);
359        };
360
361
362        /** Process state changes **/
363        private function stateHandler(evt:ModelEvent):void {
364                if(view.config['playlist'] == 'over') {
365                        if(evt.data.newstate == ModelStates.PLAYING ||
366                                evt.data.newstate == ModelStates.BUFFERING) {
367                                Animations.fade(clip,0);
368                        } else {
369                                Animations.fade(clip,1);
370                        }
371                }
372        };
373
374
375        /** Process mousewheel usage. **/
376        private function wheelHandler(evt:MouseEvent):void {};
377
378
379};
380
381
382}
Note: See TracBrowser for help on using the repository browser.