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.

94 lines
2.6KB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using GamecraftModdingAPI.App;
  5. using GamecraftModdingAPI.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 GamecraftModdingAPI.Interface.IMGUI
  13. {
  14. public static class IMGUIManager
  15. {
  16. internal static OnGuiRunner ImguiScheduler = new OnGuiRunner("GamecraftModdingAPI_IMGUIScheduler");
  17. private static FasterDictionary<string, UIElement> _activeElements = new FasterDictionary<string,UIElement>();
  18. public static void AddElement(UIElement e)
  19. {
  20. if (!ExistsElement(e))
  21. {
  22. _activeElements[e.Name] = e;
  23. }
  24. }
  25. public static bool ExistsElement(string name)
  26. {
  27. return _activeElements.ContainsKey(name);
  28. }
  29. public static bool ExistsElement(UIElement element)
  30. {
  31. return ExistsElement(element.Name);
  32. }
  33. public static bool RemoveElement(string name)
  34. {
  35. if (ExistsElement(name))
  36. {
  37. return _activeElements.Remove(name);
  38. }
  39. return false;
  40. }
  41. public static bool RemoveElement(UIElement element)
  42. {
  43. return RemoveElement(element.Name);
  44. }
  45. private static void OnGUI()
  46. {
  47. UIElement[] elements = _activeElements.GetValuesArray(out uint count);
  48. for(uint i = 0; i < count; i++)
  49. {
  50. if (elements[i].Enabled)
  51. elements[i].OnGUI();
  52. /*try
  53. {
  54. if (elements[i].Enabled)
  55. elements[i].OnGUI();
  56. }
  57. catch (ArgumentException)
  58. {
  59. // ignore these, since this is (hopefully) just Unity being dumb
  60. }
  61. catch (Exception e)
  62. {
  63. Logging.MetaDebugLog($"Element '{elements[i].Name}' threw exception:\n{e.ToString()}");
  64. }*/
  65. }
  66. }
  67. private static IEnumerator<TaskContract> OnGUIAsync()
  68. {
  69. yield return (new Svelto.Tasks.Enumerators.WaitForSecondsEnumerator(5)).Continue(); // wait for some startup
  70. while (true)
  71. {
  72. yield return Yield.It;
  73. GUI.skin = Constants.Default;
  74. OnGUI();
  75. }
  76. }
  77. internal static void Init()
  78. {
  79. OnGUIAsync().RunOn(ImguiScheduler);
  80. }
  81. }
  82. }