Android: Improve UI scaling on smaller high-density displays (#7834)
authorstujones11 <stujones111@gmail.com>
Sun, 18 Nov 2018 10:31:19 +0000 (10:31 +0000)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Sun, 18 Nov 2018 10:31:19 +0000 (11:31 +0100)
* Android: Improve UI scaling on smaller high-density displays

builtin/mainmenu/tab_settings.lua
src/client/clientlauncher.cpp
src/gui/guiFormSpecMenu.cpp
src/gui/guiTable.cpp
textures/base/pack/checkbox_16.png [new file with mode: 0644]
textures/base/pack/checkbox_32.png [new file with mode: 0644]
textures/base/pack/checkbox_64.png [new file with mode: 0644]

index 11611919e2e99be4715c3810323792726ef61b1d..7b2cee6ac1550721275c376dce9b7b8aec21a687 100644 (file)
@@ -237,9 +237,10 @@ local function formspec(tabview, name, tabdata)
 
        if core.settings:get("touchscreen_threshold") ~= nil then
                tab_string = tab_string ..
-                       "label[4.3,4.1;" .. fgettext("Touchthreshold (px)") .. "]" ..
-                       "dropdown[3.85,4.55;3.85;dd_touchthreshold;0,10,20,30,40,50;" ..
-                       ((tonumber(core.settings:get("touchscreen_threshold")) / 10) + 1) .. "]"
+                       "label[4.3,4.2;" .. fgettext("Touchthreshold: (px)") .. "]" ..
+                       "dropdown[4.25,4.65;3.5;dd_touchthreshold;0,10,20,30,40,50;" ..
+                       ((tonumber(core.settings:get("touchscreen_threshold")) / 10) + 1) ..
+                       "]box[4.0,4.5;3.75,1.0;#999999]"
        end
 
        if shaders_enabled then
index 85063ff86503d6863a6a42e016f5b166415b5bce..0820177b4d38e47a323a0266c374533990ca8f61 100644 (file)
@@ -38,6 +38,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #if USE_SOUND
        #include "sound_openal.h"
 #endif
+#ifdef __ANDROID__
+       #include "porting.h"
+#endif
 
 /* mainmenumanager.h
  */
@@ -127,7 +130,30 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
        skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(255, 0, 0, 0));
        skin->setColor(gui::EGDC_HIGH_LIGHT, video::SColor(255, 70, 120, 50));
        skin->setColor(gui::EGDC_HIGH_LIGHT_TEXT, video::SColor(255, 255, 255, 255));
-
+#ifdef __ANDROID__
+       float density = porting::getDisplayDensity();
+       skin->setSize(gui::EGDS_CHECK_BOX_WIDTH, (s32)(17.0f * density));
+       skin->setSize(gui::EGDS_SCROLLBAR_SIZE, (s32)(14.0f * density));
+       skin->setSize(gui::EGDS_WINDOW_BUTTON_WIDTH, (s32)(15.0f * density));
+       if (density > 1.5f) {
+               std::string sprite_path = porting::path_user + "/textures/base/pack/";
+               if (density > 3.5f)
+                       sprite_path.append("checkbox_64.png");
+               else if (density > 2.0f)
+                       sprite_path.append("checkbox_32.png");
+               else
+                       sprite_path.append("checkbox_16.png");
+               // Texture dimensions should be a power of 2
+               gui::IGUISpriteBank *sprites = skin->getSpriteBank();
+               video::IVideoDriver *driver = RenderingEngine::get_video_driver();
+               video::ITexture *sprite_texture = driver->getTexture(sprite_path.c_str());
+               if (sprite_texture) {
+                       s32 sprite_id = sprites->addTextureAsSprite(sprite_texture);
+                       if (sprite_id != -1)
+                               skin->setIcon(gui::EGDI_CHECK_BOX_CHECKED, sprite_id);
+               }
+       }
+#endif
        g_fontengine = new FontEngine(g_settings, guienv);
        FATAL_ERROR_IF(g_fontengine == NULL, "Font engine creation failed.");
 
index 363128012bf626012bfe29ed15feb7e1b4a2f1be..f2305582192d4c229584da06cd2e28b41ede8d80 100644 (file)
@@ -284,12 +284,13 @@ void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
                data->invsize.Y = MYMAX(0, stof(parts[1]));
 
                lockSize(false);
