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