mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-11 13:34:00 -04:00
Merge pull request #1231 from paulkaplan/reorder-tarrgets
Add top-level `reorderTarget` API to VM
This commit is contained in:
commit
bd80e4262d
2 changed files with 44 additions and 0 deletions
|
@ -6,6 +6,7 @@ const Buffer = require('buffer').Buffer;
|
|||
const centralDispatch = require('./dispatch/central-dispatch');
|
||||
const ExtensionManager = require('./extension-support/extension-manager');
|
||||
const log = require('./util/log');
|
||||
const MathUtil = require('./util/math-util');
|
||||
const Runtime = require('./engine/runtime');
|
||||
const sb2 = require('./serialization/sb2');
|
||||
const sb3 = require('./serialization/sb3');
|
||||
|
@ -1033,6 +1034,25 @@ class VirtualMachine extends EventEmitter {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reorder target by index. Return whether a change was made.
|
||||
* @param {!string} targetIndex Index of the target.
|
||||
* @param {!number} newIndex index that the target should be moved to.
|
||||
* @returns {boolean} Whether a target was reordered.
|
||||
*/
|
||||
reorderTarget (targetIndex, newIndex) {
|
||||
let targets = this.runtime.targets;
|
||||
targetIndex = MathUtil.clamp(targetIndex, 0, targets.length - 1);
|
||||
newIndex = MathUtil.clamp(newIndex, 0, targets.length - 1);
|
||||
if (targetIndex === newIndex) return false;
|
||||
const target = targets[targetIndex];
|
||||
targets = targets.slice(0, targetIndex).concat(targets.slice(targetIndex + 1));
|
||||
targets.splice(newIndex, 0, target);
|
||||
this.runtime.targets = targets;
|
||||
this.emitTargetsUpdate();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reorder the costumes of a target if it exists. Return whether it succeeded.
|
||||
* @param {!string} targetId ID of the target which owns the costumes.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue