mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
Add a block container for monitors that will run in the sequencer
This commit is contained in:
parent
75a9ad4675
commit
706e112082
5 changed files with 54 additions and 6 deletions
|
@ -273,16 +273,22 @@ class Blocks {
|
|||
*/
|
||||
changeBlock (args) {
|
||||
// Validate
|
||||
if (args.element !== 'field' && args.element !== 'mutation') return;
|
||||
if (['field', 'mutation', 'checkbox'].indexOf(args.element) === -1) return;
|
||||
const block = this._blocks[args.id];
|
||||
if (typeof block === 'undefined') return;
|
||||
|
||||
if (args.element === 'field') {
|
||||
switch (args.element) {
|
||||
case 'field':
|
||||
// Update block value
|
||||
if (!block.fields[args.name]) return;
|
||||
block.fields[args.name].value = args.value;
|
||||
} else if (args.element === 'mutation') {
|
||||
break;
|
||||
case 'mutation':
|
||||
block.mutation = mutationAdapter(args.value);
|
||||
break;
|
||||
case 'checkbox':
|
||||
block.isMonitored = args.value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -342,6 +348,20 @@ class Blocks {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Block management: run all blocks.
|
||||
* @param {!object} runtime Runtime to run all blocks in.
|
||||
*/
|
||||
runAllMonitored (runtime) {
|
||||
Object.keys(this._blocks).forEach(blockId => {
|
||||
if (this.getBlock(blockId).isMonitored) {
|
||||
// @todo handle specific targets (e.g. apple x position)
|
||||
runtime.toggleScript(blockId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Block management: delete blocks and their associated scripts.
|
||||
* @param {!object} e Blockly delete event to be processed.
|
||||
|
|
|
@ -52,6 +52,13 @@ class Runtime extends EventEmitter {
|
|||
*/
|
||||
this.flyoutBlocks = new Blocks();
|
||||
|
||||
/**
|
||||
* Storage container for monitor blocks.
|
||||
* These will execute on a target maybe
|
||||
* @type {!Blocks}
|
||||
*/
|
||||
this.monitorBlocks = new Blocks();
|
||||
|
||||
/**
|
||||
* Currently known editing target for the VM.
|
||||
* @type {?Target}
|
||||
|
@ -416,8 +423,9 @@ class Runtime extends EventEmitter {
|
|||
/**
|
||||
* Toggle a script.
|
||||
* @param {!string} topBlockId ID of block that starts the script.
|
||||
* @param {?string} optTarget target ID for target to run script on. If not supplied, uses editing target.
|
||||
*/
|
||||
toggleScript (topBlockId) {
|
||||
toggleScript (topBlockId, optTarget) {
|
||||
// Remove any existing thread.
|
||||
for (let i = 0; i < this.threads.length; i++) {
|
||||
if (this.threads[i].topBlock === topBlockId) {
|
||||
|
@ -426,7 +434,7 @@ class Runtime extends EventEmitter {
|
|||
}
|
||||
}
|
||||
// Otherwise add it.
|
||||
this._pushThread(topBlockId, this._editingTarget);
|
||||
this._pushThread(topBlockId, optTarget ? optTarget : this._editingTarget);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -632,6 +640,7 @@ class Runtime extends EventEmitter {
|
|||
}
|
||||
}
|
||||
this.redrawRequested = false;
|
||||
this._pushMonitors();
|
||||
const doneThreads = this.sequencer.stepThreads();
|
||||
this._updateGlows(doneThreads);
|
||||
this._setThreadCount(this.threads.length + doneThreads.length);
|
||||
|
@ -641,6 +650,13 @@ class Runtime extends EventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue monitor blocks to sequencer to be run.
|
||||
*/
|
||||
_pushMonitors () {
|
||||
this.monitorBlocks.runAllMonitored(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current editing target known by the runtime.
|
||||
* @param {!Target} editingTarget New editing target.
|
||||
|
|
|
@ -20,7 +20,7 @@ class Target extends EventEmitter {
|
|||
super();
|
||||
|
||||
if (!blocks) {
|
||||
blocks = new Blocks(this);
|
||||
blocks = new Blocks();
|
||||
}
|
||||
/**
|
||||
* A unique ID for this target.
|
||||
|
|
|
@ -92,6 +92,7 @@ window.onload = function () {
|
|||
workspace.addChangeListener(vm.blockListener);
|
||||
const flyoutWorkspace = workspace.getFlyout().getWorkspace();
|
||||
flyoutWorkspace.addChangeListener(vm.flyoutBlockListener);
|
||||
flyoutWorkspace.addChangeListener(vm.monitorBlockListener);
|
||||
|
||||
// Create FPS counter.
|
||||
const stats = new window.Stats();
|
||||
|
|
|
@ -58,6 +58,7 @@ class VirtualMachine extends EventEmitter {
|
|||
|
||||
this.blockListener = this.blockListener.bind(this);
|
||||
this.flyoutBlockListener = this.flyoutBlockListener.bind(this);
|
||||
this.monitorBlockListener = this.monitorBlockListener.bind(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -344,6 +345,16 @@ class VirtualMachine extends EventEmitter {
|
|||
this.runtime.flyoutBlocks.blocklyListen(e, this.runtime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a Blockly event for the flyout to be passed to the monitor container.
|
||||
* @param {!Blockly.Event} e Any Blockly event.
|
||||
*/
|
||||
monitorBlockListener (e) {
|
||||
if (['create', 'change'].indexOf(e.type) !== -1) {
|
||||
this.runtime.monitorBlocks.blocklyListen(e, this.runtime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an editing target. An editor UI can use this function to switch
|
||||
* between editing different targets, sprites, etc.
|
||||
|
|
Loading…
Reference in a new issue