< 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:650
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;
 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
 28    OpenExpressions = 200,
 29    Expression_Wave = 201,
 30    Expression_FistPump = 202,
 31    Expression_Robot = 203,
 32    Expression_RaiseHand = 204,
 33    Expression_Clap = 205,
 34    Expression_ThrowMoney = 206,
 35    Expression_SendKiss = 207,
 36
 37    //Builder In World 4xx
 38    BuildEditModeChange = 408,
 39    BuildEditModeToggleUI = 409,
 40    BuildEditModeToggleEntityList = 410,
 41    BuildEditModeToggleCatalog = 411,
 42    BuildEditModeToggleSceneInfo = 412,
 43    BuildEditModeToggleChangeCamera = 413,
 44    BuildEditModeToggleControls = 414,
 45    BuildEditModeToggleSnapMode = 415,
 46    BuildEditModeUndoAction = 417,
 47    BuildEditModeRedoAction = 418,
 48    BuildEditModeQuickBar1 = 419,
 49    BuildEditModeQuickBar2 = 420,
 50    BuildEditModeQuickBar3 = 421,
 51    BuildEditModeQuickBar4 = 422,
 52    BuildEditModeQuickBar5 = 423,
 53    BuildEditModeQuickBar6 = 424,
 54    BuildEditModeQuickBar7 = 425,
 55    BuildEditModeQuickBar8 = 426,
 56    BuildEditModeQuickBar9 = 427,
 57    BuildEditModeDuplicate = 428,
 58    BuildEditModeTranslate = 429,
 59    BuildEditModeRotate = 430,
 60    BuildEditModeScale = 431,
 61    BuildEditModeDelete = 434,
 62    BuildEditModeFocusSelectedEntities = 435,
 63    BuildEditModeReset = 443,
 64    BuildEditHideSelectedEntities = 444,
 65    BuildEditShowAllEntities = 445,
 66    BuildEditModeResetCamera = 446,
 67    BuildEditModeZoomIn = 447,
 68    BuildEditModeZoomOut = 448
 69}
 70
 71/// <summary>
 72/// Mapping for hold actions
 73/// </summary>
 74public enum DCLAction_Hold
 75{
 76    //Remember to explicitly assign the value to each entry so we minimize issues with serialization + conflicts
 77    Sprint = 1,
 78    Jump = 2,
 79    FreeCameraMode = 101,
 80    VoiceChatRecording = 102,
 81    DefaultConfirmAction = 300,
 82    DefaultCancelAction = 301,
 83    BuildEditModeMultiSelection = 432,
 84    BuildEditModeSquareMultiSelection = 433,
 85    BuildEditModeFirstPersonRotation = 436,
 86    BuildEditModeCameraAdvanceFoward = 437,
 87    BuildEditModeCameraAdvanceBack = 438,
 88    BuildEditModeCameraAdvanceLeft = 439,
 89    BuildEditModeCameraAdvanceRight = 440,
 90    BuildEditModeCameraAdvanceUp = 441,
 91    BuildEditModeCameraAdvanceDown = 442,
 92    BuildEditModeCameraPan = 446
 93}
 94
 95/// <summary>
 96/// Mapping for measurable actions
 97/// </summary>
 98public enum DCLAction_Measurable
 99{
 100    //Remember to explicitly assign the value to each entry so we minimize issues with serialization + conflicts
 101    CharacterXAxis = 1,
 102    CharacterYAxis = 2,
 103    CameraXAxis = 3,
 104    CameraYAxis = 4,
 105}
 106
 107/// <summary>
 108/// Group of actions currently actived
 109/// </summary>
 110public enum InputTypeMode
 111{
 112    OFF,
 113    GENERAL,
 114    BUILD_MODE_LOADING,
 115    BUILD_MODE
 116}
 117
 118/// <summary>
 119/// Input Controller will map inputs(keys/mouse/axis) to DCL actions, check if they can be triggered (modifiers) and rai
 120/// </summary>
 121public class InputController : MonoBehaviour
 122{
 123    public static bool ENABLE_THIRD_PERSON_CAMERA = true;
 124
 125    [Header("General Input")]
 126    public InputAction_Trigger[] triggerTimeActions;
 127
 128    public InputAction_Hold[] holdActions;
 129    public InputAction_Measurable[] measurableActions;
 130
 131    [Header("BuildMode Input")]
 132    public InputAction_Trigger[] builderTriggerTimeActions;
 133
 134    public InputAction_Hold[] builderHoldActions;
 135    public InputAction_Trigger[] loadingBuilderTriggerTimeActions;
 136
 137    bool renderingEnabled => CommonScriptableObjects.rendererState.Get();
 138    bool allUIHidden => CommonScriptableObjects.allUIHidden.Get();
 139    public InputTypeMode inputTypeMode { get; set; } = InputTypeMode.GENERAL;
 140
 141    private void Update()
 142    {
 143        if (!renderingEnabled)
 144        {
 145            Stop_Measurable(measurableActions);
 146            return;
 147        }
 148
 149        switch (inputTypeMode)
 150        {
 151            case InputTypeMode.OFF:
 152                Stop_Measurable(measurableActions);
 153                return;
 154            case InputTypeMode.GENERAL:
 155                Update_Trigger(triggerTimeActions);
 156                Update_Hold(holdActions);
 157                Update_Measurable(measurableActions);
 158                break;
 159            case InputTypeMode.BUILD_MODE_LOADING:
 160                Update_Trigger(loadingBuilderTriggerTimeActions);
 161                Stop_Measurable(measurableActions);
 162                break;
 163            case InputTypeMode.BUILD_MODE:
 164                Update_Trigger(builderTriggerTimeActions);
 165                Update_Hold(builderHoldActions);
 166                Update_Measurable(measurableActions);
 167                break;
 168        }
 169    }
 170
 171    /// <summary>
 172    /// Map the trigger actions to inputs + modifiers and check if their events must be triggered
 173    /// </summary>
 174    public void Update_Trigger(InputAction_Trigger[] triggerTimeActions)
 175    {
 176        for (var i = 0; i < triggerTimeActions.Length; i++)
 177        {
 178            var action = triggerTimeActions[i];
 179
 180            if (action.isTriggerBlocked != null && action.isTriggerBlocked.Get())
 181                continue;
 182
 183            switch (action.GetDCLAction())
 184            {
 185                case DCLAction_Trigger.CameraChange:
 186                    //Disable until the fine-tuning is ready
 187                    if (ENABLE_THIRD_PERSON_CAMERA)
 188                        InputProcessor.FromKey(action, KeyCode.V,
 189                            modifiers: InputProcessor.Modifier.NeedsPointerLocked |
 190                                       InputProcessor.Modifier.FocusNotInInput);
 191                    break;
 192                case DCLAction_Trigger.ToggleNavMap:
 193                    if (allUIHidden)
 194                        break;
 195                    InputProcessor.FromKey(action, KeyCode.M, modifiers: InputProcessor.Modifier.FocusNotInInput);
 196                    InputProcessor.FromKey(action, KeyCode.Tab, modifiers: InputProcessor.Modifier.FocusNotInInput);
 197                    InputProcessor.FromKey(action, KeyCode.Escape, modifiers: InputProcessor.Modifier.FocusNotInInput);
 198                    break;
 199                case DCLAction_Trigger.ToggleFriends:
 200                    if (allUIHidden)
 201                        break;
 202                    InputProcessor.FromKey(action, KeyCode.L, modifiers: InputProcessor.Modifier.None);
 203                    break;
 204                case DCLAction_Trigger.ToggleWorldChat:
 205                    if (allUIHidden)
 206                        break;
 207                    InputProcessor.FromKey(action, KeyCode.Return, modifiers: InputProcessor.Modifier.None);
 208                    break;
 209                case DCLAction_Trigger.ToggleUIVisibility:
 210                    InputProcessor.FromKey(action, KeyCode.U, modifiers: InputProcessor.Modifier.None);
 211                    break;
 212                case DCLAction_Trigger.CloseWindow:
 213                    if (allUIHidden || DataStore.i.isSignUpFlow.Get())
 214                        break;
 215                    InputProcessor.FromKey(action, KeyCode.Escape, modifiers: InputProcessor.Modifier.None);
 216                    break;
 217                case DCLAction_Trigger.OpenExpressions:
 218                    if (allUIHidden)
 219                        break;
 220                    InputProcessor.FromKey(action, KeyCode.B, modifiers: InputProcessor.Modifier.FocusNotInInput);
 221                    InputProcessor.FromKey(action, KeyCode.Escape, 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                default:
 351                    throw new ArgumentOutOfRangeException();
 352            }
 353        }
 354    }
 355
 356    /// <summary>
 357    /// Map the hold actions to inputs + modifiers and check if their events must be triggered
 358    /// </summary>
 359    private void Update_Hold(InputAction_Hold[] holdActions)
 360    {
 361        for (var i = 0; i < holdActions.Length; i++)
 362        {
 363            var action = holdActions[i];
 364            switch (action.GetDCLAction())
 365            {
 366                case DCLAction_Hold.Sprint:
 367                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.NeedsPointerLocked);
 368                    break;
 369                case DCLAction_Hold.Jump:
 370                    InputProcessor.FromKey(action, KeyCode.Space, InputProcessor.Modifier.NeedsPointerLocked);
 371                    break;
 372                case DCLAction_Hold.FreeCameraMode:
 373                    //Disable until the fine-tuning is ready
 374                    if (ENABLE_THIRD_PERSON_CAMERA)
 375                        InputProcessor.FromKey(action, KeyCode.Y, InputProcessor.Modifier.NeedsPointerLocked);
 376                    break;
 377                case DCLAction_Hold.VoiceChatRecording:
 378                    // Push to talk functionality only triggers if no modifier key is pressed
 379                    InputProcessor.FromKey(action, KeyCode.T, InputProcessor.Modifier.FocusNotInInput, null);
 380                    break;
 381                case DCLAction_Hold.DefaultConfirmAction:
 382                    InputProcessor.FromKey(action, KeyCode.E, InputProcessor.Modifier.None);
 383                    break;
 384                case DCLAction_Hold.DefaultCancelAction:
 385                    InputProcessor.FromKey(action, KeyCode.F, InputProcessor.Modifier.None);
 386                    break;
 387                case DCLAction_Hold.BuildEditModeMultiSelection:
 388                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 389                    break;
 390                case DCLAction_Hold.BuildEditModeSquareMultiSelection:
 391                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 392                    break;
 393                case DCLAction_Hold.BuildEditModeFirstPersonRotation:
 394                    InputProcessor.FromKey(action, KeyCode.R, InputProcessor.Modifier.FocusNotInInput);
 395                    break;
 396                case DCLAction_Hold.BuildEditModeCameraAdvanceFoward:
 397                    InputProcessor.FromKey(action, KeyCode.UpArrow, InputProcessor.Modifier.FocusNotInInput);
 398                    InputProcessor.FromKey(action, KeyCode.W, InputProcessor.Modifier.FocusNotInInput);
 399                    break;
 400                case DCLAction_Hold.BuildEditModeCameraAdvanceBack:
 401                    InputProcessor.FromKey(action, KeyCode.DownArrow, InputProcessor.Modifier.FocusNotInInput);
 402                    InputProcessor.FromKey(action, KeyCode.S, InputProcessor.Modifier.FocusNotInInput);
 403                    break;
 404                case DCLAction_Hold.BuildEditModeCameraAdvanceLeft:
 405                    InputProcessor.FromKey(action, KeyCode.LeftArrow, InputProcessor.Modifier.FocusNotInInput);
 406                    InputProcessor.FromKey(action, KeyCode.A, InputProcessor.Modifier.FocusNotInInput);
 407                    break;
 408                case DCLAction_Hold.BuildEditModeCameraAdvanceRight:
 409                    InputProcessor.FromKey(action, KeyCode.RightArrow, InputProcessor.Modifier.FocusNotInInput);
 410                    InputProcessor.FromKey(action, KeyCode.D, InputProcessor.Modifier.FocusNotInInput);
 411                    break;
 412                case DCLAction_Hold.BuildEditModeCameraAdvanceUp:
 413                    InputProcessor.FromKey(action, KeyCode.E, InputProcessor.Modifier.FocusNotInInput);
 414                    break;
 415                case DCLAction_Hold.BuildEditModeCameraAdvanceDown:
 416                    InputProcessor.FromKey(action, KeyCode.Q, InputProcessor.Modifier.FocusNotInInput);
 417                    break;
 418                case DCLAction_Hold.BuildEditModeCameraPan:
 419                    InputProcessor.FromKey(action, KeyCode.LeftShift, InputProcessor.Modifier.FocusNotInInput);
 420                    break;
 421                default:
 422                    throw new ArgumentOutOfRangeException();
 423            }
 424        }
 425    }
 426
 427    /// <summary>
 428    /// Map the measurable actions to inputs + modifiers and check if their events must be triggered
 429    /// </summary>
 430    private void Update_Measurable(InputAction_Measurable[] measurableActions)
 431    {
 432        for (var i = 0; i < measurableActions.Length; i++)
 433        {
 434            var action = measurableActions[i];
 435            switch (action.GetDCLAction())
 436            {
 437                case DCLAction_Measurable.CharacterXAxis:
 438                    InputProcessor.FromAxis(action, "Horizontal", InputProcessor.Modifier.NeedsPointerLocked);
 439                    break;
 440                case DCLAction_Measurable.CharacterYAxis:
 441                    InputProcessor.FromAxis(action, "Vertical", InputProcessor.Modifier.NeedsPointerLocked);
 442                    break;
 443                case DCLAction_Measurable.CameraXAxis:
 444                    InputProcessor.FromAxis(action, "Mouse X", InputProcessor.Modifier.NeedsPointerLocked);
 445                    break;
 446                case DCLAction_Measurable.CameraYAxis:
 447                    InputProcessor.FromAxis(action, "Mouse Y", InputProcessor.Modifier.NeedsPointerLocked);
 448                    break;
 449                default:
 450                    throw new ArgumentOutOfRangeException();
 451            }
 452        }
 453    }
 454
 455    private void Stop_Measurable(InputAction_Measurable[] measurableActions)
 456    {
 457        for (var i = 0; i < measurableActions.Length; i++)
 458        {
 459            measurableActions[i].RaiseOnValueChanged(0);
 460        }
 461    }
 462}
 463
 464/// <summary>
 465/// Helper class that wraps the processing of inputs and modifiers to trigger actions events
 466/// </summary>
 467public static class InputProcessor
 468{
 1469    private static readonly KeyCode[] MODIFIER_KEYS = new[] { KeyCode.LeftControl, KeyCode.LeftAlt, KeyCode.LeftShift, K
 470
 471    [Flags]
 472    public enum Modifier
 473    {
 474        //Set the values as bit masks
 475        None = 0b0000000, // No modifier needed
 476        NeedsPointerLocked = 0b0000001, // The pointer must be locked to the game
 477        FocusNotInInput = 0b0000010, // The game focus cannot be in an input field
 478    }
 479
 480    /// <summary>
 481    /// Check if the modifier keys are pressed
 482    /// </summary>
 483    /// <param name="modifierKeys"> Keycodes modifiers</param>
 484    /// <returns></returns>
 485    public static Boolean PassModifierKeys(KeyCode[] modifierKeys)
 486    {
 4066908487        for (var i = 0; i < MODIFIER_KEYS.Length; i++)
 488        {
 1633692489            var keyCode = MODIFIER_KEYS[i];
 1633692490            var pressed = Input.GetKey(keyCode);
 1633692491            if (modifierKeys == null)
 492            {
 1599048493                if (pressed)
 0494                    return false;
 495            }
 496            else
 497            {
 34644498                if (modifierKeys.Contains(keyCode) != pressed)
 17322499                    return false;
 500            }
 501        }
 502
 399762503        return true;
 504    }
 505
 506    /// <summary>
 507    /// Check if a miscellaneous modifiers are present. These modifiers are related to the meta-state of the application
 508    /// they can be anything such as mouse pointer state, where the focus is, camera mode...
 509    /// </summary>
 510    /// <param name="modifiers"></param>
 511    /// <returns></returns>
 512    public static bool PassModifiers(Modifier modifiers)
 513    {
 590324514        if (IsModifierSet(modifiers, Modifier.NeedsPointerLocked) && !DCL.Helpers.Utils.isCursorLocked)
 137616515            return false;
 516
 452708517        if (IsModifierSet(modifiers, Modifier.FocusNotInInput) && FocusIsInInputField())
 105518            return false;
 519
 452603520        return true;
 521    }
 522
 523    /// <summary>
 524    /// Process an input action mapped to a keyboard key.
 525    /// </summary>
 526    /// <param name="action">Trigger Action to perform</param>
 527    /// <param name="key">KeyCode mapped to this action</param>
 528    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 529    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 530    public static void FromKey(InputAction_Trigger action, KeyCode key, KeyCode[] modifierKeys = null,
 531        Modifier modifiers = Modifier.None)
 532    {
 399727533        if (!PassModifiers(modifiers))
 17297534            return;
 535
 382430536        if (!PassModifierKeys(modifierKeys))
 17322537            return;
 538
 365108539        if (Input.GetKeyDown(key))
 0540            action.RaiseOnTriggered();
 365108541    }
 542
 543    /// <summary>
 544    /// Process an input action mapped to a button.
 545    /// </summary>
 546    /// <param name="action">Trigger Action to perform</param>
 547    /// <param name="mouseButtonIdx">Index of the mouse button mapped to this action</param>
 548    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 549    public static void FromMouseButton(InputAction_Trigger action, int mouseButtonIdx,
 550        Modifier modifiers = Modifier.None)
 551    {
 0552        if (!PassModifiers(modifiers))
 0553            return;
 554
 0555        if (Input.GetMouseButton(mouseButtonIdx))
 0556            action.RaiseOnTriggered();
 0557    }
 558
 559    /// <summary>
 560    /// Process an input action mapped to a keyboard key
 561    /// </summary>
 562    /// <param name="action">Hold Action to perform</param>
 563    /// <param name="key">KeyCode mapped to this action</param>
 564    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 565    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers = Modifier.None)
 566    {
 121289567        if (!PassModifiers(modifiers))
 51616568            return;
 569
 69673570        if (Input.GetKeyDown(key))
 0571            action.RaiseOnStarted();
 69673572        if (Input.GetKeyUp(key))
 0573            action.RaiseOnFinished();
 69673574    }
 575
 576    /// <summary>
 577    /// Process an input action mapped to a keyboard key
 578    /// </summary>
 579    /// <param name="action">Hold Action to perform</param>
 580    /// <param name="key">KeyCode mapped to this action</param>
 581    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 582    /// <param name="modifierKeys">KeyCodes required to perform the action</param>
 583    public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers, KeyCode[] modifierKeys)
 584    {
 34654585        if (!PassModifierKeys(modifierKeys))
 0586            return;
 587
 34654588        FromKey(action, key, modifiers);
 34654589    }
 590
 591    /// <summary>
 592    /// Process an input action mapped to a mouse button
 593    /// </summary>
 594    /// <param name="action">Hold Action to perform</param>
 595    /// <param name="mouseButtonIdx">Index of the mouse button</param>
 596    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 597    public static void FromMouse(InputAction_Hold action, int mouseButtonIdx, Modifier modifiers = Modifier.None)
 598    {
 0599        if (!PassModifiers(modifiers))
 0600            return;
 601
 0602        if (Input.GetMouseButtonDown(mouseButtonIdx))
 0603            action.RaiseOnStarted();
 0604        if (Input.GetMouseButtonUp(mouseButtonIdx))
 0605            action.RaiseOnFinished();
 0606    }
 607
 608    /// <summary>
 609    /// Process an input action mapped to an axis
 610    /// </summary>
 611    /// <param name="action">Measurable Action to perform</param>
 612    /// <param name="axisName">Axis name</param>
 613    /// <param name="modifiers">Miscellaneous modifiers required for this action</param>
 614    public static void FromAxis(InputAction_Measurable action, string axisName, Modifier modifiers = Modifier.None)
 615    {
 69308616        if (!PassModifiers(modifiers))
 617        {
 68808618            action.RaiseOnValueChanged(0);
 68808619            return;
 620        }
 621
 500622        action.RaiseOnValueChanged(Input.GetAxis(axisName));
 500623    }
 624
 625    /// <summary>
 626    /// Bitwise check for the modifiers flags
 627    /// </summary>
 628    /// <param name="modifiers">Modifier to check</param>
 629    /// <param name="value">Modifier mapped to a bit to check</param>
 630    /// <returns></returns>
 631    public static bool IsModifierSet(Modifier modifiers, Modifier value)
 632    {
 0633        int flagsValue = (int)modifiers;
 0634        int flagValue = (int)value;
 635
 0636        return (flagsValue & flagValue) != 0;
 637    }
 638
 639    public static bool FocusIsInInputField()
 640    {
 350580641        if (EventSystem.current.currentSelectedGameObject != null &&
 642            (EventSystem.current.currentSelectedGameObject.GetComponent<TMP_InputField>() != null ||
 643             EventSystem.current.currentSelectedGameObject.GetComponent<UnityEngine.UI.InputField>() != null))
 644        {
 105645            return true;
 646        }
 647
 350475648        return false;
 649    }
 650}