Screenie/Ui/PluginUi.cs

100 lines
2.6 KiB
C#

using System.Numerics;
using ImGuiNET;
using Screenie.Ui.Tabs;
using Screenie.Util;
namespace Screenie.Ui;
internal class PluginUi : IDisposable {
private Plugin Plugin { get; }
private List<ITab> Tabs { get; } = [];
internal List<IDrawable> Drawables { get; } = [];
private List<IDrawable> ToDispose { get; } = [];
internal bool Visible;
internal PluginUi(Plugin plugin) {
this.Plugin = plugin;
this.Tabs.AddRange([
new SettingsTab(this.Plugin),
new DatabaseTab(this.Plugin),
]);
this.Plugin.Interface.UiBuilder.Draw += this.Draw;
this.Plugin.Interface.UiBuilder.OpenConfigUi += this.OpenConfigUi;
}
public void Dispose() {
this.Plugin.Interface.UiBuilder.OpenConfigUi -= this.OpenConfigUi;
this.Plugin.Interface.UiBuilder.Draw -= this.Draw;
foreach (var tab in this.Tabs) {
tab.Dispose();
}
}
private void OpenConfigUi() {
this.Visible = true;
}
private void Draw() {
foreach (var drawable in this.ToDispose) {
try {
drawable.Dispose();
} catch (Exception ex) {
Plugin.Log.Error(ex, "Failed to dispose drawable");
}
}
this.ToDispose.Clear();
this.Drawables.RemoveAll(drawable => {
// return true to stop drawing
try {
if (drawable.Draw() == DrawStatus.Finished) {
this.ToDispose.Add(drawable);
return true;
}
return false;
} catch (Exception ex) {
Plugin.Log.Error(ex, "Error drawing drawable");
return true;
}
});
if (!this.Visible) {
return;
}
using var end = new OnDispose(ImGui.End);
ImGui.SetNextWindowSize(new Vector2(420, 500), ImGuiCond.FirstUseEver);
if (!ImGui.Begin(Plugin.Name, ref this.Visible)) {
return;
}
ImGui.PushTextWrapPos();
using var popTextWrapPos = new OnDispose(ImGui.PopTextWrapPos);
if (!ImGui.BeginTabBar("##screenie-tabbar")) {
return;
}
using var endTabBar = new OnDispose(ImGui.EndTabBar);
foreach (var tab in this.Tabs) {
if (!ImGui.BeginTabItem(tab.Label)) {
continue;
}
using var endTabItem = new OnDispose(ImGui.EndTabItem);
try {
tab.Draw();
} catch (Exception ex) {
Plugin.Log.Error(ex, $"Error drawing tab \"{tab.Label}\"");
}
}
}
}