diff --git a/scenes/Main.tscn b/scenes/Main.tscn index beccb33..d0886f1 100644 --- a/scenes/Main.tscn +++ b/scenes/Main.tscn @@ -12,7 +12,6 @@ transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461314, 0.76112, -2.26831e-08, 0.648611, 0, 0, 0 ) [node name="OrbitCamera" parent="." instance=ExtResource( 3 )] -_speed = 1.0 [node name="Railway" parent="." instance=ExtResource( 2 )] script = null diff --git a/scenes/OrbitCamera.tscn b/scenes/OrbitCamera.tscn index c9e0d8f..5a203e7 100644 --- a/scenes/OrbitCamera.tscn +++ b/scenes/OrbitCamera.tscn @@ -6,4 +6,5 @@ script = ExtResource( 1 ) [node name="Camera" type="Camera" parent="."] -transform = Transform( 0.636065, -0.249781, 0.73009, 0, 0.946159, 0.323703, -0.771636, -0.205896, 0.601818, 8.49012, 5.30069, 6.71091 ) +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; } }