diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2022-05-17 22:12:00 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2022-05-17 22:12:00 +0200 |
commit | 21df26984da91143c15587f5a03c98d68c3adc4e (patch) | |
tree | aaa707a628ad331f67890023dffe1b4f60dd01d3 /src/gui | |
parent | b09fc5de5cdb021f43ad32b7e3f50dc75c0bc622 (diff) | |
parent | eabf05758e3ba5f6f4bb1b8d1d1f02179b84e410 (diff) | |
download | dragonfireclient-21df26984da91143c15587f5a03c98d68c3adc4e.tar.xz |
Merge branch 'master' of https://github.com/minetest/minetest
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/gui/cheatMenu.cpp | 4 | ||||
-rw-r--r-- | src/gui/guiBackgroundImage.cpp | 2 | ||||
-rw-r--r-- | src/gui/guiButton.cpp | 79 | ||||
-rw-r--r-- | src/gui/guiButton.h | 8 | ||||
-rw-r--r-- | src/gui/guiChatConsole.cpp | 14 | ||||
-rw-r--r-- | src/gui/guiConfirmRegistration.cpp | 10 | ||||
-rw-r--r-- | src/gui/guiEditBox.cpp | 51 | ||||
-rw-r--r-- | src/gui/guiEditBox.h | 8 | ||||
-rw-r--r-- | src/gui/guiEditBoxWithScrollbar.cpp | 20 | ||||
-rw-r--r-- | src/gui/guiEditBoxWithScrollbar.h | 6 | ||||
-rw-r--r-- | src/gui/guiEngine.cpp | 26 | ||||
-rw-r--r-- | src/gui/guiFormSpecMenu.cpp | 2268 | ||||
-rw-r--r-- | src/gui/guiFormSpecMenu.h | 10 | ||||
-rw-r--r-- | src/gui/guiHyperText.cpp | 23 | ||||
-rw-r--r-- | src/gui/guiHyperText.h | 11 | ||||
-rw-r--r-- | src/gui/guiPasswordChange.cpp | 8 | ||||
-rw-r--r-- | src/gui/guiSkin.cpp | 42 | ||||
-rw-r--r-- | src/gui/guiSkin.h | 10 | ||||
-rw-r--r-- | src/gui/guiTable.cpp | 5 | ||||
-rw-r--r-- | src/gui/modalMenu.cpp | 11 | ||||
-rw-r--r-- | src/gui/modalMenu.h | 4 | ||||
-rw-r--r-- | src/gui/profilergraph.cpp | 15 | ||||
-rw-r--r-- | src/gui/touchscreengui.cpp | 27 | ||||
-rw-r--r-- | src/gui/touchscreengui.h | 6 |
25 files changed, 1231 insertions, 1443 deletions
diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index ea6e44ab7..a5f25c0f3 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -1,3 +1,8 @@ +set(extra_gui_SRCS "") +if(ENABLE_TOUCH) + set(extra_gui_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/touchscreengui.cpp) +endif() + set(gui_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/cheatMenu.cpp ${CMAKE_CURRENT_SOURCE_DIR}/guiAnimatedImage.cpp @@ -26,5 +31,6 @@ set(gui_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/guiVolumeChange.cpp ${CMAKE_CURRENT_SOURCE_DIR}/modalMenu.cpp ${CMAKE_CURRENT_SOURCE_DIR}/profilergraph.cpp + ${extra_gui_SRCS} PARENT_SCOPE ) diff --git a/src/gui/cheatMenu.cpp b/src/gui/cheatMenu.cpp index 31acfb780..2be82f148 100644 --- a/src/gui/cheatMenu.cpp +++ b/src/gui/cheatMenu.cpp @@ -31,10 +31,6 @@ FontMode CheatMenu::fontStringToEnum(std::string str) return FM_Mono; else if (str == "FM_Fallback") return _FM_Fallback; - else if (str == "FM_Simple") - return FM_Simple; - else if (str == "FM_SimpleMono") - return FM_SimpleMono; else if (str == "FM_MaxMode") return FM_MaxMode; else if (str == "FM_Unspecified") diff --git a/src/gui/guiBackgroundImage.cpp b/src/gui/guiBackgroundImage.cpp index 21c1e88cf..85e870771 100644 --- a/src/gui/guiBackgroundImage.cpp +++ b/src/gui/guiBackgroundImage.cpp @@ -44,7 +44,7 @@ void GUIBackgroundImage::draw() core::rect<s32> rect = AbsoluteRect; if (m_autoclip) - rect.LowerRightCorner += Parent->getAbsolutePosition().getSize(); + rect.LowerRightCorner += Parent->getAbsoluteClippingRect().getSize(); video::IVideoDriver *driver = Environment->getVideoDriver(); diff --git a/src/gui/guiButton.cpp b/src/gui/guiButton.cpp index d6dbddf54..ba95b81c3 100644 --- a/src/gui/guiButton.cpp +++ b/src/gui/guiButton.cpp @@ -632,85 +632,6 @@ bool GUIButton::isDrawingBorder() const }
-//! Writes attributes of the element.
-void GUIButton::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
-{
- IGUIButton::serializeAttributes(out,options);
-
- out->addBool ("PushButton", IsPushButton );
- if (IsPushButton)
- out->addBool("Pressed", Pressed);
-
- for ( u32 i=0; i<(u32)EGBIS_COUNT; ++i )
- {
- if ( ButtonImages[i].Texture )
- {
- core::stringc name( GUIButtonImageStateNames[i] );
- out->addTexture(name.c_str(), ButtonImages[i].Texture);
- name += "Rect";
- out->addRect(name.c_str(), ButtonImages[i].SourceRect);
- }
- }
-
- out->addBool ("UseAlphaChannel", UseAlphaChannel);
- out->addBool ("Border", DrawBorder);
- out->addBool ("ScaleImage", ScaleImage);
-
- for ( u32 i=0; i<(u32)EGBS_COUNT; ++i )
- {
- if ( ButtonSprites[i].Index >= 0 )
- {
- core::stringc nameIndex( GUIButtonStateNames[i] );
- nameIndex += "Index";
- out->addInt(nameIndex.c_str(), ButtonSprites[i].Index );
-
- core::stringc nameColor( GUIButtonStateNames[i] );
- nameColor += "Color";
- out->addColor(nameColor.c_str(), ButtonSprites[i].Color );
-
- core::stringc nameLoop( GUIButtonStateNames[i] );
- nameLoop += "Loop";
- out->addBool(nameLoop.c_str(), ButtonSprites[i].Loop );
-
- core::stringc nameScale( GUIButtonStateNames[i] );
- nameScale += "Scale";
- out->addBool(nameScale.c_str(), ButtonSprites[i].Scale );
- }
- }
-
- // out->addString ("OverrideFont", OverrideFont);
-}
-
-
-//! Reads attributes of the element
-void GUIButton::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
-{
- IGUIButton::deserializeAttributes(in,options);
-
- IsPushButton = in->getAttributeAsBool("PushButton");
- Pressed = IsPushButton ? in->getAttributeAsBool("Pressed") : false;
-
- core::rect<s32> rec = in->getAttributeAsRect("ImageRect");
- if (rec.isValid())
- setImage( in->getAttributeAsTexture("Image"), rec);
- else
- setImage( in->getAttributeAsTexture("Image") );
-
- rec = in->getAttributeAsRect("PressedImageRect");
- if (rec.isValid())
- setPressedImage( in->getAttributeAsTexture("PressedImage"), rec);
- else
- setPressedImage( in->getAttributeAsTexture("PressedImage") );
-
- setDrawBorder(in->getAttributeAsBool("Border"));
- setUseAlphaChannel(in->getAttributeAsBool("UseAlphaChannel"));
- setScaleImage(in->getAttributeAsBool("ScaleImage"));
-
- // setOverrideFont(in->getAttributeAsString("OverrideFont"));
-
- updateAbsolutePosition();
-}
-
// PATCH
GUIButton* GUIButton::addButton(IGUIEnvironment *environment,
const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,
diff --git a/src/gui/guiButton.h b/src/gui/guiButton.h index 834405f51..ee9bb6f21 100644 --- a/src/gui/guiButton.h +++ b/src/gui/guiButton.h @@ -221,14 +221,6 @@ public: return ClickControlState;
}
- //! Writes attributes of the element.
- virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const override;
-
- //! Reads attributes of the element
- virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options) override;
-
-
-
void setColor(video::SColor color);
// PATCH
//! Set element properties from a StyleSpec corresponding to the button state
diff --git a/src/gui/guiChatConsole.cpp b/src/gui/guiChatConsole.cpp index 0610c85cc..01e10ea2e 100644 --- a/src/gui/guiChatConsole.cpp +++ b/src/gui/guiChatConsole.cpp @@ -30,12 +30,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "client/fontengine.h" #include "log.h" #include "gettext.h" +#include "irrlicht_changes/CGUITTFont.h" #include <string> -#if USE_FREETYPE - #include "irrlicht_changes/CGUITTFont.h" -#endif - inline u32 clamp_u8(s32 value) { return (u32) MYMIN(MYMAX(value, 0), 255); @@ -328,19 +325,16 @@ void GUIChatConsole::drawText() core::rect<s32> destrect( x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y); -#if USE_FREETYPE if (m_font->getType() == irr::gui::EGFT_CUSTOM) { - // Draw colored text if FreeType is enabled - irr::gui::CGUITTFont *tmp = dynamic_cast<irr::gui::CGUITTFont *>(m_font); + // Draw colored text if possible + gui::CGUITTFont *tmp = static_cast<gui::CGUITTFont*>(m_font); tmp->draw( fragment.text, destrect, false, false, &AbsoluteClippingRect); - } else -#endif - { + } else { // Otherwise use standard text m_font->draw( fragment.text.c_str(), diff --git a/src/gui/guiConfirmRegistration.cpp b/src/gui/guiConfirmRegistration.cpp index 4ca9a64ed..b8887a4af 100644 --- a/src/gui/guiConfirmRegistration.cpp +++ b/src/gui/guiConfirmRegistration.cpp @@ -28,6 +28,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "guiEditBoxWithScrollbar.h" #include "porting.h" +#ifdef HAVE_TOUCHSCREENGUI + #include "client/renderingengine.h" +#endif + #include "gettext.h" // Continuing from guiPasswordChange.cpp @@ -45,7 +49,7 @@ GUIConfirmRegistration::GUIConfirmRegistration(gui::IGUIEnvironment *env, m_client(client), m_playername(playername), m_password(password), m_aborted(aborted), m_tsrc(tsrc) { -#ifdef __ANDROID__ +#ifdef HAVE_TOUCHSCREENGUI m_touchscreen_visible = false; #endif } @@ -73,8 +77,8 @@ void GUIConfirmRegistration::regenerateGui(v2u32 screensize) /* Calculate new sizes and positions */ -#ifdef __ANDROID__ - const float s = m_gui_scale * porting::getDisplayDensity() / 2; +#ifdef HAVE_TOUCHSCREENGUI + const float s = m_gui_scale * RenderingEngine::getDisplayDensity() / 2; #else const float s = m_gui_scale; #endif diff --git a/src/gui/guiEditBox.cpp b/src/gui/guiEditBox.cpp index 8459107cd..4a0f5013d 100644 --- a/src/gui/guiEditBox.cpp +++ b/src/gui/guiEditBox.cpp @@ -846,54 +846,3 @@ void GUIEditBox::updateVScrollBar() } } } - -void GUIEditBox::deserializeAttributes( - io::IAttributes *in, io::SAttributeReadWriteOptions *options = 0) -{ - IGUIEditBox::deserializeAttributes(in, options); - - setOverrideColor(in->getAttributeAsColor("OverrideColor")); - enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled")); - setMax(in->getAttributeAsInt("MaxChars")); - setWordWrap(in->getAttributeAsBool("WordWrap")); - setMultiLine(in->getAttributeAsBool("MultiLine")); - setAutoScroll(in->getAttributeAsBool("AutoScroll")); - core::stringw ch = in->getAttributeAsStringW("PasswordChar"); - - if (ch.empty()) - setPasswordBox(in->getAttributeAsBool("PasswordBox")); - else - setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]); - - setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration( - "HTextAlign", GUIAlignmentNames), - (EGUI_ALIGNMENT)in->getAttributeAsEnumeration( - "VTextAlign", GUIAlignmentNames)); - - setWritable(in->getAttributeAsBool("Writable")); - // setOverrideFont(in->getAttributeAsFont("OverrideFont")); -} - -//! Writes attributes of the element. -void GUIEditBox::serializeAttributes( - io::IAttributes *out, io::SAttributeReadWriteOptions *options = 0) const -{ - // IGUIEditBox::serializeAttributes(out,options); - - out->addBool("OverrideColorEnabled", m_override_color_enabled); - out->addColor("OverrideColor", m_override_color); - // out->addFont("OverrideFont",m_override_font); - out->addInt("MaxChars", m_max); - out->addBool("WordWrap", m_word_wrap); - out->addBool("MultiLine", m_multiline); - out->addBool("AutoScroll", m_autoscroll); - out->addBool("PasswordBox", m_passwordbox); - core::stringw ch = L" "; - ch[0] = m_passwordchar; - out->addString("PasswordChar", ch.c_str()); - out->addEnum("HTextAlign", m_halign, GUIAlignmentNames); - out->addEnum("VTextAlign", m_valign, GUIAlignmentNames); - out->addBool("Writable", m_writable); - - IGUIEditBox::serializeAttributes(out, options); -} diff --git a/src/gui/guiEditBox.h b/src/gui/guiEditBox.h index 2a5c911bc..4c7413f54 100644 --- a/src/gui/guiEditBox.h +++ b/src/gui/guiEditBox.h @@ -130,14 +130,6 @@ public: //! called if an event happened. virtual bool OnEvent(const SEvent &event); - //! Writes attributes of the element. - virtual void serializeAttributes(io::IAttributes *out, - io::SAttributeReadWriteOptions *options) const; - - //! Reads attributes of the element - virtual void deserializeAttributes( - io::IAttributes *in, io::SAttributeReadWriteOptions *options); - virtual bool acceptsIME() { return isEnabled() && m_writable; }; protected: diff --git a/src/gui/guiEditBoxWithScrollbar.cpp b/src/gui/guiEditBoxWithScrollbar.cpp index fb4bc2a0b..1b7f7832a 100644 --- a/src/gui/guiEditBoxWithScrollbar.cpp +++ b/src/gui/guiEditBoxWithScrollbar.cpp @@ -652,26 +652,6 @@ void GUIEditBoxWithScrollBar::setBackgroundColor(const video::SColor &bg_color) m_bg_color_used = true; } -//! Writes attributes of the element. -void GUIEditBoxWithScrollBar::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options = 0) const -{ - out->addBool("Border", m_border); - out->addBool("Background", m_background); - // out->addFont("OverrideFont", OverrideFont); - - GUIEditBox::serializeAttributes(out, options); -} - - -//! Reads attributes of the element -void GUIEditBoxWithScrollBar::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options = 0) -{ - GUIEditBox::deserializeAttributes(in, options); - - setDrawBorder(in->getAttributeAsBool("Border")); - setDrawBackground(in->getAttributeAsBool("Background")); -} - bool GUIEditBoxWithScrollBar::isDrawBackgroundEnabled() const { return false; } bool GUIEditBoxWithScrollBar::isDrawBorderEnabled() const { return false; } void GUIEditBoxWithScrollBar::setCursorChar(const wchar_t cursorChar) { } diff --git a/src/gui/guiEditBoxWithScrollbar.h b/src/gui/guiEditBoxWithScrollbar.h index 3f7450dcb..cea482fc2 100644 --- a/src/gui/guiEditBoxWithScrollbar.h +++ b/src/gui/guiEditBoxWithScrollbar.h @@ -31,12 +31,6 @@ public: //! Change the background color virtual void setBackgroundColor(const video::SColor &bg_color); - //! Writes attributes of the element. - virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const; - - //! Reads attributes of the element - virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options); - virtual bool isDrawBackgroundEnabled() const; virtual bool isDrawBorderEnabled() const; virtual void setCursorChar(const wchar_t cursorChar); diff --git a/src/gui/guiEngine.cpp b/src/gui/guiEngine.cpp index c39c3ee0d..b65b31304 100644 --- a/src/gui/guiEngine.cpp +++ b/src/gui/guiEngine.cpp @@ -104,16 +104,22 @@ void MenuMusicFetcher::fetchSounds(const std::string &name, if(m_fetched.count(name)) return; m_fetched.insert(name); - std::string base; - base = porting::path_share + DIR_DELIM + "sounds"; - dst_paths.insert(base + DIR_DELIM + name + ".ogg"); - int i; - for(i=0; i<10; i++) - dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg"); - base = porting::path_user + DIR_DELIM + "sounds"; - dst_paths.insert(base + DIR_DELIM + name + ".ogg"); - for(i=0; i<10; i++) - dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg"); + std::vector<fs::DirListNode> list; + // Reusable local function + auto add_paths = [&dst_paths](const std::string name, const std::string base = "") { + dst_paths.insert(base + name + ".ogg"); + for (int i = 0; i < 10; i++) + dst_paths.insert(base + name + "." + itos(i) + ".ogg"); + }; + // Allow full paths + if (name.find(DIR_DELIM_CHAR) != std::string::npos) { + add_paths(name); + } else { + std::string share_prefix = porting::path_share + DIR_DELIM; + add_paths(name, share_prefix + "sounds" + DIR_DELIM); + std::string user_prefix = porting::path_user + DIR_DELIM; + add_paths(name, user_prefix + "sounds" + DIR_DELIM); + } } /******************************************************************************/ diff --git a/src/gui/guiFormSpecMenu.cpp b/src/gui/guiFormSpecMenu.cpp index 797fd3ff6..f3570ccaf 100644 --- a/src/gui/guiFormSpecMenu.cpp +++ b/src/gui/guiFormSpecMenu.cpp @@ -81,6 +81,13 @@ with this program; if not, write to the Free Software Foundation, Inc., " specified: \"" << parts[b] << "\"" << std::endl; \ return; \ } + +#define MY_CHECKCLIENT(a) \ + if (!m_client) { \ + errorstream << "Attempted to use element " << a << " with m_client == nullptr." << std::endl; \ + return; \ + } + /* GUIFormSpecMenu */ @@ -130,8 +137,6 @@ GUIFormSpecMenu::~GUIFormSpecMenu() checkbox_it.second->drop(); for (auto &scrollbar_it : m_scrollbars) scrollbar_it.second->drop(); - for (auto &background_it : m_backgrounds) - background_it->drop(); for (auto &tooltip_rect_it : m_tooltip_rects) tooltip_rect_it.first->drop(); for (auto &clickthrough_it : m_clickthrough_elements) @@ -294,8 +299,20 @@ v2s32 GUIFormSpecMenu::getRealCoordinateGeometry(const std::vector<std::string> return v2s32(stof(v_geom[0]) * imgsize.X, stof(v_geom[1]) * imgsize.Y); } +bool GUIFormSpecMenu::precheckElement(const std::string &name, const std::string &element, + size_t args_min, size_t args_max, std::vector<std::string> &parts) +{ + parts = split(element, ';'); + if (parts.size() >= args_min && (parts.size() <= args_max || m_formspec_version > FORMSPEC_API_VERSION)) + return true; + + errorstream << "Invalid " << name << " element(" << parts.size() << "): '" << element << "'" << std::endl; + return false; +} + void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element) { + // Note: do not use precheckElement due to "," separator. std::vector<std::string> parts = split(element,','); if (((parts.size() == 2) || parts.size() == 3) || @@ -308,7 +325,7 @@ void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element) data->invsize.Y = MYMAX(0, stof(parts[1])); lockSize(false); -#ifndef __ANDROID__ +#ifndef HAVE_TOUCHSCREENGUI if (parts.size() == 3) { if (parts[2] == "true") { lockSize(true,v2u32(800,600)); @@ -349,14 +366,9 @@ void GUIFormSpecMenu::parseContainerEnd(parserData* data) void GUIFormSpecMenu::parseScrollContainer(parserData *data, const std::string &element) { - std::vector<std::string> parts = split(element, ';'); - - if (parts.size() < 4 || - (parts.size() > 5 && m_formspec_version <= FORMSPEC_API_VERSION)) { - errorstream << "Invalid scroll_container start element (" << parts.size() - << "): '" << element << "'" << std::endl; + std::vector<std::string> parts; + if (!precheckElement("scroll_container start", element, 4, 5, parts)) return; - } std::vector<std::string> v_pos = split(parts[0], ','); std::vector<std::string> v_geom = split(parts[1], ','); @@ -445,105 +457,95 @@ void GUIFormSpecMenu::parseScrollContainerEnd(parserData *data) void GUIFormSpecMenu::parseList(parserData *data, const std::string &element) { - if (m_client == 0) { - warningstream<<"invalid use of 'list' with m_client==0"<<std::endl; - return; - } + MY_CHECKCLIENT("list"); - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + if (!precheckElement("list", element, 4, 5, parts)) + return; - if (((parts.size() == 4) || (parts.size() == 5)) || - ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::string location = parts[0]; - std::string listname = parts[1]; - std::vector<std::string> v_pos = split(parts[2],','); - std::vector<std::string> v_geom = split(parts[3],','); - std::string startindex; - if (parts.size() == 5) - startindex = parts[4]; + std::string location = parts[0]; + std::string listname = parts[1]; + std::vector<std::string> v_pos = split(parts[2],','); + std::vector<std::string> v_geom = split(parts[3],','); + std::string startindex; + if (parts.size() == 5) + startindex = parts[4]; - MY_CHECKPOS("list",2); - MY_CHECKGEOM("list",3); + MY_CHECKPOS("list",2); + MY_CHECKGEOM("list",3); - InventoryLocation loc; + InventoryLocation loc; - if (location == "context" || location == "current_name") - loc = m_current_inventory_location; - else - loc.deSerialize(location); + if (location == "context" || location == "current_name") + loc = m_current_inventory_location; + else + loc.deSerialize(location); - v2s32 geom; - geom.X = stoi(v_geom[0]); - geom.Y = stoi(v_geom[1]); + v2s32 geom; + geom.X = stoi(v_geom[0]); + geom.Y = stoi(v_geom[1]); - s32 start_i = 0; - if (!startindex.empty()) - start_i = stoi(startindex); + s32 start_i = 0; + if (!startindex.empty()) + start_i = stoi(startindex); - if (geom.X < 0 || geom.Y < 0 || start_i < 0) { - errorstream << "Invalid list element: '" << element << "'" << std::endl; - return; - } + if (geom.X < 0 || geom.Y < 0 || start_i < 0) { + errorstream << "Invalid list element: '" << element << "'" << std::endl; + return; + } - if (!data->explicit_size) - warningstream << "invalid use of list without a size[] element" << std::endl; + if (!data->explicit_size) + warningstream << "invalid use of list without a size[] element" << std::endl; - FieldSpec spec( - "", - L"", - L"", - 258 + m_fields.size(), - 3 - ); + FieldSpec spec( + "", + L"", + L"", + 258 + m_fields.size(), + 3 + ); - auto style = getDefaultStyleForElement("list", spec.fname); + auto style = getDefaultStyleForElement("list", spec.fname); - v2f32 slot_scale = style.getVector2f(StyleSpec::SIZE, v2f32(0, 0)); - v2f32 slot_size( - slot_scale.X <= 0 ? imgsize.X : std::max<f32>(slot_scale.X * imgsize.X, 1), - slot_scale.Y <= 0 ? imgsize.Y : std::max<f32>(slot_scale.Y * imgsize.Y, 1) - ); + v2f32 slot_scale = style.getVector2f(StyleSpec::SIZE, v2f32(0, 0)); + v2f32 slot_size( + slot_scale.X <= 0 ? imgsize.X : std::max<f32>(slot_scale.X * imgsize.X, 1), + slot_scale.Y <= 0 ? imgsize.Y : std::max<f32>(slot_scale.Y * imgsize.Y, 1) + ); - v2f32 slot_spacing = style.getVector2f(StyleSpec::SPACING, v2f32(-1, -1)); - v2f32 default_spacing = data->real_coordinates ? - v2f32(imgsize.X * 0.25f, imgsize.Y * 0.25f) : - v2f32(spacing.X - imgsize.X, spacing.Y - imgsize.Y); + v2f32 slot_spacing = style.getVector2f(StyleSpec::SPACING, v2f32(-1, -1)); + v2f32 default_spacing = data->real_coordinates ? + v2f32(imgsize.X * 0.25f, imgsize.Y * 0.25f) : + v2f32(spacing.X - imgsize.X, spacing.Y - imgsize.Y); - slot_spacing.X = slot_spacing.X < 0 ? default_spacing.X : - imgsize.X * slot_spacing.X; - slot_spacing.Y = slot_spacing.Y < 0 ? default_spacing.Y : - imgsize.Y * slot_spacing.Y; + slot_spacing.X = slot_spacing.X < 0 ? default_spacing.X : + imgsize.X * slot_spacing.X; + slot_spacing.Y = slot_spacing.Y < 0 ? default_spacing.Y : + imgsize.Y * slot_spacing.Y; - slot_spacing += slot_size; + slot_spacing += slot_size; - v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos) : - getElementBasePos(&v_pos); + v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos) : + getElementBasePos(&v_pos); - core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, - pos.X + (geom.X - 1) * slot_spacing.X + slot_size.X, - pos.Y + (geom.Y - 1) * slot_spacing.Y + slot_size.Y); + core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, + pos.X + (geom.X - 1) * slot_spacing.X + slot_size.X, + pos.Y + (geom.Y - 1) * slot_spacing.Y + slot_size.Y); - GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent, - spec.fid, rect, m_invmgr, loc, listname, geom, start_i, - v2s32(slot_size.X, slot_size.Y), slot_spacing, this, - data->inventorylist_options, m_font); + GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent, + spec.fid, rect, m_invmgr, loc, listname, geom, start_i, + v2s32(slot_size.X, slot_size.Y), slot_spacing, this, + data->inventorylist_options, m_font); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); - m_inventorylists.push_back(e); - m_fields.push_back(spec); - return; - } - errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl; + m_inventorylists.push_back(e); + m_fields.push_back(spec); } void GUIFormSpecMenu::parseListRing(parserData *data, const std::string &element) { - if (m_client == 0) { - errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl; - return; - } + MY_CHECKCLIENT("listring"); std::vector<std::string> parts = split(element, ';'); @@ -578,157 +580,150 @@ void GUIFormSpecMenu::parseListRing(parserData *data, const std::string &element void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); - - 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],','); - std::string name = parts[1]; - std::string label = parts[2]; - std::string selected; + std::vector<std::string> parts; + if (!precheckElement("checkbox", element, 3, 4, parts)) + return; - if (parts.size() >= 4) - selected = parts[3]; + std::vector<std::string> v_pos = split(parts[0],','); + std::string name = parts[1]; + std::string label = parts[2]; + std::string selected; - MY_CHECKPOS("checkbox",0); + if (parts.size() >= 4) + selected = parts[3]; - bool fselected = false; + MY_CHECKPOS("checkbox",0); - if (selected == "true") - fselected = true; + bool fselected = false; - std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label))); - const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str()); - s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH); - s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2; + if (selected == "true") + fselected = true; - v2s32 pos; - core::rect<s32> rect; + std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label))); + const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str()); + s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH); + s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2; - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); + v2s32 pos; + core::rect<s32> rect; - rect = core::rect<s32>( - pos.X, - pos.Y - y_center, - pos.X + label_size.Width + cb_size + 7, - pos.Y + y_center - ); - } else { - pos = getElementBasePos(&v_pos); - rect = core::rect<s32>( - pos.X, - pos.Y + imgsize.Y / 2 - y_center, - pos.X + label_size.Width + cb_size + 7, - pos.Y + imgsize.Y / 2 + y_center - ); - } + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); - FieldSpec spec( - name, - wlabel, //Needed for displaying text on MSVC - wlabel, - 258+m_fields.size() + rect = core::rect<s32>( + pos.X, + pos.Y - y_center, + pos.X + label_size.Width + cb_size + 7, + pos.Y + y_center + ); + } else { + pos = getElementBasePos(&v_pos); + rect = core::rect<s32>( + pos.X, + pos.Y + imgsize.Y / 2 - y_center, + pos.X + label_size.Width + cb_size + 7, + pos.Y + imgsize.Y / 2 + y_center ); + } - spec.ftype = f_CheckBox; + FieldSpec spec( + name, + wlabel, //Needed for displaying text on MSVC + wlabel, + 258+m_fields.size() + ); - gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect, - data->current_parent, spec.fid, spec.flabel.c_str()); + spec.ftype = f_CheckBox; - auto style = getDefaultStyleForElement("checkbox", name); + gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect, + data->current_parent, spec.fid, spec.flabel.c_str()); - spec.sound = style.get(StyleSpec::Property::SOUND, ""); + auto style = getDefaultStyleForElement("checkbox", name); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); + spec.sound = style.get(StyleSpec::Property::SOUND, ""); - if (spec.fname == m_focused_element) { - Environment->setFocus(e); - } + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); - e->grab(); - m_checkboxes.emplace_back(spec, e); - m_fields.push_back(spec); - return; + if (spec.fname == m_focused_element) { + Environment->setFocus(e); } - errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl; + + e->grab(); + m_checkboxes.emplace_back(spec, e); + m_fields.push_back(spec); } void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); - - if (parts.size() >= 5) { - std::vector<std::string> v_pos = split(parts[0],','); - std::vector<std::string> v_geom = split(parts[1],','); - std::string name = parts[3]; - std::string value = parts[4]; + std::vector<std::string> parts; + if (!precheckElement("scrollbar", element, 5, 5, parts)) + return; - MY_CHECKPOS("scrollbar",0); - MY_CHECKGEOM("scrollbar",1); + std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> v_geom = split(parts[1],','); + std::string name = parts[3]; + std::string value = parts[4]; - v2s32 pos; - v2s32 dim; + MY_CHECKPOS("scrollbar",0); + MY_CHECKGEOM("scrollbar",1); - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); - dim = getRealCoordinateGeometry(v_geom); - } else { - pos = getElementBasePos(&v_pos); - dim.X = stof(v_geom[0]) * spacing.X; - dim.Y = stof(v_geom[1]) * spacing.Y; - } + v2s32 pos; + v2s32 dim; - core::rect<s32> rect = - core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y); + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + dim = getRealCoordinateGeometry(v_geom); + } else { + pos = getElementBasePos(&v_pos); + dim.X = stof(v_geom[0]) * spacing.X; + dim.Y = stof(v_geom[1]) * spacing.Y; + } - FieldSpec spec( - name, - L"", - L"", - 258+m_fields.size() - ); + core::rect<s32> rect = + core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y); - bool is_horizontal = true; + FieldSpec spec( + name, + L"", + L"", + 258+m_fields.size() + ); - if (parts[2] == "vertical") - is_horizontal = false; + bool is_horizontal = true; - spec.ftype = f_ScrollBar; - spec.send = true; - GUIScrollBar *e = new GUIScrollBar(Environment, data->current_parent, - spec.fid, rect, is_horizontal, true); + if (parts[2] == "vertical") + is_horizontal = false; - auto style = getDefaultStyleForElement("scrollbar", name); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); - e->setArrowsVisible(data->scrollbar_options.arrow_visiblity); + spec.ftype = f_ScrollBar; + spec.send = true; + GUIScrollBar *e = new GUIScrollBar(Environment, data->current_parent, + spec.fid, rect, is_horizontal, true); - s32 max = data->scrollbar_options.max; - s32 min = data->scrollbar_options.min; + auto style = getDefaultStyleForElement("scrollbar", name); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); + e->setArrowsVisible(data->scrollbar_options.arrow_visiblity); - e->setMax(max); - e->setMin(min); + s32 max = data->scrollbar_options.max; + s32 min = data->scrollbar_options.min; - e->setPos(stoi(parts[4])); + e->setMax(max); + e->setMin(min); - e->setSmallStep(data->scrollbar_options.small_step); - e->setLargeStep(data->scrollbar_options.large_step); + e->setPos(stoi(parts[4])); - s32 scrollbar_size = is_horizontal ? dim.X : dim.Y; + e->setSmallStep(data->scrollbar_options.small_step); + e->setLargeStep(data->scrollbar_options.large_step); - e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size); + s32 scrollbar_size = is_horizontal ? dim.X : dim.Y; - if (spec.fname == m_focused_element) { - Environment->setFocus(e); - } + e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size); - m_scrollbars.emplace_back(spec,e); - m_fields.push_back(spec); - return; + if (spec.fname == m_focused_element) { + Environment->setFocus(e); } - errorstream << "Invalid scrollbar element(" << parts.size() << "): '" << element - << "'" << std::endl; + + m_scrollbars.emplace_back(spec,e); + m_fields.push_back(spec); } void GUIFormSpecMenu::parseScrollBarOptions(parserData* data, const std::string &element) @@ -786,11 +781,11 @@ void GUIFormSpecMenu::parseScrollBarOptions(parserData* data, const std::string void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + if (!precheckElement("image", element, 2, 3, parts)) + return; - if ((parts.size() == 3) || - ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION))) - { + if (parts.size() >= 3) { std::vector<std::string> v_pos = split(parts[0],','); std::vector<std::string> v_geom = split(parts[1],','); std::string name = unescape_string(parts[2]); @@ -842,54 +837,47 @@ void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element) return; } - if (parts.size() == 2) { - std::vector<std::string> v_pos = split(parts[0],','); - std::string name = unescape_string(parts[1]); - - MY_CHECKPOS("image", 0); + // Else: 2 arguments in "parts" - v2s32 pos = getElementBasePos(&v_pos); + std::vector<std::string> v_pos = split(parts[0],','); + std::string name = unescape_string(parts[1]); - if (!data->explicit_size) - warningstream<<"invalid use of image without a size[] element"<<std::endl; + MY_CHECKPOS("image", 0); - video::ITexture *texture = m_tsrc->getTexture(name); - if (!texture) { - errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:" - << std::endl << "\t" << name << std::endl; - return; - } + v2s32 pos = getElementBasePos(&v_pos); - FieldSpec spec( - name, - L"", - L"", - 258 + m_fields.size() - ); - gui::IGUIImage *e = Environment->addImage(texture, pos, true, - data->current_parent, spec.fid, 0); - auto style = getDefaultStyleForElement("image", spec.fname); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3)); - m_fields.push_back(spec); + if (!data->explicit_size) + warningstream<<"invalid use of image without a size[] element"<<std::endl; - // images should let events through - e->grab(); - m_clickthrough_elements.push_back(e); + video::ITexture *texture = m_tsrc->getTexture(name); + if (!texture) { + errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:" + << std::endl << "\t" << name << std::endl; return; } - errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl; + + FieldSpec spec( + name, + L"", + L"", + 258 + m_fields.size() + ); + gui::IGUIImage *e = Environment->addImage(texture, pos, true, + data->current_parent, spec.fid, 0); + auto style = getDefaultStyleForElement("image", spec.fname); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3)); + m_fields.push_back(spec); + + // images should let events through + e->grab(); + m_clickthrough_elements.push_back(e); } void GUIFormSpecMenu::parseAnimatedImage(parserData *data, const std::string &element) { - std::vector<std::string> parts = split(element, ';'); - - if (parts.size() != 6 && parts.size() != 7 && - !(parts.size() > 7 && m_formspec_version > FORMSPEC_API_VERSION)) { - errorstream << "Invalid animated_image element(" << parts.size() - << "): '" << element << "'" << std::endl; + std::vector<std::string> parts; + if (!precheckElement("animated_image", element, 6, 7, parts)) return; - } std::vector<std::string> v_pos = split(parts[0], ','); std::vector<std::string> v_geom = split(parts[1], ','); @@ -944,218 +932,205 @@ void GUIFormSpecMenu::parseAnimatedImage(parserData *data, const std::string &el void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + if (!precheckElement("item_image", element, 3, 3, parts)) + return; - if ((parts.size() == 3) || - ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::vector<std::string> v_pos = split(parts[0],','); - std::vector<std::string> v_geom = split(parts[1],','); - std::string name = parts[2]; + std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> v_geom = split(parts[1],','); + std::string name = parts[2]; - MY_CHECKPOS("itemimage",0); - MY_CHECKGEOM("itemimage",1); + MY_CHECKPOS("item_image",0); + MY_CHECKGEOM("item_image",1); - v2s32 pos; - v2s32 geom; + v2s32 pos; + v2s32 geom; - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); - geom = getRealCoordinateGeometry(v_geom); - } else { - pos = getElementBasePos(&v_pos); - geom.X = stof(v_geom[0]) * (float)imgsize.X; - geom.Y = stof(v_geom[1]) * (float)imgsize.Y; - } + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + } else { + pos = getElementBasePos(&v_pos); + geom.X = stof(v_geom[0]) * (float)imgsize.X; + geom.Y = stof(v_geom[1]) * (float)imgsize.Y; + } - if(!data->explicit_size) - warningstream<<"invalid use of item_image without a size[] element"<<std::endl; + if(!data->explicit_size) + warningstream<<"invalid use of item_image without a size[] element"<<std::endl; - FieldSpec spec( - "", - L"", - L"", - 258 + m_fields.size(), - 2 - ); - spec.ftype = f_ItemImage; + FieldSpec spec( + "", + L"", + L"", + 258 + m_fields.size(), + 2 + ); + spec.ftype = f_ItemImage; - GUIItemImage *e = new GUIItemImage(Environment, data->current_parent, spec.fid, - core::rect<s32>(pos, pos + geom), name, m_font, m_client); - auto style = getDefaultStyleForElement("item_image", spec.fname); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); + GUIItemImage *e = new GUIItemImage(Environment, data->current_parent, spec.fid, + core::rect<s32>(pos, pos + geom), name, m_font, m_client); + auto style = getDefaultStyleForElement("item_image", spec.fname); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); - // item images should let events through - m_clickthrough_elements.push_back(e); + // item images should let events through + m_clickthrough_elements.push_back(e); - m_fields.push_back(spec); - return; - } - errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl; + m_fields.push_back(spec); } void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element, const std::string &type) { - std::vector<std::string> parts = split(element,';'); - - if ((parts.size() == 4) || - ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::vector<std::string> v_pos = split(parts[0],','); - std::vector<std::string> v_geom = split(parts[1],','); - std::string name = parts[2]; - std::string label = parts[3]; + std::vector<std::string> parts; + if (!precheckElement("button", element, 4, 4, parts)) + return; - MY_CHECKPOS("button",0); - MY_CHECKGEOM("button",1); + std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> v_geom = split(parts[1],','); + std::string name = parts[2]; + std::string label = parts[3]; - v2s32 pos; - v2s32 geom; - core::rect<s32> rect; + MY_CHECKPOS("button",0); + MY_CHECKGEOM("button",1); - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); - geom = getRealCoordinateGeometry(v_geom); - rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, - pos.Y+geom.Y); - } else { - pos = getElementBasePos(&v_pos); - geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X); - pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2; + v2s32 pos; + v2s32 geom; + core::rect<s32> rect; - rect = core::rect<s32>(pos.X, pos.Y - m_btn_height, - pos.X + geom.X, pos.Y + m_btn_height); - } + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, + pos.Y+geom.Y); + } else { + pos = getElementBasePos(&v_pos); + geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X); + pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2; - if(!data->explicit_size) - warningstream<<"invalid use of button without a size[] element"<<std::endl; + rect = core::rect<s32>(pos.X, pos.Y - m_btn_height, + pos.X + geom.X, pos.Y + m_btn_height); + } - std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label))); + if(!data->explicit_size) + warningstream<<"invalid use of button without a size[] element"<<std::endl; - FieldSpec spec( - name, - wlabel, - L"", - 258 + m_fields.size() - ); - spec.ftype = f_Button; - if(type == "button_exit") - spec.is_exit = true; + std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label))); - GUIButton *e = GUIButton::addButton(Environment, rect, m_tsrc, - data->current_parent, spec.fid, spec.flabel.c_str()); + FieldSpec spec( + name, + wlabel, + L"", + 258 + m_fields.size() + ); + spec.ftype = f_Button; + if(type == "button_exit") + spec.is_exit = true; - auto style = getStyleForElement(type, name, (type != "button") ? "button" : ""); + GUIButton *e = GUIButton::addButton(Environment, rect, m_tsrc, + data->current_parent, spec.fid, spec.flabel.c_str()); - spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, ""); + auto style = getStyleForElement(type, name, (type != "button") ? "button" : ""); - e->setStyles(style); + spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, ""); - if (spec.fname == m_focused_element) { - Environment->setFocus(e); - } + e->setStyles(style); - m_fields.push_back(spec); - return; + if (spec.fname == m_focused_element) { + Environment->setFocus(e); } - errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl; + + m_fields.push_back(spec); } void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + if (!precheckElement("background", element, 3, 5, parts)) + return; - if ((parts.size() >= 3 && parts.size() <= 5) || - (parts.size() > 5 && m_formspec_version > FORMSPEC_API_VERSION)) { - std::vector<std::string> v_pos = split(parts[0],','); - std::vector<std::string> v_geom = split(parts[1],','); - std::string name = unescape_string(parts[2]); + std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> v_geom = split(parts[1],','); + std::string name = unescape_string(parts[2]); - MY_CHECKPOS("background",0); - MY_CHECKGEOM("background",1); + MY_CHECKPOS("background",0); + MY_CHECKGEOM("background",1); - v2s32 pos; - v2s32 geom; + v2s32 pos; + v2s32 geom; - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); - geom = getRealCoordinateGeometry(v_geom); - } else { - pos = getElementBasePos(&v_pos); - pos.X -= (spacing.X - (float)imgsize.X) / 2; - pos.Y -= (spacing.Y - (float)imgsize.Y) / 2; + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + } else { + pos = getElementBasePos(&v_pos); + pos.X -= (spacing.X - (float)imgsize.X) / 2; + pos.Y -= (spacing.Y - (float)imgsize.Y) / 2; - geom.X = stof(v_geom[0]) * spacing.X; - geom.Y = stof(v_geom[1]) * spacing.Y; - } + geom.X = stof(v_geom[0]) * spacing.X; + geom.Y = stof(v_geom[1]) * spacing.Y; + } - bool clip = false; - if (parts.size() >= 4 && is_yes(parts[3])) { - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos) * -1; - geom = v2s32(0, 0); - } else { - pos.X = stoi(v_pos[0]); //acts as offset - pos.Y = stoi(v_pos[1]); - } - clip = true; + bool clip = false; + if (parts.size() >= 4 && is_yes(parts[3])) { + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos) * -1; + geom = v2s32(0, 0); + } else { + pos.X = stoi(v_pos[0]); //acts as offset + pos.Y = stoi(v_pos[1]); } + clip = true; + } - core::rect<s32> middle; - if (parts.size() >= 5) { - std::vector<std::string> v_middle = split(parts[4], ','); - if (v_middle.size() == 1) { - s32 x = stoi(v_middle[0]); - middle.UpperLeftCorner = core::vector2di(x, x); - middle.LowerRightCorner = core::vector2di(-x, -x); - } else if (v_middle.size() == 2) { - s32 x = stoi(v_middle[0]); - s32 y = stoi(v_middle[1]); - middle.UpperLeftCorner = core::vector2di(x, y); - middle.LowerRightCorner = core::vector2di(-x, -y); - // `-x` is interpreted as `w - x` - } else if (v_middle.size() == 4) { - middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1])); - middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3])); - } else { - warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl; - } + core::rect<s32> middle; + if (parts.size() >= 5) { + std::vector<std::string> v_middle = split(parts[4], ','); + if (v_middle.size() == 1) { + s32 x = stoi(v_middle[0]); + middle.UpperLeftCorner = core::vector2di(x, x); + middle.LowerRightCorner = core::vector2di(-x, -x); + } else if (v_middle.size() == 2) { + s32 x = stoi(v_middle[0]); + s32 y = stoi(v_middle[1]); + middle.UpperLeftCorner = core::vector2di(x, y); + middle.LowerRightCorner = core::vector2di(-x, -y); + // `-x` is interpreted as `w - x` + } else if (v_middle.size() == 4) { + middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1])); + middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3])); + } else { + warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl; } + } - if (!data->explicit_size && !clip) - warningstream << "invalid use of unclipped background without a size[] element" << std::endl; - - FieldSpec spec( - name, - L"", - L"", - 258 + m_fields.size() - ); + if (!data->explicit_size && !clip) + warningstream << "invalid use of unclipped background without a size[] element" << std::endl; - core::rect<s32> rect; - if (!clip) { - // no auto_clip => position like normal image - rect = core::rect<s32>(pos, pos + geom); - } else { - // it will be auto-clipped when drawing - rect = core::rect<s32>(-pos, pos); - } + FieldSpec spec( + name, + L"", + L"", + 258 + m_fields.size() + ); - GUIBackgroundImage *e = new GUIBackgroundImage(Environment, this, spec.fid, - rect, name, middle, m_tsrc, clip); + core::rect<s32> rect; + if (!clip) { + // no auto_clip => position like normal image + rect = core::rect<s32>(pos, pos + geom); + } else { + // it will be auto-clipped when drawing + rect = core::rect<s32>(-pos, pos); + } - FATAL_ERROR_IF(!e, "Failed to create background formspec element"); + GUIBackgroundImage *e = new GUIBackgroundImage(Environment, data->background_parent.get(), + spec.fid, rect, name, middle, m_tsrc, clip); - e->setNotClipped(true); + FATAL_ERROR_IF(!e, "Failed to create background formspec element"); - e->setVisible(false); // the element is drawn manually before all others + e->setNotClipped(true); - m_backgrounds.push_back(e); - m_fields.push_back(spec); - return; - } - errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl; + m_fields.push_back(spec); + e->drop(); } void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element) @@ -1192,338 +1167,320 @@ void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &ele void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + if (!precheckElement("table", element, 4, 5, parts)) + return; - if (((parts.size() == 4) || (parts.size() == 5)) || - ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::vector<std::string> v_pos = split(parts[0],','); - std::vector<std::string> v_geom = split(parts[1],','); - std::string name = parts[2]; - std::vector<std::string> items = split(parts[3],','); - std::string str_initial_selection; - std::string str_transparent = "false"; + std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> v_geom = split(parts[1],','); + std::string name = parts[2]; + std::vector<std::string> items = split(parts[3],','); + std::string str_initial_selection; + std::string str_transparent = "false"; - if (parts.size() >= 5) - str_initial_selection = parts[4]; + if (parts.size() >= 5) + str_initial_selection = parts[4]; - MY_CHECKPOS("table",0); - MY_CHECKGEOM("table",1); + MY_CHECKPOS("table",0); + MY_CHECKGEOM("table",1); - v2s32 pos; - v2s32 geom; + v2s32 pos; + v2s32 geom; - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); - geom = getRealCoordinateGeometry(v_geom); - } else { - pos = getElementBasePos(&v_pos); - geom.X = stof(v_geom[0]) * spacing.X; - geom.Y = stof(v_geom[1]) * spacing.Y; - } + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + } else { + pos = getElementBasePos(&v_pos); + geom.X = stof(v_geom[0]) * spacing.X; + geom.Y = stof(v_geom[1]) * spacing.Y; + } - core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y); + core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y); - FieldSpec spec( - name, - L"", - L"", - 258 + m_fields.size() - ); + FieldSpec spec( + name, + L"", + L"", + 258 + m_fields.size() + ); - spec.ftype = f_Table; + spec.ftype = f_Table; - for (std::string &item : items) { - item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item)))); - } + for (std::string &item : items) { + item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item)))); + } - //now really show table - GUITable *e = new GUITable(Environment, data->current_parent, spec.fid, - rect, m_tsrc); + //now really show table + GUITable *e = new GUITable(Environment, data->current_parent, spec.fid, + rect, m_tsrc); - if (spec.fname == m_focused_element) { - Environment->setFocus(e); - } + if (spec.fname == m_focused_element) { + Environment->setFocus(e); + } - e->setTable(data->table_options, data->table_columns, items); + e->setTable(data->table_options, data->table_columns, items); - if (data->table_dyndata.find(name) != data->table_dyndata.end()) { - e->setDynamicData(data->table_dyndata[name]); - } + if (data->table_dyndata.find(name) != data->table_dyndata.end()) { + e->setDynamicData(data->table_dyndata[name]); + } - if (!str_initial_selection.empty() && str_initial_selection != "0") - e->setSelected(stoi(str_initial_selection)); + if (!str_initial_selection.empty() && str_initial_selection != "0") + e->setSelected(stoi(str_initial_selection)); - auto style = getDefaultStyleForElement("table", name); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); - e->setOverrideFont(style.getFont()); + auto style = getDefaultStyleForElement("table", name); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); + e->setOverrideFont(style.getFont()); - m_tables.emplace_back(spec, e); - m_fields.push_back(spec); - return; - } - errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl; + m_tables.emplace_back(spec, e); + m_fields.push_back(spec); } void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + if (!precheckElement("textlist", element, 4, 6, parts)) + return; - if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) || - ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::vector<std::string> v_pos = split(parts[0],','); - std::vector<std::string> v_geom = split(parts[1],','); - std::string name = parts[2]; - std::vector<std::string> items = split(parts[3],','); - std::string str_initial_selection; - std::string str_transparent = "false"; + std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> v_geom = split(parts[1],','); + std::string name = parts[2]; + std::vector<std::string> items = split(parts[3],','); + std::string str_initial_selection; + std::string str_transparent = "false"; - if (parts.size() >= 5) - str_initial_selection = parts[4]; + if (parts.size() >= 5) + str_initial_selection = parts[4]; - if (parts.size() >= 6) - str_transparent = parts[5]; + if (parts.size() >= 6) + str_transparent = parts[5]; - MY_CHECKPOS("textlist",0); - MY_CHECKGEOM("textlist",1); + MY_CHECKPOS("textlist",0); + MY_CHECKGEOM("textlist",1); - v2s32 pos; - v2s32 geom; + v2s32 pos; + v2s32 geom; - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); - geom = getRealCoordinateGeometry(v_geom); - } else { - pos = getElementBasePos(&v_pos); - geom.X = stof(v_geom[0]) * spacing.X; - geom.Y = stof(v_geom[1]) * spacing.Y; - } + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + } else { + pos = getElementBasePos(&v_pos); + geom.X = stof(v_geom[0]) * spacing.X; + geom.Y = stof(v_geom[1]) * spacing.Y; + } - core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y); + core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y); - FieldSpec spec( - name, - L"", - L"", - 258 + m_fields.size() - ); + FieldSpec spec( + name, + L"", + L"", + 258 + m_fields.size() + ); - spec.ftype = f_Table; + spec.ftype = f_Table; - for (std::string &item : items) { - item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item)))); - } + for (std::string &item : items) { + item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item)))); + } - //now really show list - GUITable *e = new GUITable(Environment, data->current_parent, spec.fid, - rect, m_tsrc); + //now really show list + GUITable *e = new GUITable(Environment, data->current_parent, spec.fid, + rect, m_tsrc); - if (spec.fname == m_focused_element) { - Environment->setFocus(e); - } + if (spec.fname == m_focused_element) { + Environment->setFocus(e); + } - e->setTextList(items, is_yes(str_transparent)); + e->setTextList(items, is_yes(str_transparent)); - if (data->table_dyndata.find(name) != data->table_dyndata.end()) { - e->setDynamicData(data->table_dyndata[name]); - } + if (data->table_dyndata.find(name) != data->table_dyndata.end()) { + e->setDynamicData(data->table_dyndata[name]); + } - if (!str_initial_selection.empty() && str_initial_selection != "0") - e->setSelected(stoi(str_initial_selection)); + if (!str_initial_selection.empty() && str_initial_selection != "0") + e->setSelected(stoi(str_initial_selection)); - auto style = getDefaultStyleForElement("textlist", name); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); - e->setOverrideFont(style.getFont()); + auto style = getDefaultStyleForElement("textlist", name); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); + e->setOverrideFont(style.getFont()); - m_tables.emplace_back(spec, e); - m_fields.push_back(spec); - return; - } - errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl; + m_tables.emplace_back(spec, e); + m_fields.push_back(spec); } void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element, ';'); - - if (parts.size() == 5 || parts.size() == 6 || - (parts.size() > 6 && m_formspec_version > FORMSPEC_API_VERSION)) - { - std::vector<std::string> v_pos = split(parts[0], ','); - std::string name = parts[2]; - std::vector<std::string> items = split(parts[3], ','); - std::string str_initial_selection = parts[4]; + std::vector<std::string> parts; + if (!precheckElement("dropdown", element, 5, 6, parts)) + return; - if (parts.size() >= 6 && is_yes(parts[5])) - m_dropdown_index_event[name] = true; + std::vector<std::string> v_pos = split(parts[0], ','); + std::string name = parts[2]; + std::vector<std::string> items = split(parts[3], ','); + std::string str_initial_selection = parts[4]; - MY_CHECKPOS("dropdown",0); + if (parts.size() >= 6 && is_yes(parts[5])) + m_dropdown_index_event[name] = true; - v2s32 pos; - v2s32 geom; - core::rect<s32> rect; + MY_CHECKPOS("dropdown",0); - if (data->real_coordinates) { - std::vector<std::string> v_geom = split(parts[1],','); + v2s32 pos; + v2s32 geom; + core::rect<s32> rect; - if (v_geom.size() == 1) - v_geom.emplace_back("1"); + if (data->real_coordinates) { + std::vector<std::string> v_geom = split(parts[1],','); - MY_CHECKGEOM("dropdown",1); + if (v_geom.size() == 1) + v_geom.emplace_back("1"); - pos = getRealCoordinateBasePos(v_pos); - geom = getRealCoordinateGeometry(v_geom); - rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y); - } else { - pos = getElementBasePos(&v_pos); + MY_CHECKGEOM("dropdown",1); - s32 width = stof(parts[1]) * spacing.Y; + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y); + } else { + pos = getElementBasePos(&v_pos); - rect = core::rect<s32>(pos.X, pos.Y, - pos.X + width, pos.Y + (m_btn_height * 2)); - } + s32 width = stof(parts[1]) * spacing.Y; - FieldSpec spec( - name, - L"", - L"", - 258 + m_fields.size() - ); + rect = core::rect<s32>(pos.X, pos.Y, + pos.X + width, pos.Y + (m_btn_height * 2)); + } - spec.ftype = f_DropDown; - spec.send = true; + FieldSpec spec( + name, + L"", + L"", + 258 + m_fields.size() + ); - //now really show list - gui::IGUIComboBox *e = Environment->addComboBox(rect, data->current_parent, - spec.fid); + spec.ftype = f_DropDown; + spec.send = true; - if (spec.fname == m_focused_element) { - Environment->setFocus(e); - } + //now really show list + gui::IGUIComboBox *e = Environment->addComboBox(rect, data->current_parent, + spec.fid); - for (const std::string &item : items) { - e->addItem(unescape_translate(unescape_string( - utf8_to_wide(item))).c_str()); - } + if (spec.fname == m_focused_element) { + Environment->setFocus(e); + } - if (!str_initial_selection.empty()) - e->setSelected(stoi(str_initial_selection)-1); + for (const std::string &item : items) { + e->addItem(unescape_translate(unescape_string( + utf8_to_wide(item))).c_str()); + } - auto style = getDefaultStyleForElement("dropdown", name); + if (!str_initial_selection.empty()) + e->setSelected(stoi(str_initial_selection)-1); - spec.sound = style.get(StyleSpec::Property::SOUND, ""); + auto style = getDefaultStyleForElement("dropdown", name); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); + spec.sound = style.get(StyleSpec::Property::SOUND, ""); - m_fields.push_back(spec); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); - m_dropdowns.emplace_back(spec, std::vector<std::string>()); - std::vector<std::string> &values = m_dropdowns.back().second; - for (const std::string &item : items) { - values.push_back(unescape_string(item)); - } + m_fields.push_back(spec); - return; + m_dropdowns.emplace_back(spec, std::vector<std::string>()); + std::vector<std::string> &values = m_dropdowns.back().second; + for (const std::string &item : items) { + values.push_back(unescape_string(item)); } - errorstream << "Invalid dropdown element(" << parts.size() << "): '" << element - << "'" << std::endl; } void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); - if (parts.size() == 2 || - (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) { - field_close_on_enter[parts[0]] = is_yes(parts[1]); - } + std::vector<std::string> parts; + if (!precheckElement("field_close_on_enter", element, 2, 2, parts)) + return; + + field_close_on_enter[parts[0]] = is_yes(parts[1]); } void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); - - if (parts.size() == 4 || - (parts.size() > 4 && m_formspec_version > FORMSPEC_API_VERSION)) - { - std::vector<std::string> v_pos = split(parts[0],','); - std::vector<std::string> v_geom = split(parts[1],','); - std::string name = parts[2]; - std::string label = parts[3]; - - MY_CHECKPOS("pwdfield",0); - MY_CHECKGEOM("pwdfield",1); + std::vector<std::string> parts; + if (!precheckElement("pwdfield", element, 4, 4, parts)) + return; - v2s32 pos; - v2s32 geom; + std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> v_geom = split(parts[1],','); + std::string name = parts[2]; + std::string label = parts[3]; - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); - geom = getRealCoordinateGeometry(v_geom); - } else { - pos = getElementBasePos(&v_pos); - pos -= padding; + MY_CHECKPOS("pwdfield",0); + MY_CHECKGEOM("pwdfield",1); - geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X); + v2s32 pos; + v2s32 geom; - pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2; - pos.Y -= m_btn_height; - geom.Y = m_btn_height*2; - } + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + } else { + pos = getElementBasePos(&v_pos); + pos -= padding; - core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y); + geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X); - std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label))); + pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2; + pos.Y -= m_btn_height; + geom.Y = m_btn_height*2; + } - FieldSpec spec( - name, - wlabel, - L"", - 258 + m_fields.size(), - 0, - ECI_IBEAM - ); + core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y); - spec.send = true; - gui::IGUIEditBox *e = Environment->addEditBox(0, rect, true, - data->current_parent, spec.fid); + std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label))); - if (spec.fname == m_focused_element) { - Environment->setFocus(e); - } + FieldSpec spec( + name, + wlabel, + L"", + 258 + m_fields.size(), + 0, + ECI_IBEAM + ); - if (label.length() >= 1) { - int font_height = g_fontengine->getTextHeight(); - rect.UpperLeftCorner.Y -= font_height; - rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height; - gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true, - data->current_parent, 0); - } + spec.send = true; + gui::IGUIEditBox *e = Environment->addEditBox(0, rect, true, + data->current_parent, spec.fid); - e->setPasswordBox(true,L'*'); + if (spec.fname == m_focused_element) { + Environment->setFocus(e); + } - auto style = getDefaultStyleForElement("pwdfield", name, "field"); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); - e->setDrawBorder(style.getBool(StyleSpec::BORDER, true)); - e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF))); - e->setOverrideFont(style.getFont()); + if (label.length() >= 1) { + int font_height = g_fontengine->getTextHeight(); + rect.UpperLeftCorner.Y -= font_height; + rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height; + gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true, + data->current_parent, 0); + } - irr::SEvent evt; - evt.EventType = EET_KEY_INPUT_EVENT; - evt.KeyInput.Key = KEY_END; - evt.KeyInput.Char = 0; - evt.KeyInput.Control = false; - evt.KeyInput.Shift = false; - evt.KeyInput.PressedDown = true; - e->OnEvent(evt); + e->setPasswordBox(true,L'*'); - // Note: Before 5.2.0 "parts.size() >= 5" resulted in a - // warning referring to field_close_on_enter[]! + auto style = getDefaultStyleForElement("pwdfield", name, "field"); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); + e->setDrawBorder(style.getBool(StyleSpec::BORDER, true)); + e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF))); + e->setOverrideFont(style.getFont()); + + irr::SEvent evt; + evt.EventType = EET_KEY_INPUT_EVENT; + evt.KeyInput.Key = KEY_END; + evt.KeyInput.Char = 0; + evt.KeyInput.Control = false; + evt.KeyInput.Shift = false; + evt.KeyInput.PressedDown = true; + e->OnEvent(evt); + + // Note: Before 5.2.0 "parts.size() >= 5" resulted in a + // warning referring to field_close_on_enter[]! - m_fields.push_back(spec); - return; - } - errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl; + m_fields.push_back(spec); } void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec, @@ -1710,30 +1667,26 @@ void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& void GUIFormSpecMenu::parseField(parserData* data, const std::string &element, const std::string &type) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + if (!precheckElement(type, element, 3, 5, parts)) + return; if (parts.size() == 3 || parts.size() == 4) { - parseSimpleField(data,parts); + parseSimpleField(data, parts); return; } - if ((parts.size() == 5) || - ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - parseTextArea(data,parts,type); - return; - } - errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl; + // Else: >= 5 arguments in "parts" + parseTextArea(data, parts, type); } void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &element) { - std::vector<std::string> parts = split(element, ';'); + MY_CHECKCLIENT("list"); - if (parts.size() != 4 && m_formspec_version < FORMSPEC_API_VERSION) { - errorstream << "Invalid text element(" << parts.size() << "): '" << element << "'" << std::endl; + std::vector<std::string> parts; + if (!precheckElement("hypertext", element, 4, 4, parts)) return; - } std::vector<std::string> v_pos = split(parts[0], ','); std::vector<std::string> v_geom = split(parts[1], ','); @@ -1784,539 +1737,521 @@ void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &elemen void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + if (!precheckElement("label", element, 2, 2, parts)) + return; - if ((parts.size() == 2) || - ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::vector<std::string> v_pos = split(parts[0],','); - std::string text = parts[1]; + std::vector<std::string> v_pos = split(parts[0],','); + std::string text = parts[1]; - MY_CHECKPOS("label",0); + MY_CHECKPOS("label",0); - if(!data->explicit_size) - warningstream<<"invalid use of label without a size[] element"<<std::endl; + if(!data->explicit_size) + warningstream<<"invalid use of label without a size[] element"<<std::endl; - std::vector<std::string> lines = split(text, '\n'); + std::vector<std::string> lines = split(text, '\n'); - auto style = getDefaultStyleForElement("label", ""); - gui::IGUIFont *font = style.getFont(); - if (!font) - font = m_font; + auto style = getDefaultStyleForElement("label", ""); + gui::IGUIFont *font = style.getFont(); + if (!font) + font = m_font; - for (unsigned int i = 0; i != lines.size(); i++) { - std::wstring wlabel_colors = translate_string( - utf8_to_wide(unescape_string(lines[i]))); - // Without color escapes to get the font dimensions - std::wstring wlabel_plain = unescape_enriched(wlabel_colors); + for (unsigned int i = 0; i != lines.size(); i++) { + std::wstring wlabel_colors = translate_string( + utf8_to_wide(unescape_string(lines[i]))); + // Without color escapes to get the font dimensions + std::wstring wlabel_plain = unescape_enriched(wlabel_colors); - core::rect<s32> rect; + core::rect<s32> rect; - if (data->real_coordinates) { - // Lines are spaced at the distance of 1/2 imgsize. - // This alows lines that line up with the new elements - // easily without sacrificing good line distance. If - // it was one whole imgsize, it would have too much - // spacing. - v2s32 pos = getRealCoordinateBasePos(v_pos); + if (data->real_coordinates) { + // Lines are spaced at the distance of 1/2 imgsize. + // This alows lines that line up with the new elements + // easily without sacrificing good line distance. If + // it was one whole imgsize, it would have too much + // spacing. + v2s32 pos = getRealCoordinateBasePos(v_pos); - // Labels are positioned by their center, not their top. - pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2); + // Labels are positioned by their center, not their top. + pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2); - rect = core::rect<s32>( - pos.X, pos.Y, - pos.X + font->getDimension(wlabel_plain.c_str()).Width, - pos.Y + imgsize.Y); + rect = core::rect<s32>( + pos.X, pos.Y, + pos.X + font->getDimension(wlabel_plain.c_str()).Width, + pos.Y + imgsize.Y); - } else { - // Lines are spaced at the nominal distance of - // 2/5 inventory slot, even if the font doesn't - // quite match that. This provides consistent - // form layout, at the expense of sometimes - // having sub-optimal spacing for the font. - // We multiply by 2 and then divide by 5, rather - // than multiply by 0.4, to get exact results - // in the integer cases: 0.4 is not exactly - // representable in binary floating point. - - v2s32 pos = getElementBasePos(nullptr); - pos.X += stof(v_pos[0]) * spacing.X; - pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y; - - pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0; - - rect = core::rect<s32>( - pos.X, pos.Y - m_btn_height, - pos.X + font->getDimension(wlabel_plain.c_str()).Width, - pos.Y + m_btn_height); - } + } else { + // Lines are spaced at the nominal distance of + // 2/5 inventory slot, even if the font doesn't + // quite match that. This provides consistent + // form layout, at the expense of sometimes + // having sub-optimal spacing for the font. + // We multiply by 2 and then divide by 5, rather + // than multiply by 0.4, to get exact results + // in the integer cases: 0.4 is not exactly + // representable in binary floating point. + + v2s32 pos = getElementBasePos(nullptr); + pos.X += stof(v_pos[0]) * spacing.X; + pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y; + + pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0; - FieldSpec spec( - "", - wlabel_colors, - L"", - 258 + m_fields.size(), - 4 - ); - gui::IGUIStaticText *e = gui::StaticText::add(Environment, - spec.flabel.c_str(), rect, false, false, data->current_parent, - spec.fid); - e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER); + rect = core::rect<s32>( + pos.X, pos.Y - m_btn_height, + pos.X + font->getDimension(wlabel_plain.c_str()).Width, + pos.Y + m_btn_height); + } - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); - e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF))); - e->setOverrideFont(font); + FieldSpec spec( + "", + wlabel_colors, + L"", + 258 + m_fields.size(), + 4 + ); + gui::IGUIStaticText *e = gui::StaticText::add(Environment, + spec.flabel.c_str(), rect, false, false, data->current_parent, + spec.fid); + e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER); - m_fields.push_back(spec); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); + e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF))); + e->setOverrideFont(font); - // labels should let events through - e->grab(); - m_clickthrough_elements.push_back(e); - } + m_fields.push_back(spec); - return; + // labels should let events through + e->grab(); + m_clickthrough_elements.push_back(e); } - errorstream << "Invalid label element(" << parts.size() << "): '" << element - << "'" << std::endl; } void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + if (!precheckElement("vertlabel", element, 2, 2, parts)) + return; - if ((parts.size() == 2) || - ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::vector<std::string> v_pos = split(parts[0],','); - std::wstring text = unescape_translate( - unescape_string(utf8_to_wide(parts[1]))); + std::vector<std::string> v_pos = split(parts[0],','); + std::wstring text = unescape_translate( + unescape_string(utf8_to_wide(parts[1]))); - MY_CHECKPOS("vertlabel",1); + MY_CHECKPOS("vertlabel",1); - auto style = getDefaultStyleForElement("vertlabel", "", "label"); - gui::IGUIFont *font = style.getFont(); - if (!font) - font = m_font; + auto style = getDefaultStyleForElement("vertlabel", "", "label"); + gui::IGUIFont *font = style.getFont(); + if (!font) + font = m_font; - v2s32 pos; - core::rect<s32> rect; + v2s32 pos; + core::rect<s32> rect; - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); - // Vertlabels are positioned by center, not left. - pos.X -= imgsize.X / 2; + // Vertlabels are positioned by center, not left. + pos.X -= imgsize.X / 2; - // We use text.length + 1 because without it, the rect - // isn't quite tall enough and cuts off the text. - rect = core::rect<s32>(pos.X, pos.Y, - pos.X + imgsize.X, - pos.Y + font_line_height(font) * - (text.length() + 1)); + // We use text.length + 1 because without it, the rect + // isn't quite tall enough and cuts off the text. + rect = core::rect<s32>(pos.X, pos.Y, + pos.X + imgsize.X, + pos.Y + font_line_height(font) * + (text.length() + 1)); - } else { - pos = getElementBasePos(&v_pos); + } else { + pos = getElementBasePos(&v_pos); - // As above, the length must be one longer. The width of - // the rect (15 pixels) seems rather arbitrary, but - // changing it might break something. - rect = core::rect<s32>( - pos.X, pos.Y+((imgsize.Y/2) - m_btn_height), - pos.X+15, pos.Y + - font_line_height(font) * - (text.length() + 1) + - ((imgsize.Y/2) - m_btn_height)); - } + // As above, the length must be one longer. The width of + // the rect (15 pixels) seems rather arbitrary, but + // changing it might break something. + rect = core::rect<s32>( + pos.X, pos.Y+((imgsize.Y/2) - m_btn_height), + pos.X+15, pos.Y + + font_line_height(font) * + (text.length() + 1) + + ((imgsize.Y/2) - m_btn_height)); + } - if(!data->explicit_size) - warningstream<<"invalid use of label without a size[] element"<<std::endl; + if(!data->explicit_size) + warningstream<<"invalid use of label without a size[] element"<<std::endl; - std::wstring label; + std::wstring label; - for (wchar_t i : text) { - label += i; - label += L"\n"; - } + for (wchar_t i : text) { + label += i; + label += L"\n"; + } - FieldSpec spec( - "", - label, - L"", - 258 + m_fields.size() - ); - gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(), - rect, false, false, data->current_parent, spec.fid); - e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER); + FieldSpec spec( + "", + label, + L"", + 258 + m_fields.size() + ); + gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(), + rect, false, false, data->current_parent, spec.fid); + e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); - e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF))); - e->setOverrideFont(font); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); + e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF))); + e->setOverrideFont(font); - m_fields.push_back(spec); + m_fields.push_back(spec); - // vertlabels should let events through - e->grab(); - m_clickthrough_elements.push_back(e); - return; - } - errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl; + // vertlabels should let events through + e->grab(); + m_clickthrough_elements.push_back(e); } void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element, const std::string &type) { - std::vector<std::string> parts = split(element,';'); - - if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) || - ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::vector<std::string> v_pos = split(parts[0],','); - std::vector<std::string> v_geom = split(parts[1],','); - std::string image_name = parts[2]; - std::string name = parts[3]; - std::string label = parts[4]; + std::vector<std::string> parts; + if (!precheckElement("image_button", element, 5, 8, parts)) + return; - MY_CHECKPOS("imagebutton",0); - MY_CHECKGEOM("imagebutton",1); + if (parts.size() == 6) { + // Invalid argument count. + errorstream << "Invalid image_button element(" << parts.size() << "): '" << element << "'" << std::endl; + return; + } - std::string pressed_image_name; + std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> v_geom = split(parts[1],','); + std::string image_name = parts[2]; + std::string name = parts[3]; + std::string label = parts[4]; - if (parts.size() >= 8) { - pressed_image_name = parts[7]; - } + MY_CHECKPOS("image_button",0); + MY_CHECKGEOM("image_button",1); - v2s32 pos; - v2s32 geom; + std::string pressed_image_name; - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); - geom = getRealCoordinateGeometry(v_geom); - } else { - pos = getElementBasePos(&v_pos); - geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X); - geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y); - } + if (parts.size() >= 8) { + pressed_image_name = parts[7]; + } - core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, - pos.Y+geom.Y); + v2s32 pos; + v2s32 geom; - if (!data->explicit_size) - warningstream<<"invalid use of image_button without a size[] element"<<std::endl; + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + } else { + pos = getElementBasePos(&v_pos); + geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X); + geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y); + } - image_name = unescape_string(image_name); - pressed_image_name = unescape_string(pressed_image_name); + core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, + pos.Y+geom.Y); - std::wstring wlabel = utf8_to_wide(unescape_string(label)); + if (!data->explicit_size) + warningstream<<"invalid use of image_button without a size[] element"<<std::endl; - FieldSpec spec( - name, - wlabel, - utf8_to_wide(image_name), - 258 + m_fields.size() - ); - spec.ftype = f_Button; - if (type == "image_button_exit") - spec.is_exit = true; + image_name = unescape_string(image_name); + pressed_image_name = unescape_string(pressed_image_name); - GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, m_tsrc, - data->current_parent, spec.fid, spec.flabel.c_str()); + std::wstring wlabel = utf8_to_wide(unescape_string(label)); - if (spec.fname == m_focused_element) { - Environment->setFocus(e); - } + FieldSpec spec( + name, + wlabel, + utf8_to_wide(image_name), + 258 + m_fields.size() + ); + spec.ftype = f_Button; + if (type == "image_button_exit") + spec.is_exit = true; - auto style = getStyleForElement("image_button", spec.fname); + GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, m_tsrc, + data->current_parent, spec.fid, spec.flabel.c_str()); - spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, ""); + if (spec.fname == m_focused_element) { + Environment->setFocus(e); + } - // Override style properties with values specified directly in the element - if (!image_name.empty()) - style[StyleSpec::STATE_DEFAULT].set(StyleSpec::FGIMG, image_name); + auto style = getStyleForElement("image_button", spec.fname); - if (!pressed_image_name.empty()) - style[StyleSpec::STATE_PRESSED].set(StyleSpec::FGIMG, pressed_image_name); + spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, ""); - if (parts.size() >= 7) { - style[StyleSpec::STATE_DEFAULT].set(StyleSpec::NOCLIP, parts[5]); - style[StyleSpec::STATE_DEFAULT].set(StyleSpec::BORDER, parts[6]); - } + // Override style properties with values specified directly in the element + if (!image_name.empty()) + style[StyleSpec::STATE_DEFAULT].set(StyleSpec::FGIMG, image_name); - e->setStyles(style); - e->setScaleImage(true); + if (!pressed_image_name.empty()) + style[StyleSpec::STATE_PRESSED].set(StyleSpec::FGIMG, pressed_image_name); - m_fields.push_back(spec); - return; + if (parts.size() >= 7) { + style[StyleSpec::STATE_DEFAULT].set(StyleSpec::NOCLIP, parts[5]); + style[StyleSpec::STATE_DEFAULT].set(StyleSpec::BORDER, parts[6]); } - errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl; + e->setStyles(style); + e->setScaleImage(true); + + m_fields.push_back(spec); } void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element, ';'); - - if (((parts.size() == 4) || (parts.size() == 6)) || (parts.size() == 7 && - data->real_coordinates) || ((parts.size() > 6) && - (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> parts; + if (!precheckElement("tabheader", element, 4, 7, parts)) + return; - // If we're using real coordinates, add an extra field for height. - // Width is not here because tabs are the width of the text, and - // there's no reason to change that. - unsigned int i = 0; - std::vector<std::string> v_geom = {"1", "1"}; // Dummy width and height - bool auto_width = true; - if (parts.size() == 7) { - i++; + // Length 7: Additional "height" parameter after "pos". Only valid with real_coordinates. + // Note: New arguments for the "height" syntax cannot be added without breaking older clients. + if (parts.size() == 5 || (parts.size() == 7 && !data->real_coordinates)) { + errorstream << "Invalid tabheader element(" << parts.size() << "): '" + << element << "'" << std::endl; + return; + } - v_geom = split(parts[1], ','); - if (v_geom.size() == 1) - v_geom.insert(v_geom.begin(), "1"); // Dummy value - else - auto_width = false; - } + std::vector<std::string> v_pos = split(parts[0],','); - std::string name = parts[i+1]; - std::vector<std::string> buttons = split(parts[i+2], ','); - std::string str_index = parts[i+3]; - bool show_background = true; - bool show_border = true; - int tab_index = stoi(str_index) - 1; + // If we're using real coordinates, add an extra field for height. + // Width is not here because tabs are the width of the text, and + // there's no reason to change that. + unsigned int i = 0; + std::vector<std::string> v_geom = {"1", "1"}; // Dummy width and height + bool auto_width = true; + if (parts.size() == 7) { + i++; + + v_geom = split(parts[1], ','); + if (v_geom.size() == 1) + v_geom.insert(v_geom.begin(), "1"); // Dummy value + else + auto_width = false; + } - MY_CHECKPOS("tabheader", 0); + std::string name = parts[i+1]; + std::vector<std::string> buttons = split(parts[i+2], ','); + std::string str_index = parts[i+3]; + bool show_background = true; + bool show_border = true; + int tab_index = stoi(str_index) - 1; - if (parts.size() == 6 + i) { - if (parts[4+i] == "true") - show_background = false; - if (parts[5+i] == "false") - show_border = false; - } + MY_CHECKPOS("tabheader", 0); - FieldSpec spec( - name, - L"", - L"", - 258 + m_fields.size() - ); + if (parts.size() == 6 + i) { + if (parts[4+i] == "true") + show_background = false; + if (parts[5+i] == "false") + show_border = false; + } - spec.ftype = f_TabHeader; + FieldSpec spec( + name, + L"", + L"", + 258 + m_fields.size() + ); - v2s32 pos; - v2s32 geom; + spec.ftype = f_TabHeader; - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); + v2s32 pos; + v2s32 geom; - geom = getRealCoordinateGeometry(v_geom); - // Set default height - if (parts.size() <= 6) - geom.Y = m_btn_height * 2; - pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top. - if (auto_width) - geom.X = DesiredRect.getWidth(); // Set automatic width - - MY_CHECKGEOM("tabheader", 1); - } else { - v2f32 pos_f = pos_offset * spacing; - pos_f.X += stof(v_pos[0]) * spacing.X; - pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2; - pos = v2s32(pos_f.X, pos_f.Y); + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + // Set default height + if (parts.size() <= 6) geom.Y = m_btn_height * 2; - geom.X = DesiredRect.getWidth(); - } + pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top. + if (auto_width) + geom.X = DesiredRect.getWidth(); // Set automatic width - core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, - pos.Y+geom.Y); + MY_CHECKGEOM("tabheader", 1); + } else { + v2f32 pos_f = pos_offset * spacing; + pos_f.X += stof(v_pos[0]) * spacing.X; + pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2; + pos = v2s32(pos_f.X, pos_f.Y); - gui::IGUITabControl *e = Environment->addTabControl(rect, - data->current_parent, show_background, show_border, spec.fid); - e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT, - irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT); - e->setTabHeight(geom.Y); + geom.Y = m_btn_height * 2; + geom.X = DesiredRect.getWidth(); + } - auto style = getDefaultStyleForElement("tabheader", name); + core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, + pos.Y+geom.Y); - spec.sound = style.get(StyleSpec::Property::SOUND, ""); + gui::IGUITabControl *e = Environment->addTabControl(rect, + data->current_parent, show_background, show_border, spec.fid); + e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT, + irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT); + e->setTabHeight(geom.Y); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true)); + auto style = getDefaultStyleForElement("tabheader", name); - for (const std::string &button : buttons) { - auto tab = e->addTab(unescape_translate(unescape_string( - utf8_to_wide(button))).c_str(), -1); - if (style.isNotDefault(StyleSpec::BGCOLOR)) - tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR)); + spec.sound = style.get(StyleSpec::Property::SOUND, ""); - tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF))); - } + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true)); - if ((tab_index >= 0) && - (buttons.size() < INT_MAX) && - (tab_index < (int) buttons.size())) - e->setActiveTab(tab_index); + for (const std::string &button : buttons) { + auto tab = e->addTab(unescape_translate(unescape_string( + utf8_to_wide(button))).c_str(), -1); + if (style.isNotDefault(StyleSpec::BGCOLOR)) + tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR)); - m_fields.push_back(spec); - return; + tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF))); } - errorstream << "Invalid TabHeader element(" << parts.size() << "): '" - << element << "'" << std::endl; + + if ((tab_index >= 0) && + (buttons.size() < INT_MAX) && + (tab_index < (int) buttons.size())) + e->setActiveTab(tab_index); + + m_fields.push_back(spec); } void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element) { - if (m_client == 0) { - warningstream << "invalid use of item_image_button with m_client==0" - << std::endl; - return; - } - - std::vector<std::string> parts = split(element,';'); + MY_CHECKCLIENT("item_image_button"); - if ((parts.size() == 5) || - ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::vector<std::string> v_pos = split(parts[0],','); - std::vector<std::string> v_geom = split(parts[1],','); - std::string item_name = parts[2]; - std::string name = parts[3]; - std::string label = parts[4]; + std::vector<std::string> parts; + if (!precheckElement("item_image_button", element, 5, 5, parts)) + return; - label = unescape_string(label); - item_name = unescape_string(item_name); + std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> v_geom = split(parts[1],','); + std::string item_name = parts[2]; + std::string name = parts[3]; + std::string label = parts[4]; - MY_CHECKPOS("itemimagebutton",0); - MY_CHECKGEOM("itemimagebutton",1); + label = unescape_string(label); + item_name = unescape_string(item_name); - v2s32 pos; - v2s32 geom; + MY_CHECKPOS("item_image_button",0); + MY_CHECKGEOM("item_image_button",1); - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); - geom = getRealCoordinateGeometry(v_geom); - } else { - pos = getElementBasePos(&v_pos); - geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X); - geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y); - } + v2s32 pos; + v2s32 geom; - core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y); + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + } else { + pos = getElementBasePos(&v_pos); + geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X); + geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y); + } - if(!data->explicit_size) - warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl; + core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y); - IItemDefManager *idef = m_client->idef(); - ItemStack item; - item.deSerialize(item_name, idef); + if(!data->explicit_size) + warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl; - m_tooltips[name] = - TooltipSpec(utf8_to_wide(item.getDefinition(idef).description), - m_default_tooltip_bgcolor, - m_default_tooltip_color); + IItemDefManager *idef = m_client->idef(); + ItemStack item; + item.deSerialize(item_name, idef); - // the spec for the button - FieldSpec spec_btn( - name, - utf8_to_wide(label), - utf8_to_wide(item_name), - 258 + m_fields.size(), - 2 - ); + m_tooltips[name] = + TooltipSpec(utf8_to_wide(item.getDefinition(idef).description), + m_default_tooltip_bgcolor, + m_default_tooltip_color); - GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment, - rect, m_tsrc, data->current_parent, spec_btn.fid, spec_btn.flabel.c_str(), - item_name, m_client); + // the spec for the button + FieldSpec spec_btn( + name, + utf8_to_wide(label), + utf8_to_wide(item_name), + 258 + m_fields.size(), + 2 + ); - auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button"); + GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment, + rect, m_tsrc, data->current_parent, spec_btn.fid, spec_btn.flabel.c_str(), + item_name, m_client); - spec_btn.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, ""); + auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button"); - e_btn->setStyles(style); + spec_btn.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, ""); - if (spec_btn.fname == m_focused_element) { - Environment->setFocus(e_btn); - } + e_btn->setStyles(style); - spec_btn.ftype = f_Button; - rect += data->basepos-padding; - spec_btn.rect = rect; - m_fields.push_back(spec_btn); - return; + if (spec_btn.fname == m_focused_element) { + Environment->setFocus(e_btn); } - errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl; + + spec_btn.ftype = f_Button; + rect += data->basepos-padding; + spec_btn.rect = rect; + m_fields.push_back(spec_btn); } void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element, ';'); + std::vector<std::string> parts; + if (!precheckElement("box", element, 3, 3, parts)) + return; - if ((parts.size() == 3) || - ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - std::vector<std::string> v_pos = split(parts[0], ','); - std::vector<std::string> v_geom = split(parts[1], ','); + std::vector<std::string> v_pos = split(parts[0], ','); + std::vector<std::string> v_geom = split(parts[1], ','); - MY_CHECKPOS("box", 0); - MY_CHECKGEOM("box", 1); + MY_CHECKPOS("box", 0); + MY_CHECKGEOM("box", 1); - v2s32 pos; - v2s32 geom; + v2s32 pos; + v2s32 geom; - if (data->real_coordinates) { - pos = getRealCoordinateBasePos(v_pos); - geom = getRealCoordinateGeometry(v_geom); - } else { - pos = getElementBasePos(&v_pos); - geom.X = stof(v_geom[0]) * spacing.X; - geom.Y = stof(v_geom[1]) * spacing.Y; - } + if (data->real_coordinates) { + pos = getRealCoordinateBasePos(v_pos); + geom = getRealCoordinateGeometry(v_geom); + } else { + pos = getElementBasePos(&v_pos); + geom.X = stof(v_geom[0]) * spacing.X; + geom.Y = stof(v_geom[1]) * spacing.Y; + } - FieldSpec spec( - "", - L"", - L"", - 258 + m_fields.size(), - -2 - ); - spec.ftype = f_Box; + FieldSpec spec( + "", + L"", + L"", + 258 + m_fields.size(), + -2 + ); + spec.ftype = f_Box; - auto style = getDefaultStyleForElement("box", spec.fname); + auto style = getDefaultStyleForElement("box", spec.fname); - video::SColor tmp_color; - std::array<video::SColor, 4> colors; - std::array<video::SColor, 4> bordercolors = {0x0, 0x0, 0x0, 0x0}; - std::array<s32, 4> borderwidths = {0, 0, 0, 0}; + video::SColor tmp_color; + std::array<video::SColor, 4> colors; + std::array<video::SColor, 4> bordercolors = {0x0, 0x0, 0x0, 0x0}; + std::array<s32, 4> borderwidths = {0, 0, 0, 0}; - if (parseColorString(parts[2], tmp_color, true, 0x8C)) { - colors = {tmp_color, tmp_color, tmp_color, tmp_color}; - } else { - colors = style.getColorArray(StyleSpec::COLORS, {0x0, 0x0, 0x0, 0x0}); - bordercolors = style.getColorArray(StyleSpec::BORDERCOLORS, - {0x0, 0x0, 0x0, 0x0}); - borderwidths = style.getIntArray(StyleSpec::BORDERWIDTHS, {0, 0, 0, 0}); - } + if (parseColorString(parts[2], tmp_color, true, 0x8C)) { + colors = {tmp_color, tmp_color, tmp_color, tmp_color}; + } else { + colors = style.getColorArray(StyleSpec::COLORS, {0x0, 0x0, 0x0, 0x0}); + bordercolors = style.getColorArray(StyleSpec::BORDERCOLORS, + {0x0, 0x0, 0x0, 0x0}); + borderwidths = style.getIntArray(StyleSpec::BORDERWIDTHS, {0, 0, 0, 0}); + } - core::rect<s32> rect(pos, pos + geom); + core::rect<s32> rect(pos, pos + geom); - GUIBox *e = new GUIBox(Environment, data->current_parent, spec.fid, rect, - colors, bordercolors, borderwidths); - e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3)); - e->drop(); + GUIBox *e = new GUIBox(Environment, data->current_parent, spec.fid, rect, + colors, bordercolors, borderwidths); + e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3)); + e->drop(); - m_fields.push_back(spec); - return; - } - errorstream << "Invalid Box element(" << parts.size() << "): '" << element - << "'" << std::endl; + m_fields.push_back(spec); } void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + if (!precheckElement("bgcolor", element, 1, 3, parts)) + return; + const u32 parameter_count = parts.size(); - if ((parameter_count > 2 && m_formspec_version < 3) || - (parameter_count > 3 && m_formspec_version <= FORMSPEC_API_VERSION)) { + if (parameter_count > 2 && m_formspec_version < 3) { errorstream << "Invalid bgcolor element(" << parameter_count << "): '" << element << "'" << std::endl; return; @@ -2347,49 +2282,51 @@ void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string & void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts; + // Legacy Note: If clients older than 5.5.0-dev are supplied with additional arguments, + // the tooltip colors will be ignored. + if (!precheckElement("listcolors", element, 2, 5, parts)) + return; - if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) || - ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION))) - { - parseColorString(parts[0], data->inventorylist_options.slotbg_n, false); - parseColorString(parts[1], data->inventorylist_options.slotbg_h, false); + if (parts.size() == 4) { + // Invalid argument combination + errorstream << "Invalid listcolors element(" << parts.size() << "): '" + << element << "'" << std::endl; + return; + } - if (parts.size() >= 3) { - if (parseColorString(parts[2], data->inventorylist_options.slotbordercolor, - false)) { - data->inventorylist_options.slotborder = true; - } - } - if (parts.size() == 5) { - video::SColor tmp_color; + parseColorString(parts[0], data->inventorylist_options.slotbg_n, false); + parseColorString(parts[1], data->inventorylist_options.slotbg_h, false); - if (parseColorString(parts[3], tmp_color, false)) - m_default_tooltip_bgcolor = tmp_color; - if (parseColorString(parts[4], tmp_color, false)) - m_default_tooltip_color = tmp_color; + if (parts.size() >= 3) { + if (parseColorString(parts[2], data->inventorylist_options.slotbordercolor, + false)) { + data->inventorylist_options.slotborder = true; } + } + if (parts.size() >= 5) { + video::SColor tmp_color; - // update all already parsed inventorylists - for (GUIInventoryList *e : m_inventorylists) { - e->setSlotBGColors(data->inventorylist_options.slotbg_n, - data->inventorylist_options.slotbg_h); - e->setSlotBorders(data->inventorylist_options.slotborder, - data->inventorylist_options.slotbordercolor); - } - return; + if (parseColorString(parts[3], tmp_color, false)) + m_default_tooltip_bgcolor = tmp_color; + if (parseColorString(parts[4], tmp_color, false)) + m_default_tooltip_color = tmp_color; + } + + // update all already parsed inventorylists + for (GUIInventoryList *e : m_inventorylists) { + e->setSlotBGColors(data->inventorylist_options.slotbg_n, + data->inventorylist_options.slotbg_h); + e->setSlotBorders(data->inventorylist_options.slotborder, + data->inventorylist_options.slotbordercolor); } - errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl; } void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); - if (parts.size() < 2) { - errorstream << "Invalid tooltip element(" << parts.size() << "): '" - << element << "'" << std::endl; + std::vector<std::string> parts; + if (!precheckElement("tooltip", element, 2, 5, parts)) return; - } // Get mode and check size bool rect_mode = parts[0].find(',') != std::string::npos; @@ -2529,11 +2466,16 @@ bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &e void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element) { - std::vector<std::string> parts = split(element, ','); + std::vector<std::string> parts = split(element, ';'); - if (parts.size() == 2) { - data->offset.X = stof(parts[0]); - data->offset.Y = stof(parts[1]); + if (parts.size() == 1 || + (parts.size() > 1 && m_formspec_version > FORMSPEC_API_VERSION)) { + std::vector<std::string> v_geom = split(parts[0], ','); + + MY_CHECKGEOM("position", 0); + + data->offset.X = stof(v_geom[0]); + data->offset.Y = stof(v_geom[1]); return; } @@ -2563,11 +2505,16 @@ bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &ele void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element) { - std::vector<std::string> parts = split(element, ','); + std::vector<std::string> parts = split(element, ';'); - if (parts.size() == 2) { - data->anchor.X = stof(parts[0]); - data->anchor.Y = stof(parts[1]); + if (parts.size() == 1 || + (parts.size() > 1 && m_formspec_version > FORMSPEC_API_VERSION)) { + std::vector<std::string> v_geom = split(parts[0], ','); + + MY_CHECKGEOM("anchor", 0); + + data->anchor.X = stof(v_geom[0]); + data->anchor.Y = stof(v_geom[1]); return; } @@ -2575,6 +2522,46 @@ void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element) << "'" << std::endl; } +bool GUIFormSpecMenu::parsePaddingDirect(parserData *data, const std::string &element) +{ + if (element.empty()) + return false; + + std::vector<std::string> parts = split(element, '['); + + if (parts.size() != 2) + return false; + + std::string type = trim(parts[0]); + std::string description = trim(parts[1]); + + if (type != "padding") + return false; + + parsePadding(data, description); + + return true; +} + +void GUIFormSpecMenu::parsePadding(parserData *data, const std::string &element) +{ + std::vector<std::string> parts = split(element, ';'); + + if (parts.size() == 1 || + (parts.size() > 1 && m_formspec_version > FORMSPEC_API_VERSION)) { + std::vector<std::string> v_geom = split(parts[0], ','); + + MY_CHECKGEOM("padding", 0); + + data->padding.X = stof(v_geom[0]); + data->padding.Y = stof(v_geom[1]); + return; + } + + errorstream << "Invalid padding element (" << parts.size() << "): '" << element + << "'" << std::endl; +} + bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type) { std::vector<std::string> parts = split(element, ';'); @@ -2713,35 +2700,25 @@ bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, b void GUIFormSpecMenu::parseSetFocus(const std::string &element) { - std::vector<std::string> parts = split(element, ';'); - - if (parts.size() <= 2 || - (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) - { - if (m_is_form_regenerated) - return; // Never focus on resizing - - bool force_focus = parts.size() >= 2 && is_yes(parts[1]); - if (force_focus || m_text_dst->m_formname != m_last_formname) - setFocus(parts[0]); - + std::vector<std::string> parts; + if (!precheckElement("set_focus", element, 1, 2, parts)) return; - } - errorstream << "Invalid set_focus element (" << parts.size() << "): '" << element - << "'" << std::endl; + if (m_is_form_regenerated) + return; // Never focus on resizing + + bool force_focus = parts.size() >= 2 && is_yes(parts[1]); + if (force_focus || m_text_dst->m_formname != m_last_formname) + setFocus(parts[0]); } void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element) { - std::vector<std::string> parts = split(element, ';'); + MY_CHECKCLIENT("model"); - if (parts.size() < 5 || (parts.size() > 10 && - m_formspec_version <= FORMSPEC_API_VERSION)) { - errorstream << "Invalid model element (" << parts.size() << "): '" << element - << "'" << std::endl; + std::vector<std::string> parts; + if (!precheckElement("model", element, 5, 10, parts)) return; - } // Avoid length checks by resizing if (parts.size() < 10) @@ -3078,8 +3055,6 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) checkbox_it.second->drop(); for (auto &scrollbar_it : m_scrollbars) scrollbar_it.second->drop(); - for (auto &background_it : m_backgrounds) - background_it->drop(); for (auto &tooltip_rect_it : m_tooltip_rects) tooltip_rect_it.first->drop(); for (auto &clickthrough_it : m_clickthrough_elements) @@ -3091,6 +3066,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) mydata.screensize = screensize; mydata.offset = v2f32(0.5f, 0.5f); mydata.anchor = v2f32(0.5f, 0.5f); + mydata.padding = v2f32(0.05f, 0.05f); mydata.simple_field_count = 0; // Base position of contents of form @@ -3100,7 +3076,6 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) mydata.current_parent = this; m_inventorylists.clear(); - m_backgrounds.clear(); m_tables.clear(); m_checkboxes.clear(); m_scrollbars.clear(); @@ -3193,7 +3168,14 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) } } - /* "no_prepend" element is always after "position" (or "size" element) if it used */ + /* "padding" element is always after "anchor" and previous if it is used */ + for (; i < elements.size(); i++) { + if (!parsePaddingDirect(&mydata, elements[i])) { + break; + } + } + + /* "no_prepend" element is always after "padding" and previous if it used */ bool enable_prepends = true; for (; i < elements.size(); i++) { if (elements[i].empty()) @@ -3258,11 +3240,9 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) double fitx_imgsize; double fity_imgsize; - // Pad the screensize with 5% of the screensize on all sides to ensure - // that even the largest formspecs don't touch the screen borders. v2f padded_screensize( - mydata.screensize.X * 0.9f, - mydata.screensize.Y * 0.9f + mydata.screensize.X * (1.0f - mydata.padding.X * 2.0f), + mydata.screensize.Y * (1.0f - mydata.padding.Y * 2.0f) ); if (mydata.real_coordinates) { @@ -3278,13 +3258,15 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) ((15.0 / 13.0) * (0.85 + mydata.invsize.Y)); } -#ifdef __ANDROID__ + s32 min_screen_dim = std::min(padded_screensize.X, padded_screensize.Y); + +#ifdef HAVE_TOUCHSCREENGUI // In Android, the preferred imgsize should be larger to accommodate the // smaller screensize. - double prefer_imgsize = padded_screensize.Y / 10 * gui_scaling; + double prefer_imgsize = min_screen_dim / 10 * gui_scaling; #else // Desktop computers have more space, so try to fit 15 coordinates. - double prefer_imgsize = padded_screensize.Y / 15 * gui_scaling; + double prefer_imgsize = min_screen_dim / 15 * gui_scaling; #endif // Try to use the preferred imgsize, but if that's bigger than the maximum // size, use the maximum size. @@ -3347,6 +3329,15 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) gui::IGUIFont *old_font = skin->getFont(); skin->setFont(m_font); + // Add a new element that will hold all the background elements as its children. + // Because it is the first added element, all backgrounds will be behind all + // the other elements. + // (We use an arbitrarily big rect. The actual size is determined later by + // clipping to `this`.) + core::rect<s32> background_parent_rect(0, 0, 100000, 100000); + mydata.background_parent.reset(new gui::IGUIElement(EGUIET_ELEMENT, Environment, + this, -1, background_parent_rect)); + pos_offset = v2f32(); // used for formspec versions < 3 @@ -3602,15 +3593,6 @@ void GUIFormSpecMenu::drawMenu() } } - /* - Draw backgrounds - */ - for (gui::IGUIElement *e : m_backgrounds) { - e->setVisible(true); - e->draw(); - e->setVisible(false); - } - // Some elements are only visible while being drawn for (gui::IGUIElement *e : m_clickthrough_elements) e->setVisible(true); @@ -3741,7 +3723,7 @@ void GUIFormSpecMenu::showTooltip(const std::wstring &text, v2u32 screenSize = Environment->getVideoDriver()->getScreenSize(); int tooltip_offset_x = m_btn_height; int tooltip_offset_y = m_btn_height; -#ifdef __ANDROID__ +#ifdef HAVE_TOUCHSCREENGUI tooltip_offset_x *= 3; tooltip_offset_y = 0; if (m_pointer.X > (s32)screenSize.X / 2) diff --git a/src/gui/guiFormSpecMenu.h b/src/gui/guiFormSpecMenu.h index 926de66d5..3fedb3b78 100644 --- a/src/gui/guiFormSpecMenu.h +++ b/src/gui/guiFormSpecMenu.h @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include <unordered_set> #include "irrlichttypes_extrabloated.h" +#include "irr_ptr.h" #include "inventorymanager.h" #include "modalMenu.h" #include "guiInventoryList.h" @@ -229,7 +230,7 @@ public: return m_selected_item; } - const u16 getSelectedAmount() const + u16 getSelectedAmount() const { return m_selected_amount; } @@ -279,6 +280,8 @@ protected: v2s32 getElementBasePos(const std::vector<std::string> *v_pos); v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos); v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom); + bool precheckElement(const std::string &name, const std::string &element, + size_t args_min, size_t args_max, std::vector<std::string> &parts); std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_type; std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_name; @@ -311,7 +314,6 @@ protected: std::vector<GUIInventoryList *> m_inventorylists; std::vector<ListRingSpec> m_inventory_rings; - std::vector<gui::IGUIElement *> m_backgrounds; std::unordered_map<std::string, bool> field_close_on_enter; std::unordered_map<std::string, bool> m_dropdown_index_event; std::vector<FieldSpec> m_fields; @@ -366,12 +368,14 @@ private: v2s32 size; v2f32 offset; v2f32 anchor; + v2f32 padding; core::rect<s32> rect; v2s32 basepos; v2u32 screensize; GUITable::TableOptions table_options; GUITable::TableColumns table_columns; gui::IGUIElement *current_parent = nullptr; + irr_ptr<gui::IGUIElement> background_parent; GUIInventoryList::Options inventorylist_options; @@ -447,6 +451,8 @@ private: void parsePosition(parserData *data, const std::string &element); bool parseAnchorDirect(parserData *data, const std::string &element); void parseAnchor(parserData *data, const std::string &element); + bool parsePaddingDirect(parserData *data, const std::string &element); + void parsePadding(parserData *data, const std::string &element); bool parseStyle(parserData *data, const std::string &element, bool style_type); void parseSetFocus(const std::string &element); void parseModel(parserData *data, const std::string &element); diff --git a/src/gui/guiHyperText.cpp b/src/gui/guiHyperText.cpp index ccfdcb81d..40450ce5f 100644 --- a/src/gui/guiHyperText.cpp +++ b/src/gui/guiHyperText.cpp @@ -17,31 +17,26 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "IGUIEnvironment.h" -#include "IGUIElement.h" +#include "guiHyperText.h" #include "guiScrollBar.h" -#include "IGUIFont.h" -#include <vector> -#include <list> -#include <unordered_map> -using namespace irr::gui; #include "client/fontengine.h" -#include <SColor.h> #include "client/tile.h" #include "IVideoDriver.h" #include "client/client.h" #include "client/renderingengine.h" #include "hud.h" -#include "guiHyperText.h" #include "util/string.h" +#include "irrlicht_changes/CGUITTFont.h" -bool check_color(const std::string &str) +using namespace irr::gui; + +static bool check_color(const std::string &str) { irr::video::SColor color; return parseColorString(str, color, false); } -bool check_integer(const std::string &str) +static bool check_integer(const std::string &str) { if (str.empty()) return false; @@ -616,12 +611,10 @@ TextDrawer::TextDrawer(const wchar_t *text, Client *client, if (e.font) { e.dim.Width = e.font->getDimension(e.text.c_str()).Width; e.dim.Height = e.font->getDimension(L"Yy").Height; -#if USE_FREETYPE if (e.font->getType() == irr::gui::EGFT_CUSTOM) { - e.baseline = e.dim.Height - 1 - - ((irr::gui::CGUITTFont *)e.font)->getAscender() / 64; + CGUITTFont *tmp = static_cast<CGUITTFont*>(e.font); + e.baseline = e.dim.Height - 1 - tmp->getAscender() / 64; } -#endif } else { e.dim = {0, 0}; } diff --git a/src/gui/guiHyperText.h b/src/gui/guiHyperText.h index 5b936262e..04c664df5 100644 --- a/src/gui/guiHyperText.h +++ b/src/gui/guiHyperText.h @@ -19,16 +19,17 @@ with this program; if not, write to the Free Software Foundation, Inc., #pragma once -#include "config.h" // for USE_FREETYPE +#include <vector> +#include <list> +#include <unordered_map> +#include <string> +#include "irrlichttypes_extrabloated.h" using namespace irr; class ISimpleTextureSource; class Client; - -#if USE_FREETYPE -#include "irrlicht_changes/CGUITTFont.h" -#endif +class GUIScrollBar; class ParsedText { diff --git a/src/gui/guiPasswordChange.cpp b/src/gui/guiPasswordChange.cpp index 74cd62f5b..c983260f6 100644 --- a/src/gui/guiPasswordChange.cpp +++ b/src/gui/guiPasswordChange.cpp @@ -25,6 +25,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include <IGUIStaticText.h> #include <IGUIFont.h> +#ifdef HAVE_TOUCHSCREENGUI + #include "client/renderingengine.h" +#endif + #include "porting.h" #include "gettext.h" @@ -79,8 +83,8 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize) /* Calculate new sizes and positions */ -#ifdef __ANDROID__ - const float s = m_gui_scale * porting::getDisplayDensity() / 2; +#ifdef HAVE_TOUCHSCREENGUI + const float s = m_gui_scale * RenderingEngine::getDisplayDensity() / 2; #else const float s = m_gui_scale; #endif diff --git a/src/gui/guiSkin.cpp b/src/gui/guiSkin.cpp index e09209bd9..ca692f6cb 100644 --- a/src/gui/guiSkin.cpp +++ b/src/gui/guiSkin.cpp @@ -1024,48 +1024,6 @@ void GUISkin::draw2DRectangle(IGUIElement* element, }
-//! Writes attributes of the object.
-//! Implement this to expose the attributes of your scene node animator for
-//! scripting languages, editors, debuggers or xml serialization purposes.
-void GUISkin::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
-{
- u32 i;
- for (i=0; i<EGDC_COUNT; ++i)
- out->addColor(GUISkinColorNames[i], Colors[i]);
-
- for (i=0; i<EGDS_COUNT; ++i)
- out->addInt(GUISkinSizeNames[i], Sizes[i]);
-
- for (i=0; i<EGDT_COUNT; ++i)
- out->addString(GUISkinTextNames[i], Texts[i].c_str());
-
- for (i=0; i<EGDI_COUNT; ++i)
- out->addInt(GUISkinIconNames[i], Icons[i]);
-}
-
-
-//! Reads attributes of the object.
-//! Implement this to set the attributes of your scene node animator for
-//! scripting languages, editors, debuggers or xml deserialization purposes.
-void GUISkin::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
-{
- // TODO: This is not nice code for downward compatibility, whenever new values are added and users
- // load an old skin the corresponding values will be set to 0.
- u32 i;
- for (i=0; i<EGDC_COUNT; ++i)
- Colors[i] = in->getAttributeAsColor(GUISkinColorNames[i]);
-
- for (i=0; i<EGDS_COUNT; ++i)
- Sizes[i] = in->getAttributeAsInt(GUISkinSizeNames[i]);
-
- for (i=0; i<EGDT_COUNT; ++i)
- Texts[i] = in->getAttributeAsStringW(GUISkinTextNames[i]);
-
- for (i=0; i<EGDI_COUNT; ++i)
- Icons[i] = in->getAttributeAsInt(GUISkinIconNames[i]);
-}
-
-
//! gets the colors
// PATCH
void GUISkin::getColors(video::SColor* colors)
diff --git a/src/gui/guiSkin.h b/src/gui/guiSkin.h index bbb900f9f..fa9b27bdd 100644 --- a/src/gui/guiSkin.h +++ b/src/gui/guiSkin.h @@ -290,16 +290,6 @@ namespace gui //! get the type of this skin
virtual EGUI_SKIN_TYPE getType() const;
- //! Writes attributes of the object.
- //! Implement this to expose the attributes of your scene node animator for
- //! scripting languages, editors, debuggers or xml serialization purposes.
- virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const;
-
- //! Reads attributes of the object.
- //! Implement this to set the attributes of your scene node animator for
- //! scripting languages, editors, debuggers or xml deserialization purposes.
- virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0);
-
//! gets the colors
virtual void getColors(video::SColor* colors); // ::PATCH:
diff --git a/src/gui/guiTable.cpp b/src/gui/guiTable.cpp index cab2e19fd..79ae1aea3 100644 --- a/src/gui/guiTable.cpp +++ b/src/gui/guiTable.cpp @@ -77,9 +77,10 @@ GUITable::GUITable(gui::IGUIEnvironment *env, setTabStop(true); setTabOrder(-1); updateAbsolutePosition(); +#ifdef HAVE_TOUCHSCREENGUI + float density = 1; // dp scaling is applied by the skin +#else 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)) * density * diff --git a/src/gui/modalMenu.cpp b/src/gui/modalMenu.cpp index 1016de389..56a5d2cb9 100644 --- a/src/gui/modalMenu.cpp +++ b/src/gui/modalMenu.cpp @@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifdef HAVE_TOUCHSCREENGUI #include "touchscreengui.h" +#include "client/renderingengine.h" #endif // clang-format off @@ -40,8 +41,8 @@ GUIModalMenu::GUIModalMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent, m_remap_dbl_click(remap_dbl_click) { m_gui_scale = g_settings->getFloat("gui_scaling"); -#ifdef __ANDROID__ - float d = porting::getDisplayDensity(); +#ifdef HAVE_TOUCHSCREENGUI + float d = RenderingEngine::getDisplayDensity(); m_gui_scale *= 1.1 - 0.3 * d + 0.2 * d * d; #endif setVisible(true); @@ -183,7 +184,7 @@ static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent) return false; } -#ifdef __ANDROID__ +#ifdef HAVE_TOUCHSCREENGUI bool GUIModalMenu::simulateMouseEvent( gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event) @@ -217,6 +218,8 @@ bool GUIModalMenu::simulateMouseEvent( void GUIModalMenu::enter(gui::IGUIElement *hovered) { + if (!hovered) + return; sanity_check(!m_hovered); m_hovered.grab(hovered); SEvent gui_event{}; @@ -286,7 +289,9 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event) return retval; } } +#endif +#ifdef HAVE_TOUCHSCREENGUI if (event.EventType == EET_TOUCH_INPUT_EVENT) { irr_ptr<GUIModalMenu> holder; holder.grab(this); // keep this alive until return (it might be dropped downstream [?]) diff --git a/src/gui/modalMenu.h b/src/gui/modalMenu.h index ed0da3205..06e78f06b 100644 --- a/src/gui/modalMenu.h +++ b/src/gui/modalMenu.h @@ -75,10 +75,10 @@ protected: v2u32 m_screensize_old; float m_gui_scale; #ifdef __ANDROID__ - v2s32 m_down_pos; std::string m_jni_field_name; #endif #ifdef HAVE_TOUCHSCREENGUI + v2s32 m_down_pos; bool m_touchscreen_visible = true; #endif @@ -102,7 +102,7 @@ private: // wants to launch other menus bool m_allow_focus_removal = false; -#ifdef __ANDROID__ +#ifdef HAVE_TOUCHSCREENGUI irr_ptr<gui::IGUIElement> m_hovered; bool simulateMouseEvent(gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event); diff --git a/src/gui/profilergraph.cpp b/src/gui/profilergraph.cpp index b29285e2f..f71ef3799 100644 --- a/src/gui/profilergraph.cpp +++ b/src/gui/profilergraph.cpp @@ -94,20 +94,29 @@ void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver, show_min = 0; } - s32 texth = 15; + const s32 texth = 15; char buf[10]; - porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_max); + if (floorf(show_max) == show_max) + porting::mt_snprintf(buf, sizeof(buf), "%.5g", show_max); + else + porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_max); font->draw(utf8_to_wide(buf).c_str(), core::rect<s32>(textx, y - graphh, textx2, y - graphh + texth), meta.color); - porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_min); + + if (floorf(show_min) == show_min) + porting::mt_snprintf(buf, sizeof(buf), "%.5g", show_min); + else + porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_min); font->draw(utf8_to_wide(buf).c_str(), core::rect<s32>(textx, y - texth, textx2, y), meta.color); + font->draw(utf8_to_wide(id).c_str(), core::rect<s32>(textx, y - graphh / 2 - texth / 2, textx2, y - graphh / 2 + texth / 2), meta.color); + s32 graph1y = y; s32 graph1h = graphh; bool relativegraph = (show_min != 0 && show_min != show_max); diff --git a/src/gui/touchscreengui.cpp b/src/gui/touchscreengui.cpp index eb20b7e70..ebe1a6325 100644 --- a/src/gui/touchscreengui.cpp +++ b/src/gui/touchscreengui.cpp @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" #include "porting.h" #include "client/guiscalingfilter.h" +#include "client/renderingengine.h" #include <iostream> #include <algorithm> @@ -426,7 +427,7 @@ TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver) m_joystick_triggers_aux1 = g_settings->getBool("virtual_joystick_triggers_aux1"); m_screensize = m_device->getVideoDriver()->getScreenSize(); button_size = MYMIN(m_screensize.Y / 4.5f, - porting::getDisplayDensity() * + RenderingEngine::getDisplayDensity() * g_settings->getFloat("hud_scaling") * 65.0f); } @@ -668,9 +669,9 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id) if (button != after_last_element_id) { // handle button events handleButtonEvent(button, evt_id, false); - } else if (evt_id == m_move_id) { + } else if (m_has_move_id && evt_id == m_move_id) { // handle the point used for moving view - m_move_id = -1; + m_has_move_id = false; // if this pointer issued a mouse event issue symmetric release here if (m_move_sent_as_mouse_event) { @@ -692,8 +693,8 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id) } // handle joystick - else if (evt_id == m_joystick_id) { - m_joystick_id = -1; + else if (m_has_joystick_id && evt_id == m_joystick_id) { + m_has_joystick_id = false; // reset joystick for (unsigned int i = 0; i < 4; i++) @@ -776,7 +777,8 @@ void TouchScreenGUI::translateEvent(const SEvent &event) if ((m_fixed_joystick && dxj * dxj + dyj * dyj <= button_size * button_size * 1.5 * 1.5) || (!m_fixed_joystick && event.TouchInput.X < m_screensize.X / 3.0f)) { // If we don't already have a starting point for joystick make this the one. - if (m_joystick_id == -1) { + if (!m_has_joystick_id) { + m_has_joystick_id = true; m_joystick_id = event.TouchInput.ID; m_joystick_has_really_moved = false; @@ -796,7 +798,8 @@ void TouchScreenGUI::translateEvent(const SEvent &event) } } else { // If we don't already have a moving point make this the moving one. - if (m_move_id == -1) { + if (!m_has_move_id) { + m_has_move_id = true; m_move_id = event.TouchInput.ID; m_move_has_really_moved = false; m_move_downtime = porting::getTimeMs(); @@ -819,7 +822,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event) v2s32(event.TouchInput.X, event.TouchInput.Y)) return; - if (m_move_id != -1) { + if (m_has_move_id) { if ((event.TouchInput.ID == m_move_id) && (!m_move_sent_as_mouse_event)) { @@ -862,7 +865,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event) } } - if (m_joystick_id != -1 && event.TouchInput.ID == m_joystick_id) { + if (m_has_joystick_id && event.TouchInput.ID == m_joystick_id) { s32 X = event.TouchInput.X; s32 Y = event.TouchInput.Y; @@ -941,7 +944,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event) } } - if (m_move_id == -1 && m_joystick_id == -1) + if (!m_has_move_id && !m_has_joystick_id) handleChangedButton(event); } } @@ -1086,7 +1089,7 @@ void TouchScreenGUI::step(float dtime) button.repeatcounter += dtime; // in case we're moving around digging does not happen - if (m_move_id != -1) + if (m_has_move_id) m_move_has_really_moved = true; if (button.repeatcounter < button.repeatdelay) @@ -1114,7 +1117,7 @@ void TouchScreenGUI::step(float dtime) } // if a new placed pointer isn't moved for some time start digging - if ((m_move_id != -1) && + if (m_has_move_id && (!m_move_has_really_moved) && (!m_move_sent_as_mouse_event)) { diff --git a/src/gui/touchscreengui.h b/src/gui/touchscreengui.h index ad5abae87..6b36c0d59 100644 --- a/src/gui/touchscreengui.h +++ b/src/gui/touchscreengui.h @@ -228,13 +228,15 @@ private: */ line3d<f32> m_shootline; - int m_move_id = -1; + bool m_has_move_id = false; + size_t m_move_id; bool m_move_has_really_moved = false; u64 m_move_downtime = 0; bool m_move_sent_as_mouse_event = false; v2s32 m_move_downlocation = v2s32(-10000, -10000); - int m_joystick_id = -1; + bool m_has_joystick_id = false; + size_t m_joystick_id; bool m_joystick_has_really_moved = false; bool m_fixed_joystick = false; bool m_joystick_triggers_aux1 = false; |