namespace UIWidgets { using UIWidgets.Styles; using UnityEngine; using UnityEngine.UI; /// /// TreeGraph component. /// /// Node type. [RequireComponent(typeof(MultipleConnector))] public abstract class TreeGraphComponent : MonoBehaviour, IStylable { /// /// Foreground graphics for coloring. /// [SerializeField] protected Graphic[] graphicsForeground = Compatibility.EmptyArray(); /// /// Gets foreground graphics for coloring. /// public virtual Graphic[] GraphicsForeground { get { GraphicsForegroundInit(); return graphicsForeground; } } /// /// Background graphics for coloring. /// [SerializeField] protected Graphic[] graphicsBackground = Compatibility.EmptyArray(); /// /// Get background graphics for coloring. /// public virtual Graphic[] GraphicsBackground { get { GraphicsBackgroundInit(); return graphicsBackground; } } /// /// Background. /// [SerializeField] public Image Background; RectTransform rectTransform; /// /// Gets the RectTransform. /// /// The RectTransform. public RectTransform RectTransform { get { if (rectTransform == null) { rectTransform = transform as RectTransform; } return rectTransform; } } /// /// Node. /// protected TreeNode Node; /// /// Init this instance. /// public virtual void Start() { Init(); } /// /// Init this instance. /// public virtual void Init() { if (Background != null) { return; } var bg = transform.Find("Background"); if (bg == null) { return; } Background = bg.GetComponent(); } /// /// Process locale changes. /// public virtual void LocaleChanged() { } /// /// Set data. /// /// Node. public abstract void SetData(TreeNode node); /// /// Called when item moved to cache, you can use it free used resources. /// public virtual void MovedToCache() { } /// /// Toggle node visibility. /// public virtual void ToggleVisibility() { Node.IsVisible = !Node.IsVisible; } /// /// Toggle expanded. /// public virtual void ToggleExpanded() { Node.IsExpanded = !Node.IsExpanded; } /// /// Graphics background version. /// [SerializeField] [HideInInspector] protected byte GraphicsBackgroundVersion = 0; /// /// Init graphics background. /// protected virtual void GraphicsBackgroundInit() { if (GraphicsBackgroundVersion == 0) { graphicsBackground = new Graphic[] { Background }; GraphicsBackgroundVersion = 1; } } /// /// Graphics foreground version. /// [SerializeField] [HideInInspector] protected byte GraphicsForegroundVersion = 0; /// /// Init graphics foreground. /// protected virtual void GraphicsForegroundInit() { } #if UNITY_EDITOR /// /// Validate this instance. /// protected virtual void OnValidate() { GraphicsBackgroundInit(); GraphicsForegroundInit(); #if UNITY_2018_3_OR_NEWER if (UnityEditor.PrefabUtility.IsPartOfAnyPrefab(this)) #endif { UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this); } if (Compatibility.IsPrefab(this)) { UnityEditor.EditorUtility.SetDirty(this); } } #endif #region IStylable implementation /// /// Set the style. /// /// Style for the background. /// Style for the text. /// Full style data. public virtual void SetStyle(StyleImage styleBackground, StyleText styleText, Style style) { styleBackground.ApplyTo(Background); foreach (var gf in GraphicsForeground) { if (gf != null) { styleText.ApplyTo(gf.gameObject); } } var connector = GetComponent(); if (connector != null) { connector.SetStyle(style); } } /// /// Set the specified style. /// /// true, if style was set for children gameobjects, false otherwise. /// Style data. public virtual bool SetStyle(Style style) { SetStyle(style.Collections.DefaultItemBackground, style.Collections.DefaultItemText, style); return true; } /// /// Set style options from widget properties. /// /// Style for the background. /// Style for the text. /// Full style data. public virtual void GetStyle(StyleImage styleBackground, StyleText styleText, Style style) { styleBackground.GetFrom(Background); foreach (var gf in GraphicsForeground) { if (gf != null) { styleText.GetFrom(gf.gameObject); } } var connector = GetComponent(); if (connector != null) { connector.GetStyle(style); } } /// /// Set style options from widget properties. /// /// Style data. /// true, if children gameobjects was processed, false otherwise. public virtual bool GetStyle(Style style) { GetStyle(style.Collections.DefaultItemBackground, style.Collections.DefaultItemText, style); return true; } #endregion } }