ImGui: create dedicated file for wrappers and implement RenderText

All future wrappers go here.
This commit is contained in:
Kawe Mazidjatari 2024-07-09 16:31:05 +02:00
parent 9623c1640d
commit f964db2ccf
3 changed files with 49 additions and 0 deletions

View File

@ -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"
)

View File

@ -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);
}

View File

@ -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