Skip to content
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

Addon 60fps #15

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
scratch-gui modified for use in [TurboWarp](https://turbowarp.org/) then modified for use in [PenguinMod](https://penguinmod.github.io/penguinmod.github.io/) 😀


[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/PenguinMod/penguinmod.github.io/)
## Setup

Expand Down
1 change: 1 addition & 0 deletions src/addons/addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const addons = [

const newAddons = [
'vol-slider',
'60fps',
'number-pad',
'rename-broadcasts',
'sprite-properties',
Expand Down
46 changes: 46 additions & 0 deletions src/addons/addons/60fps/addon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "60FPS project player mode",
"description": "Alt+Click the green flag to toggle 60FPS.",
"info": [
{
"type": "notice",
"text": "Most projects will not behave properly when running at 60FPS, because increasing the frame rate also increases the speed at which scripts run. This should only be used on projects that support 60FPS.",
"id": "projectRunsFasterNotice"
}
],
"credits": [
{
"name": "Jeffalo",
"link": "https://scratch.mit.edu/users/Jeffalo/"
},
{
"name": "TheColaber",
"link": "https://scratch.mit.edu/users/TheColaber/"
}
{
"name": "kokofixcomputers",
"link": "https://scratch.mit.edu/users/kokofixcomputers/"
}
],
"dynamicEnable": true,
"dynamicDisable": true,
"userscripts": [
{
"url": "userscript.js",
"matches": ["projects", "projectEmbeds"]
}
],
"settings": [
{
"name": "Alt+GreenFlag FPS",
"id": "framerate",
"type": "integer",
"min": 31,
"max": 240,
"default": 60
}
],
"tags": ["editor", "projectPlayer", "featured"],
"versionAdded": "1.1.0",
"enabledByDefault": false
}
14 changes: 14 additions & 0 deletions src/addons/addons/60fps/svg/fast-flag.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions src/addons/addons/60fps/userscript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
export default async function ({ addon, console }) {
// TODO: test whether e.altKey is true in chromebooks when alt+clicking.
// If so, no timeout needed, similar to mute-project addon.

let global_fps = 30;
const vm = addon.tab.traps.vm;
let mode = false;
let monitorUpdateFixed = false;

const fastFlag = addon.self.dir + "/svg/fast-flag.svg";
let vanillaFlag = null;

while (true) {
let button = await addon.tab.waitForElement("[class^='green-flag_green-flag']", {
markAsSeen: true,
reduxEvents: ["scratch-gui/mode/SET_PLAYER", "fontsLoaded/SET_FONTS_LOADED", "scratch-gui/locales/SELECT_LOCALE"],
});

const updateFlag = () => {
if (!vanillaFlag) vanillaFlag = button.src;
button.src = mode ? fastFlag : vanillaFlag;
};

const changeMode = (_mode = !mode) => {
mode = _mode;
if (mode) {
setFPS(addon.settings.get("framerate"));

// monitor updates are throttled by default
// https://github.com/scratchfoundation/scratch-gui/blob/ba76db7/src/reducers/monitors.js
if (!monitorUpdateFixed) {
const originalListener = vm.listeners("MONITORS_UPDATE").find((f) => f.name === "onMonitorsUpdate");
if (originalListener) vm.removeListener("MONITORS_UPDATE", originalListener);
vm.on("MONITORS_UPDATE", (monitors) =>
addon.tab.redux.dispatch({
type: "scratch-gui/monitors/UPDATE_MONITORS",
monitors,
})
);
monitorUpdateFixed = true;
}
} else setFPS(30);
updateFlag();
};
const flagListener = (e) => {
if (addon.self.disabled) return;
const isAltClick = e.type === "click" && e.altKey;
const isChromebookAltClick = navigator.userAgent.includes("CrOS") && e.type === "contextmenu";
if (isAltClick || isChromebookAltClick) {
e.cancelBubble = true;
e.preventDefault();
changeMode();
}
};
button.addEventListener("click", flagListener);
button.addEventListener("contextmenu", flagListener);

const setFPS = (fps) => {
global_fps = addon.self.disabled ? 30 : fps;

clearInterval(vm.runtime._steppingInterval);
vm.runtime._steppingInterval = null;
vm.runtime.start();
};
addon.settings.addEventListener("change", () => {
if (vm.runtime._steppingInterval) {
setFPS(addon.settings.get("framerate"));
}
});
addon.self.addEventListener("disabled", () => changeMode(false));
vm.runtime.start = function () {
if (this._steppingInterval) return;
let interval = 1000 / global_fps;
this.currentStepTime = interval;
this._steppingInterval = setInterval(() => {
this._step();
}, interval);
this.emit("RUNTIME_STARTED");
};
updateFlag();
}
}