mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 06:23:37 -05:00
Rename and delete sprites (#319)
* Add function to rename sprites * Add delete sprite function * Add `isSprite` helper and duplicate check for rename
This commit is contained in:
parent
56ef01745f
commit
0ede7e6ca1
3 changed files with 62 additions and 1 deletions
53
src/index.js
53
src/index.js
|
@ -186,6 +186,59 @@ VirtualMachine.prototype.addBackdrop = function (backdropObject) {
|
|||
stage.setCostume(stage.sprite.costumes.length - 1);
|
||||
};
|
||||
|
||||
/**
|
||||
* Rename a sprite.
|
||||
* @param {string} targetId ID of a target whose sprite to rename.
|
||||
* @param {string} newName New name of the sprite.
|
||||
*/
|
||||
VirtualMachine.prototype.renameSprite = function (targetId, newName) {
|
||||
var target = this.runtime.getTargetById(targetId);
|
||||
if (target) {
|
||||
if (!target.isSprite()) {
|
||||
throw new Error('Cannot rename non-sprite targets.');
|
||||
}
|
||||
var sprite = target.sprite;
|
||||
if (!sprite) {
|
||||
throw new Error('No sprite associated with this target.');
|
||||
}
|
||||
sprite.name = newName;
|
||||
this.emitTargetsUpdate();
|
||||
} else {
|
||||
throw new Error('No target with the provided id.');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete a sprite and all its clones.
|
||||
* @param {string} targetId ID of a target whose sprite to delete.
|
||||
*/
|
||||
VirtualMachine.prototype.deleteSprite = function (targetId) {
|
||||
var target = this.runtime.getTargetById(targetId);
|
||||
if (target) {
|
||||
if (!target.isSprite()) {
|
||||
throw new Error('Cannot delete non-sprite targets.');
|
||||
}
|
||||
var sprite = target.sprite;
|
||||
if (!sprite) {
|
||||
throw new Error('No sprite associated with this target.');
|
||||
}
|
||||
var currentEditingTarget = this.editingTarget;
|
||||
for (var i = 0; i < sprite.clones.length; i++) {
|
||||
var clone = sprite.clones[i];
|
||||
this.runtime.stopForTarget(sprite.clones[i]);
|
||||
this.runtime.disposeTarget(sprite.clones[i]);
|
||||
// Ensure editing target is switched if we are deleting it.
|
||||
if (clone === currentEditingTarget) {
|
||||
this.setEditingTarget(this.runtime.targets[0].id);
|
||||
}
|
||||
}
|
||||
// Sprite object should be deleted by GC.
|
||||
this.emitTargetsUpdate();
|
||||
} else {
|
||||
throw new Error('No target with the provided id.');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Temporary way to make an empty project, in case the desired project
|
||||
* cannot be loaded from the online server.
|
||||
|
|
|
@ -397,6 +397,14 @@ RenderedTarget.prototype.getName = function () {
|
|||
return this.sprite.name;
|
||||
};
|
||||
|
||||
/**
|
||||
* Return whether this rendered target is a sprite (not a clone, not the stage).
|
||||
* @return {boolean} True if not a clone and not the stage.
|
||||
*/
|
||||
RenderedTarget.prototype.isSprite = function () {
|
||||
return !this.isStage && this.isOriginal;
|
||||
};
|
||||
|
||||
/**
|
||||
* Return the rendered target's tight bounding box.
|
||||
* Includes top, left, bottom, right attributes in Scratch coordinates.
|
||||
|
|
|
@ -35,7 +35,7 @@ var Sprite = function (blocks, runtime) {
|
|||
this.costumes = [];
|
||||
/**
|
||||
* List of clones for this sprite, including the original.
|
||||
* @type {Array.<!Clone>}
|
||||
* @type {Array.<!RenderedTarget>}
|
||||
*/
|
||||
this.clones = [];
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue