source: branches/captions/src/com/longtailvideo/plugins/captions/Selector.as @ 1808

Revision 1808, 5.9 KB checked in by jeroen, 2 years ago (diff)

added usubs support and up/down scrolling to language selection

  • Property svn:executable set to *
Line 
1package com.longtailvideo.plugins.captions {
2
3
4    import com.longtailvideo.jwplayer.utils.Logger;
5
6    import flash.display.*;
7    import flash.events.*;
8
9
10    /** Component that renders a select box. **/
11    public class Selector extends Sprite {
12
13
14        /** Compiles of the default graphics. **/
15        [Embed(source="../../../../../assets/divider.png")]
16        private const DividerRow:Class;
17        [Embed(source="../../../../../assets/sheet.png")]
18        private const BackSheet:Class;
19
20
21        /** currently active option. **/
22        private var _active:Number;
23        /** Background graphic. **/
24        private var _back:Sprite;
25        /** Container for the buttons. **/
26        private var _container:Sprite;
27        /** Function to call when a selection is made. **/
28        private var _callback:Function;
29        /** Reference to the page down button. **/
30        private var _down:SelectorUpDown;
31        /** Offset of the visible entries. **/
32        private var _offset:Number;
33        /** List with options from the selector. **/
34        private var _options:Array;
35        /** Reference to the page up button. **/
36        private var _up:SelectorUpDown;
37
38
39        /** Constructor. **/
40        public function Selector(callback:Function) {
41            _callback = callback;
42            _back = new Sprite();
43            _back.addChild(new BackSheet());
44            addChild(_back);
45            _back.buttonMode = true;
46            _back.addEventListener(MouseEvent.CLICK,_backHandler);
47            _container = new Sprite();
48            addChild(_container);
49            _container.buttonMode = true;
50            _down = new SelectorUpDown(false,_downHandler);
51            _up = new SelectorUpDown(true,_upHandler);
52        };
53
54
55        /** Background was clicked. **/
56        private function _backHandler(event:MouseEvent):void {
57            _buttonHandler(_active);
58        };
59
60
61        /** Button was clicked. **/
62        private function _buttonHandler(index:Number):void {
63            _options[_active].button.activate(false);
64            _options[index].button.activate(true);
65            _active = index;
66            _callback(index);
67        };
68
69
70        /** The down button was clicked. **/
71        private function _downHandler():void {
72            _offset++;
73            _redraw(false);
74        };
75
76
77        /** Return the max number of buttons that fit in the screen. **/
78        private function _max():Number {
79            return Math.floor(_back.height/27) - 2;
80        };
81
82
83        /** Populate the selector with a number of items. **/
84        public function populate(options:Array,active:Number=0):void {
85            _options = options;
86            _active = active;
87            // Build all the buttons.
88            for(var i:Number=0; i < _options.length; i++) {
89                _options[i].button = new SelectorButton(_buttonHandler,i,_options[i].label);
90            }
91            _options[_active].button.activate(true);
92            // Set a sane default offset.
93            if(_active < _max()) {
94                _offset = 0;
95            } else if(_options.length > _max() && _active > _options.length - _max()) {
96                _offset = _options.length - _max();
97            } else {
98                _offset = _active;
99            }
100            // Redraw the menu.
101            _redraw();
102        };
103
104
105        /** Draw visible portion of the options. **/
106        private function _redraw(show:Boolean=true):void {
107            var button:SelectorButton;
108            var height:Number = 0;
109            var end:Number;
110            var scroll:Boolean;
111            // clear existing buttons.
112            while(_container.numChildren > 0) {
113                _container.removeChildAt(0);
114            }
115            // Check if scrollbars are needed and if offset needs correcting.
116            if(_options.length > _max()) {
117                scroll = true;
118                end = _max();
119            } else {
120                end = _options.length;
121            }
122            if(_offset < 0) {
123                _offset = 0;
124            } else if (scroll && _offset > _options.length - _max()) {
125                _offset = _options.length - _max();
126            }
127            // Draw prev button if needed.
128            if(scroll) {
129                _container.addChild(_up);
130                _up.y = height;
131                _offset == 0 ? _up.alpha = 0: _up.alpha = 1;
132                height += 26;
133                height = _redrawDivider(height);
134            }
135            // Draw new buttons and dividers
136            for(var i:Number = _offset; i < _offset + end; i++) {
137                _container.addChild(_options[i].button);
138                _options[i].button.y = height;
139                height += 26;
140                if(i < _offset + end - 1) {
141                    height = _redrawDivider(height);
142                }
143            }
144            //draw next button if needed
145            if(scroll) {
146                height = _redrawDivider(height);
147                _container.addChild(_down);
148                _down.y = height;
149                _offset == _options.length - _max() ? _down.alpha = 0: _down.alpha = 1;
150                height += 26;
151            }
152            // Rescale button list.
153            _container.x = Math.round(_back.width/2 - _container.width/2);
154            _container.y = Math.round(_back.height/2 - height/2);
155        };
156
157
158        private function _redrawDivider(height:Number):Number {
159            var divider:DisplayObject = new DividerRow();
160            _container.addChild(divider);
161            divider.y = height;
162            return height + 1;
163        };
164
165
166        /** Resize the selector screen. **/
167        public function resize(width:Number,height:Number):void {
168            _back.width = width;
169            _back.height = height;
170            if(_options) {
171                _redraw();
172            }
173        };
174
175
176        /** The up button was clicked. **/
177        private function _upHandler():void {
178            _offset --;
179            _redraw(false);
180        };
181
182
183};
184
185
186}
Note: See TracBrowser for help on using the repository browser.