Use calculated rotation center from renderer when rotation center is not provided with the costume being loaded.

This commit is contained in:
Karishma Chadha 2018-04-20 00:04:08 -04:00
parent d6f28f2266
commit 54b52ba96c
2 changed files with 21 additions and 7 deletions

View file

@ -15,7 +15,8 @@ const log = require('../util/log');
*/
const loadCostumeFromAsset = function (costume, costumeAsset, runtime) {
costume.assetId = costumeAsset.assetId;
if (!runtime.renderer) {
const renderer = runtime.renderer;
if (!renderer) {
log.error('No rendering module present; cannot load costume: ', costume.name);
return costume;
}
@ -30,8 +31,15 @@ const loadCostumeFromAsset = function (costume, costumeAsset, runtime) {
if (costumeAsset.assetType === AssetType.ImageVector) {
// createSVGSkin does the right thing if rotationCenter isn't provided, so it's okay if it's
// undefined here
costume.skinId = runtime.renderer.createSVGSkin(costumeAsset.decodeText(), rotationCenter);
costume.size = runtime.renderer.getSkinSize(costume.skinId);
costume.skinId = renderer.createSVGSkin(costumeAsset.decodeText(), rotationCenter);
costume.size = renderer.getSkinSize(costume.skinId);
// Now we should have a rotationCenter even if we didn't before
if (!rotationCenter) {
rotationCenter = renderer.getSkinRotationCenter(costume.skinId);
costume.rotationCenterX = rotationCenter[0];
costume.rotationCenterY = rotationCenter[1];
}
return costume;
}
@ -56,8 +64,14 @@ const loadCostumeFromAsset = function (costume, costumeAsset, runtime) {
imageElement.src = costumeAsset.encodeDataURI();
}).then(imageElement => {
// createBitmapSkin does the right thing if costume.bitmapResolution or rotationCenter are undefined...
costume.skinId = runtime.renderer.createBitmapSkin(imageElement, costume.bitmapResolution, rotationCenter);
costume.size = runtime.renderer.getSkinSize(costume.skinId);
costume.skinId = renderer.createBitmapSkin(imageElement, costume.bitmapResolution, rotationCenter);
costume.size = renderer.getSkinSize(costume.skinId);
if (!rotationCenter) {
rotationCenter = renderer.getSkinRotationCenter(costume.skinId);
costume.rotationCenterX = rotationCenter[0];
costume.rotationCenterY = rotationCenter[1];
}
return costume;
});
};

View file

@ -405,8 +405,8 @@ class VirtualMachine extends EventEmitter {
* @returns {?Promise} - a promise that resolves when the costume has been added
*/
addCostume (md5ext, costumeObject) {
return loadCostume(md5ext, costumeObject, this.runtime).then(() => {
this.editingTarget.addCostume(costumeObject);
return loadCostume(md5ext, costumeObject, this.runtime).then(loadedCostume => {
this.editingTarget.addCostume(loadedCostume);
this.editingTarget.setCostume(
this.editingTarget.getCostumes().length - 1
);