Skip to content

Commit 2cfb7f3

Browse files
Automatically scale imgui text based on display pixel density (#545)
1 parent 503e99d commit 2cfb7f3

File tree

7 files changed

+52
-19
lines changed

7 files changed

+52
-19
lines changed

src/Cafe/HW/Latte/Core/LatteOverlay.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ struct OverlayList
6666
const auto kPopupFlags = ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav;
6767

6868
const float kBackgroundAlpha = 0.65f;
69-
void LatteOverlay_renderOverlay(ImVec2& position, ImVec2& pivot, sint32 direction)
69+
void LatteOverlay_renderOverlay(ImVec2& position, ImVec2& pivot, sint32 direction, float fontSize, bool pad)
7070
{
7171
auto& config = GetConfig();
72-
73-
const auto font = ImGui_GetFont(14.0f * (float)config.overlay.text_scale / 100.0f);
72+
73+
const auto font = ImGui_GetFont(fontSize);
7474
ImGui::PushFont(font);
7575

7676
const ImVec4 color = ImGui::ColorConvertU32ToFloat4(config.overlay.text_color);
@@ -80,7 +80,7 @@ void LatteOverlay_renderOverlay(ImVec2& position, ImVec2& pivot, sint32 directio
8080
{
8181
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
8282
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
83-
if (ImGui::Begin("Stats overlay", nullptr, kPopupFlags))
83+
if (ImGui_BeginPadDistinct("Stats overlay", nullptr, kPopupFlags, pad))
8484
{
8585
if (config.overlay.fps)
8686
ImGui::Text("FPS: %.2lf", g_state.fps);
@@ -117,11 +117,11 @@ void LatteOverlay_renderOverlay(ImVec2& position, ImVec2& pivot, sint32 directio
117117
ImGui::PopFont();
118118
}
119119

120-
void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 direction)
120+
void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 direction, float fontSize, bool pad)
121121
{
122122
auto& config = GetConfig();
123123

124-
const auto font = ImGui_GetFont(14.0f * (float)config.notification.text_scale / 100.0f);
124+
const auto font = ImGui_GetFont(fontSize);
125125
ImGui::PushFont(font);
126126

127127
const ImVec4 color = ImGui::ColorConvertU32ToFloat4(config.notification.text_color);
@@ -141,7 +141,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
141141
// active account
142142
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
143143
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
144-
if (ImGui::Begin("Active account", nullptr, kPopupFlags))
144+
if (ImGui_BeginPadDistinct("Active account", nullptr, kPopupFlags, pad))
145145
{
146146
ImGui::TextUnformatted((const char*)ICON_FA_USER);
147147
ImGui::SameLine();
@@ -179,7 +179,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
179179
{
180180
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
181181
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
182-
if (ImGui::Begin("Controller profile names", nullptr, kPopupFlags))
182+
if (ImGui_BeginPadDistinct("Controller profile names", nullptr, kPopupFlags, pad))
183183
{
184184
auto it = profiles.cbegin();
185185
ImGui::TextUnformatted((const char*)ICON_FA_GAMEPAD);
@@ -227,7 +227,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
227227
{
228228
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
229229
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
230-
if (ImGui::Begin("Friends overlay", nullptr, kPopupFlags))
230+
if (ImGui_BeginPadDistinct("Friends overlay", nullptr, kPopupFlags, pad))
231231
{
232232
const auto tick = tick_cached();
233233
for (auto it = s_friend_list.cbegin(); it != s_friend_list.cend();)
@@ -274,7 +274,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
274274

275275
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
276276
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
277-
if (ImGui::Begin("Low battery overlay", nullptr, kPopupFlags))
277+
if (ImGui_BeginPadDistinct("Low battery overlay", nullptr, kPopupFlags, pad))
278278
{
279279
auto it = batteries.cbegin();
280280
ImGui::TextUnformatted((const char*)(s_blink_state ? ICON_FA_BATTERY_EMPTY : ICON_FA_BATTERY_QUARTER));
@@ -322,7 +322,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
322322
{
323323
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
324324
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
325-
if (ImGui::Begin("Compiling shaders overlay", nullptr, kPopupFlags))
325+
if (ImGui_BeginPadDistinct("Compiling shaders overlay", nullptr, kPopupFlags, pad))
326326
{
327327
ImRotateStart();
328328
ImGui::TextUnformatted((const char*)ICON_FA_SPINNER);
@@ -377,7 +377,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
377377
{
378378
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
379379
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
380-
if (ImGui::Begin("Compiling pipeline overlay", nullptr, kPopupFlags))
380+
if (ImGui_BeginPadDistinct("Compiling pipeline overlay", nullptr, kPopupFlags, pad))
381381
{
382382
ImRotateStart();
383383
ImGui::TextUnformatted((const char*)ICON_FA_SPINNER);
@@ -446,7 +446,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
446446
{
447447
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
448448
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
449-
if (ImGui::Begin("Misc notifications", nullptr, kPopupFlags))
449+
if (ImGui_BeginPadDistinct("Misc notifications", nullptr, kPopupFlags, pad))
450450
{
451451
const auto tick = tick_cached();
452452
for (auto it = s_misc_notifications.cbegin(); it != s_misc_notifications.cend();)
@@ -521,12 +521,18 @@ void LatteOverlay_render(bool pad_view)
521521
return;
522522

523523
const Vector2f window_size{ (float)w,(float)h };
524-
524+
525+
float fontDPIScale = !pad_view ? gui_getWindowDPIScale() : gui_getPadDPIScale();
526+
527+
float overlayFontSize = 14.0f * (float)config.overlay.text_scale / 100.0f * fontDPIScale;
528+
525529
// test if fonts are already precached
526-
if (!ImGui_GetFont(14.0f * (float)config.overlay.text_scale / 100.0f))
530+
if (!ImGui_GetFont(overlayFontSize))
527531
return;
532+
533+
float notificationsFontSize = 14.0f * (float)config.notification.text_scale / 100.0f * fontDPIScale;
528534

529-
if (!ImGui_GetFont(14.0f * (float)config.notification.text_scale / 100.0f))
535+
if (!ImGui_GetFont(notificationsFontSize))
530536
return;
531537

532538
ImVec2 position{}, pivot{};
@@ -535,7 +541,7 @@ void LatteOverlay_render(bool pad_view)
535541
if (config.overlay.position != ScreenPosition::kDisabled)
536542
{
537543
LatteOverlay_translateScreenPosition(config.overlay.position, window_size, position, pivot, direction);
538-
LatteOverlay_renderOverlay(position, pivot, direction);
544+
LatteOverlay_renderOverlay(position, pivot, direction, overlayFontSize, pad_view);
539545
}
540546

541547

@@ -544,7 +550,7 @@ void LatteOverlay_render(bool pad_view)
544550
if(config.overlay.position != config.notification.position)
545551
LatteOverlay_translateScreenPosition(config.notification.position, window_size, position, pivot, direction);
546552

547-
LatteOverlay_RenderNotifications(position, pivot, direction);
553+
LatteOverlay_RenderNotifications(position, pivot, direction, notificationsFontSize, pad_view);
548554
}
549555
}
550556

src/gui/MainWindow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,7 @@ void MainWindow::OnSizeEvent(wxSizeEvent& event)
15231523
g_window_info.height = client_size.GetHeight();
15241524
g_window_info.phys_width = ToPhys(client_size.GetWidth());
15251525
g_window_info.phys_height = ToPhys(client_size.GetHeight());
1526+
g_window_info.dpi_scale = GetDPIScaleFactor();
15261527

15271528
if (m_debugger_window && m_debugger_window->IsShown())
15281529
m_debugger_window->OnParentMove(GetPosition(), event.GetSize());
@@ -1540,6 +1541,7 @@ void MainWindow::OnDPIChangedEvent(wxDPIChangedEvent& event)
15401541
g_window_info.height = client_size.GetHeight();
15411542
g_window_info.phys_width = ToPhys(client_size.GetWidth());
15421543
g_window_info.phys_height = ToPhys(client_size.GetHeight());
1544+
g_window_info.dpi_scale = GetDPIScaleFactor();
15431545
}
15441546

15451547
void MainWindow::OnMove(wxMoveEvent& event)

src/gui/PadViewFrame.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ void PadViewFrame::OnSizeEvent(wxSizeEvent& event)
103103
g_window_info.pad_height = client_size.GetHeight();
104104
g_window_info.phys_pad_width = ToPhys(client_size.GetWidth());
105105
g_window_info.phys_pad_height = ToPhys(client_size.GetHeight());
106+
g_window_info.pad_dpi_scale = GetDPIScaleFactor();
106107

107108
event.Skip();
108109
}
@@ -115,6 +116,7 @@ void PadViewFrame::OnDPIChangedEvent(wxDPIChangedEvent& event)
115116
g_window_info.pad_height = client_size.GetHeight();
116117
g_window_info.phys_pad_width = ToPhys(client_size.GetWidth());
117118
g_window_info.phys_pad_height = ToPhys(client_size.GetHeight());
119+
g_window_info.pad_dpi_scale = GetDPIScaleFactor();
118120
}
119121

120122
void PadViewFrame::OnMoveEvent(wxMoveEvent& event)

src/gui/guiWrapper.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ void gui_getWindowSize(int& w, int& h)
143143
h = g_window_info.height;
144144
}
145145

146-
147146
void gui_getPadWindowSize(int& w, int& h)
148147
{
149148
if (g_window_info.pad_open)
@@ -178,6 +177,16 @@ void gui_getPadWindowPhysSize(int& w, int& h)
178177
}
179178
}
180179

180+
double gui_getWindowDPIScale()
181+
{
182+
return g_window_info.dpi_scale;
183+
}
184+
185+
double gui_getPadDPIScale()
186+
{
187+
return g_window_info.pad_open ? g_window_info.pad_dpi_scale.load() : 1.0;
188+
}
189+
181190
bool gui_isPadWindowOpen()
182191
{
183192
return g_window_info.pad_open;

src/gui/guiWrapper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ struct WindowInfo
5757

5858
std::atomic_int32_t width, height; // client size of main window
5959
std::atomic_int32_t phys_width, phys_height; // client size of main window in physical pixels
60+
std::atomic<double> dpi_scale;
6061

6162
std::atomic_bool pad_open; // if separate pad view is open
6263
std::atomic_int32_t pad_width, pad_height; // client size of pad window
6364
std::atomic_int32_t phys_pad_width, phys_pad_height; // client size of pad window in physical pixels
65+
std::atomic<double> pad_dpi_scale;
6466

6567
std::atomic_bool pad_maximized = false;
6668
std::atomic_int32_t restored_pad_x = -1, restored_pad_y = -1;
@@ -118,6 +120,8 @@ void gui_getWindowSize(int& w, int& h);
118120
void gui_getPadWindowSize(int& w, int& h);
119121
void gui_getWindowPhysSize(int& w, int& h);
120122
void gui_getPadWindowPhysSize(int& w, int& h);
123+
double gui_getWindowDPIScale();
124+
double gui_getPadDPIScale();
121125
bool gui_isPadWindowOpen();
122126
bool gui_isKeyDown(uint32 key);
123127
bool gui_isKeyDown(PlatformKeyCodes key);

src/imgui/imgui_extension.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ extern char const g_fontawesome_data[];
5050
std::unordered_map<int, ImFont*> g_imgui_fonts;
5151
std::stack<int> g_font_requests;
5252

53+
bool ImGui_BeginPadDistinct(const char* name, bool* p_open, ImGuiWindowFlags flags, bool pad)
54+
{
55+
std::string distinctName = name;
56+
if (pad)
57+
distinctName += "##pad";
58+
return ImGui::Begin(distinctName.c_str(), p_open, flags);
59+
}
60+
5361
void ImGui_PrecacheFonts()
5462
{
5563
while (!g_font_requests.empty())

src/imgui/imgui_extension.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ inline ImVec2 operator-(const ImVec2& l, const ImVec2& r) { return{ l.x - r.x, l
99
inline bool operator<(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x < rhs.x; }
1010
inline bool operator>=(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x >= rhs.x; }
1111

12+
bool ImGui_BeginPadDistinct(const char* name, bool* p_open, ImGuiWindowFlags flags, bool pad);
13+
1214
void ImGui_PrecacheFonts();
1315
ImFont* ImGui_GetFont(float size);
1416
void ImGui_UpdateWindowInformation(bool mainWindow);

0 commit comments

Comments
 (0)