47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
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)]
|
|
public float range;
|
|
public bool resetOnUp;
|
|
public bool updateDelta;
|
|
[Header(Constant.Header.Events)]
|
|
public UnityEvent<Vector2> onDelta;
|
|
[Header(Constant.Header.InternalVariables)]
|
|
public Vector2 delta;
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
} |