2017-04-17 15:10:04 -04:00
|
|
|
const log = require('../util/log');
|
|
|
|
const MathUtil = require('../util/math-util');
|
2017-07-25 10:32:25 -04:00
|
|
|
const StringUtil = require('../util/string-util');
|
2017-04-17 15:10:04 -04:00
|
|
|
const Target = require('../engine/target');
|
2018-05-15 22:22:44 -04:00
|
|
|
const StageLayering = require('../engine/stage-layering');
|
2016-06-29 13:48:30 -04:00
|
|
|
|
2016-07-06 14:09:06 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Rendered target: instance of a sprite (clone), or the stage.
|
2016-07-06 14:09:06 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
class RenderedTarget extends Target {
|
2017-06-16 16:16:13 -04:00
|
|
|
/**
|
|
|
|
* @param {!Sprite} sprite Reference to the parent sprite.
|
|
|
|
* @param {Runtime} runtime Reference to the runtime.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
constructor (sprite, runtime) {
|
2017-06-16 16:16:13 -04:00
|
|
|
super(runtime, sprite.blocks);
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Reference to the sprite that this is a render of.
|
|
|
|
* @type {!Sprite}
|
|
|
|
*/
|
|
|
|
this.sprite = sprite;
|
|
|
|
/**
|
|
|
|
* Reference to the global renderer for this VM, if one exists.
|
2017-06-16 16:16:13 -04:00
|
|
|
* @type {?RenderWebGL}
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
|
|
|
this.renderer = null;
|
|
|
|
if (this.runtime) {
|
|
|
|
this.renderer = this.runtime.renderer;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* ID of the drawable for this rendered target,
|
|
|
|
* returned by the renderer, if rendered.
|
|
|
|
* @type {?Number}
|
|
|
|
*/
|
|
|
|
this.drawableID = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drag state of this rendered target. If true, x/y position can't be
|
|
|
|
* changed by blocks.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.dragging = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of current graphic effect values.
|
|
|
|
* @type {!Object.<string, number>}
|
|
|
|
*/
|
|
|
|
this.effects = {
|
|
|
|
color: 0,
|
|
|
|
fisheye: 0,
|
|
|
|
whirl: 0,
|
|
|
|
pixelate: 0,
|
|
|
|
mosaic: 0,
|
|
|
|
brightness: 0,
|
|
|
|
ghost: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this represents an "original" non-clone rendered-target for a sprite,
|
|
|
|
* i.e., created by the editor and not clone blocks.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.isOriginal = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this rendered target represents the Scratch stage.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.isStage = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scratch X coordinate. Currently should range from -240 to 240.
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
|
|
|
this.x = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scratch Y coordinate. Currently should range from -180 to 180.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.y = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scratch direction. Currently should range from -179 to 180.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.direction = 90;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the rendered target is draggable on the stage
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.draggable = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the rendered target is currently visible.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.visible = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of rendered target as a percent of costume size.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.size = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently selected costume index.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.currentCostume = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current rotation style.
|
|
|
|
* @type {!string}
|
|
|
|
*/
|
|
|
|
this.rotationStyle = RenderedTarget.ROTATION_STYLE_ALL_AROUND;
|
2018-02-27 11:48:14 -05:00
|
|
|
|
|
|
|
/**
|
2018-04-03 16:36:58 -04:00
|
|
|
* Loudness for sound playback for this target, as a percentage.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.volume = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current tempo (used by the music extension).
|
|
|
|
* This property is global to the project and stored in the stage.
|
2018-02-27 11:48:14 -05:00
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.tempo = 60;
|
2018-03-13 11:43:33 -04:00
|
|
|
|
|
|
|
/**
|
2018-04-03 16:36:58 -04:00
|
|
|
* The transparency of the video (used by extensions with camera input).
|
|
|
|
* This property is global to the project and stored in the stage.
|
2018-03-13 11:43:33 -04:00
|
|
|
* @type {number}
|
|
|
|
*/
|
2018-04-03 16:36:58 -04:00
|
|
|
this.videoTransparency = 50;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The state of the video input (used by extensions with camera input).
|
|
|
|
* This property is global to the project and stored in the stage.
|
2018-04-10 13:36:39 -04:00
|
|
|
*
|
|
|
|
* Defaults to ON. This setting does not turn the video by itself. A
|
|
|
|
* video extension once loaded will set the video device to this
|
|
|
|
* setting. Set to ON when a video extension is added in the editor the
|
|
|
|
* video will start ON. If the extension is loaded as part of loading a
|
|
|
|
* saved project the extension will see the value set when the stage
|
|
|
|
* was loaded from the saved values including the video state.
|
|
|
|
*
|
2018-04-03 16:36:58 -04:00
|
|
|
* @type {string}
|
|
|
|
*/
|
2018-04-10 13:36:39 -04:00
|
|
|
this.videoState = RenderedTarget.VIDEO_STATE.ON;
|
2018-10-17 17:34:12 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The language to use for speech synthesis, in the text2speech extension.
|
|
|
|
* It is initialized to null so that on extension load, we can check for
|
|
|
|
* this and try setting it using the editor locale.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.textToSpeechLanguage = null;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
2016-08-31 11:21:32 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Create a drawable with the this.renderer.
|
2018-05-18 10:11:59 -04:00
|
|
|
* @param {boolean} layerGroup The layer group this drawable should be added to
|
2016-08-31 11:21:32 -04:00
|
|
|
*/
|
2018-05-18 10:11:59 -04:00
|
|
|
initDrawable (layerGroup) {
|
2017-04-17 19:42:48 -04:00
|
|
|
if (this.renderer) {
|
2018-05-18 10:11:59 -04:00
|
|
|
this.drawableID = this.renderer.createDrawable(layerGroup);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
// If we're a clone, start the hats.
|
|
|
|
if (!this.isOriginal) {
|
|
|
|
this.runtime.startHats(
|
|
|
|
'control_start_as_clone', null, this
|
|
|
|
);
|
|
|
|
}
|
2018-01-09 15:57:33 -05:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
2018-06-22 09:33:08 -04:00
|
|
|
get audioPlayer () {
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.warn('get audioPlayer deprecated, please update to use .sprite.soundBank methods');
|
|
|
|
console.warn(new Error('stack for debug').stack);
|
|
|
|
/* eslint-enable no-console */
|
|
|
|
const bank = this.sprite.soundBank;
|
|
|
|
const audioPlayerProxy = {
|
|
|
|
playSound: soundId => bank.play(this, soundId)
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'audioPlayer', {
|
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
writable: false,
|
|
|
|
value: audioPlayerProxy
|
|
|
|
});
|
|
|
|
|
|
|
|
return audioPlayerProxy;
|
|
|
|
}
|
|
|
|
|
2018-01-09 15:57:33 -05:00
|
|
|
/**
|
|
|
|
* Initialize the audio player for this sprite or clone.
|
|
|
|
*/
|
|
|
|
initAudio () {
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
2016-08-08 18:29:44 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Event which fires when a target moves.
|
|
|
|
* @type {string}
|
2016-08-08 18:29:44 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get EVENT_TARGET_MOVED () {
|
|
|
|
return 'TARGET_MOVED';
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
2018-04-20 10:43:45 -04:00
|
|
|
/**
|
|
|
|
* Event which fires when a target changes visually, for updating say bubbles.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
static get EVENT_TARGET_VISUAL_CHANGE () {
|
|
|
|
return 'EVENT_TARGET_VISUAL_CHANGE';
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:29:44 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Rotation style for "all around"/spinning.
|
|
|
|
* @type {string}
|
2016-08-08 18:29:44 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get ROTATION_STYLE_ALL_AROUND () {
|
|
|
|
return 'all around';
|
|
|
|
}
|
2016-10-03 10:16:43 -04:00
|
|
|
|
2017-03-03 09:35:57 -05:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Rotation style for "left-right"/flipping.
|
|
|
|
* @type {string}
|
2017-03-03 09:35:57 -05:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get ROTATION_STYLE_LEFT_RIGHT () {
|
|
|
|
return 'left-right';
|
|
|
|
}
|
2017-03-03 09:35:57 -05:00
|
|
|
|
2016-10-03 10:16:43 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Rotation style for "no rotation."
|
|
|
|
* @type {string}
|
2016-10-03 10:16:43 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get ROTATION_STYLE_NONE () {
|
|
|
|
return "don't rotate";
|
2016-09-15 19:37:12 -04:00
|
|
|
}
|
2017-01-06 16:14:27 -05:00
|
|
|
|
2018-04-03 16:36:58 -04:00
|
|
|
/**
|
|
|
|
* Available states for video input.
|
2018-04-10 13:36:39 -04:00
|
|
|
* @enum {string}
|
2018-04-03 16:36:58 -04:00
|
|
|
*/
|
|
|
|
static get VIDEO_STATE () {
|
|
|
|
return {
|
2018-04-06 16:57:52 -04:00
|
|
|
OFF: 'off',
|
|
|
|
ON: 'on',
|
2018-04-10 15:43:29 -04:00
|
|
|
ON_FLIPPED: 'on-flipped'
|
2018-04-03 16:36:58 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-21 23:28:06 -04:00
|
|
|
/**
|
|
|
|
* Round a number to n digits
|
|
|
|
* @param {number} value The number to be rounded
|
|
|
|
* @param {number} places The number of decimal places to round to
|
|
|
|
* @return {number} The rounded number
|
|
|
|
*/
|
|
|
|
_roundCoord (value, places) {
|
|
|
|
const power = Math.pow(10, places);
|
|
|
|
return Math.round(value * power) / power;
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:14:27 -05:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Set the X and Y coordinates.
|
|
|
|
* @param {!number} x New X coordinate, in Scratch coordinates.
|
|
|
|
* @param {!number} y New Y coordinate, in Scratch coordinates.
|
|
|
|
* @param {?boolean} force Force setting X/Y, in case of dragging
|
|
|
|
*/
|
|
|
|
setXY (x, y, force) {
|
|
|
|
if (this.isStage) return;
|
|
|
|
if (this.dragging && !force) return;
|
|
|
|
const oldX = this.x;
|
|
|
|
const oldY = this.y;
|
|
|
|
if (this.renderer) {
|
|
|
|
const position = this.renderer.getFencedPositionOfDrawable(this.drawableID, [x, y]);
|
2018-08-17 20:35:42 -04:00
|
|
|
position[0] = this._roundCoord(position[0], 8);
|
|
|
|
position[1] = this._roundCoord(position[1], 8);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.x = position[0];
|
|
|
|
this.y = position[1];
|
|
|
|
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
position: position
|
|
|
|
});
|
|
|
|
if (this.visible) {
|
2018-04-20 10:43:45 -04:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-17 20:35:42 -04:00
|
|
|
this.x = this._roundCoord(x, 8);
|
|
|
|
this.y = this._roundCoord(y, 8);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2017-12-29 09:01:30 -05:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_MOVED, this, oldX, oldY, force);
|
2017-05-12 11:42:22 -04:00
|
|
|
this.runtime.requestTargetsUpdate(this);
|
2017-01-06 16:14:27 -05:00
|
|
|
}
|
2016-06-30 00:11:47 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get the rendered direction and scale, after applying rotation style.
|
|
|
|
* @return {object<string, number>} Direction and scale to render.
|
|
|
|
*/
|
|
|
|
_getRenderedDirectionAndScale () {
|
|
|
|
// Default: no changes to `this.direction` or `this.scale`.
|
|
|
|
let finalDirection = this.direction;
|
|
|
|
let finalScale = [this.size, this.size];
|
|
|
|
if (this.rotationStyle === RenderedTarget.ROTATION_STYLE_NONE) {
|
|
|
|
// Force rendered direction to be 90.
|
|
|
|
finalDirection = 90;
|
|
|
|
} else if (this.rotationStyle === RenderedTarget.ROTATION_STYLE_LEFT_RIGHT) {
|
|
|
|
// Force rendered direction to be 90, and flip drawable if needed.
|
|
|
|
finalDirection = 90;
|
|
|
|
const scaleFlip = (this.direction < 0) ? -1 : 1;
|
|
|
|
finalScale = [scaleFlip * this.size, this.size];
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
return {direction: finalDirection, scale: finalScale};
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-06-29 20:56:55 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Set the direction.
|
|
|
|
* @param {!number} direction New direction.
|
|
|
|
*/
|
|
|
|
setDirection (direction) {
|
|
|
|
if (this.isStage) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-26 17:44:02 -04:00
|
|
|
if (!isFinite(direction)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// Keep direction between -179 and +180.
|
|
|
|
this.direction = MathUtil.wrapClamp(direction, -179, 180);
|
|
|
|
if (this.renderer) {
|
|
|
|
const renderedDirectionScale = this._getRenderedDirectionAndScale();
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
direction: renderedDirectionScale.direction,
|
|
|
|
scale: renderedDirectionScale.scale
|
|
|
|
});
|
|
|
|
if (this.visible) {
|
2018-04-20 10:43:45 -04:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 11:42:22 -04:00
|
|
|
this.runtime.requestTargetsUpdate(this);
|
2016-07-06 13:47:32 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Set draggability; i.e., whether it's able to be dragged in the player
|
|
|
|
* @param {!boolean} draggable True if should be draggable.
|
|
|
|
*/
|
|
|
|
setDraggable (draggable) {
|
|
|
|
if (this.isStage) return;
|
|
|
|
this.draggable = !!draggable;
|
2017-05-12 11:42:22 -04:00
|
|
|
this.runtime.requestTargetsUpdate(this);
|
2016-09-08 09:40:27 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a say bubble.
|
|
|
|
* @param {?string} type Type of say bubble: "say", "think", or null.
|
|
|
|
* @param {?string} message Message to put in say bubble.
|
|
|
|
*/
|
|
|
|
setSay (type, message) {
|
|
|
|
if (this.isStage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// @todo: Render to stage.
|
|
|
|
if (!type || !message) {
|
|
|
|
log.info('Clearing say bubble');
|
|
|
|
return;
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
log.info('Setting say bubble:', type, message);
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-07-01 12:56:59 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Set visibility; i.e., whether it's shown or hidden.
|
|
|
|
* @param {!boolean} visible True if should be shown.
|
|
|
|
*/
|
|
|
|
setVisible (visible) {
|
|
|
|
if (this.isStage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.visible = !!visible;
|
|
|
|
if (this.renderer) {
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
visible: this.visible
|
|
|
|
});
|
|
|
|
if (this.visible) {
|
2018-04-20 10:43:45 -04:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2017-05-12 11:42:22 -04:00
|
|
|
this.runtime.requestTargetsUpdate(this);
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-06-30 00:11:47 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Set size, as a percentage of the costume size.
|
|
|
|
* @param {!number} size Size of rendered target, as % of costume size.
|
|
|
|
*/
|
|
|
|
setSize (size) {
|
|
|
|
if (this.isStage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.renderer) {
|
|
|
|
// Clamp to scales relative to costume and stage size.
|
|
|
|
// See original ScratchSprite.as:setSize.
|
2018-04-10 09:50:18 -04:00
|
|
|
const costumeSize = this.renderer.getCurrentSkinSize(this.drawableID);
|
2017-04-17 19:42:48 -04:00
|
|
|
const origW = costumeSize[0];
|
|
|
|
const origH = costumeSize[1];
|
|
|
|
const minScale = Math.min(1, Math.max(5 / origW, 5 / origH));
|
|
|
|
const maxScale = Math.min(
|
|
|
|
(1.5 * this.runtime.constructor.STAGE_WIDTH) / origW,
|
|
|
|
(1.5 * this.runtime.constructor.STAGE_HEIGHT) / origH
|
|
|
|
);
|
|
|
|
this.size = MathUtil.clamp(size / 100, minScale, maxScale) * 100;
|
|
|
|
const renderedDirectionScale = this._getRenderedDirectionAndScale();
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
direction: renderedDirectionScale.direction,
|
|
|
|
scale: renderedDirectionScale.scale
|
|
|
|
});
|
|
|
|
if (this.visible) {
|
2018-04-20 10:43:45 -04:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2017-12-20 15:25:14 -05:00
|
|
|
this.runtime.requestTargetsUpdate(this);
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-06-30 00:11:47 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Set a particular graphic effect value.
|
|
|
|
* @param {!string} effectName Name of effect (see `RenderedTarget.prototype.effects`).
|
|
|
|
* @param {!number} value Numerical magnitude of effect.
|
|
|
|
*/
|
|
|
|
setEffect (effectName, value) {
|
|
|
|
if (!this.effects.hasOwnProperty(effectName)) return;
|
|
|
|
this.effects[effectName] = value;
|
|
|
|
if (this.renderer) {
|
|
|
|
const props = {};
|
|
|
|
props[effectName] = this.effects[effectName];
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, props);
|
|
|
|
if (this.visible) {
|
2018-04-20 10:43:45 -04:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-06-30 00:11:47 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Clear all graphic effects on this rendered target.
|
|
|
|
*/
|
|
|
|
clearEffects () {
|
|
|
|
for (const effectName in this.effects) {
|
|
|
|
if (!this.effects.hasOwnProperty(effectName)) continue;
|
|
|
|
this.effects[effectName] = 0;
|
2017-01-13 12:55:21 -05:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, this.effects);
|
|
|
|
if (this.visible) {
|
2018-04-20 10:43:45 -04:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2016-08-31 11:21:32 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Set the current costume.
|
|
|
|
* @param {number} index New index of costume.
|
|
|
|
*/
|
|
|
|
setCostume (index) {
|
|
|
|
// Keep the costume index within possible values.
|
|
|
|
index = Math.round(index);
|
2018-08-22 13:39:15 -04:00
|
|
|
if ([Infinity, -Infinity, NaN].includes(index)) index = 0;
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
this.currentCostume = MathUtil.wrapClamp(
|
2018-02-22 15:42:35 -05:00
|
|
|
index, 0, this.sprite.costumes.length - 1
|
2017-04-17 19:42:48 -04:00
|
|
|
);
|
|
|
|
if (this.renderer) {
|
2018-02-22 15:42:35 -05:00
|
|
|
const costume = this.getCostumes()[this.currentCostume];
|
2017-04-17 19:42:48 -04:00
|
|
|
const drawableProperties = {
|
|
|
|
skinId: costume.skinId,
|
|
|
|
costumeResolution: costume.bitmapResolution
|
|
|
|
};
|
|
|
|
if (
|
|
|
|
typeof costume.rotationCenterX !== 'undefined' &&
|
|
|
|
typeof costume.rotationCenterY !== 'undefined'
|
|
|
|
) {
|
2018-04-22 21:22:03 -04:00
|
|
|
const scale = costume.bitmapResolution || 2;
|
2017-04-17 19:42:48 -04:00
|
|
|
drawableProperties.rotationCenter = [
|
|
|
|
costume.rotationCenterX / scale,
|
|
|
|
costume.rotationCenterY / scale
|
|
|
|
];
|
|
|
|
}
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, drawableProperties);
|
|
|
|
if (this.visible) {
|
2018-04-20 10:43:45 -04:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 11:42:22 -04:00
|
|
|
this.runtime.requestTargetsUpdate(this);
|
2016-09-28 17:09:04 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
2017-07-25 10:32:25 -04:00
|
|
|
/**
|
|
|
|
* Add a costume, taking care to avoid duplicate names.
|
|
|
|
* @param {!object} costumeObject Object representing the costume.
|
2018-02-23 16:24:18 -05:00
|
|
|
* @param {?int} index Index at which to add costume
|
2017-07-25 10:32:25 -04:00
|
|
|
*/
|
2018-02-23 16:24:18 -05:00
|
|
|
addCostume (costumeObject, index) {
|
2018-06-06 09:33:23 -04:00
|
|
|
if (typeof index === 'number' && !isNaN(index)) {
|
2018-06-05 11:46:52 -04:00
|
|
|
this.sprite.addCostumeAt(costumeObject, index);
|
2018-06-06 09:33:23 -04:00
|
|
|
} else {
|
|
|
|
this.sprite.addCostumeAt(costumeObject, this.sprite.costumes.length);
|
2018-02-23 16:24:18 -05:00
|
|
|
}
|
2017-07-25 10:32:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a costume, taking care to avoid duplicate names.
|
|
|
|
* @param {int} costumeIndex - the index of the costume to be renamed.
|
|
|
|
* @param {string} newName - the desired new name of the costume (will be modified if already in use).
|
|
|
|
*/
|
|
|
|
renameCostume (costumeIndex, newName) {
|
2018-02-22 15:42:35 -05:00
|
|
|
const usedNames = this.sprite.costumes
|
2017-07-25 10:32:25 -04:00
|
|
|
.filter((costume, index) => costumeIndex !== index)
|
|
|
|
.map(costume => costume.name);
|
2018-02-22 15:42:35 -05:00
|
|
|
const oldName = this.getCostumes()[costumeIndex].name;
|
2018-02-05 14:35:52 -05:00
|
|
|
const newUnusedName = StringUtil.unusedName(newName, usedNames);
|
2018-02-22 15:42:35 -05:00
|
|
|
this.getCostumes()[costumeIndex].name = newUnusedName;
|
2018-02-05 14:35:52 -05:00
|
|
|
|
|
|
|
if (this.isStage) {
|
2018-02-05 16:54:58 -05:00
|
|
|
// Since this is a backdrop, go through all targets and
|
|
|
|
// update any blocks referencing the old backdrop name
|
2018-02-05 14:35:52 -05:00
|
|
|
const targets = this.runtime.targets;
|
|
|
|
for (let i = 0; i < targets.length; i++) {
|
|
|
|
const currTarget = targets[i];
|
|
|
|
currTarget.blocks.updateAssetName(oldName, newUnusedName, 'backdrop');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.blocks.updateAssetName(oldName, newUnusedName, 'costume');
|
|
|
|
}
|
|
|
|
|
2017-07-25 10:32:25 -04:00
|
|
|
}
|
|
|
|
|
2017-05-16 11:01:50 -04:00
|
|
|
/**
|
|
|
|
* Delete a costume by index.
|
|
|
|
* @param {number} index Costume index to be deleted
|
2018-08-21 15:12:42 -04:00
|
|
|
* @return {?object} The costume that was deleted or null
|
|
|
|
* if the index was out of bounds of the costumes list or
|
|
|
|
* this target only has one costume.
|
2017-05-16 11:01:50 -04:00
|
|
|
*/
|
|
|
|
deleteCostume (index) {
|
2018-02-22 15:42:35 -05:00
|
|
|
const originalCostumeCount = this.sprite.costumes.length;
|
2018-08-21 15:12:42 -04:00
|
|
|
if (originalCostumeCount === 1) return null;
|
2017-05-16 11:01:50 -04:00
|
|
|
|
2018-08-21 15:12:42 -04:00
|
|
|
if (index < 0 || index >= originalCostumeCount) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const deletedCostume = this.sprite.deleteCostumeAt(index);
|
2017-05-16 11:01:50 -04:00
|
|
|
|
2017-05-18 13:56:14 -04:00
|
|
|
if (index === this.currentCostume && index === originalCostumeCount - 1) {
|
2017-05-16 11:01:50 -04:00
|
|
|
this.setCostume(index - 1);
|
2017-05-18 13:56:14 -04:00
|
|
|
} else if (index < this.currentCostume) {
|
|
|
|
this.setCostume(this.currentCostume - 1);
|
2017-05-22 08:14:13 -04:00
|
|
|
} else {
|
|
|
|
this.setCostume(this.currentCostume);
|
2017-05-16 11:01:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.runtime.requestTargetsUpdate(this);
|
2018-08-21 15:12:42 -04:00
|
|
|
return deletedCostume;
|
2017-05-16 11:01:50 -04:00
|
|
|
}
|
|
|
|
|
2017-07-25 10:32:25 -04:00
|
|
|
/**
|
|
|
|
* Add a sound, taking care to avoid duplicate names.
|
|
|
|
* @param {!object} soundObject Object representing the sound.
|
2018-02-23 16:24:18 -05:00
|
|
|
* @param {?int} index Index at which to add costume
|
2017-07-25 10:32:25 -04:00
|
|
|
*/
|
2018-02-23 16:24:18 -05:00
|
|
|
addSound (soundObject, index) {
|
2017-07-25 10:32:25 -04:00
|
|
|
const usedNames = this.sprite.sounds.map(sound => sound.name);
|
|
|
|
soundObject.name = StringUtil.unusedName(soundObject.name, usedNames);
|
2018-06-06 09:33:23 -04:00
|
|
|
if (typeof index === 'number' && !isNaN(index)) {
|
2018-06-05 11:46:52 -04:00
|
|
|
this.sprite.sounds.splice(index, 0, soundObject);
|
2018-06-06 09:33:23 -04:00
|
|
|
} else {
|
|
|
|
this.sprite.sounds.push(soundObject);
|
2018-02-23 16:24:18 -05:00
|
|
|
}
|
2017-07-25 10:32:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a sound, taking care to avoid duplicate names.
|
|
|
|
* @param {int} soundIndex - the index of the sound to be renamed.
|
|
|
|
* @param {string} newName - the desired new name of the sound (will be modified if already in use).
|
|
|
|
*/
|
|
|
|
renameSound (soundIndex, newName) {
|
|
|
|
const usedNames = this.sprite.sounds
|
|
|
|
.filter((sound, index) => soundIndex !== index)
|
|
|
|
.map(sound => sound.name);
|
2018-02-05 14:35:52 -05:00
|
|
|
const oldName = this.sprite.sounds[soundIndex].name;
|
|
|
|
const newUnusedName = StringUtil.unusedName(newName, usedNames);
|
|
|
|
this.sprite.sounds[soundIndex].name = newUnusedName;
|
|
|
|
this.blocks.updateAssetName(oldName, newUnusedName, 'sound');
|
2017-07-25 10:32:25 -04:00
|
|
|
}
|
|
|
|
|
2017-05-16 11:01:50 -04:00
|
|
|
/**
|
|
|
|
* Delete a sound by index.
|
|
|
|
* @param {number} index Sound index to be deleted
|
2018-08-17 14:01:24 -04:00
|
|
|
* @return {object} The deleted sound object, or null if no sound was deleted.
|
2017-05-16 11:01:50 -04:00
|
|
|
*/
|
|
|
|
deleteSound (index) {
|
2018-08-17 14:01:24 -04:00
|
|
|
// Make sure the sound index is not out of bounds
|
2018-08-20 12:48:57 -04:00
|
|
|
if (index < 0 || index >= this.sprite.sounds.length) {
|
2018-08-17 14:01:24 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Delete the sound at the given index
|
|
|
|
const deletedSound = this.sprite.sounds.splice(index, 1)[0];
|
2017-05-16 11:01:50 -04:00
|
|
|
this.runtime.requestTargetsUpdate(this);
|
2018-08-17 14:01:24 -04:00
|
|
|
return deletedSound;
|
2017-05-16 11:01:50 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Update the rotation style.
|
|
|
|
* @param {!string} rotationStyle New rotation style.
|
|
|
|
*/
|
|
|
|
setRotationStyle (rotationStyle) {
|
|
|
|
if (rotationStyle === RenderedTarget.ROTATION_STYLE_NONE) {
|
|
|
|
this.rotationStyle = RenderedTarget.ROTATION_STYLE_NONE;
|
|
|
|
} else if (rotationStyle === RenderedTarget.ROTATION_STYLE_ALL_AROUND) {
|
|
|
|
this.rotationStyle = RenderedTarget.ROTATION_STYLE_ALL_AROUND;
|
|
|
|
} else if (rotationStyle === RenderedTarget.ROTATION_STYLE_LEFT_RIGHT) {
|
|
|
|
this.rotationStyle = RenderedTarget.ROTATION_STYLE_LEFT_RIGHT;
|
|
|
|
}
|
|
|
|
if (this.renderer) {
|
|
|
|
const renderedDirectionScale = this._getRenderedDirectionAndScale();
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
direction: renderedDirectionScale.direction,
|
|
|
|
scale: renderedDirectionScale.scale
|
|
|
|
});
|
|
|
|
if (this.visible) {
|
2018-04-20 10:43:45 -04:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2017-05-12 11:42:22 -04:00
|
|
|
this.runtime.requestTargetsUpdate(this);
|
2016-09-28 17:09:04 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get a costume index of this rendered target, by name of the costume.
|
|
|
|
* @param {?string} costumeName Name of a costume.
|
|
|
|
* @return {number} Index of the named costume, or -1 if not present.
|
|
|
|
*/
|
|
|
|
getCostumeIndexByName (costumeName) {
|
2018-02-22 15:42:35 -05:00
|
|
|
for (let i = 0; i < this.sprite.costumes.length; i++) {
|
|
|
|
if (this.getCostumes()[i].name === costumeName) {
|
2017-04-17 19:42:48 -04:00
|
|
|
return i;
|
|
|
|
}
|
2016-09-08 09:40:27 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
return -1;
|
2016-09-08 09:40:27 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get a costume of this rendered target by id.
|
|
|
|
* @return {object} current costume
|
|
|
|
*/
|
|
|
|
getCurrentCostume () {
|
2018-02-22 15:42:35 -05:00
|
|
|
return this.getCostumes()[this.currentCostume];
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-11-30 13:16:49 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get full costume list
|
|
|
|
* @return {object[]} list of costumes
|
|
|
|
*/
|
|
|
|
getCostumes () {
|
2018-02-22 15:42:35 -05:00
|
|
|
return this.sprite.costumes;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-12-05 18:02:51 -05:00
|
|
|
|
2018-06-05 11:49:07 -04:00
|
|
|
/**
|
|
|
|
* Reorder costume list by moving costume at costumeIndex to newIndex.
|
|
|
|
* @param {!number} costumeIndex Index of the costume to move.
|
|
|
|
* @param {!number} newIndex New index for that costume.
|
2018-06-05 12:12:43 -04:00
|
|
|
* @returns {boolean} If a change occurred (i.e. if the indices do not match)
|
2018-06-05 11:49:07 -04:00
|
|
|
*/
|
|
|
|
reorderCostume (costumeIndex, newIndex) {
|
2018-06-06 09:33:59 -04:00
|
|
|
newIndex = MathUtil.clamp(newIndex, 0, this.sprite.costumes.length - 1);
|
2018-06-06 15:01:02 -04:00
|
|
|
costumeIndex = MathUtil.clamp(costumeIndex, 0, this.sprite.costumes.length - 1);
|
2018-06-05 11:49:07 -04:00
|
|
|
|
2018-06-06 09:33:59 -04:00
|
|
|
if (newIndex === costumeIndex) return false;
|
2018-06-05 12:12:43 -04:00
|
|
|
|
2018-06-05 11:49:07 -04:00
|
|
|
const currentCostume = this.getCurrentCostume();
|
2018-06-06 09:33:59 -04:00
|
|
|
const costume = this.sprite.costumes[costumeIndex];
|
2018-06-05 11:49:07 -04:00
|
|
|
|
|
|
|
// Use the sprite method for deleting costumes because setCostume is handled manually
|
2018-06-06 09:33:59 -04:00
|
|
|
this.sprite.deleteCostumeAt(costumeIndex);
|
2018-06-05 11:49:07 -04:00
|
|
|
|
2018-06-06 09:33:59 -04:00
|
|
|
this.addCostume(costume, newIndex);
|
2018-06-06 17:29:48 -04:00
|
|
|
this.currentCostume = this.getCostumeIndexByName(currentCostume.name);
|
2018-06-05 12:12:43 -04:00
|
|
|
return true;
|
2018-06-05 11:49:07 -04:00
|
|
|
}
|
|
|
|
|
2018-06-05 11:49:51 -04:00
|
|
|
/**
|
|
|
|
* Reorder sound list by moving sound at soundIndex to newIndex.
|
|
|
|
* @param {!number} soundIndex Index of the sound to move.
|
|
|
|
* @param {!number} newIndex New index for that sound.
|
2018-06-05 12:12:43 -04:00
|
|
|
* @returns {boolean} If a change occurred (i.e. if the indices do not match)
|
2018-06-05 11:49:51 -04:00
|
|
|
*/
|
|
|
|
reorderSound (soundIndex, newIndex) {
|
2018-06-06 15:01:02 -04:00
|
|
|
newIndex = MathUtil.clamp(newIndex, 0, this.sprite.sounds.length - 1);
|
|
|
|
soundIndex = MathUtil.clamp(soundIndex, 0, this.sprite.sounds.length - 1);
|
2018-06-05 11:49:51 -04:00
|
|
|
|
2018-06-06 09:33:59 -04:00
|
|
|
if (newIndex === soundIndex) return false;
|
2018-06-05 11:49:51 -04:00
|
|
|
|
2018-06-06 09:33:59 -04:00
|
|
|
const sound = this.sprite.sounds[soundIndex];
|
|
|
|
this.deleteSound(soundIndex);
|
|
|
|
this.addSound(sound, newIndex);
|
2018-06-05 12:12:43 -04:00
|
|
|
return true;
|
2018-06-05 11:49:51 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get full sound list
|
|
|
|
* @return {object[]} list of sounds
|
|
|
|
*/
|
|
|
|
getSounds () {
|
|
|
|
return this.sprite.sounds;
|
|
|
|
}
|
2017-04-06 09:37:39 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Update all drawable properties for this rendered target.
|
|
|
|
* Use when a batch has changed, e.g., when the drawable is first created.
|
|
|
|
*/
|
|
|
|
updateAllDrawableProperties () {
|
|
|
|
if (this.renderer) {
|
|
|
|
const renderedDirectionScale = this._getRenderedDirectionAndScale();
|
2018-02-22 15:42:35 -05:00
|
|
|
const costume = this.getCostumes()[this.currentCostume];
|
2018-04-22 21:22:03 -04:00
|
|
|
const bitmapResolution = costume.bitmapResolution || 2;
|
2017-04-17 19:42:48 -04:00
|
|
|
const props = {
|
|
|
|
position: [this.x, this.y],
|
|
|
|
direction: renderedDirectionScale.direction,
|
|
|
|
draggable: this.draggable,
|
|
|
|
scale: renderedDirectionScale.scale,
|
|
|
|
visible: this.visible,
|
|
|
|
skinId: costume.skinId,
|
|
|
|
costumeResolution: bitmapResolution,
|
|
|
|
rotationCenter: [
|
|
|
|
costume.rotationCenterX / bitmapResolution,
|
|
|
|
costume.rotationCenterY / bitmapResolution
|
|
|
|
]
|
|
|
|
};
|
|
|
|
for (const effectName in this.effects) {
|
|
|
|
if (!this.effects.hasOwnProperty(effectName)) continue;
|
|
|
|
props[effectName] = this.effects[effectName];
|
|
|
|
}
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, props);
|
|
|
|
if (this.visible) {
|
2018-04-20 10:43:45 -04:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2017-05-12 11:42:22 -04:00
|
|
|
this.runtime.requestTargetsUpdate(this);
|
2016-08-31 11:21:32 -04:00
|
|
|
}
|
2016-08-31 11:50:10 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return the human-readable name for this rendered target, e.g., the sprite's name.
|
|
|
|
* @override
|
|
|
|
* @returns {string} Human-readable name.
|
|
|
|
*/
|
|
|
|
getName () {
|
|
|
|
return this.sprite.name;
|
2016-10-17 23:17:55 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return whether this rendered target is a sprite (not a clone, not the stage).
|
|
|
|
* @return {boolean} True if not a clone and not the stage.
|
|
|
|
*/
|
|
|
|
isSprite () {
|
|
|
|
return !this.isStage && this.isOriginal;
|
2016-10-17 23:17:55 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return the rendered target's tight bounding box.
|
|
|
|
* Includes top, left, bottom, right attributes in Scratch coordinates.
|
|
|
|
* @return {?object} Tight bounding box, or null.
|
|
|
|
*/
|
|
|
|
getBounds () {
|
|
|
|
if (this.renderer) {
|
|
|
|
return this.runtime.renderer.getBounds(this.drawableID);
|
2016-10-17 23:17:55 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
return null;
|
2016-10-17 23:17:55 -04:00
|
|
|
}
|
|
|
|
|
2018-03-14 11:33:36 -04:00
|
|
|
/**
|
|
|
|
* Return the bounding box around a slice of the top 8px of the rendered target.
|
|
|
|
* Includes top, left, bottom, right attributes in Scratch coordinates.
|
|
|
|
* @return {?object} Tight bounding box, or null.
|
|
|
|
*/
|
|
|
|
getBoundsForBubble () {
|
|
|
|
if (this.renderer) {
|
|
|
|
return this.runtime.renderer.getBoundsForBubble(this.drawableID);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-06-06 16:34:58 -04:00
|
|
|
/**
|
2018-06-07 14:12:23 -04:00
|
|
|
* Return whether this target is touching the mouse, an edge, or a sprite.
|
2018-06-06 16:34:58 -04:00
|
|
|
* @param {string} requestedObject an id for mouse or edge, or a sprite name.
|
|
|
|
* @return {boolean} True if the sprite is touching the object.
|
|
|
|
*/
|
2018-06-07 14:12:23 -04:00
|
|
|
isTouchingObject (requestedObject) {
|
2018-06-06 16:34:58 -04:00
|
|
|
if (requestedObject === '_mouse_') {
|
|
|
|
if (!this.runtime.ioDevices.mouse) return false;
|
|
|
|
const mouseX = this.runtime.ioDevices.mouse.getClientX();
|
|
|
|
const mouseY = this.runtime.ioDevices.mouse.getClientY();
|
|
|
|
return this.isTouchingPoint(mouseX, mouseY);
|
|
|
|
} else if (requestedObject === '_edge_') {
|
|
|
|
return this.isTouchingEdge();
|
|
|
|
}
|
|
|
|
return this.isTouchingSprite(requestedObject);
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return whether touching a point.
|
|
|
|
* @param {number} x X coordinate of test point.
|
|
|
|
* @param {number} y Y coordinate of test point.
|
|
|
|
* @return {boolean} True iff the rendered target is touching the point.
|
|
|
|
*/
|
|
|
|
isTouchingPoint (x, y) {
|
|
|
|
if (this.renderer) {
|
2018-08-09 10:10:23 -04:00
|
|
|
return this.renderer.drawableTouching(this.drawableID, x, y);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-10-17 23:17:55 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return whether touching a stage edge.
|
|
|
|
* @return {boolean} True iff the rendered target is touching the stage edge.
|
|
|
|
*/
|
|
|
|
isTouchingEdge () {
|
|
|
|
if (this.renderer) {
|
|
|
|
const stageWidth = this.runtime.constructor.STAGE_WIDTH;
|
|
|
|
const stageHeight = this.runtime.constructor.STAGE_HEIGHT;
|
|
|
|
const bounds = this.getBounds();
|
|
|
|
if (bounds.left < -stageWidth / 2 ||
|
|
|
|
bounds.right > stageWidth / 2 ||
|
|
|
|
bounds.top > stageHeight / 2 ||
|
|
|
|
bounds.bottom < -stageHeight / 2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2016-09-12 10:58:50 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return whether touching any of a named sprite's clones.
|
|
|
|
* @param {string} spriteName Name of the sprite.
|
|
|
|
* @return {boolean} True iff touching a clone of the sprite.
|
|
|
|
*/
|
|
|
|
isTouchingSprite (spriteName) {
|
|
|
|
const firstClone = this.runtime.getSpriteTargetByName(spriteName);
|
|
|
|
if (!firstClone || !this.renderer) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-10 14:20:51 -04:00
|
|
|
// Filter out dragging targets. This means a sprite that is being dragged
|
|
|
|
// can detect other sprites using touching <sprite>, but cannot be detected
|
|
|
|
// by other sprites while it is being dragged. This matches Scratch 2.0 behavior.
|
|
|
|
const drawableCandidates = firstClone.sprite.clones.filter(clone => !clone.dragging)
|
|
|
|
.map(clone => clone.drawableID);
|
2017-04-17 19:42:48 -04:00
|
|
|
return this.renderer.isTouchingDrawables(
|
|
|
|
this.drawableID, drawableCandidates);
|
2016-09-12 10:58:50 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return whether touching a color.
|
|
|
|
* @param {Array.<number>} rgb [r,g,b], values between 0-255.
|
|
|
|
* @return {Promise.<boolean>} True iff the rendered target is touching the color.
|
|
|
|
*/
|
|
|
|
isTouchingColor (rgb) {
|
|
|
|
if (this.renderer) {
|
|
|
|
return this.renderer.isTouchingColor(this.drawableID, rgb);
|
|
|
|
}
|
|
|
|
return false;
|
2016-10-17 23:17:55 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return whether rendered target's color is touching a color.
|
|
|
|
* @param {object} targetRgb {Array.<number>} [r,g,b], values between 0-255.
|
|
|
|
* @param {object} maskRgb {Array.<number>} [r,g,b], values between 0-255.
|
|
|
|
* @return {Promise.<boolean>} True iff the color is touching the color.
|
|
|
|
*/
|
|
|
|
colorIsTouchingColor (targetRgb, maskRgb) {
|
|
|
|
if (this.renderer) {
|
|
|
|
return this.renderer.isTouchingColor(
|
|
|
|
this.drawableID,
|
|
|
|
targetRgb,
|
|
|
|
maskRgb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
2016-10-17 23:17:55 -04:00
|
|
|
}
|
|
|
|
|
2018-07-24 11:00:48 -04:00
|
|
|
getLayerOrder () {
|
|
|
|
if (this.renderer) {
|
|
|
|
return this.renderer.getDrawableOrder(this.drawableID);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Move to the front layer.
|
|
|
|
*/
|
2018-05-15 22:22:44 -04:00
|
|
|
goToFront () { // This should only ever be used for sprites
|
2017-04-17 19:42:48 -04:00
|
|
|
if (this.renderer) {
|
2018-05-15 22:22:44 -04:00
|
|
|
// Let the renderer re-order the sprite based on its knowledge
|
|
|
|
// of what layers are present
|
|
|
|
this.renderer.setDrawableOrder(this.drawableID, Infinity, StageLayering.SPRITE_LAYER);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2018-09-27 12:26:38 -04:00
|
|
|
|
|
|
|
this.runtime.setExecutablePosition(this, Infinity);
|
2016-10-26 10:06:58 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
2017-12-28 13:14:00 -05:00
|
|
|
* Move to the back layer.
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
2018-05-15 22:22:44 -04:00
|
|
|
goToBack () { // This should only ever be used for sprites
|
2017-12-28 13:14:00 -05:00
|
|
|
if (this.renderer) {
|
2018-05-15 22:22:44 -04:00
|
|
|
// Let the renderer re-order the sprite based on its knowledge
|
|
|
|
// of what layers are present
|
|
|
|
this.renderer.setDrawableOrder(this.drawableID, -Infinity, StageLayering.SPRITE_LAYER, false);
|
2017-12-28 13:14:00 -05:00
|
|
|
}
|
2018-09-27 12:26:38 -04:00
|
|
|
|
|
|
|
this.runtime.setExecutablePosition(this, -Infinity);
|
2017-12-28 13:14:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move forward a number of layers.
|
|
|
|
* @param {number} nLayers How many layers to go forward.
|
|
|
|
*/
|
|
|
|
goForwardLayers (nLayers) {
|
|
|
|
if (this.renderer) {
|
2018-05-15 22:22:44 -04:00
|
|
|
this.renderer.setDrawableOrder(this.drawableID, nLayers, StageLayering.SPRITE_LAYER, true);
|
2017-12-28 13:14:00 -05:00
|
|
|
}
|
2018-09-27 12:26:38 -04:00
|
|
|
|
|
|
|
this.runtime.moveExecutable(this, nLayers);
|
2017-12-28 13:14:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move backward a number of layers.
|
|
|
|
* @param {number} nLayers How many layers to go backward.
|
|
|
|
*/
|
|
|
|
goBackwardLayers (nLayers) {
|
2017-04-17 19:42:48 -04:00
|
|
|
if (this.renderer) {
|
2018-05-15 22:22:44 -04:00
|
|
|
this.renderer.setDrawableOrder(this.drawableID, -nLayers, StageLayering.SPRITE_LAYER, true);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2018-09-27 12:26:38 -04:00
|
|
|
|
|
|
|
this.runtime.moveExecutable(this, -nLayers);
|
2016-10-17 23:17:55 -04:00
|
|
|
}
|
2016-09-15 19:37:12 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Move behind some other rendered target.
|
|
|
|
* @param {!RenderedTarget} other Other rendered target to move behind.
|
|
|
|
*/
|
|
|
|
goBehindOther (other) {
|
|
|
|
if (this.renderer) {
|
|
|
|
const otherLayer = this.renderer.setDrawableOrder(
|
2018-05-20 22:30:20 -04:00
|
|
|
other.drawableID, 0, StageLayering.SPRITE_LAYER, true);
|
|
|
|
this.renderer.setDrawableOrder(this.drawableID, otherLayer, StageLayering.SPRITE_LAYER);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2018-09-26 17:03:33 -04:00
|
|
|
|
|
|
|
const executionPosition = this.runtime.executableTargets.indexOf(other);
|
|
|
|
this.runtime.setExecutablePosition(this, executionPosition);
|
2016-12-21 15:18:38 -05:00
|
|
|
}
|
2016-09-21 16:31:07 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Keep a desired position within a fence.
|
|
|
|
* @param {number} newX New desired X position.
|
|
|
|
* @param {number} newY New desired Y position.
|
|
|
|
* @param {object=} optFence Optional fence with left, right, top bottom.
|
|
|
|
* @return {Array.<number>} Fenced X and Y coordinates.
|
|
|
|
*/
|
|
|
|
keepInFence (newX, newY, optFence) {
|
|
|
|
let fence = optFence;
|
|
|
|
if (!fence) {
|
|
|
|
fence = {
|
|
|
|
left: -this.runtime.constructor.STAGE_WIDTH / 2,
|
|
|
|
right: this.runtime.constructor.STAGE_WIDTH / 2,
|
|
|
|
top: this.runtime.constructor.STAGE_HEIGHT / 2,
|
|
|
|
bottom: -this.runtime.constructor.STAGE_HEIGHT / 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const bounds = this.getBounds();
|
|
|
|
if (!bounds) return;
|
|
|
|
// Adjust the known bounds to the target position.
|
|
|
|
bounds.left += (newX - this.x);
|
|
|
|
bounds.right += (newX - this.x);
|
|
|
|
bounds.top += (newY - this.y);
|
|
|
|
bounds.bottom += (newY - this.y);
|
|
|
|
// Find how far we need to move the target position.
|
|
|
|
let dx = 0;
|
|
|
|
let dy = 0;
|
|
|
|
if (bounds.left < fence.left) {
|
|
|
|
dx += fence.left - bounds.left;
|
|
|
|
}
|
|
|
|
if (bounds.right > fence.right) {
|
|
|
|
dx += fence.right - bounds.right;
|
|
|
|
}
|
|
|
|
if (bounds.top > fence.top) {
|
|
|
|
dy += fence.top - bounds.top;
|
|
|
|
}
|
|
|
|
if (bounds.bottom < fence.bottom) {
|
|
|
|
dy += fence.bottom - bounds.bottom;
|
|
|
|
}
|
|
|
|
return [newX + dx, newY + dy];
|
2016-10-26 13:27:12 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a clone, copying any run-time properties.
|
|
|
|
* If we've hit the global clone limit, returns null.
|
|
|
|
* @return {RenderedTarget} New clone.
|
|
|
|
*/
|
|
|
|
makeClone () {
|
|
|
|
if (!this.runtime.clonesAvailable() || this.isStage) {
|
|
|
|
return null; // Hit max clone limit, or this is the stage.
|
|
|
|
}
|
|
|
|
this.runtime.changeCloneCounter(1);
|
|
|
|
const newClone = this.sprite.createClone();
|
|
|
|
// Copy all properties.
|
|
|
|
newClone.x = this.x;
|
|
|
|
newClone.y = this.y;
|
|
|
|
newClone.direction = this.direction;
|
|
|
|
newClone.draggable = this.draggable;
|
|
|
|
newClone.visible = this.visible;
|
|
|
|
newClone.size = this.size;
|
|
|
|
newClone.currentCostume = this.currentCostume;
|
|
|
|
newClone.rotationStyle = this.rotationStyle;
|
|
|
|
newClone.effects = JSON.parse(JSON.stringify(this.effects));
|
2018-08-07 00:30:03 -04:00
|
|
|
newClone.variables = this.duplicateVariables();
|
2018-05-20 19:49:42 -04:00
|
|
|
newClone.initDrawable(StageLayering.SPRITE_LAYER);
|
2017-04-17 19:42:48 -04:00
|
|
|
newClone.updateAllDrawableProperties();
|
|
|
|
// Place behind the current target.
|
|
|
|
newClone.goBehindOther(this);
|
|
|
|
return newClone;
|
2016-10-26 13:27:12 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
2017-09-07 11:51:21 -04:00
|
|
|
/**
|
|
|
|
* Make a duplicate using a duplicate sprite.
|
|
|
|
* @return {RenderedTarget} New clone.
|
|
|
|
*/
|
|
|
|
duplicate () {
|
|
|
|
return this.sprite.duplicate().then(newSprite => {
|
|
|
|
const newTarget = newSprite.createClone();
|
|
|
|
// Copy all properties.
|
|
|
|
// @todo refactor with clone methods
|
2018-06-18 16:20:59 -04:00
|
|
|
newTarget.x = (Math.random() - 0.5) * 400 / 2;
|
|
|
|
newTarget.y = (Math.random() - 0.5) * 300 / 2;
|
2017-09-07 11:51:21 -04:00
|
|
|
newTarget.direction = this.direction;
|
|
|
|
newTarget.draggable = this.draggable;
|
|
|
|
newTarget.visible = this.visible;
|
|
|
|
newTarget.size = this.size;
|
|
|
|
newTarget.currentCostume = this.currentCostume;
|
|
|
|
newTarget.rotationStyle = this.rotationStyle;
|
|
|
|
newTarget.effects = JSON.parse(JSON.stringify(this.effects));
|
2018-08-07 00:30:03 -04:00
|
|
|
newTarget.variables = this.duplicateVariables(newTarget.blocks);
|
2017-09-07 11:51:21 -04:00
|
|
|
newTarget.updateAllDrawableProperties();
|
|
|
|
newTarget.goBehindOther(this);
|
|
|
|
return newTarget;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Called when the project receives a "green flag."
|
|
|
|
* For a rendered target, this clears graphic effects.
|
|
|
|
*/
|
|
|
|
onGreenFlag () {
|
|
|
|
this.clearEffects();
|
2017-03-01 18:49:17 -05:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the project receives a "stop all"
|
|
|
|
* Stop all sounds and clear graphic effects.
|
|
|
|
*/
|
|
|
|
onStopAll () {
|
|
|
|
this.clearEffects();
|
2016-10-26 13:27:12 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Post/edit sprite info.
|
|
|
|
* @param {object} data An object with sprite info data to set.
|
|
|
|
*/
|
|
|
|
postSpriteInfo (data) {
|
|
|
|
const force = data.hasOwnProperty('force') ? data.force : null;
|
2017-12-29 09:13:18 -05:00
|
|
|
const isXChanged = data.hasOwnProperty('x');
|
|
|
|
const isYChanged = data.hasOwnProperty('y');
|
2017-12-29 08:31:59 -05:00
|
|
|
if (isXChanged || isYChanged) {
|
|
|
|
this.setXY(isXChanged ? data.x : this.x, isYChanged ? data.y : this.y, force);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
if (data.hasOwnProperty('direction')) {
|
|
|
|
this.setDirection(data.direction);
|
|
|
|
}
|
|
|
|
if (data.hasOwnProperty('draggable')) {
|
|
|
|
this.setDraggable(data.draggable);
|
|
|
|
}
|
|
|
|
if (data.hasOwnProperty('rotationStyle')) {
|
|
|
|
this.setRotationStyle(data.rotationStyle);
|
|
|
|
}
|
|
|
|
if (data.hasOwnProperty('visible')) {
|
|
|
|
this.setVisible(data.visible);
|
|
|
|
}
|
2017-12-20 15:25:14 -05:00
|
|
|
if (data.hasOwnProperty('size')) {
|
|
|
|
this.setSize(data.size);
|
|
|
|
}
|
2016-10-26 13:27:12 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Put the sprite into the drag state. While in effect, setXY must be forced
|
|
|
|
*/
|
|
|
|
startDrag () {
|
|
|
|
this.dragging = true;
|
|
|
|
}
|
2017-03-03 09:35:57 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Remove the sprite from the drag state.
|
|
|
|
*/
|
|
|
|
stopDrag () {
|
|
|
|
this.dragging = false;
|
|
|
|
}
|
2017-03-03 09:35:57 -05:00
|
|
|
|
2017-04-27 17:49:57 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Serialize sprite info, used when emitting events about the sprite
|
2017-04-26 11:44:53 -04:00
|
|
|
* @returns {object} Sprite data as a simple object
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
|
|
|
toJSON () {
|
2017-05-04 13:03:42 -04:00
|
|
|
const costumes = this.getCostumes();
|
2017-04-17 19:42:48 -04:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
name: this.getName(),
|
|
|
|
isStage: this.isStage,
|
|
|
|
x: this.x,
|
|
|
|
y: this.y,
|
|
|
|
size: this.size,
|
|
|
|
direction: this.direction,
|
|
|
|
draggable: this.draggable,
|
2017-04-27 17:08:06 -04:00
|
|
|
currentCostume: this.currentCostume,
|
2017-04-26 17:45:36 -04:00
|
|
|
costume: costumes[this.currentCostume],
|
|
|
|
costumeCount: costumes.length,
|
2017-04-17 19:42:48 -04:00
|
|
|
visible: this.visible,
|
2017-04-26 11:44:53 -04:00
|
|
|
rotationStyle: this.rotationStyle,
|
2018-06-14 14:11:20 -04:00
|
|
|
comments: this.comments,
|
2017-04-26 11:44:53 -04:00
|
|
|
blocks: this.blocks._blocks,
|
|
|
|
variables: this.variables,
|
2017-04-26 17:45:36 -04:00
|
|
|
costumes: costumes,
|
2018-04-06 10:37:09 -04:00
|
|
|
sounds: this.getSounds(),
|
2018-10-17 17:34:12 -04:00
|
|
|
textToSpeechLanguage: this.textToSpeechLanguage,
|
2018-04-06 10:37:09 -04:00
|
|
|
tempo: this.tempo,
|
|
|
|
volume: this.volume,
|
|
|
|
videoTransparency: this.videoTransparency,
|
|
|
|
videoState: this.videoState
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
};
|
|
|
|
}
|
2016-12-05 18:02:51 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Dispose, destroying any run-time properties.
|
|
|
|
*/
|
|
|
|
dispose () {
|
|
|
|
this.runtime.changeCloneCounter(-1);
|
2017-06-07 20:05:24 -04:00
|
|
|
this.runtime.stopForTarget(this);
|
2018-09-26 17:03:33 -04:00
|
|
|
this.runtime.removeExecutable(this);
|
2017-06-07 20:05:24 -04:00
|
|
|
this.sprite.removeClone(this);
|
2017-04-17 19:42:48 -04:00
|
|
|
if (this.renderer && this.drawableID !== null) {
|
2018-05-20 19:49:42 -04:00
|
|
|
this.renderer.destroyDrawable(this.drawableID, this.isStage ?
|
|
|
|
StageLayering.BACKGROUND_LAYER :
|
|
|
|
StageLayering.SPRITE_LAYER);
|
2017-04-17 19:42:48 -04:00
|
|
|
if (this.visible) {
|
2018-04-20 10:43:45 -04:00
|
|
|
this.emit(RenderedTarget.EVENT_TARGET_VISUAL_CHANGE, this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2016-09-15 19:37:12 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-09-15 19:37:12 -04:00
|
|
|
|
2016-10-26 11:19:43 -04:00
|
|
|
module.exports = RenderedTarget;
|