mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-22 22:12:28 -05:00
Merge pull request #10 from tmickel/feature/toggle-thread
Fix _removeThread, add toggleStack, green flag, stop button functions
This commit is contained in:
commit
7a5341b863
4 changed files with 133 additions and 14 deletions
|
@ -172,13 +172,57 @@ Runtime.prototype._pushThread = function (id) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a thread from the list of threads.
|
* Remove a thread from the list of threads.
|
||||||
* @param {!string} id ID of block that starts the stack
|
* @param {?Thread} thread Thread object to remove from actives
|
||||||
*/
|
*/
|
||||||
Runtime.prototype._removeThread = function (id) {
|
Runtime.prototype._removeThread = function (thread) {
|
||||||
var i = this.threads.indexOf(id);
|
var i = this.threads.indexOf(thread);
|
||||||
if (i > -1) this.threads.splice(i, 1);
|
if (i > -1) this.threads.splice(i, 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle a stack
|
||||||
|
* @param {!string} stackId ID of block that starts the stack
|
||||||
|
*/
|
||||||
|
Runtime.prototype.toggleStack = function (stackId) {
|
||||||
|
// Remove any existing thread
|
||||||
|
for (var i = 0; i < this.threads.length; i++) {
|
||||||
|
if (this.threads[i].topBlock == stackId) {
|
||||||
|
this._removeThread(this.threads[i]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Otherwise add it
|
||||||
|
this._pushThread(stackId);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Green flag, which stops currently running threads
|
||||||
|
* and adds all top-level stacks that start with the green flag
|
||||||
|
*/
|
||||||
|
Runtime.prototype.greenFlag = function () {
|
||||||
|
// Remove all existing threads
|
||||||
|
for (var i = 0; i < this.threads.length; i++) {
|
||||||
|
this._removeThread(this.threads[i]);
|
||||||
|
}
|
||||||
|
// Add all top stacks with green flag
|
||||||
|
for (var j = 0; j < this.stacks.length; j++) {
|
||||||
|
var topBlock = this.stacks[j];
|
||||||
|
if (this.blocks[topBlock].opcode === 'event_whenflagclicked') {
|
||||||
|
this._pushThread(this.stacks[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop "everything"
|
||||||
|
*/
|
||||||
|
Runtime.prototype.stopAll = function () {
|
||||||
|
for (var i = 0; i < this.threads.length; i++) {
|
||||||
|
this._removeThread(this.threads[i]);
|
||||||
|
}
|
||||||
|
// @todo call stop function in all extensions/packages/WeDo stub
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Repeatedly run `sequencer.stepThreads` and filter out
|
* Repeatedly run `sequencer.stepThreads` and filter out
|
||||||
* inactive threads after each iteration.
|
* inactive threads after each iteration.
|
||||||
|
|
|
@ -4,6 +4,10 @@
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function Thread (firstBlock) {
|
function Thread (firstBlock) {
|
||||||
|
/**
|
||||||
|
* Top block of the thread
|
||||||
|
*/
|
||||||
|
this.topBlock = firstBlock;
|
||||||
/**
|
/**
|
||||||
* Next block that the thread will execute.
|
* Next block that the thread will execute.
|
||||||
* @type {string}
|
* @type {string}
|
||||||
|
|
81
vm.js
81
vm.js
|
@ -77,7 +77,7 @@
|
||||||
// Blocks
|
// Blocks
|
||||||
switch (e.type) {
|
switch (e.type) {
|
||||||
case 'create':
|
case 'create':
|
||||||
instance.runtime.createBlock(adapter(e));
|
instance.runtime.createBlock(adapter(e), false);
|
||||||
break;
|
break;
|
||||||
case 'change':
|
case 'change':
|
||||||
instance.runtime.changeBlock({
|
instance.runtime.changeBlock({
|
||||||
|
@ -103,6 +103,27 @@
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
instance.flyoutBlockListener = function (e) {
|
||||||
|
switch (e.type) {
|
||||||
|
case 'create':
|
||||||
|
instance.runtime.createBlock(adapter(e), true);
|
||||||
|
break;
|
||||||
|
case 'change':
|
||||||
|
instance.runtime.changeBlock({
|
||||||
|
id: e.blockId,
|
||||||
|
element: e.element,
|
||||||
|
name: e.name,
|
||||||
|
value: e.newValue
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case 'delete':
|
||||||
|
instance.runtime.deleteBlock({
|
||||||
|
id: e.blockId
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1207,7 +1228,7 @@
|
||||||
* Block management: create blocks and stacks from a `create` event
|
* Block management: create blocks and stacks from a `create` event
|
||||||
* @param {!Object} block Blockly create event to be processed
|
* @param {!Object} block Blockly create event to be processed
|
||||||
*/
|
*/
|
||||||
Runtime.prototype.createBlock = function (block) {
|
Runtime.prototype.createBlock = function (block, opt_isFlyoutBlock) {
|
||||||
// Create new block
|
// Create new block
|
||||||
this.blocks[block.id] = block;
|
this.blocks[block.id] = block;
|
||||||
|
|
||||||
|
@ -1224,7 +1245,9 @@
|
||||||
// Push block id to stacks array. New blocks are always a stack even if only
|
// Push block id to stacks array. New blocks are always a stack even if only
|
||||||
// momentary. If the new block is added to an existing stack this stack will
|
// momentary. If the new block is added to an existing stack this stack will
|
||||||
// be removed by the `moveBlock` method below.
|
// be removed by the `moveBlock` method below.
|
||||||
|
if (!opt_isFlyoutBlock) {
|
||||||
this.stacks.push(block.id);
|
this.stacks.push(block.id);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1327,13 +1350,57 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a thread from the list of threads.
|
* Remove a thread from the list of threads.
|
||||||
* @param {!string} id ID of block that starts the stack
|
* @param {?Thread} thread Thread object to remove from actives
|
||||||
*/
|
*/
|
||||||
Runtime.prototype._removeThread = function (id) {
|
Runtime.prototype._removeThread = function (thread) {
|
||||||
var i = this.threads.indexOf(id);
|
var i = this.threads.indexOf(thread);
|
||||||
if (i > -1) this.threads.splice(i, 1);
|
if (i > -1) this.threads.splice(i, 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle a stack
|
||||||
|
* @param {!string} stackId ID of block that starts the stack
|
||||||
|
*/
|
||||||
|
Runtime.prototype.toggleStack = function (stackId) {
|
||||||
|
// Remove any existing thread
|
||||||
|
for (var i = 0; i < this.threads.length; i++) {
|
||||||
|
if (this.threads[i].topBlock == stackId) {
|
||||||
|
this._removeThread(this.threads[i]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Otherwise add it
|
||||||
|
this._pushThread(stackId);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Green flag, which stops currently running threads
|
||||||
|
* and adds all top-level stacks that start with the green flag
|
||||||
|
*/
|
||||||
|
Runtime.prototype.greenFlag = function () {
|
||||||
|
// Remove all existing threads
|
||||||
|
for (var i = 0; i < this.threads.length; i++) {
|
||||||
|
this._removeThread(this.threads[i]);
|
||||||
|
}
|
||||||
|
// Add all top stacks with green flag
|
||||||
|
for (var j = 0; j < this.stacks.length; j++) {
|
||||||
|
var topBlock = this.stacks[j];
|
||||||
|
if (this.blocks[topBlock].opcode === 'event_whenflagclicked') {
|
||||||
|
this._pushThread(this.stacks[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop "everything"
|
||||||
|
*/
|
||||||
|
Runtime.prototype.stopAll = function () {
|
||||||
|
for (var i = 0; i < this.threads.length; i++) {
|
||||||
|
this._removeThread(this.threads[i]);
|
||||||
|
}
|
||||||
|
// @todo call stop function in all extensions/packages/WeDo stub
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Repeatedly run `sequencer.stepThreads` and filter out
|
* Repeatedly run `sequencer.stepThreads` and filter out
|
||||||
* inactive threads after each iteration.
|
* inactive threads after each iteration.
|
||||||
|
@ -1499,6 +1566,10 @@
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function Thread (firstBlock) {
|
function Thread (firstBlock) {
|
||||||
|
/**
|
||||||
|
* Top block of the thread
|
||||||
|
*/
|
||||||
|
this.topBlock = firstBlock;
|
||||||
/**
|
/**
|
||||||
* Next block that the thread will execute.
|
* Next block that the thread will execute.
|
||||||
* @type {string}
|
* @type {string}
|
||||||
|
|
10
vm.min.js
vendored
10
vm.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue