forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogBox.as
More file actions
399 lines (362 loc) · 12.2 KB
/
DialogBox.as
File metadata and controls
399 lines (362 loc) · 12.2 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* 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.*;
import flash.filters.DropShadowFilter;
import flash.text.*;
import flash.utils.Dictionary;
import translation.Translator;
import ui.parts.UIPart;
public class DialogBox extends Sprite {
public var fields:Dictionary = new Dictionary();
public var booleanFields:Dictionary = new Dictionary();
public var widget:DisplayObject;
public var w:int, h:int;
public var leftJustify:Boolean;
private var title:TextField;
protected var buttons:Array = [];
private var labelsAndFields:Array = [];
private var booleanLabelsAndFields:Array = [];
private var textLines:Array = [];
private var maxLabelWidth:int = 0;
private var maxFieldWidth:int = 0;
private var heightPerField:int = Math.max(makeLabel('foo').height, makeField(10).height) + 10;
private const spaceAfterText:int = 18;
private const blankLineSpace:int = 7;
private var acceptFunction:Function; // if not nil, called when menu interaction is accepted
public function DialogBox(acceptFunction:Function = null) {
this.acceptFunction = acceptFunction;
addFilters();
addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
addEventListener(MouseEvent.MOUSE_UP, mouseUp);
addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
addEventListener(FocusEvent.KEY_FOCUS_CHANGE, focusChange);
}
public static function ask(question:String, defaultAnswer:String, stage:Stage = null, resultFunction:Function = null):void {
function done():void { if (resultFunction != null) resultFunction(d.fields['answer'].text) }
var d:DialogBox = new DialogBox(done);
d.addTitle(question);
d.addField('answer', 120, defaultAnswer, false);
d.addButton('OK', d.accept);
d.showOnStage(stage ? stage : Scratch.app.stage);
}
public static function confirm(question:String, stage:Stage = null, okFunction:Function = null):void {
var d:DialogBox = new DialogBox(okFunction);
d.addTitle(question);
d.addAcceptCancelButtons('OK');
d.showOnStage(stage ? stage : Scratch.app.stage);
}
public static function notify(title:String, msg:String, stage:Stage = null, leftJustify:Boolean = false, okFunction:Function = null):void {
var d:DialogBox = new DialogBox(okFunction);
d.leftJustify = leftJustify;
d.addTitle(title);
d.addText(msg);
d.addButton('OK', d.accept);
d.showOnStage(stage ? stage : Scratch.app.stage);
}
public function addTitle(s:String):void {
title = makeLabel(Translator.map(s), true);
addChild(title);
}
public function addText(text:String):void {
for each (var s:String in text.split('\n')) {
var line:TextField = makeLabel(Translator.map(s));
addChild(line);
textLines.push(line);
}
}
public function addWidget(o:DisplayObject):void {
widget = o;
addChild(o);
}
public function addField(fieldName:String, width:int, defaultValue:* = null, showLabel:Boolean = true):void {
var l:TextField = null;
if (showLabel) {
l = makeLabel(Translator.map(fieldName) + ':');
addChild(l);
}
var f:TextField = makeField(width);
if (defaultValue != null) f.text = defaultValue;
addChild(f);
fields[fieldName] = f;
labelsAndFields.push([l, f]);
}
public function addBoolean(fieldName:String, defaultValue:Boolean = false, isRadioButton:Boolean = false):void {
var l:TextField = makeLabel(Translator.map(fieldName) + ':');
addChild(l);
var f:IconButton = isRadioButton ?
new IconButton(null, null, null, true) :
new IconButton(null, getCheckMark(true), getCheckMark(false));
if (defaultValue) f.turnOn(); else f.turnOff();
addChild(f);
booleanFields[fieldName] = f;
booleanLabelsAndFields.push([l, f]);
}
private function getCheckMark(b:Boolean):Sprite{
var spr:Sprite = new Sprite();
var g:Graphics = spr.graphics;
g.clear();
g.beginFill(0xFFFFFF);
g.lineStyle(1, 0x929497, 1, true);
g.drawRoundRect(0, 0, 17, 17, 3, 3);
g.endFill();
if (b) {
g.lineStyle(2, 0x4c4d4f, 1, true);
g.moveTo(3,7);
g.lineTo(5,7);
g.lineTo(8,13);
g.lineTo(14,3);
}
return spr;
}
public function addAcceptCancelButtons(acceptLabel:String = null):void {
// Add a cancel button and an optional accept button with the given label.
if (acceptLabel != null) addButton(acceptLabel, accept);
addButton('Cancel', cancel);
}
public function addButton(label:String, action:Function):void {
function doAction():void {
cancel();
if (action != null) action();
}
var b:Button = new Button(Translator.map(label), doAction);
addChild(b);
buttons.push(b);
}
public function showOnStage(stage:Stage, center:Boolean = true):void {
fixLayout();
if (center) {
x = (stage.stageWidth - width) / 2;
y = (stage.stageHeight - height) / 2;
} else {
x = stage.mouseX + 10;
y = stage.mouseY + 10;
}
x = Math.max(0, Math.min(x, stage.stageWidth - width));
y = Math.max(0, Math.min(y, stage.stageHeight - height));
stage.addChild(this);
if (labelsAndFields.length > 0) {
// note: doesn't work when testing from FlexBuilder; works when deployed
stage.focus = labelsAndFields[0][1];
}
}
public static function findDialogBoxes(targetTitle:String, stage:Stage):Array {
// Return an array of all dialogs on the stage with the given title.
// If the given title is null then return all dialogs.
var result:Array = [];
if (targetTitle) targetTitle = Translator.map(targetTitle);
for (var i:int = 0; i < stage.numChildren; i++) {
var d:DialogBox = stage.getChildAt(i) as DialogBox;
if (d) {
if (targetTitle) {
if (d.title && (d.title.text == targetTitle)) result.push(d);
} else {
result.push(d);
}
}
}
return result;
}
public function accept():void {
if (acceptFunction != null) acceptFunction(this);
if (parent != null) parent.removeChild(this);
}
public function cancel():void {
if (parent != null) parent.removeChild(this);
}
public function getField(fieldName:String):* {
if (fields[fieldName] != null) return fields[fieldName].text;
if (booleanFields[fieldName] != null) return booleanFields[fieldName].isOn();
return null;
}
public function setPasswordField(fieldName:String, flag:Boolean = true):void {
var field:* = fields[fieldName];
if (field is TextField) {
(field as TextField).displayAsPassword = flag;
}
}
private function makeLabel(s:String, forTitle:Boolean = false):TextField {
const normalFormat:TextFormat = new TextFormat(CSS.font, 14, CSS.textColor)
var result:TextField = new TextField();
result.autoSize = TextFieldAutoSize.LEFT;
result.selectable = false;
result.background = false;
result.text = s;
result.setTextFormat(forTitle ? CSS.titleFormat : normalFormat);
return result;
}
private function makeField(width:int):TextField {
var result:TextField = new TextField();
result.selectable = true;
result.type = TextFieldType.INPUT;
result.background = true;
result.border = true;
result.defaultTextFormat = CSS.normalTextFormat;
result.width = width;
result.height = result.defaultTextFormat.size + 8;
result.backgroundColor = 0xFFFFFF;
result.borderColor = CSS.borderColor;
return result;
}
public function fixLayout():void {
var label:TextField;
var i:int, totalW:int;
fixSize();
var fieldX:int = maxLabelWidth + 17;
var fieldY:int = 15;
if (title != null) {
title.x = (w - title.width) / 2;
title.y = 5;
fieldY = title.y + title.height + 20;
}
// fields
for (i = 0; i < labelsAndFields.length; i++) {
label = labelsAndFields[i][0];
var field:TextField = labelsAndFields[i][1];
if (label != null) {
label.x = fieldX - 5 - label.width;
label.y = fieldY;
}
field.x = fieldX;
field.y = fieldY + 1;
fieldY += heightPerField;
}
// widget
if (widget != null) {
widget.x = (width - widget.width) / 2;
widget.y = fieldY; // (title != null) ? title.y + title.height + 10 : 10;
fieldY = widget.y + widget.height + 15;
}
// boolean fields
for (i = 0; i < booleanLabelsAndFields.length; i++) {
label = booleanLabelsAndFields[i][0];
var ib:IconButton = booleanLabelsAndFields[i][1];
if (label != null) {
label.x = fieldX - 5 - label.width;
label.y = fieldY + 5;
}
ib.x = fieldX - 2;
ib.y = fieldY + 5;
fieldY += heightPerField;
}
// text lines
for each (var line:TextField in textLines) {
line.x = leftJustify ? 15 : (w - line.width) / 2;
line.y = fieldY;
fieldY += line.height;
if (line.text.length == 0) fieldY += blankLineSpace;
}
if (textLines.length > 0) fieldY += spaceAfterText;
// buttons
if (buttons.length > 0) {
totalW = (buttons.length - 1) * 10;
for (i = 0; i < buttons.length; i++) totalW += buttons[i].width;
var buttonX:int = (w - totalW) / 2;
var buttonY:int = h - (buttons[0].height + 15);
for (i = 0; i < buttons.length; i++) {
buttons[i].x = buttonX;
buttons[i].y = buttonY;
buttonX += buttons[i].width + 10;
}
}
}
private function fixSize():void {
var i:int, totalW:int;
w = h = 0;
// title
if (title != null) {
w = Math.max(w, title.width);
h += 10 + title.height;
}
// fields
maxLabelWidth = 0;
maxFieldWidth = 0;
for (i = 0; i < labelsAndFields.length; i++) {
var r:Array = labelsAndFields[i];
if (r[0] != null) maxLabelWidth = Math.max(maxLabelWidth, r[0].width);
maxFieldWidth = Math.max(maxFieldWidth, r[1].width);
h += heightPerField;
}
// boolean fields
for (i = 0; i < booleanLabelsAndFields.length; i++) {
r = booleanLabelsAndFields[i];
if (r[0] != null) maxLabelWidth = Math.max(maxLabelWidth, r[0].width);
maxFieldWidth = Math.max(maxFieldWidth, r[1].width);
h += heightPerField;
}
w = Math.max(w, maxLabelWidth + maxFieldWidth + 5);
// widget
if (widget != null) {
w = Math.max(w, widget.width);
h += 10 + widget.height;
}
// text lines
for each (var line:TextField in textLines) {
w = Math.max(w, line.width);
h += line.height;
if (line.length == 0) h += blankLineSpace;
}
if (textLines.length > 0) h += spaceAfterText;
// buttons
totalW = 0;
for (i = 0; i < buttons.length; i++) totalW += buttons[i].width + 10;
w = Math.max(w, totalW);
if (buttons.length > 0) h += buttons[0].height + 15;
if ((labelsAndFields.length > 0) || (booleanLabelsAndFields.length > 0)) h += 15;
w += 30;
h += 10;
drawBackground();
}
private function drawBackground():void {
var titleBarColors:Array = [0xE0E0E0, 0xD0D0D0]; // old: CSS.titleBarColors;
var borderColor:int = 0xB0B0B0; // old: CSS.borderColor;
var g:Graphics = graphics;
g.clear();
UIPart.drawTopBar(g, titleBarColors, UIPart.getTopBarPath(w, h), w, CSS.titleBarH, borderColor);
g.lineStyle(0.5, borderColor, 1, true);
g.beginFill(0xFFFFFF);
g.drawRect(0, CSS.titleBarH, w - 1, h - CSS.titleBarH - 1);
}
private function addFilters():void {
var f:DropShadowFilter = new DropShadowFilter();
f.blurX = f.blurY = 8;
f.distance = 5;
f.alpha = 0.75;
f.color = 0x333333;
filters = [f];
}
/* Events */
private function focusChange(evt:Event):void {
evt.preventDefault();
if (labelsAndFields.length == 0) return;
var focusIndex:int = -1;
for (var i:int = 0; i < labelsAndFields.length; i++) {
if (stage.focus == labelsAndFields[i][1]) focusIndex = i;
}
focusIndex++;
if (focusIndex >= labelsAndFields.length) focusIndex = 0;
stage.focus = labelsAndFields[focusIndex][1];
}
private function mouseDown(evt:MouseEvent):void {if (evt.target == this || evt.target == title) startDrag();}
private function mouseUp(evt:MouseEvent):void { stopDrag() }
private function keyDown(evt:KeyboardEvent):void {
if ((evt.keyCode == 10) || (evt.keyCode == 13)) accept();
}
}}