3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "guiFormSpecMenu.h"
28 #include "constants.h"
30 #include "client/keycode.h"
31 #include "util/strfnd.h"
32 #include <IGUIButton.h>
33 #include <IGUICheckBox.h>
34 #include <IGUIComboBox.h>
35 #include <IGUIEditBox.h>
36 #include <IGUIStaticText.h>
38 #include <IGUITabControl.h>
39 #include "client/renderingengine.h"
41 #include "client/tile.h" // ITextureSource
42 #include "client/hud.h" // drawItemStack
46 #include "scripting_server.h"
47 #include "mainmenumanager.h"
50 #include "client/client.h"
51 #include "client/fontengine.h"
52 #include "client/sound.h"
54 #include "util/numeric.h"
55 #include "util/string.h" // for parseColorString()
56 #include "irrlicht_changes/static_text.h"
57 #include "client/guiscalingfilter.h"
58 #include "guiAnimatedImage.h"
59 #include "guiBackgroundImage.h"
61 #include "guiButton.h"
62 #include "guiButtonImage.h"
63 #include "guiButtonItemImage.h"
64 #include "guiEditBoxWithScrollbar.h"
65 #include "guiInventoryList.h"
66 #include "guiItemImage.h"
67 #include "guiScrollContainer.h"
68 #include "intlGUIEditBox.h"
69 #include "guiHyperText.h"
72 #define MY_CHECKPOS(a,b) \
73 if (v_pos.size() != 2) { \
74 errorstream<< "Invalid pos for element " << a << " specified: \"" \
75 << parts[b] << "\"" << std::endl; \
79 #define MY_CHECKGEOM(a,b) \
80 if (v_geom.size() != 2) { \
81 errorstream<< "Invalid geometry for element " << a << \
82 " specified: \"" << parts[b] << "\"" << std::endl; \
88 static unsigned int font_line_height(gui::IGUIFont *font)
90 return font->getDimension(L"Ay").Height + font->getKerningHeight();
93 inline u32 clamp_u8(s32 value)
95 return (u32) MYMIN(MYMAX(value, 0), 255);
98 GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
99 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
100 Client *client, ISimpleTextureSource *tsrc, ISoundManager *sound_manager,
101 IFormSource *fsrc, TextDest *tdst,
102 const std::string &formspecPrepend, bool remap_dbl_click):
103 GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr, remap_dbl_click),
106 m_sound_manager(sound_manager),
108 m_formspec_prepend(formspecPrepend),
113 current_keys_pending.key_down = false;
114 current_keys_pending.key_up = false;
115 current_keys_pending.key_enter = false;
116 current_keys_pending.key_escape = false;
118 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
119 m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
122 GUIFormSpecMenu::~GUIFormSpecMenu()
126 for (auto &table_it : m_tables)
127 table_it.second->drop();
128 for (auto &inventorylist_it : m_inventorylists)
129 inventorylist_it->drop();
130 for (auto &checkbox_it : m_checkboxes)
131 checkbox_it.second->drop();
132 for (auto &scrollbar_it : m_scrollbars)
133 scrollbar_it.second->drop();
134 for (auto &background_it : m_backgrounds)
135 background_it->drop();
136 for (auto &tooltip_rect_it : m_tooltip_rects)
137 tooltip_rect_it.first->drop();
138 for (auto &clickthrough_it : m_clickthrough_elements)
139 clickthrough_it->drop();
140 for (auto &scroll_container_it : m_scroll_containers)
141 scroll_container_it.second->drop();
143 delete m_selected_item;
148 void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
149 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
150 const std::string &formspecPrepend, ISoundManager *sound_manager)
152 if (cur_formspec == nullptr) {
153 cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
154 client, client->getTextureSource(), sound_manager, fs_src,
155 txt_dest, formspecPrepend);
156 cur_formspec->doPause = false;
159 Caution: do not call (*cur_formspec)->drop() here --
160 the reference might outlive the menu, so we will
161 periodically check if *cur_formspec is the only
162 remaining reference (i.e. the menu was removed)
163 and delete it in that case.
167 cur_formspec->setFormspecPrepend(formspecPrepend);
168 cur_formspec->setFormSource(fs_src);
169 cur_formspec->setTextDest(txt_dest);
173 void GUIFormSpecMenu::removeChildren()
175 const core::list<gui::IGUIElement*> &children = getChildren();
177 while (!children.empty()) {
178 (*children.getLast())->remove();
181 if (m_tooltip_element) {
182 m_tooltip_element->remove();
183 m_tooltip_element->drop();
184 m_tooltip_element = nullptr;
188 void GUIFormSpecMenu::setInitialFocus()
190 // Set initial focus according to following order of precedence:
191 // 1. first empty editbox
195 // 5. first focusable (not statictext, not tabheader)
196 // 6. first child element
198 core::list<gui::IGUIElement*> children = getChildren();
200 // in case "children" contains any NULL elements, remove them
201 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
202 it != children.end();) {
206 it = children.erase(it);
209 // 1. first empty editbox
210 for (gui::IGUIElement *it : children) {
211 if (it->getType() == gui::EGUIET_EDIT_BOX
212 && it->getText()[0] == 0) {
213 Environment->setFocus(it);
219 for (gui::IGUIElement *it : children) {
220 if (it->getType() == gui::EGUIET_EDIT_BOX) {
221 Environment->setFocus(it);
227 for (gui::IGUIElement *it : children) {
228 if (it->getTypeName() == std::string("GUITable")) {
229 Environment->setFocus(it);
235 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
236 it != children.end(); --it) {
237 if ((*it)->getType() == gui::EGUIET_BUTTON) {
238 Environment->setFocus(*it);
243 // 5. first focusable (not statictext, not tabheader)
244 for (gui::IGUIElement *it : children) {
245 if (it->getType() != gui::EGUIET_STATIC_TEXT &&
246 it->getType() != gui::EGUIET_TAB_CONTROL) {
247 Environment->setFocus(it);
252 // 6. first child element
253 if (children.empty())
254 Environment->setFocus(this);
256 Environment->setFocus(*(children.begin()));
259 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
261 for (auto &table : m_tables) {
262 if (tablename == table.first.fname)
268 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
270 for (auto &dropdown : m_dropdowns) {
271 if (name == dropdown.first.fname)
272 return &dropdown.second;
277 v2s32 GUIFormSpecMenu::getElementBasePos(const std::vector<std::string> *v_pos)
279 v2f32 pos_f = v2f32(padding.X, padding.Y) + pos_offset * spacing;
281 pos_f.X += stof((*v_pos)[0]) * spacing.X;
282 pos_f.Y += stof((*v_pos)[1]) * spacing.Y;
284 return v2s32(pos_f.X, pos_f.Y);
287 v2s32 GUIFormSpecMenu::getRealCoordinateBasePos(const std::vector<std::string> &v_pos)
289 return v2s32((stof(v_pos[0]) + pos_offset.X) * imgsize.X,
290 (stof(v_pos[1]) + pos_offset.Y) * imgsize.Y);
293 v2s32 GUIFormSpecMenu::getRealCoordinateGeometry(const std::vector<std::string> &v_geom)
295 return v2s32(stof(v_geom[0]) * imgsize.X, stof(v_geom[1]) * imgsize.Y);
298 void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
300 std::vector<std::string> parts = split(element,',');
302 if (((parts.size() == 2) || parts.size() == 3) ||
303 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
305 if (parts[1].find(';') != std::string::npos)
306 parts[1] = parts[1].substr(0,parts[1].find(';'));
308 data->invsize.X = MYMAX(0, stof(parts[0]));
309 data->invsize.Y = MYMAX(0, stof(parts[1]));
313 if (parts.size() == 3) {
314 if (parts[2] == "true") {
315 lockSize(true,v2u32(800,600));
319 data->explicit_size = true;
322 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
325 void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
327 std::vector<std::string> parts = split(element, ',');
329 if (parts.size() >= 2) {
330 if (parts[1].find(';') != std::string::npos)
331 parts[1] = parts[1].substr(0, parts[1].find(';'));
333 container_stack.push(pos_offset);
334 pos_offset.X += stof(parts[0]);
335 pos_offset.Y += stof(parts[1]);
338 errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
341 void GUIFormSpecMenu::parseContainerEnd(parserData* data)
343 if (container_stack.empty()) {
344 errorstream<< "Invalid container end element, no matching container start element" << std::endl;
346 pos_offset = container_stack.top();
347 container_stack.pop();
351 void GUIFormSpecMenu::parseScrollContainer(parserData *data, const std::string &element)
353 std::vector<std::string> parts = split(element, ';');
355 if (parts.size() < 4 ||
356 (parts.size() > 5 && m_formspec_version <= FORMSPEC_API_VERSION)) {
357 errorstream << "Invalid scroll_container start element (" << parts.size()
358 << "): '" << element << "'" << std::endl;
362 std::vector<std::string> v_pos = split(parts[0], ',');
363 std::vector<std::string> v_geom = split(parts[1], ',');
364 std::string scrollbar_name = parts[2];
365 std::string orientation = parts[3];
366 f32 scroll_factor = 0.1f;
367 if (parts.size() >= 5 && !parts[4].empty())
368 scroll_factor = stof(parts[4]);
370 MY_CHECKPOS("scroll_container", 0);
371 MY_CHECKGEOM("scroll_container", 1);
373 v2s32 pos = getRealCoordinateBasePos(v_pos);
374 v2s32 geom = getRealCoordinateGeometry(v_geom);
376 if (orientation == "vertical")
377 scroll_factor *= -imgsize.Y;
378 else if (orientation == "horizontal")
379 scroll_factor *= -imgsize.X;
381 warningstream << "GUIFormSpecMenu::parseScrollContainer(): "
382 << "Invalid scroll_container orientation: " << orientation
385 // old parent (at first: this)
386 // ^ is parent of clipper
387 // ^ is parent of mover
388 // ^ is parent of other elements
391 core::rect<s32> rect_clipper = core::rect<s32>(pos, pos + geom);
393 gui::IGUIElement *clipper = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
394 data->current_parent, 0, rect_clipper);
397 FieldSpec spec_mover(
401 258 + m_fields.size()
404 core::rect<s32> rect_mover = core::rect<s32>(0, 0, geom.X, geom.Y);
406 GUIScrollContainer *mover = new GUIScrollContainer(Environment,
407 clipper, spec_mover.fid, rect_mover, orientation, scroll_factor);
409 data->current_parent = mover;
411 m_scroll_containers.emplace_back(scrollbar_name, mover);
413 m_fields.push_back(spec_mover);
417 // remove interferring offset of normal containers
418 container_stack.push(pos_offset);
423 void GUIFormSpecMenu::parseScrollContainerEnd(parserData *data)
425 if (data->current_parent == this || data->current_parent->getParent() == this ||
426 container_stack.empty()) {
427 errorstream << "Invalid scroll_container end element, "
428 << "no matching scroll_container start element" << std::endl;
432 if (pos_offset.getLengthSQ() != 0.0f) {
433 // pos_offset is only set by containers and scroll_containers.
434 // scroll_containers always set it to 0,0 which means that if it is
435 // not 0,0, it is a normal container that was opened last, not a
437 errorstream << "Invalid scroll_container end element, "
438 << "an inner container was left open" << std::endl;
442 data->current_parent = data->current_parent->getParent()->getParent();
443 pos_offset = container_stack.top();
444 container_stack.pop();
447 void GUIFormSpecMenu::parseList(parserData *data, const std::string &element)
450 warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
454 std::vector<std::string> parts = split(element,';');
456 if (((parts.size() == 4) || (parts.size() == 5)) ||
457 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
459 std::string location = parts[0];
460 std::string listname = parts[1];
461 std::vector<std::string> v_pos = split(parts[2],',');
462 std::vector<std::string> v_geom = split(parts[3],',');
463 std::string startindex;
464 if (parts.size() == 5)
465 startindex = parts[4];
467 MY_CHECKPOS("list",2);
468 MY_CHECKGEOM("list",3);
470 InventoryLocation loc;
472 if (location == "context" || location == "current_name")
473 loc = m_current_inventory_location;
475 loc.deSerialize(location);
478 geom.X = stoi(v_geom[0]);
479 geom.Y = stoi(v_geom[1]);
482 if (!startindex.empty())
483 start_i = stoi(startindex);
485 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
486 errorstream << "Invalid list element: '" << element << "'" << std::endl;
490 if (!data->explicit_size)
491 warningstream << "invalid use of list without a size[] element" << std::endl;
497 258 + m_fields.size(),
501 auto style = getDefaultStyleForElement("list", spec.fname);
503 v2f32 slot_scale = style.getVector2f(StyleSpec::SIZE, v2f32(0, 0));
505 slot_scale.X <= 0 ? imgsize.X : std::max<f32>(slot_scale.X * imgsize.X, 1),
506 slot_scale.Y <= 0 ? imgsize.Y : std::max<f32>(slot_scale.Y * imgsize.Y, 1)
509 v2f32 slot_spacing = style.getVector2f(StyleSpec::SPACING, v2f32(-1, -1));
510 v2f32 default_spacing = data->real_coordinates ?
511 v2f32(imgsize.X * 0.25f, imgsize.Y * 0.25f) :
512 v2f32(spacing.X - imgsize.X, spacing.Y - imgsize.Y);
514 slot_spacing.X = slot_spacing.X < 0 ? default_spacing.X :
515 imgsize.X * slot_spacing.X;
516 slot_spacing.Y = slot_spacing.Y < 0 ? default_spacing.Y :
517 imgsize.Y * slot_spacing.Y;
519 slot_spacing += slot_size;
521 v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos) :
522 getElementBasePos(&v_pos);
524 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
525 pos.X + (geom.X - 1) * slot_spacing.X + slot_size.X,
526 pos.Y + (geom.Y - 1) * slot_spacing.Y + slot_size.Y);
528 GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent,
529 spec.fid, rect, m_invmgr, loc, listname, geom, start_i,
530 v2s32(slot_size.X, slot_size.Y), slot_spacing, this,
531 data->inventorylist_options, m_font);
533 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
535 m_inventorylists.push_back(e);
536 m_fields.push_back(spec);
539 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
542 void GUIFormSpecMenu::parseListRing(parserData *data, const std::string &element)
545 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
549 std::vector<std::string> parts = split(element, ';');
551 if (parts.size() == 2) {
552 std::string location = parts[0];
553 std::string listname = parts[1];
555 InventoryLocation loc;
557 if (location == "context" || location == "current_name")
558 loc = m_current_inventory_location;
560 loc.deSerialize(location);
562 m_inventory_rings.emplace_back(loc, listname);
566 if (element.empty() && m_inventorylists.size() > 1) {
567 size_t siz = m_inventorylists.size();
568 // insert the last two inv list elements into the list ring
569 const GUIInventoryList *spa = m_inventorylists[siz - 2];
570 const GUIInventoryList *spb = m_inventorylists[siz - 1];
571 m_inventory_rings.emplace_back(spa->getInventoryloc(), spa->getListname());
572 m_inventory_rings.emplace_back(spb->getInventoryloc(), spb->getListname());
576 errorstream<< "Invalid list ring element(" << parts.size() << ", "
577 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
580 void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
582 std::vector<std::string> parts = split(element,';');
584 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
585 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
587 std::vector<std::string> v_pos = split(parts[0],',');
588 std::string name = parts[1];
589 std::string label = parts[2];
590 std::string selected;
592 if (parts.size() >= 4)
595 MY_CHECKPOS("checkbox",0);
597 bool fselected = false;
599 if (selected == "true")
602 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
603 const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str());
604 s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH);
605 s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2;
608 core::rect<s32> rect;
610 if (data->real_coordinates) {
611 pos = getRealCoordinateBasePos(v_pos);
613 rect = core::rect<s32>(
616 pos.X + label_size.Width + cb_size + 7,
620 pos = getElementBasePos(&v_pos);
621 rect = core::rect<s32>(
623 pos.Y + imgsize.Y / 2 - y_center,
624 pos.X + label_size.Width + cb_size + 7,
625 pos.Y + imgsize.Y / 2 + y_center
631 wlabel, //Needed for displaying text on MSVC
636 spec.ftype = f_CheckBox;
638 gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect,
639 data->current_parent, spec.fid, spec.flabel.c_str());
641 auto style = getDefaultStyleForElement("checkbox", name);
643 spec.sound = style.get(StyleSpec::Property::SOUND, "");
645 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
647 if (spec.fname == m_focused_element) {
648 Environment->setFocus(e);
652 m_checkboxes.emplace_back(spec, e);
653 m_fields.push_back(spec);
656 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
659 void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
661 std::vector<std::string> parts = split(element,';');
663 if (parts.size() >= 5) {
664 std::vector<std::string> v_pos = split(parts[0],',');
665 std::vector<std::string> v_geom = split(parts[1],',');
666 std::string name = parts[3];
667 std::string value = parts[4];
669 MY_CHECKPOS("scrollbar",0);
670 MY_CHECKGEOM("scrollbar",1);
675 if (data->real_coordinates) {
676 pos = getRealCoordinateBasePos(v_pos);
677 dim = getRealCoordinateGeometry(v_geom);
679 pos = getElementBasePos(&v_pos);
680 dim.X = stof(v_geom[0]) * spacing.X;
681 dim.Y = stof(v_geom[1]) * spacing.Y;
684 core::rect<s32> rect =
685 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
694 bool is_horizontal = true;
696 if (parts[2] == "vertical")
697 is_horizontal = false;
699 spec.ftype = f_ScrollBar;
701 GUIScrollBar *e = new GUIScrollBar(Environment, data->current_parent,
702 spec.fid, rect, is_horizontal, true);
704 auto style = getDefaultStyleForElement("scrollbar", name);
705 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
706 e->setArrowsVisible(data->scrollbar_options.arrow_visiblity);
708 s32 max = data->scrollbar_options.max;
709 s32 min = data->scrollbar_options.min;
714 e->setPos(stoi(parts[4]));
716 e->setSmallStep(data->scrollbar_options.small_step);
717 e->setLargeStep(data->scrollbar_options.large_step);
719 s32 scrollbar_size = is_horizontal ? dim.X : dim.Y;
721 e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size);
723 if (spec.fname == m_focused_element) {
724 Environment->setFocus(e);
727 m_scrollbars.emplace_back(spec,e);
728 m_fields.push_back(spec);
731 errorstream << "Invalid scrollbar element(" << parts.size() << "): '" << element
735 void GUIFormSpecMenu::parseScrollBarOptions(parserData* data, const std::string &element)
737 std::vector<std::string> parts = split(element, ';');
739 if (parts.size() == 0) {
740 warningstream << "Invalid scrollbaroptions element(" << parts.size() << "): '" <<
741 element << "'" << std::endl;
745 for (const std::string &i : parts) {
746 std::vector<std::string> options = split(i, '=');
748 if (options.size() != 2) {
749 warningstream << "Invalid scrollbaroptions option syntax: '" <<
750 element << "'" << std::endl;
751 continue; // Go to next option
754 if (options[0] == "max") {
755 data->scrollbar_options.max = stoi(options[1]);
757 } else if (options[0] == "min") {
758 data->scrollbar_options.min = stoi(options[1]);
760 } else if (options[0] == "smallstep") {
761 int value = stoi(options[1]);
762 data->scrollbar_options.small_step = value < 0 ? 10 : value;
764 } else if (options[0] == "largestep") {
765 int value = stoi(options[1]);
766 data->scrollbar_options.large_step = value < 0 ? 100 : value;
768 } else if (options[0] == "thumbsize") {
769 int value = stoi(options[1]);
770 data->scrollbar_options.thumb_size = value <= 0 ? 1 : value;
772 } else if (options[0] == "arrows") {
773 std::string value = trim(options[1]);
775 data->scrollbar_options.arrow_visiblity = GUIScrollBar::HIDE;
776 else if (value == "show")
777 data->scrollbar_options.arrow_visiblity = GUIScrollBar::SHOW;
778 else // Auto hide/show
779 data->scrollbar_options.arrow_visiblity = GUIScrollBar::DEFAULT;
783 warningstream << "Invalid scrollbaroptions option(" << options[0] <<
784 "): '" << element << "'" << std::endl;
788 void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
790 std::vector<std::string> parts = split(element,';');
792 if ((parts.size() == 3) ||
793 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
795 std::vector<std::string> v_pos = split(parts[0],',');
796 std::vector<std::string> v_geom = split(parts[1],',');
797 std::string name = unescape_string(parts[2]);
799 MY_CHECKPOS("image", 0);
800 MY_CHECKGEOM("image", 1);
805 if (data->real_coordinates) {
806 pos = getRealCoordinateBasePos(v_pos);
807 geom = getRealCoordinateGeometry(v_geom);
809 pos = getElementBasePos(&v_pos);
810 geom.X = stof(v_geom[0]) * (float)imgsize.X;
811 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
814 if (!data->explicit_size)
815 warningstream<<"invalid use of image without a size[] element"<<std::endl;
817 video::ITexture *texture = m_tsrc->getTexture(name);
819 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
820 << std::endl << "\t" << name << std::endl;
828 258 + m_fields.size(),
831 core::rect<s32> rect(pos, pos + geom);
832 gui::IGUIImage *e = Environment->addImage(rect, data->current_parent,
834 e->setImage(texture);
835 e->setScaleImage(true);
836 auto style = getDefaultStyleForElement("image", spec.fname);
837 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
838 m_fields.push_back(spec);
840 // images should let events through
842 m_clickthrough_elements.push_back(e);
846 if (parts.size() == 2) {
847 std::vector<std::string> v_pos = split(parts[0],',');
848 std::string name = unescape_string(parts[1]);
850 MY_CHECKPOS("image", 0);
852 v2s32 pos = getElementBasePos(&v_pos);
854 if (!data->explicit_size)
855 warningstream<<"invalid use of image without a size[] element"<<std::endl;
857 video::ITexture *texture = m_tsrc->getTexture(name);
859 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
860 << std::endl << "\t" << name << std::endl;
868 258 + m_fields.size()
870 gui::IGUIImage *e = Environment->addImage(texture, pos, true,
871 data->current_parent, spec.fid, 0);
872 auto style = getDefaultStyleForElement("image", spec.fname);
873 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
874 m_fields.push_back(spec);
876 // images should let events through
878 m_clickthrough_elements.push_back(e);
881 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
884 void GUIFormSpecMenu::parseAnimatedImage(parserData *data, const std::string &element)
886 std::vector<std::string> parts = split(element, ';');
888 if (parts.size() != 6 && parts.size() != 7 &&
889 !(parts.size() > 7 && m_formspec_version > FORMSPEC_API_VERSION)) {
890 errorstream << "Invalid animated_image element(" << parts.size()
891 << "): '" << element << "'" << std::endl;
895 std::vector<std::string> v_pos = split(parts[0], ',');
896 std::vector<std::string> v_geom = split(parts[1], ',');
897 std::string name = parts[2];
898 std::string texture_name = unescape_string(parts[3]);
899 s32 frame_count = stoi(parts[4]);
900 s32 frame_duration = stoi(parts[5]);
902 MY_CHECKPOS("animated_image", 0);
903 MY_CHECKGEOM("animated_image", 1);
908 if (data->real_coordinates) {
909 pos = getRealCoordinateBasePos(v_pos);
910 geom = getRealCoordinateGeometry(v_geom);
912 pos = getElementBasePos(&v_pos);
913 geom.X = stof(v_geom[0]) * (float)imgsize.X;
914 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
917 if (!data->explicit_size)
918 warningstream << "Invalid use of animated_image without a size[] element" << std::endl;
924 258 + m_fields.size()
926 spec.ftype = f_AnimatedImage;
929 core::rect<s32> rect = core::rect<s32>(pos, pos + geom);
931 GUIAnimatedImage *e = new GUIAnimatedImage(Environment, data->current_parent, spec.fid,
932 rect, texture_name, frame_count, frame_duration, m_tsrc);
934 if (parts.size() >= 7)
935 e->setFrameIndex(stoi(parts[6]) - 1);
937 auto style = getDefaultStyleForElement("animated_image", spec.fname, "image");
938 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
940 // Animated images should let events through
941 m_clickthrough_elements.push_back(e);
943 m_fields.push_back(spec);
946 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
948 std::vector<std::string> parts = split(element,';');
950 if ((parts.size() == 3) ||
951 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
953 std::vector<std::string> v_pos = split(parts[0],',');
954 std::vector<std::string> v_geom = split(parts[1],',');
955 std::string name = parts[2];
957 MY_CHECKPOS("itemimage",0);
958 MY_CHECKGEOM("itemimage",1);
963 if (data->real_coordinates) {
964 pos = getRealCoordinateBasePos(v_pos);
965 geom = getRealCoordinateGeometry(v_geom);
967 pos = getElementBasePos(&v_pos);
968 geom.X = stof(v_geom[0]) * (float)imgsize.X;
969 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
972 if(!data->explicit_size)
973 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
979 258 + m_fields.size(),
982 spec.ftype = f_ItemImage;
984 GUIItemImage *e = new GUIItemImage(Environment, data->current_parent, spec.fid,
985 core::rect<s32>(pos, pos + geom), name, m_font, m_client);
986 auto style = getDefaultStyleForElement("item_image", spec.fname);
987 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
989 // item images should let events through
990 m_clickthrough_elements.push_back(e);
992 m_fields.push_back(spec);
995 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
998 void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
999 const std::string &type)
1001 std::vector<std::string> parts = split(element,';');
1003 if ((parts.size() == 4) ||
1004 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
1006 std::vector<std::string> v_pos = split(parts[0],',');
1007 std::vector<std::string> v_geom = split(parts[1],',');
1008 std::string name = parts[2];
1009 std::string label = parts[3];
1011 MY_CHECKPOS("button",0);
1012 MY_CHECKGEOM("button",1);
1016 core::rect<s32> rect;
1018 if (data->real_coordinates) {
1019 pos = getRealCoordinateBasePos(v_pos);
1020 geom = getRealCoordinateGeometry(v_geom);
1021 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1024 pos = getElementBasePos(&v_pos);
1025 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1026 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1028 rect = core::rect<s32>(pos.X, pos.Y - m_btn_height,
1029 pos.X + geom.X, pos.Y + m_btn_height);
1032 if(!data->explicit_size)
1033 warningstream<<"invalid use of button without a size[] element"<<std::endl;
1035 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1041 258 + m_fields.size()
1043 spec.ftype = f_Button;
1044 if(type == "button_exit")
1045 spec.is_exit = true;
1047 GUIButton *e = GUIButton::addButton(Environment, rect, m_tsrc,
1048 data->current_parent, spec.fid, spec.flabel.c_str());
1050 auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
1052 spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
1054 e->setStyles(style);
1056 if (spec.fname == m_focused_element) {
1057 Environment->setFocus(e);
1060 m_fields.push_back(spec);
1063 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
1066 void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
1068 std::vector<std::string> parts = split(element,';');
1070 if ((parts.size() >= 3 && parts.size() <= 5) ||
1071 (parts.size() > 5 && m_formspec_version > FORMSPEC_API_VERSION)) {
1072 std::vector<std::string> v_pos = split(parts[0],',');
1073 std::vector<std::string> v_geom = split(parts[1],',');
1074 std::string name = unescape_string(parts[2]);
1076 MY_CHECKPOS("background",0);
1077 MY_CHECKGEOM("background",1);
1082 if (data->real_coordinates) {
1083 pos = getRealCoordinateBasePos(v_pos);
1084 geom = getRealCoordinateGeometry(v_geom);
1086 pos = getElementBasePos(&v_pos);
1087 pos.X -= (spacing.X - (float)imgsize.X) / 2;
1088 pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
1090 geom.X = stof(v_geom[0]) * spacing.X;
1091 geom.Y = stof(v_geom[1]) * spacing.Y;
1095 if (parts.size() >= 4 && is_yes(parts[3])) {
1096 if (data->real_coordinates) {
1097 pos = getRealCoordinateBasePos(v_pos) * -1;
1100 pos.X = stoi(v_pos[0]); //acts as offset
1101 pos.Y = stoi(v_pos[1]);
1106 core::rect<s32> middle;
1107 if (parts.size() >= 5) {
1108 std::vector<std::string> v_middle = split(parts[4], ',');
1109 if (v_middle.size() == 1) {
1110 s32 x = stoi(v_middle[0]);
1111 middle.UpperLeftCorner = core::vector2di(x, x);
1112 middle.LowerRightCorner = core::vector2di(-x, -x);
1113 } else if (v_middle.size() == 2) {
1114 s32 x = stoi(v_middle[0]);
1115 s32 y = stoi(v_middle[1]);
1116 middle.UpperLeftCorner = core::vector2di(x, y);
1117 middle.LowerRightCorner = core::vector2di(-x, -y);
1118 // `-x` is interpreted as `w - x`
1119 } else if (v_middle.size() == 4) {
1120 middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1]));
1121 middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3]));
1123 warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl;
1127 if (!data->explicit_size && !clip)
1128 warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
1134 258 + m_fields.size()
1137 core::rect<s32> rect;
1139 // no auto_clip => position like normal image
1140 rect = core::rect<s32>(pos, pos + geom);
1142 // it will be auto-clipped when drawing
1143 rect = core::rect<s32>(-pos, pos);
1146 GUIBackgroundImage *e = new GUIBackgroundImage(Environment, this, spec.fid,
1147 rect, name, middle, m_tsrc, clip);
1149 FATAL_ERROR_IF(!e, "Failed to create background formspec element");
1151 e->setNotClipped(true);
1153 e->setVisible(false); // the element is drawn manually before all others
1155 m_backgrounds.push_back(e);
1156 m_fields.push_back(spec);
1159 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
1162 void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
1164 std::vector<std::string> parts = split(element,';');
1166 data->table_options.clear();
1167 for (const std::string &part : parts) {
1168 // Parse table option
1169 std::string opt = unescape_string(part);
1170 data->table_options.push_back(GUITable::splitOption(opt));
1174 void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
1176 std::vector<std::string> parts = split(element,';');
1178 data->table_columns.clear();
1179 for (const std::string &part : parts) {
1180 std::vector<std::string> col_parts = split(part,',');
1181 GUITable::TableColumn column;
1182 // Parse column type
1183 if (!col_parts.empty())
1184 column.type = col_parts[0];
1185 // Parse column options
1186 for (size_t j = 1; j < col_parts.size(); ++j) {
1187 std::string opt = unescape_string(col_parts[j]);
1188 column.options.push_back(GUITable::splitOption(opt));
1190 data->table_columns.push_back(column);
1194 void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
1196 std::vector<std::string> parts = split(element,';');
1198 if (((parts.size() == 4) || (parts.size() == 5)) ||
1199 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1201 std::vector<std::string> v_pos = split(parts[0],',');
1202 std::vector<std::string> v_geom = split(parts[1],',');
1203 std::string name = parts[2];
1204 std::vector<std::string> items = split(parts[3],',');
1205 std::string str_initial_selection;
1206 std::string str_transparent = "false";
1208 if (parts.size() >= 5)
1209 str_initial_selection = parts[4];
1211 MY_CHECKPOS("table",0);
1212 MY_CHECKGEOM("table",1);
1217 if (data->real_coordinates) {
1218 pos = getRealCoordinateBasePos(v_pos);
1219 geom = getRealCoordinateGeometry(v_geom);
1221 pos = getElementBasePos(&v_pos);
1222 geom.X = stof(v_geom[0]) * spacing.X;
1223 geom.Y = stof(v_geom[1]) * spacing.Y;
1226 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1232 258 + m_fields.size()
1235 spec.ftype = f_Table;
1237 for (std::string &item : items) {
1238 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1241 //now really show table
1242 GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
1245 if (spec.fname == m_focused_element) {
1246 Environment->setFocus(e);
1249 e->setTable(data->table_options, data->table_columns, items);
1251 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1252 e->setDynamicData(data->table_dyndata[name]);
1255 if (!str_initial_selection.empty() && str_initial_selection != "0")
1256 e->setSelected(stoi(str_initial_selection));
1258 auto style = getDefaultStyleForElement("table", name);
1259 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1260 e->setOverrideFont(style.getFont());
1262 m_tables.emplace_back(spec, e);
1263 m_fields.push_back(spec);
1266 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
1269 void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
1271 std::vector<std::string> parts = split(element,';');
1273 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
1274 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1276 std::vector<std::string> v_pos = split(parts[0],',');
1277 std::vector<std::string> v_geom = split(parts[1],',');
1278 std::string name = parts[2];
1279 std::vector<std::string> items = split(parts[3],',');
1280 std::string str_initial_selection;
1281 std::string str_transparent = "false";
1283 if (parts.size() >= 5)
1284 str_initial_selection = parts[4];
1286 if (parts.size() >= 6)
1287 str_transparent = parts[5];
1289 MY_CHECKPOS("textlist",0);
1290 MY_CHECKGEOM("textlist",1);
1295 if (data->real_coordinates) {
1296 pos = getRealCoordinateBasePos(v_pos);
1297 geom = getRealCoordinateGeometry(v_geom);
1299 pos = getElementBasePos(&v_pos);
1300 geom.X = stof(v_geom[0]) * spacing.X;
1301 geom.Y = stof(v_geom[1]) * spacing.Y;
1304 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1310 258 + m_fields.size()
1313 spec.ftype = f_Table;
1315 for (std::string &item : items) {
1316 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1319 //now really show list
1320 GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
1323 if (spec.fname == m_focused_element) {
1324 Environment->setFocus(e);
1327 e->setTextList(items, is_yes(str_transparent));
1329 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1330 e->setDynamicData(data->table_dyndata[name]);
1333 if (!str_initial_selection.empty() && str_initial_selection != "0")
1334 e->setSelected(stoi(str_initial_selection));
1336 auto style = getDefaultStyleForElement("textlist", name);
1337 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1338 e->setOverrideFont(style.getFont());
1340 m_tables.emplace_back(spec, e);
1341 m_fields.push_back(spec);
1344 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
1347 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
1349 std::vector<std::string> parts = split(element, ';');
1351 if (parts.size() == 5 || parts.size() == 6 ||
1352 (parts.size() > 6 && m_formspec_version > FORMSPEC_API_VERSION))
1354 std::vector<std::string> v_pos = split(parts[0], ',');
1355 std::string name = parts[2];
1356 std::vector<std::string> items = split(parts[3], ',');
1357 std::string str_initial_selection = parts[4];
1359 if (parts.size() >= 6 && is_yes(parts[5]))
1360 m_dropdown_index_event[name] = true;
1362 MY_CHECKPOS("dropdown",0);
1366 core::rect<s32> rect;
1368 if (data->real_coordinates) {
1369 std::vector<std::string> v_geom = split(parts[1],',');
1371 if (v_geom.size() == 1)
1372 v_geom.emplace_back("1");
1374 MY_CHECKGEOM("dropdown",1);
1376 pos = getRealCoordinateBasePos(v_pos);
1377 geom = getRealCoordinateGeometry(v_geom);
1378 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1380 pos = getElementBasePos(&v_pos);
1382 s32 width = stof(parts[1]) * spacing.Y;
1384 rect = core::rect<s32>(pos.X, pos.Y,
1385 pos.X + width, pos.Y + (m_btn_height * 2));
1392 258 + m_fields.size()
1395 spec.ftype = f_DropDown;
1398 //now really show list
1399 gui::IGUIComboBox *e = Environment->addComboBox(rect, data->current_parent,
1402 if (spec.fname == m_focused_element) {
1403 Environment->setFocus(e);
1406 for (const std::string &item : items) {
1407 e->addItem(unescape_translate(unescape_string(
1408 utf8_to_wide(item))).c_str());
1411 if (!str_initial_selection.empty())
1412 e->setSelected(stoi(str_initial_selection)-1);
1414 auto style = getDefaultStyleForElement("dropdown", name);
1416 spec.sound = style.get(StyleSpec::Property::SOUND, "");
1418 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1420 m_fields.push_back(spec);
1422 m_dropdowns.emplace_back(spec, std::vector<std::string>());
1423 std::vector<std::string> &values = m_dropdowns.back().second;
1424 for (const std::string &item : items) {
1425 values.push_back(unescape_string(item));
1430 errorstream << "Invalid dropdown element(" << parts.size() << "): '" << element
1431 << "'" << std::endl;
1434 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
1436 std::vector<std::string> parts = split(element,';');
1437 if (parts.size() == 2 ||
1438 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
1439 field_close_on_enter[parts[0]] = is_yes(parts[1]);
1443 void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
1445 std::vector<std::string> parts = split(element,';');
1447 if (parts.size() == 4 ||
1448 (parts.size() > 4 && m_formspec_version > FORMSPEC_API_VERSION))
1450 std::vector<std::string> v_pos = split(parts[0],',');
1451 std::vector<std::string> v_geom = split(parts[1],',');
1452 std::string name = parts[2];
1453 std::string label = parts[3];
1455 MY_CHECKPOS("pwdfield",0);
1456 MY_CHECKGEOM("pwdfield",1);
1461 if (data->real_coordinates) {
1462 pos = getRealCoordinateBasePos(v_pos);
1463 geom = getRealCoordinateGeometry(v_geom);
1465 pos = getElementBasePos(&v_pos);
1468 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1470 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1471 pos.Y -= m_btn_height;
1472 geom.Y = m_btn_height*2;
1475 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1477 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1483 258 + m_fields.size(),
1489 gui::IGUIEditBox *e = Environment->addEditBox(0, rect, true,
1490 data->current_parent, spec.fid);
1492 if (spec.fname == m_focused_element) {
1493 Environment->setFocus(e);
1496 if (label.length() >= 1) {
1497 int font_height = g_fontengine->getTextHeight();
1498 rect.UpperLeftCorner.Y -= font_height;
1499 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1500 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1501 data->current_parent, 0);
1504 e->setPasswordBox(true,L'*');
1506 auto style = getDefaultStyleForElement("pwdfield", name, "field");
1507 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1508 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1509 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1510 e->setOverrideFont(style.getFont());
1513 evt.EventType = EET_KEY_INPUT_EVENT;
1514 evt.KeyInput.Key = KEY_END;
1515 evt.KeyInput.Char = 0;
1516 evt.KeyInput.Control = false;
1517 evt.KeyInput.Shift = false;
1518 evt.KeyInput.PressedDown = true;
1521 // Note: Before 5.2.0 "parts.size() >= 5" resulted in a
1522 // warning referring to field_close_on_enter[]!
1524 m_fields.push_back(spec);
1527 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
1530 void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
1531 core::rect<s32> &rect, bool is_multiline)
1533 bool is_editable = !spec.fname.empty();
1534 if (!is_editable && !is_multiline) {
1535 // spec field id to 0, this stops submit searching for a value that isn't there
1536 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1537 data->current_parent, 0);
1543 } else if (is_multiline &&
1544 spec.fdefault.empty() && !spec.flabel.empty()) {
1545 // Multiline textareas: swap default and label for backwards compat
1546 spec.flabel.swap(spec.fdefault);
1549 gui::IGUIEditBox *e = nullptr;
1550 static constexpr bool use_intl_edit_box = USE_FREETYPE &&
1551 IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9;
1553 if (use_intl_edit_box && g_settings->getBool("freetype")) {
1554 e = new gui::intlGUIEditBox(spec.fdefault.c_str(), true, Environment,
1555 data->current_parent, spec.fid, rect, is_editable, is_multiline);
1558 e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true, Environment,
1559 data->current_parent, spec.fid, rect, is_editable, true);
1560 } else if (is_editable) {
1561 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
1562 data->current_parent, spec.fid);
1567 auto style = getDefaultStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
1570 if (is_editable && spec.fname == m_focused_element)
1571 Environment->setFocus(e);
1574 e->setMultiLine(true);
1575 e->setWordWrap(true);
1576 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1579 evt.EventType = EET_KEY_INPUT_EVENT;
1580 evt.KeyInput.Key = KEY_END;
1581 evt.KeyInput.Char = 0;
1582 evt.KeyInput.Control = 0;
1583 evt.KeyInput.Shift = 0;
1584 evt.KeyInput.PressedDown = true;
1588 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1589 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1590 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1591 if (style.get(StyleSpec::BGCOLOR, "") == "transparent") {
1592 e->setDrawBackground(false);
1594 e->setOverrideFont(style.getFont());
1599 if (!spec.flabel.empty()) {
1600 int font_height = g_fontengine->getTextHeight();
1601 rect.UpperLeftCorner.Y -= font_height;
1602 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1603 IGUIElement *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
1604 rect, false, true, data->current_parent, 0);
1607 t->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1611 void GUIFormSpecMenu::parseSimpleField(parserData *data,
1612 std::vector<std::string> &parts)
1614 std::string name = parts[0];
1615 std::string label = parts[1];
1616 std::string default_val = parts[2];
1618 core::rect<s32> rect;
1620 if (data->explicit_size)
1621 warningstream << "invalid use of unpositioned \"field\" in inventory" << std::endl;
1623 v2s32 pos = getElementBasePos(nullptr);
1624 pos.Y = (data->simple_field_count + 2) * 60;
1625 v2s32 size = DesiredRect.getSize();
1627 rect = core::rect<s32>(
1628 size.X / 2 - 150, pos.Y,
1629 size.X / 2 - 150 + 300, pos.Y + m_btn_height * 2
1634 default_val = m_form_src->resolveText(default_val);
1637 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1642 utf8_to_wide(unescape_string(default_val)),
1643 258 + m_fields.size(),
1648 createTextField(data, spec, rect, false);
1650 m_fields.push_back(spec);
1652 data->simple_field_count++;
1655 void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
1656 const std::string &type)
1658 std::vector<std::string> v_pos = split(parts[0],',');
1659 std::vector<std::string> v_geom = split(parts[1],',');
1660 std::string name = parts[2];
1661 std::string label = parts[3];
1662 std::string default_val = parts[4];
1664 MY_CHECKPOS(type,0);
1665 MY_CHECKGEOM(type,1);
1670 if (data->real_coordinates) {
1671 pos = getRealCoordinateBasePos(v_pos);
1672 geom = getRealCoordinateGeometry(v_geom);
1674 pos = getElementBasePos(&v_pos);
1677 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1679 if (type == "textarea")
1681 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1682 pos.Y += m_btn_height;
1686 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1687 pos.Y -= m_btn_height;
1688 geom.Y = m_btn_height*2;
1692 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1694 if(!data->explicit_size)
1695 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1698 default_val = m_form_src->resolveText(default_val);
1701 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1706 utf8_to_wide(unescape_string(default_val)),
1707 258 + m_fields.size(),
1712 createTextField(data, spec, rect, type == "textarea");
1714 // Note: Before 5.2.0 "parts.size() >= 6" resulted in a
1715 // warning referring to field_close_on_enter[]!
1717 m_fields.push_back(spec);
1720 void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
1721 const std::string &type)
1723 std::vector<std::string> parts = split(element,';');
1725 if (parts.size() == 3 || parts.size() == 4) {
1726 parseSimpleField(data,parts);
1730 if ((parts.size() == 5) ||
1731 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1733 parseTextArea(data,parts,type);
1736 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1739 void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &element)
1741 std::vector<std::string> parts = split(element, ';');
1743 if (parts.size() != 4 && m_formspec_version < FORMSPEC_API_VERSION) {
1744 errorstream << "Invalid text element(" << parts.size() << "): '" << element << "'" << std::endl;
1748 std::vector<std::string> v_pos = split(parts[0], ',');
1749 std::vector<std::string> v_geom = split(parts[1], ',');
1750 std::string name = parts[2];
1751 std::string text = parts[3];
1753 MY_CHECKPOS("hypertext", 0);
1754 MY_CHECKGEOM("hypertext", 1);
1759 if (data->real_coordinates) {
1760 pos = getRealCoordinateBasePos(v_pos);
1761 geom = getRealCoordinateGeometry(v_geom);
1763 pos = getElementBasePos(&v_pos);
1766 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1767 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y - imgsize.Y);
1768 pos.Y += m_btn_height;
1771 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X + geom.X, pos.Y + geom.Y);
1774 text = m_form_src->resolveText(text);
1778 translate_string(utf8_to_wide(unescape_string(text))),
1780 258 + m_fields.size()
1783 spec.ftype = f_HyperText;
1785 auto style = getDefaultStyleForElement("hypertext", spec.fname);
1786 spec.sound = style.get(StyleSpec::Property::SOUND, "");
1788 GUIHyperText *e = new GUIHyperText(spec.flabel.c_str(), Environment,
1789 data->current_parent, spec.fid, rect, m_client, m_tsrc);
1792 m_fields.push_back(spec);
1795 void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
1797 std::vector<std::string> parts = split(element,';');
1799 if ((parts.size() == 2) ||
1800 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1802 std::vector<std::string> v_pos = split(parts[0],',');
1803 std::string text = parts[1];
1805 MY_CHECKPOS("label",0);
1807 if(!data->explicit_size)
1808 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1810 std::vector<std::string> lines = split(text, '\n');
1812 auto style = getDefaultStyleForElement("label", "");
1813 gui::IGUIFont *font = style.getFont();
1817 for (unsigned int i = 0; i != lines.size(); i++) {
1818 std::wstring wlabel_colors = translate_string(
1819 utf8_to_wide(unescape_string(lines[i])));
1820 // Without color escapes to get the font dimensions
1821 std::wstring wlabel_plain = unescape_enriched(wlabel_colors);
1823 core::rect<s32> rect;
1825 if (data->real_coordinates) {
1826 // Lines are spaced at the distance of 1/2 imgsize.
1827 // This alows lines that line up with the new elements
1828 // easily without sacrificing good line distance. If
1829 // it was one whole imgsize, it would have too much
1831 v2s32 pos = getRealCoordinateBasePos(v_pos);
1833 // Labels are positioned by their center, not their top.
1834 pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2);
1836 rect = core::rect<s32>(
1838 pos.X + font->getDimension(wlabel_plain.c_str()).Width,
1842 // Lines are spaced at the nominal distance of
1843 // 2/5 inventory slot, even if the font doesn't
1844 // quite match that. This provides consistent
1845 // form layout, at the expense of sometimes
1846 // having sub-optimal spacing for the font.
1847 // We multiply by 2 and then divide by 5, rather
1848 // than multiply by 0.4, to get exact results
1849 // in the integer cases: 0.4 is not exactly
1850 // representable in binary floating point.
1852 v2s32 pos = getElementBasePos(nullptr);
1853 pos.X += stof(v_pos[0]) * spacing.X;
1854 pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
1856 pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0;
1858 rect = core::rect<s32>(
1859 pos.X, pos.Y - m_btn_height,
1860 pos.X + font->getDimension(wlabel_plain.c_str()).Width,
1861 pos.Y + m_btn_height);
1868 258 + m_fields.size(),
1871 gui::IGUIStaticText *e = gui::StaticText::add(Environment,
1872 spec.flabel.c_str(), rect, false, false, data->current_parent,
1874 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
1876 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1877 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1878 e->setOverrideFont(font);
1880 m_fields.push_back(spec);
1882 // labels should let events through
1884 m_clickthrough_elements.push_back(e);
1889 errorstream << "Invalid label element(" << parts.size() << "): '" << element
1890 << "'" << std::endl;
1893 void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
1895 std::vector<std::string> parts = split(element,';');
1897 if ((parts.size() == 2) ||
1898 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1900 std::vector<std::string> v_pos = split(parts[0],',');
1901 std::wstring text = unescape_translate(
1902 unescape_string(utf8_to_wide(parts[1])));
1904 MY_CHECKPOS("vertlabel",1);
1906 auto style = getDefaultStyleForElement("vertlabel", "", "label");
1907 gui::IGUIFont *font = style.getFont();
1912 core::rect<s32> rect;
1914 if (data->real_coordinates) {
1915 pos = getRealCoordinateBasePos(v_pos);
1917 // Vertlabels are positioned by center, not left.
1918 pos.X -= imgsize.X / 2;
1920 // We use text.length + 1 because without it, the rect
1921 // isn't quite tall enough and cuts off the text.
1922 rect = core::rect<s32>(pos.X, pos.Y,
1924 pos.Y + font_line_height(font) *
1925 (text.length() + 1));
1928 pos = getElementBasePos(&v_pos);
1930 // As above, the length must be one longer. The width of
1931 // the rect (15 pixels) seems rather arbitrary, but
1932 // changing it might break something.
1933 rect = core::rect<s32>(
1934 pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
1936 font_line_height(font) *
1937 (text.length() + 1) +
1938 ((imgsize.Y/2) - m_btn_height));
1941 if(!data->explicit_size)
1942 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1946 for (wchar_t i : text) {
1955 258 + m_fields.size()
1957 gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
1958 rect, false, false, data->current_parent, spec.fid);
1959 e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1961 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1962 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1963 e->setOverrideFont(font);
1965 m_fields.push_back(spec);
1967 // vertlabels should let events through
1969 m_clickthrough_elements.push_back(e);
1972 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1975 void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
1976 const std::string &type)
1978 std::vector<std::string> parts = split(element,';');
1980 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1981 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1983 std::vector<std::string> v_pos = split(parts[0],',');
1984 std::vector<std::string> v_geom = split(parts[1],',');
1985 std::string image_name = parts[2];
1986 std::string name = parts[3];
1987 std::string label = parts[4];
1989 MY_CHECKPOS("imagebutton",0);
1990 MY_CHECKGEOM("imagebutton",1);
1992 std::string pressed_image_name;
1994 if (parts.size() >= 8) {
1995 pressed_image_name = parts[7];
2001 if (data->real_coordinates) {
2002 pos = getRealCoordinateBasePos(v_pos);
2003 geom = getRealCoordinateGeometry(v_geom);
2005 pos = getElementBasePos(&v_pos);
2006 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
2007 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
2010 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
2013 if (!data->explicit_size)
2014 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
2016 image_name = unescape_string(image_name);
2017 pressed_image_name = unescape_string(pressed_image_name);
2019 std::wstring wlabel = utf8_to_wide(unescape_string(label));
2024 utf8_to_wide(image_name),
2025 258 + m_fields.size()
2027 spec.ftype = f_Button;
2028 if (type == "image_button_exit")
2029 spec.is_exit = true;
2031 GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, m_tsrc,
2032 data->current_parent, spec.fid, spec.flabel.c_str());
2034 if (spec.fname == m_focused_element) {
2035 Environment->setFocus(e);
2038 auto style = getStyleForElement("image_button", spec.fname);
2040 spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
2042 // Override style properties with values specified directly in the element
2043 if (!image_name.empty())
2044 style[StyleSpec::STATE_DEFAULT].set(StyleSpec::FGIMG, image_name);
2046 if (!pressed_image_name.empty())
2047 style[StyleSpec::STATE_PRESSED].set(StyleSpec::FGIMG, pressed_image_name);
2049 if (parts.size() >= 7) {
2050 style[StyleSpec::STATE_DEFAULT].set(StyleSpec::NOCLIP, parts[5]);
2051 style[StyleSpec::STATE_DEFAULT].set(StyleSpec::BORDER, parts[6]);
2054 e->setStyles(style);
2055 e->setScaleImage(true);
2057 m_fields.push_back(spec);
2061 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
2064 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
2066 std::vector<std::string> parts = split(element, ';');
2068 if (((parts.size() == 4) || (parts.size() == 6)) || (parts.size() == 7 &&
2069 data->real_coordinates) || ((parts.size() > 6) &&
2070 (m_formspec_version > FORMSPEC_API_VERSION)))
2072 std::vector<std::string> v_pos = split(parts[0],',');
2074 // If we're using real coordinates, add an extra field for height.
2075 // Width is not here because tabs are the width of the text, and
2076 // there's no reason to change that.
2078 std::vector<std::string> v_geom = {"1", "1"}; // Dummy width and height
2079 bool auto_width = true;
2080 if (parts.size() == 7) {
2083 v_geom = split(parts[1], ',');
2084 if (v_geom.size() == 1)
2085 v_geom.insert(v_geom.begin(), "1"); // Dummy value
2090 std::string name = parts[i+1];
2091 std::vector<std::string> buttons = split(parts[i+2], ',');
2092 std::string str_index = parts[i+3];
2093 bool show_background = true;
2094 bool show_border = true;
2095 int tab_index = stoi(str_index) - 1;
2097 MY_CHECKPOS("tabheader", 0);
2099 if (parts.size() == 6 + i) {
2100 if (parts[4+i] == "true")
2101 show_background = false;
2102 if (parts[5+i] == "false")
2103 show_border = false;
2110 258 + m_fields.size()
2113 spec.ftype = f_TabHeader;
2118 if (data->real_coordinates) {
2119 pos = getRealCoordinateBasePos(v_pos);
2121 geom = getRealCoordinateGeometry(v_geom);
2122 // Set default height
2123 if (parts.size() <= 6)
2124 geom.Y = m_btn_height * 2;
2125 pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
2127 geom.X = DesiredRect.getWidth(); // Set automatic width
2129 MY_CHECKGEOM("tabheader", 1);
2131 v2f32 pos_f = pos_offset * spacing;
2132 pos_f.X += stof(v_pos[0]) * spacing.X;
2133 pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
2134 pos = v2s32(pos_f.X, pos_f.Y);
2136 geom.Y = m_btn_height * 2;
2137 geom.X = DesiredRect.getWidth();
2140 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
2143 gui::IGUITabControl *e = Environment->addTabControl(rect,
2144 data->current_parent, show_background, show_border, spec.fid);
2145 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
2146 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
2147 e->setTabHeight(geom.Y);
2149 auto style = getDefaultStyleForElement("tabheader", name);
2151 spec.sound = style.get(StyleSpec::Property::SOUND, "");
2153 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
2155 for (const std::string &button : buttons) {
2156 auto tab = e->addTab(unescape_translate(unescape_string(
2157 utf8_to_wide(button))).c_str(), -1);
2158 if (style.isNotDefault(StyleSpec::BGCOLOR))
2159 tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
2161 tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
2164 if ((tab_index >= 0) &&
2165 (buttons.size() < INT_MAX) &&
2166 (tab_index < (int) buttons.size()))
2167 e->setActiveTab(tab_index);
2169 m_fields.push_back(spec);
2172 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
2173 << element << "'" << std::endl;
2176 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
2178 if (m_client == 0) {
2179 warningstream << "invalid use of item_image_button with m_client==0"
2184 std::vector<std::string> parts = split(element,';');
2186 if ((parts.size() == 5) ||
2187 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2189 std::vector<std::string> v_pos = split(parts[0],',');
2190 std::vector<std::string> v_geom = split(parts[1],',');
2191 std::string item_name = parts[2];
2192 std::string name = parts[3];
2193 std::string label = parts[4];
2195 label = unescape_string(label);
2196 item_name = unescape_string(item_name);
2198 MY_CHECKPOS("itemimagebutton",0);
2199 MY_CHECKGEOM("itemimagebutton",1);
2204 if (data->real_coordinates) {
2205 pos = getRealCoordinateBasePos(v_pos);
2206 geom = getRealCoordinateGeometry(v_geom);
2208 pos = getElementBasePos(&v_pos);
2209 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
2210 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
2213 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
2215 if(!data->explicit_size)
2216 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
2218 IItemDefManager *idef = m_client->idef();
2220 item.deSerialize(item_name, idef);
2223 TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
2224 m_default_tooltip_bgcolor,
2225 m_default_tooltip_color);
2227 // the spec for the button
2230 utf8_to_wide(label),
2231 utf8_to_wide(item_name),
2232 258 + m_fields.size(),
2236 GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment,
2237 rect, m_tsrc, data->current_parent, spec_btn.fid, spec_btn.flabel.c_str(),
2238 item_name, m_client);
2240 auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
2242 spec_btn.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
2244 e_btn->setStyles(style);
2246 if (spec_btn.fname == m_focused_element) {
2247 Environment->setFocus(e_btn);
2250 spec_btn.ftype = f_Button;
2251 rect += data->basepos-padding;
2252 spec_btn.rect = rect;
2253 m_fields.push_back(spec_btn);
2256 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
2259 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
2261 std::vector<std::string> parts = split(element, ';');
2263 if ((parts.size() == 3) ||
2264 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
2266 std::vector<std::string> v_pos = split(parts[0], ',');
2267 std::vector<std::string> v_geom = split(parts[1], ',');
2269 MY_CHECKPOS("box", 0);
2270 MY_CHECKGEOM("box", 1);
2275 if (data->real_coordinates) {
2276 pos = getRealCoordinateBasePos(v_pos);
2277 geom = getRealCoordinateGeometry(v_geom);
2279 pos = getElementBasePos(&v_pos);
2280 geom.X = stof(v_geom[0]) * spacing.X;
2281 geom.Y = stof(v_geom[1]) * spacing.Y;
2288 258 + m_fields.size(),
2293 auto style = getDefaultStyleForElement("box", spec.fname);
2295 video::SColor tmp_color;
2296 std::array<video::SColor, 4> colors;
2297 std::array<video::SColor, 4> bordercolors = {0x0, 0x0, 0x0, 0x0};
2298 std::array<s32, 4> borderwidths = {0, 0, 0, 0};
2300 if (parseColorString(parts[2], tmp_color, true, 0x8C)) {
2301 colors = {tmp_color, tmp_color, tmp_color, tmp_color};
2303 colors = style.getColorArray(StyleSpec::COLORS, {0x0, 0x0, 0x0, 0x0});
2304 bordercolors = style.getColorArray(StyleSpec::BORDERCOLORS,
2305 {0x0, 0x0, 0x0, 0x0});
2306 borderwidths = style.getIntArray(StyleSpec::BORDERWIDTHS, {0, 0, 0, 0});
2309 core::rect<s32> rect(pos, pos + geom);
2311 GUIBox *e = new GUIBox(Environment, data->current_parent, spec.fid, rect,
2312 colors, bordercolors, borderwidths);
2313 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
2316 m_fields.push_back(spec);
2319 errorstream << "Invalid Box element(" << parts.size() << "): '" << element
2320 << "'" << std::endl;
2323 void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
2325 std::vector<std::string> parts = split(element,';');
2326 const u32 parameter_count = parts.size();
2328 if ((parameter_count > 2 && m_formspec_version < 3) ||
2329 (parameter_count > 3 && m_formspec_version <= FORMSPEC_API_VERSION)) {
2330 errorstream << "Invalid bgcolor element(" << parameter_count << "): '"
2331 << element << "'" << std::endl;
2336 if (parameter_count >= 1 && parts[0] != "")
2337 parseColorString(parts[0], m_bgcolor, false);
2340 if (parameter_count >= 2) {
2341 if (parts[1] == "both") {
2342 m_bgnonfullscreen = true;
2343 m_bgfullscreen = true;
2344 } else if (parts[1] == "neither") {
2345 m_bgnonfullscreen = false;
2346 m_bgfullscreen = false;
2347 } else if (parts[1] != "" || m_formspec_version < 3) {
2348 m_bgfullscreen = is_yes(parts[1]);
2349 m_bgnonfullscreen = !m_bgfullscreen;
2354 if (parameter_count >= 3 && parts[2] != "")
2355 parseColorString(parts[2], m_fullscreen_bgcolor, false);
2358 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
2360 std::vector<std::string> parts = split(element,';');
2362 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
2363 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2365 parseColorString(parts[0], data->inventorylist_options.slotbg_n, false);
2366 parseColorString(parts[1], data->inventorylist_options.slotbg_h, false);
2368 if (parts.size() >= 3) {
2369 if (parseColorString(parts[2], data->inventorylist_options.slotbordercolor,
2371 data->inventorylist_options.slotborder = true;
2374 if (parts.size() == 5) {
2375 video::SColor tmp_color;
2377 if (parseColorString(parts[3], tmp_color, false))
2378 m_default_tooltip_bgcolor = tmp_color;
2379 if (parseColorString(parts[4], tmp_color, false))
2380 m_default_tooltip_color = tmp_color;
2383 // update all already parsed inventorylists
2384 for (GUIInventoryList *e : m_inventorylists) {
2385 e->setSlotBGColors(data->inventorylist_options.slotbg_n,
2386 data->inventorylist_options.slotbg_h);
2387 e->setSlotBorders(data->inventorylist_options.slotborder,
2388 data->inventorylist_options.slotbordercolor);
2392 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
2395 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
2397 std::vector<std::string> parts = split(element,';');
2398 if (parts.size() < 2) {
2399 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2400 << element << "'" << std::endl;
2404 // Get mode and check size
2405 bool rect_mode = parts[0].find(',') != std::string::npos;
2406 size_t base_size = rect_mode ? 3 : 2;
2407 if (parts.size() != base_size && parts.size() != base_size + 2) {
2408 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2409 << element << "'" << std::endl;
2414 video::SColor bgcolor = m_default_tooltip_bgcolor;
2415 video::SColor color = m_default_tooltip_color;
2416 if (parts.size() == base_size + 2 &&
2417 (!parseColorString(parts[base_size], bgcolor, false) ||
2418 !parseColorString(parts[base_size + 1], color, false))) {
2419 errorstream << "Invalid color in tooltip element(" << parts.size()
2420 << "): '" << element << "'" << std::endl;
2424 // Make tooltip spec
2425 std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
2426 TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
2430 std::vector<std::string> v_pos = split(parts[0], ',');
2431 std::vector<std::string> v_geom = split(parts[1], ',');
2433 MY_CHECKPOS("tooltip", 0);
2434 MY_CHECKGEOM("tooltip", 1);
2439 if (data->real_coordinates) {
2440 pos = getRealCoordinateBasePos(v_pos);
2441 geom = getRealCoordinateGeometry(v_geom);
2443 pos = getElementBasePos(&v_pos);
2444 geom.X = stof(v_geom[0]) * spacing.X;
2445 geom.Y = stof(v_geom[1]) * spacing.Y;
2448 FieldSpec fieldspec(
2452 258 + m_fields.size()
2455 core::rect<s32> rect(pos, pos + geom);
2457 gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
2458 data->current_parent, fieldspec.fid, rect);
2460 // the element the rect tooltip is bound to should not block mouse-clicks
2461 e->setVisible(false);
2463 m_fields.push_back(fieldspec);
2464 m_tooltip_rects.emplace_back(e, spec);
2467 m_tooltips[parts[0]] = spec;
2471 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
2477 std::vector<std::string> parts = split(data,'[');
2479 if (parts.size() < 2) {
2483 if (trim(parts[0]) != "formspec_version") {
2487 if (is_number(parts[1])) {
2488 m_formspec_version = mystoi(parts[1]);
2495 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
2497 if (element.empty())
2500 std::vector<std::string> parts = split(element,'[');
2502 if (parts.size() < 2)
2505 std::string type = trim(parts[0]);
2506 std::string description = trim(parts[1]);
2508 if (type != "size" && type != "invsize")
2511 if (type == "invsize")
2512 warningstream << "Deprecated formspec element \"invsize\" is used" << std::endl;
2514 parseSize(data, description);
2519 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
2521 if (element.empty())
2524 std::vector<std::string> parts = split(element, '[');
2526 if (parts.size() != 2)
2529 std::string type = trim(parts[0]);
2530 std::string description = trim(parts[1]);
2532 if (type != "position")
2535 parsePosition(data, description);
2540 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
2542 std::vector<std::string> parts = split(element, ',');
2544 if (parts.size() == 2) {
2545 data->offset.X = stof(parts[0]);
2546 data->offset.Y = stof(parts[1]);
2550 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
2553 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
2555 if (element.empty())
2558 std::vector<std::string> parts = split(element, '[');
2560 if (parts.size() != 2)
2563 std::string type = trim(parts[0]);
2564 std::string description = trim(parts[1]);
2566 if (type != "anchor")
2569 parseAnchor(data, description);
2574 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
2576 std::vector<std::string> parts = split(element, ',');
2578 if (parts.size() == 2) {
2579 data->anchor.X = stof(parts[0]);
2580 data->anchor.Y = stof(parts[1]);
2584 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
2585 << "'" << std::endl;
2588 bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type)
2590 std::vector<std::string> parts = split(element, ';');
2592 if (parts.size() < 2) {
2593 errorstream << "Invalid style element (" << parts.size() << "): '" << element
2594 << "'" << std::endl;
2601 for (size_t i = 1; i < parts.size(); i++) {
2602 size_t equal_pos = parts[i].find('=');
2603 if (equal_pos == std::string::npos) {
2604 errorstream << "Invalid style element (Property missing value): '" << element
2605 << "'" << std::endl;
2609 std::string propname = trim(parts[i].substr(0, equal_pos));
2610 std::string value = trim(unescape_string(parts[i].substr(equal_pos + 1)));
2612 std::transform(propname.begin(), propname.end(), propname.begin(), ::tolower);
2614 StyleSpec::Property prop = StyleSpec::GetPropertyByName(propname);
2615 if (prop == StyleSpec::NONE) {
2616 if (property_warned.find(propname) != property_warned.end()) {
2617 warningstream << "Invalid style element (Unknown property " << propname << "): '"
2619 << "'" << std::endl;
2620 property_warned.insert(propname);
2625 spec.set(prop, value);
2628 std::vector<std::string> selectors = split(parts[0], ',');
2629 for (size_t sel = 0; sel < selectors.size(); sel++) {
2630 std::string selector = trim(selectors[sel]);
2632 // Copy the style properties to a new StyleSpec
2633 // This allows a separate state mask per-selector
2634 StyleSpec selector_spec = spec;
2636 // Parse state information, if it exists
2637 bool state_valid = true;
2638 size_t state_pos = selector.find(':');
2639 if (state_pos != std::string::npos) {
2640 std::string state_str = selector.substr(state_pos + 1);
2641 selector = selector.substr(0, state_pos);
2643 if (state_str.empty()) {
2644 errorstream << "Invalid style element (Invalid state): '" << element
2645 << "'" << std::endl;
2646 state_valid = false;
2648 std::vector<std::string> states = split(state_str, '+');
2649 for (std::string &state : states) {
2650 StyleSpec::State converted = StyleSpec::getStateByName(state);
2651 if (converted == StyleSpec::STATE_INVALID) {
2652 infostream << "Unknown style state " << state <<
2653 " in element '" << element << "'" << std::endl;
2654 state_valid = false;
2658 selector_spec.addState(converted);
2664 // Skip this selector
2669 theme_by_type[selector].push_back(selector_spec);
2671 theme_by_name[selector].push_back(selector_spec);
2674 // Backwards-compatibility for existing _hovered/_pressed properties
2675 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_HOVERED)
2676 || selector_spec.hasProperty(StyleSpec::BGIMG_HOVERED)
2677 || selector_spec.hasProperty(StyleSpec::FGIMG_HOVERED)) {
2678 StyleSpec hover_spec;
2679 hover_spec.addState(StyleSpec::STATE_HOVERED);
2681 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_HOVERED)) {
2682 hover_spec.set(StyleSpec::BGCOLOR, selector_spec.get(StyleSpec::BGCOLOR_HOVERED, ""));
2684 if (selector_spec.hasProperty(StyleSpec::BGIMG_HOVERED)) {
2685 hover_spec.set(StyleSpec::BGIMG, selector_spec.get(StyleSpec::BGIMG_HOVERED, ""));
2687 if (selector_spec.hasProperty(StyleSpec::FGIMG_HOVERED)) {
2688 hover_spec.set(StyleSpec::FGIMG, selector_spec.get(StyleSpec::FGIMG_HOVERED, ""));
2692 theme_by_type[selector].push_back(hover_spec);
2694 theme_by_name[selector].push_back(hover_spec);
2697 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_PRESSED)
2698 || selector_spec.hasProperty(StyleSpec::BGIMG_PRESSED)
2699 || selector_spec.hasProperty(StyleSpec::FGIMG_PRESSED)) {
2700 StyleSpec press_spec;
2701 press_spec.addState(StyleSpec::STATE_PRESSED);
2703 if (selector_spec.hasProperty(StyleSpec::BGCOLOR_PRESSED)) {
2704 press_spec.set(StyleSpec::BGCOLOR, selector_spec.get(StyleSpec::BGCOLOR_PRESSED, ""));
2706 if (selector_spec.hasProperty(StyleSpec::BGIMG_PRESSED)) {
2707 press_spec.set(StyleSpec::BGIMG, selector_spec.get(StyleSpec::BGIMG_PRESSED, ""));
2709 if (selector_spec.hasProperty(StyleSpec::FGIMG_PRESSED)) {
2710 press_spec.set(StyleSpec::FGIMG, selector_spec.get(StyleSpec::FGIMG_PRESSED, ""));
2714 theme_by_type[selector].push_back(press_spec);
2716 theme_by_name[selector].push_back(press_spec);
2724 void GUIFormSpecMenu::parseSetFocus(const std::string &element)
2726 std::vector<std::string> parts = split(element, ';');
2728 if (parts.size() <= 2 ||
2729 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION))
2731 if (m_is_form_regenerated)
2732 return; // Never focus on resizing
2734 bool force_focus = parts.size() >= 2 && is_yes(parts[1]);
2735 if (force_focus || m_text_dst->m_formname != m_last_formname)
2741 errorstream << "Invalid set_focus element (" << parts.size() << "): '" << element
2742 << "'" << std::endl;
2745 void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
2747 std::vector<std::string> parts = split(element, ';');
2749 if (parts.size() < 5 || (parts.size() > 9 &&
2750 m_formspec_version <= FORMSPEC_API_VERSION)) {
2751 errorstream << "Invalid model element (" << parts.size() << "): '" << element
2752 << "'" << std::endl;
2756 // Avoid length checks by resizing
2757 if (parts.size() < 9)
2760 std::vector<std::string> v_pos = split(parts[0], ',');
2761 std::vector<std::string> v_geom = split(parts[1], ',');
2762 std::string name = unescape_string(parts[2]);
2763 std::string meshstr = unescape_string(parts[3]);
2764 std::vector<std::string> textures = split(parts[4], ',');
2765 std::vector<std::string> vec_rot = split(parts[5], ',');
2766 bool inf_rotation = is_yes(parts[6]);
2767 bool mousectrl = is_yes(parts[7]) || parts[7].empty(); // default true
2768 std::vector<std::string> frame_loop = split(parts[8], ',');
2770 MY_CHECKPOS("model", 0);
2771 MY_CHECKGEOM("model", 1);
2776 if (data->real_coordinates) {
2777 pos = getRealCoordinateBasePos(v_pos);
2778 geom = getRealCoordinateGeometry(v_geom);
2780 pos = getElementBasePos(&v_pos);
2781 geom.X = stof(v_geom[0]) * (float)imgsize.X;
2782 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
2785 if (!data->explicit_size)
2786 warningstream << "invalid use of model without a size[] element" << std::endl;
2788 scene::IAnimatedMesh *mesh = m_client->getMesh(meshstr);
2791 errorstream << "Invalid model element: Unable to load mesh:"
2792 << std::endl << "\t" << meshstr << std::endl;
2800 258 + m_fields.size()
2803 core::rect<s32> rect(pos, pos + geom);
2805 GUIScene *e = new GUIScene(Environment, RenderingEngine::get_scene_manager(),
2806 data->current_parent, rect, spec.fid);
2808 auto meshnode = e->setMesh(mesh);
2810 for (u32 i = 0; i < textures.size() && i < meshnode->getMaterialCount(); ++i)
2811 e->setTexture(i, m_tsrc->getTexture(unescape_string(textures[i])));
2813 if (vec_rot.size() >= 2)
2814 e->setRotation(v2f(stof(vec_rot[0]), stof(vec_rot[1])));
2816 e->enableContinuousRotation(inf_rotation);
2817 e->enableMouseControl(mousectrl);
2819 s32 frame_loop_begin = 0;
2820 s32 frame_loop_end = 0x7FFFFFFF;
2822 if (frame_loop.size() == 2) {
2823 frame_loop_begin = stoi(frame_loop[0]);
2824 frame_loop_end = stoi(frame_loop[1]);
2827 e->setFrameLoop(frame_loop_begin, frame_loop_end);
2829 auto style = getStyleForElement("model", spec.fname);
2830 e->setStyles(style);
2833 m_fields.push_back(spec);
2836 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
2839 if (element.empty())
2842 if (parseVersionDirect(element))
2845 size_t pos = element.find('[');
2846 if (pos == std::string::npos)
2849 std::string type = trim(element.substr(0, pos));
2850 std::string description = element.substr(pos+1);
2852 if (type == "container") {
2853 parseContainer(data, description);
2857 if (type == "container_end") {
2858 parseContainerEnd(data);
2862 if (type == "list") {
2863 parseList(data, description);
2867 if (type == "listring") {
2868 parseListRing(data, description);
2872 if (type == "checkbox") {
2873 parseCheckbox(data, description);
2877 if (type == "image") {
2878 parseImage(data, description);
2882 if (type == "animated_image") {
2883 parseAnimatedImage(data, description);
2887 if (type == "item_image") {
2888 parseItemImage(data, description);
2892 if (type == "button" || type == "button_exit") {
2893 parseButton(data, description, type);
2897 if (type == "background" || type == "background9") {
2898 parseBackground(data, description);
2902 if (type == "tableoptions"){
2903 parseTableOptions(data,description);
2907 if (type == "tablecolumns"){
2908 parseTableColumns(data,description);
2912 if (type == "table"){
2913 parseTable(data,description);
2917 if (type == "textlist"){
2918 parseTextList(data,description);
2922 if (type == "dropdown"){
2923 parseDropDown(data,description);
2927 if (type == "field_close_on_enter") {
2928 parseFieldCloseOnEnter(data, description);
2932 if (type == "pwdfield") {
2933 parsePwdField(data,description);
2937 if ((type == "field") || (type == "textarea")){
2938 parseField(data,description,type);
2942 if (type == "hypertext") {
2943 parseHyperText(data,description);
2947 if (type == "label") {
2948 parseLabel(data,description);
2952 if (type == "vertlabel") {
2953 parseVertLabel(data,description);
2957 if (type == "item_image_button") {
2958 parseItemImageButton(data,description);
2962 if ((type == "image_button") || (type == "image_button_exit")) {
2963 parseImageButton(data,description,type);
2967 if (type == "tabheader") {
2968 parseTabHeader(data,description);
2972 if (type == "box") {
2973 parseBox(data,description);
2977 if (type == "bgcolor") {
2978 parseBackgroundColor(data,description);
2982 if (type == "listcolors") {
2983 parseListColors(data,description);
2987 if (type == "tooltip") {
2988 parseTooltip(data,description);
2992 if (type == "scrollbar") {
2993 parseScrollBar(data, description);
2997 if (type == "real_coordinates") {
2998 data->real_coordinates = is_yes(description);
3002 if (type == "style") {
3003 parseStyle(data, description, false);
3007 if (type == "style_type") {
3008 parseStyle(data, description, true);
3012 if (type == "scrollbaroptions") {
3013 parseScrollBarOptions(data, description);
3017 if (type == "scroll_container") {
3018 parseScrollContainer(data, description);
3022 if (type == "scroll_container_end") {
3023 parseScrollContainerEnd(data);
3027 if (type == "set_focus") {
3028 parseSetFocus(description);
3032 if (type == "model") {
3033 parseModel(data, description);
3038 infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
3042 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
3044 // Useless to regenerate without a screensize
3045 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
3051 // Preserve stuff only on same form, not on a new form.
3052 if (m_text_dst->m_formname == m_last_formname) {
3053 // Preserve tables/textlists
3054 for (auto &m_table : m_tables) {
3055 std::string tablename = m_table.first.fname;
3056 GUITable *table = m_table.second;
3057 mydata.table_dyndata[tablename] = table->getDynamicData();
3061 gui::IGUIElement *focused_element = Environment->getFocus();
3062 if (focused_element && focused_element->getParent() == this) {
3063 s32 focused_id = focused_element->getID();
3064 if (focused_id > 257) {
3065 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
3066 if (field.fid == focused_id) {
3067 m_focused_element = field.fname;
3074 // Don't keep old focus value
3075 m_focused_element = "";
3081 for (auto &table_it : m_tables)
3082 table_it.second->drop();
3083 for (auto &inventorylist_it : m_inventorylists)
3084 inventorylist_it->drop();
3085 for (auto &checkbox_it : m_checkboxes)
3086 checkbox_it.second->drop();
3087 for (auto &scrollbar_it : m_scrollbars)
3088 scrollbar_it.second->drop();
3089 for (auto &background_it : m_backgrounds)
3090 background_it->drop();
3091 for (auto &tooltip_rect_it : m_tooltip_rects)
3092 tooltip_rect_it.first->drop();
3093 for (auto &clickthrough_it : m_clickthrough_elements)
3094 clickthrough_it->drop();
3095 for (auto &scroll_container_it : m_scroll_containers)
3096 scroll_container_it.second->drop();
3098 mydata.size = v2s32(100, 100);
3099 mydata.screensize = screensize;
3100 mydata.offset = v2f32(0.5f, 0.5f);
3101 mydata.anchor = v2f32(0.5f, 0.5f);
3102 mydata.simple_field_count = 0;
3104 // Base position of contents of form
3105 mydata.basepos = getBasePos();
3107 // the parent for the parsed elements
3108 mydata.current_parent = this;
3110 m_inventorylists.clear();
3111 m_backgrounds.clear();
3113 m_checkboxes.clear();
3114 m_scrollbars.clear();
3117 m_tooltip_rects.clear();
3118 m_inventory_rings.clear();
3119 m_dropdowns.clear();
3120 m_scroll_containers.clear();
3121 theme_by_name.clear();
3122 theme_by_type.clear();
3123 m_clickthrough_elements.clear();
3124 field_close_on_enter.clear();
3125 m_dropdown_index_event.clear();
3127 m_bgnonfullscreen = true;
3128 m_bgfullscreen = false;
3130 m_formspec_version = 1;
3133 v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
3134 m_bgcolor = video::SColor(
3135 (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
3136 clamp_u8(myround(formspec_bgcolor.X)),
3137 clamp_u8(myround(formspec_bgcolor.Y)),
3138 clamp_u8(myround(formspec_bgcolor.Z))
3143 v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
3144 m_fullscreen_bgcolor = video::SColor(
3145 (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
3146 clamp_u8(myround(formspec_bgcolor.X)),
3147 clamp_u8(myround(formspec_bgcolor.Y)),
3148 clamp_u8(myround(formspec_bgcolor.Z))
3152 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
3153 m_default_tooltip_color = video::SColor(255,255,255,255);
3157 assert(!m_tooltip_element);
3158 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
3159 m_tooltip_element = gui::StaticText::add(Environment, L"",
3160 core::rect<s32>(0, 0, 110, 18));
3161 m_tooltip_element->enableOverrideColor(true);
3162 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
3163 m_tooltip_element->setDrawBackground(true);
3164 m_tooltip_element->setDrawBorder(true);
3165 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
3166 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
3167 m_tooltip_element->setWordWrap(false);
3168 //we're not parent so no autograb for this one!
3169 m_tooltip_element->grab();
3172 std::vector<std::string> elements = split(m_formspec_string,']');
3175 /* try to read version from first element only */
3176 if (!elements.empty()) {
3177 if (parseVersionDirect(elements[0])) {
3182 /* we need size first in order to calculate image scale */
3183 mydata.explicit_size = false;
3184 for (; i< elements.size(); i++) {
3185 if (!parseSizeDirect(&mydata, elements[i])) {
3190 /* "position" element is always after "size" element if it used */
3191 for (; i< elements.size(); i++) {
3192 if (!parsePositionDirect(&mydata, elements[i])) {
3197 /* "anchor" element is always after "position" (or "size" element) if it used */
3198 for (; i< elements.size(); i++) {
3199 if (!parseAnchorDirect(&mydata, elements[i])) {
3204 /* "no_prepend" element is always after "position" (or "size" element) if it used */
3205 bool enable_prepends = true;
3206 for (; i < elements.size(); i++) {
3207 if (elements[i].empty())
3210 std::vector<std::string> parts = split(elements[i], '[');
3211 if (trim(parts[0]) == "no_prepend")
3212 enable_prepends = false;
3217 /* Copy of the "real_coordinates" element for after the form size. */
3218 mydata.real_coordinates = m_formspec_version >= 2;
3219 for (; i < elements.size(); i++) {
3220 std::vector<std::string> parts = split(elements[i], '[');
3221 std::string name = trim(parts[0]);
3222 if (name != "real_coordinates" || parts.size() != 2)
3223 break; // Invalid format
3225 mydata.real_coordinates = is_yes(trim(parts[1]));
3228 if (mydata.explicit_size) {
3229 // compute scaling for specified form size
3231 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
3232 v2u32 delta = current_screensize - m_lockscreensize;
3234 if (current_screensize.Y > m_lockscreensize.Y)
3239 if (current_screensize.X > m_lockscreensize.X)
3244 offset = v2s32(delta.X,delta.Y);
3246 mydata.screensize = m_lockscreensize;
3248 offset = v2s32(0,0);
3251 double gui_scaling = g_settings->getFloat("gui_scaling");
3252 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
3256 // In fixed-size mode, inventory image size
3257 // is 0.53 inch multiplied by the gui_scaling
3258 // config parameter. This magic size is chosen
3259 // to make the main menu (15.5 inventory images
3260 // wide, including border) just fit into the
3261 // default window (800 pixels wide) at 96 DPI
3262 // and default scaling (1.00).
3263 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
3265 // Variables for the maximum imgsize that can fit in the screen.
3266 double fitx_imgsize;
3267 double fity_imgsize;
3269 // Pad the screensize with 5% of the screensize on all sides to ensure
3270 // that even the largest formspecs don't touch the screen borders.
3271 v2f padded_screensize(
3272 mydata.screensize.X * 0.9f,
3273 mydata.screensize.Y * 0.9f
3276 if (mydata.real_coordinates) {
3277 fitx_imgsize = padded_screensize.X / mydata.invsize.X;
3278 fity_imgsize = padded_screensize.Y / mydata.invsize.Y;
3280 // The maximum imgsize in the old coordinate system also needs to
3281 // factor in padding and spacing along with 0.1 inventory slot spare
3282 // and help text space, hence the magic numbers.
3283 fitx_imgsize = padded_screensize.X /
3284 ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
3285 fity_imgsize = padded_screensize.Y /
3286 ((15.0 / 13.0) * (0.85 + mydata.invsize.Y));
3290 // In Android, the preferred imgsize should be larger to accommodate the
3291 // smaller screensize.
3292 double prefer_imgsize = padded_screensize.Y / 10 * gui_scaling;
3294 // Desktop computers have more space, so try to fit 15 coordinates.
3295 double prefer_imgsize = padded_screensize.Y / 15 * gui_scaling;
3297 // Try to use the preferred imgsize, but if that's bigger than the maximum
3298 // size, use the maximum size.
3299 use_imgsize = std::min(prefer_imgsize,
3300 std::min(fitx_imgsize, fity_imgsize));
3303 // Everything else is scaled in proportion to the
3304 // inventory image size. The inventory slot spacing
3305 // is 5/4 image size horizontally and 15/13 image size
3306 // vertically. The padding around the form (incorporating
3307 // the border of the outer inventory slots) is 3/8
3308 // image size. Font height (baseline to baseline)
3309 // is 2/5 vertical inventory slot spacing, and button
3310 // half-height is 7/8 of font height.
3311 imgsize = v2s32(use_imgsize, use_imgsize);
3312 spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
3313 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
3314 m_btn_height = use_imgsize*15.0/13 * 0.35;
3316 m_font = g_fontengine->getFont();
3318 if (mydata.real_coordinates) {
3319 mydata.size = v2s32(
3320 mydata.invsize.X*imgsize.X,
3321 mydata.invsize.Y*imgsize.Y
3324 mydata.size = v2s32(
3325 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
3326 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
3330 DesiredRect = mydata.rect = core::rect<s32>(
3331 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
3332 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
3333 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
3334 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
3337 // Non-size[] form must consist only of text fields and
3338 // implicit "Proceed" button. Use default font, and
3339 // temporary form size which will be recalculated below.
3340 m_font = g_fontengine->getFont();
3341 m_btn_height = font_line_height(m_font) * 0.875;
3342 DesiredRect = core::rect<s32>(
3343 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
3344 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
3345 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
3346 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
3349 recalculateAbsolutePosition(false);
3350 mydata.basepos = getBasePos();
3351 m_tooltip_element->setOverrideFont(m_font);
3353 gui::IGUISkin *skin = Environment->getSkin();
3355 gui::IGUIFont *old_font = skin->getFont();
3356 skin->setFont(m_font);
3358 pos_offset = v2f32();
3360 // used for formspec versions < 3
3361 core::list<IGUIElement *>::Iterator legacy_sort_start = Children.getLast();
3363 if (enable_prepends) {
3364 // Backup the coordinates so that prepends can use the coordinates of choice.
3365 bool rc_backup = mydata.real_coordinates;
3366 u16 version_backup = m_formspec_version;
3367 mydata.real_coordinates = false; // Old coordinates by default.
3369 std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
3370 for (const auto &element : prepend_elements)
3371 parseElement(&mydata, element);
3373 // legacy sorting for formspec versions < 3
3374 if (m_formspec_version >= 3)
3375 // prepends do not need to be reordered
3376 legacy_sort_start = Children.getLast();
3377 else if (version_backup >= 3)
3378 // only prepends elements have to be reordered
3379 legacySortElements(legacy_sort_start);
3381 m_formspec_version = version_backup;
3382 mydata.real_coordinates = rc_backup; // Restore coordinates
3385 for (; i< elements.size(); i++) {
3386 parseElement(&mydata, elements[i]);
3389 if (mydata.current_parent != this) {
3390 errorstream << "Invalid formspec string: scroll_container was never closed!"
3392 } else if (!container_stack.empty()) {
3393 errorstream << "Invalid formspec string: container was never closed!"
3397 // get the scrollbar elements for scroll_containers
3398 for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers) {
3399 for (const std::pair<FieldSpec, GUIScrollBar *> &b : m_scrollbars) {
3400 if (c.first == b.first.fname) {
3401 c.second->setScrollBar(b.second);
3407 // If there are fields without explicit size[], add a "Proceed"
3408 // button and adjust size to fit all the fields.
3409 if (mydata.simple_field_count > 0 && !mydata.explicit_size) {
3410 mydata.rect = core::rect<s32>(
3411 mydata.screensize.X / 2 - 580 / 2,
3412 mydata.screensize.Y / 2 - 300 / 2,
3413 mydata.screensize.X / 2 + 580 / 2,
3414 mydata.screensize.Y / 2 + 240 / 2 + mydata.simple_field_count * 60
3417 DesiredRect = mydata.rect;
3418 recalculateAbsolutePosition(false);
3419 mydata.basepos = getBasePos();
3422 v2s32 pos = mydata.basepos;
3423 pos.Y = (mydata.simple_field_count + 2) * 60;
3425 v2s32 size = DesiredRect.getSize();
3426 mydata.rect = core::rect<s32>(
3427 size.X / 2 - 70, pos.Y,
3428 size.X / 2 - 70 + 140, pos.Y + m_btn_height * 2
3430 const wchar_t *text = wgettext("Proceed");
3431 GUIButton::addButton(Environment, mydata.rect, m_tsrc, this, 257, text);
3436 // Set initial focus if parser didn't set it
3437 gui::IGUIElement *focused_element = Environment->getFocus();
3438 if (!focused_element
3439 || !isMyChild(focused_element)
3440 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
3443 skin->setFont(old_font);
3446 if (m_formspec_version < 3)
3447 legacySortElements(legacy_sort_start);
3449 // Formname and regeneration setting
3450 if (!m_is_form_regenerated) {
3451 // Only set previous form name if we purposefully showed a new formspec
3452 m_last_formname = m_text_dst->m_formname;
3453 m_is_form_regenerated = true;
3457 void GUIFormSpecMenu::legacySortElements(core::list<IGUIElement *>::Iterator from)
3460 Draw order for formspec_version <= 2:
3464 0 All other elements
3466 2 item_image, item_image_button
3471 if (from == Children.end())
3472 from = Children.begin();
3476 core::list<IGUIElement *>::Iterator to = Children.end();
3477 // 1: Copy into a sortable container
3478 std::vector<IGUIElement *> elements;
3479 for (auto it = from; it != to; ++it)
3480 elements.emplace_back(*it);
3482 // 2: Sort the container
3483 std::stable_sort(elements.begin(), elements.end(),
3484 [this] (const IGUIElement *a, const IGUIElement *b) -> bool {
3485 const FieldSpec *spec_a = getSpecByID(a->getID());
3486 const FieldSpec *spec_b = getSpecByID(b->getID());
3487 return spec_a && spec_b &&
3488 spec_a->priority < spec_b->priority;
3491 // 3: Re-assign the pointers
3492 for (auto e : elements) {
3499 bool GUIFormSpecMenu::getAndroidUIInput()
3501 if (!hasAndroidUIInput())
3505 if (porting::getInputDialogState() == -1)
3508 std::string fieldname = m_jni_field_name;
3509 m_jni_field_name.clear();
3511 for (const FieldSpec &field : m_fields) {
3512 if (field.fname != fieldname)
3515 IGUIElement *element = getElementFromId(field.fid, true);
3517 if (!element || element->getType() != irr::gui::EGUIET_EDIT_BOX)
3520 std::string text = porting::getInputDialogValue();
3521 ((gui::IGUIEditBox *)element)->setText(utf8_to_wide(text).c_str());
3527 GUIInventoryList::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
3529 for (const GUIInventoryList *e : m_inventorylists) {
3530 s32 item_index = e->getItemIndexAtPos(p);
3531 if (item_index != -1)
3532 return GUIInventoryList::ItemSpec(e->getInventoryloc(), e->getListname(),
3536 return GUIInventoryList::ItemSpec(InventoryLocation(), "", -1);
3539 void GUIFormSpecMenu::drawSelectedItem()
3541 video::IVideoDriver* driver = Environment->getVideoDriver();
3543 if (!m_selected_item) {
3544 // reset rotation time
3545 drawItemStack(driver, m_font, ItemStack(),
3546 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)), NULL,
3547 m_client, IT_ROT_DRAGGED);
3551 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3553 InventoryList *list = inv->getList(m_selected_item->listname);
3555 ItemStack stack = list->getItem(m_selected_item->i);
3556 stack.count = m_selected_amount;
3558 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
3559 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
3560 rect.constrainTo(driver->getViewPort());
3561 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
3564 void GUIFormSpecMenu::drawMenu()
3567 const std::string &newform = m_form_src->getForm();
3568 if (newform != m_formspec_string) {
3569 m_formspec_string = newform;
3570 m_is_form_regenerated = false;
3571 regenerateGui(m_screensize_old);
3575 gui::IGUISkin* skin = Environment->getSkin();
3576 sanity_check(skin != NULL);
3577 gui::IGUIFont *old_font = skin->getFont();
3578 skin->setFont(m_font);
3580 m_hovered_item_tooltips.clear();
3582 updateSelectedItem();
3584 video::IVideoDriver* driver = Environment->getVideoDriver();
3587 Draw background color
3589 v2u32 screenSize = driver->getScreenSize();
3590 core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
3593 driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
3594 if (m_bgnonfullscreen)
3595 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
3598 Draw rect_mode tooltip
3600 m_tooltip_element->setVisible(false);
3602 for (const auto &pair : m_tooltip_rects) {
3603 const core::rect<s32> &rect = pair.first->getAbsoluteClippingRect();
3604 if (rect.getArea() > 0 && rect.isPointInside(m_pointer)) {
3605 const std::wstring &text = pair.second.tooltip;
3606 if (!text.empty()) {
3607 showTooltip(text, pair.second.color, pair.second.bgcolor);
3616 for (gui::IGUIElement *e : m_backgrounds) {
3617 e->setVisible(true);
3619 e->setVisible(false);
3622 // Some elements are only visible while being drawn
3623 for (gui::IGUIElement *e : m_clickthrough_elements)
3624 e->setVisible(true);
3627 This is where all the drawing happens.
3629 core::list<IGUIElement*>::Iterator it = Children.begin();
3630 for (; it != Children.end(); ++it)
3631 if ((*it)->isNotClipped() ||
3632 AbsoluteClippingRect.isRectCollided(
3633 (*it)->getAbsolutePosition()))
3636 for (gui::IGUIElement *e : m_clickthrough_elements)
3637 e->setVisible(false);
3639 // Draw hovered item tooltips
3640 for (const std::string &tooltip : m_hovered_item_tooltips) {
3641 showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
3642 m_default_tooltip_bgcolor);
3645 if (m_hovered_item_tooltips.empty()) {
3646 // reset rotation time
3647 drawItemStack(driver, m_font, ItemStack(),
3648 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
3649 NULL, m_client, IT_ROT_HOVERED);
3652 /* TODO find way to show tooltips on touchscreen */
3653 #ifndef HAVE_TOUCHSCREENGUI
3654 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
3658 Draw fields/buttons tooltips and update the mouse cursor
3660 gui::IGUIElement *hovered =
3661 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
3663 #ifndef HAVE_TOUCHSCREENGUI
3664 gui::ICursorControl *cursor_control = RenderingEngine::get_raw_device()->
3666 gui::ECURSOR_ICON current_cursor_icon = cursor_control->getActiveIcon();
3668 bool hovered_element_found = false;
3670 if (hovered != NULL) {
3672 core::rect<s32> rect = hovered->getAbsoluteClippingRect();
3673 driver->draw2DRectangle(0x22FFFF00, rect, &rect);
3676 s32 id = hovered->getID();
3679 m_old_tooltip_id = id;
3681 if (id == m_old_tooltip_id) {
3682 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
3684 m_hovered_time = porting::getTimeMs();
3685 m_old_tooltip_id = id;
3689 // Find and update the current tooltip and cursor icon
3691 for (const FieldSpec &field : m_fields) {
3693 if (field.fid != id)
3696 if (delta >= m_tooltip_show_delay) {
3697 const std::wstring &text = m_tooltips[field.fname].tooltip;
3699 showTooltip(text, m_tooltips[field.fname].color,
3700 m_tooltips[field.fname].bgcolor);
3703 #ifndef HAVE_TOUCHSCREENGUI
3704 if (field.ftype != f_HyperText && // Handled directly in guiHyperText
3705 current_cursor_icon != field.fcursor_icon)
3706 cursor_control->setActiveIcon(field.fcursor_icon);
3709 hovered_element_found = true;
3716 if (!hovered_element_found) {
3717 // no element is hovered
3718 #ifndef HAVE_TOUCHSCREENGUI
3719 if (current_cursor_icon != ECI_NORMAL)
3720 cursor_control->setActiveIcon(ECI_NORMAL);
3724 m_tooltip_element->draw();
3727 Draw dragged item stack
3731 skin->setFont(old_font);
3735 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
3736 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
3738 EnrichedString ntext(text);
3739 ntext.setDefaultColor(color);
3740 if (!ntext.hasBackground())
3741 ntext.setBackground(bgcolor);
3743 setStaticText(m_tooltip_element, ntext);
3745 // Tooltip size and offset
3746 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
3747 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
3749 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
3750 int tooltip_offset_x = m_btn_height;
3751 int tooltip_offset_y = m_btn_height;
3753 tooltip_offset_x *= 3;
3754 tooltip_offset_y = 0;
3755 if (m_pointer.X > (s32)screenSize.X / 2)
3756 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
3758 // Hide tooltip after ETIE_LEFT_UP
3759 if (m_pointer.X == 0)
3763 // Calculate and set the tooltip position
3764 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
3765 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
3766 if (tooltip_x + tooltip_width > (s32)screenSize.X)
3767 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
3768 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
3769 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
3771 m_tooltip_element->setRelativePosition(
3773 core::position2d<s32>(tooltip_x, tooltip_y),
3774 core::dimension2d<s32>(tooltip_width, tooltip_height)
3778 // Display the tooltip
3779 m_tooltip_element->setVisible(true);
3780 bringToFront(m_tooltip_element);
3783 void GUIFormSpecMenu::updateSelectedItem()
3785 verifySelectedItem();
3787 // If craftresult is nonempty and nothing else is selected, select it now.
3788 if (!m_selected_item) {
3789 for (const GUIInventoryList *e : m_inventorylists) {
3790 if (e->getListname() != "craftpreview")
3793 Inventory *inv = m_invmgr->getInventory(e->getInventoryloc());
3797 InventoryList *list = inv->getList("craftresult");
3799 if (!list || list->getSize() == 0)
3802 const ItemStack &item = list->getItem(0);
3806 // Grab selected item from the crafting result list
3807 m_selected_item = new GUIInventoryList::ItemSpec;
3808 m_selected_item->inventoryloc = e->getInventoryloc();
3809 m_selected_item->listname = "craftresult";
3810 m_selected_item->i = 0;
3811 m_selected_amount = item.count;
3812 m_selected_dragging = false;
3817 // If craftresult is selected, keep the whole stack selected
3818 if (m_selected_item && m_selected_item->listname == "craftresult")
3819 m_selected_amount = verifySelectedItem().count;
3822 ItemStack GUIFormSpecMenu::verifySelectedItem()
3824 // If the selected stack has become empty for some reason, deselect it.
3825 // If the selected stack has become inaccessible, deselect it.
3826 // If the selected stack has become smaller, adjust m_selected_amount.
3827 // Return the selected stack.
3829 if (m_selected_item) {
3830 if (m_selected_item->isValid()) {
3831 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3833 InventoryList *list = inv->getList(m_selected_item->listname);
3834 if (list && (u32) m_selected_item->i < list->getSize()) {
3835 ItemStack stack = list->getItem(m_selected_item->i);
3836 if (!m_selected_swap.empty()) {
3837 if (m_selected_swap.name == stack.name &&
3838 m_selected_swap.count == stack.count)
3839 m_selected_swap.clear();
3841 m_selected_amount = std::min(m_selected_amount, stack.count);
3850 // selection was not valid
3851 delete m_selected_item;
3852 m_selected_item = nullptr;
3853 m_selected_amount = 0;
3854 m_selected_dragging = false;
3859 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode)
3865 if (quitmode == quit_mode_accept) {
3866 fields["quit"] = "true";
3869 if (quitmode == quit_mode_cancel) {
3870 fields["quit"] = "true";
3871 m_text_dst->gotText(fields);
3875 if (current_keys_pending.key_down) {
3876 fields["key_down"] = "true";
3877 current_keys_pending.key_down = false;
3880 if (current_keys_pending.key_up) {
3881 fields["key_up"] = "true";
3882 current_keys_pending.key_up = false;
3885 if (current_keys_pending.key_enter) {
3886 fields["key_enter"] = "true";
3887 current_keys_pending.key_enter = false;
3890 if (!current_field_enter_pending.empty()) {
3891 fields["key_enter_field"] = current_field_enter_pending;
3892 current_field_enter_pending = "";
3895 if (current_keys_pending.key_escape) {
3896 fields["key_escape"] = "true";
3897 current_keys_pending.key_escape = false;
3900 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
3902 std::string name = s.fname;
3903 if (s.ftype == f_Button) {
3904 fields[name] = wide_to_utf8(s.flabel);
3905 } else if (s.ftype == f_Table) {
3906 GUITable *table = getTable(s.fname);
3908 fields[name] = table->checkEvent();
3910 } else if (s.ftype == f_DropDown) {
3911 // No dynamic cast possible due to some distributions shipped
3912 // without rtti support in Irrlicht
3913 IGUIElement *element = getElementFromId(s.fid, true);
3914 gui::IGUIComboBox *e = NULL;
3915 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
3916 e = static_cast<gui::IGUIComboBox *>(element);
3918 warningstream << "GUIFormSpecMenu::acceptInput: dropdown "
3919 << "field without dropdown element" << std::endl;
3922 s32 selected = e->getSelected();
3923 if (selected >= 0) {
3924 if (m_dropdown_index_event.find(s.fname) !=
3925 m_dropdown_index_event.end()) {
3926 fields[name] = std::to_string(selected + 1);
3928 std::vector<std::string> *dropdown_values =
3929 getDropDownValues(s.fname);
3930 if (dropdown_values && selected < (s32)dropdown_values->size())
3931 fields[name] = (*dropdown_values)[selected];
3934 } else if (s.ftype == f_TabHeader) {
3935 // No dynamic cast possible due to some distributions shipped
3936 // without rtti support in Irrlicht
3937 IGUIElement *element = getElementFromId(s.fid, true);
3938 gui::IGUITabControl *e = nullptr;
3939 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
3940 e = static_cast<gui::IGUITabControl *>(element);
3944 std::stringstream ss;
3945 ss << (e->getActiveTab() +1);
3946 fields[name] = ss.str();
3948 } else if (s.ftype == f_CheckBox) {
3949 // No dynamic cast possible due to some distributions shipped
3950 // without rtti support in Irrlicht
3951 IGUIElement *element = getElementFromId(s.fid, true);
3952 gui::IGUICheckBox *e = nullptr;
3953 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
3954 e = static_cast<gui::IGUICheckBox*>(element);
3959 fields[name] = "true";
3961 fields[name] = "false";
3963 } else if (s.ftype == f_ScrollBar) {
3964 // No dynamic cast possible due to some distributions shipped
3965 // without rtti support in Irrlicht
3966 IGUIElement *element = getElementFromId(s.fid, true);
3967 GUIScrollBar *e = nullptr;
3968 if (element && element->getType() == gui::EGUIET_ELEMENT)
3969 e = static_cast<GUIScrollBar *>(element);
3972 std::stringstream os;
3974 if (s.fdefault == L"Changed")
3975 fields[name] = "CHG:" + os.str();
3977 fields[name] = "VAL:" + os.str();
3979 } else if (s.ftype == f_AnimatedImage) {
3980 // No dynamic cast possible due to some distributions shipped
3981 // without rtti support in Irrlicht
3982 IGUIElement *element = getElementFromId(s.fid, true);
3983 GUIAnimatedImage *e = nullptr;
3984 if (element && element->getType() == gui::EGUIET_ELEMENT)
3985 e = static_cast<GUIAnimatedImage *>(element);
3988 fields[name] = std::to_string(e->getFrameIndex() + 1);
3990 IGUIElement *e = getElementFromId(s.fid, true);
3992 fields[name] = wide_to_utf8(e->getText());
3997 m_text_dst->gotText(fields);
4001 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
4003 // The IGUITabControl renders visually using the skin's selected
4004 // font, which we override for the duration of form drawing,
4005 // but computes tab hotspots based on how it would have rendered
4006 // using the font that is selected at the time of button release.
4007 // To make these two consistent, temporarily override the skin's
4008 // font while the IGUITabControl is processing the event.
4009 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
4010 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
4011 s32 x = event.MouseInput.X;
4012 s32 y = event.MouseInput.Y;
4013 gui::IGUIElement *hovered =
4014 Environment->getRootGUIElement()->getElementFromPoint(
4015 core::position2d<s32>(x, y));
4016 if (hovered && isMyChild(hovered) &&
4017 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
4018 gui::IGUISkin* skin = Environment->getSkin();
4019 sanity_check(skin != NULL);
4020 gui::IGUIFont *old_font = skin->getFont();
4021 skin->setFont(m_font);
4022 bool retval = hovered->OnEvent(event);
4023 skin->setFont(old_font);
4028 // Fix Esc/Return key being eaten by checkboxen and tables
4029 if (event.EventType == EET_KEY_INPUT_EVENT) {
4030 KeyPress kp(event.KeyInput);
4031 if (kp == EscapeKey || kp == CancelKey
4032 || kp == getKeySetting("keymap_inventory")
4033 || event.KeyInput.Key==KEY_RETURN) {
4034 gui::IGUIElement *focused = Environment->getFocus();
4035 if (focused && isMyChild(focused) &&
4036 (focused->getType() == gui::EGUIET_LIST_BOX ||
4037 focused->getType() == gui::EGUIET_CHECK_BOX) &&
4038 (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
4039 event.KeyInput.Key != KEY_RETURN)) {
4045 // Mouse wheel and move events: send to hovered element instead of focused
4046 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
4047 (event.MouseInput.Event == EMIE_MOUSE_WHEEL ||
4048 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
4049 event.MouseInput.ButtonStates == 0))) {
4050 s32 x = event.MouseInput.X;
4051 s32 y = event.MouseInput.Y;
4052 gui::IGUIElement *hovered =
4053 Environment->getRootGUIElement()->getElementFromPoint(
4054 core::position2d<s32>(x, y));
4055 if (hovered && isMyChild(hovered)) {
4056 hovered->OnEvent(event);
4057 return event.MouseInput.Event == EMIE_MOUSE_WHEEL;
4061 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
4062 /* TODO add a check like:
4063 if (event.JoystickEvent != joystick_we_listen_for)
4066 bool handled = m_joystick->handleEvent(event.JoystickEvent);
4068 if (m_joystick->wasKeyDown(KeyType::ESC)) {
4070 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
4072 acceptInput(quit_mode_accept);
4080 return GUIModalMenu::preprocessEvent(event);
4083 void GUIFormSpecMenu::tryClose()
4087 acceptInput(quit_mode_cancel);
4090 m_text_dst->gotText(L"MenuQuit");
4094 enum ButtonEventType : u8
4107 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
4109 if (event.EventType==EET_KEY_INPUT_EVENT) {
4110 KeyPress kp(event.KeyInput);
4111 if (event.KeyInput.PressedDown && (
4112 (kp == EscapeKey) || (kp == CancelKey) ||
4113 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
4118 if (m_client != NULL && event.KeyInput.PressedDown &&
4119 (kp == getKeySetting("keymap_screenshot"))) {
4120 m_client->makeScreenshot();
4123 if (event.KeyInput.PressedDown && kp == getKeySetting("keymap_toggle_debug"))
4124 m_show_debug = !m_show_debug;
4126 if (event.KeyInput.PressedDown &&
4127 (event.KeyInput.Key==KEY_RETURN ||
4128 event.KeyInput.Key==KEY_UP ||
4129 event.KeyInput.Key==KEY_DOWN)
4131 switch (event.KeyInput.Key) {
4133 current_keys_pending.key_enter = true;
4136 current_keys_pending.key_up = true;
4139 current_keys_pending.key_down = true;
4143 //can't happen at all!
4144 FATAL_ERROR("Reached a source line that can't ever been reached");
4147 if (current_keys_pending.key_enter && m_allowclose) {
4148 acceptInput(quit_mode_accept);
4158 /* Mouse event other than movement, or crossing the border of inventory
4159 field while holding right mouse button
4161 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
4162 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
4163 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
4164 event.MouseInput.isRightPressed() &&
4165 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
4167 // Get selected item and hovered/clicked item (s)
4169 m_old_tooltip_id = -1;
4170 updateSelectedItem();
4171 GUIInventoryList::ItemSpec s = getItemAtPos(m_pointer);
4173 Inventory *inv_selected = NULL;
4174 Inventory *inv_s = NULL;
4175 InventoryList *list_s = NULL;
4177 if (m_selected_item) {
4178 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
4179 sanity_check(inv_selected);
4180 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
4187 inv_s = m_invmgr->getInventory(s.inventoryloc);
4190 errorstream << "InventoryMenu: The selected inventory location "
4191 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
4193 s.i = -1; // make it invalid again
4197 list_s = inv_s->getList(s.listname);
4198 if (list_s == NULL) {
4199 verbosestream << "InventoryMenu: The selected inventory list \""
4200 << s.listname << "\" does not exist" << std::endl;
4201 s.i = -1; // make it invalid again
4205 if ((u32)s.i >= list_s->getSize()) {
4206 infostream << "InventoryMenu: The selected inventory list \""
4207 << s.listname << "\" is too small (i=" << s.i << ", size="
4208 << list_s->getSize() << ")" << std::endl;
4209 s.i = -1; // make it invalid again
4213 s_count = list_s->getItem(s.i).count;
4216 bool identical = m_selected_item && s.isValid() &&
4217 (inv_selected == inv_s) &&
4218 (m_selected_item->listname == s.listname) &&
4219 (m_selected_item->i == s.i);
4221 ButtonEventType button = BET_LEFT;
4222 ButtonEventType updown = BET_OTHER;
4223 switch (event.MouseInput.Event) {
4224 case EMIE_LMOUSE_PRESSED_DOWN:
4225 button = BET_LEFT; updown = BET_DOWN;
4227 case EMIE_RMOUSE_PRESSED_DOWN:
4228 button = BET_RIGHT; updown = BET_DOWN;
4230 case EMIE_MMOUSE_PRESSED_DOWN:
4231 button = BET_MIDDLE; updown = BET_DOWN;
4233 case EMIE_MOUSE_WHEEL:
4234 button = (event.MouseInput.Wheel > 0) ?
4235 BET_WHEEL_UP : BET_WHEEL_DOWN;
4238 case EMIE_LMOUSE_LEFT_UP:
4239 button = BET_LEFT; updown = BET_UP;
4241 case EMIE_RMOUSE_LEFT_UP:
4242 button = BET_RIGHT; updown = BET_UP;
4244 case EMIE_MMOUSE_LEFT_UP:
4245 button = BET_MIDDLE; updown = BET_UP;
4247 case EMIE_MOUSE_MOVED:
4254 // Set this number to a positive value to generate a move action
4255 // from m_selected_item to s.
4256 u32 move_amount = 0;
4258 // Set this number to a positive value to generate a move action
4259 // from s to the next inventory ring.
4260 u32 shift_move_amount = 0;
4262 // Set this number to a positive value to generate a drop action
4263 // from m_selected_item.
4264 u32 drop_amount = 0;
4266 // Set this number to a positive value to generate a craft action at s.
4267 u32 craft_amount = 0;
4271 // Some mouse button has been pressed
4273 //infostream << "Mouse button " << button << " pressed at p=("
4274 // << event.MouseInput.X << "," << event.MouseInput.Y << ")"
4277 m_selected_dragging = false;
4279 if (s.isValid() && s.listname == "craftpreview") {
4280 // Craft preview has been clicked: craft
4281 craft_amount = (button == BET_MIDDLE ? 10 : 1);
4282 } else if (!m_selected_item) {
4283 if (s_count && button != BET_WHEEL_UP) {
4284 // Non-empty stack has been clicked: select or shift-move it
4285 m_selected_item = new GUIInventoryList::ItemSpec(s);
4288 if (button == BET_RIGHT)
4289 count = (s_count + 1) / 2;
4290 else if (button == BET_MIDDLE)
4291 count = MYMIN(s_count, 10);
4292 else if (button == BET_WHEEL_DOWN)
4297 if (!event.MouseInput.Shift) {
4298 // no shift: select item
4299 m_selected_amount = count;
4300 m_selected_dragging = button != BET_WHEEL_DOWN;
4301 m_auto_place = false;
4303 // shift pressed: move item, right click moves 1
4304 shift_move_amount = button == BET_RIGHT ? 1 : count;
4307 } else { // m_selected_item != NULL
4308 assert(m_selected_amount >= 1);
4311 // Clicked a slot: move
4312 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4314 else if (button == BET_MIDDLE)
4315 move_amount = MYMIN(m_selected_amount, 10);
4316 else if (button == BET_LEFT)
4317 move_amount = m_selected_amount;
4321 if (button == BET_WHEEL_DOWN) {
4322 if (m_selected_amount < s_count)
4323 ++m_selected_amount;
4325 if (move_amount >= m_selected_amount)
4326 m_selected_amount = 0;
4328 m_selected_amount -= move_amount;
4332 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
4333 && button != BET_WHEEL_DOWN) {
4334 // Clicked outside of the window: drop
4335 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4337 else if (button == BET_MIDDLE)
4338 drop_amount = MYMIN(m_selected_amount, 10);
4340 drop_amount = m_selected_amount;
4345 // Some mouse button has been released
4347 //infostream<<"Mouse button "<<button<<" released at p=("
4348 // <<p.X<<","<<p.Y<<")"<<std::endl;
4350 if (m_selected_dragging && m_selected_item) {
4353 // Dragged to different slot: move all selected
4354 move_amount = m_selected_amount;
4356 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
4357 // Dragged outside of window: drop all selected
4358 drop_amount = m_selected_amount;
4362 m_selected_dragging = false;
4363 // Keep track of whether the mouse button be released
4364 // One click is drag without dropping. Click + release
4365 // + click changes to drop item when moved mode
4366 if (m_selected_item)
4367 m_auto_place = true;
4370 // Mouse has been moved and rmb is down and mouse pointer just
4371 // entered a new inventory field (checked in the entry-if, this
4372 // is the only action here that is generated by mouse movement)
4373 if (m_selected_item && s.isValid() && s.listname != "craftpreview") {
4375 // TODO: middle mouse to move 10 items might be handy
4377 // Only move an item if the destination slot is empty
4378 // or contains the same item type as what is going to be
4380 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4381 InventoryList *list_to = list_s;
4382 assert(list_from && list_to);
4383 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4384 ItemStack stack_to = list_to->getItem(s.i);
4385 if (stack_to.empty() || stack_to.name == stack_from.name)
4394 // Possibly send inventory action to server
4395 if (move_amount > 0) {
4396 // Send IAction::Move
4398 assert(m_selected_item && m_selected_item->isValid());
4399 assert(s.isValid());
4401 assert(inv_selected && inv_s);
4402 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4403 InventoryList *list_to = list_s;
4404 assert(list_from && list_to);
4405 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4406 ItemStack stack_to = list_to->getItem(s.i);
4408 // Check how many items can be moved
4409 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
4410 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
4412 // If source stack cannot be added to destination stack at all,
4414 if (leftover.count == stack_from.count &&
4415 leftover.name == stack_from.name) {
4417 if (m_selected_swap.empty()) {
4418 m_selected_amount = stack_to.count;
4419 m_selected_dragging = false;
4421 // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
4422 // Skip next validation checks due async inventory calls
4423 m_selected_swap = stack_to;
4428 // Source stack goes fully into destination stack
4429 else if (leftover.empty()) {
4430 m_selected_amount -= move_amount;
4432 // Source stack goes partly into destination stack
4434 move_amount -= leftover.count;
4435 m_selected_amount -= move_amount;
4439 infostream << "Handing IAction::Move to manager" << std::endl;
4440 IMoveAction *a = new IMoveAction();
4441 a->count = move_amount;
4442 a->from_inv = m_selected_item->inventoryloc;
4443 a->from_list = m_selected_item->listname;
4444 a->from_i = m_selected_item->i;
4445 a->to_inv = s.inventoryloc;
4446 a->to_list = s.listname;
4448 m_invmgr->inventoryAction(a);
4450 } else if (shift_move_amount > 0) {
4451 u32 mis = m_inventory_rings.size();
4453 for (; i < mis; i++) {
4454 const ListRingSpec &sp = m_inventory_rings[i];
4455 if (sp.inventoryloc == s.inventoryloc
4456 && sp.listname == s.listname)
4460 if (i >= mis) // if not found
4462 u32 to_inv_ind = (i + 1) % mis;
4463 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
4464 InventoryList *list_from = list_s;
4467 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
4470 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
4473 ItemStack stack_from = list_from->getItem(s.i);
4474 assert(shift_move_amount <= stack_from.count);
4476 infostream << "Handing IAction::Move to manager" << std::endl;
4477 IMoveAction *a = new IMoveAction();
4478 a->count = shift_move_amount;
4479 a->from_inv = s.inventoryloc;
4480 a->from_list = s.listname;
4482 a->to_inv = to_inv_sp.inventoryloc;
4483 a->to_list = to_inv_sp.listname;
4484 a->move_somewhere = true;
4485 m_invmgr->inventoryAction(a);
4487 } else if (drop_amount > 0) {
4488 // Send IAction::Drop
4490 assert(m_selected_item && m_selected_item->isValid());
4491 assert(inv_selected);
4492 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4494 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4496 // Check how many items can be dropped
4497 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
4498 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
4499 m_selected_amount -= drop_amount;
4501 infostream << "Handing IAction::Drop to manager" << std::endl;
4502 IDropAction *a = new IDropAction();
4503 a->count = drop_amount;
4504 a->from_inv = m_selected_item->inventoryloc;
4505 a->from_list = m_selected_item->listname;
4506 a->from_i = m_selected_item->i;
4507 m_invmgr->inventoryAction(a);
4508 } else if (craft_amount > 0) {
4509 assert(s.isValid());
4511 // if there are no items selected or the selected item
4512 // belongs to craftresult list, proceed with crafting
4513 if (!m_selected_item ||
4514 !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
4518 // Send IACTION_CRAFT
4519 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
4520 ICraftAction *a = new ICraftAction();
4521 a->count = craft_amount;
4522 a->craft_inv = s.inventoryloc;
4523 m_invmgr->inventoryAction(a);
4527 // If m_selected_amount has been decreased to zero, deselect
4528 if (m_selected_amount == 0) {
4529 m_selected_swap.clear();
4530 delete m_selected_item;
4531 m_selected_item = nullptr;
4532 m_selected_amount = 0;
4533 m_selected_dragging = false;
4535 m_old_pointer = m_pointer;
4538 if (event.EventType == EET_GUI_EVENT) {
4539 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
4541 // find the element that was clicked
4542 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4543 if ((s.ftype == f_TabHeader) &&
4544 (s.fid == event.GUIEvent.Caller->getID())) {
4545 if (!s.sound.empty() && m_sound_manager)
4546 m_sound_manager->playSound(s.sound, false, 1.0f);
4554 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
4556 if (!canTakeFocus(event.GUIEvent.Element)) {
4557 infostream<<"GUIFormSpecMenu: Not allowing focus change."
4559 // Returning true disables focus change
4563 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
4564 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
4565 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
4566 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
4567 s32 caller_id = event.GUIEvent.Caller->getID();
4569 if (caller_id == 257) {
4571 acceptInput(quit_mode_accept);
4575 m_text_dst->gotText(L"ExitButton");
4577 // quitMenu deallocates menu
4581 // find the element that was clicked
4582 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4583 // if its a button, set the send field so
4584 // lua knows which button was pressed
4586 if (caller_id != s.fid)
4589 if (s.ftype == f_Button || s.ftype == f_CheckBox) {
4590 if (!s.sound.empty() && m_sound_manager)
4591 m_sound_manager->playSound(s.sound, false, 1.0f);
4596 acceptInput(quit_mode_accept);
4599 m_text_dst->gotText(L"ExitButton");
4604 acceptInput(quit_mode_no);
4608 } else if (s.ftype == f_DropDown) {
4609 // only send the changed dropdown
4610 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4611 if (s2.ftype == f_DropDown) {
4615 if (!s.sound.empty() && m_sound_manager)
4616 m_sound_manager->playSound(s.sound, false, 1.0f);
4618 acceptInput(quit_mode_no);
4620 // revert configuration to make sure dropdowns are sent on
4621 // regular button click
4622 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4623 if (s2.ftype == f_DropDown) {
4628 } else if (s.ftype == f_ScrollBar) {
4629 s.fdefault = L"Changed";
4630 acceptInput(quit_mode_no);
4632 } else if (s.ftype == f_Unknown || s.ftype == f_HyperText) {
4633 if (!s.sound.empty() && m_sound_manager)
4634 m_sound_manager->playSound(s.sound, false, 1.0f);
4642 if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) {
4643 // move scroll_containers
4644 for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers)
4645 c.second->onScrollEvent(event.GUIEvent.Caller);
4648 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
4649 if (event.GUIEvent.Caller->getID() > 257) {
4650 bool close_on_enter = true;
4651 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4652 if (s.ftype == f_Unknown &&
4653 s.fid == event.GUIEvent.Caller->getID()) {
4654 current_field_enter_pending = s.fname;
4655 std::unordered_map<std::string, bool>::const_iterator it =
4656 field_close_on_enter.find(s.fname);
4657 if (it != field_close_on_enter.end())
4658 close_on_enter = (*it).second;
4664 if (m_allowclose && close_on_enter) {
4665 current_keys_pending.key_enter = true;
4666 acceptInput(quit_mode_accept);
4669 current_keys_pending.key_enter = true;
4672 // quitMenu deallocates menu
4677 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
4678 int current_id = event.GUIEvent.Caller->getID();
4679 if (current_id > 257) {
4680 // find the element that was clicked
4681 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4682 // if it's a table, set the send field
4683 // so lua knows which table was changed
4684 if ((s.ftype == f_Table) && (s.fid == current_id)) {
4695 return Parent ? Parent->OnEvent(event) : false;
4699 * get name of element by element id
4700 * @param id of element
4701 * @return name string or empty string
4703 std::string GUIFormSpecMenu::getNameByID(s32 id)
4705 for (FieldSpec &spec : m_fields) {
4713 const GUIFormSpecMenu::FieldSpec *GUIFormSpecMenu::getSpecByID(s32 id)
4715 for (FieldSpec &spec : m_fields) {
4723 * get label of element by id
4724 * @param id of element
4725 * @return label string or empty string
4727 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
4729 for (FieldSpec &spec : m_fields) {
4736 StyleSpec GUIFormSpecMenu::getDefaultStyleForElement(const std::string &type,
4737 const std::string &name, const std::string &parent_type) {
4738 return getStyleForElement(type, name, parent_type)[StyleSpec::STATE_DEFAULT];
4741 std::array<StyleSpec, StyleSpec::NUM_STATES> GUIFormSpecMenu::getStyleForElement(
4742 const std::string &type, const std::string &name, const std::string &parent_type)
4744 std::array<StyleSpec, StyleSpec::NUM_STATES> ret;
4746 auto it = theme_by_type.find("*");
4747 if (it != theme_by_type.end()) {
4748 for (const StyleSpec &spec : it->second)
4749 ret[(u32)spec.getState()] |= spec;
4752 it = theme_by_name.find("*");
4753 if (it != theme_by_name.end()) {
4754 for (const StyleSpec &spec : it->second)
4755 ret[(u32)spec.getState()] |= spec;
4758 if (!parent_type.empty()) {
4759 it = theme_by_type.find(parent_type);
4760 if (it != theme_by_type.end()) {
4761 for (const StyleSpec &spec : it->second)
4762 ret[(u32)spec.getState()] |= spec;
4766 it = theme_by_type.find(type);
4767 if (it != theme_by_type.end()) {
4768 for (const StyleSpec &spec : it->second)
4769 ret[(u32)spec.getState()] |= spec;
4772 it = theme_by_name.find(name);
4773 if (it != theme_by_name.end()) {
4774 for (const StyleSpec &spec : it->second)
4775 ret[(u32)spec.getState()] |= spec;