namespace Tilia.Interactions.Interactables.Interactables { using System; using Tilia.Interactions.Interactables.Interactors; using Tilia.Interactions.Interactables.Interactors.Collection; using UnityEngine; using UnityEngine.Events; using Zinnia.Action; using Zinnia.Data.Attribute; using Zinnia.Extension; /// /// The public interface into the Interactor Action Receiver Prefab. /// public class InteractableActionReceiverFacade : MonoBehaviour { /// /// Defines the event with the . /// [Serializable] public class UnityEvent : UnityEvent { } /// /// The states of interaction. /// public enum InteractionState { /// /// A custom state that will not automatically register any events. /// Custom, /// /// When the first interactor starts touching the interactable. /// FirstTouch, /// /// When an interactor touches the interactable. /// Touch, /// /// When the first interactor starts grabbing the interactable. /// FirstGrab, /// /// When an interactor grabs the interactable. /// Grab } #region Action Settings [Header("Receiver Settings")] [Tooltip("The InteractableFacade that the action receiver will target.")] [SerializeField] private InteractableFacade targetInteractable; /// /// The that the action receiver will target. /// public InteractableFacade TargetInteractable { get { return targetInteractable; } set { if (this.IsMemberChangeAllowed()) { OnBeforeTargetInteractableChange(); } targetInteractable = value; if (this.IsMemberChangeAllowed()) { OnAfterTargetInteractableChange(); } } } [Tooltip("The InteractionState that determies when to activate the action receiver.")] [SerializeField] private InteractionState activationState = InteractionState.Grab; /// /// The that determies when to activate the action receiver. /// public InteractionState ActivationState { get { return activationState; } set { if (this.IsMemberChangeAllowed()) { OnBeforeActivationStateChange(); } activationState = value; if (this.IsMemberChangeAllowed()) { OnAfterActivationStateChange(); } } } [Tooltip("The InteractorActionPublisherFacade collection of the publishers to receive data from.")] [SerializeField] private InteractorActionPublisherFacadeObservableList sourcePublishers; /// /// The collection of the publishers to receive data from. /// public InteractorActionPublisherFacadeObservableList SourcePublishers { get { return sourcePublishers; } set { sourcePublishers = value; } } #endregion #region Publisher Events /// /// Emitted when the Receiver is activated by an allowed publisher. /// [Header("Publisher Events")] public UnityEvent Activated = new UnityEvent(); /// /// Emitted when the Receiver is deactivated from an allowed publisher. /// public UnityEvent Deactivated = new UnityEvent(); #endregion #region Reference Settings [Header("Reference Settings")] [Tooltip("The Action that will be linked to the SourceAction.")] [SerializeField] [Restricted] private InteractableActionReceiverConfigurator configuration; /// /// The that will be linked to the . /// public InteractableActionReceiverConfigurator Configuration { get { return configuration; } set { configuration = value; } } #endregion /// /// Clears . /// public virtual void ClearTargetInteractable() { if (!this.IsValidState()) { return; } TargetInteractable = default; } /// /// Clears . /// public virtual void ClearSourcePublishers() { if (!this.IsValidState()) { return; } SourcePublishers = default; } /// /// Sets the . /// /// The index of the . public virtual void SetActivationState(int index) { ActivationState = EnumExtensions.GetByIndex(index); } /// /// Enables the given source on the . /// /// The source to enable. public virtual void EnableActionRegistrar(InteractorFacade source) { if (source == null) { return; } EnableActionRegistrar(source.gameObject); } /// /// Enables the given source on the . /// /// The source to enable. public virtual void EnableActionRegistrar(GameObject source) { if (source == null) { return; } Configuration.ActionRegistrar.enabled = true; Configuration.ActionRegistrar.Sources.EnableSource(source); } /// /// Disables the given source on the . /// /// The source to disable. public virtual void DisableActionRegistrar(InteractorFacade source) { if (source == null) { return; } DisableActionRegistrar(source.gameObject); } /// /// Disables the given source on the . /// /// The source to disable. public virtual void DisableActionRegistrar(GameObject source) { if (source == null) { return; } Configuration.ActionRegistrar.Target.ReceiveDefaultValue(); Configuration.ActionRegistrar.Sources.DisableSource(source); foreach (ActionRegistrar.ActionSource actionSource in Configuration.ActionRegistrar.Sources.SubscribableElements) { if (actionSource.Enabled) { return; } } Configuration.ActionRegistrar.enabled = false; } /// /// Called before has been changed. /// protected virtual void OnBeforeTargetInteractableChange() { Configuration.UnregisterInteractableEvents(TargetInteractable, ActivationState); } /// /// Called after has been changed. /// protected virtual void OnAfterTargetInteractableChange() { Configuration.LinkInteractableToConsumers(); Configuration.RegisterInteractableEvents(TargetInteractable, ActivationState); } /// /// Called before has been changed. /// protected virtual void OnBeforeActivationStateChange() { Configuration.UnregisterInteractableEvents(TargetInteractable, ActivationState); } /// /// Called after has been changed. /// protected virtual void OnAfterActivationStateChange() { Configuration.RegisterInteractableEvents(TargetInteractable, ActivationState); } } }