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.

120 lines
4.2KB

  1. using System.Collections.Generic;
  2. using Svelto.Tasks;
  3. using Svelto.Tasks.ExtraLean;
  4. using TechbloxModdingAPI.Tasks;
  5. using TechbloxModdingAPI.Utility;
  6. using UnityEngine;
  7. namespace TechbloxModdingAPI.Interface.IMGUI
  8. {
  9. /// <summary>
  10. /// Keeps track of UIElement instances.
  11. /// This also handles displaying and processing them during the MonoBehaviour.OnGUI phase of screen updates.
  12. /// Most of this functionality is handled implicitly by the included UIElement implementations,
  13. /// but this is left as a public API so it can be expanded.
  14. /// </summary>
  15. public static class IMGUIManager
  16. {
  17. internal static OnGuiRunner ImguiScheduler = new("TechbloxModdingAPI_IMGUIScheduler");
  18. private static readonly WeakDictionary<string, UIElement> _activeElements = new();
  19. /// <summary>
  20. /// Add an UIElement instance to be managed by IMGUIManager.
  21. /// </summary>
  22. /// <param name="e">The UIElement instance.</param>
  23. public static void AddElement(UIElement e)
  24. {
  25. if (!ExistsElement(e))
  26. {
  27. _activeElements[e.Name] = e;
  28. }
  29. }
  30. /// <summary>
  31. /// Determine whether the UIElement instance is already tracked by IMGUIManager.
  32. /// </summary>
  33. /// <param name="name">The UIElement's unique name.</param>
  34. /// <returns>Whether the UIElement instance is already tracked by IMGUIManager (true) or not (false).</returns>
  35. public static bool ExistsElement(string name)
  36. {
  37. return _activeElements.ContainsKey(name);
  38. }
  39. /// <summary>
  40. /// Determine whether the UIElement instance is already tracked by IMGUIManager.
  41. /// </summary>
  42. /// <param name="element">The UIElement instance.</param>
  43. /// <returns>Whether the UIElement instance is already tracked by IMGUIManager (true) or not (false).</returns>
  44. public static bool ExistsElement(UIElement element)
  45. {
  46. return ExistsElement(element.Name);
  47. }
  48. /// <summary>
  49. /// Remove an UIElement instance from IMGUIManager.
  50. /// The UIElement will become invisible and stop generating events.
  51. /// </summary>
  52. /// <param name="name">The UIElement's unique name.</param>
  53. /// <returns>Whether the UIElement instance existed in IMGUIManager (true) or not (false).</returns>
  54. public static bool RemoveElement(string name)
  55. {
  56. if (ExistsElement(name))
  57. {
  58. return _activeElements.Remove(name);
  59. }
  60. return false;
  61. }
  62. /// <summary>
  63. /// Remove an UIElement instance from IMGUIManager.
  64. /// The UIElement will become invisible and stop generating events.
  65. /// </summary>
  66. /// <param name="element">The UIElement instance.</param>
  67. /// <returns>Whether the UIElement instance existed in IMGUIManager (true) or not (false).</returns>
  68. public static bool RemoveElement(UIElement element)
  69. {
  70. return RemoveElement(element.Name);
  71. }
  72. private static void OnGUI()
  73. {
  74. foreach (var element in _activeElements.Values)
  75. {
  76. if (element.Enabled)
  77. element.OnGUI();
  78. /*try
  79. {
  80. if (elements[i].Enabled)
  81. elements[i].OnGUI();
  82. }
  83. catch (ArgumentException)
  84. {
  85. // ignore these, since this is (hopefully) just Unity being dumb
  86. }
  87. catch (Exception e)
  88. {
  89. Logging.MetaDebugLog($"Element '{elements[i].Name}' threw exception:\n{e.ToString()}");
  90. }*/
  91. }
  92. }
  93. private static IEnumerator<TaskContract> OnGUIAsync()
  94. {
  95. yield return (new Svelto.Tasks.Enumerators.WaitForSecondsEnumerator(5)).Continue(); // wait for some startup
  96. while (true)
  97. {
  98. yield return Yield.It;
  99. GUI.skin = Constants.Default;
  100. OnGUI();
  101. }
  102. }
  103. internal static void Init()
  104. {
  105. OnGUIAsync().RunOn(ImguiScheduler);
  106. }
  107. }
  108. }