< Summary

Class:InputProcessor
Assembly:InputController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/InputController/InputController.cs
Covered lines:36
Uncovered lines:20
Coverable lines:56
Total lines:653
Line coverage:64.2% (36 of 56)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InputProcessor()0%110100%
PassModifierKeys(...)0%5.025090.91%
PassModifiers(...)0%550100%
FromKey(...)0%4.054085.71%
FromMouseButton(...)0%12300%
FromKey(...)0%4.374071.43%
FromKey(...)0%2.062075%
FromMouse(...)0%20400%
FromAxis(...)0%220100%
IsModifierSet(...)0%2100%
FocusIsInInputField()0%440100%

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    ToggleExploreHud = 126,
 25    ToggleVoiceChatRecording = 127,
 26    ToggleAvatarEditorHud = 128,
 27    ToggleQuestsPanelHud = 129,
 28    ToggleAvatarNamesHud = 130,
 29
 30    OpenExpressions = 200,
 31    Expression_Wave = 201,
 32    Expression_FistPump = 202,
 33    Expression_Robot = 203,
 34    Expression_RaiseHand = 204,
 35    Expression_Clap = 205,
 36    Expression_ThrowMoney = 206,
 37    Expression_SendKiss = 207,
 38
 39    //Builder In World 4xx
 40    BuildEditModeChange = 408,
 41    BuildEditModeToggleUI = 409,
 42    BuildEditModeToggleEntityList = 410,
 43    BuildEditModeToggleCatalog = 411,
 44    BuildEditModeToggleSceneInfo = 412,
 45    BuildEditModeToggleChangeCamera = 413,
 46    BuildEditModeToggleControls = 414,
 47    BuildEditModeToggleSnapMode = 415,
 48    BuildEditModeUndoAction = 417,
 49    BuildEditModeRedoAction = 418,
 50    BuildEditModeQuickBar1 = 419,
 51    BuildEditModeQuickBar2 = 420,
 52    BuildEditModeQuickBar3 = 421,
 53    BuildEditModeQuickBar4 = 422,
 54    BuildEditModeQuickBar5 = 423,
 55    BuildEditModeQuickBar6 = 424,
 56    BuildEditModeQuickBar7 = 425,
 57    BuildEditModeQuickBar8 = 426,
 58    BuildEditModeQuickBar9 = 427,
 59    BuildEditModeDuplicate = 428,
 60    BuildEditModeTranslate = 429,
 61    BuildEditModeRotate = 430,
 62    BuildEditModeScale = 431,
 63    BuildEditModeDelete = 434,
 64    BuildEditModeFocusSelectedEntities = 435,
 65    BuildEditModeReset = 443,
 66    BuildEditHideSelectedEntities = 444,
 67    BuildEditShowAllEntities = 445,
 68    BuildEditModeResetCamera = 446,
 69    BuildEditModeZoomIn = 447,
 70    BuildEditModeZoomOut = 448
 71}
 72
 73/// <summary>
 74/// Mapping for hold actions
 75/// </summary>
 76public enum DCLAction_Hold
 77{
 78    //Remember to explicitly assign the value to each entry so we minimize issues with serialization + conflicts
 79    Sprint = 1,
 80    Jump = 2,
 81    FreeCameraMode = 101,
 82    VoiceChatRecording = 102,
 83    DefaultConfirmAction = 300,
 84    DefaultCancelAction = 301,
 85    BuildEditModeMultiSelection = 432,
 86    BuildEditModeSquareMultiSelection = 433,
 87    BuildEditModeFirstPersonRotation = 436,
 88    BuildEditModeCameraAdvanceFoward = 437,
 89    BuildEditModeCameraAdvanceBack = 438,
 90    BuildEditModeCameraAdvanceLeft = 439,
 91    BuildEditModeCameraAdvanceRight = 440,
 92    BuildEditModeCameraAdvanceUp = 441,
 93    BuildEditModeCameraAdvanceDown = 442,
 94    BuildEditModeCameraPan = 446
 95}
 96
 97/// <summary>
 98/// Mapping for measurable actions
 99/// </summary>
 100public enum DCLAction_Measurable
 101{
 102    //Remember to explicitly assign the value to each entry so we minimize issues with serialization + conflicts
 103    CharacterXAxis = 1,
 104    CharacterYAxis = 2,
 105    CameraXAxis = 3,
 106    CameraYAxis = 4,
 107}
 108
 109/// <summary>
 110/// Group of actions currently actived
 111/// </summary>
 112public enum InputTypeMode
 113{
 114    OFF,
 115    GENERAL,
 116    BUILD_MODE_LOADING,
 117    BUILD_MODE
 118}
 119
 120/// <summary>
 121/// Input Controller will map inputs(keys/mouse/axis) to DCL actions, check if they can be triggered (modifiers) and rai
 122/// </summary>
 123public class InputController : MonoBehaviour
 124{
 125    public static bool ENABLE_THIRD_PERSON_CAMERA = true;
 126
 127    [Header("General Input")]
 128    public InputAction_Trigger[] triggerTimeActions;
 129
 130    public InputAction_Hold[] holdActions;
 131    public InputAction_Measurable[] measurableActions;
 132
 133    [Header("BuildMode Input")]
 134    public InputAction_Trigger[] builderTriggerTimeActions;
 135
 136    public InputAction_Hold[] builderHoldActions;
 137    public InputAction_Trigger[] loadingBuilderTriggerTimeActions;
 138
 139    bool renderingEnabled => CommonScriptableObjects.rendererState.Get();
 140    bool allUIHidden => CommonScriptableObjects.allUIHidden.Get();
 141    public InputTypeMode inputTypeMode { get; set; } = InputTypeMode.GENERAL;
 142
 143    private void Update()
 144    {
 145        if (!renderingEnabled)
 146        {
 147            Stop_Measurable(measurableActions);
 148            return;
 149        }
 150
 151        switch (inputTypeMode)
 152        {
 153            case InputTypeMode.OFF:
 154                Stop_Measurable(measurableActions);
 155                return;
 156            case InputTypeMode.GENERAL:
 157                Update_Trigger(triggerTimeActions);
 158                Update_Hold(holdActions);
 159                Update_Measurable(measurableActions);
 160                break;
 161            case InputTypeMode.BUILD_MODE_LOADING:
 162                Update_Trigger(loadingBuilderTriggerTimeActions);
 163                Stop_Measurable(measurableActions);
 164                break;
 165            case InputTypeMode.BUILD_MODE:
 166                Update_Trigger(builderTriggerTimeActions);
 167                Update_Hold(builderHoldActions);
 168                Update_Measurable(measurableActions);
 169                break;
 170        }
 171    }
 172
 173    /// <summary>
 174    /// Map the trigger actions to inputs + modifiers and check if their events must be triggered
 175    /// </summary>
 176    public void Update_Trigger(InputAction_Trigger[] triggerTimeActions)
 177    {
 178        for (var i = 0; i < triggerTimeActions.Length; i++)
 179        {
 180            var action = triggerTimeActions[i];
 181
 182            if (action.isTriggerBlocked != null && action.isTriggerBlocked.Get())
 183                continue;
 184
 185            switch (action.GetDCLAction())
 186            {
 187                case DCLAction_Trigger.CameraChange:
 188                    //Disable until the fine-tuning is ready
 189                    if (ENABLE_THIRD_PERSON_CAMERA)
 190                        InputProcessor.FromKey(action, KeyCode.V,
 191                            modifiers: InputProcessor.Modifier.NeedsPointerLocked |
 192                                       InputProcessor.Modifier.FocusNotInInput);
 193                    break;
 194                case DCLAction_Trigger.ToggleNavMap:
 195                    if (allUIHidden)
 196                        break;
 197                    InputProcessor.FromKey(action, KeyCode.M, modifiers: InputProcessor.Modifier.FocusNotInInput);
 198                    InputProcessor.FromKey(action, KeyCode.Tab, modifiers: InputProcessor.Modifier.FocusNotInInput);
 199                    break;
 200                case DCLAction_Trigger.ToggleFriends:
 201                    if (allUIHidden)
 202                        break;
 203                    InputProcessor.FromKey(action, KeyCode.L, modifiers: InputProcessor.Modifier.None);
 204                    break;
 205                case DCLAction_Trigger.ToggleWorldChat:
 206                    if (allUIHidden)
 207                        break;
 208                    InputProcessor.FromKey(action, KeyCode.Return, modifiers: InputProcessor.Modifier.None);
 209                    break;
 210                case DCLAction_Trigger.ToggleUIVisibility:
 211                    InputProcessor.FromKey(action, KeyCode.U, modifiers: InputProcessor.Modifier.None);
 212                    break;
 213                case DCLAction_Trigger.CloseWindow:
 214                    if (allUIHidden || DataStore.i.isSignUpFlow.Get())
 215                        break;
 216                    InputProcessor.FromKey(action, KeyCode.Escape, modifiers: InputProcessor.Modifier.None);
 217                    break;
 218                case DCLAction_Trigger.OpenExpressions:
 219                    if (allUIHidden)
 220                        break;
 221                    InputProcessor.FromKey(action, KeyCode.B, modifiers: InputProcessor.Modifier.FocusNotInInput);
 222                    break;
 223                case DCLAction_Trigger.ToggleControlsHud:
 224                    InputProcessor.FromKey(action, KeyCode.C, modifiers: InputProcessor.Modifier.FocusNotInInput);
 225                    break;
 226                case DCLAction_Trigger.ToggleSettings:
 227                    InputProcessor.FromKey(action, KeyCode.P, modifiers: InputProcessor.Modifier.FocusNotInInput);
 228                    break;
 229                case DCLAction_Trigger.ToggleExploreHud:
 230                    if (allUIHidden)
 231                        break;
 232                    InputProcessor.FromKey(action, KeyCode.X, modifiers: InputProcessor.Modifier.FocusNotInInput);
 233                    break;
 234                case DCLAction_Trigger.Expression_Wave:
 235                    InputProcessor.FromKey(action, KeyCode.Alpha1, modifiers: InputProcessor.Modifier.FocusNotInInput);
 236                    break;
 237                case DCLAction_Trigger.Expression_FistPump:
 238                    InputProcessor.FromKey(action, KeyCode.Alpha2, modifiers: InputProcessor.Modifier.FocusNotInInput);
 239                    break;
 240                case DCLAction_Trigger.Expression_Robot:
 241                    InputProcessor.FromKey(action, KeyCode.Alpha3, modifiers: InputProcessor.Modifier.FocusNotInInput);
 242                    break;
 243                case DCLAction_Trigger.Expression_RaiseHand:
 244                    InputProcessor.FromKey(action, KeyCode.Alpha4, modifiers: InputProcessor.Modifier.FocusNotInInput);
 245                    break;
 246                case DCLAction_Trigger.Expression_Clap:
 247                    InputProcessor.FromKey(action, KeyCode.Alpha5, modifiers: InputProcessor.Modifier.FocusNotInInput);
 248                    break;
 249                case DCLAction_Trigger.Expression_ThrowMoney:
 250                    InputProcessor.FromKey(action, KeyCode.Alpha6, modifiers: InputProcessor.Modifier.FocusNotInInput);
 251                    break;
 252                case DCLAction_Trigger.Expression_SendKiss:
 253                    InputProcessor.FromKey(action, KeyCode.Alpha7, modifiers: InputProcessor.Modifier.FocusNotInInput);
 254                    break;
 255                case DCLAction_Trigger.BuildEditModeChange:
 256                    InputProcessor.FromKey(action, KeyCode.K, modifiers: InputProcessor.Modifier.FocusNotInInput);
 257                    break;
 258                case DCLAction_Trigger.ToggleVoiceChatRecording:
 259                    InputProcessor.FromKey(action, KeyCode.T, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 260                    break;
 261                case DCLAction_Trigger.ToggleAvatarEditorHud:
 262                    InputProcessor.FromKey(action, KeyCode.I, modifiers: InputProcessor.Modifier.FocusNotInInput);
 263                    break;
 264                case DCLAction_Trigger.BuildEditModeToggleUI:
 265                    InputProcessor.FromKey(action, KeyCode.U, modifiers: InputProcessor.Modifier.FocusNotInInput);
 266                    break;
 267                case DCLAction_Trigger.BuildEditModeToggleChangeCamera:
 268                    InputProcessor.FromKey(action, KeyCode.V, modifiers: InputProcessor.Modifier.FocusNotInInput);
 269                    break;
 270                case DCLAction_Trigger.BuildEditModeToggleControls:
 271                    InputProcessor.FromKey(action, KeyCode.C, modifiers: InputProcessor.Modifier.FocusNotInInput);
 272                    break;
 273                case DCLAction_Trigger.BuildEditModeToggleSnapMode:
 274                    InputProcessor.FromKey(action, KeyCode.O, modifiers: InputProcessor.Modifier.FocusNotInInput);
 275                    break;
 276                case DCLAction_Trigger.BuildEditModeRedoAction:
 277                    InputProcessor.FromKey(action, KeyCode.Y, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 278                    break;
 279                case DCLAction_Trigger.BuildEditModeUndoAction:
 280                    InputProcessor.FromKey(action, KeyCode.Z, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 281                    break;
 282                case DCLAction_Trigger.BuildEditModeQuickBar1:
 283                    InputProcessor.FromKey(action, KeyCode.Alpha1, modifiers: InputProcessor.Modifier.FocusNotInInput);
 284                    break;
 285                case DCLAction_Trigger.BuildEditModeQuickBar2:
 286                    InputProcessor.FromKey(action, KeyCode.Alpha2, modifiers: InputProcessor.Modifier.FocusNotInInput);
 287                    break;
 288                case DCLAction_Trigger.BuildEditModeQuickBar3:
 289                    InputProcessor.FromKey(action, KeyCode.Alpha3, modifiers: InputProcessor.Modifier.FocusNotInInput);
 290                    break;
 291                case DCLAction_Trigger.BuildEditModeQuickBar4:
 292                    InputProcessor.FromKey(action, KeyCode.Alpha4, modifiers: InputProcessor.Modifier.FocusNotInInput);
 293                    break;
 294                case DCLAction_Trigger.BuildEditModeQuickBar5:
 295                    InputProcessor.FromKey(action, KeyCode.Alpha5, modifiers: InputProcessor.Modifier.FocusNotInInput);
 296                    break;
 297                case DCLAction_Trigger.BuildEditModeQuickBar6:
 298                    InputProcessor.FromKey(action, KeyCode.Alpha6, modifiers: InputProcessor.Modifier.FocusNotInInput);
 299                    break;
 300                case DCLAction_Trigger.BuildEditModeQuickBar7:
 301                    InputProcessor.FromKey(action, KeyCode.Alpha7, modifiers: InputProcessor.Modifier.FocusNotInInput);
 302                    break;
 303                case DCLAction_Trigger.BuildEditModeQuickBar8:
 304                    InputProcessor.FromKey(action, KeyCode.Alpha8, modifiers: InputProcessor.Modifier.FocusNotInInput);
 305                    break;
 306                case DCLAction_Trigger.BuildEditModeQuickBar9:
 307                    InputProcessor.FromKey(action, KeyCode.Alpha9, modifiers: InputProcessor.Modifier.FocusNotInInput);
 308                    break;
 309                case DCLAction_Trigger.BuildEditModeDelete:
 310                    InputProcessor.FromKey(action, KeyCode.Delete, modifiers: InputProcessor.Modifier.FocusNotInInput);
 311                    InputProcessor.FromKey(action, KeyCode.Backspace, modifiers: InputProcessor.Modifier.FocusNotInInput
 312                    break;
 313                case DCLAction_Trigger.BuildEditModeDuplicate:
 314                    InputProcessor.FromKey(action, KeyCode.D, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 315                    break;
 316                case DCLAction_Trigger.BuildEditModeTranslate:
 317                    InputProcessor.FromKey(action, KeyCode.G, modifiers: InputProcessor.Modifier.FocusNotInInput);
 318                    InputProcessor.FromKey(action, KeyCode.M, modifiers: InputProcessor.Modifier.FocusNotInInput);
 319                    break;
 320                case DCLAction_Trigger.BuildEditModeRotate:
 321                    InputProcessor.FromKey(action, KeyCode.R, modifiers: InputProcessor.Modifier.FocusNotInInput);
 322                    break;
 323                case DCLAction_Trigger.BuildEditModeScale:
 324                    InputProcessor.FromKey(action, KeyCode.S, modifiers: InputProcessor.Modifier.FocusNotInInput);
 325                    break;
 326                case DCLAction_Trigger.BuildEditModeFocusSelectedEntities:
 327                    InputProcessor.FromKey(action, KeyCode.F, modifiers: InputProcessor.Modifier.FocusNotInInput);
 328                    break;
 329                case DCLAction_Trigger.BuildEditModeReset:
 330                    InputProcessor.FromKey(action, KeyCode.R, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 331                    break;
 332                case DCLAction_Trigger.BuildEditHideSelectedEntities:
 333                    InputProcessor.FromKey(action, KeyCode.H, modifiers: InputProcessor.Modifier.FocusNotInInput);
 334                    break;
 335                case DCLAction_Trigger.BuildEditShowAllEntities:
 336                    InputProcessor.FromKey(action, KeyCode.H, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 337                    break;
 338                case DCLAction_Trigger.BuildEditModeResetCamera:
 339                    InputProcessor.FromKey(action, KeyCode.C, modifiers: InputProcessor.Modifier.FocusNotInInput, modifi
 340                    break;
 341                case DCLAction_Trigger.BuildEditModeZoomIn:
 342                    InputProcessor.FromKey(action, KeyCode.KeypadPlus, modifiers: InputProcessor.Modifier.FocusNotInInpu
 343                    break;
 344                case DCLAction_Trigger.BuildEditModeZoomOut:
 345                    InputProcessor.FromKey(action, KeyCode.KeypadMinus, modifiers: InputProcessor.Modifier.FocusNotInInp
 346                    break;
 347                case DCLAction_Trigger.ToggleQuestsPanelHud:
 348                    InputProcessor.FromKey(action, KeyCode.J, modifiers: InputProcessor.Modifier.FocusNotInInput);
 349                    break;
 350                case DCLAction_Trigger.ToggleAvatarNamesHud:
 351                    InputProcessor.FromKey(action, KeyCode.N, modifiers: InputProcessor.Modifier.FocusNotInInput);
 352                    break;
 353                default:
 354                    throw new ArgumentOutOfRangeException();
 355            }
 356        }
 357    }
 358
 359    /// <summary>
 360    /// Map the hold actions to inputs + modifiers and check if their events must be triggered
 361    /// </summary>
 362    private void Update_Hold(InputAction_Hold[] holdActions)
 363    {
 364        for (var i = 0; i < holdActions.Length; i++)
 365        {
 366            var action = holdActions[i];
 367            switch (action.GetDCLAction())
 368            {
 369                case DCLAction_Hold.Sprint:
 370                    InputProcessor.FromKey(action, InputSettings.WalkButtonKeyCode, InputProcessor.Modifier.NeedsPointer
 371                    break;
 372                case DCLAction_Hold.Jump:
 373                    InputProcessor.FromKey(action, InputSettings.JumpButtonKeyCode, InputProcessor.Modifier.NeedsPointer
 374                    break;
 375                case DCLAction_Hold.FreeCameraMode:
 376                    //Disable until the fine-tuning is ready
 377                    if (ENABLE_THIRD_PERSON_CAMERA)
 378                        InputProcessor.FromKey(action, KeyCode.Y, InputProcessor.Modifier.NeedsPointerLocked);
 379                    break;
 380                case DCLAction_Hold.VoiceChatRecording:
 381                    // Push to talk functionality only triggers if no modifier key is pressed
 382                    InputProcessor.FromKey(action, KeyCode.T, InputProcessor.Modifier.FocusNotInInput, null);
 383                    break;
 384                case DCLAction_Hold.DefaultConfirmAction:
 385                    InputProcessor.FromKey(action, KeyCode.E, InputProcessor.Modifier.None);
 386                    break;
 387                case DCLAction_Hold.DefaultCancelAction:
 388                    InputProcessor.FromKey(action, KeyCode.F, InputProcessor.Modifier.None);
 389                    break;
 390                case DCLAction_Hold.BuildEditModeMultiSelection:
 391                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 392                    break;
 393                case DCLAction_Hold.BuildEditModeSquareMultiSelection:
 394                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 395                    break;
 396                case DCLAction_Hold.BuildEditModeFirstPersonRotation:
 397                    InputProcessor.FromKey(action, KeyCode.R, InputProcessor.Modifier.FocusNotInInput);
 398                    break;
 399                case DCLAction_Hold.BuildEditModeCameraAdvanceFoward:
 400                    InputProcessor.FromKey(action, KeyCode.UpArrow, InputProcessor.Modifier.FocusNotInInput);
 401                    InputProcessor.FromKey(action, KeyCode.W, InputProcessor.Modifier.FocusNotInInput);
 402                    break;
 403                case DCLAction_Hold.BuildEditModeCameraAdvanceBack:
 404                    InputProcessor.FromKey(action, KeyCode.DownArrow, InputProcessor.Modifier.FocusNotInInput);
 405                    InputProcessor.FromKey(action, KeyCode.S, InputProcessor.Modifier.FocusNotInInput);
 406                    break;
 407                case DCLAction_Hold.BuildEditModeCameraAdvanceLeft:
 408                    InputProcessor.FromKey(action, KeyCode.LeftArrow, InputProcessor.Modifier.FocusNotInInput);
 409                    InputProcessor.FromKey(action, KeyCode.A, InputProcessor.Modifier.FocusNotInInput);
 410                    break;
 411                case DCLAction_Hold.BuildEditModeCameraAdvanceRight:
 412                    InputProcessor.FromKey(action, KeyCode.RightArrow, InputProcessor.Modifier.FocusNotInInput);
 413                    InputProcessor.FromKey(action, KeyCode.D, InputProcessor.Modifier.FocusNotInInput);
 414                    break;
 415                case DCLAction_Hold.BuildEditModeCameraAdvanceUp:
 416                    InputProcessor.FromKey(action, KeyCode.E, InputProcessor.Modifier.FocusNotInInput);
 417                    break;
 418                case DCLAction_Hold.BuildEditModeCameraAdvanceDown:
 419                    InputProcessor.FromKey(action, KeyCode.Q, InputProcessor.Modifier.FocusNotInInput);
 420                    break;
 421                case DCLAction_Hold.BuildEditModeCameraPan:
 422                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 423                    break;
 424                default:
 425                    throw new ArgumentOutOfRangeException();
 426            }
 427        }
 428    }
 429
 430    /// <summary>
 431    /// Map the measurable actions to inputs + modifiers and check if their events must be triggered
 432    /// </summary>
 433    private void Update_Measurable(InputAction_Measurable[] measurableActions)
 434    {
 435        for (var i = 0; i < measurableActions.Length; i++)
 436        {
 437            var action = measurableActions[i];
 438            switch (action.GetDCLAction())
 439            {
 440                case DCLAction_Measurable.CharacterXAxis:
 441                    InputProcessor.FromAxis(action, "Horizontal", InputProcessor.Modifier.NeedsPointerLocked);
 442                    break;
 443                case DCLAction_Measurable.CharacterYAxis:
 444                    InputProcessor.FromAxis(action, "Vertical", InputProcessor.Modifier.NeedsPointerLocked);
 445                    break;
 446                case DCLAction_Measurable.CameraXAxis:
 447                    InputProcessor.FromAxis(action, "Mouse X", InputProcessor.Modifier.NeedsPointerLocked);
 448                    break;
 449                case DCLAction_Measurable.CameraYAxis:
 450                    InputProcessor.FromAxis(action, "Mouse Y", InputProcessor.Modifier.NeedsPointerLocked);
 451                    break;
 452                default:
 453                    throw new ArgumentOutOfRangeException();
 454            }
 455        }
 456    }
 457
 458    private void Stop_Measurable(InputAction_Measurable[] measurableActions)
 459    {
 460        for (var i = 0; i < measurableActions.Length; i++)
 461        {
 462            measurableActions[i].RaiseOnValueChanged(0);
 463        }
 464    }
 465}
 466
 467/// <summary>
 468/// Helper class that wraps the processing of inputs and modifiers to trigger actions events
 469/// </summary>
 470public static class InputProcessor
 471{
 1472    private static readonly KeyCode[] MODIFIER_KEYS = new[] { KeyCode.LeftControl, KeyCode.LeftAlt, KeyCode.LeftShift, K
 473
 474    [Flags]
 475    public enum Modifier
 476    {
 477        //Set the values as bit masks
 478        None = 0b0000000, // No modifier needed
 479        NeedsPointerLocked = 0b0000001, // The pointer must be locked to the game
 480        FocusNotInInput = 0b0000010, // The game focus cannot be in an input field
 481    }
 482
 483    /// <summary>
 484    /// Check if the modifier keys are pressed
 485    /// </summary>
 486    /// <param name="modifierKeys"> Keycodes modifiers</param>
 487    /// <returns></returns>
 488    public static Boolean PassModifierKeys(KeyCode[] modifierKeys)
 489    {
 4641038490        for (var i = 0; i < MODIFIER_KEYS.Length; i++)
 491        {
 1864452492            var keyCode = MODIFIER_KEYS[i];
 1864452493            var pressed = Input.GetKey(keyCode);
 1864452494            if (modifierKeys == null)
 495            {
 1824268496                if (pressed)
 0497                    return false;
 498            }
 499            else
 500            {
 40184501                if (modifierKeys.Contains(keyCode) != pressed)
 20092502                    return false;
 503            }
 504        }
 505
 456067506        return true;
 507    }
 508
 509    /// <summary>
 510    /// Check if a miscellaneous modifiers are present. These modifiers are related to the meta-state of the application
 511    /// they can be anything such as mouse pointer state, where the focus is, camera mode...
 512    /// </summary>
 513    /// <param name="modifiers"></param>
 514    /// <returns></returns>
 515    public static bool PassModifiers(Modifier modifiers)
 516    {
 672105517        if (IsModifierSet(modifiers, Modifier.NeedsPointerLocked) && !DCL.Helpers.Utils.isCursorLocked)
 119864518            return false;
 519
 552241520        if (IsModifierSet(modifiers, Modifier.FocusNotInInput) && FocusIsInInputField())
 100521            return false;
 522
 552141523        return true;
 524    }
 525
 526    /// <summary>
 527    /// Process an input action mapped to a keyboard key.
 528    /// </summary>
 529    /// <param name="action">Trigger Action to perform</param>
 530    /// <param name="key">KeyCode mapped to this action</param>
 531    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 532    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 533    public static void FromKey(InputAction_Trigger action, KeyCode key, KeyCode[] modifierKeys = null,
 534        Modifier modifiers = Modifier.None)
 535    {
 451038536        if (!PassModifiers(modifiers))
 15073537            return;
 538
 435965539        if (!PassModifierKeys(modifierKeys))
 20092540            return;
 541
 415873542        if (Input.GetKeyDown(key))
 0543            action.RaiseOnTriggered();
 415873544    }
 545
 546    /// <summary>
 547    /// Process an input action mapped to a button.
 548    /// </summary>
 549    /// <param name="action">Trigger Action to perform</param>
 550    /// <param name="mouseButtonIdx">Index of the mouse button mapped to this action</param>
 551    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 552    public static void FromMouseButton(InputAction_Trigger action, int mouseButtonIdx,
 553        Modifier modifiers = Modifier.None)
 554    {
 0555        if (!PassModifiers(modifiers))
 0556            return;
 557
 0558        if (Input.GetMouseButton(mouseButtonIdx))
 0559            action.RaiseOnTriggered();
 0560    }
 561
 562    /// <summary>
 563    /// Process an input action mapped to a keyboard key
 564    /// </summary>
 565    /// <param name="action">Hold Action to perform</param>
 566    /// <param name="key">KeyCode mapped to this action</param>
 567    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 568    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers = Modifier.None)
 569    {
 140679570        if (!PassModifiers(modifiers))
 44959571            return;
 572
 95720573        if (Input.GetKeyDown(key))
 0574            action.RaiseOnStarted();
 95720575        if (Input.GetKeyUp(key))
 0576            action.RaiseOnFinished();
 95720577    }
 578
 579    /// <summary>
 580    /// Process an input action mapped to a keyboard key
 581    /// </summary>
 582    /// <param name="action">Hold Action to perform</param>
 583    /// <param name="key">KeyCode mapped to this action</param>
 584    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 585    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 586    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers, KeyCode[] modifierKeys)
 587    {
 40194588        if (!PassModifierKeys(modifierKeys))
 0589            return;
 590
 40194591        FromKey(action, key, modifiers);
 40194592    }
 593
 594    /// <summary>
 595    /// Process an input action mapped to a mouse button
 596    /// </summary>
 597    /// <param name="action">Hold Action to perform</param>
 598    /// <param name="mouseButtonIdx">Index of the mouse button</param>
 599    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 600    public static void FromMouse(InputAction_Hold action, int mouseButtonIdx, Modifier modifiers = Modifier.None)
 601    {
 0602        if (!PassModifiers(modifiers))
 0603            return;
 604
 0605        if (Input.GetMouseButtonDown(mouseButtonIdx))
 0606            action.RaiseOnStarted();
 0607        if (Input.GetMouseButtonUp(mouseButtonIdx))
 0608            action.RaiseOnFinished();
 0609    }
 610
 611    /// <summary>
 612    /// Process an input action mapped to an axis
 613    /// </summary>
 614    /// <param name="action">Measurable Action to perform</param>
 615    /// <param name="axisName">Axis name</param>
 616    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 617    public static void FromAxis(InputAction_Measurable action, string axisName, Modifier modifiers = Modifier.None)
 618    {
 80388619        if (!PassModifiers(modifiers))
 620        {
 59932621            action.RaiseOnValueChanged(0);
 59932622            return;
 623        }
 624
 20456625        action.RaiseOnValueChanged(Input.GetAxis(axisName));
 20456626    }
 627
 628    /// <summary>
 629    /// Bitwise check for the modifiers flags
 630    /// </summary>
 631    /// <param name="modifiers">Modifier to check</param>
 632    /// <param name="value">Modifier mapped to a bit to check</param>
 633    /// <returns></returns>
 634    public static bool IsModifierSet(Modifier modifiers, Modifier value)
 635    {
 0636        int flagsValue = (int)modifiers;
 0637        int flagValue = (int)value;
 638
 0639        return (flagsValue & flagValue) != 0;
 640    }
 641
 642    public static bool FocusIsInInputField()
 643    {
 397961644        if (EventSystem.current.currentSelectedGameObject != null &&
 645            (EventSystem.current.currentSelectedGameObject.GetComponent<TMP_InputField>() != null ||
 646             EventSystem.current.currentSelectedGameObject.GetComponent<UnityEngine.UI.InputField>() != null))
 647        {
 100648            return true;
 649        }
 650
 397861651        return false;
 652    }
 653}