source: branches/jw6/src/flash/com/longtailvideo/jwplayer/view/components/DisplayIcon.as @ 2234

Revision 2234, 6.6 KB checked in by pablo, 12 months ago (diff)
  • Moves text to button in display (title, error) in Flash mode
  • Adds a few skin error handling cases
  • Adds completed state in Flash w/ repeat icon in display
Line 
1package com.longtailvideo.jwplayer.view.components {
2        import com.longtailvideo.jwplayer.utils.Draw;
3       
4        import flash.display.*;
5        import flash.events.*;
6        import flash.external.*;
7        import flash.geom.*;
8        import flash.text.*;
9        import flash.utils.*;
10       
11        public class DisplayIcon extends Sprite {
12                // Skin elements
13                private var _icon:DisplayObject;
14                private var _iconOver:DisplayObject;
15                private var _background:DisplayObject;
16                private var _backgroundOver:DisplayObject;
17                private var _capRight:DisplayObject;
18                private var _capRightOver:DisplayObject;
19                private var _capLeft:DisplayObject;
20                private var _capLeftOver:DisplayObject;
21       
22                private var _text:String;
23                private var _textFormat:TextFormat;
24                private var _textFormatOver:TextFormat;
25                private var _textField:TextField;
26                private var _textContainer:Sprite;
27               
28                private var _defaultBGWidth:Number;
29               
30                private var _container:Sprite;
31                private var _iconContainer:Sprite;
32               
33                private var _rotationTimer:Timer;
34                private var _rotationAngle:Number = 0;
35               
36                private var _noText:Boolean = false;
37               
38                public function DisplayIcon(elements:Object, textFormat:TextFormat, overFormat:TextFormat=null) {
39                        _icon = elements.icon ? elements.icon : new Sprite(),
40                        _iconOver = elements.iconOver;
41                       
42                        _background = elements.background ? elements.background : new Sprite();
43                        _backgroundOver = elements.backgroundOver;
44                       
45                        _capLeft = elements.capLeft ? elements.capLeft : new Sprite();
46                        _capLeftOver = elements.capLeftOver;
47
48                        _capRight = elements.capRight ? elements.capRight : new Sprite();
49                        _capRightOver = elements.capRightOver;
50
51                        _textFormat = textFormat;
52                        _textFormatOver = overFormat ? overFormat : new TextFormat(textFormat.font, textFormat.size, textFormat.color, textFormat.bold);
53
54                        if (elements.icon && !(elements.capRight || elements.capLeft)) {
55                                _noText = true;
56                        }
57                       
58                        _container = new Sprite();
59                        _container.mouseChildren = false;
60                        _container.buttonMode = true;
61                        this.mouseEnabled = false;
62                        this.mouseChildren = true;
63
64                        buildIcon();
65
66                        _container.addEventListener(MouseEvent.MOUSE_OVER, _overHandler);
67                        _container.addEventListener(MouseEvent.MOUSE_OUT, _outHandler);
68                }
69               
70                private function buildIcon():void {
71                        _iconContainer = new Sprite();
72                        _textContainer = new Sprite();
73                       
74                        _defaultBGWidth = _icon.width;
75                       
76                        _container.addChild(_capLeft);
77                        if (_capLeftOver) _container.addChild(_capLeftOver);
78                       
79                        _container.addChild(_background);
80                        if (_backgroundOver) _container.addChild(_backgroundOver);
81                       
82                        _container.addChild(_capRight);
83                        if (_capRightOver) _container.addChild(_capRightOver);
84                       
85                        _iconContainer.addChild(_icon);
86                        if (_iconOver) _iconContainer.addChild(_iconOver);
87                       
88                        _textField = new TextField();
89                        _textFormat.align = TextFormatAlign.LEFT;
90                        _textField.selectable = false;
91                        _textField.defaultTextFormat = _textFormat;
92                        _container.addChild(_textField);
93                       
94                        _container.addChild(_iconContainer);
95                        addChild(_container);
96                       
97                        _rotationTimer = new Timer(100);
98                        _rotationTimer.addEventListener(TimerEvent.TIMER, rotationInterval);
99                       
100                        hover(false);
101                }
102               
103                private static var n:Number = 0;
104               
105                private function redraw():void {
106                        positionText();
107                        _background.x = _capLeft.width;
108                        _textField.x = _background.x;
109                       
110                        _icon.x = _icon.width / -2;
111                        _icon.y = _icon.height / -2;
112                        align(_icon, _iconOver);
113
114                        _iconContainer.y = _background.height / 2;
115                       
116                        if (_noText) {
117                                _iconContainer.x = _background.x + _background.width / 2;
118                        } else {
119                                _iconContainer.x = _textField.x + _textField.width + _icon.width / 2;
120                                _background.width = Math.max(_defaultBGWidth, _textField.width + _iconContainer.width);
121                        }
122                        _capRight.x = _background.x + _background.width;
123                       
124                        align(_background, _backgroundOver);
125                        align(_capRight, _capRightOver);
126                        align(_capLeft, _capLeftOver);
127                }
128               
129                private function align(disp1:DisplayObject, disp2:DisplayObject):void {
130                        if (disp1 && disp2) {
131                                disp2.x = disp1.x;
132                                disp2.y = disp1.y;
133                                disp2.width = disp1.width;
134                                disp2.height = disp1.height;
135                        }                       
136                }
137               
138                public function set text(newText:String):void {
139                        _text = newText;
140                        if (!_text || _noText) {
141                                _textField.visible = false;
142                                _textField.width = _textField.height = 0;
143                                redraw();
144                                return;
145                        }
146                       
147                        _textField.visible = true;
148                        _textField.wordWrap = true;
149                        _textField.width = 300;
150                        _textField.text = _text.substr(0, 500);
151
152                        var elipses:Boolean = false;
153                        if (_textField.numLines > 2) {
154                                while(_textField.numLines > 2) {
155                                        elipses = true;
156                                        _textField.text = _textField.text.replace(/(.*).$/, "$1");
157                                }
158                                if (elipses) {
159                                        _textField.text = _textField.text.substr(0, _textField.text.length-3);
160                                        _textField.text = _textField.text.substr(0, _textField.text.lastIndexOf(" ")) + "...";
161                                }
162                        }
163
164                        redraw();
165                }
166               
167                public function get text():String {
168                        return _text;
169                }
170               
171                private function positionText():void {
172                        if (!_textField.text) return;
173                       
174                        var maxY:Number = -1;
175                        var maxX:Number = -1;
176                        var minY:Number = Number.MAX_VALUE;
177                        for (var i:Number = 0; i < _textField.text.length; i++) {
178                                var charDims:Rectangle = _textField.getCharBoundaries(i);
179                                if (charDims.width * charDims.height > 0) {
180                                        maxX = Math.max(maxX, (charDims.x + charDims.width));
181                                        maxY = Math.max(maxY, (charDims.y + charDims.height));
182                                        minY = Math.min(minY, charDims.y);
183                                }
184                        }
185                       
186                        if (maxX > 0 && maxY > 0) {
187                                _textField.width = Math.round(maxX + 5);
188                                _textField.height = Math.round(maxY + 5);
189                                _textField.y = Math.round((_background.height - maxY) / 2 - minY);
190                        } else {
191                                _textField.height = _textField.textHeight;
192                        }
193                }
194               
195                private function _overHandler(evt:MouseEvent):void {
196                        hover(true);
197                }
198
199                private function _outHandler(evt:MouseEvent):void {
200                        hover(false);
201                }
202
203                private function hover(state:Boolean):void {
204                        if (_iconOver) {
205                                _icon.visible = !state;
206                                _iconOver.visible = state;
207                        }
208                        if (_backgroundOver) {
209                                _background.visible = !state;
210                                _backgroundOver.visible = state;
211                        }
212                        if (_capLeftOver) {
213                                _capLeft.visible = !state;
214                                _capLeftOver.visible = state;
215                        }
216                        if (_capRightOver) {
217                                _capRight.visible = !state;
218                                _capRightOver.visible = state;
219                        }
220                        _textField.textColor = Number(state ? _textFormatOver.color : _textFormat.color);
221                }
222
223                public function setRotation(angle:Number, interval:Number=100):void {
224                        _rotationTimer.stop();
225                        _iconContainer.rotation = 0;
226                        _rotationAngle = angle;
227                        if (angle > 0) {
228                                _rotationTimer.delay = interval;
229                                _rotationTimer.start();
230                        }
231                        redraw();
232                }
233               
234                private function rotationInterval(evt:TimerEvent):void {
235                        _iconContainer.rotation += _rotationAngle;
236                }
237               
238        }
239}
Note: See TracBrowser for help on using the repository browser.