| 1 | /** |
|---|
| 2 | * Plugin that renders a dock with buttons (much like Apple's dock) |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.plugins { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import com.jeroenwijering.events.*; |
|---|
| 8 | import com.jeroenwijering.utils.*; |
|---|
| 9 | |
|---|
| 10 | import flash.display.*; |
|---|
| 11 | import flash.events.*; |
|---|
| 12 | import flash.geom.*; |
|---|
| 13 | import flash.utils.* |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | public class Dock implements PluginInterface { |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | /** Configuration vars for this plugin. **/ |
|---|
| 20 | public var config:Object = { |
|---|
| 21 | align:'right' |
|---|
| 22 | }; |
|---|
| 23 | /** Reference to the skin MVC. **/ |
|---|
| 24 | public var clip:MovieClip; |
|---|
| 25 | /** Reference to the MVC view. **/ |
|---|
| 26 | private var view:AbstractView; |
|---|
| 27 | /** Array with all the buttons in the dock. **/ |
|---|
| 28 | private var buttons:Array; |
|---|
| 29 | /** Map with color transformation objects. **/ |
|---|
| 30 | private var colors:Object; |
|---|
| 31 | /** Timeout for hiding the buttons when the video plays. **/ |
|---|
| 32 | private var timeout:Number; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /** Constructor; add all needed listeners. **/ |
|---|
| 36 | public function Dock():void { |
|---|
| 37 | buttons = new Array(); |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * Add a button to the dock. |
|---|
| 43 | * |
|---|
| 44 | * @param icn The icon to display in the button. |
|---|
| 45 | * @param txt The text to display in the button. |
|---|
| 46 | * @param fcn The function to call when the button is clicked |
|---|
| 47 | * @return |
|---|
| 48 | **/ |
|---|
| 49 | public function addButton(icn:DisplayObject,txt:String,hdl:Function):DockButton { |
|---|
| 50 | var btn:DockButton = new DockButton(icn,txt,hdl,colors); |
|---|
| 51 | clip.addChild(btn); |
|---|
| 52 | buttons.push(btn); |
|---|
| 53 | resizeHandler(); |
|---|
| 54 | return btn; |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | /** Initialize the plugin. **/ |
|---|
| 59 | public function initializePlugin(vie:AbstractView):void { |
|---|
| 60 | view = vie; |
|---|
| 61 | if(view.config['dock']) { |
|---|
| 62 | view.addControllerListener(ControllerEvent.RESIZE,resizeHandler); |
|---|
| 63 | view.addModelListener(ModelEvent.STATE,stateHandler); |
|---|
| 64 | clip.stage.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler); |
|---|
| 65 | } else { |
|---|
| 66 | clip.visible = false; |
|---|
| 67 | } |
|---|
| 68 | if(view.config['backcolor'] && view.config['frontcolor']) { |
|---|
| 69 | setColorTransforms(); |
|---|
| 70 | } |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | /** Show the buttons on mousemove. **/ |
|---|
| 75 | private function moveHandler(evt:MouseEvent=null):void { |
|---|
| 76 | clearTimeout(timeout); |
|---|
| 77 | if(view.config['state'] == ModelStates.BUFFERING || |
|---|
| 78 | view.config['state'] == ModelStates.PLAYING) { |
|---|
| 79 | timeout = setTimeout(moveTimeout,2000); |
|---|
| 80 | if(clip.alpha < 1) { |
|---|
| 81 | Animations.fade(clip,1); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | /** Hide the buttons again when move has timed out. **/ |
|---|
| 88 | private function moveTimeout():void { |
|---|
| 89 | Animations.fade(clip,0); |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Remove a button from the dock. |
|---|
| 95 | * |
|---|
| 96 | * @param btn The button to remove. |
|---|
| 97 | **/ |
|---|
| 98 | public function removeButton(btn:DockButton):void { |
|---|
| 99 | for (var i:Number=0; i<buttons.length; i++) { |
|---|
| 100 | if(buttons[i] == btn) { |
|---|
| 101 | buttons.splice(i,1); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | try { |
|---|
| 105 | clip.removeChild(btn); |
|---|
| 106 | } catch (err:Error) {} |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /** Receive resizing requests **/ |
|---|
| 111 | private function resizeHandler(evt:ControllerEvent=null):void { |
|---|
| 112 | clip.y = config['y']; |
|---|
| 113 | if(config['align'] == 'left') { |
|---|
| 114 | clip.x = config['x']; |
|---|
| 115 | } else { |
|---|
| 116 | clip.x = config['x'] + config['width'] - clip.width; |
|---|
| 117 | } |
|---|
| 118 | for (var i:Number=0; i<buttons.length; i++) { |
|---|
| 119 | buttons[i].y = buttons[i].height*i; |
|---|
| 120 | } |
|---|
| 121 | }; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | /** Set color tranformation objects so the buttons can be colorized. **/ |
|---|
| 125 | private function setColorTransforms():void { |
|---|
| 126 | var bck:ColorTransform = new ColorTransform(); |
|---|
| 127 | bck.color = uint('0x'+view.config['backcolor']); |
|---|
| 128 | var frt:ColorTransform = new ColorTransform(); |
|---|
| 129 | frt.color = uint('0x'+view.config['frontcolor']); |
|---|
| 130 | var lgt:ColorTransform = new ColorTransform(); |
|---|
| 131 | if(view.config['lightcolor']) { |
|---|
| 132 | lgt.color = uint('0x'+view.config['lightcolor']); |
|---|
| 133 | } else { |
|---|
| 134 | lgt.color = uint('0x'+view.config['backcolor']); |
|---|
| 135 | } |
|---|
| 136 | colors = {back:frt,front:bck,light:lgt}; |
|---|
| 137 | }; |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | /** Process state changes **/ |
|---|
| 141 | private function stateHandler(evt:ModelEvent=undefined):void { |
|---|
| 142 | switch(view.config['state']) { |
|---|
| 143 | case ModelStates.PLAYING: |
|---|
| 144 | case ModelStates.BUFFERING: |
|---|
| 145 | moveHandler(); |
|---|
| 146 | break; |
|---|
| 147 | default: |
|---|
| 148 | clearTimeout(timeout); |
|---|
| 149 | Animations.fade(clip,1); |
|---|
| 150 | break; |
|---|
| 151 | } |
|---|
| 152 | }; |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | }; |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | } |
|---|