Skip to content

Commit

Permalink
runtime-options: add event block for when turbo mode, etc. changed (#…
Browse files Browse the repository at this point in the history
…1210)

Co-authored-by: Muffin <[email protected]>
  • Loading branch information
SharkPool-SP and GarboMuffin authored Dec 21, 2023
1 parent 340dd46 commit 4a01420
Showing 1 changed file with 106 additions and 2 deletions.
108 changes: 106 additions & 2 deletions extensions/runtime-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,55 @@
const REMOVE_FENCING = "remove fencing";
const REMOVE_MISC_LIMITS = "remove misc limits";
const HIGH_QUALITY_PEN = "high quality pen";
const FRAMERATE = "framerate";
const CLONE_LIMIT = "clone limit";
const STAGE_SIZE = "stage size";
const USERNAME = "username";

/** @param {string} what */
const emitChanged = (what) =>
Scratch.vm.runtime.startHats("runtimeoptions_whenChange", {
WHAT: what,
});

/**
* @template T
* @param {T} obj
* @returns {T}
*/
const shallowCopy = (obj) => Object.assign({}, obj);

let previousRuntimeOptions = shallowCopy(Scratch.vm.runtime.runtimeOptions);

Scratch.vm.on("TURBO_MODE_OFF", () => emitChanged(TURBO_MODE));
Scratch.vm.on("TURBO_MODE_ON", () => emitChanged(TURBO_MODE));
Scratch.vm.on("INTERPOLATION_CHANGED", () => emitChanged(INTERPOLATION));
Scratch.vm.on("RUNTIME_OPTIONS_CHANGED", (newOptions) => {
if (newOptions.fencing !== previousRuntimeOptions.fencing) {
emitChanged(REMOVE_FENCING);
}
if (newOptions.miscLimits !== previousRuntimeOptions.miscLimits) {
emitChanged(REMOVE_MISC_LIMITS);
}
if (newOptions.maxClones !== previousRuntimeOptions.maxClones) {
emitChanged(CLONE_LIMIT);
}
previousRuntimeOptions = shallowCopy(newOptions);
});
Scratch.vm.renderer.on("UseHighQualityRenderChanged", () =>
emitChanged(HIGH_QUALITY_PEN)
);
Scratch.vm.on("FRAMERATE_CHANGED", () => emitChanged(FRAMERATE));
Scratch.vm.on("STAGE_SIZE_CHANGED", () => emitChanged(STAGE_SIZE));

const originalPostData = Scratch.vm.runtime.ioDevices.userData.postData;
Scratch.vm.runtime.ioDevices.userData.postData = function (data) {
const newUsername = data.username !== this._username;
originalPostData.call(this, data);
if (newUsername) {
emitChanged(USERNAME);
}
};

class RuntimeOptions {
getInfo() {
Expand Down Expand Up @@ -154,6 +203,18 @@
},
},
},

"---",

{
opcode: "whenChange",
blockType: Scratch.BlockType.EVENT,
text: "when [WHAT] changed",
isEdgeActivated: false,
arguments: {
WHAT: { type: Scratch.ArgumentType.STRING, menu: "changeable" },
},
},
],
menus: {
thing: {
Expand Down Expand Up @@ -182,6 +243,48 @@
],
},

changeable: {
acceptReporters: false,
items: [
{
text: Scratch.translate("turbo mode"),
value: TURBO_MODE,
},
{
text: Scratch.translate("interpolation"),
value: INTERPOLATION,
},
{
text: Scratch.translate("remove fencing"),
value: REMOVE_FENCING,
},
{
text: Scratch.translate("remove misc limits"),
value: REMOVE_MISC_LIMITS,
},
{
text: Scratch.translate("high quality pen"),
value: HIGH_QUALITY_PEN,
},
{
text: Scratch.translate("framerate"),
value: FRAMERATE,
},
{
text: Scratch.translate("clone limit"),
value: CLONE_LIMIT,
},
{
text: Scratch.translate("stage size"),
value: STAGE_SIZE,
},
{
text: Scratch.translate("username"),
value: USERNAME,
},
],
},

enabled: {
acceptReporters: true,
items: [
Expand Down Expand Up @@ -300,8 +403,9 @@
}

setUsername({ username }) {
Scratch.vm.runtime.ioDevices.userData._username =
Scratch.Cast.toString(username);
Scratch.vm.postIOData("userData", {
username: Scratch.Cast.toString(username),
});
}

greenFlag() {
Expand Down

0 comments on commit 4a01420

Please sign in to comment.