From 3a9c6ab3b351a44f69d67772b78e5dfbc0d649f6 Mon Sep 17 00:00:00 2001 From: Graham Pentheny Date: Tue, 5 Aug 2025 13:35:38 -0400 Subject: [PATCH] Added RightAlignText helper function --- RecastDemo/Include/imguiHelpers.h | 16 ++++++++++++++++ RecastDemo/Source/CrowdTool.cpp | 3 +-- RecastDemo/Source/Sample.cpp | 3 ++- RecastDemo/Source/main.cpp | 10 +--------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/RecastDemo/Include/imguiHelpers.h b/RecastDemo/Include/imguiHelpers.h index 9502ef69..3d744b90 100644 --- a/RecastDemo/Include/imguiHelpers.h +++ b/RecastDemo/Include/imguiHelpers.h @@ -69,3 +69,19 @@ inline void DrawIntSlider(int* value, int min, int max, const char* id, const ch ImVec2 valueLabelSize = ImGui::CalcTextSize(valueLabel); drawList->AddText(ImVec2(sliderMax.x - valueLabelSize.x - textPadding, textY), color, valueLabel); } + +inline void DrawRightAlignedText(const char* format, ...) +{ + static char text[1024]; // Static buffer (not thread-safe!) + + va_list args; + va_start(args, format); + vsnprintf(text, sizeof(text), format, args); + va_end(args); + + float textWidth = ImGui::CalcTextSize(text).x; + float parentWidth = ImGui::GetContentRegionAvail().x; + + ImGui::SetCursorPosX(ImGui::GetCursorPosX() + parentWidth - textWidth); + ImGui::Text("%s", text); +} diff --git a/RecastDemo/Source/CrowdTool.cpp b/RecastDemo/Source/CrowdTool.cpp index ff29bbac..b157f983 100644 --- a/RecastDemo/Source/CrowdTool.cpp +++ b/RecastDemo/Source/CrowdTool.cpp @@ -1074,8 +1074,7 @@ void CrowdTool::handleMenu() paramsChanged |= ImGui::Checkbox("Obstacle Avoidance", ¶ms->obstacleAvoidance); paramsChanged |= ImGui::SliderInt("Avoidance Quality", ¶ms->obstacleAvoidanceType, 0, 3); paramsChanged |= ImGui::Checkbox("Separation", ¶ms->separation); - paramsChanged |= - ImGui::SliderFloat("##Separation Weight", ¶ms->separationWeight, 0.0f, 20.0f, "Separation Weight = %.2f"); + paramsChanged |= ImGui::SliderFloat("##Separation Weight", ¶ms->separationWeight, 0.0f, 20.0f, "Separation Weight = %.2f"); if (paramsChanged) { diff --git a/RecastDemo/Source/Sample.cpp b/RecastDemo/Source/Sample.cpp index facb87a5..f079cb3d 100644 --- a/RecastDemo/Source/Sample.cpp +++ b/RecastDemo/Source/Sample.cpp @@ -25,6 +25,7 @@ #include "InputGeom.h" #include "Recast.h" #include "RecastDebugDraw.h" +#include "imguiHelpers.h" #include @@ -217,7 +218,7 @@ void Sample::handleCommonSettings() cellSize, &gridWidth, &gridHeight); - ImGui::Text("Voxels %d x %d", gridWidth, gridHeight); + DrawRightAlignedText("Voxels %d x %d", gridWidth, gridHeight); } ImGui::SeparatorText("Agent"); diff --git a/RecastDemo/Source/main.cpp b/RecastDemo/Source/main.cpp index ba29e100..b7645234 100644 --- a/RecastDemo/Source/main.cpp +++ b/RecastDemo/Source/main.cpp @@ -664,17 +664,9 @@ int main(int /*argc*/, char** /*argv*/) if (app.inputGeometry) { - // All this is to right-align the text. - char text[32]; - snprintf(text, 32, "Verts: %.1fk Tris: %.1fk", + DrawRightAlignedText("Verts: %.1fk Tris: %.1fk", static_cast(app.inputGeometry->getVertCount()) / 1000.0f, static_cast(app.inputGeometry->getTriCount()) / 1000.0f); - - float textWidth = ImGui::CalcTextSize(text).x; - float parentWidth = ImGui::GetContentRegionAvail().x; - - ImGui::SetCursorPosX(ImGui::GetCursorPosX() + parentWidth - textWidth); - ImGui::Text("%s", text); } if (app.sample)