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