| | | 1 | | using UnityEngine; |
| | | 2 | | using UnityEngine.InputSystem; |
| | | 3 | | |
| | | 4 | | public class InputManager : MonoBehaviour { |
| | 0 | 5 | | public static InputManager Instance { get; private set; } |
| | | 6 | | |
| | 0 | 7 | | public bool MouseActive { get; set; } = true; |
| | 0 | 8 | | public bool LockUserInput { get; set; } = false; |
| | | 9 | | |
| | | 10 | | private Vector2 _lastMousePosition; |
| | 0 | 11 | | private bool _isDragging = false; |
| | | 12 | | |
| | 0 | 13 | | private void Awake() { |
| | 0 | 14 | | if (Instance != null && Instance != this) { |
| | 0 | 15 | | Destroy(gameObject); |
| | 0 | 16 | | return; |
| | | 17 | | } |
| | 0 | 18 | | Instance = this; |
| | 0 | 19 | | DontDestroyOnLoad(gameObject); |
| | 0 | 20 | | } |
| | | 21 | | |
| | 0 | 22 | | private void Update() { |
| | 0 | 23 | | HandleInput(); |
| | 0 | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | private void HandleInput() { |
| | 0 | 27 | | if (!LockUserInput) { |
| | 0 | 28 | | HandleLockableInput(); |
| | 0 | 29 | | } |
| | 0 | 30 | | HandleNonLockableInput(); |
| | 0 | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | private void HandleLockableInput() { |
| | 0 | 34 | | if (MouseActive) { |
| | 0 | 35 | | switch (UIManager.Instance.UIMode) { |
| | 0 | 36 | | case UIMode.THREE_DIMENSIONAL: { |
| | 0 | 37 | | Handle3DModeMouseInput(); |
| | 0 | 38 | | Handle3DModeScrollWheelInput(); |
| | 0 | 39 | | break; |
| | | 40 | | } |
| | 0 | 41 | | case UIMode.TACTICAL: { |
| | 0 | 42 | | HandleTacticalModeMouseInput(); |
| | 0 | 43 | | HandleTacticalModeScrollWheelInput(); |
| | 0 | 44 | | break; |
| | | 45 | | } |
| | 0 | 46 | | default: { |
| | 0 | 47 | | Debug.LogError($"Invalid UI mode: {UIManager.Instance.UIMode}."); |
| | 0 | 48 | | break; |
| | | 49 | | } |
| | | 50 | | } |
| | 0 | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | switch (UIManager.Instance.UIMode) { |
| | 0 | 54 | | case UIMode.THREE_DIMENSIONAL: { |
| | 0 | 55 | | Handle3DModeLockableInput(); |
| | 0 | 56 | | break; |
| | | 57 | | } |
| | 0 | 58 | | case UIMode.TACTICAL: { |
| | 0 | 59 | | HandleTacticalModeLockableInput(); |
| | 0 | 60 | | break; |
| | | 61 | | } |
| | 0 | 62 | | default: { |
| | 0 | 63 | | Debug.LogError($"Invalid UI mode: {UIManager.Instance.UIMode}."); |
| | 0 | 64 | | break; |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | if (Keyboard.current.tabKey.wasPressedThisFrame) { |
| | 0 | 69 | | UIManager.Instance.ToggleUIMode(); |
| | 0 | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | private void Handle3DModeMouseInput() { |
| | 0 | 74 | | var mouse = Mouse.current; |
| | 0 | 75 | | Vector2 delta = mouse.delta.ReadValue(); |
| | 0 | 76 | | if (mouse.leftButton.isPressed) { |
| | 0 | 77 | | CameraController.Instance.OrbitCamera(delta.x, delta.y); |
| | 0 | 78 | | } else if (mouse.rightButton.isPressed) { |
| | 0 | 79 | | CameraController.Instance.RotateCamera(delta.x, delta.y); |
| | 0 | 80 | | } |
| | 0 | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | private void Handle3DModeScrollWheelInput() { |
| | 0 | 84 | | var mouse = Mouse.current; |
| | 0 | 85 | | if (mouse.scroll.ReadValue().y != 0) { |
| | 0 | 86 | | CameraController.Instance.ZoomCamera(mouse.scroll.ReadValue().y * 5); |
| | 0 | 87 | | } |
| | 0 | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | private void Handle3DModeLockableInput() { |
| | 0 | 91 | | var keyboard = Keyboard.current; |
| | 0 | 92 | | if (keyboard.leftShiftKey.isPressed) { |
| | 0 | 93 | | CameraController.Instance.CameraSpeed = CameraController.Instance.CameraSpeedMax; |
| | 0 | 94 | | } else { |
| | 0 | 95 | | CameraController.Instance.CameraSpeed = CameraController.Instance.CameraSpeedNormal; |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | // Translational movement. |
| | 0 | 99 | | if (keyboard.wKey.isPressed || keyboard.upArrowKey.isPressed) { |
| | 0 | 100 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Forward); |
| | 0 | 101 | | } |
| | 0 | 102 | | if (keyboard.aKey.isPressed || keyboard.leftArrowKey.isPressed) { |
| | 0 | 103 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Left); |
| | 0 | 104 | | } |
| | 0 | 105 | | if (keyboard.sKey.isPressed || keyboard.downArrowKey.isPressed) { |
| | 0 | 106 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Back); |
| | 0 | 107 | | } |
| | 0 | 108 | | if (keyboard.dKey.isPressed || keyboard.rightArrowKey.isPressed) { |
| | 0 | 109 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Right); |
| | 0 | 110 | | } |
| | 0 | 111 | | if (keyboard.qKey.isPressed) { |
| | 0 | 112 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Up); |
| | 0 | 113 | | } |
| | 0 | 114 | | if (keyboard.eKey.isPressed) { |
| | 0 | 115 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Down); |
| | 0 | 116 | | } |
| | 0 | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | private void HandleTacticalModeMouseInput() { |
| | 0 | 120 | | var mouse = Mouse.current; |
| | | 121 | | // Start drag on right mouse button. |
| | 0 | 122 | | if (mouse.rightButton.wasPressedThisFrame) { |
| | 0 | 123 | | _isDragging = true; |
| | 0 | 124 | | _lastMousePosition = mouse.position.ReadValue(); |
| | 0 | 125 | | } |
| | | 126 | | // End drag when button released. |
| | 0 | 127 | | else if (mouse.rightButton.wasReleasedThisFrame) { |
| | 0 | 128 | | _isDragging = false; |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | // Handle dragging. |
| | 0 | 132 | | if (_isDragging) { |
| | 0 | 133 | | Vector2 currentMousePos = mouse.position.ReadValue(); |
| | 0 | 134 | | Vector2 delta = currentMousePos - _lastMousePosition; |
| | 0 | 135 | | TacticalPanel.Instance.Pan(delta); |
| | 0 | 136 | | _lastMousePosition = currentMousePos; |
| | 0 | 137 | | } |
| | 0 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | private void HandleTacticalModeScrollWheelInput() { |
| | 0 | 141 | | var mouse = Mouse.current; |
| | 0 | 142 | | if (mouse.scroll.ReadValue().y != 0) { |
| | 0 | 143 | | TacticalPanel.Instance.ZoomIn(mouse.scroll.ReadValue().y * 0.001f); |
| | 0 | 144 | | } |
| | 0 | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | private void HandleTacticalModeLockableInput() { |
| | | 148 | | // Handle keyboard input for panning. |
| | 0 | 149 | | var keyboard = Keyboard.current; |
| | 0 | 150 | | Vector2 keyboardPanDirection = Vector2.zero; |
| | 0 | 151 | | if (keyboard.wKey.isPressed || keyboard.upArrowKey.isPressed) { |
| | 0 | 152 | | keyboardPanDirection.y += -1; |
| | 0 | 153 | | } |
| | 0 | 154 | | if (keyboard.aKey.isPressed || keyboard.leftArrowKey.isPressed) { |
| | 0 | 155 | | keyboardPanDirection.x += 1; |
| | 0 | 156 | | } |
| | 0 | 157 | | if (keyboard.sKey.isPressed || keyboard.downArrowKey.isPressed) { |
| | 0 | 158 | | keyboardPanDirection.y += 1; |
| | 0 | 159 | | } |
| | 0 | 160 | | if (keyboard.dKey.isPressed || keyboard.rightArrowKey.isPressed) { |
| | 0 | 161 | | keyboardPanDirection.x += -1; |
| | 0 | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | if (keyboard.qKey.wasPressedThisFrame) { |
| | 0 | 165 | | TacticalPanel.Instance.CycleRangeUp(); |
| | 0 | 166 | | } |
| | 0 | 167 | | if (keyboard.eKey.wasPressedThisFrame) { |
| | 0 | 168 | | TacticalPanel.Instance.CycleRangeDown(); |
| | 0 | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | if (keyboardPanDirection != Vector2.zero) { |
| | 0 | 172 | | TacticalPanel.Instance.PanWithKeyboard(keyboardPanDirection.normalized); |
| | 0 | 173 | | } |
| | 0 | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | private void HandleNonLockableInput() { |
| | 0 | 177 | | var keyboard = Keyboard.current; |
| | 0 | 178 | | if (keyboard.escapeKey.wasPressedThisFrame) { |
| | 0 | 179 | | SimManager.Instance.QuitSimulation(); |
| | 0 | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | if (keyboard.rKey.wasPressedThisFrame) { |
| | 0 | 183 | | SimManager.Instance.EndSimulation(); |
| | 0 | 184 | | SimManager.Instance.ResetAndStartSimulation(); |
| | 0 | 185 | | } |
| | | 186 | | |
| | 0 | 187 | | if (keyboard.lKey.wasPressedThisFrame) { |
| | 0 | 188 | | UIManager.Instance.ToggleConfigSelectorPanel(); |
| | 0 | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | if (keyboard.cKey.wasPressedThisFrame) { |
| | 0 | 192 | | ParticleManager.Instance.ClearHitMarkers(); |
| | 0 | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | if (keyboard.pKey.wasPressedThisFrame) { |
| | 0 | 196 | | CameraController.Instance.AutoRotate = !CameraController.Instance.AutoRotate; |
| | 0 | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | if (keyboard.spaceKey.wasPressedThisFrame) { |
| | | 200 | | // Pause the time. |
| | 0 | 201 | | if (!SimManager.Instance.IsPaused) { |
| | 0 | 202 | | SimManager.Instance.PauseSimulation(); |
| | 0 | 203 | | } else { |
| | 0 | 204 | | SimManager.Instance.ResumeSimulation(); |
| | 0 | 205 | | } |
| | 0 | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | HandleCameraFollowInput(Key.Digit1, CameraFollowType.ALL_AGENTS); |
| | 0 | 209 | | HandleCameraFollowInput(Key.Digit2, CameraFollowType.ALL_INTERCEPTORS); |
| | 0 | 210 | | HandleCameraFollowInput(Key.Digit3, CameraFollowType.ALL_THREATS); |
| | 0 | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | private void HandleCameraFollowInput(Key key, CameraFollowType followType) { |
| | 0 | 214 | | var keyboard = Keyboard.current; |
| | 0 | 215 | | if (keyboard[key].wasPressedThisFrame) { |
| | 0 | 216 | | if (keyboard.leftCtrlKey.isPressed) { |
| | 0 | 217 | | CameraController.Instance.Follow(followType); |
| | 0 | 218 | | } else { |
| | 0 | 219 | | CameraController.Instance.Snap(followType); |
| | 0 | 220 | | } |
| | 0 | 221 | | } |
| | 0 | 222 | | } |
| | | 223 | | } |