|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System;
- using GamecraftModdingAPI.Utility;
- using Svelto.DataStructures;
- using UnityEngine;
-
- namespace GamecraftModdingAPI.Interface.IMGUI
- {
- /// <summary>
- /// A group of elements.
- /// This wraps Unity's GUILayout Area and GUI Group system.
- /// </summary>
- public class Group : UIElement
- {
- private bool automaticLayout;
-
- private FasterList<UIElement> elements = new FasterList<UIElement>();
-
- /// <summary>
- /// The rectangular area in the window that the UI group can use
- /// </summary>
- public Rect Box { get; set; }
-
- public void OnGUI()
- {
- /*if (Constants.Default == null) return;
- if (Constants.Default.box == null) return;*/
- GUIStyle guiStyle = Constants.Default.box;
- UIElement[] elems = elements.ToArrayFast(out uint count);
- if (automaticLayout)
- {
- GUILayout.BeginArea(Box, guiStyle);
- for (uint i = 0; i < count; i++)
- {
- /*try
- {
- if (elems[i].Enabled)
- elems[i].OnGUI();
- }
- catch (ArgumentException)
- {
- // ignore these, since this is (hopefully) just Unity being dumb
- }
- catch (Exception e)
- {
- Logging.MetaDebugLog($"Element '{elems[i].Name}' threw exception:\n{e.ToString()}");
- }*/
- if (elems[i].Enabled)
- elems[i].OnGUI();
- }
- GUILayout.EndArea();
- }
- else
- {
- GUI.BeginGroup(Box, guiStyle);
- for (uint i = 0; i < count; i++)
- {
- if (elems[i].Enabled)
- elems[i].OnGUI();
- }
- GUI.EndGroup();
- }
- }
-
- /// <summary>
- /// The group's unique name.
- /// </summary>
- public string Name { get; }
-
- /// <summary>
- /// Whether to display the group and everything in it.
- /// </summary>
- public bool Enabled { set; get; } = true;
-
- /// <summary>
- /// The amount of elements in the group.
- /// </summary>
- public int Length
- {
- get => elements.count;
- }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="T:GamecraftModdingAPI.Interface.IMGUI.Group"/> class.
- /// </summary>
- /// <param name="box">The rectangular area to use in the window.</param>
- /// <param name="name">Name of the group.</param>
- /// <param name="automaticLayout">Whether to use automatic UI layout.</param>
- public Group(Rect box, string name = null, bool automaticLayout = false)
- {
- Box = box;
- if (name == null)
- {
- this.Name = typeof(Group).FullName + "::" + box.ToString().Replace(" ", "");
- }
- else
- {
- this.Name = name;
- }
- this.automaticLayout = automaticLayout;
- IMGUIManager.AddElement(this);
- }
-
- /// <summary>
- /// Add an element to the group.
- /// </summary>
- /// <param name="element">The element to add.</param>
- /// <returns>Index of the new element.</returns>
- public int AddElement(UIElement element)
- {
- IMGUIManager.RemoveElement(element); // groups manage internal elements themselves
- elements.Add(element);
- return elements.count - 1;
- }
-
- /// <summary>
- /// Remove an element from the group.
- /// </summary>
- /// <param name="element">The element to remove.</param>
- /// <returns>Whether removal was successful.</returns>
- public bool RemoveElement(UIElement element)
- {
- int index = IndexOf(element);
- return RemoveAt(index);
- }
-
- /// <summary>
- /// Remove the element in a specific location.
- /// </summary>
- /// <param name="index">Index of the element.</param>
- /// <returns>Whether removal was successful.</returns>
- public bool RemoveAt(int index)
- {
- if (index < 0 || index >= elements.count) return false;
- IMGUIManager.AddElement(elements[index]); // re-add to global manager
- elements.RemoveAt(index);
- return true;
- }
-
- /// <summary>
- /// Get the index of an element.
- /// </summary>
- /// <param name="element">The element to search for.</param>
- /// <returns>The element's index, or -1 if not found.</returns>
- public int IndexOf(UIElement element)
- {
- UIElement[] elems = elements.ToArrayFast(out uint count);
- for (int i = 0; i < count; i++)
- {
- if (elems[i].Name == element.Name)
- {
- return i;
- }
- }
- return -1;
- }
- }
- }
|