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
Closed
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
73 changes: 73 additions & 0 deletions examples/core_input_mouse.c
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;
}
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<script>
const wasmPaths = {
"tsoding": ["tsoding_ball", "tsoding_snake",],
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel",],
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel", "core_input_mouse",],
"shapes": ["shapes_colors_palette"],
"text": ["text_writing_anim"],
"textures": ["textures_logo_raylib"],
Expand Down
5 changes: 5 additions & 0 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ Example examples[] = {
.bin_path = "./build/core_input_keys",
.wasm_path = "./wasm/core_input_keys.wasm",
},
{
.src_path = "./examples/core_input_mouse.c",
.bin_path = "./build/core_input_mouse",
.wasm_path = "./wasm/core_input_mouse.wasm",
},
{
.src_path = "./examples/shapes_colors_palette.c",
.bin_path = "./build/shapes_colors_palette",
Expand Down
20 changes: 20 additions & 0 deletions raylib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

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.


this.wasm.instance.exports.main();
const next = (timestamp) => {
Expand Down Expand Up @@ -190,6 +196,10 @@ class RaylibJs {
IsKeyDown(key) {
return this.currentPressedKeyState.has(key);
}

IsMouseButtonPressed(button){
return this.currentMouseButton == raylibMOuseButtonMapping[button];
}
GetMouseWheelMove() {
return this.currentMouseWheelMoveState;
}
Expand Down Expand Up @@ -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
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


const glfwKeyMapping = {
"Space": 32,
"Quote": 39,
Expand Down
Binary file modified wasm/core_basic_screen_manager.wasm
Binary file not shown.
Binary file modified wasm/core_basic_window.wasm
Binary file not shown.
Binary file modified wasm/core_input_keys.wasm
Binary file not shown.
Binary file added wasm/core_input_mouse.wasm
Binary file not shown.
Binary file modified wasm/core_input_mouse_wheel.wasm
Binary file not shown.
Binary file modified wasm/shapes_colors_palette.wasm
Binary file not shown.
Binary file modified wasm/text_writing_anim.wasm
Binary file not shown.
Binary file modified wasm/tsoding_ball.wasm
Binary file not shown.
Binary file modified wasm/tsoding_snake.wasm
Binary file not shown.