mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
* Add ReadOnlySpan<byte> ImRaii overloads * Add ReadOnlySpan<byte> ImGuiHelpers overloads
80 lines
1.9 KiB
C#
80 lines
1.9 KiB
C#
using Dalamud.Bindings.ImGui;
|
|
|
|
namespace Dalamud.Interface.Utility.Raii;
|
|
|
|
// Push an arbitrary amount of ids into an object that are all popped when it is disposed.
|
|
// If condition is false, no id is pushed.
|
|
public static partial class ImRaii
|
|
{
|
|
public static Id PushId(string id, bool enabled = true)
|
|
=> enabled ? new Id().Push(id) : new Id();
|
|
|
|
public static Id PushId(ReadOnlySpan<byte> id, bool enabled = true)
|
|
=> enabled ? new Id().Push(id) : new Id();
|
|
|
|
public static Id PushId(int id, bool enabled = true)
|
|
=> enabled ? new Id().Push(id) : new Id();
|
|
|
|
public static Id PushId(IntPtr id, bool enabled = true)
|
|
=> enabled ? new Id().Push(id) : new Id();
|
|
|
|
public sealed class Id : IDisposable
|
|
{
|
|
private int count;
|
|
|
|
public Id Push(string id, bool condition = true)
|
|
{
|
|
if (condition)
|
|
{
|
|
ImGui.PushID(id);
|
|
++this.count;
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
public Id Push(ReadOnlySpan<byte> id, bool condition = true)
|
|
{
|
|
if (condition)
|
|
{
|
|
ImGui.PushID(id);
|
|
++this.count;
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
public Id Push(int id, bool condition = true)
|
|
{
|
|
if (condition)
|
|
{
|
|
ImGui.PushID(id);
|
|
++this.count;
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
public unsafe Id Push(IntPtr id, bool condition = true)
|
|
{
|
|
if (condition)
|
|
{
|
|
ImGui.PushID(id.ToPointer());
|
|
++this.count;
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
public void Pop(int num = 1)
|
|
{
|
|
num = Math.Min(num, this.count);
|
|
this.count -= num;
|
|
while (num-- > 0)
|
|
ImGui.PopID();
|
|
}
|
|
|
|
public void Dispose()
|
|
=> this.Pop(this.count);
|
|
}
|
|
}
|