| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | | using UnityEngine.UI; |
| | 8 | | using UnityEngine.UIElements; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Helper class that wraps the processing of inputs and modifiers to trigger actions events |
| | 12 | | /// </summary> |
| | 13 | | public static class InputProcessor |
| | 14 | | { |
| 1 | 15 | | private static readonly KeyCode[] MODIFIER_KEYS = { KeyCode.LeftControl, KeyCode.LeftAlt, KeyCode.LeftShift, KeyCode |
| | 16 | |
|
| | 17 | | [Flags] |
| | 18 | | public enum Modifier |
| | 19 | | { |
| | 20 | | //Set the values as bit masks |
| | 21 | | None = 0b0000000, // No modifier needed |
| | 22 | | NeedsPointerLocked = 0b0000001, // The pointer must be locked to the game |
| | 23 | | FocusNotInInput = 0b0000010, // The game focus cannot be in an input field |
| | 24 | | NotInStartMenu = 0b0000100, // The game focus cannot be in full-screen start menu |
| | 25 | | OnlyWithInputFocused = 0b0001000, // The game focus must be in an input field |
| | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Check if the modifier keys are pressed |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="modifierKeys"> Keycodes modifiers</param> |
| | 32 | | /// <returns></returns> |
| | 33 | | private static bool PassModifierKeys(KeyCode[] modifierKeys) |
| | 34 | | { |
| 3462072 | 35 | | for (var i = 0; i < MODIFIER_KEYS.Length; i++) |
| | 36 | | { |
| 1494972 | 37 | | var keyCode = MODIFIER_KEYS[i]; |
| 1494972 | 38 | | bool pressed = Input.GetKey(keyCode); |
| | 39 | |
|
| 1494972 | 40 | | if (modifierKeys == null) |
| | 41 | | { |
| 1180320 | 42 | | if (pressed) |
| 0 | 43 | | return false; |
| | 44 | | } |
| | 45 | | else |
| | 46 | | { |
| 314652 | 47 | | var anyModifierKeysActive = false; |
| | 48 | |
|
| 1192047 | 49 | | foreach (var key in modifierKeys) |
| | 50 | | { |
| 314652 | 51 | | if (key == keyCode) |
| | 52 | | { |
| 66561 | 53 | | anyModifierKeysActive = true; |
| 66561 | 54 | | break; |
| | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| 314652 | 58 | | if (anyModifierKeysActive != pressed) |
| 66561 | 59 | | return false; |
| | 60 | | } |
| | 61 | | } |
| | 62 | |
|
| 236064 | 63 | | return true; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Check if a miscellaneous modifiers are present. These modifiers are related to the meta-state of the application |
| | 68 | | /// they can be anything such as mouse pointer state, where the focus is, camera mode... |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="modifiers"></param> |
| | 71 | | /// <returns></returns> |
| | 72 | | private static bool PassModifiers(Modifier modifiers) |
| | 73 | | { |
| 484080 | 74 | | if (IsModifierSet(modifiers, Modifier.NeedsPointerLocked) && !Utils.IsCursorLocked) |
| 29880 | 75 | | return false; |
| | 76 | |
|
| 454200 | 77 | | bool isInputFieldFocused = FocusIsInInputField(); |
| | 78 | |
|
| 454200 | 79 | | if (IsModifierSet(modifiers, Modifier.FocusNotInInput) && isInputFieldFocused) |
| 0 | 80 | | return false; |
| | 81 | |
|
| 454200 | 82 | | if (IsModifierSet(modifiers, Modifier.OnlyWithInputFocused) && !isInputFieldFocused) |
| 12102 | 83 | | return false; |
| | 84 | |
|
| 442098 | 85 | | if (IsModifierSet(modifiers, Modifier.NotInStartMenu) && IsStartMenuVisible()) |
| 0 | 86 | | return false; |
| | 87 | |
|
| 442098 | 88 | | return true; |
| | 89 | | } |
| | 90 | |
|
| 78663 | 91 | | private static bool IsStartMenuVisible() => DataStore.i.exploreV2.isOpen.Get(); |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Process an input action mapped to a keyboard key. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="action">Trigger Action to perform</param> |
| | 97 | | /// <param name="key">KeyCode mapped to this action</param> |
| | 98 | | /// <param name="modifierKeys">KeyCodes required to perform the action</param> |
| | 99 | | /// <param name="modifiers">Miscellaneous modifiers required for this action</param> |
| | 100 | | public static void FromKey(InputAction_Trigger action, KeyCode key, KeyCode[] modifierKeys = null, Modifier modifier |
| | 101 | | { |
| 308601 | 102 | | if (!PassModifiers(modifiers)) |
| 18078 | 103 | | return; |
| | 104 | |
|
| 290523 | 105 | | if (!PassModifierKeys(modifierKeys)) |
| 66561 | 106 | | return; |
| | 107 | |
|
| 223962 | 108 | | if (Input.GetKeyDown(key)) |
| 0 | 109 | | action.RaiseOnTriggered(); |
| 223962 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Process an input action mapped to a button. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name="action">Trigger Action to perform</param> |
| | 116 | | /// <param name="mouseButtonIdx">Index of the mouse button mapped to this action</param> |
| | 117 | | /// <param name="modifiers">Miscellaneous modifiers required for this action</param> |
| | 118 | | public static void FromMouseButton(InputAction_Trigger action, int mouseButtonIdx, Modifier modifiers = Modifier.Non |
| | 119 | | { |
| 0 | 120 | | if (!PassModifiers(modifiers)) |
| 0 | 121 | | return; |
| | 122 | |
|
| 0 | 123 | | if (Input.GetMouseButton(mouseButtonIdx)) |
| 0 | 124 | | action.RaiseOnTriggered(); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | public static void FromMouseButtonUp(InputAction_Trigger action, int mouseButtonIdx, Modifier modifiers = Modifier.N |
| | 128 | | { |
| 6051 | 129 | | if (!PassModifiers(modifiers)) |
| 5976 | 130 | | return; |
| | 131 | |
|
| 75 | 132 | | if (Input.GetMouseButtonUp(mouseButtonIdx)) |
| 0 | 133 | | action.RaiseOnTriggered(); |
| 75 | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Process an input action mapped to a keyboard key |
| | 138 | | /// </summary> |
| | 139 | | /// <param name="action">Hold Action to perform</param> |
| | 140 | | /// <param name="key">KeyCode mapped to this action</param> |
| | 141 | | /// <param name="modifiers">Miscellaneous modifiers required for this action</param> |
| | 142 | | public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers = Modifier.None) |
| | 143 | | { |
| 96816 | 144 | | if (!PassModifiers(modifiers)) |
| 5976 | 145 | | return; |
| | 146 | |
|
| 90840 | 147 | | if (Input.GetKeyDown(key)) |
| 0 | 148 | | action.RaiseOnStarted(); |
| 90840 | 149 | | if (Input.GetKeyUp(key)) |
| 0 | 150 | | action.RaiseOnFinished(); |
| 90840 | 151 | | } |
| | 152 | |
|
| | 153 | | /// <summary> |
| | 154 | | /// Process an input action mapped to a keyboard key |
| | 155 | | /// </summary> |
| | 156 | | /// <param name="action">Hold Action to perform</param> |
| | 157 | | /// <param name="key">KeyCode mapped to this action</param> |
| | 158 | | /// <param name="modifiers">Miscellaneous modifiers required for this action</param> |
| | 159 | | /// <param name="modifierKeys">KeyCodes required to perform the action</param> |
| | 160 | | public static void FromKey(InputAction_Hold action, KeyCode key, Modifier modifiers, KeyCode[] modifierKeys) |
| | 161 | | { |
| 12102 | 162 | | if (!PassModifierKeys(modifierKeys)) |
| 0 | 163 | | return; |
| | 164 | |
|
| 12102 | 165 | | FromKey(action, key, modifiers); |
| 12102 | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary> |
| | 169 | | /// Process an input action mapped to a mouse button |
| | 170 | | /// </summary> |
| | 171 | | /// <param name="action">Hold Action to perform</param> |
| | 172 | | /// <param name="mouseButtonIdx">Index of the mouse button</param> |
| | 173 | | /// <param name="modifiers">Miscellaneous modifiers required for this action</param> |
| | 174 | | public static void FromMouse(InputAction_Hold action, int mouseButtonIdx, Modifier modifiers = Modifier.None) |
| | 175 | | { |
| 18153 | 176 | | if (!PassModifiers(modifiers)) |
| 0 | 177 | | return; |
| | 178 | |
|
| 18153 | 179 | | if (Input.GetMouseButtonDown(mouseButtonIdx)) |
| 0 | 180 | | action.RaiseOnStarted(); |
| 18153 | 181 | | if (Input.GetMouseButtonUp(mouseButtonIdx)) |
| 0 | 182 | | action.RaiseOnFinished(); |
| 18153 | 183 | | } |
| | 184 | |
|
| | 185 | | /// <summary> |
| | 186 | | /// Process an input action mapped to an axis |
| | 187 | | /// </summary> |
| | 188 | | /// <param name="action">Measurable Action to perform</param> |
| | 189 | | /// <param name="axisName">Axis name</param> |
| | 190 | | /// <param name="modifiers">Miscellaneous modifiers required for this action</param> |
| | 191 | | public static void FromAxis(InputAction_Measurable action, string axisName, Modifier modifiers = Modifier.None) |
| | 192 | | { |
| 54459 | 193 | | if (!PassModifiers(modifiers)) |
| | 194 | | { |
| 11952 | 195 | | action.SetValue(0); |
| 11952 | 196 | | return; |
| | 197 | | } |
| | 198 | |
|
| 42507 | 199 | | action.SetValue(Input.GetAxis(axisName)); |
| 42507 | 200 | | } |
| | 201 | |
|
| | 202 | | /// <summary> |
| | 203 | | /// Bitwise check for the modifiers flags |
| | 204 | | /// </summary> |
| | 205 | | /// <param name="modifiers">Modifier to check</param> |
| | 206 | | /// <param name="value">Modifier mapped to a bit to check</param> |
| | 207 | | /// <returns></returns> |
| | 208 | | private static bool IsModifierSet(Modifier modifiers, Modifier value) |
| | 209 | | { |
| 1834578 | 210 | | var flagsValue = (int)modifiers; |
| 1834578 | 211 | | var flagValue = (int)value; |
| | 212 | |
|
| 1834578 | 213 | | return (flagsValue & flagValue) != 0; |
| | 214 | | } |
| | 215 | |
|
| | 216 | | public static bool FocusIsInInputField() |
| | 217 | | { |
| 454200 | 218 | | if (EventSystem.current == null) |
| 427350 | 219 | | return false; |
| | 220 | |
|
| 26850 | 221 | | return EventSystem.current.currentSelectedGameObject != null && |
| | 222 | | (EventSystem.current.currentSelectedGameObject.GetComponent<TMP_InputField>() != null || |
| | 223 | | EventSystem.current.currentSelectedGameObject.GetComponent<InputField>() != null || |
| | 224 | | FocusIsInTextField(EventSystem.current.currentSelectedGameObject)); |
| | 225 | | } |
| | 226 | |
|
| | 227 | | /// <summary> |
| | 228 | | /// Checks VisualElement from UI Toolkit |
| | 229 | | /// </summary> |
| | 230 | | private static bool FocusIsInTextField(GameObject currentSelectedObject) => |
| 0 | 231 | | currentSelectedObject.GetComponent<PanelEventHandler>()?.panel?.focusController?.focusedElement is TextField; |
| | 232 | | } |