Skip to content

Commit

Permalink
Implement input down tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
FejZa committed Nov 9, 2023
1 parent 2a4f08d commit 1f712c1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Runtime/Input/Interactors/BaseControllerInteractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEvents = UnityEngine.EventSystems;

namespace RealityToolkit.Input.Interactors
{
Expand Down Expand Up @@ -76,6 +77,7 @@ public abstract class BaseControllerInteractor : ControllerPoseSynchronizer, ICo
private GameObject cursorInstance = null;
private Vector3 lastPointerPosition = Vector3.zero;
private readonly List<InputAction> inputDownActions = new List<InputAction>();
private readonly Dictionary<uint, List<GameObject>> inputDownTrackingDictionary = new Dictionary<uint, List<GameObject>>();

/// <inheritdoc/>
public bool IsOverUI => Result != null &&
Expand Down Expand Up @@ -621,6 +623,7 @@ protected virtual void OnRaisePointerClicked(InputAction inputAction)
/// <param name="inputAction">The <see cref="InputAction"/> about to be raised.</param>
protected virtual void OnRaisePointerUp(InputAction inputAction)
{
ResolveInputDown(inputAction);
InputService.RaisePointerUp(this, inputAction);
}

Expand All @@ -634,10 +637,60 @@ protected virtual void OnRaisePointerDown(InputAction inputAction)
{
if (Result.CurrentTarget.IsNotNull() && IsInteractionEnabled)
{
TrackInputDown(inputAction, Result.CurrentTarget);
InputService.RaisePointerDown(this, inputAction);
}
}

/// <summary>
/// Records the input down on the <paramref name="target"/> with <paramref name="inputAction"/>,
/// so that we can ensure input up is raised on the <paramref name="target"/> even without focus.
/// </summary>
/// <param name="inputAction">The <see cref="InputAction"/>.</param>
/// <param name="target">The input down target <see cref="UnityEngine.GameObject"/>.</param>
protected void TrackInputDown(InputAction inputAction, GameObject target)
{
if (inputDownTrackingDictionary.TryGetValue(inputAction.Id, out var targets))
{
targets.EnsureListItem(target);
return;
}

inputDownTrackingDictionary.Add(inputAction.Id, new List<GameObject> { target });
}

/// <summary>
/// Raises input up for <paramref name="inputAction"/> on any previously tracked
/// <see cref="UnityEngine.GameObject"/>s using <see cref="TrackInputDown(InputAction, GameObject)"/>.
/// </summary>
/// <param name="inputAction">The <see cref="InputAction"/> to resolve.</param>
protected void ResolveInputDown(InputAction inputAction)
{
if (!inputDownTrackingDictionary.TryGetValue(inputAction.Id, out var targets))
{
return;
}

for (var i = 0; i < targets.Count; i++)
{
var target = targets[i];
var handlers = target.GetComponents<IPointerHandler>();

if (handlers != null)
{
var eventData = new PointerEventData(UnityEvents.EventSystem.current);
eventData.Initialize(this, inputAction);

for (var j = 0; j < handlers.Length; j++)
{
handlers[j].OnPointerUp(eventData);
}
}
}

inputDownTrackingDictionary.Remove(inputAction.Id);
}

/// <inheritdoc />
bool IEqualityComparer.Equals(object left, object right)
{
Expand Down
1 change: 1 addition & 0 deletions Runtime/Input/Interactors/BaseDirectInteractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected override void OnRaisePointerDown(InputAction inputAction)
{
if (DirectResult.CurrentTarget.IsNotNull() && IsInteractionEnabled)
{
TrackInputDown(inputAction, DirectResult.CurrentTarget);
InputService.RaisePointerDown(this, inputAction);
}
}
Expand Down

0 comments on commit 1f712c1

Please sign in to comment.