From f964db2ccf83439b3b33a315b3b1aff4b1b5614b Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Tue, 9 Jul 2024 16:31:05 +0200 Subject: [PATCH] ImGui: create dedicated file for wrappers and implement RenderText All future wrappers go here. --- src/thirdparty/imgui/CMakeLists.txt | 2 ++ src/thirdparty/imgui/misc/imgui_wrapper.cpp | 34 +++++++++++++++++++++ src/thirdparty/imgui/misc/imgui_wrapper.h | 13 ++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/thirdparty/imgui/misc/imgui_wrapper.cpp create mode 100644 src/thirdparty/imgui/misc/imgui_wrapper.h diff --git a/src/thirdparty/imgui/CMakeLists.txt b/src/thirdparty/imgui/CMakeLists.txt index 15293e45..3f668ce5 100644 --- a/src/thirdparty/imgui/CMakeLists.txt +++ b/src/thirdparty/imgui/CMakeLists.txt @@ -42,6 +42,8 @@ add_sources( SOURCE_GROUP "Misc" "misc/imgui_style.h" "misc/imgui_utility.cpp" "misc/imgui_utility.h" + "misc/imgui_wrapper.cpp" + "misc/imgui_wrapper.h" "misc/cpp/imgui_stdlib.cpp" "misc/cpp/imgui_stdlib.h" ) diff --git a/src/thirdparty/imgui/misc/imgui_wrapper.cpp b/src/thirdparty/imgui/misc/imgui_wrapper.cpp new file mode 100644 index 00000000..461a4fd5 --- /dev/null +++ b/src/thirdparty/imgui/misc/imgui_wrapper.cpp @@ -0,0 +1,34 @@ +//============================================================================// +// +// Purpose: Set of Dear ImGui wrappers +// +//============================================================================// +#include "imgui.h" +#include "imgui_wrapper.h" + +#include "stdio.h" + +void ImGui_RenderText(const ImGuiTextAlign_e align, const ImVec2 pos, const ImVec4& color, const char* const fmt, ...) +{ + ImDrawList* const drawList = ImGui::GetBackgroundDrawList(); + assert(drawList); + + va_list argsCopy; + va_start(argsCopy, fmt); + + char textBuffer[2048]; + + const int numChars = vsnprintf(textBuffer, sizeof(textBuffer), fmt, argsCopy); + va_end(argsCopy); + + // nb(amos): 'vsnprintf' does not count the terminating null, so no -1 here. + const char* const textEnd = &textBuffer[numChars]; + ImVec2 alignedPos = pos; + + if (align == ImGuiTextAlign_e::kAlignCenter) + alignedPos.x -= ImGui::CalcTextSize(textBuffer, textEnd).x / 2; + else if (align == ImGuiTextAlign_e::kAlignRight) + alignedPos.x -= ImGui::CalcTextSize(textBuffer, textEnd).x; + + drawList->AddText(alignedPos, ImGui::ColorConvertFloat4ToU32(color), textBuffer, textEnd); +} diff --git a/src/thirdparty/imgui/misc/imgui_wrapper.h b/src/thirdparty/imgui/misc/imgui_wrapper.h new file mode 100644 index 00000000..1269fd6d --- /dev/null +++ b/src/thirdparty/imgui/misc/imgui_wrapper.h @@ -0,0 +1,13 @@ +#ifndef IMGUI_WRAPPER_H +#define IMGUI_WRAPPER_H + +enum class ImGuiTextAlign_e +{ + kAlignLeft, + kAlignCenter, + kAlignRight, +}; + +extern void ImGui_RenderText(const ImGuiTextAlign_e align, const ImVec2 pos, const ImVec4& color, const char* const fmt, ...); + +#endif // IMGUI_WRAPPER_H