2020-12-21 21:31:57 +00:00
|
|
|
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
|
|
|
|
{
|
2020-12-27 23:57:23 +00:00
|
|
|
/// <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>
|
2020-12-21 21:31:57 +00:00
|
|
|
public static class IMGUIManager
|
|
|
|
{
|
|
|
|
internal static OnGuiRunner ImguiScheduler = new OnGuiRunner("GamecraftModdingAPI_IMGUIScheduler");
|
|
|
|
|
|
|
|
private static FasterDictionary<string, UIElement> _activeElements = new FasterDictionary<string,UIElement>();
|
|
|
|
|
2020-12-27 23:57:23 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Add an UIElement instance to be managed by IMGUIManager.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="e">The UIElement instance.</param>
|
2020-12-21 21:31:57 +00:00
|
|
|
public static void AddElement(UIElement e)
|
|
|
|
{
|
|
|
|
if (!ExistsElement(e))
|
|
|
|
{
|
|
|
|
_activeElements[e.Name] = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 23:57:23 +00:00
|
|
|
/// <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>
|
2020-12-21 21:31:57 +00:00
|
|
|
public static bool ExistsElement(string name)
|
|
|
|
{
|
|
|
|
return _activeElements.ContainsKey(name);
|
|
|
|
}
|
|
|
|
|
2020-12-27 23:57:23 +00:00
|
|
|
/// <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>
|
2020-12-21 21:31:57 +00:00
|
|
|
public static bool ExistsElement(UIElement element)
|
|
|
|
{
|
|
|
|
return ExistsElement(element.Name);
|
|
|
|
}
|
|
|
|
|
2020-12-27 23:57:23 +00:00
|
|
|
/// <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>
|
2020-12-21 21:31:57 +00:00
|
|
|
public static bool RemoveElement(string name)
|
|
|
|
{
|
|
|
|
if (ExistsElement(name))
|
|
|
|
{
|
|
|
|
return _activeElements.Remove(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-27 23:57:23 +00:00
|
|
|
/// <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>
|
2020-12-21 21:31:57 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|