< Summary

Class:InputController
Assembly:InputController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/InputController/InputController.cs
Covered lines:106
Uncovered lines:89
Coverable lines:195
Total lines:654
Line coverage:54.3% (106 of 195)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InputController()0%110100%
InputController()0%110100%
Update()0%7.586064.71%
Update_Trigger(...)0%480.5461051.69%
Update_Hold(...)0%101.921043.18%
Update_Measurable(...)0%7.017093.33%
Stop_Measurable(...)0%220100%

File(s)

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

#LineLine coverage
 1using DCL;
 2using System;
 3using System.Linq;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.EventSystems;
 7
 8/// <summary>
 9/// Mapping for Trigger actions
 10/// </summary>
 11public enum DCLAction_Trigger
 12{
 13    //Remember to explicitly assign the value to each entry so we minimize issues with serialization + conflicts
 14    CameraChange = 100,
 15
 16    ToggleNavMap = 110,
 17    ToggleFriends = 120,
 18    CloseWindow = 121,
 19    ToggleWorldChat = 122,
 20    ToggleUIVisibility = 123,
 21    ToggleControlsHud = 124,
 22    ToggleSettings = 125,
 23    ToggleExploreHud = 126,
 24    ToggleVoiceChatRecording = 127,
 25    ToggleAvatarEditorHud = 128,
 26    ToggleQuestsPanelHud = 129,
 27    ToggleAvatarNamesHud = 130,
 28
 29    OpenExpressions = 200,
 30    Expression_Wave = 201,
 31    Expression_FistPump = 202,
 32    Expression_Robot = 203,
 33    Expression_RaiseHand = 204,
 34    Expression_Clap = 205,
 35    Expression_ThrowMoney = 206,
 36    Expression_SendKiss = 207,
 37
 38    //Builder In World 4xx
 39    BuildEditModeChange = 408,
 40    BuildEditModeToggleUI = 409,
 41    BuildEditModeToggleEntityList = 410,
 42    BuildEditModeToggleCatalog = 411,
 43    BuildEditModeToggleSceneInfo = 412,
 44    BuildEditModeToggleChangeCamera = 413,
 45    BuildEditModeToggleControls = 414,
 46    BuildEditModeToggleSnapMode = 415,
 47    BuildEditModeUndoAction = 417,
 48    BuildEditModeRedoAction = 418,
 49    BuildEditModeQuickBar1 = 419,
 50    BuildEditModeQuickBar2 = 420,
 51    BuildEditModeQuickBar3 = 421,
 52    BuildEditModeQuickBar4 = 422,
 53    BuildEditModeQuickBar5 = 423,
 54    BuildEditModeQuickBar6 = 424,
 55    BuildEditModeQuickBar7 = 425,
 56    BuildEditModeQuickBar8 = 426,
 57    BuildEditModeQuickBar9 = 427,
 58    BuildEditModeDuplicate = 428,
 59    BuildEditModeTranslate = 429,
 60    BuildEditModeRotate = 430,
 61    BuildEditModeScale = 431,
 62    BuildEditModeDelete = 434,
 63    BuildEditModeFocusSelectedEntities = 435,
 64    BuildEditModeReset = 443,
 65    BuildEditHideSelectedEntities = 444,
 66    BuildEditShowAllEntities = 445,
 67    BuildEditModeResetCamera = 446,
 68    BuildEditModeZoomIn = 447,
 69    BuildEditModeZoomOut = 448
 70}
 71
 72/// <summary>
 73/// Mapping for hold actions
 74/// </summary>
 75public enum DCLAction_Hold
 76{
 77    //Remember to explicitly assign the value to each entry so we minimize issues with serialization + conflicts
 78    Sprint = 1,
 79    Jump = 2,
 80    FreeCameraMode = 101,
 81    VoiceChatRecording = 102,
 82    DefaultConfirmAction = 300,
 83    DefaultCancelAction = 301,
 84    BuildEditModeMultiSelection = 432,
 85    BuildEditModeSquareMultiSelection = 433,
 86    BuildEditModeFirstPersonRotation = 436,
 87    BuildEditModeCameraAdvanceFoward = 437,
 88    BuildEditModeCameraAdvanceBack = 438,
 89    BuildEditModeCameraAdvanceLeft = 439,
 90    BuildEditModeCameraAdvanceRight = 440,
 91    BuildEditModeCameraAdvanceUp = 441,
 92    BuildEditModeCameraAdvanceDown = 442,
 93    BuildEditModeCameraPan = 446
 94}
 95
 96/// <summary>
 97/// Mapping for measurable actions
 98/// </summary>
 99public enum DCLAction_Measurable
 100{
 101    //Remember to explicitly assign the value to each entry so we minimize issues with serialization + conflicts
 102    CharacterXAxis = 1,
 103    CharacterYAxis = 2,
 104    CameraXAxis = 3,
 105    CameraYAxis = 4,
 106}
 107
 108/// <summary>
 109/// Group of actions currently actived
 110/// </summary>
 111public enum InputTypeMode
 112{
 113    OFF,
 114    GENERAL,
 115    BUILD_MODE_LOADING,
 116    BUILD_MODE
 117}
 118
 119/// <summary>
 120/// Input Controller will map inputs(keys/mouse/axis) to DCL actions, check if they can be triggered (modifiers) and rai
 121/// </summary>
 122public class InputController : MonoBehaviour
 123{
 1124    public static bool ENABLE_THIRD_PERSON_CAMERA = true;
 125
 126    [Header("General Input")]
 127    public InputAction_Trigger[] triggerTimeActions;
 128
 129    public InputAction_Hold[] holdActions;
 130    public InputAction_Measurable[] measurableActions;
 131
 132    [Header("BuildMode Input")]
 133    public InputAction_Trigger[] builderTriggerTimeActions;
 134
 135    public InputAction_Hold[] builderHoldActions;
 136    public InputAction_Trigger[] loadingBuilderTriggerTimeActions;
 137
 20150138    bool renderingEnabled => CommonScriptableObjects.rendererState.Get();
 114304139    bool allUIHidden => CommonScriptableObjects.allUIHidden.Get();
 328140    public InputTypeMode inputTypeMode { get; set; } = InputTypeMode.GENERAL;
 141
 142    private void Update()
 143    {
 20150144        if (!renderingEnabled)
 145        {
 327146            Stop_Measurable(measurableActions);
 327147            return;
 148        }
 149
 19823150        switch (inputTypeMode)
 151        {
 152            case InputTypeMode.OFF:
 0153                Stop_Measurable(measurableActions);
 0154                return;
 155            case InputTypeMode.GENERAL:
 19774156                Update_Trigger(triggerTimeActions);
 19774157                Update_Hold(holdActions);
 19774158                Update_Measurable(measurableActions);
 19774159                break;
 160            case InputTypeMode.BUILD_MODE_LOADING:
 49161                Update_Trigger(loadingBuilderTriggerTimeActions);
 49162                Stop_Measurable(measurableActions);
 49163                break;
 164            case InputTypeMode.BUILD_MODE:
 0165                Update_Trigger(builderTriggerTimeActions);
 0166                Update_Hold(builderHoldActions);
 0167                Update_Measurable(measurableActions);
 168                break;
 169        }
 0170    }
 171
 172    /// <summary>
 173    /// Map the trigger actions to inputs + modifiers and check if their events must be triggered
 174    /// </summary>
 175    public void Update_Trigger(InputAction_Trigger[] triggerTimeActions)
 176    {
 909800177        for (var i = 0; i < triggerTimeActions.Length; i++)
 178        {
 435077179            var action = triggerTimeActions[i];
 180
 435077181            if (action.isTriggerBlocked != null && action.isTriggerBlocked.Get())
 182                continue;
 183
 422057184            switch (action.GetDCLAction())
 185            {
 186                case DCLAction_Trigger.CameraChange:
 187                    //Disable until the fine-tuning is ready
 19774188                    if (ENABLE_THIRD_PERSON_CAMERA)
 19774189                        InputProcessor.FromKey(action, KeyCode.V,
 190                            modifiers: InputProcessor.Modifier.NeedsPointerLocked |
 191                                       InputProcessor.Modifier.FocusNotInInput);
 19774192                    break;
 193                case DCLAction_Trigger.ToggleNavMap:
 18906194                    if (allUIHidden)
 195                        break;
 11251196                    InputProcessor.FromKey(action, KeyCode.M, modifiers: InputProcessor.Modifier.FocusNotInInput);
 11251197                    InputProcessor.FromKey(action, KeyCode.Tab, modifiers: InputProcessor.Modifier.FocusNotInInput);
 11251198                    InputProcessor.FromKey(action, KeyCode.Escape, modifiers: InputProcessor.Modifier.FocusNotInInput);
 11251199                    break;
 200                case DCLAction_Trigger.ToggleFriends:
 18906201                    if (allUIHidden)
 202                        break;
 11251203                    InputProcessor.FromKey(action, KeyCode.L, modifiers: InputProcessor.Modifier.None);
 11251204                    break;
 205                case DCLAction_Trigger.ToggleWorldChat:
 18906206                    if (allUIHidden)
 207                        break;
 11251208                    InputProcessor.FromKey(action, KeyCode.Return, modifiers: InputProcessor.Modifier.None);
 11251209                    break;
 210                case DCLAction_Trigger.ToggleUIVisibility:
 18906211                    InputProcessor.FromKey(action, KeyCode.U, modifiers: InputProcessor.Modifier.None);
 18906212                    break;
 213                case DCLAction_Trigger.CloseWindow:
 19774214                    if (allUIHidden || DataStore.i.isSignUpFlow.Get())
 215                        break;
 11251216                    InputProcessor.FromKey(action, KeyCode.Escape, modifiers: InputProcessor.Modifier.None);
 11251217                    break;
 218                case DCLAction_Trigger.OpenExpressions:
 18906219                    if (allUIHidden)
 220                        break;
 11251221                    InputProcessor.FromKey(action, KeyCode.B, modifiers: InputProcessor.Modifier.FocusNotInInput);
 11251222                    InputProcessor.FromKey(action, KeyCode.Escape, modifiers: InputProcessor.Modifier.FocusNotInInput);
 11251223                    break;
 224                case DCLAction_Trigger.ToggleControlsHud:
 18906225                    InputProcessor.FromKey(action, KeyCode.C, modifiers: InputProcessor.Modifier.FocusNotInInput);
 18906226                    break;
 227                case DCLAction_Trigger.ToggleSettings:
 19774228                    InputProcessor.FromKey(action, KeyCode.P, modifiers: InputProcessor.Modifier.FocusNotInInput);
 19774229                    break;
 230                case DCLAction_Trigger.ToggleExploreHud:
 18906231                    if (allUIHidden)
 232                        break;
 11251233                    InputProcessor.FromKey(action, KeyCode.X, modifiers: InputProcessor.Modifier.FocusNotInInput);
 11251234                    break;
 235                case DCLAction_Trigger.Expression_Wave:
 18906236                    InputProcessor.FromKey(action, KeyCode.Alpha1, modifiers: InputProcessor.Modifier.FocusNotInInput);
 18906237                    break;
 238                case DCLAction_Trigger.Expression_FistPump:
 18906239                    InputProcessor.FromKey(action, KeyCode.Alpha2, modifiers: InputProcessor.Modifier.FocusNotInInput);
 18906240                    break;
 241                case DCLAction_Trigger.Expression_Robot:
 18906242                    InputProcessor.FromKey(action, KeyCode.Alpha3, modifiers: InputProcessor.Modifier.FocusNotInInput);
 18906243                    break;
 244                case DCLAction_Trigger.Expression_RaiseHand:
 18906245                    InputProcessor.FromKey(action, KeyCode.Alpha4, modifiers: InputProcessor.Modifier.FocusNotInInput);
 18906246                    break;
 247                case DCLAction_Trigger.Expression_Clap:
 18906248                    InputProcessor.FromKey(action, KeyCode.Alpha5, modifiers: InputProcessor.Modifier.FocusNotInInput);
 18906249                    break;
 250                case DCLAction_Trigger.Expression_ThrowMoney:
 18906251                    InputProcessor.FromKey(action, KeyCode.Alpha6, modifiers: InputProcessor.Modifier.FocusNotInInput);
 18906252                    break;
 253                case DCLAction_Trigger.Expression_SendKiss:
 18906254                    InputProcessor.FromKey(action, KeyCode.Alpha7, modifiers: InputProcessor.Modifier.FocusNotInInput);
 18906255                    break;
 256                case DCLAction_Trigger.BuildEditModeChange:
 19823257                    InputProcessor.FromKey(action, KeyCode.K, modifiers: InputProcessor.Modifier.FocusNotInInput);
 19823258                    break;
 259                case DCLAction_Trigger.ToggleVoiceChatRecording:
 19774260                    InputProcessor.FromKey(action, KeyCode.T, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 19774261                    break;
 262                case DCLAction_Trigger.ToggleAvatarEditorHud:
 18906263                    InputProcessor.FromKey(action, KeyCode.I, modifiers: InputProcessor.Modifier.FocusNotInInput);
 18906264                    break;
 265                case DCLAction_Trigger.BuildEditModeToggleUI:
 0266                    InputProcessor.FromKey(action, KeyCode.U, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0267                    break;
 268                case DCLAction_Trigger.BuildEditModeToggleChangeCamera:
 0269                    InputProcessor.FromKey(action, KeyCode.V, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0270                    break;
 271                case DCLAction_Trigger.BuildEditModeToggleControls:
 0272                    InputProcessor.FromKey(action, KeyCode.C, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0273                    break;
 274                case DCLAction_Trigger.BuildEditModeToggleSnapMode:
 0275                    InputProcessor.FromKey(action, KeyCode.O, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0276                    break;
 277                case DCLAction_Trigger.BuildEditModeRedoAction:
 0278                    InputProcessor.FromKey(action, KeyCode.Y, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0279                    break;
 280                case DCLAction_Trigger.BuildEditModeUndoAction:
 0281                    InputProcessor.FromKey(action, KeyCode.Z, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0282                    break;
 283                case DCLAction_Trigger.BuildEditModeQuickBar1:
 0284                    InputProcessor.FromKey(action, KeyCode.Alpha1, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0285                    break;
 286                case DCLAction_Trigger.BuildEditModeQuickBar2:
 0287                    InputProcessor.FromKey(action, KeyCode.Alpha2, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0288                    break;
 289                case DCLAction_Trigger.BuildEditModeQuickBar3:
 0290                    InputProcessor.FromKey(action, KeyCode.Alpha3, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0291                    break;
 292                case DCLAction_Trigger.BuildEditModeQuickBar4:
 0293                    InputProcessor.FromKey(action, KeyCode.Alpha4, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0294                    break;
 295                case DCLAction_Trigger.BuildEditModeQuickBar5:
 0296                    InputProcessor.FromKey(action, KeyCode.Alpha5, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0297                    break;
 298                case DCLAction_Trigger.BuildEditModeQuickBar6:
 0299                    InputProcessor.FromKey(action, KeyCode.Alpha6, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0300                    break;
 301                case DCLAction_Trigger.BuildEditModeQuickBar7:
 0302                    InputProcessor.FromKey(action, KeyCode.Alpha7, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0303                    break;
 304                case DCLAction_Trigger.BuildEditModeQuickBar8:
 0305                    InputProcessor.FromKey(action, KeyCode.Alpha8, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0306                    break;
 307                case DCLAction_Trigger.BuildEditModeQuickBar9:
 0308                    InputProcessor.FromKey(action, KeyCode.Alpha9, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0309                    break;
 310                case DCLAction_Trigger.BuildEditModeDelete:
 0311                    InputProcessor.FromKey(action, KeyCode.Delete, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0312                    InputProcessor.FromKey(action, KeyCode.Backspace, modifiers: InputProcessor.Modifier.FocusNotInInput
 0313                    break;
 314                case DCLAction_Trigger.BuildEditModeDuplicate:
 0315                    InputProcessor.FromKey(action, KeyCode.D, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0316                    break;
 317                case DCLAction_Trigger.BuildEditModeTranslate:
 0318                    InputProcessor.FromKey(action, KeyCode.G, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0319                    InputProcessor.FromKey(action, KeyCode.M, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0320                    break;
 321                case DCLAction_Trigger.BuildEditModeRotate:
 0322                    InputProcessor.FromKey(action, KeyCode.R, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0323                    break;
 324                case DCLAction_Trigger.BuildEditModeScale:
 0325                    InputProcessor.FromKey(action, KeyCode.S, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0326                    break;
 327                case DCLAction_Trigger.BuildEditModeFocusSelectedEntities:
 0328                    InputProcessor.FromKey(action, KeyCode.F, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0329                    break;
 330                case DCLAction_Trigger.BuildEditModeReset:
 0331                    InputProcessor.FromKey(action, KeyCode.R, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0332                    break;
 333                case DCLAction_Trigger.BuildEditHideSelectedEntities:
 0334                    InputProcessor.FromKey(action, KeyCode.H, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0335                    break;
 336                case DCLAction_Trigger.BuildEditShowAllEntities:
 0337                    InputProcessor.FromKey(action, KeyCode.H, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0338                    break;
 339                case DCLAction_Trigger.BuildEditModeResetCamera:
 0340                    InputProcessor.FromKey(action, KeyCode.C, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0341                    break;
 342                case DCLAction_Trigger.BuildEditModeZoomIn:
 0343                    InputProcessor.FromKey(action, KeyCode.KeypadPlus, modifiers: InputProcessor.Modifier.FocusNotInInpu
 0344                    break;
 345                case DCLAction_Trigger.BuildEditModeZoomOut:
 0346                    InputProcessor.FromKey(action, KeyCode.KeypadMinus, modifiers: InputProcessor.Modifier.FocusNotInInp
 0347                    break;
 348                case DCLAction_Trigger.ToggleQuestsPanelHud:
 19774349                    InputProcessor.FromKey(action, KeyCode.J, modifiers: InputProcessor.Modifier.FocusNotInInput);
 19774350                    break;
 351                case DCLAction_Trigger.ToggleAvatarNamesHud:
 19774352                    InputProcessor.FromKey(action, KeyCode.N, modifiers: InputProcessor.Modifier.FocusNotInInput);
 19774353                    break;
 354                default:
 0355                    throw new ArgumentOutOfRangeException();
 356            }
 357        }
 19823358    }
 359
 360    /// <summary>
 361    /// Map the hold actions to inputs + modifiers and check if their events must be triggered
 362    /// </summary>
 363    private void Update_Hold(InputAction_Hold[] holdActions)
 364    {
 316384365        for (var i = 0; i < holdActions.Length; i++)
 366        {
 138418367            var action = holdActions[i];
 138418368            switch (action.GetDCLAction())
 369            {
 370                case DCLAction_Hold.Sprint:
 19774371                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.NeedsPointerLocked);
 19774372                    break;
 373                case DCLAction_Hold.Jump:
 19774374                    InputProcessor.FromKey(action, KeyCode.Space, InputProcessor.Modifier.NeedsPointerLocked);
 19774375                    break;
 376                case DCLAction_Hold.FreeCameraMode:
 377                    //Disable until the fine-tuning is ready
 19774378                    if (ENABLE_THIRD_PERSON_CAMERA)
 19774379                        InputProcessor.FromKey(action, KeyCode.Y, InputProcessor.Modifier.NeedsPointerLocked);
 19774380                    break;
 381                case DCLAction_Hold.VoiceChatRecording:
 382                    // Push to talk functionality only triggers if no modifier key is pressed
 39548383                    InputProcessor.FromKey(action, KeyCode.T, InputProcessor.Modifier.FocusNotInInput, null);
 39548384                    break;
 385                case DCLAction_Hold.DefaultConfirmAction:
 19774386                    InputProcessor.FromKey(action, KeyCode.E, InputProcessor.Modifier.None);
 19774387                    break;
 388                case DCLAction_Hold.DefaultCancelAction:
 19774389                    InputProcessor.FromKey(action, KeyCode.F, InputProcessor.Modifier.None);
 19774390                    break;
 391                case DCLAction_Hold.BuildEditModeMultiSelection:
 0392                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 0393                    break;
 394                case DCLAction_Hold.BuildEditModeSquareMultiSelection:
 0395                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 0396                    break;
 397                case DCLAction_Hold.BuildEditModeFirstPersonRotation:
 0398                    InputProcessor.FromKey(action, KeyCode.R, InputProcessor.Modifier.FocusNotInInput);
 0399                    break;
 400                case DCLAction_Hold.BuildEditModeCameraAdvanceFoward:
 0401                    InputProcessor.FromKey(action, KeyCode.UpArrow, InputProcessor.Modifier.FocusNotInInput);
 0402                    InputProcessor.FromKey(action, KeyCode.W, InputProcessor.Modifier.FocusNotInInput);
 0403                    break;
 404                case DCLAction_Hold.BuildEditModeCameraAdvanceBack:
 0405                    InputProcessor.FromKey(action, KeyCode.DownArrow, InputProcessor.Modifier.FocusNotInInput);
 0406                    InputProcessor.FromKey(action, KeyCode.S, InputProcessor.Modifier.FocusNotInInput);
 0407                    break;
 408                case DCLAction_Hold.BuildEditModeCameraAdvanceLeft:
 0409                    InputProcessor.FromKey(action, KeyCode.LeftArrow, InputProcessor.Modifier.FocusNotInInput);
 0410                    InputProcessor.FromKey(action, KeyCode.A, InputProcessor.Modifier.FocusNotInInput);
 0411                    break;
 412                case DCLAction_Hold.BuildEditModeCameraAdvanceRight:
 0413                    InputProcessor.FromKey(action, KeyCode.RightArrow, InputProcessor.Modifier.FocusNotInInput);
 0414                    InputProcessor.FromKey(action, KeyCode.D, InputProcessor.Modifier.FocusNotInInput);
 0415                    break;
 416                case DCLAction_Hold.BuildEditModeCameraAdvanceUp:
 0417                    InputProcessor.FromKey(action, KeyCode.E, InputProcessor.Modifier.FocusNotInInput);
 0418                    break;
 419                case DCLAction_Hold.BuildEditModeCameraAdvanceDown:
 0420                    InputProcessor.FromKey(action, KeyCode.Q, InputProcessor.Modifier.FocusNotInInput);
 0421                    break;
 422                case DCLAction_Hold.BuildEditModeCameraPan:
 0423                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 0424                    break;
 425                default:
 0426                    throw new ArgumentOutOfRangeException();
 427            }
 428        }
 19774429    }
 430
 431    /// <summary>
 432    /// Map the measurable actions to inputs + modifiers and check if their events must be triggered
 433    /// </summary>
 434    private void Update_Measurable(InputAction_Measurable[] measurableActions)
 435    {
 197740436        for (var i = 0; i < measurableActions.Length; i++)
 437        {
 79096438            var action = measurableActions[i];
 79096439            switch (action.GetDCLAction())
 440            {
 441                case DCLAction_Measurable.CharacterXAxis:
 19774442                    InputProcessor.FromAxis(action, "Horizontal", InputProcessor.Modifier.NeedsPointerLocked);
 19774443                    break;
 444                case DCLAction_Measurable.CharacterYAxis:
 19774445                    InputProcessor.FromAxis(action, "Vertical", InputProcessor.Modifier.NeedsPointerLocked);
 19774446                    break;
 447                case DCLAction_Measurable.CameraXAxis:
 19774448                    InputProcessor.FromAxis(action, "Mouse X", InputProcessor.Modifier.NeedsPointerLocked);
 19774449                    break;
 450                case DCLAction_Measurable.CameraYAxis:
 19774451                    InputProcessor.FromAxis(action, "Mouse Y", InputProcessor.Modifier.NeedsPointerLocked);
 19774452                    break;
 453                default:
 0454                    throw new ArgumentOutOfRangeException();
 455            }
 456        }
 19774457    }
 458
 459    private void Stop_Measurable(InputAction_Measurable[] measurableActions)
 460    {
 3760461        for (var i = 0; i < measurableActions.Length; i++)
 462        {
 1504463            measurableActions[i].RaiseOnValueChanged(0);
 464        }
 376465    }
 466}
 467
 468/// <summary>
 469/// Helper class that wraps the processing of inputs and modifiers to trigger actions events
 470/// </summary>
 471public static class InputProcessor
 472{
 473    private static readonly KeyCode[] MODIFIER_KEYS = new[] { KeyCode.LeftControl, KeyCode.LeftAlt, KeyCode.LeftShift, K
 474
 475    [Flags]
 476    public enum Modifier
 477    {
 478        //Set the values as bit masks
 479        None = 0b0000000, // No modifier needed
 480        NeedsPointerLocked = 0b0000001, // The pointer must be locked to the game
 481        FocusNotInInput = 0b0000010, // The game focus cannot be in an input field
 482    }
 483
 484    /// <summary>
 485    /// Check if the modifier keys are pressed
 486    /// </summary>
 487    /// <param name="modifierKeys"> Keycodes modifiers</param>
 488    /// <returns></returns>
 489    public static Boolean PassModifierKeys(KeyCode[] modifierKeys)
 490    {
 491        for (var i = 0; i < MODIFIER_KEYS.Length; i++)
 492        {
 493            var keyCode = MODIFIER_KEYS[i];
 494            var pressed = Input.GetKey(keyCode);
 495            if (modifierKeys == null)
 496            {
 497                if (pressed)
 498                    return false;
 499            }
 500            else
 501            {
 502                if (modifierKeys.Contains(keyCode) != pressed)
 503                    return false;
 504            }
 505        }
 506
 507        return true;
 508    }
 509
 510    /// <summary>
 511    /// Check if a miscellaneous modifiers are present. These modifiers are related to the meta-state of the application
 512    /// they can be anything such as mouse pointer state, where the focus is, camera mode...
 513    /// </summary>
 514    /// <param name="modifiers"></param>
 515    /// <returns></returns>
 516    public static bool PassModifiers(Modifier modifiers)
 517    {
 518        if (IsModifierSet(modifiers, Modifier.NeedsPointerLocked) && !DCL.Helpers.Utils.isCursorLocked)
 519            return false;
 520
 521        if (IsModifierSet(modifiers, Modifier.FocusNotInInput) && FocusIsInInputField())
 522            return false;
 523
 524        return true;
 525    }
 526
 527    /// <summary>
 528    /// Process an input action mapped to a keyboard key.
 529    /// </summary>
 530    /// <param name="action">Trigger Action to perform</param>
 531    /// <param name="key">KeyCode mapped to this action</param>
 532    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 533    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 534    public static void FromKey(InputAction_Trigger action, KeyCode key, KeyCode[] modifierKeys = null,
 535        Modifier modifiers = Modifier.None)
 536    {
 537        if (!PassModifiers(modifiers))
 538            return;
 539
 540        if (!PassModifierKeys(modifierKeys))
 541            return;
 542
 543        if (Input.GetKeyDown(key))
 544            action.RaiseOnTriggered();
 545    }
 546
 547    /// <summary>
 548    /// Process an input action mapped to a button.
 549    /// </summary>
 550    /// <param name="action">Trigger Action to perform</param>
 551    /// <param name="mouseButtonIdx">Index of the mouse button mapped to this action</param>
 552    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 553    public static void FromMouseButton(InputAction_Trigger action, int mouseButtonIdx,
 554        Modifier modifiers = Modifier.None)
 555    {
 556        if (!PassModifiers(modifiers))
 557            return;
 558
 559        if (Input.GetMouseButton(mouseButtonIdx))
 560            action.RaiseOnTriggered();
 561    }
 562
 563    /// <summary>
 564    /// Process an input action mapped to a keyboard key
 565    /// </summary>
 566    /// <param name="action">Hold Action to perform</param>
 567    /// <param name="key">KeyCode mapped to this action</param>
 568    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 569    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers = Modifier.None)
 570    {
 571        if (!PassModifiers(modifiers))
 572            return;
 573
 574        if (Input.GetKeyDown(key))
 575            action.RaiseOnStarted();
 576        if (Input.GetKeyUp(key))
 577            action.RaiseOnFinished();
 578    }
 579
 580    /// <summary>
 581    /// Process an input action mapped to a keyboard key
 582    /// </summary>
 583    /// <param name="action">Hold Action to perform</param>
 584    /// <param name="key">KeyCode mapped to this action</param>
 585    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 586    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 587    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers, KeyCode[] modifierKeys)
 588    {
 589        if (!PassModifierKeys(modifierKeys))
 590            return;
 591
 592        FromKey(action, key, modifiers);
 593    }
 594
 595    /// <summary>
 596    /// Process an input action mapped to a mouse button
 597    /// </summary>
 598    /// <param name="action">Hold Action to perform</param>
 599    /// <param name="mouseButtonIdx">Index of the mouse button</param>
 600    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 601    public static void FromMouse(InputAction_Hold action, int mouseButtonIdx, Modifier modifiers = Modifier.None)
 602    {
 603        if (!PassModifiers(modifiers))
 604            return;
 605
 606        if (Input.GetMouseButtonDown(mouseButtonIdx))
 607            action.RaiseOnStarted();
 608        if (Input.GetMouseButtonUp(mouseButtonIdx))
 609            action.RaiseOnFinished();
 610    }
 611
 612    /// <summary>
 613    /// Process an input action mapped to an axis
 614    /// </summary>
 615    /// <param name="action">Measurable Action to perform</param>
 616    /// <param name="axisName">Axis name</param>
 617    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 618    public static void FromAxis(InputAction_Measurable action, string axisName, Modifier modifiers = Modifier.None)
 619    {
 620        if (!PassModifiers(modifiers))
 621        {
 622            action.RaiseOnValueChanged(0);
 623            return;
 624        }
 625
 626        action.RaiseOnValueChanged(Input.GetAxis(axisName));
 627    }
 628
 629    /// <summary>
 630    /// Bitwise check for the modifiers flags
 631    /// </summary>
 632    /// <param name="modifiers">Modifier to check</param>
 633    /// <param name="value">Modifier mapped to a bit to check</param>
 634    /// <returns></returns>
 635    public static bool IsModifierSet(Modifier modifiers, Modifier value)
 636    {
 637        int flagsValue = (int)modifiers;
 638        int flagValue = (int)value;
 639
 640        return (flagsValue & flagValue) != 0;
 641    }
 642
 643    public static bool FocusIsInInputField()
 644    {
 645        if (EventSystem.current.currentSelectedGameObject != null &&
 646            (EventSystem.current.currentSelectedGameObject.GetComponent<TMP_InputField>() != null ||
 647             EventSystem.current.currentSelectedGameObject.GetComponent<UnityEngine.UI.InputField>() != null))
 648        {
 649            return true;
 650        }
 651
 652        return false;
 653    }
 654}