Skip to content

Commit

Permalink
Merge pull request #19646 from hrydgard/imgui-ttf-support
Browse files Browse the repository at this point in the history
ImGui debugger: Add TTF font support
  • Loading branch information
hrydgard authored Nov 24, 2024
2 parents 32d8b12 + e31636c commit f692092
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 19 deletions.
10 changes: 8 additions & 2 deletions UI/EmuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -1646,7 +1646,13 @@ void EmuScreen::renderImDebugger() {
if (!imguiInited_) {
imguiInited_ = true;
imDebugger_ = std::make_unique<ImDebugger>();
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()) {
Expand Down
11 changes: 6 additions & 5 deletions UI/ImDebugger/ImDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,6 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu
Core_Break("Menu:Break");
}
}
ImGui::Separator();
if (ImGui::MenuItem("Toggle Breakpoint")) {
// TODO
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("CPU")) {
Expand Down Expand Up @@ -399,7 +395,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();
Expand All @@ -409,6 +406,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);
}
Expand Down
1 change: 1 addition & 0 deletions UI/ImDebugger/ImDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct ImConfig {
bool atracOpen = true;
bool structViewerOpen = false;
bool framebuffersOpen = false;
bool styleEditorOpen = false;

// HLE explorer settings
// bool filterByUsed = true;
Expand Down
19 changes: 12 additions & 7 deletions UI/ImDebugger/ImDisasmView.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "ext/imgui/imgui_internal.h"
#include "ext/imgui/imgui_impl_thin3d.h"

#include "Common/StringUtils.h"
#include "Common/Log.h"
Expand Down Expand Up @@ -178,8 +179,7 @@ void ImDisasmView::drawBranchLine(ImDrawList *drawList, Rect rect, std::map<u32,
bottomY = (float)addressPositions[line.second] + rowHeight_ / 2;
}

if ((topY < 0 && bottomY < 0) || (topY > rect.bottom && bottomY > rect.bottom))
{
if ((topY < 0 && bottomY < 0) || (topY > rect.bottom && bottomY > rect.bottom)) {
return;
}

Expand Down Expand Up @@ -316,6 +316,8 @@ void ImDisasmView::drawArguments(ImDrawList *drawList, Rect rc, const Disassembl

void ImDisasmView::Draw(ImDrawList *drawList) {
// TODO: Don't need to do these every frame.
ImGui_PushFixedFont();

rowHeight_ = ImGui::GetTextLineHeightWithSpacing();
charWidth_ = ImGui::CalcTextSize("W", nullptr, false, -1.0f).x;

Expand Down Expand Up @@ -435,17 +437,17 @@ void ImDisasmView::Draw(ImDrawList *drawList) {
ImGuiIO& io = ImGui::GetIO();
ImVec2 mousePos = ImVec2(io.MousePos.x - canvas_p0.x, io.MousePos.y - canvas_p0.y);
if (is_hovered && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
INFO_LOG(Log::CPU, "Mousedown %f,%f active:%d hover:%d", mousePos.x, mousePos.y, is_active, is_hovered);
// INFO_LOG(Log::CPU, "Mousedown %f,%f active:%d hover:%d", mousePos.x, mousePos.y, is_active, is_hovered);
onMouseDown(mousePos.x, mousePos.y, 1);
}
if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
INFO_LOG(Log::CPU, "Mouseup %f,%f active:%d hover:%d", mousePos.x, mousePos.y, is_active, is_hovered);
// INFO_LOG(Log::CPU, "Mouseup %f,%f active:%d hover:%d", mousePos.x, mousePos.y, is_active, is_hovered);
if (is_hovered) {
onMouseUp(mousePos.x, mousePos.y, 1);
}
}
if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
INFO_LOG(Log::CPU, "Mousedrag %f,%f active:%d hover:%d", mousePos.x, mousePos.y, is_active, is_hovered);
// INFO_LOG(Log::CPU, "Mousedrag %f,%f active:%d hover:%d", mousePos.x, mousePos.y, is_active, is_hovered);
if (is_hovered) {
onMouseMove(mousePos.x, mousePos.y, 1);
}
Expand Down Expand Up @@ -482,10 +484,13 @@ void ImDisasmView::Draw(ImDrawList *drawList) {
}
}

ImGui_PopFont();

ImGui::OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight);
PopupMenu();

drawList->PopClipRect();

}

void ImDisasmView::ScrollRelative(int amount) {
Expand Down Expand Up @@ -528,7 +533,7 @@ void ImDisasmView::onChar(int c) {
}


void ImDisasmView::editBreakpoint() {
void ImDisasmView::editBreakpoint(ImConfig &cfg) {
/*
BreakpointWindow win(wnd, debugger);
Expand Down Expand Up @@ -583,7 +588,7 @@ void ImDisasmView::ProcessKeyboardShortcuts() {
// gotoAddr(addr);
}
if (ImGui::IsKeyPressed(ImGuiKey_E)) {
editBreakpoint();
// editBreakpoint();
}
if (ImGui::IsKeyPressed(ImGuiKey_D)) {
toggleBreakpoint(true);
Expand Down
4 changes: 3 additions & 1 deletion UI/ImDebugger/ImDisasmView.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "Core/Debugger/DisassemblyManager.h"
#include "Core/Debugger/DebugInterface.h"

struct ImConfig;

// Corresponds to CtrlDisAsmView
// TODO: Fold out common code.
class ImDisasmView {
Expand Down Expand Up @@ -77,7 +79,7 @@ class ImDisasmView {
showHex_ = s;
}
void toggleBreakpoint(bool toggleEnabled = false);
void editBreakpoint();
void editBreakpoint(ImConfig &cfg);

void setCurAddress(u32 newAddress, bool extend = false) {
newAddress = manager.getStartAddress(newAddress);
Expand Down
25 changes: 23 additions & 2 deletions ext/imgui/imgui_impl_thin3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include "Common/System/Display.h"
#include "Common/Math/lin/matrix4x4.h"

Lin::Matrix4x4 g_drawMatrix;
static Lin::Matrix4x4 g_drawMatrix;

static ImFont *g_proportionalFont = nullptr;
static ImFont *g_fixedFont = nullptr;

struct ImGui_ImplThin3d_Data {
Draw::SamplerState *fontSampler = nullptr;
Expand Down Expand Up @@ -217,8 +220,18 @@ 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) {
g_proportionalFont = io.Fonts->AddFontFromMemoryTTF((void *)ttf_font, (int)size, 21.0f / g_display.dpi_scale_x, nullptr, io.Fonts->GetGlyphRangesDefault());
} else {
// fallback
g_proportionalFont = g_fixedFont;
}
g_fixedFont = io.Fonts->AddFontDefault();
ImGui::GetStyle().ScaleAllSizes(1.0f / g_display.dpi_scale_x);
ImGui::GetStyle().Colors[ImGuiCol_Border] = ImColor(IM_COL32(0x2A, 0x2F, 0x3B, 0xFF));

IMGUI_CHECKVERSION();
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");

Expand All @@ -231,6 +244,14 @@ bool ImGui_ImplThin3d_Init(Draw::DrawContext *draw) {
return true;
}

void ImGui_PushFixedFont() {
ImGui::PushFont(g_fixedFont);
}

void ImGui_PopFont() {
ImGui::PopFont();
}

void ImGui_ImplThin3d_Shutdown() {
ImGui_ImplThin3d_Data* bd = ImGui_ImplThin3d_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
Expand Down
7 changes: 5 additions & 2 deletions ext/imgui/imgui_impl_thin3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -44,6 +44,9 @@ IMGUI_IMPL_API void ImGui_ImplThin3d_DestroyDeviceObjects();
IMGUI_IMPL_API ImTextureID ImGui_ImplThin3d_AddTexture(Draw::Texture *texture);
IMGUI_IMPL_API Draw::Texture *ImGui_ImplThin3d_RemoveTexture(ImTextureID texture);

void ImGui_PushFixedFont();
void ImGui_PopFont();

// Helper structure to hold the data needed by one rendering context into one OS window
// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
struct ImGui_ImplThin3dH_Window
Expand Down

0 comments on commit f692092

Please sign in to comment.