Skip to content

Lily/LooksPlus -- 'set costume content' block must wait for content to finish loading #2075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions extensions/Lily/LooksPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@
return true;
};

/**
* @param {RenderWebGL.SVGSkin} svgSkin
* @returns {Promise<void>}
*/
const svgSkinFinishedLoading = (svgSkin) =>
new Promise((resolve) => {
if (svgSkin._svgImageLoaded) {
resolve();
return;
}

const handleEvent = () => {
cleanup();
resolve();
};
const cleanup = () => {
svgSkin._svgImage.removeEventListener("load", handleEvent);
svgSkin._svgImage.removeEventListener("error", handleEvent);
};

svgSkin._svgImage.addEventListener("load", handleEvent);
svgSkin._svgImage.addEventListener("error", handleEvent);
});

/**
* @param {VM.BlockUtility} util
* @param {unknown} targetName
Expand Down Expand Up @@ -176,6 +200,7 @@
opcode: "replaceCostumeContent",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("set [TYPE] for [COSTUME] to [CONTENT]"),
hideFromPalette: true, // needed for compatibility, also superceeded by skins
arguments: {
TYPE: {
type: Scratch.ArgumentType.STRING,
Expand All @@ -192,6 +217,21 @@
},
extensions: ["colours_looks"],
},
{
opcode: "replaceCostumeContentNew",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("set svg for [COSTUME] to [CONTENT]"),
arguments: {
COSTUME: {
type: Scratch.ArgumentType.COSTUME,
},
CONTENT: {
type: Scratch.ArgumentType.STRING,
defaultValue: "<svg />",
},
},
extensions: ["colours_looks"],
},
{
opcode: "restoreCostumeContent",
blockType: Scratch.BlockType.COMMAND,
Expand Down Expand Up @@ -483,6 +523,32 @@
}
}

async replaceCostumeContentNew(args, util) {
const costumeIndex = this.getCostumeInput(args.COSTUME, util.target);
const costume = util.target.sprite.costumes[costumeIndex];
if (!costume) {
console.error("Costume doesn't exist");
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
const format = costume.asset.assetType.runtimeFormat;
if (format !== "svg") {
console.error("Costume is not vector");
return;
}

const content = args.CONTENT;
try {
renderer.updateSVGSkin(costume.skinId, Scratch.Cast.toString(content));
renderer._allSkins[costume.skinId].differsFromAsset = true;
await svgSkinFinishedLoading(renderer._allSkins[costume.skinId]);
} catch (e) {
console.error(e);
}
}

restoreCostumeContent(args, util) {
const costumeIndex = this.getCostumeInput(args.COSTUME, util.target);
const costume = util.target.sprite.costumes[costumeIndex];
Expand Down
Loading