| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Interface; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class InputController_Legacy : IDisposable |
| | 10 | | { |
| | 11 | | public delegate void ButtonListenerCallback(WebInterface.ACTION_BUTTON buttonId, EVENT eventType, |
| | 12 | | bool useRaycast, bool enablePointerEvent); |
| | 13 | |
|
| 1766 | 14 | | private static bool renderingEnabled => CommonScriptableObjects.rendererState.Get(); |
| | 15 | |
|
| | 16 | | public enum EVENT |
| | 17 | | { |
| | 18 | | BUTTON_DOWN, |
| | 19 | | BUTTON_UP |
| | 20 | | } |
| | 21 | |
|
| | 22 | | private enum BUTTON_TYPE |
| | 23 | | { |
| | 24 | | MOUSE, |
| | 25 | | KEYBOARD |
| | 26 | | } |
| | 27 | |
|
| | 28 | | private struct BUTTON_MAP |
| | 29 | | { |
| | 30 | | public BUTTON_TYPE type; |
| | 31 | | public int buttonNum; |
| | 32 | | public WebInterface.ACTION_BUTTON buttonId; |
| | 33 | | public bool useRaycast; |
| | 34 | | public bool enablePointerEvent; |
| | 35 | | } |
| | 36 | |
|
| 83 | 37 | | private Dictionary<WebInterface.ACTION_BUTTON, List<ButtonListenerCallback>> listeners = |
| | 38 | | new Dictionary<WebInterface.ACTION_BUTTON, List<ButtonListenerCallback>>(); |
| | 39 | |
|
| 83 | 40 | | private List<BUTTON_MAP> buttonsMap = new List<BUTTON_MAP>(); |
| | 41 | |
|
| 83 | 42 | | public InputController_Legacy() |
| | 43 | | { |
| 83 | 44 | | buttonsMap.Add(new BUTTON_MAP() |
| | 45 | | { |
| | 46 | | type = BUTTON_TYPE.MOUSE, buttonNum = 0, buttonId = WebInterface.ACTION_BUTTON.POINTER, |
| | 47 | | useRaycast = true, enablePointerEvent = true |
| | 48 | | }); |
| 83 | 49 | | buttonsMap.Add(new BUTTON_MAP() |
| | 50 | | { |
| | 51 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.PrimaryButtonKeyCode, |
| | 52 | | buttonId = WebInterface.ACTION_BUTTON.PRIMARY, useRaycast = true, enablePointerEvent = true |
| | 53 | | }); |
| 83 | 54 | | buttonsMap.Add(new BUTTON_MAP() |
| | 55 | | { |
| | 56 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.SecondaryButtonKeyCode, |
| | 57 | | buttonId = WebInterface.ACTION_BUTTON.SECONDARY, useRaycast = true, enablePointerEvent = true |
| | 58 | | }); |
| | 59 | |
|
| 83 | 60 | | buttonsMap.Add(new BUTTON_MAP() |
| | 61 | | { |
| | 62 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ForwardButtonKeyCode, |
| | 63 | | buttonId = WebInterface.ACTION_BUTTON.FORWARD, useRaycast = false, enablePointerEvent = false |
| | 64 | | }); |
| 83 | 65 | | buttonsMap.Add(new BUTTON_MAP() |
| | 66 | | { |
| | 67 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ForwardButtonKeyCodeAlt, |
| | 68 | | buttonId = WebInterface.ACTION_BUTTON.FORWARD, useRaycast = false, enablePointerEvent = false |
| | 69 | | }); |
| | 70 | |
|
| 83 | 71 | | buttonsMap.Add(new BUTTON_MAP() |
| | 72 | | { |
| | 73 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.BackwardButtonKeyCode, |
| | 74 | | buttonId = WebInterface.ACTION_BUTTON.BACKWARD, useRaycast = false, enablePointerEvent = false |
| | 75 | | }); |
| 83 | 76 | | buttonsMap.Add(new BUTTON_MAP() |
| | 77 | | { |
| | 78 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.BackwardButtonKeyCodeAlt, |
| | 79 | | buttonId = WebInterface.ACTION_BUTTON.BACKWARD, useRaycast = false, enablePointerEvent = false |
| | 80 | | }); |
| | 81 | |
|
| 83 | 82 | | buttonsMap.Add(new BUTTON_MAP() |
| | 83 | | { |
| | 84 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.RightButtonKeyCode, |
| | 85 | | buttonId = WebInterface.ACTION_BUTTON.RIGHT, useRaycast = false, enablePointerEvent = false |
| | 86 | | }); |
| 83 | 87 | | buttonsMap.Add(new BUTTON_MAP() |
| | 88 | | { |
| | 89 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.RightButtonKeyCodeAlt, |
| | 90 | | buttonId = WebInterface.ACTION_BUTTON.RIGHT, useRaycast = false, enablePointerEvent = false |
| | 91 | | }); |
| | 92 | |
|
| 83 | 93 | | buttonsMap.Add(new BUTTON_MAP() |
| | 94 | | { |
| | 95 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.LeftButtonKeyCode, |
| | 96 | | buttonId = WebInterface.ACTION_BUTTON.LEFT, useRaycast = false, enablePointerEvent = false |
| | 97 | | }); |
| 83 | 98 | | buttonsMap.Add(new BUTTON_MAP() |
| | 99 | | { |
| | 100 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.LeftButtonKeyCodeAlt, |
| | 101 | | buttonId = WebInterface.ACTION_BUTTON.LEFT, useRaycast = false, enablePointerEvent = false |
| | 102 | | }); |
| | 103 | |
|
| 83 | 104 | | buttonsMap.Add(new BUTTON_MAP() |
| | 105 | | { |
| | 106 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.WalkButtonKeyCode, |
| | 107 | | buttonId = WebInterface.ACTION_BUTTON.WALK, useRaycast = false, enablePointerEvent = false |
| | 108 | | }); |
| 83 | 109 | | buttonsMap.Add(new BUTTON_MAP() |
| | 110 | | { |
| | 111 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.JumpButtonKeyCode, |
| | 112 | | buttonId = WebInterface.ACTION_BUTTON.JUMP, useRaycast = false, enablePointerEvent = false |
| | 113 | | }); |
| | 114 | |
|
| 83 | 115 | | buttonsMap.Add(new BUTTON_MAP() |
| | 116 | | { |
| | 117 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ActionButton3Keycode, |
| | 118 | | buttonId = WebInterface.ACTION_BUTTON.ACTION_3, useRaycast = true, enablePointerEvent = false |
| | 119 | | }); |
| 83 | 120 | | buttonsMap.Add(new BUTTON_MAP() |
| | 121 | | { |
| | 122 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ActionButton4Keycode, |
| | 123 | | buttonId = WebInterface.ACTION_BUTTON.ACTION_4, useRaycast = true, enablePointerEvent = false |
| | 124 | | }); |
| 83 | 125 | | buttonsMap.Add(new BUTTON_MAP() |
| | 126 | | { |
| | 127 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ActionButton5Keycode, |
| | 128 | | buttonId = WebInterface.ACTION_BUTTON.ACTION_5, useRaycast = true, enablePointerEvent = false |
| | 129 | | }); |
| 83 | 130 | | buttonsMap.Add(new BUTTON_MAP() |
| | 131 | | { |
| | 132 | | type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ActionButton6Keycode, |
| | 133 | | buttonId = WebInterface.ACTION_BUTTON.ACTION_6, useRaycast = true, enablePointerEvent = false |
| | 134 | | }); |
| | 135 | |
|
| 83 | 136 | | Environment.i.platform.updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, Update); |
| 83 | 137 | | } |
| | 138 | |
|
| | 139 | | public void AddListener(WebInterface.ACTION_BUTTON buttonId, ButtonListenerCallback callback) |
| | 140 | | { |
| 1079 | 141 | | if (!listeners.ContainsKey(buttonId)) |
| 1079 | 142 | | listeners.Add(buttonId, new List<ButtonListenerCallback>()); |
| | 143 | |
|
| 1079 | 144 | | if (!listeners[buttonId].Contains(callback)) |
| 1079 | 145 | | listeners[buttonId].Add(callback); |
| 1079 | 146 | | } |
| | 147 | |
|
| | 148 | | public void RemoveListener(WebInterface.ACTION_BUTTON buttonId, ButtonListenerCallback callback) |
| | 149 | | { |
| 1079 | 150 | | if (listeners.ContainsKey(buttonId)) |
| | 151 | | { |
| 1079 | 152 | | if (listeners[buttonId].Contains(callback)) |
| | 153 | | { |
| 1079 | 154 | | listeners[buttonId].Remove(callback); |
| | 155 | |
|
| 1079 | 156 | | if (listeners[buttonId].Count == 0) |
| 1079 | 157 | | listeners.Remove(buttonId); |
| | 158 | | } |
| | 159 | | } |
| 1079 | 160 | | } |
| | 161 | |
|
| | 162 | | // Note (Zak): it is public for testing purposes only |
| | 163 | | public void RaiseEvent(WebInterface.ACTION_BUTTON buttonId, EVENT evt, bool useRaycast, bool enablePointerEvent) |
| | 164 | | { |
| 14 | 165 | | if (!listeners.ContainsKey(buttonId)) |
| 0 | 166 | | return; |
| | 167 | |
|
| 14 | 168 | | List<ButtonListenerCallback> callbacks = listeners[buttonId]; |
| 14 | 169 | | int count = callbacks.Count; |
| | 170 | |
|
| 56 | 171 | | for (int i = 0; i < count; i++) |
| | 172 | | { |
| 14 | 173 | | callbacks[i].Invoke(buttonId, evt, useRaycast, enablePointerEvent); |
| | 174 | | } |
| 14 | 175 | | } |
| | 176 | |
|
| | 177 | | public void Update() |
| | 178 | | { |
| 1766 | 179 | | if (!renderingEnabled) |
| 0 | 180 | | return; |
| | 181 | |
|
| 1766 | 182 | | int count = buttonsMap.Count; |
| | 183 | |
|
| 63576 | 184 | | for (int i = 0; i < count; i++) |
| | 185 | | { |
| 30022 | 186 | | BUTTON_MAP btnMap = buttonsMap[i]; |
| | 187 | |
|
| 30022 | 188 | | switch (btnMap.type) |
| | 189 | | { |
| | 190 | | case BUTTON_TYPE.MOUSE: |
| 1766 | 191 | | if (CommonScriptableObjects.allUIHidden.Get()) |
| | 192 | | break; |
| 1766 | 193 | | if (Input.GetMouseButtonDown(btnMap.buttonNum)) |
| 0 | 194 | | RaiseEvent(btnMap.buttonId, EVENT.BUTTON_DOWN, btnMap.useRaycast, |
| | 195 | | btnMap.enablePointerEvent); |
| 1766 | 196 | | else if (Input.GetMouseButtonUp(btnMap.buttonNum)) |
| 0 | 197 | | RaiseEvent(btnMap.buttonId, EVENT.BUTTON_UP, btnMap.useRaycast, btnMap.enablePointerEvent); |
| 0 | 198 | | break; |
| | 199 | | case BUTTON_TYPE.KEYBOARD: |
| 28256 | 200 | | if (CommonScriptableObjects.allUIHidden.Get()) |
| | 201 | | break; |
| 28256 | 202 | | if (Input.GetKeyDown((KeyCode) btnMap.buttonNum)) |
| 0 | 203 | | RaiseEvent(btnMap.buttonId, EVENT.BUTTON_DOWN, btnMap.useRaycast, |
| | 204 | | btnMap.enablePointerEvent); |
| 28256 | 205 | | else if (Input.GetKeyUp((KeyCode) btnMap.buttonNum)) |
| 0 | 206 | | RaiseEvent(btnMap.buttonId, EVENT.BUTTON_UP, btnMap.useRaycast, btnMap.enablePointerEvent); |
| | 207 | | break; |
| | 208 | | } |
| | 209 | | } |
| 1766 | 210 | | } |
| | 211 | |
|
| | 212 | | public bool IsPressed(WebInterface.ACTION_BUTTON button) |
| | 213 | | { |
| | 214 | | switch (button) |
| | 215 | | { |
| | 216 | | case WebInterface.ACTION_BUTTON.POINTER: |
| 0 | 217 | | return Input.GetMouseButton(0); |
| | 218 | | case WebInterface.ACTION_BUTTON.PRIMARY: |
| 1 | 219 | | return Input.GetKey(InputSettings.PrimaryButtonKeyCode); |
| | 220 | | case WebInterface.ACTION_BUTTON.SECONDARY: |
| 0 | 221 | | return Input.GetKey(InputSettings.SecondaryButtonKeyCode); |
| | 222 | | default: // ANY |
| 1097 | 223 | | return Input.GetMouseButton(0) || |
| | 224 | | Input.GetKey(InputSettings.PrimaryButtonKeyCode) || |
| | 225 | | Input.GetKey(InputSettings.SecondaryButtonKeyCode); |
| | 226 | | } |
| | 227 | | } |
| | 228 | |
|
| | 229 | | public void Dispose() |
| | 230 | | { |
| 83 | 231 | | Environment.i.platform.updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update); |
| 83 | 232 | | } |
| | 233 | | } |
| | 234 | | } |