| 1 | package com.longtailvideo.jwplayer.view { |
|---|
| 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.AssetLoader; |
|---|
| 7 | import com.longtailvideo.jwplayer.utils.Logger; |
|---|
| 8 | import com.longtailvideo.jwplayer.utils.RootReference; |
|---|
| 9 | |
|---|
| 10 | import flash.display.Bitmap; |
|---|
| 11 | import flash.display.MovieClip; |
|---|
| 12 | import flash.events.ErrorEvent; |
|---|
| 13 | import flash.events.Event; |
|---|
| 14 | import flash.events.MouseEvent; |
|---|
| 15 | import flash.net.URLRequest; |
|---|
| 16 | import flash.net.navigateToURL; |
|---|
| 17 | import flash.utils.clearTimeout; |
|---|
| 18 | import flash.utils.setTimeout; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | public class Logo extends MovieClip { |
|---|
| 22 | /** Configuration defaults **/ |
|---|
| 23 | protected var defaults:Object = { |
|---|
| 24 | prefix: "http://l.longtailvideo.com/", |
|---|
| 25 | file: "logo.png", |
|---|
| 26 | link: "http://www.longtailvideo.com/players/jw-flv-player/", |
|---|
| 27 | margin: 8, |
|---|
| 28 | out: 0.5, |
|---|
| 29 | over: 1, |
|---|
| 30 | timeout: 3, |
|---|
| 31 | hide: "true", |
|---|
| 32 | position: "bottom-left" |
|---|
| 33 | } |
|---|
| 34 | /** Reference to the player **/ |
|---|
| 35 | protected var _player:IPlayer; |
|---|
| 36 | /** Reference to the current fade timer **/ |
|---|
| 37 | protected var timeout:uint; |
|---|
| 38 | /** Reference to the loader **/ |
|---|
| 39 | protected var loader:AssetLoader; |
|---|
| 40 | /** Animations handler **/ |
|---|
| 41 | protected var animations:Animations; |
|---|
| 42 | |
|---|
| 43 | /** Dimensions **/ |
|---|
| 44 | protected var _width:Number; |
|---|
| 45 | protected var _height:Number; |
|---|
| 46 | |
|---|
| 47 | /** Constructor **/ |
|---|
| 48 | public function Logo(player:IPlayer) { |
|---|
| 49 | super(); |
|---|
| 50 | this.buttonMode = true; |
|---|
| 51 | this.mouseChildren = false; |
|---|
| 52 | animations = new Animations(this); |
|---|
| 53 | _player = player; |
|---|
| 54 | player.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler); |
|---|
| 55 | addEventListener(MouseEvent.CLICK, clickHandler); |
|---|
| 56 | addEventListener(MouseEvent.MOUSE_OVER, overHandler); |
|---|
| 57 | addEventListener(MouseEvent.MOUSE_OUT, outHandler); |
|---|
| 58 | |
|---|
| 59 | loadFile(); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | protected function loadFile():void { |
|---|
| 63 | var versionRE:RegExp = /(\d+)\.(\d+)\./; |
|---|
| 64 | var versionInfo:Array = versionRE.exec(_player.version); |
|---|
| 65 | if (getConfigParam('file') && getConfigParam('prefix')) { |
|---|
| 66 | defaults['file'] = getConfigParam('prefix') + versionInfo[1] + "/" + versionInfo[2] + "/" + getConfigParam('file'); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | if (getConfigParam('file') && RootReference.root.loaderInfo.url.indexOf("http")==0) { |
|---|
| 70 | loader = new AssetLoader(); |
|---|
| 71 | loader.addEventListener(Event.COMPLETE,loaderHandler); |
|---|
| 72 | loader.addEventListener(ErrorEvent.ERROR, errorHandler); |
|---|
| 73 | loader.load(getConfigParam('file')); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** Logo loaded - add to display **/ |
|---|
| 78 | protected function loaderHandler(evt:Event):void { |
|---|
| 79 | if (getConfigParam('hide').toString() == "true") visible = false; |
|---|
| 80 | addChild(loader.loadedObject); |
|---|
| 81 | resize(_width, _height); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** Logo failed to load - die **/ |
|---|
| 85 | protected function errorHandler(evt:ErrorEvent):void { |
|---|
| 86 | Logger.log("Failed to load logo: " + evt.text); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | /** Handles mouse clicks **/ |
|---|
| 91 | protected function clickHandler(evt:MouseEvent):void { |
|---|
| 92 | _player.pause(); |
|---|
| 93 | if (getConfigParam('link')) { |
|---|
| 94 | navigateToURL(new URLRequest(getConfigParam('link'))); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** Handles mouse outs **/ |
|---|
| 99 | protected function outHandler(evt:MouseEvent):void { |
|---|
| 100 | alpha = getConfigParam('out'); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | /** Handles mouse overs **/ |
|---|
| 105 | protected function overHandler(evt:MouseEvent):void { |
|---|
| 106 | alpha = getConfigParam('over'); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /** Handles state changes **/ |
|---|
| 111 | protected function stateHandler(evt:PlayerStateEvent):void { |
|---|
| 112 | if (_player.state == PlayerState.BUFFERING) { |
|---|
| 113 | clearTimeout(timeout); |
|---|
| 114 | show(); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | /** Fade in **/ |
|---|
| 120 | protected function show():void { |
|---|
| 121 | visible = true; |
|---|
| 122 | animations.fade(getConfigParam('out'), 0.1); |
|---|
| 123 | timeout = setTimeout(hide, getConfigParam('timeout') * 1000); |
|---|
| 124 | mouseEnabled = true; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | /** Fade out **/ |
|---|
| 129 | protected function hide():void { |
|---|
| 130 | if (getConfigParam('hide').toString() == "true") { |
|---|
| 131 | mouseEnabled = false; |
|---|
| 132 | animations.fade(0, 0.1); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | /** Resizes the logo **/ |
|---|
| 138 | public function resize(width:Number, height:Number):void { |
|---|
| 139 | _width = width; |
|---|
| 140 | _height = height; |
|---|
| 141 | var image:Bitmap = loader ? (loader.loadedObject as Bitmap) : null; |
|---|
| 142 | var margin:Number = getConfigParam('margin'); |
|---|
| 143 | var position:String = (getConfigParam('position') as String).toLowerCase(); |
|---|
| 144 | if (image) { |
|---|
| 145 | if (position.indexOf('right') >= 0) { |
|---|
| 146 | image.x = _width - image.width - margin; |
|---|
| 147 | } else { |
|---|
| 148 | image.x = margin; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | if (position.indexOf('bottom') >= 0) { |
|---|
| 152 | image.y = _height - image.height - margin; |
|---|
| 153 | } else { |
|---|
| 154 | image.y = margin; |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | /** Gets a configuration parameter **/ |
|---|
| 161 | protected function getConfigParam(param:String):* { |
|---|
| 162 | return defaults[param]; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | } |
|---|