< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InputManager()0%2100%
Awake()0%3.473062.5%
Update()0%110100%
HandleInput()0%220100%
HandleLockableInput()0%13.747048.39%
Handle3DModeMouseInput()0%4.123050%
Handle3DModeScrollWheelInput()0%2.312057.14%
Handle3DModeLockableInput()0%45.9212038.24%
HandleTacticalModeMouseInput()0%20400%
HandleTacticalModeScrollWheelInput()0%6200%
HandleTacticalModeLockableInput()0%1561200%
HandleNonLockableInput()0%27.748032.43%
HandleCameraFollowInput(...)0%5.993030.77%

File(s)

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

#LineLine coverage
 1using UnityEngine;
 2using UnityEngine.InputSystem;
 3
 4public class InputManager : MonoBehaviour {
 25  public static InputManager Instance { get; private set; }
 6
 297  public bool MouseActive { get; set; } = true;
 298  public bool LockUserInput { get; set; } = false;
 9
 10  private Vector2 _lastMousePosition;
 011  private bool _isDragging = false;
 12
 113  private void Awake() {
 114    if (Instance != null && Instance != this) {
 015      Destroy(gameObject);
 016      return;
 17    }
 118    Instance = this;
 119    DontDestroyOnLoad(gameObject);
 120  }
 21
 2922  private void Update() {
 2923    HandleInput();
 2924  }
 25
 2926  private void HandleInput() {
 5827    if (!LockUserInput) {
 2928      HandleLockableInput();
 2929    }
 2930    HandleNonLockableInput();
 2931  }
 32
 2933  private void HandleLockableInput() {
 5834    if (MouseActive) {
 2935      switch (UIManager.Instance.UIMode) {
 2936        case UIMode.THREE_DIMENSIONAL: {
 2937          Handle3DModeMouseInput();
 2938          Handle3DModeScrollWheelInput();
 2939          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      }
 2951    }
 52
 2953    switch (UIManager.Instance.UIMode) {
 2954      case UIMode.THREE_DIMENSIONAL: {
 2955        Handle3DModeLockableInput();
 2956        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
 2968    if (Keyboard.current.tabKey.wasPressedThisFrame) {
 069      UIManager.Instance.ToggleUIMode();
 070    }
 2971  }
 72
 2973  private void Handle3DModeMouseInput() {
 2974    var mouse = Mouse.current;
 2975    Vector2 delta = mouse.delta.ReadValue();
 2976    if (mouse.leftButton.isPressed) {
 077      CameraController.Instance.OrbitCamera(delta.x, delta.y);
 2978    } else if (mouse.rightButton.isPressed) {
 079      CameraController.Instance.RotateCamera(delta.x, delta.y);
 080    }
 2981  }
 82
 2983  private void Handle3DModeScrollWheelInput() {
 2984    var mouse = Mouse.current;
 2985    if (mouse.scroll.ReadValue().y != 0) {
 086      CameraController.Instance.ZoomCamera(mouse.scroll.ReadValue().y * 5);
 087    }
 2988  }
 89
 2990  private void Handle3DModeLockableInput() {
 2991    var keyboard = Keyboard.current;
 2992    if (keyboard.leftShiftKey.isPressed) {
 093      CameraController.Instance.CameraSpeed = CameraController.Instance.CameraSpeedMax;
 2994    } else {
 2995      CameraController.Instance.CameraSpeed = CameraController.Instance.CameraSpeedNormal;
 2996    }
 97
 98    // Translational movement.
 2999    if (keyboard.wKey.isPressed || keyboard.upArrowKey.isPressed) {
 0100      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Forward);
 0101    }
 29102    if (keyboard.aKey.isPressed || keyboard.leftArrowKey.isPressed) {
 0103      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Left);
 0104    }
 29105    if (keyboard.sKey.isPressed || keyboard.downArrowKey.isPressed) {
 0106      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Back);
 0107    }
 29108    if (keyboard.dKey.isPressed || keyboard.rightArrowKey.isPressed) {
 0109      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Right);
 0110    }
 29111    if (keyboard.qKey.isPressed) {
 0112      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Up);
 0113    }
 29114    if (keyboard.eKey.isPressed) {
 0115      CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Down);
 0116    }
 29117  }
 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
 29176  private void HandleNonLockableInput() {
 29177    var keyboard = Keyboard.current;
 29178    if (keyboard.escapeKey.wasPressedThisFrame) {
 0179      SimManager.Instance.QuitSimulation();
 0180    }
 181
 29182    if (keyboard.rKey.wasPressedThisFrame) {
 0183      SimManager.Instance.EndSimulation();
 0184      SimManager.Instance.ResetAndStartSimulation();
 0185    }
 186
 29187    if (keyboard.lKey.wasPressedThisFrame) {
 0188      UIManager.Instance.ToggleConfigSelectorPanel();
 0189    }
 190
 29191    if (keyboard.cKey.wasPressedThisFrame) {
 0192      ParticleManager.Instance.ClearHitMarkers();
 0193    }
 194
 29195    if (keyboard.pKey.wasPressedThisFrame) {
 0196      CameraController.Instance.AutoRotate = !CameraController.Instance.AutoRotate;
 0197    }
 198
 29199    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
 29208    HandleCameraFollowInput(Key.Digit1, CameraFollowType.ALL_AGENTS);
 29209    HandleCameraFollowInput(Key.Digit2, CameraFollowType.ALL_INTERCEPTORS);
 29210    HandleCameraFollowInput(Key.Digit3, CameraFollowType.ALL_THREATS);
 29211  }
 212
 87213  private void HandleCameraFollowInput(Key key, CameraFollowType followType) {
 87214    var keyboard = Keyboard.current;
 87215    if (keyboard[key].wasPressedThisFrame) {
 0216      if (keyboard.leftCtrlKey.isPressed) {
 0217        CameraController.Instance.Follow(followType);
 0218      } else {
 0219        CameraController.Instance.Snap(followType);
 0220      }
 0221    }
 87222  }
 223}