added core_input_mouse_drag
This commit is contained in:
parent
200d344dee
commit
50708841c8
73
examples/core/core_input_mouse_drag.c
Normal file
73
examples/core/core_input_mouse_drag.c
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [core] example - input mouse drag
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 6.0, last time updated with raylib 6.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) 2026 tipudeveloper (@tipudeveloper)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - input mouse drag");
|
||||
SetTargetFPS(60);
|
||||
|
||||
Rectangle rect = { screenWidth / 2 - 50, screenHeight / 2 - 50, 100, 100 };
|
||||
Vector2 mousePosition = { 0.0f, 0.0f };
|
||||
bool dragging = false;
|
||||
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
mousePosition = GetMousePosition();
|
||||
|
||||
// Begin dragging if the cursor is inside the rectangle when the left button is pressed
|
||||
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
||||
{
|
||||
if (CheckCollisionPointRec(mousePosition, rect))
|
||||
{
|
||||
dragging = true;
|
||||
}
|
||||
}
|
||||
else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
|
||||
{
|
||||
// Stop dragging when the button is released
|
||||
dragging = false;
|
||||
}
|
||||
|
||||
if (dragging)
|
||||
{
|
||||
// Keep the rectangle centered on the cursor while dragging
|
||||
rect.x = mousePosition.x - rect.width / 2;
|
||||
rect.y = mousePosition.y - rect.height / 2;
|
||||
}
|
||||
|
||||
// Use one mouse wheel sample per frame to scale the rectangle
|
||||
float wheelMove = GetMouseWheelMove();
|
||||
rect.width += wheelMove * 10.0f;
|
||||
rect.height += wheelMove * 10.0f;
|
||||
|
||||
// Clamp rectangle size so it remains visible
|
||||
if (rect.width < 10) rect.width = 10;
|
||||
if (rect.height < 10) rect.height = 10;
|
||||
if (rect.width > 500) rect.width = 500;
|
||||
if (rect.height > 500) rect.height = 500;
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
DrawRectangleRec(rect, RED);
|
||||
DrawText("Drag the rectangle and scale it with the mouse wheel", 10, 10, 20, BLACK);
|
||||
EndDrawing();
|
||||
}
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
BIN
examples/core/core_input_mouse_drag.png
Normal file
BIN
examples/core/core_input_mouse_drag.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 48 KiB |
Loading…
Reference in New Issue
Block a user