Skip to content

more mouse controls #2104

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 5 commits into
base: master
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
1 change: 1 addition & 0 deletions extensions/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"files",
"pointerlock",
"cursor",
"legobrainbiker/MoreMouseControls",
"runtime-options",
"fetch",
"text",
Expand Down
185 changes: 185 additions & 0 deletions extensions/legobrainbiker/MoreMouseControls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Name: More Mouse Controls
// ID: legobrainbikerMoreMouseControls
// Description: Adds more options to detect properties of the cursor, including its position, clicking, and scrolling.
// By: legobrainbiker <https://scratch.mit.edu/users/legobrainbiker/>
// License: MPL-2.0
(function (Scratch) {
"use strict";
const iconURI =
"data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSIzNi4zOTI4MSIgaGVpZ2h0PSIzNS40MjY1OSIgdmlld0JveD0iMCwwLDM2LjM5MjgxLDM1LjQyNjU5Ij48ZyB0cmFuc2Zvcm09InRyYW5zbGF0ZSgtMTU2Ljg4NTQxLC0xNTcuNjM4NDcpIj48ZyBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS1taXRlcmxpbWl0PSIxMCI+PHBhdGggZD0iTTE2Mi44NjU0NCwxNjcuNDYzMDljMCwtMi4wNDY3NCAxLjc0NjIsLTMuNzA1OTQgMy45MDAyNCwtMy43MDU5NG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5MDEgLTE3Ljk1NzAzLC01LjQ2NTE4IC0yMS41ODI1MiwtNi41Njg1OWMtMi4xNzUxNCwtMC42NjIgLTMuNDAzNTEsMS4xMjY3MyAtMi4zNDkwMywzLjMwMzczeiIgZmlsbD0iI2EzYTNhMyIgc3Ryb2tlLXdpZHRoPSIzLjUiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIvPjwvZz48L2c+PC9zdmc+";
class moreMouseControls {
constructor() {
this.mouseX = 0;
this.mouseY = 0;
this.mouseWheelDelta = 0;
this.mouseWheelDeltaX = 0;
this.mouseZoomDelta = 0;
this.buttons = [];
this.contextMenuDissabled = false;
document.addEventListener("contextmenu", (e) => {
if (this.contextMenuDissabled) {
e.preventDefault();
}
});
this.scrollDissabled = false;
document.addEventListener(
"wheel",
(e) => {
if (this.scrollDissabled) {
e.preventDefault();
}
if (e.ctrlKey) {
this.mouseZoomDelta = e.deltaY;
this.mouseWheelDelta = 0;
this.mouseWheelDeltaX = 0;
} else {
this.mouseZoomDelta = 0;
this.mouseWheelDelta = e.deltaY;
this.mouseWheelDeltaX = e.deltaX;
}
Scratch.vm.runtime.startHats(
"legobrainbikerMoreMouseControls_onScroll"
);
},
{ passive: false }
);
document.addEventListener("mouseup", (e) => {
this.buttons[e.button] = false;
});
document.addEventListener("mousedown", (e) => {
this.buttons[e.button] = true;
});
document.addEventListener("mousemove", (e) => {
this.mouseX = e.clientX;
this.mouseY = e.clientY;
});
document.addEventListener("mouseleave", (e) => {
this.mouseX = NaN;
this.mouseY = NaN;
// this could be problomatic for some projects. it's not great to add another edge case
});
}
getInfo() {
return {
id: "legobrainbikerMoreMouseControls",
name: Scratch.translate("More Mouse Controls"),
menuIconURI: iconURI,
blockIconURI: iconURI,
color1: "#99bbbb",
color2: "#88bbbb",
color3: "#88bbbb",
blocks: [
{
opcode: "onMouseUp",
blockType: Scratch.BlockType.HAT,
text: Scratch.translate("when mouse button [button] is released"),
isEdgeActivated: true,
arguments: {
button: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "0",
},
},
},
{
opcode: "onMouseDown",
blockType: Scratch.BlockType.HAT,
text: Scratch.translate("when mouse button [button] is pressed"),
isEdgeActivated: true,
arguments: {
button: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "0",
},
},
},
{
opcode: "buttonPressed",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("mouse button [button] is pressed"),
arguments: {
button: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "0",
},
},
},
{
opcode: "toggleContextMenu",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("toggle context menu"),
},
{
opcode: "toggleScroll",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("toggle scrolling and zooming"),
},
{
opcode: "onScroll",
blockType: Scratch.BlockType.EVENT,
text: Scratch.translate("when scrolling"),
isEdgeActivated: false,
},
{
opcode: "scrollAmount",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("scroll amount"),
},
{
opcode: "horozontalScrollAmount",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("horozontal scroll amount"),
},
{
opcode: "zoomAmount",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("zoom amount"),
},
{
opcode: "mouseGlobalX",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("mouse global x position"),
},
{
opcode: "mouseGlobalY",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("mouse global y position"),
},
],
};
}
onMouseUp(args) {
return !this.buttonPressed(args);
}
onMouseDown(args) {
return this.buttonPressed(args);
}
buttonPressed({ button }) {
return this.buttons[Scratch.Cast.toNumber(button)] || false;
}
toggleContextMenu() {
this.contextMenuDissabled = !this.contextMenuDissabled;
}
toggleScroll() {
this.scrollDissabled = !this.scrollDissabled;
}
scrollAmount() {
return -this.mouseWheelDelta;
}
horozontalScrollAmount() {
return this.mouseWheelDeltaX;
}
zoomAmount() {
return -this.mouseZoomDelta;
}
mouseGlobalX() {
const canvas = Scratch.renderer.canvas.getBoundingClientRect();
return this.mouseX - canvas.x - canvas.width / 2;
}
mouseGlobalY() {
const canvas = Scratch.renderer.canvas.getBoundingClientRect();
return -(this.mouseY - canvas.y - canvas.height / 2);
}
}
Scratch.extensions.register(new moreMouseControls());
})(Scratch);
1 change: 1 addition & 0 deletions images/legobrainbiker/MoreMouseControls.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading