77 lines
2.6 KiB
C#
77 lines
2.6 KiB
C#
|
#if PACKAGE_DOCS_GENERATION || UNITY_INPUT_SYSTEM_ENABLE_UI
|
||
|
using UnityEngine.EventSystems;
|
||
|
using UnityEngine.Serialization;
|
||
|
using UnityEngine.InputSystem.Layouts;
|
||
|
|
||
|
////TODO: custom icon for OnScreenStick component
|
||
|
|
||
|
namespace UnityEngine.InputSystem.OnScreen
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// A stick control displayed on screen and moved around by touch or other pointer
|
||
|
/// input.
|
||
|
/// </summary>
|
||
|
[AddComponentMenu("Input/On-Screen Stick")]
|
||
|
[HelpURL(InputSystem.kDocUrl + "/manual/OnScreen.html#on-screen-sticks")]
|
||
|
public class OnScreenStick : OnScreenControl, IPointerDownHandler, IPointerUpHandler, IDragHandler
|
||
|
{
|
||
|
public void OnPointerDown(PointerEventData eventData)
|
||
|
{
|
||
|
if (eventData == null)
|
||
|
throw new System.ArgumentNullException(nameof(eventData));
|
||
|
|
||
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponentInParent<RectTransform>(), eventData.position, eventData.pressEventCamera, out m_PointerDownPos);
|
||
|
}
|
||
|
|
||
|
public void OnDrag(PointerEventData eventData)
|
||
|
{
|
||
|
if (eventData == null)
|
||
|
throw new System.ArgumentNullException(nameof(eventData));
|
||
|
|
||
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponentInParent<RectTransform>(), eventData.position, eventData.pressEventCamera, out var position);
|
||
|
var delta = position - m_PointerDownPos;
|
||
|
|
||
|
delta = Vector2.ClampMagnitude(delta, movementRange);
|
||
|
((RectTransform)transform).anchoredPosition = m_StartPos + (Vector3)delta;
|
||
|
|
||
|
var newPos = new Vector2(delta.x / movementRange, delta.y / movementRange);
|
||
|
SendValueToControl(newPos);
|
||
|
}
|
||
|
|
||
|
public void OnPointerUp(PointerEventData eventData)
|
||
|
{
|
||
|
((RectTransform)transform).anchoredPosition = m_StartPos;
|
||
|
SendValueToControl(Vector2.zero);
|
||
|
}
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
m_StartPos = ((RectTransform)transform).anchoredPosition;
|
||
|
}
|
||
|
|
||
|
public float movementRange
|
||
|
{
|
||
|
get => m_MovementRange;
|
||
|
set => m_MovementRange = value;
|
||
|
}
|
||
|
|
||
|
[FormerlySerializedAs("movementRange")]
|
||
|
[SerializeField]
|
||
|
private float m_MovementRange = 50;
|
||
|
|
||
|
[InputControl(layout = "Vector2")]
|
||
|
[SerializeField]
|
||
|
private string m_ControlPath;
|
||
|
|
||
|
private Vector3 m_StartPos;
|
||
|
private Vector2 m_PointerDownPos;
|
||
|
|
||
|
protected override string controlPathInternal
|
||
|
{
|
||
|
get => m_ControlPath;
|
||
|
set => m_ControlPath = value;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#endif
|