1 | import mx.utils.Delegate; |
---|
2 | |
---|
3 | class Volume extends MovieClip |
---|
4 | { |
---|
5 | public var icon_mc:MovieClip; |
---|
6 | public var control_mc:MovieClip; |
---|
7 | public var background_mc:MovieClip; |
---|
8 | public var button_mc:MovieClip; |
---|
9 | |
---|
10 | public var realWidth:Number; |
---|
11 | |
---|
12 | private var _settingVolume:Boolean; |
---|
13 | private var _initialMaskPos:Number; |
---|
14 | |
---|
15 | public var addListener:Function; |
---|
16 | public var removeListener:Function; |
---|
17 | private var broadcastMessage:Function; |
---|
18 | |
---|
19 | private var _clearID:Number; |
---|
20 | |
---|
21 | private var _rtl:Boolean = false; |
---|
22 | |
---|
23 | /** |
---|
24 | * Constructor |
---|
25 | */ |
---|
26 | function Volume() |
---|
27 | { |
---|
28 | AsBroadcaster.initialize(this); |
---|
29 | |
---|
30 | control_mc._alpha = 0; |
---|
31 | this.button_mc._visible = false; |
---|
32 | icon_mc._alpha = 100; |
---|
33 | |
---|
34 | _settingVolume = false; |
---|
35 | |
---|
36 | _initialMaskPos = this.control_mc.mask_mc._x; |
---|
37 | |
---|
38 | this.realWidth = this.background_mc._width; |
---|
39 | |
---|
40 | this.button_mc.onPress = Delegate.create(this, function() { |
---|
41 | this._settingVolume = true; |
---|
42 | this._moveVolumeBar(); |
---|
43 | }); |
---|
44 | this.button_mc.onMouseMove = Delegate.create(this, function() { |
---|
45 | if(this._settingVolume) |
---|
46 | { |
---|
47 | this._moveVolumeBar(); |
---|
48 | this.broadcastMessage("onSetVolume", this._getValue(), false); |
---|
49 | } |
---|
50 | }); |
---|
51 | this.button_mc.onRelease = this.button_mc.onReleaseOutside = Delegate.create(this, function() { |
---|
52 | this._settingVolume = false; |
---|
53 | this._moveVolumeBar(); |
---|
54 | this.broadcastMessage("onSetVolume", this._getValue(), true); |
---|
55 | }); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Updates volume |
---|
60 | */ |
---|
61 | public function update(volume:Number):Void |
---|
62 | { |
---|
63 | if(!_settingVolume) this.control_mc.mask_mc._x = _initialMaskPos + Math.round(this.control_mc.track_mc._width * volume / 100); |
---|
64 | } |
---|
65 | |
---|
66 | private function _moveVolumeBar():Void |
---|
67 | { |
---|
68 | if(this.control_mc.track_mc._xmouse > this.control_mc.track_mc._width) this.control_mc.mask_mc._x = _initialMaskPos + this.control_mc.track_mc._width; |
---|
69 | else if(this.control_mc.track_mc._xmouse < 0) this.control_mc.mask_mc._x = _initialMaskPos; |
---|
70 | else this.control_mc.mask_mc._x = _initialMaskPos + this.control_mc.track_mc._xmouse; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Returns the current position of the volume slider as a percentage |
---|
75 | * @return number between 0 and 100 |
---|
76 | */ |
---|
77 | private function _getValue():Number |
---|
78 | { |
---|
79 | return Math.round((this.control_mc.mask_mc._x - _initialMaskPos) / this.control_mc.track_mc._width * 100); |
---|
80 | } |
---|
81 | |
---|
82 | public function toggleControl(toggle:Boolean, immediate:Boolean):Void |
---|
83 | { |
---|
84 | clearInterval(_clearID); |
---|
85 | if(toggle) _clearID = setInterval(this, "_animate", 41, 100, 0, _rtl ? 11 : 6); |
---|
86 | else _clearID = setInterval(this, "_animate", 41, 0, 100, _rtl ? 21 : 16); |
---|
87 | } |
---|
88 | |
---|
89 | private function _animate(targetControl:Number, targetIcon:Number, targetIconX:Number):Void |
---|
90 | { |
---|
91 | var dAlphaControl:Number = targetControl - control_mc._alpha; |
---|
92 | var dAlphaIcon:Number = targetIcon - icon_mc._alpha; |
---|
93 | var dAlphaIconX:Number = targetIconX - icon_mc._x; |
---|
94 | var speed:Number = 0.3; |
---|
95 | |
---|
96 | dAlphaControl = dAlphaControl * speed; |
---|
97 | dAlphaIcon = dAlphaIcon * speed; |
---|
98 | dAlphaIconX = dAlphaIconX * speed; |
---|
99 | |
---|
100 | // Stop animation when we are at less than a pixel from the target |
---|
101 | if(Math.abs(dAlphaControl) < 1) |
---|
102 | { |
---|
103 | // Position the control element to the exact target position |
---|
104 | control_mc._alpha = targetControl; |
---|
105 | icon_mc._alpha = targetIcon; |
---|
106 | icon_mc._x = targetIconX; |
---|
107 | |
---|
108 | button_mc._visible = (control_mc._alpha == 100); |
---|
109 | |
---|
110 | clearInterval(_clearID); |
---|
111 | return; |
---|
112 | } |
---|
113 | |
---|
114 | control_mc._alpha += dAlphaControl; |
---|
115 | icon_mc._alpha += dAlphaIcon; |
---|
116 | icon_mc._x += dAlphaIconX; |
---|
117 | } |
---|
118 | |
---|
119 | public function flip():Void { |
---|
120 | _rtl = true; |
---|
121 | |
---|
122 | this.background_mc._rotation = 180; |
---|
123 | this.background_mc._y = this.background_mc._height; |
---|
124 | this.background_mc._x = this.background_mc._width; |
---|
125 | this.control_mc._x += 2; |
---|
126 | this.icon_mc._x += 2; |
---|
127 | |
---|
128 | } |
---|
129 | } |
---|