< Summary

Class:InputManager
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Managers/InputManager.cs
Covered lines:0
Uncovered lines:176
Coverable lines:176
Total lines:223
Line coverage:0% (0 of 176)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:19
Method coverage:0% (0 of 19)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InputManager()0%2100%
Awake()0%12300%
Update()0%2100%
HandleInput()0%6200%
HandleLockableInput()0%56700%
Handle3DModeMouseInput()0%12300%
Handle3DModeScrollWheelInput()0%6200%
Handle3DModeLockableInput()0%1561200%
HandleTacticalModeMouseInput()0%20400%
HandleTacticalModeScrollWheelInput()0%6200%
HandleTacticalModeLockableInput()0%1561200%
HandleNonLockableInput()0%72800%
HandleCameraFollowInput(...)0%12300%

File(s)

/github/workspace/Assets/Scripts/Managers/InputManager.cs

#LineLine coverage
 1using UnityEngine;
 2using UnityEngine.InputSystem;
 3
 4public class InputManager : MonoBehaviour {
 05  public static InputManager Instance { get; private set; }
 6
 07  public bool MouseActive { get; set; } = true;
 08  public bool LockUserInput { get; set; } = false;
 9
 10  private Vector2 _lastMousePosition;
 011  private bool _isDragging = false;
 12
 013  private void Awake() {
 014    if (Instance != null && Instance != this) {
 015      Destroy(gameObject);
 016      return;
 17    }
 018    Instance = this;
 019    DontDestroyOnLoad(gameObject);
 020  }
 21
 022  private void Update() {
 023    HandleInput();
 024  }
 25
 026  private void HandleInput() {
 027    if (!LockUserInput) {
 028      HandleLockableInput();
 029    }
 030    HandleNonLockableInput();
 031  }
 32
 033  private void HandleLockableInput() {
 034    if (MouseActive) {
 035      switch (UIManager.Instance.UIMode) {
 036        case UIMode.THREE_DIMENSIONAL: {
 037          Handle3DModeMouseInput();
 038          Handle3DModeScrollWheelInput();
 039          break;
 40        }
 041        case UIMode.TACTICAL: {
 042          HandleTacticalModeMouseInput();
 043          HandleTacticalModeScrollWheelInput();
 044          break;
 45        }
 046        default: {
 047          Debug.LogError($"Invalid UI mode: {UIManager.Instance.UIMode}.");
 048          break;
 49        }
 50      }
 051    }
 52
 053    switch (UIManager.Instance.UIMode) {
 054      case UIMode.THREE_DIMENSIONAL: {
 055        Handle3DModeLockableInput();
 056        break;
 57      }
 058      case UIMode.TACTICAL: {
 059        HandleTacticalModeLockableInput();
 060        break;
 61      }
 062      default: {
 063        Debug.LogError($"Invalid UI mode: {UIManager.Instance.UIMode}.");
 064        break;
 65      }
 66    }
 67
 068    if (Keyboard.current.tabKey.wasPressedThisFrame) {
 069      UIManager.Instance.ToggleUIMode();
 070    }
 071  }
 72
 073  private void Handle3DModeMouseInput() {
 074    var mouse = Mouse.current;
 075    Vector2 delta = mouse.delta.ReadValue();
 076    if (mouse.leftButton.isPressed) {
 077      CameraController.Instance.OrbitCamera(delta.x, delta.y);
 078    } else if (mouse.rightButton.isPressed) {
 079      CameraController.Instance.RotateCamera(delta.x, delta.y);
 080    }
 081  }
 82
 083  private void Handle3DModeScrollWheelInput() {
 084    var mouse = Mouse.current;
 085    if (mouse.scroll.ReadValue().y != 0) {
 086      CameraController.Instance.ZoomCamera(mouse.scroll.ReadValue().y * 5);
 087    }
 088  }
 89
 090  private void Handle3DModeLockableInput() {
 091    var keyboard = Keyboard.current;
 092    if (keyboard.leftShiftKey.isPressed) {
 093      CameraController.Instance.CameraSpeed = CameraController.Instance.CameraSpeedMax;
 094    } else {
 095      CameraController.Instance.CameraSpeed = CameraController.Instance.CameraSpeedNormal;
 096    }
 97
 98    // Translational movement.
 099    if (keyboard.wKey.isPressed || keyboard.upArrowKey.isPressed) {
 0100      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Forward);
 0101    }
 0102    if (keyboard.aKey.isPressed || keyboard.leftArrowKey.isPressed) {
 0103      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Left);
 0104    }
 0105    if (keyboard.sKey.isPressed || keyboard.downArrowKey.isPressed) {
 0106      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Back);
 0107    }
 0108    if (keyboard.dKey.isPressed || keyboard.rightArrowKey.isPressed) {
 0109      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Right);
 0110    }
 0111    if (keyboard.qKey.isPressed) {
 0112      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Up);
 0113    }
 0114    if (keyboard.eKey.isPressed) {
 0115      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Down);
 0116    }
 0117  }
 118
 0119  private void HandleTacticalModeMouseInput() {
 0120    var mouse = Mouse.current;
 121    // Start drag on right mouse button.
 0122    if (mouse.rightButton.wasPressedThisFrame) {
 0123      _isDragging = true;
 0124      _lastMousePosition = mouse.position.ReadValue();
 0125    }
 126    // End drag when button released.
 0127    else if (mouse.rightButton.wasReleasedThisFrame) {
 0128      _isDragging = false;
 0129    }
 130
 131    // Handle dragging.
 0132    if (_isDragging) {
 0133      Vector2 currentMousePos = mouse.position.ReadValue();
 0134      Vector2 delta = currentMousePos - _lastMousePosition;
 0135      TacticalPanel.Instance.Pan(delta);
 0136      _lastMousePosition = currentMousePos;
 0137    }
 0138  }
 139
 0140  private void HandleTacticalModeScrollWheelInput() {
 0141    var mouse = Mouse.current;
 0142    if (mouse.scroll.ReadValue().y != 0) {
 0143      TacticalPanel.Instance.ZoomIn(mouse.scroll.ReadValue().y * 0.001f);
 0144    }
 0145  }
 146
 0147  private void HandleTacticalModeLockableInput() {
 148    // Handle keyboard input for panning.
 0149    var keyboard = Keyboard.current;
 0150    Vector2 keyboardPanDirection = Vector2.zero;
 0151    if (keyboard.wKey.isPressed || keyboard.upArrowKey.isPressed) {
 0152      keyboardPanDirection.y += -1;
 0153    }
 0154    if (keyboard.aKey.isPressed || keyboard.leftArrowKey.isPressed) {
 0155      keyboardPanDirection.x += 1;
 0156    }
 0157    if (keyboard.sKey.isPressed || keyboard.downArrowKey.isPressed) {
 0158      keyboardPanDirection.y += 1;
 0159    }
 0160    if (keyboard.dKey.isPressed || keyboard.rightArrowKey.isPressed) {
 0161      keyboardPanDirection.x += -1;
 0162    }
 163
 0164    if (keyboard.qKey.wasPressedThisFrame) {
 0165      TacticalPanel.Instance.CycleRangeUp();
 0166    }
 0167    if (keyboard.eKey.wasPressedThisFrame) {
 0168      TacticalPanel.Instance.CycleRangeDown();
 0169    }
 170
 0171    if (keyboardPanDirection != Vector2.zero) {
 0172      TacticalPanel.Instance.PanWithKeyboard(keyboardPanDirection.normalized);
 0173    }
 0174  }
 175
 0176  private void HandleNonLockableInput() {
 0177    var keyboard = Keyboard.current;
 0178    if (keyboard.escapeKey.wasPressedThisFrame) {
 0179      SimManager.Instance.QuitSimulation();
 0180    }
 181
 0182    if (keyboard.rKey.wasPressedThisFrame) {
 0183      SimManager.Instance.EndSimulation();
 0184      SimManager.Instance.ResetAndStartSimulation();
 0185    }
 186
 0187    if (keyboard.lKey.wasPressedThisFrame) {
 0188      UIManager.Instance.ToggleConfigSelectorPanel();
 0189    }
 190
 0191    if (keyboard.cKey.wasPressedThisFrame) {
 0192      ParticleManager.Instance.ClearHitMarkers();
 0193    }
 194
 0195    if (keyboard.pKey.wasPressedThisFrame) {
 0196      CameraController.Instance.AutoRotate = !CameraController.Instance.AutoRotate;
 0197    }
 198
 0199    if (keyboard.spaceKey.wasPressedThisFrame) {
 200      // Pause the time.
 0201      if (!SimManager.Instance.IsPaused) {
 0202        SimManager.Instance.PauseSimulation();
 0203      } else {
 0204        SimManager.Instance.ResumeSimulation();
 0205      }
 0206    }
 207
 0208    HandleCameraFollowInput(Key.Digit1, CameraFollowType.ALL_AGENTS);
 0209    HandleCameraFollowInput(Key.Digit2, CameraFollowType.ALL_INTERCEPTORS);
 0210    HandleCameraFollowInput(Key.Digit3, CameraFollowType.ALL_THREATS);
 0211  }
 212
 0213  private void HandleCameraFollowInput(Key key, CameraFollowType followType) {
 0214    var keyboard = Keyboard.current;
 0215    if (keyboard[key].wasPressedThisFrame) {
 0216      if (keyboard.leftCtrlKey.isPressed) {
 0217        CameraController.Instance.Follow(followType);
 0218      } else {
 0219        CameraController.Instance.Snap(followType);
 0220      }
 0221    }
 0222  }
 223}