From 02d98a3e44b13584c98a7aaee0ca6fc5fb55a975 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 15 May 2024 16:33:06 +0200 Subject: [PATCH] REVIEWED: 2d camera zoom, add alternative method #3977 --- examples/core/core_2d_camera_mouse_zoom.c | 76 ++++++++++++++++------- 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/examples/core/core_2d_camera_mouse_zoom.c b/examples/core/core_2d_camera_mouse_zoom.c index b96a3db4..8af94440 100644 --- a/examples/core/core_2d_camera_mouse_zoom.c +++ b/examples/core/core_2d_camera_mouse_zoom.c @@ -31,6 +31,8 @@ int main () Camera2D camera = { 0 }; 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 //-------------------------------------------------------------------------------------- @@ -39,41 +41,69 @@ int main () { // Update //---------------------------------------------------------------------------------- + if (IsKeyPressed(KEY_ONE)) zoomMode = 0; + else if (IsKeyPressed(KEY_TWO)) zoomMode = 1; + // Translate based on mouse right click if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) { Vector2 delta = GetMouseDelta(); delta = Vector2Scale(delta, -1.0f/camera.zoom); - camera.target = Vector2Add(camera.target, delta); } - // Zoom based on mouse wheel - float wheel = GetMouseWheelMove(); - if (wheel != 0) + if (zoomMode == 0) { - // 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; - - // Zoom increment - float scaleFactor = 1.0f + (0.25f * fabsf(wheel)); - if (wheel < 0) scaleFactor = 1.0f / scaleFactor; - camera.zoom = Clamp(camera.zoom * scaleFactor, 0.125, 64); + // Zoom based on mouse wheel + 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 + 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; + + // Zoom increment + float scaleFactor = 1.0f + (0.25f*fabsf(wheel)); + if (wheel < 0) scaleFactor = 1.0f/scaleFactor; + 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 //---------------------------------------------------------------------------------- BeginDrawing(); - ClearBackground(BLACK); + ClearBackground(RAYWHITE); BeginMode2D(camera); @@ -86,11 +116,13 @@ int main () rlPopMatrix(); // Draw a reference circle - DrawCircle(100, 100, 50, YELLOW); + DrawCircle(GetScreenWidth()/2, GetScreenHeight()/2, 50, MAROON); 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(); //----------------------------------------------------------------------------------