TechbloxModdingAPI/GamecraftModdingAPI/Interface/IMGUI/IMGUIManager.cs

125 lines
4.4 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
{
/// <summary>
/// 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.
/// </summary>
public static class IMGUIManager
{
internal static OnGuiRunner ImguiScheduler = new OnGuiRunner("GamecraftModdingAPI_IMGUIScheduler");
private static Dictionary<string, UIElement> _activeElements = new Dictionary<string,UIElement>();
/// <summary>
/// Add an UIElement instance to be managed by IMGUIManager.
/// </summary>
/// <param name="e">The UIElement instance.</param>
public static void AddElement(UIElement e)
{
if (!ExistsElement(e))
{
_activeElements[e.Name] = e;
}
}
/// <summary>
/// Determine whether the UIElement instance is already tracked by IMGUIManager.
/// </summary>
/// <param name="name">The UIElement's unique name.</param>
/// <returns>Whether the UIElement instance is already tracked by IMGUIManager (true) or not (false).</returns>
public static bool ExistsElement(string name)
{
return _activeElements.ContainsKey(name);
}
/// <summary>
/// Determine whether the UIElement instance is already tracked by IMGUIManager.
/// </summary>
/// <param name="element">The UIElement instance.</param>
/// <returns>Whether the UIElement instance is already tracked by IMGUIManager (true) or not (false).</returns>
public static bool ExistsElement(UIElement element)
{
return ExistsElement(element.Name);
}
/// <summary>
/// Remove an UIElement instance from IMGUIManager.
/// The UIElement will become invisible and stop generating events.
/// </summary>
/// <param name="name">The UIElement's unique name.</param>
/// <returns>Whether the UIElement instance existed in IMGUIManager (true) or not (false).</returns>
public static bool RemoveElement(string name)
{
if (ExistsElement(name))
{
return _activeElements.Remove(name);
}
return false;
}
/// <summary>
/// Remove an UIElement instance from IMGUIManager.
/// The UIElement will become invisible and stop generating events.
/// </summary>
/// <param name="element">The UIElement instance.</param>
/// <returns>Whether the UIElement instance existed in IMGUIManager (true) or not (false).</returns>
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<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);
}
}
}