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

Added core_input_mouse.c example #41

Closed
wants to merge 2 commits into from

Conversation

hiranyey
Copy link

@hiranyey hiranyey commented Feb 19, 2024

Just a small change to make core_input_mouse.c working, but the mapping between raylib mouse constants and browser mouse constants are different.
For me, left button is showing correct color but right and middle button are swapped.

This patch implements IsMouseButtonPressed
and i cant test out MOUSE_BUTTON_SIDE,MOUSE_BUTTON_EXTRA,MOUSE_BUTTON_FORWARD,MOUSE_BUTTON_BACK as i dont have those mouse buttons in any of my mouses

Tested other pages too and i can conclude that this patch wont create a regression in other pages.

@ZakChrom
Copy link

It doesn't seem like the MOUSE_BUTTON_FORWARD and MOUSE_BUTTON_BACK buttons work in the browser.
I found this Stack Overflow question that might fix this tho.

Comment on lines +362 to +370
const raylibMOuseButtonMapping = {
0: 0,
1: 2,
2: 1,
3: 3,
4: 4,
5: 5,
6: 6,
}
Copy link

Choose a reason for hiding this comment

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

Such naive mapping is not needed imo, return this.currentMouseButton == button would suffice.

Copy link
Author

@hiranyey hiranyey Feb 21, 2024

Choose a reason for hiding this comment

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

It's needed because in later examples, it is important to have correct mapping for the right and middle click.
My previous commit just used return this.currentMouseButton == button but had to change it to accommodate mouse constant swap, there can also be swaps in 3,4,5,6 mouse buttons but I don't have a way to test them out

Choose a reason for hiding this comment

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

@hiranyey is correct here. The mappings are not 1:1. See the differences below...

typedef enum {
    MOUSE_BUTTON_LEFT    = 0,       // Mouse button left
    MOUSE_BUTTON_RIGHT   = 1,       // Mouse button right
    MOUSE_BUTTON_MIDDLE  = 2,       // Mouse button middle (pressed wheel)
    MOUSE_BUTTON_SIDE    = 3,       // Mouse button side (advanced mouse device)
    MOUSE_BUTTON_EXTRA   = 4,       // Mouse button extra (advanced mouse device)
    MOUSE_BUTTON_FORWARD = 5,       // Mouse button forward (advanced mouse device)
    MOUSE_BUTTON_BACK    = 6,       // Mouse button back (advanced mouse device)
} MouseButton;

Web API: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button#value

0: Main button pressed, usually the left button or the un-initialized state
1: Auxiliary button pressed, usually the wheel button or the middle button (if present)
2: Secondary button pressed, usually the right button
3: Fourth button, typically the Browser Back button
4: Fifth button, typically the Browser Forward button

window.addEventListener("keydown", keyDown);
window.addEventListener("keyup", keyUp);
window.addEventListener("wheel", wheelMove);
window.addEventListener("mousemove", mouseMove);
window.addEventListener("mousedown", mouseClick);

Choose a reason for hiding this comment

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

I understand you're just adding IsMouseButtonPressed() right now, but could we also implement the other mouse functions here too?

IsMouseButtonPressed
IsMouseButtonDown
IsMouseButtonReleased
IsMouseButtonUp

The pattern is already implemented with keys, so could do the same here.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, this is easy to do,
I will check if there are examples in Raylib using these new functions and include them in this PR

Choose a reason for hiding this comment

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

In #52 , I wasn't able to find examples that used the related keyboard functions, so I wouldn't be surprised if examples don't exist.

@hiranyey hiranyey closed this by deleting the head repository Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants