| | 1 | | using Builder; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | |
|
| | 8 | | public class BuilderInWorldInputWrapper : MonoBehaviour |
| | 9 | | { |
| 198 | 10 | | public float msClickThreshold = 200; |
| 198 | 11 | | public float movementClickThreshold = 50; |
| | 12 | |
|
| | 13 | | public static event Action<int, Vector3> OnMouseClick; |
| | 14 | | public static event Action<int, Vector3> OnMouseClickOnUI; |
| | 15 | | public static event Action<int, Vector3> OnMouseDown; |
| | 16 | | public static event Action<int, Vector3> OnMouseUp; |
| | 17 | | public static event Action<int, Vector3> OnMouseUpOnUI; |
| | 18 | |
|
| | 19 | | public static event Action<float> OnMouseWheel; |
| | 20 | |
|
| | 21 | | public static event OnMouseDragDelegate OnMouseDrag; |
| | 22 | | public static event OnMouseDragDelegateRaw OnMouseDragRaw; |
| | 23 | |
|
| | 24 | | public delegate void OnMouseDragDelegate(int buttonId, Vector3 position, float axisX, float axisY); |
| | 25 | | public delegate void OnMouseDragDelegateRaw(int buttonId, Vector3 position, float axisX, float axisY); |
| | 26 | |
|
| | 27 | | private float lastTimeMouseDown = 0; |
| | 28 | | private Vector3 lastMousePosition; |
| 198 | 29 | | private bool canInputBeMade = true; |
| | 30 | | private bool currentClickIsOnUi = false; |
| | 31 | |
|
| | 32 | | private void Awake() |
| | 33 | | { |
| 0 | 34 | | DCLBuilderInput.OnMouseDrag += MouseDrag; |
| 0 | 35 | | DCLBuilderInput.OnMouseRawDrag += MouseRawDrag; |
| 0 | 36 | | DCLBuilderInput.OnMouseWheel += MouseWheel; |
| 0 | 37 | | DCLBuilderInput.OnMouseDown += MouseDown; |
| 0 | 38 | | DCLBuilderInput.OnMouseUp += MouseUp; |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | private void OnDestroy() |
| | 42 | | { |
| 0 | 43 | | DCLBuilderInput.OnMouseDrag -= MouseDrag; |
| 0 | 44 | | DCLBuilderInput.OnMouseRawDrag -= MouseRawDrag; |
| 0 | 45 | | DCLBuilderInput.OnMouseWheel -= MouseWheel; |
| 0 | 46 | | DCLBuilderInput.OnMouseDown -= MouseDown; |
| 0 | 47 | | DCLBuilderInput.OnMouseUp -= MouseUp; |
| 0 | 48 | | } |
| | 49 | |
|
| 0 | 50 | | public void StopInput() { canInputBeMade = false; } |
| | 51 | |
|
| 0 | 52 | | public void ResumeInput() { canInputBeMade = true; } |
| | 53 | |
|
| | 54 | | private void MouseUp(int buttonId, Vector3 mousePosition) |
| | 55 | | { |
| 0 | 56 | | if (currentClickIsOnUi) |
| | 57 | | { |
| 0 | 58 | | OnMouseClickOnUI?.Invoke(buttonId, mousePosition); |
| 0 | 59 | | currentClickIsOnUi = false; |
| 0 | 60 | | return; |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | if (!canInputBeMade) |
| 0 | 64 | | return; |
| | 65 | |
|
| 0 | 66 | | if (!BuilderInWorldUtils.IsPointerOverUIElement()) |
| | 67 | | { |
| 0 | 68 | | OnMouseUp?.Invoke(buttonId, mousePosition); |
| 0 | 69 | | if (Vector3.Distance(mousePosition, lastMousePosition) >= movementClickThreshold) |
| 0 | 70 | | return; |
| 0 | 71 | | if (Time.unscaledTime >= lastTimeMouseDown + msClickThreshold / 1000) |
| 0 | 72 | | return; |
| 0 | 73 | | OnMouseClick?.Invoke(buttonId, mousePosition); |
| 0 | 74 | | } |
| | 75 | | else |
| 0 | 76 | | OnMouseUpOnUI?.Invoke(buttonId, mousePosition); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | private void MouseDown(int buttonId, Vector3 mousePosition) |
| | 80 | | { |
| 0 | 81 | | lastTimeMouseDown = Time.unscaledTime; |
| 0 | 82 | | lastMousePosition = mousePosition; |
| 0 | 83 | | currentClickIsOnUi = BuilderInWorldUtils.IsPointerOverUIElement(); |
| | 84 | |
|
| 0 | 85 | | if (!canInputBeMade) |
| 0 | 86 | | return; |
| 0 | 87 | | if (!currentClickIsOnUi) |
| 0 | 88 | | OnMouseDown?.Invoke(buttonId, mousePosition); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void MouseWheel(float axisValue) |
| | 92 | | { |
| 0 | 93 | | if (!canInputBeMade) |
| 0 | 94 | | return; |
| 0 | 95 | | if (!BuilderInWorldUtils.IsPointerOverUIElement()) |
| 0 | 96 | | OnMouseWheel?.Invoke(axisValue); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | private void MouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 100 | | { |
| 0 | 101 | | if (!CanDrag()) |
| 0 | 102 | | return; |
| | 103 | |
|
| 0 | 104 | | OnMouseDrag?.Invoke(buttonId, mousePosition, axisX, axisY); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | private void MouseRawDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 108 | | { |
| 0 | 109 | | if (!CanDrag()) |
| 0 | 110 | | return; |
| | 111 | |
|
| 0 | 112 | | OnMouseDragRaw?.Invoke(buttonId, mousePosition, axisX, axisY); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | private bool CanDrag() |
| | 116 | | { |
| 0 | 117 | | if (!canInputBeMade || |
| | 118 | | currentClickIsOnUi || |
| | 119 | | BuilderInWorldUtils.IsPointerOverUIElement()) |
| 0 | 120 | | return false; |
| | 121 | |
|
| 0 | 122 | | return true; |
| | 123 | | } |
| | 124 | | } |