mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-10 15:02:06 -05:00
Fix Runtime.stopAll
Removing threads as we were looping was causing not all of them to be hit.
This commit is contained in:
parent
5b834667eb
commit
90a5982ff9
3 changed files with 158 additions and 20 deletions
|
@ -295,8 +295,9 @@ Runtime.prototype.greenFlag = function () {
|
|||
* Stop "everything"
|
||||
*/
|
||||
Runtime.prototype.stopAll = function () {
|
||||
for (var i = 0; i < this.threads.length; i++) {
|
||||
this._removeThread(this.threads[i]);
|
||||
var threadsCopy = this.threads.slice();
|
||||
while (threadsCopy.length > 0) {
|
||||
this._removeThread(threadsCopy.pop());
|
||||
}
|
||||
// @todo call stop function in all extensions/packages/WeDo stub
|
||||
};
|
||||
|
|
163
vm.js
163
vm.js
|
@ -1473,8 +1473,9 @@
|
|||
* Stop "everything"
|
||||
*/
|
||||
Runtime.prototype.stopAll = function () {
|
||||
for (var i = 0; i < this.threads.length; i++) {
|
||||
this._removeThread(this.threads[i]);
|
||||
var threadsCopy = this.threads.slice();
|
||||
while (threadsCopy.length > 0) {
|
||||
this._removeThread(threadsCopy.pop());
|
||||
}
|
||||
// @todo call stop function in all extensions/packages/WeDo stub
|
||||
};
|
||||
|
@ -1747,8 +1748,9 @@
|
|||
});
|
||||
}
|
||||
catch(e) {
|
||||
console.error('Exception calling block function',
|
||||
{opcode: opcode, exception: e});
|
||||
console.error(
|
||||
'Exception calling block function for opcode: ' +
|
||||
opcode + '\n' + e);
|
||||
} finally {
|
||||
// Update if the thread has set a yield timer ID
|
||||
// @todo hack
|
||||
|
@ -2037,15 +2039,31 @@
|
|||
|
||||
/***/ },
|
||||
/* 12 */
|
||||
/***/ function(module, exports) {
|
||||
/***/ function(module, exports, __webpack_require__) {
|
||||
|
||||
|
||||
var YieldTimers = __webpack_require__(10);
|
||||
|
||||
function WeDo2Blocks(runtime) {
|
||||
/**
|
||||
* The runtime instantiating this block package.
|
||||
* @type {Runtime}
|
||||
*/
|
||||
this.runtime = runtime;
|
||||
|
||||
/**
|
||||
* Current motor speed, as a percentage (100 = full speed).
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
this._motorSpeed = 100;
|
||||
|
||||
/**
|
||||
* The timeout ID for a pending motor action.
|
||||
* @type {?int}
|
||||
* @private
|
||||
*/
|
||||
this._motorTimeout = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2063,20 +2081,139 @@
|
|||
};
|
||||
};
|
||||
|
||||
WeDo2Blocks.prototype.motorClockwise = function() {
|
||||
console.log('Running: wedo_motorclockwise');
|
||||
/**
|
||||
* Clamp a value between a minimum and maximum value.
|
||||
* @todo move this to a common utility class.
|
||||
* @param val The value to clamp.
|
||||
* @param min The minimum return value.
|
||||
* @param max The maximum return value.
|
||||
* @returns {number} The clamped value.
|
||||
* @private
|
||||
*/
|
||||
WeDo2Blocks.prototype._clamp = function(val, min, max) {
|
||||
return Math.max(min, Math.min(val, max));
|
||||
};
|
||||
|
||||
WeDo2Blocks.prototype.motorCounterClockwise = function() {
|
||||
console.log('Running: wedo_motorcounterclockwise');
|
||||
/**
|
||||
* Convert HSV to RGB.
|
||||
* See https://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
|
||||
* @todo move this to a common utility class.
|
||||
* @param hueDegrees Hue, in degrees.
|
||||
* @param saturation Saturation in the range [0,1].
|
||||
* @param value Value in the range [0,1].
|
||||
* @returns {number[]} An array of [r,g,b], each in the range [0,255].
|
||||
* @private
|
||||
*/
|
||||
WeDo2Blocks.prototype._HSVToRGB = function(hueDegrees, saturation, value) {
|
||||
hueDegrees %= 360;
|
||||
if (hueDegrees < 0) hueDegrees += 360;
|
||||
saturation = this._clamp(saturation, 0, 1);
|
||||
value = this._clamp(value, 0, 1);
|
||||
|
||||
var chroma = value * saturation;
|
||||
var huePrime = hueDegrees / 60;
|
||||
var x = chroma * (1 - Math.abs(huePrime % 2 - 1));
|
||||
var rgb;
|
||||
switch (Math.floor(huePrime)) {
|
||||
case 0:
|
||||
rgb = [chroma, x, 0];
|
||||
break;
|
||||
case 1:
|
||||
rgb = [x, chroma, 0];
|
||||
break;
|
||||
case 2:
|
||||
rgb = [0, chroma, x];
|
||||
break;
|
||||
case 3:
|
||||
rgb = [0, x, chroma];
|
||||
break;
|
||||
case 4:
|
||||
rgb = [x, 0, chroma];
|
||||
break;
|
||||
case 5:
|
||||
rgb = [chroma, 0, x];
|
||||
break;
|
||||
}
|
||||
|
||||
var m = value - chroma;
|
||||
rgb[0] = (rgb[0] + m) * 255;
|
||||
rgb[1] = (rgb[1] + m) * 255;
|
||||
rgb[2] = (rgb[2] + m) * 255;
|
||||
|
||||
return rgb;
|
||||
};
|
||||
|
||||
WeDo2Blocks.prototype.motorSpeed = function() {
|
||||
console.log('Running: wedo_motorspeed');
|
||||
/**
|
||||
* Common implementation for motor blocks.
|
||||
* @param direction The direction to turn ('left' or 'right').
|
||||
* @param durationSeconds The number of seconds to run.
|
||||
* @param util The util instance to use for yielding and finishing.
|
||||
* @private
|
||||
*/
|
||||
WeDo2Blocks.prototype._motorOnFor = function(direction, durationSeconds, util) {
|
||||
if (this._motorTimeout > 0) {
|
||||
// @todo maybe this should go through util
|
||||
YieldTimers.reject(this._motorTimeout);
|
||||
this._motorTimeout = null;
|
||||
}
|
||||
if (window.native) {
|
||||
window.native.motorRun(direction, this._motorSpeed);
|
||||
}
|
||||
|
||||
var instance = this;
|
||||
var myTimeout = this._motorTimeout = util.timeout(function() {
|
||||
if (instance._motorTimeout == myTimeout) {
|
||||
instance._motorTimeout = null;
|
||||
}
|
||||
if (window.native) {
|
||||
window.native.motorStop();
|
||||
}
|
||||
util.done();
|
||||
}, 1000 * durationSeconds);
|
||||
|
||||
util.yield();
|
||||
};
|
||||
|
||||
WeDo2Blocks.prototype.setColor = function() {
|
||||
console.log('Running: wedo_setcolor');
|
||||
WeDo2Blocks.prototype.motorClockwise = function(argValues, util) {
|
||||
this._motorOnFor('right', argValues[0], util);
|
||||
};
|
||||
|
||||
WeDo2Blocks.prototype.motorCounterClockwise = function(argValues, util) {
|
||||
this._motorOnFor('left', argValues[0], util);
|
||||
};
|
||||
|
||||
WeDo2Blocks.prototype.motorSpeed = function(argValues) {
|
||||
this._motorSpeed = this._clamp(argValues[0], 1, 100);
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert a color name to an [r,b,g] array.
|
||||
* Supports 'mystery' for a random hue.
|
||||
* @param colorName The color to retrieve.
|
||||
* @returns {number[]} The [r,g,b] values for the color in [0,255] range.
|
||||
* @private
|
||||
*/
|
||||
WeDo2Blocks.prototype._getColor = function(colorName) {
|
||||
if (colorName == 'mystery') {
|
||||
return this._HSVToRGB(Math.random() * 360, 1, 1);
|
||||
}
|
||||
return {
|
||||
'yellow': [255, 255, 0],
|
||||
'orange': [255, 165, 0],
|
||||
'coral': [255, 127, 80],
|
||||
'magenta': [255, 0, 255],
|
||||
'purple': [128, 0, 128],
|
||||
'blue': [0, 0, 255],
|
||||
'green': [0, 255, 0],
|
||||
'white': [255, 255, 255]
|
||||
}[colorName];
|
||||
};
|
||||
|
||||
WeDo2Blocks.prototype.setColor = function(argValues) {
|
||||
if (window.native) {
|
||||
var rgbColor = this._getColor(argValues[0]);
|
||||
window.native.setLedColor(rgbColor[0], rgbColor[1], rgbColor[2]);
|
||||
}
|
||||
};
|
||||
|
||||
WeDo2Blocks.prototype.whenDistanceClose = function() {
|
||||
|
|
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