Make "when clicked" blocks target dependent.

This involves adding a new opcode, event_whenstageclicked, and adding a
method to the blocks container to update the opcode pair depending on
whether the target is the stage. This method is then called in two
places: first after the sb2 import parses the blocks (not done in the
sb2 parsing itself because no other blocks are target specific) and then
again when blocks are shared between targets.

Also added tests for the block container method, and a fixture project
that tests the opcode splitting on sb2 import.
This commit is contained in:
Paul Kaplan 2018-03-20 09:50:57 -04:00
parent 2b985fa083
commit 140094a7ad
8 changed files with 82 additions and 0 deletions

View file

@ -593,6 +593,21 @@ class Blocks {
}
}
/**
* Keep blocks up to date after they are shared between targets.
* @param {boolean} isStage If the new target is a stage.
*/
updateTargetSpecificBlocks (isStage) {
const blocks = this._blocks;
for (const blockId in blocks) {
if (isStage && blocks[blockId].opcode === 'event_whenthisspriteclicked') {
blocks[blockId].opcode = 'event_whenstageclicked';
} else if (!isStage && blocks[blockId].opcode === 'event_whenstageclicked') {
blocks[blockId].opcode = 'event_whenthisspriteclicked';
}
}
}
/**
* Update blocks after a sound, costume, or backdrop gets renamed.
* Any block referring to the old name of the asset should get updated