source: trunk/as3/com/jeroenwijering/plugins/Playlist.as @ 88

Revision 88, 11.4 KB checked in by jeroen, 5 years ago (diff)

merged 4.2 player to trunk

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