2023-06-05 19:57:17 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
namespace BITKit.UX
|
|
|
|
{
|
|
|
|
public class UXScreenJoyStick : UXDrag
|
|
|
|
{
|
|
|
|
[Header(Constant.Header.Settings)]
|
2024-05-31 01:23:15 +08:00
|
|
|
[SerializeField] private float range;
|
|
|
|
[SerializeField] private bool resetOnUp;
|
|
|
|
[SerializeField] private bool updateDelta;
|
|
|
|
[SerializeField] private bool raw;
|
2023-06-05 19:57:17 +08:00
|
|
|
[Header(Constant.Header.Events)]
|
2024-05-31 01:23:15 +08:00
|
|
|
[SerializeField] private UnityEvent<Vector2> onDelta;
|
2023-06-05 19:57:17 +08:00
|
|
|
[Header(Constant.Header.InternalVariables)]
|
2024-05-31 01:23:15 +08:00
|
|
|
[SerializeField,ReadOnly] private Vector2 delta;
|
|
|
|
|
|
|
|
public override void OnStart()
|
|
|
|
{
|
|
|
|
base.OnStart();
|
|
|
|
visualElement.RegisterCallback<PointerMoveEvent>(OnPointerMove);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnPointerMove(PointerMoveEvent evt)
|
|
|
|
{
|
|
|
|
if (raw&& isDraging)
|
|
|
|
{
|
|
|
|
onDelta.Invoke(evt.deltaPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
protected override Vector2 GetProcessDelta(Vector2 delta)
|
|
|
|
{
|
|
|
|
delta = Vector2.ClampMagnitude(delta, range);
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
protected override void PointerUpEventHandler(PointerUpEvent evt)
|
|
|
|
{
|
|
|
|
base.PointerUpEventHandler(evt);
|
|
|
|
if (resetOnUp)
|
|
|
|
{
|
|
|
|
visualElement.transform.position = default;
|
|
|
|
onDelta.Invoke(this.delta = default);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protected override void OnDelta(Vector2 delta)
|
|
|
|
{
|
|
|
|
delta.y = -delta.y;
|
|
|
|
if (updateDelta is false)
|
|
|
|
onDelta.Invoke(delta);
|
|
|
|
this.delta = delta;
|
|
|
|
}
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
if (updateDelta && delta.sqrMagnitude is not 0)
|
|
|
|
{
|
|
|
|
onDelta.Invoke(delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|