@scratch/scratch-vm 11.0.0-UEPR-176
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +12 -0
- package/README.md +120 -0
- package/dist/node/extension-worker.js +2 -0
- package/dist/node/extension-worker.js.map +1 -0
- package/dist/node/scratch-vm.js +3 -0
- package/dist/node/scratch-vm.js.LICENSE.txt +205 -0
- package/dist/node/scratch-vm.js.map +1 -0
- package/dist/web/extension-worker.js +2 -0
- package/dist/web/extension-worker.js.map +1 -0
- package/dist/web/scratch-vm.js +3 -0
- package/dist/web/scratch-vm.js.LICENSE.txt +42 -0
- package/dist/web/scratch-vm.js.map +1 -0
- package/package.json +108 -0
- package/src/.eslintrc.js +7 -0
- package/src/blocks/scratch3_control.js +206 -0
- package/src/blocks/scratch3_core_example.js +69 -0
- package/src/blocks/scratch3_data.js +257 -0
- package/src/blocks/scratch3_event.js +137 -0
- package/src/blocks/scratch3_looks.js +612 -0
- package/src/blocks/scratch3_motion.js +288 -0
- package/src/blocks/scratch3_operators.js +154 -0
- package/src/blocks/scratch3_procedures.js +79 -0
- package/src/blocks/scratch3_sensing.js +336 -0
- package/src/blocks/scratch3_sound.js +350 -0
- package/src/dispatch/central-dispatch.js +143 -0
- package/src/dispatch/shared-dispatch.js +235 -0
- package/src/dispatch/worker-dispatch.js +110 -0
- package/src/engine/adapter.js +176 -0
- package/src/engine/block-utility.js +242 -0
- package/src/engine/blocks-execute-cache.js +19 -0
- package/src/engine/blocks-runtime-cache.js +78 -0
- package/src/engine/blocks.js +1299 -0
- package/src/engine/comment.js +56 -0
- package/src/engine/execute.js +580 -0
- package/src/engine/monitor-record.js +23 -0
- package/src/engine/mutation-adapter.js +48 -0
- package/src/engine/profiler.js +390 -0
- package/src/engine/runtime.js +2686 -0
- package/src/engine/scratch-blocks-constants.js +27 -0
- package/src/engine/sequencer.js +361 -0
- package/src/engine/stage-layering.js +29 -0
- package/src/engine/target.js +799 -0
- package/src/engine/thread.js +404 -0
- package/src/engine/variable.js +70 -0
- package/src/extension-support/argument-type.js +47 -0
- package/src/extension-support/block-type.js +50 -0
- package/src/extension-support/define-messages.js +18 -0
- package/src/extension-support/extension-manager.js +440 -0
- package/src/extension-support/extension-metadata.js +64 -0
- package/src/extension-support/extension-worker.js +59 -0
- package/src/extension-support/reporter-scope.js +18 -0
- package/src/extension-support/target-type.js +17 -0
- package/src/extensions/scratch3_boost/index.js +2113 -0
- package/src/extensions/scratch3_ev3/index.js +1355 -0
- package/src/extensions/scratch3_gdx_for/index.js +981 -0
- package/src/extensions/scratch3_gdx_for/scratch-link-device-adapter.js +44 -0
- package/src/extensions/scratch3_makeymakey/index.js +396 -0
- package/src/extensions/scratch3_microbit/index.js +984 -0
- package/src/extensions/scratch3_music/assets/drums/1-snare.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/10-wood-block.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/11-cowbell.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/12-triangle.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/13-bongo.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/14-conga.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/15-cabasa.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/16-guiro.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/17-vibraslap.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/18-cuica.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/2-bass-drum.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/3-side-stick.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/4-crash-cymbal.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/5-open-hi-hat.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/6-closed-hi-hat.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/7-tambourine.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/8-hand-clap.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/9-claves.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/108.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/24.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/72.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/84.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/96.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/10-clarinet/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/10-clarinet/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/11-saxophone/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/11-saxophone/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/11-saxophone/84.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/12-flute/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/12-flute/72.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/13-wooden-flute/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/13-wooden-flute/72.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/14-bassoon/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/14-bassoon/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/14-bassoon/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/15-choir/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/15-choir/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/15-choir/72.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/16-vibraphone/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/16-vibraphone/72.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/17-music-box/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/18-steel-drum/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/19-marimba/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/2-electric-piano/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/20-synth-lead/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/21-synth-pad/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/3-organ/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/4-guitar/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/5-electric-guitar/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/6-bass/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/6-bass/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/7-pizzicato/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/8-cello/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/8-cello/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/8-cello/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/9-trombone/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/9-trombone/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/9-trombone/60.mp3 +0 -0
- package/src/extensions/scratch3_music/index.js +1333 -0
- package/src/extensions/scratch3_music/manifest.js +63 -0
- package/src/extensions/scratch3_pen/index.js +770 -0
- package/src/extensions/scratch3_speech2text/index.js +700 -0
- package/src/extensions/scratch3_text2speech/index.js +766 -0
- package/src/extensions/scratch3_translate/index.js +286 -0
- package/src/extensions/scratch3_video_sensing/debug.js +13 -0
- package/src/extensions/scratch3_video_sensing/index.js +594 -0
- package/src/extensions/scratch3_video_sensing/library.js +384 -0
- package/src/extensions/scratch3_video_sensing/math.js +76 -0
- package/src/extensions/scratch3_video_sensing/view.js +509 -0
- package/src/extensions/scratch3_wedo2/index.js +1616 -0
- package/src/import/load-costume.js +416 -0
- package/src/import/load-sound.js +116 -0
- package/src/index.js +10 -0
- package/src/io/ble.js +256 -0
- package/src/io/bt.js +202 -0
- package/src/io/clock.js +39 -0
- package/src/io/cloud.js +169 -0
- package/src/io/keyboard.js +146 -0
- package/src/io/mouse.js +145 -0
- package/src/io/mouseWheel.js +28 -0
- package/src/io/userData.js +24 -0
- package/src/io/video.js +210 -0
- package/src/playground/benchmark.css +83 -0
- package/src/playground/benchmark.js +753 -0
- package/src/playground/index.html +111 -0
- package/src/playground/suite.css +78 -0
- package/src/playground/suite.html +25 -0
- package/src/playground/suite.js +544 -0
- package/src/playground/video-sensing.html +18 -0
- package/src/playground/video-sensing.js +133 -0
- package/src/serialization/deserialize-assets.js +178 -0
- package/src/serialization/sb2.js +1295 -0
- package/src/serialization/sb2_specmap.js +1818 -0
- package/src/serialization/sb3.js +1320 -0
- package/src/serialization/serialize-assets.js +60 -0
- package/src/sprites/rendered-target.js +1115 -0
- package/src/sprites/sprite.js +177 -0
- package/src/util/base64-util.js +48 -0
- package/src/util/cast.js +218 -0
- package/src/util/clone.js +17 -0
- package/src/util/color.js +204 -0
- package/src/util/fetch-with-timeout.js +53 -0
- package/src/util/get-monitor-id.js +33 -0
- package/src/util/jsonrpc.js +114 -0
- package/src/util/log.js +4 -0
- package/src/util/math-util.js +122 -0
- package/src/util/maybe-format-message.js +18 -0
- package/src/util/new-block-ids.js +33 -0
- package/src/util/rateLimiter.js +73 -0
- package/src/util/scratch-link-websocket.js +133 -0
- package/src/util/string-util.js +94 -0
- package/src/util/task-queue.js +203 -0
- package/src/util/timer.js +103 -0
- package/src/util/uid.js +29 -0
- package/src/util/variable-util.js +48 -0
- package/src/util/xml-escape.js +33 -0
- package/src/virtual-machine.js +1586 -0
|
@@ -0,0 +1,1115 @@
|
|
|
1
|
+
const MathUtil = require('../util/math-util');
|
|
2
|
+
const StringUtil = require('../util/string-util');
|
|
3
|
+
const Cast = require('../util/cast');
|
|
4
|
+
const Clone = require('../util/clone');
|
|
5
|
+
const Target = require('../engine/target');
|
|
6
|
+
const StageLayering = require('../engine/stage-layering');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Rendered target: instance of a sprite (clone), or the stage.
|
|
10
|
+
*/
|
|
11
|
+
class RenderedTarget extends Target {
|
|
12
|
+
/**
|
|
13
|
+
* @param {!Sprite} sprite Reference to the parent sprite.
|
|
14
|
+
* @param {Runtime} runtime Reference to the runtime.
|
|
15
|
+
* @constructor
|
|
16
|
+
*/
|
|
17
|
+
constructor (sprite, runtime) {
|
|
18
|
+
super(runtime, sprite.blocks);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Reference to the sprite that this is a render of.
|
|
22
|
+
* @type {!Sprite}
|
|
23
|
+
*/
|
|
24
|
+
this.sprite = sprite;
|
|
25
|
+
/**
|
|
26
|
+
* Reference to the global renderer for this VM, if one exists.
|
|
27
|
+
* @type {?RenderWebGL}
|
|
28
|
+
*/
|
|
29
|
+
this.renderer = null;
|
|
30
|
+
if (this.runtime) {
|
|
31
|
+
this.renderer = this.runtime.renderer;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* ID of the drawable for this rendered target,
|
|
35
|
+
* returned by the renderer, if rendered.
|
|
36
|
+
* @type {?Number}
|
|
37
|
+
*/
|
|
38
|
+
this.drawableID = null;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Drag state of this rendered target. If true, x/y position can't be
|
|
42
|
+
* changed by blocks.
|
|
43
|
+
* @type {boolean}
|
|
44
|
+
*/
|
|
45
|
+
this.dragging = false;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Map of current graphic effect values.
|
|
49
|
+
* @type {!Object.<string, number>}
|
|
50
|
+
*/
|
|
51
|
+
this.effects = {
|
|
52
|
+
color: 0,
|
|
53
|
+
fisheye: 0,
|
|
54
|
+
whirl: 0,
|
|
55
|
+
pixelate: 0,
|
|
56
|
+
mosaic: 0,
|
|
57
|
+
brightness: 0,
|
|
58
|
+
ghost: 0
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether this represents an "original" non-clone rendered-target for a sprite,
|
|
63
|
+
* i.e., created by the editor and not clone blocks.
|
|
64
|
+
* @type {boolean}
|
|
65
|
+
*/
|
|
66
|
+
this.isOriginal = true;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Whether this rendered target represents the Scratch stage.
|
|
70
|
+
* @type {boolean}
|
|
71
|
+
*/
|
|
72
|
+
this.isStage = false;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Scratch X coordinate. Currently should range from -240 to 240.
|
|
76
|
+
* @type {Number}
|
|
77
|
+
*/
|
|
78
|
+
this.x = 0;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Scratch Y coordinate. Currently should range from -180 to 180.
|
|
82
|
+
* @type {number}
|
|
83
|
+
*/
|
|
84
|
+
this.y = 0;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Scratch direction. Currently should range from -179 to 180.
|
|
88
|
+
* @type {number}
|
|
89
|
+
*/
|
|
90
|
+
this.direction = 90;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Whether the rendered target is draggable on the stage
|
|
94
|
+
* @type {boolean}
|
|
95
|
+
*/
|
|
96
|
+
this.draggable = false;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Whether the rendered target is currently visible.
|
|
100
|
+
* @type {boolean}
|
|
101
|
+
*/
|
|
102
|
+
this.visible = true;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Size of rendered target as a percent of costume size.
|
|
106
|
+
* @type {number}
|
|
107
|
+
*/
|
|
108
|
+
this.size = 100;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Currently selected costume index.
|
|
112
|
+
* @type {number}
|
|
113
|
+
*/
|
|
114
|
+
this.currentCostume = 0;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Current rotation style.
|
|
118
|
+
* @type {!string}
|
|
119
|
+
*/
|
|
120
|
+
this.rotationStyle = RenderedTarget.ROTATION_STYLE_ALL_AROUND;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Loudness for sound playback for this target, as a percentage.
|
|
124
|
+
* @type {number}
|
|
125
|
+
*/
|
|
126
|
+
this.volume = 100;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Current tempo (used by the music extension).
|
|
130
|
+
* This property is global to the project and stored in the stage.
|
|
131
|
+
* @type {number}
|
|
132
|
+
*/
|
|
133
|
+
this.tempo = 60;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The transparency of the video (used by extensions with camera input).
|
|
137
|
+
* This property is global to the project and stored in the stage.
|
|
138
|
+
* @type {number}
|
|
139
|
+
*/
|
|
140
|
+
this.videoTransparency = 50;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The state of the video input (used by extensions with camera input).
|
|
144
|
+
* This property is global to the project and stored in the stage.
|
|
145
|
+
*
|
|
146
|
+
* Defaults to ON. This setting does not turn the video by itself. A
|
|
147
|
+
* video extension once loaded will set the video device to this
|
|
148
|
+
* setting. Set to ON when a video extension is added in the editor the
|
|
149
|
+
* video will start ON. If the extension is loaded as part of loading a
|
|
150
|
+
* saved project the extension will see the value set when the stage
|
|
151
|
+
* was loaded from the saved values including the video state.
|
|
152
|
+
*
|
|
153
|
+
* @type {string}
|
|
154
|
+
*/
|
|
155
|
+
this.videoState = RenderedTarget.VIDEO_STATE.ON;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The language to use for speech synthesis, in the text2speech extension.
|
|
159
|
+
* It is initialized to null so that on extension load, we can check for
|
|
160
|
+
* this and try setting it using the editor locale.
|
|
161
|
+
* @type {string}
|
|
162
|
+
*/
|
|
163
|
+
this.textToSpeechLanguage = null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Create a drawable with the this.renderer.
|
|
168
|
+
* @param {boolean} layerGroup The layer group this drawable should be added to
|
|
169
|
+
*/
|
|
170
|
+
initDrawable (layerGroup) {
|
|
171
|
+
if (this.renderer) {
|
|
172
|
+
this.drawableID = this.renderer.createDrawable(layerGroup);
|
|
173
|
+
}
|
|
174
|
+
// If we're a clone, start the hats.
|
|
175
|
+
if (!this.isOriginal) {
|
|
176
|
+
this.runtime.startHats(
|
|
177
|
+
'control_start_as_clone', null, this
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
get audioPlayer () {
|
|
183
|
+
/* eslint-disable no-console */
|
|
184
|
+
console.warn('get audioPlayer deprecated, please update to use .sprite.soundBank methods');
|
|
185
|
+
console.warn(new Error('stack for debug').stack);
|
|
186
|
+
/* eslint-enable no-console */
|
|
187
|
+
const bank = this.sprite.soundBank;
|
|
188
|
+
const audioPlayerProxy = {
|
|
189
|
+
playSound: soundId => bank.play(this, soundId)
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
Object.defineProperty(this, 'audioPlayer', {
|
|
193
|
+
configurable: false,
|
|
194
|
+
enumerable: true,
|
|
195
|
+
writable: false,
|
|
196
|
+
value: audioPlayerProxy
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
return audioPlayerProxy;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Initialize the audio player for this sprite or clone.
|
|
204
|
+
*/
|
|
205
|
+
initAudio () {
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Event which fires when a target moves.
|
|
210
|
+
* @type {string}
|
|
211
|
+
*/
|
|
212
|
+
static get EVENT_TARGET_MOVED () {
|
|
213
|
+
return 'TARGET_MOVED';
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Event which fires when a target changes visually, for updating say bubbles.
|
|
218
|
+
* @type {string}
|
|
219
|
+
*/
|
|
220
|
+
static get EVENT_TARGET_VISUAL_CHANGE () {
|
|
221
|
+
return 'EVENT_TARGET_VISUAL_CHANGE';
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Rotation style for "all around"/spinning.
|
|
226
|
+
* @type {string}
|
|
227
|
+
*/
|
|
228
|
+
static get ROTATION_STYLE_ALL_AROUND () {
|
|
229
|
+
return 'all around';
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Rotation style for "left-right"/flipping.
|
|
234
|
+
* @type {string}
|
|
235
|
+
*/
|
|
236
|
+
static get ROTATION_STYLE_LEFT_RIGHT () {
|
|
237
|
+
return 'left-right';
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Rotation style for "no rotation."
|
|
242
|
+
* @type {string}
|
|
243
|
+
*/
|
|
244
|
+
static get ROTATION_STYLE_NONE () {
|
|
245
|
+
return "don't rotate";
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Available states for video input.
|
|
250
|
+
* @enum {string}
|
|
251
|
+
*/
|
|
252
|
+
static get VIDEO_STATE () {
|
|
253
|
+
return {
|
|
254
|
+
OFF: 'off',
|
|
255
|
+
ON: 'on',
|
|
256
|
+
ON_FLIPPED: 'on-flipped'
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Set the X and Y coordinates.
|
|
262
|
+
* @param {!number} x New X coordinate, in Scratch coordinates.
|
|
263
|
+
* @param {!number} y New Y coordinate, in Scratch coordinates.
|
|
264
|
+
* @param {?boolean} force Force setting X/Y, in case of dragging
|
|
265
|
+
*/
|
|
266
|
+
setXY (x, y, force) {
|
|
267
|
+
if (this.isStage) return;
|
|
268
|
+
if (this.dragging && !force) return;
|
|
269
|
+
const oldX = this.x;
|
|
270
|
+
const oldY = this.y;
|
|
271
|
+
if (this.renderer) {
|
|
272
|
+
const position = this.renderer.getFencedPositionOfDrawable(this.drawableID, [x, y]);
|
|
273
|
+
this.x = position[0];
|
|
274
|
+
this.y = position[1];
|
|
275
|
+
|
|
276
|
+
this.renderer.updateDrawablePosition(this.drawableID, position);
|
|
277
|
+
if (this.visible) {
|
|
278
|
+
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
|
279
|
+
this.runtime.requestRedraw();
|
|
280
|
+
}
|
|
281
|
+
} else {
|
|
282
|
+
this.x = x;
|
|
283
|
+
this.y = y;
|
|
284
|
+
}
|
|
285
|
+
this.emit(RenderedTarget.EVENT_TARGET_MOVED, this, oldX, oldY, force);
|
|
286
|
+
this.runtime.requestTargetsUpdate(this);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Get the rendered direction and scale, after applying rotation style.
|
|
291
|
+
* @return {object<string, number>} Direction and scale to render.
|
|
292
|
+
*/
|
|
293
|
+
_getRenderedDirectionAndScale () {
|
|
294
|
+
// Default: no changes to `this.direction` or `this.scale`.
|
|
295
|
+
let finalDirection = this.direction;
|
|
296
|
+
let finalScale = [this.size, this.size];
|
|
297
|
+
if (this.rotationStyle === RenderedTarget.ROTATION_STYLE_NONE) {
|
|
298
|
+
// Force rendered direction to be 90.
|
|
299
|
+
finalDirection = 90;
|
|
300
|
+
} else if (this.rotationStyle === RenderedTarget.ROTATION_STYLE_LEFT_RIGHT) {
|
|
301
|
+
// Force rendered direction to be 90, and flip drawable if needed.
|
|
302
|
+
finalDirection = 90;
|
|
303
|
+
const scaleFlip = (this.direction < 0) ? -1 : 1;
|
|
304
|
+
finalScale = [scaleFlip * this.size, this.size];
|
|
305
|
+
}
|
|
306
|
+
return {direction: finalDirection, scale: finalScale};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Set the direction.
|
|
311
|
+
* @param {!number} direction New direction.
|
|
312
|
+
*/
|
|
313
|
+
setDirection (direction) {
|
|
314
|
+
if (this.isStage) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (!isFinite(direction)) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
// Keep direction between -179 and +180.
|
|
321
|
+
this.direction = MathUtil.wrapClamp(direction, -179, 180);
|
|
322
|
+
if (this.renderer) {
|
|
323
|
+
const {direction: renderedDirection, scale} = this._getRenderedDirectionAndScale();
|
|
324
|
+
this.renderer.updateDrawableDirectionScale(this.drawableID, renderedDirection, scale);
|
|
325
|
+
if (this.visible) {
|
|
326
|
+
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
|
327
|
+
this.runtime.requestRedraw();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
this.runtime.requestTargetsUpdate(this);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Set draggability; i.e., whether it's able to be dragged in the player
|
|
335
|
+
* @param {!boolean} draggable True if should be draggable.
|
|
336
|
+
*/
|
|
337
|
+
setDraggable (draggable) {
|
|
338
|
+
if (this.isStage) return;
|
|
339
|
+
this.draggable = !!draggable;
|
|
340
|
+
this.runtime.requestTargetsUpdate(this);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Set visibility; i.e., whether it's shown or hidden.
|
|
345
|
+
* @param {!boolean} visible True if should be shown.
|
|
346
|
+
*/
|
|
347
|
+
setVisible (visible) {
|
|
348
|
+
if (this.isStage) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
this.visible = !!visible;
|
|
352
|
+
if (this.renderer) {
|
|
353
|
+
this.renderer.updateDrawableVisible(this.drawableID, this.visible);
|
|
354
|
+
if (this.visible) {
|
|
355
|
+
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
|
356
|
+
this.runtime.requestRedraw();
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
this.runtime.requestTargetsUpdate(this);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Set size, as a percentage of the costume size.
|
|
364
|
+
* @param {!number} size Size of rendered target, as % of costume size.
|
|
365
|
+
*/
|
|
366
|
+
setSize (size) {
|
|
367
|
+
if (this.isStage) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (this.renderer) {
|
|
371
|
+
// Clamp to scales relative to costume and stage size.
|
|
372
|
+
// See original ScratchSprite.as:setSize.
|
|
373
|
+
const costumeSize = this.renderer.getCurrentSkinSize(this.drawableID);
|
|
374
|
+
const origW = costumeSize[0];
|
|
375
|
+
const origH = costumeSize[1];
|
|
376
|
+
const minScale = Math.min(1, Math.max(5 / origW, 5 / origH));
|
|
377
|
+
const maxScale = Math.min(
|
|
378
|
+
(1.5 * this.runtime.constructor.STAGE_WIDTH) / origW,
|
|
379
|
+
(1.5 * this.runtime.constructor.STAGE_HEIGHT) / origH
|
|
380
|
+
);
|
|
381
|
+
this.size = MathUtil.clamp(size / 100, minScale, maxScale) * 100;
|
|
382
|
+
const {direction, scale} = this._getRenderedDirectionAndScale();
|
|
383
|
+
this.renderer.updateDrawableDirectionScale(this.drawableID, direction, scale);
|
|
384
|
+
if (this.visible) {
|
|
385
|
+
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
|
386
|
+
this.runtime.requestRedraw();
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
this.runtime.requestTargetsUpdate(this);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Set a particular graphic effect value.
|
|
394
|
+
* @param {!string} effectName Name of effect (see `RenderedTarget.prototype.effects`).
|
|
395
|
+
* @param {!number} value Numerical magnitude of effect.
|
|
396
|
+
*/
|
|
397
|
+
setEffect (effectName, value) {
|
|
398
|
+
if (!Object.prototype.hasOwnProperty.call(this.effects, effectName)) return;
|
|
399
|
+
this.effects[effectName] = value;
|
|
400
|
+
if (this.renderer) {
|
|
401
|
+
this.renderer.updateDrawableEffect(this.drawableID, effectName, value);
|
|
402
|
+
if (this.visible) {
|
|
403
|
+
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
|
404
|
+
this.runtime.requestRedraw();
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Clear all graphic effects on this rendered target.
|
|
411
|
+
*/
|
|
412
|
+
clearEffects () {
|
|
413
|
+
for (const effectName in this.effects) {
|
|
414
|
+
if (!Object.prototype.hasOwnProperty.call(this.effects, effectName)) continue;
|
|
415
|
+
this.effects[effectName] = 0;
|
|
416
|
+
}
|
|
417
|
+
if (this.renderer) {
|
|
418
|
+
for (const effectName in this.effects) {
|
|
419
|
+
if (!Object.prototype.hasOwnProperty.call(this.effects, effectName)) continue;
|
|
420
|
+
this.renderer.updateDrawableEffect(this.drawableID, effectName, 0);
|
|
421
|
+
}
|
|
422
|
+
if (this.visible) {
|
|
423
|
+
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
|
424
|
+
this.runtime.requestRedraw();
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Set the current costume.
|
|
431
|
+
* @param {number} index New index of costume.
|
|
432
|
+
*/
|
|
433
|
+
setCostume (index) {
|
|
434
|
+
// Keep the costume index within possible values.
|
|
435
|
+
index = Math.round(index);
|
|
436
|
+
if ([Infinity, -Infinity, NaN].includes(index)) index = 0;
|
|
437
|
+
|
|
438
|
+
this.currentCostume = MathUtil.wrapClamp(
|
|
439
|
+
index, 0, this.sprite.costumes.length - 1
|
|
440
|
+
);
|
|
441
|
+
if (this.renderer) {
|
|
442
|
+
const costume = this.getCostumes()[this.currentCostume];
|
|
443
|
+
this.renderer.updateDrawableSkinId(this.drawableID, costume.skinId);
|
|
444
|
+
|
|
445
|
+
if (this.visible) {
|
|
446
|
+
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
|
447
|
+
this.runtime.requestRedraw();
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
this.runtime.requestTargetsUpdate(this);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Add a costume, taking care to avoid duplicate names.
|
|
455
|
+
* @param {!object} costumeObject Object representing the costume.
|
|
456
|
+
* @param {?int} index Index at which to add costume
|
|
457
|
+
*/
|
|
458
|
+
addCostume (costumeObject, index) {
|
|
459
|
+
if (typeof index === 'number' && !isNaN(index)) {
|
|
460
|
+
this.sprite.addCostumeAt(costumeObject, index);
|
|
461
|
+
} else {
|
|
462
|
+
this.sprite.addCostumeAt(costumeObject, this.sprite.costumes.length);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Rename a costume, taking care to avoid duplicate names.
|
|
468
|
+
* @param {int} costumeIndex - the index of the costume to be renamed.
|
|
469
|
+
* @param {string} newName - the desired new name of the costume (will be modified if already in use).
|
|
470
|
+
*/
|
|
471
|
+
renameCostume (costumeIndex, newName) {
|
|
472
|
+
const usedNames = this.sprite.costumes
|
|
473
|
+
.filter((costume, index) => costumeIndex !== index)
|
|
474
|
+
.map(costume => costume.name);
|
|
475
|
+
const oldName = this.getCostumes()[costumeIndex].name;
|
|
476
|
+
const newUnusedName = StringUtil.unusedName(newName, usedNames);
|
|
477
|
+
this.getCostumes()[costumeIndex].name = newUnusedName;
|
|
478
|
+
|
|
479
|
+
if (this.isStage) {
|
|
480
|
+
// Since this is a backdrop, go through all targets and
|
|
481
|
+
// update any blocks referencing the old backdrop name
|
|
482
|
+
const targets = this.runtime.targets;
|
|
483
|
+
for (let i = 0; i < targets.length; i++) {
|
|
484
|
+
const currTarget = targets[i];
|
|
485
|
+
currTarget.blocks.updateAssetName(oldName, newUnusedName, 'backdrop');
|
|
486
|
+
}
|
|
487
|
+
} else {
|
|
488
|
+
this.blocks.updateAssetName(oldName, newUnusedName, 'costume');
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Delete a costume by index.
|
|
495
|
+
* @param {number} index Costume index to be deleted
|
|
496
|
+
* @return {?object} The costume that was deleted or null
|
|
497
|
+
* if the index was out of bounds of the costumes list or
|
|
498
|
+
* this target only has one costume.
|
|
499
|
+
*/
|
|
500
|
+
deleteCostume (index) {
|
|
501
|
+
const originalCostumeCount = this.sprite.costumes.length;
|
|
502
|
+
if (originalCostumeCount === 1) return null;
|
|
503
|
+
|
|
504
|
+
if (index < 0 || index >= originalCostumeCount) {
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
const deletedCostume = this.sprite.deleteCostumeAt(index);
|
|
509
|
+
|
|
510
|
+
if (index === this.currentCostume && index === originalCostumeCount - 1) {
|
|
511
|
+
this.setCostume(index - 1);
|
|
512
|
+
} else if (index < this.currentCostume) {
|
|
513
|
+
this.setCostume(this.currentCostume - 1);
|
|
514
|
+
} else {
|
|
515
|
+
this.setCostume(this.currentCostume);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
this.runtime.requestTargetsUpdate(this);
|
|
519
|
+
return deletedCostume;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Add a sound, taking care to avoid duplicate names.
|
|
524
|
+
* @param {!object} soundObject Object representing the sound.
|
|
525
|
+
* @param {?int} index Index at which to add costume
|
|
526
|
+
*/
|
|
527
|
+
addSound (soundObject, index) {
|
|
528
|
+
const usedNames = this.sprite.sounds.map(sound => sound.name);
|
|
529
|
+
soundObject.name = StringUtil.unusedName(soundObject.name, usedNames);
|
|
530
|
+
if (typeof index === 'number' && !isNaN(index)) {
|
|
531
|
+
this.sprite.sounds.splice(index, 0, soundObject);
|
|
532
|
+
} else {
|
|
533
|
+
this.sprite.sounds.push(soundObject);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Rename a sound, taking care to avoid duplicate names.
|
|
539
|
+
* @param {int} soundIndex - the index of the sound to be renamed.
|
|
540
|
+
* @param {string} newName - the desired new name of the sound (will be modified if already in use).
|
|
541
|
+
*/
|
|
542
|
+
renameSound (soundIndex, newName) {
|
|
543
|
+
const usedNames = this.sprite.sounds
|
|
544
|
+
.filter((sound, index) => soundIndex !== index)
|
|
545
|
+
.map(sound => sound.name);
|
|
546
|
+
const oldName = this.sprite.sounds[soundIndex].name;
|
|
547
|
+
const newUnusedName = StringUtil.unusedName(newName, usedNames);
|
|
548
|
+
this.sprite.sounds[soundIndex].name = newUnusedName;
|
|
549
|
+
this.blocks.updateAssetName(oldName, newUnusedName, 'sound');
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Delete a sound by index.
|
|
554
|
+
* @param {number} index Sound index to be deleted
|
|
555
|
+
* @return {object} The deleted sound object, or null if no sound was deleted.
|
|
556
|
+
*/
|
|
557
|
+
deleteSound (index) {
|
|
558
|
+
// Make sure the sound index is not out of bounds
|
|
559
|
+
if (index < 0 || index >= this.sprite.sounds.length) {
|
|
560
|
+
return null;
|
|
561
|
+
}
|
|
562
|
+
// Delete the sound at the given index
|
|
563
|
+
const deletedSound = this.sprite.sounds.splice(index, 1)[0];
|
|
564
|
+
this.runtime.requestTargetsUpdate(this);
|
|
565
|
+
return deletedSound;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Update the rotation style.
|
|
570
|
+
* @param {!string} rotationStyle New rotation style.
|
|
571
|
+
*/
|
|
572
|
+
setRotationStyle (rotationStyle) {
|
|
573
|
+
if (rotationStyle === RenderedTarget.ROTATION_STYLE_NONE) {
|
|
574
|
+
this.rotationStyle = RenderedTarget.ROTATION_STYLE_NONE;
|
|
575
|
+
} else if (rotationStyle === RenderedTarget.ROTATION_STYLE_ALL_AROUND) {
|
|
576
|
+
this.rotationStyle = RenderedTarget.ROTATION_STYLE_ALL_AROUND;
|
|
577
|
+
} else if (rotationStyle === RenderedTarget.ROTATION_STYLE_LEFT_RIGHT) {
|
|
578
|
+
this.rotationStyle = RenderedTarget.ROTATION_STYLE_LEFT_RIGHT;
|
|
579
|
+
}
|
|
580
|
+
if (this.renderer) {
|
|
581
|
+
const {direction, scale} = this._getRenderedDirectionAndScale();
|
|
582
|
+
this.renderer.updateDrawableDirectionScale(this.drawableID, direction, scale);
|
|
583
|
+
if (this.visible) {
|
|
584
|
+
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
|
585
|
+
this.runtime.requestRedraw();
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
this.runtime.requestTargetsUpdate(this);
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Get a costume index of this rendered target, by name of the costume.
|
|
593
|
+
* @param {?string} costumeName Name of a costume.
|
|
594
|
+
* @return {number} Index of the named costume, or -1 if not present.
|
|
595
|
+
*/
|
|
596
|
+
getCostumeIndexByName (costumeName) {
|
|
597
|
+
for (let i = 0; i < this.sprite.costumes.length; i++) {
|
|
598
|
+
if (this.getCostumes()[i].name === costumeName) {
|
|
599
|
+
return i;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return -1;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* Get a costume of this rendered target by id.
|
|
607
|
+
* @return {object} current costume
|
|
608
|
+
*/
|
|
609
|
+
getCurrentCostume () {
|
|
610
|
+
return this.getCostumes()[this.currentCostume];
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Get full costume list
|
|
615
|
+
* @return {object[]} list of costumes
|
|
616
|
+
*/
|
|
617
|
+
getCostumes () {
|
|
618
|
+
return this.sprite.costumes;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Reorder costume list by moving costume at costumeIndex to newIndex.
|
|
623
|
+
* @param {!number} costumeIndex Index of the costume to move.
|
|
624
|
+
* @param {!number} newIndex New index for that costume.
|
|
625
|
+
* @returns {boolean} If a change occurred (i.e. if the indices do not match)
|
|
626
|
+
*/
|
|
627
|
+
reorderCostume (costumeIndex, newIndex) {
|
|
628
|
+
newIndex = MathUtil.clamp(newIndex, 0, this.sprite.costumes.length - 1);
|
|
629
|
+
costumeIndex = MathUtil.clamp(costumeIndex, 0, this.sprite.costumes.length - 1);
|
|
630
|
+
|
|
631
|
+
if (newIndex === costumeIndex) return false;
|
|
632
|
+
|
|
633
|
+
const currentCostume = this.getCurrentCostume();
|
|
634
|
+
const costume = this.sprite.costumes[costumeIndex];
|
|
635
|
+
|
|
636
|
+
// Use the sprite method for deleting costumes because setCostume is handled manually
|
|
637
|
+
this.sprite.deleteCostumeAt(costumeIndex);
|
|
638
|
+
|
|
639
|
+
this.addCostume(costume, newIndex);
|
|
640
|
+
this.currentCostume = this.getCostumeIndexByName(currentCostume.name);
|
|
641
|
+
return true;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Reorder sound list by moving sound at soundIndex to newIndex.
|
|
646
|
+
* @param {!number} soundIndex Index of the sound to move.
|
|
647
|
+
* @param {!number} newIndex New index for that sound.
|
|
648
|
+
* @returns {boolean} If a change occurred (i.e. if the indices do not match)
|
|
649
|
+
*/
|
|
650
|
+
reorderSound (soundIndex, newIndex) {
|
|
651
|
+
newIndex = MathUtil.clamp(newIndex, 0, this.sprite.sounds.length - 1);
|
|
652
|
+
soundIndex = MathUtil.clamp(soundIndex, 0, this.sprite.sounds.length - 1);
|
|
653
|
+
|
|
654
|
+
if (newIndex === soundIndex) return false;
|
|
655
|
+
|
|
656
|
+
const sound = this.sprite.sounds[soundIndex];
|
|
657
|
+
this.deleteSound(soundIndex);
|
|
658
|
+
this.addSound(sound, newIndex);
|
|
659
|
+
return true;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* Get full sound list
|
|
664
|
+
* @return {object[]} list of sounds
|
|
665
|
+
*/
|
|
666
|
+
getSounds () {
|
|
667
|
+
return this.sprite.sounds;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Update all drawable properties for this rendered target.
|
|
672
|
+
* Use when a batch has changed, e.g., when the drawable is first created.
|
|
673
|
+
*/
|
|
674
|
+
updateAllDrawableProperties () {
|
|
675
|
+
if (this.renderer) {
|
|
676
|
+
const {direction, scale} = this._getRenderedDirectionAndScale();
|
|
677
|
+
this.renderer.updateDrawablePosition(this.drawableID, [this.x, this.y]);
|
|
678
|
+
this.renderer.updateDrawableDirectionScale(this.drawableID, direction, scale);
|
|
679
|
+
this.renderer.updateDrawableVisible(this.drawableID, this.visible);
|
|
680
|
+
|
|
681
|
+
const costume = this.getCostumes()[this.currentCostume];
|
|
682
|
+
this.renderer.updateDrawableSkinId(this.drawableID, costume.skinId);
|
|
683
|
+
|
|
684
|
+
for (const effectName in this.effects) {
|
|
685
|
+
if (!Object.prototype.hasOwnProperty.call(this.effects, effectName)) continue;
|
|
686
|
+
this.renderer.updateDrawableEffect(this.drawableID, effectName, this.effects[effectName]);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
if (this.visible) {
|
|
690
|
+
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
|
691
|
+
this.runtime.requestRedraw();
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
this.runtime.requestTargetsUpdate(this);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Return the human-readable name for this rendered target, e.g., the sprite's name.
|
|
699
|
+
* @override
|
|
700
|
+
* @returns {string} Human-readable name.
|
|
701
|
+
*/
|
|
702
|
+
getName () {
|
|
703
|
+
return this.sprite.name;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Return whether this rendered target is a sprite (not a clone, not the stage).
|
|
708
|
+
* @return {boolean} True if not a clone and not the stage.
|
|
709
|
+
*/
|
|
710
|
+
isSprite () {
|
|
711
|
+
return !this.isStage && this.isOriginal;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Return the rendered target's tight bounding box.
|
|
716
|
+
* Includes top, left, bottom, right attributes in Scratch coordinates.
|
|
717
|
+
* @return {?object} Tight bounding box, or null.
|
|
718
|
+
*/
|
|
719
|
+
getBounds () {
|
|
720
|
+
if (this.renderer) {
|
|
721
|
+
return this.runtime.renderer.getBounds(this.drawableID);
|
|
722
|
+
}
|
|
723
|
+
return null;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Return the bounding box around a slice of the top 8px of the rendered target.
|
|
728
|
+
* Includes top, left, bottom, right attributes in Scratch coordinates.
|
|
729
|
+
* @return {?object} Tight bounding box, or null.
|
|
730
|
+
*/
|
|
731
|
+
getBoundsForBubble () {
|
|
732
|
+
if (this.renderer) {
|
|
733
|
+
return this.runtime.renderer.getBoundsForBubble(this.drawableID);
|
|
734
|
+
}
|
|
735
|
+
return null;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* Return whether this target is touching the mouse, an edge, or a sprite.
|
|
740
|
+
* @param {string} requestedObject an id for mouse or edge, or a sprite name.
|
|
741
|
+
* @return {boolean} True if the sprite is touching the object.
|
|
742
|
+
*/
|
|
743
|
+
isTouchingObject (requestedObject) {
|
|
744
|
+
if (requestedObject === '_mouse_') {
|
|
745
|
+
if (!this.runtime.ioDevices.mouse) return false;
|
|
746
|
+
const mouseX = this.runtime.ioDevices.mouse.getClientX();
|
|
747
|
+
const mouseY = this.runtime.ioDevices.mouse.getClientY();
|
|
748
|
+
return this.isTouchingPoint(mouseX, mouseY);
|
|
749
|
+
} else if (requestedObject === '_edge_') {
|
|
750
|
+
return this.isTouchingEdge();
|
|
751
|
+
}
|
|
752
|
+
return this.isTouchingSprite(requestedObject);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* Return whether touching a point.
|
|
757
|
+
* @param {number} x X coordinate of test point.
|
|
758
|
+
* @param {number} y Y coordinate of test point.
|
|
759
|
+
* @return {boolean} True iff the rendered target is touching the point.
|
|
760
|
+
*/
|
|
761
|
+
isTouchingPoint (x, y) {
|
|
762
|
+
if (this.renderer) {
|
|
763
|
+
return this.renderer.drawableTouching(this.drawableID, x, y);
|
|
764
|
+
}
|
|
765
|
+
return false;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* Return whether touching a stage edge.
|
|
770
|
+
* @return {boolean} True iff the rendered target is touching the stage edge.
|
|
771
|
+
*/
|
|
772
|
+
isTouchingEdge () {
|
|
773
|
+
if (this.renderer) {
|
|
774
|
+
const stageWidth = this.runtime.constructor.STAGE_WIDTH;
|
|
775
|
+
const stageHeight = this.runtime.constructor.STAGE_HEIGHT;
|
|
776
|
+
const bounds = this.getBounds();
|
|
777
|
+
if (bounds.left < -stageWidth / 2 ||
|
|
778
|
+
bounds.right > stageWidth / 2 ||
|
|
779
|
+
bounds.top > stageHeight / 2 ||
|
|
780
|
+
bounds.bottom < -stageHeight / 2) {
|
|
781
|
+
return true;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
return false;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* Return whether touching any of a named sprite's clones.
|
|
789
|
+
* @param {string} spriteName Name of the sprite.
|
|
790
|
+
* @return {boolean} True iff touching a clone of the sprite.
|
|
791
|
+
*/
|
|
792
|
+
isTouchingSprite (spriteName) {
|
|
793
|
+
spriteName = Cast.toString(spriteName);
|
|
794
|
+
const firstClone = this.runtime.getSpriteTargetByName(spriteName);
|
|
795
|
+
if (!firstClone || !this.renderer) {
|
|
796
|
+
return false;
|
|
797
|
+
}
|
|
798
|
+
// Filter out dragging targets. This means a sprite that is being dragged
|
|
799
|
+
// can detect other sprites using touching <sprite>, but cannot be detected
|
|
800
|
+
// by other sprites while it is being dragged. This matches Scratch 2.0 behavior.
|
|
801
|
+
const drawableCandidates = firstClone.sprite.clones.filter(clone => !clone.dragging)
|
|
802
|
+
.map(clone => clone.drawableID);
|
|
803
|
+
return this.renderer.isTouchingDrawables(
|
|
804
|
+
this.drawableID, drawableCandidates);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* Return whether touching a color.
|
|
809
|
+
* @param {Array.<number>} rgb [r,g,b], values between 0-255.
|
|
810
|
+
* @return {Promise.<boolean>} True iff the rendered target is touching the color.
|
|
811
|
+
*/
|
|
812
|
+
isTouchingColor (rgb) {
|
|
813
|
+
if (this.renderer) {
|
|
814
|
+
return this.renderer.isTouchingColor(this.drawableID, rgb);
|
|
815
|
+
}
|
|
816
|
+
return false;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* Return whether rendered target's color is touching a color.
|
|
821
|
+
* @param {object} targetRgb {Array.<number>} [r,g,b], values between 0-255.
|
|
822
|
+
* @param {object} maskRgb {Array.<number>} [r,g,b], values between 0-255.
|
|
823
|
+
* @return {Promise.<boolean>} True iff the color is touching the color.
|
|
824
|
+
*/
|
|
825
|
+
colorIsTouchingColor (targetRgb, maskRgb) {
|
|
826
|
+
if (this.renderer) {
|
|
827
|
+
return this.renderer.isTouchingColor(
|
|
828
|
+
this.drawableID,
|
|
829
|
+
targetRgb,
|
|
830
|
+
maskRgb
|
|
831
|
+
);
|
|
832
|
+
}
|
|
833
|
+
return false;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
getLayerOrder () {
|
|
837
|
+
if (this.renderer) {
|
|
838
|
+
return this.renderer.getDrawableOrder(this.drawableID);
|
|
839
|
+
}
|
|
840
|
+
return null;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/**
|
|
844
|
+
* Move to the front layer.
|
|
845
|
+
*/
|
|
846
|
+
goToFront () { // This should only ever be used for sprites
|
|
847
|
+
if (this.renderer) {
|
|
848
|
+
// Let the renderer re-order the sprite based on its knowledge
|
|
849
|
+
// of what layers are present
|
|
850
|
+
this.renderer.setDrawableOrder(this.drawableID, Infinity, StageLayering.SPRITE_LAYER);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
this.runtime.setExecutablePosition(this, Infinity);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* Move to the back layer.
|
|
858
|
+
*/
|
|
859
|
+
goToBack () { // This should only ever be used for sprites
|
|
860
|
+
if (this.renderer) {
|
|
861
|
+
// Let the renderer re-order the sprite based on its knowledge
|
|
862
|
+
// of what layers are present
|
|
863
|
+
this.renderer.setDrawableOrder(this.drawableID, -Infinity, StageLayering.SPRITE_LAYER, false);
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
this.runtime.setExecutablePosition(this, -Infinity);
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/**
|
|
870
|
+
* Move forward a number of layers.
|
|
871
|
+
* @param {number} nLayers How many layers to go forward.
|
|
872
|
+
*/
|
|
873
|
+
goForwardLayers (nLayers) {
|
|
874
|
+
if (this.renderer) {
|
|
875
|
+
this.renderer.setDrawableOrder(this.drawableID, nLayers, StageLayering.SPRITE_LAYER, true);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
this.runtime.moveExecutable(this, nLayers);
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* Move backward a number of layers.
|
|
883
|
+
* @param {number} nLayers How many layers to go backward.
|
|
884
|
+
*/
|
|
885
|
+
goBackwardLayers (nLayers) {
|
|
886
|
+
if (this.renderer) {
|
|
887
|
+
this.renderer.setDrawableOrder(this.drawableID, -nLayers, StageLayering.SPRITE_LAYER, true);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
this.runtime.moveExecutable(this, -nLayers);
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
/**
|
|
894
|
+
* Move behind some other rendered target.
|
|
895
|
+
* @param {!RenderedTarget} other Other rendered target to move behind.
|
|
896
|
+
*/
|
|
897
|
+
goBehindOther (other) {
|
|
898
|
+
if (this.renderer) {
|
|
899
|
+
const otherLayer = this.renderer.setDrawableOrder(
|
|
900
|
+
other.drawableID, 0, StageLayering.SPRITE_LAYER, true);
|
|
901
|
+
this.renderer.setDrawableOrder(this.drawableID, otherLayer, StageLayering.SPRITE_LAYER);
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
const executionPosition = this.runtime.executableTargets.indexOf(other);
|
|
905
|
+
this.runtime.setExecutablePosition(this, executionPosition);
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* Keep a desired position within a fence.
|
|
910
|
+
* @param {number} newX New desired X position.
|
|
911
|
+
* @param {number} newY New desired Y position.
|
|
912
|
+
* @param {object=} optFence Optional fence with left, right, top bottom.
|
|
913
|
+
* @return {Array.<number>} Fenced X and Y coordinates.
|
|
914
|
+
*/
|
|
915
|
+
keepInFence (newX, newY, optFence) {
|
|
916
|
+
let fence = optFence;
|
|
917
|
+
if (!fence) {
|
|
918
|
+
fence = {
|
|
919
|
+
left: -this.runtime.constructor.STAGE_WIDTH / 2,
|
|
920
|
+
right: this.runtime.constructor.STAGE_WIDTH / 2,
|
|
921
|
+
top: this.runtime.constructor.STAGE_HEIGHT / 2,
|
|
922
|
+
bottom: -this.runtime.constructor.STAGE_HEIGHT / 2
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
const bounds = this.getBounds();
|
|
926
|
+
if (!bounds) return;
|
|
927
|
+
// Adjust the known bounds to the target position.
|
|
928
|
+
bounds.left += (newX - this.x);
|
|
929
|
+
bounds.right += (newX - this.x);
|
|
930
|
+
bounds.top += (newY - this.y);
|
|
931
|
+
bounds.bottom += (newY - this.y);
|
|
932
|
+
// Find how far we need to move the target position.
|
|
933
|
+
let dx = 0;
|
|
934
|
+
let dy = 0;
|
|
935
|
+
if (bounds.left < fence.left) {
|
|
936
|
+
dx += fence.left - bounds.left;
|
|
937
|
+
}
|
|
938
|
+
if (bounds.right > fence.right) {
|
|
939
|
+
dx += fence.right - bounds.right;
|
|
940
|
+
}
|
|
941
|
+
if (bounds.top > fence.top) {
|
|
942
|
+
dy += fence.top - bounds.top;
|
|
943
|
+
}
|
|
944
|
+
if (bounds.bottom < fence.bottom) {
|
|
945
|
+
dy += fence.bottom - bounds.bottom;
|
|
946
|
+
}
|
|
947
|
+
return [newX + dx, newY + dy];
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/**
|
|
951
|
+
* Make a clone, copying any run-time properties.
|
|
952
|
+
* If we've hit the global clone limit, returns null.
|
|
953
|
+
* @return {RenderedTarget} New clone.
|
|
954
|
+
*/
|
|
955
|
+
makeClone () {
|
|
956
|
+
if (!this.runtime.clonesAvailable() || this.isStage) {
|
|
957
|
+
return null; // Hit max clone limit, or this is the stage.
|
|
958
|
+
}
|
|
959
|
+
this.runtime.changeCloneCounter(1);
|
|
960
|
+
const newClone = this.sprite.createClone();
|
|
961
|
+
// Copy all properties.
|
|
962
|
+
newClone.x = this.x;
|
|
963
|
+
newClone.y = this.y;
|
|
964
|
+
newClone.direction = this.direction;
|
|
965
|
+
newClone.draggable = this.draggable;
|
|
966
|
+
newClone.visible = this.visible;
|
|
967
|
+
newClone.size = this.size;
|
|
968
|
+
newClone.currentCostume = this.currentCostume;
|
|
969
|
+
newClone.rotationStyle = this.rotationStyle;
|
|
970
|
+
newClone.effects = Clone.simple(this.effects);
|
|
971
|
+
newClone.variables = this.duplicateVariables();
|
|
972
|
+
newClone._edgeActivatedHatValues = Clone.simple(this._edgeActivatedHatValues);
|
|
973
|
+
newClone.initDrawable(StageLayering.SPRITE_LAYER);
|
|
974
|
+
newClone.updateAllDrawableProperties();
|
|
975
|
+
return newClone;
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* Make a duplicate using a duplicate sprite.
|
|
980
|
+
* @return {RenderedTarget} New clone.
|
|
981
|
+
*/
|
|
982
|
+
duplicate () {
|
|
983
|
+
return this.sprite.duplicate().then(newSprite => {
|
|
984
|
+
const newTarget = newSprite.createClone();
|
|
985
|
+
// Copy all properties.
|
|
986
|
+
// @todo refactor with clone methods
|
|
987
|
+
newTarget.x = (Math.random() - 0.5) * 400 / 2;
|
|
988
|
+
newTarget.y = (Math.random() - 0.5) * 300 / 2;
|
|
989
|
+
newTarget.direction = this.direction;
|
|
990
|
+
newTarget.draggable = this.draggable;
|
|
991
|
+
newTarget.visible = this.visible;
|
|
992
|
+
newTarget.size = this.size;
|
|
993
|
+
newTarget.currentCostume = this.currentCostume;
|
|
994
|
+
newTarget.rotationStyle = this.rotationStyle;
|
|
995
|
+
newTarget.effects = JSON.parse(JSON.stringify(this.effects));
|
|
996
|
+
newTarget.variables = this.duplicateVariables(newTarget.blocks);
|
|
997
|
+
newTarget.updateAllDrawableProperties();
|
|
998
|
+
return newTarget;
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* Called when the project receives a "green flag."
|
|
1004
|
+
* For a rendered target, this clears graphic effects.
|
|
1005
|
+
*/
|
|
1006
|
+
onGreenFlag () {
|
|
1007
|
+
this.clearEffects();
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* Called when the project receives a "stop all"
|
|
1012
|
+
* Stop all sounds and clear graphic effects.
|
|
1013
|
+
*/
|
|
1014
|
+
onStopAll () {
|
|
1015
|
+
this.clearEffects();
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
/**
|
|
1019
|
+
* Post/edit sprite info.
|
|
1020
|
+
* @param {object} data An object with sprite info data to set.
|
|
1021
|
+
*/
|
|
1022
|
+
postSpriteInfo (data) {
|
|
1023
|
+
const force = Object.prototype.hasOwnProperty.call(data, 'force') ? data.force : null;
|
|
1024
|
+
const isXChanged = Object.prototype.hasOwnProperty.call(data, 'x');
|
|
1025
|
+
const isYChanged = Object.prototype.hasOwnProperty.call(data, 'y');
|
|
1026
|
+
if (isXChanged || isYChanged) {
|
|
1027
|
+
this.setXY(isXChanged ? data.x : this.x, isYChanged ? data.y : this.y, force);
|
|
1028
|
+
}
|
|
1029
|
+
if (Object.prototype.hasOwnProperty.call(data, 'direction')) {
|
|
1030
|
+
this.setDirection(data.direction);
|
|
1031
|
+
}
|
|
1032
|
+
if (Object.prototype.hasOwnProperty.call(data, 'draggable')) {
|
|
1033
|
+
this.setDraggable(data.draggable);
|
|
1034
|
+
}
|
|
1035
|
+
if (Object.prototype.hasOwnProperty.call(data, 'rotationStyle')) {
|
|
1036
|
+
this.setRotationStyle(data.rotationStyle);
|
|
1037
|
+
}
|
|
1038
|
+
if (Object.prototype.hasOwnProperty.call(data, 'visible')) {
|
|
1039
|
+
this.setVisible(data.visible);
|
|
1040
|
+
}
|
|
1041
|
+
if (Object.prototype.hasOwnProperty.call(data, 'size')) {
|
|
1042
|
+
this.setSize(data.size);
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* Put the sprite into the drag state. While in effect, setXY must be forced
|
|
1048
|
+
*/
|
|
1049
|
+
startDrag () {
|
|
1050
|
+
this.dragging = true;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
/**
|
|
1054
|
+
* Remove the sprite from the drag state.
|
|
1055
|
+
*/
|
|
1056
|
+
stopDrag () {
|
|
1057
|
+
this.dragging = false;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* Serialize sprite info, used when emitting events about the sprite
|
|
1063
|
+
* @returns {object} Sprite data as a simple object
|
|
1064
|
+
*/
|
|
1065
|
+
toJSON () {
|
|
1066
|
+
const costumes = this.getCostumes();
|
|
1067
|
+
return {
|
|
1068
|
+
id: this.id,
|
|
1069
|
+
name: this.getName(),
|
|
1070
|
+
isStage: this.isStage,
|
|
1071
|
+
x: this.x,
|
|
1072
|
+
y: this.y,
|
|
1073
|
+
size: this.size,
|
|
1074
|
+
direction: this.direction,
|
|
1075
|
+
draggable: this.draggable,
|
|
1076
|
+
currentCostume: this.currentCostume,
|
|
1077
|
+
costume: costumes[this.currentCostume],
|
|
1078
|
+
costumeCount: costumes.length,
|
|
1079
|
+
visible: this.visible,
|
|
1080
|
+
rotationStyle: this.rotationStyle,
|
|
1081
|
+
comments: this.comments,
|
|
1082
|
+
blocks: this.blocks._blocks,
|
|
1083
|
+
variables: this.variables,
|
|
1084
|
+
costumes: costumes,
|
|
1085
|
+
sounds: this.getSounds(),
|
|
1086
|
+
textToSpeechLanguage: this.textToSpeechLanguage,
|
|
1087
|
+
tempo: this.tempo,
|
|
1088
|
+
volume: this.volume,
|
|
1089
|
+
videoTransparency: this.videoTransparency,
|
|
1090
|
+
videoState: this.videoState
|
|
1091
|
+
|
|
1092
|
+
};
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
/**
|
|
1096
|
+
* Dispose, destroying any run-time properties.
|
|
1097
|
+
*/
|
|
1098
|
+
dispose () {
|
|
1099
|
+
this.runtime.changeCloneCounter(-1);
|
|
1100
|
+
this.runtime.stopForTarget(this);
|
|
1101
|
+
this.runtime.removeExecutable(this);
|
|
1102
|
+
this.sprite.removeClone(this);
|
|
1103
|
+
if (this.renderer && this.drawableID !== null) {
|
|
1104
|
+
this.renderer.destroyDrawable(this.drawableID, this.isStage ?
|
|
1105
|
+
StageLayering.BACKGROUND_LAYER :
|
|
1106
|
+
StageLayering.SPRITE_LAYER);
|
|
1107
|
+
if (this.visible) {
|
|
1108
|
+
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
|
1109
|
+
this.runtime.requestRedraw();
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
module.exports = RenderedTarget;
|