81 lines
2.4 KiB
C#
81 lines
2.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITKit.UX
|
|
{
|
|
public class OnScreenStick:OnScreenControl
|
|
{
|
|
public new class UxmlTraits : OnScreenControl.UxmlTraits
|
|
{
|
|
private readonly UxmlBoolAttributeDescription m_IsDelteaAttribute = new ()
|
|
{
|
|
name = "IsDelta"
|
|
};
|
|
|
|
private readonly UxmlFloatAttributeDescription m_MoveRangeAttribute = new()
|
|
{
|
|
name = "MoveRange",defaultValue = 32,
|
|
};
|
|
|
|
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
|
{
|
|
base.Init(ve, bag, cc);
|
|
var table = (OnScreenStick)ve;
|
|
table.IsDelta = m_IsDelteaAttribute.GetValueFromBag(bag, cc);
|
|
table.MoveRange=m_MoveRangeAttribute.GetValueFromBag(bag, cc);
|
|
}
|
|
}
|
|
public new class UxmlFactory : UxmlFactory<OnScreenStick, UxmlTraits> { }
|
|
public bool IsDelta { get; set; }
|
|
public float MoveRange { get; set; } = 32;
|
|
protected override string ControlPathInternal { get; set; }
|
|
private int _ignoreFrame=1;
|
|
private Vector2 _startPosition;
|
|
public OnScreenStick()
|
|
{
|
|
RegisterCallback<PointerDownEvent>(OnPointerDown);
|
|
RegisterCallback<PointerMoveEvent>(OnPointerMove);
|
|
RegisterCallback<PointerUpEvent>(OnPointerUp);
|
|
}
|
|
|
|
private void OnPointerUp(PointerUpEvent evt)
|
|
{
|
|
SendValueToControl(Vector2.zero);
|
|
_startPosition = evt.position;
|
|
_ignoreFrame = 1;
|
|
}
|
|
|
|
private void OnPointerDown(PointerDownEvent evt)
|
|
{
|
|
_ignoreFrame = 1;
|
|
_startPosition = evt.position;
|
|
}
|
|
|
|
private void OnPointerMove(PointerMoveEvent evt)
|
|
{
|
|
if (_ignoreFrame-- > 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Vector2.Distance(evt.deltaPosition, default) > Vector2.Distance(_startPosition, evt.position))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var pos = evt.deltaPosition;
|
|
if (IsDelta)
|
|
{
|
|
var newPos = evt.position;
|
|
pos = new Vector2(newPos.x, newPos.y) - _startPosition;
|
|
|
|
pos /= MoveRange;
|
|
}
|
|
SendValueToControl(new Vector2(pos.x, -pos.y));
|
|
}
|
|
}
|
|
|
|
}
|