mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-31 21:03:43 +01:00
feat: ImGuiHelpers.Center helpers, toggle switch component
This commit is contained in:
parent
46c2511c84
commit
30b1a4d383
2 changed files with 102 additions and 0 deletions
72
Dalamud/Interface/Components/ImGuiComponents.ToggleSwitch.cs
Normal file
72
Dalamud/Interface/Components/ImGuiComponents.ToggleSwitch.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue