Merge pull request #671 from fsih/setGetSvg

Add methods to retrieve and update SVGs
This commit is contained in:
DD Liu 2017-08-31 13:43:41 -04:00 committed by GitHub
commit bf97383b42

View file

@ -361,6 +361,40 @@ class VirtualMachine extends EventEmitter {
this.editingTarget.deleteSound(soundIndex);
}
/**
* Get an SVG string from storage.
* @param {int} costumeIndex - the index of the costume 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.