Skip to content

Commit

Permalink
Lily/LooksPlus: Optimize restore content block (#1638)
Browse files Browse the repository at this point in the history
- restore content block: do nothing if the costume wasn't changed
- remove unnecessary emitTargetsUpdate
- try/catch invalid SVGs
  • Loading branch information
GarboMuffin authored Aug 5, 2024
1 parent aee8630 commit 44ad6f2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
48 changes: 31 additions & 17 deletions extensions/Lily/LooksPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
return util.runtime.getSpriteTargetByName(nameString);
};

const renderer = Scratch.vm.runtime.renderer;

class LooksPlus {
getInfo() {
return {
Expand Down Expand Up @@ -380,7 +382,7 @@
const drawableID = target.drawableID;
const layerOrder = target.getLayerOrder();
const newLayer = args.LAYER - layerOrder;
target.renderer.setDrawableOrder(drawableID, newLayer, "sprite", true);
renderer.setDrawableOrder(drawableID, newLayer, "sprite", true);
}

spriteLayerNumber(args, util) {
Expand Down Expand Up @@ -442,7 +444,7 @@

snapshotStage(args, util) {
return new Promise((resolve) => {
Scratch.vm.runtime.renderer.requestSnapshot((uri) => {
renderer.requestSnapshot((uri) => {
resolve(uri);
});
});
Expand All @@ -467,15 +469,18 @@
const contentType = args.TYPE;
const content = args.CONTENT;
if (contentType === "SVG") {
Scratch.vm.runtime.renderer.updateSVGSkin(
costume.skinId,
Scratch.Cast.toString(content)
);
try {
renderer.updateSVGSkin(
costume.skinId,
Scratch.Cast.toString(content)
);
renderer._allSkins[costume.skinId].differsFromAsset = true;
} catch (e) {
console.error(e);
}
} else {
console.error("Options other than SVG are currently unavailable");
return;
}
Scratch.vm.emitTargetsUpdate();
}

restoreCostumeContent(args, util) {
Expand All @@ -490,21 +495,30 @@
return;
}

//This is here to ensure no changes are made to bitmap costumes, as changes are irreversible
//Check will be removed when it's possible to edit bitmap skins
// This is here to ensure no changes are made to bitmap costumes, as changes are irreversible
// Check will be removed when it's possible to edit bitmap skins
const format = costume.asset.assetType.runtimeFormat;
if (format !== "svg") {
console.error("Costume is not vector");
return;
}

const content = costume.asset.decodeText();
const rotationCenterX = costume.rotationCenterX;
const rotationCenterY = costume.rotationCenterY;
util.target.renderer.updateSVGSkin(costume.skinId, content, [
rotationCenterX,
rotationCenterY,
]);
if (!renderer._allSkins[costume.skinId].differsFromAsset) {
return;
}

try {
const content = costume.asset.decodeText();
const rotationCenterX = costume.rotationCenterX;
const rotationCenterY = costume.rotationCenterY;
renderer.updateSVGSkin(costume.skinId, content, [
rotationCenterX,
rotationCenterY,
]);
renderer._allSkins[costume.skinId].differsFromAsset = false;
} catch (e) {
console.error(e);
}
}

costumeContent(args, util) {
Expand Down
11 changes: 5 additions & 6 deletions extensions/Lily/lmsutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1356,14 +1356,13 @@

setSpriteSVG(args, util) {
try {
Scratch.vm.runtime.renderer.updateSVGSkin(
util.target.sprite.costumes[args.INPUTA - 1].skinId,
args.INPUTB
);
const skinId = util.target.sprite.costumes[args.INPUTA - 1].skinId;
const renderer = Scratch.vm.runtime.renderer;
renderer.updateSVGSkin(skinId, Scratch.Cast.toString(args.INPUTB));
renderer._allSkins[skinId].differsFromAsset = true;
} catch (error) {
return;
console.error(error);
}
Scratch.vm.emitTargetsUpdate();
}

alertBlock(args) {
Expand Down

0 comments on commit 44ad6f2

Please sign in to comment.