From 4f158857ff60c674c6143c50ba93e56e0f9aced6 Mon Sep 17 00:00:00 2001 From: Shariff Faleel Date: Thu, 18 Jul 2024 17:32:19 -0700 Subject: [PATCH] Adding hover update event --- Runtime/Interaction/HPUIEvents.cs | 44 ++++++++++++++++++++++++++ Runtime/Interaction/HPUIInteractor.cs | 35 +++++++++++++++++--- Runtime/Interaction/IHPUIInteractor.cs | 5 +++ 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/Runtime/Interaction/HPUIEvents.cs b/Runtime/Interaction/HPUIEvents.cs index 558538a..acaae8f 100644 --- a/Runtime/Interaction/HPUIEvents.cs +++ b/Runtime/Interaction/HPUIEvents.cs @@ -19,6 +19,50 @@ public enum HPUIGestureState } #region events classes + /// + /// Event class that reports hover events + /// + public class HPUIHoverUpdateEvent : UnityEvent + {} + + // NOTE: This is used instead of the IXRHoverStrength* interfaces as those + // interfaces don't report the data being exposed here. + /// + /// Event data associated with a Hover update on HPUI + /// + public class HPUIHoverUpdateEventArgs + { + /// + /// Instantiate hover event. + /// + public HPUIHoverUpdateEventArgs(IHPUIInteractor interactorObject, Vector3 hoverPoint, Vector3 attachPoint) + { + this.interactorObject = interactorObject; + this.hoverPoint = hoverPoint; + this.attachPoint = attachPoint; + } + + /// + /// The Interactor associated with the interaction event. + /// + public IHPUIInteractor interactorObject { get; set; } + + /// + /// The endpoint of the hover. This is generally the centroid when + /// the interactor is hovering on targets. + /// + public Vector3 hoverPoint { get; set; } + + /// + /// The location of the attach transform. + /// + /// + public Vector3 attachPoint { get; set; } + } + + /// + /// Base event class for tap/gesture events + /// public class HPUIInteractionEvent: UnityEvent where T: HPUIInteractionEventArgs { protected int eventsCount = 0; diff --git a/Runtime/Interaction/HPUIInteractor.cs b/Runtime/Interaction/HPUIInteractor.cs index 0831228..baa74b0 100644 --- a/Runtime/Interaction/HPUIInteractor.cs +++ b/Runtime/Interaction/HPUIInteractor.cs @@ -148,6 +148,13 @@ public RayCastTechniqueEnum RayCastTechnique { /// public HPUIGestureEvent GestureEvent { get => gestureEvent; set => gestureEvent = value; } + [SerializeField] + [Tooltip("Event triggered on hover update.")] + private HPUIHoverUpdateEvent hoverUpdateEvent = new HPUIHoverUpdateEvent(); + + /// + public HPUIHoverUpdateEvent HoverUpdateEvent { get => hoverUpdateEvent; set => hoverUpdateEvent = value; } + [SerializeField] [Tooltip("Interation hover radius.")] private float interactionHoverRadius = 0.015f; @@ -395,7 +402,6 @@ protected override void OnEnable() //FIXME: debug code public Transform o1, o2, o3; - public LineRenderer lineRenderer; /// public override void PreprocessInteractor(XRInteractionUpdateOrder.UpdatePhase updatePhase) @@ -561,10 +567,12 @@ interactable is IHPUIInteractable hpuiInteractable && ListPool.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(); @@ -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]; @@ -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))); diff --git a/Runtime/Interaction/IHPUIInteractor.cs b/Runtime/Interaction/IHPUIInteractor.cs index 7926093..592cb4a 100644 --- a/Runtime/Interaction/IHPUIInteractor.cs +++ b/Runtime/Interaction/IHPUIInteractor.cs @@ -15,6 +15,11 @@ public interface IHPUIInteractor: IXRSelectInteractor, IXRHoverInteractor /// public HPUIGestureEvent GestureEvent { get; } + /// + /// Event that triggers with hover strength data + /// + public HPUIHoverUpdateEvent HoverUpdateEvent { get; } + /// /// This is called when a tap event occurs on the interactable. ///