// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System; using System.Collections.Generic; using System.Linq; using Doozy.Runtime.UIManager.Components; using UnityEngine; namespace Doozy.Runtime.UIManager { [Serializable] public class UIBehaviours { [SerializeField] private List Behaviours; /// List of UIBehaviour public List behaviours => Behaviours; [SerializeField] private GameObject SignalSource; /// Signal source public GameObject signalSource => SignalSource; [SerializeField] private UISelectable Selectable; /// UISelectable target public UISelectable selectable => Selectable; /// Construct a new UIBehaviours with a null signal source public UIBehaviours() : this(null) {} /// Construct a new UIBehaviours with the given signal source public UIBehaviours(GameObject signalSource) { SignalSource = signalSource; Behaviours = new List(); } /// Connect all behaviours public UIBehaviours Connect() { if (signalSource == null) return this; Behaviours.ForEach(ConnectBehaviour); return this; } /// Disconnect all behaviours public UIBehaviours Disconnect() { Behaviours.ForEach(DisconnectBehaviour); return this; } /// Connect behaviour /// Target behaviour private void ConnectBehaviour(UIBehaviour behaviour) { if (behaviour == null) return; behaviour.Disconnect(); behaviour .SetSelectable(selectable) .SetSignalSource(signalSource) .Connect(); } /// Disconnect behaviour /// Target Behaviour private void DisconnectBehaviour(UIBehaviour behaviour) => behaviour?.Disconnect(); /// /// Add the given behaviour and get a reference to it (automatically connects) /// If the behaviour already exists, the reference to it will get automatically returned. /// /// UIBehaviour.Name public UIBehaviour AddBehaviour(UIBehaviour.Name behaviourName) { if (HasBehaviour(behaviourName)) return GetBehaviour(behaviourName); UIBehaviour newBehaviour = new UIBehaviour(behaviourName, signalSource) .SetSelectable(selectable); Behaviours.Add(newBehaviour); if (Application.isPlaying) ConnectBehaviour(newBehaviour); var temp = (from UIBehaviour.Name name in Enum.GetValues(typeof(UIBehaviour.Name)) select GetBehaviour(name) into b where b != null select b).ToList(); Behaviours.Clear(); Behaviours.AddRange(temp); return newBehaviour; } /// Remove the given behaviour (automatically disconnects) /// UIBehaviour.Name public void RemoveBehaviour(UIBehaviour.Name behaviourName) { UIBehaviour behaviour = GetBehaviour(behaviourName); if (behaviour == null) return; DisconnectBehaviour(behaviour); Behaviours.Remove(behaviour); } /// Check if the given behaviour has been added (exists) /// UIBehaviour.Name public bool HasBehaviour(UIBehaviour.Name behaviourName) => Behaviours.Any(b => b.behaviourName == behaviourName); /// /// Get the behaviour with the given name. /// Returns null if the behaviour has not been added (does not exist) /// /// UIBehaviour.Name public UIBehaviour GetBehaviour(UIBehaviour.Name behaviourName) => Behaviours.FirstOrDefault(b => b.behaviourName == behaviourName); /// /// Set a new signal source for all behaviours /// /// Signal source public UIBehaviours SetSignalSource(GameObject target) { SignalSource = target; foreach (UIBehaviour behaviour in Behaviours) behaviour.SetSignalSource(target); return this; } /// /// Set target selectable for all behaviours /// /// Target selectable /// public UIBehaviours SetSelectable(UISelectable uiSelectable) { Selectable = uiSelectable; foreach (UIBehaviour behaviour in behaviours) behaviour.SetSelectable(selectable); return this; } /// /// Clear the target selectable for all behaviours /// public UIBehaviours ClearSelectable() => SetSelectable(null); } }