forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundLevelMeter.as
More file actions
79 lines (64 loc) · 2.1 KB
/
SoundLevelMeter.as
File metadata and controls
79 lines (64 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Scratch Project Editor and Player
* Copyright (C) 2014 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// SoundLevelMeter.as
// John Maloney, March 2012
package soundedit {
import flash.display.*;
import flash.text.TextFormat;
import assets.Resources;
public class SoundLevelMeter extends Sprite {
private var w:int, h:int;
private var bar:Shape;
private var recentMax:Number = 0;
public function SoundLevelMeter(barWidth:int, barHeight:int) {
w = barWidth;
h = barHeight;
// frame
graphics.lineStyle(1, CSS.borderColor, 1, true);
graphics.drawRoundRect(0, 0, w, h, 7, 7);
// meter bar
addChild(bar = new Shape());
}
public function clear():void {
recentMax = 0;
setLevel(0);
}
public function setLevel(percent:Number):void {
recentMax *= 0.85;
recentMax = Math.max(percent, recentMax);
drawBar(recentMax);
}
private function drawBar(percent:Number):void {
const red:int = 0xFF0000;
const yellow:int = 0xFFFF00;
const green:int = 0xFF00;
const r:int = 3;
var g:Graphics = bar.graphics;
g.clear();
g.beginFill(red);
var barH:int = (h - 1) * Math.min(percent, 100) / 100;
g.drawRoundRect(1, h - barH, w - 1, barH, r, r);
g.beginFill(yellow);
barH = h * Math.min(percent, 95) / 100;
g.drawRoundRect(1, h - barH, w - 1, barH, r, r);
g.beginFill(green);
barH = h * Math.min(percent, 70) / 100;
g.drawRoundRect(1, h - barH, w - 1, barH, r, r);
}
}}