Skip to content

Commit

Permalink
Merge pull request #1075 from kchadha/load-costume-no-metadata
Browse files Browse the repository at this point in the history
Load costume without metadata
  • Loading branch information
kchadha committed Apr 23, 2018
2 parents eeab069 + af7ed61 commit cde801b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
38 changes: 29 additions & 9 deletions src/import/load-costume.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,31 @@ 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;
}
const AssetType = runtime.storage.AssetType;
const rotationCenter = [
costume.rotationCenterX / costume.bitmapResolution,
costume.rotationCenterY / costume.bitmapResolution
];
let rotationCenter;
if (costume.rotationCenterX && costume.rotationCenterY && costume.bitmapResolution) {
rotationCenter = [
costume.rotationCenterX / costume.bitmapResolution,
costume.rotationCenterY / costume.bitmapResolution
];
}
if (costumeAsset.assetType === AssetType.ImageVector) {
costume.skinId = runtime.renderer.createSVGSkin(costumeAsset.decodeText(), rotationCenter);
costume.size = runtime.renderer.getSkinSize(costume.skinId);
// createSVGSkin does the right thing if rotationCenter isn't provided, so it's okay if it's
// undefined here
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;
}

Expand All @@ -50,8 +63,15 @@ const loadCostumeFromAsset = function (costume, costumeAsset, runtime) {
imageElement.addEventListener('load', onLoad);
imageElement.src = costumeAsset.encodeDataURI();
}).then(imageElement => {
costume.skinId = runtime.renderer.createBitmapSkin(imageElement, costume.bitmapResolution, rotationCenter);
costume.size = runtime.renderer.getSkinSize(costume.skinId);
// createBitmapSkin does the right thing if costume.bitmapResolution or rotationCenter are undefined...
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] * 2;
costume.rotationCenterY = rotationCenter[1] * 2;
}
return costume;
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/sprites/rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class RenderedTarget extends Target {
typeof costume.rotationCenterX !== 'undefined' &&
typeof costume.rotationCenterY !== 'undefined'
) {
const scale = costume.bitmapResolution || 1;
const scale = costume.bitmapResolution || 2;
drawableProperties.rotationCenter = [
costume.rotationCenterX / scale,
costume.rotationCenterY / scale
Expand Down Expand Up @@ -641,7 +641,7 @@ class RenderedTarget extends Target {
if (this.renderer) {
const renderedDirectionScale = this._getRenderedDirectionAndScale();
const costume = this.getCostumes()[this.currentCostume];
const bitmapResolution = costume.bitmapResolution || 1;
const bitmapResolution = costume.bitmapResolution || 2;
const props = {
position: [this.x, this.y],
direction: renderedDirectionScale.direction,
Expand Down

0 comments on commit cde801b

Please sign in to comment.