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 { /// /// A radio-like input that uses icon buttons. /// /// The type of the value being set. /// Text that will be used to generate individual labels for the buttons. /// The value to set. /// The icons that will be displayed on each button. /// The options that each button will apply. /// Arranges the buttons in a grid with the given number of columns. 0 = ignored (all buttons drawn in one row). /// Sets the size of all buttons. If either dimension is set to 0, that dimension will conform to the size of the icon. /// The default color of the button range. /// The color of the actively-selected button. /// The color of the buttons when hovered. /// True if any button is clicked. public static bool IconButtonSelect(string label, ref T val, IEnumerable optionIcons, IEnumerable 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(icon, value)); return IconButtonSelect(label, ref val, options, columns, buttonSize, defaultColor, activeColor, hoveredColor); } /// /// A radio-like input that uses icon buttons. /// /// The type of the value being set. /// Text that will be used to generate individual labels for the buttons. /// The value to set. /// A list of all icon/option pairs. /// Arranges the buttons in a grid with the given number of columns. 0 = ignored (all buttons drawn in one row). /// Sets the size of all buttons. If either dimension is set to 0, that dimension will conform to the size of the icon. /// The default color of the button range. /// The color of the actively-selected button. /// The color of the buttons when hovered. /// True if any button is clicked. public static unsafe bool IconButtonSelect(string label, ref T val, IEnumerable> 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; } }