This commit is contained in:
Paul Kaplan 2017-10-05 17:03:30 -04:00
parent 43a17bdaa8
commit 43d061227c

View file

@ -1,6 +1,6 @@
const Cast = require('../util/cast'); const Cast = require('../util/cast');
const Clone = require('../util/clone'); const Clone = require('../util/clone');
const Runtime = require('../engine/runtime');
const RenderedTarget = require('../sprites/rendered-target'); const RenderedTarget = require('../sprites/rendered-target');
/** /**
@ -20,6 +20,11 @@ class Scratch3LooksBlocks {
this.runtime = runtime; this.runtime = runtime;
this._onTargetMoved = this._onTargetMoved.bind(this); this._onTargetMoved = this._onTargetMoved.bind(this);
this._onResetBubbles = this._onResetBubbles.bind(this);
// Reset all bubbles on start/stop
this.runtime.on('PROJECT_RUN_START', this._onResetBubbles);
this.runtime.on('PROJECT_RUN_STOP', this._onResetBubbles);
} }
/** /**
@ -32,7 +37,8 @@ class Scratch3LooksBlocks {
onSpriteRight: true, onSpriteRight: true,
skinId: null, skinId: null,
text: '', text: '',
type: 'say' type: 'say',
visible: true
}; };
} }
@ -81,6 +87,16 @@ class Scratch3LooksBlocks {
} }
} }
_onResetBubbles () {
// for (let n = 0; n < this.runtime.targets.length; n++) {
// const target = this.runtime.targets[n];
// const bubbleState = this._getBubbleState(target);
// if (bubbleState.drawableId) {
// this._clearBubble(target);
// }
// }
}
_positionBubble (target) { _positionBubble (target) {
const bubbleState = this._getBubbleState(target); const bubbleState = this._getBubbleState(target);
const [bubbleWidth, bubbleHeight] = this.runtime.renderer.getSkinSize(bubbleState.drawableId); const [bubbleWidth, bubbleHeight] = this.runtime.renderer.getSkinSize(bubbleState.drawableId);
@ -116,15 +132,27 @@ class Scratch3LooksBlocks {
const {type, text, onSpriteRight} = bubbleState; const {type, text, onSpriteRight} = bubbleState;
if (bubbleState.skinId) { if (bubbleState.skinId) {
if (!bubbleState.visible) {
bubbleState.visible = true;
this.runtime.renderer.updateDrawableProperties(bubbleState.drawableId, {
visible: bubbleState.visible
});
}
this.runtime.renderer.updateTextSkin(bubbleState.skinId, type, text, onSpriteRight, [0, 0]); this.runtime.renderer.updateTextSkin(bubbleState.skinId, type, text, onSpriteRight, [0, 0]);
} else { } else {
target.addListener(RenderedTarget.EVENT_TARGET_MOVED, this._onTargetMoved); target.addListener(RenderedTarget.EVENT_TARGET_MOVED, this._onTargetMoved);
// TODO is there a way to figure out before rendering whether to default left or right?
const targetBounds = target.getBounds();
const stageBounds = this.runtime.getTargetForStage().getBounds();
if (targetBounds.right + 170 > stageBounds.right) {
bubbleState.onSpriteRight = false;
}
bubbleState.drawableId = this.runtime.renderer.createDrawable(); bubbleState.drawableId = this.runtime.renderer.createDrawable();
bubbleState.skinId = this.runtime.renderer.createTextSkin(type, text, bubbleState.onSpriteRight, [0, 0]); bubbleState.skinId = this.runtime.renderer.createTextSkin(type, text, bubbleState.onSpriteRight, [0, 0]);
const order = this.runtime.renderer.getDrawableOrder(target.drawableID); this.runtime.renderer.setDrawableOrder(bubbleState.drawableId, Infinity);
this.runtime.renderer.setDrawableOrder(bubbleState.drawableId, order + 1);
this.runtime.renderer.updateDrawableProperties(bubbleState.drawableId, { this.runtime.renderer.updateDrawableProperties(bubbleState.drawableId, {
skinId: bubbleState.skinId skinId: bubbleState.skinId
}); });
@ -134,23 +162,26 @@ class Scratch3LooksBlocks {
this._positionBubble(target); this._positionBubble(target);
} }
_updateBubble (target, type, text) { _updateBubble (target, type, text) {
// Say/think empty string should clear any bubble
if (text === '') {
this._clearBubble(target);
} else {
const bubbleState = this._getBubbleState(target); const bubbleState = this._getBubbleState(target);
bubbleState.type = type; bubbleState.type = type;
bubbleState.text = text; bubbleState.text = text;
this._renderBubble(target); this._renderBubble(target);
} }
}
_clearBubble (target) { _clearBubble (target) {
const bubbleState = this._getBubbleState(target); const bubbleState = this._getBubbleState(target);
if (bubbleState.drawableId) { bubbleState.visible = false;
this.runtime.renderer.destroyDrawable(bubbleState.drawableId); this.runtime.renderer.updateDrawableProperties(bubbleState.drawableId, {
} visible: bubbleState.visible
if (bubbleState.drawableId) { });
this.runtime.renderer.destroySkin(bubbleState.skinId);
}
this._resetBubbleState(target);
// @TODO is this safe? It could have been already removed? // @TODO is this safe? It could have been already removed?
target.removeListener(RenderedTarget.EVENT_TARGET_MOVED, this._onTargetMoved); target.removeListener(RenderedTarget.EVENT_TARGET_MOVED, this._onTargetMoved);