Merge pull request #619 from cwillisf/clone-custom-state

Clone custom state
This commit is contained in:
Chris Willis-Ford 2017-06-27 22:37:30 -07:00 committed by GitHub
commit 47fc38fd9a
7 changed files with 83 additions and 13 deletions

View file

@ -40,7 +40,10 @@ class Scratch3PenBlocks {
*/
this._penSkinId = -1;
this._onTargetCreated = this._onTargetCreated.bind(this);
this._onTargetMoved = this._onTargetMoved.bind(this);
runtime.on('targetWasCreated', this._onTargetCreated);
}
/**
@ -129,6 +132,25 @@ class Scratch3PenBlocks {
return penState;
}
/**
* When a pen-using Target is cloned, clone the pen state.
* @param {Target} newTarget - the newly created target.
* @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
* @listens Runtime#event:targetWasCreated
* @private
*/
_onTargetCreated (newTarget, sourceTarget) {
if (sourceTarget) {
const penState = sourceTarget.getCustomState(Scratch3PenBlocks.STATE_KEY);
if (penState) {
newTarget.setCustomState(Scratch3PenBlocks.STATE_KEY, Clone.simple(penState));
if (penState.penDown) {
newTarget.addListener(RenderedTarget.EVENT_TARGET_MOVED, this._onTargetMoved);
}
}
}
}
/**
* Handle a target which has moved. This only fires when the pen is down.
* @param {RenderedTarget} target - the target which has moved.

View file

@ -968,6 +968,16 @@ class Runtime extends EventEmitter {
return this._cloneCounter < Runtime.MAX_CLONES;
}
/**
* Report that a new target has been created, possibly by cloning an existing target.
* @param {Target} newTarget - the newly created target.
* @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
* @fires Runtime#targetWasCreated
*/
fireTargetWasCreated (newTarget, sourceTarget) {
this.emit('targetWasCreated', newTarget, sourceTarget);
}
/**
* Get a target representing the Scratch stage, if one exists.
* @return {?Target} The target, if found.
@ -1014,4 +1024,12 @@ class Runtime extends EventEmitter {
}
}
/**
* Event fired after a new target has been created, possibly by cloning an existing target.
*
* @event Runtime#targetWasCreated
* @param {Target} newTarget - the newly created target.
* @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
*/
module.exports = Runtime;

View file

@ -11,17 +11,25 @@ const uid = require('../util/uid');
* Examples include sprites/clones or potentially physical-world devices.
*/
/**
* @param {?Blocks} blocks Blocks instance for the blocks owned by this target.
* @constructor
*/
class Target extends EventEmitter {
constructor (blocks) {
/**
* @param {Runtime} runtime Reference to the runtime.
* @param {?Blocks} blocks Blocks instance for the blocks owned by this target.
* @constructor
*/
constructor (runtime, blocks) {
super();
if (!blocks) {
blocks = new Blocks();
}
/**
* Reference to the runtime.
* @type {Runtime}
*/
this.runtime = runtime;
/**
* A unique ID for this target.
* @type {string}

View file

@ -4,14 +4,16 @@ const Target = require('../engine/target');
/**
* Rendered target: instance of a sprite (clone), or the stage.
* @param {!Sprite} sprite Reference to the parent sprite.
* @param {Runtime} runtime Reference to the runtime.
* @constructor
*/
class RenderedTarget extends Target {
/**
* @param {!Sprite} sprite Reference to the parent sprite.
* @param {Runtime} runtime Reference to the runtime.
* @constructor
*/
constructor (sprite, runtime) {
super(sprite.blocks);
this.runtime = runtime;
super(runtime, sprite.blocks);
/**
* Reference to the sprite that this is a render of.
* @type {!Sprite}
@ -19,7 +21,7 @@ class RenderedTarget extends Target {
this.sprite = sprite;
/**
* Reference to the global renderer for this VM, if one exists.
* @type {?RenderWebGLWorker}
* @type {?RenderWebGL}
*/
this.renderer = null;
if (this.runtime) {
@ -730,7 +732,6 @@ class RenderedTarget extends Target {
newClone.effects = JSON.parse(JSON.stringify(this.effects));
newClone.variables = JSON.parse(JSON.stringify(this.variables));
newClone.lists = JSON.parse(JSON.stringify(this.lists));
newClone._customState = JSON.parse(JSON.stringify(this._customState));
newClone.initDrawable();
newClone.updateAllDrawableProperties();
// Place behind the current target.

View file

@ -55,6 +55,9 @@ class Sprite {
this.clones.push(newClone);
if (newClone.isOriginal) {
newClone.initDrawable();
this.runtime.fireTargetWasCreated(newClone);
} else {
this.runtime.fireTargetWasCreated(newClone, this.clones[0]);
}
return newClone;
}

BIN
test/fixtures/pen.sb2 vendored

Binary file not shown.

View file

@ -1,8 +1,11 @@
const path = require('path');
const test = require('tap').test;
const Scratch3PenBlocks = require('../../src/blocks/scratch3_pen');
const VirtualMachine = require('../../src/index');
const makeTestStorage = require('../fixtures/make-test-storage');
const extract = require('../fixtures/extract');
const VirtualMachine = require('../../src/index');
const uri = path.resolve(__dirname, '../fixtures/pen.sb2');
const project = extract(uri);
@ -14,6 +17,21 @@ test('pen', t => {
// Evaluate playground data and exit
vm.on('playgroundData', () => {
// @todo Additional tests
const catSprite = vm.runtime.targets[1].sprite;
const [originalCat, cloneCat] = catSprite.clones;
t.notStrictEqual(originalCat, cloneCat);
/** @type {PenState} */
const originalPenState = originalCat.getCustomState(Scratch3PenBlocks.STATE_KEY);
/** @type {PenState} */
const clonePenState = cloneCat.getCustomState(Scratch3PenBlocks.STATE_KEY);
t.notStrictEqual(originalPenState, clonePenState);
t.equal(originalPenState.penAttributes.diameter, 51);
t.equal(clonePenState.penAttributes.diameter, 42);
t.end();
process.nextTick(process.exit);
});