feat: Allow switching to specially named backdrops

The `switch costume` block accepts special values like "next costume" and
"previous costume". If you create a costume with these names, these take
priority over the special values. However, the `switch backdrop` block
keeps these special values for values like "next backdrop", "previous
backdrop", "random backdrop". It is impossible to navigate to such a
backdrop by name via block. This commit also modifies tests to allow for
this.

BREAKING CHANGE: specially-named backdrops can now be navigated
This commit is contained in:
jokebookservice1 2018-10-10 20:06:54 +01:00
parent 0c18b4952d
commit 341bd8f3d3
No known key found for this signature in database
GPG key ID: A872407E9C478467
2 changed files with 11 additions and 8 deletions

View file

@ -353,7 +353,7 @@ class Scratch3LooksBlocks {
*/
_setCostume (target, requestedCostume, optZeroIndex) {
if (typeof requestedCostume === 'number') {
// Numbers should be treated as comments, always
// Numbers should be treated as costume indices, always
target.setCostume(optZeroIndex ? requestedCostume : requestedCostume - 1);
} else {
const costumeIndex = target.getCostumeIndexByName(requestedCostume.toString());
@ -386,16 +386,17 @@ class Scratch3LooksBlocks {
*/
_setBackdrop (stage, requestedBackdrop, optZeroIndex) {
if (typeof requestedBackdrop === 'number') {
// Numbers should be treated as backdrop indices, always
stage.setCostume(optZeroIndex ? requestedBackdrop : requestedBackdrop - 1);
} else if (requestedBackdrop === 'next backdrop') {
stage.setCostume(stage.currentCostume + 1);
} else if (requestedBackdrop === 'previous backdrop') {
stage.setCostume(stage.currentCostume - 1);
} else {
const costumeIndex = stage.getCostumeIndexByName(requestedBackdrop.toString());
if (costumeIndex !== -1) {
stage.setCostume(costumeIndex);
} else if (requestedBackdrop === 'next backdrop') {
stage.setCostume(stage.currentCostume + 1);
} else if (requestedBackdrop === 'previous backdrop') {
stage.setCostume(stage.currentCostume - 1);
} else if (requestedBackdrop === 'random backdrop') {
const numCostumes = stage.getCostumes().length;
if (numCostumes > 1) {

View file

@ -129,9 +129,11 @@ test('switch backdrop block runs correctly', t => {
t.strictEqual(testBackdrop(['a', 'b', 'c', 'd'], 'previous backdrop', 3), 2);
t.strictEqual(testBackdrop(['a', 'b', 'c', 'd'], 'next backdrop', 2), 3);
// 'previous backdrop' and 'next backdrop' can't be overriden
t.strictEqual(testBackdrop(['a', 'previous backdrop', 'c', 'd'], 'previous backdrop', 4), 3);
t.strictEqual(testBackdrop(['next backdrop', 'b', 'c', 'd'], 'next backdrop'), 2);
// 'previous backdrop', 'previous backdrop', 'random backdrop' can be overriden
// Test is deterministic since 'random backdrop' will not pick the same backdrop as currently selected
t.strictEqual(testBackdrop(['a', 'previous backdrop', 'c', 'd'], 'previous backdrop', 4), 2);
t.strictEqual(testBackdrop(['next backdrop', 'b', 'c', 'd'], 'next backdrop', 3), 1);
t.strictEqual(testBackdrop(['random backdrop', 'b', 'c', 'd'], 'random backdrop'), 1);
// NaN, Infinity, and true are the first costume
t.strictEqual(testBackdrop(['a', 'b', 'c', 'd'], NaN, 2), 1);