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 "hud.h" // drawItemStack
46 #include "scripting_server.h"
50 #include "fontengine.h"
52 #include "util/numeric.h"
53 #include "util/string.h" // for parseColorString()
54 #include "irrlicht_changes/static_text.h"
55 #include "guiscalingfilter.h"
57 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
58 #include "intlGUIEditBox.h"
61 #define MY_CHECKPOS(a,b) \
62 if (v_pos.size() != 2) { \
63 errorstream<< "Invalid pos for element " << a << "specified: \"" \
64 << parts[b] << "\"" << std::endl; \
68 #define MY_CHECKGEOM(a,b) \
69 if (v_geom.size() != 2) { \
70 errorstream<< "Invalid pos for element " << a << "specified: \"" \
71 << parts[b] << "\"" << std::endl; \
77 static unsigned int font_line_height(gui::IGUIFont *font)
79 return font->getDimension(L"Ay").Height + font->getKerningHeight();
82 GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
83 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
84 Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst,
85 bool remap_dbl_click) :
86 GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr),
93 m_remap_dbl_click(remap_dbl_click)
95 , m_JavaDialogFieldName("")
98 current_keys_pending.key_down = false;
99 current_keys_pending.key_up = false;
100 current_keys_pending.key_enter = false;
101 current_keys_pending.key_escape = false;
103 m_doubleclickdetect[0].time = 0;
104 m_doubleclickdetect[1].time = 0;
106 m_doubleclickdetect[0].pos = v2s32(0, 0);
107 m_doubleclickdetect[1].pos = v2s32(0, 0);
109 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
112 GUIFormSpecMenu::~GUIFormSpecMenu()
116 for (auto &table_it : m_tables) {
117 table_it.second->drop();
120 delete m_selected_item;
125 void GUIFormSpecMenu::removeChildren()
127 const core::list<gui::IGUIElement*> &children = getChildren();
129 while(!children.empty()) {
130 (*children.getLast())->remove();
133 if(m_tooltip_element) {
134 m_tooltip_element->remove();
135 m_tooltip_element->drop();
136 m_tooltip_element = NULL;
141 void GUIFormSpecMenu::setInitialFocus()
143 // Set initial focus according to following order of precedence:
144 // 1. first empty editbox
148 // 5. first focusable (not statictext, not tabheader)
149 // 6. first child element
151 core::list<gui::IGUIElement*> children = getChildren();
153 // in case "children" contains any NULL elements, remove them
154 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
155 it != children.end();) {
159 it = children.erase(it);
162 // 1. first empty editbox
163 for (gui::IGUIElement *it : children) {
164 if (it->getType() == gui::EGUIET_EDIT_BOX
165 && it->getText()[0] == 0) {
166 Environment->setFocus(it);
172 for (gui::IGUIElement *it : children) {
173 if (it->getType() == gui::EGUIET_EDIT_BOX) {
174 Environment->setFocus(it);
180 for (gui::IGUIElement *it : children) {
181 if (it->getTypeName() == std::string("GUITable")) {
182 Environment->setFocus(it);
188 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
189 it != children.end(); --it) {
190 if ((*it)->getType() == gui::EGUIET_BUTTON) {
191 Environment->setFocus(*it);
196 // 5. first focusable (not statictext, not tabheader)
197 for (gui::IGUIElement *it : children) {
198 if (it->getType() != gui::EGUIET_STATIC_TEXT &&
199 it->getType() != gui::EGUIET_TAB_CONTROL) {
200 Environment->setFocus(it);
205 // 6. first child element
206 if (children.empty())
207 Environment->setFocus(this);
209 Environment->setFocus(*(children.begin()));
212 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
214 for (auto &table : m_tables) {
215 if (tablename == table.first.fname)
221 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
223 for (auto &dropdown : m_dropdowns) {
224 if (name == dropdown.first.fname)
225 return &dropdown.second;
230 void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
232 std::vector<std::string> parts = split(element,',');
234 if (((parts.size() == 2) || parts.size() == 3) ||
235 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
237 if (parts[1].find(';') != std::string::npos)
238 parts[1] = parts[1].substr(0,parts[1].find(';'));
240 data->invsize.X = MYMAX(0, stof(parts[0]));
241 data->invsize.Y = MYMAX(0, stof(parts[1]));
244 if (parts.size() == 3) {
245 if (parts[2] == "true") {
246 lockSize(true,v2u32(800,600));
250 data->explicit_size = true;
253 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
256 void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
258 std::vector<std::string> parts = split(element, ',');
260 if (parts.size() >= 2) {
261 if (parts[1].find(';') != std::string::npos)
262 parts[1] = parts[1].substr(0, parts[1].find(';'));
264 container_stack.push(pos_offset);
265 pos_offset.X += MYMAX(0, stof(parts[0]));
266 pos_offset.Y += MYMAX(0, stof(parts[1]));
269 errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
272 void GUIFormSpecMenu::parseContainerEnd(parserData* data)
274 if (container_stack.empty()) {
275 errorstream<< "Invalid container end element, no matching container start element" << std::endl;
277 pos_offset = container_stack.top();
278 container_stack.pop();
282 void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
285 warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
289 std::vector<std::string> parts = split(element,';');
291 if (((parts.size() == 4) || (parts.size() == 5)) ||
292 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
294 std::string location = parts[0];
295 std::string listname = parts[1];
296 std::vector<std::string> v_pos = split(parts[2],',');
297 std::vector<std::string> v_geom = split(parts[3],',');
298 std::string startindex;
299 if (parts.size() == 5)
300 startindex = parts[4];
302 MY_CHECKPOS("list",2);
303 MY_CHECKGEOM("list",3);
305 InventoryLocation loc;
307 if(location == "context" || location == "current_name")
308 loc = m_current_inventory_location;
310 loc.deSerialize(location);
312 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
313 pos.X += stof(v_pos[0]) * (float)spacing.X;
314 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
317 geom.X = stoi(v_geom[0]);
318 geom.Y = stoi(v_geom[1]);
321 if (!startindex.empty())
322 start_i = stoi(startindex);
324 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
325 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
329 if(!data->explicit_size)
330 warningstream<<"invalid use of list without a size[] element"<<std::endl;
331 m_inventorylists.emplace_back(loc, listname, pos, geom, start_i);
334 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
337 void GUIFormSpecMenu::parseListRing(parserData* data, const std::string &element)
340 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
344 std::vector<std::string> parts = split(element, ';');
346 if (parts.size() == 2) {
347 std::string location = parts[0];
348 std::string listname = parts[1];
350 InventoryLocation loc;
352 if (location == "context" || location == "current_name")
353 loc = m_current_inventory_location;
355 loc.deSerialize(location);
357 m_inventory_rings.emplace_back(loc, listname);
361 if (element.empty() && m_inventorylists.size() > 1) {
362 size_t siz = m_inventorylists.size();
363 // insert the last two inv list elements into the list ring
364 const ListDrawSpec &spa = m_inventorylists[siz - 2];
365 const ListDrawSpec &spb = m_inventorylists[siz - 1];
366 m_inventory_rings.emplace_back(spa.inventoryloc, spa.listname);
367 m_inventory_rings.emplace_back(spb.inventoryloc, spb.listname);
371 errorstream<< "Invalid list ring element(" << parts.size() << ", "
372 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
375 void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
377 std::vector<std::string> parts = split(element,';');
379 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
380 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
382 std::vector<std::string> v_pos = split(parts[0],',');
383 std::string name = parts[1];
384 std::string label = parts[2];
385 std::string selected;
387 if (parts.size() >= 4)
390 MY_CHECKPOS("checkbox",0);
392 v2s32 pos = padding + pos_offset * spacing;
393 pos.X += stof(v_pos[0]) * (float) spacing.X;
394 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
396 bool fselected = false;
398 if (selected == "true")
401 std::wstring wlabel = utf8_to_wide(unescape_string(label));
403 core::rect<s32> rect = core::rect<s32>(
404 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
405 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
406 pos.Y + ((imgsize.Y/2) + m_btn_height));
410 wlabel, //Needed for displaying text on MSVC
415 spec.ftype = f_CheckBox;
417 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
418 spec.fid, spec.flabel.c_str());
420 if (spec.fname == data->focused_fieldname) {
421 Environment->setFocus(e);
424 m_checkboxes.emplace_back(spec,e);
425 m_fields.push_back(spec);
428 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
431 void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
433 std::vector<std::string> parts = split(element,';');
435 if (parts.size() >= 5) {
436 std::vector<std::string> v_pos = split(parts[0],',');
437 std::vector<std::string> v_dim = split(parts[1],',');
438 std::string name = parts[3];
439 std::string value = parts[4];
441 MY_CHECKPOS("scrollbar",0);
443 v2s32 pos = padding + pos_offset * spacing;
444 pos.X += stof(v_pos[0]) * (float) spacing.X;
445 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
447 if (v_dim.size() != 2) {
448 errorstream<< "Invalid size for element " << "scrollbar"
449 << "specified: \"" << parts[1] << "\"" << std::endl;
454 dim.X = stof(v_dim[0]) * (float) spacing.X;
455 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
457 core::rect<s32> rect =
458 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
467 bool is_horizontal = true;
469 if (parts[2] == "vertical")
470 is_horizontal = false;
472 spec.ftype = f_ScrollBar;
474 gui::IGUIScrollBar* e =
475 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
479 e->setPos(stoi(parts[4]));
481 e->setLargeStep(100);
483 m_scrollbars.emplace_back(spec,e);
484 m_fields.push_back(spec);
487 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
490 void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
492 std::vector<std::string> parts = split(element,';');
494 if ((parts.size() == 3) ||
495 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
497 std::vector<std::string> v_pos = split(parts[0],',');
498 std::vector<std::string> v_geom = split(parts[1],',');
499 std::string name = unescape_string(parts[2]);
501 MY_CHECKPOS("image", 0);
502 MY_CHECKGEOM("image", 1);
504 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
505 pos.X += stof(v_pos[0]) * (float) spacing.X;
506 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
509 geom.X = stof(v_geom[0]) * (float)imgsize.X;
510 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
512 if (!data->explicit_size)
513 warningstream<<"invalid use of image without a size[] element"<<std::endl;
514 m_images.emplace_back(name, pos, geom);
518 if (parts.size() == 2) {
519 std::vector<std::string> v_pos = split(parts[0],',');
520 std::string name = unescape_string(parts[1]);
522 MY_CHECKPOS("image", 0);
524 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
525 pos.X += stof(v_pos[0]) * (float) spacing.X;
526 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
528 if (!data->explicit_size)
529 warningstream<<"invalid use of image without a size[] element"<<std::endl;
530 m_images.emplace_back(name, pos);
533 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
536 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
538 std::vector<std::string> parts = split(element,';');
540 if ((parts.size() == 3) ||
541 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
543 std::vector<std::string> v_pos = split(parts[0],',');
544 std::vector<std::string> v_geom = split(parts[1],',');
545 std::string name = parts[2];
547 MY_CHECKPOS("itemimage",0);
548 MY_CHECKGEOM("itemimage",1);
550 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
551 pos.X += stof(v_pos[0]) * (float) spacing.X;
552 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
555 geom.X = stof(v_geom[0]) * (float)imgsize.X;
556 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
558 if(!data->explicit_size)
559 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
560 m_itemimages.emplace_back("", name, pos, geom);
563 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
566 void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
567 const std::string &type)
569 std::vector<std::string> parts = split(element,';');
571 if ((parts.size() == 4) ||
572 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
574 std::vector<std::string> v_pos = split(parts[0],',');
575 std::vector<std::string> v_geom = split(parts[1],',');
576 std::string name = parts[2];
577 std::string label = parts[3];
579 MY_CHECKPOS("button",0);
580 MY_CHECKGEOM("button",1);
582 v2s32 pos = padding + pos_offset * spacing;
583 pos.X += stof(v_pos[0]) * (float)spacing.X;
584 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
587 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
588 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
590 core::rect<s32> rect =
591 core::rect<s32>(pos.X, pos.Y - m_btn_height,
592 pos.X + geom.X, pos.Y + m_btn_height);
594 if(!data->explicit_size)
595 warningstream<<"invalid use of button without a size[] element"<<std::endl;
597 std::wstring wlabel = utf8_to_wide(unescape_string(label));
605 spec.ftype = f_Button;
606 if(type == "button_exit")
608 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
609 spec.flabel.c_str());
611 if (spec.fname == data->focused_fieldname) {
612 Environment->setFocus(e);
615 m_fields.push_back(spec);
618 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
621 void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
623 std::vector<std::string> parts = split(element,';');
625 if (((parts.size() == 3) || (parts.size() == 4)) ||
626 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
628 std::vector<std::string> v_pos = split(parts[0],',');
629 std::vector<std::string> v_geom = split(parts[1],',');
630 std::string name = unescape_string(parts[2]);
632 MY_CHECKPOS("background",0);
633 MY_CHECKGEOM("background",1);
635 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
636 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X - (float)imgsize.X)/2;
637 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y - (float)imgsize.Y)/2;
640 geom.X = stof(v_geom[0]) * (float)spacing.X;
641 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
643 if (!data->explicit_size)
644 warningstream<<"invalid use of background without a size[] element"<<std::endl;
647 if (parts.size() == 4 && is_yes(parts[3])) {
648 pos.X = stoi(v_pos[0]); //acts as offset
649 pos.Y = stoi(v_pos[1]); //acts as offset
652 m_backgrounds.emplace_back(name, pos, geom, clip);
656 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
659 void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
661 std::vector<std::string> parts = split(element,';');
663 data->table_options.clear();
664 for (const std::string &part : parts) {
665 // Parse table option
666 std::string opt = unescape_string(part);
667 data->table_options.push_back(GUITable::splitOption(opt));
671 void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
673 std::vector<std::string> parts = split(element,';');
675 data->table_columns.clear();
676 for (const std::string &part : parts) {
677 std::vector<std::string> col_parts = split(part,',');
678 GUITable::TableColumn column;
680 if (!col_parts.empty())
681 column.type = col_parts[0];
682 // Parse column options
683 for (size_t j = 1; j < col_parts.size(); ++j) {
684 std::string opt = unescape_string(col_parts[j]);
685 column.options.push_back(GUITable::splitOption(opt));
687 data->table_columns.push_back(column);
691 void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
693 std::vector<std::string> parts = split(element,';');
695 if (((parts.size() == 4) || (parts.size() == 5)) ||
696 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
698 std::vector<std::string> v_pos = split(parts[0],',');
699 std::vector<std::string> v_geom = split(parts[1],',');
700 std::string name = parts[2];
701 std::vector<std::string> items = split(parts[3],',');
702 std::string str_initial_selection;
703 std::string str_transparent = "false";
705 if (parts.size() >= 5)
706 str_initial_selection = parts[4];
708 MY_CHECKPOS("table",0);
709 MY_CHECKGEOM("table",1);
711 v2s32 pos = padding + pos_offset * spacing;
712 pos.X += stof(v_pos[0]) * (float)spacing.X;
713 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
716 geom.X = stof(v_geom[0]) * (float)spacing.X;
717 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
719 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
728 spec.ftype = f_Table;
730 for (std::string &item : items) {
731 item = unescape_enriched(unescape_string(item));
734 //now really show table
735 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
738 if (spec.fname == data->focused_fieldname) {
739 Environment->setFocus(e);
742 e->setTable(data->table_options, data->table_columns, items);
744 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
745 e->setDynamicData(data->table_dyndata[name]);
748 if (!str_initial_selection.empty() && str_initial_selection != "0")
749 e->setSelected(stoi(str_initial_selection));
751 m_tables.emplace_back(spec, e);
752 m_fields.push_back(spec);
755 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
758 void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
760 std::vector<std::string> parts = split(element,';');
762 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
763 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
765 std::vector<std::string> v_pos = split(parts[0],',');
766 std::vector<std::string> v_geom = split(parts[1],',');
767 std::string name = parts[2];
768 std::vector<std::string> items = split(parts[3],',');
769 std::string str_initial_selection;
770 std::string str_transparent = "false";
772 if (parts.size() >= 5)
773 str_initial_selection = parts[4];
775 if (parts.size() >= 6)
776 str_transparent = parts[5];
778 MY_CHECKPOS("textlist",0);
779 MY_CHECKGEOM("textlist",1);
781 v2s32 pos = padding + pos_offset * spacing;
782 pos.X += stof(v_pos[0]) * (float)spacing.X;
783 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
786 geom.X = stof(v_geom[0]) * (float)spacing.X;
787 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
790 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
799 spec.ftype = f_Table;
801 for (std::string &item : items) {
802 item = unescape_enriched(unescape_string(item));
805 //now really show list
806 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
809 if (spec.fname == data->focused_fieldname) {
810 Environment->setFocus(e);
813 e->setTextList(items, is_yes(str_transparent));
815 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
816 e->setDynamicData(data->table_dyndata[name]);
819 if (!str_initial_selection.empty() && str_initial_selection != "0")
820 e->setSelected(stoi(str_initial_selection));
822 m_tables.emplace_back(spec, e);
823 m_fields.push_back(spec);
826 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
830 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
832 std::vector<std::string> parts = split(element,';');
834 if ((parts.size() == 5) ||
835 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
837 std::vector<std::string> v_pos = split(parts[0],',');
838 std::string name = parts[2];
839 std::vector<std::string> items = split(parts[3],',');
840 std::string str_initial_selection;
841 str_initial_selection = parts[4];
843 MY_CHECKPOS("dropdown",0);
845 v2s32 pos = padding + pos_offset * spacing;
846 pos.X += stof(v_pos[0]) * (float)spacing.X;
847 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
849 s32 width = stof(parts[1]) * (float)spacing.Y;
851 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
852 pos.X + width, pos.Y + (m_btn_height * 2));
861 spec.ftype = f_DropDown;
864 //now really show list
865 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
867 if (spec.fname == data->focused_fieldname) {
868 Environment->setFocus(e);
871 for (const std::string &item : items) {
872 e->addItem(unescape_enriched(unescape_string(
873 utf8_to_wide(item))).c_str());
876 if (!str_initial_selection.empty())
877 e->setSelected(stoi(str_initial_selection)-1);
879 m_fields.push_back(spec);
881 m_dropdowns.emplace_back(spec, std::vector<std::string>());
882 std::vector<std::string> &values = m_dropdowns.back().second;
883 for (const std::string &item : items) {
884 values.push_back(unescape_string(item));
889 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
890 << element << "'" << std::endl;
893 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
895 std::vector<std::string> parts = split(element,';');
896 if (parts.size() == 2 ||
897 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
898 field_close_on_enter[parts[0]] = is_yes(parts[1]);
902 void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
904 std::vector<std::string> parts = split(element,';');
906 if ((parts.size() == 4) || (parts.size() == 5) ||
907 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
909 std::vector<std::string> v_pos = split(parts[0],',');
910 std::vector<std::string> v_geom = split(parts[1],',');
911 std::string name = parts[2];
912 std::string label = parts[3];
914 MY_CHECKPOS("pwdfield",0);
915 MY_CHECKGEOM("pwdfield",1);
917 v2s32 pos = pos_offset * spacing;
918 pos.X += stof(v_pos[0]) * (float)spacing.X;
919 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
922 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
924 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
925 pos.Y -= m_btn_height;
926 geom.Y = m_btn_height*2;
928 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
930 std::wstring wlabel = utf8_to_wide(unescape_string(label));
940 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
942 if (spec.fname == data->focused_fieldname) {
943 Environment->setFocus(e);
946 if (label.length() >= 1)
948 int font_height = g_fontengine->getTextHeight();
949 rect.UpperLeftCorner.Y -= font_height;
950 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
951 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
954 e->setPasswordBox(true,L'*');
957 evt.EventType = EET_KEY_INPUT_EVENT;
958 evt.KeyInput.Key = KEY_END;
959 evt.KeyInput.Char = 0;
960 evt.KeyInput.Control = false;
961 evt.KeyInput.Shift = false;
962 evt.KeyInput.PressedDown = true;
965 if (parts.size() >= 5) {
966 // TODO: remove after 2016-11-03
967 warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
968 " instead of the 5th param" << std::endl;
969 field_close_on_enter[name] = is_yes(parts[4]);
972 m_fields.push_back(spec);
975 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
978 void GUIFormSpecMenu::parseSimpleField(parserData* data,
979 std::vector<std::string> &parts)
981 std::string name = parts[0];
982 std::string label = parts[1];
983 std::string default_val = parts[2];
985 core::rect<s32> rect;
987 if(data->explicit_size)
988 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
990 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
991 pos.Y = ((m_fields.size()+2)*60);
992 v2s32 size = DesiredRect.getSize();
994 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
995 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
999 default_val = m_form_src->resolveText(default_val);
1002 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1007 utf8_to_wide(unescape_string(default_val)),
1012 // spec field id to 0, this stops submit searching for a value that isn't there
1013 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1016 gui::IGUIElement *e;
1017 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1018 if (g_settings->getBool("freetype")) {
1019 e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1020 true, Environment, this, spec.fid, rect);
1026 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1028 if (spec.fname == data->focused_fieldname) {
1029 Environment->setFocus(e);
1033 evt.EventType = EET_KEY_INPUT_EVENT;
1034 evt.KeyInput.Key = KEY_END;
1035 evt.KeyInput.Char = 0;
1036 evt.KeyInput.Control = 0;
1037 evt.KeyInput.Shift = 0;
1038 evt.KeyInput.PressedDown = true;
1041 if (label.length() >= 1)
1043 int font_height = g_fontengine->getTextHeight();
1044 rect.UpperLeftCorner.Y -= font_height;
1045 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1046 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1050 if (parts.size() >= 4) {
1051 // TODO: remove after 2016-11-03
1052 warningstream << "field/simple: use field_close_on_enter[name, enabled]" <<
1053 " instead of the 4th param" << std::endl;
1054 field_close_on_enter[name] = is_yes(parts[3]);
1057 m_fields.push_back(spec);
1060 void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
1061 const std::string &type)
1064 std::vector<std::string> v_pos = split(parts[0],',');
1065 std::vector<std::string> v_geom = split(parts[1],',');
1066 std::string name = parts[2];
1067 std::string label = parts[3];
1068 std::string default_val = parts[4];
1070 MY_CHECKPOS(type,0);
1071 MY_CHECKGEOM(type,1);
1073 v2s32 pos = pos_offset * spacing;
1074 pos.X += stof(v_pos[0]) * (float) spacing.X;
1075 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1079 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1081 if (type == "textarea")
1083 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1084 pos.Y += m_btn_height;
1088 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1089 pos.Y -= m_btn_height;
1090 geom.Y = m_btn_height*2;
1093 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1095 if(!data->explicit_size)
1096 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1099 default_val = m_form_src->resolveText(default_val);
1102 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1107 utf8_to_wide(unescape_string(default_val)),
1112 // spec field id to 0, this stops submit searching for a value that isn't there
1113 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1117 gui::IGUIEditBox *e;
1118 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1119 if (g_settings->getBool("freetype")) {
1120 e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1121 true, Environment, this, spec.fid, rect);
1127 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1130 if (spec.fname == data->focused_fieldname) {
1131 Environment->setFocus(e);
1134 if (type == "textarea")
1136 e->setMultiLine(true);
1137 e->setWordWrap(true);
1138 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1141 evt.EventType = EET_KEY_INPUT_EVENT;
1142 evt.KeyInput.Key = KEY_END;
1143 evt.KeyInput.Char = 0;
1144 evt.KeyInput.Control = 0;
1145 evt.KeyInput.Shift = 0;
1146 evt.KeyInput.PressedDown = true;
1150 if (label.length() >= 1)
1152 int font_height = g_fontengine->getTextHeight();
1153 rect.UpperLeftCorner.Y -= font_height;
1154 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1155 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1159 if (parts.size() >= 6) {
1160 // TODO: remove after 2016-11-03
1161 warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
1162 " instead of the 6th param" << std::endl;
1163 field_close_on_enter[name] = is_yes(parts[5]);
1166 m_fields.push_back(spec);
1169 void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
1170 const std::string &type)
1172 std::vector<std::string> parts = split(element,';');
1174 if (parts.size() == 3 || parts.size() == 4) {
1175 parseSimpleField(data,parts);
1179 if ((parts.size() == 5) || (parts.size() == 6) ||
1180 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1182 parseTextArea(data,parts,type);
1185 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1188 void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
1190 std::vector<std::string> parts = split(element,';');
1192 if ((parts.size() == 2) ||
1193 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1195 std::vector<std::string> v_pos = split(parts[0],',');
1196 std::string text = parts[1];
1198 MY_CHECKPOS("label",0);
1200 v2s32 pos = padding + pos_offset * spacing;
1201 pos.X += stof(v_pos[0]) * (float)spacing.X;
1202 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1204 if(!data->explicit_size)
1205 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1207 std::vector<std::string> lines = split(text, '\n');
1209 for (unsigned int i = 0; i != lines.size(); i++) {
1210 // Lines are spaced at the nominal distance of
1211 // 2/5 inventory slot, even if the font doesn't
1212 // quite match that. This provides consistent
1213 // form layout, at the expense of sometimes
1214 // having sub-optimal spacing for the font.
1215 // We multiply by 2 and then divide by 5, rather
1216 // than multiply by 0.4, to get exact results
1217 // in the integer cases: 0.4 is not exactly
1218 // representable in binary floating point.
1219 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1220 std::wstring wlabel = utf8_to_wide(unescape_string(lines[i]));
1221 core::rect<s32> rect = core::rect<s32>(
1222 pos.X, posy - m_btn_height,
1223 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1224 posy + m_btn_height);
1231 gui::IGUIStaticText *e =
1232 addStaticText(Environment, spec.flabel.c_str(),
1233 rect, false, false, this, spec.fid);
1234 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1236 m_fields.push_back(spec);
1241 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1244 void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
1246 std::vector<std::string> parts = split(element,';');
1248 if ((parts.size() == 2) ||
1249 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1251 std::vector<std::string> v_pos = split(parts[0],',');
1252 std::wstring text = unescape_enriched(
1253 unescape_string(utf8_to_wide(parts[1])));
1255 MY_CHECKPOS("vertlabel",1);
1257 v2s32 pos = padding + pos_offset * spacing;
1258 pos.X += stof(v_pos[0]) * (float)spacing.X;
1259 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1261 core::rect<s32> rect = core::rect<s32>(
1262 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1264 font_line_height(m_font)
1266 +((imgsize.Y/2)- m_btn_height));
1267 //actually text.length() would be correct but adding +1 avoids to break all mods
1269 if(!data->explicit_size)
1270 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1274 for (wchar_t i : text) {
1285 gui::IGUIStaticText *t =
1286 addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid);
1287 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1288 m_fields.push_back(spec);
1291 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1294 void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
1295 const std::string &type)
1297 std::vector<std::string> parts = split(element,';');
1299 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1300 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1302 std::vector<std::string> v_pos = split(parts[0],',');
1303 std::vector<std::string> v_geom = split(parts[1],',');
1304 std::string image_name = parts[2];
1305 std::string name = parts[3];
1306 std::string label = parts[4];
1308 MY_CHECKPOS("imagebutton",0);
1309 MY_CHECKGEOM("imagebutton",1);
1311 v2s32 pos = padding + pos_offset * spacing;
1312 pos.X += stof(v_pos[0]) * (float)spacing.X;
1313 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1315 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1316 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1318 bool noclip = false;
1319 bool drawborder = true;
1320 std::string pressed_image_name;
1322 if (parts.size() >= 7) {
1323 if (parts[5] == "true")
1325 if (parts[6] == "false")
1329 if (parts.size() >= 8) {
1330 pressed_image_name = parts[7];
1333 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1335 if(!data->explicit_size)
1336 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1338 image_name = unescape_string(image_name);
1339 pressed_image_name = unescape_string(pressed_image_name);
1341 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1346 utf8_to_wide(image_name),
1349 spec.ftype = f_Button;
1350 if(type == "image_button_exit")
1351 spec.is_exit = true;
1353 video::ITexture *texture = 0;
1354 video::ITexture *pressed_texture = 0;
1355 texture = m_tsrc->getTexture(image_name);
1356 if (!pressed_image_name.empty())
1357 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1359 pressed_texture = texture;
1361 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1363 if (spec.fname == data->focused_fieldname) {
1364 Environment->setFocus(e);
1367 e->setUseAlphaChannel(true);
1368 e->setImage(guiScalingImageButton(
1369 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1370 e->setPressedImage(guiScalingImageButton(
1371 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1372 e->setScaleImage(true);
1373 e->setNotClipped(noclip);
1374 e->setDrawBorder(drawborder);
1376 m_fields.push_back(spec);
1380 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1383 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
1385 std::vector<std::string> parts = split(element,';');
1387 if (((parts.size() == 4) || (parts.size() == 6)) ||
1388 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1390 std::vector<std::string> v_pos = split(parts[0],',');
1391 std::string name = parts[1];
1392 std::vector<std::string> buttons = split(parts[2],',');
1393 std::string str_index = parts[3];
1394 bool show_background = true;
1395 bool show_border = true;
1396 int tab_index = stoi(str_index) -1;
1398 MY_CHECKPOS("tabheader",0);
1400 if (parts.size() == 6) {
1401 if (parts[4] == "true")
1402 show_background = false;
1403 if (parts[5] == "false")
1404 show_border = false;
1414 spec.ftype = f_TabHeader;
1416 v2s32 pos = pos_offset * spacing;
1417 pos.X += stof(v_pos[0]) * (float)spacing.X;
1418 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1420 geom.X = DesiredRect.getWidth();
1421 geom.Y = m_btn_height*2;
1423 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1426 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1427 show_background, show_border, spec.fid);
1428 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1429 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1430 e->setTabHeight(m_btn_height*2);
1432 if (spec.fname == data->focused_fieldname) {
1433 Environment->setFocus(e);
1436 e->setNotClipped(true);
1438 for (const std::string &button : buttons) {
1439 e->addTab(unescape_enriched(unescape_string(
1440 utf8_to_wide(button))).c_str(), -1);
1443 if ((tab_index >= 0) &&
1444 (buttons.size() < INT_MAX) &&
1445 (tab_index < (int) buttons.size()))
1446 e->setActiveTab(tab_index);
1448 m_fields.push_back(spec);
1451 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1452 << element << "'" << std::endl;
1455 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
1458 if (m_client == 0) {
1459 warningstream << "invalid use of item_image_button with m_client==0"
1464 std::vector<std::string> parts = split(element,';');
1466 if ((parts.size() == 5) ||
1467 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1469 std::vector<std::string> v_pos = split(parts[0],',');
1470 std::vector<std::string> v_geom = split(parts[1],',');
1471 std::string item_name = parts[2];
1472 std::string name = parts[3];
1473 std::string label = parts[4];
1475 label = unescape_string(label);
1476 item_name = unescape_string(item_name);
1478 MY_CHECKPOS("itemimagebutton",0);
1479 MY_CHECKGEOM("itemimagebutton",1);
1481 v2s32 pos = padding + pos_offset * spacing;
1482 pos.X += stof(v_pos[0]) * (float)spacing.X;
1483 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1485 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1486 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1488 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1490 if(!data->explicit_size)
1491 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1493 IItemDefManager *idef = m_client->idef();
1495 item.deSerialize(item_name, idef);
1498 TooltipSpec(item.getDefinition(idef).description,
1499 m_default_tooltip_bgcolor,
1500 m_default_tooltip_color);
1504 utf8_to_wide(label),
1505 utf8_to_wide(item_name),
1506 258 + m_fields.size()
1509 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1511 if (spec.fname == data->focused_fieldname) {
1512 Environment->setFocus(e);
1515 spec.ftype = f_Button;
1516 rect+=data->basepos-padding;
1518 m_fields.push_back(spec);
1519 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
1520 pos.X += stof(v_pos[0]) * (float) spacing.X;
1521 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1522 m_itemimages.emplace_back("", item_name, e, pos, geom);
1523 m_static_texts.emplace_back(utf8_to_wide(label), rect, e);
1526 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1529 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
1531 std::vector<std::string> parts = split(element,';');
1533 if ((parts.size() == 3) ||
1534 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1536 std::vector<std::string> v_pos = split(parts[0],',');
1537 std::vector<std::string> v_geom = split(parts[1],',');
1539 MY_CHECKPOS("box",0);
1540 MY_CHECKGEOM("box",1);
1542 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
1543 pos.X += stof(v_pos[0]) * (float) spacing.X;
1544 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1547 geom.X = stof(v_geom[0]) * (float)spacing.X;
1548 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1550 video::SColor tmp_color;
1552 if (parseColorString(parts[2], tmp_color, false)) {
1553 BoxDrawSpec spec(pos, geom, tmp_color);
1555 m_boxes.push_back(spec);
1558 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1562 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1565 void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
1567 std::vector<std::string> parts = split(element,';');
1569 if (((parts.size() == 1) || (parts.size() == 2)) ||
1570 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1572 parseColorString(parts[0],m_bgcolor,false);
1574 if (parts.size() == 2) {
1575 std::string fullscreen = parts[1];
1576 m_bgfullscreen = is_yes(fullscreen);
1580 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1583 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
1585 std::vector<std::string> parts = split(element,';');
1587 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1588 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1590 parseColorString(parts[0], m_slotbg_n, false);
1591 parseColorString(parts[1], m_slotbg_h, false);
1593 if (parts.size() >= 3) {
1594 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1595 m_slotborder = true;
1598 if (parts.size() == 5) {
1599 video::SColor tmp_color;
1601 if (parseColorString(parts[3], tmp_color, false))
1602 m_default_tooltip_bgcolor = tmp_color;
1603 if (parseColorString(parts[4], tmp_color, false))
1604 m_default_tooltip_color = tmp_color;
1608 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1611 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
1613 std::vector<std::string> parts = split(element,';');
1614 if (parts.size() == 2) {
1615 std::string name = parts[0];
1616 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1617 m_default_tooltip_bgcolor, m_default_tooltip_color);
1621 if (parts.size() == 4) {
1622 std::string name = parts[0];
1623 video::SColor tmp_color1, tmp_color2;
1624 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1625 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1626 tmp_color1, tmp_color2);
1630 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1633 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
1639 std::vector<std::string> parts = split(data,'[');
1641 if (parts.size() < 2) {
1645 if (parts[0] != "formspec_version") {
1649 if (is_number(parts[1])) {
1650 m_formspec_version = mystoi(parts[1]);
1657 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
1659 if (element.empty())
1662 std::vector<std::string> parts = split(element,'[');
1664 if (parts.size() < 2)
1667 std::string type = trim(parts[0]);
1668 std::string description = trim(parts[1]);
1670 if (type != "size" && type != "invsize")
1673 if (type == "invsize")
1674 log_deprecated("Deprecated formspec element \"invsize\" is used");
1676 parseSize(data, description);
1681 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
1683 if (element.empty())
1686 std::vector<std::string> parts = split(element, '[');
1688 if (parts.size() != 2)
1691 std::string type = trim(parts[0]);
1692 std::string description = trim(parts[1]);
1694 if (type != "position")
1697 parsePosition(data, description);
1702 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
1704 std::vector<std::string> parts = split(element, ',');
1706 if (parts.size() == 2) {
1707 data->offset.X = stof(parts[0]);
1708 data->offset.Y = stof(parts[1]);
1712 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
1715 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
1717 if (element.empty())
1720 std::vector<std::string> parts = split(element, '[');
1722 if (parts.size() != 2)
1725 std::string type = trim(parts[0]);
1726 std::string description = trim(parts[1]);
1728 if (type != "anchor")
1731 parseAnchor(data, description);
1736 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
1738 std::vector<std::string> parts = split(element, ',');
1740 if (parts.size() == 2) {
1741 data->anchor.X = stof(parts[0]);
1742 data->anchor.Y = stof(parts[1]);
1746 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
1747 << "'" << std::endl;
1750 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
1753 if (element.empty())
1756 std::vector<std::string> parts = split(element,'[');
1758 // ugly workaround to keep compatibility
1759 if (parts.size() > 2) {
1760 if (trim(parts[0]) == "image") {
1761 for (unsigned int i=2;i< parts.size(); i++) {
1762 parts[1] += "[" + parts[i];
1768 if (parts.size() < 2) {
1772 std::string type = trim(parts[0]);
1773 std::string description = trim(parts[1]);
1775 if (type == "container") {
1776 parseContainer(data, description);
1780 if (type == "container_end") {
1781 parseContainerEnd(data);
1785 if (type == "list") {
1786 parseList(data, description);
1790 if (type == "listring") {
1791 parseListRing(data, description);
1795 if (type == "checkbox") {
1796 parseCheckbox(data, description);
1800 if (type == "image") {
1801 parseImage(data, description);
1805 if (type == "item_image") {
1806 parseItemImage(data, description);
1810 if (type == "button" || type == "button_exit") {
1811 parseButton(data, description, type);
1815 if (type == "background") {
1816 parseBackground(data,description);
1820 if (type == "tableoptions"){
1821 parseTableOptions(data,description);
1825 if (type == "tablecolumns"){
1826 parseTableColumns(data,description);
1830 if (type == "table"){
1831 parseTable(data,description);
1835 if (type == "textlist"){
1836 parseTextList(data,description);
1840 if (type == "dropdown"){
1841 parseDropDown(data,description);
1845 if (type == "field_close_on_enter") {
1846 parseFieldCloseOnEnter(data, description);
1850 if (type == "pwdfield") {
1851 parsePwdField(data,description);
1855 if ((type == "field") || (type == "textarea")){
1856 parseField(data,description,type);
1860 if (type == "label") {
1861 parseLabel(data,description);
1865 if (type == "vertlabel") {
1866 parseVertLabel(data,description);
1870 if (type == "item_image_button") {
1871 parseItemImageButton(data,description);
1875 if ((type == "image_button") || (type == "image_button_exit")) {
1876 parseImageButton(data,description,type);
1880 if (type == "tabheader") {
1881 parseTabHeader(data,description);
1885 if (type == "box") {
1886 parseBox(data,description);
1890 if (type == "bgcolor") {
1891 parseBackgroundColor(data,description);
1895 if (type == "listcolors") {
1896 parseListColors(data,description);
1900 if (type == "tooltip") {
1901 parseTooltip(data,description);
1905 if (type == "scrollbar") {
1906 parseScrollBar(data, description);
1912 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1916 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1918 /* useless to regenerate without a screensize */
1919 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1926 for (auto &m_table : m_tables) {
1927 std::string tablename = m_table.first.fname;
1928 GUITable *table = m_table.second;
1929 mydata.table_dyndata[tablename] = table->getDynamicData();
1933 if (!m_focused_element.empty())
1934 mydata.focused_fieldname = m_focused_element;
1937 gui::IGUIElement *focused_element = Environment->getFocus();
1938 if (focused_element && focused_element->getParent() == this) {
1939 s32 focused_id = focused_element->getID();
1940 if (focused_id > 257) {
1941 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
1942 if (field.fid == focused_id) {
1943 mydata.focused_fieldname = field.fname;
1953 for (auto &table_it : m_tables) {
1954 table_it.second->drop();
1957 mydata.size= v2s32(100,100);
1958 mydata.screensize = screensize;
1959 mydata.offset = v2f32(0.5f, 0.5f);
1960 mydata.anchor = v2f32(0.5f, 0.5f);
1962 // Base position of contents of form
1963 mydata.basepos = getBasePos();
1965 /* Convert m_init_draw_spec to m_inventorylists */
1967 m_inventorylists.clear();
1969 m_backgrounds.clear();
1970 m_itemimages.clear();
1972 m_checkboxes.clear();
1973 m_scrollbars.clear();
1977 m_inventory_rings.clear();
1978 m_static_texts.clear();
1979 m_dropdowns.clear();
1981 // Set default values (fits old formspec values)
1982 m_bgcolor = video::SColor(140,0,0,0);
1983 m_bgfullscreen = false;
1985 m_slotbg_n = video::SColor(255,128,128,128);
1986 m_slotbg_h = video::SColor(255,192,192,192);
1988 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1989 m_default_tooltip_color = video::SColor(255,255,255,255);
1991 m_slotbordercolor = video::SColor(200,0,0,0);
1992 m_slotborder = false;
1996 assert(!m_tooltip_element);
1997 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1998 m_tooltip_element = addStaticText(Environment, L"",core::rect<s32>(0,0,110,18));
1999 m_tooltip_element->enableOverrideColor(true);
2000 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2001 m_tooltip_element->setDrawBackground(true);
2002 m_tooltip_element->setDrawBorder(true);
2003 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2004 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
2005 m_tooltip_element->setWordWrap(false);
2006 //we're not parent so no autograb for this one!
2007 m_tooltip_element->grab();
2010 std::vector<std::string> elements = split(m_formspec_string,']');
2013 /* try to read version from first element only */
2014 if (!elements.empty()) {
2015 if ( parseVersionDirect(elements[0]) ) {
2020 /* we need size first in order to calculate image scale */
2021 mydata.explicit_size = false;
2022 for (; i< elements.size(); i++) {
2023 if (!parseSizeDirect(&mydata, elements[i])) {
2028 /* "position" element is always after "size" element if it used */
2029 for (; i< elements.size(); i++) {
2030 if (!parsePositionDirect(&mydata, elements[i])) {
2035 /* "anchor" element is always after "position" (or "size" element) if it used */
2036 for (; i< elements.size(); i++) {
2037 if (!parseAnchorDirect(&mydata, elements[i])) {
2043 if (mydata.explicit_size) {
2044 // compute scaling for specified form size
2046 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
2047 v2u32 delta = current_screensize - m_lockscreensize;
2049 if (current_screensize.Y > m_lockscreensize.Y)
2054 if (current_screensize.X > m_lockscreensize.X)
2059 offset = v2s32(delta.X,delta.Y);
2061 mydata.screensize = m_lockscreensize;
2063 offset = v2s32(0,0);
2066 double gui_scaling = g_settings->getFloat("gui_scaling");
2067 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2071 // In fixed-size mode, inventory image size
2072 // is 0.53 inch multiplied by the gui_scaling
2073 // config parameter. This magic size is chosen
2074 // to make the main menu (15.5 inventory images
2075 // wide, including border) just fit into the
2076 // default window (800 pixels wide) at 96 DPI
2077 // and default scaling (1.00).
2078 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
2080 // In variable-size mode, we prefer to make the
2081 // inventory image size 1/15 of screen height,
2082 // multiplied by the gui_scaling config parameter.
2083 // If the preferred size won't fit the whole
2084 // form on the screen, either horizontally or
2085 // vertically, then we scale it down to fit.
2086 // (The magic numbers in the computation of what
2087 // fits arise from the scaling factors in the
2088 // following stanza, including the form border,
2089 // help text space, and 0.1 inventory slot spare.)
2090 // However, a minimum size is also set, that
2091 // the image size can't be less than 0.3 inch
2092 // multiplied by gui_scaling, even if this means
2093 // the form doesn't fit the screen.
2094 double prefer_imgsize = mydata.screensize.Y / 15 *
2096 double fitx_imgsize = mydata.screensize.X /
2097 ((5.0/4.0) * (0.5 + mydata.invsize.X));
2098 double fity_imgsize = mydata.screensize.Y /
2099 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
2100 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2101 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
2102 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2103 MYMIN(fitx_imgsize, fity_imgsize)));
2106 // Everything else is scaled in proportion to the
2107 // inventory image size. The inventory slot spacing
2108 // is 5/4 image size horizontally and 15/13 image size
2109 // vertically. The padding around the form (incorporating
2110 // the border of the outer inventory slots) is 3/8
2111 // image size. Font height (baseline to baseline)
2112 // is 2/5 vertical inventory slot spacing, and button
2113 // half-height is 7/8 of font height.
2114 imgsize = v2s32(use_imgsize, use_imgsize);
2115 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2116 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2117 m_btn_height = use_imgsize*15.0/13 * 0.35;
2119 m_font = g_fontengine->getFont();
2121 mydata.size = v2s32(
2122 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2123 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2125 DesiredRect = mydata.rect = core::rect<s32>(
2126 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
2127 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
2128 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
2129 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
2132 // Non-size[] form must consist only of text fields and
2133 // implicit "Proceed" button. Use default font, and
2134 // temporary form size which will be recalculated below.
2135 m_font = g_fontengine->getFont();
2136 m_btn_height = font_line_height(m_font) * 0.875;
2137 DesiredRect = core::rect<s32>(
2138 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
2139 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
2140 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
2141 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
2144 recalculateAbsolutePosition(false);
2145 mydata.basepos = getBasePos();
2146 m_tooltip_element->setOverrideFont(m_font);
2148 gui::IGUISkin* skin = Environment->getSkin();
2150 gui::IGUIFont *old_font = skin->getFont();
2151 skin->setFont(m_font);
2153 pos_offset = v2s32();
2154 for (; i< elements.size(); i++) {
2155 parseElement(&mydata, elements[i]);
2158 if (!container_stack.empty()) {
2159 errorstream << "Invalid formspec string: container was never closed!"
2163 // If there are fields without explicit size[], add a "Proceed"
2164 // button and adjust size to fit all the fields.
2165 if (!m_fields.empty() && !mydata.explicit_size) {
2166 mydata.rect = core::rect<s32>(
2167 mydata.screensize.X/2 - 580/2,
2168 mydata.screensize.Y/2 - 300/2,
2169 mydata.screensize.X/2 + 580/2,
2170 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2172 DesiredRect = mydata.rect;
2173 recalculateAbsolutePosition(false);
2174 mydata.basepos = getBasePos();
2177 v2s32 pos = mydata.basepos;
2178 pos.Y = ((m_fields.size()+2)*60);
2180 v2s32 size = DesiredRect.getSize();
2182 core::rect<s32>(size.X/2-70, pos.Y,
2183 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2184 const wchar_t *text = wgettext("Proceed");
2185 Environment->addButton(mydata.rect, this, 257, text);
2191 //set initial focus if parser didn't set it
2192 focused_element = Environment->getFocus();
2193 if (!focused_element
2194 || !isMyChild(focused_element)
2195 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2198 skin->setFont(old_font);
2202 bool GUIFormSpecMenu::getAndroidUIInput()
2204 /* no dialog shown */
2205 if (m_JavaDialogFieldName == "") {
2210 if (porting::getInputDialogState() == -1) {
2214 std::string fieldname = m_JavaDialogFieldName;
2215 m_JavaDialogFieldName = "";
2217 /* no value abort dialog processing */
2218 if (porting::getInputDialogState() != 0) {
2222 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2223 iter != m_fields.end(); ++iter) {
2225 if (iter->fname != fieldname) {
2228 IGUIElement* tochange = getElementFromId(iter->fid);
2230 if (tochange == 0) {
2234 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2238 std::string text = porting::getInputDialogValue();
2240 ((gui::IGUIEditBox*) tochange)->
2241 setText(utf8_to_wide(text).c_str());
2247 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2249 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2251 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
2252 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2253 s32 item_i = i + s.start_item_i;
2254 s32 x = (i%s.geom.X) * spacing.X;
2255 s32 y = (i/s.geom.X) * spacing.Y;
2257 core::rect<s32> rect = imgrect + s.pos + p0;
2258 if(rect.isPointInside(p))
2260 return ItemSpec(s.inventoryloc, s.listname, item_i);
2265 return ItemSpec(InventoryLocation(), "", -1);
2268 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
2271 video::IVideoDriver* driver = Environment->getVideoDriver();
2273 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2275 warningstream<<"GUIFormSpecMenu::drawList(): "
2276 <<"The inventory location "
2277 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2281 InventoryList *ilist = inv->getList(s.listname);
2283 warningstream<<"GUIFormSpecMenu::drawList(): "
2284 <<"The inventory list \""<<s.listname<<"\" @ \""
2285 <<s.inventoryloc.dump()<<"\" doesn't exist"
2290 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2292 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2294 s32 item_i = i + s.start_item_i;
2295 if(item_i >= (s32) ilist->getSize())
2297 s32 x = (i%s.geom.X) * spacing.X;
2298 s32 y = (i/s.geom.X) * spacing.Y;
2300 core::rect<s32> rect = imgrect + s.pos + p;
2303 item = ilist->getItem(item_i);
2305 bool selected = m_selected_item
2306 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2307 && m_selected_item->listname == s.listname
2308 && m_selected_item->i == item_i;
2309 bool hovering = rect.isPointInside(m_pointer);
2310 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2311 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2315 item_hovered = true;
2316 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2318 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2322 //Draw inv slot borders
2324 s32 x1 = rect.UpperLeftCorner.X;
2325 s32 y1 = rect.UpperLeftCorner.Y;
2326 s32 x2 = rect.LowerRightCorner.X;
2327 s32 y2 = rect.LowerRightCorner.Y;
2329 driver->draw2DRectangle(m_slotbordercolor,
2330 core::rect<s32>(v2s32(x1 - border, y1 - border),
2331 v2s32(x2 + border, y1)), NULL);
2332 driver->draw2DRectangle(m_slotbordercolor,
2333 core::rect<s32>(v2s32(x1 - border, y2),
2334 v2s32(x2 + border, y2 + border)), NULL);
2335 driver->draw2DRectangle(m_slotbordercolor,
2336 core::rect<s32>(v2s32(x1 - border, y1),
2337 v2s32(x1, y2)), NULL);
2338 driver->draw2DRectangle(m_slotbordercolor,
2339 core::rect<s32>(v2s32(x2, y1),
2340 v2s32(x2 + border, y2)), NULL);
2348 item.takeItem(m_selected_amount);
2352 drawItemStack(driver, m_font, item,
2353 rect, &AbsoluteClippingRect, m_client,
2358 std::wstring tooltip_text;
2359 if (hovering && !m_selected_item) {
2360 const std::string &desc = item.metadata.getString("description");
2363 utf8_to_wide(item.getDefinition(m_client->idef()).description);
2365 tooltip_text = utf8_to_wide(desc);
2366 // Show itemstring as fallback for easier debugging
2367 if (!item.name.empty() && tooltip_text.empty())
2368 tooltip_text = utf8_to_wide(item.name);
2370 if (!tooltip_text.empty()) {
2371 showTooltip(tooltip_text, m_default_tooltip_color,
2372 m_default_tooltip_bgcolor);
2378 void GUIFormSpecMenu::drawSelectedItem()
2380 video::IVideoDriver* driver = Environment->getVideoDriver();
2382 if (!m_selected_item) {
2383 drawItemStack(driver, m_font, ItemStack(),
2384 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2385 NULL, m_client, IT_ROT_DRAGGED);
2389 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2391 InventoryList *list = inv->getList(m_selected_item->listname);
2393 ItemStack stack = list->getItem(m_selected_item->i);
2394 stack.count = m_selected_amount;
2396 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2397 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2398 rect.constrainTo(driver->getViewPort());
2399 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
2402 void GUIFormSpecMenu::drawMenu()
2405 std::string newform = m_form_src->getForm();
2406 if(newform != m_formspec_string){
2407 m_formspec_string = newform;
2408 regenerateGui(m_screensize_old);
2412 gui::IGUISkin* skin = Environment->getSkin();
2413 sanity_check(skin != NULL);
2414 gui::IGUIFont *old_font = skin->getFont();
2415 skin->setFont(m_font);
2417 updateSelectedItem();
2419 video::IVideoDriver* driver = Environment->getVideoDriver();
2421 v2u32 screenSize = driver->getScreenSize();
2422 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2424 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2426 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2428 m_tooltip_element->setVisible(false);
2433 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_backgrounds) {
2434 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2437 // Image size on screen
2438 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2439 // Image rectangle on screen
2440 core::rect<s32> rect = imgrect + spec.pos;
2443 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2444 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2445 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2446 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2447 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2450 const video::SColor color(255,255,255,255);
2451 const video::SColor colors[] = {color,color,color,color};
2452 draw2DImageFilterScaled(driver, texture, rect,
2453 core::rect<s32>(core::position2d<s32>(0,0),
2454 core::dimension2di(texture->getOriginalSize())),
2455 NULL/*&AbsoluteClippingRect*/, colors, true);
2457 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2458 errorstream << "\t" << spec.name << std::endl;
2465 for (const GUIFormSpecMenu::BoxDrawSpec &spec : m_boxes) {
2466 irr::video::SColor todraw = spec.color;
2468 todraw.setAlpha(140);
2470 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2471 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2473 driver->draw2DRectangle(todraw, rect, 0);
2479 gui::IGUIElement::draw();
2484 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_images) {
2485 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2488 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2489 // Image size on screen
2490 core::rect<s32> imgrect;
2493 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2496 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2498 // Image rectangle on screen
2499 core::rect<s32> rect = imgrect + spec.pos;
2500 const video::SColor color(255,255,255,255);
2501 const video::SColor colors[] = {color,color,color,color};
2502 draw2DImageFilterScaled(driver, texture, rect,
2503 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2504 NULL/*&AbsoluteClippingRect*/, colors, true);
2507 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2508 errorstream << "\t" << spec.name << std::endl;
2515 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_itemimages) {
2519 IItemDefManager *idef = m_client->idef();
2521 item.deSerialize(spec.item_name, idef);
2522 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2523 // Viewport rectangle on screen
2524 core::rect<s32> rect = imgrect + spec.pos;
2525 if (spec.parent_button && spec.parent_button->isPressed()) {
2526 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2527 rect += core::dimension2d<s32>(
2528 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2530 rect += core::dimension2d<s32>(
2531 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2532 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2535 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2536 m_client, IT_ROT_NONE);
2541 Phase 0: Item slot rectangles
2542 Phase 1: Item images; prepare tooltip
2544 bool item_hovered = false;
2545 int start_phase = 0;
2546 for (int phase = start_phase; phase <= 1; phase++) {
2547 for (const GUIFormSpecMenu::ListDrawSpec &spec : m_inventorylists) {
2548 drawList(spec, phase, item_hovered);
2551 if (!item_hovered) {
2552 drawItemStack(driver, m_font, ItemStack(),
2553 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2554 NULL, m_client, IT_ROT_HOVERED);
2557 /* TODO find way to show tooltips on touchscreen */
2558 #ifndef HAVE_TOUCHSCREENGUI
2559 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
2563 Draw static text elements
2565 for (const GUIFormSpecMenu::StaticTextSpec &spec : m_static_texts) {
2566 core::rect<s32> rect = spec.rect;
2567 if (spec.parent_button && spec.parent_button->isPressed()) {
2568 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2569 rect += core::dimension2d<s32>(
2570 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2572 // Use image offset instead of text's because its a bit smaller
2573 // and fits better, also TEXT_OFFSET_X is always 0
2574 rect += core::dimension2d<s32>(
2575 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2576 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2579 video::SColor color(255, 255, 255, 255);
2580 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
2584 Draw fields/buttons tooltips
2586 gui::IGUIElement *hovered =
2587 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2589 if (hovered != NULL) {
2590 s32 id = hovered->getID();
2594 m_old_tooltip_id = id;
2596 if (id == m_old_tooltip_id) {
2597 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
2599 m_hovered_time = porting::getTimeMs();
2600 m_old_tooltip_id = id;
2604 // Find and update the current tooltip
2605 if (id != -1 && delta >= m_tooltip_show_delay) {
2606 for (const FieldSpec &field : m_fields) {
2608 if (field.fid != id)
2611 const std::wstring &text = m_tooltips[field.fname].tooltip;
2613 showTooltip(text, m_tooltips[field.fname].color,
2614 m_tooltips[field.fname].bgcolor);
2621 m_tooltip_element->draw();
2624 Draw dragged item stack
2628 skin->setFont(old_font);
2632 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
2633 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
2635 m_tooltip_element->setOverrideColor(color);
2636 m_tooltip_element->setBackgroundColor(bgcolor);
2637 setStaticText(m_tooltip_element, text.c_str());
2639 // Tooltip size and offset
2640 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2641 #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
2642 std::vector<std::wstring> text_rows = str_split(text, L'\n');
2643 s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
2645 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2647 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
2648 int tooltip_offset_x = m_btn_height;
2649 int tooltip_offset_y = m_btn_height;
2651 tooltip_offset_x *= 3;
2652 tooltip_offset_y = 0;
2653 if (m_pointer.X > (s32)screenSize.X / 2)
2654 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
2657 // Calculate and set the tooltip position
2658 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2659 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2660 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2661 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2662 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2663 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2665 m_tooltip_element->setRelativePosition(
2667 core::position2d<s32>(tooltip_x, tooltip_y),
2668 core::dimension2d<s32>(tooltip_width, tooltip_height)
2672 // Display the tooltip
2673 m_tooltip_element->setVisible(true);
2674 bringToFront(m_tooltip_element);
2677 void GUIFormSpecMenu::updateSelectedItem()
2679 // If the selected stack has become empty for some reason, deselect it.
2680 // If the selected stack has become inaccessible, deselect it.
2681 // If the selected stack has become smaller, adjust m_selected_amount.
2682 ItemStack selected = verifySelectedItem();
2684 // WARNING: BLACK MAGIC
2685 // See if there is a stack suited for our current guess.
2686 // If such stack does not exist, clear the guess.
2687 if (!m_selected_content_guess.name.empty() &&
2688 selected.name == m_selected_content_guess.name &&
2689 selected.count == m_selected_content_guess.count){
2690 // Selected item fits the guess. Skip the black magic.
2691 } else if (!m_selected_content_guess.name.empty()) {
2693 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2694 const ListDrawSpec &s = m_inventorylists[i];
2695 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2698 InventoryList *list = inv->getList(s.listname);
2701 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2702 u32 item_i = i + s.start_item_i;
2703 if(item_i >= list->getSize())
2705 ItemStack stack = list->getItem(item_i);
2706 if(stack.name == m_selected_content_guess.name &&
2707 stack.count == m_selected_content_guess.count){
2709 infostream<<"Client: Changing selected content guess to "
2710 <<s.inventoryloc.dump()<<" "<<s.listname
2711 <<" "<<item_i<<std::endl;
2712 delete m_selected_item;
2713 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2714 m_selected_amount = stack.count;
2719 infostream<<"Client: Discarding selected content guess: "
2720 <<m_selected_content_guess.getItemString()<<std::endl;
2721 m_selected_content_guess.name = "";
2725 // If craftresult is nonempty and nothing else is selected, select it now.
2726 if(!m_selected_item)
2728 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
2729 if(s.listname == "craftpreview")
2731 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2732 InventoryList *list = inv->getList("craftresult");
2733 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2735 m_selected_item = new ItemSpec;
2736 m_selected_item->inventoryloc = s.inventoryloc;
2737 m_selected_item->listname = "craftresult";
2738 m_selected_item->i = 0;
2739 m_selected_amount = 0;
2740 m_selected_dragging = false;
2747 // If craftresult is selected, keep the whole stack selected
2748 if(m_selected_item && m_selected_item->listname == "craftresult")
2750 m_selected_amount = verifySelectedItem().count;
2754 ItemStack GUIFormSpecMenu::verifySelectedItem()
2756 // If the selected stack has become empty for some reason, deselect it.
2757 // If the selected stack has become inaccessible, deselect it.
2758 // If the selected stack has become smaller, adjust m_selected_amount.
2759 // Return the selected stack.
2763 if(m_selected_item->isValid())
2765 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2768 InventoryList *list = inv->getList(m_selected_item->listname);
2769 if(list && (u32) m_selected_item->i < list->getSize())
2771 ItemStack stack = list->getItem(m_selected_item->i);
2772 if(m_selected_amount > stack.count)
2773 m_selected_amount = stack.count;
2780 // selection was not valid
2781 delete m_selected_item;
2782 m_selected_item = NULL;
2783 m_selected_amount = 0;
2784 m_selected_dragging = false;
2789 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2795 if (quitmode == quit_mode_accept) {
2796 fields["quit"] = "true";
2799 if (quitmode == quit_mode_cancel) {
2800 fields["quit"] = "true";
2801 m_text_dst->gotText(fields);
2805 if (current_keys_pending.key_down) {
2806 fields["key_down"] = "true";
2807 current_keys_pending.key_down = false;
2810 if (current_keys_pending.key_up) {
2811 fields["key_up"] = "true";
2812 current_keys_pending.key_up = false;
2815 if (current_keys_pending.key_enter) {
2816 fields["key_enter"] = "true";
2817 current_keys_pending.key_enter = false;
2820 if (!current_field_enter_pending.empty()) {
2821 fields["key_enter_field"] = current_field_enter_pending;
2822 current_field_enter_pending = "";
2825 if (current_keys_pending.key_escape) {
2826 fields["key_escape"] = "true";
2827 current_keys_pending.key_escape = false;
2830 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
2832 std::string name = s.fname;
2833 if (s.ftype == f_Button) {
2834 fields[name] = wide_to_utf8(s.flabel);
2835 } else if (s.ftype == f_Table) {
2836 GUITable *table = getTable(s.fname);
2838 fields[name] = table->checkEvent();
2841 else if(s.ftype == f_DropDown) {
2842 // no dynamic cast possible due to some distributions shipped
2843 // without rtti support in irrlicht
2844 IGUIElement * element = getElementFromId(s.fid);
2845 gui::IGUIComboBox *e = NULL;
2846 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2847 e = dynamic_cast<gui::IGUIComboBox*>(element);
2849 s32 selected = e->getSelected();
2850 if (selected >= 0) {
2851 std::vector<std::string> *dropdown_values =
2852 getDropDownValues(s.fname);
2853 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2854 fields[name] = (*dropdown_values)[selected];
2858 else if (s.ftype == f_TabHeader) {
2859 // no dynamic cast possible due to some distributions shipped
2860 // without rtti support in irrlicht
2861 IGUIElement * element = getElementFromId(s.fid);
2862 gui::IGUITabControl *e = NULL;
2863 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2864 e = dynamic_cast<gui::IGUITabControl*>(element);
2868 std::stringstream ss;
2869 ss << (e->getActiveTab() +1);
2870 fields[name] = ss.str();
2873 else if (s.ftype == f_CheckBox) {
2874 // no dynamic cast possible due to some distributions shipped
2875 // without rtti support in irrlicht
2876 IGUIElement * element = getElementFromId(s.fid);
2877 gui::IGUICheckBox *e = NULL;
2878 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2879 e = dynamic_cast<gui::IGUICheckBox*>(element);
2884 fields[name] = "true";
2886 fields[name] = "false";
2889 else if (s.ftype == f_ScrollBar) {
2890 // no dynamic cast possible due to some distributions shipped
2891 // without rtti support in irrlicht
2892 IGUIElement * element = getElementFromId(s.fid);
2893 gui::IGUIScrollBar *e = NULL;
2894 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2895 e = dynamic_cast<gui::IGUIScrollBar*>(element);
2899 std::stringstream os;
2901 if (s.fdefault == L"Changed")
2902 fields[name] = "CHG:" + os.str();
2904 fields[name] = "VAL:" + os.str();
2909 IGUIElement* e = getElementFromId(s.fid);
2911 fields[name] = wide_to_utf8(e->getText());
2917 m_text_dst->gotText(fields);
2921 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2923 while(tocheck != NULL) {
2924 if (tocheck == parent) {
2927 tocheck = tocheck->getParent();
2932 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2934 // The IGUITabControl renders visually using the skin's selected
2935 // font, which we override for the duration of form drawing,
2936 // but computes tab hotspots based on how it would have rendered
2937 // using the font that is selected at the time of button release.
2938 // To make these two consistent, temporarily override the skin's
2939 // font while the IGUITabControl is processing the event.
2940 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2941 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2942 s32 x = event.MouseInput.X;
2943 s32 y = event.MouseInput.Y;
2944 gui::IGUIElement *hovered =
2945 Environment->getRootGUIElement()->getElementFromPoint(
2946 core::position2d<s32>(x, y));
2947 if (hovered && isMyChild(hovered) &&
2948 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2949 gui::IGUISkin* skin = Environment->getSkin();
2950 sanity_check(skin != NULL);
2951 gui::IGUIFont *old_font = skin->getFont();
2952 skin->setFont(m_font);
2953 bool retval = hovered->OnEvent(event);
2954 skin->setFont(old_font);
2959 // Fix Esc/Return key being eaten by checkboxen and tables
2960 if(event.EventType==EET_KEY_INPUT_EVENT) {
2961 KeyPress kp(event.KeyInput);
2962 if (kp == EscapeKey || kp == CancelKey
2963 || kp == getKeySetting("keymap_inventory")
2964 || event.KeyInput.Key==KEY_RETURN) {
2965 gui::IGUIElement *focused = Environment->getFocus();
2966 if (focused && isMyChild(focused) &&
2967 (focused->getType() == gui::EGUIET_LIST_BOX ||
2968 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2974 // Mouse wheel events: send to hovered element instead of focused
2975 if(event.EventType==EET_MOUSE_INPUT_EVENT
2976 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2977 s32 x = event.MouseInput.X;
2978 s32 y = event.MouseInput.Y;
2979 gui::IGUIElement *hovered =
2980 Environment->getRootGUIElement()->getElementFromPoint(
2981 core::position2d<s32>(x, y));
2982 if (hovered && isMyChild(hovered)) {
2983 hovered->OnEvent(event);
2988 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2989 s32 x = event.MouseInput.X;
2990 s32 y = event.MouseInput.Y;
2991 gui::IGUIElement *hovered =
2992 Environment->getRootGUIElement()->getElementFromPoint(
2993 core::position2d<s32>(x, y));
2994 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2995 m_old_tooltip_id = -1;
2997 if (!isChild(hovered,this)) {
2998 if (DoubleClickDetection(event)) {
3005 // display software keyboard when clicking edit boxes
3006 if (event.EventType == EET_MOUSE_INPUT_EVENT
3007 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3008 gui::IGUIElement *hovered =
3009 Environment->getRootGUIElement()->getElementFromPoint(
3010 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
3011 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
3012 bool retval = hovered->OnEvent(event);
3014 Environment->setFocus(hovered);
3016 m_JavaDialogFieldName = getNameByID(hovered->getID());
3017 std::string message = gettext("Enter ");
3018 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
3022 message += gettext(label) + ":";
3024 /* single line text input */
3027 /* multi line text input */
3028 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
3032 /* passwords are always single line */
3033 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
3037 porting::showInputDialog(gettext("ok"), "",
3038 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
3044 if (event.EventType == EET_TOUCH_INPUT_EVENT)
3047 memset(&translated, 0, sizeof(SEvent));
3048 translated.EventType = EET_MOUSE_INPUT_EVENT;
3049 gui::IGUIElement* root = Environment->getRootGUIElement();
3053 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
3057 gui::IGUIElement* hovered = root->getElementFromPoint(
3058 core::position2d<s32>(
3060 event.TouchInput.Y));
3062 translated.MouseInput.X = event.TouchInput.X;
3063 translated.MouseInput.Y = event.TouchInput.Y;
3064 translated.MouseInput.Control = false;
3066 bool dont_send_event = false;
3068 if (event.TouchInput.touchedCount == 1) {
3069 switch (event.TouchInput.Event) {
3070 case ETIE_PRESSED_DOWN:
3071 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
3072 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
3073 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3074 m_down_pos = m_pointer;
3077 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
3078 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
3079 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3082 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
3083 translated.MouseInput.ButtonStates = 0;
3084 hovered = root->getElementFromPoint(m_down_pos);
3085 /* we don't have a valid pointer element use last
3086 * known pointer pos */
3087 translated.MouseInput.X = m_pointer.X;
3088 translated.MouseInput.Y = m_pointer.Y;
3090 /* reset down pos */
3091 m_down_pos = v2s32(0,0);
3094 dont_send_event = true;
3095 //this is not supposed to happen
3097 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
3098 << event.TouchInput.Event << std::endl;
3100 } else if ( (event.TouchInput.touchedCount == 2) &&
3101 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
3102 hovered = root->getElementFromPoint(m_down_pos);
3104 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
3105 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
3106 translated.MouseInput.X = m_pointer.X;
3107 translated.MouseInput.Y = m_pointer.Y;
3110 hovered->OnEvent(translated);
3113 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
3114 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3118 hovered->OnEvent(translated);
3120 dont_send_event = true;
3122 /* ignore unhandled 2 touch events ... accidental moving for example */
3123 else if (event.TouchInput.touchedCount == 2) {
3124 dont_send_event = true;
3126 else if (event.TouchInput.touchedCount > 2) {
3128 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
3129 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3132 if (dont_send_event) {
3136 /* check if translated event needs to be preprocessed again */
3137 if (preprocessEvent(translated)) {
3142 bool retval = hovered->OnEvent(translated);
3144 if (event.TouchInput.Event == ETIE_LEFT_UP) {
3146 m_pointer = v2s32(0,0);
3154 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3155 /* TODO add a check like:
3156 if (event.JoystickEvent != joystick_we_listen_for)
3159 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3161 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3163 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3165 acceptInput(quit_mode_accept);
3176 /******************************************************************************/
3177 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3179 /* The following code is for capturing double-clicks of the mouse button
3180 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3181 * -- which closes the form -- under some circumstances.
3183 * There have been many github issues reporting this as a bug even though it
3184 * was an intended feature. For this reason, remapping the double-click as
3185 * an ESC must be explicitly set when creating this class via the
3186 * /p remap_dbl_click parameter of the constructor.
3189 if (!m_remap_dbl_click)
3192 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3193 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3194 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3196 m_doubleclickdetect[1].pos = m_pointer;
3197 m_doubleclickdetect[1].time = porting::getTimeMs();
3199 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3200 u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
3205 double squaredistance =
3206 m_doubleclickdetect[0].pos
3207 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3209 if (squaredistance > (30*30)) {
3213 SEvent* translated = new SEvent();
3214 assert(translated != 0);
3215 //translate doubleclick to escape
3216 memset(translated, 0, sizeof(SEvent));
3217 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3218 translated->KeyInput.Key = KEY_ESCAPE;
3219 translated->KeyInput.Control = false;
3220 translated->KeyInput.Shift = false;
3221 translated->KeyInput.PressedDown = true;
3222 translated->KeyInput.Char = 0;
3223 OnEvent(*translated);
3225 // no need to send the key up event as we're already deleted
3226 // and no one else did notice this event
3234 void GUIFormSpecMenu::tryClose()
3238 acceptInput(quit_mode_cancel);
3241 m_text_dst->gotText(L"MenuQuit");
3245 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3247 if (event.EventType==EET_KEY_INPUT_EVENT) {
3248 KeyPress kp(event.KeyInput);
3249 if (event.KeyInput.PressedDown && (
3250 (kp == EscapeKey) || (kp == CancelKey) ||
3251 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
3256 if (m_client != NULL && event.KeyInput.PressedDown &&
3257 (kp == getKeySetting("keymap_screenshot"))) {
3258 m_client->makeScreenshot();
3260 if (event.KeyInput.PressedDown &&
3261 (event.KeyInput.Key==KEY_RETURN ||
3262 event.KeyInput.Key==KEY_UP ||
3263 event.KeyInput.Key==KEY_DOWN)
3265 switch (event.KeyInput.Key) {
3267 current_keys_pending.key_enter = true;
3270 current_keys_pending.key_up = true;
3273 current_keys_pending.key_down = true;
3277 //can't happen at all!
3278 FATAL_ERROR("Reached a source line that can't ever been reached");
3281 if (current_keys_pending.key_enter && m_allowclose) {
3282 acceptInput(quit_mode_accept);
3292 /* Mouse event other than movement, or crossing the border of inventory
3293 field while holding right mouse button
3295 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3296 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3297 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3298 event.MouseInput.isRightPressed() &&
3299 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3301 // Get selected item and hovered/clicked item (s)
3303 m_old_tooltip_id = -1;
3304 updateSelectedItem();
3305 ItemSpec s = getItemAtPos(m_pointer);
3307 Inventory *inv_selected = NULL;
3308 Inventory *inv_s = NULL;
3309 InventoryList *list_s = NULL;
3311 if (m_selected_item) {
3312 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3313 sanity_check(inv_selected);
3314 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3321 inv_s = m_invmgr->getInventory(s.inventoryloc);
3324 errorstream << "InventoryMenu: The selected inventory location "
3325 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3327 s.i = -1; // make it invalid again
3331 list_s = inv_s->getList(s.listname);
3332 if (list_s == NULL) {
3333 verbosestream << "InventoryMenu: The selected inventory list \""
3334 << s.listname << "\" does not exist" << std::endl;
3335 s.i = -1; // make it invalid again
3339 if ((u32)s.i >= list_s->getSize()) {
3340 infostream << "InventoryMenu: The selected inventory list \""
3341 << s.listname << "\" is too small (i=" << s.i << ", size="
3342 << list_s->getSize() << ")" << std::endl;
3343 s.i = -1; // make it invalid again
3347 s_count = list_s->getItem(s.i).count;
3350 bool identical = (m_selected_item != NULL) && s.isValid() &&
3351 (inv_selected == inv_s) &&
3352 (m_selected_item->listname == s.listname) &&
3353 (m_selected_item->i == s.i);
3355 // buttons: 0 = left, 1 = right, 2 = middle
3356 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3359 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3360 { button = 0; updown = 0; }
3361 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3362 { button = 1; updown = 0; }
3363 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3364 { button = 2; updown = 0; }
3365 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3366 { button = 0; updown = 1; }
3367 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3368 { button = 1; updown = 1; }
3369 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3370 { button = 2; updown = 1; }
3371 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3374 // Set this number to a positive value to generate a move action
3375 // from m_selected_item to s.
3376 u32 move_amount = 0;
3378 // Set this number to a positive value to generate a move action
3379 // from s to the next inventory ring.
3380 u32 shift_move_amount = 0;
3382 // Set this number to a positive value to generate a drop action
3383 // from m_selected_item.
3384 u32 drop_amount = 0;
3386 // Set this number to a positive value to generate a craft action at s.
3387 u32 craft_amount = 0;
3390 // Some mouse button has been pressed
3392 //infostream<<"Mouse button "<<button<<" pressed at p=("
3393 // <<p.X<<","<<p.Y<<")"<<std::endl;
3395 m_selected_dragging = false;
3397 if (s.isValid() && s.listname == "craftpreview") {
3398 // Craft preview has been clicked: craft
3399 craft_amount = (button == 2 ? 10 : 1);
3400 } else if (m_selected_item == NULL) {
3402 // Non-empty stack has been clicked: select or shift-move it
3403 m_selected_item = new ItemSpec(s);
3406 if (button == 1) // right
3407 count = (s_count + 1) / 2;
3408 else if (button == 2) // middle
3409 count = MYMIN(s_count, 10);
3413 if (!event.MouseInput.Shift) {
3414 // no shift: select item
3415 m_selected_amount = count;
3416 m_selected_dragging = true;
3417 m_rmouse_auto_place = false;
3419 // shift pressed: move item
3421 shift_move_amount = count;
3422 else // count of 1 at left click like after drag & drop
3423 shift_move_amount = 1;
3426 } else { // m_selected_item != NULL
3427 assert(m_selected_amount >= 1);
3430 // Clicked a slot: move
3431 if (button == 1) // right
3433 else if (button == 2) // middle
3434 move_amount = MYMIN(m_selected_amount, 10);
3436 move_amount = m_selected_amount;
3439 if (move_amount >= m_selected_amount)
3440 m_selected_amount = 0;
3442 m_selected_amount -= move_amount;
3446 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3447 // Clicked outside of the window: drop
3448 if (button == 1) // right
3450 else if (button == 2) // middle
3451 drop_amount = MYMIN(m_selected_amount, 10);
3453 drop_amount = m_selected_amount;
3457 else if (updown == 1) {
3458 // Some mouse button has been released
3460 //infostream<<"Mouse button "<<button<<" released at p=("
3461 // <<p.X<<","<<p.Y<<")"<<std::endl;
3463 if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3465 // Dragged to different slot: move all selected
3466 move_amount = m_selected_amount;
3468 } else if (m_selected_item != NULL && m_selected_dragging &&
3469 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3470 // Dragged outside of window: drop all selected
3471 drop_amount = m_selected_amount;
3474 m_selected_dragging = false;
3475 // Keep count of how many times right mouse button has been
3476 // clicked. One click is drag without dropping. Click + release
3477 // + click changes to drop one item when moved mode
3478 if (button == 1 && m_selected_item != NULL)
3479 m_rmouse_auto_place = !m_rmouse_auto_place;
3480 } else if (updown == -1) {
3481 // Mouse has been moved and rmb is down and mouse pointer just
3482 // entered a new inventory field (checked in the entry-if, this
3483 // is the only action here that is generated by mouse movement)
3484 if (m_selected_item != NULL && s.isValid()) {
3486 // TODO: middle mouse to move 10 items might be handy
3487 if (m_rmouse_auto_place) {
3488 // Only move an item if the destination slot is empty
3489 // or contains the same item type as what is going to be
3491 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3492 InventoryList *list_to = list_s;
3493 assert(list_from && list_to);
3494 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3495 ItemStack stack_to = list_to->getItem(s.i);
3496 if (stack_to.empty() || stack_to.name == stack_from.name)
3502 // Possibly send inventory action to server
3503 if (move_amount > 0) {
3504 // Send IAction::Move
3506 assert(m_selected_item && m_selected_item->isValid());
3507 assert(s.isValid());
3509 assert(inv_selected && inv_s);
3510 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3511 InventoryList *list_to = list_s;
3512 assert(list_from && list_to);
3513 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3514 ItemStack stack_to = list_to->getItem(s.i);
3516 // Check how many items can be moved
3517 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3518 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
3519 // If source stack cannot be added to destination stack at all,
3521 if ((leftover.count == stack_from.count) &&
3522 (leftover.name == stack_from.name)) {
3523 m_selected_amount = stack_to.count;
3524 // In case the server doesn't directly swap them but instead
3525 // moves stack_to somewhere else, set this
3526 m_selected_content_guess = stack_to;
3527 m_selected_content_guess_inventory = s.inventoryloc;
3529 // Source stack goes fully into destination stack
3530 else if (leftover.empty()) {
3531 m_selected_amount -= move_amount;
3532 m_selected_content_guess = ItemStack(); // Clear
3534 // Source stack goes partly into destination stack
3536 move_amount -= leftover.count;
3537 m_selected_amount -= move_amount;
3538 m_selected_content_guess = ItemStack(); // Clear
3541 infostream << "Handing IAction::Move to manager" << std::endl;
3542 IMoveAction *a = new IMoveAction();
3543 a->count = move_amount;
3544 a->from_inv = m_selected_item->inventoryloc;
3545 a->from_list = m_selected_item->listname;
3546 a->from_i = m_selected_item->i;
3547 a->to_inv = s.inventoryloc;
3548 a->to_list = s.listname;
3550 m_invmgr->inventoryAction(a);
3551 } else if (shift_move_amount > 0) {
3552 u32 mis = m_inventory_rings.size();
3554 for (; i < mis; i++) {
3555 const ListRingSpec &sp = m_inventory_rings[i];
3556 if (sp.inventoryloc == s.inventoryloc
3557 && sp.listname == s.listname)
3561 if (i >= mis) // if not found
3563 u32 to_inv_ind = (i + 1) % mis;
3564 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3565 InventoryList *list_from = list_s;
3568 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3571 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3574 ItemStack stack_from = list_from->getItem(s.i);
3575 assert(shift_move_amount <= stack_from.count);
3576 if (m_client->getProtoVersion() >= 25) {
3577 infostream << "Handing IAction::Move to manager" << std::endl;
3578 IMoveAction *a = new IMoveAction();
3579 a->count = shift_move_amount;
3580 a->from_inv = s.inventoryloc;
3581 a->from_list = s.listname;
3583 a->to_inv = to_inv_sp.inventoryloc;
3584 a->to_list = to_inv_sp.listname;
3585 a->move_somewhere = true;
3586 m_invmgr->inventoryAction(a);
3588 // find a place (or more than one) to add the new item
3589 u32 ilt_size = list_to->getSize();
3591 for (u32 slot_to = 0; slot_to < ilt_size
3592 && shift_move_amount > 0; slot_to++) {
3593 list_to->itemFits(slot_to, stack_from, &leftover);
3594 if (leftover.count < stack_from.count) {
3595 infostream << "Handing IAction::Move to manager" << std::endl;
3596 IMoveAction *a = new IMoveAction();
3597 a->count = MYMIN(shift_move_amount,
3598 (u32) (stack_from.count - leftover.count));
3599 shift_move_amount -= a->count;
3600 a->from_inv = s.inventoryloc;
3601 a->from_list = s.listname;
3603 a->to_inv = to_inv_sp.inventoryloc;
3604 a->to_list = to_inv_sp.listname;
3606 m_invmgr->inventoryAction(a);
3607 stack_from = leftover;
3612 } else if (drop_amount > 0) {
3613 m_selected_content_guess = ItemStack(); // Clear
3615 // Send IAction::Drop
3617 assert(m_selected_item && m_selected_item->isValid());
3618 assert(inv_selected);
3619 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3621 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3623 // Check how many items can be dropped
3624 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3625 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3626 m_selected_amount -= drop_amount;
3628 infostream << "Handing IAction::Drop to manager" << std::endl;
3629 IDropAction *a = new IDropAction();
3630 a->count = drop_amount;
3631 a->from_inv = m_selected_item->inventoryloc;
3632 a->from_list = m_selected_item->listname;
3633 a->from_i = m_selected_item->i;
3634 m_invmgr->inventoryAction(a);
3635 } else if (craft_amount > 0) {
3636 m_selected_content_guess = ItemStack(); // Clear
3638 // Send IAction::Craft
3640 assert(s.isValid());
3643 infostream << "Handing IAction::Craft to manager" << std::endl;
3644 ICraftAction *a = new ICraftAction();
3645 a->count = craft_amount;
3646 a->craft_inv = s.inventoryloc;
3647 m_invmgr->inventoryAction(a);
3650 // If m_selected_amount has been decreased to zero, deselect
3651 if (m_selected_amount == 0) {
3652 delete m_selected_item;
3653 m_selected_item = NULL;
3654 m_selected_amount = 0;
3655 m_selected_dragging = false;
3656 m_selected_content_guess = ItemStack();
3658 m_old_pointer = m_pointer;
3660 if (event.EventType == EET_GUI_EVENT) {
3662 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3664 // find the element that was clicked
3665 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3666 if ((s.ftype == f_TabHeader) &&
3667 (s.fid == event.GUIEvent.Caller->getID())) {
3675 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3677 if (!canTakeFocus(event.GUIEvent.Element)) {
3678 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3680 // Returning true disables focus change
3684 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3685 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3686 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3687 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3688 unsigned int btn_id = event.GUIEvent.Caller->getID();
3690 if (btn_id == 257) {
3692 acceptInput(quit_mode_accept);
3696 m_text_dst->gotText(L"ExitButton");
3698 // quitMenu deallocates menu
3702 // find the element that was clicked
3703 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3704 // if its a button, set the send field so
3705 // lua knows which button was pressed
3706 if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
3707 s.fid == event.GUIEvent.Caller->getID()) {
3711 acceptInput(quit_mode_accept);
3714 m_text_dst->gotText(L"ExitButton");
3719 acceptInput(quit_mode_no);
3723 } else if ((s.ftype == f_DropDown) &&
3724 (s.fid == event.GUIEvent.Caller->getID())) {
3725 // only send the changed dropdown
3726 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
3727 if (s2.ftype == f_DropDown) {
3732 acceptInput(quit_mode_no);
3734 // revert configuration to make sure dropdowns are sent on
3735 // regular button click
3736 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
3737 if (s2.ftype == f_DropDown) {
3742 } else if ((s.ftype == f_ScrollBar) &&
3743 (s.fid == event.GUIEvent.Caller->getID())) {
3744 s.fdefault = L"Changed";
3745 acceptInput(quit_mode_no);
3751 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3752 if (event.GUIEvent.Caller->getID() > 257) {
3753 bool close_on_enter = true;
3754 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3755 if (s.ftype == f_Unknown &&
3756 s.fid == event.GUIEvent.Caller->getID()) {
3757 current_field_enter_pending = s.fname;
3758 std::unordered_map<std::string, bool>::const_iterator it =
3759 field_close_on_enter.find(s.fname);
3760 if (it != field_close_on_enter.end())
3761 close_on_enter = (*it).second;
3767 if (m_allowclose && close_on_enter) {
3768 current_keys_pending.key_enter = true;
3769 acceptInput(quit_mode_accept);
3772 current_keys_pending.key_enter = true;
3775 // quitMenu deallocates menu
3780 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3781 int current_id = event.GUIEvent.Caller->getID();
3782 if (current_id > 257) {
3783 // find the element that was clicked
3784 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3785 // if it's a table, set the send field
3786 // so lua knows which table was changed
3787 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3798 return Parent ? Parent->OnEvent(event) : false;
3802 * get name of element by element id
3803 * @param id of element
3804 * @return name string or empty string
3806 std::string GUIFormSpecMenu::getNameByID(s32 id)
3808 for (FieldSpec &spec : m_fields) {
3809 if (spec.fid == id) {
3817 * get label of element by id
3818 * @param id of element
3819 * @return label string or empty string
3821 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3823 for (FieldSpec &spec : m_fields) {
3824 if (spec.fid == id) {