From ce823c06b6418255a9c6926eb31d6674e3697598 Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Sun, 4 Sep 2022 17:37:35 +0100 Subject: [PATCH] drag to rotate camera --- scenes/OrbitCamera.tscn | 2 +- scripts/OrbitCamera.cs | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/scenes/OrbitCamera.tscn b/scenes/OrbitCamera.tscn index 4780caa..5a203e7 100644 --- a/scenes/OrbitCamera.tscn +++ b/scenes/OrbitCamera.tscn @@ -6,5 +6,5 @@ script = ExtResource( 1 ) [node name="Camera" type="Camera" parent="."] -transform = Transform( 1, 0, 0, 0, 0.913325, 0.407231, 0, -0.407231, 0.913325, 0, 24.1191, 58.6188 ) +transform = Transform( 1, 0, 0, 0, 0.964146, 0.265371, 0, -0.265371, 0.964146, 0, 2.21115, 9.48434 ) fov = 25.0 diff --git a/scripts/OrbitCamera.cs b/scripts/OrbitCamera.cs index 2aa7677..55763eb 100644 --- a/scripts/OrbitCamera.cs +++ b/scripts/OrbitCamera.cs @@ -4,15 +4,44 @@ using System; public class OrbitCamera : Spatial { [Export] - private float _speed; + private float _sensitivity = 200f; - public override void _Ready() + 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) { - Rotate(Vector3.Up, _speed * 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; } }