| 1 | package com.longtailvideo.jwplayer.view.components { |
|---|
| 2 | import com.longtailvideo.jwplayer.events.PlayerStateEvent; |
|---|
| 3 | import com.longtailvideo.jwplayer.player.Player; |
|---|
| 4 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 5 | import com.longtailvideo.jwplayer.utils.Animations; |
|---|
| 6 | import com.longtailvideo.jwplayer.utils.RootReference; |
|---|
| 7 | import com.longtailvideo.jwplayer.view.interfaces.IDockComponent; |
|---|
| 8 | |
|---|
| 9 | import flash.display.DisplayObject; |
|---|
| 10 | import flash.events.MouseEvent; |
|---|
| 11 | import flash.utils.clearTimeout; |
|---|
| 12 | import flash.utils.setTimeout; |
|---|
| 13 | import flash.utils.describeType; |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | public class DockComponent extends CoreComponent implements IDockComponent { |
|---|
| 17 | /** Default configuration vars for this plugin. **/ |
|---|
| 18 | public var defaults:Object = { |
|---|
| 19 | align: 'right' |
|---|
| 20 | }; |
|---|
| 21 | /** Object with all the buttons in the dock. **/ |
|---|
| 22 | private var buttons:Array; |
|---|
| 23 | /** Map with color transformation objects. **/ |
|---|
| 24 | private var colors:Object; |
|---|
| 25 | /** Timeout for hiding the buttons when the video plays. **/ |
|---|
| 26 | private var timeout:Number; |
|---|
| 27 | /** Reference to the animations handler **/ |
|---|
| 28 | private var animations:Animations; |
|---|
| 29 | |
|---|
| 30 | public function DockComponent(player:Player) { |
|---|
| 31 | super(player); |
|---|
| 32 | animations = new Animations(this); |
|---|
| 33 | buttons = new Array(); |
|---|
| 34 | if (player.config.dock) { |
|---|
| 35 | player.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler); |
|---|
| 36 | RootReference.stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler); |
|---|
| 37 | } else { |
|---|
| 38 | visible = false; |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | public function addButton(icon:DisplayObject, text:String, clickHandler:Function, name:String = null):void { |
|---|
| 44 | //TODO: Make this work with the existing skin |
|---|
| 45 | var btn:DockButton = new DockButton(icon, text, clickHandler, player.config.frontcolor, player.config.backcolor, player.config.lightcolor); |
|---|
| 46 | addChild(btn); |
|---|
| 47 | buttons[name] = btn; |
|---|
| 48 | resize(width, height); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | public function removeButton(name:String):void { |
|---|
| 53 | try { |
|---|
| 54 | removeChild(buttons[name]); |
|---|
| 55 | } catch (err:Error) { |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | public function resize(width:Number, height:Number):void { |
|---|
| 61 | var margin:Number = 10; |
|---|
| 62 | var usedHeight:Number = margin; |
|---|
| 63 | var direction:Number = 1; |
|---|
| 64 | if (getConfigParam('align') != 'left') { |
|---|
| 65 | direction = -1; |
|---|
| 66 | } |
|---|
| 67 | for (var i:Number = 0; i < buttons.length; i++) { |
|---|
| 68 | var row:Number = Math.floor(usedHeight / height); |
|---|
| 69 | if ((usedHeight + buttons[i].height + margin) > ((row + 1) * height)){ |
|---|
| 70 | usedHeight = ((row + 1) * height) + margin; |
|---|
| 71 | row = Math.floor(usedHeight / height); |
|---|
| 72 | } |
|---|
| 73 | buttons[i].y = usedHeight % height; |
|---|
| 74 | buttons[i].x = (buttons[i].width + margin) * row * direction; |
|---|
| 75 | usedHeight += buttons[i].height + margin; |
|---|
| 76 | } |
|---|
| 77 | setConfigParam('y', player.controls.display.y); |
|---|
| 78 | if (getConfigParam('align') == 'left') { |
|---|
| 79 | setConfigParam('x', player.controls.display.x + margin); |
|---|
| 80 | } else { |
|---|
| 81 | // No need to subtract the width: all of the positions are negative |
|---|
| 82 | setConfigParam('x', player.controls.display.x + player.controls.display.width - buttons[0].width - margin); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | /** Show the buttons on mousemove. **/ |
|---|
| 88 | private function moveHandler(evt:MouseEvent = null):void { |
|---|
| 89 | clearTimeout(timeout); |
|---|
| 90 | if (player.state == PlayerState.BUFFERING || player.state == PlayerState.PLAYING) { |
|---|
| 91 | timeout = setTimeout(moveTimeout, 2000); |
|---|
| 92 | if (alpha < 1) { |
|---|
| 93 | animations.fade(1); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** Hide the buttons again when move has timed out. **/ |
|---|
| 100 | private function moveTimeout():void { |
|---|
| 101 | animations.fade(0); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /** Process state changes **/ |
|---|
| 106 | private function stateHandler(evt:PlayerStateEvent = undefined):void { |
|---|
| 107 | switch (player.state) { |
|---|
| 108 | case PlayerState.PLAYING: |
|---|
| 109 | case PlayerState.BUFFERING: |
|---|
| 110 | moveHandler(); |
|---|
| 111 | break; |
|---|
| 112 | default: |
|---|
| 113 | clearTimeout(timeout); |
|---|
| 114 | animations.fade(1); |
|---|
| 115 | break; |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** Gets a configuration parameter **/ |
|---|
| 120 | private function getConfigParam(param:String):* { |
|---|
| 121 | return _player.config.pluginConfig("dock")[param]; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | private function setConfigParam(param:String, value:*):void { |
|---|
| 125 | _player.config.pluginConfig("dock")[param] = value; |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | } |
|---|