From c529743e4a063693d708c00028957bae342589e6 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 ++-- ext/lua | 2 +- 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index bda4fd09d2..1b7f256fb6 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 04e6773871..70e9da16c2 100644 --- a/UI/ImDebugger/ImDebugger.cpp +++ b/UI/ImDebugger/ImDebugger.cpp @@ -371,7 +371,6 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug) { ImGui::EndMenu(); } if (ImGui::BeginMenu("Window")) { - ImGui::Checkbox("Dear ImGUI Demo", &cfg_.demoOpen); ImGui::Checkbox("CPU debugger", &cfg_.disasmOpen); ImGui::Checkbox("Registers", &cfg_.regsOpen); ImGui::Checkbox("Callstacks", &cfg_.callstackOpen); @@ -382,6 +381,8 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug) { ImGui::EndMenu(); } if (ImGui::BeginMenu("Misc")) { + ImGui::Checkbox("Dear ImGui Demo", &cfg_.demoOpen); + ImGui::Checkbox("ImGui Style editor", &cfg_.styleEditorOpen); if (ImGui::MenuItem("Close Debugger")) { g_Config.bShowImDebugger = false; } @@ -394,6 +395,10 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug) { 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 b084dcf492..ab0f6d64cf 100644 --- a/UI/ImDebugger/ImDebugger.h +++ b/UI/ImDebugger/ImDebugger.h @@ -59,6 +59,7 @@ struct ImConfig { bool hleModulesOpen = false; bool atracOpen = true; bool structViewerOpen = 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); diff --git a/ext/lua b/ext/lua index f3271af11a..7648485f14 160000 --- a/ext/lua +++ b/ext/lua @@ -1 +1 @@ -Subproject commit f3271af11ab8591164b871e36520a7210964f3f6 +Subproject commit 7648485f14e8e5ee45e8e39b1eb4d3206dbd405a