3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guiFormSpecMenu.h"
27 #include "guiScrollBar.h"
29 #include "constants.h"
31 #include "client/keycode.h"
32 #include "util/strfnd.h"
33 #include <IGUIButton.h>
34 #include <IGUICheckBox.h>
35 #include <IGUIComboBox.h>
36 #include <IGUIEditBox.h>
37 #include <IGUIStaticText.h>
39 #include <IGUITabControl.h>
40 #include "client/renderingengine.h"
42 #include "client/tile.h" // ITextureSource
43 #include "client/hud.h" // drawItemStack
47 #include "scripting_server.h"
48 #include "mainmenumanager.h"
51 #include "client/client.h"
52 #include "client/fontengine.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 "guiScrollBar.h"
69 #include "intlGUIEditBox.h"
70 #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, IFormSource *fsrc, TextDest *tdst,
101 const std::string &formspecPrepend,
102 bool remap_dbl_click):
103 GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr),
107 m_formspec_prepend(formspecPrepend),
110 m_joystick(joystick),
111 m_remap_dbl_click(remap_dbl_click)
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_doubleclickdetect[0].time = 0;
119 m_doubleclickdetect[1].time = 0;
121 m_doubleclickdetect[0].pos = v2s32(0, 0);
122 m_doubleclickdetect[1].pos = v2s32(0, 0);
124 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
125 m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
128 GUIFormSpecMenu::~GUIFormSpecMenu()
132 for (auto &table_it : m_tables)
133 table_it.second->drop();
134 for (auto &inventorylist_it : m_inventorylists)
135 inventorylist_it->drop();
136 for (auto &checkbox_it : m_checkboxes)
137 checkbox_it.second->drop();
138 for (auto &scrollbar_it : m_scrollbars)
139 scrollbar_it.second->drop();
140 for (auto &background_it : m_backgrounds)
141 background_it->drop();
142 for (auto &tooltip_rect_it : m_tooltip_rects)
143 tooltip_rect_it.first->drop();
145 delete m_selected_item;
150 void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
151 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
152 const std::string &formspecPrepend)
154 if (cur_formspec == nullptr) {
155 cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
156 client, client->getTextureSource(), fs_src, txt_dest, formspecPrepend);
157 cur_formspec->doPause = false;
160 Caution: do not call (*cur_formspec)->drop() here --
161 the reference might outlive the menu, so we will
162 periodically check if *cur_formspec is the only
163 remaining reference (i.e. the menu was removed)
164 and delete it in that case.
168 cur_formspec->setFormspecPrepend(formspecPrepend);
169 cur_formspec->setFormSource(fs_src);
170 cur_formspec->setTextDest(txt_dest);
174 void GUIFormSpecMenu::removeChildren()
176 const core::list<gui::IGUIElement*> &children = getChildren();
178 while (!children.empty()) {
179 (*children.getLast())->remove();
182 if (m_tooltip_element) {
183 m_tooltip_element->remove();
184 m_tooltip_element->drop();
185 m_tooltip_element = nullptr;
189 void GUIFormSpecMenu::setInitialFocus()
191 // Set initial focus according to following order of precedence:
192 // 1. first empty editbox
196 // 5. first focusable (not statictext, not tabheader)
197 // 6. first child element
199 core::list<gui::IGUIElement*> children = getChildren();
201 // in case "children" contains any NULL elements, remove them
202 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
203 it != children.end();) {
207 it = children.erase(it);
210 // 1. first empty editbox
211 for (gui::IGUIElement *it : children) {
212 if (it->getType() == gui::EGUIET_EDIT_BOX
213 && it->getText()[0] == 0) {
214 Environment->setFocus(it);
220 for (gui::IGUIElement *it : children) {
221 if (it->getType() == gui::EGUIET_EDIT_BOX) {
222 Environment->setFocus(it);
228 for (gui::IGUIElement *it : children) {
229 if (it->getTypeName() == std::string("GUITable")) {
230 Environment->setFocus(it);
236 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
237 it != children.end(); --it) {
238 if ((*it)->getType() == gui::EGUIET_BUTTON) {
239 Environment->setFocus(*it);
244 // 5. first focusable (not statictext, not tabheader)
245 for (gui::IGUIElement *it : children) {
246 if (it->getType() != gui::EGUIET_STATIC_TEXT &&
247 it->getType() != gui::EGUIET_TAB_CONTROL) {
248 Environment->setFocus(it);
253 // 6. first child element
254 if (children.empty())
255 Environment->setFocus(this);
257 Environment->setFocus(*(children.begin()));
260 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
262 for (auto &table : m_tables) {
263 if (tablename == table.first.fname)
269 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
271 for (auto &dropdown : m_dropdowns) {
272 if (name == dropdown.first.fname)
273 return &dropdown.second;
278 v2s32 GUIFormSpecMenu::getElementBasePos(const std::vector<std::string> *v_pos)
280 v2f32 pos_f = v2f32(padding.X, padding.Y) + pos_offset * spacing;
282 pos_f.X += stof((*v_pos)[0]) * spacing.X;
283 pos_f.Y += stof((*v_pos)[1]) * spacing.Y;
285 return v2s32(pos_f.X, pos_f.Y);
288 v2s32 GUIFormSpecMenu::getRealCoordinateBasePos(const std::vector<std::string> &v_pos)
290 return v2s32((stof(v_pos[0]) + pos_offset.X) * imgsize.X,
291 (stof(v_pos[1]) + pos_offset.Y) * imgsize.Y);
294 v2s32 GUIFormSpecMenu::getRealCoordinateGeometry(const std::vector<std::string> &v_geom)
296 return v2s32(stof(v_geom[0]) * imgsize.X, stof(v_geom[1]) * imgsize.Y);
299 void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
301 std::vector<std::string> parts = split(element,',');
303 if (((parts.size() == 2) || parts.size() == 3) ||
304 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
306 if (parts[1].find(';') != std::string::npos)
307 parts[1] = parts[1].substr(0,parts[1].find(';'));
309 data->invsize.X = MYMAX(0, stof(parts[0]));
310 data->invsize.Y = MYMAX(0, stof(parts[1]));
314 if (parts.size() == 3) {
315 if (parts[2] == "true") {
316 lockSize(true,v2u32(800,600));
320 data->explicit_size = true;
323 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
326 void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
328 std::vector<std::string> parts = split(element, ',');
330 if (parts.size() >= 2) {
331 if (parts[1].find(';') != std::string::npos)
332 parts[1] = parts[1].substr(0, parts[1].find(';'));
334 container_stack.push(pos_offset);
335 pos_offset.X += stof(parts[0]);
336 pos_offset.Y += stof(parts[1]);
339 errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
342 void GUIFormSpecMenu::parseContainerEnd(parserData* data)
344 if (container_stack.empty()) {
345 errorstream<< "Invalid container end element, no matching container start element" << std::endl;
347 pos_offset = container_stack.top();
348 container_stack.pop();
352 void GUIFormSpecMenu::parseList(parserData *data, const std::string &element)
355 warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
359 std::vector<std::string> parts = split(element,';');
361 if (((parts.size() == 4) || (parts.size() == 5)) ||
362 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
364 std::string location = parts[0];
365 std::string listname = parts[1];
366 std::vector<std::string> v_pos = split(parts[2],',');
367 std::vector<std::string> v_geom = split(parts[3],',');
368 std::string startindex;
369 if (parts.size() == 5)
370 startindex = parts[4];
372 MY_CHECKPOS("list",2);
373 MY_CHECKGEOM("list",3);
375 InventoryLocation loc;
377 if (location == "context" || location == "current_name")
378 loc = m_current_inventory_location;
380 loc.deSerialize(location);
383 geom.X = stoi(v_geom[0]);
384 geom.Y = stoi(v_geom[1]);
387 if (!startindex.empty())
388 start_i = stoi(startindex);
390 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
391 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
395 // check for the existence of inventory and list
396 Inventory *inv = m_invmgr->getInventory(loc);
398 warningstream << "GUIFormSpecMenu::parseList(): "
399 << "The inventory location "
400 << "\"" << loc.dump() << "\" doesn't exist"
404 InventoryList *ilist = inv->getList(listname);
406 warningstream << "GUIFormSpecMenu::parseList(): "
407 << "The inventory list \"" << listname << "\" "
408 << "@ \"" << loc.dump() << "\" doesn't exist"
413 // trim geom if it is larger than the actual inventory size
414 s32 list_size = (s32)ilist->getSize();
415 if (list_size < geom.X * geom.Y + start_i) {
416 list_size -= MYMAX(start_i, 0);
417 geom.Y = list_size / geom.X;
418 geom.Y += list_size % geom.X > 0 ? 1 : 0;
423 if (!data->explicit_size)
424 warningstream << "invalid use of list without a size[] element" << std::endl;
430 258 + m_fields.size(),
434 v2f32 slot_spacing = data->real_coordinates ?
435 v2f32(imgsize.X * 1.25f, imgsize.Y * 1.25f) : spacing;
437 v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos)
438 : getElementBasePos(&v_pos);
440 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
441 pos.X + (geom.X - 1) * slot_spacing.X + imgsize.X,
442 pos.Y + (geom.Y - 1) * slot_spacing.Y + imgsize.Y);
444 GUIInventoryList *e = new GUIInventoryList(Environment, this, spec.fid,
445 rect, m_invmgr, loc, listname, geom, start_i, imgsize, slot_spacing,
446 this, data->inventorylist_options, m_font);
448 m_inventorylists.push_back(e);
449 m_fields.push_back(spec);
452 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
455 void GUIFormSpecMenu::parseListRing(parserData *data, const std::string &element)
458 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
462 std::vector<std::string> parts = split(element, ';');
464 if (parts.size() == 2) {
465 std::string location = parts[0];
466 std::string listname = parts[1];
468 InventoryLocation loc;
470 if (location == "context" || location == "current_name")
471 loc = m_current_inventory_location;
473 loc.deSerialize(location);
475 m_inventory_rings.emplace_back(loc, listname);
479 if (element.empty() && m_inventorylists.size() > 1) {
480 size_t siz = m_inventorylists.size();
481 // insert the last two inv list elements into the list ring
482 const GUIInventoryList *spa = m_inventorylists[siz - 2];
483 const GUIInventoryList *spb = m_inventorylists[siz - 1];
484 m_inventory_rings.emplace_back(spa->getInventoryloc(), spa->getListname());
485 m_inventory_rings.emplace_back(spb->getInventoryloc(), spb->getListname());
489 errorstream<< "Invalid list ring element(" << parts.size() << ", "
490 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
493 void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
495 std::vector<std::string> parts = split(element,';');
497 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
498 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
500 std::vector<std::string> v_pos = split(parts[0],',');
501 std::string name = parts[1];
502 std::string label = parts[2];
503 std::string selected;
505 if (parts.size() >= 4)
508 MY_CHECKPOS("checkbox",0);
510 bool fselected = false;
512 if (selected == "true")
515 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
516 const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str());
517 s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH);
518 s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2;
521 core::rect<s32> rect;
523 if (data->real_coordinates) {
524 pos = getRealCoordinateBasePos(v_pos);
526 rect = core::rect<s32>(
529 pos.X + label_size.Width + cb_size + 7,
533 pos = getElementBasePos(&v_pos);
534 rect = core::rect<s32>(
536 pos.Y + imgsize.Y / 2 - y_center,
537 pos.X + label_size.Width + cb_size + 7,
538 pos.Y + imgsize.Y / 2 + y_center
544 wlabel, //Needed for displaying text on MSVC
549 spec.ftype = f_CheckBox;
551 gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect, this,
552 spec.fid, spec.flabel.c_str());
554 auto style = getStyleForElement("checkbox", name);
555 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
557 if (spec.fname == data->focused_fieldname) {
558 Environment->setFocus(e);
562 m_checkboxes.emplace_back(spec, e);
563 m_fields.push_back(spec);
566 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
569 void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
571 std::vector<std::string> parts = split(element,';');
573 if (parts.size() >= 5) {
574 std::vector<std::string> v_pos = split(parts[0],',');
575 std::vector<std::string> v_geom = split(parts[1],',');
576 std::string name = parts[3];
577 std::string value = parts[4];
579 MY_CHECKPOS("scrollbar",0);
580 MY_CHECKGEOM("scrollbar",1);
585 if (data->real_coordinates) {
586 pos = getRealCoordinateBasePos(v_pos);
587 dim = getRealCoordinateGeometry(v_geom);
589 pos = getElementBasePos(&v_pos);
590 dim.X = stof(v_geom[0]) * spacing.X;
591 dim.Y = stof(v_geom[1]) * spacing.Y;
594 core::rect<s32> rect =
595 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
604 bool is_horizontal = true;
606 if (parts[2] == "vertical")
607 is_horizontal = false;
609 spec.ftype = f_ScrollBar;
611 GUIScrollBar *e = new GUIScrollBar(Environment, this, spec.fid, rect,
612 is_horizontal, true);
614 auto style = getStyleForElement("scrollbar", name);
615 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
616 e->setArrowsVisible(data->scrollbar_options.arrow_visiblity);
618 s32 max = data->scrollbar_options.max;
619 s32 min = data->scrollbar_options.min;
624 e->setPos(stoi(parts[4]));
626 e->setSmallStep(data->scrollbar_options.small_step);
627 e->setLargeStep(data->scrollbar_options.large_step);
629 s32 scrollbar_size = is_horizontal ? dim.X : dim.Y;
631 e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size);
633 m_scrollbars.emplace_back(spec,e);
634 m_fields.push_back(spec);
637 errorstream << "Invalid scrollbar element(" << parts.size() << "): '" << element
641 void GUIFormSpecMenu::parseScrollBarOptions(parserData* data, const std::string &element)
643 std::vector<std::string> parts = split(element, ';');
645 if (parts.size() == 0) {
646 warningstream << "Invalid scrollbaroptions element(" << parts.size() << "): '" <<
647 element << "'" << std::endl;
651 for (const std::string &i : parts) {
652 std::vector<std::string> options = split(i, '=');
654 if (options.size() != 2) {
655 warningstream << "Invalid scrollbaroptions option syntax: '" <<
656 element << "'" << std::endl;
657 continue; // Go to next option
660 if (options[0] == "max") {
661 data->scrollbar_options.max = stoi(options[1]);
663 } else if (options[0] == "min") {
664 data->scrollbar_options.min = stoi(options[1]);
666 } else if (options[0] == "smallstep") {
667 int value = stoi(options[1]);
668 data->scrollbar_options.small_step = value < 0 ? 10 : value;
670 } else if (options[0] == "largestep") {
671 int value = stoi(options[1]);
672 data->scrollbar_options.large_step = value < 0 ? 100 : value;
674 } else if (options[0] == "thumbsize") {
675 int value = stoi(options[1]);
676 data->scrollbar_options.thumb_size = value <= 0 ? 1 : value;
678 } else if (options[0] == "arrows") {
679 std::string value = trim(options[1]);
681 data->scrollbar_options.arrow_visiblity = GUIScrollBar::HIDE;
682 else if (value == "show")
683 data->scrollbar_options.arrow_visiblity = GUIScrollBar::SHOW;
684 else // Auto hide/show
685 data->scrollbar_options.arrow_visiblity = GUIScrollBar::DEFAULT;
689 warningstream << "Invalid scrollbaroptions option(" << options[0] <<
690 "): '" << element << "'" << std::endl;
694 void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
696 std::vector<std::string> parts = split(element,';');
698 if ((parts.size() == 3) ||
699 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
701 std::vector<std::string> v_pos = split(parts[0],',');
702 std::vector<std::string> v_geom = split(parts[1],',');
703 std::string name = unescape_string(parts[2]);
705 MY_CHECKPOS("image", 0);
706 MY_CHECKGEOM("image", 1);
711 if (data->real_coordinates) {
712 pos = getRealCoordinateBasePos(v_pos);
713 geom = getRealCoordinateGeometry(v_geom);
715 pos = getElementBasePos(&v_pos);
716 geom.X = stof(v_geom[0]) * (float)imgsize.X;
717 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
720 if (!data->explicit_size)
721 warningstream<<"invalid use of image without a size[] element"<<std::endl;
723 video::ITexture *texture = m_tsrc->getTexture(name);
725 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
726 << std::endl << "\t" << name << std::endl;
734 258 + m_fields.size(),
737 core::rect<s32> rect(pos, pos + geom);
738 gui::IGUIImage *e = Environment->addImage(rect, this, spec.fid, 0, true);
739 e->setImage(texture);
740 e->setScaleImage(true);
741 auto style = getStyleForElement("image", spec.fname);
742 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
743 m_fields.push_back(spec);
748 if (parts.size() == 2) {
749 std::vector<std::string> v_pos = split(parts[0],',');
750 std::string name = unescape_string(parts[1]);
752 MY_CHECKPOS("image", 0);
754 v2s32 pos = getElementBasePos(&v_pos);
756 if (!data->explicit_size)
757 warningstream<<"invalid use of image without a size[] element"<<std::endl;
759 video::ITexture *texture = m_tsrc->getTexture(name);
761 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
762 << std::endl << "\t" << name << std::endl;
770 258 + m_fields.size()
772 gui::IGUIImage *e = Environment->addImage(texture, pos, true, this,
774 auto style = getStyleForElement("image", spec.fname);
775 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
776 m_fields.push_back(spec);
780 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
783 void GUIFormSpecMenu::parseAnimatedImage(parserData *data, const std::string &element)
785 std::vector<std::string> parts = split(element, ';');
787 if (parts.size() != 3 &&
788 !(parts.size() > 3 && m_formspec_version > FORMSPEC_API_VERSION)) {
789 errorstream << "Invalid animated image element(" << parts.size()
790 << "): '" << element << "'" << std::endl;
794 std::vector<std::string> v_pos = split(parts[0], ',');
795 std::vector<std::string> v_geom = split(parts[1], ',');
796 std::string name = unescape_string(parts[2]);
798 MY_CHECKPOS("animated_image", 0);
799 MY_CHECKGEOM("animated_image", 1);
804 if (data->real_coordinates) {
805 pos = getRealCoordinateBasePos(v_pos);
806 geom = getRealCoordinateGeometry(v_geom);
808 pos = getElementBasePos(&v_pos);
809 geom.X = stof(v_geom[0]) * (float)imgsize.X;
810 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
813 if (!data->explicit_size)
814 warningstream << "invalid use of animated_image without a size[] element" << std::endl;
820 258 + m_fields.size()
823 core::rect<s32> rect = core::rect<s32>(pos, pos + geom);
825 gui::IGUIElement *e = new GUIAnimatedImage(Environment, this, spec.fid,
828 auto style = getStyleForElement("animated_image", spec.fname);
829 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
832 m_fields.push_back(spec);
835 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
837 std::vector<std::string> parts = split(element,';');
839 if ((parts.size() == 3) ||
840 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
842 std::vector<std::string> v_pos = split(parts[0],',');
843 std::vector<std::string> v_geom = split(parts[1],',');
844 std::string name = parts[2];
846 MY_CHECKPOS("itemimage",0);
847 MY_CHECKGEOM("itemimage",1);
852 if (data->real_coordinates) {
853 pos = getRealCoordinateBasePos(v_pos);
854 geom = getRealCoordinateGeometry(v_geom);
856 pos = getElementBasePos(&v_pos);
857 geom.X = stof(v_geom[0]) * (float)imgsize.X;
858 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
861 if(!data->explicit_size)
862 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
868 258 + m_fields.size(),
871 spec.ftype = f_ItemImage;
873 GUIItemImage *e = new GUIItemImage(Environment, this, spec.fid,
874 core::rect<s32>(pos, pos + geom), name, m_font, m_client);
875 auto style = getStyleForElement("item_image", spec.fname);
876 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
879 m_fields.push_back(spec);
882 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
885 void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
886 const std::string &type)
888 std::vector<std::string> parts = split(element,';');
890 if ((parts.size() == 4) ||
891 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
893 std::vector<std::string> v_pos = split(parts[0],',');
894 std::vector<std::string> v_geom = split(parts[1],',');
895 std::string name = parts[2];
896 std::string label = parts[3];
898 MY_CHECKPOS("button",0);
899 MY_CHECKGEOM("button",1);
903 core::rect<s32> rect;
905 if (data->real_coordinates) {
906 pos = getRealCoordinateBasePos(v_pos);
907 geom = getRealCoordinateGeometry(v_geom);
908 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
911 pos = getElementBasePos(&v_pos);
912 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
913 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
915 rect = core::rect<s32>(pos.X, pos.Y - m_btn_height,
916 pos.X + geom.X, pos.Y + m_btn_height);
919 if(!data->explicit_size)
920 warningstream<<"invalid use of button without a size[] element"<<std::endl;
922 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
928 258 + m_fields.size()
930 spec.ftype = f_Button;
931 if(type == "button_exit")
934 GUIButton *e = GUIButton::addButton(Environment, rect, this, spec.fid, spec.flabel.c_str());
936 auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
937 e->setFromStyle(style, m_tsrc);
939 if (spec.fname == data->focused_fieldname) {
940 Environment->setFocus(e);
943 m_fields.push_back(spec);
946 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
949 void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
951 std::vector<std::string> parts = split(element,';');
953 if ((parts.size() >= 3 && parts.size() <= 5) ||
954 (parts.size() > 5 && m_formspec_version > FORMSPEC_API_VERSION)) {
955 std::vector<std::string> v_pos = split(parts[0],',');
956 std::vector<std::string> v_geom = split(parts[1],',');
957 std::string name = unescape_string(parts[2]);
959 MY_CHECKPOS("background",0);
960 MY_CHECKGEOM("background",1);
965 if (data->real_coordinates) {
966 pos = getRealCoordinateBasePos(v_pos);
967 geom = getRealCoordinateGeometry(v_geom);
969 pos = getElementBasePos(&v_pos);
970 pos.X -= (spacing.X - (float)imgsize.X) / 2;
971 pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
973 geom.X = stof(v_geom[0]) * spacing.X;
974 geom.Y = stof(v_geom[1]) * spacing.Y;
978 if (parts.size() >= 4 && is_yes(parts[3])) {
979 if (data->real_coordinates) {
980 pos = getRealCoordinateBasePos(v_pos) * -1;
983 pos.X = stoi(v_pos[0]); //acts as offset
984 pos.Y = stoi(v_pos[1]);
989 core::rect<s32> middle;
990 if (parts.size() >= 5) {
991 std::vector<std::string> v_middle = split(parts[4], ',');
992 if (v_middle.size() == 1) {
993 s32 x = stoi(v_middle[0]);
994 middle.UpperLeftCorner = core::vector2di(x, x);
995 middle.LowerRightCorner = core::vector2di(-x, -x);
996 } else if (v_middle.size() == 2) {
997 s32 x = stoi(v_middle[0]);
998 s32 y = stoi(v_middle[1]);
999 middle.UpperLeftCorner = core::vector2di(x, y);
1000 middle.LowerRightCorner = core::vector2di(-x, -y);
1001 // `-x` is interpreted as `w - x`
1002 } else if (v_middle.size() == 4) {
1003 middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1]));
1004 middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3]));
1006 warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl;
1010 if (!data->explicit_size && !clip)
1011 warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
1017 258 + m_fields.size()
1020 core::rect<s32> rect;
1022 // no auto_clip => position like normal image
1023 rect = core::rect<s32>(pos, pos + geom);
1025 // it will be auto-clipped when drawing
1026 rect = core::rect<s32>(-pos, pos);
1029 GUIBackgroundImage *e = new GUIBackgroundImage(Environment, this, spec.fid,
1030 rect, name, middle, m_tsrc, clip);
1032 FATAL_ERROR_IF(!e, "Failed to create background formspec element");
1034 e->setNotClipped(true);
1036 e->setVisible(false); // the element is drawn manually before all others
1038 m_backgrounds.push_back(e);
1039 m_fields.push_back(spec);
1042 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
1045 void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
1047 std::vector<std::string> parts = split(element,';');
1049 data->table_options.clear();
1050 for (const std::string &part : parts) {
1051 // Parse table option
1052 std::string opt = unescape_string(part);
1053 data->table_options.push_back(GUITable::splitOption(opt));
1057 void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
1059 std::vector<std::string> parts = split(element,';');
1061 data->table_columns.clear();
1062 for (const std::string &part : parts) {
1063 std::vector<std::string> col_parts = split(part,',');
1064 GUITable::TableColumn column;
1065 // Parse column type
1066 if (!col_parts.empty())
1067 column.type = col_parts[0];
1068 // Parse column options
1069 for (size_t j = 1; j < col_parts.size(); ++j) {
1070 std::string opt = unescape_string(col_parts[j]);
1071 column.options.push_back(GUITable::splitOption(opt));
1073 data->table_columns.push_back(column);
1077 void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
1079 std::vector<std::string> parts = split(element,';');
1081 if (((parts.size() == 4) || (parts.size() == 5)) ||
1082 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1084 std::vector<std::string> v_pos = split(parts[0],',');
1085 std::vector<std::string> v_geom = split(parts[1],',');
1086 std::string name = parts[2];
1087 std::vector<std::string> items = split(parts[3],',');
1088 std::string str_initial_selection;
1089 std::string str_transparent = "false";
1091 if (parts.size() >= 5)
1092 str_initial_selection = parts[4];
1094 MY_CHECKPOS("table",0);
1095 MY_CHECKGEOM("table",1);
1100 if (data->real_coordinates) {
1101 pos = getRealCoordinateBasePos(v_pos);
1102 geom = getRealCoordinateGeometry(v_geom);
1104 pos = getElementBasePos(&v_pos);
1105 geom.X = stof(v_geom[0]) * spacing.X;
1106 geom.Y = stof(v_geom[1]) * spacing.Y;
1109 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1115 258 + m_fields.size()
1118 spec.ftype = f_Table;
1120 for (std::string &item : items) {
1121 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1124 //now really show table
1125 GUITable *e = new GUITable(Environment, this, spec.fid, rect, m_tsrc);
1127 if (spec.fname == data->focused_fieldname) {
1128 Environment->setFocus(e);
1131 e->setTable(data->table_options, data->table_columns, items);
1133 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1134 e->setDynamicData(data->table_dyndata[name]);
1137 if (!str_initial_selection.empty() && str_initial_selection != "0")
1138 e->setSelected(stoi(str_initial_selection));
1140 auto style = getStyleForElement("table", name);
1141 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1143 m_tables.emplace_back(spec, e);
1144 m_fields.push_back(spec);
1147 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
1150 void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
1152 std::vector<std::string> parts = split(element,';');
1154 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
1155 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1157 std::vector<std::string> v_pos = split(parts[0],',');
1158 std::vector<std::string> v_geom = split(parts[1],',');
1159 std::string name = parts[2];
1160 std::vector<std::string> items = split(parts[3],',');
1161 std::string str_initial_selection;
1162 std::string str_transparent = "false";
1164 if (parts.size() >= 5)
1165 str_initial_selection = parts[4];
1167 if (parts.size() >= 6)
1168 str_transparent = parts[5];
1170 MY_CHECKPOS("textlist",0);
1171 MY_CHECKGEOM("textlist",1);
1176 if (data->real_coordinates) {
1177 pos = getRealCoordinateBasePos(v_pos);
1178 geom = getRealCoordinateGeometry(v_geom);
1180 pos = getElementBasePos(&v_pos);
1181 geom.X = stof(v_geom[0]) * spacing.X;
1182 geom.Y = stof(v_geom[1]) * spacing.Y;
1185 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1191 258 + m_fields.size()
1194 spec.ftype = f_Table;
1196 for (std::string &item : items) {
1197 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1200 //now really show list
1201 GUITable *e = new GUITable(Environment, this, spec.fid, rect, m_tsrc);
1203 if (spec.fname == data->focused_fieldname) {
1204 Environment->setFocus(e);
1207 e->setTextList(items, is_yes(str_transparent));
1209 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1210 e->setDynamicData(data->table_dyndata[name]);
1213 if (!str_initial_selection.empty() && str_initial_selection != "0")
1214 e->setSelected(stoi(str_initial_selection));
1216 auto style = getStyleForElement("textlist", name);
1217 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1219 m_tables.emplace_back(spec, e);
1220 m_fields.push_back(spec);
1223 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
1227 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
1229 std::vector<std::string> parts = split(element,';');
1231 if ((parts.size() == 5) ||
1232 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1234 std::vector<std::string> v_pos = split(parts[0],',');
1235 std::string name = parts[2];
1236 std::vector<std::string> items = split(parts[3],',');
1237 std::string str_initial_selection;
1238 str_initial_selection = parts[4];
1240 MY_CHECKPOS("dropdown",0);
1244 core::rect<s32> rect;
1246 if (data->real_coordinates) {
1247 std::vector<std::string> v_geom = split(parts[1],',');
1249 if (v_geom.size() == 1)
1250 v_geom.emplace_back("1");
1252 MY_CHECKGEOM("dropdown",1);
1254 pos = getRealCoordinateBasePos(v_pos);
1255 geom = getRealCoordinateGeometry(v_geom);
1256 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1258 pos = getElementBasePos(&v_pos);
1260 s32 width = stof(parts[1]) * spacing.Y;
1262 rect = core::rect<s32>(pos.X, pos.Y,
1263 pos.X + width, pos.Y + (m_btn_height * 2));
1270 258 + m_fields.size()
1273 spec.ftype = f_DropDown;
1276 //now really show list
1277 gui::IGUIComboBox *e = Environment->addComboBox(rect, this, spec.fid);
1279 if (spec.fname == data->focused_fieldname) {
1280 Environment->setFocus(e);
1283 for (const std::string &item : items) {
1284 e->addItem(unescape_translate(unescape_string(
1285 utf8_to_wide(item))).c_str());
1288 if (!str_initial_selection.empty())
1289 e->setSelected(stoi(str_initial_selection)-1);
1291 auto style = getStyleForElement("dropdown", name);
1292 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1294 m_fields.push_back(spec);
1296 m_dropdowns.emplace_back(spec, std::vector<std::string>());
1297 std::vector<std::string> &values = m_dropdowns.back().second;
1298 for (const std::string &item : items) {
1299 values.push_back(unescape_string(item));
1304 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
1305 << element << "'" << std::endl;
1308 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
1310 std::vector<std::string> parts = split(element,';');
1311 if (parts.size() == 2 ||
1312 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
1313 field_close_on_enter[parts[0]] = is_yes(parts[1]);
1317 void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
1319 std::vector<std::string> parts = split(element,';');
1321 if ((parts.size() == 4) || (parts.size() == 5) ||
1322 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1324 std::vector<std::string> v_pos = split(parts[0],',');
1325 std::vector<std::string> v_geom = split(parts[1],',');
1326 std::string name = parts[2];
1327 std::string label = parts[3];
1329 MY_CHECKPOS("pwdfield",0);
1330 MY_CHECKGEOM("pwdfield",1);
1335 if (data->real_coordinates) {
1336 pos = getRealCoordinateBasePos(v_pos);
1337 geom = getRealCoordinateGeometry(v_geom);
1339 pos = getElementBasePos(&v_pos);
1342 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1344 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1345 pos.Y -= m_btn_height;
1346 geom.Y = m_btn_height*2;
1349 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1351 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1357 258 + m_fields.size(),
1363 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
1365 if (spec.fname == data->focused_fieldname) {
1366 Environment->setFocus(e);
1369 if (label.length() >= 1) {
1370 int font_height = g_fontengine->getTextHeight();
1371 rect.UpperLeftCorner.Y -= font_height;
1372 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1373 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1377 e->setPasswordBox(true,L'*');
1379 auto style = getStyleForElement("pwdfield", name, "field");
1380 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1381 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1382 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1385 evt.EventType = EET_KEY_INPUT_EVENT;
1386 evt.KeyInput.Key = KEY_END;
1387 evt.KeyInput.Char = 0;
1388 evt.KeyInput.Control = false;
1389 evt.KeyInput.Shift = false;
1390 evt.KeyInput.PressedDown = true;
1393 if (parts.size() >= 5) {
1394 // TODO: remove after 2016-11-03
1395 warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
1396 " instead of the 5th param" << std::endl;
1397 field_close_on_enter[name] = is_yes(parts[4]);
1400 m_fields.push_back(spec);
1403 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
1406 void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
1407 core::rect<s32> &rect, bool is_multiline)
1409 bool is_editable = !spec.fname.empty();
1410 if (!is_editable && !is_multiline) {
1411 // spec field id to 0, this stops submit searching for a value that isn't there
1412 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1419 } else if (is_multiline &&
1420 spec.fdefault.empty() && !spec.flabel.empty()) {
1421 // Multiline textareas: swap default and label for backwards compat
1422 spec.flabel.swap(spec.fdefault);
1425 gui::IGUIEditBox *e = nullptr;
1426 static constexpr bool use_intl_edit_box = USE_FREETYPE &&
1427 IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9;
1429 if (use_intl_edit_box && g_settings->getBool("freetype")) {
1430 e = new gui::intlGUIEditBox(spec.fdefault.c_str(), true, Environment,
1431 this, spec.fid, rect, is_editable, is_multiline);
1434 e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true,
1435 Environment, this, spec.fid, rect, is_editable, true);
1436 } else if (is_editable) {
1437 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this,
1443 auto style = getStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
1446 if (is_editable && spec.fname == data->focused_fieldname)
1447 Environment->setFocus(e);
1450 e->setMultiLine(true);
1451 e->setWordWrap(true);
1452 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1455 evt.EventType = EET_KEY_INPUT_EVENT;
1456 evt.KeyInput.Key = KEY_END;
1457 evt.KeyInput.Char = 0;
1458 evt.KeyInput.Control = 0;
1459 evt.KeyInput.Shift = 0;
1460 evt.KeyInput.PressedDown = true;
1464 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1465 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1466 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1467 if (style.get(StyleSpec::BGCOLOR, "") == "transparent") {
1468 e->setDrawBackground(false);
1474 if (!spec.flabel.empty()) {
1475 int font_height = g_fontengine->getTextHeight();
1476 rect.UpperLeftCorner.Y -= font_height;
1477 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1478 IGUIElement *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
1479 rect, false, true, this, 0);
1482 t->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1486 void GUIFormSpecMenu::parseSimpleField(parserData *data,
1487 std::vector<std::string> &parts)
1489 std::string name = parts[0];
1490 std::string label = parts[1];
1491 std::string default_val = parts[2];
1493 core::rect<s32> rect;
1495 if (data->explicit_size)
1496 warningstream << "invalid use of unpositioned \"field\" in inventory" << std::endl;
1498 v2s32 pos = getElementBasePos(nullptr);
1499 pos.Y = (data->simple_field_count + 2) * 60;
1500 v2s32 size = DesiredRect.getSize();
1502 rect = core::rect<s32>(
1503 size.X / 2 - 150, pos.Y,
1504 size.X / 2 - 150 + 300, pos.Y + m_btn_height * 2
1509 default_val = m_form_src->resolveText(default_val);
1512 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1517 utf8_to_wide(unescape_string(default_val)),
1518 258 + m_fields.size(),
1523 createTextField(data, spec, rect, false);
1525 m_fields.push_back(spec);
1527 data->simple_field_count++;
1530 void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
1531 const std::string &type)
1533 std::vector<std::string> v_pos = split(parts[0],',');
1534 std::vector<std::string> v_geom = split(parts[1],',');
1535 std::string name = parts[2];
1536 std::string label = parts[3];
1537 std::string default_val = parts[4];
1539 MY_CHECKPOS(type,0);
1540 MY_CHECKGEOM(type,1);
1545 if (data->real_coordinates) {
1546 pos = getRealCoordinateBasePos(v_pos);
1547 geom = getRealCoordinateGeometry(v_geom);
1549 pos = getElementBasePos(&v_pos);
1552 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1554 if (type == "textarea")
1556 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1557 pos.Y += m_btn_height;
1561 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1562 pos.Y -= m_btn_height;
1563 geom.Y = m_btn_height*2;
1567 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1569 if(!data->explicit_size)
1570 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1573 default_val = m_form_src->resolveText(default_val);
1576 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1581 utf8_to_wide(unescape_string(default_val)),
1582 258 + m_fields.size(),
1587 createTextField(data, spec, rect, type == "textarea");
1589 if (parts.size() >= 6) {
1590 // TODO: remove after 2016-11-03
1591 warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
1592 " instead of the 6th param" << std::endl;
1593 field_close_on_enter[name] = is_yes(parts[5]);
1596 m_fields.push_back(spec);
1599 void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
1600 const std::string &type)
1602 std::vector<std::string> parts = split(element,';');
1604 if (parts.size() == 3 || parts.size() == 4) {
1605 parseSimpleField(data,parts);
1609 if ((parts.size() == 5) || (parts.size() == 6) ||
1610 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1612 parseTextArea(data,parts,type);
1615 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1618 void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &element)
1620 std::vector<std::string> parts = split(element, ';');
1622 if (parts.size() != 4 && m_formspec_version < FORMSPEC_API_VERSION) {
1623 errorstream << "Invalid text element(" << parts.size() << "): '" << element << "'" << std::endl;
1627 std::vector<std::string> v_pos = split(parts[0], ',');
1628 std::vector<std::string> v_geom = split(parts[1], ',');
1629 std::string name = parts[2];
1630 std::string text = parts[3];
1632 MY_CHECKPOS("hypertext", 0);
1633 MY_CHECKGEOM("hypertext", 1);
1638 if (data->real_coordinates) {
1639 pos = getRealCoordinateBasePos(v_pos);
1640 geom = getRealCoordinateGeometry(v_geom);
1642 pos = getElementBasePos(&v_pos);
1645 pos.X += stof(v_pos[0]) * spacing.X;
1646 pos.Y += stof(v_pos[1]) * spacing.Y + (m_btn_height * 2);
1648 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1649 geom.Y = (stof(v_geom[1]) * imgsize.Y) - (spacing.Y - imgsize.Y);
1652 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X + geom.X, pos.Y + geom.Y);
1655 text = m_form_src->resolveText(text);
1659 utf8_to_wide(unescape_string(text)),
1661 258 + m_fields.size()
1664 spec.ftype = f_Unknown;
1666 spec.flabel.c_str(), Environment, this, spec.fid, rect, m_client, m_tsrc);
1668 m_fields.push_back(spec);
1671 void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
1673 std::vector<std::string> parts = split(element,';');
1675 if ((parts.size() == 2) ||
1676 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1678 std::vector<std::string> v_pos = split(parts[0],',');
1679 std::string text = parts[1];
1681 MY_CHECKPOS("label",0);
1683 if(!data->explicit_size)
1684 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1686 std::vector<std::string> lines = split(text, '\n');
1688 for (unsigned int i = 0; i != lines.size(); i++) {
1689 std::wstring wlabel_colors = translate_string(
1690 utf8_to_wide(unescape_string(lines[i])));
1691 // Without color escapes to get the font dimensions
1692 std::wstring wlabel_plain = unescape_enriched(wlabel_colors);
1694 core::rect<s32> rect;
1696 if (data->real_coordinates) {
1697 // Lines are spaced at the distance of 1/2 imgsize.
1698 // This alows lines that line up with the new elements
1699 // easily without sacrificing good line distance. If
1700 // it was one whole imgsize, it would have too much
1702 v2s32 pos = getRealCoordinateBasePos(v_pos);
1704 // Labels are positioned by their center, not their top.
1705 pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2);
1707 rect = core::rect<s32>(
1709 pos.X + m_font->getDimension(wlabel_plain.c_str()).Width,
1713 // Lines are spaced at the nominal distance of
1714 // 2/5 inventory slot, even if the font doesn't
1715 // quite match that. This provides consistent
1716 // form layout, at the expense of sometimes
1717 // having sub-optimal spacing for the font.
1718 // We multiply by 2 and then divide by 5, rather
1719 // than multiply by 0.4, to get exact results
1720 // in the integer cases: 0.4 is not exactly
1721 // representable in binary floating point.
1723 v2s32 pos = getElementBasePos(nullptr);
1724 pos.X += stof(v_pos[0]) * spacing.X;
1725 pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
1727 pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0;
1729 rect = core::rect<s32>(
1730 pos.X, pos.Y - m_btn_height,
1731 pos.X + m_font->getDimension(wlabel_plain.c_str()).Width,
1732 pos.Y + m_btn_height);
1739 258 + m_fields.size(),
1742 gui::IGUIStaticText *e = gui::StaticText::add(Environment,
1743 spec.flabel.c_str(), rect, false, false, this, spec.fid);
1744 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
1746 auto style = getStyleForElement("label", spec.fname);
1747 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1748 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1750 m_fields.push_back(spec);
1755 errorstream << "Invalid label element(" << parts.size() << "): '" << element
1756 << "'" << std::endl;
1759 void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
1761 std::vector<std::string> parts = split(element,';');
1763 if ((parts.size() == 2) ||
1764 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1766 std::vector<std::string> v_pos = split(parts[0],',');
1767 std::wstring text = unescape_translate(
1768 unescape_string(utf8_to_wide(parts[1])));
1770 MY_CHECKPOS("vertlabel",1);
1773 core::rect<s32> rect;
1775 if (data->real_coordinates) {
1776 pos = getRealCoordinateBasePos(v_pos);
1778 // Vertlabels are positioned by center, not left.
1779 pos.X -= imgsize.X / 2;
1781 // We use text.length + 1 because without it, the rect
1782 // isn't quite tall enough and cuts off the text.
1783 rect = core::rect<s32>(pos.X, pos.Y,
1785 pos.Y + font_line_height(m_font) *
1786 (text.length() + 1));
1789 pos = getElementBasePos(&v_pos);
1791 // As above, the length must be one longer. The width of
1792 // the rect (15 pixels) seems rather arbitrary, but
1793 // changing it might break something.
1794 rect = core::rect<s32>(
1795 pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
1797 font_line_height(m_font) *
1798 (text.length() + 1) +
1799 ((imgsize.Y/2) - m_btn_height));
1802 if(!data->explicit_size)
1803 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1807 for (wchar_t i : text) {
1816 258 + m_fields.size()
1818 gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
1819 rect, false, false, this, spec.fid);
1820 e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1822 auto style = getStyleForElement("vertlabel", spec.fname, "label");
1823 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1824 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1826 m_fields.push_back(spec);
1829 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1832 void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
1833 const std::string &type)
1835 std::vector<std::string> parts = split(element,';');
1837 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1838 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1840 std::vector<std::string> v_pos = split(parts[0],',');
1841 std::vector<std::string> v_geom = split(parts[1],',');
1842 std::string image_name = parts[2];
1843 std::string name = parts[3];
1844 std::string label = parts[4];
1846 MY_CHECKPOS("imagebutton",0);
1847 MY_CHECKGEOM("imagebutton",1);
1849 bool noclip = false;
1850 bool drawborder = true;
1851 std::string pressed_image_name;
1853 if (parts.size() >= 7) {
1854 if (parts[5] == "true")
1856 if (parts[6] == "false")
1860 if (parts.size() >= 8) {
1861 pressed_image_name = parts[7];
1867 if (data->real_coordinates) {
1868 pos = getRealCoordinateBasePos(v_pos);
1869 geom = getRealCoordinateGeometry(v_geom);
1871 pos = getElementBasePos(&v_pos);
1872 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1873 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1876 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1879 if (!data->explicit_size)
1880 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1882 image_name = unescape_string(image_name);
1883 pressed_image_name = unescape_string(pressed_image_name);
1885 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1890 utf8_to_wide(image_name),
1891 258 + m_fields.size()
1893 spec.ftype = f_Button;
1894 if (type == "image_button_exit")
1895 spec.is_exit = true;
1897 GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, this, spec.fid, spec.flabel.c_str());
1899 if (spec.fname == data->focused_fieldname) {
1900 Environment->setFocus(e);
1903 auto style = getStyleForElement("image_button", spec.fname);
1904 e->setFromStyle(style, m_tsrc);
1906 // We explicitly handle these arguments *after* the style properties in
1907 // order to override them if they are provided
1908 if (!image_name.empty())
1910 video::ITexture *texture = m_tsrc->getTexture(image_name);
1911 e->setForegroundImage(guiScalingImageButton(
1912 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1914 if (!pressed_image_name.empty()) {
1915 video::ITexture *pressed_texture = m_tsrc->getTexture(pressed_image_name);
1916 e->setPressedForegroundImage(guiScalingImageButton(
1917 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1919 e->setScaleImage(true);
1921 if (parts.size() >= 7) {
1922 e->setNotClipped(noclip);
1923 e->setDrawBorder(drawborder);
1926 m_fields.push_back(spec);
1930 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1933 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
1935 std::vector<std::string> parts = split(element, ';');
1937 if (((parts.size() == 4) || (parts.size() == 6)) || (parts.size() == 7 &&
1938 data->real_coordinates) || ((parts.size() > 6) &&
1939 (m_formspec_version > FORMSPEC_API_VERSION)))
1941 std::vector<std::string> v_pos = split(parts[0],',');
1943 // If we're using real coordinates, add an extra field for height.
1944 // Width is not here because tabs are the width of the text, and
1945 // there's no reason to change that.
1947 std::vector<std::string> v_geom = {"1", "0.75"}; // Dummy width and default height
1948 bool auto_width = true;
1949 if (parts.size() == 7) {
1952 v_geom = split(parts[1], ',');
1953 if (v_geom.size() == 1)
1954 v_geom.insert(v_geom.begin(), "1"); // Dummy value
1959 std::string name = parts[i+1];
1960 std::vector<std::string> buttons = split(parts[i+2], ',');
1961 std::string str_index = parts[i+3];
1962 bool show_background = true;
1963 bool show_border = true;
1964 int tab_index = stoi(str_index) - 1;
1966 MY_CHECKPOS("tabheader", 0);
1968 if (parts.size() == 6 + i) {
1969 if (parts[4+i] == "true")
1970 show_background = false;
1971 if (parts[5+i] == "false")
1972 show_border = false;
1979 258 + m_fields.size()
1982 spec.ftype = f_TabHeader;
1987 if (data->real_coordinates) {
1988 pos = getRealCoordinateBasePos(v_pos);
1990 geom = getRealCoordinateGeometry(v_geom);
1991 pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
1993 geom.X = DesiredRect.getWidth(); // Set automatic width
1995 MY_CHECKGEOM("tabheader", 1);
1997 v2f32 pos_f = pos_offset * spacing;
1998 pos_f.X += stof(v_pos[0]) * spacing.X;
1999 pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
2000 pos = v2s32(pos_f.X, pos_f.Y);
2002 geom.Y = m_btn_height * 2;
2003 geom.X = DesiredRect.getWidth();
2006 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
2009 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
2010 show_background, show_border, spec.fid);
2011 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
2012 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
2013 e->setTabHeight(geom.Y);
2015 if (spec.fname == data->focused_fieldname) {
2016 Environment->setFocus(e);
2019 auto style = getStyleForElement("tabheader", name);
2020 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
2022 for (const std::string &button : buttons) {
2023 auto tab = e->addTab(unescape_translate(unescape_string(
2024 utf8_to_wide(button))).c_str(), -1);
2025 if (style.isNotDefault(StyleSpec::BGCOLOR))
2026 tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
2028 tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
2031 if ((tab_index >= 0) &&
2032 (buttons.size() < INT_MAX) &&
2033 (tab_index < (int) buttons.size()))
2034 e->setActiveTab(tab_index);
2036 m_fields.push_back(spec);
2039 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
2040 << element << "'" << std::endl;
2043 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
2046 if (m_client == 0) {
2047 warningstream << "invalid use of item_image_button with m_client==0"
2052 std::vector<std::string> parts = split(element,';');
2054 if ((parts.size() == 5) ||
2055 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2057 std::vector<std::string> v_pos = split(parts[0],',');
2058 std::vector<std::string> v_geom = split(parts[1],',');
2059 std::string item_name = parts[2];
2060 std::string name = parts[3];
2061 std::string label = parts[4];
2063 label = unescape_string(label);
2064 item_name = unescape_string(item_name);
2066 MY_CHECKPOS("itemimagebutton",0);
2067 MY_CHECKGEOM("itemimagebutton",1);
2072 if (data->real_coordinates) {
2073 pos = getRealCoordinateBasePos(v_pos);
2074 geom = getRealCoordinateGeometry(v_geom);
2076 pos = getElementBasePos(&v_pos);
2077 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
2078 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
2081 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
2083 if(!data->explicit_size)
2084 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
2086 IItemDefManager *idef = m_client->idef();
2088 item.deSerialize(item_name, idef);
2091 TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
2092 m_default_tooltip_bgcolor,
2093 m_default_tooltip_color);
2095 // the spec for the button
2098 utf8_to_wide(label),
2099 utf8_to_wide(item_name),
2100 258 + m_fields.size(),
2104 GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment, rect, this, spec_btn.fid, spec_btn.flabel.c_str(), item_name, m_client);
2106 auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
2107 e_btn->setFromStyle(style, m_tsrc);
2109 if (spec_btn.fname == data->focused_fieldname) {
2110 Environment->setFocus(e_btn);
2113 spec_btn.ftype = f_Button;
2114 rect += data->basepos-padding;
2115 spec_btn.rect = rect;
2116 m_fields.push_back(spec_btn);
2119 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
2122 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
2124 std::vector<std::string> parts = split(element,';');
2126 if ((parts.size() == 3) ||
2127 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
2129 std::vector<std::string> v_pos = split(parts[0],',');
2130 std::vector<std::string> v_geom = split(parts[1],',');
2132 MY_CHECKPOS("box",0);
2133 MY_CHECKGEOM("box",1);
2138 if (data->real_coordinates) {
2139 pos = getRealCoordinateBasePos(v_pos);
2140 geom = getRealCoordinateGeometry(v_geom);
2142 pos = getElementBasePos(&v_pos);
2143 geom.X = stof(v_geom[0]) * spacing.X;
2144 geom.Y = stof(v_geom[1]) * spacing.Y;
2147 video::SColor tmp_color;
2149 if (parseColorString(parts[2], tmp_color, false, 0x8C)) {
2154 258 + m_fields.size(),
2159 core::rect<s32> rect(pos, pos + geom);
2161 GUIBox *e = new GUIBox(Environment, this, spec.fid, rect, tmp_color);
2163 auto style = getStyleForElement("box", spec.fname);
2164 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
2168 m_fields.push_back(spec);
2171 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
2175 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
2178 void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
2180 std::vector<std::string> parts = split(element,';');
2181 const u32 parameter_count = parts.size();
2183 if ((parameter_count > 2 && m_formspec_version < 3) ||
2184 (parameter_count > 3 && m_formspec_version <= FORMSPEC_API_VERSION)) {
2185 errorstream << "Invalid bgcolor element(" << parameter_count << "): '"
2186 << element << "'" << std::endl;
2191 if (parameter_count >= 1 && parts[0] != "")
2192 parseColorString(parts[0], m_bgcolor, false);
2195 if (parameter_count >= 2) {
2196 if (parts[1] == "both") {
2197 m_bgnonfullscreen = true;
2198 m_bgfullscreen = true;
2199 } else if (parts[1] == "neither") {
2200 m_bgnonfullscreen = false;
2201 m_bgfullscreen = false;
2202 } else if (parts[1] != "" || m_formspec_version < 3) {
2203 m_bgfullscreen = is_yes(parts[1]);
2204 m_bgnonfullscreen = !m_bgfullscreen;
2209 if (parameter_count >= 3 && parts[2] != "")
2210 parseColorString(parts[2], m_fullscreen_bgcolor, false);
2213 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
2215 std::vector<std::string> parts = split(element,';');
2217 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
2218 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2220 parseColorString(parts[0], data->inventorylist_options.slotbg_n, false);
2221 parseColorString(parts[1], data->inventorylist_options.slotbg_h, false);
2223 if (parts.size() >= 3) {
2224 if (parseColorString(parts[2], data->inventorylist_options.slotbordercolor,
2226 data->inventorylist_options.slotborder = true;
2229 if (parts.size() == 5) {
2230 video::SColor tmp_color;
2232 if (parseColorString(parts[3], tmp_color, false))
2233 m_default_tooltip_bgcolor = tmp_color;
2234 if (parseColorString(parts[4], tmp_color, false))
2235 m_default_tooltip_color = tmp_color;
2238 // update all already parsed inventorylists
2239 for (GUIInventoryList *e : m_inventorylists) {
2240 e->setSlotBGColors(data->inventorylist_options.slotbg_n,
2241 data->inventorylist_options.slotbg_h);
2242 e->setSlotBorders(data->inventorylist_options.slotborder,
2243 data->inventorylist_options.slotbordercolor);
2247 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
2250 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
2252 std::vector<std::string> parts = split(element,';');
2253 if (parts.size() < 2) {
2254 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2255 << element << "'" << std::endl;
2259 // Get mode and check size
2260 bool rect_mode = parts[0].find(',') != std::string::npos;
2261 size_t base_size = rect_mode ? 3 : 2;
2262 if (parts.size() != base_size && parts.size() != base_size + 2) {
2263 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2264 << element << "'" << std::endl;
2269 video::SColor bgcolor = m_default_tooltip_bgcolor;
2270 video::SColor color = m_default_tooltip_color;
2271 if (parts.size() == base_size + 2 &&
2272 (!parseColorString(parts[base_size], bgcolor, false) ||
2273 !parseColorString(parts[base_size + 1], color, false))) {
2274 errorstream << "Invalid color in tooltip element(" << parts.size()
2275 << "): '" << element << "'" << std::endl;
2279 // Make tooltip spec
2280 std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
2281 TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
2285 std::vector<std::string> v_pos = split(parts[0], ',');
2286 std::vector<std::string> v_geom = split(parts[1], ',');
2288 MY_CHECKPOS("tooltip", 0);
2289 MY_CHECKGEOM("tooltip", 1);
2294 if (data->real_coordinates) {
2295 pos = getRealCoordinateBasePos(v_pos);
2296 geom = getRealCoordinateGeometry(v_geom);
2298 pos = getElementBasePos(&v_pos);
2299 geom.X = stof(v_geom[0]) * spacing.X;
2300 geom.Y = stof(v_geom[1]) * spacing.Y;
2303 FieldSpec fieldspec(
2307 258 + m_fields.size()
2310 core::rect<s32> rect(pos, pos + geom);
2312 gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
2313 this, fieldspec.fid, rect);
2315 // the element the rect tooltip is bound to should not block mouse-clicks
2316 e->setVisible(false);
2318 m_fields.push_back(fieldspec);
2319 m_tooltip_rects.emplace_back(e, spec);
2322 m_tooltips[parts[0]] = spec;
2326 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
2332 std::vector<std::string> parts = split(data,'[');
2334 if (parts.size() < 2) {
2338 if (trim(parts[0]) != "formspec_version") {
2342 if (is_number(parts[1])) {
2343 m_formspec_version = mystoi(parts[1]);
2350 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
2352 if (element.empty())
2355 std::vector<std::string> parts = split(element,'[');
2357 if (parts.size() < 2)
2360 std::string type = trim(parts[0]);
2361 std::string description = trim(parts[1]);
2363 if (type != "size" && type != "invsize")
2366 if (type == "invsize")
2367 warningstream << "Deprecated formspec element \"invsize\" is used" << std::endl;
2369 parseSize(data, description);
2374 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
2376 if (element.empty())
2379 std::vector<std::string> parts = split(element, '[');
2381 if (parts.size() != 2)
2384 std::string type = trim(parts[0]);
2385 std::string description = trim(parts[1]);
2387 if (type != "position")
2390 parsePosition(data, description);
2395 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
2397 std::vector<std::string> parts = split(element, ',');
2399 if (parts.size() == 2) {
2400 data->offset.X = stof(parts[0]);
2401 data->offset.Y = stof(parts[1]);
2405 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
2408 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
2410 if (element.empty())
2413 std::vector<std::string> parts = split(element, '[');
2415 if (parts.size() != 2)
2418 std::string type = trim(parts[0]);
2419 std::string description = trim(parts[1]);
2421 if (type != "anchor")
2424 parseAnchor(data, description);
2429 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
2431 std::vector<std::string> parts = split(element, ',');
2433 if (parts.size() == 2) {
2434 data->anchor.X = stof(parts[0]);
2435 data->anchor.Y = stof(parts[1]);
2439 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
2440 << "'" << std::endl;
2443 bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type)
2445 std::vector<std::string> parts = split(element, ';');
2447 if (parts.size() < 2) {
2448 errorstream << "Invalid style element (" << parts.size() << "): '" << element
2449 << "'" << std::endl;
2455 for (size_t i = 1; i < parts.size(); i++) {
2456 size_t equal_pos = parts[i].find('=');
2457 if (equal_pos == std::string::npos) {
2458 errorstream << "Invalid style element (Property missing value): '" << element
2459 << "'" << std::endl;
2463 std::string propname = trim(parts[i].substr(0, equal_pos));
2464 std::string value = trim(unescape_string(parts[i].substr(equal_pos + 1)));
2466 std::transform(propname.begin(), propname.end(), propname.begin(), ::tolower);
2468 StyleSpec::Property prop = StyleSpec::GetPropertyByName(propname);
2469 if (prop == StyleSpec::NONE) {
2470 if (property_warned.find(propname) != property_warned.end()) {
2471 warningstream << "Invalid style element (Unknown property " << propname << "): '"
2473 << "'" << std::endl;
2474 property_warned.insert(propname);
2479 spec.set(prop, value);
2482 std::vector<std::string> selectors = split(parts[0], ',');
2483 for (size_t sel = 0; sel < selectors.size(); sel++) {
2484 std::string selector = trim(selectors[sel]);
2486 if (selector.empty()) {
2487 errorstream << "Invalid style element (Empty selector): '" << element
2488 << "'" << std::endl;
2493 theme_by_type[selector] |= spec;
2495 theme_by_name[selector] |= spec;
2502 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
2505 if (element.empty())
2508 if (parseVersionDirect(element))
2511 std::vector<std::string> parts = split(element,'[');
2513 // ugly workaround to keep compatibility
2514 if (parts.size() > 2) {
2515 if (trim(parts[0]) == "image") {
2516 for (unsigned int i=2;i< parts.size(); i++) {
2517 parts[1] += "[" + parts[i];
2523 if (parts.size() < 2) {
2527 std::string type = trim(parts[0]);
2528 std::string description = trim(parts[1]);
2530 if (type == "container") {
2531 parseContainer(data, description);
2535 if (type == "container_end") {
2536 parseContainerEnd(data);
2540 if (type == "list") {
2541 parseList(data, description);
2545 if (type == "listring") {
2546 parseListRing(data, description);
2550 if (type == "checkbox") {
2551 parseCheckbox(data, description);
2555 if (type == "image") {
2556 parseImage(data, description);
2560 if (type == "animated_image") {
2561 parseAnimatedImage(data, description);
2565 if (type == "item_image") {
2566 parseItemImage(data, description);
2570 if (type == "button" || type == "button_exit") {
2571 parseButton(data, description, type);
2575 if (type == "background" || type == "background9") {
2576 parseBackground(data, description);
2580 if (type == "tableoptions"){
2581 parseTableOptions(data,description);
2585 if (type == "tablecolumns"){
2586 parseTableColumns(data,description);
2590 if (type == "table"){
2591 parseTable(data,description);
2595 if (type == "textlist"){
2596 parseTextList(data,description);
2600 if (type == "dropdown"){
2601 parseDropDown(data,description);
2605 if (type == "field_close_on_enter") {
2606 parseFieldCloseOnEnter(data, description);
2610 if (type == "pwdfield") {
2611 parsePwdField(data,description);
2615 if ((type == "field") || (type == "textarea")){
2616 parseField(data,description,type);
2620 if (type == "hypertext") {
2621 parseHyperText(data,description);
2625 if (type == "label") {
2626 parseLabel(data,description);
2630 if (type == "vertlabel") {
2631 parseVertLabel(data,description);
2635 if (type == "item_image_button") {
2636 parseItemImageButton(data,description);
2640 if ((type == "image_button") || (type == "image_button_exit")) {
2641 parseImageButton(data,description,type);
2645 if (type == "tabheader") {
2646 parseTabHeader(data,description);
2650 if (type == "box") {
2651 parseBox(data,description);
2655 if (type == "bgcolor") {
2656 parseBackgroundColor(data,description);
2660 if (type == "listcolors") {
2661 parseListColors(data,description);
2665 if (type == "tooltip") {
2666 parseTooltip(data,description);
2670 if (type == "scrollbar") {
2671 parseScrollBar(data, description);
2675 if (type == "real_coordinates") {
2676 data->real_coordinates = is_yes(description);
2680 if (type == "style") {
2681 parseStyle(data, description, false);
2685 if (type == "style_type") {
2686 parseStyle(data, description, true);
2690 if (type == "scrollbaroptions") {
2691 parseScrollBarOptions(data, description);
2696 infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
2700 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
2702 /* useless to regenerate without a screensize */
2703 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
2710 for (auto &m_table : m_tables) {
2711 std::string tablename = m_table.first.fname;
2712 GUITable *table = m_table.second;
2713 mydata.table_dyndata[tablename] = table->getDynamicData();
2717 if (!m_focused_element.empty())
2718 mydata.focused_fieldname = m_focused_element;
2721 gui::IGUIElement *focused_element = Environment->getFocus();
2722 if (focused_element && focused_element->getParent() == this) {
2723 s32 focused_id = focused_element->getID();
2724 if (focused_id > 257) {
2725 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
2726 if (field.fid == focused_id) {
2727 mydata.focused_fieldname = field.fname;
2737 for (auto &table_it : m_tables)
2738 table_it.second->drop();
2739 for (auto &inventorylist_it : m_inventorylists)
2740 inventorylist_it->drop();
2741 for (auto &checkbox_it : m_checkboxes)
2742 checkbox_it.second->drop();
2743 for (auto &scrollbar_it : m_scrollbars)
2744 scrollbar_it.second->drop();
2745 for (auto &background_it : m_backgrounds)
2746 background_it->drop();
2747 for (auto &tooltip_rect_it : m_tooltip_rects)
2748 tooltip_rect_it.first->drop();
2750 mydata.size= v2s32(100,100);
2751 mydata.screensize = screensize;
2752 mydata.offset = v2f32(0.5f, 0.5f);
2753 mydata.anchor = v2f32(0.5f, 0.5f);
2754 mydata.simple_field_count = 0;
2756 // Base position of contents of form
2757 mydata.basepos = getBasePos();
2759 m_inventorylists.clear();
2760 m_backgrounds.clear();
2762 m_checkboxes.clear();
2763 m_scrollbars.clear();
2766 m_tooltip_rects.clear();
2767 m_inventory_rings.clear();
2768 m_dropdowns.clear();
2769 theme_by_name.clear();
2770 theme_by_type.clear();
2772 m_bgnonfullscreen = true;
2773 m_bgfullscreen = false;
2775 m_formspec_version = 1;
2778 v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
2779 m_bgcolor = video::SColor(
2780 (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
2781 clamp_u8(myround(formspec_bgcolor.X)),
2782 clamp_u8(myround(formspec_bgcolor.Y)),
2783 clamp_u8(myround(formspec_bgcolor.Z))
2788 v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
2789 m_fullscreen_bgcolor = video::SColor(
2790 (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
2791 clamp_u8(myround(formspec_bgcolor.X)),
2792 clamp_u8(myround(formspec_bgcolor.Y)),
2793 clamp_u8(myround(formspec_bgcolor.Z))
2797 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
2798 m_default_tooltip_color = video::SColor(255,255,255,255);
2802 assert(!m_tooltip_element);
2803 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
2804 m_tooltip_element = gui::StaticText::add(Environment, L"",
2805 core::rect<s32>(0, 0, 110, 18));
2806 m_tooltip_element->enableOverrideColor(true);
2807 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2808 m_tooltip_element->setDrawBackground(true);
2809 m_tooltip_element->setDrawBorder(true);
2810 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2811 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
2812 m_tooltip_element->setWordWrap(false);
2813 //we're not parent so no autograb for this one!
2814 m_tooltip_element->grab();
2817 std::vector<std::string> elements = split(m_formspec_string,']');
2820 /* try to read version from first element only */
2821 if (!elements.empty()) {
2822 if (parseVersionDirect(elements[0])) {
2827 /* we need size first in order to calculate image scale */
2828 mydata.explicit_size = false;
2829 for (; i< elements.size(); i++) {
2830 if (!parseSizeDirect(&mydata, elements[i])) {
2835 /* "position" element is always after "size" element if it used */
2836 for (; i< elements.size(); i++) {
2837 if (!parsePositionDirect(&mydata, elements[i])) {
2842 /* "anchor" element is always after "position" (or "size" element) if it used */
2843 for (; i< elements.size(); i++) {
2844 if (!parseAnchorDirect(&mydata, elements[i])) {
2849 /* "no_prepend" element is always after "position" (or "size" element) if it used */
2850 bool enable_prepends = true;
2851 for (; i < elements.size(); i++) {
2852 if (elements[i].empty())
2855 std::vector<std::string> parts = split(elements[i], '[');
2856 if (trim(parts[0]) == "no_prepend")
2857 enable_prepends = false;
2862 /* Copy of the "real_coordinates" element for after the form size. */
2863 mydata.real_coordinates = m_formspec_version >= 2;
2864 for (; i < elements.size(); i++) {
2865 std::vector<std::string> parts = split(elements[i], '[');
2866 std::string name = trim(parts[0]);
2867 if (name != "real_coordinates" || parts.size() != 2)
2868 break; // Invalid format
2870 mydata.real_coordinates = is_yes(trim(parts[1]));
2873 if (mydata.explicit_size) {
2874 // compute scaling for specified form size
2876 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
2877 v2u32 delta = current_screensize - m_lockscreensize;
2879 if (current_screensize.Y > m_lockscreensize.Y)
2884 if (current_screensize.X > m_lockscreensize.X)
2889 offset = v2s32(delta.X,delta.Y);
2891 mydata.screensize = m_lockscreensize;
2893 offset = v2s32(0,0);
2896 double gui_scaling = g_settings->getFloat("gui_scaling");
2897 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2901 // In fixed-size mode, inventory image size
2902 // is 0.53 inch multiplied by the gui_scaling
2903 // config parameter. This magic size is chosen
2904 // to make the main menu (15.5 inventory images
2905 // wide, including border) just fit into the
2906 // default window (800 pixels wide) at 96 DPI
2907 // and default scaling (1.00).
2908 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
2910 // In variable-size mode, we prefer to make the
2911 // inventory image size 1/15 of screen height,
2912 // multiplied by the gui_scaling config parameter.
2913 // If the preferred size won't fit the whole
2914 // form on the screen, either horizontally or
2915 // vertically, then we scale it down to fit.
2916 // (The magic numbers in the computation of what
2917 // fits arise from the scaling factors in the
2918 // following stanza, including the form border,
2919 // help text space, and 0.1 inventory slot spare.)
2920 // However, a minimum size is also set, that
2921 // the image size can't be less than 0.3 inch
2922 // multiplied by gui_scaling, even if this means
2923 // the form doesn't fit the screen.
2925 // For mobile devices these magic numbers are
2926 // different and forms should always use the
2927 // maximum screen space available.
2928 double prefer_imgsize = mydata.screensize.Y / 10 * gui_scaling;
2929 double fitx_imgsize = mydata.screensize.X /
2930 ((12.0 / 8.0) * (0.5 + mydata.invsize.X));
2931 double fity_imgsize = mydata.screensize.Y /
2932 ((15.0 / 11.0) * (0.85 + mydata.invsize.Y));
2933 use_imgsize = MYMIN(prefer_imgsize,
2934 MYMIN(fitx_imgsize, fity_imgsize));
2936 double prefer_imgsize = mydata.screensize.Y / 15 * gui_scaling;
2937 double fitx_imgsize = mydata.screensize.X /
2938 ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
2939 double fity_imgsize = mydata.screensize.Y /
2940 ((15.0 / 13.0) * (0.85 * mydata.invsize.Y));
2941 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2942 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
2943 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2944 MYMIN(fitx_imgsize, fity_imgsize)));
2948 // Everything else is scaled in proportion to the
2949 // inventory image size. The inventory slot spacing
2950 // is 5/4 image size horizontally and 15/13 image size
2951 // vertically. The padding around the form (incorporating
2952 // the border of the outer inventory slots) is 3/8
2953 // image size. Font height (baseline to baseline)
2954 // is 2/5 vertical inventory slot spacing, and button
2955 // half-height is 7/8 of font height.
2956 imgsize = v2s32(use_imgsize, use_imgsize);
2957 spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2958 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2959 m_btn_height = use_imgsize*15.0/13 * 0.35;
2961 m_font = g_fontengine->getFont();
2963 if (mydata.real_coordinates) {
2964 mydata.size = v2s32(
2965 mydata.invsize.X*imgsize.X,
2966 mydata.invsize.Y*imgsize.Y
2969 mydata.size = v2s32(
2970 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2971 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2975 DesiredRect = mydata.rect = core::rect<s32>(
2976 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
2977 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
2978 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
2979 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
2982 // Non-size[] form must consist only of text fields and
2983 // implicit "Proceed" button. Use default font, and
2984 // temporary form size which will be recalculated below.
2985 m_font = g_fontengine->getFont();
2986 m_btn_height = font_line_height(m_font) * 0.875;
2987 DesiredRect = core::rect<s32>(
2988 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
2989 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
2990 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
2991 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
2994 recalculateAbsolutePosition(false);
2995 mydata.basepos = getBasePos();
2996 m_tooltip_element->setOverrideFont(m_font);
2998 gui::IGUISkin *skin = Environment->getSkin();
3000 gui::IGUIFont *old_font = skin->getFont();
3001 skin->setFont(m_font);
3003 pos_offset = v2f32();
3005 // used for formspec versions < 3
3006 core::list<IGUIElement *>::Iterator legacy_sort_start = Children.getLast();
3008 if (enable_prepends) {
3009 // Backup the coordinates so that prepends can use the coordinates of choice.
3010 bool rc_backup = mydata.real_coordinates;
3011 u16 version_backup = m_formspec_version;
3012 mydata.real_coordinates = false; // Old coordinates by default.
3014 std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
3015 for (const auto &element : prepend_elements)
3016 parseElement(&mydata, element);
3018 // legacy sorting for formspec versions < 3
3019 if (m_formspec_version >= 3)
3020 // prepends do not need to be reordered
3021 legacy_sort_start = Children.getLast();
3022 else if (version_backup >= 3)
3023 // only prepends elements have to be reordered
3024 legacySortElements(legacy_sort_start);
3026 m_formspec_version = version_backup;
3027 mydata.real_coordinates = rc_backup; // Restore coordinates
3030 for (; i< elements.size(); i++) {
3031 parseElement(&mydata, elements[i]);
3034 if (!container_stack.empty()) {
3035 errorstream << "Invalid formspec string: container was never closed!"
3039 // If there are fields without explicit size[], add a "Proceed"
3040 // button and adjust size to fit all the fields.
3041 if (mydata.simple_field_count > 0 && !mydata.explicit_size) {
3042 mydata.rect = core::rect<s32>(
3043 mydata.screensize.X / 2 - 580 / 2,
3044 mydata.screensize.Y / 2 - 300 / 2,
3045 mydata.screensize.X / 2 + 580 / 2,
3046 mydata.screensize.Y / 2 + 240 / 2 + mydata.simple_field_count * 60
3049 DesiredRect = mydata.rect;
3050 recalculateAbsolutePosition(false);
3051 mydata.basepos = getBasePos();
3054 v2s32 pos = mydata.basepos;
3055 pos.Y = (mydata.simple_field_count + 2) * 60;
3057 v2s32 size = DesiredRect.getSize();
3058 mydata.rect = core::rect<s32>(
3059 size.X / 2 - 70, pos.Y,
3060 size.X / 2 - 70 + 140, pos.Y + m_btn_height * 2
3062 const wchar_t *text = wgettext("Proceed");
3063 GUIButton::addButton(Environment, mydata.rect, this, 257, text);
3068 //set initial focus if parser didn't set it
3069 focused_element = Environment->getFocus();
3070 if (!focused_element
3071 || !isMyChild(focused_element)
3072 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
3075 skin->setFont(old_font);
3078 if (m_formspec_version < 3)
3079 legacySortElements(legacy_sort_start);
3082 void GUIFormSpecMenu::legacySortElements(core::list<IGUIElement *>::Iterator from)
3085 Draw order for formspec_version <= 2:
3089 0 All other elements
3091 2 item_image, item_image_button
3096 if (from == Children.end())
3097 from = Children.begin();
3101 core::list<IGUIElement *>::Iterator to = Children.end();
3102 // 1: Copy into a sortable container
3103 std::vector<IGUIElement *> elements;
3104 for (auto it = from; it != to; ++it)
3105 elements.emplace_back(*it);
3107 // 2: Sort the container
3108 std::stable_sort(elements.begin(), elements.end(),
3109 [this] (const IGUIElement *a, const IGUIElement *b) -> bool {
3110 const FieldSpec *spec_a = getSpecByID(a->getID());
3111 const FieldSpec *spec_b = getSpecByID(b->getID());
3112 return spec_a && spec_b &&
3113 spec_a->priority < spec_b->priority;
3116 // 3: Re-assign the pointers
3117 for (auto e : elements) {
3124 bool GUIFormSpecMenu::getAndroidUIInput()
3126 if (!hasAndroidUIInput())
3129 std::string fieldname = m_jni_field_name;
3130 m_jni_field_name.clear();
3132 for (std::vector<FieldSpec>::iterator iter = m_fields.begin();
3133 iter != m_fields.end(); ++iter) {
3135 if (iter->fname != fieldname) {
3138 IGUIElement *tochange = getElementFromId(iter->fid, true);
3140 if (tochange == 0) {
3144 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
3148 std::string text = porting::getInputDialogValue();
3150 ((gui::IGUIEditBox *)tochange)->setText(utf8_to_wide(text).c_str());
3156 GUIInventoryList::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
3158 core::rect<s32> imgrect(0, 0, imgsize.X, imgsize.Y);
3160 for (const GUIInventoryList *e : m_inventorylists) {
3161 s32 item_index = e->getItemIndexAtPos(p);
3162 if (item_index != -1)
3163 return GUIInventoryList::ItemSpec(e->getInventoryloc(), e->getListname(),
3167 return GUIInventoryList::ItemSpec(InventoryLocation(), "", -1);
3170 void GUIFormSpecMenu::drawSelectedItem()
3172 video::IVideoDriver* driver = Environment->getVideoDriver();
3174 if (!m_selected_item) {
3175 // reset rotation time
3176 drawItemStack(driver, m_font, ItemStack(),
3177 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)), NULL,
3178 m_client, IT_ROT_DRAGGED);
3182 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3184 InventoryList *list = inv->getList(m_selected_item->listname);
3186 ItemStack stack = list->getItem(m_selected_item->i);
3187 stack.count = m_selected_amount;
3189 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
3190 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
3191 rect.constrainTo(driver->getViewPort());
3192 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
3195 void GUIFormSpecMenu::drawMenu()
3198 const std::string &newform = m_form_src->getForm();
3199 if (newform != m_formspec_string) {
3200 m_formspec_string = newform;
3201 regenerateGui(m_screensize_old);
3205 gui::IGUISkin* skin = Environment->getSkin();
3206 sanity_check(skin != NULL);
3207 gui::IGUIFont *old_font = skin->getFont();
3208 skin->setFont(m_font);
3210 m_hovered_item_tooltips.clear();
3212 updateSelectedItem();
3214 video::IVideoDriver* driver = Environment->getVideoDriver();
3217 Draw background color
3219 v2u32 screenSize = driver->getScreenSize();
3220 core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
3223 driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
3224 if (m_bgnonfullscreen)
3225 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
3228 Draw rect_mode tooltip
3230 m_tooltip_element->setVisible(false);
3232 for (const auto &pair : m_tooltip_rects) {
3233 const core::rect<s32> &rect = pair.first->getAbsoluteClippingRect();
3234 if (rect.getArea() > 0 && rect.isPointInside(m_pointer)) {
3235 const std::wstring &text = pair.second.tooltip;
3236 if (!text.empty()) {
3237 showTooltip(text, pair.second.color, pair.second.bgcolor);
3246 for (gui::IGUIElement *e : m_backgrounds) {
3247 e->setVisible(true);
3249 e->setVisible(false);
3254 (This is where all the drawing happens.)
3256 gui::IGUIElement::draw();
3258 // Draw hovered item tooltips
3259 for (const std::string &tooltip : m_hovered_item_tooltips) {
3260 showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
3261 m_default_tooltip_bgcolor);
3264 if (m_hovered_item_tooltips.empty()) {
3265 // reset rotation time
3266 drawItemStack(driver, m_font, ItemStack(),
3267 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
3268 NULL, m_client, IT_ROT_HOVERED);
3271 /* TODO find way to show tooltips on touchscreen */
3272 #ifndef HAVE_TOUCHSCREENGUI
3273 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
3277 Draw fields/buttons tooltips and update the mouse cursor
3279 gui::IGUIElement *hovered =
3280 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
3282 #ifndef HAVE_TOUCHSCREENGUI
3283 gui::ICursorControl *cursor_control = RenderingEngine::get_raw_device()->
3285 gui::ECURSOR_ICON current_cursor_icon = cursor_control->getActiveIcon();
3287 bool hovered_element_found = false;
3289 if (hovered != NULL) {
3290 s32 id = hovered->getID();
3294 m_old_tooltip_id = id;
3296 if (id == m_old_tooltip_id) {
3297 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
3299 m_hovered_time = porting::getTimeMs();
3300 m_old_tooltip_id = id;
3304 // Find and update the current tooltip and cursor icon
3306 for (const FieldSpec &field : m_fields) {
3308 if (field.fid != id)
3311 if (delta >= m_tooltip_show_delay) {
3312 const std::wstring &text = m_tooltips[field.fname].tooltip;
3314 showTooltip(text, m_tooltips[field.fname].color,
3315 m_tooltips[field.fname].bgcolor);
3318 #ifndef HAVE_TOUCHSCREENGUI
3319 if (current_cursor_icon != field.fcursor_icon)
3320 cursor_control->setActiveIcon(field.fcursor_icon);
3323 hovered_element_found = true;
3330 if (!hovered_element_found) {
3331 // no element is hovered
3332 #ifndef HAVE_TOUCHSCREENGUI
3333 if (current_cursor_icon != ECI_NORMAL)
3334 cursor_control->setActiveIcon(ECI_NORMAL);
3338 m_tooltip_element->draw();
3341 Draw dragged item stack
3345 skin->setFont(old_font);
3349 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
3350 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
3352 EnrichedString ntext(text);
3353 ntext.setDefaultColor(color);
3354 ntext.setBackground(bgcolor);
3356 setStaticText(m_tooltip_element, ntext);
3358 // Tooltip size and offset
3359 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
3360 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
3362 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
3363 int tooltip_offset_x = m_btn_height;
3364 int tooltip_offset_y = m_btn_height;
3366 tooltip_offset_x *= 3;
3367 tooltip_offset_y = 0;
3368 if (m_pointer.X > (s32)screenSize.X / 2)
3369 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
3372 // Calculate and set the tooltip position
3373 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
3374 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
3375 if (tooltip_x + tooltip_width > (s32)screenSize.X)
3376 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
3377 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
3378 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
3380 m_tooltip_element->setRelativePosition(
3382 core::position2d<s32>(tooltip_x, tooltip_y),
3383 core::dimension2d<s32>(tooltip_width, tooltip_height)
3387 // Display the tooltip
3388 m_tooltip_element->setVisible(true);
3389 bringToFront(m_tooltip_element);
3392 void GUIFormSpecMenu::updateSelectedItem()
3394 verifySelectedItem();
3396 // If craftresult is nonempty and nothing else is selected, select it now.
3397 if (!m_selected_item) {
3398 for (const GUIInventoryList *e : m_inventorylists) {
3399 if (e->getListname() != "craftpreview")
3402 Inventory *inv = m_invmgr->getInventory(e->getInventoryloc());
3406 InventoryList *list = inv->getList("craftresult");
3408 if (!list || list->getSize() == 0)
3411 const ItemStack &item = list->getItem(0);
3415 // Grab selected item from the crafting result list
3416 m_selected_item = new GUIInventoryList::ItemSpec;
3417 m_selected_item->inventoryloc = e->getInventoryloc();
3418 m_selected_item->listname = "craftresult";
3419 m_selected_item->i = 0;
3420 m_selected_amount = item.count;
3421 m_selected_dragging = false;
3426 // If craftresult is selected, keep the whole stack selected
3427 if (m_selected_item && m_selected_item->listname == "craftresult")
3428 m_selected_amount = verifySelectedItem().count;
3431 ItemStack GUIFormSpecMenu::verifySelectedItem()
3433 // If the selected stack has become empty for some reason, deselect it.
3434 // If the selected stack has become inaccessible, deselect it.
3435 // If the selected stack has become smaller, adjust m_selected_amount.
3436 // Return the selected stack.
3438 if (m_selected_item) {
3439 if (m_selected_item->isValid()) {
3440 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3442 InventoryList *list = inv->getList(m_selected_item->listname);
3443 if (list && (u32) m_selected_item->i < list->getSize()) {
3444 ItemStack stack = list->getItem(m_selected_item->i);
3445 if (!m_selected_swap.empty()) {
3446 if (m_selected_swap.name == stack.name &&
3447 m_selected_swap.count == stack.count)
3448 m_selected_swap.clear();
3450 m_selected_amount = std::min(m_selected_amount, stack.count);
3459 // selection was not valid
3460 delete m_selected_item;
3461 m_selected_item = nullptr;
3462 m_selected_amount = 0;
3463 m_selected_dragging = false;
3468 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
3474 if (quitmode == quit_mode_accept) {
3475 fields["quit"] = "true";
3478 if (quitmode == quit_mode_cancel) {
3479 fields["quit"] = "true";
3480 m_text_dst->gotText(fields);
3484 if (current_keys_pending.key_down) {
3485 fields["key_down"] = "true";
3486 current_keys_pending.key_down = false;
3489 if (current_keys_pending.key_up) {
3490 fields["key_up"] = "true";
3491 current_keys_pending.key_up = false;
3494 if (current_keys_pending.key_enter) {
3495 fields["key_enter"] = "true";
3496 current_keys_pending.key_enter = false;
3499 if (!current_field_enter_pending.empty()) {
3500 fields["key_enter_field"] = current_field_enter_pending;
3501 current_field_enter_pending = "";
3504 if (current_keys_pending.key_escape) {
3505 fields["key_escape"] = "true";
3506 current_keys_pending.key_escape = false;
3509 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
3511 std::string name = s.fname;
3512 if (s.ftype == f_Button) {
3513 fields[name] = wide_to_utf8(s.flabel);
3514 } else if (s.ftype == f_Table) {
3515 GUITable *table = getTable(s.fname);
3517 fields[name] = table->checkEvent();
3520 else if(s.ftype == f_DropDown) {
3521 // no dynamic cast possible due to some distributions shipped
3522 // without rtti support in irrlicht
3523 IGUIElement *element = getElementFromId(s.fid, true);
3524 gui::IGUIComboBox *e = NULL;
3525 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
3526 e = static_cast<gui::IGUIComboBox*>(element);
3528 warningstream << "GUIFormSpecMenu::acceptInput: dropdown "
3529 << "field without dropdown element" << std::endl;
3532 s32 selected = e->getSelected();
3533 if (selected >= 0) {
3534 std::vector<std::string> *dropdown_values =
3535 getDropDownValues(s.fname);
3536 if (dropdown_values && selected < (s32)dropdown_values->size()) {
3537 fields[name] = (*dropdown_values)[selected];
3541 else if (s.ftype == f_TabHeader) {
3542 // no dynamic cast possible due to some distributions shipped
3543 // without rttzi support in irrlicht
3544 IGUIElement *element = getElementFromId(s.fid, true);
3545 gui::IGUITabControl *e = nullptr;
3546 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
3547 e = static_cast<gui::IGUITabControl *>(element);
3551 std::stringstream ss;
3552 ss << (e->getActiveTab() +1);
3553 fields[name] = ss.str();
3556 else if (s.ftype == f_CheckBox) {
3557 // no dynamic cast possible due to some distributions shipped
3558 // without rtti support in irrlicht
3559 IGUIElement *element = getElementFromId(s.fid, true);
3560 gui::IGUICheckBox *e = nullptr;
3561 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
3562 e = static_cast<gui::IGUICheckBox*>(element);
3567 fields[name] = "true";
3569 fields[name] = "false";
3572 else if (s.ftype == f_ScrollBar) {
3573 // no dynamic cast possible due to some distributions shipped
3574 // without rtti support in irrlicht
3575 IGUIElement *element = getElementFromId(s.fid, true);
3576 GUIScrollBar *e = nullptr;
3577 if (element && element->getType() == gui::EGUIET_ELEMENT)
3578 e = static_cast<GUIScrollBar *>(element);
3581 std::stringstream os;
3583 if (s.fdefault == L"Changed")
3584 fields[name] = "CHG:" + os.str();
3586 fields[name] = "VAL:" + os.str();
3590 IGUIElement *e = getElementFromId(s.fid, true);
3592 fields[name] = wide_to_utf8(e->getText());
3597 m_text_dst->gotText(fields);
3601 static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
3604 if (tocheck == parent) {
3607 tocheck = tocheck->getParent();
3612 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
3614 // The IGUITabControl renders visually using the skin's selected
3615 // font, which we override for the duration of form drawing,
3616 // but computes tab hotspots based on how it would have rendered
3617 // using the font that is selected at the time of button release.
3618 // To make these two consistent, temporarily override the skin's
3619 // font while the IGUITabControl is processing the event.
3620 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3621 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3622 s32 x = event.MouseInput.X;
3623 s32 y = event.MouseInput.Y;
3624 gui::IGUIElement *hovered =
3625 Environment->getRootGUIElement()->getElementFromPoint(
3626 core::position2d<s32>(x, y));
3627 if (hovered && isMyChild(hovered) &&
3628 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
3629 gui::IGUISkin* skin = Environment->getSkin();
3630 sanity_check(skin != NULL);
3631 gui::IGUIFont *old_font = skin->getFont();
3632 skin->setFont(m_font);
3633 bool retval = hovered->OnEvent(event);
3634 skin->setFont(old_font);
3639 // Fix Esc/Return key being eaten by checkboxen and tables
3640 if (event.EventType == EET_KEY_INPUT_EVENT) {
3641 KeyPress kp(event.KeyInput);
3642 if (kp == EscapeKey || kp == CancelKey
3643 || kp == getKeySetting("keymap_inventory")
3644 || event.KeyInput.Key==KEY_RETURN) {
3645 gui::IGUIElement *focused = Environment->getFocus();
3646 if (focused && isMyChild(focused) &&
3647 (focused->getType() == gui::EGUIET_LIST_BOX ||
3648 focused->getType() == gui::EGUIET_CHECK_BOX) &&
3649 (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
3650 event.KeyInput.Key != KEY_RETURN)) {
3656 // Mouse wheel and move events: send to hovered element instead of focused
3657 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3658 (event.MouseInput.Event == EMIE_MOUSE_WHEEL ||
3659 event.MouseInput.Event == EMIE_MOUSE_MOVED)) {
3660 s32 x = event.MouseInput.X;
3661 s32 y = event.MouseInput.Y;
3662 gui::IGUIElement *hovered =
3663 Environment->getRootGUIElement()->getElementFromPoint(
3664 core::position2d<s32>(x, y));
3665 if (hovered && isMyChild(hovered)) {
3666 hovered->OnEvent(event);
3667 return event.MouseInput.Event == EMIE_MOUSE_WHEEL;
3671 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
3672 s32 x = event.MouseInput.X;
3673 s32 y = event.MouseInput.Y;
3674 gui::IGUIElement *hovered =
3675 Environment->getRootGUIElement()->getElementFromPoint(
3676 core::position2d<s32>(x, y));
3677 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3678 m_old_tooltip_id = -1;
3680 if (!isChild(hovered, this)) {
3681 if (DoubleClickDetection(event)) {
3687 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3688 /* TODO add a check like:
3689 if (event.JoystickEvent != joystick_we_listen_for)
3692 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3694 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3696 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3698 acceptInput(quit_mode_accept);
3706 return GUIModalMenu::preprocessEvent(event);
3709 /******************************************************************************/
3710 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3712 /* The following code is for capturing double-clicks of the mouse button
3713 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3714 * -- which closes the form -- under some circumstances.
3716 * There have been many github issues reporting this as a bug even though it
3717 * was an intended feature. For this reason, remapping the double-click as
3718 * an ESC must be explicitly set when creating this class via the
3719 * /p remap_dbl_click parameter of the constructor.
3722 if (!m_remap_dbl_click)
3725 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3726 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3727 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3729 m_doubleclickdetect[1].pos = m_pointer;
3730 m_doubleclickdetect[1].time = porting::getTimeMs();
3732 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3733 u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
3738 double squaredistance =
3739 m_doubleclickdetect[0].pos
3740 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3742 if (squaredistance > (30*30)) {
3746 SEvent* translated = new SEvent();
3747 assert(translated != 0);
3748 //translate doubleclick to escape
3749 memset(translated, 0, sizeof(SEvent));
3750 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3751 translated->KeyInput.Key = KEY_ESCAPE;
3752 translated->KeyInput.Control = false;
3753 translated->KeyInput.Shift = false;
3754 translated->KeyInput.PressedDown = true;
3755 translated->KeyInput.Char = 0;
3756 OnEvent(*translated);
3758 // no need to send the key up event as we're already deleted
3759 // and no one else did notice this event
3767 void GUIFormSpecMenu::tryClose()
3771 acceptInput(quit_mode_cancel);
3774 m_text_dst->gotText(L"MenuQuit");
3778 enum ButtonEventType : u8
3791 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3793 if (event.EventType==EET_KEY_INPUT_EVENT) {
3794 KeyPress kp(event.KeyInput);
3795 if (event.KeyInput.PressedDown && (
3796 (kp == EscapeKey) || (kp == CancelKey) ||
3797 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
3802 if (m_client != NULL && event.KeyInput.PressedDown &&
3803 (kp == getKeySetting("keymap_screenshot"))) {
3804 m_client->makeScreenshot();
3806 if (event.KeyInput.PressedDown &&
3807 (event.KeyInput.Key==KEY_RETURN ||
3808 event.KeyInput.Key==KEY_UP ||
3809 event.KeyInput.Key==KEY_DOWN)
3811 switch (event.KeyInput.Key) {
3813 current_keys_pending.key_enter = true;
3816 current_keys_pending.key_up = true;
3819 current_keys_pending.key_down = true;
3823 //can't happen at all!
3824 FATAL_ERROR("Reached a source line that can't ever been reached");
3827 if (current_keys_pending.key_enter && m_allowclose) {
3828 acceptInput(quit_mode_accept);
3838 /* Mouse event other than movement, or crossing the border of inventory
3839 field while holding right mouse button
3841 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3842 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3843 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3844 event.MouseInput.isRightPressed() &&
3845 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3847 // Get selected item and hovered/clicked item (s)
3849 m_old_tooltip_id = -1;
3850 updateSelectedItem();
3851 GUIInventoryList::ItemSpec s = getItemAtPos(m_pointer);
3853 Inventory *inv_selected = NULL;
3854 Inventory *inv_s = NULL;
3855 InventoryList *list_s = NULL;
3857 if (m_selected_item) {
3858 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3859 sanity_check(inv_selected);
3860 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3867 inv_s = m_invmgr->getInventory(s.inventoryloc);
3870 errorstream << "InventoryMenu: The selected inventory location "
3871 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3873 s.i = -1; // make it invalid again
3877 list_s = inv_s->getList(s.listname);
3878 if (list_s == NULL) {
3879 verbosestream << "InventoryMenu: The selected inventory list \""
3880 << s.listname << "\" does not exist" << std::endl;
3881 s.i = -1; // make it invalid again
3885 if ((u32)s.i >= list_s->getSize()) {
3886 infostream << "InventoryMenu: The selected inventory list \""
3887 << s.listname << "\" is too small (i=" << s.i << ", size="
3888 << list_s->getSize() << ")" << std::endl;
3889 s.i = -1; // make it invalid again
3893 s_count = list_s->getItem(s.i).count;
3896 bool identical = m_selected_item && s.isValid() &&
3897 (inv_selected == inv_s) &&
3898 (m_selected_item->listname == s.listname) &&
3899 (m_selected_item->i == s.i);
3901 ButtonEventType button = BET_LEFT;
3902 ButtonEventType updown = BET_OTHER;
3903 switch (event.MouseInput.Event) {
3904 case EMIE_LMOUSE_PRESSED_DOWN:
3905 button = BET_LEFT; updown = BET_DOWN;
3907 case EMIE_RMOUSE_PRESSED_DOWN:
3908 button = BET_RIGHT; updown = BET_DOWN;
3910 case EMIE_MMOUSE_PRESSED_DOWN:
3911 button = BET_MIDDLE; updown = BET_DOWN;
3913 case EMIE_MOUSE_WHEEL:
3914 button = (event.MouseInput.Wheel > 0) ?
3915 BET_WHEEL_UP : BET_WHEEL_DOWN;
3918 case EMIE_LMOUSE_LEFT_UP:
3919 button = BET_LEFT; updown = BET_UP;
3921 case EMIE_RMOUSE_LEFT_UP:
3922 button = BET_RIGHT; updown = BET_UP;
3924 case EMIE_MMOUSE_LEFT_UP:
3925 button = BET_MIDDLE; updown = BET_UP;
3927 case EMIE_MOUSE_MOVED:
3934 // Set this number to a positive value to generate a move action
3935 // from m_selected_item to s.
3936 u32 move_amount = 0;
3938 // Set this number to a positive value to generate a move action
3939 // from s to the next inventory ring.
3940 u32 shift_move_amount = 0;
3942 // Set this number to a positive value to generate a drop action
3943 // from m_selected_item.
3944 u32 drop_amount = 0;
3946 // Set this number to a positive value to generate a craft action at s.
3947 u32 craft_amount = 0;
3951 // Some mouse button has been pressed
3953 //infostream << "Mouse button " << button << " pressed at p=("
3954 // << event.MouseInput.X << "," << event.MouseInput.Y << ")"
3957 m_selected_dragging = false;
3959 if (s.isValid() && s.listname == "craftpreview") {
3960 // Craft preview has been clicked: craft
3961 craft_amount = (button == BET_MIDDLE ? 10 : 1);
3962 } else if (!m_selected_item) {
3963 if (s_count && button != BET_WHEEL_UP) {
3964 // Non-empty stack has been clicked: select or shift-move it
3965 m_selected_item = new GUIInventoryList::ItemSpec(s);
3968 if (button == BET_RIGHT)
3969 count = (s_count + 1) / 2;
3970 else if (button == BET_MIDDLE)
3971 count = MYMIN(s_count, 10);
3972 else if (button == BET_WHEEL_DOWN)
3977 if (!event.MouseInput.Shift) {
3978 // no shift: select item
3979 m_selected_amount = count;
3980 m_selected_dragging = button != BET_WHEEL_DOWN;
3981 m_auto_place = false;
3983 // shift pressed: move item, right click moves 1
3984 shift_move_amount = button == BET_RIGHT ? 1 : count;
3987 } else { // m_selected_item != NULL
3988 assert(m_selected_amount >= 1);
3991 // Clicked a slot: move
3992 if (button == BET_RIGHT || button == BET_WHEEL_UP)
3994 else if (button == BET_MIDDLE)
3995 move_amount = MYMIN(m_selected_amount, 10);
3996 else if (button == BET_LEFT)
3997 move_amount = m_selected_amount;
4001 if (button == BET_WHEEL_DOWN) {
4002 if (m_selected_amount < s_count)
4003 ++m_selected_amount;
4005 if (move_amount >= m_selected_amount)
4006 m_selected_amount = 0;
4008 m_selected_amount -= move_amount;
4012 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
4013 && button != BET_WHEEL_DOWN) {
4014 // Clicked outside of the window: drop
4015 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4017 else if (button == BET_MIDDLE)
4018 drop_amount = MYMIN(m_selected_amount, 10);
4020 drop_amount = m_selected_amount;
4025 // Some mouse button has been released
4027 //infostream<<"Mouse button "<<button<<" released at p=("
4028 // <<p.X<<","<<p.Y<<")"<<std::endl;
4030 if (m_selected_dragging && m_selected_item) {
4033 // Dragged to different slot: move all selected
4034 move_amount = m_selected_amount;
4036 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
4037 // Dragged outside of window: drop all selected
4038 drop_amount = m_selected_amount;
4042 m_selected_dragging = false;
4043 // Keep track of whether the mouse button be released
4044 // One click is drag without dropping. Click + release
4045 // + click changes to drop item when moved mode
4046 if (m_selected_item)
4047 m_auto_place = true;
4050 // Mouse has been moved and rmb is down and mouse pointer just
4051 // entered a new inventory field (checked in the entry-if, this
4052 // is the only action here that is generated by mouse movement)
4053 if (m_selected_item && s.isValid() && s.listname != "craftpreview") {
4055 // TODO: middle mouse to move 10 items might be handy
4057 // Only move an item if the destination slot is empty
4058 // or contains the same item type as what is going to be
4060 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4061 InventoryList *list_to = list_s;
4062 assert(list_from && list_to);
4063 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4064 ItemStack stack_to = list_to->getItem(s.i);
4065 if (stack_to.empty() || stack_to.name == stack_from.name)
4074 // Possibly send inventory action to server
4075 if (move_amount > 0) {
4076 // Send IAction::Move
4078 assert(m_selected_item && m_selected_item->isValid());
4079 assert(s.isValid());
4081 assert(inv_selected && inv_s);
4082 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4083 InventoryList *list_to = list_s;
4084 assert(list_from && list_to);
4085 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4086 ItemStack stack_to = list_to->getItem(s.i);
4088 // Check how many items can be moved
4089 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
4090 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
4092 // If source stack cannot be added to destination stack at all,
4094 if (leftover.count == stack_from.count &&
4095 leftover.name == stack_from.name) {
4097 if (m_selected_swap.empty()) {
4098 m_selected_amount = stack_to.count;
4099 m_selected_dragging = false;
4101 // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
4102 // Skip next validation checks due async inventory calls
4103 m_selected_swap = stack_to;
4108 // Source stack goes fully into destination stack
4109 else if (leftover.empty()) {
4110 m_selected_amount -= move_amount;
4112 // Source stack goes partly into destination stack
4114 move_amount -= leftover.count;
4115 m_selected_amount -= move_amount;
4119 infostream << "Handing IAction::Move to manager" << std::endl;
4120 IMoveAction *a = new IMoveAction();
4121 a->count = move_amount;
4122 a->from_inv = m_selected_item->inventoryloc;
4123 a->from_list = m_selected_item->listname;
4124 a->from_i = m_selected_item->i;
4125 a->to_inv = s.inventoryloc;
4126 a->to_list = s.listname;
4128 m_invmgr->inventoryAction(a);
4130 } else if (shift_move_amount > 0) {
4131 u32 mis = m_inventory_rings.size();
4133 for (; i < mis; i++) {
4134 const ListRingSpec &sp = m_inventory_rings[i];
4135 if (sp.inventoryloc == s.inventoryloc
4136 && sp.listname == s.listname)
4140 if (i >= mis) // if not found
4142 u32 to_inv_ind = (i + 1) % mis;
4143 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
4144 InventoryList *list_from = list_s;
4147 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
4150 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
4153 ItemStack stack_from = list_from->getItem(s.i);
4154 assert(shift_move_amount <= stack_from.count);
4156 infostream << "Handing IAction::Move to manager" << std::endl;
4157 IMoveAction *a = new IMoveAction();
4158 a->count = shift_move_amount;
4159 a->from_inv = s.inventoryloc;
4160 a->from_list = s.listname;
4162 a->to_inv = to_inv_sp.inventoryloc;
4163 a->to_list = to_inv_sp.listname;
4164 a->move_somewhere = true;
4165 m_invmgr->inventoryAction(a);
4167 } else if (drop_amount > 0) {
4168 // Send IAction::Drop
4170 assert(m_selected_item && m_selected_item->isValid());
4171 assert(inv_selected);
4172 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4174 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4176 // Check how many items can be dropped
4177 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
4178 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
4179 m_selected_amount -= drop_amount;
4181 infostream << "Handing IAction::Drop to manager" << std::endl;
4182 IDropAction *a = new IDropAction();
4183 a->count = drop_amount;
4184 a->from_inv = m_selected_item->inventoryloc;
4185 a->from_list = m_selected_item->listname;
4186 a->from_i = m_selected_item->i;
4187 m_invmgr->inventoryAction(a);
4188 } else if (craft_amount > 0) {
4189 assert(s.isValid());
4191 // if there are no items selected or the selected item
4192 // belongs to craftresult list, proceed with crafting
4193 if (!m_selected_item ||
4194 !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
4198 // Send IACTION_CRAFT
4199 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
4200 ICraftAction *a = new ICraftAction();
4201 a->count = craft_amount;
4202 a->craft_inv = s.inventoryloc;
4203 m_invmgr->inventoryAction(a);
4207 // If m_selected_amount has been decreased to zero, deselect
4208 if (m_selected_amount == 0) {
4209 m_selected_swap.clear();
4210 delete m_selected_item;
4211 m_selected_item = nullptr;
4212 m_selected_amount = 0;
4213 m_selected_dragging = false;
4215 m_old_pointer = m_pointer;
4218 if (event.EventType == EET_GUI_EVENT) {
4219 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
4221 // find the element that was clicked
4222 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4223 if ((s.ftype == f_TabHeader) &&
4224 (s.fid == event.GUIEvent.Caller->getID())) {
4232 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
4234 if (!canTakeFocus(event.GUIEvent.Element)) {
4235 infostream<<"GUIFormSpecMenu: Not allowing focus change."
4237 // Returning true disables focus change
4241 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
4242 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
4243 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
4244 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
4245 unsigned int btn_id = event.GUIEvent.Caller->getID();
4247 if (btn_id == 257) {
4249 acceptInput(quit_mode_accept);
4253 m_text_dst->gotText(L"ExitButton");
4255 // quitMenu deallocates menu
4259 // find the element that was clicked
4260 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4261 // if its a button, set the send field so
4262 // lua knows which button was pressed
4263 if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
4264 s.fid == event.GUIEvent.Caller->getID()) {
4268 acceptInput(quit_mode_accept);
4271 m_text_dst->gotText(L"ExitButton");
4276 acceptInput(quit_mode_no);
4280 } else if ((s.ftype == f_DropDown) &&
4281 (s.fid == event.GUIEvent.Caller->getID())) {
4282 // only send the changed dropdown
4283 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4284 if (s2.ftype == f_DropDown) {
4289 acceptInput(quit_mode_no);
4291 // revert configuration to make sure dropdowns are sent on
4292 // regular button click
4293 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4294 if (s2.ftype == f_DropDown) {
4299 } else if ((s.ftype == f_ScrollBar) &&
4300 (s.fid == event.GUIEvent.Caller->getID())) {
4301 s.fdefault = L"Changed";
4302 acceptInput(quit_mode_no);
4304 } else if ((s.ftype == f_Unknown) &&
4305 (s.fid == event.GUIEvent.Caller->getID())) {
4313 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
4314 if (event.GUIEvent.Caller->getID() > 257) {
4315 bool close_on_enter = true;
4316 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4317 if (s.ftype == f_Unknown &&
4318 s.fid == event.GUIEvent.Caller->getID()) {
4319 current_field_enter_pending = s.fname;
4320 std::unordered_map<std::string, bool>::const_iterator it =
4321 field_close_on_enter.find(s.fname);
4322 if (it != field_close_on_enter.end())
4323 close_on_enter = (*it).second;
4329 if (m_allowclose && close_on_enter) {
4330 current_keys_pending.key_enter = true;
4331 acceptInput(quit_mode_accept);
4334 current_keys_pending.key_enter = true;
4337 // quitMenu deallocates menu
4342 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
4343 int current_id = event.GUIEvent.Caller->getID();
4344 if (current_id > 257) {
4345 // find the element that was clicked
4346 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4347 // if it's a table, set the send field
4348 // so lua knows which table was changed
4349 if ((s.ftype == f_Table) && (s.fid == current_id)) {
4360 return Parent ? Parent->OnEvent(event) : false;
4364 * get name of element by element id
4365 * @param id of element
4366 * @return name string or empty string
4368 std::string GUIFormSpecMenu::getNameByID(s32 id)
4370 for (FieldSpec &spec : m_fields) {
4378 const GUIFormSpecMenu::FieldSpec *GUIFormSpecMenu::getSpecByID(s32 id)
4380 for (FieldSpec &spec : m_fields) {
4388 * get label of element by id
4389 * @param id of element
4390 * @return label string or empty string
4392 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
4394 for (FieldSpec &spec : m_fields) {
4401 StyleSpec GUIFormSpecMenu::getStyleForElement(const std::string &type,
4402 const std::string &name, const std::string &parent_type) {
4405 if (!parent_type.empty()) {
4406 auto it = theme_by_type.find(parent_type);
4407 if (it != theme_by_type.end()) {
4412 auto it = theme_by_type.find(type);
4413 if (it != theme_by_type.end()) {
4417 it = theme_by_name.find(name);
4418 if (it != theme_by_name.end()) {