Merge commit 'refs/pull/2187/head' of github.com:LLK/scratch-vm into develop

This commit is contained in:
DD Liu 2020-03-05 14:54:00 -05:00
commit 7dad0d76dc
2 changed files with 32 additions and 14 deletions

View file

@ -233,6 +233,26 @@ class Scratch3LooksBlocks {
this._positionBubble(target); this._positionBubble(target);
} }
/**
* Properly format text for a text bubble.
* @param {string} text The text to be formatted
* @return {string} The formatted text
* @private
*/
_formatBubbleText (text) {
if (text === '') return text;
// Limit decimal precision to 2 digits.
if (typeof text === 'number') {
text = parseFloat(text.toFixed(2));
}
// Limit the length of the string.
text = String(text).substr(0, Scratch3LooksBlocks.SAY_BUBBLE_LIMIT);
return text;
}
/** /**
* The entry point for say/think blocks. Clears existing bubble if the text is empty. * The entry point for say/think blocks. Clears existing bubble if the text is empty.
* Set the bubble custom state and then call _renderBubble. * Set the bubble custom state and then call _renderBubble.
@ -244,7 +264,7 @@ class Scratch3LooksBlocks {
_updateBubble (target, type, text) { _updateBubble (target, type, text) {
const bubbleState = this._getBubbleState(target); const bubbleState = this._getBubbleState(target);
bubbleState.type = type; bubbleState.type = type;
bubbleState.text = text; bubbleState.text = this._formatBubbleText(text);
bubbleState.usageId = uid(); bubbleState.usageId = uid();
this._renderBubble(target); this._renderBubble(target);
} }
@ -300,12 +320,7 @@ class Scratch3LooksBlocks {
say (args, util) { say (args, util) {
// @TODO in 2.0 calling say/think resets the right/left bias of the bubble // @TODO in 2.0 calling say/think resets the right/left bias of the bubble
let message = args.MESSAGE; this.runtime.emit('SAY', util.target, 'say', args.MESSAGE);
if (typeof message === 'number') {
message = parseFloat(message.toFixed(2));
}
message = String(message).substr(0, Scratch3LooksBlocks.SAY_BUBBLE_LIMIT);
this.runtime.emit('SAY', util.target, 'say', message);
} }
sayforsecs (args, util) { sayforsecs (args, util) {
@ -325,7 +340,7 @@ class Scratch3LooksBlocks {
} }
think (args, util) { think (args, util) {
this._updateBubble(util.target, 'think', String(args.MESSAGE).substr(0, Scratch3LooksBlocks.SAY_BUBBLE_LIMIT)); this.runtime.emit('SAY', util.target, 'think', args.MESSAGE);
} }
thinkforsecs (args, util) { thinkforsecs (args, util) {

View file

@ -15,7 +15,9 @@ const util = {
{name: 'second name'}, {name: 'second name'},
{name: 'third name'} {name: 'third name'}
] ]
} },
_customState: {},
getCustomState: () => util.target._customState
} }
}; };
@ -202,14 +204,15 @@ test('numbers should be rounded to two decimals in say', t => {
const args = {MESSAGE: 3.14159}; const args = {MESSAGE: 3.14159};
const expectedSayString = '3.14'; const expectedSayString = '3.14';
rt.removeAllListeners('SAY'); // Prevent say blocks from executing rt.addListener('SAY', () => {
const bubbleState = util.target.getCustomState(Looks.STATE_KEY);
rt.addListener('SAY', (target, type, sayString) => { t.strictEqual(bubbleState.text, expectedSayString);
t.strictEqual(sayString, expectedSayString);
t.end();
}); });
looks.say(args, util); looks.say(args, util);
looks.think(args, util);
t.end();
}); });
test('clamp graphic effects', t => { test('clamp graphic effects', t => {