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
This commit is contained in:
Loïc Blot 2017-08-29 19:25:16 +02:00 committed by GitHub
parent 72c09f524b
commit 1b3e4e1736
6 changed files with 83 additions and 19 deletions

@ -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)

@ -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)

@ -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");

@ -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;

@ -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<std::string> 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="<<type<<", data=\""<<description<<"\""
<<std::endl;
infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
<< std::endl;
}
void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
@ -1978,10 +1984,29 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
m_static_texts.clear();
m_dropdowns.clear();
// Set default values (fits old formspec values)
m_bgcolor = video::SColor(140,0,0,0);
m_bgfullscreen = false;
{
v3f formspec_bgcolor = g_settings->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<s32> allbg(0, 0, screenSize.X , screenSize.Y);
core::rect<s32> 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);

@ -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;
};