< Summary

Class:InputProcessor
Assembly:InputController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/InputController/InputProcessor.cs
Covered lines:50
Uncovered lines:23
Coverable lines:73
Total lines:231
Line coverage:68.4% (50 of 73)
Covered branches:0
Total branches:0

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%4.943040%
FromKey(...)0%4.374071.43%
FromKey(...)0%2.062075%
FromMouse(...)0%20400%
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 System;
 3using System.Linq;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.EventSystems;
 7using UnityEngine.UIElements;
 8
 9/// <summary>
 10/// Helper class that wraps the processing of inputs and modifiers to trigger actions events
 11/// </summary>
 12public static class InputProcessor
 13{
 114    private static readonly KeyCode[] MODIFIER_KEYS = { KeyCode.LeftControl, KeyCode.LeftAlt, KeyCode.LeftShift, KeyCode
 15
 16    [Flags]
 17    public enum Modifier
 18    {
 19        //Set the values as bit masks
 20        None = 0b0000000, // No modifier needed
 21        NeedsPointerLocked = 0b0000001, // The pointer must be locked to the game
 22        FocusNotInInput = 0b0000010, // The game focus cannot be in an input field
 23        NotInStartMenu = 0b0000100, // The game focus cannot be in full-screen start menu
 24        OnlyWithInputFocused = 0b0001000 // The game focus must be in an input field
 25    }
 26
 27    /// <summary>
 28    /// Check if the modifier keys are pressed
 29    /// </summary>
 30    /// <param name="modifierKeys"> Keycodes modifiers</param>
 31    /// <returns></returns>
 32    private static bool PassModifierKeys(KeyCode[] modifierKeys)
 33    {
 730292034        for (var i = 0; i < MODIFIER_KEYS.Length; i++)
 35        {
 317258036            var keyCode = MODIFIER_KEYS[i];
 317258037            bool pressed = Input.GetKey(keyCode);
 38
 317258039            if (modifierKeys == null)
 40            {
 239440041                if (pressed)
 042                    return false;
 43            }
 44            else
 45            {
 77818046                var anyModifierKeysActive = false;
 47
 294810548                foreach (var key in modifierKeys)
 49                {
 77818050                    if (key == keyCode)
 51                    {
 16461552                        anyModifierKeysActive = true;
 16461553                        break;
 54                    }
 55                }
 56
 77818057                if (anyModifierKeysActive != pressed)
 16461558                    return false;
 59            }
 60        }
 61
 47888062        return true;
 63    }
 64
 65    /// <summary>
 66    /// Check if a miscellaneous modifiers are present. These modifiers are related to the meta-state of the application
 67    /// they can be anything such as mouse pointer state, where the focus is, camera mode...
 68    /// </summary>
 69    /// <param name="modifiers"></param>
 70    /// <returns></returns>
 71    private static bool PassModifiers(Modifier modifiers)
 72    {
 92783073        if (IsModifierSet(modifiers, Modifier.NeedsPointerLocked) && !DCL.Helpers.Utils.IsCursorLocked)
 7482574            return false;
 75
 85300576        bool isInputFieldFocused = FocusIsInInputField();
 77
 85300578        if (IsModifierSet(modifiers, Modifier.FocusNotInInput) && isInputFieldFocused)
 079            return false;
 80
 85300581        if (IsModifierSet(modifiers, Modifier.OnlyWithInputFocused) && !isInputFieldFocused)
 2993082            return false;
 83
 82307584        if (IsModifierSet(modifiers, Modifier.NotInStartMenu) && IsStartMenuVisible())
 085            return false;
 86
 82307587        return true;
 88    }
 89
 16461590    private static bool IsStartMenuVisible() => DataStore.i.exploreV2.isOpen.Get();
 91
 92    /// <summary>
 93    /// Process an input action mapped to a keyboard key.
 94    /// </summary>
 95    /// <param name="action">Trigger Action to perform</param>
 96    /// <param name="key">KeyCode mapped to this action</param>
 97    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 98    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 99    public static void FromKey(InputAction_Trigger action, KeyCode key, KeyCode[] modifierKeys = null, Modifier modifier
 100    {
 658460101        if (!PassModifiers(modifiers))
 44895102            return;
 103
 613565104        if (!PassModifierKeys(modifierKeys))
 164615105            return;
 106
 448950107        if (Input.GetKeyDown(key))
 0108            action.RaiseOnTriggered();
 448950109    }
 110
 111    /// <summary>
 112    /// Process an input action mapped to a button.
 113    /// </summary>
 114    /// <param name="action">Trigger Action to perform</param>
 115    /// <param name="mouseButtonIdx">Index of the mouse button mapped to this action</param>
 116    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 117    public static void FromMouseButton(InputAction_Trigger action, int mouseButtonIdx, Modifier modifiers = Modifier.Non
 118    {
 0119        if (!PassModifiers(modifiers))
 0120            return;
 121
 0122        if (Input.GetMouseButton(mouseButtonIdx))
 0123            action.RaiseOnTriggered();
 0124    }
 125
 126    public static void FromMouseButtonUp(InputAction_Trigger action, int mouseButtonIdx, Modifier modifiers = Modifier.N
 127    {
 14965128        if (!PassModifiers(modifiers))
 14965129            return;
 130
 0131        if (Input.GetMouseButtonUp(mouseButtonIdx))
 0132            action.RaiseOnTriggered();
 0133    }
 134
 135    /// <summary>
 136    /// Process an input action mapped to a keyboard key
 137    /// </summary>
 138    /// <param name="action">Hold Action to perform</param>
 139    /// <param name="key">KeyCode mapped to this action</param>
 140    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 141    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers = Modifier.None)
 142    {
 179580143        if (!PassModifiers(modifiers))
 14965144            return;
 145
 164615146        if (Input.GetKeyDown(key))
 0147            action.RaiseOnStarted();
 164615148        if (Input.GetKeyUp(key))
 0149            action.RaiseOnFinished();
 164615150    }
 151
 152    /// <summary>
 153    /// Process an input action mapped to a keyboard key
 154    /// </summary>
 155    /// <param name="action">Hold Action to perform</param>
 156    /// <param name="key">KeyCode mapped to this action</param>
 157    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 158    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 159    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers, KeyCode[] modifierKeys)
 160    {
 29930161        if (!PassModifierKeys(modifierKeys))
 0162            return;
 163
 29930164        FromKey(action, key, modifiers);
 29930165    }
 166
 167    /// <summary>
 168    /// Process an input action mapped to a mouse button
 169    /// </summary>
 170    /// <param name="action">Hold Action to perform</param>
 171    /// <param name="mouseButtonIdx">Index of the mouse button</param>
 172    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 173    public static void FromMouse(InputAction_Hold action, int mouseButtonIdx, Modifier modifiers = Modifier.None)
 174    {
 0175        if (!PassModifiers(modifiers))
 0176            return;
 177
 0178        if (Input.GetMouseButtonDown(mouseButtonIdx))
 0179            action.RaiseOnStarted();
 0180        if (Input.GetMouseButtonUp(mouseButtonIdx))
 0181            action.RaiseOnFinished();
 0182    }
 183
 184    /// <summary>
 185    /// Process an input action mapped to an axis
 186    /// </summary>
 187    /// <param name="action">Measurable Action to perform</param>
 188    /// <param name="axisName">Axis name</param>
 189    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 190    public static void FromAxis(InputAction_Measurable action, string axisName, Modifier modifiers = Modifier.None)
 191    {
 74825192        if (!PassModifiers(modifiers))
 193        {
 29930194            action.RaiseOnValueChanged(0);
 29930195            return;
 196        }
 197
 44895198        action.RaiseOnValueChanged(Input.GetAxis(axisName));
 44895199    }
 200
 201    /// <summary>
 202    /// Bitwise check for the modifiers flags
 203    /// </summary>
 204    /// <param name="modifiers">Modifier to check</param>
 205    /// <param name="value">Modifier mapped to a bit to check</param>
 206    /// <returns></returns>
 207    private static bool IsModifierSet(Modifier modifiers, Modifier value)
 208    {
 3456915209        var flagsValue = (int)modifiers;
 3456915210        var flagValue = (int)value;
 211
 3456915212        return (flagsValue & flagValue) != 0;
 213    }
 214
 215    public static bool FocusIsInInputField()
 216    {
 853005217        if (EventSystem.current == null)
 825588218            return false;
 219
 27417220        return EventSystem.current.currentSelectedGameObject != null &&
 221               (EventSystem.current.currentSelectedGameObject.GetComponent<TMP_InputField>() != null ||
 222                EventSystem.current.currentSelectedGameObject.GetComponent<UnityEngine.UI.InputField>() != null ||
 223                FocusIsInTextField(EventSystem.current.currentSelectedGameObject));
 224    }
 225
 226    /// <summary>
 227    /// Checks VisualElement from UI Toolkit
 228    /// </summary>
 229    private static bool FocusIsInTextField(GameObject currentSelectedObject) =>
 0230        currentSelectedObject.GetComponent<PanelEventHandler>()?.panel?.focusController?.focusedElement is TextField;
 231}