3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "guiFormSpecMenu.h"
28 #include "constants.h"
30 #include "client/keycode.h"
31 #include "util/strfnd.h"
32 #include <IGUIButton.h>
33 #include <IGUICheckBox.h>
34 #include <IGUIComboBox.h>
35 #include <IGUIEditBox.h>
36 #include <IGUIStaticText.h>
38 #include <IGUITabControl.h>
39 #include "client/renderingengine.h"
41 #include "client/tile.h" // ITextureSource
42 #include "client/hud.h" // drawItemStack
46 #include "scripting_server.h"
47 #include "mainmenumanager.h"
50 #include "client/client.h"
51 #include "client/fontengine.h"
52 #include "client/sound.h"
54 #include "util/numeric.h"
55 #include "util/string.h" // for parseColorString()
56 #include "irrlicht_changes/static_text.h"
57 #include "client/guiscalingfilter.h"
58 #include "guiAnimatedImage.h"
59 #include "guiBackgroundImage.h"
61 #include "guiButton.h"
62 #include "guiButtonImage.h"
63 #include "guiButtonItemImage.h"
64 #include "guiEditBoxWithScrollbar.h"
65 #include "guiInventoryList.h"
66 #include "guiItemImage.h"
67 #include "guiScrollContainer.h"
68 #include "guiHyperText.h"
71 #define MY_CHECKPOS(a,b) \
72 if (v_pos.size() != 2) { \
73 errorstream<< "Invalid pos for element " << a << " specified: \"" \
74 << parts[b] << "\"" << std::endl; \
78 #define MY_CHECKGEOM(a,b) \
79 if (v_geom.size() != 2) { \
80 errorstream<< "Invalid geometry for element " << a << \
81 " specified: \"" << parts[b] << "\"" << std::endl; \
87 static unsigned int font_line_height(gui::IGUIFont *font)
89 return font->getDimension(L"Ay").Height + font->getKerningHeight();
92 inline u32 clamp_u8(s32 value)
94 return (u32) MYMIN(MYMAX(value, 0), 255);
97 GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
98 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
99 Client *client, gui::IGUIEnvironment *guienv, ISimpleTextureSource *tsrc,
100 ISoundManager *sound_manager, IFormSource *fsrc, TextDest *tdst,
101 const std::string &formspecPrepend, bool remap_dbl_click):
102 GUIModalMenu(guienv, parent, id, menumgr, remap_dbl_click),
105 m_sound_manager(sound_manager),
107 m_formspec_prepend(formspecPrepend),
112 current_keys_pending.key_down = false;
113 current_keys_pending.key_up = false;
114 current_keys_pending.key_enter = false;
115 current_keys_pending.key_escape = false;
117 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
118 m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
121 GUIFormSpecMenu::~GUIFormSpecMenu()
125 for (auto &table_it : m_tables)
126 table_it.second->drop();
127 for (auto &inventorylist_it : m_inventorylists)
128 inventorylist_it->drop();
129 for (auto &checkbox_it : m_checkboxes)
130 checkbox_it.second->drop();
131 for (auto &scrollbar_it : m_scrollbars)
132 scrollbar_it.second->drop();
133 for (auto &background_it : m_backgrounds)
134 background_it->drop();
135 for (auto &tooltip_rect_it : m_tooltip_rects)
136 tooltip_rect_it.first->drop();
137 for (auto &clickthrough_it : m_clickthrough_elements)
138 clickthrough_it->drop();
139 for (auto &scroll_container_it : m_scroll_containers)
140 scroll_container_it.second->drop();
142 delete m_selected_item;
147 void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
148 gui::IGUIEnvironment *guienv, JoystickController *joystick, IFormSource *fs_src,
149 TextDest *txt_dest, const std::string &formspecPrepend, ISoundManager *sound_manager)
151 if (cur_formspec == nullptr) {
152 cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
153 client, guienv, client->getTextureSource(), sound_manager, fs_src,
154 txt_dest, formspecPrepend);
155 cur_formspec->doPause = false;
158 Caution: do not call (*cur_formspec)->drop() here --
159 the reference might outlive the menu, so we will
160 periodically check if *cur_formspec is the only
161 remaining reference (i.e. the menu was removed)
162 and delete it in that case.
166 cur_formspec->setFormspecPrepend(formspecPrepend);
167 cur_formspec->setFormSource(fs_src);
168 cur_formspec->setTextDest(txt_dest);
172 void GUIFormSpecMenu::removeChildren()
174 const core::list<gui::IGUIElement*> &children = getChildren();
176 while (!children.empty()) {
177 (*children.getLast())->remove();
180 if (m_tooltip_element) {
181 m_tooltip_element->remove();
182 m_tooltip_element->drop();
183 m_tooltip_element = nullptr;
187 void GUIFormSpecMenu::setInitialFocus()
189 // Set initial focus according to following order of precedence:
190 // 1. first empty editbox
194 // 5. first focusable (not statictext, not tabheader)
195 // 6. first child element
197 core::list<gui::IGUIElement*> children = getChildren();
199 // in case "children" contains any NULL elements, remove them
200 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
201 it != children.end();) {
205 it = children.erase(it);
208 // 1. first empty editbox
209 for (gui::IGUIElement *it : children) {
210 if (it->getType() == gui::EGUIET_EDIT_BOX
211 && it->getText()[0] == 0) {
212 Environment->setFocus(it);
218 for (gui::IGUIElement *it : children) {
219 if (it->getType() == gui::EGUIET_EDIT_BOX) {
220 Environment->setFocus(it);
226 for (gui::IGUIElement *it : children) {
227 if (it->getTypeName() == std::string("GUITable")) {
228 Environment->setFocus(it);
234 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
235 it != children.end(); --it) {
236 if ((*it)->getType() == gui::EGUIET_BUTTON) {
237 Environment->setFocus(*it);
242 // 5. first focusable (not statictext, not tabheader)
243 for (gui::IGUIElement *it : children) {
244 if (it->getType() != gui::EGUIET_STATIC_TEXT &&
245 it->getType() != gui::EGUIET_TAB_CONTROL) {
246 Environment->setFocus(it);
251 // 6. first child element
252 if (children.empty())
253 Environment->setFocus(this);
255 Environment->setFocus(*(children.begin()));
258 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
260 for (auto &table : m_tables) {
261 if (tablename == table.first.fname)
267 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
269 for (auto &dropdown : m_dropdowns) {
270 if (name == dropdown.first.fname)
271 return &dropdown.second;
276 v2s32 GUIFormSpecMenu::getElementBasePos(const std::vector<std::string> *v_pos)
278 v2f32 pos_f = v2f32(padding.X, padding.Y) + pos_offset * spacing;
280 pos_f.X += stof((*v_pos)[0]) * spacing.X;
281 pos_f.Y += stof((*v_pos)[1]) * spacing.Y;
283 return v2s32(pos_f.X, pos_f.Y);
286 v2s32 GUIFormSpecMenu::getRealCoordinateBasePos(const std::vector<std::string> &v_pos)
288 return v2s32((stof(v_pos[0]) + pos_offset.X) * imgsize.X,
289 (stof(v_pos[1]) + pos_offset.Y) * imgsize.Y);
292 v2s32 GUIFormSpecMenu::getRealCoordinateGeometry(const std::vector<std::string> &v_geom)
294 return v2s32(stof(v_geom[0]) * imgsize.X, stof(v_geom[1]) * imgsize.Y);
297 void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
299 std::vector<std::string> parts = split(element,',');
301 if (((parts.size() == 2) || parts.size() == 3) ||
302 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
304 if (parts[1].find(';') != std::string::npos)
305 parts[1] = parts[1].substr(0,parts[1].find(';'));
307 data->invsize.X = MYMAX(0, stof(parts[0]));
308 data->invsize.Y = MYMAX(0, stof(parts[1]));
312 if (parts.size() == 3) {
313 if (parts[2] == "true") {
314 lockSize(true,v2u32(800,600));
318 data->explicit_size = true;
321 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
324 void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
326 std::vector<std::string> parts = split(element, ',');
328 if (parts.size() >= 2) {
329 if (parts[1].find(';') != std::string::npos)
330 parts[1] = parts[1].substr(0, parts[1].find(';'));
332 container_stack.push(pos_offset);
333 pos_offset.X += stof(parts[0]);
334 pos_offset.Y += stof(parts[1]);
337 errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
340 void GUIFormSpecMenu::parseContainerEnd(parserData* data)
342 if (container_stack.empty()) {
343 errorstream<< "Invalid container end element, no matching container start element" << std::endl;
345 pos_offset = container_stack.top();
346 container_stack.pop();
350 void GUIFormSpecMenu::parseScrollContainer(parserData *data, const std::string &element)
352 std::vector<std::string> parts = split(element, ';');
354 if (parts.size() < 4 ||
355 (parts.size() > 5 && m_formspec_version <= FORMSPEC_API_VERSION)) {
356 errorstream << "Invalid scroll_container start element (" << parts.size()
357 << "): '" << element << "'" << std::endl;
361 std::vector<std::string> v_pos = split(parts[0], ',');
362 std::vector<std::string> v_geom = split(parts[1], ',');
363 std::string scrollbar_name = parts[2];
364 std::string orientation = parts[3];
365 f32 scroll_factor = 0.1f;
366 if (parts.size() >= 5 && !parts[4].empty())
367 scroll_factor = stof(parts[4]);
369 MY_CHECKPOS("scroll_container", 0);
370 MY_CHECKGEOM("scroll_container", 1);
372 v2s32 pos = getRealCoordinateBasePos(v_pos);
373 v2s32 geom = getRealCoordinateGeometry(v_geom);
375 if (orientation == "vertical")
376 scroll_factor *= -imgsize.Y;
377 else if (orientation == "horizontal")
378 scroll_factor *= -imgsize.X;
380 warningstream << "GUIFormSpecMenu::parseScrollContainer(): "
381 << "Invalid scroll_container orientation: " << orientation
384 // old parent (at first: this)
385 // ^ is parent of clipper
386 // ^ is parent of mover
387 // ^ is parent of other elements
390 core::rect<s32> rect_clipper = core::rect<s32>(pos, pos + geom);
392 gui::IGUIElement *clipper = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
393 data->current_parent, 0, rect_clipper);
396 FieldSpec spec_mover(
400 258 + m_fields.size()
403 core::rect<s32> rect_mover = core::rect<s32>(0, 0, geom.X, geom.Y);
405 GUIScrollContainer *mover = new GUIScrollContainer(Environment,
406 clipper, spec_mover.fid, rect_mover, orientation, scroll_factor);
408 data->current_parent = mover;
410 m_scroll_containers.emplace_back(scrollbar_name, mover);
412 m_fields.push_back(spec_mover);
416 // remove interferring offset of normal containers
417 container_stack.push(pos_offset);
422 void GUIFormSpecMenu::parseScrollContainerEnd(parserData *data)
424 if (data->current_parent == this || data->current_parent->getParent() == this ||
425 container_stack.empty()) {
426 errorstream << "Invalid scroll_container end element, "
427 << "no matching scroll_container start element" << std::endl;
431 if (pos_offset.getLengthSQ() != 0.0f) {
432 // pos_offset is only set by containers and scroll_containers.
433 // scroll_containers always set it to 0,0 which means that if it is
434 // not 0,0, it is a normal container that was opened last, not a
436 errorstream << "Invalid scroll_container end element, "
437 << "an inner container was left open" << std::endl;
441 data->current_parent = data->current_parent->getParent()->getParent();
442 pos_offset = container_stack.top();
443 container_stack.pop();
446 void GUIFormSpecMenu::parseList(parserData *data, const std::string &element)
449 warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
453 std::vector<std::string> parts = split(element,';');
455 if (((parts.size() == 4) || (parts.size() == 5)) ||
456 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
458 std::string location = parts[0];
459 std::string listname = parts[1];
460 std::vector<std::string> v_pos = split(parts[2],',');
461 std::vector<std::string> v_geom = split(parts[3],',');
462 std::string startindex;
463 if (parts.size() == 5)
464 startindex = parts[4];
466 MY_CHECKPOS("list",2);
467 MY_CHECKGEOM("list",3);
469 InventoryLocation loc;
471 if (location == "context" || location == "current_name")
472 loc = m_current_inventory_location;
474 loc.deSerialize(location);
477 geom.X = stoi(v_geom[0]);
478 geom.Y = stoi(v_geom[1]);
481 if (!startindex.empty())
482 start_i = stoi(startindex);
484 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
485 errorstream << "Invalid list element: '" << element << "'" << std::endl;
489 if (!data->explicit_size)
490 warningstream << "invalid use of list without a size[] element" << std::endl;
496 258 + m_fields.size(),
500 auto style = getDefaultStyleForElement("list", spec.fname);
502 v2f32 slot_scale = style.getVector2f(StyleSpec::SIZE, v2f32(0, 0));
504 slot_scale.X <= 0 ? imgsize.X : std::max<f32>(slot_scale.X * imgsize.X, 1),
505 slot_scale.Y <= 0 ? imgsize.Y : std::max<f32>(slot_scale.Y * imgsize.Y, 1)
508 v2f32 slot_spacing = style.getVector2f(StyleSpec::SPACING, v2f32(-1, -1));
509 v2f32 default_spacing = data->real_coordinates ?
510 v2f32(imgsize.X * 0.25f, imgsize.Y * 0.25f) :
511 v2f32(spacing.X - imgsize.X, spacing.Y - imgsize.Y);
513 slot_spacing.X = slot_spacing.X < 0 ? default_spacing.X :
514 imgsize.X * slot_spacing.X;
515 slot_spacing.Y = slot_spacing.Y < 0 ? default_spacing.Y :
516 imgsize.Y * slot_spacing.Y;
518 slot_spacing += slot_size;
520 v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos) :
521 getElementBasePos(&v_pos);
523 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
524 pos.X + (geom.X - 1) * slot_spacing.X + slot_size.X,
525 pos.Y + (geom.Y - 1) * slot_spacing.Y + slot_size.Y);
527 GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent,
528 spec.fid, rect, m_invmgr, loc, listname, geom, start_i,
529 v2s32(slot_size.X, slot_size.Y), slot_spacing, this,
530 data->inventorylist_options, m_font);
532 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
534 m_inventorylists.push_back(e);
535 m_fields.push_back(spec);
538 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
541 void GUIFormSpecMenu::parseListRing(parserData *data, const std::string &element)
544 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
548 std::vector<std::string> parts = split(element, ';');
550 if (parts.size() == 2) {
551 std::string location = parts[0];
552 std::string listname = parts[1];
554 InventoryLocation loc;
556 if (location == "context" || location == "current_name")
557 loc = m_current_inventory_location;
559 loc.deSerialize(location);
561 m_inventory_rings.emplace_back(loc, listname);
565 if (element.empty() && m_inventorylists.size() > 1) {
566 size_t siz = m_inventorylists.size();
567 // insert the last two inv list elements into the list ring
568 const GUIInventoryList *spa = m_inventorylists[siz - 2];
569 const GUIInventoryList *spb = m_inventorylists[siz - 1];
570 m_inventory_rings.emplace_back(spa->getInventoryloc(), spa->getListname());
571 m_inventory_rings.emplace_back(spb->getInventoryloc(), spb->getListname());
575 errorstream<< "Invalid list ring element(" << parts.size() << ", "
576 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
579 void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
581 std::vector<std::string> parts = split(element,';');
583 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
584 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
586 std::vector<std::string> v_pos = split(parts[0],',');
587 std::string name = parts[1];
588 std::string label = parts[2];
589 std::string selected;
591 if (parts.size() >= 4)
594 MY_CHECKPOS("checkbox",0);
596 bool fselected = false;
598 if (selected == "true")
601 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
602 const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str());
603 s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH);
604 s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2;
607 core::rect<s32> rect;
609 if (data->real_coordinates) {
610 pos = getRealCoordinateBasePos(v_pos);
612 rect = core::rect<s32>(
615 pos.X + label_size.Width + cb_size + 7,
619 pos = getElementBasePos(&v_pos);
620 rect = core::rect<s32>(
622 pos.Y + imgsize.Y / 2 - y_center,
623 pos.X + label_size.Width + cb_size + 7,
624 pos.Y + imgsize.Y / 2 + y_center
630 wlabel, //Needed for displaying text on MSVC
635 spec.ftype = f_CheckBox;
637 gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect,
638 data->current_parent, spec.fid, spec.flabel.c_str());
640 auto style = getDefaultStyleForElement("checkbox", name);
642 spec.sound = style.get(StyleSpec::Property::SOUND, "");
644 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
646 if (spec.fname == m_focused_element) {
647 Environment->setFocus(e);
651 m_checkboxes.emplace_back(spec, e);
652 m_fields.push_back(spec);
655 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
658 void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
660 std::vector<std::string> parts = split(element,';');
662 if (parts.size() >= 5) {
663 std::vector<std::string> v_pos = split(parts[0],',');
664 std::vector<std::string> v_geom = split(parts[1],',');
665 std::string name = parts[3];
666 std::string value = parts[4];
668 MY_CHECKPOS("scrollbar",0);
669 MY_CHECKGEOM("scrollbar",1);
674 if (data->real_coordinates) {
675 pos = getRealCoordinateBasePos(v_pos);
676 dim = getRealCoordinateGeometry(v_geom);
678 pos = getElementBasePos(&v_pos);
679 dim.X = stof(v_geom[0]) * spacing.X;
680 dim.Y = stof(v_geom[1]) * spacing.Y;
683 core::rect<s32> rect =
684 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
693 bool is_horizontal = true;
695 if (parts[2] == "vertical")
696 is_horizontal = false;
698 spec.ftype = f_ScrollBar;
700 GUIScrollBar *e = new GUIScrollBar(Environment, data->current_parent,
701 spec.fid, rect, is_horizontal, true);
703 auto style = getDefaultStyleForElement("scrollbar", name);
704 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
705 e->setArrowsVisible(data->scrollbar_options.arrow_visiblity);
707 s32 max = data->scrollbar_options.max;
708 s32 min = data->scrollbar_options.min;
713 e->setPos(stoi(parts[4]));
715 e->setSmallStep(data->scrollbar_options.small_step);
716 e->setLargeStep(data->scrollbar_options.large_step);
718 s32 scrollbar_size = is_horizontal ? dim.X : dim.Y;
720 e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size);
722 if (spec.fname == m_focused_element) {
723 Environment->setFocus(e);
726 m_scrollbars.emplace_back(spec,e);
727 m_fields.push_back(spec);
730 errorstream << "Invalid scrollbar element(" << parts.size() << "): '" << element
734 void GUIFormSpecMenu::parseScrollBarOptions(parserData* data, const std::string &element)
736 std::vector<std::string> parts = split(element, ';');
738 if (parts.size() == 0) {
739 warningstream << "Invalid scrollbaroptions element(" << parts.size() << "): '" <<
740 element << "'" << std::endl;
744 for (const std::string &i : parts) {
745 std::vector<std::string> options = split(i, '=');
747 if (options.size() != 2) {
748 warningstream << "Invalid scrollbaroptions option syntax: '" <<
749 element << "'" << std::endl;
750 continue; // Go to next option
753 if (options[0] == "max") {
754 data->scrollbar_options.max = stoi(options[1]);
756 } else if (options[0] == "min") {
757 data->scrollbar_options.min = stoi(options[1]);
759 } else if (options[0] == "smallstep") {
760 int value = stoi(options[1]);
761 data->scrollbar_options.small_step = value < 0 ? 10 : value;
763 } else if (options[0] == "largestep") {
764 int value = stoi(options[1]);
765 data->scrollbar_options.large_step = value < 0 ? 100 : value;
767 } else if (options[0] == "thumbsize") {
768 int value = stoi(options[1]);
769 data->scrollbar_options.thumb_size = value <= 0 ? 1 : value;
771 } else if (options[0] == "arrows") {
772 std::string value = trim(options[1]);
774 data->scrollbar_options.arrow_visiblity = GUIScrollBar::HIDE;
775 else if (value == "show")
776 data->scrollbar_options.arrow_visiblity = GUIScrollBar::SHOW;
777 else // Auto hide/show
778 data->scrollbar_options.arrow_visiblity = GUIScrollBar::DEFAULT;
782 warningstream << "Invalid scrollbaroptions option(" << options[0] <<
783 "): '" << element << "'" << std::endl;
787 void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
789 std::vector<std::string> parts = split(element,';');
791 if ((parts.size() == 3) ||
792 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
794 std::vector<std::string> v_pos = split(parts[0],',');
795 std::vector<std::string> v_geom = split(parts[1],',');
796 std::string name = unescape_string(parts[2]);
798 MY_CHECKPOS("image", 0);
799 MY_CHECKGEOM("image", 1);
804 if (data->real_coordinates) {
805 pos = getRealCoordinateBasePos(v_pos);
806 geom = getRealCoordinateGeometry(v_geom);
808 pos = getElementBasePos(&v_pos);
809 geom.X = stof(v_geom[0]) * (float)imgsize.X;
810 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
813 if (!data->explicit_size)
814 warningstream<<"invalid use of image without a size[] element"<<std::endl;
816 video::ITexture *texture = m_tsrc->getTexture(name);
818 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
819 << std::endl << "\t" << name << std::endl;
827 258 + m_fields.size(),
830 core::rect<s32> rect(pos, pos + geom);
831 gui::IGUIImage *e = Environment->addImage(rect, data->current_parent,
833 e->setImage(texture);
834 e->setScaleImage(true);
835 auto style = getDefaultStyleForElement("image", spec.fname);
836 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
837 m_fields.push_back(spec);
839 // images should let events through
841 m_clickthrough_elements.push_back(e);
845 if (parts.size() == 2) {
846 std::vector<std::string> v_pos = split(parts[0],',');
847 std::string name = unescape_string(parts[1]);
849 MY_CHECKPOS("image", 0);
851 v2s32 pos = getElementBasePos(&v_pos);
853 if (!data->explicit_size)
854 warningstream<<"invalid use of image without a size[] element"<<std::endl;
856 video::ITexture *texture = m_tsrc->getTexture(name);
858 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
859 << std::endl << "\t" << name << std::endl;
867 258 + m_fields.size()
869 gui::IGUIImage *e = Environment->addImage(texture, pos, true,
870 data->current_parent, spec.fid, 0);
871 auto style = getDefaultStyleForElement("image", spec.fname);
872 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
873 m_fields.push_back(spec);
875 // images should let events through
877 m_clickthrough_elements.push_back(e);
880 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
883 void GUIFormSpecMenu::parseAnimatedImage(parserData *data, const std::string &element)
885 std::vector<std::string> parts = split(element, ';');
887 if (parts.size() != 6 && parts.size() != 7 &&
888 !(parts.size() > 7 && m_formspec_version > FORMSPEC_API_VERSION)) {
889 errorstream << "Invalid animated_image element(" << parts.size()
890 << "): '" << element << "'" << std::endl;
894 std::vector<std::string> v_pos = split(parts[0], ',');
895 std::vector<std::string> v_geom = split(parts[1], ',');
896 std::string name = parts[2];
897 std::string texture_name = unescape_string(parts[3]);
898 s32 frame_count = stoi(parts[4]);
899 s32 frame_duration = stoi(parts[5]);
901 MY_CHECKPOS("animated_image", 0);
902 MY_CHECKGEOM("animated_image", 1);
907 if (data->real_coordinates) {
908 pos = getRealCoordinateBasePos(v_pos);
909 geom = getRealCoordinateGeometry(v_geom);
911 pos = getElementBasePos(&v_pos);
912 geom.X = stof(v_geom[0]) * (float)imgsize.X;
913 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
916 if (!data->explicit_size)
917 warningstream << "Invalid use of animated_image without a size[] element" << std::endl;
923 258 + m_fields.size()
925 spec.ftype = f_AnimatedImage;
928 core::rect<s32> rect = core::rect<s32>(pos, pos + geom);
930 GUIAnimatedImage *e = new GUIAnimatedImage(Environment, data->current_parent, spec.fid,
931 rect, texture_name, frame_count, frame_duration, m_tsrc);
933 if (parts.size() >= 7)
934 e->setFrameIndex(stoi(parts[6]) - 1);
936 auto style = getDefaultStyleForElement("animated_image", spec.fname, "image");
937 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
939 // Animated images should let events through
940 m_clickthrough_elements.push_back(e);
942 m_fields.push_back(spec);
945 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
947 std::vector<std::string> parts = split(element,';');
949 if ((parts.size() == 3) ||
950 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
952 std::vector<std::string> v_pos = split(parts[0],',');
953 std::vector<std::string> v_geom = split(parts[1],',');
954 std::string name = parts[2];
956 MY_CHECKPOS("itemimage",0);
957 MY_CHECKGEOM("itemimage",1);
962 if (data->real_coordinates) {
963 pos = getRealCoordinateBasePos(v_pos);
964 geom = getRealCoordinateGeometry(v_geom);
966 pos = getElementBasePos(&v_pos);
967 geom.X = stof(v_geom[0]) * (float)imgsize.X;
968 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
971 if(!data->explicit_size)
972 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
978 258 + m_fields.size(),
981 spec.ftype = f_ItemImage;
983 GUIItemImage *e = new GUIItemImage(Environment, data->current_parent, spec.fid,
984 core::rect<s32>(pos, pos + geom), name, m_font, m_client);
985 auto style = getDefaultStyleForElement("item_image", spec.fname);
986 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
988 // item images should let events through
989 m_clickthrough_elements.push_back(e);
991 m_fields.push_back(spec);
994 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
997 void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
998 const std::string &type)
1000 std::vector<std::string> parts = split(element,';');
1002 if ((parts.size() == 4) ||
1003 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
1005 std::vector<std::string> v_pos = split(parts[0],',');
1006 std::vector<std::string> v_geom = split(parts[1],',');
1007 std::string name = parts[2];
1008 std::string label = parts[3];
1010 MY_CHECKPOS("button",0);
1011 MY_CHECKGEOM("button",1);
1015 core::rect<s32> rect;
1017 if (data->real_coordinates) {
1018 pos = getRealCoordinateBasePos(v_pos);
1019 geom = getRealCoordinateGeometry(v_geom);
1020 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1023 pos = getElementBasePos(&v_pos);
1024 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1025 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1027 rect = core::rect<s32>(pos.X, pos.Y - m_btn_height,
1028 pos.X + geom.X, pos.Y + m_btn_height);
1031 if(!data->explicit_size)
1032 warningstream<<"invalid use of button without a size[] element"<<std::endl;
1034 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1040 258 + m_fields.size()
1042 spec.ftype = f_Button;
1043 if(type == "button_exit")
1044 spec.is_exit = true;
1046 GUIButton *e = GUIButton::addButton(Environment, rect, m_tsrc,
1047 data->current_parent, spec.fid, spec.flabel.c_str());
1049 auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
1051 spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
1053 e->setStyles(style);
1055 if (spec.fname == m_focused_element) {
1056 Environment->setFocus(e);
1059 m_fields.push_back(spec);
1062 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
1065 void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
1067 std::vector<std::string> parts = split(element,';');
1069 if ((parts.size() >= 3 && parts.size() <= 5) ||
1070 (parts.size() > 5 && m_formspec_version > FORMSPEC_API_VERSION)) {
1071 std::vector<std::string> v_pos = split(parts[0],',');
1072 std::vector<std::string> v_geom = split(parts[1],',');
1073 std::string name = unescape_string(parts[2]);
1075 MY_CHECKPOS("background",0);
1076 MY_CHECKGEOM("background",1);
1081 if (data->real_coordinates) {
1082 pos = getRealCoordinateBasePos(v_pos);
1083 geom = getRealCoordinateGeometry(v_geom);
1085 pos = getElementBasePos(&v_pos);
1086 pos.X -= (spacing.X - (float)imgsize.X) / 2;
1087 pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
1089 geom.X = stof(v_geom[0]) * spacing.X;
1090 geom.Y = stof(v_geom[1]) * spacing.Y;
1094 if (parts.size() >= 4 && is_yes(parts[3])) {
1095 if (data->real_coordinates) {
1096 pos = getRealCoordinateBasePos(v_pos) * -1;
1099 pos.X = stoi(v_pos[0]); //acts as offset
1100 pos.Y = stoi(v_pos[1]);
1105 core::rect<s32> middle;
1106 if (parts.size() >= 5) {
1107 std::vector<std::string> v_middle = split(parts[4], ',');
1108 if (v_middle.size() == 1) {
1109 s32 x = stoi(v_middle[0]);
1110 middle.UpperLeftCorner = core::vector2di(x, x);
1111 middle.LowerRightCorner = core::vector2di(-x, -x);
1112 } else if (v_middle.size() == 2) {
1113 s32 x = stoi(v_middle[0]);
1114 s32 y = stoi(v_middle[1]);
1115 middle.UpperLeftCorner = core::vector2di(x, y);
1116 middle.LowerRightCorner = core::vector2di(-x, -y);
1117 // `-x` is interpreted as `w - x`
1118 } else if (v_middle.size() == 4) {
1119 middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1]));
1120 middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3]));
1122 warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl;
1126 if (!data->explicit_size && !clip)
1127 warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
1133 258 + m_fields.size()
1136 core::rect<s32> rect;
1138 // no auto_clip => position like normal image
1139 rect = core::rect<s32>(pos, pos + geom);
1141 // it will be auto-clipped when drawing
1142 rect = core::rect<s32>(-pos, pos);
1145 GUIBackgroundImage *e = new GUIBackgroundImage(Environment, this, spec.fid,
1146 rect, name, middle, m_tsrc, clip);
1148 FATAL_ERROR_IF(!e, "Failed to create background formspec element");
1150 e->setNotClipped(true);
1152 e->setVisible(false); // the element is drawn manually before all others
1154 m_backgrounds.push_back(e);
1155 m_fields.push_back(spec);
1158 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
1161 void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
1163 std::vector<std::string> parts = split(element,';');
1165 data->table_options.clear();
1166 for (const std::string &part : parts) {
1167 // Parse table option
1168 std::string opt = unescape_string(part);
1169 data->table_options.push_back(GUITable::splitOption(opt));
1173 void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
1175 std::vector<std::string> parts = split(element,';');
1177 data->table_columns.clear();
1178 for (const std::string &part : parts) {
1179 std::vector<std::string> col_parts = split(part,',');
1180 GUITable::TableColumn column;
1181 // Parse column type
1182 if (!col_parts.empty())
1183 column.type = col_parts[0];
1184 // Parse column options
1185 for (size_t j = 1; j < col_parts.size(); ++j) {
1186 std::string opt = unescape_string(col_parts[j]);
1187 column.options.push_back(GUITable::splitOption(opt));
1189 data->table_columns.push_back(column);
1193 void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
1195 std::vector<std::string> parts = split(element,';');
1197 if (((parts.size() == 4) || (parts.size() == 5)) ||
1198 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1200 std::vector<std::string> v_pos = split(parts[0],',');
1201 std::vector<std::string> v_geom = split(parts[1],',');
1202 std::string name = parts[2];
1203 std::vector<std::string> items = split(parts[3],',');
1204 std::string str_initial_selection;
1205 std::string str_transparent = "false";
1207 if (parts.size() >= 5)
1208 str_initial_selection = parts[4];
1210 MY_CHECKPOS("table",0);
1211 MY_CHECKGEOM("table",1);
1216 if (data->real_coordinates) {
1217 pos = getRealCoordinateBasePos(v_pos);
1218 geom = getRealCoordinateGeometry(v_geom);
1220 pos = getElementBasePos(&v_pos);
1221 geom.X = stof(v_geom[0]) * spacing.X;
1222 geom.Y = stof(v_geom[1]) * spacing.Y;
1225 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1231 258 + m_fields.size()
1234 spec.ftype = f_Table;
1236 for (std::string &item : items) {
1237 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1240 //now really show table
1241 GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
1244 if (spec.fname == m_focused_element) {
1245 Environment->setFocus(e);
1248 e->setTable(data->table_options, data->table_columns, items);
1250 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1251 e->setDynamicData(data->table_dyndata[name]);
1254 if (!str_initial_selection.empty() && str_initial_selection != "0")
1255 e->setSelected(stoi(str_initial_selection));
1257 auto style = getDefaultStyleForElement("table", name);
1258 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1259 e->setOverrideFont(style.getFont());
1261 m_tables.emplace_back(spec, e);
1262 m_fields.push_back(spec);
1265 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
1268 void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
1270 std::vector<std::string> parts = split(element,';');
1272 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
1273 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1275 std::vector<std::string> v_pos = split(parts[0],',');
1276 std::vector<std::string> v_geom = split(parts[1],',');
1277 std::string name = parts[2];
1278 std::vector<std::string> items = split(parts[3],',');
1279 std::string str_initial_selection;
1280 std::string str_transparent = "false";
1282 if (parts.size() >= 5)
1283 str_initial_selection = parts[4];
1285 if (parts.size() >= 6)
1286 str_transparent = parts[5];
1288 MY_CHECKPOS("textlist",0);
1289 MY_CHECKGEOM("textlist",1);
1294 if (data->real_coordinates) {
1295 pos = getRealCoordinateBasePos(v_pos);
1296 geom = getRealCoordinateGeometry(v_geom);
1298 pos = getElementBasePos(&v_pos);
1299 geom.X = stof(v_geom[0]) * spacing.X;
1300 geom.Y = stof(v_geom[1]) * spacing.Y;
1303 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1309 258 + m_fields.size()
1312 spec.ftype = f_Table;
1314 for (std::string &item : items) {
1315 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1318 //now really show list
1319 GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
1322 if (spec.fname == m_focused_element) {
1323 Environment->setFocus(e);
1326 e->setTextList(items, is_yes(str_transparent));
1328 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1329 e->setDynamicData(data->table_dyndata[name]);
1332 if (!str_initial_selection.empty() && str_initial_selection != "0")
1333 e->setSelected(stoi(str_initial_selection));
1335 auto style = getDefaultStyleForElement("textlist", name);
1336 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1337 e->setOverrideFont(style.getFont());
1339 m_tables.emplace_back(spec, e);
1340 m_fields.push_back(spec);
1343 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
1346 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
1348 std::vector<std::string> parts = split(element, ';');
1350 if (parts.size() == 5 || parts.size() == 6 ||
1351 (parts.size() > 6 && m_formspec_version > FORMSPEC_API_VERSION))
1353 std::vector<std::string> v_pos = split(parts[0], ',');
1354 std::string name = parts[2];
1355 std::vector<std::string> items = split(parts[3], ',');
1356 std::string str_initial_selection = parts[4];
1358 if (parts.size() >= 6 && is_yes(parts[5]))
1359 m_dropdown_index_event[name] = true;
1361 MY_CHECKPOS("dropdown",0);
1365 core::rect<s32> rect;
1367 if (data->real_coordinates) {
1368 std::vector<std::string> v_geom = split(parts[1],',');
1370 if (v_geom.size() == 1)
1371 v_geom.emplace_back("1");
1373 MY_CHECKGEOM("dropdown",1);
1375 pos = getRealCoordinateBasePos(v_pos);
1376 geom = getRealCoordinateGeometry(v_geom);
1377 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1379 pos = getElementBasePos(&v_pos);
1381 s32 width = stof(parts[1]) * spacing.Y;
1383 rect = core::rect<s32>(pos.X, pos.Y,
1384 pos.X + width, pos.Y + (m_btn_height * 2));
1391 258 + m_fields.size()
1394 spec.ftype = f_DropDown;
1397 //now really show list
1398 gui::IGUIComboBox *e = Environment->addComboBox(rect, data->current_parent,
1401 if (spec.fname == m_focused_element) {
1402 Environment->setFocus(e);
1405 for (const std::string &item : items) {
1406 e->addItem(unescape_translate(unescape_string(
1407 utf8_to_wide(item))).c_str());
1410 if (!str_initial_selection.empty())
1411 e->setSelected(stoi(str_initial_selection)-1);
1413 auto style = getDefaultStyleForElement("dropdown", name);
1415 spec.sound = style.get(StyleSpec::Property::SOUND, "");
1417 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1419 m_fields.push_back(spec);
1421 m_dropdowns.emplace_back(spec, std::vector<std::string>());
1422 std::vector<std::string> &values = m_dropdowns.back().second;
1423 for (const std::string &item : items) {
1424 values.push_back(unescape_string(item));
1429 errorstream << "Invalid dropdown element(" << parts.size() << "): '" << element
1430 << "'" << std::endl;
1433 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
1435 std::vector<std::string> parts = split(element,';');
1436 if (parts.size() == 2 ||
1437 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
1438 field_close_on_enter[parts[0]] = is_yes(parts[1]);
1442 void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
1444 std::vector<std::string> parts = split(element,';');
1446 if (parts.size() == 4 ||
1447 (parts.size() > 4 && m_formspec_version > FORMSPEC_API_VERSION))
1449 std::vector<std::string> v_pos = split(parts[0],',');
1450 std::vector<std::string> v_geom = split(parts[1],',');
1451 std::string name = parts[2];
1452 std::string label = parts[3];
1454 MY_CHECKPOS("pwdfield",0);
1455 MY_CHECKGEOM("pwdfield",1);
1460 if (data->real_coordinates) {
1461 pos = getRealCoordinateBasePos(v_pos);
1462 geom = getRealCoordinateGeometry(v_geom);
1464 pos = getElementBasePos(&v_pos);
1467 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1469 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1470 pos.Y -= m_btn_height;
1471 geom.Y = m_btn_height*2;
1474 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1476 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1482 258 + m_fields.size(),
1488 gui::IGUIEditBox *e = Environment->addEditBox(0, rect, true,
1489 data->current_parent, spec.fid);
1491 if (spec.fname == m_focused_element) {
1492 Environment->setFocus(e);
1495 if (label.length() >= 1) {
1496 int font_height = g_fontengine->getTextHeight();
1497 rect.UpperLeftCorner.Y -= font_height;
1498 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1499 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1500 data->current_parent, 0);
1503 e->setPasswordBox(true,L'*');
1505 auto style = getDefaultStyleForElement("pwdfield", name, "field");
1506 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1507 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1508 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1509 e->setOverrideFont(style.getFont());
1512 evt.EventType = EET_KEY_INPUT_EVENT;
1513 evt.KeyInput.Key = KEY_END;
1514 evt.KeyInput.Char = 0;
1515 evt.KeyInput.Control = false;
1516 evt.KeyInput.Shift = false;
1517 evt.KeyInput.PressedDown = true;
1520 // Note: Before 5.2.0 "parts.size() >= 5" resulted in a
1521 // warning referring to field_close_on_enter[]!
1523 m_fields.push_back(spec);
1526 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
1529 void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
1530 core::rect<s32> &rect, bool is_multiline)
1532 bool is_editable = !spec.fname.empty();
1533 if (!is_editable && !is_multiline) {
1534 // spec field id to 0, this stops submit searching for a value that isn't there
1535 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1536 data->current_parent, 0);
1542 } else if (is_multiline &&
1543 spec.fdefault.empty() && !spec.flabel.empty()) {
1544 // Multiline textareas: swap default and label for backwards compat
1545 spec.flabel.swap(spec.fdefault);
1548 gui::IGUIEditBox *e = nullptr;
1550 e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true, Environment,
1551 data->current_parent, spec.fid, rect, is_editable, true);
1552 } else if (is_editable) {
1553 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
1554 data->current_parent, spec.fid);
1558 auto style = getDefaultStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
1561 if (is_editable && spec.fname == m_focused_element)
1562 Environment->setFocus(e);
1565 e->setMultiLine(true);
1566 e->setWordWrap(true);
1567 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1570 evt.EventType = EET_KEY_INPUT_EVENT;
1571 evt.KeyInput.Key = KEY_END;
1572 evt.KeyInput.Char = 0;
1573 evt.KeyInput.Control = 0;
1574 evt.KeyInput.Shift = 0;
1575 evt.KeyInput.PressedDown = true;
1579 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1580 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1581 bool border = style.getBool(StyleSpec::BORDER, true);
1582 e->setDrawBorder(border);
1583 e->setDrawBackground(border);
1584 e->setOverrideFont(style.getFont());
1589 if (!spec.flabel.empty()) {
1590 int font_height = g_fontengine->getTextHeight();
1591 rect.UpperLeftCorner.Y -= font_height;
1592 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1593 IGUIElement *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
1594 rect, false, true, data->current_parent, 0);
1597 t->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1601 void GUIFormSpecMenu::parseSimpleField(parserData *data,
1602 std::vector<std::string> &parts)
1604 std::string name = parts[0];
1605 std::string label = parts[1];
1606 std::string default_val = parts[2];
1608 core::rect<s32> rect;
1610 if (data->explicit_size)
1611 warningstream << "invalid use of unpositioned \"field\" in inventory" << std::endl;
1613 v2s32 pos = getElementBasePos(nullptr);
1614 pos.Y = (data->simple_field_count + 2) * 60;
1615 v2s32 size = DesiredRect.getSize();
1617 rect = core::rect<s32>(
1618 size.X / 2 - 150, pos.Y,
1619 size.X / 2 - 150 + 300, pos.Y + m_btn_height * 2
1624 default_val = m_form_src->resolveText(default_val);
1627 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1632 utf8_to_wide(unescape_string(default_val)),
1633 258 + m_fields.size(),
1638 createTextField(data, spec, rect, false);
1640 m_fields.push_back(spec);
1642 data->simple_field_count++;
1645 void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
1646 const std::string &type)
1648 std::vector<std::string> v_pos = split(parts[0],',');
1649 std::vector<std::string> v_geom = split(parts[1],',');
1650 std::string name = parts[2];
1651 std::string label = parts[3];
1652 std::string default_val = parts[4];
1654 MY_CHECKPOS(type,0);
1655 MY_CHECKGEOM(type,1);
1660 if (data->real_coordinates) {
1661 pos = getRealCoordinateBasePos(v_pos);
1662 geom = getRealCoordinateGeometry(v_geom);
1664 pos = getElementBasePos(&v_pos);
1667 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1669 if (type == "textarea")
1671 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1672 pos.Y += m_btn_height;
1676 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1677 pos.Y -= m_btn_height;
1678 geom.Y = m_btn_height*2;
1682 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1684 if(!data->explicit_size)
1685 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1688 default_val = m_form_src->resolveText(default_val);
1691 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1696 utf8_to_wide(unescape_string(default_val)),
1697 258 + m_fields.size(),
1702 createTextField(data, spec, rect, type == "textarea");
1704 // Note: Before 5.2.0 "parts.size() >= 6" resulted in a
1705 // warning referring to field_close_on_enter[]!
1707 m_fields.push_back(spec);
1710 void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
1711 const std::string &type)
1713 std::vector<std::string> parts = split(element,';');
1715 if (parts.size() == 3 || parts.size() == 4) {
1716 parseSimpleField(data,parts);
1720 if ((parts.size() == 5) ||
1721 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1723 parseTextArea(data,parts,type);
1726 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1729 void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &element)
1731 std::vector<std::string> parts = split(element, ';');
1733 if (parts.size() != 4 && m_formspec_version < FORMSPEC_API_VERSION) {
1734 errorstream << "Invalid text element(" << parts.size() << "): '" << element << "'" << std::endl;
1738 std::vector<std::string> v_pos = split(parts[0], ',');
1739 std::vector<std::string> v_geom = split(parts[1], ',');
1740 std::string name = parts[2];
1741 std::string text = parts[3];
1743 MY_CHECKPOS("hypertext", 0);
1744 MY_CHECKGEOM("hypertext", 1);
1749 if (data->real_coordinates) {
1750 pos = getRealCoordinateBasePos(v_pos);
1751 geom = getRealCoordinateGeometry(v_geom);
1753 pos = getElementBasePos(&v_pos);
1756 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1757 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y - imgsize.Y);
1758 pos.Y += m_btn_height;
1761 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X + geom.X, pos.Y + geom.Y);
1764 text = m_form_src->resolveText(text);
1768 translate_string(utf8_to_wide(unescape_string(text))),
1770 258 + m_fields.size()
1773 spec.ftype = f_HyperText;
1775 auto style = getDefaultStyleForElement("hypertext", spec.fname);
1776 spec.sound = style.get(StyleSpec::Property::SOUND, "");
1778 GUIHyperText *e = new GUIHyperText(spec.flabel.c_str(), Environment,
1779 data->current_parent, spec.fid, rect, m_client, m_tsrc);
1782 m_fields.push_back(spec);
1785 void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
1787 std::vector<std::string> parts = split(element,';');
1789 if ((parts.size() == 2) ||
1790 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1792 std::vector<std::string> v_pos = split(parts[0],',');
1793 std::string text = parts[1];
1795 MY_CHECKPOS("label",0);
1797 if(!data->explicit_size)
1798 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1800 std::vector<std::string> lines = split(text, '\n');
1802 auto style = getDefaultStyleForElement("label", "");
1803 gui::IGUIFont *font = style.getFont();
1807 for (unsigned int i = 0; i != lines.size(); i++) {
1808 std::wstring wlabel_colors = translate_string(
1809 utf8_to_wide(unescape_string(lines[i])));
1810 // Without color escapes to get the font dimensions
1811 std::wstring wlabel_plain = unescape_enriched(wlabel_colors);
1813 core::rect<s32> rect;
1815 if (data->real_coordinates) {
1816 // Lines are spaced at the distance of 1/2 imgsize.
1817 // This alows lines that line up with the new elements
1818 // easily without sacrificing good line distance. If
1819 // it was one whole imgsize, it would have too much
1821 v2s32 pos = getRealCoordinateBasePos(v_pos);
1823 // Labels are positioned by their center, not their top.
1824 pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2);
1826 rect = core::rect<s32>(
1828 pos.X + font->getDimension(wlabel_plain.c_str()).Width,
1832 // Lines are spaced at the nominal distance of
1833 // 2/5 inventory slot, even if the font doesn't
1834 // quite match that. This provides consistent
1835 // form layout, at the expense of sometimes
1836 // having sub-optimal spacing for the font.
1837 // We multiply by 2 and then divide by 5, rather
1838 // than multiply by 0.4, to get exact results
1839 // in the integer cases: 0.4 is not exactly
1840 // representable in binary floating point.
1842 v2s32 pos = getElementBasePos(nullptr);
1843 pos.X += stof(v_pos[0]) * spacing.X;
1844 pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
1846 pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0;
1848 rect = core::rect<s32>(
1849 pos.X, pos.Y - m_btn_height,
1850 pos.X + font->getDimension(wlabel_plain.c_str()).Width,
1851 pos.Y + m_btn_height);
1858 258 + m_fields.size(),
1861 gui::IGUIStaticText *e = gui::StaticText::add(Environment,
1862 spec.flabel.c_str(), rect, false, false, data->current_parent,
1864 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
1866 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1867 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1868 e->setOverrideFont(font);
1870 m_fields.push_back(spec);
1872 // labels should let events through
1874 m_clickthrough_elements.push_back(e);
1879 errorstream << "Invalid label element(" << parts.size() << "): '" << element
1880 << "'" << std::endl;
1883 void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
1885 std::vector<std::string> parts = split(element,';');
1887 if ((parts.size() == 2) ||
1888 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1890 std::vector<std::string> v_pos = split(parts[0],',');
1891 std::wstring text = unescape_translate(
1892 unescape_string(utf8_to_wide(parts[1])));
1894 MY_CHECKPOS("vertlabel",1);
1896 auto style = getDefaultStyleForElement("vertlabel", "", "label");
1897 gui::IGUIFont *font = style.getFont();
1902 core::rect<s32> rect;
1904 if (data->real_coordinates) {
1905 pos = getRealCoordinateBasePos(v_pos);
1907 // Vertlabels are positioned by center, not left.
1908 pos.X -= imgsize.X / 2;
1910 // We use text.length + 1 because without it, the rect
1911 // isn't quite tall enough and cuts off the text.
1912 rect = core::rect<s32>(pos.X, pos.Y,
1914 pos.Y + font_line_height(font) *
1915 (text.length() + 1));
1918 pos = getElementBasePos(&v_pos);
1920 // As above, the length must be one longer. The width of
1921 // the rect (15 pixels) seems rather arbitrary, but
1922 // changing it might break something.
1923 rect = core::rect<s32>(
1924 pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
1926 font_line_height(font) *
1927 (text.length() + 1) +
1928 ((imgsize.Y/2) - m_btn_height));
1931 if(!data->explicit_size)
1932 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1936 for (wchar_t i : text) {
1945 258 + m_fields.size()
1947 gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
1948 rect, false, false, data->current_parent, spec.fid);
1949 e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1951 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1952 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1953 e->setOverrideFont(font);
1955 m_fields.push_back(spec);
1957 // vertlabels should let events through
1959 m_clickthrough_elements.push_back(e);
1962 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1965 void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
1966 const std::string &type)
1968 std::vector<std::string> parts = split(element,';');
1970 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1971 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1973 std::vector<std::string> v_pos = split(parts[0],',');
1974 std::vector<std::string> v_geom = split(parts[1],',');
1975 std::string image_name = parts[2];
1976 std::string name = parts[3];
1977 std::string label = parts[4];
1979 MY_CHECKPOS("imagebutton",0);
1980 MY_CHECKGEOM("imagebutton",1);
1982 std::string pressed_image_name;
1984 if (parts.size() >= 8) {
1985 pressed_image_name = parts[7];
1991 if (data->real_coordinates) {
1992 pos = getRealCoordinateBasePos(v_pos);
1993 geom = getRealCoordinateGeometry(v_geom);
1995 pos = getElementBasePos(&v_pos);
1996 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1997 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
2000 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
2003 if (!data->explicit_size)
2004 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
2006 image_name = unescape_string(image_name);
2007 pressed_image_name = unescape_string(pressed_image_name);
2009 std::wstring wlabel = utf8_to_wide(unescape_string(label));
2014 utf8_to_wide(image_name),
2015 258 + m_fields.size()
2017 spec.ftype = f_Button;
2018 if (type == "image_button_exit")
2019 spec.is_exit = true;
2021 GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, m_tsrc,
2022 data->current_parent, spec.fid, spec.flabel.c_str());
2024 if (spec.fname == m_focused_element) {
2025 Environment->setFocus(e);
2028 auto style = getStyleForElement("image_button", spec.fname);
2030 spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
2032 // Override style properties with values specified directly in the element
2033 if (!image_name.empty())
2034 style[StyleSpec::STATE_DEFAULT].set(StyleSpec::FGIMG, image_name);
2036 if (!pressed_image_name.empty())
2037 style[StyleSpec::STATE_PRESSED].set(StyleSpec::FGIMG, pressed_image_name);
2039 if (parts.size() >= 7) {
2040 style[StyleSpec::STATE_DEFAULT].set(StyleSpec::NOCLIP, parts[5]);
2041 style[StyleSpec::STATE_DEFAULT].set(StyleSpec::BORDER, parts[6]);
2044 e->setStyles(style);
2045 e->setScaleImage(true);
2047 m_fields.push_back(spec);
2051 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
2054 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
2056 std::vector<std::string> parts = split(element, ';');
2058 if (((parts.size() == 4) || (parts.size() == 6)) || (parts.size() == 7 &&
2059 data->real_coordinates) || ((parts.size() > 6) &&
2060 (m_formspec_version > FORMSPEC_API_VERSION)))
2062 std::vector<std::string> v_pos = split(parts[0],',');
2064 // If we're using real coordinates, add an extra field for height.
2065 // Width is not here because tabs are the width of the text, and
2066 // there's no reason to change that.
2068 std::vector<std::string> v_geom = {"1", "1"}; // Dummy width and height
2069 bool auto_width = true;
2070 if (parts.size() == 7) {
2073 v_geom = split(parts[1], ',');
2074 if (v_geom.size() == 1)
2075 v_geom.insert(v_geom.begin(), "1"); // Dummy value
2080 std::string name = parts[i+1];
2081 std::vector<std::string> buttons = split(parts[i+2], ',');
2082 std::string str_index = parts[i+3];
2083 bool show_background = true;
2084 bool show_border = true;
2085 int tab_index = stoi(str_index) - 1;
2087 MY_CHECKPOS("tabheader", 0);
2089 if (parts.size() == 6 + i) {
2090 if (parts[4+i] == "true")
2091 show_background = false;
2092 if (parts[5+i] == "false")
2093 show_border = false;
2100 258 + m_fields.size()
2103 spec.ftype = f_TabHeader;
2108 if (data->real_coordinates) {
2109 pos = getRealCoordinateBasePos(v_pos);
2111 geom = getRealCoordinateGeometry(v_geom);
2112 // Set default height
2113 if (parts.size() <= 6)
2114 geom.Y = m_btn_height * 2;
2115 pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
2117 geom.X = DesiredRect.getWidth(); // Set automatic width
2119 MY_CHECKGEOM("tabheader", 1);
2121 v2f32 pos_f = pos_offset * spacing;
2122 pos_f.X += stof(v_pos[0]) * spacing.X;
2123 pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
2124 pos = v2s32(pos_f.X, pos_f.Y);
2126 geom.Y = m_btn_height * 2;
2127 geom.X = DesiredRect.getWidth();
2130 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
2133 gui::IGUITabControl *e = Environment->addTabControl(rect,
2134 data->current_parent, show_background, show_border, spec.fid);
2135 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
2136 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
2137 e->setTabHeight(geom.Y);
2139 auto style = getDefaultStyleForElement("tabheader", name);
2141 spec.sound = style.get(StyleSpec::Property::SOUND, "");
2143 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
2145 for (const std::string &button : buttons) {
2146 auto tab = e->addTab(unescape_translate(unescape_string(
2147 utf8_to_wide(button))).c_str(), -1);
2148 if (style.isNotDefault(StyleSpec::BGCOLOR))
2149 tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
2151 tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
2154 if ((tab_index >= 0) &&
2155 (buttons.size() < INT_MAX) &&
2156 (tab_index < (int) buttons.size()))
2157 e->setActiveTab(tab_index);
2159 m_fields.push_back(spec);
2162 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
2163 << element << "'" << std::endl;
2166 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
2168 if (m_client == 0) {
2169 warningstream << "invalid use of item_image_button with m_client==0"
2174 std::vector<std::string> parts = split(element,';');
2176 if ((parts.size() == 5) ||
2177 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2179 std::vector<std::string> v_pos = split(parts[0],',');
2180 std::vector<std::string> v_geom = split(parts[1],',');
2181 std::string item_name = parts[2];
2182 std::string name = parts[3];
2183 std::string label = parts[4];
2185 label = unescape_string(label);
2186 item_name = unescape_string(item_name);
2188 MY_CHECKPOS("itemimagebutton",0);
2189 MY_CHECKGEOM("itemimagebutton",1);
2194 if (data->real_coordinates) {
2195 pos = getRealCoordinateBasePos(v_pos);
2196 geom = getRealCoordinateGeometry(v_geom);
2198 pos = getElementBasePos(&v_pos);
2199 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
2200 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
2203 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
2205 if(!data->explicit_size)
2206 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
2208 IItemDefManager *idef = m_client->idef();
2210 item.deSerialize(item_name, idef);
2213 TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
2214 m_default_tooltip_bgcolor,
2215 m_default_tooltip_color);
2217 // the spec for the button
2220 utf8_to_wide(label),
2221 utf8_to_wide(item_name),
2222 258 + m_fields.size(),
2226 GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment,
2227 rect, m_tsrc, data->current_parent, spec_btn.fid, spec_btn.flabel.c_str(),
2228 item_name, m_client);
2230 auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
2232 spec_btn.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
2234 e_btn->setStyles(style);
2236 if (spec_btn.fname == m_focused_element) {
2237 Environment->setFocus(e_btn);
2240 spec_btn.ftype = f_Button;
2241 rect += data->basepos-padding;
2242 spec_btn.rect = rect;
2243 m_fields.push_back(spec_btn);
2246 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
2249 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
2251 std::vector<std::string> parts = split(element, ';');
2253 if ((parts.size() == 3) ||
2254 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
2256 std::vector<std::string> v_pos = split(parts[0], ',');
2257 std::vector<std::string> v_geom = split(parts[1], ',');
2259 MY_CHECKPOS("box", 0);
2260 MY_CHECKGEOM("box", 1);
2265 if (data->real_coordinates) {
2266 pos = getRealCoordinateBasePos(v_pos);
2267 geom = getRealCoordinateGeometry(v_geom);
2269 pos = getElementBasePos(&v_pos);
2270 geom.X = stof(v_geom[0]) * spacing.X;
2271 geom.Y = stof(v_geom[1]) * spacing.Y;
2278 258 + m_fields.size(),
2283 auto style = getDefaultStyleForElement("box", spec.fname);
2285 video::SColor tmp_color;
2286 std::array<video::SColor, 4> colors;
2287 std::array<video::SColor, 4> bordercolors = {0x0, 0x0, 0x0, 0x0};
2288 std::array<s32, 4> borderwidths = {0, 0, 0, 0};
2290 if (parseColorString(parts[2], tmp_color, true, 0x8C)) {
2291 colors = {tmp_color, tmp_color, tmp_color, tmp_color};
2293 colors = style.getColorArray(StyleSpec::COLORS, {0x0, 0x0, 0x0, 0x0});
2294 bordercolors = style.getColorArray(StyleSpec::BORDERCOLORS,
2295 {0x0, 0x0, 0x0, 0x0});
2296 borderwidths = style.getIntArray(StyleSpec::BORDERWIDTHS, {0, 0, 0, 0});
2299 core::rect<s32> rect(pos, pos + geom);
2301 GUIBox *e = new GUIBox(Environment, data->current_parent, spec.fid, rect,
2302 colors, bordercolors, borderwidths);
2303 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
2306 m_fields.push_back(spec);
2309 errorstream << "Invalid Box element(" << parts.size() << "): '" << element
2310 << "'" << std::endl;
2313 void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
2315 std::vector<std::string> parts = split(element,';');
2316 const u32 parameter_count = parts.size();
2318 if ((parameter_count > 2 && m_formspec_version < 3) ||
2319 (parameter_count > 3 && m_formspec_version <= FORMSPEC_API_VERSION)) {
2320 errorstream << "Invalid bgcolor element(" << parameter_count << "): '"
2321 << element << "'" << std::endl;
2326 if (parameter_count >= 1 && parts[0] != "")
2327 parseColorString(parts[0], m_bgcolor, false);
2330 if (parameter_count >= 2) {
2331 if (parts[1] == "both") {
2332 m_bgnonfullscreen = true;
2333 m_bgfullscreen = true;
2334 } else if (parts[1] == "neither") {
2335 m_bgnonfullscreen = false;
2336 m_bgfullscreen = false;
2337 } else if (parts[1] != "" || m_formspec_version < 3) {
2338 m_bgfullscreen = is_yes(parts[1]);
2339 m_bgnonfullscreen = !m_bgfullscreen;
2344 if (parameter_count >= 3 && parts[2] != "")
2345 parseColorString(parts[2], m_fullscreen_bgcolor, false);
2348 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
2350 std::vector<std::string> parts = split(element,';');
2352 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
2353 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2355 parseColorString(parts[0], data->inventorylist_options.slotbg_n, false);
2356 parseColorString(parts[1], data->inventorylist_options.slotbg_h, false);
2358 if (parts.size() >= 3) {
2359 if (parseColorString(parts[2], data->inventorylist_options.slotbordercolor,
2361 data->inventorylist_options.slotborder = true;
2364 if (parts.size() == 5) {
2365 video::SColor tmp_color;
2367 if (parseColorString(parts[3], tmp_color, false))
2368 m_default_tooltip_bgcolor = tmp_color;
2369 if (parseColorString(parts[4], tmp_color, false))
2370 m_default_tooltip_color = tmp_color;
2373 // update all already parsed inventorylists
2374 for (GUIInventoryList *e : m_inventorylists) {
2375 e->setSlotBGColors(data->inventorylist_options.slotbg_n,
2376 data->inventorylist_options.slotbg_h);
2377 e->setSlotBorders(data->inventorylist_options.slotborder,
2378 data->inventorylist_options.slotbordercolor);
2382 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
2385 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
2387 std::vector<std::string> parts = split(element,';');
2388 if (parts.size() < 2) {
2389 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2390 << element << "'" << std::endl;
2394 // Get mode and check size
2395 bool rect_mode = parts[0].find(',') != std::string::npos;
2396 size_t base_size = rect_mode ? 3 : 2;
2397 if (parts.size() != base_size && parts.size() != base_size + 2) {
2398 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2399 << element << "'" << std::endl;
2404 video::SColor bgcolor = m_default_tooltip_bgcolor;
2405 video::SColor color = m_default_tooltip_color;
2406 if (parts.size() == base_size + 2 &&
2407 (!parseColorString(parts[base_size], bgcolor, false) ||
2408 !parseColorString(parts[base_size + 1], color, false))) {
2409 errorstream << "Invalid color in tooltip element(" << parts.size()
2410 << "): '" << element << "'" << std::endl;
2414 // Make tooltip spec
2415 std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
2416 TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
2420 std::vector<std::string> v_pos = split(parts[0], ',');
2421 std::vector<std::string> v_geom = split(parts[1], ',');
2423 MY_CHECKPOS("tooltip", 0);
2424 MY_CHECKGEOM("tooltip", 1);
2429 if (data->real_coordinates) {
2430 pos = getRealCoordinateBasePos(v_pos);
2431 geom = getRealCoordinateGeometry(v_geom);
2433 pos = getElementBasePos(&v_pos);
2434 geom.X = stof(v_geom[0]) * spacing.X;
2435 geom.Y = stof(v_geom[1]) * spacing.Y;
2438 FieldSpec fieldspec(
2442 258 + m_fields.size()
2445 core::rect<s32> rect(pos, pos + geom);
2447 gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
2448 data->current_parent, fieldspec.fid, rect);
2450 // the element the rect tooltip is bound to should not block mouse-clicks
2451 e->setVisible(false);
2453 m_fields.push_back(fieldspec);
2454 m_tooltip_rects.emplace_back(e, spec);
2457 m_tooltips[parts[0]] = spec;
2461 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
2467 std::vector<std::string> parts = split(data,'[');
2469 if (parts.size() < 2) {
2473 if (trim(parts[0]) != "formspec_version") {
2477 if (is_number(parts[1])) {
2478 m_formspec_version = mystoi(parts[1]);
2485 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
2487 if (element.empty())
2490 std::vector<std::string> parts = split(element,'[');
2492 if (parts.size() < 2)
2495 std::string type = trim(parts[0]);
2496 std::string description = trim(parts[1]);
2498 if (type != "size" && type != "invsize")
2501 if (type == "invsize")
2502 warningstream << "Deprecated formspec element \"invsize\" is used" << std::endl;
2504 parseSize(data, description);
2509 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
2511 if (element.empty())
2514 std::vector<std::string> parts = split(element, '[');
2516 if (parts.size() != 2)
2519 std::string type = trim(parts[0]);
2520 std::string description = trim(parts[1]);
2522 if (type != "position")
2525 parsePosition(data, description);
2530 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
2532 std::vector<std::string> parts = split(element, ',');
2534 if (parts.size() == 2) {
2535 data->offset.X = stof(parts[0]);
2536 data->offset.Y = stof(parts[1]);
2540 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
2543 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
2545 if (element.empty())
2548 std::vector<std::string> parts = split(element, '[');
2550 if (parts.size() != 2)
2553 std::string type = trim(parts[0]);
2554 std::string description = trim(parts[1]);
2556 if (type != "anchor")
2559 parseAnchor(data, description);
2564 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
2566 std::vector<std::string> parts = split(element, ',');
2568 if (parts.size() == 2) {
2569 data->anchor.X = stof(parts[0]);
2570 data->anchor.Y = stof(parts[1]);
2574 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
2575 << "'" << std::endl;
2578 bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type)
2580 std::vector<std::string> parts = split(element, ';');
2582 if (parts.size() < 2) {
2583 errorstream << "Invalid style element (" << parts.size() << "): '" << element
2584 << "'" << std::endl;
2591 for (size_t i = 1; i < parts.size(); i++) {
2592 size_t equal_pos = parts[i].find('=');
2593 if (equal_pos == std::string::npos) {
2594 errorstream << "Invalid style element (Property missing value): '" << element
2595 << "'" << std::endl;
2599 std::string propname = trim(parts[i].substr(0, equal_pos));
2600 std::string value = trim(unescape_string(parts[i].substr(equal_pos + 1)));
2602 std::transform(propname.begin(), propname.end(), propname.begin(), ::tolower);
2604 StyleSpec::Property prop = StyleSpec::GetPropertyByName(propname);
2605 if (prop == StyleSpec::NONE) {
2606 if (property_warned.find(propname) != property_warned.end()) {
2607 warningstream << "Invalid style element (Unknown property " << propname << "): '"
2609 << "'" << std::endl;
2610 property_warned.insert(propname);
2615 spec.set(prop, value);
2618 std::vector<std::string> selectors = split(parts[0], ',');
2619 for (size_t sel = 0; sel < selectors.size(); sel++) {
2620 std::string selector = trim(selectors[sel]);
2622 // Copy the style properties to a new StyleSpec
2623 // This allows a separate state mask per-selector
2624 StyleSpec selector_spec = spec;
2626 // Parse state information, if it exists
2627 bool state_valid = true;
2628 size_t state_pos = selector.find(':');
2629 if (state_pos != std::string::npos) {
2630 std::string state_str = selector.substr(state_pos + 1);
2631 selector = selector.substr(0, state_pos);
2633 if (state_str.empty()) {
2634 errorstream << "Invalid style element (Invalid state): '" << element
2635 << "'" << std::endl;
2636 state_valid = false;
2638 std::vector<std::string> states = split(state_str, '+');
2639 for (std::string &state : states) {
2640 StyleSpec::State converted = StyleSpec::getStateByName(state);
2641 if (converted == StyleSpec::STATE_INVALID) {
2642 infostream << "Unknown style state " << state <<
2643 " in element '" << element << "'" << std::endl;
2644 state_valid = false;
2648 selector_spec.addState(converted);
2654 // Skip this selector
2659 theme_by_type[selector].push_back(selector_spec);
2661 theme_by_name[selector].push_back(selector_spec);
2664 // Backwards-compatibility for existing _hovered/_pressed properties
2665 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_HOVERED)
2666 || selector_spec.hasProperty(StyleSpec::BGIMG_HOVERED)
2667 || selector_spec.hasProperty(StyleSpec::FGIMG_HOVERED)) {
2668 StyleSpec hover_spec;
2669 hover_spec.addState(StyleSpec::STATE_HOVERED);
2671 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_HOVERED)) {
2672 hover_spec.set(StyleSpec::BGCOLOR, selector_spec.get(StyleSpec::BGCOLOR_HOVERED, ""));
2674 if (selector_spec.hasProperty(StyleSpec::BGIMG_HOVERED)) {
2675 hover_spec.set(StyleSpec::BGIMG, selector_spec.get(StyleSpec::BGIMG_HOVERED, ""));
2677 if (selector_spec.hasProperty(StyleSpec::FGIMG_HOVERED)) {
2678 hover_spec.set(StyleSpec::FGIMG, selector_spec.get(StyleSpec::FGIMG_HOVERED, ""));
2682 theme_by_type[selector].push_back(hover_spec);
2684 theme_by_name[selector].push_back(hover_spec);
2687 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_PRESSED)
2688 || selector_spec.hasProperty(StyleSpec::BGIMG_PRESSED)
2689 || selector_spec.hasProperty(StyleSpec::FGIMG_PRESSED)) {
2690 StyleSpec press_spec;
2691 press_spec.addState(StyleSpec::STATE_PRESSED);
2693 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_PRESSED)) {
2694 press_spec.set(StyleSpec::BGCOLOR, selector_spec.get(StyleSpec::BGCOLOR_PRESSED, ""));
2696 if (selector_spec.hasProperty(StyleSpec::BGIMG_PRESSED)) {
2697 press_spec.set(StyleSpec::BGIMG, selector_spec.get(StyleSpec::BGIMG_PRESSED, ""));
2699 if (selector_spec.hasProperty(StyleSpec::FGIMG_PRESSED)) {
2700 press_spec.set(StyleSpec::FGIMG, selector_spec.get(StyleSpec::FGIMG_PRESSED, ""));
2704 theme_by_type[selector].push_back(press_spec);
2706 theme_by_name[selector].push_back(press_spec);
2714 void GUIFormSpecMenu::parseSetFocus(const std::string &element)
2716 std::vector<std::string> parts = split(element, ';');
2718 if (parts.size() <= 2 ||
2719 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION))
2721 if (m_is_form_regenerated)
2722 return; // Never focus on resizing
2724 bool force_focus = parts.size() >= 2 && is_yes(parts[1]);
2725 if (force_focus || m_text_dst->m_formname != m_last_formname)
2731 errorstream << "Invalid set_focus element (" << parts.size() << "): '" << element
2732 << "'" << std::endl;
2735 void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
2737 std::vector<std::string> parts = split(element, ';');
2739 if (parts.size() < 5 || (parts.size() > 10 &&
2740 m_formspec_version <= FORMSPEC_API_VERSION)) {
2741 errorstream << "Invalid model element (" << parts.size() << "): '" << element
2742 << "'" << std::endl;
2746 // Avoid length checks by resizing
2747 if (parts.size() < 10)
2750 std::vector<std::string> v_pos = split(parts[0], ',');
2751 std::vector<std::string> v_geom = split(parts[1], ',');
2752 std::string name = unescape_string(parts[2]);
2753 std::string meshstr = unescape_string(parts[3]);
2754 std::vector<std::string> textures = split(parts[4], ',');
2755 std::vector<std::string> vec_rot = split(parts[5], ',');
2756 bool inf_rotation = is_yes(parts[6]);
2757 bool mousectrl = is_yes(parts[7]) || parts[7].empty(); // default true
2758 std::vector<std::string> frame_loop = split(parts[8], ',');
2759 std::string speed = unescape_string(parts[9]);
2761 MY_CHECKPOS("model", 0);
2762 MY_CHECKGEOM("model", 1);
2767 if (data->real_coordinates) {
2768 pos = getRealCoordinateBasePos(v_pos);
2769 geom = getRealCoordinateGeometry(v_geom);
2771 pos = getElementBasePos(&v_pos);
2772 geom.X = stof(v_geom[0]) * (float)imgsize.X;
2773 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
2776 if (!data->explicit_size)
2777 warningstream << "invalid use of model without a size[] element" << std::endl;
2779 scene::IAnimatedMesh *mesh = m_client->getMesh(meshstr);
2782 errorstream << "Invalid model element: Unable to load mesh:"
2783 << std::endl << "\t" << meshstr << std::endl;
2791 258 + m_fields.size()
2794 core::rect<s32> rect(pos, pos + geom);
2796 GUIScene *e = new GUIScene(Environment, m_client->getSceneManager(),
2797 data->current_parent, rect, spec.fid);
2799 auto meshnode = e->setMesh(mesh);
2801 for (u32 i = 0; i < textures.size() && i < meshnode->getMaterialCount(); ++i)
2802 e->setTexture(i, m_tsrc->getTexture(unescape_string(textures[i])));
2804 if (vec_rot.size() >= 2)
2805 e->setRotation(v2f(stof(vec_rot[0]), stof(vec_rot[1])));
2807 e->enableContinuousRotation(inf_rotation);
2808 e->enableMouseControl(mousectrl);
2810 s32 frame_loop_begin = 0;
2811 s32 frame_loop_end = 0x7FFFFFFF;
2813 if (frame_loop.size() == 2) {
2814 frame_loop_begin = stoi(frame_loop[0]);
2815 frame_loop_end = stoi(frame_loop[1]);
2818 e->setFrameLoop(frame_loop_begin, frame_loop_end);
2819 e->setAnimationSpeed(stof(speed));
2821 auto style = getStyleForElement("model", spec.fname);
2822 e->setStyles(style);
2825 m_fields.push_back(spec);
2828 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
2831 if (element.empty())
2834 if (parseVersionDirect(element))
2837 size_t pos = element.find('[');
2838 if (pos == std::string::npos)
2841 std::string type = trim(element.substr(0, pos));
2842 std::string description = element.substr(pos+1);
2844 if (type == "container") {
2845 parseContainer(data, description);
2849 if (type == "container_end") {
2850 parseContainerEnd(data);
2854 if (type == "list") {
2855 parseList(data, description);
2859 if (type == "listring") {
2860 parseListRing(data, description);
2864 if (type == "checkbox") {
2865 parseCheckbox(data, description);
2869 if (type == "image") {
2870 parseImage(data, description);
2874 if (type == "animated_image") {
2875 parseAnimatedImage(data, description);
2879 if (type == "item_image") {
2880 parseItemImage(data, description);
2884 if (type == "button" || type == "button_exit") {
2885 parseButton(data, description, type);
2889 if (type == "background" || type == "background9") {
2890 parseBackground(data, description);
2894 if (type == "tableoptions"){
2895 parseTableOptions(data,description);
2899 if (type == "tablecolumns"){
2900 parseTableColumns(data,description);
2904 if (type == "table"){
2905 parseTable(data,description);
2909 if (type == "textlist"){
2910 parseTextList(data,description);
2914 if (type == "dropdown"){
2915 parseDropDown(data,description);
2919 if (type == "field_close_on_enter") {
2920 parseFieldCloseOnEnter(data, description);
2924 if (type == "pwdfield") {
2925 parsePwdField(data,description);
2929 if ((type == "field") || (type == "textarea")){
2930 parseField(data,description,type);
2934 if (type == "hypertext") {
2935 parseHyperText(data,description);
2939 if (type == "label") {
2940 parseLabel(data,description);
2944 if (type == "vertlabel") {
2945 parseVertLabel(data,description);
2949 if (type == "item_image_button") {
2950 parseItemImageButton(data,description);
2954 if ((type == "image_button") || (type == "image_button_exit")) {
2955 parseImageButton(data,description,type);
2959 if (type == "tabheader") {
2960 parseTabHeader(data,description);
2964 if (type == "box") {
2965 parseBox(data,description);
2969 if (type == "bgcolor") {
2970 parseBackgroundColor(data,description);
2974 if (type == "listcolors") {
2975 parseListColors(data,description);
2979 if (type == "tooltip") {
2980 parseTooltip(data,description);
2984 if (type == "scrollbar") {
2985 parseScrollBar(data, description);
2989 if (type == "real_coordinates") {
2990 data->real_coordinates = is_yes(description);
2994 if (type == "style") {
2995 parseStyle(data, description, false);
2999 if (type == "style_type") {
3000 parseStyle(data, description, true);
3004 if (type == "scrollbaroptions") {
3005 parseScrollBarOptions(data, description);
3009 if (type == "scroll_container") {
3010 parseScrollContainer(data, description);
3014 if (type == "scroll_container_end") {
3015 parseScrollContainerEnd(data);
3019 if (type == "set_focus") {
3020 parseSetFocus(description);
3024 if (type == "model") {
3025 parseModel(data, description);
3030 infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
3034 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
3036 // Useless to regenerate without a screensize
3037 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
3043 // Preserve stuff only on same form, not on a new form.
3044 if (m_text_dst->m_formname == m_last_formname) {
3045 // Preserve tables/textlists
3046 for (auto &m_table : m_tables) {
3047 std::string tablename = m_table.first.fname;
3048 GUITable *table = m_table.second;
3049 mydata.table_dyndata[tablename] = table->getDynamicData();
3053 gui::IGUIElement *focused_element = Environment->getFocus();
3054 if (focused_element && focused_element->getParent() == this) {
3055 s32 focused_id = focused_element->getID();
3056 if (focused_id > 257) {
3057 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
3058 if (field.fid == focused_id) {
3059 m_focused_element = field.fname;
3066 // Don't keep old focus value
3067 m_focused_element = "";
3073 for (auto &table_it : m_tables)
3074 table_it.second->drop();
3075 for (auto &inventorylist_it : m_inventorylists)
3076 inventorylist_it->drop();
3077 for (auto &checkbox_it : m_checkboxes)
3078 checkbox_it.second->drop();
3079 for (auto &scrollbar_it : m_scrollbars)
3080 scrollbar_it.second->drop();
3081 for (auto &background_it : m_backgrounds)
3082 background_it->drop();
3083 for (auto &tooltip_rect_it : m_tooltip_rects)
3084 tooltip_rect_it.first->drop();
3085 for (auto &clickthrough_it : m_clickthrough_elements)
3086 clickthrough_it->drop();
3087 for (auto &scroll_container_it : m_scroll_containers)
3088 scroll_container_it.second->drop();
3090 mydata.size = v2s32(100, 100);
3091 mydata.screensize = screensize;
3092 mydata.offset = v2f32(0.5f, 0.5f);
3093 mydata.anchor = v2f32(0.5f, 0.5f);
3094 mydata.simple_field_count = 0;
3096 // Base position of contents of form
3097 mydata.basepos = getBasePos();
3099 // the parent for the parsed elements
3100 mydata.current_parent = this;
3102 m_inventorylists.clear();
3103 m_backgrounds.clear();
3105 m_checkboxes.clear();
3106 m_scrollbars.clear();
3109 m_tooltip_rects.clear();
3110 m_inventory_rings.clear();
3111 m_dropdowns.clear();
3112 m_scroll_containers.clear();
3113 theme_by_name.clear();
3114 theme_by_type.clear();
3115 m_clickthrough_elements.clear();
3116 field_close_on_enter.clear();
3117 m_dropdown_index_event.clear();
3119 m_bgnonfullscreen = true;
3120 m_bgfullscreen = false;
3122 m_formspec_version = 1;
3125 v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
3126 m_bgcolor = video::SColor(
3127 (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
3128 clamp_u8(myround(formspec_bgcolor.X)),
3129 clamp_u8(myround(formspec_bgcolor.Y)),
3130 clamp_u8(myround(formspec_bgcolor.Z))
3135 v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
3136 m_fullscreen_bgcolor = video::SColor(
3137 (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
3138 clamp_u8(myround(formspec_bgcolor.X)),
3139 clamp_u8(myround(formspec_bgcolor.Y)),
3140 clamp_u8(myround(formspec_bgcolor.Z))
3144 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
3145 m_default_tooltip_color = video::SColor(255,255,255,255);
3149 assert(!m_tooltip_element);
3150 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
3151 m_tooltip_element = gui::StaticText::add(Environment, L"",
3152 core::rect<s32>(0, 0, 110, 18));
3153 m_tooltip_element->enableOverrideColor(true);
3154 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
3155 m_tooltip_element->setDrawBackground(true);
3156 m_tooltip_element->setDrawBorder(true);
3157 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
3158 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
3159 m_tooltip_element->setWordWrap(false);
3160 //we're not parent so no autograb for this one!
3161 m_tooltip_element->grab();
3164 std::vector<std::string> elements = split(m_formspec_string,']');
3167 /* try to read version from first element only */
3168 if (!elements.empty()) {
3169 if (parseVersionDirect(elements[0])) {
3174 /* we need size first in order to calculate image scale */
3175 mydata.explicit_size = false;
3176 for (; i< elements.size(); i++) {
3177 if (!parseSizeDirect(&mydata, elements[i])) {
3182 /* "position" element is always after "size" element if it used */
3183 for (; i< elements.size(); i++) {
3184 if (!parsePositionDirect(&mydata, elements[i])) {
3189 /* "anchor" element is always after "position" (or "size" element) if it used */
3190 for (; i< elements.size(); i++) {
3191 if (!parseAnchorDirect(&mydata, elements[i])) {
3196 /* "no_prepend" element is always after "position" (or "size" element) if it used */
3197 bool enable_prepends = true;
3198 for (; i < elements.size(); i++) {
3199 if (elements[i].empty())
3202 std::vector<std::string> parts = split(elements[i], '[');
3203 if (trim(parts[0]) == "no_prepend")
3204 enable_prepends = false;
3209 /* Copy of the "real_coordinates" element for after the form size. */
3210 mydata.real_coordinates = m_formspec_version >= 2;
3211 for (; i < elements.size(); i++) {
3212 std::vector<std::string> parts = split(elements[i], '[');
3213 std::string name = trim(parts[0]);
3214 if (name != "real_coordinates" || parts.size() != 2)
3215 break; // Invalid format
3217 mydata.real_coordinates = is_yes(trim(parts[1]));
3220 if (mydata.explicit_size) {
3221 // compute scaling for specified form size
3223 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
3224 v2u32 delta = current_screensize - m_lockscreensize;
3226 if (current_screensize.Y > m_lockscreensize.Y)
3231 if (current_screensize.X > m_lockscreensize.X)
3236 offset = v2s32(delta.X,delta.Y);
3238 mydata.screensize = m_lockscreensize;
3240 offset = v2s32(0,0);
3243 double gui_scaling = g_settings->getFloat("gui_scaling");
3244 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
3248 // In fixed-size mode, inventory image size
3249 // is 0.53 inch multiplied by the gui_scaling
3250 // config parameter. This magic size is chosen
3251 // to make the main menu (15.5 inventory images
3252 // wide, including border) just fit into the
3253 // default window (800 pixels wide) at 96 DPI
3254 // and default scaling (1.00).
3255 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
3257 // Variables for the maximum imgsize that can fit in the screen.
3258 double fitx_imgsize;
3259 double fity_imgsize;
3261 // Pad the screensize with 5% of the screensize on all sides to ensure
3262 // that even the largest formspecs don't touch the screen borders.
3263 v2f padded_screensize(
3264 mydata.screensize.X * 0.9f,
3265 mydata.screensize.Y * 0.9f
3268 if (mydata.real_coordinates) {
3269 fitx_imgsize = padded_screensize.X / mydata.invsize.X;
3270 fity_imgsize = padded_screensize.Y / mydata.invsize.Y;
3272 // The maximum imgsize in the old coordinate system also needs to
3273 // factor in padding and spacing along with 0.1 inventory slot spare
3274 // and help text space, hence the magic numbers.
3275 fitx_imgsize = padded_screensize.X /
3276 ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
3277 fity_imgsize = padded_screensize.Y /
3278 ((15.0 / 13.0) * (0.85 + mydata.invsize.Y));
3282 // In Android, the preferred imgsize should be larger to accommodate the
3283 // smaller screensize.
3284 double prefer_imgsize = padded_screensize.Y / 10 * gui_scaling;
3286 // Desktop computers have more space, so try to fit 15 coordinates.
3287 double prefer_imgsize = padded_screensize.Y / 15 * gui_scaling;
3289 // Try to use the preferred imgsize, but if that's bigger than the maximum
3290 // size, use the maximum size.
3291 use_imgsize = std::min(prefer_imgsize,
3292 std::min(fitx_imgsize, fity_imgsize));
3295 // Everything else is scaled in proportion to the
3296 // inventory image size. The inventory slot spacing
3297 // is 5/4 image size horizontally and 15/13 image size
3298 // vertically. The padding around the form (incorporating
3299 // the border of the outer inventory slots) is 3/8
3300 // image size. Font height (baseline to baseline)
3301 // is 2/5 vertical inventory slot spacing, and button
3302 // half-height is 7/8 of font height.
3303 imgsize = v2s32(use_imgsize, use_imgsize);
3304 spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
3305 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
3306 m_btn_height = use_imgsize*15.0/13 * 0.35;
3308 m_font = g_fontengine->getFont();
3310 if (mydata.real_coordinates) {
3311 mydata.size = v2s32(
3312 mydata.invsize.X*imgsize.X,
3313 mydata.invsize.Y*imgsize.Y
3316 mydata.size = v2s32(
3317 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
3318 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
3322 DesiredRect = mydata.rect = core::rect<s32>(
3323 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
3324 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
3325 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
3326 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
3329 // Non-size[] form must consist only of text fields and
3330 // implicit "Proceed" button. Use default font, and
3331 // temporary form size which will be recalculated below.
3332 m_font = g_fontengine->getFont();
3333 m_btn_height = font_line_height(m_font) * 0.875;
3334 DesiredRect = core::rect<s32>(
3335 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
3336 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
3337 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
3338 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
3341 recalculateAbsolutePosition(false);
3342 mydata.basepos = getBasePos();
3343 m_tooltip_element->setOverrideFont(m_font);
3345 gui::IGUISkin *skin = Environment->getSkin();
3347 gui::IGUIFont *old_font = skin->getFont();
3348 skin->setFont(m_font);
3350 pos_offset = v2f32();
3352 // used for formspec versions < 3
3353 core::list<IGUIElement *>::Iterator legacy_sort_start = Children.getLast();
3355 if (enable_prepends) {
3356 // Backup the coordinates so that prepends can use the coordinates of choice.
3357 bool rc_backup = mydata.real_coordinates;
3358 u16 version_backup = m_formspec_version;
3359 mydata.real_coordinates = false; // Old coordinates by default.
3361 std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
3362 for (const auto &element : prepend_elements)
3363 parseElement(&mydata, element);
3365 // legacy sorting for formspec versions < 3
3366 if (m_formspec_version >= 3)
3367 // prepends do not need to be reordered
3368 legacy_sort_start = Children.getLast();
3369 else if (version_backup >= 3)
3370 // only prepends elements have to be reordered
3371 legacySortElements(legacy_sort_start);
3373 m_formspec_version = version_backup;
3374 mydata.real_coordinates = rc_backup; // Restore coordinates
3377 for (; i< elements.size(); i++) {
3378 parseElement(&mydata, elements[i]);
3381 if (mydata.current_parent != this) {
3382 errorstream << "Invalid formspec string: scroll_container was never closed!"
3384 } else if (!container_stack.empty()) {
3385 errorstream << "Invalid formspec string: container was never closed!"
3389 // get the scrollbar elements for scroll_containers
3390 for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers) {
3391 for (const std::pair<FieldSpec, GUIScrollBar *> &b : m_scrollbars) {
3392 if (c.first == b.first.fname) {
3393 c.second->setScrollBar(b.second);
3399 // If there are fields without explicit size[], add a "Proceed"
3400 // button and adjust size to fit all the fields.
3401 if (mydata.simple_field_count > 0 && !mydata.explicit_size) {
3402 mydata.rect = core::rect<s32>(
3403 mydata.screensize.X / 2 - 580 / 2,
3404 mydata.screensize.Y / 2 - 300 / 2,
3405 mydata.screensize.X / 2 + 580 / 2,
3406 mydata.screensize.Y / 2 + 240 / 2 + mydata.simple_field_count * 60
3409 DesiredRect = mydata.rect;
3410 recalculateAbsolutePosition(false);
3411 mydata.basepos = getBasePos();
3414 v2s32 pos = mydata.basepos;
3415 pos.Y = (mydata.simple_field_count + 2) * 60;
3417 v2s32 size = DesiredRect.getSize();
3418 mydata.rect = core::rect<s32>(
3419 size.X / 2 - 70, pos.Y,
3420 size.X / 2 - 70 + 140, pos.Y + m_btn_height * 2
3422 const wchar_t *text = wgettext("Proceed");
3423 GUIButton::addButton(Environment, mydata.rect, m_tsrc, this, 257, text);
3428 // Set initial focus if parser didn't set it
3429 gui::IGUIElement *focused_element = Environment->getFocus();
3430 if (!focused_element
3431 || !isMyChild(focused_element)
3432 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
3435 skin->setFont(old_font);
3438 if (m_formspec_version < 3)
3439 legacySortElements(legacy_sort_start);
3441 // Formname and regeneration setting
3442 if (!m_is_form_regenerated) {
3443 // Only set previous form name if we purposefully showed a new formspec
3444 m_last_formname = m_text_dst->m_formname;
3445 m_is_form_regenerated = true;
3449 void GUIFormSpecMenu::legacySortElements(core::list<IGUIElement *>::Iterator from)
3452 Draw order for formspec_version <= 2:
3456 0 All other elements
3458 2 item_image, item_image_button
3463 if (from == Children.end())
3464 from = Children.begin();
3468 core::list<IGUIElement *>::Iterator to = Children.end();
3469 // 1: Copy into a sortable container
3470 std::vector<IGUIElement *> elements;
3471 for (auto it = from; it != to; ++it)
3472 elements.emplace_back(*it);
3474 // 2: Sort the container
3475 std::stable_sort(elements.begin(), elements.end(),
3476 [this] (const IGUIElement *a, const IGUIElement *b) -> bool {
3477 const FieldSpec *spec_a = getSpecByID(a->getID());
3478 const FieldSpec *spec_b = getSpecByID(b->getID());
3479 return spec_a && spec_b &&
3480 spec_a->priority < spec_b->priority;
3483 // 3: Re-assign the pointers
3484 for (auto e : elements) {
3491 bool GUIFormSpecMenu::getAndroidUIInput()
3493 if (!hasAndroidUIInput())
3497 if (porting::getInputDialogState() == -1)
3500 std::string fieldname = m_jni_field_name;
3501 m_jni_field_name.clear();
3503 for (const FieldSpec &field : m_fields) {
3504 if (field.fname != fieldname)
3507 IGUIElement *element = getElementFromId(field.fid, true);
3509 if (!element || element->getType() != irr::gui::EGUIET_EDIT_BOX)
3512 std::string text = porting::getInputDialogValue();
3513 ((gui::IGUIEditBox *)element)->setText(utf8_to_wide(text).c_str());
3519 GUIInventoryList::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
3521 for (const GUIInventoryList *e : m_inventorylists) {
3522 s32 item_index = e->getItemIndexAtPos(p);
3523 if (item_index != -1)
3524 return GUIInventoryList::ItemSpec(e->getInventoryloc(), e->getListname(),
3528 return GUIInventoryList::ItemSpec(InventoryLocation(), "", -1);
3531 void GUIFormSpecMenu::drawSelectedItem()
3533 video::IVideoDriver* driver = Environment->getVideoDriver();
3535 if (!m_selected_item) {
3536 // reset rotation time
3537 drawItemStack(driver, m_font, ItemStack(),
3538 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)), NULL,
3539 m_client, IT_ROT_DRAGGED);
3543 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3545 InventoryList *list = inv->getList(m_selected_item->listname);
3547 ItemStack stack = list->getItem(m_selected_item->i);
3548 stack.count = m_selected_amount;
3550 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
3551 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
3552 rect.constrainTo(driver->getViewPort());
3553 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
3556 void GUIFormSpecMenu::drawMenu()
3559 const std::string &newform = m_form_src->getForm();
3560 if (newform != m_formspec_string) {
3561 m_formspec_string = newform;
3562 m_is_form_regenerated = false;
3563 regenerateGui(m_screensize_old);
3567 gui::IGUISkin* skin = Environment->getSkin();
3568 sanity_check(skin != NULL);
3569 gui::IGUIFont *old_font = skin->getFont();
3570 skin->setFont(m_font);
3572 m_hovered_item_tooltips.clear();
3574 updateSelectedItem();
3576 video::IVideoDriver* driver = Environment->getVideoDriver();
3579 Draw background color
3581 v2u32 screenSize = driver->getScreenSize();
3582 core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
3585 driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
3586 if (m_bgnonfullscreen)
3587 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
3590 Draw rect_mode tooltip
3592 m_tooltip_element->setVisible(false);
3594 for (const auto &pair : m_tooltip_rects) {
3595 const core::rect<s32> &rect = pair.first->getAbsoluteClippingRect();
3596 if (rect.getArea() > 0 && rect.isPointInside(m_pointer)) {
3597 const std::wstring &text = pair.second.tooltip;
3598 if (!text.empty()) {
3599 showTooltip(text, pair.second.color, pair.second.bgcolor);
3608 for (gui::IGUIElement *e : m_backgrounds) {
3609 e->setVisible(true);
3611 e->setVisible(false);
3614 // Some elements are only visible while being drawn
3615 for (gui::IGUIElement *e : m_clickthrough_elements)
3616 e->setVisible(true);
3619 This is where all the drawing happens.
3621 core::list<IGUIElement*>::Iterator it = Children.begin();
3622 for (; it != Children.end(); ++it)
3623 if ((*it)->isNotClipped() ||
3624 AbsoluteClippingRect.isRectCollided(
3625 (*it)->getAbsolutePosition()))
3628 for (gui::IGUIElement *e : m_clickthrough_elements)
3629 e->setVisible(false);
3631 // Draw hovered item tooltips
3632 for (const std::string &tooltip : m_hovered_item_tooltips) {
3633 showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
3634 m_default_tooltip_bgcolor);
3637 if (m_hovered_item_tooltips.empty()) {
3638 // reset rotation time
3639 drawItemStack(driver, m_font, ItemStack(),
3640 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
3641 NULL, m_client, IT_ROT_HOVERED);
3644 /* TODO find way to show tooltips on touchscreen */
3645 #ifndef HAVE_TOUCHSCREENGUI
3646 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
3650 Draw fields/buttons tooltips and update the mouse cursor
3652 gui::IGUIElement *hovered =
3653 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
3655 #ifndef HAVE_TOUCHSCREENGUI
3656 gui::ICursorControl *cursor_control = RenderingEngine::get_raw_device()->
3658 gui::ECURSOR_ICON current_cursor_icon = cursor_control->getActiveIcon();
3660 bool hovered_element_found = false;
3662 if (hovered != NULL) {
3664 core::rect<s32> rect = hovered->getAbsoluteClippingRect();
3665 driver->draw2DRectangle(0x22FFFF00, rect, &rect);
3668 s32 id = hovered->getID();
3671 m_old_tooltip_id = id;
3673 if (id == m_old_tooltip_id) {
3674 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
3676 m_hovered_time = porting::getTimeMs();
3677 m_old_tooltip_id = id;
3681 // Find and update the current tooltip and cursor icon
3683 for (const FieldSpec &field : m_fields) {
3685 if (field.fid != id)
3688 if (delta >= m_tooltip_show_delay) {
3689 const std::wstring &text = m_tooltips[field.fname].tooltip;
3691 showTooltip(text, m_tooltips[field.fname].color,
3692 m_tooltips[field.fname].bgcolor);
3695 #ifndef HAVE_TOUCHSCREENGUI
3696 if (field.ftype != f_HyperText && // Handled directly in guiHyperText
3697 current_cursor_icon != field.fcursor_icon)
3698 cursor_control->setActiveIcon(field.fcursor_icon);
3701 hovered_element_found = true;
3708 if (!hovered_element_found) {
3709 // no element is hovered
3710 #ifndef HAVE_TOUCHSCREENGUI
3711 if (current_cursor_icon != ECI_NORMAL)
3712 cursor_control->setActiveIcon(ECI_NORMAL);
3716 m_tooltip_element->draw();
3719 Draw dragged item stack
3723 skin->setFont(old_font);
3727 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
3728 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
3730 EnrichedString ntext(text);
3731 ntext.setDefaultColor(color);
3732 if (!ntext.hasBackground())
3733 ntext.setBackground(bgcolor);
3735 setStaticText(m_tooltip_element, ntext);
3737 // Tooltip size and offset
3738 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
3739 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
3741 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
3742 int tooltip_offset_x = m_btn_height;
3743 int tooltip_offset_y = m_btn_height;
3745 tooltip_offset_x *= 3;
3746 tooltip_offset_y = 0;
3747 if (m_pointer.X > (s32)screenSize.X / 2)
3748 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
3750 // Hide tooltip after ETIE_LEFT_UP
3751 if (m_pointer.X == 0)
3755 // Calculate and set the tooltip position
3756 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
3757 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
3758 if (tooltip_x + tooltip_width > (s32)screenSize.X)
3759 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
3760 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
3761 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
3763 m_tooltip_element->setRelativePosition(
3765 core::position2d<s32>(tooltip_x, tooltip_y),
3766 core::dimension2d<s32>(tooltip_width, tooltip_height)
3770 // Display the tooltip
3771 m_tooltip_element->setVisible(true);
3772 bringToFront(m_tooltip_element);
3775 void GUIFormSpecMenu::updateSelectedItem()
3777 verifySelectedItem();
3779 // If craftresult is nonempty and nothing else is selected, select it now.
3780 if (!m_selected_item) {
3781 for (const GUIInventoryList *e : m_inventorylists) {
3782 if (e->getListname() != "craftpreview")
3785 Inventory *inv = m_invmgr->getInventory(e->getInventoryloc());
3789 InventoryList *list = inv->getList("craftresult");
3791 if (!list || list->getSize() == 0)
3794 const ItemStack &item = list->getItem(0);
3798 // Grab selected item from the crafting result list
3799 m_selected_item = new GUIInventoryList::ItemSpec;
3800 m_selected_item->inventoryloc = e->getInventoryloc();
3801 m_selected_item->listname = "craftresult";
3802 m_selected_item->i = 0;
3803 m_selected_amount = item.count;
3804 m_selected_dragging = false;
3809 // If craftresult is selected, keep the whole stack selected
3810 if (m_selected_item && m_selected_item->listname == "craftresult")
3811 m_selected_amount = verifySelectedItem().count;
3814 ItemStack GUIFormSpecMenu::verifySelectedItem()
3816 // If the selected stack has become empty for some reason, deselect it.
3817 // If the selected stack has become inaccessible, deselect it.
3818 // If the selected stack has become smaller, adjust m_selected_amount.
3819 // Return the selected stack.
3821 if (m_selected_item) {
3822 if (m_selected_item->isValid()) {
3823 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3825 InventoryList *list = inv->getList(m_selected_item->listname);
3826 if (list && (u32) m_selected_item->i < list->getSize()) {
3827 ItemStack stack = list->getItem(m_selected_item->i);
3828 if (!m_selected_swap.empty()) {
3829 if (m_selected_swap.name == stack.name &&
3830 m_selected_swap.count == stack.count)
3831 m_selected_swap.clear();
3833 m_selected_amount = std::min(m_selected_amount, stack.count);
3842 // selection was not valid
3843 delete m_selected_item;
3844 m_selected_item = nullptr;
3845 m_selected_amount = 0;
3846 m_selected_dragging = false;
3851 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode)
3857 if (quitmode == quit_mode_accept) {
3858 fields["quit"] = "true";
3861 if (quitmode == quit_mode_cancel) {
3862 fields["quit"] = "true";
3863 m_text_dst->gotText(fields);
3867 if (current_keys_pending.key_down) {
3868 fields["key_down"] = "true";
3869 current_keys_pending.key_down = false;
3872 if (current_keys_pending.key_up) {
3873 fields["key_up"] = "true";
3874 current_keys_pending.key_up = false;
3877 if (current_keys_pending.key_enter) {
3878 fields["key_enter"] = "true";
3879 current_keys_pending.key_enter = false;
3882 if (!current_field_enter_pending.empty()) {
3883 fields["key_enter_field"] = current_field_enter_pending;
3884 current_field_enter_pending = "";
3887 if (current_keys_pending.key_escape) {
3888 fields["key_escape"] = "true";
3889 current_keys_pending.key_escape = false;
3892 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
3894 std::string name = s.fname;
3895 if (s.ftype == f_Button) {
3896 fields[name] = wide_to_utf8(s.flabel);
3897 } else if (s.ftype == f_Table) {
3898 GUITable *table = getTable(s.fname);
3900 fields[name] = table->checkEvent();
3902 } else if (s.ftype == f_DropDown) {
3903 // No dynamic cast possible due to some distributions shipped
3904 // without rtti support in Irrlicht
3905 IGUIElement *element = getElementFromId(s.fid, true);
3906 gui::IGUIComboBox *e = NULL;
3907 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
3908 e = static_cast<gui::IGUIComboBox *>(element);
3910 warningstream << "GUIFormSpecMenu::acceptInput: dropdown "
3911 << "field without dropdown element" << std::endl;
3914 s32 selected = e->getSelected();
3915 if (selected >= 0) {
3916 if (m_dropdown_index_event.find(s.fname) !=
3917 m_dropdown_index_event.end()) {
3918 fields[name] = std::to_string(selected + 1);
3920 std::vector<std::string> *dropdown_values =
3921 getDropDownValues(s.fname);
3922 if (dropdown_values && selected < (s32)dropdown_values->size())
3923 fields[name] = (*dropdown_values)[selected];
3926 } else if (s.ftype == f_TabHeader) {
3927 // No dynamic cast possible due to some distributions shipped
3928 // without rtti support in Irrlicht
3929 IGUIElement *element = getElementFromId(s.fid, true);
3930 gui::IGUITabControl *e = nullptr;
3931 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
3932 e = static_cast<gui::IGUITabControl *>(element);
3936 std::stringstream ss;
3937 ss << (e->getActiveTab() +1);
3938 fields[name] = ss.str();
3940 } else if (s.ftype == f_CheckBox) {
3941 // No dynamic cast possible due to some distributions shipped
3942 // without rtti support in Irrlicht
3943 IGUIElement *element = getElementFromId(s.fid, true);
3944 gui::IGUICheckBox *e = nullptr;
3945 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
3946 e = static_cast<gui::IGUICheckBox*>(element);
3951 fields[name] = "true";
3953 fields[name] = "false";
3955 } else if (s.ftype == f_ScrollBar) {
3956 // No dynamic cast possible due to some distributions shipped
3957 // without rtti support in Irrlicht
3958 IGUIElement *element = getElementFromId(s.fid, true);
3959 GUIScrollBar *e = nullptr;
3960 if (element && element->getType() == gui::EGUIET_ELEMENT)
3961 e = static_cast<GUIScrollBar *>(element);
3964 std::stringstream os;
3966 if (s.fdefault == L"Changed")
3967 fields[name] = "CHG:" + os.str();
3969 fields[name] = "VAL:" + os.str();
3971 } else if (s.ftype == f_AnimatedImage) {
3972 // No dynamic cast possible due to some distributions shipped
3973 // without rtti support in Irrlicht
3974 IGUIElement *element = getElementFromId(s.fid, true);
3975 GUIAnimatedImage *e = nullptr;
3976 if (element && element->getType() == gui::EGUIET_ELEMENT)
3977 e = static_cast<GUIAnimatedImage *>(element);
3980 fields[name] = std::to_string(e->getFrameIndex() + 1);
3982 IGUIElement *e = getElementFromId(s.fid, true);
3984 fields[name] = wide_to_utf8(e->getText());
3989 m_text_dst->gotText(fields);
3993 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
3995 // The IGUITabControl renders visually using the skin's selected
3996 // font, which we override for the duration of form drawing,
3997 // but computes tab hotspots based on how it would have rendered
3998 // using the font that is selected at the time of button release.
3999 // To make these two consistent, temporarily override the skin's
4000 // font while the IGUITabControl is processing the event.
4001 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
4002 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
4003 s32 x = event.MouseInput.X;
4004 s32 y = event.MouseInput.Y;
4005 gui::IGUIElement *hovered =
4006 Environment->getRootGUIElement()->getElementFromPoint(
4007 core::position2d<s32>(x, y));
4008 if (hovered && isMyChild(hovered) &&
4009 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
4010 gui::IGUISkin* skin = Environment->getSkin();
4011 sanity_check(skin != NULL);
4012 gui::IGUIFont *old_font = skin->getFont();
4013 skin->setFont(m_font);
4014 bool retval = hovered->OnEvent(event);
4015 skin->setFont(old_font);
4020 // Fix Esc/Return key being eaten by checkboxen and tables
4021 if (event.EventType == EET_KEY_INPUT_EVENT) {
4022 KeyPress kp(event.KeyInput);
4023 if (kp == EscapeKey || kp == CancelKey
4024 || kp == getKeySetting("keymap_inventory")
4025 || event.KeyInput.Key==KEY_RETURN) {
4026 gui::IGUIElement *focused = Environment->getFocus();
4027 if (focused && isMyChild(focused) &&
4028 (focused->getType() == gui::EGUIET_LIST_BOX ||
4029 focused->getType() == gui::EGUIET_CHECK_BOX) &&
4030 (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
4031 event.KeyInput.Key != KEY_RETURN)) {
4037 // Mouse wheel and move events: send to hovered element instead of focused
4038 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
4039 (event.MouseInput.Event == EMIE_MOUSE_WHEEL ||
4040 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
4041 event.MouseInput.ButtonStates == 0))) {
4042 s32 x = event.MouseInput.X;
4043 s32 y = event.MouseInput.Y;
4044 gui::IGUIElement *hovered =
4045 Environment->getRootGUIElement()->getElementFromPoint(
4046 core::position2d<s32>(x, y));
4047 if (hovered && isMyChild(hovered)) {
4048 hovered->OnEvent(event);
4049 return event.MouseInput.Event == EMIE_MOUSE_WHEEL;
4053 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
4054 /* TODO add a check like:
4055 if (event.JoystickEvent != joystick_we_listen_for)
4058 bool handled = m_joystick->handleEvent(event.JoystickEvent);
4060 if (m_joystick->wasKeyDown(KeyType::ESC)) {
4062 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
4064 acceptInput(quit_mode_accept);
4072 return GUIModalMenu::preprocessEvent(event);
4075 void GUIFormSpecMenu::tryClose()
4079 acceptInput(quit_mode_cancel);
4082 m_text_dst->gotText(L"MenuQuit");
4086 enum ButtonEventType : u8
4099 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
4101 if (event.EventType==EET_KEY_INPUT_EVENT) {
4102 KeyPress kp(event.KeyInput);
4103 if (event.KeyInput.PressedDown && (
4104 (kp == EscapeKey) || (kp == CancelKey) ||
4105 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
4110 if (m_client != NULL && event.KeyInput.PressedDown &&
4111 (kp == getKeySetting("keymap_screenshot"))) {
4112 m_client->makeScreenshot();
4115 if (event.KeyInput.PressedDown && kp == getKeySetting("keymap_toggle_debug"))
4116 m_show_debug = !m_show_debug;
4118 if (event.KeyInput.PressedDown &&
4119 (event.KeyInput.Key==KEY_RETURN ||
4120 event.KeyInput.Key==KEY_UP ||
4121 event.KeyInput.Key==KEY_DOWN)
4123 switch (event.KeyInput.Key) {
4125 current_keys_pending.key_enter = true;
4128 current_keys_pending.key_up = true;
4131 current_keys_pending.key_down = true;
4135 //can't happen at all!
4136 FATAL_ERROR("Reached a source line that can't ever been reached");
4139 if (current_keys_pending.key_enter && m_allowclose) {
4140 acceptInput(quit_mode_accept);
4150 /* Mouse event other than movement, or crossing the border of inventory
4151 field while holding right mouse button
4153 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
4154 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
4155 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
4156 event.MouseInput.isRightPressed() &&
4157 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
4159 // Get selected item and hovered/clicked item (s)
4161 m_old_tooltip_id = -1;
4162 updateSelectedItem();
4163 GUIInventoryList::ItemSpec s = getItemAtPos(m_pointer);
4165 Inventory *inv_selected = NULL;
4166 Inventory *inv_s = NULL;
4167 InventoryList *list_s = NULL;
4169 if (m_selected_item) {
4170 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
4171 sanity_check(inv_selected);
4172 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
4179 inv_s = m_invmgr->getInventory(s.inventoryloc);
4182 errorstream << "InventoryMenu: The selected inventory location "
4183 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
4185 s.i = -1; // make it invalid again
4189 list_s = inv_s->getList(s.listname);
4190 if (list_s == NULL) {
4191 verbosestream << "InventoryMenu: The selected inventory list \""
4192 << s.listname << "\" does not exist" << std::endl;
4193 s.i = -1; // make it invalid again
4197 if ((u32)s.i >= list_s->getSize()) {
4198 infostream << "InventoryMenu: The selected inventory list \""
4199 << s.listname << "\" is too small (i=" << s.i << ", size="
4200 << list_s->getSize() << ")" << std::endl;
4201 s.i = -1; // make it invalid again
4205 s_count = list_s->getItem(s.i).count;
4208 bool identical = m_selected_item && s.isValid() &&
4209 (inv_selected == inv_s) &&
4210 (m_selected_item->listname == s.listname) &&
4211 (m_selected_item->i == s.i);
4213 ButtonEventType button = BET_LEFT;
4214 ButtonEventType updown = BET_OTHER;
4215 switch (event.MouseInput.Event) {
4216 case EMIE_LMOUSE_PRESSED_DOWN:
4217 button = BET_LEFT; updown = BET_DOWN;
4219 case EMIE_RMOUSE_PRESSED_DOWN:
4220 button = BET_RIGHT; updown = BET_DOWN;
4222 case EMIE_MMOUSE_PRESSED_DOWN:
4223 button = BET_MIDDLE; updown = BET_DOWN;
4225 case EMIE_MOUSE_WHEEL:
4226 button = (event.MouseInput.Wheel > 0) ?
4227 BET_WHEEL_UP : BET_WHEEL_DOWN;
4230 case EMIE_LMOUSE_LEFT_UP:
4231 button = BET_LEFT; updown = BET_UP;
4233 case EMIE_RMOUSE_LEFT_UP:
4234 button = BET_RIGHT; updown = BET_UP;
4236 case EMIE_MMOUSE_LEFT_UP:
4237 button = BET_MIDDLE; updown = BET_UP;
4239 case EMIE_MOUSE_MOVED:
4246 // Set this number to a positive value to generate a move action
4247 // from m_selected_item to s.
4248 u32 move_amount = 0;
4250 // Set this number to a positive value to generate a move action
4251 // from s to the next inventory ring.
4252 u32 shift_move_amount = 0;
4254 // Set this number to a positive value to generate a drop action
4255 // from m_selected_item.
4256 u32 drop_amount = 0;
4258 // Set this number to a positive value to generate a craft action at s.
4259 u32 craft_amount = 0;
4263 // Some mouse button has been pressed
4265 //infostream << "Mouse button " << button << " pressed at p=("
4266 // << event.MouseInput.X << "," << event.MouseInput.Y << ")"
4269 m_selected_dragging = false;
4271 if (s.isValid() && s.listname == "craftpreview") {
4272 // Craft preview has been clicked: craft
4273 craft_amount = (button == BET_MIDDLE ? 10 : 1);
4274 } else if (!m_selected_item) {
4275 if (s_count && button != BET_WHEEL_UP) {
4276 // Non-empty stack has been clicked: select or shift-move it
4277 m_selected_item = new GUIInventoryList::ItemSpec(s);
4280 if (button == BET_RIGHT)
4281 count = (s_count + 1) / 2;
4282 else if (button == BET_MIDDLE)
4283 count = MYMIN(s_count, 10);
4284 else if (button == BET_WHEEL_DOWN)
4289 if (!event.MouseInput.Shift) {
4290 // no shift: select item
4291 m_selected_amount = count;
4292 m_selected_dragging = button != BET_WHEEL_DOWN;
4293 m_auto_place = false;
4295 // shift pressed: move item, right click moves 1
4296 shift_move_amount = button == BET_RIGHT ? 1 : count;
4299 } else { // m_selected_item != NULL
4300 assert(m_selected_amount >= 1);
4303 // Clicked a slot: move
4304 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4306 else if (button == BET_MIDDLE)
4307 move_amount = MYMIN(m_selected_amount, 10);
4308 else if (button == BET_LEFT)
4309 move_amount = m_selected_amount;
4313 if (button == BET_WHEEL_DOWN) {
4314 if (m_selected_amount < s_count)
4315 ++m_selected_amount;
4317 if (move_amount >= m_selected_amount)
4318 m_selected_amount = 0;
4320 m_selected_amount -= move_amount;
4324 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
4325 && button != BET_WHEEL_DOWN) {
4326 // Clicked outside of the window: drop
4327 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4329 else if (button == BET_MIDDLE)
4330 drop_amount = MYMIN(m_selected_amount, 10);
4332 drop_amount = m_selected_amount;
4337 // Some mouse button has been released
4339 //infostream<<"Mouse button "<<button<<" released at p=("
4340 // <<p.X<<","<<p.Y<<")"<<std::endl;
4342 if (m_selected_dragging && m_selected_item) {
4345 // Dragged to different slot: move all selected
4346 move_amount = m_selected_amount;
4348 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
4349 // Dragged outside of window: drop all selected
4350 drop_amount = m_selected_amount;
4354 m_selected_dragging = false;
4355 // Keep track of whether the mouse button be released
4356 // One click is drag without dropping. Click + release
4357 // + click changes to drop item when moved mode
4358 if (m_selected_item)
4359 m_auto_place = true;
4362 // Mouse has been moved and rmb is down and mouse pointer just
4363 // entered a new inventory field (checked in the entry-if, this
4364 // is the only action here that is generated by mouse movement)
4365 if (m_selected_item && s.isValid() && s.listname != "craftpreview") {
4367 // TODO: middle mouse to move 10 items might be handy
4369 // Only move an item if the destination slot is empty
4370 // or contains the same item type as what is going to be
4372 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4373 InventoryList *list_to = list_s;
4374 assert(list_from && list_to);
4375 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4376 ItemStack stack_to = list_to->getItem(s.i);
4377 if (stack_to.empty() || stack_to.name == stack_from.name)
4386 // Possibly send inventory action to server
4387 if (move_amount > 0) {
4388 // Send IAction::Move
4390 assert(m_selected_item && m_selected_item->isValid());
4391 assert(s.isValid());
4393 assert(inv_selected && inv_s);
4394 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4395 InventoryList *list_to = list_s;
4396 assert(list_from && list_to);
4397 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4398 ItemStack stack_to = list_to->getItem(s.i);
4400 // Check how many items can be moved
4401 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
4402 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
4404 // If source stack cannot be added to destination stack at all,
4406 if (leftover.count == stack_from.count &&
4407 leftover.name == stack_from.name) {
4409 if (m_selected_swap.empty()) {
4410 m_selected_amount = stack_to.count;
4411 m_selected_dragging = false;
4413 // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
4414 // Skip next validation checks due async inventory calls
4415 m_selected_swap = stack_to;
4420 // Source stack goes fully into destination stack
4421 else if (leftover.empty()) {
4422 m_selected_amount -= move_amount;
4424 // Source stack goes partly into destination stack
4426 move_amount -= leftover.count;
4427 m_selected_amount -= move_amount;
4431 infostream << "Handing IAction::Move to manager" << std::endl;
4432 IMoveAction *a = new IMoveAction();
4433 a->count = move_amount;
4434 a->from_inv = m_selected_item->inventoryloc;
4435 a->from_list = m_selected_item->listname;
4436 a->from_i = m_selected_item->i;
4437 a->to_inv = s.inventoryloc;
4438 a->to_list = s.listname;
4440 m_invmgr->inventoryAction(a);
4442 } else if (shift_move_amount > 0) {
4443 u32 mis = m_inventory_rings.size();
4445 for (; i < mis; i++) {
4446 const ListRingSpec &sp = m_inventory_rings[i];
4447 if (sp.inventoryloc == s.inventoryloc
4448 && sp.listname == s.listname)
4452 if (i >= mis) // if not found
4454 u32 to_inv_ind = (i + 1) % mis;
4455 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
4456 InventoryList *list_from = list_s;
4459 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
4462 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
4465 ItemStack stack_from = list_from->getItem(s.i);
4466 assert(shift_move_amount <= stack_from.count);
4468 infostream << "Handing IAction::Move to manager" << std::endl;
4469 IMoveAction *a = new IMoveAction();
4470 a->count = shift_move_amount;
4471 a->from_inv = s.inventoryloc;
4472 a->from_list = s.listname;
4474 a->to_inv = to_inv_sp.inventoryloc;
4475 a->to_list = to_inv_sp.listname;
4476 a->move_somewhere = true;
4477 m_invmgr->inventoryAction(a);
4479 } else if (drop_amount > 0) {
4480 // Send IAction::Drop
4482 assert(m_selected_item && m_selected_item->isValid());
4483 assert(inv_selected);
4484 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4486 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4488 // Check how many items can be dropped
4489 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
4490 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
4491 m_selected_amount -= drop_amount;
4493 infostream << "Handing IAction::Drop to manager" << std::endl;
4494 IDropAction *a = new IDropAction();
4495 a->count = drop_amount;
4496 a->from_inv = m_selected_item->inventoryloc;
4497 a->from_list = m_selected_item->listname;
4498 a->from_i = m_selected_item->i;
4499 m_invmgr->inventoryAction(a);
4500 } else if (craft_amount > 0) {
4501 assert(s.isValid());
4503 // if there are no items selected or the selected item
4504 // belongs to craftresult list, proceed with crafting
4505 if (!m_selected_item ||
4506 !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
4510 // Send IACTION_CRAFT
4511 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
4512 ICraftAction *a = new ICraftAction();
4513 a->count = craft_amount;
4514 a->craft_inv = s.inventoryloc;
4515 m_invmgr->inventoryAction(a);
4519 // If m_selected_amount has been decreased to zero, deselect
4520 if (m_selected_amount == 0) {
4521 m_selected_swap.clear();
4522 delete m_selected_item;
4523 m_selected_item = nullptr;
4524 m_selected_amount = 0;
4525 m_selected_dragging = false;
4527 m_old_pointer = m_pointer;
4530 if (event.EventType == EET_GUI_EVENT) {
4531 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
4533 // find the element that was clicked
4534 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4535 if ((s.ftype == f_TabHeader) &&
4536 (s.fid == event.GUIEvent.Caller->getID())) {
4537 if (!s.sound.empty() && m_sound_manager)
4538 m_sound_manager->playSound(s.sound, false, 1.0f);
4546 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
4548 if (!canTakeFocus(event.GUIEvent.Element)) {
4549 infostream<<"GUIFormSpecMenu: Not allowing focus change."
4551 // Returning true disables focus change
4555 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
4556 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
4557 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
4558 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
4559 s32 caller_id = event.GUIEvent.Caller->getID();
4561 if (caller_id == 257) {
4563 acceptInput(quit_mode_accept);
4567 m_text_dst->gotText(L"ExitButton");
4569 // quitMenu deallocates menu
4573 // find the element that was clicked
4574 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4575 // if its a button, set the send field so
4576 // lua knows which button was pressed
4578 if (caller_id != s.fid)
4581 if (s.ftype == f_Button || s.ftype == f_CheckBox) {
4582 if (!s.sound.empty() && m_sound_manager)
4583 m_sound_manager->playSound(s.sound, false, 1.0f);
4588 acceptInput(quit_mode_accept);
4591 m_text_dst->gotText(L"ExitButton");
4596 acceptInput(quit_mode_no);
4600 } else if (s.ftype == f_DropDown) {
4601 // only send the changed dropdown
4602 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4603 if (s2.ftype == f_DropDown) {
4607 if (!s.sound.empty() && m_sound_manager)
4608 m_sound_manager->playSound(s.sound, false, 1.0f);
4610 acceptInput(quit_mode_no);
4612 // revert configuration to make sure dropdowns are sent on
4613 // regular button click
4614 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4615 if (s2.ftype == f_DropDown) {
4620 } else if (s.ftype == f_ScrollBar) {
4621 s.fdefault = L"Changed";
4622 acceptInput(quit_mode_no);
4624 } else if (s.ftype == f_Unknown || s.ftype == f_HyperText) {
4625 if (!s.sound.empty() && m_sound_manager)
4626 m_sound_manager->playSound(s.sound, false, 1.0f);
4634 if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) {
4635 // move scroll_containers
4636 for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers)
4637 c.second->onScrollEvent(event.GUIEvent.Caller);
4640 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
4641 if (event.GUIEvent.Caller->getID() > 257) {
4642 bool close_on_enter = true;
4643 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4644 if (s.ftype == f_Unknown &&
4645 s.fid == event.GUIEvent.Caller->getID()) {
4646 current_field_enter_pending = s.fname;
4647 std::unordered_map<std::string, bool>::const_iterator it =
4648 field_close_on_enter.find(s.fname);
4649 if (it != field_close_on_enter.end())
4650 close_on_enter = (*it).second;
4656 if (m_allowclose && close_on_enter) {
4657 current_keys_pending.key_enter = true;
4658 acceptInput(quit_mode_accept);
4661 current_keys_pending.key_enter = true;
4664 // quitMenu deallocates menu
4669 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
4670 int current_id = event.GUIEvent.Caller->getID();
4671 if (current_id > 257) {
4672 // find the element that was clicked
4673 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4674 // if it's a table, set the send field
4675 // so lua knows which table was changed
4676 if ((s.ftype == f_Table) && (s.fid == current_id)) {
4687 return Parent ? Parent->OnEvent(event) : false;
4691 * get name of element by element id
4692 * @param id of element
4693 * @return name string or empty string
4695 std::string GUIFormSpecMenu::getNameByID(s32 id)
4697 for (FieldSpec &spec : m_fields) {
4705 const GUIFormSpecMenu::FieldSpec *GUIFormSpecMenu::getSpecByID(s32 id)
4707 for (FieldSpec &spec : m_fields) {
4715 * get label of element by id
4716 * @param id of element
4717 * @return label string or empty string
4719 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
4721 for (FieldSpec &spec : m_fields) {
4728 StyleSpec GUIFormSpecMenu::getDefaultStyleForElement(const std::string &type,
4729 const std::string &name, const std::string &parent_type) {
4730 return getStyleForElement(type, name, parent_type)[StyleSpec::STATE_DEFAULT];
4733 std::array<StyleSpec, StyleSpec::NUM_STATES> GUIFormSpecMenu::getStyleForElement(
4734 const std::string &type, const std::string &name, const std::string &parent_type)
4736 std::array<StyleSpec, StyleSpec::NUM_STATES> ret;
4738 auto it = theme_by_type.find("*");
4739 if (it != theme_by_type.end()) {
4740 for (const StyleSpec &spec : it->second)
4741 ret[(u32)spec.getState()] |= spec;
4744 it = theme_by_name.find("*");
4745 if (it != theme_by_name.end()) {
4746 for (const StyleSpec &spec : it->second)
4747 ret[(u32)spec.getState()] |= spec;
4750 if (!parent_type.empty()) {
4751 it = theme_by_type.find(parent_type);
4752 if (it != theme_by_type.end()) {
4753 for (const StyleSpec &spec : it->second)
4754 ret[(u32)spec.getState()] |= spec;
4758 it = theme_by_type.find(type);
4759 if (it != theme_by_type.end()) {
4760 for (const StyleSpec &spec : it->second)
4761 ret[(u32)spec.getState()] |= spec;
4764 it = theme_by_name.find(name);
4765 if (it != theme_by_name.end()) {
4766 for (const StyleSpec &spec : it->second)
4767 ret[(u32)spec.getState()] |= spec;