Allow empty text and hashing for GroupPanel

This commit is contained in:
Asriel Camora
2023-10-25 12:43:09 -07:00
parent a81b6b3b1c
commit 281b0d27a2
+43 -20
View File
@@ -24,6 +24,8 @@ internal static class ImGuiUtils
// ^ only useful if width = -1 // ^ only useful if width = -1
public static float BeginGroupPanel(string name, float width) public static float BeginGroupPanel(string name, float width)
{ {
ImGui.PushID(name);
// container group // container group
ImGui.BeginGroup(); ImGui.BeginGroup();
@@ -43,14 +45,22 @@ internal static class ImGuiUtils
// label group // label group
ImGui.BeginGroup(); ImGui.BeginGroup();
ImGui.Dummy(new Vector2(frameHeight / 2, 0)); // shifts text by fh/2 if (ImGui.CalcTextSize(name, true).X == 0)
ImGui.SameLine(0, 0); {
var textFrameHeight = ImGui.GetFrameHeight(); GroupPanelLabelStack.Push(default);
ImGui.AlignTextToFramePadding(); ImGui.Dummy(new Vector2(0f, itemSpacing.Y)); // shifts content by is.y
ImGui.TextUnformatted(name); }
GroupPanelLabelStack.Push((ImGui.GetItemRectMin(), ImGui.GetItemRectMax(), textFrameHeight / 2f)); // push rect to stack else
ImGui.SameLine(0, 0); {
ImGui.Dummy(new Vector2(0f, textFrameHeight + itemSpacing.Y)); // shifts content by fh + is.y ImGui.Dummy(new Vector2(frameHeight / 2, 0)); // shifts text by fh/2
ImGui.SameLine(0, 0);
var textFrameHeight = ImGui.GetFrameHeight();
ImGui.AlignTextToFramePadding();
ImGui.Text(name);
GroupPanelLabelStack.Push((ImGui.GetItemRectMin(), ImGui.GetItemRectMax(), textFrameHeight / 2f)); // push rect to stack
ImGui.SameLine(0, 0);
ImGui.Dummy(new Vector2(0f, textFrameHeight + itemSpacing.Y)); // shifts content by fh + is.y
}
// content group // content group
ImGui.BeginGroup(); ImGui.BeginGroup();
@@ -80,28 +90,33 @@ internal static class ImGuiUtils
// inner group // inner group
ImGui.EndGroup(); ImGui.EndGroup();
var labelRect = GroupPanelLabelStack.Pop(); var (labelMin, labelMax, labelPadding) = GroupPanelLabelStack.Pop();
var innerMin = ImGui.GetItemRectMin() + new Vector2(0, labelRect.TopPadding);
var innerMax = ImGui.GetItemRectMax();
(Vector2 Min, Vector2 Max) frameRect = (innerMin, innerMax); var innerMin = ImGui.GetItemRectMin();
// add itemspacing padding on the label's sides var innerMax = ImGui.GetItemRectMax();
labelRect.Min.X -= itemSpacing.X / 2; // If there was actual text
labelRect.Max.X += itemSpacing.X / 2; if (labelMax.X != labelMin.X)
{
innerMin += new Vector2(0, labelPadding);
// add itemspacing padding on the label's sides
labelMin.X -= itemSpacing.X / 2;
labelMax.X += itemSpacing.X / 2;
}
for (var i = 0; i < 4; ++i) for (var i = 0; i < 4; ++i)
{ {
var (minClip, maxClip) = i switch var (minClip, maxClip) = i switch
{ {
0 => (new Vector2(float.NegativeInfinity), new Vector2(labelRect.Min.X, float.PositiveInfinity)), 0 => (new Vector2(float.NegativeInfinity), new Vector2(labelMin.X, float.PositiveInfinity)),
1 => (new Vector2(labelRect.Max.X, float.NegativeInfinity), new Vector2(float.PositiveInfinity)), 1 => (new Vector2(labelMax.X, float.NegativeInfinity), new Vector2(float.PositiveInfinity)),
2 => (new Vector2(labelRect.Min.X, float.NegativeInfinity), new Vector2(labelRect.Max.X, labelRect.Min.Y)), 2 => (new Vector2(labelMin.X, float.NegativeInfinity), new Vector2(labelMax.X, labelMin.Y)),
3 => (new Vector2(labelRect.Min.X, labelRect.Max.Y), new Vector2(labelRect.Max.X, float.PositiveInfinity)), 3 => (new Vector2(labelMin.X, labelMax.Y), new Vector2(labelMax.X, float.PositiveInfinity)),
_ => (Vector2.Zero, Vector2.Zero) _ => (Vector2.Zero, Vector2.Zero)
}; };
ImGui.PushClipRect(minClip, maxClip, true); ImGui.PushClipRect(minClip, maxClip, true);
ImGui.GetWindowDrawList().AddRect( ImGui.GetWindowDrawList().AddRect(
frameRect.Min, frameRect.Max, innerMin, innerMax,
ImGui.GetColorU32(ImGuiCol.Border), ImGui.GetColorU32(ImGuiCol.Border),
itemSpacing.X); itemSpacing.X);
ImGui.PopClipRect(); ImGui.PopClipRect();
@@ -111,6 +126,8 @@ internal static class ImGuiUtils
} }
ImGui.EndGroup(); ImGui.EndGroup();
ImGui.PopID();
} }
private struct EndUnconditionally : ImRaii.IEndObject, IDisposable private struct EndUnconditionally : ImRaii.IEndObject, IDisposable
@@ -444,6 +461,12 @@ internal static class ImGuiUtils
} }
} }
public static bool InputTextMultilineWithHint(string label, string hint, ref string input, int maxLength, Vector2 size, ImGuiInputTextFlags flags = ImGuiInputTextFlags.None, ImGuiInputTextCallback? callback = null, IntPtr user_data = default)
{
const ImGuiInputTextFlags Multiline = (ImGuiInputTextFlags)(1 << 26);
return ImGuiExtras.InputTextEx(label, hint, ref input, maxLength, size, flags | Multiline, callback, user_data);
}
public static bool IconButtonSized(FontAwesomeIcon icon, Vector2 size) public static bool IconButtonSized(FontAwesomeIcon icon, Vector2 size)
{ {
using var font = ImRaii.PushFont(UiBuilder.IconFont); using var font = ImRaii.PushFont(UiBuilder.IconFont);