| | 1 | | using DCL.Configuration; |
| | 2 | | using DCL.Interface; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class InputController_Legacy |
| | 10 | | { |
| 19774 | 11 | | private static bool renderingEnabled => CommonScriptableObjects.rendererState.Get(); |
| | 12 | | private static InputController_Legacy instance = null; |
| | 13 | |
|
| | 14 | | public static InputController_Legacy i |
| | 15 | | { |
| | 16 | | get |
| | 17 | | { |
| 23885 | 18 | | if (instance == null) |
| 1 | 19 | | instance = new InputController_Legacy(); |
| | 20 | |
|
| 23885 | 21 | | return instance; |
| | 22 | | } |
| | 23 | | } |
| | 24 | |
|
| | 25 | | public enum EVENT |
| | 26 | | { |
| | 27 | | BUTTON_DOWN, |
| | 28 | | BUTTON_UP |
| | 29 | | } |
| | 30 | |
|
| | 31 | | private enum BUTTON_TYPE |
| | 32 | | { |
| | 33 | | MOUSE, |
| | 34 | | KEYBOARD |
| | 35 | | } |
| | 36 | |
|
| | 37 | | private struct BUTTON_MAP |
| | 38 | | { |
| | 39 | | public BUTTON_TYPE type; |
| | 40 | | public int buttonNum; |
| | 41 | | public WebInterface.ACTION_BUTTON buttonId; |
| | 42 | | public bool useRaycast; |
| | 43 | | } |
| | 44 | |
|
| 1 | 45 | | private Dictionary<WebInterface.ACTION_BUTTON, List<Action<WebInterface.ACTION_BUTTON, EVENT, bool>>> listeners |
| 1 | 46 | | private List<BUTTON_MAP> buttonsMap = new List<BUTTON_MAP>(); |
| | 47 | |
|
| 1 | 48 | | private InputController_Legacy() |
| | 49 | | { |
| 1 | 50 | | buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.MOUSE, buttonNum = 0, buttonId = WebInterface.ACTION_BU |
| 1 | 51 | | buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.PrimaryButton |
| 1 | 52 | | buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.SecondaryButt |
| 1 | 53 | | } |
| | 54 | |
|
| | 55 | | public void AddListener(WebInterface.ACTION_BUTTON buttonId, Action<WebInterface.ACTION_BUTTON, EVENT, bool> cal |
| | 56 | | { |
| 1998 | 57 | | if (!listeners.ContainsKey(buttonId)) |
| 1998 | 58 | | listeners.Add(buttonId, new List<Action<WebInterface.ACTION_BUTTON, EVENT, bool>>()); |
| | 59 | |
|
| 1998 | 60 | | if (!listeners[buttonId].Contains(callback)) |
| 1998 | 61 | | listeners[buttonId].Add(callback); |
| 1998 | 62 | | } |
| | 63 | |
|
| | 64 | | public void RemoveListener(WebInterface.ACTION_BUTTON buttonId, Action<WebInterface.ACTION_BUTTON, EVENT, bool> |
| | 65 | | { |
| 2061 | 66 | | if (listeners.ContainsKey(buttonId)) |
| | 67 | | { |
| 1998 | 68 | | if (listeners[buttonId].Contains(callback)) |
| | 69 | | { |
| 1998 | 70 | | listeners[buttonId].Remove(callback); |
| | 71 | |
|
| 1998 | 72 | | if (listeners[buttonId].Count == 0) |
| 1998 | 73 | | listeners.Remove(buttonId); |
| | 74 | | } |
| | 75 | | } |
| 2061 | 76 | | } |
| | 77 | |
|
| | 78 | | // Note (Zak): it is public for testing purposes only |
| | 79 | | public void RaiseEvent(WebInterface.ACTION_BUTTON buttonId, EVENT evt, bool useRaycast) |
| | 80 | | { |
| 14 | 81 | | if (!listeners.ContainsKey(buttonId)) |
| 0 | 82 | | return; |
| | 83 | |
|
| 14 | 84 | | List<Action<WebInterface.ACTION_BUTTON, EVENT, bool>> callbacks = listeners[buttonId]; |
| 14 | 85 | | int count = callbacks.Count; |
| | 86 | |
|
| 56 | 87 | | for (int i = 0; i < count; i++) |
| | 88 | | { |
| 14 | 89 | | callbacks[i].Invoke(buttonId, evt, useRaycast); |
| | 90 | | } |
| 14 | 91 | | } |
| | 92 | |
|
| | 93 | | public void Update() |
| | 94 | | { |
| 19774 | 95 | | if (!renderingEnabled) |
| 348 | 96 | | return; |
| | 97 | |
|
| 19426 | 98 | | int count = buttonsMap.Count; |
| | 99 | |
|
| 155408 | 100 | | for (int i = 0; i < count; i++) |
| | 101 | | { |
| 58278 | 102 | | BUTTON_MAP btnMap = buttonsMap[i]; |
| | 103 | |
|
| 58278 | 104 | | switch (btnMap.type) |
| | 105 | | { |
| | 106 | | case BUTTON_TYPE.MOUSE: |
| 19426 | 107 | | if (CommonScriptableObjects.allUIHidden.Get()) |
| | 108 | | break; |
| 11805 | 109 | | if (Input.GetMouseButtonDown(btnMap.buttonNum)) |
| 0 | 110 | | RaiseEvent(btnMap.buttonId, EVENT.BUTTON_DOWN, btnMap.useRaycast); |
| 11805 | 111 | | else if (Input.GetMouseButtonUp(btnMap.buttonNum)) |
| 0 | 112 | | RaiseEvent(btnMap.buttonId, EVENT.BUTTON_UP, btnMap.useRaycast); |
| 0 | 113 | | break; |
| | 114 | | case BUTTON_TYPE.KEYBOARD: |
| 38852 | 115 | | if (CommonScriptableObjects.allUIHidden.Get()) |
| | 116 | | break; |
| 23610 | 117 | | if (Input.GetKeyDown((KeyCode) btnMap.buttonNum)) |
| 0 | 118 | | RaiseEvent(btnMap.buttonId, EVENT.BUTTON_DOWN, btnMap.useRaycast); |
| 23610 | 119 | | else if (Input.GetKeyUp((KeyCode) btnMap.buttonNum)) |
| 0 | 120 | | RaiseEvent(btnMap.buttonId, EVENT.BUTTON_UP, btnMap.useRaycast); |
| | 121 | | break; |
| | 122 | | } |
| | 123 | | } |
| 19426 | 124 | | } |
| | 125 | |
|
| | 126 | | public bool IsPressed(WebInterface.ACTION_BUTTON button) |
| | 127 | | { |
| | 128 | | switch (button) |
| | 129 | | { |
| | 130 | | case WebInterface.ACTION_BUTTON.POINTER: |
| 0 | 131 | | return Input.GetMouseButton(0); |
| | 132 | | case WebInterface.ACTION_BUTTON.PRIMARY: |
| 2 | 133 | | return Input.GetKey(InputSettings.PrimaryButtonKeyCode); |
| | 134 | | case WebInterface.ACTION_BUTTON.SECONDARY: |
| 0 | 135 | | return Input.GetKey(InputSettings.SecondaryButtonKeyCode); |
| | 136 | | default: // ANY |
| 36 | 137 | | return Input.GetMouseButton(0) || |
| | 138 | | Input.GetKey(InputSettings.PrimaryButtonKeyCode) || |
| | 139 | | Input.GetKey(InputSettings.SecondaryButtonKeyCode); |
| | 140 | | } |
| | 141 | | } |
| | 142 | | } |
| | 143 | | } |