62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class GobalCamera : Camera2D
|
|
{
|
|
public Vector2 zoomSpeed = new Vector2(0.05f, 0.05f);
|
|
public float zoomMin = 0.1f;
|
|
public float zoomMax = 4.0f;
|
|
[Export] public float dragSensitivity = 0.35f;
|
|
[Export] public float cameraStepSize = 10.0f;
|
|
|
|
public override void _Input(InputEvent @event)
|
|
{
|
|
base._Input(@event);
|
|
|
|
if (@event is InputEventMouseMotion mouseMotion && Input.IsMouseButtonPressed(MouseButton.Middle))
|
|
{
|
|
Position -= mouseMotion.Relative * dragSensitivity / Zoom;
|
|
}
|
|
|
|
if (@event is InputEventAction eventAction && eventAction.IsPressed())
|
|
{
|
|
switch (eventAction.AsText())
|
|
{
|
|
case "camera_up":
|
|
Position -= new Vector2(0, cameraStepSize);
|
|
break;
|
|
case "camera_down":
|
|
Position += new Vector2(0, cameraStepSize);
|
|
break;
|
|
case "camera_left":
|
|
Position -= new Vector2(cameraStepSize, 0);
|
|
break;
|
|
case "camera_right":
|
|
Position += new Vector2(cameraStepSize, 0);
|
|
break;
|
|
}
|
|
|
|
GD.Print(eventAction.AsText());
|
|
}
|
|
|
|
if (@event is InputEventMouseButton mouseButton)
|
|
{
|
|
switch (mouseButton.ButtonIndex)
|
|
{
|
|
case MouseButton.WheelUp:
|
|
Zoom += zoomSpeed;
|
|
Zoom.Clamp(zoomMin, zoomMax);
|
|
break;
|
|
case MouseButton.WheelDown:
|
|
Zoom -= zoomSpeed;
|
|
Zoom.Clamp(zoomMin, zoomMax);
|
|
break;
|
|
}
|
|
|
|
Zoom.Clamp(zoomMin, zoomMax);
|
|
}
|
|
}
|
|
|
|
public static Vector2 Lerp(Vector2 from, Vector2 to, float weight) => from + (to - from) * weight;
|
|
}
|