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 2 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
136 changes: 136 additions & 0 deletions extensions/legobrainbiker/MoreMouseControls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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+PHBhdGggZD0iTTE2Mi44NjU0NCwxNjcuNDYzMDljMCwtMi4wNDY3NCAxLjc0NjIsLTMuNzA1OTQgMy45MDAyNCwtMy43MDU5N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g5MDEgLTE3Ljk1NzAzLC01LjQ2NTE4IC0yMS41ODI1MiwtNi41Njg1OWMtMi4xNzUxNCwtMC42NjIgLTMuNDAzNTEsMS4xMjY3MyAtMi4zNDkwMywzLjMwMzczeiIgZmlsbD0iI2EzYTNhMyIgc3Ryb2tlLXdpZHRoPSIzLjUiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIvPjwvZz48L2c+PC9zdmc+'
class moreMouseControls {
constructor() {
this.mouseX = 0;
this.mouseY = 0;
this.scroll = 0;
this.buttons = [];
document.addEventListener("mousewheel", (e) => {
this.mouseWheelDelta = e.deltaY;
Scratch.vm.runtime.startHats(
"legobrainbikerMoreMouseControls_onScroll"
);
});
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;
});
}
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: "dissableContextMenu",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("disable context menu"),
},
{
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: "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 }) {
console.log(button);
console.log(Scratch.Cast.toNumber(button));
console.log(this.buttons[Scratch.Cast.toNumber(button)]);
return this.buttons[Scratch.Cast.toNumber(button)] || false;
}
dissableContextMenu() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the way you implement this breaks some addons, and you should allow for reenabling the context menu and scrolling

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I fix it? which addons does it break? all of them with 'contex menu' or right click in their descriptions seem to work (although I didn't test them that much).

document.addEventListener("contextmenu", (e) => {
e.preventDefault();
});
}
scrollamount() {
return this.mouseWheelDelta || 0;
}
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