mirror of https://github.com/raysan5/raylib
REVIEWED: 2d camera zoom, add alternative method #3977
This commit is contained in:
parent
479bd84400
commit
02d98a3e44
|
@ -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();
|
||||
// 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 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;
|
||||
// Set the offset to where the mouse is
|
||||
camera.offset = GetMousePosition();
|
||||
|
||||
// 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);
|
||||
// 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();
|
||||
//----------------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue