Skip to content

Commit 02d98a3

Browse files
committed
REVIEWED: 2d camera zoom, add alternative method #3977
1 parent 479bd84 commit 02d98a3

File tree

1 file changed

+54
-22
lines changed

1 file changed

+54
-22
lines changed

examples/core/core_2d_camera_mouse_zoom.c

+54-22
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ int main ()
3131
Camera2D camera = { 0 };
3232
camera.zoom = 1.0f;
3333

34+
int zoomMode = 0; // 0-Mouse Wheel, 1-Mouse Move
35+
3436
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
3537
//--------------------------------------------------------------------------------------
3638

@@ -39,41 +41,69 @@ int main ()
3941
{
4042
// Update
4143
//----------------------------------------------------------------------------------
44+
if (IsKeyPressed(KEY_ONE)) zoomMode = 0;
45+
else if (IsKeyPressed(KEY_TWO)) zoomMode = 1;
46+
4247
// Translate based on mouse right click
4348
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
4449
{
4550
Vector2 delta = GetMouseDelta();
4651
delta = Vector2Scale(delta, -1.0f/camera.zoom);
47-
4852
camera.target = Vector2Add(camera.target, delta);
4953
}
5054

51-
// Zoom based on mouse wheel
52-
float wheel = GetMouseWheelMove();
53-
if (wheel != 0)
55+
if (zoomMode == 0)
5456
{
55-
// Get the world point that is under the mouse
56-
Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera);
57-
58-
// Set the offset to where the mouse is
59-
camera.offset = GetMousePosition();
60-
61-
// Set the target to match, so that the camera maps the world space point
62-
// under the cursor to the screen space point under the cursor at any zoom
63-
camera.target = mouseWorldPos;
64-
65-
// Zoom increment
66-
float scaleFactor = 1.0f + (0.25f * fabsf(wheel));
67-
if (wheel < 0) scaleFactor = 1.0f / scaleFactor;
68-
camera.zoom = Clamp(camera.zoom * scaleFactor, 0.125, 64);
57+
// Zoom based on mouse wheel
58+
float wheel = GetMouseWheelMove();
59+
if (wheel != 0)
60+
{
61+
// Get the world point that is under the mouse
62+
Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera);
63+
64+
// Set the offset to where the mouse is
65+
camera.offset = GetMousePosition();
66+
67+
// Set the target to match, so that the camera maps the world space point
68+
// under the cursor to the screen space point under the cursor at any zoom
69+
camera.target = mouseWorldPos;
70+
71+
// Zoom increment
72+
float scaleFactor = 1.0f + (0.25f*fabsf(wheel));
73+
if (wheel < 0) scaleFactor = 1.0f/scaleFactor;
74+
camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f);
75+
}
76+
}
77+
else
78+
{
79+
// Zoom based on mouse left click
80+
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
81+
{
82+
// Get the world point that is under the mouse
83+
Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera);
84+
85+
// Set the offset to where the mouse is
86+
camera.offset = GetMousePosition();
87+
88+
// Set the target to match, so that the camera maps the world space point
89+
// under the cursor to the screen space point under the cursor at any zoom
90+
camera.target = mouseWorldPos;
91+
}
92+
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
93+
{
94+
// Zoom increment
95+
float deltaX = GetMouseDelta().x;
96+
float scaleFactor = 1.0f + (0.01f*fabsf(deltaX));
97+
if (deltaX < 0) scaleFactor = 1.0f/scaleFactor;
98+
camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f);
99+
}
69100
}
70-
71101
//----------------------------------------------------------------------------------
72102

73103
// Draw
74104
//----------------------------------------------------------------------------------
75105
BeginDrawing();
76-
ClearBackground(BLACK);
106+
ClearBackground(RAYWHITE);
77107

78108
BeginMode2D(camera);
79109

@@ -86,11 +116,13 @@ int main ()
86116
rlPopMatrix();
87117

88118
// Draw a reference circle
89-
DrawCircle(100, 100, 50, YELLOW);
119+
DrawCircle(GetScreenWidth()/2, GetScreenHeight()/2, 50, MAROON);
90120

91121
EndMode2D();
92122

93-
DrawText("Mouse right button drag to move, mouse wheel to zoom", 10, 10, 20, WHITE);
123+
DrawText("[1][2] Select mouse zoom mode (Wheel or Move)", 20, 20, 20, DARKGRAY);
124+
if (zoomMode == 0) DrawText("Mouse right button drag to move, mouse wheel to zoom", 20, 50, 20, DARKGRAY);
125+
else DrawText("Mouse right button drag to move, mouse press and move to zoom", 20, 50, 20, DARKGRAY);
94126

95127
EndDrawing();
96128
//----------------------------------------------------------------------------------

0 commit comments

Comments
 (0)