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.

119 lines
4.2KB

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