< Summary

Class:InputController
Assembly:InputController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/InputController/InputController.cs
Covered lines:105
Uncovered lines:89
Coverable lines:194
Total lines:662
Line coverage:54.1% (105 of 194)
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%506.5362051.28%
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;
 7using DCL.Configuration;
 8
 9/// <summary>
 10/// Mapping for Trigger actions
 11/// </summary>
 12public enum DCLAction_Trigger
 13{
 14    //Remember to explicitly assign the value to each entry so we minimize issues with serialization + conflicts
 15    CameraChange = 100,
 16
 17    ToggleNavMap = 110,
 18    ToggleFriends = 120,
 19    CloseWindow = 121,
 20    ToggleWorldChat = 122,
 21    ToggleUIVisibility = 123,
 22    ToggleControlsHud = 124,
 23    ToggleSettings = 125,
 24    ToggleStartMenu = 126,
 25    ToggleVoiceChatRecording = 127,
 26    ToggleAvatarEditorHud = 128,
 27    ToggleQuestsPanelHud = 129,
 28    ToggleAvatarNamesHud = 130,
 29    TogglePlacesAndEventsHud = 131,
 30
 31    OpenExpressions = 200,
 32    Expression_Wave = 201,
 33    Expression_FistPump = 202,
 34    Expression_Robot = 203,
 35    Expression_RaiseHand = 204,
 36    Expression_Clap = 205,
 37    Expression_ThrowMoney = 206,
 38    Expression_SendKiss = 207,
 39    Expression_Dance = 208,
 40    Expression_Hohoho = 209,
 41    Expression_Snowfall = 210,
 42
 43    //Builder In World 4xx
 44    BuildEditModeChange = 408,
 45    BuildEditModeToggleUI = 409,
 46    BuildEditModeToggleEntityList = 410,
 47    BuildEditModeToggleCatalog = 411,
 48    BuildEditModeToggleSceneInfo = 412,
 49    BuildEditModeToggleChangeCamera = 413,
 50    BuildEditModeToggleControls = 414,
 51    BuildEditModeToggleSnapMode = 415,
 52    BuildEditModeUndoAction = 417,
 53    BuildEditModeRedoAction = 418,
 54    BuildEditModeQuickBar1 = 419,
 55    BuildEditModeQuickBar2 = 420,
 56    BuildEditModeQuickBar3 = 421,
 57    BuildEditModeQuickBar4 = 422,
 58    BuildEditModeQuickBar5 = 423,
 59    BuildEditModeQuickBar6 = 424,
 60    BuildEditModeQuickBar7 = 425,
 61    BuildEditModeQuickBar8 = 426,
 62    BuildEditModeQuickBar9 = 427,
 63    BuildEditModeDuplicate = 428,
 64    BuildEditModeTranslate = 429,
 65    BuildEditModeRotate = 430,
 66    BuildEditModeScale = 431,
 67    BuildEditModeDelete = 434,
 68    BuildEditModeFocusSelectedEntities = 435,
 69    BuildEditModeReset = 443,
 70    BuildEditHideSelectedEntities = 444,
 71    BuildEditShowAllEntities = 445,
 72    BuildEditModeResetCamera = 446,
 73    BuildEditModeZoomIn = 447,
 74    BuildEditModeZoomOut = 448
 75}
 76
 77/// <summary>
 78/// Mapping for hold actions
 79/// </summary>
 80public enum DCLAction_Hold
 81{
 82    //Remember to explicitly assign the value to each entry so we minimize issues with serialization + conflicts
 83    Sprint = 1,
 84    Jump = 2,
 85    FreeCameraMode = 101,
 86    VoiceChatRecording = 102,
 87    DefaultConfirmAction = 300,
 88    DefaultCancelAction = 301,
 89    BuildEditModeMultiSelection = 432,
 90    BuildEditModeSquareMultiSelection = 433,
 91    BuildEditModeFirstPersonRotation = 436,
 92    BuildEditModeCameraAdvanceFoward = 437,
 93    BuildEditModeCameraAdvanceBack = 438,
 94    BuildEditModeCameraAdvanceLeft = 439,
 95    BuildEditModeCameraAdvanceRight = 440,
 96    BuildEditModeCameraAdvanceUp = 441,
 97    BuildEditModeCameraAdvanceDown = 442,
 98    BuildEditModeCameraPan = 446
 99}
 100
 101/// <summary>
 102/// Mapping for measurable actions
 103/// </summary>
 104public enum DCLAction_Measurable
 105{
 106    //Remember to explicitly assign the value to each entry so we minimize issues with serialization + conflicts
 107    CharacterXAxis = 1,
 108    CharacterYAxis = 2,
 109    CameraXAxis = 3,
 110    CameraYAxis = 4,
 111}
 112
 113/// <summary>
 114/// Group of actions currently actived
 115/// </summary>
 116public enum InputTypeMode
 117{
 118    OFF,
 119    GENERAL,
 120    BUILD_MODE_LOADING,
 121    BUILD_MODE
 122}
 123
 124/// <summary>
 125/// Input Controller will map inputs(keys/mouse/axis) to DCL actions, check if they can be triggered (modifiers) and rai
 126/// </summary>
 127public class InputController : MonoBehaviour
 128{
 1129    public static bool ENABLE_THIRD_PERSON_CAMERA = true;
 130
 131    [Header("General Input")]
 132    public InputAction_Trigger[] triggerTimeActions;
 133
 134    public InputAction_Hold[] holdActions;
 135    public InputAction_Measurable[] measurableActions;
 136
 137    [Header("BuildMode Input")]
 138    public InputAction_Trigger[] builderTriggerTimeActions;
 139
 140    public InputAction_Hold[] builderHoldActions;
 141    public InputAction_Trigger[] loadingBuilderTriggerTimeActions;
 142
 8684143    bool renderingEnabled => CommonScriptableObjects.rendererState.Get();
 52086144    bool allUIHidden => CommonScriptableObjects.allUIHidden.Get();
 613145    public InputTypeMode inputTypeMode { get; set; } = InputTypeMode.GENERAL;
 146
 147    private void Update()
 148    {
 8684149        if (!renderingEnabled)
 150        {
 1151            Stop_Measurable(measurableActions);
 1152            return;
 153        }
 154
 8683155        switch (inputTypeMode)
 156        {
 157            case InputTypeMode.OFF:
 0158                Stop_Measurable(measurableActions);
 0159                return;
 160            case InputTypeMode.GENERAL:
 8681161                Update_Trigger(triggerTimeActions);
 8681162                Update_Hold(holdActions);
 8681163                Update_Measurable(measurableActions);
 8681164                break;
 165            case InputTypeMode.BUILD_MODE_LOADING:
 2166                Update_Trigger(loadingBuilderTriggerTimeActions);
 2167                Stop_Measurable(measurableActions);
 2168                break;
 169            case InputTypeMode.BUILD_MODE:
 0170                Update_Trigger(builderTriggerTimeActions);
 0171                Update_Hold(builderHoldActions);
 0172                Update_Measurable(measurableActions);
 173                break;
 174        }
 0175    }
 176
 177    /// <summary>
 178    /// Map the trigger actions to inputs + modifiers and check if their events must be triggered
 179    /// </summary>
 180    public void Update_Trigger(InputAction_Trigger[] triggerTimeActions)
 181    {
 416696182        for (var i = 0; i < triggerTimeActions.Length; i++)
 183        {
 199665184            var action = triggerTimeActions[i];
 185
 199665186            if (action.isTriggerBlocked != null && action.isTriggerBlocked.Get())
 187                continue;
 188
 199665189            switch (action.GetDCLAction())
 190            {
 191                case DCLAction_Trigger.CameraChange:
 192                    //Disable until the fine-tuning is ready
 8681193                    if (ENABLE_THIRD_PERSON_CAMERA)
 8681194                        InputProcessor.FromKey(action, KeyCode.V,
 195                            modifiers: InputProcessor.Modifier.NeedsPointerLocked |
 196                                       InputProcessor.Modifier.FocusNotInInput);
 8681197                    break;
 198                case DCLAction_Trigger.ToggleNavMap:
 8681199                    if (allUIHidden)
 200                        break;
 8593201                    InputProcessor.FromKey(action, KeyCode.M, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8593202                    break;
 203                case DCLAction_Trigger.ToggleFriends:
 8681204                    if (allUIHidden)
 205                        break;
 8593206                    InputProcessor.FromKey(action, KeyCode.L, modifiers: InputProcessor.Modifier.None);
 8593207                    break;
 208                case DCLAction_Trigger.ToggleWorldChat:
 8681209                    if (allUIHidden)
 210                        break;
 8593211                    InputProcessor.FromKey(action, KeyCode.Return, modifiers: InputProcessor.Modifier.None);
 8593212                    break;
 213                case DCLAction_Trigger.ToggleUIVisibility:
 8681214                    InputProcessor.FromKey(action, KeyCode.U, modifiers: InputProcessor.Modifier.None);
 8681215                    break;
 216                case DCLAction_Trigger.CloseWindow:
 8681217                    if (allUIHidden || DataStore.i.common.isSignUpFlow.Get())
 218                        break;
 8593219                    InputProcessor.FromKey(action, KeyCode.Escape, modifiers: InputProcessor.Modifier.None);
 8593220                    break;
 221                case DCLAction_Trigger.OpenExpressions:
 8681222                    if (allUIHidden)
 223                        break;
 8593224                    InputProcessor.FromKey(action, KeyCode.B, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8593225                    break;
 226                case DCLAction_Trigger.ToggleControlsHud:
 8681227                    InputProcessor.FromKey(action, KeyCode.C, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681228                    break;
 229                case DCLAction_Trigger.ToggleSettings:
 8681230                    InputProcessor.FromKey(action, KeyCode.P, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681231                    break;
 232                case DCLAction_Trigger.ToggleStartMenu:
 8681233                    if (allUIHidden)
 234                        break;
 8593235                    InputProcessor.FromKey(action, KeyCode.Tab, modifiers: InputProcessor.Modifier.None);
 8593236                    break;
 237                case DCLAction_Trigger.TogglePlacesAndEventsHud:
 8681238                    InputProcessor.FromKey(action, KeyCode.X, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681239                    break;
 240                case DCLAction_Trigger.Expression_Wave:
 8681241                    InputProcessor.FromKey(action, KeyCode.Alpha1, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681242                    break;
 243                case DCLAction_Trigger.Expression_FistPump:
 8681244                    InputProcessor.FromKey(action, KeyCode.Alpha2, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681245                    break;
 246                case DCLAction_Trigger.Expression_Robot:
 8681247                    InputProcessor.FromKey(action, KeyCode.Alpha3, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681248                    break;
 249                case DCLAction_Trigger.Expression_RaiseHand:
 8681250                    InputProcessor.FromKey(action, KeyCode.Alpha4, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681251                    break;
 252                case DCLAction_Trigger.Expression_Clap:
 8681253                    InputProcessor.FromKey(action, KeyCode.Alpha5, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681254                    break;
 255                case DCLAction_Trigger.Expression_ThrowMoney:
 8681256                    InputProcessor.FromKey(action, KeyCode.Alpha6, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681257                    break;
 258                case DCLAction_Trigger.Expression_SendKiss:
 8681259                    InputProcessor.FromKey(action, KeyCode.Alpha7, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681260                    break;
 261                case DCLAction_Trigger.BuildEditModeChange:
 8683262                    InputProcessor.FromKey(action, KeyCode.K, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8683263                    break;
 264                case DCLAction_Trigger.ToggleVoiceChatRecording:
 8681265                    InputProcessor.FromKey(action, KeyCode.T, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 8681266                    break;
 267                case DCLAction_Trigger.ToggleAvatarEditorHud:
 8681268                    InputProcessor.FromKey(action, KeyCode.I, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681269                    break;
 270                case DCLAction_Trigger.BuildEditModeToggleUI:
 0271                    InputProcessor.FromKey(action, KeyCode.U, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0272                    break;
 273                case DCLAction_Trigger.BuildEditModeToggleChangeCamera:
 0274                    InputProcessor.FromKey(action, KeyCode.V, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0275                    break;
 276                case DCLAction_Trigger.BuildEditModeToggleControls:
 0277                    InputProcessor.FromKey(action, KeyCode.C, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0278                    break;
 279                case DCLAction_Trigger.BuildEditModeToggleSnapMode:
 0280                    InputProcessor.FromKey(action, KeyCode.O, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0281                    break;
 282                case DCLAction_Trigger.BuildEditModeRedoAction:
 0283                    InputProcessor.FromKey(action, KeyCode.Y, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0284                    break;
 285                case DCLAction_Trigger.BuildEditModeUndoAction:
 0286                    InputProcessor.FromKey(action, KeyCode.Z, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0287                    break;
 288                case DCLAction_Trigger.BuildEditModeQuickBar1:
 0289                    InputProcessor.FromKey(action, KeyCode.Alpha1, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0290                    break;
 291                case DCLAction_Trigger.BuildEditModeQuickBar2:
 0292                    InputProcessor.FromKey(action, KeyCode.Alpha2, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0293                    break;
 294                case DCLAction_Trigger.BuildEditModeQuickBar3:
 0295                    InputProcessor.FromKey(action, KeyCode.Alpha3, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0296                    break;
 297                case DCLAction_Trigger.BuildEditModeQuickBar4:
 0298                    InputProcessor.FromKey(action, KeyCode.Alpha4, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0299                    break;
 300                case DCLAction_Trigger.BuildEditModeQuickBar5:
 0301                    InputProcessor.FromKey(action, KeyCode.Alpha5, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0302                    break;
 303                case DCLAction_Trigger.BuildEditModeQuickBar6:
 0304                    InputProcessor.FromKey(action, KeyCode.Alpha6, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0305                    break;
 306                case DCLAction_Trigger.BuildEditModeQuickBar7:
 0307                    InputProcessor.FromKey(action, KeyCode.Alpha7, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0308                    break;
 309                case DCLAction_Trigger.BuildEditModeQuickBar8:
 0310                    InputProcessor.FromKey(action, KeyCode.Alpha8, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0311                    break;
 312                case DCLAction_Trigger.BuildEditModeQuickBar9:
 0313                    InputProcessor.FromKey(action, KeyCode.Alpha9, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0314                    break;
 315                case DCLAction_Trigger.BuildEditModeDelete:
 0316                    InputProcessor.FromKey(action, KeyCode.Delete, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0317                    InputProcessor.FromKey(action, KeyCode.Backspace, modifiers: InputProcessor.Modifier.FocusNotInInput
 0318                    break;
 319                case DCLAction_Trigger.BuildEditModeDuplicate:
 0320                    InputProcessor.FromKey(action, KeyCode.D, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0321                    break;
 322                case DCLAction_Trigger.BuildEditModeTranslate:
 0323                    InputProcessor.FromKey(action, KeyCode.G, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0324                    InputProcessor.FromKey(action, KeyCode.M, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0325                    break;
 326                case DCLAction_Trigger.BuildEditModeRotate:
 0327                    InputProcessor.FromKey(action, KeyCode.R, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0328                    break;
 329                case DCLAction_Trigger.BuildEditModeScale:
 0330                    InputProcessor.FromKey(action, KeyCode.S, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0331                    break;
 332                case DCLAction_Trigger.BuildEditModeFocusSelectedEntities:
 0333                    InputProcessor.FromKey(action, KeyCode.F, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0334                    break;
 335                case DCLAction_Trigger.BuildEditModeReset:
 0336                    InputProcessor.FromKey(action, KeyCode.R, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0337                    break;
 338                case DCLAction_Trigger.BuildEditHideSelectedEntities:
 0339                    InputProcessor.FromKey(action, KeyCode.H, modifiers: InputProcessor.Modifier.FocusNotInInput);
 0340                    break;
 341                case DCLAction_Trigger.BuildEditShowAllEntities:
 0342                    InputProcessor.FromKey(action, KeyCode.H, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0343                    break;
 344                case DCLAction_Trigger.BuildEditModeResetCamera:
 0345                    InputProcessor.FromKey(action, KeyCode.C, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 0346                    break;
 347                case DCLAction_Trigger.BuildEditModeZoomIn:
 0348                    InputProcessor.FromKey(action, KeyCode.KeypadPlus, modifiers: InputProcessor.Modifier.FocusNotInInpu
 0349                    break;
 350                case DCLAction_Trigger.BuildEditModeZoomOut:
 0351                    InputProcessor.FromKey(action, KeyCode.KeypadMinus, modifiers: InputProcessor.Modifier.FocusNotInInp
 0352                    break;
 353                case DCLAction_Trigger.ToggleQuestsPanelHud:
 8681354                    InputProcessor.FromKey(action, KeyCode.J, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681355                    break;
 356                case DCLAction_Trigger.ToggleAvatarNamesHud:
 8681357                    InputProcessor.FromKey(action, KeyCode.N, modifiers: InputProcessor.Modifier.FocusNotInInput);
 8681358                    break;
 359                default:
 0360                    throw new ArgumentOutOfRangeException();
 361            }
 362        }
 8683363    }
 364
 365    /// <summary>
 366    /// Map the hold actions to inputs + modifiers and check if their events must be triggered
 367    /// </summary>
 368    private void Update_Hold(InputAction_Hold[] holdActions)
 369    {
 138896370        for (var i = 0; i < holdActions.Length; i++)
 371        {
 60767372            var action = holdActions[i];
 60767373            switch (action.GetDCLAction())
 374            {
 375                case DCLAction_Hold.Sprint:
 8681376                    InputProcessor.FromKey(action, InputSettings.WalkButtonKeyCode, InputProcessor.Modifier.NeedsPointer
 8681377                    break;
 378                case DCLAction_Hold.Jump:
 8681379                    InputProcessor.FromKey(action, InputSettings.JumpButtonKeyCode, InputProcessor.Modifier.NeedsPointer
 8681380                    break;
 381                case DCLAction_Hold.FreeCameraMode:
 382                    //Disable until the fine-tuning is ready
 8681383                    if (ENABLE_THIRD_PERSON_CAMERA)
 8681384                        InputProcessor.FromKey(action, KeyCode.Y, InputProcessor.Modifier.NeedsPointerLocked);
 8681385                    break;
 386                case DCLAction_Hold.VoiceChatRecording:
 387                    // Push to talk functionality only triggers if no modifier key is pressed
 17362388                    InputProcessor.FromKey(action, KeyCode.T, InputProcessor.Modifier.FocusNotInInput, null);
 17362389                    break;
 390                case DCLAction_Hold.DefaultConfirmAction:
 8681391                    InputProcessor.FromKey(action, KeyCode.E, InputProcessor.Modifier.None);
 8681392                    break;
 393                case DCLAction_Hold.DefaultCancelAction:
 8681394                    InputProcessor.FromKey(action, KeyCode.F, InputProcessor.Modifier.None);
 8681395                    break;
 396                case DCLAction_Hold.BuildEditModeMultiSelection:
 0397                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 0398                    break;
 399                case DCLAction_Hold.BuildEditModeSquareMultiSelection:
 0400                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 0401                    break;
 402                case DCLAction_Hold.BuildEditModeFirstPersonRotation:
 0403                    InputProcessor.FromKey(action, KeyCode.R, InputProcessor.Modifier.FocusNotInInput);
 0404                    break;
 405                case DCLAction_Hold.BuildEditModeCameraAdvanceFoward:
 0406                    InputProcessor.FromKey(action, KeyCode.UpArrow, InputProcessor.Modifier.FocusNotInInput);
 0407                    InputProcessor.FromKey(action, KeyCode.W, InputProcessor.Modifier.FocusNotInInput);
 0408                    break;
 409                case DCLAction_Hold.BuildEditModeCameraAdvanceBack:
 0410                    InputProcessor.FromKey(action, KeyCode.DownArrow, InputProcessor.Modifier.FocusNotInInput);
 0411                    InputProcessor.FromKey(action, KeyCode.S, InputProcessor.Modifier.FocusNotInInput);
 0412                    break;
 413                case DCLAction_Hold.BuildEditModeCameraAdvanceLeft:
 0414                    InputProcessor.FromKey(action, KeyCode.LeftArrow, InputProcessor.Modifier.FocusNotInInput);
 0415                    InputProcessor.FromKey(action, KeyCode.A, InputProcessor.Modifier.FocusNotInInput);
 0416                    break;
 417                case DCLAction_Hold.BuildEditModeCameraAdvanceRight:
 0418                    InputProcessor.FromKey(action, KeyCode.RightArrow, InputProcessor.Modifier.FocusNotInInput);
 0419                    InputProcessor.FromKey(action, KeyCode.D, InputProcessor.Modifier.FocusNotInInput);
 0420                    break;
 421                case DCLAction_Hold.BuildEditModeCameraAdvanceUp:
 0422                    InputProcessor.FromKey(action, KeyCode.E, InputProcessor.Modifier.FocusNotInInput);
 0423                    break;
 424                case DCLAction_Hold.BuildEditModeCameraAdvanceDown:
 0425                    InputProcessor.FromKey(action, KeyCode.Q, InputProcessor.Modifier.FocusNotInInput);
 0426                    break;
 427                case DCLAction_Hold.BuildEditModeCameraPan:
 0428                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 0429                    break;
 430                default:
 0431                    throw new ArgumentOutOfRangeException();
 432            }
 433        }
 8681434    }
 435
 436    /// <summary>
 437    /// Map the measurable actions to inputs + modifiers and check if their events must be triggered
 438    /// </summary>
 439    private void Update_Measurable(InputAction_Measurable[] measurableActions)
 440    {
 86810441        for (var i = 0; i < measurableActions.Length; i++)
 442        {
 34724443            var action = measurableActions[i];
 34724444            switch (action.GetDCLAction())
 445            {
 446                case DCLAction_Measurable.CharacterXAxis:
 8681447                    InputProcessor.FromAxis(action, "Horizontal", InputProcessor.Modifier.NeedsPointerLocked);
 8681448                    break;
 449                case DCLAction_Measurable.CharacterYAxis:
 8681450                    InputProcessor.FromAxis(action, "Vertical", InputProcessor.Modifier.NeedsPointerLocked);
 8681451                    break;
 452                case DCLAction_Measurable.CameraXAxis:
 8681453                    InputProcessor.FromAxis(action, "Mouse X", InputProcessor.Modifier.NeedsPointerLocked);
 8681454                    break;
 455                case DCLAction_Measurable.CameraYAxis:
 8681456                    InputProcessor.FromAxis(action, "Mouse Y", InputProcessor.Modifier.NeedsPointerLocked);
 8681457                    break;
 458                default:
 0459                    throw new ArgumentOutOfRangeException();
 460            }
 461        }
 8681462    }
 463
 464    private void Stop_Measurable(InputAction_Measurable[] measurableActions)
 465    {
 30466        for (var i = 0; i < measurableActions.Length; i++)
 467        {
 12468            measurableActions[i].RaiseOnValueChanged(0);
 469        }
 3470    }
 471}
 472
 473/// <summary>
 474/// Helper class that wraps the processing of inputs and modifiers to trigger actions events
 475/// </summary>
 476public static class InputProcessor
 477{
 478    private static readonly KeyCode[] MODIFIER_KEYS = new[] { KeyCode.LeftControl, KeyCode.LeftAlt, KeyCode.LeftShift, K
 479
 480    [Flags]
 481    public enum Modifier
 482    {
 483        //Set the values as bit masks
 484        None = 0b0000000, // No modifier needed
 485        NeedsPointerLocked = 0b0000001, // The pointer must be locked to the game
 486        FocusNotInInput = 0b0000010, // The game focus cannot be in an input field
 487    }
 488
 489    /// <summary>
 490    /// Check if the modifier keys are pressed
 491    /// </summary>
 492    /// <param name="modifierKeys"> Keycodes modifiers</param>
 493    /// <returns></returns>
 494    public static Boolean PassModifierKeys(KeyCode[] modifierKeys)
 495    {
 496        for (var i = 0; i < MODIFIER_KEYS.Length; i++)
 497        {
 498            var keyCode = MODIFIER_KEYS[i];
 499            var pressed = Input.GetKey(keyCode);
 500            if (modifierKeys == null)
 501            {
 502                if (pressed)
 503                    return false;
 504            }
 505            else
 506            {
 507                if (modifierKeys.Contains(keyCode) != pressed)
 508                    return false;
 509            }
 510        }
 511
 512        return true;
 513    }
 514
 515    /// <summary>
 516    /// Check if a miscellaneous modifiers are present. These modifiers are related to the meta-state of the application
 517    /// they can be anything such as mouse pointer state, where the focus is, camera mode...
 518    /// </summary>
 519    /// <param name="modifiers"></param>
 520    /// <returns></returns>
 521    public static bool PassModifiers(Modifier modifiers)
 522    {
 523        if (IsModifierSet(modifiers, Modifier.NeedsPointerLocked) && !DCL.Helpers.Utils.isCursorLocked)
 524            return false;
 525
 526        if (IsModifierSet(modifiers, Modifier.FocusNotInInput) && FocusIsInInputField())
 527            return false;
 528
 529        return true;
 530    }
 531
 532    /// <summary>
 533    /// Process an input action mapped to a keyboard key.
 534    /// </summary>
 535    /// <param name="action">Trigger Action to perform</param>
 536    /// <param name="key">KeyCode mapped to this action</param>
 537    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 538    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 539    public static void FromKey(InputAction_Trigger action, KeyCode key, KeyCode[] modifierKeys = null,
 540        Modifier modifiers = Modifier.None)
 541    {
 542        if (!PassModifiers(modifiers))
 543            return;
 544
 545        if (!PassModifierKeys(modifierKeys))
 546            return;
 547
 548        if (Input.GetKeyDown(key))
 549            action.RaiseOnTriggered();
 550    }
 551
 552    /// <summary>
 553    /// Process an input action mapped to a button.
 554    /// </summary>
 555    /// <param name="action">Trigger Action to perform</param>
 556    /// <param name="mouseButtonIdx">Index of the mouse button mapped to this action</param>
 557    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 558    public static void FromMouseButton(InputAction_Trigger action, int mouseButtonIdx,
 559        Modifier modifiers = Modifier.None)
 560    {
 561        if (!PassModifiers(modifiers))
 562            return;
 563
 564        if (Input.GetMouseButton(mouseButtonIdx))
 565            action.RaiseOnTriggered();
 566    }
 567
 568    /// <summary>
 569    /// Process an input action mapped to a keyboard key
 570    /// </summary>
 571    /// <param name="action">Hold Action to perform</param>
 572    /// <param name="key">KeyCode mapped to this action</param>
 573    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 574    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers = Modifier.None)
 575    {
 576        if (!PassModifiers(modifiers))
 577            return;
 578
 579        if (Input.GetKeyDown(key))
 580            action.RaiseOnStarted();
 581        if (Input.GetKeyUp(key))
 582            action.RaiseOnFinished();
 583    }
 584
 585    /// <summary>
 586    /// Process an input action mapped to a keyboard key
 587    /// </summary>
 588    /// <param name="action">Hold Action to perform</param>
 589    /// <param name="key">KeyCode mapped to this action</param>
 590    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 591    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 592    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers, KeyCode[] modifierKeys)
 593    {
 594        if (!PassModifierKeys(modifierKeys))
 595            return;
 596
 597        FromKey(action, key, modifiers);
 598    }
 599
 600    /// <summary>
 601    /// Process an input action mapped to a mouse button
 602    /// </summary>
 603    /// <param name="action">Hold Action to perform</param>
 604    /// <param name="mouseButtonIdx">Index of the mouse button</param>
 605    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 606    public static void FromMouse(InputAction_Hold action, int mouseButtonIdx, Modifier modifiers = Modifier.None)
 607    {
 608        if (!PassModifiers(modifiers))
 609            return;
 610
 611        if (Input.GetMouseButtonDown(mouseButtonIdx))
 612            action.RaiseOnStarted();
 613        if (Input.GetMouseButtonUp(mouseButtonIdx))
 614            action.RaiseOnFinished();
 615    }
 616
 617    /// <summary>
 618    /// Process an input action mapped to an axis
 619    /// </summary>
 620    /// <param name="action">Measurable Action to perform</param>
 621    /// <param name="axisName">Axis name</param>
 622    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 623    public static void FromAxis(InputAction_Measurable action, string axisName, Modifier modifiers = Modifier.None)
 624    {
 625        if (!PassModifiers(modifiers))
 626        {
 627            action.RaiseOnValueChanged(0);
 628            return;
 629        }
 630
 631        action.RaiseOnValueChanged(Input.GetAxis(axisName));
 632    }
 633
 634    /// <summary>
 635    /// Bitwise check for the modifiers flags
 636    /// </summary>
 637    /// <param name="modifiers">Modifier to check</param>
 638    /// <param name="value">Modifier mapped to a bit to check</param>
 639    /// <returns></returns>
 640    public static bool IsModifierSet(Modifier modifiers, Modifier value)
 641    {
 642        int flagsValue = (int)modifiers;
 643        int flagValue = (int)value;
 644
 645        return (flagsValue & flagValue) != 0;
 646    }
 647
 648    public static bool FocusIsInInputField()
 649    {
 650        if (EventSystem.current == null)
 651            return false;
 652
 653        if (EventSystem.current.currentSelectedGameObject != null &&
 654            (EventSystem.current.currentSelectedGameObject.GetComponent<TMP_InputField>() != null ||
 655             EventSystem.current.currentSelectedGameObject.GetComponent<UnityEngine.UI.InputField>() != null))
 656        {
 657            return true;
 658        }
 659
 660        return false;
 661    }
 662}