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