Rework TextAligned() api to take size input. (#7024)

This commit is contained in:
ocornut
2025-04-23 14:52:29 +02:00
parent 839e3274e1
commit f2ba3a937b
2 changed files with 15 additions and 16 deletions

View File

@@ -3746,9 +3746,8 @@ namespace ImGui
// Widgets: Text // Widgets: Text
IMGUI_API void TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0); IMGUI_API void TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0);
IMGUI_API void TextAligned(float align_x, const char* fmt, ...); // FIXME-WIP: Works but API is likely to be reworked. This is designed for 1 item on the line. (#7024) IMGUI_API void TextAligned(float align_x, float size_x, const char* fmt, ...); // FIXME-WIP: Works but API is likely to be reworked. This is designed for 1 item on the line. (#7024)
IMGUI_API void TextAlignedV(float align_x, const char* fmt, va_list args); IMGUI_API void TextAlignedV(float align_x, float size_x, const char* fmt, va_list args);
IMGUI_API void TextAlignedExV(float align_x, float avail_x, const char* fmt, va_list args);
// Widgets // Widgets
IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0, 0), ImGuiButtonFlags flags = 0); IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0, 0), ImGuiButtonFlags flags = 0);

View File

@@ -339,39 +339,39 @@ void ImGui::TextWrappedV(const char* fmt, va_list args)
PopTextWrapPos(); PopTextWrapPos();
} }
void ImGui::TextAligned(float align_x, const char* fmt, ...) void ImGui::TextAligned(float align_x, float size_x, const char* fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
TextAlignedV(align_x, fmt, args); TextAlignedV(align_x, size_x, fmt, args);
va_end(args); va_end(args);
} }
// align_x: 0.0f = left, 0.5f = center, 1.0f = right. // align_x: 0.0f = left, 0.5f = center, 1.0f = right.
// size_x : 0.0f = shortcut for GetContentRegionAvail().x
// FIXME-WIP: Works but API is likely to be reworked. This is designed for 1 item on the line. (#7024) // FIXME-WIP: Works but API is likely to be reworked. This is designed for 1 item on the line. (#7024)
void ImGui::TextAlignedV(float align_x, const char* fmt, va_list args) void ImGui::TextAlignedV(float align_x, float size_x, const char* fmt, va_list args)
{
TextAlignedExV(align_x, GetContentRegionAvail().x, fmt, args);
}
void ImGui::TextAlignedExV(float align_x, float avail_x, const char* fmt, va_list args)
{ {
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems) if (window->SkipItems)
return; return;
// ~CalcItemSize()
if (size_x <= 0.0f)
size_x = GetContentRegionAvail().x + size_x; // <-- Remember that size_x is negative here
const char* text, *text_end; const char* text, *text_end;
ImFormatStringToTempBufferV(&text, &text_end, fmt, args); ImFormatStringToTempBufferV(&text, &text_end, fmt, args);
const ImVec2 text_size = CalcTextSize(text, text_end); const ImVec2 text_size = CalcTextSize(text, text_end);
ImVec2 pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); ImVec2 pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
ImVec2 pos_max(pos.x + avail_x, window->ClipRect.Max.y); ImVec2 pos_max(pos.x + size_x, window->ClipRect.Max.y);
ImVec2 size(ImMin(avail_x, text_size.x), text_size.y); ImVec2 size(ImMin(size_x, text_size.x), text_size.y);
window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, pos.x + text_size.x); window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, pos.x + text_size.x);
window->DC.IdealMaxPos.x = ImMax(window->DC.IdealMaxPos.x, pos.x + text_size.x); window->DC.IdealMaxPos.x = ImMax(window->DC.IdealMaxPos.x, pos.x + text_size.x);
if (align_x > 0.0f && text_size.x < avail_x) if (align_x > 0.0f && text_size.x < size_x)
{ {
pos.x += ImTrunc((avail_x - text_size.x) * align_x); pos.x += ImTrunc((size_x - text_size.x) * align_x);
window->DC.CursorPos = pos; window->DC.CursorPos = pos;
} }
RenderTextEllipsis(window->DrawList, pos, pos_max, pos_max.x, text, text_end, &text_size); RenderTextEllipsis(window->DrawList, pos, pos_max, pos_max.x, text, text_end, &text_size);
@@ -381,7 +381,7 @@ void ImGui::TextAlignedExV(float align_x, float avail_x, const char* fmt, va_lis
ItemAdd(ImRect(pos, pos + size), 0); ItemAdd(ImRect(pos, pos + size), 0);
window->DC.CursorMaxPos.x = backup_max_pos.x; // Cancel out extending content size because right-aligned text would otherwise mess it up. window->DC.CursorMaxPos.x = backup_max_pos.x; // Cancel out extending content size because right-aligned text would otherwise mess it up.
if (avail_x < text_size.x && IsItemHovered(ImGuiHoveredFlags_NoNavOverride | ImGuiHoveredFlags_AllowWhenDisabled | ImGuiHoveredFlags_ForTooltip)) if (size_x < text_size.x && IsItemHovered(ImGuiHoveredFlags_NoNavOverride | ImGuiHoveredFlags_AllowWhenDisabled | ImGuiHoveredFlags_ForTooltip))
SetTooltip("%.*s", (int)(text_end - text), text); SetTooltip("%.*s", (int)(text_end - text), text);
} }