ui: Account for pixel density and display scaling

This commit is contained in:
mborgerson
2026-05-19 17:51:49 -07:00
committed by GitHub
parent ebc2ddf786
commit e00c1511d1
7 changed files with 39 additions and 44 deletions

View File

@ -279,7 +279,7 @@ display:
values: [native, auto, 4x3, 16x9]
default: auto
scale:
type: integer
type: float
default: 1
auto_scale:
type: bool

View File

@ -29,90 +29,86 @@ FontManager g_font_mgr;
FontManager::FontManager()
{
m_last_viewport_scale = 1;
m_font_scale = 1;
m_last_pixel_density = 1;
}
void FontManager::Rebuild()
{
ImGuiIO &io = ImGui::GetIO();
// FIXME: Trim FA to only glyphs in use
io.Fonts->Clear();
float scale = g_viewport_mgr.m_scale;
float pixel_density = g_viewport_mgr.m_pixel_density;
{
ImFontConfig config;
config.FontDataOwnedByAtlas = false;
config.RasterizerDensity = pixel_density;
m_default_font = io.Fonts->AddFontFromMemoryTTF(
(void *)Roboto_Medium_data, Roboto_Medium_size,
16.0f * g_viewport_mgr.m_scale * m_font_scale, &config);
16.0f * scale, &config);
m_menu_font_small = io.Fonts->AddFontFromMemoryTTF(
(void *)RobotoCondensed_Regular_data, RobotoCondensed_Regular_size,
22.0f * g_viewport_mgr.m_scale * m_font_scale, &config);
22.0f * scale, &config);
}
{
ImFontConfig config;
config.FontDataOwnedByAtlas = false;
config.RasterizerDensity = pixel_density;
config.MergeMode = true;
config.GlyphOffset =
ImVec2(0, 13 * g_viewport_mgr.m_scale * m_font_scale);
config.GlyphMaxAdvanceX = 24.0f * g_viewport_mgr.m_scale * m_font_scale;
ImVec2(0, 13 * scale);
config.GlyphMaxAdvanceX = 24.0f * scale;
static const ImWchar icon_ranges[] = { 0xf900, 0xf903, 0 };
io.Fonts->AddFontFromMemoryTTF((void *)abxy_data, abxy_size,
40.0f * g_viewport_mgr.m_scale *
m_font_scale,
40.0f * scale,
&config, icon_ranges);
}
{
ImFontConfig config;
config.FontDataOwnedByAtlas = false;
config.RasterizerDensity = pixel_density;
m_menu_font_medium = io.Fonts->AddFontFromMemoryTTF(
(void *)RobotoCondensed_Regular_data, RobotoCondensed_Regular_size,
26.0f * g_viewport_mgr.m_scale * m_font_scale, &config);
26.0f * scale, &config);
m_menu_font = io.Fonts->AddFontFromMemoryTTF(
(void *)RobotoCondensed_Regular_data, RobotoCondensed_Regular_size,
34.0f * g_viewport_mgr.m_scale * m_font_scale, &config);
34.0f * scale, &config);
}
{
ImFontConfig config;
config.FontDataOwnedByAtlas = false;
config.RasterizerDensity = pixel_density;
config.MergeMode = true;
config.GlyphOffset =
ImVec2(0, -3 * g_viewport_mgr.m_scale * m_font_scale);
config.GlyphMinAdvanceX = 32.0f * g_viewport_mgr.m_scale * m_font_scale;
ImVec2(0, -3 * scale);
config.GlyphMinAdvanceX = 32.0f * scale;
static const ImWchar icon_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
io.Fonts->AddFontFromMemoryTTF((void *)font_awesome_6_1_1_solid_min_data,
font_awesome_6_1_1_solid_min_size,
18.0f * g_viewport_mgr.m_scale *
m_font_scale,
18.0f * scale,
&config, icon_ranges);
}
// {
// ImFontConfig config;
// config.FontDataOwnedByAtlas = false;
// static const ImWchar icon_ranges[] = { 0xf04c, 0xf04c, 0 };
// m_big_state_icon_font = io.Fonts->AddFontFromMemoryTTF(
// (void *)font_awesome_6_1_1_solid_data,
// font_awesome_6_1_1_solid_size,
// 64.0f * g_viewport_mgr.m_scale * m_font_scale, &config,
// icon_ranges);
// }
{
ImFontConfig config = ImFontConfig();
config.OversampleH = config.OversampleV = 1;
config.PixelSnapH = true;
config.SizePixels = 13.0f*g_viewport_mgr.m_scale;
config.RasterizerDensity = pixel_density;
config.SizePixels = 13.0f * scale;
m_fixed_width_font = io.Fonts->AddFontDefault(&config);
}
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
}
void FontManager::Update()
{
if (g_viewport_mgr.m_scale != m_last_viewport_scale) {
if (g_viewport_mgr.m_scale != m_last_viewport_scale ||
g_viewport_mgr.m_pixel_density != m_last_pixel_density) {
Rebuild();
m_last_viewport_scale = g_viewport_mgr.m_scale;
m_last_pixel_density = g_viewport_mgr.m_pixel_density;
}
}

View File

@ -35,7 +35,7 @@ public:
ImFont *m_menu_font_medium;
// ImFont *m_big_state_icon_font;
float m_last_viewport_scale;
float m_font_scale;
float m_last_pixel_density;
FontManager();
void Rebuild();

View File

@ -805,7 +805,7 @@ void MainMenuDisplayView::Draw()
ui_scale_idx = 0;
} else {
ui_scale_idx = g_config.display.ui.scale;
if (ui_scale_idx < 0) ui_scale_idx = 0;
if (ui_scale_idx < 1) ui_scale_idx = 1;
else if (ui_scale_idx > 2) ui_scale_idx = 2;
}
if (ChevronCombo("UI scale", &ui_scale_idx,

View File

@ -160,7 +160,7 @@ void ShowMainMenu()
ui_scale_idx = 0;
} else {
ui_scale_idx = g_config.display.ui.scale;
if (ui_scale_idx < 0) ui_scale_idx = 0;
if (ui_scale_idx < 1) ui_scale_idx = 1;
else if (ui_scale_idx > 2) ui_scale_idx = 2;
}
if (ImGui::Combo("UI Scale", &ui_scale_idx,

View File

@ -17,11 +17,13 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "viewport-manager.hh"
#include "xemu-hud.h"
ViewportManager g_viewport_mgr;
ViewportManager::ViewportManager() {
m_scale = 1;
m_pixel_density = 1;
m_extents.x = 25 * m_scale; // Distance from Left
m_extents.y = 25 * m_scale; // '' Top
m_extents.z = 25 * m_scale; // '' Right
@ -62,21 +64,17 @@ void ViewportManager::Update()
{
ImGuiIO &io = ImGui::GetIO();
SDL_Window *window = xemu_get_window();
m_pixel_density = fmaxf(SDL_GetWindowPixelDensity(window), 1.0f);
if (g_config.display.ui.auto_scale) {
if (io.DisplaySize.x > 1920) {
g_config.display.ui.scale = 2;
} else {
g_config.display.ui.scale = 1;
}
float window_display_scale = fmaxf(SDL_GetWindowDisplayScale(window), 1.0f);
m_scale = window_display_scale / m_pixel_density;
} else {
m_scale = g_config.display.ui.scale;
}
m_scale = g_config.display.ui.scale;
if (m_scale < 1) {
m_scale = 1;
} else if (m_scale > 2) {
m_scale = 2;
}
m_scale = fmaxf(m_scale, 1.0);
if (io.DisplaySize.x > 640*m_scale) {
m_extents.x = 25 * m_scale; // Distance from Left

View File

@ -26,6 +26,7 @@ protected:
public:
float m_scale;
float m_pixel_density;
ViewportManager();
ImVec4 GetExtents();
void DrawExtents();