forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScratchComment.as
More file actions
279 lines (245 loc) · 7.64 KB
/
ScratchComment.as
File metadata and controls
279 lines (245 loc) · 7.64 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
/*
* 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 scratch {
import flash.display.*;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.*;
import blocks.Block;
import uiwidgets.*;
public class ScratchComment extends Sprite {
public var blockID:int;
public var blockRef:Block;
private const contentsFormat:TextFormat = new TextFormat(CSS.font, 12, CSS.textColor, false);
private const titleFormat:TextFormat = new TextFormat(CSS.font, 12, CSS.textColor, true);
private const arrowColor:int = 0x808080;
private const bodyColor:int = 0xFFFFD2;
private const titleBarColor:int = 0xFFFFA5;
private var frame:ResizeableFrame;
private var titleBar:Shape;
private var expandButton:IconButton;
private var title:TextField;
private var contents:TextField;
private var clipMask:Shape;
private var isOpen:Boolean;
private var expandedSize:Point;
public function ScratchComment(s:String = 'add comment here...', isOpen:Boolean = true, width:int = 150, blockID:int = -1) {
this.isOpen = isOpen;
this.blockID = blockID;
addFrame();
addChild(titleBar = new Shape());
addChild(clipMask = new Shape());
addExpandButton();
addTitle();
addContents();
contents.text = s;
contents.mask = clipMask;
frame.setWidthHeight(width, 200);
expandedSize = new Point(width, 200);
addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
fixLayout();
setExpanded(isOpen);
}
public function objToGrab(evt:*):* { return this }
public function fixLayout():void {
contents.x = 5;
contents.y = 20;
var w:int = frame.w - contents.x - 6;
var h:int = frame.h - contents.y - 2;
contents.width = w;
contents.height = h;
var g:Graphics = clipMask.graphics;
g.clear();
g.beginFill(0xFFFF00);
g.drawRect(contents.x, contents.y, w, h);
drawTitleBar();
}
private function drawTitleBar():void {
// Draw darker yellow title area used when comment expanded.
var g:Graphics = titleBar.graphics;
g.clear();
g.lineStyle();
g.beginFill(titleBarColor);
g.drawRoundRect(1, 1, frame.w - 1, 21, 11, 11);
g.beginFill(bodyColor);
g.drawRect(1, 18, frame.w - 1, 4);
}
public function toArray():Array {
return [x, y, isOpen ? frame.width : expandedSize.x, isOpen ? frame.height : expandedSize.y, isOpen, blockID, contents.text];
}
public static function fromArray(a:Array):ScratchComment {
var c:ScratchComment = new ScratchComment();
c.x = a[0];
c.y = a[1];
c.blockID = a[5];
c.contents.text = a[6];
if (a[4]) {
c.expandedSize = new Point(a[2], a[3]);
} else {
c.frame.setWidthHeight(a[2], a[3] == 19 ? 200 : a[3]);
}
c.setExpanded(a[4]);
return c;
}
public function updateBlockID(blockList:Array):void {
if (blockRef) {
blockID = blockList.indexOf(blockRef);
}
}
public function updateBlockRef(blockList:Array):void {
if ((blockID >= 0) && (blockID < blockList.length)) {
blockRef = blockList[blockID];
}
}
/* Expand/Contract */
public function isExpanded():Boolean { return isOpen }
public function setExpanded(flag:Boolean):void {
isOpen = flag;
contents.visible = isOpen;
titleBar.visible = isOpen;
title.visible = !isOpen;
expandButton.setOn(isOpen);
if (flag) {
frame.showResizer();
frame.setColor(bodyColor);
frame.setWidthHeight(expandedSize.x, expandedSize.y);
if (parent) parent.addChild(this); // go to front
fixLayout();
} else {
if (stage && stage.focus == contents) stage.focus = null; // give up focus
expandedSize = new Point(frame.w, frame.h);
updateTitleText();
frame.hideResizer();
frame.setWidthHeight(frame.w, 19);
frame.setColor(titleBarColor);
}
var scriptsPane:ScriptsPane = parent as ScriptsPane;
if (scriptsPane) scriptsPane.fixCommentLayout();
}
private function updateTitleText():void {
const ellipses:String = '...';
var maxW:int = frame.w - title.x - 5;
var s:String = contents.text;
var i:int = s.indexOf('\r');
if (i > -1) s = s.slice(0, i);
i = s.indexOf('\n');
if (i > -1) s = s.slice(0, i);
// Keep adding letters to the title until either
// the entire first line fits or out of space
i = 1;
while (i < s.length) {
title.text = s.slice(0, i) + ellipses;
if (title.textWidth > maxW) {
title.text = s.slice(0, i - 1) + ellipses;
return;
}
i++;
}
title.text = s; // entire string fits; remove ellipses
}
/* Menu/Tool Operations */
public function menu(evt:MouseEvent):Menu {
var m:Menu = new Menu();
var startX:Number = stage.mouseX;
var startY:Number = stage.mouseY;
m.addItem('duplicate', function():void {
duplicateComment(stage.mouseX - startX, stage.mouseY - startY);
});
m.addItem('delete', deleteComment);
return m;
}
public function handleTool(tool:String, evt:MouseEvent):void {
if (tool == 'copy') duplicateComment(10, 5);
if (tool == 'cut') deleteComment();
}
public function deleteComment():void {
if (parent) parent.removeChild(this);
Scratch.app.runtime.recordForUndelete(this, x, y, 0, Scratch.app.viewedObj());
Scratch.app.scriptsPane.saveScripts();
}
public function duplicateComment(deltaX:Number, deltaY:Number):void {
if (!parent) return;
var dup:ScratchComment = new ScratchComment(contents.text, isOpen);
dup.x = x + deltaX;
dup.y = y + deltaY;
parent.addChild(dup);
Scratch.app.gh.grabOnMouseUp(dup);
}
private function mouseDown(evt:MouseEvent):void {
// When open, clicks below the title bar set keyboard focus.
if (isOpen && (evt.localY > 20)) {
var end:int = contents.text.length;
contents.setSelection(end, end);
stage.focus = contents;
}
}
/* Construction */
private function addFrame():void {
frame = new ResizeableFrame(CSS.borderColor, bodyColor, 11, false, 1);
frame.minWidth = 100;
frame.minHeight = 34;
frame.showResizer();
addChild(frame);
}
private function addTitle():void {
title = new TextField();
title.autoSize = TextFieldAutoSize.LEFT;
title.selectable = false;
title.defaultTextFormat = titleFormat;
title.visible = false;
title.x = 14;
title.y = 1;
addChild(title);
}
private function addContents():void {
contents = new TextField();
contents.type = 'input';
contents.wordWrap = true;
contents.multiline = true;
contents.autoSize = TextFieldAutoSize.LEFT;
contents.defaultTextFormat = contentsFormat;
addChild(contents);
}
private function addExpandButton():void {
function toggleExpand(b:IconButton):void { setExpanded(!isOpen) }
expandButton = new IconButton(toggleExpand, expandIcon(true), expandIcon(false));
expandButton.setOn(true);
expandButton.disableMouseover();
expandButton.x = 4;
expandButton.y = 4;
addChild(expandButton);
}
private function expandIcon(pointDown:Boolean):Shape {
var icon:Shape = new Shape();
var g:Graphics = icon.graphics;
g.lineStyle();
g.beginFill(arrowColor);
if (pointDown) {
g.moveTo(0, 2);
g.lineTo(5.5, 8);
g.lineTo(11, 2);
} else {
g.moveTo(2, 0);
g.lineTo(8, 5.5);
g.lineTo(2, 11);
}
g.endFill();
return icon;
}
}}