99 lines
3.0 KiB
C#
99 lines
3.0 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using WI.UI;
|
|
|
|
namespace XED.UI
|
|
{
|
|
public class Panel_DragHandler : PanelBase
|
|
{
|
|
Image selectionBoxImage;
|
|
Vector2 startPosition;
|
|
Rect selectionRect;
|
|
public event System.Action<Rect> onDragBoxSelect;
|
|
private void Awake()
|
|
{
|
|
foreach (Image img in GetComponentsInChildren<Image>(true))
|
|
{
|
|
if (img.transform != transform)
|
|
{
|
|
selectionBoxImage = img;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
selectionBoxImage.gameObject.SetActive(true);
|
|
startPosition = eventData.position;
|
|
selectionRect = new Rect();
|
|
}
|
|
public void OnBeginDrag(Vector3 position)
|
|
{
|
|
selectionBoxImage.gameObject.SetActive(true);
|
|
startPosition = position;
|
|
selectionRect = new Rect();
|
|
StopAllCoroutines();
|
|
StartCoroutine(CoroutineOnDrag());
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
SetSelectionBoxSize(eventData.position);
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
selectionBoxImage.gameObject.SetActive(false);
|
|
onDragBoxSelect?.Invoke(selectionRect);
|
|
}
|
|
public void OnEndDrag(Vector3 position)
|
|
{
|
|
if (!selectionBoxImage.gameObject.activeSelf) return;
|
|
selectionBoxImage.gameObject.SetActive(false);
|
|
StopAllCoroutines();
|
|
SetSelectionBoxSize(position);
|
|
onDragBoxSelect?.Invoke(selectionRect);
|
|
}
|
|
public void ForceEndDrag()
|
|
{
|
|
selectionBoxImage.gameObject.SetActive(false);
|
|
StopAllCoroutines();
|
|
}
|
|
public void SetSelectionBoxSize(Vector3 position)
|
|
{
|
|
if (position.x < startPosition.x)
|
|
{
|
|
selectionRect.xMin = position.x;
|
|
selectionRect.xMax = startPosition.x;
|
|
}
|
|
else
|
|
{
|
|
selectionRect.xMin = startPosition.x;
|
|
selectionRect.xMax = position.x;
|
|
}
|
|
if (position.y < startPosition.y)
|
|
{
|
|
selectionRect.yMin = position.y;
|
|
selectionRect.yMax = startPosition.y;
|
|
}
|
|
else
|
|
{
|
|
selectionRect.yMin = startPosition.y;
|
|
selectionRect.yMax = position.y;
|
|
}
|
|
selectionBoxImage.rectTransform.offsetMin = selectionRect.min;
|
|
selectionBoxImage.rectTransform.offsetMax = selectionRect.max;
|
|
}
|
|
IEnumerator CoroutineOnDrag()
|
|
{
|
|
while (true)
|
|
{
|
|
SetSelectionBoxSize(Input.mousePosition);
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|
|
}
|