Dalamud/Dalamud/Interface/Components/ImGuiComponents.IconButtonSelect.cs
ItsBexy ee63f60877
Update UIDebug2, ImGuiComponents, ImGuiHelpers (#2081)
* Update ImGuiComponents & ImGuiHelpers
Took some helper functions created for `UiDebug2`, and incorporated them into `ImGuiComponents` / `ImGuiHelpers` instead

- `IconButton()` (and its various overloads) now includes an optional size parameter
- `IconButtonSelect()` has been added, allowing a row or grid of IconButtons to serve as a radio-like input
- `HelpMarker()` now includes an optional color parameter
- `ClickToCopyText()` now includes an optional color parameter, and includes the `FontAwesome.Copy` icon in the tooltip.
- Implemented ImRaii in these files

These changes are intended not to break any existing calls in plugins or within Dalamud itself.

* Fix ambiguous overloads

* UiDebug2 Updates
- Fixed XY coordinate display
- Added AtkValue table to AddonTree display
- Restored old behaviour wherein the Addon display initially only shows the Root node and a collapsed node list
- The above should also fix the Element Selector / Search behaviour, allowing it to scroll correctly to the searched node
- Now displays field offsets for any node/component whose pointer exists in the addon struct
- Tidied up node tree headers by removing memory addresses (they're still readable in the opened tree, of course)
2024-11-14 15:36:27 -08:00

87 lines
4.3 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Dalamud.Interface.Utility;
using ImGuiNET;
namespace Dalamud.Interface.Components;
public static partial class ImGuiComponents
{
/// <summary>
/// A radio-like input that uses icon buttons.
/// </summary>
/// <typeparam name="T">The type of the value being set.</typeparam>
/// <param name="label">Text that will be used to generate individual labels for the buttons.</param>
/// <param name="val">The value to set.</param>
/// <param name="optionIcons">The icons that will be displayed on each button.</param>
/// <param name="optionValues">The options that each button will apply.</param>
/// <param name="columns">Arranges the buttons in a grid with the given number of columns. 0 = ignored (all buttons drawn in one row).</param>
/// <param name="buttonSize">Sets the size of all buttons. If either dimension is set to 0, that dimension will conform to the size of the icon.</param>
/// <param name="defaultColor">The default color of the button range.</param>
/// <param name="activeColor">The color of the actively-selected button.</param>
/// <param name="hoveredColor">The color of the buttons when hovered.</param>
/// <returns>True if any button is clicked.</returns>
internal static bool IconButtonSelect<T>(string label, ref T val, IEnumerable<FontAwesomeIcon> optionIcons, IEnumerable<T> optionValues, uint columns = 0, Vector2? buttonSize = null, Vector4? defaultColor = null, Vector4? activeColor = null, Vector4? hoveredColor = null)
{
var options = optionIcons.Zip(optionValues, static (icon, value) => new KeyValuePair<FontAwesomeIcon, T>(icon, value));
return IconButtonSelect(label, ref val, options, columns, buttonSize, defaultColor, activeColor, hoveredColor);
}
/// <summary>
/// A radio-like input that uses icon buttons.
/// </summary>
/// <typeparam name="T">The type of the value being set.</typeparam>
/// <param name="label">Text that will be used to generate individual labels for the buttons.</param>
/// <param name="val">The value to set.</param>
/// <param name="options">A list of all icon/option pairs.</param>
/// <param name="columns">Arranges the buttons in a grid with the given number of columns. 0 = ignored (all buttons drawn in one row).</param>
/// <param name="buttonSize">Sets the size of all buttons. If either dimension is set to 0, that dimension will conform to the size of the icon.</param>
/// <param name="defaultColor">The default color of the button range.</param>
/// <param name="activeColor">The color of the actively-selected button.</param>
/// <param name="hoveredColor">The color of the buttons when hovered.</param>
/// <returns>True if any button is clicked.</returns>
internal static unsafe bool IconButtonSelect<T>(string label, ref T val, IEnumerable<KeyValuePair<FontAwesomeIcon, T>> options, uint columns = 0, Vector2? buttonSize = null, Vector4? defaultColor = null, Vector4? activeColor = null, Vector4? hoveredColor = null)
{
defaultColor ??= *ImGui.GetStyleColorVec4(ImGuiCol.Button);
activeColor ??= *ImGui.GetStyleColorVec4(ImGuiCol.ButtonActive);
hoveredColor ??= *ImGui.GetStyleColorVec4(ImGuiCol.ButtonHovered);
var result = false;
var innerSpacing = ImGui.GetStyle().ItemInnerSpacing;
var y = ImGui.GetCursorPosY();
var optArr = options.ToArray();
for (var i = 0; i < optArr.Length; i++)
{
if (i > 0)
{
if (columns == 0 || i % columns != 0)
{
ImGui.SameLine(0, innerSpacing.X);
}
else
{
y += (buttonSize is { Y: not 0 } ? buttonSize.Value.Y * ImGuiHelpers.GlobalScale : ImGui.GetFrameHeight()) + innerSpacing.Y;
ImGui.SetCursorPosY(y);
}
}
optArr[i].Deconstruct(out var icon, out var option);
var selected = val is not null && val.Equals(option);
if (IconButton($"{label}{option}{i}", icon, selected ? activeColor : defaultColor, activeColor, hoveredColor, buttonSize))
{
val = option;
result = true;
}
}
return result;
}
}