Skip to content

Commit

Permalink
REVIEWED: 2d camera zoom, add alternative method raysan5#3977
Browse files Browse the repository at this point in the history
  • Loading branch information
raysan5 committed May 15, 2024
1 parent 479bd84 commit 02d98a3
Showing 1 changed file with 54 additions and 22 deletions.
76 changes: 54 additions & 22 deletions examples/core/core_2d_camera_mouse_zoom.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
//--------------------------------------------------------------------------------------

Expand All @@ -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);

Expand All @@ -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();
//----------------------------------------------------------------------------------
Expand Down

0 comments on commit 02d98a3

Please sign in to comment.