From f71fa2f704478160997936d2ec114eb7489d3643 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 2 Dec 2023 23:46:20 +0100 Subject: [PATCH] impr: Better centered text rendering --- .../include/hex/ui/imgui_imhex_extensions.h | 11 +++------- .../source/ui/imgui_imhex_extensions.cpp | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h b/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h index dbd40d275..d0d80916a 100644 --- a/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h +++ b/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h @@ -227,18 +227,13 @@ namespace ImGuiExt { ImGui::PopID(); } + void TextUnformattedCentered(const char *text); inline void TextFormattedCentered(const std::string &fmt, auto &&...args) { auto text = hex::format(fmt, std::forward(args)...); - auto availableSpace = ImGui::GetContentRegionAvail(); - auto textSize = ImGui::CalcTextSize(text.c_str(), nullptr, false, availableSpace.x * 0.75F); - - ImGui::SetCursorPos(((availableSpace - textSize) / 2.0F)); - - ImGui::PushTextWrapPos(availableSpace.x * 0.75F); - ImGuiExt::TextFormattedWrapped("{}", text); - ImGui::PopTextWrapPos(); + TextUnformattedCentered(text.c_str()); } + inline void TextFormattedCenteredHorizontal(const std::string &fmt, auto &&...args) { auto text = hex::format(fmt, std::forward(args)...); auto availableSpace = ImGui::GetContentRegionAvail(); diff --git a/lib/libimhex/source/ui/imgui_imhex_extensions.cpp b/lib/libimhex/source/ui/imgui_imhex_extensions.cpp index 3e2f5540a..80c6a182c 100644 --- a/lib/libimhex/source/ui/imgui_imhex_extensions.cpp +++ b/lib/libimhex/source/ui/imgui_imhex_extensions.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include #undef IMGUI_DEFINE_MATH_OPERATORS @@ -11,6 +13,7 @@ #include + #include #include @@ -695,6 +698,23 @@ namespace ImGuiExt { RenderRectFilledRangeH(window->DrawList, bb, GetColorU32(ImGuiCol_PlotHistogram), 0.0f, fraction, style.FrameRounding); } + void TextUnformattedCentered(const char *text) { + auto availableSpace = ImGui::GetContentRegionAvail(); + + std::string drawString; + auto textEnd = text + strlen(text); + for (auto wrapPos = text; wrapPos != textEnd;) { + wrapPos = ImGui::GetFont()->CalcWordWrapPositionA(1, wrapPos, textEnd, availableSpace.x * 0.8F); + drawString += std::string(text, wrapPos) + "\n"; + text = wrapPos; + } + + drawString.pop_back(); + + auto textSize = ImGui::CalcTextSize(drawString.c_str()); + + ImPlot::AddTextCentered(ImGui::GetWindowDrawList(), ImGui::GetCursorScreenPos() + availableSpace / 2 - ImVec2(0, textSize.y / 2), ImGui::GetColorU32(ImGuiCol_Text), drawString.c_str()); + } bool InputTextIcon(const char *label, const char *icon, std::string &buffer, ImGuiInputTextFlags flags) { auto window = GetCurrentWindow();