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.

Group.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using GamecraftModdingAPI.Utility;
  3. using Svelto.DataStructures;
  4. using UnityEngine;
  5. namespace GamecraftModdingAPI.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 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 uint 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 group's unique name.
  61. /// </summary>
  62. public string Name { get; }
  63. /// <summary>
  64. /// Whether to display the group and everything in it.
  65. /// </summary>
  66. public bool Enabled { set; get; } = true;
  67. /// <summary>
  68. /// The amount of elements in the group.
  69. /// </summary>
  70. public int Length
  71. {
  72. get => elements.count;
  73. }
  74. /// <summary>
  75. /// Initializes a new instance of the <see cref="T:GamecraftModdingAPI.Interface.IMGUI.Group"/> class.
  76. /// </summary>
  77. /// <param name="box">The rectangular area to use in the window.</param>
  78. /// <param name="name">Name of the group.</param>
  79. /// <param name="automaticLayout">Whether to use automatic UI layout.</param>
  80. public Group(Rect box, string name = null, bool automaticLayout = false)
  81. {
  82. Box = box;
  83. if (name == null)
  84. {
  85. this.Name = typeof(Group).FullName + "::" + box.ToString().Replace(" ", "");
  86. }
  87. else
  88. {
  89. this.Name = name;
  90. }
  91. this.automaticLayout = automaticLayout;
  92. IMGUIManager.AddElement(this);
  93. }
  94. /// <summary>
  95. /// Add an element to the group.
  96. /// </summary>
  97. /// <param name="element">The element to add.</param>
  98. /// <returns>Index of the new element.</returns>
  99. public int AddElement(UIElement element)
  100. {
  101. IMGUIManager.RemoveElement(element); // groups manage internal elements themselves
  102. elements.Add(element);
  103. return elements.count - 1;
  104. }
  105. /// <summary>
  106. /// Remove an element from the group.
  107. /// </summary>
  108. /// <param name="element">The element to remove.</param>
  109. /// <returns>Whether removal was successful.</returns>
  110. public bool RemoveElement(UIElement element)
  111. {
  112. int index = IndexOf(element);
  113. return RemoveAt(index);
  114. }
  115. /// <summary>
  116. /// Remove the element in a specific location.
  117. /// </summary>
  118. /// <param name="index">Index of the element.</param>
  119. /// <returns>Whether removal was successful.</returns>
  120. public bool RemoveAt(int index)
  121. {
  122. if (index < 0 || index >= elements.count) return false;
  123. IMGUIManager.AddElement(elements[index]); // re-add to global manager
  124. elements.RemoveAt(index);
  125. return true;
  126. }
  127. /// <summary>
  128. /// Get the index of an element.
  129. /// </summary>
  130. /// <param name="element">The element to search for.</param>
  131. /// <returns>The element's index, or -1 if not found.</returns>
  132. public int IndexOf(UIElement element)
  133. {
  134. UIElement[] elems = elements.ToArrayFast(out uint count);
  135. for (int i = 0; i < count; i++)
  136. {
  137. if (elems[i].Name == element.Name)
  138. {
  139. return i;
  140. }
  141. }
  142. return -1;
  143. }
  144. }
  145. }