1
0
mirror of synced 2024-11-28 17:40:51 +01:00

impr: Better centered text rendering

This commit is contained in:
WerWolv 2023-12-02 23:46:20 +01:00
parent 9afbfec64e
commit f71fa2f704
2 changed files with 23 additions and 8 deletions

View File

@ -227,18 +227,13 @@ namespace ImGuiExt {
ImGui::PopID(); ImGui::PopID();
} }
void TextUnformattedCentered(const char *text);
inline void TextFormattedCentered(const std::string &fmt, auto &&...args) { inline void TextFormattedCentered(const std::string &fmt, auto &&...args) {
auto text = hex::format(fmt, std::forward<decltype(args)>(args)...); auto text = hex::format(fmt, std::forward<decltype(args)>(args)...);
auto availableSpace = ImGui::GetContentRegionAvail(); TextUnformattedCentered(text.c_str());
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();
} }
inline void TextFormattedCenteredHorizontal(const std::string &fmt, auto &&...args) { inline void TextFormattedCenteredHorizontal(const std::string &fmt, auto &&...args) {
auto text = hex::format(fmt, std::forward<decltype(args)>(args)...); auto text = hex::format(fmt, std::forward<decltype(args)>(args)...);
auto availableSpace = ImGui::GetContentRegionAvail(); auto availableSpace = ImGui::GetContentRegionAvail();

View File

@ -2,6 +2,8 @@
#include <imgui.h> #include <imgui.h>
#include <imgui_internal.h> #include <imgui_internal.h>
#include <implot.h>
#include <implot_internal.h>
#include <opengl_support.h> #include <opengl_support.h>
#undef IMGUI_DEFINE_MATH_OPERATORS #undef IMGUI_DEFINE_MATH_OPERATORS
@ -11,6 +13,7 @@
#include <string> #include <string>
#include <hex/api/imhex_api.hpp> #include <hex/api/imhex_api.hpp>
#include <fonts/codicons_font.h> #include <fonts/codicons_font.h>
@ -695,6 +698,23 @@ namespace ImGuiExt {
RenderRectFilledRangeH(window->DrawList, bb, GetColorU32(ImGuiCol_PlotHistogram), 0.0f, fraction, style.FrameRounding); 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) { bool InputTextIcon(const char *label, const char *icon, std::string &buffer, ImGuiInputTextFlags flags) {
auto window = GetCurrentWindow(); auto window = GetCurrentWindow();