Fix HUD scaling (#9721)
authorMaksim <MoNTE48@mail.ua>
Fri, 5 Jun 2020 19:42:33 +0000 (21:42 +0200)
committerGitHub <noreply@github.com>
Fri, 5 Jun 2020 19:42:33 +0000 (21:42 +0200)
This resolves HUD scaling issues on Android and desktops with custom DPI settings.

src/client/hud.cpp
src/client/hud.h

index 4edc229b2b7af02d87ac494a5af497538cbf3e89..31e633bc2618c2d89c2668d7bf063376c2e13be5 100644 (file)
@@ -51,6 +51,7 @@ Hud::Hud(gui::IGUIEnvironment *guienv, Client *client, LocalPlayer *player,
        this->inventory   = inventory;
 
        m_hud_scaling      = g_settings->getFloat("hud_scaling");
+       m_scale_factor     = m_hud_scaling * RenderingEngine::getDisplayDensity();
        m_hotbar_imagesize = std::floor(HOTBAR_IMAGE_SIZE *
                RenderingEngine::getDisplayDensity() + 0.5f);
        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
-       v2s32 pos = screen_offset;
-       pos.X *= m_hud_scaling * RenderingEngine::getDisplayDensity();
-       pos.Y *= m_hud_scaling * RenderingEngine::getDisplayDensity();
+       v2s32 pos = screen_offset * m_scale_factor;
        pos += upperleftpos;
 
        // Store hotbar_image in member variable, used by drawItem()
@@ -320,20 +319,40 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
                switch (e->type) {
                        case HUD_ELEM_TEXT: {
                                irr::gui::IGUIFont *textfont = font;
+                               unsigned int font_size = g_fontengine->getDefaultFontSize();
+
                                if (e->size.X > 0)
-                                       textfont = g_fontengine->getFont(
-                                               e->size.X * g_fontengine->getDefaultFontSize());
+                                       font_size *= e->size.X;
+
+                               if (font_size != g_fontengine->getDefaultFontSize())
+                                       textfont = g_fontengine->getFont(font_size);
 
                                video::SColor color(255, (e->number >> 16) & 0xFF,
                                                                                 (e->number >> 8)  & 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));
                                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),
                                             (e->align.Y - 1.0) * (textsize.Height / 2));
-                               v2s32 offs(e->offset.X, e->offset.Y);
-                               textfont->draw(text.c_str(), size + pos + offset + offs, color);
+                               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);
+                               }
                                break; }
                        case HUD_ELEM_STATBAR: {
                                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 colors[] = {color, color, color, color};
                                core::dimension2di imgsize(texture->getOriginalSize());
-                               v2s32 dstsize(imgsize.Width * e->scale.X,
-                                             imgsize.Height * e->scale.Y);
+                               v2s32 dstsize(imgsize.Width * e->scale.X * m_scale_factor,
+                                             imgsize.Height * e->scale.Y * m_scale_factor);
                                if (e->scale.X < 0)
                                        dstsize.X = m_screensize.X * (e->scale.X * -0.01);
                                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,
                                             (e->align.Y - 1.0) * dstsize.Y / 2);
                                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,
                                        core::rect<s32>(core::position2d<s32>(0,0), imgsize),
                                        NULL, colors, true);
@@ -427,12 +447,15 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
        core::dimension2di dstd;
        if (size == v2s32()) {
                dstd = srcd;
+               dstd.Height *= m_scale_factor;
+               dstd.Width  *= m_scale_factor;
+               offset.X *= m_scale_factor;
+               offset.Y *= m_scale_factor;
        } else {
-               float size_factor = m_hud_scaling * RenderingEngine::getDisplayDensity();
-               dstd.Height = size.Y * size_factor;
-               dstd.Width  = size.X * size_factor;
-               offset.X *= size_factor;
-               offset.Y *= size_factor;
+               dstd.Height = size.Y * m_scale_factor;
+               dstd.Width  = size.X * m_scale_factor;
+               offset.X *= m_scale_factor;
+               offset.Y *= m_scale_factor;
        }
 
        v2s32 p = pos;
@@ -555,7 +578,7 @@ void Hud::drawHotbar(u16 playeritem) {
        v2s32 pos = centerlowerpos - v2s32(width / 2, m_hotbar_imagesize + m_padding * 3);
 
        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")) {
                if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE) {
                        drawItems(pos, v2s32(0, 0), hotbar_itemcount, 0, mainlist, playeritem + 1, 0);
@@ -607,12 +630,10 @@ void Hud::drawSelectionMesh()
                // Draw 3D selection boxes
                video::SMaterial oldmaterial = driver->getMaterial2D();
                driver->setMaterial(m_selection_material);
-               for (std::vector<aabb3f>::const_iterator
-                               i = m_selection_boxes.begin();
-                               i != m_selection_boxes.end(); ++i) {
+               for (auto & selection_box : m_selection_boxes) {
                        aabb3f box = aabb3f(
-                               i->MinEdge + m_selection_pos_with_offset,
-                               i->MaxEdge + m_selection_pos_with_offset);
+                               selection_box.MinEdge + m_selection_pos_with_offset,
+                               selection_box.MaxEdge + m_selection_pos_with_offset);
 
                        u32 r = (selectionbox_argb.getRed() *
                                        m_selection_mesh_color.getRed() / 255);
index e2abf326c7cd1f7590d570942a083f7f33fd651b..6f4c5462615887f4da55a812ebaaf6b8a7659778 100644 (file)
@@ -92,6 +92,7 @@ private:
        void drawItem(const ItemStack &item, const core::rect<s32> &rect, bool selected);
 
        float m_hud_scaling; // cached minetest setting
+       float m_scale_factor;
        v3s16 m_camera_offset;
        v2u32 m_screensize;
        v2s32 m_displaycenter;