REVIEWED: 2d camera zoom, add alternative method #3977

This commit is contained in:
Ray 2024-05-15 16:33:06 +02:00
parent 479bd84400
commit 02d98a3e44

View file

@ -31,6 +31,8 @@ int main ()
Camera2D camera = { 0 }; Camera2D camera = { 0 };
camera.zoom = 1.0f; camera.zoom = 1.0f;
int zoomMode = 0; // 0-Mouse Wheel, 1-Mouse Move
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -39,41 +41,69 @@ int main ()
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_ONE)) zoomMode = 0;
else if (IsKeyPressed(KEY_TWO)) zoomMode = 1;
// Translate based on mouse right click // Translate based on mouse right click
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
{ {
Vector2 delta = GetMouseDelta(); Vector2 delta = GetMouseDelta();
delta = Vector2Scale(delta, -1.0f/camera.zoom); delta = Vector2Scale(delta, -1.0f/camera.zoom);
camera.target = Vector2Add(camera.target, delta); camera.target = Vector2Add(camera.target, delta);
} }
// Zoom based on mouse wheel if (zoomMode == 0)
float wheel = GetMouseWheelMove();
if (wheel != 0)
{ {
// Get the world point that is under the mouse // Zoom based on mouse wheel
Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera); float wheel = GetMouseWheelMove();
if (wheel != 0)
{
// Get the world point that is under the mouse
Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera);
// Set the offset to where the mouse is // Set the offset to where the mouse is
camera.offset = GetMousePosition(); camera.offset = GetMousePosition();
// Set the target to match, so that the camera maps the world space point // Set the target to match, so that the camera maps the world space point
// under the cursor to the screen space point under the cursor at any zoom // under the cursor to the screen space point under the cursor at any zoom
camera.target = mouseWorldPos; camera.target = mouseWorldPos;
// Zoom increment // Zoom increment
float scaleFactor = 1.0f + (0.25f * fabsf(wheel)); float scaleFactor = 1.0f + (0.25f*fabsf(wheel));
if (wheel < 0) scaleFactor = 1.0f / scaleFactor; if (wheel < 0) scaleFactor = 1.0f/scaleFactor;
camera.zoom = Clamp(camera.zoom * scaleFactor, 0.125, 64); camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f);
}
} }
else
{
// Zoom based on mouse left click
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
// Get the world point that is under the mouse
Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera);
// Set the offset to where the mouse is
camera.offset = GetMousePosition();
// Set the target to match, so that the camera maps the world space point
// under the cursor to the screen space point under the cursor at any zoom
camera.target = mouseWorldPos;
}
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
// Zoom increment
float deltaX = GetMouseDelta().x;
float scaleFactor = 1.0f + (0.01f*fabsf(deltaX));
if (deltaX < 0) scaleFactor = 1.0f/scaleFactor;
camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f);
}
}
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(RAYWHITE);
BeginMode2D(camera); BeginMode2D(camera);
@ -86,11 +116,13 @@ int main ()
rlPopMatrix(); rlPopMatrix();
// Draw a reference circle // Draw a reference circle
DrawCircle(100, 100, 50, YELLOW); DrawCircle(GetScreenWidth()/2, GetScreenHeight()/2, 50, MAROON);
EndMode2D(); EndMode2D();
DrawText("Mouse right button drag to move, mouse wheel to zoom", 10, 10, 20, WHITE); DrawText("[1][2] Select mouse zoom mode (Wheel or Move)", 20, 20, 20, DARKGRAY);
if (zoomMode == 0) DrawText("Mouse right button drag to move, mouse wheel to zoom", 20, 50, 20, DARKGRAY);
else DrawText("Mouse right button drag to move, mouse press and move to zoom", 20, 50, 20, DARKGRAY);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------