Skip to content

Commit 1b3e4e1

Browse files
authoredAug 29, 2017
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
1 parent 72c09f5 commit 1b3e4e1

File tree

6 files changed

+83
-19
lines changed

6 files changed

+83
-19
lines changed
 

Diff for: ‎builtin/settingtypes.txt

+12
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,18 @@ console_color (Console color) string (0,0,0)
606606
# In-game chat console background alpha (opaqueness, between 0 and 255).
607607
console_alpha (Console alpha) int 200 0 255
608608

609+
# Formspec full-screen background opacity (between 0 and 255).
610+
formspec_fullscreen_bg_opacity (Formspec Full-Screen Background Opacity) int 140 0 255
611+
612+
# Formspec full-screen background color (R,G,B).
613+
formspec_fullscreen_bg_color (Formspec Full-Screen Background Color) string (0,0,0)
614+
615+
# Formspec default background opacity (between 0 and 255).
616+
formspec_default_bg_opacity (Formspec Default Background Opacity) int 140 0 255
617+
618+
# Formspec default background color (R,G,B).
619+
formspec_default_bg_color (Formspec Default Background Color) string (0,0,0)
620+
609621
# Selection box border color (R,G,B).
610622
selectionbox_color (Selection box color) string (0,0,0)
611623

Diff for: ‎minetest.conf.example

+16
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,22 @@
762762
# type: int min: 0 max: 255
763763
# console_alpha = 200
764764

765+
# Formspec full-screen background opacity (between 0 and 255).
766+
# type: int
767+
# formspec_fullscreen_bg_opacity = 140
768+
769+
# Formspec full-screen background color (R,G,B).
770+
# type: string
771+
# formspec_fullscreen_bg_color = (0,0,0)
772+
773+
# Formspec default background opacity (between 0 and 255).
774+
# type: int
775+
# formspec_default_bg_opacity = 140
776+
777+
# Formspec default background color (R,G,B).
778+
# type: string
779+
# formspec_default_bg_color = (0,0,0)
780+
765781
# Selection box border color (R,G,B).
766782
# type: string
767783
# selectionbox_color = (0,0,0)

Diff for: ‎src/defaultsettings.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ void set_default_settings(Settings *settings)
187187
settings->setDefault("console_height", "1.0");
188188
settings->setDefault("console_color", "(0,0,0)");
189189
settings->setDefault("console_alpha", "200");
190+
settings->setDefault("formspec_fullscreen_bg_color", "(0,0,0)");
191+
settings->setDefault("formspec_fullscreen_bg_opacity", "140");
192+
settings->setDefault("formspec_default_bg_color", "(0,0,0)");
193+
settings->setDefault("formspec_default_bg_opacity", "140");
190194
settings->setDefault("selectionbox_color", "(0,0,0)");
191195
settings->setDefault("selectionbox_width", "2");
192196
settings->setDefault("node_highlighting", "box");

Diff for: ‎src/game.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ struct LocalFormspecHandler : public TextDest
185185

186186
/* Form update callback */
187187

