Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer
#define IM_F32_TO_INT8_UNBOUND(_VAL) ((int)((_VAL) * 255.0f + ((_VAL)>=0 ? 0.5f : -0.5f))) // Unsaturated, for display purpose
#define IM_F32_TO_INT8_SAT(_VAL) ((int)(ImSaturate(_VAL) * 255.0f + 0.5f)) // Saturated, always output 0..255
#define IM_TRUNC(_VAL) ((float)(int)(_VAL)) // Positive values only! ImTrunc() is not inlined in MSVC debug builds
#define IM_ROUND(_VAL) ((float)(int)((_VAL) + 0.5f)) // Positive values only!
#define IM_ROUND(_VAL) ((float)(int)((_VAL) + 0.5f)) // Positive values only!
//#define IM_FLOOR IM_TRUNC // [OBSOLETE] Renamed in 1.90.0 (Sept 2023)

// Hint for branch prediction
Expand Down Expand Up @@ -3210,7 +3210,7 @@ struct ImGuiTableSettings
};

namespace ImGui
{
{
// Tables: Candidates for public API
IMGUI_API void TableOpenContextMenu(int column_n = -1);
IMGUI_API void TableSetColumnWidth(int column_n, float width);
Expand Down Expand Up @@ -3697,8 +3697,8 @@ namespace ImGui

// Widgets: Text
IMGUI_API void TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0);
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, float size_x, const char* fmt, va_list args);
IMGUI_API void TextAligned(float align_x, float width, const char* fmt, ...) IM_FMTARGS(3); // FIXME-WIP: Works but API is likely to be reworked.
IMGUI_API void TextAlignedV(float align_x, float width, const char* fmt, va_list args) IM_FMTLIST(3);

// Widgets
IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0, 0), ImGuiButtonFlags flags = 0);
Expand Down
36 changes: 17 additions & 19 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,18 +342,20 @@ void ImGui::TextWrappedV(const char* fmt, va_list args)
PopTextWrapPos();
}

void ImGui::TextAligned(float align_x, float size_x, const char* fmt, ...)
void ImGui::TextAligned(float align_x, float width, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
TextAlignedV(align_x, size_x, fmt, args);
TextAlignedV(align_x, width, fmt, args);
va_end(args);
}

// align_x: 0.0f = left, 0.5f = center, 1.0f = right.
// size_x : 0.0f = shortcut for GetContentRegionAvail().x
// width :
// - 0.0f: shortcut for CalcTextSize().x
// - negative: shortcut for GetContentRegionAvail().x
// 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, float size_x, const char* fmt, va_list args)
void ImGui::TextAlignedV(float align_x, float width, const char* fmt, va_list args)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
Expand All @@ -362,23 +364,19 @@ void ImGui::TextAlignedV(float align_x, float size_x, const char* fmt, va_list a
const char* text, *text_end;
ImFormatStringToTempBufferV(&text, &text_end, fmt, args);
const ImVec2 text_size = CalcTextSize(text, text_end);
size_x = CalcItemSize(ImVec2(size_x, 0.0f), 0.0f, text_size.y).x;
const ImVec2 widget_size = CalcItemSize(ImVec2(width, 0.0f), text_size.x, text_size.y);

ImVec2 pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
ImVec2 pos_max(pos.x + size_x, window->ClipRect.Max.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.IdealMaxPos.x = ImMax(window->DC.IdealMaxPos.x, pos.x + text_size.x);
if (align_x > 0.0f && text_size.x < size_x)
pos.x += ImTrunc((size_x - text_size.x) * align_x);
RenderTextEllipsis(window->DrawList, pos, pos_max, pos_max.x, text, text_end, &text_size);

const ImVec2 backup_max_pos = window->DC.CursorMaxPos;
ItemSize(size);
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.
const ImVec2 start_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
ImVec2 text_pos = start_pos;
if (align_x > 0.0f && text_size.x < widget_size.x)
text_pos.x += ImTrunc((widget_size.x - text_size.x) * align_x);
ImVec2 pos_max(start_pos.x + widget_size.x, window->ClipRect.Max.y);
RenderTextEllipsis(window->DrawList, text_pos, pos_max, pos_max.x, text, text_end, &text_size);

ItemSize(widget_size);
ItemAdd(ImRect(start_pos, start_pos + widget_size), 0);

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

Expand Down