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