63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UIElements;
|
||
|
namespace BITKit.UX
|
||
|
{
|
||
|
public class UXDrag : UXElement<VisualElement>
|
||
|
{
|
||
|
Vector3 m_IconStartPosition;
|
||
|
Vector3 m_PointerStartPosition;
|
||
|
bool isDraging;
|
||
|
public override void OnStart()
|
||
|
{
|
||
|
base.OnStart();
|
||
|
// listen for LMB down
|
||
|
visualElement.RegisterCallback<PointerDownEvent>(PointerDownEventHandler);
|
||
|
|
||
|
// listen for mouse movement and LMB up
|
||
|
visualElement.RegisterCallback<PointerMoveEvent>(PointerMoveEventHandler);
|
||
|
visualElement.RegisterCallback<PointerUpEvent>(PointerUpEventHandler);
|
||
|
}
|
||
|
protected virtual void PointerMoveEventHandler(PointerMoveEvent evt)
|
||
|
{
|
||
|
if (isDraging && visualElement.HasPointerCapture(evt.pointerId))
|
||
|
{
|
||
|
float newX = m_IconStartPosition.x + (evt.position.x - m_PointerStartPosition.x);
|
||
|
float newY = m_IconStartPosition.y + (evt.position.y - m_PointerStartPosition.y);
|
||
|
|
||
|
var delta = new Vector2(newX, newY);
|
||
|
delta = GetProcessDelta(delta);
|
||
|
visualElement.transform.position = delta;
|
||
|
OnDelta(delta);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void PointerDownEventHandler(PointerDownEvent evt)
|
||
|
{
|
||
|
// set the icon and pointer starting positions
|
||
|
m_IconStartPosition = visualElement.transform.position;
|
||
|
m_PointerStartPosition = evt.position;
|
||
|
|
||
|
visualElement.CapturePointer(evt.pointerId);
|
||
|
|
||
|
isDraging = true;
|
||
|
}
|
||
|
|
||
|
protected virtual void PointerUpEventHandler(PointerUpEvent evt)
|
||
|
{
|
||
|
isDraging = false;
|
||
|
|
||
|
visualElement.ReleasePointer(evt.pointerId);
|
||
|
}
|
||
|
|
||
|
protected virtual Vector2 GetProcessDelta(Vector2 delta)
|
||
|
{
|
||
|
return delta;
|
||
|
}
|
||
|
protected virtual void OnDelta(Vector2 delta)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|