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