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"
28 #include "constants.h"
31 #include "util/strfnd.h"
32 #include <IGUICheckBox.h>
33 #include <IGUIEditBox.h>
34 #include <IGUIButton.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include <IGUIComboBox.h>
39 #include "client/renderingengine.h"
41 #include "client/tile.h" // ITextureSource
42 #include "client/hud.h" // drawItemStack
46 #include "scripting_server.h"
47 #include "mainmenumanager.h"
51 #include "fontengine.h"
53 #include "util/numeric.h"
54 #include "util/string.h" // for parseColorString()
55 #include "irrlicht_changes/static_text.h"
56 #include "guiscalingfilter.h"
57 #include "guiEditBoxWithScrollbar.h"
58 #include "intlGUIEditBox.h"
60 #define MY_CHECKPOS(a,b) \
61 if (v_pos.size() != 2) { \
62 errorstream<< "Invalid pos for element " << a << "specified: \"" \
63 << parts[b] << "\"" << std::endl; \
67 #define MY_CHECKGEOM(a,b) \
68 if (v_geom.size() != 2) { \
69 errorstream<< "Invalid pos for element " << a << "specified: \"" \
70 << parts[b] << "\"" << std::endl; \
76 static unsigned int font_line_height(gui::IGUIFont *font)
78 return font->getDimension(L"Ay").Height + font->getKerningHeight();
81 inline u32 clamp_u8(s32 value)
83 return (u32) MYMIN(MYMAX(value, 0), 255);
86 GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
87 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
88 Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst,
89 std::string formspecPrepend,
90 bool remap_dbl_click):
91 GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr),
95 m_formspec_prepend(formspecPrepend),
99 m_remap_dbl_click(remap_dbl_click)
101 , m_JavaDialogFieldName("")
104 current_keys_pending.key_down = false;
105 current_keys_pending.key_up = false;
106 current_keys_pending.key_enter = false;
107 current_keys_pending.key_escape = false;
109 m_doubleclickdetect[0].time = 0;
110 m_doubleclickdetect[1].time = 0;
112 m_doubleclickdetect[0].pos = v2s32(0, 0);
113 m_doubleclickdetect[1].pos = v2s32(0, 0);
115 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
116 m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
119 GUIFormSpecMenu::~GUIFormSpecMenu()
123 for (auto &table_it : m_tables) {
124 table_it.second->drop();
127 delete m_selected_item;
132 void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
133 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
134 const std::string &formspecPrepend)
136 if (cur_formspec == nullptr) {
137 cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
138 client, client->getTextureSource(), fs_src, txt_dest, formspecPrepend);
139 cur_formspec->doPause = false;
142 Caution: do not call (*cur_formspec)->drop() here --
143 the reference might outlive the menu, so we will
144 periodically check if *cur_formspec is the only
145 remaining reference (i.e. the menu was removed)
146 and delete it in that case.
150 cur_formspec->setFormspecPrepend(formspecPrepend);
151 cur_formspec->setFormSource(fs_src);
152 cur_formspec->setTextDest(txt_dest);
156 void GUIFormSpecMenu::removeChildren()
158 const core::list<gui::IGUIElement*> &children = getChildren();
160 while(!children.empty()) {
161 (*children.getLast())->remove();
164 if(m_tooltip_element) {
165 m_tooltip_element->remove();
166 m_tooltip_element->drop();
167 m_tooltip_element = NULL;
172 void GUIFormSpecMenu::setInitialFocus()
174 // Set initial focus according to following order of precedence:
175 // 1. first empty editbox
179 // 5. first focusable (not statictext, not tabheader)
180 // 6. first child element
182 core::list<gui::IGUIElement*> children = getChildren();
184 // in case "children" contains any NULL elements, remove them
185 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
186 it != children.end();) {
190 it = children.erase(it);
193 // 1. first empty editbox
194 for (gui::IGUIElement *it : children) {
195 if (it->getType() == gui::EGUIET_EDIT_BOX
196 && it->getText()[0] == 0) {
197 Environment->setFocus(it);
203 for (gui::IGUIElement *it : children) {
204 if (it->getType() == gui::EGUIET_EDIT_BOX) {
205 Environment->setFocus(it);
211 for (gui::IGUIElement *it : children) {
212 if (it->getTypeName() == std::string("GUITable")) {
213 Environment->setFocus(it);
219 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
220 it != children.end(); --it) {
221 if ((*it)->getType() == gui::EGUIET_BUTTON) {
222 Environment->setFocus(*it);
227 // 5. first focusable (not statictext, not tabheader)
228 for (gui::IGUIElement *it : children) {
229 if (it->getType() != gui::EGUIET_STATIC_TEXT &&
230 it->getType() != gui::EGUIET_TAB_CONTROL) {
231 Environment->setFocus(it);
236 // 6. first child element
237 if (children.empty())
238 Environment->setFocus(this);
240 Environment->setFocus(*(children.begin()));
243 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
245 for (auto &table : m_tables) {
246 if (tablename == table.first.fname)
252 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
254 for (auto &dropdown : m_dropdowns) {
255 if (name == dropdown.first.fname)
256 return &dropdown.second;
261 v2s32 GUIFormSpecMenu::getElementBasePos(bool absolute,
262 const std::vector<std::string> *v_pos)
266 pos += AbsoluteRect.UpperLeftCorner;
268 v2f32 pos_f = v2f32(pos.X, pos.Y) + pos_offset * spacing;
270 pos_f.X += stof((*v_pos)[0]) * spacing.X;
271 pos_f.Y += stof((*v_pos)[1]) * spacing.Y;
273 return v2s32(pos_f.X, pos_f.Y);
276 void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
278 std::vector<std::string> parts = split(element,',');
280 if (((parts.size() == 2) || parts.size() == 3) ||
281 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
283 if (parts[1].find(';') != std::string::npos)
284 parts[1] = parts[1].substr(0,parts[1].find(';'));
286 data->invsize.X = MYMAX(0, stof(parts[0]));
287 data->invsize.Y = MYMAX(0, stof(parts[1]));
290 if (parts.size() == 3) {
291 if (parts[2] == "true") {
292 lockSize(true,v2u32(800,600));
296 data->explicit_size = true;
299 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
302 void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
304 std::vector<std::string> parts = split(element, ',');
306 if (parts.size() >= 2) {
307 if (parts[1].find(';') != std::string::npos)
308 parts[1] = parts[1].substr(0, parts[1].find(';'));
310 container_stack.push(pos_offset);
311 pos_offset.X += MYMAX(0, stof(parts[0]));
312 pos_offset.Y += MYMAX(0, stof(parts[1]));
315 errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
318 void GUIFormSpecMenu::parseContainerEnd(parserData* data)
320 if (container_stack.empty()) {
321 errorstream<< "Invalid container end element, no matching container start element" << std::endl;
323 pos_offset = container_stack.top();
324 container_stack.pop();
328 void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
331 warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
335 std::vector<std::string> parts = split(element,';');
337 if (((parts.size() == 4) || (parts.size() == 5)) ||
338 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
340 std::string location = parts[0];
341 std::string listname = parts[1];
342 std::vector<std::string> v_pos = split(parts[2],',');
343 std::vector<std::string> v_geom = split(parts[3],',');
344 std::string startindex;
345 if (parts.size() == 5)
346 startindex = parts[4];
348 MY_CHECKPOS("list",2);
349 MY_CHECKGEOM("list",3);
351 InventoryLocation loc;
353 if(location == "context" || location == "current_name")
354 loc = m_current_inventory_location;
356 loc.deSerialize(location);
358 v2s32 pos = getElementBasePos(true, &v_pos);
360 geom.X = stoi(v_geom[0]);
361 geom.Y = stoi(v_geom[1]);
364 if (!startindex.empty())
365 start_i = stoi(startindex);
367 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
368 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
372 if(!data->explicit_size)
373 warningstream<<"invalid use of list without a size[] element"<<std::endl;
374 m_inventorylists.emplace_back(loc, listname, pos, geom, start_i);
377 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
380 void GUIFormSpecMenu::parseListRing(parserData* data, const std::string &element)
383 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
387 std::vector<std::string> parts = split(element, ';');
389 if (parts.size() == 2) {
390 std::string location = parts[0];
391 std::string listname = parts[1];
393 InventoryLocation loc;
395 if (location == "context" || location == "current_name")
396 loc = m_current_inventory_location;
398 loc.deSerialize(location);
400 m_inventory_rings.emplace_back(loc, listname);
404 if (element.empty() && m_inventorylists.size() > 1) {
405 size_t siz = m_inventorylists.size();
406 // insert the last two inv list elements into the list ring
407 const ListDrawSpec &spa = m_inventorylists[siz - 2];
408 const ListDrawSpec &spb = m_inventorylists[siz - 1];
409 m_inventory_rings.emplace_back(spa.inventoryloc, spa.listname);
410 m_inventory_rings.emplace_back(spb.inventoryloc, spb.listname);
414 errorstream<< "Invalid list ring element(" << parts.size() << ", "
415 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
418 void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
420 std::vector<std::string> parts = split(element,';');
422 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
423 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
425 std::vector<std::string> v_pos = split(parts[0],',');
426 std::string name = parts[1];
427 std::string label = parts[2];
428 std::string selected;
430 if (parts.size() >= 4)
433 MY_CHECKPOS("checkbox",0);
435 v2s32 pos = getElementBasePos(false, &v_pos);
437 bool fselected = false;
439 if (selected == "true")
442 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
444 core::rect<s32> rect = core::rect<s32>(
445 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
446 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
447 pos.Y + ((imgsize.Y/2) + m_btn_height));
451 wlabel, //Needed for displaying text on MSVC
456 spec.ftype = f_CheckBox;
458 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
459 spec.fid, spec.flabel.c_str());
461 if (spec.fname == data->focused_fieldname) {
462 Environment->setFocus(e);
465 m_checkboxes.emplace_back(spec,e);
466 m_fields.push_back(spec);
469 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
472 void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
474 std::vector<std::string> parts = split(element,';');
476 if (parts.size() >= 5) {
477 std::vector<std::string> v_pos = split(parts[0],',');
478 std::vector<std::string> v_dim = split(parts[1],',');
479 std::string name = parts[3];
480 std::string value = parts[4];
482 MY_CHECKPOS("scrollbar",0);
484 v2s32 pos = getElementBasePos(false, &v_pos);
486 if (v_dim.size() != 2) {
487 errorstream<< "Invalid size for element " << "scrollbar"
488 << "specified: \"" << parts[1] << "\"" << std::endl;
493 dim.X = stof(v_dim[0]) * spacing.X;
494 dim.Y = stof(v_dim[1]) * spacing.Y;
496 core::rect<s32> rect =
497 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
506 bool is_horizontal = true;
508 if (parts[2] == "vertical")
509 is_horizontal = false;
511 spec.ftype = f_ScrollBar;
513 gui::IGUIScrollBar* e =
514 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
518 e->setPos(stoi(parts[4]));
520 e->setLargeStep(100);
522 m_scrollbars.emplace_back(spec,e);
523 m_fields.push_back(spec);
526 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
529 void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
531 std::vector<std::string> parts = split(element,';');
533 if ((parts.size() == 3) ||
534 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
536 std::vector<std::string> v_pos = split(parts[0],',');
537 std::vector<std::string> v_geom = split(parts[1],',');
538 std::string name = unescape_string(parts[2]);
540 MY_CHECKPOS("image", 0);
541 MY_CHECKGEOM("image", 1);
543 v2s32 pos = getElementBasePos(true, &v_pos);
545 geom.X = stof(v_geom[0]) * (float)imgsize.X;
546 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
548 if (!data->explicit_size)
549 warningstream<<"invalid use of image without a size[] element"<<std::endl;
550 m_images.emplace_back(name, pos, geom);
554 if (parts.size() == 2) {
555 std::vector<std::string> v_pos = split(parts[0],',');
556 std::string name = unescape_string(parts[1]);
558 MY_CHECKPOS("image", 0);
560 v2s32 pos = getElementBasePos(true, &v_pos);
562 if (!data->explicit_size)
563 warningstream<<"invalid use of image without a size[] element"<<std::endl;
564 m_images.emplace_back(name, pos);
567 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
570 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
572 std::vector<std::string> parts = split(element,';');
574 if ((parts.size() == 3) ||
575 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
577 std::vector<std::string> v_pos = split(parts[0],',');
578 std::vector<std::string> v_geom = split(parts[1],',');
579 std::string name = parts[2];
581 MY_CHECKPOS("itemimage",0);
582 MY_CHECKGEOM("itemimage",1);
584 v2s32 pos = getElementBasePos(true, &v_pos);
586 geom.X = stof(v_geom[0]) * (float)imgsize.X;
587 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
589 if(!data->explicit_size)
590 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
591 m_itemimages.emplace_back("", name, pos, geom);
594 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
597 void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
598 const std::string &type)
600 std::vector<std::string> parts = split(element,';');
602 if ((parts.size() == 4) ||
603 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
605 std::vector<std::string> v_pos = split(parts[0],',');
606 std::vector<std::string> v_geom = split(parts[1],',');
607 std::string name = parts[2];
608 std::string label = parts[3];
610 MY_CHECKPOS("button",0);
611 MY_CHECKGEOM("button",1);
613 v2s32 pos = getElementBasePos(false, &v_pos);
615 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
616 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
618 core::rect<s32> rect =
619 core::rect<s32>(pos.X, pos.Y - m_btn_height,
620 pos.X + geom.X, pos.Y + m_btn_height);
622 if(!data->explicit_size)
623 warningstream<<"invalid use of button without a size[] element"<<std::endl;
625 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
633 spec.ftype = f_Button;
634 if(type == "button_exit")
636 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
637 spec.flabel.c_str());
639 if (spec.fname == data->focused_fieldname) {
640 Environment->setFocus(e);
643 m_fields.push_back(spec);
646 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
649 void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
651 std::vector<std::string> parts = split(element,';');
653 if (((parts.size() == 3) || (parts.size() == 4)) ||
654 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
656 std::vector<std::string> v_pos = split(parts[0],',');
657 std::vector<std::string> v_geom = split(parts[1],',');
658 std::string name = unescape_string(parts[2]);
660 MY_CHECKPOS("background",0);
661 MY_CHECKGEOM("background",1);
663 v2s32 pos = getElementBasePos(true, &v_pos);
664 pos.X -= (spacing.X - (float)imgsize.X) / 2;
665 pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
668 geom.X = stof(v_geom[0]) * spacing.X;
669 geom.Y = stof(v_geom[1]) * spacing.Y;
672 if (parts.size() == 4 && is_yes(parts[3])) {
673 pos.X = stoi(v_pos[0]); //acts as offset
674 pos.Y = stoi(v_pos[1]); //acts as offset
678 if (!data->explicit_size && !clip)
679 warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
681 m_backgrounds.emplace_back(name, pos, geom, clip);
685 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
688 void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
690 std::vector<std::string> parts = split(element,';');
692 data->table_options.clear();
693 for (const std::string &part : parts) {
694 // Parse table option
695 std::string opt = unescape_string(part);
696 data->table_options.push_back(GUITable::splitOption(opt));
700 void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
702 std::vector<std::string> parts = split(element,';');
704 data->table_columns.clear();
705 for (const std::string &part : parts) {
706 std::vector<std::string> col_parts = split(part,',');
707 GUITable::TableColumn column;
709 if (!col_parts.empty())
710 column.type = col_parts[0];
711 // Parse column options
712 for (size_t j = 1; j < col_parts.size(); ++j) {
713 std::string opt = unescape_string(col_parts[j]);
714 column.options.push_back(GUITable::splitOption(opt));
716 data->table_columns.push_back(column);
720 void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
722 std::vector<std::string> parts = split(element,';');
724 if (((parts.size() == 4) || (parts.size() == 5)) ||
725 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
727 std::vector<std::string> v_pos = split(parts[0],',');
728 std::vector<std::string> v_geom = split(parts[1],',');
729 std::string name = parts[2];
730 std::vector<std::string> items = split(parts[3],',');
731 std::string str_initial_selection;
732 std::string str_transparent = "false";
734 if (parts.size() >= 5)
735 str_initial_selection = parts[4];
737 MY_CHECKPOS("table",0);
738 MY_CHECKGEOM("table",1);
740 v2s32 pos = getElementBasePos(false, &v_pos);
742 geom.X = stof(v_geom[0]) * spacing.X;
743 geom.Y = stof(v_geom[1]) * spacing.Y;
745 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
754 spec.ftype = f_Table;
756 for (std::string &item : items) {
757 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
760 //now really show table
761 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
764 if (spec.fname == data->focused_fieldname) {
765 Environment->setFocus(e);
768 e->setTable(data->table_options, data->table_columns, items);
770 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
771 e->setDynamicData(data->table_dyndata[name]);
774 if (!str_initial_selection.empty() && str_initial_selection != "0")
775 e->setSelected(stoi(str_initial_selection));
777 m_tables.emplace_back(spec, e);
778 m_fields.push_back(spec);
781 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
784 void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
786 std::vector<std::string> parts = split(element,';');
788 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
789 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
791 std::vector<std::string> v_pos = split(parts[0],',');
792 std::vector<std::string> v_geom = split(parts[1],',');
793 std::string name = parts[2];
794 std::vector<std::string> items = split(parts[3],',');
795 std::string str_initial_selection;
796 std::string str_transparent = "false";
798 if (parts.size() >= 5)
799 str_initial_selection = parts[4];
801 if (parts.size() >= 6)
802 str_transparent = parts[5];
804 MY_CHECKPOS("textlist",0);
805 MY_CHECKGEOM("textlist",1);
807 v2s32 pos = getElementBasePos(false, &v_pos);
809 geom.X = stof(v_geom[0]) * spacing.X;
810 geom.Y = stof(v_geom[1]) * spacing.Y;
813 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
822 spec.ftype = f_Table;
824 for (std::string &item : items) {
825 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
828 //now really show list
829 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
832 if (spec.fname == data->focused_fieldname) {
833 Environment->setFocus(e);
836 e->setTextList(items, is_yes(str_transparent));
838 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
839 e->setDynamicData(data->table_dyndata[name]);
842 if (!str_initial_selection.empty() && str_initial_selection != "0")
843 e->setSelected(stoi(str_initial_selection));
845 m_tables.emplace_back(spec, e);
846 m_fields.push_back(spec);
849 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
853 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
855 std::vector<std::string> parts = split(element,';');
857 if ((parts.size() == 5) ||
858 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
860 std::vector<std::string> v_pos = split(parts[0],',');
861 std::string name = parts[2];
862 std::vector<std::string> items = split(parts[3],',');
863 std::string str_initial_selection;
864 str_initial_selection = parts[4];
866 MY_CHECKPOS("dropdown",0);
868 v2s32 pos = getElementBasePos(false, &v_pos);
870 s32 width = stof(parts[1]) * spacing.Y;
872 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
873 pos.X + width, pos.Y + (m_btn_height * 2));
882 spec.ftype = f_DropDown;
885 //now really show list
886 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
888 if (spec.fname == data->focused_fieldname) {
889 Environment->setFocus(e);
892 for (const std::string &item : items) {
893 e->addItem(unescape_translate(unescape_string(
894 utf8_to_wide(item))).c_str());
897 if (!str_initial_selection.empty())
898 e->setSelected(stoi(str_initial_selection)-1);
900 m_fields.push_back(spec);
902 m_dropdowns.emplace_back(spec, std::vector<std::string>());
903 std::vector<std::string> &values = m_dropdowns.back().second;
904 for (const std::string &item : items) {
905 values.push_back(unescape_string(item));
910 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
911 << element << "'" << std::endl;
914 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
916 std::vector<std::string> parts = split(element,';');
917 if (parts.size() == 2 ||
918 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
919 field_close_on_enter[parts[0]] = is_yes(parts[1]);
923 void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
925 std::vector<std::string> parts = split(element,';');
927 if ((parts.size() == 4) || (parts.size() == 5) ||
928 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
930 std::vector<std::string> v_pos = split(parts[0],',');
931 std::vector<std::string> v_geom = split(parts[1],',');
932 std::string name = parts[2];
933 std::string label = parts[3];
935 MY_CHECKPOS("pwdfield",0);
936 MY_CHECKGEOM("pwdfield",1);
938 v2s32 pos = getElementBasePos(false, &v_pos);
942 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
944 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
945 pos.Y -= m_btn_height;
946 geom.Y = m_btn_height*2;
948 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
950 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
960 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
962 if (spec.fname == data->focused_fieldname) {
963 Environment->setFocus(e);
966 if (label.length() >= 1) {
967 int font_height = g_fontengine->getTextHeight();
968 rect.UpperLeftCorner.Y -= font_height;
969 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
970 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
974 e->setPasswordBox(true,L'*');
977 evt.EventType = EET_KEY_INPUT_EVENT;
978 evt.KeyInput.Key = KEY_END;
979 evt.KeyInput.Char = 0;
980 evt.KeyInput.Control = false;
981 evt.KeyInput.Shift = false;
982 evt.KeyInput.PressedDown = true;
985 if (parts.size() >= 5) {
986 // TODO: remove after 2016-11-03
987 warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
988 " instead of the 5th param" << std::endl;
989 field_close_on_enter[name] = is_yes(parts[4]);
992 m_fields.push_back(spec);
995 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
998 void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
999 core::rect<s32> &rect, bool is_multiline)
1001 bool is_editable = !spec.fname.empty();
1002 if (!is_editable && !is_multiline) {
1003 // spec field id to 0, this stops submit searching for a value that isn't there
1004 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1011 } else if (is_multiline &&
1012 spec.fdefault.empty() && !spec.flabel.empty()) {
1013 // Multiline textareas: swap default and label for backwards compat
1014 spec.flabel.swap(spec.fdefault);
1017 gui::IGUIEditBox *e = nullptr;
1018 static constexpr bool use_intl_edit_box = USE_FREETYPE &&
1019 IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9;
1021 if (use_intl_edit_box && g_settings->getBool("freetype")) {
1022 e = new gui::intlGUIEditBox(spec.fdefault.c_str(),
1023 true, Environment, this, spec.fid, rect, is_editable, is_multiline);
1027 e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true,
1028 Environment, this, spec.fid, rect, is_editable, true);
1029 else if (is_editable)
1030 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
1035 if (is_editable && spec.fname == data->focused_fieldname)
1036 Environment->setFocus(e);
1039 e->setMultiLine(true);
1040 e->setWordWrap(true);
1041 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1044 evt.EventType = EET_KEY_INPUT_EVENT;
1045 evt.KeyInput.Key = KEY_END;
1046 evt.KeyInput.Char = 0;
1047 evt.KeyInput.Control = 0;
1048 evt.KeyInput.Shift = 0;
1049 evt.KeyInput.PressedDown = true;
1054 if (!spec.flabel.empty()) {
1055 int font_height = g_fontengine->getTextHeight();
1056 rect.UpperLeftCorner.Y -= font_height;
1057 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1058 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1063 void GUIFormSpecMenu::parseSimpleField(parserData* data,
1064 std::vector<std::string> &parts)
1066 std::string name = parts[0];
1067 std::string label = parts[1];
1068 std::string default_val = parts[2];
1070 core::rect<s32> rect;
1072 if(data->explicit_size)
1073 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
1075 v2s32 pos = getElementBasePos(false, nullptr);
1076 pos.Y = ((m_fields.size()+2)*60);
1077 v2s32 size = DesiredRect.getSize();
1079 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
1080 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
1084 default_val = m_form_src->resolveText(default_val);
1087 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1092 utf8_to_wide(unescape_string(default_val)),
1096 createTextField(data, spec, rect, false);
1098 if (parts.size() >= 4) {
1099 // TODO: remove after 2016-11-03
1100 warningstream << "field/simple: use field_close_on_enter[name, enabled]" <<
1101 " instead of the 4th param" << std::endl;
1102 field_close_on_enter[name] = is_yes(parts[3]);
1105 m_fields.push_back(spec);
1108 void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
1109 const std::string &type)
1112 std::vector<std::string> v_pos = split(parts[0],',');
1113 std::vector<std::string> v_geom = split(parts[1],',');
1114 std::string name = parts[2];
1115 std::string label = parts[3];
1116 std::string default_val = parts[4];
1118 MY_CHECKPOS(type,0);
1119 MY_CHECKGEOM(type,1);
1121 v2s32 pos = getElementBasePos(false, &v_pos);
1126 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1128 if (type == "textarea")
1130 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1131 pos.Y += m_btn_height;
1135 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1136 pos.Y -= m_btn_height;
1137 geom.Y = m_btn_height*2;
1140 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1142 if(!data->explicit_size)
1143 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1146 default_val = m_form_src->resolveText(default_val);
1149 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1154 utf8_to_wide(unescape_string(default_val)),
1158 createTextField(data, spec, rect, type == "textarea");
1160 if (parts.size() >= 6) {
1161 // TODO: remove after 2016-11-03
1162 warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
1163 " instead of the 6th param" << std::endl;
1164 field_close_on_enter[name] = is_yes(parts[5]);
1167 m_fields.push_back(spec);
1170 void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
1171 const std::string &type)
1173 std::vector<std::string> parts = split(element,';');
1175 if (parts.size() == 3 || parts.size() == 4) {
1176 parseSimpleField(data,parts);
1180 if ((parts.size() == 5) || (parts.size() == 6) ||
1181 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1183 parseTextArea(data,parts,type);
1186 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1189 void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
1191 std::vector<std::string> parts = split(element,';');
1193 if ((parts.size() == 2) ||
1194 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1196 std::vector<std::string> v_pos = split(parts[0],',');
1197 std::string text = parts[1];
1199 MY_CHECKPOS("label",0);
1201 v2s32 pos = getElementBasePos(false, nullptr);
1202 pos.X += stof(v_pos[0]) * spacing.X;
1203 pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
1205 if(!data->explicit_size)
1206 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1208 std::vector<std::string> lines = split(text, '\n');
1210 for (unsigned int i = 0; i != lines.size(); i++) {
1211 // Lines are spaced at the nominal distance of
1212 // 2/5 inventory slot, even if the font doesn't
1213 // quite match that. This provides consistent
1214 // form layout, at the expense of sometimes
1215 // having sub-optimal spacing for the font.
1216 // We multiply by 2 and then divide by 5, rather
1217 // than multiply by 0.4, to get exact results
1218 // in the integer cases: 0.4 is not exactly
1219 // representable in binary floating point.
1220 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1221 std::wstring wlabel = utf8_to_wide(unescape_string(lines[i]));
1222 core::rect<s32> rect = core::rect<s32>(
1223 pos.X, posy - m_btn_height,
1224 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1225 posy + m_btn_height);
1232 gui::IGUIStaticText *e = gui::StaticText::add(Environment,
1233 spec.flabel.c_str(), rect, false, false, this, spec.fid);
1234 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
1235 m_fields.push_back(spec);
1240 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1243 void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
1245 std::vector<std::string> parts = split(element,';');
1247 if ((parts.size() == 2) ||
1248 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1250 std::vector<std::string> v_pos = split(parts[0],',');
1251 std::wstring text = unescape_translate(
1252 unescape_string(utf8_to_wide(parts[1])));
1254 MY_CHECKPOS("vertlabel",1);
1256 v2s32 pos = getElementBasePos(false, &v_pos);
1258 core::rect<s32> rect = core::rect<s32>(
1259 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1261 font_line_height(m_font)
1263 +((imgsize.Y/2)- m_btn_height));
1264 //actually text.length() would be correct but adding +1 avoids to break all mods
1266 if(!data->explicit_size)
1267 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1271 for (wchar_t i : text) {
1282 gui::IGUIStaticText *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
1283 rect, false, false, this, spec.fid);
1284 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1285 m_fields.push_back(spec);
1288 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1291 void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
1292 const std::string &type)
1294 std::vector<std::string> parts = split(element,';');
1296 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1297 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1299 std::vector<std::string> v_pos = split(parts[0],',');
1300 std::vector<std::string> v_geom = split(parts[1],',');
1301 std::string image_name = parts[2];
1302 std::string name = parts[3];
1303 std::string label = parts[4];
1305 MY_CHECKPOS("imagebutton",0);
1306 MY_CHECKGEOM("imagebutton",1);
1308 v2s32 pos = getElementBasePos(false, &v_pos);
1310 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1311 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1313 bool noclip = false;
1314 bool drawborder = true;
1315 std::string pressed_image_name;
1317 if (parts.size() >= 7) {
1318 if (parts[5] == "true")
1320 if (parts[6] == "false")
1324 if (parts.size() >= 8) {
1325 pressed_image_name = parts[7];
1328 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1330 if(!data->explicit_size)
1331 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1333 image_name = unescape_string(image_name);
1334 pressed_image_name = unescape_string(pressed_image_name);
1336 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1341 utf8_to_wide(image_name),
1344 spec.ftype = f_Button;
1345 if(type == "image_button_exit")
1346 spec.is_exit = true;
1348 video::ITexture *texture = 0;
1349 video::ITexture *pressed_texture = 0;
1350 texture = m_tsrc->getTexture(image_name);
1351 if (!pressed_image_name.empty())
1352 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1354 pressed_texture = texture;
1356 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1358 if (spec.fname == data->focused_fieldname) {
1359 Environment->setFocus(e);
1362 e->setUseAlphaChannel(true);
1363 e->setImage(guiScalingImageButton(
1364 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1365 e->setPressedImage(guiScalingImageButton(
1366 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1367 e->setScaleImage(true);
1368 e->setNotClipped(noclip);
1369 e->setDrawBorder(drawborder);
1371 m_fields.push_back(spec);
1375 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1378 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
1380 std::vector<std::string> parts = split(element,';');
1382 if (((parts.size() == 4) || (parts.size() == 6)) ||
1383 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1385 std::vector<std::string> v_pos = split(parts[0],',');
1386 std::string name = parts[1];
1387 std::vector<std::string> buttons = split(parts[2],',');
1388 std::string str_index = parts[3];
1389 bool show_background = true;
1390 bool show_border = true;
1391 int tab_index = stoi(str_index) -1;
1393 MY_CHECKPOS("tabheader",0);
1395 if (parts.size() == 6) {
1396 if (parts[4] == "true")
1397 show_background = false;
1398 if (parts[5] == "false")
1399 show_border = false;
1409 spec.ftype = f_TabHeader;
1413 v2f32 pos_f = pos_offset * spacing;
1414 pos_f.X += stof(v_pos[0]) * spacing.X;
1415 pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
1416 pos = v2s32(pos_f.X, pos_f.Y);
1419 geom.X = DesiredRect.getWidth();
1420 geom.Y = m_btn_height*2;
1422 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1425 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1426 show_background, show_border, spec.fid);
1427 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1428 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1429 e->setTabHeight(m_btn_height*2);
1431 if (spec.fname == data->focused_fieldname) {
1432 Environment->setFocus(e);
1435 e->setNotClipped(true);
1437 for (const std::string &button : buttons) {
1438 e->addTab(unescape_translate(unescape_string(
1439 utf8_to_wide(button))).c_str(), -1);
1442 if ((tab_index >= 0) &&
1443 (buttons.size() < INT_MAX) &&
1444 (tab_index < (int) buttons.size()))
1445 e->setActiveTab(tab_index);
1447 m_fields.push_back(spec);
1450 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1451 << element << "'" << std::endl;
1454 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
1457 if (m_client == 0) {
1458 warningstream << "invalid use of item_image_button with m_client==0"
1463 std::vector<std::string> parts = split(element,';');
1465 if ((parts.size() == 5) ||
1466 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1468 std::vector<std::string> v_pos = split(parts[0],',');
1469 std::vector<std::string> v_geom = split(parts[1],',');
1470 std::string item_name = parts[2];
1471 std::string name = parts[3];
1472 std::string label = parts[4];
1474 label = unescape_string(label);
1475 item_name = unescape_string(item_name);
1477 MY_CHECKPOS("itemimagebutton",0);
1478 MY_CHECKGEOM("itemimagebutton",1);
1480 v2s32 pos = getElementBasePos(false, &v_pos);
1482 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1483 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1485 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1487 if(!data->explicit_size)
1488 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1490 IItemDefManager *idef = m_client->idef();
1492 item.deSerialize(item_name, idef);
1495 TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
1496 m_default_tooltip_bgcolor,
1497 m_default_tooltip_color);
1501 utf8_to_wide(label),
1502 utf8_to_wide(item_name),
1503 258 + m_fields.size()
1506 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1508 if (spec.fname == data->focused_fieldname) {
1509 Environment->setFocus(e);
1512 spec.ftype = f_Button;
1513 rect+=data->basepos-padding;
1515 m_fields.push_back(spec);
1517 pos = getElementBasePos(true, &v_pos);
1518 m_itemimages.emplace_back("", item_name, e, pos, geom);
1519 m_static_texts.emplace_back(utf8_to_wide(label), rect, e);
1522 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1525 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
1527 std::vector<std::string> parts = split(element,';');
1529 if ((parts.size() == 3) ||
1530 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1532 std::vector<std::string> v_pos = split(parts[0],',');
1533 std::vector<std::string> v_geom = split(parts[1],',');
1535 MY_CHECKPOS("box",0);
1536 MY_CHECKGEOM("box",1);
1538 v2s32 pos = getElementBasePos(true, &v_pos);
1540 geom.X = stof(v_geom[0]) * spacing.X;
1541 geom.Y = stof(v_geom[1]) * spacing.Y;
1543 video::SColor tmp_color;
1545 if (parseColorString(parts[2], tmp_color, false, 0x8C)) {
1546 BoxDrawSpec spec(pos, geom, tmp_color);
1548 m_boxes.push_back(spec);
1551 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1555 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1558 void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
1560 std::vector<std::string> parts = split(element,';');
1562 if (((parts.size() == 1) || (parts.size() == 2)) ||
1563 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION))) {
1564 parseColorString(parts[0], m_bgcolor, false);
1566 if (parts.size() == 2) {
1567 std::string fullscreen = parts[1];
1568 m_bgfullscreen = is_yes(fullscreen);
1574 errorstream << "Invalid bgcolor element(" << parts.size() << "): '" << element << "'"
1578 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
1580 std::vector<std::string> parts = split(element,';');
1582 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1583 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1585 parseColorString(parts[0], m_slotbg_n, false);
1586 parseColorString(parts[1], m_slotbg_h, false);
1588 if (parts.size() >= 3) {
1589 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1590 m_slotborder = true;
1593 if (parts.size() == 5) {
1594 video::SColor tmp_color;
1596 if (parseColorString(parts[3], tmp_color, false))
1597 m_default_tooltip_bgcolor = tmp_color;
1598 if (parseColorString(parts[4], tmp_color, false))
1599 m_default_tooltip_color = tmp_color;
1603 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1606 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
1608 std::vector<std::string> parts = split(element,';');
1609 if (parts.size() < 2) {
1610 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
1611 << element << "'" << std::endl;
1615 // Get mode and check size
1616 bool rect_mode = parts[0].find(',') != std::string::npos;
1617 size_t base_size = rect_mode ? 3 : 2;
1618 if (parts.size() != base_size && parts.size() != base_size + 2) {
1619 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
1620 << element << "'" << std::endl;
1625 video::SColor bgcolor = m_default_tooltip_bgcolor;
1626 video::SColor color = m_default_tooltip_color;
1627 if (parts.size() == base_size + 2 &&
1628 (!parseColorString(parts[base_size], bgcolor, false) ||
1629 !parseColorString(parts[base_size + 1], color, false))) {
1630 errorstream << "Invalid color in tooltip element(" << parts.size()
1631 << "): '" << element << "'" << std::endl;
1635 // Make tooltip spec
1636 std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
1637 TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
1641 std::vector<std::string> v_pos = split(parts[0], ',');
1642 std::vector<std::string> v_geom = split(parts[1], ',');
1644 MY_CHECKPOS("tooltip", 0);
1645 MY_CHECKGEOM("tooltip", 1);
1647 v2s32 pos = getElementBasePos(true, &v_pos);
1649 geom.X = stof(v_geom[0]) * spacing.X;
1650 geom.Y = stof(v_geom[1]) * spacing.Y;
1652 irr::core::rect<s32> rect(pos, pos + geom);
1653 m_tooltip_rects.emplace_back(rect, spec);
1655 m_tooltips[parts[0]] = spec;
1659 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
1665 std::vector<std::string> parts = split(data,'[');
1667 if (parts.size() < 2) {
1671 if (parts[0] != "formspec_version") {
1675 if (is_number(parts[1])) {
1676 m_formspec_version = mystoi(parts[1]);
1683 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
1685 if (element.empty())
1688 std::vector<std::string> parts = split(element,'[');
1690 if (parts.size() < 2)
1693 std::string type = trim(parts[0]);
1694 std::string description = trim(parts[1]);
1696 if (type != "size" && type != "invsize")
1699 if (type == "invsize")
1700 log_deprecated("Deprecated formspec element \"invsize\" is used");
1702 parseSize(data, description);
1707 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
1709 if (element.empty())
1712 std::vector<std::string> parts = split(element, '[');
1714 if (parts.size() != 2)
1717 std::string type = trim(parts[0]);
1718 std::string description = trim(parts[1]);
1720 if (type != "position")
1723 parsePosition(data, description);
1728 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
1730 std::vector<std::string> parts = split(element, ',');
1732 if (parts.size() == 2) {
1733 data->offset.X = stof(parts[0]);
1734 data->offset.Y = stof(parts[1]);
1738 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
1741 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
1743 if (element.empty())
1746 std::vector<std::string> parts = split(element, '[');
1748 if (parts.size() != 2)
1751 std::string type = trim(parts[0]);
1752 std::string description = trim(parts[1]);
1754 if (type != "anchor")
1757 parseAnchor(data, description);
1762 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
1764 std::vector<std::string> parts = split(element, ',');
1766 if (parts.size() == 2) {
1767 data->anchor.X = stof(parts[0]);
1768 data->anchor.Y = stof(parts[1]);
1772 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
1773 << "'" << std::endl;
1776 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
1779 if (element.empty())
1782 std::vector<std::string> parts = split(element,'[');
1784 // ugly workaround to keep compatibility
1785 if (parts.size() > 2) {
1786 if (trim(parts[0]) == "image") {
1787 for (unsigned int i=2;i< parts.size(); i++) {
1788 parts[1] += "[" + parts[i];
1794 if (parts.size() < 2) {
1798 std::string type = trim(parts[0]);
1799 std::string description = trim(parts[1]);
1801 if (type == "container") {
1802 parseContainer(data, description);
1806 if (type == "container_end") {
1807 parseContainerEnd(data);
1811 if (type == "list") {
1812 parseList(data, description);
1816 if (type == "listring") {
1817 parseListRing(data, description);
1821 if (type == "checkbox") {
1822 parseCheckbox(data, description);
1826 if (type == "image") {
1827 parseImage(data, description);
1831 if (type == "item_image") {
1832 parseItemImage(data, description);
1836 if (type == "button" || type == "button_exit") {
1837 parseButton(data, description, type);
1841 if (type == "background") {
1842 parseBackground(data,description);
1846 if (type == "tableoptions"){
1847 parseTableOptions(data,description);
1851 if (type == "tablecolumns"){
1852 parseTableColumns(data,description);
1856 if (type == "table"){
1857 parseTable(data,description);
1861 if (type == "textlist"){
1862 parseTextList(data,description);
1866 if (type == "dropdown"){
1867 parseDropDown(data,description);
1871 if (type == "field_close_on_enter") {
1872 parseFieldCloseOnEnter(data, description);
1876 if (type == "pwdfield") {
1877 parsePwdField(data,description);
1881 if ((type == "field") || (type == "textarea")){
1882 parseField(data,description,type);
1886 if (type == "label") {
1887 parseLabel(data,description);
1891 if (type == "vertlabel") {
1892 parseVertLabel(data,description);
1896 if (type == "item_image_button") {
1897 parseItemImageButton(data,description);
1901 if ((type == "image_button") || (type == "image_button_exit")) {
1902 parseImageButton(data,description,type);
1906 if (type == "tabheader") {
1907 parseTabHeader(data,description);
1911 if (type == "box") {
1912 parseBox(data,description);
1916 if (type == "bgcolor") {
1917 parseBackgroundColor(data,description);
1921 if (type == "listcolors") {
1922 parseListColors(data,description);
1926 if (type == "tooltip") {
1927 parseTooltip(data,description);
1931 if (type == "scrollbar") {
1932 parseScrollBar(data, description);
1937 infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
1941 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1943 /* useless to regenerate without a screensize */
1944 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1951 for (auto &m_table : m_tables) {
1952 std::string tablename = m_table.first.fname;
1953 GUITable *table = m_table.second;
1954 mydata.table_dyndata[tablename] = table->getDynamicData();
1958 if (!m_focused_element.empty())
1959 mydata.focused_fieldname = m_focused_element;
1962 gui::IGUIElement *focused_element = Environment->getFocus();
1963 if (focused_element && focused_element->getParent() == this) {
1964 s32 focused_id = focused_element->getID();
1965 if (focused_id > 257) {
1966 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
1967 if (field.fid == focused_id) {
1968 mydata.focused_fieldname = field.fname;
1978 for (auto &table_it : m_tables) {
1979 table_it.second->drop();
1982 mydata.size= v2s32(100,100);
1983 mydata.screensize = screensize;
1984 mydata.offset = v2f32(0.5f, 0.5f);
1985 mydata.anchor = v2f32(0.5f, 0.5f);
1987 // Base position of contents of form
1988 mydata.basepos = getBasePos();
1990 /* Convert m_init_draw_spec to m_inventorylists */
1992 m_inventorylists.clear();
1994 m_backgrounds.clear();
1995 m_itemimages.clear();
1997 m_checkboxes.clear();
1998 m_scrollbars.clear();
2002 m_tooltip_rects.clear();
2003 m_inventory_rings.clear();
2004 m_static_texts.clear();
2005 m_dropdowns.clear();
2007 m_bgfullscreen = false;
2010 v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
2011 m_bgcolor = video::SColor(
2012 (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
2013 clamp_u8(myround(formspec_bgcolor.X)),
2014 clamp_u8(myround(formspec_bgcolor.Y)),
2015 clamp_u8(myround(formspec_bgcolor.Z))
2020 v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
2021 m_fullscreen_bgcolor = video::SColor(
2022 (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
2023 clamp_u8(myround(formspec_bgcolor.X)),
2024 clamp_u8(myround(formspec_bgcolor.Y)),
2025 clamp_u8(myround(formspec_bgcolor.Z))
2029 m_slotbg_n = video::SColor(255,128,128,128);
2030 m_slotbg_h = video::SColor(255,192,192,192);
2032 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
2033 m_default_tooltip_color = video::SColor(255,255,255,255);
2035 m_slotbordercolor = video::SColor(200,0,0,0);
2036 m_slotborder = false;
2040 assert(!m_tooltip_element);
2041 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
2042 m_tooltip_element = gui::StaticText::add(Environment, L"",
2043 core::rect<s32>(0, 0, 110, 18));
2044 m_tooltip_element->enableOverrideColor(true);
2045 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2046 m_tooltip_element->setDrawBackground(true);
2047 m_tooltip_element->setDrawBorder(true);
2048 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2049 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
2050 m_tooltip_element->setWordWrap(false);
2051 //we're not parent so no autograb for this one!
2052 m_tooltip_element->grab();
2055 std::vector<std::string> elements = split(m_formspec_string,']');
2058 /* try to read version from first element only */
2059 if (!elements.empty()) {
2060 if (parseVersionDirect(elements[0])) {
2065 /* we need size first in order to calculate image scale */
2066 mydata.explicit_size = false;
2067 for (; i< elements.size(); i++) {
2068 if (!parseSizeDirect(&mydata, elements[i])) {
2073 /* "position" element is always after "size" element if it used */
2074 for (; i< elements.size(); i++) {
2075 if (!parsePositionDirect(&mydata, elements[i])) {
2080 /* "anchor" element is always after "position" (or "size" element) if it used */
2081 for (; i< elements.size(); i++) {
2082 if (!parseAnchorDirect(&mydata, elements[i])) {
2087 /* "no_prepend" element is always after "position" (or "size" element) if it used */
2088 bool enable_prepends = true;
2089 for (; i < elements.size(); i++) {
2090 if (elements[i].empty())
2093 std::vector<std::string> parts = split(elements[i], '[');
2094 if (trim(parts[0]) == "no_prepend")
2095 enable_prepends = false;
2100 if (mydata.explicit_size) {
2101 // compute scaling for specified form size
2103 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
2104 v2u32 delta = current_screensize - m_lockscreensize;
2106 if (current_screensize.Y > m_lockscreensize.Y)
2111 if (current_screensize.X > m_lockscreensize.X)
2116 offset = v2s32(delta.X,delta.Y);
2118 mydata.screensize = m_lockscreensize;
2120 offset = v2s32(0,0);
2123 double gui_scaling = g_settings->getFloat("gui_scaling");
2124 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2128 // In fixed-size mode, inventory image size
2129 // is 0.53 inch multiplied by the gui_scaling
2130 // config parameter. This magic size is chosen
2131 // to make the main menu (15.5 inventory images
2132 // wide, including border) just fit into the
2133 // default window (800 pixels wide) at 96 DPI
2134 // and default scaling (1.00).
2135 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
2137 // In variable-size mode, we prefer to make the
2138 // inventory image size 1/15 of screen height,
2139 // multiplied by the gui_scaling config parameter.
2140 // If the preferred size won't fit the whole
2141 // form on the screen, either horizontally or
2142 // vertically, then we scale it down to fit.
2143 // (The magic numbers in the computation of what
2144 // fits arise from the scaling factors in the
2145 // following stanza, including the form border,
2146 // help text space, and 0.1 inventory slot spare.)
2147 // However, a minimum size is also set, that
2148 // the image size can't be less than 0.3 inch
2149 // multiplied by gui_scaling, even if this means
2150 // the form doesn't fit the screen.
2151 double prefer_imgsize = mydata.screensize.Y / 15 *
2153 double fitx_imgsize = mydata.screensize.X /
2154 ((5.0/4.0) * (0.5 + mydata.invsize.X));
2155 double fity_imgsize = mydata.screensize.Y /
2156 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
2157 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2158 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
2159 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2160 MYMIN(fitx_imgsize, fity_imgsize)));
2163 // Everything else is scaled in proportion to the
2164 // inventory image size. The inventory slot spacing
2165 // is 5/4 image size horizontally and 15/13 image size
2166 // vertically. The padding around the form (incorporating
2167 // the border of the outer inventory slots) is 3/8
2168 // image size. Font height (baseline to baseline)
2169 // is 2/5 vertical inventory slot spacing, and button
2170 // half-height is 7/8 of font height.
2171 imgsize = v2s32(use_imgsize, use_imgsize);
2172 spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2173 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2174 m_btn_height = use_imgsize*15.0/13 * 0.35;
2176 m_font = g_fontengine->getFont();
2178 mydata.size = v2s32(
2179 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2180 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2182 DesiredRect = mydata.rect = core::rect<s32>(
2183 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
2184 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
2185 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
2186 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
2189 // Non-size[] form must consist only of text fields and
2190 // implicit "Proceed" button. Use default font, and
2191 // temporary form size which will be recalculated below.
2192 m_font = g_fontengine->getFont();
2193 m_btn_height = font_line_height(m_font) * 0.875;
2194 DesiredRect = core::rect<s32>(
2195 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
2196 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
2197 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
2198 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
2201 recalculateAbsolutePosition(false);
2202 mydata.basepos = getBasePos();
2203 m_tooltip_element->setOverrideFont(m_font);
2205 gui::IGUISkin *skin = Environment->getSkin();
2207 gui::IGUIFont *old_font = skin->getFont();
2208 skin->setFont(m_font);
2210 pos_offset = v2f32();
2212 if (enable_prepends) {
2213 std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
2214 for (const auto &element : prepend_elements)
2215 parseElement(&mydata, element);
2218 for (; i< elements.size(); i++) {
2219 parseElement(&mydata, elements[i]);
2222 if (!container_stack.empty()) {
2223 errorstream << "Invalid formspec string: container was never closed!"
2227 // If there are fields without explicit size[], add a "Proceed"
2228 // button and adjust size to fit all the fields.
2229 if (!m_fields.empty() && !mydata.explicit_size) {
2230 mydata.rect = core::rect<s32>(
2231 mydata.screensize.X/2 - 580/2,
2232 mydata.screensize.Y/2 - 300/2,
2233 mydata.screensize.X/2 + 580/2,
2234 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2236 DesiredRect = mydata.rect;
2237 recalculateAbsolutePosition(false);
2238 mydata.basepos = getBasePos();
2241 v2s32 pos = mydata.basepos;
2242 pos.Y = ((m_fields.size()+2)*60);
2244 v2s32 size = DesiredRect.getSize();
2246 core::rect<s32>(size.X/2-70, pos.Y,
2247 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2248 const wchar_t *text = wgettext("Proceed");
2249 Environment->addButton(mydata.rect, this, 257, text);
2255 //set initial focus if parser didn't set it
2256 focused_element = Environment->getFocus();
2257 if (!focused_element
2258 || !isMyChild(focused_element)
2259 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2262 skin->setFont(old_font);
2266 bool GUIFormSpecMenu::getAndroidUIInput()
2268 /* no dialog shown */
2269 if (m_JavaDialogFieldName == "") {
2274 if (porting::getInputDialogState() == -1) {
2278 std::string fieldname = m_JavaDialogFieldName;
2279 m_JavaDialogFieldName = "";
2281 /* no value abort dialog processing */
2282 if (porting::getInputDialogState() != 0) {
2286 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2287 iter != m_fields.end(); ++iter) {
2289 if (iter->fname != fieldname) {
2292 IGUIElement* tochange = getElementFromId(iter->fid);
2294 if (tochange == 0) {
2298 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2302 std::string text = porting::getInputDialogValue();
2304 ((gui::IGUIEditBox*) tochange)->
2305 setText(utf8_to_wide(text).c_str());
2311 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2313 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2315 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
2316 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2317 s32 item_i = i + s.start_item_i;
2318 s32 x = (i%s.geom.X) * spacing.X;
2319 s32 y = (i/s.geom.X) * spacing.Y;
2321 core::rect<s32> rect = imgrect + s.pos + p0;
2322 if(rect.isPointInside(p))
2324 return ItemSpec(s.inventoryloc, s.listname, item_i);
2329 return ItemSpec(InventoryLocation(), "", -1);
2332 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int layer,
2335 video::IVideoDriver* driver = Environment->getVideoDriver();
2337 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2339 warningstream<<"GUIFormSpecMenu::drawList(): "
2340 <<"The inventory location "
2341 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2345 InventoryList *ilist = inv->getList(s.listname);
2347 warningstream<<"GUIFormSpecMenu::drawList(): "
2348 <<"The inventory list \""<<s.listname<<"\" @ \""
2349 <<s.inventoryloc.dump()<<"\" doesn't exist"
2354 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2356 for (s32 i = 0; i < s.geom.X * s.geom.Y; i++) {
2357 s32 item_i = i + s.start_item_i;
2358 if (item_i >= (s32)ilist->getSize())
2361 s32 x = (i%s.geom.X) * spacing.X;
2362 s32 y = (i/s.geom.X) * spacing.Y;
2364 core::rect<s32> rect = imgrect + s.pos + p;
2365 ItemStack item = ilist->getItem(item_i);
2367 bool selected = m_selected_item
2368 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2369 && m_selected_item->listname == s.listname
2370 && m_selected_item->i == item_i;
2371 bool hovering = rect.isPointInside(m_pointer);
2372 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2373 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2377 item_hovered = true;
2378 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2380 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2384 //Draw inv slot borders
2386 s32 x1 = rect.UpperLeftCorner.X;
2387 s32 y1 = rect.UpperLeftCorner.Y;
2388 s32 x2 = rect.LowerRightCorner.X;
2389 s32 y2 = rect.LowerRightCorner.Y;
2391 driver->draw2DRectangle(m_slotbordercolor,
2392 core::rect<s32>(v2s32(x1 - border, y1 - border),
2393 v2s32(x2 + border, y1)), NULL);
2394 driver->draw2DRectangle(m_slotbordercolor,
2395 core::rect<s32>(v2s32(x1 - border, y2),
2396 v2s32(x2 + border, y2 + border)), NULL);
2397 driver->draw2DRectangle(m_slotbordercolor,
2398 core::rect<s32>(v2s32(x1 - border, y1),
2399 v2s32(x1, y2)), NULL);
2400 driver->draw2DRectangle(m_slotbordercolor,
2401 core::rect<s32>(v2s32(x2, y1),
2402 v2s32(x2 + border, y2)), NULL);
2408 item.takeItem(m_selected_amount);
2410 if (!item.empty()) {
2411 drawItemStack(driver, m_font, item,
2412 rect, &AbsoluteClippingRect, m_client,
2417 std::wstring tooltip_text;
2418 if (hovering && !m_selected_item) {
2419 const std::string &desc = item.metadata.getString("description");
2422 utf8_to_wide(item.getDefinition(m_client->idef()).description);
2424 tooltip_text = utf8_to_wide(desc);
2426 if (!item.name.empty()) {
2427 if (tooltip_text.empty())
2428 tooltip_text = utf8_to_wide(item.name);
2429 if (m_tooltip_append_itemname)
2430 tooltip_text += utf8_to_wide(" [" + item.name + "]");
2433 if (!tooltip_text.empty()) {
2434 showTooltip(tooltip_text, m_default_tooltip_color,
2435 m_default_tooltip_bgcolor);
2441 void GUIFormSpecMenu::drawSelectedItem()
2443 video::IVideoDriver* driver = Environment->getVideoDriver();
2445 if (!m_selected_item) {
2446 drawItemStack(driver, m_font, ItemStack(),
2447 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2448 NULL, m_client, IT_ROT_DRAGGED);
2452 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2454 InventoryList *list = inv->getList(m_selected_item->listname);
2456 ItemStack stack = list->getItem(m_selected_item->i);
2457 stack.count = m_selected_amount;
2459 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2460 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2461 rect.constrainTo(driver->getViewPort());
2462 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
2465 void GUIFormSpecMenu::drawMenu()
2468 const std::string &newform = m_form_src->getForm();
2469 if (newform != m_formspec_string) {
2470 m_formspec_string = newform;
2471 regenerateGui(m_screensize_old);
2475 gui::IGUISkin* skin = Environment->getSkin();
2476 sanity_check(skin != NULL);
2477 gui::IGUIFont *old_font = skin->getFont();
2478 skin->setFont(m_font);
2480 updateSelectedItem();
2482 video::IVideoDriver* driver = Environment->getVideoDriver();
2484 v2u32 screenSize = driver->getScreenSize();
2485 core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
2488 driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
2490 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2492 m_tooltip_element->setVisible(false);
2494 for (const auto &pair : m_tooltip_rects) {
2495 if (pair.first.isPointInside(m_pointer)) {
2496 const std::wstring &text = pair.second.tooltip;
2497 if (!text.empty()) {
2498 showTooltip(text, pair.second.color, pair.second.bgcolor);
2507 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_backgrounds) {
2508 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2511 // Image size on screen
2512 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2513 // Image rectangle on screen
2514 core::rect<s32> rect = imgrect + spec.pos;
2517 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2518 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2519 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2520 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2521 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2524 const video::SColor color(255,255,255,255);
2525 const video::SColor colors[] = {color,color,color,color};
2526 draw2DImageFilterScaled(driver, texture, rect,
2527 core::rect<s32>(core::position2d<s32>(0,0),
2528 core::dimension2di(texture->getOriginalSize())),
2529 NULL/*&AbsoluteClippingRect*/, colors, true);
2531 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2532 errorstream << "\t" << spec.name << std::endl;
2539 for (const GUIFormSpecMenu::BoxDrawSpec &spec : m_boxes) {
2540 irr::video::SColor todraw = spec.color;
2542 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2543 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2545 driver->draw2DRectangle(todraw, rect, 0);
2551 gui::IGUIElement::draw();
2556 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_images) {
2557 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2560 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2561 // Image size on screen
2562 core::rect<s32> imgrect;
2565 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2568 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2570 // Image rectangle on screen
2571 core::rect<s32> rect = imgrect + spec.pos;
2572 const video::SColor color(255,255,255,255);
2573 const video::SColor colors[] = {color,color,color,color};
2574 draw2DImageFilterScaled(driver, texture, rect,
2575 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2576 NULL/*&AbsoluteClippingRect*/, colors, true);
2579 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2580 errorstream << "\t" << spec.name << std::endl;
2587 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_itemimages) {
2591 IItemDefManager *idef = m_client->idef();
2593 item.deSerialize(spec.item_name, idef);
2594 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2595 // Viewport rectangle on screen
2596 core::rect<s32> rect = imgrect + spec.pos;
2597 if (spec.parent_button && spec.parent_button->isPressed()) {
2598 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2599 rect += core::dimension2d<s32>(
2600 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2602 rect += core::dimension2d<s32>(
2603 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2604 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2607 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2608 m_client, IT_ROT_NONE);
2613 Layer 0: Item slot rectangles
2614 Layer 1: Item images; prepare tooltip
2616 bool item_hovered = false;
2617 for (int layer = 0; layer < 2; layer++) {
2618 for (const GUIFormSpecMenu::ListDrawSpec &spec : m_inventorylists) {
2619 drawList(spec, layer, item_hovered);
2622 if (!item_hovered) {
2623 drawItemStack(driver, m_font, ItemStack(),
2624 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2625 NULL, m_client, IT_ROT_HOVERED);
2628 /* TODO find way to show tooltips on touchscreen */
2629 #ifndef HAVE_TOUCHSCREENGUI
2630 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
2634 Draw static text elements
2636 for (const GUIFormSpecMenu::StaticTextSpec &spec : m_static_texts) {
2637 core::rect<s32> rect = spec.rect;
2638 if (spec.parent_button && spec.parent_button->isPressed()) {
2639 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2640 rect += core::dimension2d<s32>(
2641 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2643 // Use image offset instead of text's because its a bit smaller
2644 // and fits better, also TEXT_OFFSET_X is always 0
2645 rect += core::dimension2d<s32>(
2646 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2647 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2650 video::SColor color(255, 255, 255, 255);
2651 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
2655 Draw fields/buttons tooltips
2657 gui::IGUIElement *hovered =
2658 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2660 if (hovered != NULL) {
2661 s32 id = hovered->getID();
2665 m_old_tooltip_id = id;
2667 if (id == m_old_tooltip_id) {
2668 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
2670 m_hovered_time = porting::getTimeMs();
2671 m_old_tooltip_id = id;
2675 // Find and update the current tooltip
2676 if (id != -1 && delta >= m_tooltip_show_delay) {
2677 for (const FieldSpec &field : m_fields) {
2679 if (field.fid != id)
2682 const std::wstring &text = m_tooltips[field.fname].tooltip;
2684 showTooltip(text, m_tooltips[field.fname].color,
2685 m_tooltips[field.fname].bgcolor);
2692 m_tooltip_element->draw();
2695 Draw dragged item stack
2699 skin->setFont(old_font);
2703 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
2704 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
2706 const std::wstring ntext = translate_string(text);
2707 m_tooltip_element->setOverrideColor(color);
2708 m_tooltip_element->setBackgroundColor(bgcolor);
2709 setStaticText(m_tooltip_element, ntext.c_str());
2711 // Tooltip size and offset
2712 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2713 #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
2714 std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
2715 s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
2717 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2719 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
2720 int tooltip_offset_x = m_btn_height;
2721 int tooltip_offset_y = m_btn_height;
2723 tooltip_offset_x *= 3;
2724 tooltip_offset_y = 0;
2725 if (m_pointer.X > (s32)screenSize.X / 2)
2726 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
2729 // Calculate and set the tooltip position
2730 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2731 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2732 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2733 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2734 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2735 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2737 m_tooltip_element->setRelativePosition(
2739 core::position2d<s32>(tooltip_x, tooltip_y),
2740 core::dimension2d<s32>(tooltip_width, tooltip_height)
2744 // Display the tooltip
2745 m_tooltip_element->setVisible(true);
2746 bringToFront(m_tooltip_element);
2749 void GUIFormSpecMenu::updateSelectedItem()
2751 verifySelectedItem();
2753 // If craftresult is nonempty and nothing else is selected, select it now.
2754 if (!m_selected_item) {
2755 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
2756 if (s.listname != "craftpreview")
2759 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2763 InventoryList *list = inv->getList("craftresult");
2765 if (!list || list->getSize() == 0)
2768 const ItemStack &item = list->getItem(0);
2772 // Grab selected item from the crafting result list
2773 m_selected_item = new ItemSpec;
2774 m_selected_item->inventoryloc = s.inventoryloc;
2775 m_selected_item->listname = "craftresult";
2776 m_selected_item->i = 0;
2777 m_selected_amount = item.count;
2778 m_selected_dragging = false;
2783 // If craftresult is selected, keep the whole stack selected
2784 if (m_selected_item && m_selected_item->listname == "craftresult")
2785 m_selected_amount = verifySelectedItem().count;
2788 ItemStack GUIFormSpecMenu::verifySelectedItem()
2790 // If the selected stack has become empty for some reason, deselect it.
2791 // If the selected stack has become inaccessible, deselect it.
2792 // If the selected stack has become smaller, adjust m_selected_amount.
2793 // Return the selected stack.
2797 if(m_selected_item->isValid())
2799 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2802 InventoryList *list = inv->getList(m_selected_item->listname);
2803 if(list && (u32) m_selected_item->i < list->getSize())
2805 ItemStack stack = list->getItem(m_selected_item->i);
2806 if (!m_selected_swap.empty()) {
2807 if (m_selected_swap.name == stack.name &&
2808 m_selected_swap.count == stack.count)
2809 m_selected_swap.clear();
2811 m_selected_amount = std::min(m_selected_amount, stack.count);
2820 // selection was not valid
2821 delete m_selected_item;
2822 m_selected_item = NULL;
2823 m_selected_amount = 0;
2824 m_selected_dragging = false;
2829 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2835 if (quitmode == quit_mode_accept) {
2836 fields["quit"] = "true";
2839 if (quitmode == quit_mode_cancel) {
2840 fields["quit"] = "true";
2841 m_text_dst->gotText(fields);
2845 if (current_keys_pending.key_down) {
2846 fields["key_down"] = "true";
2847 current_keys_pending.key_down = false;
2850 if (current_keys_pending.key_up) {
2851 fields["key_up"] = "true";
2852 current_keys_pending.key_up = false;
2855 if (current_keys_pending.key_enter) {
2856 fields["key_enter"] = "true";
2857 current_keys_pending.key_enter = false;
2860 if (!current_field_enter_pending.empty()) {
2861 fields["key_enter_field"] = current_field_enter_pending;
2862 current_field_enter_pending = "";
2865 if (current_keys_pending.key_escape) {
2866 fields["key_escape"] = "true";
2867 current_keys_pending.key_escape = false;
2870 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
2872 std::string name = s.fname;
2873 if (s.ftype == f_Button) {
2874 fields[name] = wide_to_utf8(s.flabel);
2875 } else if (s.ftype == f_Table) {
2876 GUITable *table = getTable(s.fname);
2878 fields[name] = table->checkEvent();
2881 else if(s.ftype == f_DropDown) {
2882 // no dynamic cast possible due to some distributions shipped
2883 // without rtti support in irrlicht
2884 IGUIElement * element = getElementFromId(s.fid);
2885 gui::IGUIComboBox *e = NULL;
2886 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2887 e = static_cast<gui::IGUIComboBox*>(element);
2889 s32 selected = e->getSelected();
2890 if (selected >= 0) {
2891 std::vector<std::string> *dropdown_values =
2892 getDropDownValues(s.fname);
2893 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2894 fields[name] = (*dropdown_values)[selected];
2898 else if (s.ftype == f_TabHeader) {
2899 // no dynamic cast possible due to some distributions shipped
2900 // without rttzi support in irrlicht
2901 IGUIElement * element = getElementFromId(s.fid);
2902 gui::IGUITabControl *e = NULL;
2903 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2904 e = static_cast<gui::IGUITabControl *>(element);
2908 std::stringstream ss;
2909 ss << (e->getActiveTab() +1);
2910 fields[name] = ss.str();
2913 else if (s.ftype == f_CheckBox) {
2914 // no dynamic cast possible due to some distributions shipped
2915 // without rtti support in irrlicht
2916 IGUIElement * element = getElementFromId(s.fid);
2917 gui::IGUICheckBox *e = NULL;
2918 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2919 e = static_cast<gui::IGUICheckBox*>(element);
2924 fields[name] = "true";
2926 fields[name] = "false";
2929 else if (s.ftype == f_ScrollBar) {
2930 // no dynamic cast possible due to some distributions shipped
2931 // without rtti support in irrlicht
2932 IGUIElement * element = getElementFromId(s.fid);
2933 gui::IGUIScrollBar *e = NULL;
2934 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2935 e = static_cast<gui::IGUIScrollBar*>(element);
2939 std::stringstream os;
2941 if (s.fdefault == L"Changed")
2942 fields[name] = "CHG:" + os.str();
2944 fields[name] = "VAL:" + os.str();
2949 IGUIElement* e = getElementFromId(s.fid);
2951 fields[name] = wide_to_utf8(e->getText());
2957 m_text_dst->gotText(fields);
2961 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2963 while(tocheck != NULL) {
2964 if (tocheck == parent) {
2967 tocheck = tocheck->getParent();
2972 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2974 // The IGUITabControl renders visually using the skin's selected
2975 // font, which we override for the duration of form drawing,
2976 // but computes tab hotspots based on how it would have rendered
2977 // using the font that is selected at the time of button release.
2978 // To make these two consistent, temporarily override the skin's
2979 // font while the IGUITabControl is processing the event.
2980 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2981 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2982 s32 x = event.MouseInput.X;
2983 s32 y = event.MouseInput.Y;
2984 gui::IGUIElement *hovered =
2985 Environment->getRootGUIElement()->getElementFromPoint(
2986 core::position2d<s32>(x, y));
2987 if (hovered && isMyChild(hovered) &&
2988 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2989 gui::IGUISkin* skin = Environment->getSkin();
2990 sanity_check(skin != NULL);
2991 gui::IGUIFont *old_font = skin->getFont();
2992 skin->setFont(m_font);
2993 bool retval = hovered->OnEvent(event);
2994 skin->setFont(old_font);
2999 // Fix Esc/Return key being eaten by checkboxen and tables
3000 if(event.EventType==EET_KEY_INPUT_EVENT) {
3001 KeyPress kp(event.KeyInput);
3002 if (kp == EscapeKey || kp == CancelKey
3003 || kp == getKeySetting("keymap_inventory")
3004 || event.KeyInput.Key==KEY_RETURN) {
3005 gui::IGUIElement *focused = Environment->getFocus();
3006 if (focused && isMyChild(focused) &&
3007 (focused->getType() == gui::EGUIET_LIST_BOX ||
3008 focused->getType() == gui::EGUIET_CHECK_BOX) &&
3009 (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
3010 event.KeyInput.Key != KEY_RETURN)) {
3016 // Mouse wheel events: send to hovered element instead of focused
3017 if(event.EventType==EET_MOUSE_INPUT_EVENT
3018 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
3019 s32 x = event.MouseInput.X;
3020 s32 y = event.MouseInput.Y;
3021 gui::IGUIElement *hovered =
3022 Environment->getRootGUIElement()->getElementFromPoint(
3023 core::position2d<s32>(x, y));
3024 if (hovered && isMyChild(hovered)) {
3025 hovered->OnEvent(event);
3030 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
3031 s32 x = event.MouseInput.X;
3032 s32 y = event.MouseInput.Y;
3033 gui::IGUIElement *hovered =
3034 Environment->getRootGUIElement()->getElementFromPoint(
3035 core::position2d<s32>(x, y));
3036 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3037 m_old_tooltip_id = -1;
3039 if (!isChild(hovered,this)) {
3040 if (DoubleClickDetection(event)) {
3047 // display software keyboard when clicking edit boxes
3048 if (event.EventType == EET_MOUSE_INPUT_EVENT
3049 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3050 gui::IGUIElement *hovered =
3051 Environment->getRootGUIElement()->getElementFromPoint(
3052 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
3053 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
3054 bool retval = hovered->OnEvent(event);
3056 Environment->setFocus(hovered);
3058 std::string field_name = getNameByID(hovered->getID());
3059 /* read-only field */
3060 if (field_name.empty())
3063 m_JavaDialogFieldName = field_name;
3064 std::string message = gettext("Enter ");
3065 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
3068 message += gettext(label) + ":";
3070 /* single line text input */
3073 /* multi line text input */
3074 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled())
3077 /* passwords are always single line */
3078 if (((gui::IGUIEditBox*) hovered)->isPasswordBox())
3081 porting::showInputDialog(gettext("ok"), "",
3082 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
3088 if (event.EventType == EET_TOUCH_INPUT_EVENT)
3091 memset(&translated, 0, sizeof(SEvent));
3092 translated.EventType = EET_MOUSE_INPUT_EVENT;
3093 gui::IGUIElement* root = Environment->getRootGUIElement();
3097 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
3101 gui::IGUIElement* hovered = root->getElementFromPoint(
3102 core::position2d<s32>(
3104 event.TouchInput.Y));
3106 translated.MouseInput.X = event.TouchInput.X;
3107 translated.MouseInput.Y = event.TouchInput.Y;
3108 translated.MouseInput.Control = false;
3110 bool dont_send_event = false;
3112 if (event.TouchInput.touchedCount == 1) {
3113 switch (event.TouchInput.Event) {
3114 case ETIE_PRESSED_DOWN:
3115 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
3116 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
3117 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3118 m_down_pos = m_pointer;
3121 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
3122 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
3123 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3126 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
3127 translated.MouseInput.ButtonStates = 0;
3128 hovered = root->getElementFromPoint(m_down_pos);
3129 /* we don't have a valid pointer element use last
3130 * known pointer pos */
3131 translated.MouseInput.X = m_pointer.X;
3132 translated.MouseInput.Y = m_pointer.Y;
3134 /* reset down pos */
3135 m_down_pos = v2s32(0,0);
3138 dont_send_event = true;
3139 //this is not supposed to happen
3141 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
3142 << event.TouchInput.Event << std::endl;
3144 } else if ( (event.TouchInput.touchedCount == 2) &&
3145 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
3146 hovered = root->getElementFromPoint(m_down_pos);
3148 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
3149 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
3150 translated.MouseInput.X = m_pointer.X;
3151 translated.MouseInput.Y = m_pointer.Y;
3154 hovered->OnEvent(translated);
3157 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
3158 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3162 hovered->OnEvent(translated);
3164 dont_send_event = true;
3166 /* ignore unhandled 2 touch events ... accidental moving for example */
3167 else if (event.TouchInput.touchedCount == 2) {
3168 dont_send_event = true;
3170 else if (event.TouchInput.touchedCount > 2) {
3172 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
3173 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3176 if (dont_send_event) {
3180 /* check if translated event needs to be preprocessed again */
3181 if (preprocessEvent(translated)) {
3186 bool retval = hovered->OnEvent(translated);
3188 if (event.TouchInput.Event == ETIE_LEFT_UP) {
3190 m_pointer = v2s32(0,0);
3198 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3199 /* TODO add a check like:
3200 if (event.JoystickEvent != joystick_we_listen_for)
3203 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3205 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3207 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3209 acceptInput(quit_mode_accept);
3220 /******************************************************************************/
3221 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3223 /* The following code is for capturing double-clicks of the mouse button
3224 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3225 * -- which closes the form -- under some circumstances.
3227 * There have been many github issues reporting this as a bug even though it
3228 * was an intended feature. For this reason, remapping the double-click as
3229 * an ESC must be explicitly set when creating this class via the
3230 * /p remap_dbl_click parameter of the constructor.
3233 if (!m_remap_dbl_click)
3236 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3237 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3238 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3240 m_doubleclickdetect[1].pos = m_pointer;
3241 m_doubleclickdetect[1].time = porting::getTimeMs();
3243 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3244 u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
3249 double squaredistance =
3250 m_doubleclickdetect[0].pos
3251 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3253 if (squaredistance > (30*30)) {
3257 SEvent* translated = new SEvent();
3258 assert(translated != 0);
3259 //translate doubleclick to escape
3260 memset(translated, 0, sizeof(SEvent));
3261 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3262 translated->KeyInput.Key = KEY_ESCAPE;
3263 translated->KeyInput.Control = false;
3264 translated->KeyInput.Shift = false;
3265 translated->KeyInput.PressedDown = true;
3266 translated->KeyInput.Char = 0;
3267 OnEvent(*translated);
3269 // no need to send the key up event as we're already deleted
3270 // and no one else did notice this event
3278 void GUIFormSpecMenu::tryClose()
3282 acceptInput(quit_mode_cancel);
3285 m_text_dst->gotText(L"MenuQuit");
3289 enum ButtonEventType : u8
3302 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3304 if (event.EventType==EET_KEY_INPUT_EVENT) {
3305 KeyPress kp(event.KeyInput);
3306 if (event.KeyInput.PressedDown && (
3307 (kp == EscapeKey) || (kp == CancelKey) ||
3308 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
3313 if (m_client != NULL && event.KeyInput.PressedDown &&
3314 (kp == getKeySetting("keymap_screenshot"))) {
3315 m_client->makeScreenshot();
3317 if (event.KeyInput.PressedDown &&
3318 (event.KeyInput.Key==KEY_RETURN ||
3319 event.KeyInput.Key==KEY_UP ||
3320 event.KeyInput.Key==KEY_DOWN)
3322 switch (event.KeyInput.Key) {
3324 current_keys_pending.key_enter = true;
3327 current_keys_pending.key_up = true;
3330 current_keys_pending.key_down = true;
3334 //can't happen at all!
3335 FATAL_ERROR("Reached a source line that can't ever been reached");
3338 if (current_keys_pending.key_enter && m_allowclose) {
3339 acceptInput(quit_mode_accept);
3349 /* Mouse event other than movement, or crossing the border of inventory
3350 field while holding right mouse button
3352 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3353 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3354 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3355 event.MouseInput.isRightPressed() &&
3356 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3358 // Get selected item and hovered/clicked item (s)
3360 m_old_tooltip_id = -1;
3361 updateSelectedItem();
3362 ItemSpec s = getItemAtPos(m_pointer);
3364 Inventory *inv_selected = NULL;
3365 Inventory *inv_s = NULL;
3366 InventoryList *list_s = NULL;
3368 if (m_selected_item) {
3369 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3370 sanity_check(inv_selected);
3371 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3378 inv_s = m_invmgr->getInventory(s.inventoryloc);
3381 errorstream << "InventoryMenu: The selected inventory location "
3382 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3384 s.i = -1; // make it invalid again
3388 list_s = inv_s->getList(s.listname);
3389 if (list_s == NULL) {
3390 verbosestream << "InventoryMenu: The selected inventory list \""
3391 << s.listname << "\" does not exist" << std::endl;
3392 s.i = -1; // make it invalid again
3396 if ((u32)s.i >= list_s->getSize()) {
3397 infostream << "InventoryMenu: The selected inventory list \""
3398 << s.listname << "\" is too small (i=" << s.i << ", size="
3399 << list_s->getSize() << ")" << std::endl;
3400 s.i = -1; // make it invalid again
3404 s_count = list_s->getItem(s.i).count;
3407 bool identical = m_selected_item && s.isValid() &&
3408 (inv_selected == inv_s) &&
3409 (m_selected_item->listname == s.listname) &&
3410 (m_selected_item->i == s.i);
3412 ButtonEventType button = BET_LEFT;
3413 ButtonEventType updown = BET_OTHER;
3414 switch (event.MouseInput.Event) {
3415 case EMIE_LMOUSE_PRESSED_DOWN:
3416 button = BET_LEFT; updown = BET_DOWN;
3418 case EMIE_RMOUSE_PRESSED_DOWN:
3419 button = BET_RIGHT; updown = BET_DOWN;
3421 case EMIE_MMOUSE_PRESSED_DOWN:
3422 button = BET_MIDDLE; updown = BET_DOWN;
3424 case EMIE_MOUSE_WHEEL:
3425 button = (event.MouseInput.Wheel > 0) ?
3426 BET_WHEEL_UP : BET_WHEEL_DOWN;
3429 case EMIE_LMOUSE_LEFT_UP:
3430 button = BET_LEFT; updown = BET_UP;
3432 case EMIE_RMOUSE_LEFT_UP:
3433 button = BET_RIGHT; updown = BET_UP;
3435 case EMIE_MMOUSE_LEFT_UP:
3436 button = BET_MIDDLE; updown = BET_UP;
3438 case EMIE_MOUSE_MOVED:
3445 // Set this number to a positive value to generate a move action
3446 // from m_selected_item to s.
3447 u32 move_amount = 0;
3449 // Set this number to a positive value to generate a move action
3450 // from s to the next inventory ring.
3451 u32 shift_move_amount = 0;
3453 // Set this number to a positive value to generate a drop action
3454 // from m_selected_item.
3455 u32 drop_amount = 0;
3457 // Set this number to a positive value to generate a craft action at s.
3458 u32 craft_amount = 0;
3462 // Some mouse button has been pressed
3464 //infostream<<"Mouse button "<<button<<" pressed at p=("
3465 // <<p.X<<","<<p.Y<<")"<<std::endl;
3467 m_selected_dragging = false;
3469 if (s.isValid() && s.listname == "craftpreview") {
3470 // Craft preview has been clicked: craft
3471 craft_amount = (button == BET_MIDDLE ? 10 : 1);
3472 } else if (!m_selected_item) {
3473 if (s_count && button != BET_WHEEL_UP) {
3474 // Non-empty stack has been clicked: select or shift-move it
3475 m_selected_item = new ItemSpec(s);
3478 if (button == BET_RIGHT)
3479 count = (s_count + 1) / 2;
3480 else if (button == BET_MIDDLE)
3481 count = MYMIN(s_count, 10);
3482 else if (button == BET_WHEEL_DOWN)
3487 if (!event.MouseInput.Shift) {
3488 // no shift: select item
3489 m_selected_amount = count;
3490 m_selected_dragging = button != BET_WHEEL_DOWN;
3491 m_auto_place = false;
3493 // shift pressed: move item, right click moves 1
3494 shift_move_amount = button == BET_RIGHT ? 1 : count;
3497 } else { // m_selected_item != NULL
3498 assert(m_selected_amount >= 1);
3501 // Clicked a slot: move
3502 if (button == BET_RIGHT || button == BET_WHEEL_UP)
3504 else if (button == BET_MIDDLE)
3505 move_amount = MYMIN(m_selected_amount, 10);
3506 else if (button == BET_LEFT)
3507 move_amount = m_selected_amount;
3511 if (button == BET_WHEEL_DOWN) {
3512 if (m_selected_amount < s_count)
3513 ++m_selected_amount;
3515 if (move_amount >= m_selected_amount)
3516 m_selected_amount = 0;
3518 m_selected_amount -= move_amount;
3522 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
3523 && button != BET_WHEEL_DOWN) {
3524 // Clicked outside of the window: drop
3525 if (button == BET_RIGHT || button == BET_WHEEL_UP)
3527 else if (button == BET_MIDDLE)
3528 drop_amount = MYMIN(m_selected_amount, 10);
3530 drop_amount = m_selected_amount;
3535 // Some mouse button has been released
3537 //infostream<<"Mouse button "<<button<<" released at p=("
3538 // <<p.X<<","<<p.Y<<")"<<std::endl;
3540 if (m_selected_dragging && m_selected_item) {
3543 // Dragged to different slot: move all selected
3544 move_amount = m_selected_amount;
3546 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3547 // Dragged outside of window: drop all selected
3548 drop_amount = m_selected_amount;
3552 m_selected_dragging = false;
3553 // Keep track of whether the mouse button be released
3554 // One click is drag without dropping. Click + release
3555 // + click changes to drop item when moved mode
3556 if (m_selected_item)
3557 m_auto_place = true;
3560 // Mouse has been moved and rmb is down and mouse pointer just
3561 // entered a new inventory field (checked in the entry-if, this
3562 // is the only action here that is generated by mouse movement)
3563 if (m_selected_item && s.isValid()) {
3565 // TODO: middle mouse to move 10 items might be handy
3567 // Only move an item if the destination slot is empty
3568 // or contains the same item type as what is going to be
3570 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3571 InventoryList *list_to = list_s;
3572 assert(list_from && list_to);
3573 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3574 ItemStack stack_to = list_to->getItem(s.i);
3575 if (stack_to.empty() || stack_to.name == stack_from.name)
3584 // Possibly send inventory action to server
3585 if (move_amount > 0) {
3586 // Send IAction::Move
3588 assert(m_selected_item && m_selected_item->isValid());
3589 assert(s.isValid());
3591 assert(inv_selected && inv_s);
3592 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3593 InventoryList *list_to = list_s;
3594 assert(list_from && list_to);
3595 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3596 ItemStack stack_to = list_to->getItem(s.i);
3598 // Check how many items can be moved
3599 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3600 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
3602 // If source stack cannot be added to destination stack at all,
3604 if (leftover.count == stack_from.count &&
3605 leftover.name == stack_from.name) {
3607 if (m_selected_swap.empty()) {
3608 m_selected_amount = stack_to.count;
3609 m_selected_dragging = false;
3611 // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
3612 // Skip next validation checks due async inventory calls
3613 m_selected_swap = stack_to;
3618 // Source stack goes fully into destination stack
3619 else if (leftover.empty()) {
3620 m_selected_amount -= move_amount;
3622 // Source stack goes partly into destination stack
3624 move_amount -= leftover.count;
3625 m_selected_amount -= move_amount;
3629 infostream << "Handing IAction::Move to manager" << std::endl;
3630 IMoveAction *a = new IMoveAction();
3631 a->count = move_amount;
3632 a->from_inv = m_selected_item->inventoryloc;
3633 a->from_list = m_selected_item->listname;
3634 a->from_i = m_selected_item->i;
3635 a->to_inv = s.inventoryloc;
3636 a->to_list = s.listname;
3638 m_invmgr->inventoryAction(a);
3640 } else if (shift_move_amount > 0) {
3641 u32 mis = m_inventory_rings.size();
3643 for (; i < mis; i++) {
3644 const ListRingSpec &sp = m_inventory_rings[i];
3645 if (sp.inventoryloc == s.inventoryloc
3646 && sp.listname == s.listname)
3650 if (i >= mis) // if not found
3652 u32 to_inv_ind = (i + 1) % mis;
3653 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3654 InventoryList *list_from = list_s;
3657 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3660 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3663 ItemStack stack_from = list_from->getItem(s.i);
3664 assert(shift_move_amount <= stack_from.count);
3666 infostream << "Handing IAction::Move to manager" << std::endl;
3667 IMoveAction *a = new IMoveAction();
3668 a->count = shift_move_amount;
3669 a->from_inv = s.inventoryloc;
3670 a->from_list = s.listname;
3672 a->to_inv = to_inv_sp.inventoryloc;
3673 a->to_list = to_inv_sp.listname;
3674 a->move_somewhere = true;
3675 m_invmgr->inventoryAction(a);
3677 } else if (drop_amount > 0) {
3678 // Send IAction::Drop
3680 assert(m_selected_item && m_selected_item->isValid());
3681 assert(inv_selected);
3682 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3684 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3686 // Check how many items can be dropped
3687 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3688 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3689 m_selected_amount -= drop_amount;
3691 infostream << "Handing IAction::Drop to manager" << std::endl;
3692 IDropAction *a = new IDropAction();
3693 a->count = drop_amount;
3694 a->from_inv = m_selected_item->inventoryloc;
3695 a->from_list = m_selected_item->listname;
3696 a->from_i = m_selected_item->i;
3697 m_invmgr->inventoryAction(a);
3698 } else if (craft_amount > 0) {
3699 assert(s.isValid());
3701 // if there are no items selected or the selected item
3702 // belongs to craftresult list, proceed with crafting
3703 if (m_selected_item == NULL ||
3704 !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
3708 // Send IACTION_CRAFT
3709 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3710 ICraftAction *a = new ICraftAction();
3711 a->count = craft_amount;
3712 a->craft_inv = s.inventoryloc;
3713 m_invmgr->inventoryAction(a);
3717 // If m_selected_amount has been decreased to zero, deselect
3718 if (m_selected_amount == 0) {
3719 m_selected_swap.clear();
3720 delete m_selected_item;
3721 m_selected_item = NULL;
3722 m_selected_amount = 0;
3723 m_selected_dragging = false;
3725 m_old_pointer = m_pointer;
3727 if (event.EventType == EET_GUI_EVENT) {
3729 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3731 // find the element that was clicked
3732 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3733 if ((s.ftype == f_TabHeader) &&
3734 (s.fid == event.GUIEvent.Caller->getID())) {
3742 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3744 if (!canTakeFocus(event.GUIEvent.Element)) {
3745 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3747 // Returning true disables focus change
3751 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3752 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3753 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3754 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3755 unsigned int btn_id = event.GUIEvent.Caller->getID();
3757 if (btn_id == 257) {
3759 acceptInput(quit_mode_accept);
3763 m_text_dst->gotText(L"ExitButton");
3765 // quitMenu deallocates menu
3769 // find the element that was clicked
3770 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3771 // if its a button, set the send field so
3772 // lua knows which button was pressed
3773 if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
3774 s.fid == event.GUIEvent.Caller->getID()) {
3778 acceptInput(quit_mode_accept);
3781 m_text_dst->gotText(L"ExitButton");
3786 acceptInput(quit_mode_no);
3790 } else if ((s.ftype == f_DropDown) &&
3791 (s.fid == event.GUIEvent.Caller->getID())) {
3792 // only send the changed dropdown
3793 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
3794 if (s2.ftype == f_DropDown) {
3799 acceptInput(quit_mode_no);
3801 // revert configuration to make sure dropdowns are sent on
3802 // regular button click
3803 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
3804 if (s2.ftype == f_DropDown) {
3809 } else if ((s.ftype == f_ScrollBar) &&
3810 (s.fid == event.GUIEvent.Caller->getID())) {
3811 s.fdefault = L"Changed";
3812 acceptInput(quit_mode_no);
3818 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3819 if (event.GUIEvent.Caller->getID() > 257) {
3820 bool close_on_enter = true;
3821 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3822 if (s.ftype == f_Unknown &&
3823 s.fid == event.GUIEvent.Caller->getID()) {
3824 current_field_enter_pending = s.fname;
3825 std::unordered_map<std::string, bool>::const_iterator it =
3826 field_close_on_enter.find(s.fname);
3827 if (it != field_close_on_enter.end())
3828 close_on_enter = (*it).second;
3834 if (m_allowclose && close_on_enter) {
3835 current_keys_pending.key_enter = true;
3836 acceptInput(quit_mode_accept);
3839 current_keys_pending.key_enter = true;
3842 // quitMenu deallocates menu
3847 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3848 int current_id = event.GUIEvent.Caller->getID();
3849 if (current_id > 257) {
3850 // find the element that was clicked
3851 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3852 // if it's a table, set the send field
3853 // so lua knows which table was changed
3854 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3865 return Parent ? Parent->OnEvent(event) : false;
3869 * get name of element by element id
3870 * @param id of element
3871 * @return name string or empty string
3873 std::string GUIFormSpecMenu::getNameByID(s32 id)
3875 for (FieldSpec &spec : m_fields) {
3876 if (spec.fid == id) {
3884 * get label of element by id
3885 * @param id of element
3886 * @return label string or empty string
3888 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3890 for (FieldSpec &spec : m_fields) {
3891 if (spec.fid == id) {