From 6dfc5ea9ef6314c997368011cd67a0115d396fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 22 Nov 2024 13:54:19 +0100 Subject: [PATCH] ImGui: Add TTF font support We use the Roboto font that we're already shipping for now, although, we could also support other fonts or have a setting. --- UI/EmuScreen.cpp | 10 ++++++++-- UI/ImDebugger/ImDebugger.cpp | 7 ++++++- UI/ImDebugger/ImDebugger.h | 1 + ext/imgui/imgui_impl_thin3d.cpp | 10 +++++++++- ext/imgui/imgui_impl_thin3d.h | 4 ++-- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 2fbb02f8b1..5c6d8372ef 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -27,7 +27,7 @@ using namespace std::placeholders; #include "Common/Render/Text/draw_text.h" #include "Common/File/FileUtil.h" #include "Common/Battery/Battery.h" - +#include "Common/File/VFS/VFS.h" #include "Common/UI/Root.h" #include "Common/UI/UI.h" #include "Common/UI/Context.h" @@ -1646,7 +1646,13 @@ void EmuScreen::renderImDebugger() { if (!imguiInited_) { imguiInited_ = true; imDebugger_ = std::make_unique(); - ImGui_ImplThin3d_Init(draw); + + // Read the TTF font + size_t size = 0; + uint8_t *fontData = g_VFS.ReadFile("Roboto-Condensed.ttf", &size); + // This call works even if fontData is nullptr, in which case the font just won't get loaded. + // This takes ownership of the font array. + ImGui_ImplThin3d_Init(draw, fontData, size); } if (PSP_IsInited()) { diff --git a/UI/ImDebugger/ImDebugger.cpp b/UI/ImDebugger/ImDebugger.cpp index 022ec93b24..f587b37ca6 100644 --- a/UI/ImDebugger/ImDebugger.cpp +++ b/UI/ImDebugger/ImDebugger.cpp @@ -399,7 +399,8 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu if (ImGui::MenuItem("Close Debugger")) { g_Config.bShowImDebugger = false; } - ImGui::MenuItem("Dear ImGUI Demo", nullptr, &cfg_.demoOpen); + ImGui::MenuItem("Dear ImGui Demo", nullptr, &cfg_.demoOpen); + ImGui::MenuItem("Dear ImGui Style editor", nullptr, &cfg_.styleEditorOpen); ImGui::EndMenu(); } ImGui::EndMainMenuBar(); @@ -409,6 +410,10 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu ImGui::ShowDemoWindow(&cfg_.demoOpen); } + if (cfg_.styleEditorOpen) { + ImGui::ShowStyleEditor(); + } + if (cfg_.disasmOpen) { disasm_.Draw(mipsDebug, &cfg_.disasmOpen, coreState); } diff --git a/UI/ImDebugger/ImDebugger.h b/UI/ImDebugger/ImDebugger.h index 75aa976612..8d3368d1d6 100644 --- a/UI/ImDebugger/ImDebugger.h +++ b/UI/ImDebugger/ImDebugger.h @@ -65,6 +65,7 @@ struct ImConfig { bool atracOpen = true; bool structViewerOpen = false; bool framebuffersOpen = false; + bool styleEditorOpen = false; // HLE explorer settings // bool filterByUsed = true; diff --git a/ext/imgui/imgui_impl_thin3d.cpp b/ext/imgui/imgui_impl_thin3d.cpp index c4f9c888cf..aa935d4ecb 100644 --- a/ext/imgui/imgui_impl_thin3d.cpp +++ b/ext/imgui/imgui_impl_thin3d.cpp @@ -217,8 +217,16 @@ void ImGui_ImplThin3d_DestroyDeviceObjects() { } } -bool ImGui_ImplThin3d_Init(Draw::DrawContext *draw) { +bool ImGui_ImplThin3d_Init(Draw::DrawContext *draw, const uint8_t *ttf_font, size_t size) { ImGuiIO& io = ImGui::GetIO(); + if (ttf_font) { + io.Fonts->AddFontFromMemoryTTF((void *)ttf_font, size, 18.0f * g_display.dpi_scale_x, nullptr, io.Fonts->GetGlyphRangesDefault()); + } else { + // necessary? + io.Fonts->AddFontDefault(); + } + ImGui::GetStyle().ScaleAllSizes(g_display.dpi_scale_x); + IMGUI_CHECKVERSION(); IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); diff --git a/ext/imgui/imgui_impl_thin3d.h b/ext/imgui/imgui_impl_thin3d.h index c06090d35c..697d12b3aa 100644 --- a/ext/imgui/imgui_impl_thin3d.h +++ b/ext/imgui/imgui_impl_thin3d.h @@ -33,8 +33,8 @@ #include "Common/GPU/thin3d.h" #include "Common/Math/lin/matrix4x4.h" -// Called by user code -IMGUI_IMPL_API bool ImGui_ImplThin3d_Init(Draw::DrawContext *draw); +// Called by user code. Takes ownership of the font buffer and later deletes it. +IMGUI_IMPL_API bool ImGui_ImplThin3d_Init(Draw::DrawContext *draw, const uint8_t *ttf_font, size_t size); IMGUI_IMPL_API void ImGui_ImplThin3d_Shutdown(); IMGUI_IMPL_API void ImGui_ImplThin3d_NewFrame(Draw::DrawContext *draw, Lin::Matrix4x4 drawMatrix); IMGUI_IMPL_API void ImGui_ImplThin3d_RenderDrawData(ImDrawData* draw_data, Draw::DrawContext *draw);