-
Notifications
You must be signed in to change notification settings - Fork 30
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/******************************************************************************************* | ||
* | ||
* raylib [core] example - Mouse input | ||
* | ||
* Example originally created with raylib 1.0, last time updated with raylib 4.0 | ||
* | ||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, | ||
* BSD-like license that allows static linking with closed source software | ||
* | ||
* Copyright (c) 2014-2024 Ramon Santamaria (@raysan5) | ||
* | ||
********************************************************************************************/ | ||
|
||
#include "raylib.h" | ||
void raylib_js_set_entry(void (*entry)(void)); | ||
|
||
Color ballColor = DARKBLUE; | ||
Vector2 ballPosition = { -100.0f, -100.0f }; | ||
|
||
void GameFrame(){ | ||
ballPosition = GetMousePosition(); | ||
|
||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) ballColor = MAROON; | ||
else if (IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE)) ballColor = LIME; | ||
else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) ballColor = DARKBLUE; | ||
else if (IsMouseButtonPressed(MOUSE_BUTTON_SIDE)) ballColor = PURPLE; | ||
else if (IsMouseButtonPressed(MOUSE_BUTTON_EXTRA)) ballColor = YELLOW; | ||
else if (IsMouseButtonPressed(MOUSE_BUTTON_FORWARD)) ballColor = ORANGE; | ||
else if (IsMouseButtonPressed(MOUSE_BUTTON_BACK)) ballColor = BEIGE; | ||
//---------------------------------------------------------------------------------- | ||
|
||
// Draw | ||
//---------------------------------------------------------------------------------- | ||
BeginDrawing(); | ||
|
||
ClearBackground(RAYWHITE); | ||
|
||
DrawCircleV(ballPosition, 40, ballColor); | ||
|
||
DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY); | ||
|
||
EndDrawing(); | ||
} | ||
//------------------------------------------------------------------------------------ | ||
// Program main entry point | ||
//------------------------------------------------------------------------------------ | ||
int main(void) | ||
{ | ||
// Initialization | ||
//-------------------------------------------------------------------------------------- | ||
const int screenWidth = 800; | ||
const int screenHeight = 450; | ||
|
||
InitWindow(screenWidth, screenHeight, "raylib [core] example - mouse input"); | ||
|
||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second | ||
//--------------------------------------------------------------------------------------- | ||
#ifdef PLATFORM_WEB | ||
raylib_js_set_entry(GameFrame); | ||
#else | ||
// Main game loop | ||
while (!WindowShouldClose()) // Detect window close button or ESC key | ||
{ | ||
GameFrame(); | ||
} | ||
|
||
// De-Initialization | ||
//-------------------------------------------------------------------------------------- | ||
CloseWindow(); // Close window and OpenGL context | ||
//-------------------------------------------------------------------------------------- | ||
#endif | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ class RaylibJs { | |
this.currentPressedKeyState = new Set(); | ||
this.currentMouseWheelMoveState = 0; | ||
this.currentMousePosition = {x: 0, y: 0}; | ||
this.currentMouseButton = undefined; | ||
this.images = []; | ||
this.quit = false; | ||
} | ||
|
@@ -82,10 +83,15 @@ class RaylibJs { | |
const mouseMove = (e) => { | ||
this.currentMousePosition = {x: e.clientX, y: e.clientY}; | ||
}; | ||
const mouseClick = (e) => { | ||
this.currentMouseButton = e.button; | ||
} | ||
|
||
window.addEventListener("keydown", keyDown); | ||
window.addEventListener("keyup", keyUp); | ||
window.addEventListener("wheel", wheelMove); | ||
window.addEventListener("mousemove", mouseMove); | ||
window.addEventListener("mousedown", mouseClick); | ||
|
||
this.wasm.instance.exports.main(); | ||
const next = (timestamp) => { | ||
|
@@ -190,6 +196,10 @@ class RaylibJs { | |
IsKeyDown(key) { | ||
return this.currentPressedKeyState.has(key); | ||
} | ||
|
||
IsMouseButtonPressed(button){ | ||
return this.currentMouseButton == raylibMOuseButtonMapping[button]; | ||
} | ||
GetMouseWheelMove() { | ||
return this.currentMouseWheelMoveState; | ||
} | ||
|
@@ -349,6 +359,16 @@ class RaylibJs { | |
} | ||
} | ||
|
||
const raylibMOuseButtonMapping = { | ||
0: 0, | ||
1: 2, | ||
2: 1, | ||
3: 3, | ||
4: 4, | ||
5: 5, | ||
6: 6, | ||
} | ||
Comment on lines
+362
to
+370
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Such naive mapping is not needed imo, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
|
||
const glfwKeyMapping = { | ||
"Space": 32, | ||
"Quote": 39, | ||
|
There was a problem hiding this comment.
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?
The pattern is already implemented with keys, so could do the same here.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.