188+
static const std::string empty_string = "";
188189
class NodeMetadataFormSource: public IFormSource
189190
{
190191
public:
@@ -193,12 +194,12 @@ class NodeMetadataFormSource: public IFormSource
193194
m_p(p)
194195
{
195196
}
196-
std::string getForm()
197+
const std::string &getForm() const
197198
{
198199
NodeMetadata *meta = m_map->getNodeMetadata(m_p);
199200

200201
if (!meta)
201-
return "";
202+
return empty_string;
202203

203204
return meta->getString("formspec");
204205
}
@@ -224,7 +225,8 @@ class PlayerInventoryFormSource: public IFormSource
224225
m_client(client)
225226
{
226227
}
227-
std::string getForm()
228+
229+
const std::string &getForm() const
228230
{
229231
LocalPlayer *player = m_client->getEnv().getLocalPlayer();
230232
return player->inventory_formspec;

Diff for: ‎src/guiFormSpecMenu.cpp

+40-14
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ static unsigned int font_line_height(gui::IGUIFont *font)
7979
return font->getDimension(L"Ay").Height + font->getKerningHeight();
8080
}
8181

82+
inline u32 clamp_u8(s32 value)
83+
{
84+
return (u32) MYMIN(MYMAX(value, 0), 255);
85+
}
86+
8287
GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
8388
gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
8489
Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst,
@@ -1567,17 +1572,19 @@ void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &
15671572
std::vector<std::string> parts = split(element,';');
15681573

15691574
if (((parts.size() == 1) || (parts.size() == 2)) ||
1570-
((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1571-
{
1572-
parseColorString(parts[0],m_bgcolor,false);
1575+
((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION))) {
1576+
parseColorString(parts[0], m_bgcolor, false);
15731577

15741578
if (parts.size() == 2) {
15751579
std::string fullscreen = parts[1];
15761580
m_bgfullscreen = is_yes(fullscreen);
15771581
}
1582+
15781583
return;
15791584
}
1580-
errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1585+
1586+
errorstream << "Invalid bgcolor element(" << parts.size() << "): '" << element << "'"
1587+
<< std::endl;
15811588
}
15821589

15831590
void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
@@ -1908,9 +1915,8 @@ void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
19081915
}
19091916

19101917
// Ignore others
1911-
infostream
1912-
<< "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1913-
<<std::endl;
1918+
infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
1919+
<< std::endl;
19141920
}
19151921

19161922
void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
@@ -1978,10 +1984,29 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
19781984
m_static_texts.clear();
19791985
m_dropdowns.clear();
19801986

1981-
// Set default values (fits old formspec values)
1982-
m_bgcolor = video::SColor(140,0,0,0);
19831987
m_bgfullscreen = false;
19841988

1989+
{
1990+
v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
1991+
m_bgcolor = video::SColor(
1992+
(u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
1993+
clamp_u8(myround(formspec_bgcolor.X)),
1994+
clamp_u8(myround(formspec_bgcolor.Y)),
1995+
clamp_u8(myround(formspec_bgcolor.Z))
1996+
);
1997+
}
1998+
1999+
{
2000+
v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
2001+
m_fullscreen_bgcolor = video::SColor(
2002+
(u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
2003+
clamp_u8(myround(formspec_bgcolor.X)),
2004+
clamp_u8(myround(formspec_bgcolor.Y)),
2005+
clamp_u8(myround(formspec_bgcolor.Z))
2006+
);
2007+
}
2008+
2009+
19852010
m_slotbg_n = video::SColor(255,128,128,128);
19862011
m_slotbg_h = video::SColor(255,192,192,192);
19872012

@@ -2401,9 +2426,9 @@ void GUIFormSpecMenu::drawSelectedItem()
24012426

24022427
void GUIFormSpecMenu::drawMenu()
24032428
{
2404-
if(m_form_src){
2405-
std::string newform = m_form_src->getForm();
2406-
if(newform != m_formspec_string){
2429+
if (m_form_src) {
2430+
const std::string &newform = m_form_src->getForm();
2431+
if (newform != m_formspec_string) {
24072432
m_formspec_string = newform;
24082433
regenerateGui(m_screensize_old);
24092434
}
@@ -2419,9 +2444,10 @@ void GUIFormSpecMenu::drawMenu()
24192444
video::IVideoDriver* driver = Environment->getVideoDriver();
24202445

24212446
v2u32 screenSize = driver->getScreenSize();
2422-
core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2447+
core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
2448+
24232449
if (m_bgfullscreen)
2424-
driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2450+
driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
24252451
else
24262452
driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
24272453

Diff for: ‎src/guiFormSpecMenu.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class IFormSource
6666
{
6767
public:
6868
virtual ~IFormSource() = default;
69-
virtual std::string getForm() = 0;
69+
virtual const std::string &getForm() const = 0;
7070
// Fill in variables in field text
7171
virtual std::string resolveText(const std::string &str) { return str; }
7272
};
@@ -419,6 +419,7 @@ class GUIFormSpecMenu : public GUIModalMenu
419419
bool m_bgfullscreen;
420420
bool m_slotborder;
421421
video::SColor m_bgcolor;
422+
video::SColor m_fullscreen_bgcolor;
422423
video::SColor m_slotbg_n;
423424
video::SColor m_slotbg_h;
424425
video::SColor m_slotbordercolor;
@@ -554,7 +555,10 @@ class FormspecFormSource: public IFormSource
554555
m_formspec = FORMSPEC_VERSION_STRING + formspec;
555556
}
556557

557-
std::string getForm() { return m_formspec; }
558+
const std::string &getForm() const
559+
{
560+
return m_formspec;
561+
}
558562

559563
std::string m_formspec;
560564
};

0 commit comments

Comments
 (0)
Please sign in to comment.