Skip to content

Commit

Permalink
Adding more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AMollis committed Jun 26, 2023
1 parent 446d443 commit 23f4b23
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ private void DisconnectEventRouteFromKnownInteractables(IXRInteractableEventRout
private bool IsValidChild(IXRInteractable interactable)
{
return interactable is MonoBehaviour behaviour &&
behaviour.GetComponentInParent<InteractableEventRouter>() == this &&
behaviour.gameObject != gameObject;
}

Expand Down Expand Up @@ -453,12 +452,22 @@ public abstract class InteractableEventRoute<S, T> : IXRInteractableEventRoute
where S : IXRInteractable
where T : IXRInteractableEventRouteTarget
{
private T[] targets = null;
private List<T> targets = null;

/// <inheritdoc/>
public void OnEnabled(GameObject origin)
{
targets = GetTargets(origin);
if (targets == null)
{
targets = new List<T>();
}
else
{
targets.Clear();
}

GetTargets(origin, targets);
FilterTargets(origin, targets);
}

/// <inheritdoc/>
Expand All @@ -472,7 +481,7 @@ public void Register(IXRInteractable interactable)

if (interactable is S source)
{
for (int i = 0; i < targets.Length; i++)
for (int i = 0; i < targets.Count; i++)
{
if (targets[i] is T target)
{
Expand All @@ -492,7 +501,7 @@ public void Unregister(IXRInteractable interactable)

if (interactable is S source)
{
for (int i = 0; i < targets.Length; i++)
for (int i = 0; i < targets.Count; i++)
{
if (targets[i] is T target)
{
Expand All @@ -502,14 +511,34 @@ public void Unregister(IXRInteractable interactable)
}
}

/// <summary>
/// FIlter targets that are being targeted by other <see cref="InteractableEventRouter"/> objects.
/// </summary>
/// <param name="origin">The origin that is targetting <see cref="IXRInteractableEventRouteTarget"/>.</param>
private void FilterTargets(GameObject origin, List<T> targets)
{
for (int i = targets.Count - 1; i >= 0; i--)
{
var target = targets[i];
if (target is MonoBehaviour behaviour)
{
var router = behaviour.GetComponentInParent<InteractableEventRouter>();
if (router != null && router.gameObject != origin)
{
targets.RemoveAt(i);
}
}
}
}

/// <summary>
/// Search for and return all <see cref="IXRInteractableEventRouteTarget"/> objects that should receive events from the
/// registered <see href="https://docs.unity3d.com/Packages/[email protected]/api/UnityEngine.XR.Interaction.Toolkit.IXRInteractable.html">IXRInteractable</see>
/// objects.
/// </summary>
/// <param name="origin">This game object will be queried for components that implement <see cref="IXRInteractableEventRouteTarget"/>.</param>
/// <returns>An array of specialized <see cref="IXRInteractableEventRouteTarget"/> components.</returns>
protected abstract T[] GetTargets(GameObject origin);
/// <param name="targets">A list that should be filled with specialized <see cref="IXRInteractableEventRouteTarget"/> component objects.</param>
protected abstract void GetTargets(GameObject origin, List<T> targets);

/// <summary>
/// Add event handlers that will handle events from the <paramref name="source"/> and direct them to the <paramref name="target"/>.
Expand Down Expand Up @@ -564,10 +593,10 @@ public abstract class InteractableParentEventRoute<S, T> : InteractableEventRout
/// <param name="origin">
/// The game object whose children will be queried for components that implement <see cref="IXRInteractableEventRouteTarget"/>.
/// </param>
/// <returns>An array of specialized <see cref="IXRInteractableEventRouteTarget"/> components.</returns>
protected override T[] GetTargets(GameObject origin)
/// <param name="targets">A list that should be filled with specialized <see cref="IXRInteractableEventRouteTarget"/> component objects.</param>
protected override void GetTargets(GameObject origin, List<T> targets)
{
return origin.GetComponentsInChildren<T>(includeInactive: true);
origin.GetComponentsInChildren<T>(includeInactive: true, targets);
}
}

Expand Down Expand Up @@ -602,10 +631,10 @@ public abstract class InteractableChildrenEventRoute<S, T> : InteractableEventRo
/// <param name="origin">
/// The game object whose children will be queried for components that implement <see cref="IXRInteractableEventRouteTarget"/>.
/// </param>
/// <returns>An array of specialized <see cref="IXRInteractableEventRouteTarget"/> components.</returns>
protected override T[] GetTargets(GameObject origin)
/// <param name="targets">A list that should be filled with specialized <see cref="IXRInteractableEventRouteTarget"/> component objects.</param>
protected override void GetTargets(GameObject origin, List<T> targets)
{
return origin.GetComponentsInChildren<T>(includeInactive: true);
origin.GetComponentsInChildren<T>(includeInactive: true, targets);
}
}

Expand Down
Loading

0 comments on commit 23f4b23

Please sign in to comment.