- Add new PushColor overload that checks for nullable Vector4

This commit is contained in:
Infi 2026-02-14 19:52:17 +01:00
parent f593433fcb
commit 8d55dccd75
6 changed files with 33 additions and 70 deletions

View file

@ -45,28 +45,10 @@ public static partial class ImGuiComponents
/// <returns>Indicator if button is clicked.</returns> /// <returns>Indicator if button is clicked.</returns>
public static bool DisabledButton(string labelWithId, Vector4? defaultColor = null, Vector4? activeColor = null, Vector4? hoveredColor = null, float alphaMult = .5f) public static bool DisabledButton(string labelWithId, Vector4? defaultColor = null, Vector4? activeColor = null, Vector4? hoveredColor = null, float alphaMult = .5f)
{ {
using var col = new ImRaii.ColorDisposable(); using var col = ImRaii.PushColor(ImGuiCol.Button, defaultColor)
.Push(ImGuiCol.ButtonActive, activeColor)
if (defaultColor.HasValue) .Push(ImGuiCol.ButtonHovered, hoveredColor);
{ using var style = ImRaii.PushStyle(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * alphaMult);
col.Push(ImGuiCol.Button, defaultColor.Value); return ImGui.Button(labelWithId);
}
if (activeColor.HasValue)
{
col.Push(ImGuiCol.ButtonActive, activeColor.Value);
}
if (hoveredColor.HasValue)
{
col.Push(ImGuiCol.ButtonHovered, hoveredColor.Value);
}
var style = ImGui.GetStyle();
using (ImRaii.PushStyle(ImGuiStyleVar.Alpha, style.Alpha * alphaMult))
{
return ImGui.Button(labelWithId);
}
} }
} }

View file

