forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptsPart.as
More file actions
191 lines (163 loc) · 6.04 KB
/
ScriptsPart.as
File metadata and controls
191 lines (163 loc) · 6.04 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
/*
* 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.
*/
// ScriptsPart.as
// John Maloney, November 2011
//
// This part holds the palette and scripts pane for the current sprite (or stage).
package ui.parts {
import flash.display.*;
import flash.text.*;
import flash.utils.getTimer;
import scratch.*;
import ui.*;
import uiwidgets.*;
public class ScriptsPart extends UIPart {
private var shape:Shape;
private var selector:PaletteSelector;
private var spriteWatermark:Bitmap;
private var paletteFrame:ScrollFrame;
private var scriptsFrame:ScrollFrame;
private var zoomWidget:ZoomWidget;
private const readoutLabelFormat:TextFormat = new TextFormat(CSS.font, 12, CSS.textColor, true);
private const readoutFormat:TextFormat = new TextFormat(CSS.font, 12, CSS.textColor);
private var xyDisplay:Sprite;
private var xLabel:TextField;
private var yLabel:TextField;
private var xReadout:TextField;
private var yReadout:TextField;
private var lastX:int = -10000000; // impossible value to force initial update
private var lastY:int = -10000000; // impossible value to force initial update
public function ScriptsPart(app:Scratch) {
this.app = app;
addChild(shape = new Shape());
addChild(spriteWatermark = new Bitmap());
addXYDisplay();
addChild(selector = new PaletteSelector(app));
var palette:BlockPalette = new BlockPalette();
palette.color = CSS.tabColor;
paletteFrame = new ScrollFrame();
paletteFrame.allowHorizontalScrollbar = false;
paletteFrame.setContents(palette);
addChild(paletteFrame);
var scriptsPane:ScriptsPane = new ScriptsPane(app);
scriptsFrame = new ScrollFrame();
scriptsFrame.setContents(scriptsPane);
addChild(scriptsFrame);
app.palette = palette;
app.scriptsPane = scriptsPane;
addChild(zoomWidget = new ZoomWidget(scriptsPane));
}
public function resetCategory():void { selector.select(Specs.motionCategory) }
public function updatePalette():void {
selector.updateTranslation();
selector.select(selector.selectedCategory);
}
public function updateSpriteWatermark():void {
var target:ScratchObj = app.viewedObj();
if (target && !target.isStage) {
spriteWatermark.bitmapData = target.currentCostume().thumbnail(40, 40, false);
} else {
spriteWatermark.bitmapData = null;
}
}
public function step():void {
// Update the mouse reaadouts. Do nothing if they are up-to-date (to minimize CPU load).
var target:ScratchObj = app.viewedObj();
if (target.isStage) {
if (xyDisplay.visible) xyDisplay.visible = false;
} else {
if (!xyDisplay.visible) xyDisplay.visible = true;
var spr:ScratchSprite = target as ScratchSprite;
if (!spr) return;
if (spr.scratchX != lastX) {
lastX = spr.scratchX;
xReadout.text = String(lastX);
}
if (spr.scratchY != lastY) {
lastY = spr.scratchY;
yReadout.text = String(lastY);
}
}
updateExtensionIndicators();
}
private var lastUpdateTime:uint;
private function updateExtensionIndicators():void {
if ((getTimer() - lastUpdateTime) < 500) return;
for (var i:int = 0; i < app.palette.numChildren; i++) {
var indicator:IndicatorLight = app.palette.getChildAt(i) as IndicatorLight;
if (indicator) app.extensionManager.updateIndicator(indicator, indicator.target);
}
lastUpdateTime = getTimer();
}
public function setWidthHeight(w:int, h:int):void {
this.w = w;
this.h = h;
fixlayout();
redraw();
}
private function fixlayout():void {
selector.x = 1;
selector.y = 5;
paletteFrame.x = selector.x;
paletteFrame.y = selector.y + selector.height + 2;
paletteFrame.setWidthHeight(selector.width + 1, h - paletteFrame.y - 2); // 5
scriptsFrame.x = selector.x + selector.width + 2;
scriptsFrame.y = selector.y + 1;
scriptsFrame.setWidthHeight(w - scriptsFrame.x - 5, h - scriptsFrame.y - 5);
spriteWatermark.x = w - 60;
spriteWatermark.y = scriptsFrame.y + 10;
xyDisplay.x = spriteWatermark.x + 1;
xyDisplay.y = spriteWatermark.y + 43;
zoomWidget.x = w - zoomWidget.width - 15;
zoomWidget.y = h - zoomWidget.height - 15;
}
private function redraw():void {
var paletteW:int = paletteFrame.visibleW();
var paletteH:int = paletteFrame.visibleH();
var scriptsW:int = scriptsFrame.visibleW();
var scriptsH:int = scriptsFrame.visibleH();
var g:Graphics = shape.graphics;
g.clear();
g.lineStyle(1, CSS.borderColor, 1, true);
g.beginFill(CSS.tabColor);
g.drawRect(0, 0, w, h);
g.endFill();
var lineY:int = selector.y + selector.height;
var darkerBorder:int = CSS.borderColor - 0x141414;
var lighterBorder:int = 0xF2F2F2;
g.lineStyle(1, darkerBorder, 1, true);
hLine(g, paletteFrame.x + 8, lineY, paletteW - 20);
g.lineStyle(1, lighterBorder, 1, true);
hLine(g, paletteFrame.x + 8, lineY + 1, paletteW - 20);
g.lineStyle(1, darkerBorder, 1, true);
g.drawRect(scriptsFrame.x - 1, scriptsFrame.y - 1, scriptsW + 1, scriptsH + 1);
}
private function hLine(g:Graphics, x:int, y:int, w:int):void {
g.moveTo(x, y);
g.lineTo(x + w, y);
}
private function addXYDisplay():void {
xyDisplay = new Sprite();
xyDisplay.addChild(xLabel = makeLabel('x:', readoutLabelFormat, 0, 0));
xyDisplay.addChild(xReadout = makeLabel('-888', readoutFormat, 15, 0));
xyDisplay.addChild(yLabel = makeLabel('y:', readoutLabelFormat, 0, 13));
xyDisplay.addChild(yReadout = makeLabel('-888', readoutFormat, 15, 13));
addChild(xyDisplay);
}
}}