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.

125 lines
4.4KB

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