source: trunk/fl5/src/com/longtailvideo/jwplayer/view/Logo.as @ 801

Revision 801, 5.1 KB checked in by pablo, 3 years ago (diff)
Line 
1package 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                        this.buttonMode = true;
55                        this.mouseChildren = false;
56                        animations = new Animations(this);
57                        _player = player;
58                        player.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler);
59                        addEventListener(MouseEvent.CLICK, clickHandler);
60                        addEventListener(MouseEvent.MOUSE_OVER, overHandler);
61                        addEventListener(MouseEvent.MOUSE_OUT, outHandler);
62                       
63                        loadFile();
64                }
65               
66                protected function loadFile():void {
67                        var versionRE:RegExp = /(\d+)\.(\d+)\./;
68                        var versionInfo:Array = versionRE.exec(_player.version);
69                        if (getConfigParam('file') && getConfigParam('prefix')) {
70                                defaults['file'] = getConfigParam('prefix') + versionInfo[1] + "/" + versionInfo[2] + "/" + getConfigParam('file');
71                        }
72                       
73                        if (getConfigParam('file') && RootReference.root.loaderInfo.url.indexOf("http")==0) {
74                                loader = new Loader();
75                                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderHandler);
76                                loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
77                                loader.load(new URLRequest(getConfigParam('file')),new LoaderContext(true));
78                        }
79                }
80               
81                /** Logo loaded - add to display **/
82                protected function loaderHandler(evt:Event):void {
83                        if (getConfigParam('hide').toString() == "true") visible = false;
84                        addChild(loader);
85                        resize(_width, _height);
86                        try {
87                                Draw.smooth(loader.content as Bitmap);
88                        } catch (e:Error) {
89                                Logger.log("Could not smooth logo: " + e.message);
90                        }
91                }
92               
93                /** Logo failed to load - die **/
94                protected function errorHandler(evt:ErrorEvent):void {
95                        Logger.log("Failed to load logo: " + evt.text);
96                }
97               
98               
99                /** Handles mouse clicks **/
100                protected function clickHandler(evt:MouseEvent):void {
101                        _player.pause();
102                        if (getConfigParam('link')) {
103                                navigateToURL(new URLRequest(getConfigParam('link')));
104                        }
105                }
106               
107                /** Handles mouse outs **/
108                protected function outHandler(evt:MouseEvent):void {
109                        alpha = getConfigParam('out');
110                }
111               
112               
113                /** Handles mouse overs **/
114                protected function overHandler(evt:MouseEvent):void {
115                        alpha = getConfigParam('over');
116                }
117               
118               
119                /** Handles state changes **/
120                protected function stateHandler(evt:PlayerStateEvent):void {
121                        if (_player.state == PlayerState.BUFFERING) {
122                                clearTimeout(timeout);
123                                show();
124                        }
125                }
126               
127               
128                /** Fade in **/
129                protected function show():void {
130                        visible = true;
131                        animations.fade(getConfigParam('out'), 0.1);
132                        timeout = setTimeout(hide, getConfigParam('timeout') * 1000);
133                        mouseEnabled = true;
134                }
135               
136               
137                /** Fade out **/
138                protected function hide():void {
139                        if (getConfigParam('hide').toString() == "true") {
140                                mouseEnabled = false;
141                                animations.fade(0, 0.1);
142                        }
143                }
144               
145               
146                /** Resizes the logo **/
147                public function resize(width:Number, height:Number):void {
148                        _width = width;
149                        _height = height;
150                        var image:DisplayObject = loader ? loader : null;
151                        var margin:Number = getConfigParam('margin');
152                        var position:String = (getConfigParam('position') as String).toLowerCase();
153                        if (image) {
154                                if (position.indexOf('right') >= 0) {
155                                        image.x = _width - image.width - margin;
156                                } else {
157                                        image.x = margin;
158                                }
159                               
160                                if (position.indexOf('bottom') >= 0) {
161                                        image.y = _height - image.height - margin;
162                                } else {
163                                        image.y = margin;
164                                }
165                        }
166                }
167               
168               
169                /** Gets a configuration parameter **/
170                protected function getConfigParam(param:String):* {
171                        return defaults[param];
172                }
173        }
174}
Note: See TracBrowser for help on using the repository browser.