Merge pull request #884 from paulkaplan/costume-backdrop-blocks

Implement opcodes for new costume/backdrop reporters
This commit is contained in:
Paul Kaplan 2018-01-04 22:05:47 -05:00 committed by GitHub
commit 980efedf0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 17 deletions

View file

@ -236,9 +236,8 @@ class Scratch3LooksBlocks {
looks_gotofrontback: this.goToFrontBack, looks_gotofrontback: this.goToFrontBack,
looks_goforwardbackwardlayers: this.goForwardBackwardLayers, looks_goforwardbackwardlayers: this.goForwardBackwardLayers,
looks_size: this.getSize, looks_size: this.getSize,
looks_costumeorder: this.getCostumeIndex, looks_costumenumbername: this.getCostumeNumberName,
looks_backdroporder: this.getBackdropIndex, looks_backdropnumbername: this.getBackdropNumberName
looks_backdropname: this.getBackdropName
}; };
} }
@ -433,19 +432,22 @@ class Scratch3LooksBlocks {
return Math.round(util.target.size); return Math.round(util.target.size);
} }
getBackdropIndex () { getBackdropNumberName (args) {
const stage = this.runtime.getTargetForStage(); const stage = this.runtime.getTargetForStage();
if (args.NUMBER_NAME === 'number') {
return stage.currentCostume + 1; return stage.currentCostume + 1;
} }
// Else return name
getBackdropName () {
const stage = this.runtime.getTargetForStage();
return stage.sprite.costumes[stage.currentCostume].name; return stage.sprite.costumes[stage.currentCostume].name;
} }
getCostumeIndex (args, util) { getCostumeNumberName (args, util) {
if (args.NUMBER_NAME === 'number') {
return util.target.currentCostume + 1; return util.target.currentCostume + 1;
} }
// Else return name
return util.target.sprite.costumes[util.target.currentCostume].name;
}
} }
module.exports = Scratch3LooksBlocks; module.exports = Scratch3LooksBlocks;

View file

@ -602,17 +602,38 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
} }
} }
// Updated layering blocks // Updates for blocks that have new menus (e.g. in Looks)
if (oldOpcode === 'comeToFront') { switch (oldOpcode) {
case 'comeToFront':
activeBlock.fields.FRONT_BACK = { activeBlock.fields.FRONT_BACK = {
name: 'FRONT_BACK', name: 'FRONT_BACK',
value: 'front' value: 'front'
}; };
} else if (oldOpcode === 'goBackByLayers:') { break;
case 'goBackByLayers:':
activeBlock.fields.FORWARD_BACKWARD = { activeBlock.fields.FORWARD_BACKWARD = {
name: 'FORWARD_BACKWARD', name: 'FORWARD_BACKWARD',
value: 'backward' value: 'backward'
}; };
break;
case 'backgroundIndex':
activeBlock.fields.NUMBER_NAME = {
name: 'NUMBER_NAME',
value: 'number'
};
break;
case 'sceneName':
activeBlock.fields.NUMBER_NAME = {
name: 'NUMBER_NAME',
value: 'name'
};
break;
case 'costumeIndex':
activeBlock.fields.NUMBER_NAME = {
name: 'NUMBER_NAME',
value: 'number'
};
break;
} }
// Special cases to generate mutations. // Special cases to generate mutations.

View file

@ -360,12 +360,12 @@ const specMap = {
] ]
}, },
'costumeIndex': { 'costumeIndex': {
opcode: 'looks_costumeorder', opcode: 'looks_costumenumbername',
argMap: [ argMap: [
] ]
}, },
'sceneName': { 'sceneName': {
opcode: 'looks_backdropname', opcode: 'looks_backdropnumbername',
argMap: [ argMap: [
] ]
}, },
@ -390,7 +390,7 @@ const specMap = {
] ]
}, },
'backgroundIndex': { 'backgroundIndex': {
opcode: 'looks_backdroporder', opcode: 'looks_backdropnumbername',
argMap: [ argMap: [
] ]
}, },

52
test/unit/blocks_looks.js Normal file
View file

@ -0,0 +1,52 @@
const test = require('tap').test;
const Looks = require('../../src/blocks/scratch3_looks');
const util = {
target: {
currentCostume: 0, // Internally, current costume is 0 indexed
sprite: {
costumes: [
{name: 'first name'},
{name: 'second name'},
{name: 'third name'}
]
}
}
};
const fakeRuntime = {
getTargetForStage: () => util.target, // Just return the dummy target above.
on: () => {} // Stub out listener methods used in constructor.
};
const blocks = new Looks(fakeRuntime);
test('getCostumeNumberName returns 1-indexed costume number', t => {
util.target.currentCostume = 0; // This is 0-indexed.
const args = {NUMBER_NAME: 'number'};
const number = blocks.getCostumeNumberName(args, util);
t.strictEqual(number, 1);
t.end();
});
test('getCostumeNumberName can return costume name', t => {
util.target.currentCostume = 0; // This is 0-indexed.
const args = {NUMBER_NAME: 'name'};
const number = blocks.getCostumeNumberName(args, util);
t.strictEqual(number, 'first name');
t.end();
});
test('getBackdropNumberName returns 1-indexed costume number', t => {
util.target.currentCostume = 2; // This is 0-indexed.
const args = {NUMBER_NAME: 'number'};
const number = blocks.getBackdropNumberName(args, util);
t.strictEqual(number, 3);
t.end();
});
test('getBackdropNumberName can return costume name', t => {
util.target.currentCostume = 2; // This is 0-indexed.
const args = {NUMBER_NAME: 'name'};
const number = blocks.getBackdropNumberName(args, util);
t.strictEqual(number, 'third name');
t.end();
});