Ignore:
Timestamp:
06/06/08 13:11:42 (5 years ago)
Author:
jeroen
Message:

implemented stacker; display of media and some controlbaritems still broken

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/as3/com/jeroenwijering/views/PlaylistView.as

    r3 r4  
    77import com.jeroenwijering.events.*; 
    88import com.jeroenwijering.player.View; 
    9 import com.jeroenwijering.utils.Draw; 
     9import com.jeroenwijering.utils.*; 
     10import flash.display.Loader; 
    1011import flash.display.MovieClip; 
     12import flash.events.Event; 
    1113import flash.events.MouseEvent; 
    1214import flash.geom.Rectangle; 
     15import flash.net.URLRequest; 
    1316import flash.utils.setInterval; 
    1417import flash.utils.clearInterval; 
     
    2528        private var buttons:Array; 
    2629        /** Height of a button (to calculate scrolling) **/ 
    27         private var buttonsize:Number; 
     30        private var buttonheight:Number; 
     31        /** Currently active button. **/ 
     32        private var active:Number; 
    2833        /** Proportion between clip and mask. **/ 
    2934        private var proportion:Number; 
     
    3439        public function PlaylistView(vie:View) { 
    3540                view = vie; 
    36                 clip = view.skin['playlist']; 
    37                 clip.visible = false; 
    3841                view.addControllerListener(ControllerEvent.ITEM,itemHandler); 
    3942                view.addControllerListener(ControllerEvent.PLAYLIST,playlistHandler); 
    4043                view.addControllerListener(ControllerEvent.RESIZE,resizeHandler); 
    41                 buttonsize = clip.scrollClip.getChildByName('button').height; 
    42                 clip.list.mask = clip.scrollMask; 
     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; 
    4349                clip.slider.buttonMode = true; 
    4450                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); 
    4555                clip.slider.addEventListener(MouseEvent.MOUSE_DOWN,startHandler); 
    46                 view.skin.addEventListener(MouseEvent.MOUSE_UP,stopHandler); 
     56                clip.visible = false; 
     57                trace(clip); 
    4758        }; 
    4859 
     
    5263                var wid = clip.back.width; 
    5364                var hei = clip.back.height; 
    54                 proportion = view.playlist.length*buttonsize/hei; 
     65                proportion = view.playlist.length*buttonheight/hei; 
    5566                if (proportion > 1) { 
    5667                        wid -=20; 
    57                         buildScroller(); 
     68                        buildSlider(); 
    5869                } else { 
    59                         clip.scrollBar.visible = false; 
    60                 } 
    61                 clip.scrollMask.height = hei; 
    62                 clip.scrollMask.width = wid; 
     70                        clip.slider.visible = false; 
     71                } 
     72                clip.masker.height = hei; 
     73                clip.masker.width = wid; 
    6374                if(clr) { 
    64                         clip.scrollClip.y = 0; 
    65                         Draw.clear(clip.scrollClip); 
     75                        clip.list.y = 0; 
     76                        Draw.clear(clip.list); 
    6677                        buttons = new Array(); 
     78                        clip.visible= true; 
     79                } else {  
     80                        if(proportion > 1) { scrollCheck(); } 
    6781                } 
    6882                for(var i=0; i<view.playlist.length; i++) { 
    69                         if(clr) {  
    70                                 var btn = Draw.clone(clip.scrollClip.getChildByName('button'));  
    71                                 // new PlaylistButton(i,wid,view); 
    72                                 clip.scrollClip.addChild(btn); 
    73                                 buttons.push(btn); 
    74                         } else {  
    75                                 buttons[i].resize(wid); 
    76                                 if(proportion > 1) {  
    77                                         scrollCheck(); 
    78                                 } 
    79                         } 
     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); 
    8095                } 
    8196        }; 
     
    8398 
    8499        /** Setup the scrollbar component **/ 
    85         private function buildScroller() { 
    86                 var scr = clip.scrollBar; 
     100        private function buildSlider() { 
     101                var scr = clip.slider; 
    87102                scr.visible = true; 
    88                 scr.x = clip.back.width - scr.width; 
    89                 var dif = clip.back.height - scr.height; 
     103                scr.x = clip.back.width-scr.width; 
     104                var dif = clip.back.height-scr.height; 
    90105                scr.back.height += dif; 
    91106                scr.rail.height += dif; 
     
    94109 
    95110 
     111        /** Handle a click on a button. **/ 
     112        private function clickHandler(evt:MouseEvent) { 
     113                view.sendEvent('item',Number(evt.target.name)); 
     114        }; 
     115 
     116 
    96117        /** Switch the currently active item */ 
    97118        private function itemHandler(evt:ControllerEvent) { 
    98                 // code for highlighting a certain button. 
     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                } 
    99150        }; 
    100151 
     
    102153        /** New playlist loaded: rebuild the playclip. **/ 
    103154        private function playlistHandler(evt:ControllerEvent) { 
    104                 buildList(true); 
     155                if(view.config['playlist'] != 'none') {  
     156                        buildList(true); 
     157                } 
    105158        }; 
    106159 
     
    109162        private function resizeHandler(evt:ControllerEvent) { 
    110163                if(view.config['playlist'] == 'right') { 
    111                         clip.visible = true; 
    112164                        clip.x = evt.data.width; 
    113165                        clip.y = 0; 
    114166                        clip.back.width = view.config['playlistsize']; 
    115167                        clip.back.height = evt.data.height; 
    116                 } else if (view.config['playlist'] == 'below') { 
    117                         clip.visible = true; 
     168                } else if (view.config['playlist'] == 'bottom') { 
    118169                        clip.x = 0; 
    119170                        clip.y = evt.data.height; 
    120                         if (view.config['controlbar'] == 'below') { 
     171                        if (view.config['controlbar'] == 'bottom') { 
    121172                                clip.y += view.config['controlbarsize']; 
    122173                        } 
    123174                        clip.back.height = view.config['playlistsize']; 
    124175                        clip.back.width = evt.data.width; 
    125                 } else if (view.config['playlist'] == 'above') { 
    126                         clip.visible = true; 
    127                         var wid = evt.data.width-2*view.config['controlbarsize']; 
    128                         var hei = evt.data.height-2*view.config['controlbarsize']; 
    129                         if(evt.data.width > 640) { wid = 600; } 
    130                         if(view.config['controlbar'] == 'above') { hei -= 2*view.config['controlbarsize']; } 
    131                         clip.x = Math.round(evt.data.width/2 - wid/2); 
    132                         clip.y = view.config['controlbarsize']; 
    133                         clip.back.height = hei; 
    134                         clip.back.width = wid; 
    135                 } else {  
    136                         clip.visible = false; 
     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; 
    137180                } 
    138181                buildList(false); 
     
    142185        /** Make sure the playlist is not out of range. **/ 
    143186        private function scrollCheck() { 
    144                 var scr = clip.scrollBar; 
    145                 if(clip.scrollClip.y > 0) { 
    146                         clip.scrollClip.y = 0; 
     187                var scr = clip.slider; 
     188                if(clip.list.y > 0) { 
     189                        clip.list.y = 0; 
    147190                        scr.icon.y = scr.rail.y; 
    148                 } else if (clip.scrollClip.y < clip.scrollMask.height-clip.scrollClip.height) { 
     191                } else if (clip.list.y < clip.masker.height-clip.list.height) { 
    149192                        scr.icon.y = scr.rail.y+scr.rail.height-scr.icon.height; 
    150                         clip.scrollClip.y = clip.scrollMask.height-clip.scrollClip.height; 
     193                        clip.list.y = clip.masker.height-clip.list.height; 
    151194                } 
    152195        }; 
     
    155198        /** Scrolling handler. **/ 
    156199        private function scrollHandler() { 
    157                 var scr = clip.scrollBar; 
     200                var scr = clip.slider; 
    158201                var yps = scr.mouseY; 
    159202                var ips = yps - scr.icon.height/2; 
    160                 var cps = clip.scrollMask.y+clip.scrollMask.height/2-proportion*yps; 
     203                var cps = clip.masker.y+clip.masker.height/2-proportion*yps; 
    161204                scr.icon.y = Math.round(ips - (ips-scr.icon.y)/1.5); 
    162                 clip.scrollClip.y = Math.round((cps - (cps-clip.scrollClip.y)/1.5)); 
     205                clip.list.y = Math.round((cps - (cps-clip.list.y)/1.5)); 
    163206                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                } 
    164234        }; 
    165235 
     
    172242        }; 
    173243 
     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 
    174255 
    175256        /** Stop scrolling the playlist. **/ 
     
    179260 
    180261 
    181  
    182  
    183262}; 
    184263 
Note: See TracChangeset for help on using the changeset viewer.