namespace Zinnia.Data.Collection.List { using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using Zinnia.Extension; /// /// Allows observing changes to a of s. /// public class GameObjectMultiRelationObservableList : DefaultObservableList { /// /// A relationship between a key and its associated collection. /// [Serializable] public class MultiRelation { [Tooltip("The GameObject acting as the key.")] [SerializeField] private GameObject key; /// /// The acting as the key. /// public GameObject Key { get { return key; } set { key = value; } } [Tooltip("The GameObject collection of relation values.")] [SerializeField] private List values; /// /// The collection of relation values. /// public List Values { get { return values; } set { values = value; } } /// /// Clears . /// public virtual void ClearKey() { Key = default; } /// /// Clears . /// public virtual void ClearValues() { Values.Clear(); } } /// /// Defines the event with the . /// [Serializable] public class MultiRelationUnityEvent : UnityEvent { } /// /// Defines the event with the "/>. /// [Serializable] public class ListUnityEvent : UnityEvent> { } #region Relationship Events /// /// Emitted when the given key is matched with a relationship. /// [Header("Relationship Events")] public ListUnityEvent RelationshipFound = new ListUnityEvent(); /// /// Emitted when the given key not is matched with a relationship. /// public UnityEvent RelationshipNotFound = new UnityEvent(); #endregion /// /// Whether the contains a relationship between the given key and provides the related values. /// /// The key to check relationship for. /// The values associated with any found key. /// Whether there are any relationships for the given key. public virtual bool HasRelationship(GameObject key, out List values) { foreach (MultiRelation element in Elements) { if (element.Key == key) { values = element.Values; if (this.IsValidState()) { RelationshipFound?.Invoke(values); } return true; } } values = null; if (this.IsValidState()) { RelationshipNotFound.Invoke(); } return false; } } }