+#ifndef __ANDROID__
                if (parts.size() == 3) {
                        if (parts[2] == "true") {
                                lockSize(true,v2u32(800,600));
                        }
                }
-
+#endif
                data->explicit_size = true;
                return;
        }
@@ -437,11 +438,12 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element
                        fselected = true;
 
                std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
+               s32 spacing = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH) + 7;
 
                core::rect<s32> rect = core::rect<s32>(
-                               pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
-                               pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
-                               pos.Y + ((imgsize.Y/2) + m_btn_height));
+                               pos.X, pos.Y + ((imgsize.Y / 2) - m_btn_height),
+                               pos.X + m_font->getDimension(wlabel.c_str()).Width + spacing,
+                               pos.Y + ((imgsize.Y / 2) + m_btn_height));
 
                FieldSpec spec(
                                name,
@@ -2145,16 +2147,28 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
                        // the image size can't be less than 0.3 inch
                        // multiplied by gui_scaling, even if this means
                        // the form doesn't fit the screen.
-                       double prefer_imgsize = mydata.screensize.Y / 15 *
-                               gui_scaling;
+#ifdef __ANDROID__
+                       // For mobile devices these magic numbers are
+                       // different and forms should always use the
+                       // maximum screen space available.
+                       double prefer_imgsize = mydata.screensize.Y / 10 * gui_scaling;
                        double fitx_imgsize = mydata.screensize.X /
-                               ((5.0/4.0) * (0.5 + mydata.invsize.X));
+                               ((12.0 / 8.0) * (0.5 + mydata.invsize.X));
                        double fity_imgsize = mydata.screensize.Y /
-                               ((15.0/13.0) * (0.85 * mydata.invsize.Y));
+                               ((15.0 / 11.0) * (0.85 + mydata.invsize.Y));
+                       use_imgsize = MYMIN(prefer_imgsize,
+                                       MYMIN(fitx_imgsize, fity_imgsize));
+#else
+                       double prefer_imgsize = mydata.screensize.Y / 15 * gui_scaling;
+                       double fitx_imgsize = mydata.screensize.X /
+                               ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
+                       double fity_imgsize = mydata.screensize.Y /
+                               ((15.0 / 13.0) * (0.85 * mydata.invsize.Y));
                        double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
                        double min_imgsize = 0.3 * screen_dpi * gui_scaling;
                        use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
                                MYMIN(fitx_imgsize, fity_imgsize)));
+#endif
                }
 
                // Everything else is scaled in proportion to the
index 501b425f8b0f8d2c976840c89e610c4d635cc631..b1a027e9b76495513b737f6cb7e874e7b4b4885c 100644 (file)
@@ -78,10 +78,12 @@ GUITable::GUITable(gui::IGUIEnvironment *env,
        setTabStop(true);
        setTabOrder(-1);
        updateAbsolutePosition();
-
+       float density = RenderingEngine::getDisplayDensity();
+#ifdef __ANDROID__
+       density = 1; // dp scaling is applied by the skin
+#endif
        core::rect<s32> relative_rect = m_scrollbar->getRelativePosition();
-       s32 width = (relative_rect.getWidth()/(2.0/3.0)) *
-                       RenderingEngine::getDisplayDensity() *
+       s32 width = (relative_rect.getWidth() / (2.0 / 3.0)) * density *
                        g_settings->getFloat("gui_scaling");
        m_scrollbar->setRelativePosition(core::rect<s32>(
                        relative_rect.LowerRightCorner.X-width,relative_rect.UpperLeftCorner.Y,
diff --git a/textures/base/pack/checkbox_16.png b/textures/base/pack/checkbox_16.png
new file mode 100644 (file)
index 0000000..db6101f
Binary files /dev/null and b/textures/base/pack/checkbox_16.png differ
diff --git a/textures/base/pack/checkbox_32.png b/textures/base/pack/checkbox_32.png
new file mode 100644 (file)
index 0000000..f5ff59a
Binary files /dev/null and b/textures/base/pack/checkbox_32.png differ
diff --git a/textures/base/pack/checkbox_64.png b/textures/base/pack/checkbox_64.png
new file mode 100644 (file)
index 0000000..03eb90b
Binary files /dev/null and b/textures/base/pack/checkbox_64.png differ