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 { /// /// Keeps track of UIElement instances. /// This also handles displaying and processing them during the MonoBehaviour.OnGUI phase of screen updates. /// Most of this functionality is handled implicitly by the included UIElement implementations, /// but this is left as a public API so it can be expanded. /// public static class IMGUIManager { internal static OnGuiRunner ImguiScheduler = new OnGuiRunner("GamecraftModdingAPI_IMGUIScheduler"); private static Dictionary _activeElements = new Dictionary(); /// /// Add an UIElement instance to be managed by IMGUIManager. /// /// The UIElement instance. public static void AddElement(UIElement e) { if (!ExistsElement(e)) { _activeElements[e.Name] = e; } } /// /// Determine whether the UIElement instance is already tracked by IMGUIManager. /// /// The UIElement's unique name. /// Whether the UIElement instance is already tracked by IMGUIManager (true) or not (false). public static bool ExistsElement(string name) { return _activeElements.ContainsKey(name); } /// /// Determine whether the UIElement instance is already tracked by IMGUIManager. /// /// The UIElement instance. /// Whether the UIElement instance is already tracked by IMGUIManager (true) or not (false). public static bool ExistsElement(UIElement element) { return ExistsElement(element.Name); } /// /// Remove an UIElement instance from IMGUIManager. /// The UIElement will become invisible and stop generating events. /// /// The UIElement's unique name. /// Whether the UIElement instance existed in IMGUIManager (true) or not (false). public static bool RemoveElement(string name) { if (ExistsElement(name)) { return _activeElements.Remove(name); } return false; } /// /// Remove an UIElement instance from IMGUIManager. /// The UIElement will become invisible and stop generating events. /// /// The UIElement instance. /// Whether the UIElement instance existed in IMGUIManager (true) or not (false). public static bool RemoveElement(UIElement element) { return RemoveElement(element.Name); } private static void OnGUI() { foreach (var element in _activeElements.Values) { if (element.Enabled) element.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 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); } } }