source: branches/jw6/src/flash/com/longtailvideo/jwplayer/view/components/DisplayComponent.as @ 2198

Revision 2198, 13.9 KB checked in by pablo, 12 months ago (diff)

Adds onDisplayClick event; removes "displayclick" option

Line 
1package com.longtailvideo.jwplayer.view.components {
2        import com.longtailvideo.jwplayer.events.ComponentEvent;
3        import com.longtailvideo.jwplayer.events.MediaEvent;
4        import com.longtailvideo.jwplayer.events.PlayerEvent;
5        import com.longtailvideo.jwplayer.events.PlayerStateEvent;
6        import com.longtailvideo.jwplayer.events.PlaylistEvent;
7        import com.longtailvideo.jwplayer.events.ViewEvent;
8        import com.longtailvideo.jwplayer.player.IPlayer;
9        import com.longtailvideo.jwplayer.player.PlayerState;
10        import com.longtailvideo.jwplayer.utils.Draw;
11        import com.longtailvideo.jwplayer.utils.Strings;
12        import com.longtailvideo.jwplayer.view.interfaces.IDisplayComponent;
13        import com.longtailvideo.jwplayer.view.skins.PNGSkin;
14       
15        import flash.display.Bitmap;
16        import flash.display.DisplayObject;
17        import flash.display.MovieClip;
18        import flash.display.Sprite;
19        import flash.events.MouseEvent;
20        import flash.events.TimerEvent;
21        import flash.geom.ColorTransform;
22        import flash.geom.Rectangle;
23        import flash.net.URLRequest;
24        import flash.net.navigateToURL;
25        import flash.text.GridFitType;
26        import flash.text.TextField;
27        import flash.text.TextFormat;
28        import flash.text.TextFormatAlign;
29        import flash.utils.Timer;
30        import flash.utils.clearInterval;
31        import flash.utils.setInterval;
32
33        /**
34         * Sent when the display icon begins to become visible
35         *
36         * @eventType com.longtailvideo.jwplayer.events.ComponentEvent.JWPLAYER_COMPONENT_SHOW
37         */
38        [Event(name="jwPlayerComponentShow", type="com.longtailvideo.jwplayer.events.ComponentEvent")]
39        /**
40         * Sent when the display icon begins to hide
41         *
42         * @eventType com.longtailvideo.jwplayer.events.ComponentEvent.JWPLAYER_COMPONENT_HIDE
43         */
44        [Event(name="jwPlayerComponentHide", type="com.longtailvideo.jwplayer.events.ComponentEvent")]
45       
46        public class DisplayComponent extends CoreComponent implements IDisplayComponent {
47                protected var _icon:DisplayObject;
48                protected var _iconArea:Rectangle;
49                protected var _background:MovieClip;
50                protected var _overlay:Sprite;
51                protected var _text:TextField;
52                protected var _textBack:Sprite;
53                protected var _icons:Object;
54                protected var _rotateInterval:Number;
55                protected var _bufferIcon:Sprite;
56                protected var _rotate:Boolean = true;
57                protected var _youtubeMask:MovieClip;
58               
59                protected var _bufferRotationTime:Number = 100;
60                protected var _bufferRotationAngle:Number = 15;
61               
62                protected var _bufferStateTimer:Timer;
63                protected var _playStateTimer:Timer;
64                protected var _previousState:String;
65               
66                protected var _forced:String = "";
67               
68                public function DisplayComponent(player:IPlayer) {
69                        super(player, "display");
70                        addListeners();
71                        setupDisplayObjects();
72                        setupIcons();
73                        if (!isNaN(getConfigParam('bufferrotation'))) _bufferRotationAngle = Number(getConfigParam('bufferrotation'));
74                        if (!isNaN(getConfigParam('bufferinterval'))) _bufferRotationTime = Number(getConfigParam('bufferinterval'));
75                       
76                        _bufferStateTimer = new Timer(50, 1);
77                        _bufferStateTimer.addEventListener(TimerEvent.TIMER_COMPLETE, showBufferIcon);
78                       
79                        _playStateTimer = new Timer(50, 1);
80                        _playStateTimer.addEventListener(TimerEvent.TIMER_COMPLETE, showPlayIcon);
81                }
82               
83               
84                private function itemHandler(evt:PlaylistEvent):void {
85                        _playStateTimer.delay = (_icon ? 10 : 50);
86                        _playStateTimer.start();
87                        if (background) {
88                                if (_player.playlist.currentItem && _player.playlist.currentItem.provider == "youtube") {
89                                        background.mask = _youtubeMask;
90                                } else {
91                                        background.mask = null;
92                                }
93                        }
94                }
95               
96
97                private function addListeners():void {
98                        player.addEventListener(MediaEvent.JWPLAYER_MEDIA_MUTE, stateHandler);
99                        player.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler);
100                        player.addEventListener(PlayerEvent.JWPLAYER_ERROR, errorHandler);
101                        player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, itemHandler);
102                        addEventListener(MouseEvent.CLICK, clickHandler);
103                        this.buttonMode = true;
104                }
105               
106               
107                private function setupDisplayObjects():void {
108                        _background = new MovieClip();
109                        background.name = "background";
110                        addChildAt(background, 0);
111                        background.graphics.beginFill(0, 0);
112                        background.graphics.drawRect(0, 0, 1, 1);
113                        background.graphics.endFill();
114                       
115                        _overlay = new Sprite();
116                        _overlay.name = "overlay";
117                        addChildAt(_overlay, 1);
118                       
119                        _textBack = new Sprite();
120                        _textBack.name = "textBackground";
121                        _textBack.graphics.beginFill(0, 0.8);
122                        _textBack.graphics.drawRect(0, 0, 1, 1);
123                        _textBack.visible = false;
124                        _overlay.addChild(_textBack);
125
126                        _text = new TextField();
127                        text.gridFitType = GridFitType.NONE;
128                        text.defaultTextFormat = new TextFormat("_sans", null, 0xFFFFFF);
129                        _overlay.addChild(text);
130                       
131                        _youtubeMask = new MovieClip();
132                }
133               
134               
135                protected function setupIcons():void {
136                        _icons = {};
137                        setupIcon('buffer');
138                        setupIcon('play');
139                        setupIcon('mute');
140                }
141               
142               
143                /**
144                 * Takes in an icon from a PNG skin and rearranges its children so that it's centered around 0, 0
145                 */
146                protected function centerIcon(icon:Sprite):void {
147                        if (icon) {
148                                for (var i:Number=0; i < icon.numChildren; i++) {
149                                        icon.getChildAt(i).x = -Math.round(icon.getChildAt(i).width)/2;
150                                        icon.getChildAt(i).y = -Math.round(icon.getChildAt(i).height)/2;
151                                }
152                        }
153                }
154               
155                protected function setupIcon(name:String):void {
156                        var iconElement:Sprite = getSkinElement(name + 'Icon') as Sprite;
157                        var iconOver:Sprite = getSkinElement(name + 'IconOver') as Sprite;
158
159                        if (!iconElement) { return; }
160                       
161                        if (_player.skin is PNGSkin) {
162                                if (iconElement.getChildByName("bitmap")) {
163                                        centerIcon(iconElement);
164                                        iconElement.name = 'out';
165                                }
166                                if (iconOver && iconOver.getChildByName("bitmap")) {
167                                        centerIcon(iconOver);
168                                        iconOver.name = 'over';
169                                }
170                        }
171                       
172                        if (name == "buffer") {
173                                if (player.skin is PNGSkin) {
174                                        if (iconElement is MovieClip && (iconElement as MovieClip).totalFrames > 1) {
175                                                // Buffer is already animated; no need to rotate.
176                                                _rotate = false;
177                                        } else {
178                                                try {
179                                                        _bufferIcon = iconElement;
180                                                        var bufferBitmap:Bitmap = _bufferIcon.getChildByName('bitmap') as Bitmap;
181                                                        if (bufferBitmap) {
182                                                                Draw.smooth(bufferBitmap);
183                                                        } else {
184                                                                centerIcon(iconElement);
185                                                        }
186                                                } catch (e:Error) {
187                                                        _rotate = false;
188                                                }
189                                        }
190                                } else {
191                                        _rotate = false;
192                                }
193                        }
194                       
195                        var back:Sprite = getSkinElement('background') as Sprite;
196                        if (back) {
197                                if (_player.skin is PNGSkin) centerIcon(back);
198                        } else {
199                                back = new Sprite();
200                        }
201
202                        if (iconOver && player.skin is PNGSkin && name != "buffer") {
203                                iconOver.visible = false;
204                                back.addChild(iconOver);
205                                back.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
206                                back.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
207                        }
208                        back.addChild(iconElement);
209                        if (player.skin is PNGSkin && !iconElement.getChildByName("bitmap")) {
210                                if (name != "buffer" || !_rotate) {
211                                        centerIcon(back);
212                                }
213                        } else {
214                                back.x = back.y = iconElement.x = iconElement.y = 0;
215                        }
216                        _icons[name] = back;
217
218                }
219               
220                protected function overHandler(evt:MouseEvent):void {
221                        var button:Sprite = _icon as Sprite;
222                        if (button) {
223                                setIconHover(button, true);
224                        }
225                }
226
227                protected function outHandler(evt:MouseEvent):void {
228                        var button:Sprite = _icon as Sprite;
229                        if (button) {
230                                setIconHover(button, false);
231                        }
232                }
233               
234                protected function setIconHover(icon:Sprite, state:Boolean):void {
235                        var over:DisplayObject = icon.getChildByName('over');
236                        var out:DisplayObject = icon.getChildByName('out');
237                       
238                        if (over && out) {
239                                over.visible = state;
240                                out.visible = !state;
241                        }               
242                }
243               
244                override public function resize(width:Number, height:Number):void {
245                        _background.width = width;
246                        _background.height = height;
247                       
248                        _youtubeMask.graphics.clear();
249                        _youtubeMask.graphics.beginFill(0x00AA00, 0.3);
250                        _youtubeMask.graphics.drawRect(0, 0, width, height - 100);
251                        _youtubeMask.graphics.endFill();
252
253                        positionIcon();
254                        positionText();
255                        stateHandler();
256                }
257               
258               
259                public function setIcon(displayIcon:DisplayObject):void {
260                        var sendShowEvent:Boolean = false;
261                        var sendHideEvent:Boolean = false;
262                        try {
263                                if (_icon && _icon.parent == _overlay) {
264                                        _overlay.removeChild(_icon);
265                                        _icon = null;
266                                        sendHideEvent = !_hiding;
267                                } else {
268                                        sendShowEvent = !_hiding;
269                                }
270                        } catch (err:Error) {
271                        }
272                        if (_fullscreen != _player.config.fullscreen) {
273                                _fullscreen = _player.config.fullscreen;
274                                sendShowEvent = true;
275                        }
276                        if (displayIcon && _player.config.icons && (getConfigParam("icons") === true || typeof(getConfigParam("icons")) == "undefined")) {
277                                if (displayIcon is Sprite) {
278                                        setIconHover(displayIcon as Sprite, false);
279                                }
280                                _icon = displayIcon;
281                                _overlay.addChild(_icon);
282                                positionIcon();
283                                _iconArea = _icon.getRect(_overlay);
284
285                                if (sendShowEvent) {
286                                        sendShow();
287                                }
288                        } else {
289                                if (sendHideEvent) {
290                                        sendHide();
291                                }
292                                _iconArea = null;
293                        }
294                }
295               
296               
297                private function positionIcon():void {
298                        if (_icon) {
299                                _icon.x = background.scaleX / 2;
300                                _icon.y = background.scaleY / 2;
301                        }
302                }
303               
304               
305                public function setText(displayText:String):void {
306                        if (_icon is Sprite && (_icon as Sprite).getChildByName('txt') is TextField) {
307                                ((_icon as Sprite).getChildByName('txt') as TextField).text = displayText ? displayText : '';
308                                text.text = '';
309                        } else {
310                                text.text = displayText ? displayText : '';
311                        }
312                        positionText();
313                }
314               
315               
316                private function positionText():void {
317                        if (text.text) {
318                                text.visible = true;
319                                _textBack.visible = true;
320                                if (text.width > background.scaleX * .75) {
321                                        text.width = background.scaleX * .75;
322                                        text.wordWrap = true;
323                                } else {
324                                        text.autoSize = TextFormatAlign.CENTER;
325                                }
326                                text.x = (background.scaleX - text.textWidth) / 2;
327                                if (_icon && contains(_icon)) {
328                                        text.y = _icon.y + (_icon.height/2) + 10;
329                                } else {
330                                        text.y = (background.scaleY - text.textHeight) / 2;
331                                }
332                                _textBack.y = text.y - 2;
333                                _textBack.width = getConfigParam('width');
334                                _textBack.height = text.height + 4;
335                        } else {
336                                text.visible = false;
337                                _textBack.visible = false;
338                        }
339                }
340               
341               
342                protected function setDisplay(displayIcon:DisplayObject, displayText:String = null):void {
343                        setIcon(displayIcon);
344                        setText(displayText != null ? displayText : text.text);
345                }
346               
347               
348                protected function clearDisplay():void {
349                        setDisplay(null, '');
350                }
351               
352                protected function get currentState():String {
353                        return (_forced ? _forced : (_player ? _player.state : PlayerState.IDLE));
354                }
355               
356                protected function stateHandler(event:PlayerEvent = null):void {
357                        if (_previousState != currentState || !(event is PlayerStateEvent)) {
358                                _previousState = currentState;
359                                //TODO: Handle mute button in error state
360                                clearRotation();
361                                _bufferStateTimer.reset();
362                                _playStateTimer.reset();
363                                _bufferStateTimer.delay = (_icon ? 10 : 200);
364                                _playStateTimer.delay = (_icon ? 10 : 10);
365                                switch (currentState) {
366                                        case PlayerState.BUFFERING:
367                                                _bufferStateTimer.start();
368                                                break;
369                                        case PlayerState.PAUSED:
370                                        case PlayerState.IDLE:
371                                                _playStateTimer.start();
372                                                break;
373                                        default:
374                                                if ( player.config.mute && getConfigParam("showmute") ) {
375                                                        setDisplay(_icons['mute']);
376                                                } else {
377                                                        clearDisplay();
378                                                }
379                                }
380                        }
381                }
382               
383                protected function showBufferIcon(evt:TimerEvent):void {
384                        setDisplay(_icons['buffer'], '');
385                        if (_rotate){
386                                startRotation();
387                        }
388                }
389
390                protected function showPlayIcon(evt:TimerEvent):void {
391                        setDisplay(_icons['play']);
392                }
393
394                protected function startRotation():void {
395                        if (!_rotateInterval && (_bufferRotationAngle % 360) != 0) {
396                                _rotateInterval = setInterval(updateRotation, _bufferRotationTime);
397                        }
398                }
399               
400               
401                protected function updateRotation():void {
402                        if (_bufferIcon) _bufferIcon.rotation += _bufferRotationAngle;
403                }
404               
405               
406                protected function clearRotation():void {
407                        if (_bufferIcon) _bufferIcon.rotation = 0;
408                        if (_rotateInterval) {
409                                clearInterval(_rotateInterval);
410                                _rotateInterval = undefined;
411                        }
412                }
413               
414               
415                protected function errorHandler(event:PlayerEvent):void {
416                        setDisplay(null, event.message);
417                }
418               
419               
420                protected function clickHandler(event:MouseEvent):void {
421                        dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_CLICK));
422                        if (currentState == PlayerState.PLAYING || currentState == PlayerState.BUFFERING) {
423                                dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_PAUSE));
424                        } else {
425                                dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_PLAY));
426                        }
427                }
428               
429               
430                protected function get text():TextField {
431                        return _text;
432                }
433               
434               
435                protected function get background():MovieClip {
436                        return _background;
437                }
438               
439               
440                /** Hide the display icon **/
441                public override function hide():void {
442                        if (_overlay) {
443                                _overlay.visible = false;
444                        }
445                        if (!_hiding) {
446                                if (_icon) {
447                                        sendHide();
448                                }
449                                _hiding = true;
450                        }
451                }
452               
453                /** Show the display icon **/
454                public override function show():void {
455                        if (_overlay) {
456                                _overlay.visible = true;
457                        }
458                        if (_hiding) {
459                                if (_icon) {
460                                        sendShow();
461                                }
462                                _hiding = false;
463                        }
464                }
465               
466                public function forceState(forcedState:String):void {
467                        switch(forcedState) {
468                                case PlayerState.BUFFERING:
469                                case PlayerState.PAUSED:
470                                case PlayerState.IDLE:
471                                case PlayerState.PLAYING:
472                                        _forced = forcedState;
473                                        stateHandler();
474                                        break;
475                                default:
476                                        _forced = "";
477                        }
478                       
479                }
480               
481                public function releaseState():void {
482                        _forced = "";
483                        stateHandler();
484                }
485               
486                protected override function get displayRect():Rectangle {
487                        return _iconArea ? _iconArea : super.displayRect;
488                }
489               
490        }
491}
Note: See TracBrowser for help on using the repository browser.