From 1b3e4e173624bb2523d4386aeef6987709d9b022 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Lo=C3=AFc=20Blot?= Date: Tue, 29 Aug 2017 19:25:16 +0200 Subject: [PATCH] Formspec: Add options to set background color and opacity (fullscreen mode + default mode) (#5493) * Formspec: Add options to set background color and opacity (fullscreen mode) * Enhance previous comment: Set formspec background when regenerate UI. * This permit to do the calcul only at regen and override it with bgcolor tag * Add a setting for default background color into formspec, separated from fullscreen * Add a little performance gain on formspecs using a const ref instead of copying formspec string --- builtin/settingtypes.txt | 12 +++++++++ minetest.conf.example | 16 ++++++++++++ src/defaultsettings.cpp | 4 +++ src/game.cpp | 8 +++--- src/guiFormSpecMenu.cpp | 54 +++++++++++++++++++++++++++++----------- src/guiFormSpecMenu.h | 8 ++++-- 6 files changed, 83 insertions(+), 19 deletions(-) diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index a3a4e961a..9dedd8b8d 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -606,6 +606,18 @@ console_color (Console color) string (0,0,0) # In-game chat console background alpha (opaqueness, between 0 and 255). console_alpha (Console alpha) int 200 0 255 +# Formspec full-screen background opacity (between 0 and 255). +formspec_fullscreen_bg_opacity (Formspec Full-Screen Background Opacity) int 140 0 255 + +# Formspec full-screen background color (R,G,B). +formspec_fullscreen_bg_color (Formspec Full-Screen Background Color) string (0,0,0) + +# Formspec default background opacity (between 0 and 255). +formspec_default_bg_opacity (Formspec Default Background Opacity) int 140 0 255 + +# Formspec default background color (R,G,B). +formspec_default_bg_color (Formspec Default Background Color) string (0,0,0) + # Selection box border color (R,G,B). selectionbox_color (Selection box color) string (0,0,0) diff --git a/minetest.conf.example b/minetest.conf.example index 6413d136a..9ce5bbd49 100644 --- a/minetest.conf.example +++ b/minetest.conf.example @@ -762,6 +762,22 @@ # type: int min: 0 max: 255 # console_alpha = 200 +# Formspec full-screen background opacity (between 0 and 255). +# type: int +# formspec_fullscreen_bg_opacity = 140 + +# Formspec full-screen background color (R,G,B). +# type: string +# formspec_fullscreen_bg_color = (0,0,0) + +# Formspec default background opacity (between 0 and 255). +# type: int +# formspec_default_bg_opacity = 140 + +# Formspec default background color (R,G,B). +# type: string +# formspec_default_bg_color = (0,0,0) + # Selection box border color (R,G,B). # type: string # selectionbox_color = (0,0,0) diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 502223fcd..2a3003f76 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -187,6 +187,10 @@ void set_default_settings(Settings *settings) settings->setDefault("console_height", "1.0"); settings->setDefault("console_color", "(0,0,0)"); settings->setDefault("console_alpha", "200"); + settings->setDefault("formspec_fullscreen_bg_color", "(0,0,0)"); + settings->setDefault("formspec_fullscreen_bg_opacity", "140"); + settings->setDefault("formspec_default_bg_color", "(0,0,0)"); + settings->setDefault("formspec_default_bg_opacity", "140"); settings->setDefault("selectionbox_color", "(0,0,0)"); settings->setDefault("selectionbox_width", "2"); settings->setDefault("node_highlighting", "box"); diff --git a/src/game.cpp b/src/game.cpp index 17ab68e7c..8a2ca00a2 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -185,6 +185,7 @@ struct LocalFormspecHandler : public TextDest /* Form update callback */ +static const std::string empty_string = ""; class NodeMetadataFormSource: public IFormSource { public: @@ -193,12 +194,12 @@ public: m_p(p) { } - std::string getForm() + const std::string &getForm() const { NodeMetadata *meta = m_map->getNodeMetadata(m_p); if (!meta) - return ""; + return empty_string; return meta->getString("formspec"); } @@ -224,7 +225,8 @@ public: m_client(client) { } - std::string getForm() + + const std::string &getForm() const { LocalPlayer *player = m_client->getEnv().getLocalPlayer(); return player->inventory_formspec; diff --git a/src/guiFormSpecMenu.cpp b/src/guiFormSpecMenu.cpp index 5ae652601..7b7bd7c29 100644 --- a/src/guiFormSpecMenu.cpp +++ b/src/guiFormSpecMenu.cpp @@ -79,6 +79,11 @@ static unsigned int font_line_height(gui::IGUIFont *font) return font->getDimension(L"Ay").Height + font->getKerningHeight(); } +inline u32 clamp_u8(s32 value) +{ + return (u32) MYMIN(MYMAX(value, 0), 255); +} + GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick, gui::IGUIElement *parent, s32 id, IMenuManager *menumgr, Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst, @@ -1567,17 +1572,19 @@ void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string & std::vector parts = split(element,';'); if (((parts.size() == 1) || (parts.size() == 2)) || - ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - parseColorString(parts[0],m_bgcolor,false); + ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION))) { + parseColorString(parts[0], m_bgcolor, false); if (parts.size() == 2) { std::string fullscreen = parts[1]; m_bgfullscreen = is_yes(fullscreen); } + return; } - errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl; + + errorstream << "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" + << std::endl; } void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element) @@ -1908,9 +1915,8 @@ void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element) } // Ignore others - infostream - << "Unknown DrawSpec: type="<getV3F("formspec_default_bg_color"); + m_bgcolor = video::SColor( + (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")), + clamp_u8(myround(formspec_bgcolor.X)), + clamp_u8(myround(formspec_bgcolor.Y)), + clamp_u8(myround(formspec_bgcolor.Z)) + ); + } + + { + v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color"); + m_fullscreen_bgcolor = video::SColor( + (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")), + clamp_u8(myround(formspec_bgcolor.X)), + clamp_u8(myround(formspec_bgcolor.Y)), + clamp_u8(myround(formspec_bgcolor.Z)) + ); + } + + m_slotbg_n = video::SColor(255,128,128,128); m_slotbg_h = video::SColor(255,192,192,192); @@ -2401,9 +2426,9 @@ void GUIFormSpecMenu::drawSelectedItem() void GUIFormSpecMenu::drawMenu() { - if(m_form_src){ - std::string newform = m_form_src->getForm(); - if(newform != m_formspec_string){ + if (m_form_src) { + const std::string &newform = m_form_src->getForm(); + if (newform != m_formspec_string) { m_formspec_string = newform; regenerateGui(m_screensize_old); } @@ -2419,9 +2444,10 @@ void GUIFormSpecMenu::drawMenu() video::IVideoDriver* driver = Environment->getVideoDriver(); v2u32 screenSize = driver->getScreenSize(); - core::rect allbg(0, 0, screenSize.X , screenSize.Y); + core::rect allbg(0, 0, screenSize.X, screenSize.Y); + if (m_bgfullscreen) - driver->draw2DRectangle(m_bgcolor, allbg, &allbg); + driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg); else driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect); diff --git a/src/guiFormSpecMenu.h b/src/guiFormSpecMenu.h index 02f367fbf..c86d596b7 100644 --- a/src/guiFormSpecMenu.h +++ b/src/guiFormSpecMenu.h @@ -66,7 +66,7 @@ class IFormSource { public: virtual ~IFormSource() = default; - virtual std::string getForm() = 0; + virtual const std::string &getForm() const = 0; // Fill in variables in field text virtual std::string resolveText(const std::string &str) { return str; } }; @@ -419,6 +419,7 @@ protected: bool m_bgfullscreen; bool m_slotborder; video::SColor m_bgcolor; + video::SColor m_fullscreen_bgcolor; video::SColor m_slotbg_n; video::SColor m_slotbg_h; video::SColor m_slotbordercolor; @@ -554,7 +555,10 @@ public: m_formspec = FORMSPEC_VERSION_STRING + formspec; } - std::string getForm() { return m_formspec; } + const std::string &getForm() const + { + return m_formspec; + } std::string m_formspec; }; -- 2.25.1