feat: ImGuiHelpers.Center helpers, toggle switch component

This commit is contained in:
goat 2022-07-16 00:18:34 +02:00
parent 46c2511c84
commit 30b1a4d383
No known key found for this signature in database
GPG key ID: 7773BB5B43BA52E5
2 changed files with 102 additions and 0 deletions

View file

@ -0,0 +1,72 @@
using System.Numerics;
using ImGuiNET;
namespace Dalamud.Interface.Components
{
/// <summary>
/// Component for toggle buttons.
/// </summary>
public static partial class ImGuiComponents
{
/// <summary>
/// Draw a toggle button.
/// </summary>
/// <param name="id">The id of the button.</param>
/// <param name="v">The state of the switch.</param>
/// <returns>If the button has been interacted with this frame.</returns>
public static bool ToggleButton(string id, ref bool v)
{
var colors = ImGui.GetStyle().Colors;
var p = ImGui.GetCursorScreenPos();
var drawList = ImGui.GetWindowDrawList();
var height = ImGui.GetFrameHeight();
var width = height * 1.55f;
var radius = height * 0.50f;
// TODO: animate
var changed = false;
ImGui.InvisibleButton(id, new Vector2(width, height));
if (ImGui.IsItemClicked())
{
v = !v;
changed = true;
}
if (ImGui.IsItemHovered())
drawList.AddRectFilled(p, new Vector2(p.X + width, p.Y + height), ImGui.GetColorU32(v ? colors[(int)ImGuiCol.ButtonActive] : new Vector4(0.78f, 0.78f, 0.78f, 1.0f)), height * 0.5f);
else
drawList.AddRectFilled(p, new Vector2(p.X + width, p.Y + height), ImGui.GetColorU32(v ? colors[(int)ImGuiCol.Button] : new Vector4(0.55f, 0.55f, 0.55f, 1.0f)), height * 0.50f);
drawList.AddCircleFilled(new Vector2(p.X + radius + ((v ? 1 : 0) * (width - (radius * 2.0f))), p.Y + radius), radius - 1.5f, ImGui.ColorConvertFloat4ToU32(new Vector4(255, 255, 255, 255)));
return changed;
}
/// <summary>
/// Draw a disabled toggle button.
/// </summary>
/// <param name="id">The id of the button.</param>
/// <param name="v">The state of the switch.</param>
public static void DisabledToggleButton(string id, bool v)
{
var colors = ImGui.GetStyle().Colors;
var p = ImGui.GetCursorScreenPos();
var drawList = ImGui.GetWindowDrawList();
var height = ImGui.GetFrameHeight();
var width = height * 1.55f;
var radius = height * 0.50f;
// TODO: animate
// TODO: dim colors
ImGui.InvisibleButton(id, new Vector2(width, height));
var dimFactor = 0.5f;
drawList.AddRectFilled(p, new Vector2(p.X + width, p.Y + height), ImGui.GetColorU32(v ? colors[(int)ImGuiCol.Button] * dimFactor : new Vector4(0.55f, 0.55f, 0.55f, 1.0f) * dimFactor), height * 0.50f);
drawList.AddCircleFilled(new Vector2(p.X + radius + ((v ? 1 : 0) * (width - (radius * 2.0f))), p.Y + radius), radius - 1.5f, ImGui.ColorConvertFloat4ToU32(new Vector4(255, 255, 255, 255) * dimFactor));
}
}
}

View file

@ -237,6 +237,36 @@ namespace Dalamud.Interface
return (VirtualKey)ImGui_Input_Impl_Direct.ImGuiKeyToVirtualKey(key);
}
/// <summary>
/// Show centered text.
/// </summary>
/// <param name="text">Text to show.</param>
public static void CenteredText(string text)
{
CenterCursorForText(text);
ImGui.TextUnformatted(text);
}
/// <summary>
/// Center the ImGui cursor for a certain text.
/// </summary>
/// <param name="text">The text to center for.</param>
public static void CenterCursorForText(string text)
{
var textWidth = ImGui.CalcTextSize(text).X;
CenterCursorFor((int)textWidth);
}
/// <summary>
/// Center the ImGui cursor for an item with a certain width.
/// </summary>
/// <param name="itemWidth">The width to center for.</param>
public static void CenterCursorFor(int itemWidth)
{
var window = (int)ImGui.GetWindowWidth();
ImGui.SetCursorPosX((window / 2) - (itemWidth / 2));
}
/// <summary>
/// Get data needed for each new frame.
/// </summary>