summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRay <[email protected]>2024-05-15 16:33:06 +0200
committerRay <[email protected]>2024-05-15 16:33:06 +0200
commit02d98a3e44b13584c98a7aaee0ca6fc5fb55a975 (patch)
treee59c4fb239bfba8d9872947ff20c22b2e6e3a099
parent479bd84400d2da4af4f1c902db2f579611d36b99 (diff)
downloadraylib-02d98a3e44b13584c98a7aaee0ca6fc5fb55a975.tar.gz
raylib-02d98a3e44b13584c98a7aaee0ca6fc5fb55a975.zip
REVIEWED: 2d camera zoom, add alternative method #3977
-rw-r--r--examples/core/core_2d_camera_mouse_zoom.c76
1 files 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();
//----------------------------------------------------------------------------------