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