TechbloxModdingAPI/GamecraftModdingAPI/Interface/IMGUI/IMGUIManager.cs

94 lines
2.6 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using GamecraftModdingAPI.App;
using GamecraftModdingAPI.Utility;
using Rewired.Internal;
using Svelto.DataStructures;
using Svelto.Tasks;
using Svelto.Tasks.ExtraLean;
using Svelto.Tasks.ExtraLean.Unity;
using UnityEngine;
namespace GamecraftModdingAPI.Interface.IMGUI
{
public static class IMGUIManager
{
internal static OnGuiRunner ImguiScheduler = new OnGuiRunner("GamecraftModdingAPI_IMGUIScheduler");
private static FasterDictionary<string, UIElement> _activeElements = new FasterDictionary<string,UIElement>();
public static void AddElement(UIElement e)
{
if (!ExistsElement(e))
{
_activeElements[e.Name] = e;
}
}
public static bool ExistsElement(string name)
{
return _activeElements.ContainsKey(name);
}
public static bool ExistsElement(UIElement element)
{
return ExistsElement(element.Name);
}
public static bool RemoveElement(string name)
{
if (ExistsElement(name))
{
return _activeElements.Remove(name);
}
return false;
}
public static bool RemoveElement(UIElement element)
{
return RemoveElement(element.Name);
}
private static void OnGUI()
{
UIElement[] elements = _activeElements.GetValuesArray(out uint count);
for(uint i = 0; i < count; i++)
{
if (elements[i].Enabled)
elements[i].OnGUI();
/*try
{
if (elements[i].Enabled)
elements[i].OnGUI();
}
catch (ArgumentException)
{
// ignore these, since this is (hopefully) just Unity being dumb
}
catch (Exception e)
{
Logging.MetaDebugLog($"Element '{elements[i].Name}' threw exception:\n{e.ToString()}");
}*/
}
}
private static IEnumerator<TaskContract> OnGUIAsync()
{
yield return (new Svelto.Tasks.Enumerators.WaitForSecondsEnumerator(5)).Continue(); // wait for some startup
while (true)
{
yield return Yield.It;
GUI.skin = Constants.Default;
OnGUI();
}
}
internal static void Init()
{
OnGUIAsync().RunOn(ImguiScheduler);
}
}
}