| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | |
|
| | 8 | | public interface IBIWInputWrapper : IBIWController |
| | 9 | | { |
| | 10 | | public void StopInput(); |
| | 11 | |
|
| | 12 | | public void ResumeInput(); |
| | 13 | | } |
| | 14 | |
|
| | 15 | | public class BIWInputWrapper : BIWController, IBIWInputWrapper |
| | 16 | | { |
| | 17 | | private const float MS_CLICK_THRESHOLD = 500; |
| | 18 | | private const float MOVEMENT_CLICK_THRESHOLD = 50; |
| | 19 | | private const float MOUSE_WHEEL_THROTTLE = 0.1f; |
| | 20 | |
|
| | 21 | | private const string MOUSE_X_AXIS = "Mouse X"; |
| | 22 | | private const string MOUSE_Y_AXIS = "Mouse Y"; |
| | 23 | | private const string MOUSE_SCROLLWHEEL = "Mouse ScrollWheel"; |
| | 24 | |
|
| | 25 | | public static event Action<int, Vector3> OnMouseClick; |
| | 26 | | public static event Action<int, Vector3> OnMouseClickOnUI; |
| | 27 | | public static event Action<int, Vector3> OnMouseDown; |
| | 28 | | public static event Action<int, Vector3> OnMouseUp; |
| | 29 | | public static event Action<int, Vector3> OnMouseUpOnUI; |
| | 30 | |
|
| | 31 | | public static event Action<float> OnMouseWheel; |
| | 32 | |
|
| | 33 | | public static event OnMouseDragDelegate OnMouseDrag; |
| | 34 | | public static event OnMouseDragDelegateRaw OnMouseDragRaw; |
| | 35 | |
|
| | 36 | | public delegate void OnMouseDragDelegate(int buttonId, Vector3 position, float axisX, float axisY); |
| | 37 | |
|
| | 38 | | public delegate void OnMouseDragDelegateRaw(int buttonId, Vector3 position, float axisX, float axisY); |
| | 39 | |
|
| | 40 | | internal float lastTimeMouseDown = 0; |
| | 41 | | internal Vector3 lastMousePosition; |
| 0 | 42 | | internal bool canInputBeMade = true; |
| | 43 | | internal bool currentClickIsOnUi = false; |
| | 44 | | internal int lastMouseWheelAxisDirection = 0; |
| | 45 | | internal float lastMouseWheelTime = 0; |
| | 46 | |
|
| | 47 | | public override void Update() |
| | 48 | | { |
| 2 | 49 | | base.Update(); |
| 16 | 50 | | for (int i = 0; i <= 2; i++) |
| | 51 | | { |
| 6 | 52 | | if (HasMouseButtonInput(i)) |
| | 53 | | break; |
| | 54 | | } |
| | 55 | |
|
| 2 | 56 | | UpdateMouseWheelInput(); |
| 2 | 57 | | } |
| | 58 | |
|
| | 59 | | public override void Dispose() |
| | 60 | | { |
| 43 | 61 | | base.Dispose(); |
| | 62 | |
|
| 43 | 63 | | currentClickIsOnUi = false; |
| 43 | 64 | | } |
| | 65 | |
|
| | 66 | | internal bool HasMouseButtonInput(int button) |
| | 67 | | { |
| 6 | 68 | | if (Input.GetMouseButtonDown(button)) |
| | 69 | | { |
| 0 | 70 | | MouseDown(button, Input.mousePosition); |
| 0 | 71 | | return true; |
| | 72 | | } |
| | 73 | |
|
| 6 | 74 | | if (Input.GetMouseButton(button)) |
| | 75 | | { |
| 0 | 76 | | MouseDrag(button, Input.mousePosition, Input.GetAxis(MOUSE_X_AXIS), Input.GetAxis(MOUSE_Y_AXIS)); |
| 0 | 77 | | MouseRawDrag(button, Input.mousePosition, Input.GetAxis(MOUSE_X_AXIS), Input.GetAxis(MOUSE_Y_AXIS)); |
| 0 | 78 | | return true; |
| | 79 | | } |
| | 80 | |
|
| 6 | 81 | | if (Input.GetMouseButtonUp(button)) |
| | 82 | | { |
| 0 | 83 | | MouseUp(button, Input.mousePosition); |
| 0 | 84 | | return true; |
| | 85 | | } |
| | 86 | |
|
| 6 | 87 | | return false; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | internal void OnMouseWheelInput(float axisValue) |
| | 91 | | { |
| 1 | 92 | | int axisDirection = (int)Mathf.Sign(axisValue); |
| 1 | 93 | | if (lastMouseWheelAxisDirection == axisDirection) |
| | 94 | | { |
| 0 | 95 | | if (Time.unscaledTime - lastMouseWheelTime >= MOUSE_WHEEL_THROTTLE) |
| 0 | 96 | | SetMouseWheelDelta(axisValue, axisDirection); |
| 0 | 97 | | } |
| | 98 | | else |
| | 99 | | { |
| 1 | 100 | | SetMouseWheelDelta(axisValue, axisDirection); |
| | 101 | | } |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | internal void SetMouseWheelDelta(float axisValue, int axisDirection) |
| | 105 | | { |
| 1 | 106 | | MouseWheel(axisValue); |
| 1 | 107 | | lastMouseWheelTime = Time.unscaledTime; |
| 1 | 108 | | lastMouseWheelAxisDirection = axisDirection; |
| 1 | 109 | | } |
| | 110 | |
|
| | 111 | | internal void UpdateMouseWheelInput() |
| | 112 | | { |
| 2 | 113 | | float axisValue = Input.GetAxis(MOUSE_SCROLLWHEEL); |
| 2 | 114 | | if (axisValue != 0) |
| 0 | 115 | | OnMouseWheelInput(axisValue); |
| 2 | 116 | | } |
| | 117 | |
|
| 0 | 118 | | public void StopInput() { canInputBeMade = false; } |
| | 119 | |
|
| 0 | 120 | | public void ResumeInput() { canInputBeMade = true; } |
| | 121 | |
|
| | 122 | | internal void MouseUp(int buttonId, Vector3 mousePosition) |
| | 123 | | { |
| 1 | 124 | | if (!isEditModeActive) |
| 1 | 125 | | return; |
| | 126 | |
|
| 0 | 127 | | if (currentClickIsOnUi) |
| | 128 | | { |
| 0 | 129 | | OnMouseClickOnUI?.Invoke(buttonId, mousePosition); |
| 0 | 130 | | currentClickIsOnUi = false; |
| 0 | 131 | | return; |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | if (!canInputBeMade) |
| 0 | 135 | | return; |
| | 136 | |
|
| 0 | 137 | | if (!BIWUtils.IsPointerOverUIElement()) |
| 0 | 138 | | MouseUpInvoke(buttonId, mousePosition); |
| | 139 | | else |
| 0 | 140 | | MouseUpOnGUI(buttonId, mousePosition); |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | internal void MouseUpInvoke(int buttonId, Vector3 mousePosition) |
| | 144 | | { |
| 1 | 145 | | OnMouseUp?.Invoke(buttonId, mousePosition); |
| 1 | 146 | | if (Vector3.Distance(mousePosition, lastMousePosition) >= MOVEMENT_CLICK_THRESHOLD) |
| 0 | 147 | | return; |
| 1 | 148 | | if (Time.unscaledTime >= lastTimeMouseDown + MS_CLICK_THRESHOLD / 1000) |
| 1 | 149 | | return; |
| 0 | 150 | | OnMouseClick?.Invoke(buttonId, mousePosition); |
| 0 | 151 | | } |
| | 152 | |
|
| 2 | 153 | | internal void MouseUpOnGUI(int buttonId, Vector3 mousePosition) { OnMouseUpOnUI?.Invoke(buttonId, mousePosition); } |
| | 154 | |
|
| | 155 | | internal void MouseDown(int buttonId, Vector3 mousePosition) |
| | 156 | | { |
| 1 | 157 | | if (!isEditModeActive) |
| 1 | 158 | | return; |
| | 159 | |
|
| 0 | 160 | | lastTimeMouseDown = Time.unscaledTime; |
| 0 | 161 | | lastMousePosition = mousePosition; |
| 0 | 162 | | currentClickIsOnUi = BIWUtils.IsPointerOverUIElement(); |
| | 163 | |
|
| 0 | 164 | | if (!canInputBeMade) |
| 0 | 165 | | return; |
| 0 | 166 | | if (!currentClickIsOnUi) |
| 0 | 167 | | OnMouseDown?.Invoke(buttonId, mousePosition); |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | internal void MouseWheel(float axisValue) |
| | 171 | | { |
| 1 | 172 | | if (!isEditModeActive) |
| 1 | 173 | | return; |
| | 174 | |
|
| 0 | 175 | | if (!canInputBeMade) |
| 0 | 176 | | return; |
| 0 | 177 | | if (!BIWUtils.IsPointerOverUIElement()) |
| 0 | 178 | | OnMouseWheel?.Invoke(axisValue); |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | internal void MouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 182 | | { |
| 1 | 183 | | if (!isEditModeActive) |
| 1 | 184 | | return; |
| | 185 | |
|
| 0 | 186 | | if (!CanDrag()) |
| 0 | 187 | | return; |
| | 188 | |
|
| 0 | 189 | | OnMouseDrag?.Invoke(buttonId, mousePosition, axisX, axisY); |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | internal void MouseRawDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 193 | | { |
| 1 | 194 | | if (!isEditModeActive) |
| 1 | 195 | | return; |
| | 196 | |
|
| 0 | 197 | | if (!CanDrag()) |
| 0 | 198 | | return; |
| | 199 | |
|
| 0 | 200 | | OnMouseDragRaw?.Invoke(buttonId, mousePosition, axisX, axisY); |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | private bool CanDrag() |
| | 204 | | { |
| 0 | 205 | | if (!canInputBeMade || |
| | 206 | | currentClickIsOnUi || |
| | 207 | | BIWUtils.IsPointerOverUIElement()) |
| 0 | 208 | | return false; |
| | 209 | |
|
| 0 | 210 | | return true; |
| | 211 | | } |
| | 212 | | } |