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 = translate_string(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 = translate_string(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 = wide_to_utf8(unescape_translate(utf8_to_wide(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 = wide_to_utf8(unescape_translate(utf8_to_wide(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_translate(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 = translate_string(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 = translate_string(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 = translate_string(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_translate(
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_translate(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(utf8_to_wide(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(utf8_to_wide(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(utf8_to_wide(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 const std::wstring ntext = translate_string(text);
2636 m_tooltip_element->setOverrideColor(color);
2637 m_tooltip_element->setBackgroundColor(bgcolor);
2638 setStaticText(m_tooltip_element, ntext.c_str());
2640 // Tooltip size and offset
2641 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2642 #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
2643 std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
2644 s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
2646 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2648 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
2649 int tooltip_offset_x = m_btn_height;
2650 int tooltip_offset_y = m_btn_height;
2652 tooltip_offset_x *= 3;
2653 tooltip_offset_y = 0;
2654 if (m_pointer.X > (s32)screenSize.X / 2)
2655 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
2658 // Calculate and set the tooltip position
2659 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2660 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2661 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2662 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2663 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2664 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2666 m_tooltip_element->setRelativePosition(
2668 core::position2d<s32>(tooltip_x, tooltip_y),
2669 core::dimension2d<s32>(tooltip_width, tooltip_height)
2673 // Display the tooltip
2674 m_tooltip_element->setVisible(true);
2675 bringToFront(m_tooltip_element);
2678 void GUIFormSpecMenu::updateSelectedItem()
2680 // If the selected stack has become empty for some reason, deselect it.
2681 // If the selected stack has become inaccessible, deselect it.
2682 // If the selected stack has become smaller, adjust m_selected_amount.
2683 ItemStack selected = verifySelectedItem();
2685 // WARNING: BLACK MAGIC
2686 // See if there is a stack suited for our current guess.
2687 // If such stack does not exist, clear the guess.
2688 if (!m_selected_content_guess.name.empty() &&
2689 selected.name == m_selected_content_guess.name &&
2690 selected.count == m_selected_content_guess.count){
2691 // Selected item fits the guess. Skip the black magic.
2692 } else if (!m_selected_content_guess.name.empty()) {
2694 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2695 const ListDrawSpec &s = m_inventorylists[i];
2696 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2699 InventoryList *list = inv->getList(s.listname);
2702 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2703 u32 item_i = i + s.start_item_i;
2704 if(item_i >= list->getSize())
2706 ItemStack stack = list->getItem(item_i);
2707 if(stack.name == m_selected_content_guess.name &&
2708 stack.count == m_selected_content_guess.count){
2710 infostream<<"Client: Changing selected content guess to "
2711 <<s.inventoryloc.dump()<<" "<<s.listname
2712 <<" "<<item_i<<std::endl;
2713 delete m_selected_item;
2714 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2715 m_selected_amount = stack.count;
2720 infostream<<"Client: Discarding selected content guess: "
2721 <<m_selected_content_guess.getItemString()<<std::endl;
2722 m_selected_content_guess.name = "";
2726 // If craftresult is nonempty and nothing else is selected, select it now.
2727 if(!m_selected_item)
2729 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
2730 if(s.listname == "craftpreview")
2732 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2733 InventoryList *list = inv->getList("craftresult");
2734 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2736 m_selected_item = new ItemSpec;
2737 m_selected_item->inventoryloc = s.inventoryloc;
2738 m_selected_item->listname = "craftresult";
2739 m_selected_item->i = 0;
2740 m_selected_amount = 0;
2741 m_selected_dragging = false;
2748 // If craftresult is selected, keep the whole stack selected
2749 if(m_selected_item && m_selected_item->listname == "craftresult")
2751 m_selected_amount = verifySelectedItem().count;
2755 ItemStack GUIFormSpecMenu::verifySelectedItem()
2757 // If the selected stack has become empty for some reason, deselect it.
2758 // If the selected stack has become inaccessible, deselect it.
2759 // If the selected stack has become smaller, adjust m_selected_amount.
2760 // Return the selected stack.
2764 if(m_selected_item->isValid())
2766 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2769 InventoryList *list = inv->getList(m_selected_item->listname);
2770 if(list && (u32) m_selected_item->i < list->getSize())
2772 ItemStack stack = list->getItem(m_selected_item->i);
2773 if(m_selected_amount > stack.count)
2774 m_selected_amount = stack.count;
2781 // selection was not valid
2782 delete m_selected_item;
2783 m_selected_item = NULL;
2784 m_selected_amount = 0;
2785 m_selected_dragging = false;
2790 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2796 if (quitmode == quit_mode_accept) {
2797 fields["quit"] = "true";
2800 if (quitmode == quit_mode_cancel) {
2801 fields["quit"] = "true";
2802 m_text_dst->gotText(fields);
2806 if (current_keys_pending.key_down) {
2807 fields["key_down"] = "true";
2808 current_keys_pending.key_down = false;
2811 if (current_keys_pending.key_up) {
2812 fields["key_up"] = "true";
2813 current_keys_pending.key_up = false;
2816 if (current_keys_pending.key_enter) {
2817 fields["key_enter"] = "true";
2818 current_keys_pending.key_enter = false;
2821 if (!current_field_enter_pending.empty()) {
2822 fields["key_enter_field"] = current_field_enter_pending;
2823 current_field_enter_pending = "";
2826 if (current_keys_pending.key_escape) {
2827 fields["key_escape"] = "true";
2828 current_keys_pending.key_escape = false;
2831 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
2833 std::string name = s.fname;
2834 if (s.ftype == f_Button) {
2835 fields[name] = wide_to_utf8(s.flabel);
2836 } else if (s.ftype == f_Table) {
2837 GUITable *table = getTable(s.fname);
2839 fields[name] = table->checkEvent();
2842 else if(s.ftype == f_DropDown) {
2843 // no dynamic cast possible due to some distributions shipped
2844 // without rtti support in irrlicht
2845 IGUIElement * element = getElementFromId(s.fid);
2846 gui::IGUIComboBox *e = NULL;
2847 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2848 e = static_cast<gui::IGUIComboBox*>(element);
2850 s32 selected = e->getSelected();
2851 if (selected >= 0) {
2852 std::vector<std::string> *dropdown_values =
2853 getDropDownValues(s.fname);
2854 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2855 fields[name] = (*dropdown_values)[selected];
2859 else if (s.ftype == f_TabHeader) {
2860 // no dynamic cast possible due to some distributions shipped
2861 // without rttzi support in irrlicht
2862 IGUIElement * element = getElementFromId(s.fid);
2863 gui::IGUITabControl *e = NULL;
2864 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2865 e = static_cast<gui::IGUITabControl *>(element);
2869 std::stringstream ss;
2870 ss << (e->getActiveTab() +1);
2871 fields[name] = ss.str();
2874 else if (s.ftype == f_CheckBox) {
2875 // no dynamic cast possible due to some distributions shipped
2876 // without rtti support in irrlicht
2877 IGUIElement * element = getElementFromId(s.fid);
2878 gui::IGUICheckBox *e = NULL;
2879 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2880 e = static_cast<gui::IGUICheckBox*>(element);
2885 fields[name] = "true";
2887 fields[name] = "false";
2890 else if (s.ftype == f_ScrollBar) {
2891 // no dynamic cast possible due to some distributions shipped
2892 // without rtti support in irrlicht
2893 IGUIElement * element = getElementFromId(s.fid);
2894 gui::IGUIScrollBar *e = NULL;
2895 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2896 e = static_cast<gui::IGUIScrollBar*>(element);
2900 std::stringstream os;
2902 if (s.fdefault == L"Changed")
2903 fields[name] = "CHG:" + os.str();
2905 fields[name] = "VAL:" + os.str();
2910 IGUIElement* e = getElementFromId(s.fid);
2912 fields[name] = wide_to_utf8(e->getText());
2918 m_text_dst->gotText(fields);
2922 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2924 while(tocheck != NULL) {
2925 if (tocheck == parent) {
2928 tocheck = tocheck->getParent();
2933 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2935 // The IGUITabControl renders visually using the skin's selected
2936 // font, which we override for the duration of form drawing,
2937 // but computes tab hotspots based on how it would have rendered
2938 // using the font that is selected at the time of button release.
2939 // To make these two consistent, temporarily override the skin's
2940 // font while the IGUITabControl is processing the event.
2941 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2942 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2943 s32 x = event.MouseInput.X;
2944 s32 y = event.MouseInput.Y;
2945 gui::IGUIElement *hovered =
2946 Environment->getRootGUIElement()->getElementFromPoint(
2947 core::position2d<s32>(x, y));
2948 if (hovered && isMyChild(hovered) &&
2949 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2950 gui::IGUISkin* skin = Environment->getSkin();
2951 sanity_check(skin != NULL);
2952 gui::IGUIFont *old_font = skin->getFont();
2953 skin->setFont(m_font);
2954 bool retval = hovered->OnEvent(event);
2955 skin->setFont(old_font);
2960 // Fix Esc/Return key being eaten by checkboxen and tables
2961 if(event.EventType==EET_KEY_INPUT_EVENT) {
2962 KeyPress kp(event.KeyInput);
2963 if (kp == EscapeKey || kp == CancelKey
2964 || kp == getKeySetting("keymap_inventory")
2965 || event.KeyInput.Key==KEY_RETURN) {
2966 gui::IGUIElement *focused = Environment->getFocus();
2967 if (focused && isMyChild(focused) &&
2968 (focused->getType() == gui::EGUIET_LIST_BOX ||
2969 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2975 // Mouse wheel events: send to hovered element instead of focused
2976 if(event.EventType==EET_MOUSE_INPUT_EVENT
2977 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2978 s32 x = event.MouseInput.X;
2979 s32 y = event.MouseInput.Y;
2980 gui::IGUIElement *hovered =
2981 Environment->getRootGUIElement()->getElementFromPoint(
2982 core::position2d<s32>(x, y));
2983 if (hovered && isMyChild(hovered)) {
2984 hovered->OnEvent(event);
2989 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2990 s32 x = event.MouseInput.X;
2991 s32 y = event.MouseInput.Y;
2992 gui::IGUIElement *hovered =
2993 Environment->getRootGUIElement()->getElementFromPoint(
2994 core::position2d<s32>(x, y));
2995 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2996 m_old_tooltip_id = -1;
2998 if (!isChild(hovered,this)) {
2999 if (DoubleClickDetection(event)) {
3006 // display software keyboard when clicking edit boxes
3007 if (event.EventType == EET_MOUSE_INPUT_EVENT
3008 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3009 gui::IGUIElement *hovered =
3010 Environment->getRootGUIElement()->getElementFromPoint(
3011 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
3012 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
3013 bool retval = hovered->OnEvent(event);
3015 Environment->setFocus(hovered);
3017 m_JavaDialogFieldName = getNameByID(hovered->getID());
3018 std::string message = gettext("Enter ");
3019 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
3023 message += gettext(label) + ":";
3025 /* single line text input */
3028 /* multi line text input */
3029 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
3033 /* passwords are always single line */
3034 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
3038 porting::showInputDialog(gettext("ok"), "",
3039 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
3045 if (event.EventType == EET_TOUCH_INPUT_EVENT)
3048 memset(&translated, 0, sizeof(SEvent));
3049 translated.EventType = EET_MOUSE_INPUT_EVENT;
3050 gui::IGUIElement* root = Environment->getRootGUIElement();
3054 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
3058 gui::IGUIElement* hovered = root->getElementFromPoint(
3059 core::position2d<s32>(
3061 event.TouchInput.Y));
3063 translated.MouseInput.X = event.TouchInput.X;
3064 translated.MouseInput.Y = event.TouchInput.Y;
3065 translated.MouseInput.Control = false;
3067 bool dont_send_event = false;
3069 if (event.TouchInput.touchedCount == 1) {
3070 switch (event.TouchInput.Event) {
3071 case ETIE_PRESSED_DOWN:
3072 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
3073 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
3074 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3075 m_down_pos = m_pointer;
3078 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
3079 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
3080 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3083 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
3084 translated.MouseInput.ButtonStates = 0;
3085 hovered = root->getElementFromPoint(m_down_pos);
3086 /* we don't have a valid pointer element use last
3087 * known pointer pos */
3088 translated.MouseInput.X = m_pointer.X;
3089 translated.MouseInput.Y = m_pointer.Y;
3091 /* reset down pos */
3092 m_down_pos = v2s32(0,0);
3095 dont_send_event = true;
3096 //this is not supposed to happen
3098 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
3099 << event.TouchInput.Event << std::endl;
3101 } else if ( (event.TouchInput.touchedCount == 2) &&
3102 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
3103 hovered = root->getElementFromPoint(m_down_pos);
3105 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
3106 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
3107 translated.MouseInput.X = m_pointer.X;
3108 translated.MouseInput.Y = m_pointer.Y;
3111 hovered->OnEvent(translated);
3114 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
3115 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3119 hovered->OnEvent(translated);
3121 dont_send_event = true;
3123 /* ignore unhandled 2 touch events ... accidental moving for example */
3124 else if (event.TouchInput.touchedCount == 2) {
3125 dont_send_event = true;
3127 else if (event.TouchInput.touchedCount > 2) {
3129 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
3130 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3133 if (dont_send_event) {
3137 /* check if translated event needs to be preprocessed again */
3138 if (preprocessEvent(translated)) {
3143 bool retval = hovered->OnEvent(translated);
3145 if (event.TouchInput.Event == ETIE_LEFT_UP) {
3147 m_pointer = v2s32(0,0);
3155 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3156 /* TODO add a check like:
3157 if (event.JoystickEvent != joystick_we_listen_for)
3160 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3162 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3164 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3166 acceptInput(quit_mode_accept);
3177 /******************************************************************************/
3178 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3180 /* The following code is for capturing double-clicks of the mouse button
3181 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3182 * -- which closes the form -- under some circumstances.
3184 * There have been many github issues reporting this as a bug even though it
3185 * was an intended feature. For this reason, remapping the double-click as
3186 * an ESC must be explicitly set when creating this class via the
3187 * /p remap_dbl_click parameter of the constructor.
3190 if (!m_remap_dbl_click)
3193 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3194 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3195 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3197 m_doubleclickdetect[1].pos = m_pointer;
3198 m_doubleclickdetect[1].time = porting::getTimeMs();
3200 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3201 u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
3206 double squaredistance =
3207 m_doubleclickdetect[0].pos
3208 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3210 if (squaredistance > (30*30)) {
3214 SEvent* translated = new SEvent();
3215 assert(translated != 0);
3216 //translate doubleclick to escape
3217 memset(translated, 0, sizeof(SEvent));
3218 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3219 translated->KeyInput.Key = KEY_ESCAPE;
3220 translated->KeyInput.Control = false;
3221 translated->KeyInput.Shift = false;
3222 translated->KeyInput.PressedDown = true;
3223 translated->KeyInput.Char = 0;
3224 OnEvent(*translated);
3226 // no need to send the key up event as we're already deleted
3227 // and no one else did notice this event
3235 void GUIFormSpecMenu::tryClose()
3239 acceptInput(quit_mode_cancel);
3242 m_text_dst->gotText(L"MenuQuit");
3246 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3248 if (event.EventType==EET_KEY_INPUT_EVENT) {
3249 KeyPress kp(event.KeyInput);
3250 if (event.KeyInput.PressedDown && (
3251 (kp == EscapeKey) || (kp == CancelKey) ||
3252 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
3257 if (m_client != NULL && event.KeyInput.PressedDown &&
3258 (kp == getKeySetting("keymap_screenshot"))) {
3259 m_client->makeScreenshot();
3261 if (event.KeyInput.PressedDown &&
3262 (event.KeyInput.Key==KEY_RETURN ||
3263 event.KeyInput.Key==KEY_UP ||
3264 event.KeyInput.Key==KEY_DOWN)
3266 switch (event.KeyInput.Key) {
3268 current_keys_pending.key_enter = true;
3271 current_keys_pending.key_up = true;
3274 current_keys_pending.key_down = true;
3278 //can't happen at all!
3279 FATAL_ERROR("Reached a source line that can't ever been reached");
3282 if (current_keys_pending.key_enter && m_allowclose) {
3283 acceptInput(quit_mode_accept);
3293 /* Mouse event other than movement, or crossing the border of inventory
3294 field while holding right mouse button
3296 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3297 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3298 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3299 event.MouseInput.isRightPressed() &&
3300 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3302 // Get selected item and hovered/clicked item (s)
3304 m_old_tooltip_id = -1;
3305 updateSelectedItem();
3306 ItemSpec s = getItemAtPos(m_pointer);
3308 Inventory *inv_selected = NULL;
3309 Inventory *inv_s = NULL;
3310 InventoryList *list_s = NULL;
3312 if (m_selected_item) {
3313 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3314 sanity_check(inv_selected);
3315 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3322 inv_s = m_invmgr->getInventory(s.inventoryloc);
3325 errorstream << "InventoryMenu: The selected inventory location "
3326 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3328 s.i = -1; // make it invalid again
3332 list_s = inv_s->getList(s.listname);
3333 if (list_s == NULL) {
3334 verbosestream << "InventoryMenu: The selected inventory list \""
3335 << s.listname << "\" does not exist" << std::endl;
3336 s.i = -1; // make it invalid again
3340 if ((u32)s.i >= list_s->getSize()) {
3341 infostream << "InventoryMenu: The selected inventory list \""
3342 << s.listname << "\" is too small (i=" << s.i << ", size="
3343 << list_s->getSize() << ")" << std::endl;
3344 s.i = -1; // make it invalid again
3348 s_count = list_s->getItem(s.i).count;
3351 bool identical = (m_selected_item != NULL) && s.isValid() &&
3352 (inv_selected == inv_s) &&
3353 (m_selected_item->listname == s.listname) &&
3354 (m_selected_item->i == s.i);
3356 // buttons: 0 = left, 1 = right, 2 = middle
3357 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3360 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3361 { button = 0; updown = 0; }
3362 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3363 { button = 1; updown = 0; }
3364 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3365 { button = 2; updown = 0; }
3366 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3367 { button = 0; updown = 1; }
3368 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3369 { button = 1; updown = 1; }
3370 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3371 { button = 2; updown = 1; }
3372 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3375 // Set this number to a positive value to generate a move action
3376 // from m_selected_item to s.
3377 u32 move_amount = 0;
3379 // Set this number to a positive value to generate a move action
3380 // from s to the next inventory ring.
3381 u32 shift_move_amount = 0;
3383 // Set this number to a positive value to generate a drop action
3384 // from m_selected_item.
3385 u32 drop_amount = 0;
3387 // Set this number to a positive value to generate a craft action at s.
3388 u32 craft_amount = 0;
3391 // Some mouse button has been pressed
3393 //infostream<<"Mouse button "<<button<<" pressed at p=("
3394 // <<p.X<<","<<p.Y<<")"<<std::endl;
3396 m_selected_dragging = false;
3398 if (s.isValid() && s.listname == "craftpreview") {
3399 // Craft preview has been clicked: craft
3400 craft_amount = (button == 2 ? 10 : 1);
3401 } else if (m_selected_item == NULL) {
3403 // Non-empty stack has been clicked: select or shift-move it
3404 m_selected_item = new ItemSpec(s);
3407 if (button == 1) // right
3408 count = (s_count + 1) / 2;
3409 else if (button == 2) // middle
3410 count = MYMIN(s_count, 10);
3414 if (!event.MouseInput.Shift) {
3415 // no shift: select item
3416 m_selected_amount = count;
3417 m_selected_dragging = true;
3418 m_rmouse_auto_place = false;
3420 // shift pressed: move item
3422 shift_move_amount = count;
3423 else // count of 1 at left click like after drag & drop
3424 shift_move_amount = 1;
3427 } else { // m_selected_item != NULL
3428 assert(m_selected_amount >= 1);
3431 // Clicked a slot: move
3432 if (button == 1) // right
3434 else if (button == 2) // middle
3435 move_amount = MYMIN(m_selected_amount, 10);
3437 move_amount = m_selected_amount;
3440 if (move_amount >= m_selected_amount)
3441 m_selected_amount = 0;
3443 m_selected_amount -= move_amount;
3447 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3448 // Clicked outside of the window: drop
3449 if (button == 1) // right
3451 else if (button == 2) // middle
3452 drop_amount = MYMIN(m_selected_amount, 10);
3454 drop_amount = m_selected_amount;
3458 else if (updown == 1) {
3459 // Some mouse button has been released
3461 //infostream<<"Mouse button "<<button<<" released at p=("
3462 // <<p.X<<","<<p.Y<<")"<<std::endl;
3464 if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3466 // Dragged to different slot: move all selected
3467 move_amount = m_selected_amount;
3469 } else if (m_selected_item != NULL && m_selected_dragging &&
3470 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3471 // Dragged outside of window: drop all selected
3472 drop_amount = m_selected_amount;
3475 m_selected_dragging = false;
3476 // Keep count of how many times right mouse button has been
3477 // clicked. One click is drag without dropping. Click + release
3478 // + click changes to drop one item when moved mode
3479 if (button == 1 && m_selected_item != NULL)
3480 m_rmouse_auto_place = !m_rmouse_auto_place;
3481 } else if (updown == -1) {
3482 // Mouse has been moved and rmb is down and mouse pointer just
3483 // entered a new inventory field (checked in the entry-if, this
3484 // is the only action here that is generated by mouse movement)
3485 if (m_selected_item != NULL && s.isValid()) {
3487 // TODO: middle mouse to move 10 items might be handy
3488 if (m_rmouse_auto_place) {
3489 // Only move an item if the destination slot is empty
3490 // or contains the same item type as what is going to be
3492 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3493 InventoryList *list_to = list_s;
3494 assert(list_from && list_to);
3495 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3496 ItemStack stack_to = list_to->getItem(s.i);
3497 if (stack_to.empty() || stack_to.name == stack_from.name)
3503 // Possibly send inventory action to server
3504 if (move_amount > 0) {
3505 // Send IAction::Move
3507 assert(m_selected_item && m_selected_item->isValid());
3508 assert(s.isValid());
3510 assert(inv_selected && inv_s);
3511 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3512 InventoryList *list_to = list_s;
3513 assert(list_from && list_to);
3514 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3515 ItemStack stack_to = list_to->getItem(s.i);
3517 // Check how many items can be moved
3518 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3519 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
3520 // If source stack cannot be added to destination stack at all,
3522 if ((leftover.count == stack_from.count) &&
3523 (leftover.name == stack_from.name)) {
3524 m_selected_amount = stack_to.count;
3525 // In case the server doesn't directly swap them but instead
3526 // moves stack_to somewhere else, set this
3527 m_selected_content_guess = stack_to;
3528 m_selected_content_guess_inventory = s.inventoryloc;
3530 // Source stack goes fully into destination stack
3531 else if (leftover.empty()) {
3532 m_selected_amount -= move_amount;
3533 m_selected_content_guess = ItemStack(); // Clear
3535 // Source stack goes partly into destination stack
3537 move_amount -= leftover.count;
3538 m_selected_amount -= move_amount;
3539 m_selected_content_guess = ItemStack(); // Clear
3542 infostream << "Handing IAction::Move to manager" << std::endl;
3543 IMoveAction *a = new IMoveAction();
3544 a->count = move_amount;
3545 a->from_inv = m_selected_item->inventoryloc;
3546 a->from_list = m_selected_item->listname;
3547 a->from_i = m_selected_item->i;
3548 a->to_inv = s.inventoryloc;
3549 a->to_list = s.listname;
3551 m_invmgr->inventoryAction(a);
3552 } else if (shift_move_amount > 0) {
3553 u32 mis = m_inventory_rings.size();
3555 for (; i < mis; i++) {
3556 const ListRingSpec &sp = m_inventory_rings[i];
3557 if (sp.inventoryloc == s.inventoryloc
3558 && sp.listname == s.listname)
3562 if (i >= mis) // if not found
3564 u32 to_inv_ind = (i + 1) % mis;
3565 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3566 InventoryList *list_from = list_s;
3569 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3572 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3575 ItemStack stack_from = list_from->getItem(s.i);
3576 assert(shift_move_amount <= stack_from.count);
3577 if (m_client->getProtoVersion() >= 25) {
3578 infostream << "Handing IAction::Move to manager" << std::endl;
3579 IMoveAction *a = new IMoveAction();
3580 a->count = shift_move_amount;
3581 a->from_inv = s.inventoryloc;
3582 a->from_list = s.listname;
3584 a->to_inv = to_inv_sp.inventoryloc;
3585 a->to_list = to_inv_sp.listname;
3586 a->move_somewhere = true;
3587 m_invmgr->inventoryAction(a);
3589 // find a place (or more than one) to add the new item
3590 u32 ilt_size = list_to->getSize();
3592 for (u32 slot_to = 0; slot_to < ilt_size
3593 && shift_move_amount > 0; slot_to++) {
3594 list_to->itemFits(slot_to, stack_from, &leftover);
3595 if (leftover.count < stack_from.count) {
3596 infostream << "Handing IAction::Move to manager" << std::endl;
3597 IMoveAction *a = new IMoveAction();
3598 a->count = MYMIN(shift_move_amount,
3599 (u32) (stack_from.count - leftover.count));
3600 shift_move_amount -= a->count;
3601 a->from_inv = s.inventoryloc;
3602 a->from_list = s.listname;
3604 a->to_inv = to_inv_sp.inventoryloc;
3605 a->to_list = to_inv_sp.listname;
3607 m_invmgr->inventoryAction(a);
3608 stack_from = leftover;
3613 } else if (drop_amount > 0) {
3614 m_selected_content_guess = ItemStack(); // Clear
3616 // Send IAction::Drop
3618 assert(m_selected_item && m_selected_item->isValid());
3619 assert(inv_selected);
3620 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3622 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3624 // Check how many items can be dropped
3625 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3626 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3627 m_selected_amount -= drop_amount;
3629 infostream << "Handing IAction::Drop to manager" << std::endl;
3630 IDropAction *a = new IDropAction();
3631 a->count = drop_amount;
3632 a->from_inv = m_selected_item->inventoryloc;
3633 a->from_list = m_selected_item->listname;
3634 a->from_i = m_selected_item->i;
3635 m_invmgr->inventoryAction(a);
3636 } else if (craft_amount > 0) {
3637 m_selected_content_guess = ItemStack(); // Clear
3639 // Send IAction::Craft
3641 assert(s.isValid());
3644 infostream << "Handing IAction::Craft to manager" << std::endl;
3645 ICraftAction *a = new ICraftAction();
3646 a->count = craft_amount;
3647 a->craft_inv = s.inventoryloc;
3648 m_invmgr->inventoryAction(a);
3651 // If m_selected_amount has been decreased to zero, deselect
3652 if (m_selected_amount == 0) {
3653 delete m_selected_item;
3654 m_selected_item = NULL;
3655 m_selected_amount = 0;
3656 m_selected_dragging = false;
3657 m_selected_content_guess = ItemStack();
3659 m_old_pointer = m_pointer;
3661 if (event.EventType == EET_GUI_EVENT) {
3663 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3665 // find the element that was clicked
3666 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3667 if ((s.ftype == f_TabHeader) &&
3668 (s.fid == event.GUIEvent.Caller->getID())) {
3676 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3678 if (!canTakeFocus(event.GUIEvent.Element)) {
3679 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3681 // Returning true disables focus change
3685 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3686 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3687 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3688 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3689 unsigned int btn_id = event.GUIEvent.Caller->getID();
3691 if (btn_id == 257) {
3693 acceptInput(quit_mode_accept);
3697 m_text_dst->gotText(L"ExitButton");
3699 // quitMenu deallocates menu
3703 // find the element that was clicked
3704 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3705 // if its a button, set the send field so
3706 // lua knows which button was pressed
3707 if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
3708 s.fid == event.GUIEvent.Caller->getID()) {
3712 acceptInput(quit_mode_accept);
3715 m_text_dst->gotText(L"ExitButton");
3720 acceptInput(quit_mode_no);
3724 } else if ((s.ftype == f_DropDown) &&
3725 (s.fid == event.GUIEvent.Caller->getID())) {
3726 // only send the changed dropdown
3727 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
3728 if (s2.ftype == f_DropDown) {
3733 acceptInput(quit_mode_no);
3735 // revert configuration to make sure dropdowns are sent on
3736 // regular button click
3737 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
3738 if (s2.ftype == f_DropDown) {
3743 } else if ((s.ftype == f_ScrollBar) &&
3744 (s.fid == event.GUIEvent.Caller->getID())) {
3745 s.fdefault = L"Changed";
3746 acceptInput(quit_mode_no);
3752 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3753 if (event.GUIEvent.Caller->getID() > 257) {
3754 bool close_on_enter = true;
3755 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3756 if (s.ftype == f_Unknown &&
3757 s.fid == event.GUIEvent.Caller->getID()) {
3758 current_field_enter_pending = s.fname;
3759 std::unordered_map<std::string, bool>::const_iterator it =
3760 field_close_on_enter.find(s.fname);
3761 if (it != field_close_on_enter.end())
3762 close_on_enter = (*it).second;
3768 if (m_allowclose && close_on_enter) {
3769 current_keys_pending.key_enter = true;
3770 acceptInput(quit_mode_accept);
3773 current_keys_pending.key_enter = true;
3776 // quitMenu deallocates menu
3781 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3782 int current_id = event.GUIEvent.Caller->getID();
3783 if (current_id > 257) {
3784 // find the element that was clicked
3785 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3786 // if it's a table, set the send field
3787 // so lua knows which table was changed
3788 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3799 return Parent ? Parent->OnEvent(event) : false;
3803 * get name of element by element id
3804 * @param id of element
3805 * @return name string or empty string
3807 std::string GUIFormSpecMenu::getNameByID(s32 id)
3809 for (FieldSpec &spec : m_fields) {
3810 if (spec.fid == id) {
3818 * get label of element by id
3819 * @param id of element
3820 * @return label string or empty string
3822 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3824 for (FieldSpec &spec : m_fields) {
3825 if (spec.fid == id) {