Automatically scale imgui text based on display pixel density (#545)

This commit is contained in:
goeiecool9999 2022-12-01 15:58:08 +01:00 committed by GitHub
parent 503e99d069
commit 2cfb7f3737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 19 deletions

View File

@ -66,11 +66,11 @@ struct OverlayList
const auto kPopupFlags = ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav; const auto kPopupFlags = ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav;
const float kBackgroundAlpha = 0.65f; const float kBackgroundAlpha = 0.65f;
void LatteOverlay_renderOverlay(ImVec2& position, ImVec2& pivot, sint32 direction) void LatteOverlay_renderOverlay(ImVec2& position, ImVec2& pivot, sint32 direction, float fontSize, bool pad)
{ {
auto& config = GetConfig(); auto& config = GetConfig();
const auto font = ImGui_GetFont(14.0f * (float)config.overlay.text_scale / 100.0f); const auto font = ImGui_GetFont(fontSize);
ImGui::PushFont(font); ImGui::PushFont(font);
const ImVec4 color = ImGui::ColorConvertU32ToFloat4(config.overlay.text_color); const ImVec4 color = ImGui::ColorConvertU32ToFloat4(config.overlay.text_color);
@ -80,7 +80,7 @@ void LatteOverlay_renderOverlay(ImVec2& position, ImVec2& pivot, sint32 directio
{ {
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot); ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha); ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
if (ImGui::Begin("Stats overlay", nullptr, kPopupFlags)) if (ImGui_BeginPadDistinct("Stats overlay", nullptr, kPopupFlags, pad))
{ {
if (config.overlay.fps) if (config.overlay.fps)
ImGui::Text("FPS: %.2lf", g_state.fps); ImGui::Text("FPS: %.2lf", g_state.fps);
@ -117,11 +117,11 @@ void LatteOverlay_renderOverlay(ImVec2& position, ImVec2& pivot, sint32 directio
ImGui::PopFont(); ImGui::PopFont();
} }
void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 direction) void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 direction, float fontSize, bool pad)
{ {
auto& config = GetConfig(); auto& config = GetConfig();
const auto font = ImGui_GetFont(14.0f * (float)config.notification.text_scale / 100.0f); const auto font = ImGui_GetFont(fontSize);
ImGui::PushFont(font); ImGui::PushFont(font);
const ImVec4 color = ImGui::ColorConvertU32ToFloat4(config.notification.text_color); const ImVec4 color = ImGui::ColorConvertU32ToFloat4(config.notification.text_color);
@ -141,7 +141,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
// active account // active account
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot); ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha); ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
if (ImGui::Begin("Active account", nullptr, kPopupFlags)) if (ImGui_BeginPadDistinct("Active account", nullptr, kPopupFlags, pad))
{ {
ImGui::TextUnformatted((const char*)ICON_FA_USER); ImGui::TextUnformatted((const char*)ICON_FA_USER);
ImGui::SameLine(); ImGui::SameLine();
@ -179,7 +179,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
{ {
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot); ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha); ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
if (ImGui::Begin("Controller profile names", nullptr, kPopupFlags)) if (ImGui_BeginPadDistinct("Controller profile names", nullptr, kPopupFlags, pad))
{ {
auto it = profiles.cbegin(); auto it = profiles.cbegin();
ImGui::TextUnformatted((const char*)ICON_FA_GAMEPAD); ImGui::TextUnformatted((const char*)ICON_FA_GAMEPAD);
@ -227,7 +227,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
{ {
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot); ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha); ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
if (ImGui::Begin("Friends overlay", nullptr, kPopupFlags)) if (ImGui_BeginPadDistinct("Friends overlay", nullptr, kPopupFlags, pad))
{ {
const auto tick = tick_cached(); const auto tick = tick_cached();
for (auto it = s_friend_list.cbegin(); it != s_friend_list.cend();) 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
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot); ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha); ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
if (ImGui::Begin("Low battery overlay", nullptr, kPopupFlags)) if (ImGui_BeginPadDistinct("Low battery overlay", nullptr, kPopupFlags, pad))
{ {
auto it = batteries.cbegin(); auto it = batteries.cbegin();
ImGui::TextUnformatted((const char*)(s_blink_state ? ICON_FA_BATTERY_EMPTY : ICON_FA_BATTERY_QUARTER)); 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
{ {
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot); ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha); ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
if (ImGui::Begin("Compiling shaders overlay", nullptr, kPopupFlags)) if (ImGui_BeginPadDistinct("Compiling shaders overlay", nullptr, kPopupFlags, pad))
{ {
ImRotateStart(); ImRotateStart();
ImGui::TextUnformatted((const char*)ICON_FA_SPINNER); ImGui::TextUnformatted((const char*)ICON_FA_SPINNER);
@ -377,7 +377,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
{ {
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot); ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha); ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
if (ImGui::Begin("Compiling pipeline overlay", nullptr, kPopupFlags)) if (ImGui_BeginPadDistinct("Compiling pipeline overlay", nullptr, kPopupFlags, pad))
{ {
ImRotateStart(); ImRotateStart();
ImGui::TextUnformatted((const char*)ICON_FA_SPINNER); ImGui::TextUnformatted((const char*)ICON_FA_SPINNER);
@ -446,7 +446,7 @@ void LatteOverlay_RenderNotifications(ImVec2& position, ImVec2& pivot, sint32 di
{ {
ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot); ImGui::SetNextWindowPos(position, ImGuiCond_Always, pivot);
ImGui::SetNextWindowBgAlpha(kBackgroundAlpha); ImGui::SetNextWindowBgAlpha(kBackgroundAlpha);
if (ImGui::Begin("Misc notifications", nullptr, kPopupFlags)) if (ImGui_BeginPadDistinct("Misc notifications", nullptr, kPopupFlags, pad))
{ {
const auto tick = tick_cached(); const auto tick = tick_cached();
for (auto it = s_misc_notifications.cbegin(); it != s_misc_notifications.cend();) for (auto it = s_misc_notifications.cbegin(); it != s_misc_notifications.cend();)
@ -521,12 +521,18 @@ void LatteOverlay_render(bool pad_view)
return; return;
const Vector2f window_size{ (float)w,(float)h }; const Vector2f window_size{ (float)w,(float)h };
float fontDPIScale = !pad_view ? gui_getWindowDPIScale() : gui_getPadDPIScale();
float overlayFontSize = 14.0f * (float)config.overlay.text_scale / 100.0f * fontDPIScale;
// test if fonts are already precached // test if fonts are already precached
if (!ImGui_GetFont(14.0f * (float)config.overlay.text_scale / 100.0f)) if (!ImGui_GetFont(overlayFontSize))
return; return;
float notificationsFontSize = 14.0f * (float)config.notification.text_scale / 100.0f * fontDPIScale;
if (!ImGui_GetFont(14.0f * (float)config.notification.text_scale / 100.0f)) if (!ImGui_GetFont(notificationsFontSize))
return; return;
ImVec2 position{}, pivot{}; ImVec2 position{}, pivot{};
@ -535,7 +541,7 @@ void LatteOverlay_render(bool pad_view)
if (config.overlay.position != ScreenPosition::kDisabled) if (config.overlay.position != ScreenPosition::kDisabled)
{ {
LatteOverlay_translateScreenPosition(config.overlay.position, window_size, position, pivot, direction); LatteOverlay_translateScreenPosition(config.overlay.position, window_size, position, pivot, direction);
LatteOverlay_renderOverlay(position, pivot, direction); LatteOverlay_renderOverlay(position, pivot, direction, overlayFontSize, pad_view);
} }
@ -544,7 +550,7 @@ void LatteOverlay_render(bool pad_view)
if(config.overlay.position != config.notification.position) if(config.overlay.position != config.notification.position)
LatteOverlay_translateScreenPosition(config.notification.position, window_size, position, pivot, direction); LatteOverlay_translateScreenPosition(config.notification.position, window_size, position, pivot, direction);
LatteOverlay_RenderNotifications(position, pivot, direction); LatteOverlay_RenderNotifications(position, pivot, direction, notificationsFontSize, pad_view);
} }
} }

View File

@ -1523,6 +1523,7 @@ void MainWindow::OnSizeEvent(wxSizeEvent& event)
g_window_info.height = client_size.GetHeight(); g_window_info.height = client_size.GetHeight();
g_window_info.phys_width = ToPhys(client_size.GetWidth()); g_window_info.phys_width = ToPhys(client_size.GetWidth());
g_window_info.phys_height = ToPhys(client_size.GetHeight()); g_window_info.phys_height = ToPhys(client_size.GetHeight());
g_window_info.dpi_scale = GetDPIScaleFactor();
if (m_debugger_window && m_debugger_window->IsShown()) if (m_debugger_window && m_debugger_window->IsShown())
m_debugger_window->OnParentMove(GetPosition(), event.GetSize()); m_debugger_window->OnParentMove(GetPosition(), event.GetSize());
@ -1540,6 +1541,7 @@ void MainWindow::OnDPIChangedEvent(wxDPIChangedEvent& event)
g_window_info.height = client_size.GetHeight(); g_window_info.height = client_size.GetHeight();
g_window_info.phys_width = ToPhys(client_size.GetWidth()); g_window_info.phys_width = ToPhys(client_size.GetWidth());
g_window_info.phys_height = ToPhys(client_size.GetHeight()); g_window_info.phys_height = ToPhys(client_size.GetHeight());
g_window_info.dpi_scale = GetDPIScaleFactor();
} }
void MainWindow::OnMove(wxMoveEvent& event) void MainWindow::OnMove(wxMoveEvent& event)

View File

@ -103,6 +103,7 @@ void PadViewFrame::OnSizeEvent(wxSizeEvent& event)
g_window_info.pad_height = client_size.GetHeight(); g_window_info.pad_height = client_size.GetHeight();
g_window_info.phys_pad_width = ToPhys(client_size.GetWidth()); g_window_info.phys_pad_width = ToPhys(client_size.GetWidth());
g_window_info.phys_pad_height = ToPhys(client_size.GetHeight()); g_window_info.phys_pad_height = ToPhys(client_size.GetHeight());
g_window_info.pad_dpi_scale = GetDPIScaleFactor();
event.Skip(); event.Skip();
} }
@ -115,6 +116,7 @@ void PadViewFrame::OnDPIChangedEvent(wxDPIChangedEvent& event)
g_window_info.pad_height = client_size.GetHeight(); g_window_info.pad_height = client_size.GetHeight();
g_window_info.phys_pad_width = ToPhys(client_size.GetWidth()); g_window_info.phys_pad_width = ToPhys(client_size.GetWidth());
g_window_info.phys_pad_height = ToPhys(client_size.GetHeight()); g_window_info.phys_pad_height = ToPhys(client_size.GetHeight());
g_window_info.pad_dpi_scale = GetDPIScaleFactor();
} }
void PadViewFrame::OnMoveEvent(wxMoveEvent& event) void PadViewFrame::OnMoveEvent(wxMoveEvent& event)

View File

@ -143,7 +143,6 @@ void gui_getWindowSize(int& w, int& h)
h = g_window_info.height; h = g_window_info.height;
} }
void gui_getPadWindowSize(int& w, int& h) void gui_getPadWindowSize(int& w, int& h)
{ {
if (g_window_info.pad_open) if (g_window_info.pad_open)
@ -178,6 +177,16 @@ void gui_getPadWindowPhysSize(int& w, int& h)
} }
} }
double gui_getWindowDPIScale()
{
return g_window_info.dpi_scale;
}
double gui_getPadDPIScale()
{
return g_window_info.pad_open ? g_window_info.pad_dpi_scale.load() : 1.0;
}
bool gui_isPadWindowOpen() bool gui_isPadWindowOpen()
{ {
return g_window_info.pad_open; return g_window_info.pad_open;

View File

@ -57,10 +57,12 @@ struct WindowInfo
std::atomic_int32_t width, height; // client size of main window std::atomic_int32_t width, height; // client size of main window
std::atomic_int32_t phys_width, phys_height; // client size of main window in physical pixels std::atomic_int32_t phys_width, phys_height; // client size of main window in physical pixels
std::atomic<double> dpi_scale;
std::atomic_bool pad_open; // if separate pad view is open std::atomic_bool pad_open; // if separate pad view is open
std::atomic_int32_t pad_width, pad_height; // client size of pad window std::atomic_int32_t pad_width, pad_height; // client size of pad window
std::atomic_int32_t phys_pad_width, phys_pad_height; // client size of pad window in physical pixels std::atomic_int32_t phys_pad_width, phys_pad_height; // client size of pad window in physical pixels
std::atomic<double> pad_dpi_scale;
std::atomic_bool pad_maximized = false; std::atomic_bool pad_maximized = false;
std::atomic_int32_t restored_pad_x = -1, restored_pad_y = -1; std::atomic_int32_t restored_pad_x = -1, restored_pad_y = -1;
@ -118,6 +120,8 @@ void gui_getWindowSize(int& w, int& h);
void gui_getPadWindowSize(int& w, int& h); void gui_getPadWindowSize(int& w, int& h);
void gui_getWindowPhysSize(int& w, int& h); void gui_getWindowPhysSize(int& w, int& h);
void gui_getPadWindowPhysSize(int& w, int& h); void gui_getPadWindowPhysSize(int& w, int& h);
double gui_getWindowDPIScale();
double gui_getPadDPIScale();
bool gui_isPadWindowOpen(); bool gui_isPadWindowOpen();
bool gui_isKeyDown(uint32 key); bool gui_isKeyDown(uint32 key);
bool gui_isKeyDown(PlatformKeyCodes key); bool gui_isKeyDown(PlatformKeyCodes key);

View File

@ -50,6 +50,14 @@ extern char const g_fontawesome_data[];
std::unordered_map<int, ImFont*> g_imgui_fonts; std::unordered_map<int, ImFont*> g_imgui_fonts;
std::stack<int> g_font_requests; std::stack<int> g_font_requests;
bool ImGui_BeginPadDistinct(const char* name, bool* p_open, ImGuiWindowFlags flags, bool pad)
{
std::string distinctName = name;
if (pad)
distinctName += "##pad";
return ImGui::Begin(distinctName.c_str(), p_open, flags);
}
void ImGui_PrecacheFonts() void ImGui_PrecacheFonts()
{ {
while (!g_font_requests.empty()) while (!g_font_requests.empty())

View File

@ -9,6 +9,8 @@ inline ImVec2 operator-(const ImVec2& l, const ImVec2& r) { return{ l.x - r.x, l
inline bool operator<(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x < rhs.x; } inline bool operator<(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x < rhs.x; }
inline bool operator>=(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x >= rhs.x; } inline bool operator>=(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x >= rhs.x; }
bool ImGui_BeginPadDistinct(const char* name, bool* p_open, ImGuiWindowFlags flags, bool pad);
void ImGui_PrecacheFonts(); void ImGui_PrecacheFonts();
ImFont* ImGui_GetFont(float size); ImFont* ImGui_GetFont(float size);
void ImGui_UpdateWindowInformation(bool mainWindow); void ImGui_UpdateWindowInformation(bool mainWindow);