< Summary

Class:InputProcessor
Assembly:InputController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/InputController/InputProcessor.cs
Covered lines:56
Uncovered lines:17
Coverable lines:73
Total lines:232
Line coverage:76.7% (56 of 73)
Covered branches:0
Total branches:0
Covered methods:12
Total methods:14
Method coverage:85.7% (12 of 14)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InputProcessor()0%110100%
PassModifierKeys(...)0%7.017094.44%
PassModifiers(...)0%8.518080%
IsStartMenuVisible()0%110100%
FromKey(...)0%4.054085.71%
FromMouseButton(...)0%12300%
FromMouseButtonUp(...)0%3.073080%
FromKey(...)0%4.374071.43%
FromKey(...)0%2.062075%
FromMouse(...)0%5.264057.14%
FromAxis(...)0%220100%
IsModifierSet(...)0%110100%
FocusIsInInputField()0%550100%
FocusIsInTextField(...)0%56700%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/InputController/InputProcessor.cs

#LineLine coverage
 1using DCL;
 2using DCL.Helpers;
 3using System;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.EventSystems;
 7using UnityEngine.UI;
 8using UnityEngine.UIElements;
 9
 10/// <summary>
 11/// Helper class that wraps the processing of inputs and modifiers to trigger actions events
 12/// </summary>
 13public static class InputProcessor
 14{
 115    private static readonly KeyCode[] MODIFIER_KEYS = { KeyCode.LeftControl, KeyCode.LeftAlt, KeyCode.LeftShift, KeyCode
 16
 17    [Flags]
 18    public enum Modifier
 19    {
 20        //Set the values as bit masks
 21        None = 0b0000000, // No modifier needed
 22        NeedsPointerLocked = 0b0000001, // The pointer must be locked to the game
 23        FocusNotInInput = 0b0000010, // The game focus cannot be in an input field
 24        NotInStartMenu = 0b0000100, // The game focus cannot be in full-screen start menu
 25        OnlyWithInputFocused = 0b0001000, // The game focus must be in an input field
 26    }
 27
 28    /// <summary>
 29    /// Check if the modifier keys are pressed
 30    /// </summary>
 31    /// <param name="modifierKeys"> Keycodes modifiers</param>
 32    /// <returns></returns>
 33    private static bool PassModifierKeys(KeyCode[] modifierKeys)
 34    {
 346207235        for (var i = 0; i < MODIFIER_KEYS.Length; i++)
 36        {
 149497237            var keyCode = MODIFIER_KEYS[i];
 149497238            bool pressed = Input.GetKey(keyCode);
 39
 149497240            if (modifierKeys == null)
 41            {
 118032042                if (pressed)
 043                    return false;
 44            }
 45            else
 46            {
 31465247                var anyModifierKeysActive = false;
 48
 119204749                foreach (var key in modifierKeys)
 50                {
 31465251                    if (key == keyCode)
 52                    {
 6656153                        anyModifierKeysActive = true;
 6656154                        break;
 55                    }
 56                }
 57
 31465258                if (anyModifierKeysActive != pressed)
 6656159                    return false;
 60            }
 61        }
 62
 23606463        return true;
 64    }
 65
 66    /// <summary>
 67    /// Check if a miscellaneous modifiers are present. These modifiers are related to the meta-state of the application
 68    /// they can be anything such as mouse pointer state, where the focus is, camera mode...
 69    /// </summary>
 70    /// <param name="modifiers"></param>
 71    /// <returns></returns>
 72    private static bool PassModifiers(Modifier modifiers)
 73    {
 48408074        if (IsModifierSet(modifiers, Modifier.NeedsPointerLocked) && !Utils.IsCursorLocked)
 2988075            return false;
 76
 45420077        bool isInputFieldFocused = FocusIsInInputField();
 78
 45420079        if (IsModifierSet(modifiers, Modifier.FocusNotInInput) && isInputFieldFocused)
 080            return false;
 81
 45420082        if (IsModifierSet(modifiers, Modifier.OnlyWithInputFocused) && !isInputFieldFocused)
 1210283            return false;
 84
 44209885        if (IsModifierSet(modifiers, Modifier.NotInStartMenu) && IsStartMenuVisible())
 086            return false;
 87
 44209888        return true;
 89    }
 90
 7866391    private static bool IsStartMenuVisible() => DataStore.i.exploreV2.isOpen.Get();
 92
 93    /// <summary>
 94    /// Process an input action mapped to a keyboard key.
 95    /// </summary>
 96    /// <param name="action">Trigger Action to perform</param>
 97    /// <param name="key">KeyCode mapped to this action</param>
 98    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 99    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 100    public static void FromKey(InputAction_Trigger action, KeyCode key, KeyCode[] modifierKeys = null, Modifier modifier
 101    {
 308601102        if (!PassModifiers(modifiers))
 18078103            return;
 104
 290523105        if (!PassModifierKeys(modifierKeys))
 66561106            return;
 107
 223962108        if (Input.GetKeyDown(key))
 0109            action.RaiseOnTriggered();
 223962110    }
 111
 112    /// <summary>
 113    /// Process an input action mapped to a button.
 114    /// </summary>
 115    /// <param name="action">Trigger Action to perform</param>
 116    /// <param name="mouseButtonIdx">Index of the mouse button mapped to this action</param>
 117    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 118    public static void FromMouseButton(InputAction_Trigger action, int mouseButtonIdx, Modifier modifiers = Modifier.Non
 119    {
 0120        if (!PassModifiers(modifiers))
 0121            return;
 122
 0123        if (Input.GetMouseButton(mouseButtonIdx))
 0124            action.RaiseOnTriggered();
 0125    }
 126
 127    public static void FromMouseButtonUp(InputAction_Trigger action, int mouseButtonIdx, Modifier modifiers = Modifier.N
 128    {
 6051129        if (!PassModifiers(modifiers))
 5976130            return;
 131
 75132        if (Input.GetMouseButtonUp(mouseButtonIdx))
 0133            action.RaiseOnTriggered();
 75134    }
 135
 136    /// <summary>
 137    /// Process an input action mapped to a keyboard key
 138    /// </summary>
 139    /// <param name="action">Hold Action to perform</param>
 140    /// <param name="key">KeyCode mapped to this action</param>
 141    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 142    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers = Modifier.None)
 143    {
 96816144        if (!PassModifiers(modifiers))
 5976145            return;
 146
 90840147        if (Input.GetKeyDown(key))
 0148            action.RaiseOnStarted();
 90840149        if (Input.GetKeyUp(key))
 0150            action.RaiseOnFinished();
 90840151    }
 152
 153    /// <summary>
 154    /// Process an input action mapped to a keyboard key
 155    /// </summary>
 156    /// <param name="action">Hold Action to perform</param>
 157    /// <param name="key">KeyCode mapped to this action</param>
 158    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 159    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 160    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers, KeyCode[] modifierKeys)
 161    {
 12102162        if (!PassModifierKeys(modifierKeys))
 0163            return;
 164
 12102165        FromKey(action, key, modifiers);
 12102166    }
 167
 168    /// <summary>
 169    /// Process an input action mapped to a mouse button
 170    /// </summary>
 171    /// <param name="action">Hold Action to perform</param>
 172    /// <param name="mouseButtonIdx">Index of the mouse button</param>
 173    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 174    public static void FromMouse(InputAction_Hold action, int mouseButtonIdx, Modifier modifiers = Modifier.None)
 175    {
 18153176        if (!PassModifiers(modifiers))
 0177            return;
 178
 18153179        if (Input.GetMouseButtonDown(mouseButtonIdx))
 0180            action.RaiseOnStarted();
 18153181        if (Input.GetMouseButtonUp(mouseButtonIdx))
 0182            action.RaiseOnFinished();
 18153183    }
 184
 185    /// <summary>
 186    /// Process an input action mapped to an axis
 187    /// </summary>
 188    /// <param name="action">Measurable Action to perform</param>
 189    /// <param name="axisName">Axis name</param>
 190    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 191    public static void FromAxis(InputAction_Measurable action, string axisName, Modifier modifiers = Modifier.None)
 192    {
 54459193        if (!PassModifiers(modifiers))
 194        {
 11952195            action.SetValue(0);
 11952196            return;
 197        }
 198
 42507199        action.SetValue(Input.GetAxis(axisName));
 42507200    }
 201
 202    /// <summary>
 203    /// Bitwise check for the modifiers flags
 204    /// </summary>
 205    /// <param name="modifiers">Modifier to check</param>
 206    /// <param name="value">Modifier mapped to a bit to check</param>
 207    /// <returns></returns>
 208    private static bool IsModifierSet(Modifier modifiers, Modifier value)
 209    {
 1834578210        var flagsValue = (int)modifiers;
 1834578211        var flagValue = (int)value;
 212
 1834578213        return (flagsValue & flagValue) != 0;
 214    }
 215
 216    public static bool FocusIsInInputField()
 217    {
 454200218        if (EventSystem.current == null)
 427350219            return false;
 220
 26850221        return EventSystem.current.currentSelectedGameObject != null &&
 222               (EventSystem.current.currentSelectedGameObject.GetComponent<TMP_InputField>() != null ||
 223                EventSystem.current.currentSelectedGameObject.GetComponent<InputField>() != null ||
 224                FocusIsInTextField(EventSystem.current.currentSelectedGameObject));
 225    }
 226
 227    /// <summary>
 228    /// Checks VisualElement from UI Toolkit
 229    /// </summary>
 230    private static bool FocusIsInTextField(GameObject currentSelectedObject) =>
 0231        currentSelectedObject.GetComponent<PanelEventHandler>()?.panel?.focusController?.focusedElement is TextField;
 232}