namespace UIWidgets.WidgetGeneration { using System.Collections.Generic; using UIWidgets; using UIWidgets.Styles; using UnityEngine; /// /// Base class for the test script for the DataItem. /// /// Type of the test item. public abstract class TestBase : MonoBehaviour { /// /// Generate item for specified index. /// /// Index. /// Item. protected abstract T GenerateItem(int index); /// /// Generate item for specified index with specified name. /// /// Name. /// Index. /// Item. protected abstract T GenerateItem(string name, int index); /// /// Sprites. /// [SerializeField] public List Sprites; /// /// Textures. /// [SerializeField] public List Textures; /// /// Root canvas. /// [SerializeField] public GameObject RootCanvas; /// /// Default style. /// [SerializeField] public Style StyleDefault; /// /// Blue style. /// [SerializeField] public Style StyleBlue; /// /// Is items can be generated? /// protected virtual bool CanGenerateItems { get { return true; } } /// /// Set default style. /// public void SetDefaultStyle() { StyleDefault.ApplyTo(RootCanvas); } /// /// Set blue style. /// public void SetBlueStyle() { StyleBlue.ApplyTo(RootCanvas); } /// /// Get random sprite. /// /// Sprite. protected Sprite GetSprite() { if (Sprites.Count == 0) { return null; } return Sprites[Random.Range(0, Sprites.Count - 1)]; } /// /// Get random texture. /// /// Texture. protected Texture2D GetTexture() { if (Textures.Count == 0) { return null; } return Textures[Random.Range(0, Textures.Count - 1)]; } /// /// Generate items list. /// /// Items count. /// List. protected ObservableList GenerateList(int count) { var result = new ObservableList(); if (!CanGenerateItems) { return result; } result.BeginUpdate(); for (int i = 0; i < count; i++) { result.Add(GenerateItem(i + 1)); } result.EndUpdate(); return result; } /// /// Generate nodes. /// /// List of required nodes amount per level. /// Name prefix. /// Start index in the configuration. /// Nodes. protected ObservableList> GenerateNodes(List config, string namePrefix = "Node ", int start = 0) { if (!CanGenerateItems) { return new ObservableList>(); } var count = config[start]; var result = new ObservableList>(true, count); result.BeginUpdate(); for (int i = 1; i <= count; i++) { result.Add(GenerateNode(i, config, namePrefix, start)); } result.EndUpdate(); return result; } /// /// Generate node. /// /// Node index. /// List of required nodes amount per level. /// Node name prefix. /// Start index in the configuration. /// Node. protected TreeNode GenerateNode(int index, List config, string namePrefix, int start) { var item_name = string.Format("{0}{1}", namePrefix, index.ToString()); var item = GenerateItem(item_name, index); var nodes = config.Count > (start + 1) ? GenerateNodes(config, string.Format("{0} - ", item_name), start + 1) : null; return new TreeNode(item, nodes, true); } /// /// Get random color. /// /// Color. protected static Color RandomColor() { return new Color(Random.value, Random.value, Random.value, Random.value); } } }