Pipe through whether a monitor is sprite-specific

This commit is contained in:
DD 2017-11-13 16:28:50 -05:00
parent 95ca10038b
commit 9048a9b9e7
5 changed files with 22 additions and 8 deletions

View file

@ -226,7 +226,8 @@ class Blocks {
id: e.blockId, id: e.blockId,
element: e.element, element: e.element,
name: e.name, name: e.name,
value: e.newValue value: e.newValue,
isSpriteSpecific: e.isSpriteSpecific
}, optRuntime); }, optRuntime);
break; break;
case 'move': case 'move':
@ -326,12 +327,14 @@ class Blocks {
break; break;
case 'checkbox': case 'checkbox':
block.isMonitored = args.value; block.isMonitored = args.value;
block.targetId = args.isSpriteSpecific ? this._getTargetIdFromBlockId(block.id) : null;
if (optRuntime && wasMonitored && !block.isMonitored) { if (optRuntime && wasMonitored && !block.isMonitored) {
optRuntime.requestRemoveMonitor(block.id); optRuntime.requestRemoveMonitor(block.id);
} else if (optRuntime && !wasMonitored && block.isMonitored) { } else if (optRuntime && !wasMonitored && block.isMonitored) {
optRuntime.requestAddMonitor(MonitorRecord({ optRuntime.requestAddMonitor(MonitorRecord({
// @todo(vm#564) this will collide if multiple sprites use same block // @todo(vm#564) this will collide if multiple sprites use same block
id: block.id, id: block.id,
spriteName: block.targetId ? optRuntime.getTargetById(block.targetId).getName() : null,
opcode: block.opcode, opcode: block.opcode,
params: this._getBlockParams(block), params: this._getBlockParams(block),
// @todo(vm#565) for numerical values with decimals, some countries use comma // @todo(vm#565) for numerical values with decimals, some countries use comma
@ -406,12 +409,17 @@ class Blocks {
runAllMonitored (runtime) { runAllMonitored (runtime) {
Object.keys(this._blocks).forEach(blockId => { Object.keys(this._blocks).forEach(blockId => {
if (this.getBlock(blockId).isMonitored) { if (this.getBlock(blockId).isMonitored) {
// @todo handle specific targets (e.g. apple x position) const targetId = this.getBlock(blockId).targetId;
runtime.addMonitorScript(blockId); runtime.addMonitorScript(blockId, targetId ? runtime.getTargetById(targetId) : null);
} }
}); });
} }
_getTargetIdFromBlockId (blockId) {
// First word of block ID. See makeToolboxXML in scratch-gui
return blockId.split('_')[0];
}
/** /**
* Block management: delete blocks and their associated scripts. * Block management: delete blocks and their associated scripts.
* @param {!object} e Blockly delete event to be processed. * @param {!object} e Blockly delete event to be processed.

View file

@ -68,8 +68,10 @@ const handleReport = function (
sequencer.runtime.visualReport(currentBlockId, resolvedValue); sequencer.runtime.visualReport(currentBlockId, resolvedValue);
} }
if (thread.updateMonitor) { if (thread.updateMonitor) {
const targetId = sequencer.runtime.monitorBlocks.getBlock(currentBlockId).targetId;
sequencer.runtime.requestUpdateMonitor(Map({ sequencer.runtime.requestUpdateMonitor(Map({
id: currentBlockId, id: currentBlockId,
spriteName: targetId ? sequencer.runtime.getTargetById(targetId).getName() : null,
value: String(resolvedValue) value: String(resolvedValue)
})); }));
} }

View file

@ -2,6 +2,8 @@ const {Record} = require('immutable');
const MonitorRecord = Record({ const MonitorRecord = Record({
id: null, id: null,
/** Present only if the monitor is sprite-specific, such as x position */
spriteName: null,
opcode: null, opcode: null,
value: null, value: null,
params: null params: null

View file

@ -838,7 +838,7 @@ class Runtime extends EventEmitter {
/** /**
* Enqueue a script that when finished will update the monitor for the block. * Enqueue a script that when finished will update the monitor for the block.
* @param {!string} topBlockId ID of block that starts the 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. * @param {?string} optTarget target Target to run script on. If not supplied, uses editing target.
*/ */
addMonitorScript (topBlockId, optTarget) { addMonitorScript (topBlockId, optTarget) {
if (!optTarget) optTarget = this._editingTarget; if (!optTarget) optTarget = this._editingTarget;
@ -1260,7 +1260,7 @@ class Runtime extends EventEmitter {
* @param {!MonitorRecord} monitor Monitor to add. * @param {!MonitorRecord} monitor Monitor to add.
*/ */
requestAddMonitor (monitor) { requestAddMonitor (monitor) {
this._monitorState = this._monitorState.set(monitor.id, monitor); this._monitorState = this._monitorState.set(monitor.get('id'), monitor);
} }
/** /**
@ -1271,9 +1271,10 @@ class Runtime extends EventEmitter {
* the old monitor will keep its old value. * the old monitor will keep its old value.
*/ */
requestUpdateMonitor (monitor) { requestUpdateMonitor (monitor) {
if (this._monitorState.has(monitor.get('id'))) { const id = monitor.get('id');
if (this._monitorState.has(id)) {
this._monitorState = this._monitorState =
this._monitorState.set(monitor.get('id'), this._monitorState.get(monitor.get('id')).merge(monitor)); this._monitorState.set(id, this._monitorState.get(id).merge(monitor));
} }
} }

View file

@ -6,9 +6,10 @@
* Legal characters for the unique ID. * Legal characters for the unique ID.
* Should be all on a US keyboard. No XML special characters or control codes. * Should be all on a US keyboard. No XML special characters or control codes.
* Removed $ due to issue 251. * Removed $ due to issue 251.
* Removed _ which denotes word separation in XML.
* @private * @private
*/ */
const soup_ = '!#%()*+,-./:;=?@[]^_`{|}~' + const soup_ = '!#%()*+,-./:;=?@[]^`{|}~' +
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
/** /**