@ -1,8 +1,8 @@
using System.Numerics;
using Dalamud.Bindings.ImGui; using Dalamud.Bindings.ImGui;
using Dalamud.Interface.Utility.Raii; using Dalamud.Interface.Utility.Raii;
using FFXIVClientStructs.FFXIV.Common.Math;
namespace Dalamud.Interface.Components; namespace Dalamud.Interface.Components;
/// <summary> /// <summary>
@ -24,12 +24,7 @@ public static partial class ImGuiComponents
/// <param name="color">The color of the icon.</param> /// <param name="color">The color of the icon.</param>
public static void HelpMarker(string helpText, FontAwesomeIcon icon, Vector4? color = null) public static void HelpMarker(string helpText, FontAwesomeIcon icon, Vector4? color = null)
{ {
using var col = new ImRaii.ColorDisposable(); using var col = ImRaii.PushColor(ImGuiCol.TextDisabled, color);
if (color.HasValue)
{
col.Push(ImGuiCol.TextDisabled, color.Value);
}
ImGui.SameLine(); ImGui.SameLine();

View file

@ -123,22 +123,9 @@ public static partial class ImGuiComponents
/// <returns>Indicator if button is clicked.</returns> /// <returns>Indicator if button is clicked.</returns>
public static bool IconButton(string iconText, Vector4? defaultColor, Vector4? activeColor = null, Vector4? hoveredColor = null, Vector2? size = null) public static bool IconButton(string iconText, Vector4? defaultColor, Vector4? activeColor = null, Vector4? hoveredColor = null, Vector2? size = null)
{ {
using var col = new ImRaii.ColorDisposable(); using var col = ImRaii.PushColor(ImGuiCol.Button, defaultColor)
.Push(ImGuiCol.ButtonActive, activeColor)
if (defaultColor.HasValue) .Push(ImGuiCol.ButtonHovered, hoveredColor);
{
col.Push(ImGuiCol.Button, defaultColor.Value);
}
if (activeColor.HasValue)
{
col.Push(ImGuiCol.ButtonActive, activeColor.Value);
}
if (hoveredColor.HasValue)
{
col.Push(ImGuiCol.ButtonHovered, hoveredColor.Value);
}
if (size.HasValue) if (size.HasValue)
{ {
@ -203,22 +190,9 @@ public static partial class ImGuiComponents
/// <returns>Indicator if button is clicked.</returns> /// <returns>Indicator if button is clicked.</returns>
public static bool IconButtonWithText(FontAwesomeIcon icon, string text, Vector4? defaultColor = null, Vector4? activeColor = null, Vector4? hoveredColor = null, Vector2? size = null) public static bool IconButtonWithText(FontAwesomeIcon icon, string text, Vector4? defaultColor = null, Vector4? activeColor = null, Vector4? hoveredColor = null, Vector2? size = null)
{ {
using var col = new ImRaii.ColorDisposable(); using var col = ImRaii.PushColor(ImGuiCol.Button, defaultColor)
.Push(ImGuiCol.ButtonActive, activeColor)
if (defaultColor.HasValue) .Push(ImGuiCol.ButtonHovered, hoveredColor);
{
col.Push(ImGuiCol.Button, defaultColor.Value);
}
if (activeColor.HasValue)
{
col.Push(ImGuiCol.ButtonActive, activeColor.Value);
}
if (hoveredColor.HasValue)
{
col.Push(ImGuiCol.ButtonHovered, hoveredColor.Value);
}
if (size.HasValue) if (size.HasValue)
{ {

View file

@ -172,13 +172,8 @@ public static partial class ImGuiHelpers
/// <param name="color">The color of the text.</param> /// <param name="color">The color of the text.</param>
public static void ClickToCopyText(ImU8String text, ImU8String textCopy = default, Vector4? color = null) public static void ClickToCopyText(ImU8String text, ImU8String textCopy = default, Vector4? color = null)
{ {
using (var col = new ImRaii.ColorDisposable()) using (ImRaii.PushColor(ImGuiCol.Text, color))
{ {
if (color.HasValue)
{
col.Push(ImGuiCol.Text, color.Value);
}
ImGui.Text(text.Span); ImGui.Text(text.Span);
} }

View file

@ -26,13 +26,27 @@ public static partial class ImRaii
/// <summary> Push a color to the color stack. </summary> /// <summary> Push a color to the color stack. </summary>
/// <param name="type"> The type of color to change. </param> /// <param name="type"> The type of color to change. </param>
/// <param name="color"> The color to change it to. If this is null/default, no color will be set. </param> /// <param name="color"> The color to change it to. </param>
/// /// <param name="condition"> If this is false, the color is not pushed. </param> /// /// <param name="condition"> If this is false, the color is not pushed. </param>
/// <returns> A disposable object that can be used to push further colors and pops those colors after leaving scope. Use with using. </returns> /// <returns> A disposable object that can be used to push further colors and pops those colors after leaving scope. Use with using. </returns>
/// <remarks> If you need to keep colors pushed longer than the current scope, use without using and use <seealso cref="PopUnsafe"/>. </remarks> /// <remarks> If you need to keep colors pushed longer than the current scope, use without using and use <seealso cref="PopUnsafe"/>. </remarks>
public ColorDisposable Push(ImGuiCol type, Vector4 color, bool condition = true) public ColorDisposable Push(ImGuiCol type, Vector4 color, bool condition = true)
=> condition ? this.InternalPush(type, color) : this; => condition ? this.InternalPush(type, color) : this;
/// <summary> Push a color to the color stack. </summary>
/// <param name="type"> The type of color to change. </param>
/// <param name="color"> The color to change it to. If this is null, no color will be set. </param>
/// /// <param name="condition"> If this is false, the color is not pushed. </param>
/// <returns> A disposable object that can be used to push further colors and pops those colors after leaving scope. Use with using. </returns>
/// <remarks> If you need to keep colors pushed longer than the current scope, use without using and use <seealso cref="PopUnsafe"/>. </remarks>
public ColorDisposable Push(ImGuiCol type, Vector4? color, bool condition = true)
{
if (!color.HasValue)
return this;
return condition ? this.InternalPush(type, color.Value) : this;
}
private ColorDisposable InternalPush(ImGuiCol type, uint color) private ColorDisposable InternalPush(ImGuiCol type, uint color)
{ {
Stack.Add((type, ImGui.GetColorU32(type))); Stack.Add((type, ImGui.GetColorU32(type)));

View file

@ -26,6 +26,9 @@ public static partial class ImRaii
public static ColorDisposable PushColor(ImGuiCol idx, Vector4 color, bool condition = true) public static ColorDisposable PushColor(ImGuiCol idx, Vector4 color, bool condition = true)
=> new ColorDisposable().Push(idx, color, condition); => new ColorDisposable().Push(idx, color, condition);
public static ColorDisposable PushColor(ImGuiCol idx, Vector4? color, bool condition = true)
=> new ColorDisposable().Push(idx, color, condition);
public static ColorDisposable DefaultColors() public static ColorDisposable DefaultColors()
=> ColorDisposable.DefaultColors(); => ColorDisposable.DefaultColors();