2016-06-29 13:48:30 -04:00
|
|
|
var util = require('util');
|
2016-10-23 18:01:11 -04:00
|
|
|
|
|
|
|
var log = require('../util/log');
|
2016-06-29 23:07:34 -04:00
|
|
|
var MathUtil = require('../util/math-util');
|
2016-06-29 13:48:30 -04:00
|
|
|
var Target = require('../engine/target');
|
|
|
|
|
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.
|
|
|
|
* @param {!Sprite} sprite Reference to the parent sprite.
|
2016-09-15 19:37:12 -04:00
|
|
|
* @param {Runtime} runtime Reference to the runtime.
|
2016-07-06 14:09:06 -04:00
|
|
|
* @constructor
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
var RenderedTarget = function (sprite, runtime) {
|
2016-08-31 11:21:32 -04:00
|
|
|
Target.call(this, sprite.blocks);
|
2016-09-15 19:37:12 -04:00
|
|
|
this.runtime = runtime;
|
2016-08-31 11:21:32 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Reference to the sprite that this is a render of.
|
2016-08-31 11:21:32 -04:00
|
|
|
* @type {!Sprite}
|
|
|
|
*/
|
|
|
|
this.sprite = sprite;
|
2016-08-08 18:29:44 -04:00
|
|
|
/**
|
|
|
|
* Reference to the global renderer for this VM, if one exists.
|
|
|
|
* @type {?RenderWebGLWorker}
|
|
|
|
*/
|
|
|
|
this.renderer = null;
|
2016-09-20 02:42:05 -04:00
|
|
|
if (this.runtime) {
|
|
|
|
this.renderer = this.runtime.renderer;
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* ID of the drawable for this rendered target,
|
|
|
|
* returned by the renderer, if rendered.
|
2016-08-08 18:29:44 -04:00
|
|
|
* @type {?Number}
|
|
|
|
*/
|
2016-06-29 20:56:55 -04:00
|
|
|
this.drawableID = null;
|
2016-10-03 10:16:43 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of current graphic effect values.
|
|
|
|
* @type {!Object.<string, number>}
|
|
|
|
*/
|
|
|
|
this.effects = {
|
2016-10-24 11:02:19 -04:00
|
|
|
color: 0,
|
|
|
|
fisheye: 0,
|
|
|
|
whirl: 0,
|
|
|
|
pixelate: 0,
|
|
|
|
mosaic: 0,
|
|
|
|
brightness: 0,
|
|
|
|
ghost: 0
|
2016-10-03 10:16:43 -04:00
|
|
|
};
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-10-26 11:19:43 -04:00
|
|
|
util.inherits(RenderedTarget, Target);
|
2016-06-29 13:48:30 -04:00
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Create a drawable with the this.renderer.
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.initDrawable = function () {
|
2016-08-08 18:29:44 -04:00
|
|
|
if (this.renderer) {
|
2016-09-15 19:02:03 -04:00
|
|
|
this.drawableID = this.renderer.createDrawable();
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-09-15 19:37:12 -04:00
|
|
|
// If we're a clone, start the hats.
|
|
|
|
if (!this.isOriginal) {
|
|
|
|
this.runtime.startHats(
|
|
|
|
'control_start_as_clone', null, this
|
|
|
|
);
|
|
|
|
}
|
2016-06-29 20:56:55 -04:00
|
|
|
};
|
|
|
|
|
2016-11-29 17:18:20 -05:00
|
|
|
RenderedTarget.prototype.export = function () {
|
|
|
|
var result = new Object();
|
|
|
|
var notSaved = ["renderer","effects","sprite","drawableID","runtime"];
|
|
|
|
for (x in this) {
|
|
|
|
if (typeof(this[x]) === "function") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (notSaved.indexOf(x) == -1) {
|
|
|
|
result[x] = this[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.sprite = this.sprite.export();
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2016-09-15 19:37:12 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Whether this represents an "original" non-clone rendered-target for a sprite,
|
|
|
|
* i.e., created by the editor and not clone blocks.
|
2016-09-15 19:37:12 -04:00
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.isOriginal = true;
|
2016-09-15 19:37:12 -04:00
|
|
|
|
2016-09-08 09:40:27 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Whether this rendered target represents the Scratch stage.
|
2016-09-08 09:40:27 -04:00
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.isStage = false;
|
2016-09-08 09:40:27 -04:00
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Scratch X coordinate. Currently should range from -240 to 240.
|
2016-08-31 11:21:32 -04:00
|
|
|
* @type {Number}
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.x = 0;
|
2016-06-29 13:48:30 -04:00
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Scratch Y coordinate. Currently should range from -180 to 180.
|
2016-08-31 11:21:32 -04:00
|
|
|
* @type {number}
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.y = 0;
|
2016-06-29 13:48:30 -04:00
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
|
|
|
* Scratch direction. Currently should range from -179 to 180.
|
2016-08-31 11:21:32 -04:00
|
|
|
* @type {number}
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.direction = 90;
|
2016-06-29 13:48:30 -04:00
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Whether the rendered target is currently visible.
|
2016-08-31 11:21:32 -04:00
|
|
|
* @type {boolean}
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.visible = true;
|
2016-07-01 12:56:59 -04:00
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Size of rendered target as a percent of costume size.
|
2016-08-31 11:21:32 -04:00
|
|
|
* @type {number}
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.size = 100;
|
2016-06-30 00:11:47 -04:00
|
|
|
|
2016-08-31 11:21:32 -04:00
|
|
|
/**
|
|
|
|
* Currently selected costume index.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.currentCostume = 0;
|
2016-08-31 11:21:32 -04:00
|
|
|
|
2016-09-28 17:09:04 -04:00
|
|
|
/**
|
|
|
|
* Rotation style for "all around"/spinning.
|
|
|
|
* @enum
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.ROTATION_STYLE_ALL_AROUND = 'all around';
|
2016-09-28 17:09:04 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotation style for "left-right"/flipping.
|
|
|
|
* @enum
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.ROTATION_STYLE_LEFT_RIGHT = 'left-right';
|
2016-09-28 17:09:04 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotation style for "no rotation."
|
|
|
|
* @enum
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.ROTATION_STYLE_NONE = 'don\'t rotate';
|
2016-09-28 17:09:04 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current rotation style.
|
|
|
|
* @type {!string}
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.rotationStyle = (
|
|
|
|
RenderedTarget.ROTATION_STYLE_ALL_AROUND
|
|
|
|
);
|
2016-06-30 00:11:47 -04:00
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -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.
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.setXY = function (x, y) {
|
2016-09-08 09:40:27 -04:00
|
|
|
if (this.isStage) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-29 20:56:55 -04:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2016-08-08 18:29:44 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
position: [this.x, this.y]
|
|
|
|
});
|
2016-10-17 23:23:16 -04:00
|
|
|
if (this.visible) {
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-10-26 13:27:12 -04:00
|
|
|
this.runtime.spriteInfoReport(this);
|
2016-06-29 20:56:55 -04:00
|
|
|
};
|
|
|
|
|
2016-09-28 17:09:04 -04:00
|
|
|
/**
|
|
|
|
* Get the rendered direction and scale, after applying rotation style.
|
|
|
|
* @return {Object<string, number>} Direction and scale to render.
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype._getRenderedDirectionAndScale = function () {
|
2016-09-28 17:09:04 -04:00
|
|
|
// Default: no changes to `this.direction` or `this.scale`.
|
|
|
|
var finalDirection = this.direction;
|
|
|
|
var finalScale = [this.size, this.size];
|
2016-10-26 11:19:43 -04:00
|
|
|
if (this.rotationStyle === RenderedTarget.ROTATION_STYLE_NONE) {
|
2016-09-28 17:09:04 -04:00
|
|
|
// Force rendered direction to be 90.
|
|
|
|
finalDirection = 90;
|
2016-10-26 11:19:43 -04:00
|
|
|
} else if (this.rotationStyle === RenderedTarget.ROTATION_STYLE_LEFT_RIGHT) {
|
2016-09-28 17:09:04 -04:00
|
|
|
// Force rendered direction to be 90, and flip drawable if needed.
|
|
|
|
finalDirection = 90;
|
|
|
|
var scaleFlip = (this.direction < 0) ? -1 : 1;
|
|
|
|
finalScale = [scaleFlip * this.size, this.size];
|
|
|
|
}
|
|
|
|
return {direction: finalDirection, scale: finalScale};
|
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Set the direction.
|
|
|
|
* @param {!number} direction New direction.
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.setDirection = function (direction) {
|
2016-09-08 09:40:27 -04:00
|
|
|
if (this.isStage) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-06 13:57:58 -04:00
|
|
|
// Keep direction between -179 and +180.
|
2016-06-29 23:07:34 -04:00
|
|
|
this.direction = MathUtil.wrapClamp(direction, -179, 180);
|
2016-08-08 18:29:44 -04:00
|
|
|
if (this.renderer) {
|
2016-09-28 17:09:04 -04:00
|
|
|
var renderedDirectionScale = this._getRenderedDirectionAndScale();
|
2016-08-08 18:29:44 -04:00
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
2016-09-28 17:09:04 -04:00
|
|
|
direction: renderedDirectionScale.direction,
|
|
|
|
scale: renderedDirectionScale.scale
|
2016-08-08 18:29:44 -04:00
|
|
|
});
|
2016-10-17 23:23:16 -04:00
|
|
|
if (this.visible) {
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-10-26 13:27:12 -04:00
|
|
|
this.runtime.spriteInfoReport(this);
|
2016-06-29 20:56:55 -04:00
|
|
|
};
|
|
|
|
|
2016-07-06 13:47:32 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Set a say bubble.
|
2016-07-06 13:47:32 -04:00
|
|
|
* @param {?string} type Type of say bubble: "say", "think", or null.
|
|
|
|
* @param {?string} message Message to put in say bubble.
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.setSay = function (type, message) {
|
2016-09-08 09:40:27 -04:00
|
|
|
if (this.isStage) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-06 13:47:32 -04:00
|
|
|
// @todo: Render to stage.
|
|
|
|
if (!type || !message) {
|
2016-10-23 18:01:11 -04:00
|
|
|
log.info('Clearing say bubble');
|
2016-07-06 13:47:32 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-10-23 18:01:11 -04:00
|
|
|
log.info('Setting say bubble:', type, message);
|
2016-07-06 13:47:32 -04:00
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Set visibility; i.e., whether it's shown or hidden.
|
|
|
|
* @param {!boolean} visible True if should be shown.
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.setVisible = function (visible) {
|
2016-09-08 09:40:27 -04:00
|
|
|
if (this.isStage) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-26 13:27:12 -04:00
|
|
|
this.visible = !!visible;
|
2016-08-08 18:29:44 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
visible: this.visible
|
|
|
|
});
|
2016-10-17 23:23:16 -04:00
|
|
|
if (this.visible) {
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-10-26 13:27:12 -04:00
|
|
|
this.runtime.spriteInfoReport(this);
|
2016-07-01 12:56:59 -04:00
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Set size, as a percentage of the costume size.
|
|
|
|
* @param {!number} size Size of rendered target, as % of costume size.
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.setSize = function (size) {
|
2016-09-08 09:40:27 -04:00
|
|
|
if (this.isStage) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-08 18:29:44 -04:00
|
|
|
if (this.renderer) {
|
2016-11-14 12:20:29 -05:00
|
|
|
// Clamp to scales relative to costume and stage size.
|
|
|
|
// See original ScratchSprite.as:setSize.
|
|
|
|
var costumeSize = this.renderer.getSkinSize(this.drawableID);
|
|
|
|
var origW = Math.round(costumeSize[0]);
|
|
|
|
var origH = Math.round(costumeSize[1]);
|
|
|
|
var minScale = Math.min(1, Math.max(5 / origW, 5 / origH));
|
|
|
|
var maxScale = Math.min(
|
|
|
|
(1.5 * this.runtime.constructor.STAGE_WIDTH) / origW,
|
|
|
|
(1.5 * this.runtime.constructor.STAGE_HEIGHT) / origH
|
|
|
|
);
|
|
|
|
this.size = Math.round(MathUtil.clamp(size / 100, minScale, maxScale) * 100);
|
2016-09-28 17:09:04 -04:00
|
|
|
var renderedDirectionScale = this._getRenderedDirectionAndScale();
|
2016-08-08 18:29:44 -04:00
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
2016-09-28 17:09:04 -04:00
|
|
|
direction: renderedDirectionScale.direction,
|
|
|
|
scale: renderedDirectionScale.scale
|
2016-08-08 18:29:44 -04:00
|
|
|
});
|
2016-10-17 23:23:16 -04:00
|
|
|
if (this.visible) {
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-06-30 00:11:47 -04:00
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Set a particular graphic effect value.
|
|
|
|
* @param {!string} effectName Name of effect (see `RenderedTarget.prototype.effects`).
|
2016-07-06 13:57:58 -04:00
|
|
|
* @param {!number} value Numerical magnitude of effect.
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.setEffect = function (effectName, value) {
|
2016-09-12 13:14:16 -04:00
|
|
|
if (!this.effects.hasOwnProperty(effectName)) return;
|
2016-06-30 00:11:47 -04:00
|
|
|
this.effects[effectName] = value;
|
2016-08-08 18:29:44 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
var props = {};
|
|
|
|
props[effectName] = this.effects[effectName];
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, props);
|
2016-10-17 23:23:16 -04:00
|
|
|
if (this.visible) {
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-06-30 00:11:47 -04:00
|
|
|
};
|
|
|
|
|
2016-07-06 13:57:58 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Clear all graphic effects on this rendered target.
|
2016-07-06 13:57:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.clearEffects = function () {
|
2016-06-30 00:11:47 -04:00
|
|
|
for (var effectName in this.effects) {
|
|
|
|
this.effects[effectName] = 0;
|
|
|
|
}
|
2016-08-08 18:29:44 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, this.effects);
|
2016-10-17 23:23:16 -04:00
|
|
|
if (this.visible) {
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-08-08 18:29:44 -04:00
|
|
|
}
|
2016-06-30 00:11:47 -04:00
|
|
|
};
|
|
|
|
|
2016-08-31 11:21:32 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Set the current costume.
|
2016-08-31 11:21:32 -04:00
|
|
|
* @param {number} index New index of costume.
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.setCostume = function (index) {
|
2016-09-08 09:40:27 -04:00
|
|
|
// Keep the costume index within possible values.
|
2016-09-28 16:43:12 -04:00
|
|
|
index = Math.round(index);
|
2016-09-08 09:40:27 -04:00
|
|
|
this.currentCostume = MathUtil.wrapClamp(
|
|
|
|
index, 0, this.sprite.costumes.length - 1
|
|
|
|
);
|
2016-08-31 11:21:32 -04:00
|
|
|
if (this.renderer) {
|
2016-10-19 17:01:48 -04:00
|
|
|
var costume = this.sprite.costumes[this.currentCostume];
|
2016-08-31 11:21:32 -04:00
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
2016-10-19 17:01:48 -04:00
|
|
|
skin: costume.skin,
|
|
|
|
costumeResolution: costume.bitmapResolution,
|
2016-10-26 10:38:24 -04:00
|
|
|
rotationCenter: [
|
|
|
|
costume.rotationCenterX / costume.bitmapResolution,
|
|
|
|
costume.rotationCenterY / costume.bitmapResolution
|
|
|
|
]
|
2016-08-31 11:21:32 -04:00
|
|
|
});
|
2016-10-17 23:23:16 -04:00
|
|
|
if (this.visible) {
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-08-31 11:21:32 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-28 17:09:04 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Update the rotation style.
|
2016-09-28 17:09:04 -04:00
|
|
|
* @param {!string} rotationStyle New rotation style.
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.setRotationStyle = function (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;
|
2016-09-28 17:09:04 -04:00
|
|
|
}
|
|
|
|
if (this.renderer) {
|
|
|
|
var renderedDirectionScale = this._getRenderedDirectionAndScale();
|
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
direction: renderedDirectionScale.direction,
|
|
|
|
scale: renderedDirectionScale.scale
|
|
|
|
});
|
2016-10-17 23:23:16 -04:00
|
|
|
if (this.visible) {
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-09-28 17:09:04 -04:00
|
|
|
}
|
2016-10-26 13:27:12 -04:00
|
|
|
this.runtime.spriteInfoReport(this);
|
2016-09-28 17:09:04 -04:00
|
|
|
};
|
|
|
|
|
2016-09-08 09:40:27 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Get a costume index of this rendered target, by name of the costume.
|
2016-09-08 09:40:27 -04:00
|
|
|
* @param {?string} costumeName Name of a costume.
|
|
|
|
* @return {number} Index of the named costume, or -1 if not present.
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.getCostumeIndexByName = function (costumeName) {
|
2016-09-08 09:40:27 -04:00
|
|
|
for (var i = 0; i < this.sprite.costumes.length; i++) {
|
2016-10-23 17:55:31 -04:00
|
|
|
if (this.sprite.costumes[i].name === costumeName) {
|
2016-09-08 09:40:27 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2016-08-31 11:21:32 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Update all drawable properties for this rendered target.
|
2016-08-31 11:21:32 -04:00
|
|
|
* Use when a batch has changed, e.g., when the drawable is first created.
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.updateAllDrawableProperties = function () {
|
2016-08-31 11:21:32 -04:00
|
|
|
if (this.renderer) {
|
2016-09-28 17:09:04 -04:00
|
|
|
var renderedDirectionScale = this._getRenderedDirectionAndScale();
|
2016-10-19 17:01:48 -04:00
|
|
|
var costume = this.sprite.costumes[this.currentCostume];
|
2016-08-31 11:21:32 -04:00
|
|
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
|
|
|
position: [this.x, this.y],
|
2016-09-28 17:09:04 -04:00
|
|
|
direction: renderedDirectionScale.direction,
|
|
|
|
scale: renderedDirectionScale.scale,
|
2016-08-31 11:21:32 -04:00
|
|
|
visible: this.visible,
|
2016-10-19 17:01:48 -04:00
|
|
|
skin: costume.skin,
|
|
|
|
costumeResolution: costume.bitmapResolution,
|
2016-10-26 10:38:24 -04:00
|
|
|
rotationCenter: [
|
|
|
|
costume.rotationCenterX / costume.bitmapResolution,
|
|
|
|
costume.rotationCenterY / costume.bitmapResolution
|
|
|
|
]
|
2016-08-31 11:21:32 -04:00
|
|
|
});
|
2016-10-17 23:23:16 -04:00
|
|
|
if (this.visible) {
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-08-31 11:21:32 -04:00
|
|
|
}
|
2016-10-26 13:27:12 -04:00
|
|
|
this.runtime.spriteInfoReport(this);
|
2016-08-31 11:21:32 -04:00
|
|
|
};
|
|
|
|
|
2016-08-31 11:50:10 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Return the human-readable name for this rendered target, e.g., the sprite's name.
|
2016-08-31 11:50:10 -04:00
|
|
|
* @override
|
2016-10-26 11:19:43 -04:00
|
|
|
* @returns {string} Human-readable name.
|
2016-08-31 11:50:10 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.getName = function () {
|
2016-08-31 11:50:10 -04:00
|
|
|
return this.sprite.name;
|
|
|
|
};
|
|
|
|
|
2016-10-26 13:27:24 -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.
|
|
|
|
*/
|
|
|
|
RenderedTarget.prototype.isSprite = function () {
|
|
|
|
return !this.isStage && this.isOriginal;
|
|
|
|
};
|
|
|
|
|
2016-10-17 23:17:55 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Return the rendered target's tight bounding box.
|
2016-10-17 23:17:55 -04:00
|
|
|
* Includes top, left, bottom, right attributes in Scratch coordinates.
|
2016-10-26 11:19:43 -04:00
|
|
|
* @return {?Object} Tight bounding box, or null.
|
2016-10-17 23:17:55 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.getBounds = function () {
|
2016-10-17 23:17:55 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
return this.runtime.renderer.getBounds(this.drawableID);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Return whether touching a point.
|
2016-10-17 23:17:55 -04:00
|
|
|
* @param {number} x X coordinate of test point.
|
|
|
|
* @param {number} y Y coordinate of test point.
|
2016-10-26 11:19:43 -04:00
|
|
|
* @return {Boolean} True iff the rendered target is touching the point.
|
2016-10-17 23:17:55 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.isTouchingPoint = function (x, y) {
|
2016-10-17 23:17:55 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
// @todo: Update once pick is in Scratch coordinates.
|
|
|
|
// Limits test to this Drawable, so this will return true
|
|
|
|
// even if the clone is obscured by another Drawable.
|
|
|
|
var pickResult = this.runtime.renderer.pick(
|
2016-10-23 17:55:31 -04:00
|
|
|
x + (this.runtime.constructor.STAGE_WIDTH / 2),
|
|
|
|
-y + (this.runtime.constructor.STAGE_HEIGHT / 2),
|
2016-10-17 23:17:55 -04:00
|
|
|
null, null,
|
|
|
|
[this.drawableID]
|
|
|
|
);
|
|
|
|
return pickResult === this.drawableID;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Return whether touching a stage edge.
|
|
|
|
* @return {Boolean} True iff the rendered target is touching the stage edge.
|
2016-10-17 23:17:55 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.isTouchingEdge = function () {
|
2016-10-17 23:17:55 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
var stageWidth = this.runtime.constructor.STAGE_WIDTH;
|
|
|
|
var stageHeight = this.runtime.constructor.STAGE_HEIGHT;
|
|
|
|
var 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-10-26 11:19:43 -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.
|
2016-10-17 23:17:55 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.isTouchingSprite = function (spriteName) {
|
2016-10-17 23:17:55 -04:00
|
|
|
var firstClone = this.runtime.getSpriteTargetByName(spriteName);
|
|
|
|
if (!firstClone || !this.renderer) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-23 12:41:45 -04:00
|
|
|
var drawableCandidates = firstClone.sprite.clones.map(function (clone) {
|
2016-10-17 23:17:55 -04:00
|
|
|
return clone.drawableID;
|
|
|
|
});
|
|
|
|
return this.renderer.isTouchingDrawables(
|
|
|
|
this.drawableID, drawableCandidates);
|
|
|
|
};
|
|
|
|
|
2016-09-12 10:58:50 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Return whether touching a color.
|
2016-09-12 10:58:50 -04:00
|
|
|
* @param {Array.<number>} rgb [r,g,b], values between 0-255.
|
2016-10-26 11:19:43 -04:00
|
|
|
* @return {Promise.<Boolean>} True iff the rendered target is touching the color.
|
2016-09-12 10:58:50 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.isTouchingColor = function (rgb) {
|
2016-09-12 10:58:50 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
return this.renderer.isTouchingColor(this.drawableID, rgb);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Return whether rendered target's color is touching a color.
|
2016-09-12 10:58:50 -04:00
|
|
|
* @param {Object} targetRgb {Array.<number>} [r,g,b], values between 0-255.
|
|
|
|
* @param {Object} maskRgb {Array.<number>} [r,g,b], values between 0-255.
|
2016-10-26 11:19:43 -04:00
|
|
|
* @return {Promise.<Boolean>} True iff the color is touching the color.
|
2016-09-12 10:58:50 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.colorIsTouchingColor = function (targetRgb, maskRgb) {
|
2016-09-12 10:58:50 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
return this.renderer.isTouchingColor(
|
|
|
|
this.drawableID,
|
|
|
|
targetRgb,
|
|
|
|
maskRgb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-10-17 23:17:55 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Move to the front layer.
|
2016-10-17 23:17:55 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.goToFront = function () {
|
2016-10-17 23:17:55 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
this.renderer.setDrawableOrder(this.drawableID, Infinity);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Move back a number of layers.
|
2016-10-17 23:17:55 -04:00
|
|
|
* @param {number} nLayers How many layers to go back.
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.goBackLayers = function (nLayers) {
|
2016-10-17 23:17:55 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
this.renderer.setDrawableOrder(this.drawableID, -nLayers, true, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-26 10:06:58 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Move behind some other rendered target.
|
|
|
|
* @param {!Clone} other Other rendered target to move behind.
|
2016-10-26 10:06:58 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.goBehindOther = function (other) {
|
2016-10-26 10:06:58 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
var otherLayer = this.renderer.setDrawableOrder(
|
2016-10-26 11:19:43 -04:00
|
|
|
other.drawableID, 0, true);
|
2016-10-26 10:06:58 -04:00
|
|
|
this.renderer.setDrawableOrder(this.drawableID, otherLayer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-17 23:17:55 -04:00
|
|
|
/**
|
|
|
|
* Keep a desired position within a fence.
|
|
|
|
* @param {number} newX New desired X position.
|
|
|
|
* @param {number} newY New desired Y position.
|
2016-10-23 17:55:31 -04:00
|
|
|
* @param {Object=} optFence Optional fence with left, right, top bottom.
|
2016-10-17 23:17:55 -04:00
|
|
|
* @return {Array.<number>} Fenced X and Y coordinates.
|
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.keepInFence = function (newX, newY, optFence) {
|
2016-10-23 17:55:31 -04:00
|
|
|
var fence = optFence;
|
2016-10-17 23:17:55 -04:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
var 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.
|
|
|
|
var dx = 0;
|
|
|
|
var 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-09-15 19:37:12 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Make a clone, copying any run-time properties.
|
2016-09-15 19:37:12 -04:00
|
|
|
* If we've hit the global clone limit, returns null.
|
2016-10-26 11:19:43 -04:00
|
|
|
* @return {!RenderedTarget} New clone.
|
2016-09-15 19:37:12 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.makeClone = function () {
|
2016-10-26 10:06:58 -04:00
|
|
|
if (!this.runtime.clonesAvailable() || this.isStage) {
|
|
|
|
return; // Hit max clone limit, or this is the stage.
|
2016-09-15 19:37:12 -04:00
|
|
|
}
|
|
|
|
this.runtime.changeCloneCounter(1);
|
|
|
|
var newClone = this.sprite.createClone();
|
2016-10-26 10:06:58 -04:00
|
|
|
// Copy all properties.
|
2016-09-15 19:37:12 -04:00
|
|
|
newClone.x = this.x;
|
|
|
|
newClone.y = this.y;
|
|
|
|
newClone.direction = this.direction;
|
|
|
|
newClone.visible = this.visible;
|
|
|
|
newClone.size = this.size;
|
|
|
|
newClone.currentCostume = this.currentCostume;
|
2016-09-28 17:09:04 -04:00
|
|
|
newClone.rotationStyle = this.rotationStyle;
|
2016-09-15 19:37:12 -04:00
|
|
|
newClone.effects = JSON.parse(JSON.stringify(this.effects));
|
2016-09-21 16:38:33 -04:00
|
|
|
newClone.variables = JSON.parse(JSON.stringify(this.variables));
|
|
|
|
newClone.lists = JSON.parse(JSON.stringify(this.lists));
|
2016-09-15 19:37:12 -04:00
|
|
|
newClone.initDrawable();
|
|
|
|
newClone.updateAllDrawableProperties();
|
2016-10-26 11:19:43 -04:00
|
|
|
// Place behind the current target.
|
|
|
|
newClone.goBehindOther(this);
|
2016-09-15 19:37:12 -04:00
|
|
|
return newClone;
|
|
|
|
};
|
|
|
|
|
2016-09-21 16:31:07 -04:00
|
|
|
/**
|
|
|
|
* Called when the project receives a "green flag."
|
2016-10-26 11:19:43 -04:00
|
|
|
* For a rendered target, this clears graphic effects.
|
2016-09-21 16:31:07 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.onGreenFlag = function () {
|
2016-09-21 16:31:07 -04:00
|
|
|
this.clearEffects();
|
|
|
|
};
|
|
|
|
|
2016-10-26 13:27:12 -04:00
|
|
|
/**
|
|
|
|
* Post/edit sprite info.
|
|
|
|
* @param {object} data An object with sprite info data to set.
|
|
|
|
*/
|
|
|
|
RenderedTarget.prototype.postSpriteInfo = function (data) {
|
|
|
|
if (data.hasOwnProperty('x')) {
|
|
|
|
this.setXY(data.x, this.y);
|
|
|
|
}
|
|
|
|
if (data.hasOwnProperty('y')) {
|
|
|
|
this.setXY(this.x, data.y);
|
|
|
|
}
|
|
|
|
if (data.hasOwnProperty('direction')) {
|
|
|
|
this.setDirection(data.direction);
|
|
|
|
}
|
|
|
|
if (data.hasOwnProperty('rotationStyle')) {
|
|
|
|
this.setRotationStyle(data.rotationStyle);
|
|
|
|
}
|
|
|
|
if (data.hasOwnProperty('visible')) {
|
|
|
|
this.setVisible(data.visible);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-15 19:37:12 -04:00
|
|
|
/**
|
2016-10-26 11:19:43 -04:00
|
|
|
* Dispose, destroying any run-time properties.
|
2016-09-15 19:37:12 -04:00
|
|
|
*/
|
2016-10-26 11:19:43 -04:00
|
|
|
RenderedTarget.prototype.dispose = function () {
|
2016-09-15 19:37:12 -04:00
|
|
|
this.runtime.changeCloneCounter(-1);
|
|
|
|
if (this.renderer && this.drawableID !== null) {
|
|
|
|
this.renderer.destroyDrawable(this.drawableID);
|
2016-10-17 23:23:16 -04:00
|
|
|
if (this.visible) {
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2016-09-15 19:37:12 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-26 11:19:43 -04:00
|
|
|
module.exports = RenderedTarget;
|