namespace Tilia.Interactions.Interactables.Interactors { using System; using System.Collections.Generic; using UnityEngine; using Zinnia.Action; using Zinnia.Extension; using Zinnia.Tracking.Velocity; /// /// Allows the modification of the settings on a specified found in a collection. /// public class InteractorFacadeSettingsModifier : MonoBehaviour { [Serializable] public class InteractorElement { [Tooltip("The InteractorFacade to modify the settings on.")] [SerializeField] private InteractorFacade targetFacade; /// /// The to modify the settings on. /// public InteractorFacade TargetFacade { get { return targetFacade; } set { targetFacade = value; OnAfterTargetFacadeChange(); } } [Tooltip("The BooleanAction to update the InteractorFacade.GrabAction to.")] [SerializeField] private BooleanAction targetGrabAction; /// /// The to update the to. /// public BooleanAction TargetGrabAction { get { return targetGrabAction; } set { targetGrabAction = value; } } [Tooltip("The VelocityTracker to update the InteractorFacade.VelocityTracker to.")] [SerializeField] private VelocityTracker targetVelocityTracker; /// /// The to update the to. /// public VelocityTracker TargetVelocityTracker { get { return targetVelocityTracker; } set { targetVelocityTracker = value; } } [Tooltip("The updated value to set the InteractorFacade.GrabPrecognition to.")] [SerializeField] private float targetGrabPrecognition; /// /// The updated value to set the to. /// public float TargetGrabPrecognition { get { return targetGrabPrecognition; } set { targetGrabPrecognition = value; } } /// /// The original on the to revert back to. /// protected BooleanAction cachedGrabAction; /// /// The original on the to revert back to. /// protected VelocityTracker cachedVelocityTracker; /// /// The original on the to revert back to. /// protected float cachedGrabPrecognition; /// /// Clears . /// public virtual void ClearTargetFacade() { TargetFacade = default; } /// /// Clears . /// public virtual void ClearTargetGrabAction() { TargetGrabAction = default; } /// /// Clears . /// public virtual void ClearTargetVelocityTracker() { TargetVelocityTracker = default; } /// /// Sets the to the value of . /// /// Caches the current value to be restored later. public virtual void SetTargetGrabAction(bool cacheCurrentGrabAction) { if (cacheCurrentGrabAction) { cachedGrabAction = TargetFacade.GrabAction; } TargetFacade.GrabAction = TargetGrabAction; } /// /// Restores the cached . /// public virtual void RestoreCachedGrabAction() { TargetFacade.GrabAction = cachedGrabAction; } /// /// Sets the to the value of . /// /// Caches the current value to be restored later. public virtual void SetTargetVelocityTracker(bool cacheCurrentVelocityTracker) { if (cacheCurrentVelocityTracker) { cachedVelocityTracker = TargetFacade.VelocityTracker; } TargetFacade.VelocityTracker = TargetVelocityTracker; } /// /// Restores the cached . /// public virtual void RestoreCachedVelocityTracker() { TargetFacade.VelocityTracker = cachedVelocityTracker; } /// /// Sets the to the value of . /// /// Caches the current value to be restored later. public virtual void SetTargetGrabPrecognition(bool cacheCurrentGrabPrecognition) { if (cacheCurrentGrabPrecognition) { cachedGrabPrecognition = TargetFacade.GrabPrecognition; } TargetFacade.GrabPrecognition = TargetGrabPrecognition; } /// /// Restores the cached . /// public virtual void RestoreCachedGrabPrecognition() { TargetFacade.GrabPrecognition = cachedGrabPrecognition; } /// /// Called after has been changed. /// protected virtual void OnAfterTargetFacadeChange() { cachedGrabAction = TargetFacade.GrabAction; cachedVelocityTracker = TargetFacade.VelocityTracker; cachedGrabPrecognition = TargetFacade.GrabPrecognition; } [Obsolete("Use `OnAfterTargetFacadeChange` instead.")] protected virtual void OnAfterFacadeChange() { OnAfterTargetFacadeChange(); } } [Tooltip("The InteractorElement collection of settings to modify per InteractorFacade.")] [SerializeField] private List elements = new List(); /// /// The collection of settings to modify per . /// public List Elements { get { return elements; } set { elements = value; } } [Tooltip("Determines whether to cache the element settings when attempting to set them.")] [SerializeField] private bool cacheElementSettings = true; /// /// Determines whether to cache the element settings when attempting to set them. /// public bool CacheElementSettings { get { return cacheElementSettings; } set { cacheElementSettings = value; } } /// /// Clears . /// public virtual void ClearElements() { if (!this.IsValidState()) { return; } Elements.Clear(); } /// /// Matches the given in the and updates the with the relevant . /// /// The to update. public virtual void SetTargetGrabActionFor(InteractorFacade interactorFacade) { if (!this.IsValidState()) { return; } InteractorElement interactor = Elements.Find(x => x.TargetFacade.Equals(interactorFacade)); if (interactor != null) { interactor.SetTargetGrabAction(CacheElementSettings); } } /// /// Matches the given in the and restores the cached . /// /// The to update. public virtual void RestoreCachedGrabAction(InteractorFacade interactorFacade) { InteractorElement interactor = Elements.Find(interactorElement => interactorElement.TargetFacade.Equals(interactorFacade)); if (interactor != null) { interactor.RestoreCachedGrabAction(); } } /// /// Matches the given in the and updates the with the relevant . /// /// The to update. public virtual void SetTargetVelocityTracker(InteractorFacade interactorFacade) { if (!this.IsValidState()) { return; } InteractorElement interactor = Elements.Find(x => x.TargetFacade.Equals(interactorFacade)); if (interactor != null) { interactor.SetTargetVelocityTracker(CacheElementSettings); } } /// /// Matches the given in the and restores the cached . /// /// The to update. public virtual void RestoreCachedVelocityTracker(InteractorFacade interactorFacade) { InteractorElement interactor = Elements.Find(interactorElement => interactorElement.TargetFacade.Equals(interactorFacade)); if (interactor != null) { interactor.RestoreCachedVelocityTracker(); } } /// /// Matches the given in the and updates the with the relevant . /// /// The to update. public virtual void SetTargetGrabPrecognition(InteractorFacade interactorFacade) { if (!this.IsValidState()) { return; } InteractorElement interactor = Elements.Find(x => x.TargetFacade.Equals(interactorFacade)); if (interactor != null) { interactor.SetTargetGrabPrecognition(CacheElementSettings); } } /// /// Matches the given in the and restores the cached . /// /// The to update. public virtual void RestoreCachedGrabPrecognition(InteractorFacade interactorFacade) { InteractorElement interactor = Elements.Find(interactorElement => interactorElement.TargetFacade.Equals(interactorFacade)); if (interactor != null) { interactor.RestoreCachedGrabPrecognition(); } } } }