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