forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuItem.as
More file actions
160 lines (140 loc) · 4.68 KB
/
MenuItem.as
File metadata and controls
160 lines (140 loc) · 4.68 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* 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.
*/
// MenuItem.as
// John Maloney, October 2009
//
// A single menu item for Menu.as.
package uiwidgets {
import flash.display.*;
import flash.events.MouseEvent;
import flash.text.*;
import scratch.BlockMenus;
import translation.Translator;
import util.Color;
public class MenuItem extends Sprite {
private const leftMargin:int = 22;
private const rightMargin:int = 10;
private const checkmarkColor:int = 0xF0F0F0;
private var menu:Menu;
private var label:TextField; // if label is null, this item is a divider line
private var checkmark:Shape;
private var selection:*;
private var base:Shape;
private var w:int, h:int;
public function MenuItem(menu:Menu, labelText:String, selection:*, enabled:Boolean) {
this.menu = menu;
this.selection = (selection == null) ? labelText : selection;
addChild(base = new Shape());
if (labelText == '---') return;
addCheckmark();
addLabel(labelText, enabled);
setBaseColor(menu.color);
if (enabled) {
addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
addEventListener(MouseEvent.MOUSE_UP, mouseUp);
}
}
public function setWidthHeight(w:int, itemHeight:int):void {
this.w = w;
this.h = 1;
if (label) {
this.h = Math.max(label.height, itemHeight);
label.y = Math.max(0, (h - label.height) / 2);
}
setBaseColor(menu.color);
}
public function isLine():Boolean { return !label }
public function getLabel():String { return label ? label.text : '' }
public function showCheckmark(flag:Boolean):void { if (checkmark) checkmark.visible = flag }
public function translate(menuName:String):void {
if (label && BlockMenus.shouldTranslateItemForMenu(label.text, menuName)) {
label.text = Translator.map(label.text);
}
}
private function addCheckmark():void {
checkmark = new Shape();
drawCheckmark(checkmarkColor);
checkmark.x = 6;
checkmark.y = 8;
checkmark.visible = true;
addChild(checkmark);
}
private function drawCheckmark(c:int):void {
var g:Graphics = checkmark.graphics;
g.clear();
g.beginFill(c);
g.moveTo(0, 6);
g.lineTo(5, 12);
g.lineTo(12.5, 0.5);
g.lineTo(12, 0);
g.lineTo(5, 9);
g.lineTo(0, 5.5);
g.endFill();
}
private function addLabel(s:String, enabled:Boolean):void {
label = new TextField();
label.autoSize = TextFieldAutoSize.LEFT;
label.selectable = false;
label.defaultTextFormat = new TextFormat(CSS.font, CSS.menuFontSize, CSS.white);
label.text = s;
label.x = leftMargin;
label.y = 0;
label.alpha = enabled ? 1 : 0.5;
w = label.width + leftMargin + rightMargin;
h = Math.max(label.height, menu.itemHeight);
addChild(label);
setBaseColor(menu.color);
}
private function setHighlight(highlight:Boolean):void {
setBaseColor(highlight ? selectedColorFrom(menu.color) : menu.color);
label.textColor = highlight ? colorWithBrightness(menu.color, 0.3) : CSS.white;
if (checkmark.visible) drawCheckmark(highlight ? colorWithBrightness(menu.color, 0.5) : checkmarkColor);
}
private function setBaseColor(c:int):void {
var g:Graphics = base.graphics;
g.clear();
if (label) {
g.beginFill(c);
g.drawRect(0, 0, w, h);
g.endFill();
} else { // divider line
g.beginFill(colorWithBrightness(menu.color, 0.5));
base.graphics.drawRect(0, 0, w, 1);
g.endFill();
}
}
private function selectedColorFrom(rgb:Number):int {
var hsv:Array = Color.rgb2hsv(rgb);
var sat:Number = hsv[1];
var bri:Number = 0.9;
if (sat > 0.5) {
sat = 0.3;
bri = 1;
}
return Color.fromHSV(hsv[0], sat, bri);
}
private function colorWithBrightness(rgb:Number, brightness:Number):int {
var hsv:Array = Color.rgb2hsv(rgb);
return Color.fromHSV(hsv[0], hsv[1], brightness);
}
private function mouseOver(evt:MouseEvent):void { setHighlight(true) }
private function mouseOut(evt:MouseEvent):void { setHighlight(false) }
private function mouseUp(evt:MouseEvent):void { menu.selected(selection) }
}}