mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Return function to restore a costume when it gets deleted.
This commit is contained in:
parent
6f9da87abe
commit
e5ca208a41
3 changed files with 23 additions and 6 deletions
|
@ -534,12 +534,19 @@ class RenderedTarget extends Target {
|
||||||
/**
|
/**
|
||||||
* Delete a costume by index.
|
* Delete a costume by index.
|
||||||
* @param {number} index Costume index to be deleted
|
* @param {number} index Costume index to be deleted
|
||||||
|
* @return {?object} The costume that was deleted or null
|
||||||
|
* if the index was out of bounds of the costumes list or
|
||||||
|
* this target only has one costume.
|
||||||
*/
|
*/
|
||||||
deleteCostume (index) {
|
deleteCostume (index) {
|
||||||
const originalCostumeCount = this.sprite.costumes.length;
|
const originalCostumeCount = this.sprite.costumes.length;
|
||||||
if (originalCostumeCount === 1) return;
|
if (originalCostumeCount === 1) return null;
|
||||||
|
|
||||||
this.sprite.deleteCostumeAt(index);
|
if (index < 0 || index >= originalCostumeCount) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const deletedCostume = this.sprite.deleteCostumeAt(index);
|
||||||
|
|
||||||
if (index === this.currentCostume && index === originalCostumeCount - 1) {
|
if (index === this.currentCostume && index === originalCostumeCount - 1) {
|
||||||
this.setCostume(index - 1);
|
this.setCostume(index - 1);
|
||||||
|
@ -550,6 +557,7 @@ class RenderedTarget extends Target {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.runtime.requestTargetsUpdate(this);
|
this.runtime.requestTargetsUpdate(this);
|
||||||
|
return deletedCostume;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -93,11 +93,10 @@ class Sprite {
|
||||||
/**
|
/**
|
||||||
* Delete a costume by index.
|
* Delete a costume by index.
|
||||||
* @param {number} index Costume index to be deleted
|
* @param {number} index Costume index to be deleted
|
||||||
|
* @return {?object} The deleted costume
|
||||||
*/
|
*/
|
||||||
deleteCostumeAt (index) {
|
deleteCostumeAt (index) {
|
||||||
this.costumes_ = this.costumes_
|
return this.costumes.splice(index, 1)[0];
|
||||||
.slice(0, index)
|
|
||||||
.concat(this.costumes_.slice(index + 1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -589,9 +589,19 @@ class VirtualMachine extends EventEmitter {
|
||||||
/**
|
/**
|
||||||
* Delete a costume from the current editing target.
|
* Delete a costume from the current editing target.
|
||||||
* @param {int} costumeIndex - the index of the costume to be removed.
|
* @param {int} costumeIndex - the index of the costume to be removed.
|
||||||
|
* @return {?function} A function to restore the deleted costume, or null,
|
||||||
|
* if no costume was deleted.
|
||||||
*/
|
*/
|
||||||
deleteCostume (costumeIndex) {
|
deleteCostume (costumeIndex) {
|
||||||
this.editingTarget.deleteCostume(costumeIndex);
|
const deletedCostume = this.editingTarget.deleteCostume(costumeIndex);
|
||||||
|
if (deletedCostume) {
|
||||||
|
const target = this.editingTarget;
|
||||||
|
return () => {
|
||||||
|
target.addCostume(deletedCostume);
|
||||||
|
this.emitTargetsUpdate();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue