]> git.lizzy.rs Git - minetest.git/blobdiff - src/guiFormSpecMenu.cpp
Change lower limit of display_gamma to 1.0 (linear light)
[minetest.git] / src / guiFormSpecMenu.cpp
index 35a0380bad90dbff3a1adc06707f5b6d86c89fd8..5f67bb22afbd3b96f5325cc2522f39cfbab51b9e 100644 (file)
@@ -37,21 +37,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <IGUITabControl.h>
 #include <IGUIComboBox.h>
 #include "log.h"
-#include "tile.h" // ITextureSource
+#include "client/tile.h" // ITextureSource
 #include "hud.h" // drawItemStack
-#include "hex.h"
-#include "util/string.h"
-#include "util/numeric.h"
 #include "filesys.h"
 #include "gettime.h"
 #include "gettext.h"
 #include "scripting_game.h"
 #include "porting.h"
-#include "main.h"
 #include "settings.h"
 #include "client.h"
-#include "util/string.h" // for parseColorString()
 #include "fontengine.h"
+#include "util/hex.h"
+#include "util/numeric.h"
+#include "util/string.h" // for parseColorString()
+#include "guiscalingfilter.h"
 
 #define MY_CHECKPOS(a,b)                                                                                                       \
        if (v_pos.size() != 2) {                                                                                                \
@@ -74,60 +73,11 @@ static unsigned int font_line_height(gui::IGUIFont *font)
        return font->getDimension(L"Ay").Height + font->getKerningHeight();
 }
 
-static gui::IGUIFont *select_font_by_line_height(double target_line_height)
-{
-       return g_fontengine->getFont();
-
-/* I have no idea what this is trying to achieve, but scaling the font according
- * to the size of a formspec/dialog does not seem to be a standard (G)UI
- * design and AFAIK no existing nor proposed GUI does this. Besides that it:
- * a) breaks most (current) formspec layouts
- * b) font sizes change depending on the size of the formspec/dialog (see above)
- *    meaning that there is no UI consistency
- * c) the chosen fonts are, in general, probably too large
- *
- * Disabling for now.
- *
- * FIXME
- */
-#if 0
-       // We don't get to directly select a font according to its
-       // baseline-to-baseline height.  Rather, we select by em size.
-       // The ratio between these varies between fonts.  The font
-       // engine also takes its size parameter not specified in pixels,
-       // as we want, but scaled by display density and gui_scaling,
-       // so invert that scaling here.  Use a binary search among
-       // requested sizes to find the right font.  Our starting bounds
-       // are an em height of 1 (being careful not to request size 0,
-       // which crashes the freetype system) and an em height of the
-       // target baseline-to-baseline height.
-       unsigned int loreq = ceil(1 / porting::getDisplayDensity()
-                               / g_settings->getFloat("gui_scaling"));
-       unsigned int hireq = ceil(target_line_height
-                               / porting::getDisplayDensity()
-                               / g_settings->getFloat("gui_scaling"));
-       unsigned int lohgt = font_line_height(g_fontengine->getFont(loreq));
-       unsigned int hihgt = font_line_height(g_fontengine->getFont(hireq));
-       while(hireq - loreq > 1 && lohgt != hihgt) {
-               unsigned int nureq = (loreq + hireq) >> 1;
-               unsigned int nuhgt = font_line_height(g_fontengine->getFont(nureq));
-               if(nuhgt < target_line_height) {
-                       loreq = nureq;
-                       lohgt = nuhgt;
-               } else {
-                       hireq = nureq;
-                       hihgt = nuhgt;
-               }
-       }
-       return g_fontengine->getFont(target_line_height - lohgt < hihgt - target_line_height ? loreq : hireq);
-#endif
-}
-
 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
                gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
                InventoryManager *invmgr, IGameDef *gamedef,
                ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
-               Client* client) :
+               Client* client, bool remap_dbl_click) :
        GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
        m_device(dev),
        m_invmgr(invmgr),
@@ -146,7 +96,9 @@ GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
        m_form_src(fsrc),
        m_text_dst(tdst),
        m_formspec_version(0),
-       m_font(NULL)
+       m_focused_element(L""),
+       m_font(NULL),
+       m_remap_dbl_click(remap_dbl_click)
 #ifdef __ANDROID__
        ,m_JavaDialogFieldName(L"")
 #endif
@@ -401,7 +353,7 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
 {
        std::vector<std::string> parts = split(element,';');
 
-       if (((parts.size() >= 3) || (parts.size() <= 4)) ||
+       if (((parts.size() >= 3) && (parts.size() <= 4)) ||
                ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
        {
                std::vector<std::string> v_pos = split(parts[0],',');
@@ -423,7 +375,7 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
                if (selected == "true")
                        fselected = true;
 
-               std::wstring wlabel = narrow_to_wide(label.c_str());
+               std::wstring wlabel = narrow_to_wide(label);
 
                core::rect<s32> rect = core::rect<s32>(
                                pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
@@ -431,7 +383,7 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
                                pos.Y + ((imgsize.Y/2) + m_btn_height));
 
                FieldSpec spec(
-                               narrow_to_wide(name.c_str()),
+                               narrow_to_wide(name),
                                wlabel, //Needed for displaying text on MSVC
                                wlabel,
                                258+m_fields.size()
@@ -483,7 +435,7 @@ void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
                                core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
 
                FieldSpec spec(
-                               narrow_to_wide(name.c_str()),
+                               narrow_to_wide(name),
                                L"",
                                L"",
                                258+m_fields.size()
@@ -621,10 +573,10 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
 
                label = unescape_string(label);
 
-               std::wstring wlabel = narrow_to_wide(label.c_str());
+               std::wstring wlabel = narrow_to_wide(label);
 
                FieldSpec spec(
-                       narrow_to_wide(name.c_str()),
+                       narrow_to_wide(name),
                        wlabel,
                        L"",
                        258+m_fields.size()
@@ -746,7 +698,7 @@ void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
 
                core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
 
-               std::wstring fname_w = narrow_to_wide(name.c_str());
+               std::wstring fname_w = narrow_to_wide(name);
 
                FieldSpec spec(
                        fname_w,
@@ -820,7 +772,7 @@ void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
 
                core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
 
-               std::wstring fname_w = narrow_to_wide(name.c_str());
+               std::wstring fname_w = narrow_to_wide(name);
 
                FieldSpec spec(
                        fname_w,
@@ -885,7 +837,7 @@ void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
                core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
                                pos.X + width, pos.Y + (m_btn_height * 2));
 
-               std::wstring fname_w = narrow_to_wide(name.c_str());
+               std::wstring fname_w = narrow_to_wide(name);
 
                FieldSpec spec(
                        fname_w,
@@ -948,10 +900,10 @@ void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
 
                label = unescape_string(label);
 
-               std::wstring wlabel = narrow_to_wide(label.c_str());
+               std::wstring wlabel = narrow_to_wide(label);
 
                FieldSpec spec(
-                       narrow_to_wide(name.c_str()),
+                       narrow_to_wide(name),
                        wlabel,
                        L"",
                        258+m_fields.size()
@@ -966,7 +918,7 @@ void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
 
                if (label.length() >= 1)
                {
-                       int font_height = font_line_height(m_font);
+                       int font_height = g_fontengine->getTextHeight();
                        rect.UpperLeftCorner.Y -= font_height;
                        rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
                        Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
@@ -1014,12 +966,12 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,
        default_val = unescape_string(default_val);
        label = unescape_string(label);
 
-       std::wstring wlabel = narrow_to_wide(label.c_str());
+       std::wstring wlabel = narrow_to_wide(label);
 
        FieldSpec spec(
-               narrow_to_wide(name.c_str()),
+               narrow_to_wide(name),
                wlabel,
-               narrow_to_wide(default_val.c_str()),
+               narrow_to_wide(default_val),
                258+m_fields.size()
        );
 
@@ -1049,7 +1001,7 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,
 
                if (label.length() >= 1)
                {
-                       int font_height = font_line_height(m_font);
+                       int font_height = g_fontengine->getTextHeight();
                        rect.UpperLeftCorner.Y -= font_height;
                        rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
                        Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
@@ -1104,12 +1056,12 @@ void GUIFormSpecMenu::parseTextArea(parserData* data,
        default_val = unescape_string(default_val);
        label = unescape_string(label);
 
-       std::wstring wlabel = narrow_to_wide(label.c_str());
+       std::wstring wlabel = narrow_to_wide(label);
 
        FieldSpec spec(
-               narrow_to_wide(name.c_str()),
+               narrow_to_wide(name),
                wlabel,
-               narrow_to_wide(default_val.c_str()),
+               narrow_to_wide(default_val),
                258+m_fields.size()
        );
 
@@ -1146,7 +1098,7 @@ void GUIFormSpecMenu::parseTextArea(parserData* data,
 
                if (label.length() >= 1)
                {
-                       int font_height = font_line_height(m_font);
+                       int font_height = g_fontengine->getTextHeight();
                        rect.UpperLeftCorner.Y -= font_height;
                        rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
                        Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
@@ -1193,8 +1145,6 @@ void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
                if(!data->explicit_size)
                        errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
 
-               int font_height = font_line_height(m_font);
-
                text = unescape_string(text);
                std::vector<std::string> lines = split(text, '\n');
 
@@ -1209,11 +1159,11 @@ void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
                        // in the integer cases: 0.4 is not exactly
                        // representable in binary floating point.
                        s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
-                       std::wstring wlabel = narrow_to_wide(lines[i].c_str());
+                       std::wstring wlabel = narrow_to_wide(lines[i]);
                        core::rect<s32> rect = core::rect<s32>(
-                               pos.X, posy - font_height,
+                               pos.X, posy - m_btn_height,
                                pos.X + m_font->getDimension(wlabel.c_str()).Width,
-                               posy + font_height);
+                               posy + m_btn_height);
                        FieldSpec spec(
                                L"",
                                wlabel,
@@ -1330,12 +1280,12 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
                pressed_image_name = unescape_string(pressed_image_name);
                label = unescape_string(label);
 
-               std::wstring wlabel = narrow_to_wide(label.c_str());
+               std::wstring wlabel = narrow_to_wide(label);
 
                FieldSpec spec(
-                       narrow_to_wide(name.c_str()),
+                       narrow_to_wide(name),
                        wlabel,
-                       narrow_to_wide(image_name.c_str()),
+                       narrow_to_wide(image_name),
                        258+m_fields.size()
                );
                spec.ftype = f_Button;
@@ -1357,8 +1307,10 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
                }
 
                e->setUseAlphaChannel(true);
-               e->setImage(texture);
-               e->setPressedImage(pressed_texture);
+               e->setImage(guiScalingImageButton(
+                       Environment->getVideoDriver(), texture, geom.X, geom.Y));
+               e->setPressedImage(guiScalingImageButton(
+                       Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
                e->setScaleImage(true);
                e->setNotClipped(noclip);
                e->setDrawBorder(drawborder);
@@ -1395,7 +1347,7 @@ void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
                }
 
                FieldSpec spec(
-                       narrow_to_wide(name.c_str()),
+                       narrow_to_wide(name),
                        L"",
                        L"",
                        258+m_fields.size()
@@ -1482,16 +1434,16 @@ void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
                item.deSerialize(item_name, idef);
                video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
 
-               m_tooltips[narrow_to_wide(name.c_str())] =
-                       TooltipSpec (item.getDefinition(idef).description,
+               m_tooltips[narrow_to_wide(name)] =
+                       TooltipSpec(item.getDefinition(idef).description,
                                                m_default_tooltip_bgcolor,
                                                m_default_tooltip_color);
 
                label = unescape_string(label);
                FieldSpec spec(
-                       narrow_to_wide(name.c_str()),
-                       narrow_to_wide(label.c_str()),
-                       narrow_to_wide(item_name.c_str()),
+                       narrow_to_wide(name),
+                       narrow_to_wide(label),
+                       narrow_to_wide(item_name),
                        258+m_fields.size()
                );
 
@@ -1502,8 +1454,8 @@ void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
                }
 
                e->setUseAlphaChannel(true);
-               e->setImage(texture);
-               e->setPressedImage(texture);
+               e->setImage(guiScalingImageButton(Environment->getVideoDriver(), texture, geom.X, geom.Y));
+               e->setPressedImage(guiScalingImageButton(Environment->getVideoDriver(), texture, geom.X, geom.Y));
                e->setScaleImage(true);
                spec.ftype = f_Button;
                rect+=data->basepos-padding;
@@ -1601,13 +1553,15 @@ void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
        std::vector<std::string> parts = split(element,';');
        if (parts.size() == 2) {
                std::string name = parts[0];
-               m_tooltips[narrow_to_wide(name.c_str())] = TooltipSpec (parts[1], m_default_tooltip_bgcolor, m_default_tooltip_color);
+               m_tooltips[narrow_to_wide(name)] = TooltipSpec(unescape_string(parts[1]),
+                       m_default_tooltip_bgcolor, m_default_tooltip_color);
                return;
        } else if (parts.size() == 4) {
                std::string name = parts[0];
                video::SColor tmp_color1, tmp_color2;
                if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
-                       m_tooltips[narrow_to_wide(name.c_str())] = TooltipSpec (parts[1], tmp_color1, tmp_color2);
+                       m_tooltips[narrow_to_wide(name)] = TooltipSpec(unescape_string(parts[1]),
+                               tmp_color1, tmp_color2);
                        return;
                }
        }
@@ -1808,8 +1762,6 @@ void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
                <<std::endl;
 }
 
-
-
 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
 {
        /* useless to regenerate without a screensize */
@@ -1826,6 +1778,10 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
                mydata.table_dyndata[tablename] = table->getDynamicData();
        }
 
+       //set focus
+       if (!m_focused_element.empty())
+               mydata.focused_fieldname = m_focused_element;
+
        //preserve focus
        gui::IGUIElement *focused_element = Environment->getFocus();
        if (focused_element && focused_element->getParent() == this) {
@@ -1990,11 +1946,9 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
                imgsize = v2s32(use_imgsize, use_imgsize);
                spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
                padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
-               double target_font_height = use_imgsize*15.0/13 * 0.4;
-               target_font_height *= g_settings->getFloat("font_size")/TTF_DEFAULT_FONT_SIZE;
                m_btn_height = use_imgsize*15.0/13 * 0.35;
 
-               m_font = select_font_by_line_height(target_font_height);
+               m_font = g_fontengine->getFont();
 
                mydata.size = v2s32(
                        padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
@@ -2024,7 +1978,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
        m_tooltip_element->setOverrideFont(m_font);
 
        gui::IGUISkin* skin = Environment->getSkin();
-       assert(skin != NULL);
+       sanity_check(skin != NULL);
        gui::IGUIFont *old_font = skin->getFont();
        skin->setFont(m_font);
 
@@ -2053,7 +2007,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
                        mydata.rect =
                                        core::rect<s32>(size.X/2-70, pos.Y,
                                                        (size.X/2-70)+140, pos.Y + (m_btn_height*2));
-                       wchar_t* text = wgettext("Proceed");
+                       const wchar_t *text = wgettext("Proceed");
                        Environment->addButton(mydata.rect, this, 257, text);
                        delete[] text;
                }
@@ -2268,9 +2222,9 @@ void GUIFormSpecMenu::drawSelectedItem()
        video::IVideoDriver* driver = Environment->getVideoDriver();
 
        Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
-       assert(inv);
+       sanity_check(inv);
        InventoryList *list = inv->getList(m_selected_item->listname);
-       assert(list);
+       sanity_check(list);
        ItemStack stack = list->getItem(m_selected_item->i);
        stack.count = m_selected_amount;
 
@@ -2290,7 +2244,7 @@ void GUIFormSpecMenu::drawMenu()
        }
 
        gui::IGUISkin* skin = Environment->getSkin();
-       assert(skin != NULL);
+       sanity_check(skin != NULL);
        gui::IGUIFont *old_font = skin->getFont();
        skin->setFont(m_font);
 
@@ -2331,7 +2285,7 @@ void GUIFormSpecMenu::drawMenu()
 
                        const video::SColor color(255,255,255,255);
                        const video::SColor colors[] = {color,color,color,color};
-                       driver->draw2DImage(texture, rect,
+                       draw2DImageFilterScaled(driver, texture, rect,
                                core::rect<s32>(core::position2d<s32>(0,0),
                                                core::dimension2di(texture->getOriginalSize())),
                                NULL/*&AbsoluteClippingRect*/, colors, true);
@@ -2381,7 +2335,7 @@ void GUIFormSpecMenu::drawMenu()
                        core::rect<s32> rect = imgrect + spec.pos;
                        const video::SColor color(255,255,255,255);
                        const video::SColor colors[] = {color,color,color,color};
-                       driver->draw2DImage(texture, rect,
+                       draw2DImageFilterScaled(driver, texture, rect,
                                core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
                                NULL/*&AbsoluteClippingRect*/, colors, true);
                }
@@ -2410,7 +2364,7 @@ void GUIFormSpecMenu::drawMenu()
                core::rect<s32> rect = imgrect + spec.pos;
                const video::SColor color(255,255,255,255);
                const video::SColor colors[] = {color,color,color,color};
-               driver->draw2DImage(texture, rect,
+               draw2DImageFilterScaled(driver, texture, rect,
                        core::rect<s32>(core::position2d<s32>(0,0),
                                        core::dimension2di(texture->getOriginalSize())),
                        NULL/*&AbsoluteClippingRect*/, colors, true);
@@ -2776,7 +2730,7 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
                if (hovered && isMyChild(hovered) &&
                                hovered->getType() == gui::EGUIET_TAB_CONTROL) {
                        gui::IGUISkin* skin = Environment->getSkin();
-                       assert(skin != NULL);
+                       sanity_check(skin != NULL);
                        gui::IGUIFont *old_font = skin->getFont();
                        skin->setFont(m_font);
                        bool retval = hovered->OnEvent(event);
@@ -2987,6 +2941,19 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
 /******************************************************************************/
 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
 {
+       /* The following code is for capturing double-clicks of the mouse button
+        * and translating the double-click into an EET_KEY_INPUT_EVENT event
+        * -- which closes the form -- under some circumstances.
+        *
+        * There have been many github issues reporting this as a bug even though it
+        * was an intended feature.  For this reason, remapping the double-click as
+        * an ESC must be explicitly set when creating this class via the
+        * /p remap_dbl_click parameter of the constructor.
+        */
+
+       if (!m_remap_dbl_click)
+               return false;
+
        if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
                m_doubleclickdetect[0].pos  = m_doubleclickdetect[1].pos;
                m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
@@ -3025,6 +2992,7 @@ bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
                delete translated;
                return true;
        }
+
        return false;
 }
 
@@ -3064,7 +3032,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                                break;
                                default:
                                        //can't happen at all!
-                                       assert("reached a source line that can't ever been reached" == 0);
+                                       FATAL_ERROR("Reached a source line that can't ever been reached");
                                        break;
                        }
                        if (current_keys_pending.key_enter && m_allowclose) {
@@ -3098,8 +3066,8 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
 
                if(m_selected_item) {
                        inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
-                       assert(inv_selected);
-                       assert(inv_selected->getList(m_selected_item->listname) != NULL);
+                       sanity_check(inv_selected);
+                       sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
                }
 
                u32 s_count = 0;