| 1 | package com.longtailvideo.jwplayer.view.components { |
|---|
| 2 | import com.longtailvideo.jwplayer.events.PlayerStateEvent; |
|---|
| 3 | import com.longtailvideo.jwplayer.player.IPlayer; |
|---|
| 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 | import com.longtailvideo.jwplayer.view.skins.SWFSkin; |
|---|
| 9 | |
|---|
| 10 | import flash.display.DisplayObject; |
|---|
| 11 | import flash.display.MovieClip; |
|---|
| 12 | import flash.display.Sprite; |
|---|
| 13 | import flash.events.MouseEvent; |
|---|
| 14 | import flash.utils.clearTimeout; |
|---|
| 15 | import flash.utils.setTimeout; |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | public class DockComponent extends CoreComponent implements IDockComponent { |
|---|
| 19 | /** Default configuration vars for this plugin. **/ |
|---|
| 20 | public var defaults:Object = { |
|---|
| 21 | align: 'right' |
|---|
| 22 | }; |
|---|
| 23 | /** Object with all the buttons in the dock. **/ |
|---|
| 24 | private var buttons:Array; |
|---|
| 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:IPlayer) { |
|---|
| 31 | super(player, "dock"); |
|---|
| 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):MovieClip { |
|---|
| 44 | var button:DockButton = new DockButton(); |
|---|
| 45 | if (name){ |
|---|
| 46 | button.name = name; |
|---|
| 47 | } |
|---|
| 48 | if (_player.skin is SWFSkin) { |
|---|
| 49 | button.colorize = true; |
|---|
| 50 | } |
|---|
| 51 | button.setOutIcon(icon); |
|---|
| 52 | button.outBackground = getSkinElement("button") as Sprite; |
|---|
| 53 | button.overBackground = getSkinElement("buttonOver") as Sprite; |
|---|
| 54 | button.assetColor = fontColor ? fontColor : player.config.backcolor; |
|---|
| 55 | button.outColor = player.config.frontcolor; |
|---|
| 56 | button.overColor = player.config.lightcolor; |
|---|
| 57 | button.clickFunction = clickHandler; |
|---|
| 58 | button.init(); |
|---|
| 59 | button.text = text; |
|---|
| 60 | addChild(button); |
|---|
| 61 | buttons.push(button); |
|---|
| 62 | resize(getConfigParam('width'), getConfigParam('height')); |
|---|
| 63 | return button; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | public function removeButton(name:String):void { |
|---|
| 68 | try { |
|---|
| 69 | removeChild(getChildByName(name)); |
|---|
| 70 | } catch (err:Error) { |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | public function resize(width:Number, height:Number):void { |
|---|
| 76 | if (buttons.length > 0) { |
|---|
| 77 | var margin:Number = 10; |
|---|
| 78 | var xStart:Number = width - buttons[0].width - margin; |
|---|
| 79 | var usedHeight:Number = margin; |
|---|
| 80 | var direction:Number = -1; |
|---|
| 81 | if (getConfigParam('position') == 'left') { |
|---|
| 82 | direction = 1; |
|---|
| 83 | xStart = margin; |
|---|
| 84 | } |
|---|
| 85 | for (var i:Number = 0; i < buttons.length; i++) { |
|---|
| 86 | var row:Number = Math.floor(usedHeight / height); |
|---|
| 87 | if ((usedHeight + buttons[i].height + margin) > ((row + 1) * height)){ |
|---|
| 88 | usedHeight = ((row + 1) * height) + margin; |
|---|
| 89 | row = Math.floor(usedHeight / height); |
|---|
| 90 | } |
|---|
| 91 | buttons[i].y = usedHeight % height; |
|---|
| 92 | buttons[i].x = xStart + (buttons[i].width + margin) * row * direction; |
|---|
| 93 | usedHeight += buttons[i].height + margin; |
|---|
| 94 | (buttons[i] as DockButton).centerText(); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | /** Show the buttons on mousemove. **/ |
|---|
| 101 | private function moveHandler(evt:MouseEvent = null):void { |
|---|
| 102 | clearTimeout(timeout); |
|---|
| 103 | if (player.state == PlayerState.BUFFERING || player.state == PlayerState.PLAYING) { |
|---|
| 104 | timeout = setTimeout(moveTimeout, 2000); |
|---|
| 105 | if (alpha < 1) { |
|---|
| 106 | animations.fade(1); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** Hide the buttons again when move has timed out. **/ |
|---|
| 113 | private function moveTimeout():void { |
|---|
| 114 | animations.fade(0); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | /** Process state changes **/ |
|---|
| 119 | private function stateHandler(evt:PlayerStateEvent = undefined):void { |
|---|
| 120 | switch (player.state) { |
|---|
| 121 | case PlayerState.PLAYING: |
|---|
| 122 | case PlayerState.BUFFERING: |
|---|
| 123 | moveHandler(); |
|---|
| 124 | break; |
|---|
| 125 | default: |
|---|
| 126 | clearTimeout(timeout); |
|---|
| 127 | animations.fade(1); |
|---|
| 128 | break; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|