A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.6KB

  1. using System;
  2. using TechbloxModdingAPI.Utility;
  3. using Svelto.DataStructures;
  4. using UnityEngine;
  5. namespace TechbloxModdingAPI.Interface.IMGUI
  6. {
  7. /// <summary>
  8. /// A group of elements.
  9. /// This wraps Unity's GUILayout Area and GUI Group system.
  10. /// </summary>
  11. public class Group : UIElement
  12. {
  13. private bool automaticLayout;
  14. private FasterList<UIElement> elements = new FasterList<UIElement>();
  15. /// <summary>
  16. /// The rectangular area in the window that the UI group can use
  17. /// </summary>
  18. public Rect Box { get; set; }
  19. public override void OnGUI()
  20. {
  21. /*if (Constants.Default == null) return;
  22. if (Constants.Default.box == null) return;*/
  23. GUIStyle guiStyle = Constants.Default.box;
  24. UIElement[] elems = elements.ToArrayFast(out int count);
  25. if (automaticLayout)
  26. {
  27. GUILayout.BeginArea(Box, guiStyle);
  28. for (uint i = 0; i < count; i++)
  29. {
  30. /*try
  31. {
  32. if (elems[i].Enabled)
  33. elems[i].OnGUI();
  34. }
  35. catch (ArgumentException)
  36. {
  37. // ignore these, since this is (hopefully) just Unity being dumb
  38. }
  39. catch (Exception e)
  40. {
  41. Logging.MetaDebugLog($"Element '{elems[i].Name}' threw exception:\n{e.ToString()}");
  42. }*/
  43. if (elems[i].Enabled)
  44. elems[i].OnGUI();
  45. }
  46. GUILayout.EndArea();
  47. }
  48. else
  49. {
  50. GUI.BeginGroup(Box, guiStyle);
  51. for (uint i = 0; i < count; i++)
  52. {
  53. if (elems[i].Enabled)
  54. elems[i].OnGUI();
  55. }
  56. GUI.EndGroup();
  57. }
  58. }
  59. /// <summary>
  60. /// The amount of elements in the group.
  61. /// </summary>
  62. public int Length
  63. {
  64. get => elements.count;
  65. }
  66. /// <summary>
  67. /// Initializes a new instance of the <see cref="T:TechbloxModdingAPI.Interface.IMGUI.Group"/> class.
  68. /// </summary>
  69. /// <param name="box">The rectangular area to use in the window.</param>
  70. /// <param name="name">Name of the group.</param>
  71. /// <param name="automaticLayout">Whether to use automatic UI layout.</param>
  72. public Group(Rect box, string name = null, bool automaticLayout = false) : base(box.ToString().Replace(" ", ""), name)
  73. {
  74. Box = box;
  75. this.automaticLayout = automaticLayout;
  76. }
  77. /// <summary>
  78. /// Add an element to the group.
  79. /// </summary>
  80. /// <param name="element">The element to add.</param>
  81. /// <returns>Index of the new element.</returns>
  82. public int AddElement(UIElement element)
  83. {
  84. IMGUIManager.RemoveElement(element); // groups manage internal elements themselves
  85. elements.Add(element);
  86. return elements.count - 1;
  87. }
  88. /// <summary>
  89. /// Remove an element from the group.
  90. /// </summary>
  91. /// <param name="element">The element to remove.</param>
  92. /// <returns>Whether removal was successful.</returns>
  93. public bool RemoveElement(UIElement element)
  94. {
  95. int index = IndexOf(element);
  96. return RemoveAt(index);
  97. }
  98. /// <summary>
  99. /// Remove the element in a specific location.
  100. /// </summary>
  101. /// <param name="index">Index of the element.</param>
  102. /// <returns>Whether removal was successful.</returns>
  103. public bool RemoveAt(int index)
  104. {
  105. if (index < 0 || index >= elements.count) return false;
  106. IMGUIManager.AddElement(elements[index]); // re-add to global manager
  107. elements.RemoveAt((uint) index);
  108. return true;
  109. }
  110. /// <summary>
  111. /// Get the index of an element.
  112. /// </summary>
  113. /// <param name="element">The element to search for.</param>
  114. /// <returns>The element's index, or -1 if not found.</returns>
  115. public int IndexOf(UIElement element)
  116. {
  117. UIElement[] elems = elements.ToArrayFast(out int count);
  118. for (int i = 0; i < count; i++)
  119. {
  120. if (elems[i].Name == element.Name)
  121. {
  122. return i;
  123. }
  124. }
  125. return -1;
  126. }
  127. }
  128. }