From a8065c0ea5db8ec1dacc4ac3ccb7f73b98233407 Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Mon, 5 Sep 2022 19:40:39 +0100 Subject: [PATCH] add camera zoom --- scenes/OrbitCamera.tscn | 2 ++ scripts/OrbitCamera.cs | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/scenes/OrbitCamera.tscn b/scenes/OrbitCamera.tscn index 5a203e7..5486939 100644 --- a/scenes/OrbitCamera.tscn +++ b/scenes/OrbitCamera.tscn @@ -4,6 +4,8 @@ [node name="OrbitCamera" type="Spatial"] script = ExtResource( 1 ) +_zoomSensitivity = 50.0 +_cameraPath = NodePath("Camera") [node name="Camera" type="Camera" parent="."] transform = Transform( 1, 0, 0, 0, 0.964146, 0.265371, 0, -0.265371, 0.964146, 0, 2.21115, 9.48434 ) diff --git a/scripts/OrbitCamera.cs b/scripts/OrbitCamera.cs index 55763eb..feac7d0 100644 --- a/scripts/OrbitCamera.cs +++ b/scripts/OrbitCamera.cs @@ -4,7 +4,25 @@ using System; public class OrbitCamera : Spatial { [Export] - private float _sensitivity = 200f; + private float _lookSensitivity = 200f; + + [Export] + private float _zoomSensitivity = 10f; + + [Export] + private NodePath _cameraPath; + private Camera _camera = null; + private Camera Camera + { + get + { + if (_camera == null) + { + _camera = GetNode(_cameraPath); + } + return _camera; + } + } private Vector2 _rotation; private bool _canRotate = false; @@ -24,7 +42,8 @@ public class OrbitCamera : Spatial public override void _Process(float delta) { Rotation = Vector3.Zero; - var sensitivity = -1f / _sensitivity; + var sensitivity = -1f / _lookSensitivity; + Rotate(Vector3.Right, _rotation.y * sensitivity); Rotate(Vector3.Up, _rotation.x * sensitivity); } @@ -32,9 +51,23 @@ public class OrbitCamera : Spatial // left click to drag private void HandleMouseButton(InputEventMouseButton mouseButton) { - if (mouseButton.ButtonIndex != (int)ButtonList.Left) return; + switch ((ButtonList)mouseButton.ButtonIndex) + { + case ButtonList.Left: + _canRotate = mouseButton.Pressed; + break; + case ButtonList.WheelUp: + Zoom(-1); + break; + case ButtonList.WheelDown: + Zoom(1); + break; + } + } - _canRotate = mouseButton.Pressed; + private void Zoom(float amount) + { + Camera.Fov += amount * _zoomSensitivity / Camera.Fov; } private void HandleMouseMovement(InputEventMouseMotion mouseMotion)