Fix HUD scaling (#9721)

This resolves HUD scaling issues on Android and desktops with custom DPI settings.
master
Maksim 2020-06-05 21:42:33 +02:00 committed by GitHub
parent 08c0b7696a
commit 7ec0e3df35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 22 deletions

View File

@ -51,6 +51,7 @@ Hud::Hud(gui::IGUIEnvironment *guienv, Client *client, LocalPlayer *player,
this->inventory = inventory; this->inventory = inventory;
m_hud_scaling = g_settings->getFloat("hud_scaling"); m_hud_scaling = g_settings->getFloat("hud_scaling");
m_scale_factor = m_hud_scaling * RenderingEngine::getDisplayDensity();
m_hotbar_imagesize = std::floor(HOTBAR_IMAGE_SIZE * m_hotbar_imagesize = std::floor(HOTBAR_IMAGE_SIZE *
RenderingEngine::getDisplayDensity() + 0.5f); RenderingEngine::getDisplayDensity() + 0.5f);
m_hotbar_imagesize *= m_hud_scaling; m_hotbar_imagesize *= m_hud_scaling;
@ -213,9 +214,7 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
} }
// Position of upper left corner of bar // Position of upper left corner of bar
v2s32 pos = screen_offset; v2s32 pos = screen_offset * m_scale_factor;
pos.X *= m_hud_scaling * RenderingEngine::getDisplayDensity();
pos.Y *= m_hud_scaling * RenderingEngine::getDisplayDensity();
pos += upperleftpos; pos += upperleftpos;
// Store hotbar_image in member variable, used by drawItem() // Store hotbar_image in member variable, used by drawItem()
@ -320,20 +319,40 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
switch (e->type) { switch (e->type) {
case HUD_ELEM_TEXT: { case HUD_ELEM_TEXT: {
irr::gui::IGUIFont *textfont = font; irr::gui::IGUIFont *textfont = font;
unsigned int font_size = g_fontengine->getDefaultFontSize();
if (e->size.X > 0) if (e->size.X > 0)
textfont = g_fontengine->getFont( font_size *= e->size.X;
e->size.X * g_fontengine->getDefaultFontSize());
if (font_size != g_fontengine->getDefaultFontSize())
textfont = g_fontengine->getFont(font_size);
video::SColor color(255, (e->number >> 16) & 0xFF, video::SColor color(255, (e->number >> 16) & 0xFF,
(e->number >> 8) & 0xFF, (e->number >> 8) & 0xFF,
(e->number >> 0) & 0xFF); (e->number >> 0) & 0xFF);
core::rect<s32> size(0, 0, e->scale.X, text_height * e->scale.Y);
std::wstring text = unescape_translate(utf8_to_wide(e->text)); std::wstring text = unescape_translate(utf8_to_wide(e->text));
core::dimension2d<u32> textsize = textfont->getDimension(text.c_str()); core::dimension2d<u32> textsize = textfont->getDimension(text.c_str());
#ifdef __ANDROID__
// The text size on Android is not proportional with the actual scaling
irr::gui::IGUIFont *font_scaled = font_size <= 3 ?
textfont : g_fontengine->getFont(font_size - 3);
if (e->offset.X < -20)
textsize = font_scaled->getDimension(text.c_str());
#endif
v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2), v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2),
(e->align.Y - 1.0) * (textsize.Height / 2)); (e->align.Y - 1.0) * (textsize.Height / 2));
v2s32 offs(e->offset.X, e->offset.Y); core::rect<s32> size(0, 0, e->scale.X * m_scale_factor,
text_height * e->scale.Y * m_scale_factor);
v2s32 offs(e->offset.X * m_scale_factor,
e->offset.Y * m_scale_factor);
#ifdef __ANDROID__
if (e->offset.X < -20)
font_scaled->draw(text.c_str(), size + pos + offset + offs, color);
else
#endif
{
textfont->draw(text.c_str(), size + pos + offset + offs, color); textfont->draw(text.c_str(), size + pos + offset + offs, color);
}
break; } break; }
case HUD_ELEM_STATBAR: { case HUD_ELEM_STATBAR: {
v2s32 offs(e->offset.X, e->offset.Y); v2s32 offs(e->offset.X, e->offset.Y);
@ -385,8 +404,8 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
const video::SColor color(255, 255, 255, 255); const video::SColor color(255, 255, 255, 255);
const video::SColor colors[] = {color, color, color, color}; const video::SColor colors[] = {color, color, color, color};
core::dimension2di imgsize(texture->getOriginalSize()); core::dimension2di imgsize(texture->getOriginalSize());
v2s32 dstsize(imgsize.Width * e->scale.X, v2s32 dstsize(imgsize.Width * e->scale.X * m_scale_factor,
imgsize.Height * e->scale.Y); imgsize.Height * e->scale.Y * m_scale_factor);
if (e->scale.X < 0) if (e->scale.X < 0)
dstsize.X = m_screensize.X * (e->scale.X * -0.01); dstsize.X = m_screensize.X * (e->scale.X * -0.01);
if (e->scale.Y < 0) if (e->scale.Y < 0)
@ -394,7 +413,8 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
v2s32 offset((e->align.X - 1.0) * dstsize.X / 2, v2s32 offset((e->align.X - 1.0) * dstsize.X / 2,
(e->align.Y - 1.0) * dstsize.Y / 2); (e->align.Y - 1.0) * dstsize.Y / 2);
core::rect<s32> rect(0, 0, dstsize.X, dstsize.Y); core::rect<s32> rect(0, 0, dstsize.X, dstsize.Y);
rect += pos + offset + v2s32(e->offset.X, e->offset.Y); rect += pos + offset + v2s32(e->offset.X * m_scale_factor,
e->offset.Y * m_scale_factor);
draw2DImageFilterScaled(driver, texture, rect, draw2DImageFilterScaled(driver, texture, rect,
core::rect<s32>(core::position2d<s32>(0,0), imgsize), core::rect<s32>(core::position2d<s32>(0,0), imgsize),
NULL, colors, true); NULL, colors, true);
@ -427,12 +447,15 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
core::dimension2di dstd; core::dimension2di dstd;
if (size == v2s32()) { if (size == v2s32()) {
dstd = srcd; dstd = srcd;
dstd.Height *= m_scale_factor;
dstd.Width *= m_scale_factor;
offset.X *= m_scale_factor;
offset.Y *= m_scale_factor;
} else { } else {
float size_factor = m_hud_scaling * RenderingEngine::getDisplayDensity(); dstd.Height = size.Y * m_scale_factor;
dstd.Height = size.Y * size_factor; dstd.Width = size.X * m_scale_factor;
dstd.Width = size.X * size_factor; offset.X *= m_scale_factor;
offset.X *= size_factor; offset.Y *= m_scale_factor;
offset.Y *= size_factor;
} }
v2s32 p = pos; v2s32 p = pos;
@ -555,7 +578,7 @@ void Hud::drawHotbar(u16 playeritem) {
v2s32 pos = centerlowerpos - v2s32(width / 2, m_hotbar_imagesize + m_padding * 3); v2s32 pos = centerlowerpos - v2s32(width / 2, m_hotbar_imagesize + m_padding * 3);
const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize(); const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
if ( (float) width / (float) window_size.X <= if ((float) width / (float) window_size.X <=
g_settings->getFloat("hud_hotbar_max_width")) { g_settings->getFloat("hud_hotbar_max_width")) {
if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE) { if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE) {
drawItems(pos, v2s32(0, 0), hotbar_itemcount, 0, mainlist, playeritem + 1, 0); drawItems(pos, v2s32(0, 0), hotbar_itemcount, 0, mainlist, playeritem + 1, 0);
@ -607,12 +630,10 @@ void Hud::drawSelectionMesh()
// Draw 3D selection boxes // Draw 3D selection boxes
video::SMaterial oldmaterial = driver->getMaterial2D(); video::SMaterial oldmaterial = driver->getMaterial2D();
driver->setMaterial(m_selection_material); driver->setMaterial(m_selection_material);
for (std::vector<aabb3f>::const_iterator for (auto & selection_box : m_selection_boxes) {
i = m_selection_boxes.begin();
i != m_selection_boxes.end(); ++i) {
aabb3f box = aabb3f( aabb3f box = aabb3f(
i->MinEdge + m_selection_pos_with_offset, selection_box.MinEdge + m_selection_pos_with_offset,
i->MaxEdge + m_selection_pos_with_offset); selection_box.MaxEdge + m_selection_pos_with_offset);
u32 r = (selectionbox_argb.getRed() * u32 r = (selectionbox_argb.getRed() *
m_selection_mesh_color.getRed() / 255); m_selection_mesh_color.getRed() / 255);

View File

@ -92,6 +92,7 @@ private:
void drawItem(const ItemStack &item, const core::rect<s32> &rect, bool selected); void drawItem(const ItemStack &item, const core::rect<s32> &rect, bool selected);
float m_hud_scaling; // cached minetest setting float m_hud_scaling; // cached minetest setting
float m_scale_factor;
v3s16 m_camera_offset; v3s16 m_camera_offset;
v2u32 m_screensize; v2u32 m_screensize;
v2s32 m_displaycenter; v2s32 m_displaycenter;