Skip to content

Commit

Permalink
Adding hover update event
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-shariff committed Sep 29, 2024
1 parent 65cdb92 commit 4f15885
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
44 changes: 44 additions & 0 deletions Runtime/Interaction/HPUIEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,50 @@ public enum HPUIGestureState
}

#region events classes
/// <summary>
/// Event class that reports hover events
/// </summary>
public class HPUIHoverUpdateEvent : UnityEvent<HPUIHoverUpdateEventArgs>
{}

// NOTE: This is used instead of the IXRHoverStrength* interfaces as those
// interfaces don't report the data being exposed here.
/// <summary>
/// Event data associated with a Hover update on HPUI
/// </summary>
public class HPUIHoverUpdateEventArgs
{
/// <summary>
/// Instantiate hover event.
/// </summary>
public HPUIHoverUpdateEventArgs(IHPUIInteractor interactorObject, Vector3 hoverPoint, Vector3 attachPoint)
{
this.interactorObject = interactorObject;
this.hoverPoint = hoverPoint;
this.attachPoint = attachPoint;
}

/// <summary>
/// The Interactor associated with the interaction event.
/// </summary>
public IHPUIInteractor interactorObject { get; set; }

/// <summary>
/// The endpoint of the hover. This is generally the centroid when
/// the interactor is hovering on targets.
/// </summary>
public Vector3 hoverPoint { get; set; }

/// <summary>
/// The location of the attach transform.
/// <seealso cref="XRBaseInteractor.GetAttachTransform"/>
/// </summary>
public Vector3 attachPoint { get; set; }
}

/// <summary>
/// Base event class for tap/gesture events
/// </summary>
public class HPUIInteractionEvent<T>: UnityEvent<T> where T: HPUIInteractionEventArgs
{
protected int eventsCount = 0;
Expand Down
35 changes: 30 additions & 5 deletions Runtime/Interaction/HPUIInteractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ public RayCastTechniqueEnum RayCastTechnique {
/// <inheritdoc />
public HPUIGestureEvent GestureEvent { get => gestureEvent; set => gestureEvent = value; }

[SerializeField]
[Tooltip("Event triggered on hover update.")]
private HPUIHoverUpdateEvent hoverUpdateEvent = new HPUIHoverUpdateEvent();

/// <inheritdoc />
public HPUIHoverUpdateEvent HoverUpdateEvent { get => hoverUpdateEvent; set => hoverUpdateEvent = value; }

[SerializeField]
[Tooltip("Interation hover radius.")]
private float interactionHoverRadius = 0.015f;
Expand Down Expand Up @@ -395,7 +402,6 @@ protected override void OnEnable()

//FIXME: debug code
public Transform o1, o2, o3;
public LineRenderer lineRenderer;

/// <inheritdoc />
public override void PreprocessInteractor(XRInteractionUpdateOrder.UpdatePhase updatePhase)
Expand Down Expand Up @@ -561,10 +567,12 @@ interactable is IHPUIInteractable hpuiInteractable &&
ListPool<InteractionInfo>.Release(kvp.Value);
}

if (lineRenderer != null)
if (validTargets.Count > 0)
{
lineRenderer.SetPosition(0, attachTransform.position);
lineRenderer.SetPosition(1, new Vector3(xEndPoint, yEndPoint, zEndPoint) / count);
HoverUpdateEvent?.Invoke(new HPUIHoverUpdateEventArgs(
this,
new Vector3(xEndPoint, yEndPoint, zEndPoint) / count,
attachTransform.position));
}

tempValidTargets.Clear();
Expand All @@ -580,6 +588,9 @@ interactable is IHPUIInteractable hpuiInteractable &&
// FIXME: QueryTriggerInteraction should be allowed to be set in inpsector
QueryTriggerInteraction.Ignore);

Vector3 hoverPoint = attachTransform.position;
float shortestInteractableDist = float.MaxValue;

for (var i = 0; i < numberOfOverlaps; ++i)
{
Collider collider = overlapSphereHits[i];
Expand All @@ -589,9 +600,23 @@ interactable is IHPUIInteractable hpuiInteractable &&
hpuiInteractable.IsHoverableBy(this))
{
XRInteractableUtility.TryGetClosestPointOnCollider(interactable, interactionPoint, out DistanceInfo info);
validTargets.Add(hpuiInteractable, new InteractionInfo(Mathf.Sqrt(info.distanceSqr), info.point));
float dist = Mathf.Sqrt(info.distanceSqr);
validTargets.Add(hpuiInteractable, new InteractionInfo(dist, info.point));
if (dist < shortestInteractableDist)
{
hoverPoint = info.point;
}
}
}

if (validTargets.Count > 0)
{
HoverUpdateEvent?.Invoke(new HPUIHoverUpdateEventArgs(
this,
hoverPoint,
attachTransform.position));
}

}
}
gestureLogic.Update(validTargets.ToDictionary(kvp => kvp.Key, kvp => new HPUIInteractionData(kvp.Value.distance, kvp.Value.huristic)));
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Interaction/IHPUIInteractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public interface IHPUIInteractor: IXRSelectInteractor, IXRHoverInteractor
/// </summary>
public HPUIGestureEvent GestureEvent { get; }

/// <summary>
/// Event that triggers with hover strength data
/// </summary>
public HPUIHoverUpdateEvent HoverUpdateEvent { get; }

/// <summary>
/// This is called when a tap event occurs on the interactable.
/// </summary>
Expand Down

0 comments on commit 4f15885

Please sign in to comment.