Skip to content

Commit 26c1278

Browse files
Daniel K. O. (dkosmari)ocornut
authored andcommitted
Changed TextAlignedV() so it behaves better with cursor position and window DC boundary.
1 parent 5de45fb commit 26c1278

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

imgui_widgets.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -342,18 +342,20 @@ void ImGui::TextWrappedV(const char* fmt, va_list args)
342342
PopTextWrapPos();
343343
}
344344

345-
void ImGui::TextAligned(float align_x, float size_x, const char* fmt, ...)
345+
void ImGui::TextAligned(float align_x, float width, const char* fmt, ...)
346346
{
347347
va_list args;
348348
va_start(args, fmt);
349-
TextAlignedV(align_x, size_x, fmt, args);
349+
TextAlignedV(align_x, width, fmt, args);
350350
va_end(args);
351351
}
352352

353353
// align_x: 0.0f = left, 0.5f = center, 1.0f = right.
354-
// size_x : 0.0f = shortcut for GetContentRegionAvail().x
354+
// width :
355+
// - 0.0f: shortcut for CalcTextSize().x
356+
// - negative: shortcut for GetContentRegionAvail().x
355357
// FIXME-WIP: Works but API is likely to be reworked. This is designed for 1 item on the line. (#7024)
356-
void ImGui::TextAlignedV(float align_x, float size_x, const char* fmt, va_list args)
358+
void ImGui::TextAlignedV(float align_x, float width, const char* fmt, va_list args)
357359
{
358360
ImGuiWindow* window = GetCurrentWindow();
359361
if (window->SkipItems)
@@ -362,23 +364,19 @@ void ImGui::TextAlignedV(float align_x, float size_x, const char* fmt, va_list a
362364
const char* text, *text_end;
363365
ImFormatStringToTempBufferV(&text, &text_end, fmt, args);
364366
const ImVec2 text_size = CalcTextSize(text, text_end);
365-
size_x = CalcItemSize(ImVec2(size_x, 0.0f), 0.0f, text_size.y).x;
367+
const ImVec2 widget_size = CalcItemSize(ImVec2(width, 0.0f), text_size.x, text_size.y);
366368

367-
ImVec2 pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
368-
ImVec2 pos_max(pos.x + size_x, window->ClipRect.Max.y);
369-
ImVec2 size(ImMin(size_x, text_size.x), text_size.y);
370-
window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, pos.x + text_size.x);
371-
window->DC.IdealMaxPos.x = ImMax(window->DC.IdealMaxPos.x, pos.x + text_size.x);
372-
if (align_x > 0.0f && text_size.x < size_x)
373-
pos.x += ImTrunc((size_x - text_size.x) * align_x);
374-
RenderTextEllipsis(window->DrawList, pos, pos_max, pos_max.x, text, text_end, &text_size);
375-
376-
const ImVec2 backup_max_pos = window->DC.CursorMaxPos;
377-
ItemSize(size);
378-
ItemAdd(ImRect(pos, pos + size), 0);
379-
window->DC.CursorMaxPos.x = backup_max_pos.x; // Cancel out extending content size because right-aligned text would otherwise mess it up.
369+
const ImVec2 start_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
370+
ImVec2 text_pos = start_pos;
371+
if (align_x > 0.0f && text_size.x < widget_size.x)
372+
text_pos.x += ImTrunc((widget_size.x - text_size.x) * align_x);
373+
ImVec2 pos_max(start_pos.x + widget_size.x, window->ClipRect.Max.y);
374+
RenderTextEllipsis(window->DrawList, text_pos, pos_max, pos_max.x, text, text_end, &text_size);
375+
376+
ItemSize(widget_size);
377+
ItemAdd(ImRect(start_pos, start_pos + widget_size), 0);
380378

381-
if (size_x < text_size.x && IsItemHovered(ImGuiHoveredFlags_NoNavOverride | ImGuiHoveredFlags_AllowWhenDisabled | ImGuiHoveredFlags_ForTooltip))
379+
if (widget_size.x < text_size.x && IsItemHovered(ImGuiHoveredFlags_NoNavOverride | ImGuiHoveredFlags_AllowWhenDisabled | ImGuiHoveredFlags_ForTooltip))
382380
SetTooltip("%.*s", (int)(text_end - text), text);
383381
}
384382

0 commit comments

Comments
 (0)