namespace VRTK.Prefabs.Interactions.Interactors
{
using UnityEngine;
using System.Collections.Generic;
using Malimbe.XmlDocumentationAttribute;
using Malimbe.PropertySerializationAttribute;
using Zinnia.Extension;
using Zinnia.Data.Attribute;
using Zinnia.Tracking.Collision;
using Zinnia.Tracking.Collision.Active;
using Zinnia.Tracking.Collision.Active.Operation;
///
/// Sets up the Interactor Prefab touch settings based on the provided user settings.
///
public class TouchInteractorConfigurator : MonoBehaviour
{
#region Facade Settings
///
/// The public interface facade.
///
[Serialized]
[field: Header("Facade Settings"), DocumentedByXml, Restricted]
public InteractorFacade Facade { get; protected set; }
#endregion
#region Touch Settings
///
/// The that holds all current collisions.
///
[Serialized]
[field: Header("Touch Settings"), DocumentedByXml, Restricted]
public ActiveCollisionsContainer ActiveCollisionsContainer { get; protected set; }
///
/// The that holds the current active collision.
///
[Serialized]
[field: DocumentedByXml, Restricted]
public Slicer CurrentActiveCollision { get; protected set; }
///
/// The for checking valid start touching collisions.
///
[Serialized]
[field: DocumentedByXml, Restricted]
public ActiveCollisionPublisher StartTouchingPublisher { get; protected set; }
///
/// The for checking valid stop touching collisions.
///
[Serialized]
[field: DocumentedByXml, Restricted]
public ActiveCollisionPublisher StopTouchingPublisher { get; protected set; }
#endregion
///
/// A collection of currently touched GameObjects.
///
public IReadOnlyList TouchedObjects => GetTouchedObjects();
///
/// The currently active touched GameObject.
///
public GameObject ActiveTouchedObject => GetActiveTouchedObject();
///
/// A reusable collection to hold the returned touched objects.
///
protected readonly List touchedObjects = new List();
///
/// Configures the components for touching and untouching.
///
public virtual void ConfigurePublishers()
{
if (StartTouchingPublisher != null)
{
StartTouchingPublisher.Payload.SourceContainer = Facade.gameObject;
}
if (StopTouchingPublisher != null)
{
StopTouchingPublisher.Payload.SourceContainer = Facade.gameObject;
}
}
protected virtual void OnEnable()
{
ConfigurePublishers();
}
protected virtual void OnDisable()
{
StopTouchingPublisher.SetActiveCollisionsEvenWhenDisabled(StartTouchingPublisher.Payload);
StopTouchingPublisher.ForcePublish();
}
///
/// Retrieves a collection of currently touched GameObjects.
///
/// The currently touched GameObjects.
protected virtual IReadOnlyList GetTouchedObjects()
{
touchedObjects.Clear();
if (ActiveCollisionsContainer == null)
{
return touchedObjects;
}
foreach (CollisionNotifier.EventData element in ActiveCollisionsContainer.Elements)
{
touchedObjects.Add(element.ColliderData.GetContainingTransform().gameObject);
}
return touchedObjects;
}
///
/// Retrieves the currently active touched GameObject.
///
/// The currently active touched GameObject.
protected virtual GameObject GetActiveTouchedObject()
{
if (CurrentActiveCollision == null || CurrentActiveCollision.SlicedList.ActiveCollisions.Count == 0)
{
return null;
}
return CurrentActiveCollision.SlicedList.ActiveCollisions[0].ColliderData.GetContainingTransform().gameObject;
}
}
}