mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-22 15:57:44 +01:00
chore: add raii, tables code from OtterGui into new Dalamud.Interface assembly
This commit is contained in:
parent
e0d4e60aad
commit
6bf1376515
22 changed files with 1356 additions and 0 deletions
51
Dalamud.Interface/Raii/Font.cs
Normal file
51
Dalamud.Interface/Raii/Font.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
using ImGuiNET;
|
||||
|
||||
namespace Dalamud.Interface.Raii;
|
||||
|
||||
// Push an arbitrary amount of fonts into an object that are all popped when it is disposed.
|
||||
// If condition is false, no font is pushed.
|
||||
public static partial class ImRaii
|
||||
{
|
||||
public static Font PushFont(ImFontPtr font, bool condition = true)
|
||||
=> condition ? new Font().Push(font) : new Font();
|
||||
|
||||
// Push the default font if any other font is currently pushed.
|
||||
public static Font DefaultFont()
|
||||
=> new Font().Push(Font.DefaultPushed, Font.FontPushCounter > 0);
|
||||
|
||||
public sealed class Font : IDisposable
|
||||
{
|
||||
internal static int FontPushCounter = 0;
|
||||
internal static ImFontPtr DefaultPushed;
|
||||
|
||||
private int _count;
|
||||
|
||||
public Font()
|
||||
=> this._count = 0;
|
||||
|
||||
public Font Push(ImFontPtr font, bool condition = true)
|
||||
{
|
||||
if (condition)
|
||||
{
|
||||
if (FontPushCounter++ == 0)
|
||||
DefaultPushed = ImGui.GetFont();
|
||||
ImGui.PushFont(font);
|
||||
++this._count;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Pop(int num = 1)
|
||||
{
|
||||
num = Math.Min(num, this._count);
|
||||
this._count -= num;
|
||||
FontPushCounter -= num;
|
||||
while (num-- > 0)
|
||||
ImGui.PopFont();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
=> this.Pop(this._count);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue