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.
26 #include "guiFormSpecMenu.h"
27 #include "constants.h"
29 #include "client/keycode.h"
30 #include "util/strfnd.h"
31 #include <IGUIButton.h>
32 #include <IGUICheckBox.h>
33 #include <IGUIComboBox.h>
34 #include <IGUIEditBox.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include "client/renderingengine.h"
40 #include "client/tile.h" // ITextureSource
41 #include "client/hud.h" // drawItemStack
45 #include "scripting_server.h"
46 #include "mainmenumanager.h"
49 #include "client/client.h"
50 #include "client/fontengine.h"
51 #include "client/sound.h"
53 #include "util/numeric.h"
54 #include "util/string.h" // for parseColorString()
55 #include "irrlicht_changes/static_text.h"
56 #include "client/guiscalingfilter.h"
57 #include "guiAnimatedImage.h"
58 #include "guiBackgroundImage.h"
60 #include "guiButton.h"
61 #include "guiButtonImage.h"
62 #include "guiButtonItemImage.h"
63 #include "guiEditBoxWithScrollbar.h"
64 #include "guiInventoryList.h"
65 #include "guiItemImage.h"
66 #include "guiScrollContainer.h"
67 #include "intlGUIEditBox.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, ISimpleTextureSource *tsrc, ISoundManager *sound_manager,
100 IFormSource *fsrc, TextDest *tdst,
101 const std::string &formspecPrepend, bool remap_dbl_click):
102 GUIModalMenu(RenderingEngine::get_gui_env(), 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 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
149 const std::string &formspecPrepend, ISoundManager *sound_manager)
151 if (cur_formspec == nullptr) {
152 cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
153 client, 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 v2f32 slot_spacing = data->real_coordinates ?
501 v2f32(imgsize.X * 1.25f, imgsize.Y * 1.25f) : spacing;
503 v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos)
504 : getElementBasePos(&v_pos);
506 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
507 pos.X + (geom.X - 1) * slot_spacing.X + imgsize.X,
508 pos.Y + (geom.Y - 1) * slot_spacing.Y + imgsize.Y);
510 GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent,
511 spec.fid, rect, m_invmgr, loc, listname, geom, start_i, imgsize,
512 slot_spacing, this, data->inventorylist_options, m_font);
514 m_inventorylists.push_back(e);
515 m_fields.push_back(spec);
518 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
521 void GUIFormSpecMenu::parseListRing(parserData *data, const std::string &element)
524 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
528 std::vector<std::string> parts = split(element, ';');
530 if (parts.size() == 2) {
531 std::string location = parts[0];
532 std::string listname = parts[1];
534 InventoryLocation loc;
536 if (location == "context" || location == "current_name")
537 loc = m_current_inventory_location;
539 loc.deSerialize(location);
541 m_inventory_rings.emplace_back(loc, listname);
545 if (element.empty() && m_inventorylists.size() > 1) {
546 size_t siz = m_inventorylists.size();
547 // insert the last two inv list elements into the list ring
548 const GUIInventoryList *spa = m_inventorylists[siz - 2];
549 const GUIInventoryList *spb = m_inventorylists[siz - 1];
550 m_inventory_rings.emplace_back(spa->getInventoryloc(), spa->getListname());
551 m_inventory_rings.emplace_back(spb->getInventoryloc(), spb->getListname());
555 errorstream<< "Invalid list ring element(" << parts.size() << ", "
556 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
559 void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
561 std::vector<std::string> parts = split(element,';');
563 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
564 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
566 std::vector<std::string> v_pos = split(parts[0],',');
567 std::string name = parts[1];
568 std::string label = parts[2];
569 std::string selected;
571 if (parts.size() >= 4)
574 MY_CHECKPOS("checkbox",0);
576 bool fselected = false;
578 if (selected == "true")
581 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
582 const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str());
583 s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH);
584 s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2;
587 core::rect<s32> rect;
589 if (data->real_coordinates) {
590 pos = getRealCoordinateBasePos(v_pos);
592 rect = core::rect<s32>(
595 pos.X + label_size.Width + cb_size + 7,
599 pos = getElementBasePos(&v_pos);
600 rect = core::rect<s32>(
602 pos.Y + imgsize.Y / 2 - y_center,
603 pos.X + label_size.Width + cb_size + 7,
604 pos.Y + imgsize.Y / 2 + y_center
610 wlabel, //Needed for displaying text on MSVC
615 spec.ftype = f_CheckBox;
617 gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect,
618 data->current_parent, spec.fid, spec.flabel.c_str());
620 auto style = getDefaultStyleForElement("checkbox", name);
622 spec.sound = style.get(StyleSpec::Property::SOUND, "");
624 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
626 if (spec.fname == m_focused_element) {
627 Environment->setFocus(e);
631 m_checkboxes.emplace_back(spec, e);
632 m_fields.push_back(spec);
635 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
638 void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
640 std::vector<std::string> parts = split(element,';');
642 if (parts.size() >= 5) {
643 std::vector<std::string> v_pos = split(parts[0],',');
644 std::vector<std::string> v_geom = split(parts[1],',');
645 std::string name = parts[3];
646 std::string value = parts[4];
648 MY_CHECKPOS("scrollbar",0);
649 MY_CHECKGEOM("scrollbar",1);
654 if (data->real_coordinates) {
655 pos = getRealCoordinateBasePos(v_pos);
656 dim = getRealCoordinateGeometry(v_geom);
658 pos = getElementBasePos(&v_pos);
659 dim.X = stof(v_geom[0]) * spacing.X;
660 dim.Y = stof(v_geom[1]) * spacing.Y;
663 core::rect<s32> rect =
664 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
673 bool is_horizontal = true;
675 if (parts[2] == "vertical")
676 is_horizontal = false;
678 spec.ftype = f_ScrollBar;
680 GUIScrollBar *e = new GUIScrollBar(Environment, data->current_parent,
681 spec.fid, rect, is_horizontal, true);
683 auto style = getDefaultStyleForElement("scrollbar", name);
684 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
685 e->setArrowsVisible(data->scrollbar_options.arrow_visiblity);
687 s32 max = data->scrollbar_options.max;
688 s32 min = data->scrollbar_options.min;
693 e->setPos(stoi(parts[4]));
695 e->setSmallStep(data->scrollbar_options.small_step);
696 e->setLargeStep(data->scrollbar_options.large_step);
698 s32 scrollbar_size = is_horizontal ? dim.X : dim.Y;
700 e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size);
702 if (spec.fname == m_focused_element) {
703 Environment->setFocus(e);
706 m_scrollbars.emplace_back(spec,e);
707 m_fields.push_back(spec);
710 errorstream << "Invalid scrollbar element(" << parts.size() << "): '" << element
714 void GUIFormSpecMenu::parseScrollBarOptions(parserData* data, const std::string &element)
716 std::vector<std::string> parts = split(element, ';');
718 if (parts.size() == 0) {
719 warningstream << "Invalid scrollbaroptions element(" << parts.size() << "): '" <<
720 element << "'" << std::endl;
724 for (const std::string &i : parts) {
725 std::vector<std::string> options = split(i, '=');
727 if (options.size() != 2) {
728 warningstream << "Invalid scrollbaroptions option syntax: '" <<
729 element << "'" << std::endl;
730 continue; // Go to next option
733 if (options[0] == "max") {
734 data->scrollbar_options.max = stoi(options[1]);
736 } else if (options[0] == "min") {
737 data->scrollbar_options.min = stoi(options[1]);
739 } else if (options[0] == "smallstep") {
740 int value = stoi(options[1]);
741 data->scrollbar_options.small_step = value < 0 ? 10 : value;
743 } else if (options[0] == "largestep") {
744 int value = stoi(options[1]);
745 data->scrollbar_options.large_step = value < 0 ? 100 : value;
747 } else if (options[0] == "thumbsize") {
748 int value = stoi(options[1]);
749 data->scrollbar_options.thumb_size = value <= 0 ? 1 : value;
751 } else if (options[0] == "arrows") {
752 std::string value = trim(options[1]);
754 data->scrollbar_options.arrow_visiblity = GUIScrollBar::HIDE;
755 else if (value == "show")
756 data->scrollbar_options.arrow_visiblity = GUIScrollBar::SHOW;
757 else // Auto hide/show
758 data->scrollbar_options.arrow_visiblity = GUIScrollBar::DEFAULT;
762 warningstream << "Invalid scrollbaroptions option(" << options[0] <<
763 "): '" << element << "'" << std::endl;
767 void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
769 std::vector<std::string> parts = split(element,';');
771 if ((parts.size() == 3) ||
772 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
774 std::vector<std::string> v_pos = split(parts[0],',');
775 std::vector<std::string> v_geom = split(parts[1],',');
776 std::string name = unescape_string(parts[2]);
778 MY_CHECKPOS("image", 0);
779 MY_CHECKGEOM("image", 1);
784 if (data->real_coordinates) {
785 pos = getRealCoordinateBasePos(v_pos);
786 geom = getRealCoordinateGeometry(v_geom);
788 pos = getElementBasePos(&v_pos);
789 geom.X = stof(v_geom[0]) * (float)imgsize.X;
790 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
793 if (!data->explicit_size)
794 warningstream<<"invalid use of image without a size[] element"<<std::endl;
796 video::ITexture *texture = m_tsrc->getTexture(name);
798 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
799 << std::endl << "\t" << name << std::endl;
807 258 + m_fields.size(),
810 core::rect<s32> rect(pos, pos + geom);
811 gui::IGUIImage *e = Environment->addImage(rect, data->current_parent,
813 e->setImage(texture);
814 e->setScaleImage(true);
815 auto style = getDefaultStyleForElement("image", spec.fname);
816 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
817 m_fields.push_back(spec);
819 // images should let events through
821 m_clickthrough_elements.push_back(e);
825 if (parts.size() == 2) {
826 std::vector<std::string> v_pos = split(parts[0],',');
827 std::string name = unescape_string(parts[1]);
829 MY_CHECKPOS("image", 0);
831 v2s32 pos = getElementBasePos(&v_pos);
833 if (!data->explicit_size)
834 warningstream<<"invalid use of image without a size[] element"<<std::endl;
836 video::ITexture *texture = m_tsrc->getTexture(name);
838 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
839 << std::endl << "\t" << name << std::endl;
847 258 + m_fields.size()
849 gui::IGUIImage *e = Environment->addImage(texture, pos, true,
850 data->current_parent, spec.fid, 0);
851 auto style = getDefaultStyleForElement("image", spec.fname);
852 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
853 m_fields.push_back(spec);
855 // images should let events through
857 m_clickthrough_elements.push_back(e);
860 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
863 void GUIFormSpecMenu::parseAnimatedImage(parserData *data, const std::string &element)
865 std::vector<std::string> parts = split(element, ';');
867 if (parts.size() != 6 && parts.size() != 7 &&
868 !(parts.size() > 7 && m_formspec_version > FORMSPEC_API_VERSION)) {
869 errorstream << "Invalid animated_image element(" << parts.size()
870 << "): '" << element << "'" << std::endl;
874 std::vector<std::string> v_pos = split(parts[0], ',');
875 std::vector<std::string> v_geom = split(parts[1], ',');
876 std::string name = parts[2];
877 std::string texture_name = unescape_string(parts[3]);
878 s32 frame_count = stoi(parts[4]);
879 s32 frame_duration = stoi(parts[5]);
881 MY_CHECKPOS("animated_image", 0);
882 MY_CHECKGEOM("animated_image", 1);
887 if (data->real_coordinates) {
888 pos = getRealCoordinateBasePos(v_pos);
889 geom = getRealCoordinateGeometry(v_geom);
891 pos = getElementBasePos(&v_pos);
892 geom.X = stof(v_geom[0]) * (float)imgsize.X;
893 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
896 if (!data->explicit_size)
897 warningstream << "Invalid use of animated_image without a size[] element" << std::endl;
903 258 + m_fields.size()
905 spec.ftype = f_AnimatedImage;
908 core::rect<s32> rect = core::rect<s32>(pos, pos + geom);
910 GUIAnimatedImage *e = new GUIAnimatedImage(Environment, this, spec.fid,
911 rect, texture_name, frame_count, frame_duration, m_tsrc);
913 if (parts.size() >= 7)
914 e->setFrameIndex(stoi(parts[6]) - 1);
916 auto style = getDefaultStyleForElement("animated_image", spec.fname, "image");
917 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
919 // Animated images should let events through
920 m_clickthrough_elements.push_back(e);
922 m_fields.push_back(spec);
925 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
927 std::vector<std::string> parts = split(element,';');
929 if ((parts.size() == 3) ||
930 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
932 std::vector<std::string> v_pos = split(parts[0],',');
933 std::vector<std::string> v_geom = split(parts[1],',');
934 std::string name = parts[2];
936 MY_CHECKPOS("itemimage",0);
937 MY_CHECKGEOM("itemimage",1);
942 if (data->real_coordinates) {
943 pos = getRealCoordinateBasePos(v_pos);
944 geom = getRealCoordinateGeometry(v_geom);
946 pos = getElementBasePos(&v_pos);
947 geom.X = stof(v_geom[0]) * (float)imgsize.X;
948 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
951 if(!data->explicit_size)
952 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
958 258 + m_fields.size(),
961 spec.ftype = f_ItemImage;
963 GUIItemImage *e = new GUIItemImage(Environment, data->current_parent, spec.fid,
964 core::rect<s32>(pos, pos + geom), name, m_font, m_client);
965 auto style = getDefaultStyleForElement("item_image", spec.fname);
966 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
968 // item images should let events through
969 m_clickthrough_elements.push_back(e);
971 m_fields.push_back(spec);
974 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
977 void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
978 const std::string &type)
980 std::vector<std::string> parts = split(element,';');
982 if ((parts.size() == 4) ||
983 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
985 std::vector<std::string> v_pos = split(parts[0],',');
986 std::vector<std::string> v_geom = split(parts[1],',');
987 std::string name = parts[2];
988 std::string label = parts[3];
990 MY_CHECKPOS("button",0);
991 MY_CHECKGEOM("button",1);
995 core::rect<s32> rect;
997 if (data->real_coordinates) {
998 pos = getRealCoordinateBasePos(v_pos);
999 geom = getRealCoordinateGeometry(v_geom);
1000 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1003 pos = getElementBasePos(&v_pos);
1004 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1005 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1007 rect = core::rect<s32>(pos.X, pos.Y - m_btn_height,
1008 pos.X + geom.X, pos.Y + m_btn_height);
1011 if(!data->explicit_size)
1012 warningstream<<"invalid use of button without a size[] element"<<std::endl;
1014 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1020 258 + m_fields.size()
1022 spec.ftype = f_Button;
1023 if(type == "button_exit")
1024 spec.is_exit = true;
1026 GUIButton *e = GUIButton::addButton(Environment, rect, m_tsrc,
1027 data->current_parent, spec.fid, spec.flabel.c_str());
1029 auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
1031 spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
1033 e->setStyles(style);
1035 if (spec.fname == m_focused_element) {
1036 Environment->setFocus(e);
1039 m_fields.push_back(spec);
1042 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
1045 void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
1047 std::vector<std::string> parts = split(element,';');
1049 if ((parts.size() >= 3 && parts.size() <= 5) ||
1050 (parts.size() > 5 && m_formspec_version > FORMSPEC_API_VERSION)) {
1051 std::vector<std::string> v_pos = split(parts[0],',');
1052 std::vector<std::string> v_geom = split(parts[1],',');
1053 std::string name = unescape_string(parts[2]);
1055 MY_CHECKPOS("background",0);
1056 MY_CHECKGEOM("background",1);
1061 if (data->real_coordinates) {
1062 pos = getRealCoordinateBasePos(v_pos);
1063 geom = getRealCoordinateGeometry(v_geom);
1065 pos = getElementBasePos(&v_pos);
1066 pos.X -= (spacing.X - (float)imgsize.X) / 2;
1067 pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
1069 geom.X = stof(v_geom[0]) * spacing.X;
1070 geom.Y = stof(v_geom[1]) * spacing.Y;
1074 if (parts.size() >= 4 && is_yes(parts[3])) {
1075 if (data->real_coordinates) {
1076 pos = getRealCoordinateBasePos(v_pos) * -1;
1079 pos.X = stoi(v_pos[0]); //acts as offset
1080 pos.Y = stoi(v_pos[1]);
1085 core::rect<s32> middle;
1086 if (parts.size() >= 5) {
1087 std::vector<std::string> v_middle = split(parts[4], ',');
1088 if (v_middle.size() == 1) {
1089 s32 x = stoi(v_middle[0]);
1090 middle.UpperLeftCorner = core::vector2di(x, x);
1091 middle.LowerRightCorner = core::vector2di(-x, -x);
1092 } else if (v_middle.size() == 2) {
1093 s32 x = stoi(v_middle[0]);
1094 s32 y = stoi(v_middle[1]);
1095 middle.UpperLeftCorner = core::vector2di(x, y);
1096 middle.LowerRightCorner = core::vector2di(-x, -y);
1097 // `-x` is interpreted as `w - x`
1098 } else if (v_middle.size() == 4) {
1099 middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1]));
1100 middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3]));
1102 warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl;
1106 if (!data->explicit_size && !clip)
1107 warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
1113 258 + m_fields.size()
1116 core::rect<s32> rect;
1118 // no auto_clip => position like normal image
1119 rect = core::rect<s32>(pos, pos + geom);
1121 // it will be auto-clipped when drawing
1122 rect = core::rect<s32>(-pos, pos);
1125 GUIBackgroundImage *e = new GUIBackgroundImage(Environment, this, spec.fid,
1126 rect, name, middle, m_tsrc, clip);
1128 FATAL_ERROR_IF(!e, "Failed to create background formspec element");
1130 e->setNotClipped(true);
1132 e->setVisible(false); // the element is drawn manually before all others
1134 m_backgrounds.push_back(e);
1135 m_fields.push_back(spec);
1138 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
1141 void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
1143 std::vector<std::string> parts = split(element,';');
1145 data->table_options.clear();
1146 for (const std::string &part : parts) {
1147 // Parse table option
1148 std::string opt = unescape_string(part);
1149 data->table_options.push_back(GUITable::splitOption(opt));
1153 void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
1155 std::vector<std::string> parts = split(element,';');
1157 data->table_columns.clear();
1158 for (const std::string &part : parts) {
1159 std::vector<std::string> col_parts = split(part,',');
1160 GUITable::TableColumn column;
1161 // Parse column type
1162 if (!col_parts.empty())
1163 column.type = col_parts[0];
1164 // Parse column options
1165 for (size_t j = 1; j < col_parts.size(); ++j) {
1166 std::string opt = unescape_string(col_parts[j]);
1167 column.options.push_back(GUITable::splitOption(opt));
1169 data->table_columns.push_back(column);
1173 void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
1175 std::vector<std::string> parts = split(element,';');
1177 if (((parts.size() == 4) || (parts.size() == 5)) ||
1178 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1180 std::vector<std::string> v_pos = split(parts[0],',');
1181 std::vector<std::string> v_geom = split(parts[1],',');
1182 std::string name = parts[2];
1183 std::vector<std::string> items = split(parts[3],',');
1184 std::string str_initial_selection;
1185 std::string str_transparent = "false";
1187 if (parts.size() >= 5)
1188 str_initial_selection = parts[4];
1190 MY_CHECKPOS("table",0);
1191 MY_CHECKGEOM("table",1);
1196 if (data->real_coordinates) {
1197 pos = getRealCoordinateBasePos(v_pos);
1198 geom = getRealCoordinateGeometry(v_geom);
1200 pos = getElementBasePos(&v_pos);
1201 geom.X = stof(v_geom[0]) * spacing.X;
1202 geom.Y = stof(v_geom[1]) * spacing.Y;
1205 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1211 258 + m_fields.size()
1214 spec.ftype = f_Table;
1216 for (std::string &item : items) {
1217 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1220 //now really show table
1221 GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
1224 if (spec.fname == m_focused_element) {
1225 Environment->setFocus(e);
1228 e->setTable(data->table_options, data->table_columns, items);
1230 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1231 e->setDynamicData(data->table_dyndata[name]);
1234 if (!str_initial_selection.empty() && str_initial_selection != "0")
1235 e->setSelected(stoi(str_initial_selection));
1237 auto style = getDefaultStyleForElement("table", name);
1238 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1239 e->setOverrideFont(style.getFont());
1241 m_tables.emplace_back(spec, e);
1242 m_fields.push_back(spec);
1245 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
1248 void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
1250 std::vector<std::string> parts = split(element,';');
1252 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
1253 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1255 std::vector<std::string> v_pos = split(parts[0],',');
1256 std::vector<std::string> v_geom = split(parts[1],',');
1257 std::string name = parts[2];
1258 std::vector<std::string> items = split(parts[3],',');
1259 std::string str_initial_selection;
1260 std::string str_transparent = "false";
1262 if (parts.size() >= 5)
1263 str_initial_selection = parts[4];
1265 if (parts.size() >= 6)
1266 str_transparent = parts[5];
1268 MY_CHECKPOS("textlist",0);
1269 MY_CHECKGEOM("textlist",1);
1274 if (data->real_coordinates) {
1275 pos = getRealCoordinateBasePos(v_pos);
1276 geom = getRealCoordinateGeometry(v_geom);
1278 pos = getElementBasePos(&v_pos);
1279 geom.X = stof(v_geom[0]) * spacing.X;
1280 geom.Y = stof(v_geom[1]) * spacing.Y;
1283 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1289 258 + m_fields.size()
1292 spec.ftype = f_Table;
1294 for (std::string &item : items) {
1295 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1298 //now really show list
1299 GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
1302 if (spec.fname == m_focused_element) {
1303 Environment->setFocus(e);
1306 e->setTextList(items, is_yes(str_transparent));
1308 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1309 e->setDynamicData(data->table_dyndata[name]);
1312 if (!str_initial_selection.empty() && str_initial_selection != "0")
1313 e->setSelected(stoi(str_initial_selection));
1315 auto style = getDefaultStyleForElement("textlist", name);
1316 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1317 e->setOverrideFont(style.getFont());
1319 m_tables.emplace_back(spec, e);
1320 m_fields.push_back(spec);
1323 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
1326 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
1328 std::vector<std::string> parts = split(element, ';');
1330 if (parts.size() == 5 || parts.size() == 6 ||
1331 (parts.size() > 6 && m_formspec_version > FORMSPEC_API_VERSION))
1333 std::vector<std::string> v_pos = split(parts[0], ',');
1334 std::string name = parts[2];
1335 std::vector<std::string> items = split(parts[3], ',');
1336 std::string str_initial_selection = parts[4];
1338 if (parts.size() >= 6 && is_yes(parts[5]))
1339 m_dropdown_index_event[name] = true;
1341 MY_CHECKPOS("dropdown",0);
1345 core::rect<s32> rect;
1347 if (data->real_coordinates) {
1348 std::vector<std::string> v_geom = split(parts[1],',');
1350 if (v_geom.size() == 1)
1351 v_geom.emplace_back("1");
1353 MY_CHECKGEOM("dropdown",1);
1355 pos = getRealCoordinateBasePos(v_pos);
1356 geom = getRealCoordinateGeometry(v_geom);
1357 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1359 pos = getElementBasePos(&v_pos);
1361 s32 width = stof(parts[1]) * spacing.Y;
1363 rect = core::rect<s32>(pos.X, pos.Y,
1364 pos.X + width, pos.Y + (m_btn_height * 2));
1371 258 + m_fields.size()
1374 spec.ftype = f_DropDown;
1377 //now really show list
1378 gui::IGUIComboBox *e = Environment->addComboBox(rect, data->current_parent,
1381 if (spec.fname == m_focused_element) {
1382 Environment->setFocus(e);
1385 for (const std::string &item : items) {
1386 e->addItem(unescape_translate(unescape_string(
1387 utf8_to_wide(item))).c_str());
1390 if (!str_initial_selection.empty())
1391 e->setSelected(stoi(str_initial_selection)-1);
1393 auto style = getDefaultStyleForElement("dropdown", name);
1395 spec.sound = style.get(StyleSpec::Property::SOUND, "");
1397 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1399 m_fields.push_back(spec);
1401 m_dropdowns.emplace_back(spec, std::vector<std::string>());
1402 std::vector<std::string> &values = m_dropdowns.back().second;
1403 for (const std::string &item : items) {
1404 values.push_back(unescape_string(item));
1409 errorstream << "Invalid dropdown element(" << parts.size() << "): '" << element
1410 << "'" << std::endl;
1413 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
1415 std::vector<std::string> parts = split(element,';');
1416 if (parts.size() == 2 ||
1417 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
1418 field_close_on_enter[parts[0]] = is_yes(parts[1]);
1422 void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
1424 std::vector<std::string> parts = split(element,';');
1426 if (parts.size() == 4 ||
1427 (parts.size() > 4 && m_formspec_version > FORMSPEC_API_VERSION))
1429 std::vector<std::string> v_pos = split(parts[0],',');
1430 std::vector<std::string> v_geom = split(parts[1],',');
1431 std::string name = parts[2];
1432 std::string label = parts[3];
1434 MY_CHECKPOS("pwdfield",0);
1435 MY_CHECKGEOM("pwdfield",1);
1440 if (data->real_coordinates) {
1441 pos = getRealCoordinateBasePos(v_pos);
1442 geom = getRealCoordinateGeometry(v_geom);
1444 pos = getElementBasePos(&v_pos);
1447 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1449 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1450 pos.Y -= m_btn_height;
1451 geom.Y = m_btn_height*2;
1454 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1456 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1462 258 + m_fields.size(),
1468 gui::IGUIEditBox *e = Environment->addEditBox(0, rect, true,
1469 data->current_parent, spec.fid);
1471 if (spec.fname == m_focused_element) {
1472 Environment->setFocus(e);
1475 if (label.length() >= 1) {
1476 int font_height = g_fontengine->getTextHeight();
1477 rect.UpperLeftCorner.Y -= font_height;
1478 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1479 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1480 data->current_parent, 0);
1483 e->setPasswordBox(true,L'*');
1485 auto style = getDefaultStyleForElement("pwdfield", name, "field");
1486 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1487 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1488 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1489 e->setOverrideFont(style.getFont());
1492 evt.EventType = EET_KEY_INPUT_EVENT;
1493 evt.KeyInput.Key = KEY_END;
1494 evt.KeyInput.Char = 0;
1495 evt.KeyInput.Control = false;
1496 evt.KeyInput.Shift = false;
1497 evt.KeyInput.PressedDown = true;
1500 // Note: Before 5.2.0 "parts.size() >= 5" resulted in a
1501 // warning referring to field_close_on_enter[]!
1503 m_fields.push_back(spec);
1506 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
1509 void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
1510 core::rect<s32> &rect, bool is_multiline)
1512 bool is_editable = !spec.fname.empty();
1513 if (!is_editable && !is_multiline) {
1514 // spec field id to 0, this stops submit searching for a value that isn't there
1515 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1516 data->current_parent, 0);
1522 } else if (is_multiline &&
1523 spec.fdefault.empty() && !spec.flabel.empty()) {
1524 // Multiline textareas: swap default and label for backwards compat
1525 spec.flabel.swap(spec.fdefault);
1528 gui::IGUIEditBox *e = nullptr;
1529 static constexpr bool use_intl_edit_box = USE_FREETYPE &&
1530 IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9;
1532 if (use_intl_edit_box && g_settings->getBool("freetype")) {
1533 e = new gui::intlGUIEditBox(spec.fdefault.c_str(), true, Environment,
1534 data->current_parent, spec.fid, rect, is_editable, is_multiline);
1537 e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true, Environment,
1538 data->current_parent, spec.fid, rect, is_editable, true);
1539 } else if (is_editable) {
1540 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
1541 data->current_parent, spec.fid);
1546 auto style = getDefaultStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
1549 if (is_editable && spec.fname == m_focused_element)
1550 Environment->setFocus(e);
1553 e->setMultiLine(true);
1554 e->setWordWrap(true);
1555 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1558 evt.EventType = EET_KEY_INPUT_EVENT;
1559 evt.KeyInput.Key = KEY_END;
1560 evt.KeyInput.Char = 0;
1561 evt.KeyInput.Control = 0;
1562 evt.KeyInput.Shift = 0;
1563 evt.KeyInput.PressedDown = true;
1567 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1568 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1569 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1570 if (style.get(StyleSpec::BGCOLOR, "") == "transparent") {
1571 e->setDrawBackground(false);
1573 e->setOverrideFont(style.getFont());
1578 if (!spec.flabel.empty()) {
1579 int font_height = g_fontengine->getTextHeight();
1580 rect.UpperLeftCorner.Y -= font_height;
1581 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1582 IGUIElement *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
1583 rect, false, true, data->current_parent, 0);
1586 t->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1590 void GUIFormSpecMenu::parseSimpleField(parserData *data,
1591 std::vector<std::string> &parts)
1593 std::string name = parts[0];
1594 std::string label = parts[1];
1595 std::string default_val = parts[2];
1597 core::rect<s32> rect;
1599 if (data->explicit_size)
1600 warningstream << "invalid use of unpositioned \"field\" in inventory" << std::endl;
1602 v2s32 pos = getElementBasePos(nullptr);
1603 pos.Y = (data->simple_field_count + 2) * 60;
1604 v2s32 size = DesiredRect.getSize();
1606 rect = core::rect<s32>(
1607 size.X / 2 - 150, pos.Y,
1608 size.X / 2 - 150 + 300, pos.Y + m_btn_height * 2
1613 default_val = m_form_src->resolveText(default_val);
1616 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1621 utf8_to_wide(unescape_string(default_val)),
1622 258 + m_fields.size(),
1627 createTextField(data, spec, rect, false);
1629 m_fields.push_back(spec);
1631 data->simple_field_count++;
1634 void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
1635 const std::string &type)
1637 std::vector<std::string> v_pos = split(parts[0],',');
1638 std::vector<std::string> v_geom = split(parts[1],',');
1639 std::string name = parts[2];
1640 std::string label = parts[3];
1641 std::string default_val = parts[4];
1643 MY_CHECKPOS(type,0);
1644 MY_CHECKGEOM(type,1);
1649 if (data->real_coordinates) {
1650 pos = getRealCoordinateBasePos(v_pos);
1651 geom = getRealCoordinateGeometry(v_geom);
1653 pos = getElementBasePos(&v_pos);
1656 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1658 if (type == "textarea")
1660 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1661 pos.Y += m_btn_height;
1665 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1666 pos.Y -= m_btn_height;
1667 geom.Y = m_btn_height*2;
1671 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1673 if(!data->explicit_size)
1674 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1677 default_val = m_form_src->resolveText(default_val);
1680 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1685 utf8_to_wide(unescape_string(default_val)),
1686 258 + m_fields.size(),
1691 createTextField(data, spec, rect, type == "textarea");
1693 // Note: Before 5.2.0 "parts.size() >= 6" resulted in a
1694 // warning referring to field_close_on_enter[]!
1696 m_fields.push_back(spec);
1699 void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
1700 const std::string &type)
1702 std::vector<std::string> parts = split(element,';');
1704 if (parts.size() == 3 || parts.size() == 4) {
1705 parseSimpleField(data,parts);
1709 if ((parts.size() == 5) ||
1710 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1712 parseTextArea(data,parts,type);
1715 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1718 void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &element)
1720 std::vector<std::string> parts = split(element, ';');
1722 if (parts.size() != 4 && m_formspec_version < FORMSPEC_API_VERSION) {
1723 errorstream << "Invalid text element(" << parts.size() << "): '" << element << "'" << std::endl;
1727 std::vector<std::string> v_pos = split(parts[0], ',');
1728 std::vector<std::string> v_geom = split(parts[1], ',');
1729 std::string name = parts[2];
1730 std::string text = parts[3];
1732 MY_CHECKPOS("hypertext", 0);
1733 MY_CHECKGEOM("hypertext", 1);
1738 if (data->real_coordinates) {
1739 pos = getRealCoordinateBasePos(v_pos);
1740 geom = getRealCoordinateGeometry(v_geom);
1742 pos = getElementBasePos(&v_pos);
1745 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1746 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y - imgsize.Y);
1747 pos.Y += m_btn_height;
1750 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X + geom.X, pos.Y + geom.Y);
1753 text = m_form_src->resolveText(text);
1757 translate_string(utf8_to_wide(unescape_string(text))),
1759 258 + m_fields.size()
1762 spec.ftype = f_HyperText;
1764 auto style = getDefaultStyleForElement("hypertext", spec.fname);
1765 spec.sound = style.get(StyleSpec::Property::SOUND, "");
1767 GUIHyperText *e = new GUIHyperText(spec.flabel.c_str(), Environment,
1768 data->current_parent, spec.fid, rect, m_client, m_tsrc);
1771 m_fields.push_back(spec);
1774 void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
1776 std::vector<std::string> parts = split(element,';');
1778 if ((parts.size() == 2) ||
1779 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1781 std::vector<std::string> v_pos = split(parts[0],',');
1782 std::string text = parts[1];
1784 MY_CHECKPOS("label",0);
1786 if(!data->explicit_size)
1787 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1789 std::vector<std::string> lines = split(text, '\n');
1791 auto style = getDefaultStyleForElement("label", "");
1792 gui::IGUIFont *font = style.getFont();
1796 for (unsigned int i = 0; i != lines.size(); i++) {
1797 std::wstring wlabel_colors = translate_string(
1798 utf8_to_wide(unescape_string(lines[i])));
1799 // Without color escapes to get the font dimensions
1800 std::wstring wlabel_plain = unescape_enriched(wlabel_colors);
1802 core::rect<s32> rect;
1804 if (data->real_coordinates) {
1805 // Lines are spaced at the distance of 1/2 imgsize.
1806 // This alows lines that line up with the new elements
1807 // easily without sacrificing good line distance. If
1808 // it was one whole imgsize, it would have too much
1810 v2s32 pos = getRealCoordinateBasePos(v_pos);
1812 // Labels are positioned by their center, not their top.
1813 pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2);
1815 rect = core::rect<s32>(
1817 pos.X + font->getDimension(wlabel_plain.c_str()).Width,
1821 // Lines are spaced at the nominal distance of
1822 // 2/5 inventory slot, even if the font doesn't
1823 // quite match that. This provides consistent
1824 // form layout, at the expense of sometimes
1825 // having sub-optimal spacing for the font.
1826 // We multiply by 2 and then divide by 5, rather
1827 // than multiply by 0.4, to get exact results
1828 // in the integer cases: 0.4 is not exactly
1829 // representable in binary floating point.
1831 v2s32 pos = getElementBasePos(nullptr);
1832 pos.X += stof(v_pos[0]) * spacing.X;
1833 pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
1835 pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0;
1837 rect = core::rect<s32>(
1838 pos.X, pos.Y - m_btn_height,
1839 pos.X + font->getDimension(wlabel_plain.c_str()).Width,
1840 pos.Y + m_btn_height);
1847 258 + m_fields.size(),
1850 gui::IGUIStaticText *e = gui::StaticText::add(Environment,
1851 spec.flabel.c_str(), rect, false, false, data->current_parent,
1853 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
1855 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1856 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1857 e->setOverrideFont(font);
1859 m_fields.push_back(spec);
1861 // labels should let events through
1863 m_clickthrough_elements.push_back(e);
1868 errorstream << "Invalid label element(" << parts.size() << "): '" << element
1869 << "'" << std::endl;
1872 void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
1874 std::vector<std::string> parts = split(element,';');
1876 if ((parts.size() == 2) ||
1877 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1879 std::vector<std::string> v_pos = split(parts[0],',');
1880 std::wstring text = unescape_translate(
1881 unescape_string(utf8_to_wide(parts[1])));
1883 MY_CHECKPOS("vertlabel",1);
1885 auto style = getDefaultStyleForElement("vertlabel", "", "label");
1886 gui::IGUIFont *font = style.getFont();
1891 core::rect<s32> rect;
1893 if (data->real_coordinates) {
1894 pos = getRealCoordinateBasePos(v_pos);
1896 // Vertlabels are positioned by center, not left.
1897 pos.X -= imgsize.X / 2;
1899 // We use text.length + 1 because without it, the rect
1900 // isn't quite tall enough and cuts off the text.
1901 rect = core::rect<s32>(pos.X, pos.Y,
1903 pos.Y + font_line_height(font) *
1904 (text.length() + 1));
1907 pos = getElementBasePos(&v_pos);
1909 // As above, the length must be one longer. The width of
1910 // the rect (15 pixels) seems rather arbitrary, but
1911 // changing it might break something.
1912 rect = core::rect<s32>(
1913 pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
1915 font_line_height(font) *
1916 (text.length() + 1) +
1917 ((imgsize.Y/2) - m_btn_height));
1920 if(!data->explicit_size)
1921 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1925 for (wchar_t i : text) {
1934 258 + m_fields.size()
1936 gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
1937 rect, false, false, data->current_parent, spec.fid);
1938 e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1940 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1941 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1942 e->setOverrideFont(font);
1944 m_fields.push_back(spec);
1946 // vertlabels should let events through
1948 m_clickthrough_elements.push_back(e);
1951 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1954 void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
1955 const std::string &type)
1957 std::vector<std::string> parts = split(element,';');
1959 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1960 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1962 std::vector<std::string> v_pos = split(parts[0],',');
1963 std::vector<std::string> v_geom = split(parts[1],',');
1964 std::string image_name = parts[2];
1965 std::string name = parts[3];
1966 std::string label = parts[4];
1968 MY_CHECKPOS("imagebutton",0);
1969 MY_CHECKGEOM("imagebutton",1);
1971 std::string pressed_image_name;
1973 if (parts.size() >= 8) {
1974 pressed_image_name = parts[7];
1980 if (data->real_coordinates) {
1981 pos = getRealCoordinateBasePos(v_pos);
1982 geom = getRealCoordinateGeometry(v_geom);
1984 pos = getElementBasePos(&v_pos);
1985 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1986 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1989 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1992 if (!data->explicit_size)
1993 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1995 image_name = unescape_string(image_name);
1996 pressed_image_name = unescape_string(pressed_image_name);
1998 std::wstring wlabel = utf8_to_wide(unescape_string(label));
2003 utf8_to_wide(image_name),
2004 258 + m_fields.size()
2006 spec.ftype = f_Button;
2007 if (type == "image_button_exit")
2008 spec.is_exit = true;
2010 GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, m_tsrc,
2011 data->current_parent, spec.fid, spec.flabel.c_str());
2013 if (spec.fname == m_focused_element) {
2014 Environment->setFocus(e);
2017 auto style = getStyleForElement("image_button", spec.fname);
2019 spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
2021 // Override style properties with values specified directly in the element
2022 if (!image_name.empty())
2023 style[StyleSpec::STATE_DEFAULT].set(StyleSpec::FGIMG, image_name);
2025 if (!pressed_image_name.empty())
2026 style[StyleSpec::STATE_PRESSED].set(StyleSpec::FGIMG, pressed_image_name);
2028 if (parts.size() >= 7) {
2029 style[StyleSpec::STATE_DEFAULT].set(StyleSpec::NOCLIP, parts[5]);
2030 style[StyleSpec::STATE_DEFAULT].set(StyleSpec::BORDER, parts[6]);
2033 e->setStyles(style);
2034 e->setScaleImage(true);
2036 m_fields.push_back(spec);
2040 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
2043 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
2045 std::vector<std::string> parts = split(element, ';');
2047 if (((parts.size() == 4) || (parts.size() == 6)) || (parts.size() == 7 &&
2048 data->real_coordinates) || ((parts.size() > 6) &&
2049 (m_formspec_version > FORMSPEC_API_VERSION)))
2051 std::vector<std::string> v_pos = split(parts[0],',');
2053 // If we're using real coordinates, add an extra field for height.
2054 // Width is not here because tabs are the width of the text, and
2055 // there's no reason to change that.
2057 std::vector<std::string> v_geom = {"1", "1"}; // Dummy width and height
2058 bool auto_width = true;
2059 if (parts.size() == 7) {
2062 v_geom = split(parts[1], ',');
2063 if (v_geom.size() == 1)
2064 v_geom.insert(v_geom.begin(), "1"); // Dummy value
2069 std::string name = parts[i+1];
2070 std::vector<std::string> buttons = split(parts[i+2], ',');
2071 std::string str_index = parts[i+3];
2072 bool show_background = true;
2073 bool show_border = true;
2074 int tab_index = stoi(str_index) - 1;
2076 MY_CHECKPOS("tabheader", 0);
2078 if (parts.size() == 6 + i) {
2079 if (parts[4+i] == "true")
2080 show_background = false;
2081 if (parts[5+i] == "false")
2082 show_border = false;
2089 258 + m_fields.size()
2092 spec.ftype = f_TabHeader;
2097 if (data->real_coordinates) {
2098 pos = getRealCoordinateBasePos(v_pos);
2100 geom = getRealCoordinateGeometry(v_geom);
2101 // Set default height
2102 if (parts.size() <= 6)
2103 geom.Y = m_btn_height * 2;
2104 pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
2106 geom.X = DesiredRect.getWidth(); // Set automatic width
2108 MY_CHECKGEOM("tabheader", 1);
2110 v2f32 pos_f = pos_offset * spacing;
2111 pos_f.X += stof(v_pos[0]) * spacing.X;
2112 pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
2113 pos = v2s32(pos_f.X, pos_f.Y);
2115 geom.Y = m_btn_height * 2;
2116 geom.X = DesiredRect.getWidth();
2119 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
2122 gui::IGUITabControl *e = Environment->addTabControl(rect,
2123 data->current_parent, show_background, show_border, spec.fid);
2124 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
2125 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
2126 e->setTabHeight(geom.Y);
2128 auto style = getDefaultStyleForElement("tabheader", name);
2130 spec.sound = style.get(StyleSpec::Property::SOUND, "");
2132 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
2134 for (const std::string &button : buttons) {
2135 auto tab = e->addTab(unescape_translate(unescape_string(
2136 utf8_to_wide(button))).c_str(), -1);
2137 if (style.isNotDefault(StyleSpec::BGCOLOR))
2138 tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
2140 tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
2143 if ((tab_index >= 0) &&
2144 (buttons.size() < INT_MAX) &&
2145 (tab_index < (int) buttons.size()))
2146 e->setActiveTab(tab_index);
2148 m_fields.push_back(spec);
2151 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
2152 << element << "'" << std::endl;
2155 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
2157 if (m_client == 0) {
2158 warningstream << "invalid use of item_image_button with m_client==0"
2163 std::vector<std::string> parts = split(element,';');
2165 if ((parts.size() == 5) ||
2166 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2168 std::vector<std::string> v_pos = split(parts[0],',');
2169 std::vector<std::string> v_geom = split(parts[1],',');
2170 std::string item_name = parts[2];
2171 std::string name = parts[3];
2172 std::string label = parts[4];
2174 label = unescape_string(label);
2175 item_name = unescape_string(item_name);
2177 MY_CHECKPOS("itemimagebutton",0);
2178 MY_CHECKGEOM("itemimagebutton",1);
2183 if (data->real_coordinates) {
2184 pos = getRealCoordinateBasePos(v_pos);
2185 geom = getRealCoordinateGeometry(v_geom);
2187 pos = getElementBasePos(&v_pos);
2188 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
2189 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
2192 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
2194 if(!data->explicit_size)
2195 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
2197 IItemDefManager *idef = m_client->idef();
2199 item.deSerialize(item_name, idef);
2202 TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
2203 m_default_tooltip_bgcolor,
2204 m_default_tooltip_color);
2206 // the spec for the button
2209 utf8_to_wide(label),
2210 utf8_to_wide(item_name),
2211 258 + m_fields.size(),
2215 GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment,
2216 rect, m_tsrc, data->current_parent, spec_btn.fid, spec_btn.flabel.c_str(),
2217 item_name, m_client);
2219 auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
2221 spec_btn.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
2223 e_btn->setStyles(style);
2225 if (spec_btn.fname == m_focused_element) {
2226 Environment->setFocus(e_btn);
2229 spec_btn.ftype = f_Button;
2230 rect += data->basepos-padding;
2231 spec_btn.rect = rect;
2232 m_fields.push_back(spec_btn);
2235 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
2238 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
2240 std::vector<std::string> parts = split(element, ';');
2242 if ((parts.size() == 3) ||
2243 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
2245 std::vector<std::string> v_pos = split(parts[0], ',');
2246 std::vector<std::string> v_geom = split(parts[1], ',');
2248 MY_CHECKPOS("box", 0);
2249 MY_CHECKGEOM("box", 1);
2254 if (data->real_coordinates) {
2255 pos = getRealCoordinateBasePos(v_pos);
2256 geom = getRealCoordinateGeometry(v_geom);
2258 pos = getElementBasePos(&v_pos);
2259 geom.X = stof(v_geom[0]) * spacing.X;
2260 geom.Y = stof(v_geom[1]) * spacing.Y;
2267 258 + m_fields.size(),
2272 auto style = getDefaultStyleForElement("box", spec.fname);
2274 video::SColor tmp_color;
2275 std::array<video::SColor, 4> colors;
2276 std::array<video::SColor, 4> bordercolors = {0x0, 0x0, 0x0, 0x0};
2277 std::array<s32, 4> borderwidths = {0, 0, 0, 0};
2279 if (parseColorString(parts[2], tmp_color, true, 0x8C)) {
2280 colors = {tmp_color, tmp_color, tmp_color, tmp_color};
2282 colors = style.getColorArray(StyleSpec::COLORS, {0x0, 0x0, 0x0, 0x0});
2283 bordercolors = style.getColorArray(StyleSpec::BORDERCOLORS,
2284 {0x0, 0x0, 0x0, 0x0});
2285 borderwidths = style.getIntArray(StyleSpec::BORDERWIDTHS, {0, 0, 0, 0});
2288 core::rect<s32> rect(pos, pos + geom);
2290 GUIBox *e = new GUIBox(Environment, data->current_parent, spec.fid, rect,
2291 colors, bordercolors, borderwidths);
2292 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
2295 m_fields.push_back(spec);
2298 errorstream << "Invalid Box element(" << parts.size() << "): '" << element
2299 << "'" << std::endl;
2302 void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
2304 std::vector<std::string> parts = split(element,';');
2305 const u32 parameter_count = parts.size();
2307 if ((parameter_count > 2 && m_formspec_version < 3) ||
2308 (parameter_count > 3 && m_formspec_version <= FORMSPEC_API_VERSION)) {
2309 errorstream << "Invalid bgcolor element(" << parameter_count << "): '"
2310 << element << "'" << std::endl;
2315 if (parameter_count >= 1 && parts[0] != "")
2316 parseColorString(parts[0], m_bgcolor, false);
2319 if (parameter_count >= 2) {
2320 if (parts[1] == "both") {
2321 m_bgnonfullscreen = true;
2322 m_bgfullscreen = true;
2323 } else if (parts[1] == "neither") {
2324 m_bgnonfullscreen = false;
2325 m_bgfullscreen = false;
2326 } else if (parts[1] != "" || m_formspec_version < 3) {
2327 m_bgfullscreen = is_yes(parts[1]);
2328 m_bgnonfullscreen = !m_bgfullscreen;
2333 if (parameter_count >= 3 && parts[2] != "")
2334 parseColorString(parts[2], m_fullscreen_bgcolor, false);
2337 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
2339 std::vector<std::string> parts = split(element,';');
2341 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
2342 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2344 parseColorString(parts[0], data->inventorylist_options.slotbg_n, false);
2345 parseColorString(parts[1], data->inventorylist_options.slotbg_h, false);
2347 if (parts.size() >= 3) {
2348 if (parseColorString(parts[2], data->inventorylist_options.slotbordercolor,
2350 data->inventorylist_options.slotborder = true;
2353 if (parts.size() == 5) {
2354 video::SColor tmp_color;
2356 if (parseColorString(parts[3], tmp_color, false))
2357 m_default_tooltip_bgcolor = tmp_color;
2358 if (parseColorString(parts[4], tmp_color, false))
2359 m_default_tooltip_color = tmp_color;
2362 // update all already parsed inventorylists
2363 for (GUIInventoryList *e : m_inventorylists) {
2364 e->setSlotBGColors(data->inventorylist_options.slotbg_n,
2365 data->inventorylist_options.slotbg_h);
2366 e->setSlotBorders(data->inventorylist_options.slotborder,
2367 data->inventorylist_options.slotbordercolor);
2371 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
2374 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
2376 std::vector<std::string> parts = split(element,';');
2377 if (parts.size() < 2) {
2378 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2379 << element << "'" << std::endl;
2383 // Get mode and check size
2384 bool rect_mode = parts[0].find(',') != std::string::npos;
2385 size_t base_size = rect_mode ? 3 : 2;
2386 if (parts.size() != base_size && parts.size() != base_size + 2) {
2387 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2388 << element << "'" << std::endl;
2393 video::SColor bgcolor = m_default_tooltip_bgcolor;
2394 video::SColor color = m_default_tooltip_color;
2395 if (parts.size() == base_size + 2 &&
2396 (!parseColorString(parts[base_size], bgcolor, false) ||
2397 !parseColorString(parts[base_size + 1], color, false))) {
2398 errorstream << "Invalid color in tooltip element(" << parts.size()
2399 << "): '" << element << "'" << std::endl;
2403 // Make tooltip spec
2404 std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
2405 TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
2409 std::vector<std::string> v_pos = split(parts[0], ',');
2410 std::vector<std::string> v_geom = split(parts[1], ',');
2412 MY_CHECKPOS("tooltip", 0);
2413 MY_CHECKGEOM("tooltip", 1);
2418 if (data->real_coordinates) {
2419 pos = getRealCoordinateBasePos(v_pos);
2420 geom = getRealCoordinateGeometry(v_geom);
2422 pos = getElementBasePos(&v_pos);
2423 geom.X = stof(v_geom[0]) * spacing.X;
2424 geom.Y = stof(v_geom[1]) * spacing.Y;
2427 FieldSpec fieldspec(
2431 258 + m_fields.size()
2434 core::rect<s32> rect(pos, pos + geom);
2436 gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
2437 data->current_parent, fieldspec.fid, rect);
2439 // the element the rect tooltip is bound to should not block mouse-clicks
2440 e->setVisible(false);
2442 m_fields.push_back(fieldspec);
2443 m_tooltip_rects.emplace_back(e, spec);
2446 m_tooltips[parts[0]] = spec;
2450 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
2456 std::vector<std::string> parts = split(data,'[');
2458 if (parts.size() < 2) {
2462 if (trim(parts[0]) != "formspec_version") {
2466 if (is_number(parts[1])) {
2467 m_formspec_version = mystoi(parts[1]);
2474 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
2476 if (element.empty())
2479 std::vector<std::string> parts = split(element,'[');
2481 if (parts.size() < 2)
2484 std::string type = trim(parts[0]);
2485 std::string description = trim(parts[1]);
2487 if (type != "size" && type != "invsize")
2490 if (type == "invsize")
2491 warningstream << "Deprecated formspec element \"invsize\" is used" << std::endl;
2493 parseSize(data, description);
2498 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
2500 if (element.empty())
2503 std::vector<std::string> parts = split(element, '[');
2505 if (parts.size() != 2)
2508 std::string type = trim(parts[0]);
2509 std::string description = trim(parts[1]);
2511 if (type != "position")
2514 parsePosition(data, description);
2519 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
2521 std::vector<std::string> parts = split(element, ',');
2523 if (parts.size() == 2) {
2524 data->offset.X = stof(parts[0]);
2525 data->offset.Y = stof(parts[1]);
2529 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
2532 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
2534 if (element.empty())
2537 std::vector<std::string> parts = split(element, '[');
2539 if (parts.size() != 2)
2542 std::string type = trim(parts[0]);
2543 std::string description = trim(parts[1]);
2545 if (type != "anchor")
2548 parseAnchor(data, description);
2553 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
2555 std::vector<std::string> parts = split(element, ',');
2557 if (parts.size() == 2) {
2558 data->anchor.X = stof(parts[0]);
2559 data->anchor.Y = stof(parts[1]);
2563 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
2564 << "'" << std::endl;
2567 bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type)
2569 std::vector<std::string> parts = split(element, ';');
2571 if (parts.size() < 2) {
2572 errorstream << "Invalid style element (" << parts.size() << "): '" << element
2573 << "'" << std::endl;
2580 for (size_t i = 1; i < parts.size(); i++) {
2581 size_t equal_pos = parts[i].find('=');
2582 if (equal_pos == std::string::npos) {
2583 errorstream << "Invalid style element (Property missing value): '" << element
2584 << "'" << std::endl;
2588 std::string propname = trim(parts[i].substr(0, equal_pos));
2589 std::string value = trim(unescape_string(parts[i].substr(equal_pos + 1)));
2591 std::transform(propname.begin(), propname.end(), propname.begin(), ::tolower);
2593 StyleSpec::Property prop = StyleSpec::GetPropertyByName(propname);
2594 if (prop == StyleSpec::NONE) {
2595 if (property_warned.find(propname) != property_warned.end()) {
2596 warningstream << "Invalid style element (Unknown property " << propname << "): '"
2598 << "'" << std::endl;
2599 property_warned.insert(propname);
2604 spec.set(prop, value);
2607 std::vector<std::string> selectors = split(parts[0], ',');
2608 for (size_t sel = 0; sel < selectors.size(); sel++) {
2609 std::string selector = trim(selectors[sel]);
2611 // Copy the style properties to a new StyleSpec
2612 // This allows a separate state mask per-selector
2613 StyleSpec selector_spec = spec;
2615 // Parse state information, if it exists
2616 bool state_valid = true;
2617 size_t state_pos = selector.find(':');
2618 if (state_pos != std::string::npos) {
2619 std::string state_str = selector.substr(state_pos + 1);
2620 selector = selector.substr(0, state_pos);
2622 if (state_str.empty()) {
2623 errorstream << "Invalid style element (Invalid state): '" << element
2624 << "'" << std::endl;
2625 state_valid = false;
2627 std::vector<std::string> states = split(state_str, '+');
2628 for (std::string &state : states) {
2629 StyleSpec::State converted = StyleSpec::getStateByName(state);
2630 if (converted == StyleSpec::STATE_INVALID) {
2631 infostream << "Unknown style state " << state <<
2632 " in element '" << element << "'" << std::endl;
2633 state_valid = false;
2637 selector_spec.addState(converted);
2643 // Skip this selector
2648 theme_by_type[selector].push_back(selector_spec);
2650 theme_by_name[selector].push_back(selector_spec);
2653 // Backwards-compatibility for existing _hovered/_pressed properties
2654 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_HOVERED)
2655 || selector_spec.hasProperty(StyleSpec::BGIMG_HOVERED)
2656 || selector_spec.hasProperty(StyleSpec::FGIMG_HOVERED)) {
2657 StyleSpec hover_spec;
2658 hover_spec.addState(StyleSpec::STATE_HOVERED);
2660 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_HOVERED)) {
2661 hover_spec.set(StyleSpec::BGCOLOR, selector_spec.get(StyleSpec::BGCOLOR_HOVERED, ""));
2663 if (selector_spec.hasProperty(StyleSpec::BGIMG_HOVERED)) {
2664 hover_spec.set(StyleSpec::BGIMG, selector_spec.get(StyleSpec::BGIMG_HOVERED, ""));
2666 if (selector_spec.hasProperty(StyleSpec::FGIMG_HOVERED)) {
2667 hover_spec.set(StyleSpec::FGIMG, selector_spec.get(StyleSpec::FGIMG_HOVERED, ""));
2671 theme_by_type[selector].push_back(hover_spec);
2673 theme_by_name[selector].push_back(hover_spec);
2676 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_PRESSED)
2677 || selector_spec.hasProperty(StyleSpec::BGIMG_PRESSED)
2678 || selector_spec.hasProperty(StyleSpec::FGIMG_PRESSED)) {
2679 StyleSpec press_spec;
2680 press_spec.addState(StyleSpec::STATE_PRESSED);
2682 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_PRESSED)) {
2683 press_spec.set(StyleSpec::BGCOLOR, selector_spec.get(StyleSpec::BGCOLOR_PRESSED, ""));
2685 if (selector_spec.hasProperty(StyleSpec::BGIMG_PRESSED)) {
2686 press_spec.set(StyleSpec::BGIMG, selector_spec.get(StyleSpec::BGIMG_PRESSED, ""));
2688 if (selector_spec.hasProperty(StyleSpec::FGIMG_PRESSED)) {
2689 press_spec.set(StyleSpec::FGIMG, selector_spec.get(StyleSpec::FGIMG_PRESSED, ""));
2693 theme_by_type[selector].push_back(press_spec);
2695 theme_by_name[selector].push_back(press_spec);
2703 void GUIFormSpecMenu::parseSetFocus(const std::string &element)
2705 std::vector<std::string> parts = split(element, ';');
2707 if (parts.size() <= 2 ||
2708 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION))
2710 if (m_is_form_regenerated)
2711 return; // Never focus on resizing
2713 bool force_focus = parts.size() >= 2 && is_yes(parts[1]);
2714 if (force_focus || m_text_dst->m_formname != m_last_formname)
2720 errorstream << "Invalid set_focus element (" << parts.size() << "): '" << element
2721 << "'" << std::endl;
2724 void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
2726 std::vector<std::string> parts = split(element, ';');
2728 if (parts.size() < 5 || (parts.size() > 9 &&
2729 m_formspec_version <= FORMSPEC_API_VERSION)) {
2730 errorstream << "Invalid model element (" << parts.size() << "): '" << element
2731 << "'" << std::endl;
2735 // Avoid length checks by resizing
2736 if (parts.size() < 9)
2739 std::vector<std::string> v_pos = split(parts[0], ',');
2740 std::vector<std::string> v_geom = split(parts[1], ',');
2741 std::string name = unescape_string(parts[2]);
2742 std::string meshstr = unescape_string(parts[3]);
2743 std::vector<std::string> textures = split(parts[4], ',');
2744 std::vector<std::string> vec_rot = split(parts[5], ',');
2745 bool inf_rotation = is_yes(parts[6]);
2746 bool mousectrl = is_yes(parts[7]) || parts[7].empty(); // default true
2747 std::vector<std::string> frame_loop = split(parts[8], ',');
2749 MY_CHECKPOS("model", 0);
2750 MY_CHECKGEOM("model", 1);
2755 if (data->real_coordinates) {
2756 pos = getRealCoordinateBasePos(v_pos);
2757 geom = getRealCoordinateGeometry(v_geom);
2759 pos = getElementBasePos(&v_pos);
2760 geom.X = stof(v_geom[0]) * (float)imgsize.X;
2761 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
2764 if (!data->explicit_size)
2765 warningstream << "invalid use of model without a size[] element" << std::endl;
2767 scene::IAnimatedMesh *mesh = m_client->getMesh(meshstr);
2770 errorstream << "Invalid model element: Unable to load mesh:"
2771 << std::endl << "\t" << meshstr << std::endl;
2779 258 + m_fields.size()
2782 core::rect<s32> rect(pos, pos + geom);
2784 GUIScene *e = new GUIScene(Environment, RenderingEngine::get_scene_manager(),
2785 data->current_parent, rect, spec.fid);
2787 auto meshnode = e->setMesh(mesh);
2789 for (u32 i = 0; i < textures.size() && i < meshnode->getMaterialCount(); ++i)
2790 e->setTexture(i, m_tsrc->getTexture(unescape_string(textures[i])));
2792 if (vec_rot.size() >= 2)
2793 e->setRotation(v2f(stof(vec_rot[0]), stof(vec_rot[1])));
2795 e->enableContinuousRotation(inf_rotation);
2796 e->enableMouseControl(mousectrl);
2798 s32 frame_loop_begin = 0;
2799 s32 frame_loop_end = 0x7FFFFFFF;
2801 if (frame_loop.size() == 2) {
2802 frame_loop_begin = stoi(frame_loop[0]);
2803 frame_loop_end = stoi(frame_loop[1]);
2806 e->setFrameLoop(frame_loop_begin, frame_loop_end);
2808 auto style = getStyleForElement("model", spec.fname);
2809 e->setStyles(style);
2812 m_fields.push_back(spec);
2815 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
2818 if (element.empty())
2821 if (parseVersionDirect(element))
2824 size_t pos = element.find('[');
2825 if (pos == std::string::npos)
2828 std::string type = trim(element.substr(0, pos));
2829 std::string description = element.substr(pos+1);
2831 if (type == "container") {
2832 parseContainer(data, description);
2836 if (type == "container_end") {
2837 parseContainerEnd(data);
2841 if (type == "list") {
2842 parseList(data, description);
2846 if (type == "listring") {
2847 parseListRing(data, description);
2851 if (type == "checkbox") {
2852 parseCheckbox(data, description);
2856 if (type == "image") {
2857 parseImage(data, description);
2861 if (type == "animated_image") {
2862 parseAnimatedImage(data, description);
2866 if (type == "item_image") {
2867 parseItemImage(data, description);
2871 if (type == "button" || type == "button_exit") {
2872 parseButton(data, description, type);
2876 if (type == "background" || type == "background9") {
2877 parseBackground(data, description);
2881 if (type == "tableoptions"){
2882 parseTableOptions(data,description);
2886 if (type == "tablecolumns"){
2887 parseTableColumns(data,description);
2891 if (type == "table"){
2892 parseTable(data,description);
2896 if (type == "textlist"){
2897 parseTextList(data,description);
2901 if (type == "dropdown"){
2902 parseDropDown(data,description);
2906 if (type == "field_close_on_enter") {
2907 parseFieldCloseOnEnter(data, description);
2911 if (type == "pwdfield") {
2912 parsePwdField(data,description);
2916 if ((type == "field") || (type == "textarea")){
2917 parseField(data,description,type);
2921 if (type == "hypertext") {
2922 parseHyperText(data,description);
2926 if (type == "label") {
2927 parseLabel(data,description);
2931 if (type == "vertlabel") {
2932 parseVertLabel(data,description);
2936 if (type == "item_image_button") {
2937 parseItemImageButton(data,description);
2941 if ((type == "image_button") || (type == "image_button_exit")) {
2942 parseImageButton(data,description,type);
2946 if (type == "tabheader") {
2947 parseTabHeader(data,description);
2951 if (type == "box") {
2952 parseBox(data,description);
2956 if (type == "bgcolor") {
2957 parseBackgroundColor(data,description);
2961 if (type == "listcolors") {
2962 parseListColors(data,description);
2966 if (type == "tooltip") {
2967 parseTooltip(data,description);
2971 if (type == "scrollbar") {
2972 parseScrollBar(data, description);
2976 if (type == "real_coordinates") {
2977 data->real_coordinates = is_yes(description);
2981 if (type == "style") {
2982 parseStyle(data, description, false);
2986 if (type == "style_type") {
2987 parseStyle(data, description, true);
2991 if (type == "scrollbaroptions") {
2992 parseScrollBarOptions(data, description);
2996 if (type == "scroll_container") {
2997 parseScrollContainer(data, description);
3001 if (type == "scroll_container_end") {
3002 parseScrollContainerEnd(data);
3006 if (type == "set_focus") {
3007 parseSetFocus(description);
3011 if (type == "model") {
3012 parseModel(data, description);
3017 infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
3021 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
3023 // Useless to regenerate without a screensize
3024 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
3030 // Preserve stuff only on same form, not on a new form.
3031 if (m_text_dst->m_formname == m_last_formname) {
3032 // Preserve tables/textlists
3033 for (auto &m_table : m_tables) {
3034 std::string tablename = m_table.first.fname;
3035 GUITable *table = m_table.second;
3036 mydata.table_dyndata[tablename] = table->getDynamicData();
3040 gui::IGUIElement *focused_element = Environment->getFocus();
3041 if (focused_element && focused_element->getParent() == this) {
3042 s32 focused_id = focused_element->getID();
3043 if (focused_id > 257) {
3044 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
3045 if (field.fid == focused_id) {
3046 m_focused_element = field.fname;
3053 // Don't keep old focus value
3054 m_focused_element = "";
3060 for (auto &table_it : m_tables)
3061 table_it.second->drop();
3062 for (auto &inventorylist_it : m_inventorylists)
3063 inventorylist_it->drop();
3064 for (auto &checkbox_it : m_checkboxes)
3065 checkbox_it.second->drop();
3066 for (auto &scrollbar_it : m_scrollbars)
3067 scrollbar_it.second->drop();
3068 for (auto &background_it : m_backgrounds)
3069 background_it->drop();
3070 for (auto &tooltip_rect_it : m_tooltip_rects)
3071 tooltip_rect_it.first->drop();
3072 for (auto &clickthrough_it : m_clickthrough_elements)
3073 clickthrough_it->drop();
3074 for (auto &scroll_container_it : m_scroll_containers)
3075 scroll_container_it.second->drop();
3077 mydata.size = v2s32(100, 100);
3078 mydata.screensize = screensize;
3079 mydata.offset = v2f32(0.5f, 0.5f);
3080 mydata.anchor = v2f32(0.5f, 0.5f);
3081 mydata.simple_field_count = 0;
3083 // Base position of contents of form
3084 mydata.basepos = getBasePos();
3086 // the parent for the parsed elements
3087 mydata.current_parent = this;
3089 m_inventorylists.clear();
3090 m_backgrounds.clear();
3092 m_checkboxes.clear();
3093 m_scrollbars.clear();
3096 m_tooltip_rects.clear();
3097 m_inventory_rings.clear();
3098 m_dropdowns.clear();
3099 m_scroll_containers.clear();
3100 theme_by_name.clear();
3101 theme_by_type.clear();
3102 m_clickthrough_elements.clear();
3103 field_close_on_enter.clear();
3104 m_dropdown_index_event.clear();
3106 m_bgnonfullscreen = true;
3107 m_bgfullscreen = false;
3109 m_formspec_version = 1;
3112 v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
3113 m_bgcolor = video::SColor(
3114 (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
3115 clamp_u8(myround(formspec_bgcolor.X)),
3116 clamp_u8(myround(formspec_bgcolor.Y)),
3117 clamp_u8(myround(formspec_bgcolor.Z))
3122 v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
3123 m_fullscreen_bgcolor = video::SColor(
3124 (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
3125 clamp_u8(myround(formspec_bgcolor.X)),
3126 clamp_u8(myround(formspec_bgcolor.Y)),
3127 clamp_u8(myround(formspec_bgcolor.Z))
3131 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
3132 m_default_tooltip_color = video::SColor(255,255,255,255);
3136 assert(!m_tooltip_element);
3137 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
3138 m_tooltip_element = gui::StaticText::add(Environment, L"",
3139 core::rect<s32>(0, 0, 110, 18));
3140 m_tooltip_element->enableOverrideColor(true);
3141 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
3142 m_tooltip_element->setDrawBackground(true);
3143 m_tooltip_element->setDrawBorder(true);
3144 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
3145 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
3146 m_tooltip_element->setWordWrap(false);
3147 //we're not parent so no autograb for this one!
3148 m_tooltip_element->grab();
3151 std::vector<std::string> elements = split(m_formspec_string,']');
3154 /* try to read version from first element only */
3155 if (!elements.empty()) {
3156 if (parseVersionDirect(elements[0])) {
3161 /* we need size first in order to calculate image scale */
3162 mydata.explicit_size = false;
3163 for (; i< elements.size(); i++) {
3164 if (!parseSizeDirect(&mydata, elements[i])) {
3169 /* "position" element is always after "size" element if it used */
3170 for (; i< elements.size(); i++) {
3171 if (!parsePositionDirect(&mydata, elements[i])) {
3176 /* "anchor" element is always after "position" (or "size" element) if it used */
3177 for (; i< elements.size(); i++) {
3178 if (!parseAnchorDirect(&mydata, elements[i])) {
3183 /* "no_prepend" element is always after "position" (or "size" element) if it used */
3184 bool enable_prepends = true;
3185 for (; i < elements.size(); i++) {
3186 if (elements[i].empty())
3189 std::vector<std::string> parts = split(elements[i], '[');
3190 if (trim(parts[0]) == "no_prepend")
3191 enable_prepends = false;
3196 /* Copy of the "real_coordinates" element for after the form size. */
3197 mydata.real_coordinates = m_formspec_version >= 2;
3198 for (; i < elements.size(); i++) {
3199 std::vector<std::string> parts = split(elements[i], '[');
3200 std::string name = trim(parts[0]);
3201 if (name != "real_coordinates" || parts.size() != 2)
3202 break; // Invalid format
3204 mydata.real_coordinates = is_yes(trim(parts[1]));
3207 if (mydata.explicit_size) {
3208 // compute scaling for specified form size
3210 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
3211 v2u32 delta = current_screensize - m_lockscreensize;
3213 if (current_screensize.Y > m_lockscreensize.Y)
3218 if (current_screensize.X > m_lockscreensize.X)
3223 offset = v2s32(delta.X,delta.Y);
3225 mydata.screensize = m_lockscreensize;
3227 offset = v2s32(0,0);
3230 double gui_scaling = g_settings->getFloat("gui_scaling");
3231 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
3235 // In fixed-size mode, inventory image size
3236 // is 0.53 inch multiplied by the gui_scaling
3237 // config parameter. This magic size is chosen
3238 // to make the main menu (15.5 inventory images
3239 // wide, including border) just fit into the
3240 // default window (800 pixels wide) at 96 DPI
3241 // and default scaling (1.00).
3242 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
3244 // Variables for the maximum imgsize that can fit in the screen.
3245 double fitx_imgsize;
3246 double fity_imgsize;
3248 // Pad the screensize with 5% of the screensize on all sides to ensure
3249 // that even the largest formspecs don't touch the screen borders.
3250 v2f padded_screensize(
3251 mydata.screensize.X * 0.9f,
3252 mydata.screensize.Y * 0.9f
3255 if (mydata.real_coordinates) {
3256 fitx_imgsize = padded_screensize.X / mydata.invsize.X;
3257 fity_imgsize = padded_screensize.Y / mydata.invsize.Y;
3259 // The maximum imgsize in the old coordinate system also needs to
3260 // factor in padding and spacing along with 0.1 inventory slot spare
3261 // and help text space, hence the magic numbers.
3262 fitx_imgsize = padded_screensize.X /
3263 ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
3264 fity_imgsize = padded_screensize.Y /
3265 ((15.0 / 13.0) * (0.85 + mydata.invsize.Y));
3269 // In Android, the preferred imgsize should be larger to accommodate the
3270 // smaller screensize.
3271 double prefer_imgsize = padded_screensize.Y / 10 * gui_scaling;
3273 // Desktop computers have more space, so try to fit 15 coordinates.
3274 double prefer_imgsize = padded_screensize.Y / 15 * gui_scaling;
3276 // Try to use the preferred imgsize, but if that's bigger than the maximum
3277 // size, use the maximum size.
3278 use_imgsize = std::min(prefer_imgsize,
3279 std::min(fitx_imgsize, fity_imgsize));
3282 // Everything else is scaled in proportion to the
3283 // inventory image size. The inventory slot spacing
3284 // is 5/4 image size horizontally and 15/13 image size
3285 // vertically. The padding around the form (incorporating
3286 // the border of the outer inventory slots) is 3/8
3287 // image size. Font height (baseline to baseline)
3288 // is 2/5 vertical inventory slot spacing, and button
3289 // half-height is 7/8 of font height.
3290 imgsize = v2s32(use_imgsize, use_imgsize);
3291 spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
3292 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
3293 m_btn_height = use_imgsize*15.0/13 * 0.35;
3295 m_font = g_fontengine->getFont();
3297 if (mydata.real_coordinates) {
3298 mydata.size = v2s32(
3299 mydata.invsize.X*imgsize.X,
3300 mydata.invsize.Y*imgsize.Y
3303 mydata.size = v2s32(
3304 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
3305 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
3309 DesiredRect = mydata.rect = core::rect<s32>(
3310 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
3311 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
3312 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
3313 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
3316 // Non-size[] form must consist only of text fields and
3317 // implicit "Proceed" button. Use default font, and
3318 // temporary form size which will be recalculated below.
3319 m_font = g_fontengine->getFont();
3320 m_btn_height = font_line_height(m_font) * 0.875;
3321 DesiredRect = core::rect<s32>(
3322 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
3323 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
3324 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
3325 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
3328 recalculateAbsolutePosition(false);
3329 mydata.basepos = getBasePos();
3330 m_tooltip_element->setOverrideFont(m_font);
3332 gui::IGUISkin *skin = Environment->getSkin();
3334 gui::IGUIFont *old_font = skin->getFont();
3335 skin->setFont(m_font);
3337 pos_offset = v2f32();
3339 // used for formspec versions < 3
3340 core::list<IGUIElement *>::Iterator legacy_sort_start = Children.getLast();
3342 if (enable_prepends) {
3343 // Backup the coordinates so that prepends can use the coordinates of choice.
3344 bool rc_backup = mydata.real_coordinates;
3345 u16 version_backup = m_formspec_version;
3346 mydata.real_coordinates = false; // Old coordinates by default.
3348 std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
3349 for (const auto &element : prepend_elements)
3350 parseElement(&mydata, element);
3352 // legacy sorting for formspec versions < 3
3353 if (m_formspec_version >= 3)
3354 // prepends do not need to be reordered
3355 legacy_sort_start = Children.getLast();
3356 else if (version_backup >= 3)
3357 // only prepends elements have to be reordered
3358 legacySortElements(legacy_sort_start);
3360 m_formspec_version = version_backup;
3361 mydata.real_coordinates = rc_backup; // Restore coordinates
3364 for (; i< elements.size(); i++) {
3365 parseElement(&mydata, elements[i]);
3368 if (mydata.current_parent != this) {
3369 errorstream << "Invalid formspec string: scroll_container was never closed!"
3371 } else if (!container_stack.empty()) {
3372 errorstream << "Invalid formspec string: container was never closed!"
3376 // get the scrollbar elements for scroll_containers
3377 for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers) {
3378 for (const std::pair<FieldSpec, GUIScrollBar *> &b : m_scrollbars) {
3379 if (c.first == b.first.fname) {
3380 c.second->setScrollBar(b.second);
3386 // If there are fields without explicit size[], add a "Proceed"
3387 // button and adjust size to fit all the fields.
3388 if (mydata.simple_field_count > 0 && !mydata.explicit_size) {
3389 mydata.rect = core::rect<s32>(
3390 mydata.screensize.X / 2 - 580 / 2,
3391 mydata.screensize.Y / 2 - 300 / 2,
3392 mydata.screensize.X / 2 + 580 / 2,
3393 mydata.screensize.Y / 2 + 240 / 2 + mydata.simple_field_count * 60
3396 DesiredRect = mydata.rect;
3397 recalculateAbsolutePosition(false);
3398 mydata.basepos = getBasePos();
3401 v2s32 pos = mydata.basepos;
3402 pos.Y = (mydata.simple_field_count + 2) * 60;
3404 v2s32 size = DesiredRect.getSize();
3405 mydata.rect = core::rect<s32>(
3406 size.X / 2 - 70, pos.Y,
3407 size.X / 2 - 70 + 140, pos.Y + m_btn_height * 2
3409 const wchar_t *text = wgettext("Proceed");
3410 GUIButton::addButton(Environment, mydata.rect, m_tsrc, this, 257, text);
3415 // Set initial focus if parser didn't set it
3416 gui::IGUIElement *focused_element = Environment->getFocus();
3417 if (!focused_element
3418 || !isMyChild(focused_element)
3419 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
3422 skin->setFont(old_font);
3425 if (m_formspec_version < 3)
3426 legacySortElements(legacy_sort_start);
3428 // Formname and regeneration setting
3429 if (!m_is_form_regenerated) {
3430 // Only set previous form name if we purposefully showed a new formspec
3431 m_last_formname = m_text_dst->m_formname;
3432 m_is_form_regenerated = true;
3436 void GUIFormSpecMenu::legacySortElements(core::list<IGUIElement *>::Iterator from)
3439 Draw order for formspec_version <= 2:
3443 0 All other elements
3445 2 item_image, item_image_button
3450 if (from == Children.end())
3451 from = Children.begin();
3455 core::list<IGUIElement *>::Iterator to = Children.end();
3456 // 1: Copy into a sortable container
3457 std::vector<IGUIElement *> elements;
3458 for (auto it = from; it != to; ++it)
3459 elements.emplace_back(*it);
3461 // 2: Sort the container
3462 std::stable_sort(elements.begin(), elements.end(),
3463 [this] (const IGUIElement *a, const IGUIElement *b) -> bool {
3464 const FieldSpec *spec_a = getSpecByID(a->getID());
3465 const FieldSpec *spec_b = getSpecByID(b->getID());
3466 return spec_a && spec_b &&
3467 spec_a->priority < spec_b->priority;
3470 // 3: Re-assign the pointers
3471 for (auto e : elements) {
3478 bool GUIFormSpecMenu::getAndroidUIInput()
3480 if (!hasAndroidUIInput())
3484 if (porting::getInputDialogState() == -1)
3487 std::string fieldname = m_jni_field_name;
3488 m_jni_field_name.clear();
3490 for (const FieldSpec &field : m_fields) {
3491 if (field.fname != fieldname)
3494 IGUIElement *element = getElementFromId(field.fid, true);
3496 if (!element || element->getType() != irr::gui::EGUIET_EDIT_BOX)
3499 std::string text = porting::getInputDialogValue();
3500 ((gui::IGUIEditBox *)element)->setText(utf8_to_wide(text).c_str());
3506 GUIInventoryList::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
3508 for (const GUIInventoryList *e : m_inventorylists) {
3509 s32 item_index = e->getItemIndexAtPos(p);
3510 if (item_index != -1)
3511 return GUIInventoryList::ItemSpec(e->getInventoryloc(), e->getListname(),
3515 return GUIInventoryList::ItemSpec(InventoryLocation(), "", -1);
3518 void GUIFormSpecMenu::drawSelectedItem()
3520 video::IVideoDriver* driver = Environment->getVideoDriver();
3522 if (!m_selected_item) {
3523 // reset rotation time
3524 drawItemStack(driver, m_font, ItemStack(),
3525 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)), NULL,
3526 m_client, IT_ROT_DRAGGED);
3530 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3532 InventoryList *list = inv->getList(m_selected_item->listname);
3534 ItemStack stack = list->getItem(m_selected_item->i);
3535 stack.count = m_selected_amount;
3537 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
3538 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
3539 rect.constrainTo(driver->getViewPort());
3540 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
3543 void GUIFormSpecMenu::drawMenu()
3546 const std::string &newform = m_form_src->getForm();
3547 if (newform != m_formspec_string) {
3548 m_formspec_string = newform;
3549 m_is_form_regenerated = false;
3550 regenerateGui(m_screensize_old);
3554 gui::IGUISkin* skin = Environment->getSkin();
3555 sanity_check(skin != NULL);
3556 gui::IGUIFont *old_font = skin->getFont();
3557 skin->setFont(m_font);
3559 m_hovered_item_tooltips.clear();
3561 updateSelectedItem();
3563 video::IVideoDriver* driver = Environment->getVideoDriver();
3566 Draw background color
3568 v2u32 screenSize = driver->getScreenSize();
3569 core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
3572 driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
3573 if (m_bgnonfullscreen)
3574 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
3577 Draw rect_mode tooltip
3579 m_tooltip_element->setVisible(false);
3581 for (const auto &pair : m_tooltip_rects) {
3582 const core::rect<s32> &rect = pair.first->getAbsoluteClippingRect();
3583 if (rect.getArea() > 0 && rect.isPointInside(m_pointer)) {
3584 const std::wstring &text = pair.second.tooltip;
3585 if (!text.empty()) {
3586 showTooltip(text, pair.second.color, pair.second.bgcolor);
3595 for (gui::IGUIElement *e : m_backgrounds) {
3596 e->setVisible(true);
3598 e->setVisible(false);
3601 // Some elements are only visible while being drawn
3602 for (gui::IGUIElement *e : m_clickthrough_elements)
3603 e->setVisible(true);
3606 This is where all the drawing happens.
3608 core::list<IGUIElement*>::Iterator it = Children.begin();
3609 for (; it != Children.end(); ++it)
3610 if ((*it)->isNotClipped() ||
3611 AbsoluteClippingRect.isRectCollided(
3612 (*it)->getAbsolutePosition()))
3615 for (gui::IGUIElement *e : m_clickthrough_elements)
3616 e->setVisible(false);
3618 // Draw hovered item tooltips
3619 for (const std::string &tooltip : m_hovered_item_tooltips) {
3620 showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
3621 m_default_tooltip_bgcolor);
3624 if (m_hovered_item_tooltips.empty()) {
3625 // reset rotation time
3626 drawItemStack(driver, m_font, ItemStack(),
3627 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
3628 NULL, m_client, IT_ROT_HOVERED);
3631 /* TODO find way to show tooltips on touchscreen */
3632 #ifndef HAVE_TOUCHSCREENGUI
3633 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
3637 Draw fields/buttons tooltips and update the mouse cursor
3639 gui::IGUIElement *hovered =
3640 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
3642 #ifndef HAVE_TOUCHSCREENGUI
3643 gui::ICursorControl *cursor_control = RenderingEngine::get_raw_device()->
3645 gui::ECURSOR_ICON current_cursor_icon = cursor_control->getActiveIcon();
3647 bool hovered_element_found = false;
3649 if (hovered != NULL) {
3651 core::rect<s32> rect = hovered->getAbsoluteClippingRect();
3652 driver->draw2DRectangle(0x22FFFF00, rect, &rect);
3655 s32 id = hovered->getID();
3658 m_old_tooltip_id = id;
3660 if (id == m_old_tooltip_id) {
3661 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
3663 m_hovered_time = porting::getTimeMs();
3664 m_old_tooltip_id = id;
3668 // Find and update the current tooltip and cursor icon
3670 for (const FieldSpec &field : m_fields) {
3672 if (field.fid != id)
3675 if (delta >= m_tooltip_show_delay) {
3676 const std::wstring &text = m_tooltips[field.fname].tooltip;
3678 showTooltip(text, m_tooltips[field.fname].color,
3679 m_tooltips[field.fname].bgcolor);
3682 #ifndef HAVE_TOUCHSCREENGUI
3683 if (field.ftype != f_HyperText && // Handled directly in guiHyperText
3684 current_cursor_icon != field.fcursor_icon)
3685 cursor_control->setActiveIcon(field.fcursor_icon);
3688 hovered_element_found = true;
3695 if (!hovered_element_found) {
3696 // no element is hovered
3697 #ifndef HAVE_TOUCHSCREENGUI
3698 if (current_cursor_icon != ECI_NORMAL)
3699 cursor_control->setActiveIcon(ECI_NORMAL);
3703 m_tooltip_element->draw();
3706 Draw dragged item stack
3710 skin->setFont(old_font);
3714 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
3715 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
3717 EnrichedString ntext(text);
3718 ntext.setDefaultColor(color);
3719 if (!ntext.hasBackground())
3720 ntext.setBackground(bgcolor);
3722 setStaticText(m_tooltip_element, ntext);
3724 // Tooltip size and offset
3725 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
3726 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
3728 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
3729 int tooltip_offset_x = m_btn_height;
3730 int tooltip_offset_y = m_btn_height;
3732 tooltip_offset_x *= 3;
3733 tooltip_offset_y = 0;
3734 if (m_pointer.X > (s32)screenSize.X / 2)
3735 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
3737 // Hide tooltip after ETIE_LEFT_UP
3738 if (m_pointer.X == 0)
3742 // Calculate and set the tooltip position
3743 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
3744 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
3745 if (tooltip_x + tooltip_width > (s32)screenSize.X)
3746 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
3747 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
3748 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
3750 m_tooltip_element->setRelativePosition(
3752 core::position2d<s32>(tooltip_x, tooltip_y),
3753 core::dimension2d<s32>(tooltip_width, tooltip_height)
3757 // Display the tooltip
3758 m_tooltip_element->setVisible(true);
3759 bringToFront(m_tooltip_element);
3762 void GUIFormSpecMenu::updateSelectedItem()
3764 verifySelectedItem();
3766 // If craftresult is nonempty and nothing else is selected, select it now.
3767 if (!m_selected_item) {
3768 for (const GUIInventoryList *e : m_inventorylists) {
3769 if (e->getListname() != "craftpreview")
3772 Inventory *inv = m_invmgr->getInventory(e->getInventoryloc());
3776 InventoryList *list = inv->getList("craftresult");
3778 if (!list || list->getSize() == 0)
3781 const ItemStack &item = list->getItem(0);
3785 // Grab selected item from the crafting result list
3786 m_selected_item = new GUIInventoryList::ItemSpec;
3787 m_selected_item->inventoryloc = e->getInventoryloc();
3788 m_selected_item->listname = "craftresult";
3789 m_selected_item->i = 0;
3790 m_selected_amount = item.count;
3791 m_selected_dragging = false;
3796 // If craftresult is selected, keep the whole stack selected
3797 if (m_selected_item && m_selected_item->listname == "craftresult")
3798 m_selected_amount = verifySelectedItem().count;
3801 ItemStack GUIFormSpecMenu::verifySelectedItem()
3803 // If the selected stack has become empty for some reason, deselect it.
3804 // If the selected stack has become inaccessible, deselect it.
3805 // If the selected stack has become smaller, adjust m_selected_amount.
3806 // Return the selected stack.
3808 if (m_selected_item) {
3809 if (m_selected_item->isValid()) {
3810 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3812 InventoryList *list = inv->getList(m_selected_item->listname);
3813 if (list && (u32) m_selected_item->i < list->getSize()) {
3814 ItemStack stack = list->getItem(m_selected_item->i);
3815 if (!m_selected_swap.empty()) {
3816 if (m_selected_swap.name == stack.name &&
3817 m_selected_swap.count == stack.count)
3818 m_selected_swap.clear();
3820 m_selected_amount = std::min(m_selected_amount, stack.count);
3829 // selection was not valid
3830 delete m_selected_item;
3831 m_selected_item = nullptr;
3832 m_selected_amount = 0;
3833 m_selected_dragging = false;
3838 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode)
3844 if (quitmode == quit_mode_accept) {
3845 fields["quit"] = "true";
3848 if (quitmode == quit_mode_cancel) {
3849 fields["quit"] = "true";
3850 m_text_dst->gotText(fields);
3854 if (current_keys_pending.key_down) {
3855 fields["key_down"] = "true";
3856 current_keys_pending.key_down = false;
3859 if (current_keys_pending.key_up) {
3860 fields["key_up"] = "true";
3861 current_keys_pending.key_up = false;
3864 if (current_keys_pending.key_enter) {
3865 fields["key_enter"] = "true";
3866 current_keys_pending.key_enter = false;
3869 if (!current_field_enter_pending.empty()) {
3870 fields["key_enter_field"] = current_field_enter_pending;
3871 current_field_enter_pending = "";
3874 if (current_keys_pending.key_escape) {
3875 fields["key_escape"] = "true";
3876 current_keys_pending.key_escape = false;
3879 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
3881 std::string name = s.fname;
3882 if (s.ftype == f_Button) {
3883 fields[name] = wide_to_utf8(s.flabel);
3884 } else if (s.ftype == f_Table) {
3885 GUITable *table = getTable(s.fname);
3887 fields[name] = table->checkEvent();
3889 } else if (s.ftype == f_DropDown) {
3890 // No dynamic cast possible due to some distributions shipped
3891 // without rtti support in Irrlicht
3892 IGUIElement *element = getElementFromId(s.fid, true);
3893 gui::IGUIComboBox *e = NULL;
3894 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
3895 e = static_cast<gui::IGUIComboBox *>(element);
3897 warningstream << "GUIFormSpecMenu::acceptInput: dropdown "
3898 << "field without dropdown element" << std::endl;
3901 s32 selected = e->getSelected();
3902 if (selected >= 0) {
3903 if (m_dropdown_index_event.find(s.fname) !=
3904 m_dropdown_index_event.end()) {
3905 fields[name] = std::to_string(selected + 1);
3907 std::vector<std::string> *dropdown_values =
3908 getDropDownValues(s.fname);
3909 if (dropdown_values && selected < (s32)dropdown_values->size())
3910 fields[name] = (*dropdown_values)[selected];
3913 } else if (s.ftype == f_TabHeader) {
3914 // No dynamic cast possible due to some distributions shipped
3915 // without rtti support in Irrlicht
3916 IGUIElement *element = getElementFromId(s.fid, true);
3917 gui::IGUITabControl *e = nullptr;
3918 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
3919 e = static_cast<gui::IGUITabControl *>(element);
3923 std::stringstream ss;
3924 ss << (e->getActiveTab() +1);
3925 fields[name] = ss.str();
3927 } else if (s.ftype == f_CheckBox) {
3928 // No dynamic cast possible due to some distributions shipped
3929 // without rtti support in Irrlicht
3930 IGUIElement *element = getElementFromId(s.fid, true);
3931 gui::IGUICheckBox *e = nullptr;
3932 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
3933 e = static_cast<gui::IGUICheckBox*>(element);
3938 fields[name] = "true";
3940 fields[name] = "false";
3942 } else if (s.ftype == f_ScrollBar) {
3943 // No dynamic cast possible due to some distributions shipped
3944 // without rtti support in Irrlicht
3945 IGUIElement *element = getElementFromId(s.fid, true);
3946 GUIScrollBar *e = nullptr;
3947 if (element && element->getType() == gui::EGUIET_ELEMENT)
3948 e = static_cast<GUIScrollBar *>(element);
3951 std::stringstream os;
3953 if (s.fdefault == L"Changed")
3954 fields[name] = "CHG:" + os.str();
3956 fields[name] = "VAL:" + os.str();
3958 } else if (s.ftype == f_AnimatedImage) {
3959 // No dynamic cast possible due to some distributions shipped
3960 // without rtti support in Irrlicht
3961 IGUIElement *element = getElementFromId(s.fid, true);
3962 GUIAnimatedImage *e = nullptr;
3963 if (element && element->getType() == gui::EGUIET_ELEMENT)
3964 e = static_cast<GUIAnimatedImage *>(element);
3967 fields[name] = std::to_string(e->getFrameIndex() + 1);
3969 IGUIElement *e = getElementFromId(s.fid, true);
3971 fields[name] = wide_to_utf8(e->getText());
3976 m_text_dst->gotText(fields);
3980 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
3982 // The IGUITabControl renders visually using the skin's selected
3983 // font, which we override for the duration of form drawing,
3984 // but computes tab hotspots based on how it would have rendered
3985 // using the font that is selected at the time of button release.
3986 // To make these two consistent, temporarily override the skin's
3987 // font while the IGUITabControl is processing the event.
3988 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3989 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3990 s32 x = event.MouseInput.X;
3991 s32 y = event.MouseInput.Y;
3992 gui::IGUIElement *hovered =
3993 Environment->getRootGUIElement()->getElementFromPoint(
3994 core::position2d<s32>(x, y));
3995 if (hovered && isMyChild(hovered) &&
3996 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
3997 gui::IGUISkin* skin = Environment->getSkin();
3998 sanity_check(skin != NULL);
3999 gui::IGUIFont *old_font = skin->getFont();
4000 skin->setFont(m_font);
4001 bool retval = hovered->OnEvent(event);
4002 skin->setFont(old_font);
4007 // Fix Esc/Return key being eaten by checkboxen and tables
4008 if (event.EventType == EET_KEY_INPUT_EVENT) {
4009 KeyPress kp(event.KeyInput);
4010 if (kp == EscapeKey || kp == CancelKey
4011 || kp == getKeySetting("keymap_inventory")
4012 || event.KeyInput.Key==KEY_RETURN) {
4013 gui::IGUIElement *focused = Environment->getFocus();
4014 if (focused && isMyChild(focused) &&
4015 (focused->getType() == gui::EGUIET_LIST_BOX ||
4016 focused->getType() == gui::EGUIET_CHECK_BOX) &&
4017 (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
4018 event.KeyInput.Key != KEY_RETURN)) {
4024 // Mouse wheel and move events: send to hovered element instead of focused
4025 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
4026 (event.MouseInput.Event == EMIE_MOUSE_WHEEL ||
4027 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
4028 event.MouseInput.ButtonStates == 0))) {
4029 s32 x = event.MouseInput.X;
4030 s32 y = event.MouseInput.Y;
4031 gui::IGUIElement *hovered =
4032 Environment->getRootGUIElement()->getElementFromPoint(
4033 core::position2d<s32>(x, y));
4034 if (hovered && isMyChild(hovered)) {
4035 hovered->OnEvent(event);
4036 return event.MouseInput.Event == EMIE_MOUSE_WHEEL;
4040 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
4041 /* TODO add a check like:
4042 if (event.JoystickEvent != joystick_we_listen_for)
4045 bool handled = m_joystick->handleEvent(event.JoystickEvent);
4047 if (m_joystick->wasKeyDown(KeyType::ESC)) {
4049 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
4051 acceptInput(quit_mode_accept);
4059 return GUIModalMenu::preprocessEvent(event);
4062 void GUIFormSpecMenu::tryClose()
4066 acceptInput(quit_mode_cancel);
4069 m_text_dst->gotText(L"MenuQuit");
4073 enum ButtonEventType : u8
4086 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
4088 if (event.EventType==EET_KEY_INPUT_EVENT) {
4089 KeyPress kp(event.KeyInput);
4090 if (event.KeyInput.PressedDown && (
4091 (kp == EscapeKey) || (kp == CancelKey) ||
4092 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
4097 if (m_client != NULL && event.KeyInput.PressedDown &&
4098 (kp == getKeySetting("keymap_screenshot"))) {
4099 m_client->makeScreenshot();
4102 if (event.KeyInput.PressedDown && kp == getKeySetting("keymap_toggle_debug"))
4103 m_show_debug = !m_show_debug;
4105 if (event.KeyInput.PressedDown &&
4106 (event.KeyInput.Key==KEY_RETURN ||
4107 event.KeyInput.Key==KEY_UP ||
4108 event.KeyInput.Key==KEY_DOWN)
4110 switch (event.KeyInput.Key) {
4112 current_keys_pending.key_enter = true;
4115 current_keys_pending.key_up = true;
4118 current_keys_pending.key_down = true;
4122 //can't happen at all!
4123 FATAL_ERROR("Reached a source line that can't ever been reached");
4126 if (current_keys_pending.key_enter && m_allowclose) {
4127 acceptInput(quit_mode_accept);
4137 /* Mouse event other than movement, or crossing the border of inventory
4138 field while holding right mouse button
4140 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
4141 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
4142 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
4143 event.MouseInput.isRightPressed() &&
4144 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
4146 // Get selected item and hovered/clicked item (s)
4148 m_old_tooltip_id = -1;
4149 updateSelectedItem();
4150 GUIInventoryList::ItemSpec s = getItemAtPos(m_pointer);
4152 Inventory *inv_selected = NULL;
4153 Inventory *inv_s = NULL;
4154 InventoryList *list_s = NULL;
4156 if (m_selected_item) {
4157 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
4158 sanity_check(inv_selected);
4159 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
4166 inv_s = m_invmgr->getInventory(s.inventoryloc);
4169 errorstream << "InventoryMenu: The selected inventory location "
4170 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
4172 s.i = -1; // make it invalid again
4176 list_s = inv_s->getList(s.listname);
4177 if (list_s == NULL) {
4178 verbosestream << "InventoryMenu: The selected inventory list \""
4179 << s.listname << "\" does not exist" << std::endl;
4180 s.i = -1; // make it invalid again
4184 if ((u32)s.i >= list_s->getSize()) {
4185 infostream << "InventoryMenu: The selected inventory list \""
4186 << s.listname << "\" is too small (i=" << s.i << ", size="
4187 << list_s->getSize() << ")" << std::endl;
4188 s.i = -1; // make it invalid again
4192 s_count = list_s->getItem(s.i).count;
4195 bool identical = m_selected_item && s.isValid() &&
4196 (inv_selected == inv_s) &&
4197 (m_selected_item->listname == s.listname) &&
4198 (m_selected_item->i == s.i);
4200 ButtonEventType button = BET_LEFT;
4201 ButtonEventType updown = BET_OTHER;
4202 switch (event.MouseInput.Event) {
4203 case EMIE_LMOUSE_PRESSED_DOWN:
4204 button = BET_LEFT; updown = BET_DOWN;
4206 case EMIE_RMOUSE_PRESSED_DOWN:
4207 button = BET_RIGHT; updown = BET_DOWN;
4209 case EMIE_MMOUSE_PRESSED_DOWN:
4210 button = BET_MIDDLE; updown = BET_DOWN;
4212 case EMIE_MOUSE_WHEEL:
4213 button = (event.MouseInput.Wheel > 0) ?
4214 BET_WHEEL_UP : BET_WHEEL_DOWN;
4217 case EMIE_LMOUSE_LEFT_UP:
4218 button = BET_LEFT; updown = BET_UP;
4220 case EMIE_RMOUSE_LEFT_UP:
4221 button = BET_RIGHT; updown = BET_UP;
4223 case EMIE_MMOUSE_LEFT_UP:
4224 button = BET_MIDDLE; updown = BET_UP;
4226 case EMIE_MOUSE_MOVED:
4233 // Set this number to a positive value to generate a move action
4234 // from m_selected_item to s.
4235 u32 move_amount = 0;
4237 // Set this number to a positive value to generate a move action
4238 // from s to the next inventory ring.
4239 u32 shift_move_amount = 0;
4241 // Set this number to a positive value to generate a drop action
4242 // from m_selected_item.
4243 u32 drop_amount = 0;
4245 // Set this number to a positive value to generate a craft action at s.
4246 u32 craft_amount = 0;
4250 // Some mouse button has been pressed
4252 //infostream << "Mouse button " << button << " pressed at p=("
4253 // << event.MouseInput.X << "," << event.MouseInput.Y << ")"
4256 m_selected_dragging = false;
4258 if (s.isValid() && s.listname == "craftpreview") {
4259 // Craft preview has been clicked: craft
4260 craft_amount = (button == BET_MIDDLE ? 10 : 1);
4261 } else if (!m_selected_item) {
4262 if (s_count && button != BET_WHEEL_UP) {
4263 // Non-empty stack has been clicked: select or shift-move it
4264 m_selected_item = new GUIInventoryList::ItemSpec(s);
4267 if (button == BET_RIGHT)
4268 count = (s_count + 1) / 2;
4269 else if (button == BET_MIDDLE)
4270 count = MYMIN(s_count, 10);
4271 else if (button == BET_WHEEL_DOWN)
4276 if (!event.MouseInput.Shift) {
4277 // no shift: select item
4278 m_selected_amount = count;
4279 m_selected_dragging = button != BET_WHEEL_DOWN;
4280 m_auto_place = false;
4282 // shift pressed: move item, right click moves 1
4283 shift_move_amount = button == BET_RIGHT ? 1 : count;
4286 } else { // m_selected_item != NULL
4287 assert(m_selected_amount >= 1);
4290 // Clicked a slot: move
4291 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4293 else if (button == BET_MIDDLE)
4294 move_amount = MYMIN(m_selected_amount, 10);
4295 else if (button == BET_LEFT)
4296 move_amount = m_selected_amount;
4300 if (button == BET_WHEEL_DOWN) {
4301 if (m_selected_amount < s_count)
4302 ++m_selected_amount;
4304 if (move_amount >= m_selected_amount)
4305 m_selected_amount = 0;
4307 m_selected_amount -= move_amount;
4311 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
4312 && button != BET_WHEEL_DOWN) {
4313 // Clicked outside of the window: drop
4314 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4316 else if (button == BET_MIDDLE)
4317 drop_amount = MYMIN(m_selected_amount, 10);
4319 drop_amount = m_selected_amount;
4324 // Some mouse button has been released
4326 //infostream<<"Mouse button "<<button<<" released at p=("
4327 // <<p.X<<","<<p.Y<<")"<<std::endl;
4329 if (m_selected_dragging && m_selected_item) {
4332 // Dragged to different slot: move all selected
4333 move_amount = m_selected_amount;
4335 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
4336 // Dragged outside of window: drop all selected
4337 drop_amount = m_selected_amount;
4341 m_selected_dragging = false;
4342 // Keep track of whether the mouse button be released
4343 // One click is drag without dropping. Click + release
4344 // + click changes to drop item when moved mode
4345 if (m_selected_item)
4346 m_auto_place = true;
4349 // Mouse has been moved and rmb is down and mouse pointer just
4350 // entered a new inventory field (checked in the entry-if, this
4351 // is the only action here that is generated by mouse movement)
4352 if (m_selected_item && s.isValid() && s.listname != "craftpreview") {
4354 // TODO: middle mouse to move 10 items might be handy
4356 // Only move an item if the destination slot is empty
4357 // or contains the same item type as what is going to be
4359 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4360 InventoryList *list_to = list_s;
4361 assert(list_from && list_to);
4362 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4363 ItemStack stack_to = list_to->getItem(s.i);
4364 if (stack_to.empty() || stack_to.name == stack_from.name)
4373 // Possibly send inventory action to server
4374 if (move_amount > 0) {
4375 // Send IAction::Move
4377 assert(m_selected_item && m_selected_item->isValid());
4378 assert(s.isValid());
4380 assert(inv_selected && inv_s);
4381 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4382 InventoryList *list_to = list_s;
4383 assert(list_from && list_to);
4384 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4385 ItemStack stack_to = list_to->getItem(s.i);
4387 // Check how many items can be moved
4388 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
4389 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
4391 // If source stack cannot be added to destination stack at all,
4393 if (leftover.count == stack_from.count &&
4394 leftover.name == stack_from.name) {
4396 if (m_selected_swap.empty()) {
4397 m_selected_amount = stack_to.count;
4398 m_selected_dragging = false;
4400 // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
4401 // Skip next validation checks due async inventory calls
4402 m_selected_swap = stack_to;
4407 // Source stack goes fully into destination stack
4408 else if (leftover.empty()) {
4409 m_selected_amount -= move_amount;
4411 // Source stack goes partly into destination stack
4413 move_amount -= leftover.count;
4414 m_selected_amount -= move_amount;
4418 infostream << "Handing IAction::Move to manager" << std::endl;
4419 IMoveAction *a = new IMoveAction();
4420 a->count = move_amount;
4421 a->from_inv = m_selected_item->inventoryloc;
4422 a->from_list = m_selected_item->listname;
4423 a->from_i = m_selected_item->i;
4424 a->to_inv = s.inventoryloc;
4425 a->to_list = s.listname;
4427 m_invmgr->inventoryAction(a);
4429 } else if (shift_move_amount > 0) {
4430 u32 mis = m_inventory_rings.size();
4432 for (; i < mis; i++) {
4433 const ListRingSpec &sp = m_inventory_rings[i];
4434 if (sp.inventoryloc == s.inventoryloc
4435 && sp.listname == s.listname)
4439 if (i >= mis) // if not found
4441 u32 to_inv_ind = (i + 1) % mis;
4442 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
4443 InventoryList *list_from = list_s;
4446 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
4449 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
4452 ItemStack stack_from = list_from->getItem(s.i);
4453 assert(shift_move_amount <= stack_from.count);
4455 infostream << "Handing IAction::Move to manager" << std::endl;
4456 IMoveAction *a = new IMoveAction();
4457 a->count = shift_move_amount;
4458 a->from_inv = s.inventoryloc;
4459 a->from_list = s.listname;
4461 a->to_inv = to_inv_sp.inventoryloc;
4462 a->to_list = to_inv_sp.listname;
4463 a->move_somewhere = true;
4464 m_invmgr->inventoryAction(a);
4466 } else if (drop_amount > 0) {
4467 // Send IAction::Drop
4469 assert(m_selected_item && m_selected_item->isValid());
4470 assert(inv_selected);
4471 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4473 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4475 // Check how many items can be dropped
4476 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
4477 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
4478 m_selected_amount -= drop_amount;
4480 infostream << "Handing IAction::Drop to manager" << std::endl;
4481 IDropAction *a = new IDropAction();
4482 a->count = drop_amount;
4483 a->from_inv = m_selected_item->inventoryloc;
4484 a->from_list = m_selected_item->listname;
4485 a->from_i = m_selected_item->i;
4486 m_invmgr->inventoryAction(a);
4487 } else if (craft_amount > 0) {
4488 assert(s.isValid());
4490 // if there are no items selected or the selected item
4491 // belongs to craftresult list, proceed with crafting
4492 if (!m_selected_item ||
4493 !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
4497 // Send IACTION_CRAFT
4498 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
4499 ICraftAction *a = new ICraftAction();
4500 a->count = craft_amount;
4501 a->craft_inv = s.inventoryloc;
4502 m_invmgr->inventoryAction(a);
4506 // If m_selected_amount has been decreased to zero, deselect
4507 if (m_selected_amount == 0) {
4508 m_selected_swap.clear();
4509 delete m_selected_item;
4510 m_selected_item = nullptr;
4511 m_selected_amount = 0;
4512 m_selected_dragging = false;
4514 m_old_pointer = m_pointer;
4517 if (event.EventType == EET_GUI_EVENT) {
4518 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
4520 // find the element that was clicked
4521 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4522 if ((s.ftype == f_TabHeader) &&
4523 (s.fid == event.GUIEvent.Caller->getID())) {
4524 if (!s.sound.empty() && m_sound_manager)
4525 m_sound_manager->playSound(s.sound, false, 1.0f);
4533 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
4535 if (!canTakeFocus(event.GUIEvent.Element)) {
4536 infostream<<"GUIFormSpecMenu: Not allowing focus change."
4538 // Returning true disables focus change
4542 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
4543 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
4544 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
4545 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
4546 s32 caller_id = event.GUIEvent.Caller->getID();
4548 if (caller_id == 257) {
4550 acceptInput(quit_mode_accept);
4554 m_text_dst->gotText(L"ExitButton");
4556 // quitMenu deallocates menu
4560 // find the element that was clicked
4561 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4562 // if its a button, set the send field so
4563 // lua knows which button was pressed
4565 if (caller_id != s.fid)
4568 if (s.ftype == f_Button || s.ftype == f_CheckBox) {
4569 if (!s.sound.empty() && m_sound_manager)
4570 m_sound_manager->playSound(s.sound, false, 1.0f);
4575 acceptInput(quit_mode_accept);
4578 m_text_dst->gotText(L"ExitButton");
4583 acceptInput(quit_mode_no);
4587 } else if (s.ftype == f_DropDown) {
4588 // only send the changed dropdown
4589 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4590 if (s2.ftype == f_DropDown) {
4594 if (!s.sound.empty() && m_sound_manager)
4595 m_sound_manager->playSound(s.sound, false, 1.0f);
4597 acceptInput(quit_mode_no);
4599 // revert configuration to make sure dropdowns are sent on
4600 // regular button click
4601 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4602 if (s2.ftype == f_DropDown) {
4607 } else if (s.ftype == f_ScrollBar) {
4608 s.fdefault = L"Changed";
4609 acceptInput(quit_mode_no);
4611 } else if (s.ftype == f_Unknown || s.ftype == f_HyperText) {
4612 if (!s.sound.empty() && m_sound_manager)
4613 m_sound_manager->playSound(s.sound, false, 1.0f);
4621 if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) {
4622 // move scroll_containers
4623 for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers)
4624 c.second->onScrollEvent(event.GUIEvent.Caller);
4627 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
4628 if (event.GUIEvent.Caller->getID() > 257) {
4629 bool close_on_enter = true;
4630 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4631 if (s.ftype == f_Unknown &&
4632 s.fid == event.GUIEvent.Caller->getID()) {
4633 current_field_enter_pending = s.fname;
4634 std::unordered_map<std::string, bool>::const_iterator it =
4635 field_close_on_enter.find(s.fname);
4636 if (it != field_close_on_enter.end())
4637 close_on_enter = (*it).second;
4643 if (m_allowclose && close_on_enter) {
4644 current_keys_pending.key_enter = true;
4645 acceptInput(quit_mode_accept);
4648 current_keys_pending.key_enter = true;
4651 // quitMenu deallocates menu
4656 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
4657 int current_id = event.GUIEvent.Caller->getID();
4658 if (current_id > 257) {
4659 // find the element that was clicked
4660 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4661 // if it's a table, set the send field
4662 // so lua knows which table was changed
4663 if ((s.ftype == f_Table) && (s.fid == current_id)) {
4674 return Parent ? Parent->OnEvent(event) : false;
4678 * get name of element by element id
4679 * @param id of element
4680 * @return name string or empty string
4682 std::string GUIFormSpecMenu::getNameByID(s32 id)
4684 for (FieldSpec &spec : m_fields) {
4692 const GUIFormSpecMenu::FieldSpec *GUIFormSpecMenu::getSpecByID(s32 id)
4694 for (FieldSpec &spec : m_fields) {
4702 * get label of element by id
4703 * @param id of element
4704 * @return label string or empty string
4706 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
4708 for (FieldSpec &spec : m_fields) {
4715 StyleSpec GUIFormSpecMenu::getDefaultStyleForElement(const std::string &type,
4716 const std::string &name, const std::string &parent_type) {
4717 return getStyleForElement(type, name, parent_type)[StyleSpec::STATE_DEFAULT];
4720 std::array<StyleSpec, StyleSpec::NUM_STATES> GUIFormSpecMenu::getStyleForElement(
4721 const std::string &type, const std::string &name, const std::string &parent_type)
4723 std::array<StyleSpec, StyleSpec::NUM_STATES> ret;
4725 auto it = theme_by_type.find("*");
4726 if (it != theme_by_type.end()) {
4727 for (const StyleSpec &spec : it->second)
4728 ret[(u32)spec.getState()] |= spec;
4731 it = theme_by_name.find("*");
4732 if (it != theme_by_name.end()) {
4733 for (const StyleSpec &spec : it->second)
4734 ret[(u32)spec.getState()] |= spec;
4737 if (!parent_type.empty()) {
4738 it = theme_by_type.find(parent_type);
4739 if (it != theme_by_type.end()) {
4740 for (const StyleSpec &spec : it->second)
4741 ret[(u32)spec.getState()] |= spec;
4745 it = theme_by_type.find(type);
4746 if (it != theme_by_type.end()) {
4747 for (const StyleSpec &spec : it->second)
4748 ret[(u32)spec.getState()] |= spec;
4751 it = theme_by_name.find(name);
4752 if (it != theme_by_name.end()) {
4753 for (const StyleSpec &spec : it->second)
4754 ret[(u32)spec.getState()] |= spec;