-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLooksPrims.as
More file actions
323 lines (278 loc) · 10.8 KB
/
Copy pathLooksPrims.as
File metadata and controls
323 lines (278 loc) · 10.8 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
/*
* 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.
*/
// LooksPrims.as
// John Maloney, April 2010
//
// Looks primitives.
package primitives {
import flash.utils.Dictionary;
import blocks.*;
import interpreter.*;
import scratch.*;
public class LooksPrims {
private var app:Scratch;
private var interp:Interpreter;
public function LooksPrims(app:Scratch, interpreter:Interpreter) {
this.app = app;
this.interp = interpreter;
}
public function addPrimsTo(primTable:Dictionary):void {
primTable['lookLike:'] = primShowCostume;
primTable['nextCostume'] = primNextCostume;
primTable['costumeIndex'] = primCostumeIndex;
primTable['costumeName'] = primCostumeName;
primTable['showBackground:'] = primShowCostume; // used by Scratch 1.4 and earlier (doesn't start scene hats)
primTable['nextBackground'] = primNextCostume; // used by Scratch 1.4 and earlier (doesn't start scene hats)
primTable['backgroundIndex'] = primSceneIndex;
primTable['sceneName'] = primSceneName;
primTable['nextScene'] = function(b:*):* { startScene('next backdrop', false) };
primTable['startScene'] = function(b:*):* { startScene(interp.arg(b, 0), false) };
primTable['startSceneAndWait'] = function(b:*):* { startScene(interp.arg(b, 0), true) };
primTable['say:duration:elapsed:from:'] = function(b:*):* { showBubbleAndWait(b, 'talk') };
primTable['say:'] = function(b:*):* { showBubble(b, 'talk') };
primTable['think:duration:elapsed:from:'] = function(b:*):* { showBubbleAndWait(b, 'think') };
primTable['think:'] = function(b:*):* { showBubble(b, 'think') };
primTable['changeGraphicEffect:by:'] = primChangeEffect;
primTable['setGraphicEffect:to:'] = primSetEffect;
primTable['filterReset'] = primClearEffects;
primTable['changeSizeBy:'] = primChangeSize;
primTable['setSizeTo:'] = primSetSize;
primTable['scale'] = primSize;
primTable['show'] = primShow;
primTable['hide'] = primHide;
primTable['showing'] = function(b:*):* {return interp.targetObj().visible; };
// primTable['hideAll'] = primHideAll;
primTable['comeToFront'] = primGoFront;
primTable['goBackByLayers:'] = primGoBack;
primTable['setVideoState'] = primSetVideoState;
primTable['setVideoTransparency'] = primSetVideoTransparency;
// primTable['scrollAlign'] = primScrollAlign;
// primTable['scrollRight'] = primScrollRight;
// primTable['scrollUp'] = primScrollUp;
// primTable['xScroll'] = function(b:*):* { return app.stagePane.xScroll };
// primTable['yScroll'] = function(b:*):* { return app.stagePane.yScroll };
primTable['setRotationStyle'] = primSetRotationStyle;
}
private function primNextCostume(b:Block):void {
var s:ScratchObj = interp.targetObj();
if (s != null) s.showCostume(s.currentCostumeIndex + 1);
if (s.visible) interp.redraw();
}
private function primShowCostume(b:Block):void {
var s:ScratchObj = interp.targetObj();
if (s == null) return;
var arg:* = interp.arg(b, 0);
if (typeof(arg) == 'number') {
s.showCostume(arg - 1);
} else {
var i:int = s.indexOfCostumeNamed(arg);
if (i >= 0) {
s.showCostume(i);
} else if ('previous costume' == arg) {
s.showCostume(s.currentCostumeIndex - 1);
} else if ('next costume' == arg) {
s.showCostume(s.currentCostumeIndex + 1);
} else {
var n:Number = Interpreter.asNumber(arg);
if (!isNaN(n)) s.showCostume(n - 1);
else return; // arg did not match a costume name nor is it a valid number
}
}
if (s.visible) interp.redraw();
}
private function primCostumeIndex(b:Block):Number {
var s:ScratchObj = interp.targetObj();
return (s == null) ? 1 : s.costumeNumber();
}
private function primCostumeName(b:Block):String {
var s:ScratchObj = interp.targetObj();
return (s == null) ? '' : s.currentCostume().costumeName;
}
private function primSceneIndex(b:Block):Number {
return app.stagePane.costumeNumber();
}
private function primSceneName(b:Block):String {
return app.stagePane.currentCostume().costumeName;
}
private function startScene(s:String, waitFlag:Boolean):void {
if ('next backdrop' == s) s = backdropNameAt(app.stagePane.currentCostumeIndex + 1);
else if ('previous backdrop' == s) s = backdropNameAt(app.stagePane.currentCostumeIndex - 1);
else {
var n:Number = Interpreter.asNumber(s);
if (!isNaN(n)) {
n = (Math.round(n) - 1) % app.stagePane.costumes.length;
if (n < 0) n += app.stagePane.costumes.length;
s = app.stagePane.costumes[n].costumeName;
}
}
interp.startScene(s, waitFlag);
}
private function backdropNameAt(i:int):String {
var costumes:Array = app.stagePane.costumes;
return costumes[(i + costumes.length) % costumes.length].costumeName;
}
private function showBubbleAndWait(b:Block, type:String):void {
var text:*, secs:Number;
var s:ScratchSprite = interp.targetSprite();
if (s == null) return;
if (interp.activeThread.firstTime) {
text = interp.arg(b, 0);
secs = interp.numarg(b, 1);
s.showBubble(text, type, b);
if (s.visible) interp.redraw();
interp.startTimer(secs);
} else {
if (interp.checkTimer() && s.bubble && (s.bubble.getSource() == b)) {
s.hideBubble();
}
}
}
private function showBubble(b:Block, type:String = null):void {
var text:*, secs:Number;
var s:ScratchSprite = interp.targetSprite();
if (s == null) return;
if (type == null) { // combined talk/think/shout/whisper command
type = interp.arg(b, 0);
text = interp.arg(b, 1);
} else { // talk or think command
text = interp.arg(b, 0);
}
s.showBubble(text, type, b);
if (s.visible) interp.redraw();
}
private function primChangeEffect(b:Block):void {
var s:ScratchObj = interp.targetObj();
if (s == null) return;
var filterName:String = interp.arg(b, 0);
var delta:Number = interp.numarg(b, 1);
if(delta == 0) return;
var newValue:Number = s.filterPack.getFilterSetting(filterName) + delta;
s.filterPack.setFilter(filterName, newValue);
s.applyFilters();
if (s.visible || s == Scratch.app.stagePane) interp.redraw();
}
private function primSetEffect(b:Block):void {
var s:ScratchObj = interp.targetObj();
if (s == null) return;
var filterName:String = interp.arg(b, 0);
var newValue:Number = interp.numarg(b, 1);
if(s.filterPack.setFilter(filterName, newValue))
s.applyFilters();
if (s.visible || s == Scratch.app.stagePane) interp.redraw();
}
private function primClearEffects(b:Block):void {
var s:ScratchObj = interp.targetObj();
s.clearFilters();
s.applyFilters();
if (s.visible || s == Scratch.app.stagePane) interp.redraw();
}
private function primChangeSize(b:Block):void {
var s:ScratchSprite = interp.targetSprite();
if (s == null) return;
var oldScale:Number = s.scaleX;
s.setSize(s.getSize() + interp.numarg(b, 0));
if (s.visible && (s.scaleX != oldScale)) interp.redraw();
}
private function primSetRotationStyle(b:Block):void {
var s:ScratchSprite = interp.targetSprite();
var newStyle:String = interp.arg(b, 0) as String;
if ((s == null) || (newStyle == null)) return;
s.setRotationStyle(newStyle);
}
private function primSetSize(b:Block):void {
var s:ScratchSprite = interp.targetSprite();
if (s == null) return;
s.setSize(interp.numarg(b, 0));
if (s.visible) interp.redraw();
}
private function primSize(b:Block):Number {
var s:ScratchSprite = interp.targetSprite();
if (s == null) return 100;
return Math.round(s.getSize()); // reporter returns rounded size, as in Scratch 1.4
}
private function primShow(b:Block):void {
var s:ScratchSprite = interp.targetSprite();
if (s == null) return;
s.visible = true;
if(!app.isIn3D) s.applyFilters();
s.updateBubble();
if (s.visible) interp.redraw();
}
private function primHide(b:Block):void {
var s:ScratchSprite = interp.targetSprite();
if ((s == null) || !s.visible) return;
s.visible = false;
if(!app.isIn3D) s.applyFilters();
s.updateBubble();
interp.redraw();
}
private function primHideAll(b:Block):void {
// Hide all sprites and delete all clones. Only works from the stage.
if (!interp.targetObj().isStage) return;
app.stagePane.deleteClones();
for (var i:int = 0; i < app.stagePane.numChildren; i++) {
var o:* = app.stagePane.getChildAt(i);
if (o is ScratchSprite) {
o.visible = false;
o.updateBubble();
}
}
interp.redraw();
}
private function primGoFront(b:Block):void {
var s:ScratchSprite = interp.targetSprite();
if ((s == null) || (s.parent == null)) return;
s.parent.setChildIndex(s, s.parent.numChildren - 1);
if (s.visible) interp.redraw();
}
private function primGoBack(b:Block):void {
var s:ScratchSprite = interp.targetSprite();
if ((s == null) || (s.parent == null)) return;
var newIndex:int = s.parent.getChildIndex(s) - interp.numarg(b, 0);
newIndex = Math.max(minSpriteLayer(), Math.min(newIndex, s.parent.numChildren - 1));
if (newIndex > 0 && newIndex < s.parent.numChildren) {
s.parent.setChildIndex(s, newIndex);
if (s.visible) interp.redraw();
}
}
private function minSpriteLayer():int {
// Return the lowest sprite layer.
var stg:ScratchStage = app.stagePane;
return stg.getChildIndex(stg.videoImage ? stg.videoImage : stg.penLayer) + 1;
}
private function primSetVideoState(b:Block):void {
app.stagePane.setVideoState(interp.arg(b, 0));
}
private function primSetVideoTransparency(b:Block):void {
app.stagePane.setVideoTransparency(interp.numarg(b, 0));
app.stagePane.setVideoState('on');
}
private function primScrollAlign(b:Block):void {
if (!interp.targetObj().isStage) return;
app.stagePane.scrollAlign(interp.arg(b, 0));
}
private function primScrollRight(b:Block):void {
if (!interp.targetObj().isStage) return;
app.stagePane.scrollRight(interp.numarg(b, 0));
}
private function primScrollUp(b:Block):void {
if (!interp.targetObj().isStage) return;
app.stagePane.scrollUp(interp.numarg(b, 0));
}
}}