using CHN; using System; using UnityEngine; using UnityEngine.EventSystems; public class UIDraggerEventArg : EventArgs { private Vector3 pos; public Vector3 Pos { get => pos; } public UIDraggerEventArg(Vector3 pos) { this.pos = pos; } } public class Dragger : UnityEngine.MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public static string PathPrefab = "UI/UIDragger"; public RectTransform rect; [Header("Resources")] private RectTransform dragArea; private RectTransform dragObject; [Header("Settings")] private bool topOnDrag = true; public bool dashBoardDrag; private Vector2 originalLocalPointerPosition; private Vector3 originalPanelLocalPosition; public EventHandler OnCommand { get; set; } public Action onBeginDrag; private float yMinHeight; public void SetDrag(RectTransform parent) { if (dragArea == null) { try { var canvas = transform.GetComponentInParent(); dragArea = canvas.GetComponent(); } catch { Debug.LogWarning("[Movable Window] Drag Area has not been assigned."); } } rect = GetComponent(); var toptoolbar = FindObjectOfType(); var topbarrect = toptoolbar.GetComponent(); yMinHeight = topbarrect.sizeDelta.y; baseSibling = DragObjectInternal.transform.GetSiblingIndex(); dragObject = parent; } void OnDestroy() { if (OnCommand != null) OnCommand = null; } private RectTransform DragObjectInternal { get { if (dragObject == null) { return (transform.parent as RectTransform); } else { return dragObject; } } } private RectTransform DragAreaInternal { get { if (dragArea == null) { RectTransform canvas = transform as RectTransform; while (canvas.parent != null && canvas.parent is RectTransform) { canvas = canvas.parent as RectTransform; } return canvas; } else { return dragArea; } } } private int baseSibling; public void OnBeginDrag(PointerEventData data) { if (!Input.GetMouseButton(0)) return; originalPanelLocalPosition = DragObjectInternal.localPosition; RectTransformUtility.ScreenPointToLocalPointInRectangle(DragAreaInternal, data.position, data.pressEventCamera, out originalLocalPointerPosition); if (topOnDrag == true) { DragObjectInternal.transform.SetAsLastSibling(); } } public void OnDrag(PointerEventData data) { if (!Input.GetMouseButton(0)) return; Vector2 localPointerPosition; if (RectTransformUtility.ScreenPointToLocalPointInRectangle(DragAreaInternal, data.position, data.pressEventCamera, out localPointerPosition)) { Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition; DragObjectInternal.localPosition = originalPanelLocalPosition + offsetToOriginal; } ClampToArea(); } private void ClampToArea() { Vector3 pos = DragObjectInternal.localPosition; Vector3 minPosition = DragAreaInternal.rect.min - DragObjectInternal.rect.min; Vector3 maxPosition = DragAreaInternal.rect.max - DragObjectInternal.rect.max; pos.x = Mathf.Clamp(DragObjectInternal.localPosition.x, minPosition.x, maxPosition.x); pos.y = Mathf.Clamp(DragObjectInternal.localPosition.y, minPosition.y, maxPosition.y - yMinHeight); DragObjectInternal.localPosition = pos; } public void OnEndDrag(PointerEventData eventData) { if (!dashBoardDrag) DragObjectInternal.transform.SetSiblingIndex(baseSibling); if (OnCommand != null) OnCommand(this, new UIDraggerEventArg(DragObjectInternal.anchoredPosition)); } }