namespace Zinnia.Action { using UnityEngine; using Zinnia.Action.Collection; using Zinnia.Extension; /// /// Emits a value when any given actions are in their active state. /// public class AnyAction : BooleanAction { [Tooltip("Actions to check the active state on.")] [SerializeField] private ActionObservableList actions; /// /// s to check the active state on. /// public ActionObservableList Actions { get { return actions; } set { if (this.IsMemberChangeAllowed()) { OnBeforeActionsChange(); } actions = value; if (this.IsMemberChangeAllowed()) { OnAfterActionsChange(); } } } protected override void OnEnable() { if (Actions == null) { return; } AddActionsListeners(); CheckAllActions(); } protected override void OnDisable() { if (Actions == null) { return; } RemoveActionsListeners(); } /// /// Subscribes to events of . /// protected virtual void AddActionsListeners() { Actions.Added.AddListener(OnActionAdded); Actions.Removed.AddListener(OnActionRemoved); foreach (Action action in Actions.SubscribableElements) { action.ActivationStateChanged.AddListener(OnActionActivationStateChanged); } } /// /// Unsubscribes from events of . /// protected virtual void RemoveActionsListeners() { Actions.Added.RemoveListener(OnActionAdded); Actions.Removed.RemoveListener(OnActionRemoved); foreach (Action action in Actions.SubscribableElements) { if (action != null) { action.ActivationStateChanged.RemoveListener(OnActionActivationStateChanged); } } } /// /// Checks whether all are and calls on this instance to update its own activation state if necessary. /// protected virtual void CheckAllActions() { if (Actions == null) { if (IsActivated) { Receive(DefaultValue); } return; } bool areAllActionsActivated = DefaultValue; foreach (Action action in Actions.SubscribableElements) { if (action.IsActivated) { areAllActionsActivated = !DefaultValue; break; } } if (areAllActionsActivated != IsActivated) { Receive(areAllActionsActivated); } } /// /// Called after the state of any element in changes. /// /// Whether the action is activated. protected virtual void OnActionActivationStateChanged(bool isActionActivated) { if (!this.IsValidState()) { return; } if (IsActivated && !isActionActivated) { CheckAllActions(); } else if (!IsActivated && isActionActivated) { Receive(!DefaultValue); } } /// /// Called after an element is added to . /// /// The element added to the collection. protected virtual void OnActionAdded(Action action) { if (!this.IsValidState() || action == null) { return; } OnActionActivationStateChanged(action.IsActivated); action.ActivationStateChanged.AddListener(OnActionActivationStateChanged); } /// /// Called after an element is removed from . /// /// The element removed from the collection. protected virtual void OnActionRemoved(Action action) { if (!this.IsValidState() || action != null) { action.ActivationStateChanged.RemoveListener(OnActionActivationStateChanged); } CheckAllActions(); } /// /// Called before has been changed. /// protected virtual void OnBeforeActionsChange() { if (Actions != null) { RemoveActionsListeners(); } } /// /// Called after has been changed. /// protected virtual void OnAfterActionsChange() { if (Actions != null) { AddActionsListeners(); } CheckAllActions(); } } }