|
const MathUtil = require('../util/math-util'); |
|
const StringUtil = require('../util/string-util'); |
|
const Cast = require('../util/cast'); |
|
const Clone = require('../util/clone'); |
|
const { translateForCamera } = require('../util/pos-math'); |
|
const Target = require('../engine/target'); |
|
const StageLayering = require('../engine/stage-layering'); |
|
const getCostumeUrl = require('../util/get-costume-url'); |
|
const xmlEscape = require('../util/xml-escape'); |
|
|
|
|
|
|
|
|
|
class RenderedTarget extends Target { |
|
|
|
|
|
|
|
|
|
|
|
constructor (sprite, runtime) { |
|
super(runtime, sprite.blocks); |
|
|
|
this.customId = 'pm-rendered-target'; |
|
|
|
|
|
|
|
|
|
|
|
this.sprite = sprite; |
|
|
|
|
|
|
|
|
|
this.renderer = null; |
|
if (this.runtime) { |
|
this.renderer = this.runtime.renderer; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.drawableID = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
this.dragging = false; |
|
|
|
|
|
|
|
|
|
|
|
this.effects = { |
|
color: 0, |
|
fisheye: 0, |
|
whirl: 0, |
|
pixelate: 0, |
|
mosaic: 0, |
|
brightness: 0, |
|
ghost: 0, |
|
red: 0, |
|
green: 0, |
|
blue: 0, |
|
opaque: 0, |
|
saturation: 0, |
|
|
|
|
|
tintColor: 0xffffff + 1 |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
this.isOriginal = true; |
|
|
|
|
|
|
|
|
|
|
|
this.isStage = false; |
|
|
|
|
|
|
|
|
|
|
|
this.isDisposed = false; |
|
|
|
|
|
|
|
|
|
|
|
this.x = 0; |
|
|
|
|
|
|
|
|
|
|
|
this.y = 0; |
|
|
|
|
|
|
|
|
|
|
|
this.transform = [0, 0]; |
|
|
|
|
|
|
|
|
|
|
|
this.direction = 90; |
|
|
|
|
|
|
|
|
|
|
|
this.draggable = false; |
|
|
|
|
|
|
|
|
|
|
|
this.visible = true; |
|
|
|
|
|
|
|
|
|
|
|
this.size = 100; |
|
|
|
|
|
|
|
|
|
|
|
this.stretch = [100, 100]; |
|
|
|
|
|
|
|
|
|
|
|
this.currentCostume = 0; |
|
|
|
|
|
|
|
|
|
|
|
this.rotationStyle = RenderedTarget.ROTATION_STYLE_ALL_AROUND; |
|
|
|
|
|
|
|
|
|
|
|
this.volume = 100; |
|
|
|
|
|
|
|
|
|
|
|
|
|
this.tempo = 60; |
|
|
|
|
|
|
|
|
|
|
|
|
|
this.videoTransparency = 50; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.videoState = RenderedTarget.VIDEO_STATE.ON; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.textToSpeechLanguage = null; |
|
|
|
|
|
|
|
this.onTargetMoved = null; |
|
this.onTargetVisualChange = null; |
|
|
|
this.interpolationData = null; |
|
|
|
this.cameraBound = 'default'; |
|
} |
|
cameraUpdateEvent() { |
|
const {direction, scale} = this._getRenderedDirectionAndScale(); |
|
const translatedPos = this._translatePossitionToCamera(); |
|
this.renderer.updateDrawablePosition(this.drawableID, translatedPos); |
|
this.renderer.updateDrawableDirectionScale(this.drawableID, direction, scale, this.transform); |
|
this.renderer.updateDrawableVisible(this.drawableID, this.visible); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
initDrawable (layerGroup) { |
|
if (this.renderer) { |
|
this.drawableID = this.renderer.createDrawable(layerGroup); |
|
} |
|
|
|
if (!this.isOriginal) { |
|
this.runtime.startHats( |
|
'control_start_as_clone', null, this |
|
); |
|
} |
|
} |
|
|
|
get audioPlayer () { |
|
|
|
console.warn('get audioPlayer deprecated, please update to use .sprite.soundBank methods'); |
|
console.warn(new Error('stack for debug').stack); |
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
initAudio () { |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static get ROTATION_STYLE_ALL_AROUND () { |
|
return 'all around'; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static get ROTATION_STYLE_LOOK_AT () { |
|
return 'look at'; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static get ROTATION_STYLE_LEFT_RIGHT () { |
|
return 'left-right'; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static get ROTATION_STYLE_UP_DOWN () { |
|
return 'up-down'; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static get ROTATION_STYLE_NONE () { |
|
return "don't rotate"; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static get VIDEO_STATE () { |
|
return { |
|
OFF: 'off', |
|
ON: 'on', |
|
ON_FLIPPED: 'on-flipped' |
|
}; |
|
} |
|
|
|
emitVisualChange () { |
|
if (this.onTargetVisualChange) { |
|
this.onTargetVisualChange(this); |
|
} |
|
} |
|
|
|
bindToCamera(screen) { |
|
this.cameraBound = screen; |
|
this.updateAllDrawableProperties(); |
|
} |
|
|
|
removeCameraBinding() { |
|
this.cameraBound = null; |
|
this.updateAllDrawableProperties(); |
|
} |
|
|
|
_translatePossitionToCamera() { |
|
if (!this.cameraBound) return [this.x, this.y]; |
|
return translateForCamera(this.runtime, this.cameraBound, this.x, this.y); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setXY (x, y, force, ignoreFencing) { |
|
if (this.isStage) return; |
|
if (this.dragging && !force) return; |
|
const oldX = this.x; |
|
const oldY = this.y; |
|
if (this.renderer) { |
|
const position = this.runtime.runtimeOptions.fencing && !ignoreFencing ? |
|
this.renderer.getFencedPositionOfDrawable(this.drawableID, [x, y]) : |
|
[x, y]; |
|
this.x = position[0]; |
|
this.y = position[1]; |
|
|
|
this.renderer.updateDrawablePosition(this.drawableID, this._translatePossitionToCamera()); |
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} else { |
|
this.x = x; |
|
this.y = y; |
|
} |
|
if (this.onTargetMoved) { |
|
this.onTargetMoved(this, oldX, oldY, force); |
|
} |
|
this.runtime.requestTargetsUpdate(this); |
|
} |
|
|
|
setTransform (transform) { |
|
if (!Array.isArray(transform) || transform.length !== 2) |
|
throw new TypeError('Expected an Array of length 2 for the transform input'); |
|
if (this.isStage) { |
|
return; |
|
} |
|
this.transform = [transform[0], transform[1]]; |
|
if (this.renderer) { |
|
const {direction: renderedDirection, scale} = this._getRenderedDirectionAndScale(); |
|
this.renderer.updateDrawableDirectionScale(this.drawableID, renderedDirection, scale, this.transform); |
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} |
|
this.runtime.requestTargetsUpdate(this); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
_getRenderedDirectionAndScale () { |
|
const cameraState = this.runtime.getCamera(this.cameraBound); |
|
|
|
let finalDirection = this.direction; |
|
let finalScale = [this.size, this.size]; |
|
if (this.rotationStyle === RenderedTarget.ROTATION_STYLE_NONE) { |
|
|
|
finalDirection = 90; |
|
} else if (this.rotationStyle === RenderedTarget.ROTATION_STYLE_LEFT_RIGHT) { |
|
|
|
finalDirection = 90; |
|
const scaleFlip = (this.direction < 0) ? -1 : 1; |
|
finalScale = [scaleFlip * this.size, this.size]; |
|
} else if (this.rotationStyle === RenderedTarget.ROTATION_STYLE_UP_DOWN) { |
|
|
|
finalDirection = 90; |
|
const scaleFlip = ((this.direction > 90) || (this.direction < -90)) ? -1 : 1; |
|
finalScale = [this.size, scaleFlip * this.size]; |
|
} else if (this.rotationStyle === RenderedTarget.ROTATION_STYLE_LOOK_AT) { |
|
|
|
const scaleFlip = (this.direction < 0) ? -1 : 1; |
|
finalScale = [this.size, scaleFlip * this.size]; |
|
} |
|
finalScale[0] *= this.stretch[0] / 100; |
|
finalScale[1] *= this.stretch[1] / 100; |
|
|
|
if (this.cameraBound) { |
|
finalScale[0] *= cameraState.scale; |
|
finalScale[1] *= cameraState.scale; |
|
finalDirection -= cameraState.dir; |
|
} |
|
return {direction: finalDirection, scale: finalScale, stretch: this.stretch}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
setStretch (x, y) { |
|
if (this.isStage) { |
|
return; |
|
} |
|
|
|
this.stretch = [x, y]; |
|
if (this.renderer) { |
|
const {direction: renderedDirection, scale} = this._getRenderedDirectionAndScale(); |
|
this.renderer.updateDrawableDirectionScale(this.drawableID, renderedDirection, scale, this.transform); |
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} |
|
this.runtime.requestTargetsUpdate(this); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
setDirection (direction) { |
|
if (this.isStage) { |
|
return; |
|
} |
|
if (!isFinite(direction)) { |
|
return; |
|
} |
|
|
|
this.direction = MathUtil.wrapClamp(direction, -179, 180); |
|
if (this.renderer) { |
|
const {direction: renderedDirection, scale} = this._getRenderedDirectionAndScale(); |
|
this.renderer.updateDrawableDirectionScale(this.drawableID, renderedDirection, scale, this.transform); |
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} |
|
this.runtime.requestTargetsUpdate(this); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
setDraggable (draggable) { |
|
if (this.isStage) return; |
|
this.draggable = !!draggable; |
|
this.runtime.requestTargetsUpdate(this); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
setVisible (visible) { |
|
if (this.isStage) { |
|
return; |
|
} |
|
this.visible = !!visible; |
|
if (this.renderer) { |
|
this.renderer.updateDrawableVisible(this.drawableID, this.visible); |
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} |
|
this.runtime.requestTargetsUpdate(this); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
setSize (size) { |
|
if (this.isStage) { |
|
return; |
|
} |
|
if (this.renderer) { |
|
this.size = Math.max(0, size); |
|
const {direction, scale} = this._getRenderedDirectionAndScale(); |
|
this.renderer.updateDrawableDirectionScale(this.drawableID, direction, scale, this.transform); |
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} else { |
|
|
|
|
|
this.size = size; |
|
} |
|
this.runtime.requestTargetsUpdate(this); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
setEffect (effectName, value) { |
|
if (!this.effects.hasOwnProperty(effectName)) return; |
|
this.effects[effectName] = value; |
|
if (this.renderer) { |
|
this.renderer.updateDrawableEffect(this.drawableID, effectName, value); |
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
clearEffects () { |
|
for (const effectName in this.effects) { |
|
if (!this.effects.hasOwnProperty(effectName)) continue; |
|
this.effects[effectName] = 0; |
|
} |
|
if (this.renderer) { |
|
for (const effectName in this.effects) { |
|
if (!this.effects.hasOwnProperty(effectName)) continue; |
|
this.renderer.updateDrawableEffect(this.drawableID, effectName, 0); |
|
} |
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
setCostume (index) { |
|
|
|
index = Math.round(index); |
|
if (index === Infinity || index === -Infinity || !index) { |
|
index = 0; |
|
} |
|
|
|
this.currentCostume = MathUtil.wrapClamp( |
|
index, 0, this.sprite.costumes.length - 1 |
|
); |
|
if (this.renderer) { |
|
const costume = this.sprite.costumes[this.currentCostume]; |
|
this.renderer.updateDrawableSkinId(this.drawableID, costume.skinId); |
|
|
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} |
|
this.runtime.requestTargetsUpdate(this); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
addCostume (costumeObject, index) { |
|
if (typeof index === 'number' && !isNaN(index)) { |
|
this.sprite.addCostumeAt(costumeObject, index); |
|
} else { |
|
this.sprite.addCostumeAt(costumeObject, this.sprite.costumes.length); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
renameCostume (costumeIndex, newName) { |
|
const usedNames = this.sprite.costumes |
|
.filter((costume, index) => costumeIndex !== index) |
|
.map(costume => costume.name); |
|
const oldName = this.getCostumes()[costumeIndex].name; |
|
const newUnusedName = StringUtil.unusedName(newName, usedNames); |
|
this.getCostumes()[costumeIndex].name = newUnusedName; |
|
|
|
if (this.isStage) { |
|
|
|
|
|
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'); |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deleteCostume (index) { |
|
const originalCostumeCount = this.sprite.costumes.length; |
|
if (originalCostumeCount === 1) return null; |
|
|
|
if (index < 0 || index >= originalCostumeCount) { |
|
return null; |
|
} |
|
|
|
const deletedCostume = this.sprite.deleteCostumeAt(index); |
|
|
|
if (index === this.currentCostume && index === originalCostumeCount - 1) { |
|
this.setCostume(index - 1); |
|
} else if (index < this.currentCostume) { |
|
this.setCostume(this.currentCostume - 1); |
|
} else { |
|
this.setCostume(this.currentCostume); |
|
} |
|
|
|
this.runtime.requestTargetsUpdate(this); |
|
return deletedCostume; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
addSound (soundObject, index) { |
|
const usedNames = this.sprite.sounds.map(sound => sound.name); |
|
soundObject.name = StringUtil.unusedName(soundObject.name, usedNames); |
|
if (typeof index === 'number' && !isNaN(index)) { |
|
this.sprite.sounds.splice(index, 0, soundObject); |
|
} else { |
|
this.sprite.sounds.push(soundObject); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
renameSound (soundIndex, newName) { |
|
const usedNames = this.sprite.sounds |
|
.filter((sound, index) => soundIndex !== index) |
|
.map(sound => sound.name); |
|
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'); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
deleteSound (index) { |
|
|
|
if (index < 0 || index >= this.sprite.sounds.length) { |
|
return null; |
|
} |
|
|
|
const deletedSound = this.sprite.sounds.splice(index, 1)[0]; |
|
this.runtime.requestTargetsUpdate(this); |
|
return deletedSound; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
} else if (rotationStyle === RenderedTarget.ROTATION_STYLE_UP_DOWN) { |
|
this.rotationStyle = RenderedTarget.ROTATION_STYLE_UP_DOWN; |
|
} else if (rotationStyle === RenderedTarget.ROTATION_STYLE_LOOK_AT) { |
|
this.rotationStyle = RenderedTarget.ROTATION_STYLE_LOOK_AT; |
|
} |
|
if (this.renderer) { |
|
const {direction, scale} = this._getRenderedDirectionAndScale(); |
|
this.renderer.updateDrawableDirectionScale(this.drawableID, direction, scale, this.transform); |
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} |
|
this.runtime.requestTargetsUpdate(this); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getCostumeIndexByName (costumeName) { |
|
const costumes = this.getCostumes(); |
|
for (let i = 0; i < costumes.length; i++) { |
|
if (costumes[i].name === costumeName) { |
|
return i; |
|
} |
|
} |
|
return -1; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
getCurrentCostume () { |
|
return this.getCostumes()[this.currentCostume]; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
getCostumes () { |
|
return this.sprite.costumes; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reorderCostume (costumeIndex, newIndex) { |
|
newIndex = MathUtil.clamp(newIndex, 0, this.sprite.costumes.length - 1); |
|
costumeIndex = MathUtil.clamp(costumeIndex, 0, this.sprite.costumes.length - 1); |
|
|
|
if (newIndex === costumeIndex) return false; |
|
|
|
const currentCostume = this.getCurrentCostume(); |
|
const costume = this.sprite.costumes[costumeIndex]; |
|
|
|
|
|
this.sprite.deleteCostumeAt(costumeIndex); |
|
|
|
this.addCostume(costume, newIndex); |
|
this.currentCostume = this.getCostumeIndexByName(currentCostume.name); |
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reorderSound (soundIndex, newIndex) { |
|
newIndex = MathUtil.clamp(newIndex, 0, this.sprite.sounds.length - 1); |
|
soundIndex = MathUtil.clamp(soundIndex, 0, this.sprite.sounds.length - 1); |
|
|
|
if (newIndex === soundIndex) return false; |
|
|
|
const sound = this.sprite.sounds[soundIndex]; |
|
this.deleteSound(soundIndex); |
|
this.addSound(sound, newIndex); |
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
getSounds () { |
|
return this.sprite.sounds; |
|
} |
|
getSoundIndexByName (soundName) { |
|
const sounds = this.getSounds(); |
|
for (let i = 0; i < sounds.length; i++) { |
|
if (sounds[i].name === soundName) { |
|
return i; |
|
} |
|
} |
|
|
|
return -1; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
updateAllDrawableProperties () { |
|
if (this.renderer) { |
|
const {direction, scale} = this._getRenderedDirectionAndScale(); |
|
const translatedPos = this._translatePossitionToCamera(); |
|
this.renderer.updateDrawablePosition(this.drawableID, translatedPos); |
|
this.renderer.updateDrawableDirectionScale(this.drawableID, direction, scale, this.transform); |
|
this.renderer.updateDrawableVisible(this.drawableID, this.visible); |
|
|
|
const costume = this.getCostumes()[this.currentCostume]; |
|
this.renderer.updateDrawableSkinId(this.drawableID, costume.skinId); |
|
|
|
for (const effectName in this.effects) { |
|
if (!this.effects.hasOwnProperty(effectName)) continue; |
|
this.renderer.updateDrawableEffect(this.drawableID, effectName, this.effects[effectName]); |
|
} |
|
|
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} |
|
this.runtime.requestTargetsUpdate(this); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getName () { |
|
return this.sprite.name; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
isSprite () { |
|
return !this.isStage && this.isOriginal; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getBounds () { |
|
if (this.renderer) { |
|
return this.runtime.renderer.getBounds(this.drawableID); |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getBoundsForBubble () { |
|
if (this.renderer) { |
|
return this.runtime.renderer.getBoundsForBubble(this.drawableID); |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isTouchingObject (requestedObject, unoriginalOnly) { |
|
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(); |
|
} |
|
|
|
if (unoriginalOnly) { |
|
return this.isTouchingSpriteUnoriginals(requestedObject); |
|
} else { |
|
return this.isTouchingSprite(requestedObject); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isTouchingPoint (x, y) { |
|
if (this.renderer) { |
|
return this.renderer.drawableTouching(this.drawableID, x, y); |
|
} |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
isTouchingEdge () { |
|
if (this.renderer) { |
|
const stageWidth = this.runtime.stageWidth; |
|
const stageHeight = this.runtime.stageHeight; |
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
isTouchingSprite (spriteName) { |
|
spriteName = Cast.toString(spriteName); |
|
const firstClone = this.runtime.getSpriteTargetByName(spriteName); |
|
if (!firstClone || !this.renderer) { |
|
return false; |
|
} |
|
|
|
|
|
|
|
const drawableCandidates = firstClone.sprite.clones.filter(clone => !clone.dragging) |
|
.map(clone => clone.drawableID); |
|
return this.renderer.isTouchingDrawables( |
|
this.drawableID, drawableCandidates); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
isTouchingTarget (targetId) { |
|
targetId = Cast.toString(targetId); |
|
const target = this.runtime.getTargetById(targetId); |
|
if (!target || !this.renderer || target.dragging) { |
|
return false; |
|
} |
|
return this.renderer.isTouchingDrawables( |
|
this.drawableID, [target.drawableID]); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
isTouchingSpriteUnoriginals (spriteName) { |
|
spriteName = Cast.toString(spriteName); |
|
const firstClone = this.runtime.getSpriteTargetByName(spriteName); |
|
if (!firstClone || !this.renderer) { |
|
return false; |
|
} |
|
|
|
|
|
|
|
const drawableCandidates = firstClone.sprite.clones.filter(clone => !clone.dragging && !clone.isOriginal) |
|
.map(clone => clone.drawableID); |
|
return this.renderer.isTouchingDrawables( |
|
this.drawableID, drawableCandidates); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
spriteTouchingPoint (spriteName) { |
|
spriteName = Cast.toString(spriteName); |
|
const firstClone = this.runtime.getSpriteTargetByName(spriteName); |
|
if (!firstClone || !this.renderer) { |
|
return null; |
|
} |
|
|
|
|
|
|
|
const drawableCandidates = firstClone.sprite.clones.filter(clone => !clone.dragging) |
|
.map(clone => clone.drawableID); |
|
return this.renderer.getTouchingDrawablesPoint( |
|
this.drawableID, drawableCandidates); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
isTouchingColor (rgb) { |
|
if (this.renderer) { |
|
return this.renderer.isTouchingColor(this.drawableID, rgb); |
|
} |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
colorIsTouchingColor (targetRgb, maskRgb) { |
|
if (this.renderer) { |
|
return this.renderer.isTouchingColor( |
|
this.drawableID, |
|
targetRgb, |
|
maskRgb |
|
); |
|
} |
|
return false; |
|
} |
|
|
|
getLayerOrder () { |
|
if (this.renderer) { |
|
return this.renderer.getDrawableOrder(this.drawableID); |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
goToFront () { |
|
if (this.renderer) { |
|
|
|
|
|
this.renderer.setDrawableOrder(this.drawableID, Infinity, StageLayering.SPRITE_LAYER); |
|
} |
|
|
|
this.runtime.setExecutablePosition(this, Infinity); |
|
} |
|
|
|
|
|
|
|
|
|
goToBack () { |
|
if (this.renderer) { |
|
|
|
|
|
this.renderer.setDrawableOrder(this.drawableID, -Infinity, StageLayering.SPRITE_LAYER, false); |
|
} |
|
|
|
this.runtime.setExecutablePosition(this, -Infinity); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
goForwardLayers (nLayers) { |
|
if (this.renderer) { |
|
this.renderer.setDrawableOrder(this.drawableID, nLayers, StageLayering.SPRITE_LAYER, true); |
|
} |
|
|
|
this.runtime.moveExecutable(this, nLayers); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
goBackwardLayers (nLayers) { |
|
if (this.renderer) { |
|
this.renderer.setDrawableOrder(this.drawableID, -nLayers, StageLayering.SPRITE_LAYER, true); |
|
} |
|
|
|
this.runtime.moveExecutable(this, -nLayers); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
goBehindOther (other) { |
|
if (this.renderer) { |
|
const otherLayer = this.renderer.setDrawableOrder( |
|
other.drawableID, 0, StageLayering.SPRITE_LAYER, true); |
|
this.renderer.setDrawableOrder(this.drawableID, otherLayer, StageLayering.SPRITE_LAYER); |
|
} |
|
|
|
const executionPosition = this.runtime.executableTargets.indexOf(other); |
|
this.runtime.setExecutablePosition(this, executionPosition); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
keepInFence (newX, newY, optFence) { |
|
let fence = optFence; |
|
if (!fence) { |
|
fence = { |
|
left: -this.runtime.stageWidth / 2, |
|
right: this.runtime.stageWidth / 2, |
|
top: this.runtime.stageHeight / 2, |
|
bottom: -this.runtime.stageHeight / 2 |
|
}; |
|
} |
|
const bounds = this.getBounds(); |
|
if (!bounds) return; |
|
|
|
bounds.left += (newX - this.x); |
|
bounds.right += (newX - this.x); |
|
bounds.top += (newY - this.y); |
|
bounds.bottom += (newY - this.y); |
|
|
|
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]; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
makeClone () { |
|
if (!this.runtime.clonesAvailable() || this.isStage) { |
|
return null; |
|
} |
|
this.runtime.changeCloneCounter(1); |
|
const newClone = this.sprite.createClone(); |
|
|
|
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.stretch = this.stretch; |
|
newClone.currentCostume = this.currentCostume; |
|
newClone.rotationStyle = this.rotationStyle; |
|
newClone.effects = Clone.simple(this.effects); |
|
newClone.variables = this.duplicateVariables(); |
|
newClone.cameraBound = this.cameraBound; |
|
newClone._edgeActivatedHatValues = Clone.simple(this._edgeActivatedHatValues); |
|
newClone.initDrawable(StageLayering.SPRITE_LAYER); |
|
newClone.updateAllDrawableProperties(); |
|
return newClone; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
duplicate () { |
|
return this.sprite.duplicate().then(newSprite => { |
|
const newTarget = newSprite.createClone(); |
|
|
|
|
|
newTarget.x = (Math.random() - 0.5) * 400 / 2; |
|
newTarget.y = (Math.random() - 0.5) * 300 / 2; |
|
newTarget.direction = this.direction; |
|
newTarget.draggable = this.draggable; |
|
newTarget.visible = this.visible; |
|
newTarget.size = this.size; |
|
newTarget.stretch = this.stretch; |
|
newTarget.currentCostume = this.currentCostume; |
|
newTarget.rotationStyle = this.rotationStyle; |
|
newTarget.effects = JSON.parse(JSON.stringify(this.effects)); |
|
newTarget.variables = this.duplicateVariables(newTarget.blocks); |
|
newTarget.cameraBound = this.cameraBound; |
|
newTarget.updateAllDrawableProperties(); |
|
return newTarget; |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
onGreenFlag () { |
|
this.clearEffects(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
onStopAll () { |
|
this.clearEffects(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
postSpriteInfo (data) { |
|
const force = data.hasOwnProperty('force') ? data.force : null; |
|
const isXChanged = data.hasOwnProperty('x'); |
|
const isYChanged = data.hasOwnProperty('y'); |
|
if (isXChanged || isYChanged) { |
|
this.setXY(isXChanged ? data.x : this.x, isYChanged ? data.y : this.y, force); |
|
} |
|
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); |
|
} |
|
if (data.hasOwnProperty('size')) { |
|
this.setSize(data.size); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
startDrag () { |
|
this.dragging = true; |
|
} |
|
|
|
|
|
|
|
|
|
stopDrag () { |
|
this.dragging = false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
toJSON () { |
|
const costumes = this.getCostumes(); |
|
return { |
|
id: this.id, |
|
name: this.getName(), |
|
isStage: this.isStage, |
|
isDisposed: this.isDisposed, |
|
x: this.x, |
|
y: this.y, |
|
size: this.size, |
|
direction: this.direction, |
|
draggable: this.draggable, |
|
currentCostume: this.currentCostume, |
|
costume: costumes[this.currentCostume], |
|
costumeCount: costumes.length, |
|
visible: this.visible, |
|
rotationStyle: this.rotationStyle, |
|
comments: this.comments, |
|
blocks: this.blocks._blocks, |
|
variables: this.variables, |
|
costumes: costumes, |
|
sounds: this.getSounds(), |
|
textToSpeechLanguage: this.textToSpeechLanguage, |
|
tempo: this.tempo, |
|
volume: this.volume, |
|
videoTransparency: this.videoTransparency, |
|
videoState: this.videoState |
|
|
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
dispose () { |
|
|
|
this.runtime.removeListener('CAMERA_CHANGED', this.cameraUpdateEvent); |
|
|
|
if (!this.isOriginal) { |
|
this.runtime.changeCloneCounter(-1); |
|
} |
|
this.isDisposed = true; |
|
this.runtime.stopForTarget(this); |
|
this.runtime.removeExecutable(this); |
|
this.sprite.removeClone(this); |
|
if (this.renderer && this.drawableID !== null) { |
|
this.renderer.destroyDrawable(this.drawableID, this.isStage ? |
|
StageLayering.BACKGROUND_LAYER : |
|
StageLayering.SPRITE_LAYER); |
|
if (this.visible) { |
|
this.emitVisualChange(); |
|
this.runtime.requestRedraw(); |
|
} |
|
} |
|
} |
|
|
|
|
|
getCostumeType(idx) { |
|
const owner = this; |
|
const ent = this.getCostumes()[idx]; |
|
if (!ent) return; |
|
ent.customId = 'pm-costume-asset'; |
|
|
|
Object.defineProperty(ent, '_monitorUpToDate', { |
|
get() { |
|
if (this._oldName !== this.name) return false; |
|
if (this._oldSizeX !== this.size[0]) return false; |
|
if (this._oldSizeY !== this.size[1]) return false; |
|
if (this._oldAssetId !== this.assetId) return false; |
|
if (this._oldIndex !== owner.getCostumeIndexByName(this.name)) return false; |
|
return true; |
|
} |
|
}); |
|
ent.toReporterContent = function() { |
|
this._oldName = this.name; |
|
this._oldSizeX = this.size[0]; |
|
this._oldSizeY = this.size[1]; |
|
this._oldAssetId = this.assetId; |
|
this._oldIndex = owner.getCostumeIndexByName(this.name); |
|
const wrap = document.createElement('div'); |
|
wrap.innerHTML = `<div style=" |
|
box-sizing: border-box; |
|
width: 5rem; |
|
height: 5rem; |
|
display: flex; |
|
flex-direction: column; |
|
justify-content: flex-start; |
|
font-size: 0.625rem; |
|
overflow: hidden; |
|
border: 2px solid hsla(0, 0%, 0%, 0.15); |
|
border-radius: 0.5rem; |
|
color: hsla(225, 15%, 40%, 1); |
|
user-select: none; |
|
"> |
|
<div style=" |
|
width: 100%; |
|
height: 100%; |
|
display: flex; |
|
align-items: center; |
|
justify-content: center; |
|
"> |
|
<div style=" |
|
position: absolute; |
|
font-weight: bold; |
|
left: 0.45rem; |
|
top: 0.55rem; |
|
width: 1em; |
|
height: 1em; |
|
white-space: nowrap; |
|
"> |
|
${xmlEscape(((owner.getCostumeIndexByName(this.name) +1) || 'X').toString())} |
|
</div> |
|
<img |
|
style="max-width: 32px; max-height: 32px;" |
|
src="${xmlEscape(getCostumeUrl(this.asset))}" |
|
></img> |
|
</div> |
|
<div style="padding: 0.25rem; text-overflow: ellipsis; white-space: nowrap;"> |
|
${xmlEscape(this.name)} |
|
<div style="font-size: 0.5rem; margin-top: 0.125rem"> |
|
${xmlEscape(Math.round(this.size[0]).toString())} x ${xmlEscape(Math.round(this.size[1]).toString())} |
|
</div> |
|
</div> |
|
</div>`; |
|
return wrap; |
|
}; |
|
return ent; |
|
} |
|
getSoundType(idx) { |
|
const owner = this; |
|
const ent = this.getSounds()[idx]; |
|
if (!ent) return; |
|
ent.customId = 'pm-sound-asset'; |
|
Object.defineProperty(ent, '_monitorUpToDate', { |
|
get() { |
|
if (this._oldName !== this.name) return false; |
|
if (this._oldAssetId !== this.assetId) return false; |
|
if (this._oldSampleRate !== this.rate) return false; |
|
if (this._oldSampleCount !== this.sampleCount) return false; |
|
if (this._oldIndex !== owner.getSoundIndexByName(this.name)) return false; |
|
return true; |
|
} |
|
}); |
|
ent.toReporterContent = function() { |
|
this._oldName = this.name; |
|
this._oldAssetId = this.assetId; |
|
this._oldSampleRate = this.rate; |
|
this._oldSampleCount = this.sampleCount; |
|
this._oldIndex = owner.getSoundIndexByName(this.name); |
|
const wrap = document.createElement('div'); |
|
wrap.innerHTML = `<div style=" |
|
box-sizing: border-box; |
|
width: 5rem; |
|
height: 5rem; |
|
display: flex; |
|
flex-direction: column; |
|
justify-content: flex-start; |
|
font-size: 0.625rem; |
|
overflow: hidden; |
|
border: 2px solid hsla(0, 0%, 0%, 0.15); |
|
border-radius: 0.5rem; |
|
color: hsla(225, 15%, 40%, 1); |
|
user-select: none; |
|
"> |
|
<div style=" |
|
width: 100%; |
|
height: 100%; |
|
display: flex; |
|
align-items: center; |
|
justify-content: center; |
|
"> |
|
<div style=" |
|
position: absolute; |
|
font-weight: bold; |
|
left: 0.45rem; |
|
top: 0.55rem; |
|
width: 1em; |
|
height: 1em; |
|
white-space: nowrap; |
|
"> |
|
${xmlEscape(((owner.getSoundIndexByName(this.name) +1) || 'X').toString())} |
|
</div> |
|
<img |
|
style="max-width: 32px; max-height: 32px;" |
|
src="static/assets/63e5827c1506216bd7c9927a4e5eb558.svg" |
|
></img> |
|
</div> |
|
<div style="padding: 0.25rem; text-overflow: ellipsis; white-space: nowrap;"> |
|
${xmlEscape(this.name)} |
|
<div style="font-size: 0.5rem; margin-top: 0.125rem"> |
|
${xmlEscape((this.sampleCount / this.rate).toFixed(2))} |
|
</div> |
|
</div> |
|
</div>`; |
|
return wrap; |
|
}; |
|
return ent; |
|
} |
|
|
|
get _monitorUpToDate() { |
|
if (this._oldName !== this.getName()) return false; |
|
if (this._oldCostumeIdx !== this.currentCostume) return false; |
|
if (this._oldCostumeAssetId !== this.getCurrentCostume().assetId) return false; |
|
return true; |
|
} |
|
toString() { return this.getName(); } |
|
toReporterContent() { |
|
this._oldName = this.getName(); |
|
this._oldCostumeIdx = this.currentCostume; |
|
this._oldCostumeAssetId = this.getCurrentCostume().assetId; |
|
const wrap = document.createElement('div'); |
|
wrap.innerHTML = `<div style=" |
|
box-sizing: border-box; |
|
width: 4rem; |
|
height: 4rem; |
|
display: flex; |
|
flex-direction: column; |
|
justify-content: flex-start; |
|
font-size: 0.625rem; |
|
overflow: hidden; |
|
cursor: pointer; |
|
border: 2px solid hsla(0, 0%, 0%, 0.15); |
|
border-radius: 0.5rem; |
|
color: hsla(225, 15%, 40%, 1); |
|
user-select: none; |
|
"> |
|
<div style=" |
|
width: 100%; |
|
height: 100%; |
|
display: flex; |
|
align-items: center; |
|
justify-content: center; |
|
"> |
|
<img |
|
style="max-width: 32px; max-height: 32px;" |
|
src="${xmlEscape(getCostumeUrl(this.getCurrentCostume().asset))}" |
|
></img> |
|
</div> |
|
<div style="padding: 0.25rem; text-overflow: ellipsis; white-space: nowrap;"> |
|
${xmlEscape(this.getName())} |
|
</div> |
|
</div>`; |
|
wrap.onclick = () => |
|
this.runtime.vm.setEditingTarget(this.id); |
|
return wrap; |
|
} |
|
} |
|
|
|
module.exports = RenderedTarget; |
|
|