| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace Builder |
| | 6 | | { |
| | 7 | | public class DCLBuilderObjectDragger : MonoBehaviour |
| | 8 | | { |
| | 9 | | public DCLBuilderRaycast builderRaycast; |
| | 10 | |
|
| | 11 | | public static event Action OnDraggingObjectStart; |
| | 12 | | public static event Action OnDraggingObject; |
| | 13 | | public static event Action OnDraggingObjectEnd; |
| | 14 | |
|
| | 15 | | private List<EditableEntity> selectedEntities; |
| | 16 | | private Vector3 targetOffset; |
| | 17 | | private Vector3 initialHitPoint; |
| | 18 | | private Transform selectedEntitiesParent; |
| | 19 | | private bool isDragging = false; |
| | 20 | |
|
| | 21 | | private float snapFactorPosition = 0; |
| | 22 | |
|
| | 23 | | private bool isGameObjectActive = false; |
| | 24 | |
|
| | 25 | | private void OnEnable() |
| | 26 | | { |
| 1 | 27 | | if (!isGameObjectActive) |
| | 28 | | { |
| 1 | 29 | | DCLBuilderObjectSelector.OnSelectedObjectListChanged += OnSelectedObjectListChanged; |
| 1 | 30 | | DCLBuilderObjectSelector.OnEntityPressed += OnEntityPressed; |
| 1 | 31 | | DCLBuilderInput.OnMouseDrag += OnMouseDrag; |
| 1 | 32 | | DCLBuilderInput.OnMouseUp += OnMouseUp; |
| 1 | 33 | | DCLBuilderBridge.OnSetGridResolution += OnSetGridResolution; |
| | 34 | | } |
| 1 | 35 | | isGameObjectActive = true; |
| 1 | 36 | | } |
| | 37 | |
|
| | 38 | | private void OnDisable() |
| | 39 | | { |
| 1 | 40 | | isGameObjectActive = false; |
| 1 | 41 | | DCLBuilderObjectSelector.OnSelectedObjectListChanged -= OnSelectedObjectListChanged; |
| 1 | 42 | | DCLBuilderObjectSelector.OnEntityPressed -= OnEntityPressed; |
| 1 | 43 | | DCLBuilderInput.OnMouseDrag -= OnMouseDrag; |
| 1 | 44 | | DCLBuilderInput.OnMouseUp -= OnMouseUp; |
| 1 | 45 | | DCLBuilderBridge.OnSetGridResolution -= OnSetGridResolution; |
| 1 | 46 | | } |
| | 47 | |
|
| | 48 | | private void OnSelectedObjectListChanged(Transform selectionParent, List<EditableEntity> selectedEntities) |
| | 49 | | { |
| 0 | 50 | | this.selectedEntitiesParent = selectionParent; |
| 0 | 51 | | this.selectedEntities = selectedEntities; |
| 0 | 52 | | if (isDragging) |
| | 53 | | { |
| 0 | 54 | | targetOffset = selectedEntitiesParent.position - initialHitPoint; |
| | 55 | | } |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | private void OnEntityPressed(DCLBuilderEntity entity, Vector3 hitPoint) |
| | 59 | | { |
| 0 | 60 | | if (selectedEntities == null || !selectedEntities.Contains(entity)) |
| | 61 | | { |
| 0 | 62 | | return; |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | OnDraggingObjectStart?.Invoke(); |
| | 66 | |
|
| 0 | 67 | | initialHitPoint = hitPoint; |
| 0 | 68 | | targetOffset = selectedEntitiesParent.position - hitPoint; |
| 0 | 69 | | builderRaycast.SetEntityHitPlane(hitPoint.y); |
| 0 | 70 | | isDragging = true; |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | private void OnMouseUp(int buttonId, Vector3 mousePosition) |
| | 74 | | { |
| | 75 | | // NOTE: only process mouse's left button press |
| 0 | 76 | | if (buttonId != 0) |
| | 77 | | { |
| 0 | 78 | | return; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | if (isDragging && selectedEntities != null) |
| | 82 | | { |
| 0 | 83 | | OnDraggingObjectEnd?.Invoke(); |
| | 84 | | } |
| 0 | 85 | | isDragging = false; |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | private void OnMouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 89 | | { |
| | 90 | | // NOTE: only process if mouse's left button is pressed and if there is any entity selected |
| 0 | 91 | | if (buttonId != 0 || selectedEntities == null) |
| | 92 | | { |
| 0 | 93 | | return; |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | bool hasMouseMoved = (axisX != 0 || axisY != 0); |
| 0 | 97 | | if (isDragging && hasMouseMoved) |
| | 98 | | { |
| 0 | 99 | | DragTargetEntity(mousePosition); |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | private void DragTargetEntity(Vector3 mousePosition) |
| | 104 | | { |
| 0 | 105 | | Vector3 hitPosition = builderRaycast.RaycastToEntityHitPlane(mousePosition); |
| 0 | 106 | | Vector3 targetPosition = hitPosition + targetOffset; |
| 0 | 107 | | targetPosition.y = selectedEntitiesParent.position.y; |
| | 108 | |
|
| 0 | 109 | | if (snapFactorPosition > 0) |
| | 110 | | { |
| 0 | 111 | | targetPosition.x = targetPosition.x - (targetPosition.x % snapFactorPosition); |
| 0 | 112 | | targetPosition.z = targetPosition.z - (targetPosition.z % snapFactorPosition); |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | Vector3 moveAmount = targetPosition - selectedEntitiesParent.transform.position; |
| 0 | 116 | | selectedEntitiesParent.transform.position = targetPosition; |
| | 117 | |
|
| 0 | 118 | | OnDraggingObject?.Invoke(); |
| 0 | 119 | | } |
| | 120 | |
|
| 0 | 121 | | private void OnSetGridResolution(float position, float rotation, float scale) { snapFactorPosition = position; } |
| | 122 | | } |
| | 123 | | } |