diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 07ce65e1..07b0a505 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -340,20 +340,6 @@ class VirtualMachine extends EventEmitter { return null; } - /** - * Get an SVG string from the renderer. - * @param {int} costumeIndex - the index of the sound to be got. - * @return {string} the costume's SVG string, or null if it's not an SVG costume. - */ - getCostumeSVG (costumeIndex) { - const id = this.editingTarget.sprite.costumes[costumeIndex].assetId; - if (id && this.runtime && this.runtime.storage && - this.runtime.storage.get(id).dataFormat === 'svg') { - return this.runtime.storage.get(id).decodeText(); - } - return null; - } - /** * Update a sound buffer. * @param {int} soundIndex - the index of the sound to be updated. @@ -375,6 +361,40 @@ class VirtualMachine extends EventEmitter { this.editingTarget.deleteSound(soundIndex); } + /** + * Get an SVG string from the renderer. + * @param {int} costumeIndex - the index of the sound to be got. + * @return {string} the costume's SVG string, or null if it's not an SVG costume. + */ + getCostumeSvg (costumeIndex) { + const id = this.editingTarget.sprite.costumes[costumeIndex].assetId; + if (id && this.runtime && this.runtime.storage && + this.runtime.storage.get(id).dataFormat === 'svg') { + return this.runtime.storage.get(id).decodeText(); + } + return null; + } + + /** + * Update a costume with the given SVG + * @param {int} costumeIndex - the index of the costume to be updated. + * @param {string} svg - new SVG for the renderer. + */ + updateSvg (costumeIndex, svg) { + const costume = this.editingTarget.sprite.costumes[costumeIndex]; + if (costume && this.runtime && this.runtime.renderer) { + const rotationCenter = [ + costume.rotationCenterX / costume.bitmapResolution, + costume.rotationCenterY / costume.bitmapResolution + ]; + + this.runtime.renderer.updateSVGSkin(costume.skinId, svg, rotationCenter); + } + // TODO: Also update storage in addition to renderer. Without storage, if you switch + // costumes and switch back, you will lose your changes in the paint editor. + // TODO: emitTargetsUpdate if we need to update the storage ID on the updated costume; + } + /** * Add a backdrop to the stage. * @param {string} md5ext - the MD5 and extension of the backdrop to be loaded.