Repository navigation
Expand file tree
/
Copy pathButton.as
More file actions
150 lines (132 loc) · 4.57 KB
/
Copy pathButton.as
File metadata and controls
150 lines (132 loc) · 4.57 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
/*
* 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.
*/
package uiwidgets {
import flash.display.*;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.text.*;
public class Button extends Sprite {
private var labelOrIcon:DisplayObject;
private var color:* = CSS.titleBarColors;
private var minWidth:int = 50;
private var paddingX:Number = 5.5;
private var compact:Boolean;
private var action:Function; // takes no arguments
private var eventAction:Function; // like action, but takes the event as an argument
private var tipName:String;
public function Button(label:String, action:Function = null, compact:Boolean = false, tipName:String = null) {
this.action = action;
this.compact = compact;
this.tipName = tipName;
addLabel(label);
mouseChildren = false;
addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
addEventListener(MouseEvent.MOUSE_UP, mouseUp);
setColor(CSS.titleBarColors);
}
public function setLabel(s:String):void {
if (labelOrIcon is TextField) {
TextField(labelOrIcon).text = s;
setMinWidthHeight(0, 0);
} else {
if ((labelOrIcon != null) && (labelOrIcon.parent != null)) labelOrIcon.parent.removeChild(labelOrIcon);
addLabel(s);
}
}
public function setIcon(icon:DisplayObject):void {
if ((labelOrIcon != null) && (labelOrIcon.parent != null)) {
labelOrIcon.parent.removeChild(labelOrIcon);
}
labelOrIcon = icon;
if (icon != null) addChild(labelOrIcon);
setMinWidthHeight(0, 0);
}
public function setWidth(val:int):void{
paddingX = (val - labelOrIcon.width)/2;
setMinWidthHeight(5, 5);
}
public function setMinWidthHeight(minW:int, minH:int):void {
if (labelOrIcon != null) {
if (labelOrIcon is TextField) {
minW = Math.max(minWidth, labelOrIcon.width + paddingX * 2);
minH = compact ? 20 : 25;
} else {
minW = Math.max(minWidth, labelOrIcon.width + 12);
minH = Math.max(minH, labelOrIcon.height + 11);
}
labelOrIcon.x = ((minW - labelOrIcon.width) / 2);
labelOrIcon.y = ((minH - labelOrIcon.height) / 2);
}
// outline
graphics.clear();
graphics.lineStyle(0.5, CSS.borderColor, 1, true);
if (color is Array) {
var matr:Matrix = new Matrix();
matr.createGradientBox(minW, minH, Math.PI / 2, 0, 0);
graphics.beginGradientFill(GradientType.LINEAR, CSS.titleBarColors, [100, 100], [0x00, 0xFF], matr);
}
else graphics.beginFill(color);
graphics.drawRoundRect(0, 0, minW, minH, 12);
graphics.endFill();
}
public function setEventAction(newEventAction:Function):Function {
var oldEventAction:Function = eventAction;
eventAction = newEventAction;
return oldEventAction;
}
private function mouseOver(evt:MouseEvent):void {
setColor(CSS.overColor)
}
private function mouseOut(evt:MouseEvent):void {
setColor(CSS.titleBarColors)
}
private function mouseDown(evt:MouseEvent):void {
Menu.removeMenusFrom(stage)
}
private function mouseUp(evt:MouseEvent):void {
if (action != null) action();
if (eventAction != null) eventAction(evt);
evt.stopImmediatePropagation();
}
public function handleTool(tool:String, evt:MouseEvent):void {
if (tool == 'help' && tipName) Scratch.app.showTip(tipName);
}
private function setColor(c:*):void {
color = c;
if (labelOrIcon is TextField) {
(labelOrIcon as TextField).textColor = (c == CSS.overColor) ? CSS.white : CSS.buttonLabelColor;
}
setMinWidthHeight(5, 5);
}
private function addLabel(s:String):void {
var label:TextField = new TextField();
label.autoSize = TextFieldAutoSize.LEFT;
label.selectable = false;
label.background = false;
label.defaultTextFormat = CSS.normalTextFormat;
label.textColor = CSS.buttonLabelColor;
label.text = s;
labelOrIcon = label;
setMinWidthHeight(0, 0);
addChild(label);
}
}
}