Paulo Laureano Because coding is fun!

Example "mouse input" in Rust


https://www.raylib.com/examples/web/core/loader.html?name=core_input_mouse

The original C example, edited for easier comparison:


#include "raylib.h"

int main(void)
{
const int screenWidth = 800;
const int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "raylib [core] example - mouse input");

Vector2 ballPosition = { -100.0f, -100.0f };
Color ballColor = DARKBLUE;

SetTargetFPS(60); // Set our game to run at 60 frames-per-second

while (!WindowShouldClose()) // Detect window close button or ESC key
{
ballPosition = GetMousePosition();

if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) ballColor = MAROON;
else if (IsMouseButtonPressed(MOUSE_MIDDLE_BUTTON)) ballColor = LIME;
else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) ballColor = DARKBLUE;

BeginDrawing();

ClearBackground(RAYWHITE);

DrawCircleV(ballPosition, 40, ballColor);

DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);

EndDrawing();
}

// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context

return 0;
}



The Rust version:


use raylib::prelude::MouseButton::*;
use raylib::prelude::*;

fn main() {
let screen_width = 800.0;
let screen_height = 450.0;

let (mut rl, thread) = raylib::init()
.size(screen_width as i32, screen_height as i32)
.title("Raylib [core] example - mouse input")
.build();

let mut ball_color = Color::DARKBLUE;

rl.set_target_fps(60); // Set our game to run at 60 frames-per-second

while !rl.window_should_close() {
if rl.is_mouse_button_pressed(MOUSE_LEFT_BUTTON) {
ball_color = Color::MAROON;
} else if rl.is_mouse_button_pressed(MOUSE_MIDDLE_BUTTON) {
ball_color = Color::LIME;
} else if rl.is_mouse_button_pressed(MOUSE_RIGHT_BUTTON) {
ball_color = Color::DARKBLUE;
}

let mut d = rl.begin_drawing(&thread);

d.clear_background(Color::RAYWHITE);
d.draw_text(
"move ball with mouse and click mouse button to change color",
10,
10,
20,
Color::DARKGRAY,
);

d.draw_circle_v(d.get_mouse_position(), 50.0, ball_color);
}
}