shmodot/scripts/OrbitCamera.cs

48 lines
1.1 KiB
C#

using Godot;
using System;
public class OrbitCamera : Spatial
{
[Export]
private float _sensitivity = 200f;
private Vector2 _rotation;
private bool _canRotate = false;
public override void _Input(InputEvent e)
{
if (e is InputEventMouseMotion mouseMotion)
{
HandleMouseMovement(mouseMotion);
}
if (e is InputEventMouseButton mouseButton)
{
HandleMouseButton(mouseButton);
}
}
public override void _Process(float delta)
{
Rotation = Vector3.Zero;
var sensitivity = -1f / _sensitivity;
Rotate(Vector3.Right, _rotation.y * sensitivity);
Rotate(Vector3.Up, _rotation.x * sensitivity);
}
// left click to drag
private void HandleMouseButton(InputEventMouseButton mouseButton)
{
if (mouseButton.ButtonIndex != (int)ButtonList.Left) return;
_canRotate = mouseButton.Pressed;
}
private void HandleMouseMovement(InputEventMouseMotion mouseMotion)
{
if (!_canRotate) return;
var delta = mouseMotion.Relative;
_rotation += delta;
}
}