| | 1 | | using UnityEngine; |
| | 2 | |
|
| | 3 | | namespace Builder |
| | 4 | | { |
| | 5 | | public class DCLBuilderInput : MonoBehaviour |
| | 6 | | { |
| | 7 | | const string MouseXAxis = "Mouse X"; |
| | 8 | | const string MouseYAxis = "Mouse Y"; |
| | 9 | |
|
| 199 | 10 | | [SerializeField] float mouseWheelThrottle = 0.1f; |
| | 11 | |
|
| | 12 | | public delegate void MouseClickDelegate(int buttonId, Vector3 mousePosition); |
| | 13 | |
|
| | 14 | | public delegate void MouseDragDelegate(int buttonId, Vector3 mousePosition, float axisX, float axisY); |
| | 15 | |
|
| | 16 | | public delegate void MouseRawDragDelegate(int buttonId, Vector3 mousePosition, float axisX, float axisY); |
| | 17 | |
|
| | 18 | | public delegate void MouseWheelDelegate(float axisValue); |
| | 19 | |
|
| | 20 | | public static event MouseClickDelegate OnMouseDown; |
| | 21 | | public static event MouseClickDelegate OnMouseUp; |
| | 22 | | public static event MouseDragDelegate OnMouseDrag; |
| | 23 | | public static event MouseRawDragDelegate OnMouseRawDrag; |
| | 24 | | public static event MouseWheelDelegate OnMouseWheel; |
| | 25 | |
|
| | 26 | | private int lastMouseWheelAxisDirection = 0; |
| | 27 | | private float lastMouseWheelTime = 0; |
| | 28 | |
|
| | 29 | | private void Update() |
| | 30 | | { |
| 88 | 31 | | for (int i = 0; i <= 2; i++) |
| | 32 | | { |
| 33 | 33 | | if (HasMouseButtonInput(i)) |
| | 34 | | break; |
| | 35 | | } |
| | 36 | |
|
| 11 | 37 | | UpdateMouseWheelInput(); |
| | 38 | |
|
| | 39 | | #if UNITY_EDITOR |
| 11 | 40 | | EditorKeyDownEvent(); |
| | 41 | | #endif |
| 11 | 42 | | } |
| | 43 | |
|
| | 44 | | private bool HasMouseButtonInput(int button) |
| | 45 | | { |
| 33 | 46 | | if (Input.GetMouseButtonDown(button)) |
| | 47 | | { |
| 0 | 48 | | OnMouseDown?.Invoke(button, Input.mousePosition); |
| 0 | 49 | | return true; |
| | 50 | | } |
| 33 | 51 | | else if (Input.GetMouseButton(button)) |
| | 52 | | { |
| 0 | 53 | | OnMouseDrag?.Invoke(button, Input.mousePosition, Input.GetAxis(MouseXAxis), Input.GetAxis(MouseYAxis)); |
| 0 | 54 | | OnMouseRawDrag?.Invoke(button, Input.mousePosition, Input.GetAxisRaw(MouseXAxis), Input.GetAxisRaw(Mouse |
| 0 | 55 | | return true; |
| | 56 | | } |
| 33 | 57 | | else if (Input.GetMouseButtonUp(button)) |
| | 58 | | { |
| 0 | 59 | | OnMouseUp?.Invoke(button, Input.mousePosition); |
| 0 | 60 | | return true; |
| | 61 | | } |
| | 62 | |
|
| 33 | 63 | | return false; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | private void OnMouseWheelInput(float axisValue) |
| | 67 | | { |
| 0 | 68 | | int axisDirection = (int)Mathf.Sign(axisValue); |
| 0 | 69 | | if (lastMouseWheelAxisDirection == axisDirection) |
| | 70 | | { |
| 0 | 71 | | if (Time.unscaledTime - lastMouseWheelTime >= mouseWheelThrottle) |
| | 72 | | { |
| 0 | 73 | | SetMouseWheelDelta(axisValue, axisDirection); |
| | 74 | | } |
| 0 | 75 | | } |
| | 76 | | else |
| | 77 | | { |
| 0 | 78 | | SetMouseWheelDelta(axisValue, axisDirection); |
| | 79 | | } |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | private void SetMouseWheelDelta(float axisValue, int axisDirection) |
| | 83 | | { |
| 0 | 84 | | OnMouseWheel?.Invoke(axisValue); |
| 0 | 85 | | lastMouseWheelTime = Time.unscaledTime; |
| 0 | 86 | | lastMouseWheelAxisDirection = axisDirection; |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private void UpdateMouseWheelInput() |
| | 90 | | { |
| 11 | 91 | | float axisValue = Input.GetAxis("Mouse ScrollWheel"); |
| 11 | 92 | | if (axisValue != 0) |
| | 93 | | { |
| 0 | 94 | | OnMouseWheelInput(axisValue); |
| | 95 | | } |
| 11 | 96 | | } |
| | 97 | |
|
| | 98 | | #if UNITY_EDITOR |
| | 99 | | GameObject bridgeGameObject; |
| | 100 | |
|
| | 101 | | private void EditorKeyDownEvent() |
| | 102 | | { |
| 11 | 103 | | if (Input.GetKey(KeyCode.UpArrow)) |
| | 104 | | { |
| 0 | 105 | | SendMessageToBridge("OnBuilderKeyDown", "UpArrow"); |
| | 106 | | } |
| | 107 | |
|
| 11 | 108 | | if (Input.GetKey(KeyCode.DownArrow)) |
| | 109 | | { |
| 0 | 110 | | SendMessageToBridge("OnBuilderKeyDown", "DownArrow"); |
| | 111 | | } |
| | 112 | |
|
| 11 | 113 | | if (Input.GetKey(KeyCode.LeftArrow)) |
| | 114 | | { |
| 0 | 115 | | SendMessageToBridge("OnBuilderKeyDown", "LeftArrow"); |
| | 116 | | } |
| | 117 | |
|
| 11 | 118 | | if (Input.GetKey(KeyCode.RightArrow)) |
| | 119 | | { |
| 0 | 120 | | SendMessageToBridge("OnBuilderKeyDown", "RightArrow"); |
| | 121 | | } |
| | 122 | |
|
| 11 | 123 | | if (Input.GetKey(KeyCode.LeftShift)) |
| | 124 | | { |
| 0 | 125 | | SendMessageToBridge("OnBuilderKeyDown", "LeftShift"); |
| | 126 | | } |
| | 127 | |
|
| | 128 | |
|
| 11 | 129 | | if (Input.GetKeyDown(KeyCode.W)) |
| | 130 | | { |
| 0 | 131 | | SendMessageToBridge("SelectGizmo", DCL.Components.DCLGizmos.Gizmo.MOVE); |
| | 132 | | } |
| | 133 | |
|
| 11 | 134 | | if (Input.GetKeyDown(KeyCode.R)) |
| | 135 | | { |
| 0 | 136 | | SendMessageToBridge("SelectGizmo", DCL.Components.DCLGizmos.Gizmo.ROTATE); |
| | 137 | | } |
| | 138 | |
|
| 11 | 139 | | if (Input.GetKeyDown(KeyCode.S)) |
| | 140 | | { |
| 0 | 141 | | SendMessageToBridge("SelectGizmo", DCL.Components.DCLGizmos.Gizmo.SCALE); |
| | 142 | | } |
| 11 | 143 | | } |
| | 144 | |
|
| | 145 | | private void SendMessageToBridge(string method, string arg) |
| | 146 | | { |
| 0 | 147 | | if (bridgeGameObject == null) |
| | 148 | | { |
| 0 | 149 | | bridgeGameObject = GameObject.Find("BuilderController"); |
| | 150 | | } |
| | 151 | |
|
| 0 | 152 | | if (bridgeGameObject != null) |
| | 153 | | { |
| 0 | 154 | | bridgeGameObject.SendMessage(method, arg); |
| | 155 | | } |
| 0 | 156 | | } |
| | 157 | | #endif |
| | 158 | | } |
| | 159 | | } |