| 1 | package com.longtailvideo.plugins.captions { |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | import flash.display.*; |
|---|
| 5 | import flash.events.*; |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | /** Component that renders a select box. **/ |
|---|
| 9 | public class Selector extends Sprite { |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | /** Compiles of the default graphics. **/ |
|---|
| 13 | [Embed(source="../../../../../assets/down.png")] |
|---|
| 14 | private const DownIcon:Class; |
|---|
| 15 | [Embed(source="../../../../../assets/divider.png")] |
|---|
| 16 | private const DividerRow:Class; |
|---|
| 17 | [Embed(source="../../../../../assets/up.png")] |
|---|
| 18 | private const UpIcon: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 down clip. **/ |
|---|
| 30 | private var _down:Sprite; |
|---|
| 31 | /** List with options from the selector. **/ |
|---|
| 32 | private var _options:Array; |
|---|
| 33 | /** List with buttons in the container. **/ |
|---|
| 34 | private var _buttons:Array; |
|---|
| 35 | /** Reference to the up clip. **/ |
|---|
| 36 | private var _up:Sprite; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** Constructor. **/ |
|---|
| 40 | public function Selector(callback:Function) { |
|---|
| 41 | _callback = callback; |
|---|
| 42 | _drawBack(); |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /** Background was clicked. **/ |
|---|
| 47 | private function _backHandler(event:MouseEvent):void { |
|---|
| 48 | _buttonHandler(_active); |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | /** Button was clicked. **/ |
|---|
| 53 | private function _buttonHandler(index:Number):void { |
|---|
| 54 | _buttons[_active].activate(false); |
|---|
| 55 | _buttons[index].activate(true); |
|---|
| 56 | _active = index; |
|---|
| 57 | _callback(index); |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /** Draw the background graphic. **/ |
|---|
| 62 | private function _drawBack():void { |
|---|
| 63 | _back = new Sprite(); |
|---|
| 64 | _back.graphics.beginFill(0x000000,0.8); |
|---|
| 65 | _back.graphics.drawRect(0,0,320,240); |
|---|
| 66 | addChild(_back); |
|---|
| 67 | _back.buttonMode = true; |
|---|
| 68 | _back.addEventListener(MouseEvent.CLICK,_backHandler); |
|---|
| 69 | _container = new Sprite(); |
|---|
| 70 | addChild(_container); |
|---|
| 71 | _container.buttonMode = true; |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /** Populate the selector with a number of items. **/ |
|---|
| 76 | public function populate(options:Array,active:Number=0):void { |
|---|
| 77 | var offset:Number = 0; |
|---|
| 78 | var button:SelectorButton; |
|---|
| 79 | var divider:DisplayObject; |
|---|
| 80 | // clear existing buttons. |
|---|
| 81 | while(_container.numChildren > 0) { |
|---|
| 82 | _container.removeChildAt(0); |
|---|
| 83 | } |
|---|
| 84 | _buttons = new Array(); |
|---|
| 85 | // Draw new buttons. |
|---|
| 86 | for(var i:Number=0; i <= options.length; i++) { |
|---|
| 87 | divider = new DividerRow(); |
|---|
| 88 | _container.addChild(divider); |
|---|
| 89 | divider.y = offset; |
|---|
| 90 | offset++; |
|---|
| 91 | if(i < options.length) { |
|---|
| 92 | button = new SelectorButton(_buttonHandler,i,options[i]); |
|---|
| 93 | _buttons.push(button); |
|---|
| 94 | _container.addChild(button); |
|---|
| 95 | button.y = offset; |
|---|
| 96 | offset += button.height; |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | // Select initial option and set dimensions. |
|---|
| 100 | _buttons[active].activate(true); |
|---|
| 101 | _active = active; |
|---|
| 102 | resize(_back.width, _back.height); |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | /** Resize the selector screen. **/ |
|---|
| 107 | public function resize(width:Number,height:Number):void { |
|---|
| 108 | _back.width = width; |
|---|
| 109 | _back.height = height; |
|---|
| 110 | _container.x = Math.round(width/2 - _container.width/2); |
|---|
| 111 | _container.y = Math.round(height/2 - _container.height/2); |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | }; |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | } |
|---|