shoko/Gui.cs

135 lines
3.3 KiB
C#

using System.Numerics;
using ImGuiNET;
namespace Shoko;
/// <summary>
/// This class mainly wraps ImGui calls to use lambdas instead of BeginX/EndX, or those that gives out a boolean as a return parameter
/// </summary>
static class Gui
{
public static void Window(ReadOnlySpan<char> name, Action f)
{
if(ImGui.Begin(name))
{
f();
}
ImGui.End();
}
public static void Window(ReadOnlySpan<char> name, ImGuiWindowFlags flags, Action f)
{
if(ImGui.Begin(name, flags))
{
f();
}
ImGui.End();
}
public static void Window(ReadOnlySpan<char> name, ref bool open, Action f)
{
if(ImGui.Begin(name, ref open))
{
f();
}
ImGui.End();
}
public static void Window(ReadOnlySpan<char> name, ref bool open, ImGuiWindowFlags flags, Action f)
{
if(ImGui.Begin(name, ref open, flags))
{
f();
}
ImGui.End();
}
public static void Menu(ReadOnlySpan<char> label, Action f)
{
if(ImGui.BeginMenu(label))
{
f();
ImGui.EndMenu();
}
}
public static void MainMenuBar(Action f)
{
if(ImGui.BeginMainMenuBar())
{
f();
ImGui.EndMainMenuBar();
}
}
public static void MenuItem(ReadOnlySpan<char> name, ReadOnlySpan<char> shortcut, Action f)
{
bool result = false;
ImGui.MenuItem(name, shortcut, ref result);
if(result) f();
}
public static void Button(ReadOnlySpan<char> label, Action f)
{
if(ImGui.Button(label)) f();
}
public static void MenuBar(Action f)
{
if(ImGui.BeginMenuBar())
{
f();
ImGui.EndMenuBar();
}
}
public static void TreeNode(ReadOnlySpan<char> label, Action f)
{
if(ImGui.TreeNode(label))
{
f();
ImGui.TreePop();
}
}
public static void Popup(ReadOnlySpan<char> str_id, Action f)
{
if(ImGui.BeginPopup(str_id))
{
f();
ImGui.EndPopup();
}
}
public static void Popup(ReadOnlySpan<char> str_id, ImGuiWindowFlags flags, Action f)
{
if(ImGui.BeginPopup(str_id, flags))
{
f();
ImGui.EndPopup();
}
}
public static void PopupModal(ReadOnlySpan<char> name, Action f)
{
if(ImGui.BeginPopupModal(name))
{
f();
ImGui.EndPopup();
}
}
public static void Text(ReadOnlySpan<char> text)
{
ImGui.TextUnformatted(text);
}
public static void Link(ReadOnlySpan<char> text, ReadOnlySpan<char> tip, Action clicked)
{
ImGui.TextColored(new Vector4(0,0,255,255), text);
ImGui.SetItemTooltip(tip);
if(ImGui.IsItemHovered())
ImGui.SetMouseCursor(ImGuiMouseCursor.Hand);
if(ImGui.IsItemClicked(ImGuiMouseButton.Left)) clicked();
}
public static void Font(ImFontPtr font, Action f)
{
ImGui.PushFont(font);
f();
ImGui.PopFont();
}
public static void Table(ReadOnlySpan<char> id, int column, Action f)
{
if(ImGui.BeginTable(id, column))
{
f();
ImGui.EndTable();
}
}
}