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"
30 #include "client/keycode.h"
31 #include "util/strfnd.h"
32 #include <IGUICheckBox.h>
33 #include <IGUIEditBox.h>
34 #include <IGUIButton.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include <IGUIComboBox.h>
39 #include "client/renderingengine.h"
41 #include "client/tile.h" // ITextureSource
42 #include "client/hud.h" // drawItemStack
46 #include "scripting_server.h"
47 #include "mainmenumanager.h"
50 #include "client/client.h"
51 #include "client/fontengine.h"
53 #include "util/numeric.h"
54 #include "util/string.h" // for parseColorString()
55 #include "irrlicht_changes/static_text.h"
56 #include "client/guiscalingfilter.h"
57 #include "guiEditBoxWithScrollbar.h"
58 #include "intlGUIEditBox.h"
60 #define MY_CHECKPOS(a,b) \
61 if (v_pos.size() != 2) { \
62 errorstream<< "Invalid pos for element " << a << "specified: \"" \
63 << parts[b] << "\"" << std::endl; \
67 #define MY_CHECKGEOM(a,b) \
68 if (v_geom.size() != 2) { \
69 errorstream<< "Invalid pos for element " << a << "specified: \"" \
70 << parts[b] << "\"" << std::endl; \
76 static unsigned int font_line_height(gui::IGUIFont *font)
78 return font->getDimension(L"Ay").Height + font->getKerningHeight();
81 inline u32 clamp_u8(s32 value)
83 return (u32) MYMIN(MYMAX(value, 0), 255);
86 GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
87 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
88 Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst,
89 std::string formspecPrepend,
90 bool remap_dbl_click):
91 GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr),
95 m_formspec_prepend(formspecPrepend),
99 m_remap_dbl_click(remap_dbl_click)
101 current_keys_pending.key_down = false;
102 current_keys_pending.key_up = false;
103 current_keys_pending.key_enter = false;
104 current_keys_pending.key_escape = false;
106 m_doubleclickdetect[0].time = 0;
107 m_doubleclickdetect[1].time = 0;
109 m_doubleclickdetect[0].pos = v2s32(0, 0);
110 m_doubleclickdetect[1].pos = v2s32(0, 0);
112 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
113 m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
116 GUIFormSpecMenu::~GUIFormSpecMenu()
120 for (auto &table_it : m_tables) {
121 table_it.second->drop();
124 delete m_selected_item;
129 void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
130 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
131 const std::string &formspecPrepend)
133 if (cur_formspec == nullptr) {
134 cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
135 client, client->getTextureSource(), fs_src, txt_dest, formspecPrepend);
136 cur_formspec->doPause = false;
139 Caution: do not call (*cur_formspec)->drop() here --
140 the reference might outlive the menu, so we will
141 periodically check if *cur_formspec is the only
142 remaining reference (i.e. the menu was removed)
143 and delete it in that case.
147 cur_formspec->setFormspecPrepend(formspecPrepend);
148 cur_formspec->setFormSource(fs_src);
149 cur_formspec->setTextDest(txt_dest);
153 void GUIFormSpecMenu::removeChildren()
155 const core::list<gui::IGUIElement*> &children = getChildren();
157 while(!children.empty()) {
158 (*children.getLast())->remove();
161 if(m_tooltip_element) {
162 m_tooltip_element->remove();
163 m_tooltip_element->drop();
164 m_tooltip_element = NULL;
169 void GUIFormSpecMenu::setInitialFocus()
171 // Set initial focus according to following order of precedence:
172 // 1. first empty editbox
176 // 5. first focusable (not statictext, not tabheader)
177 // 6. first child element
179 core::list<gui::IGUIElement*> children = getChildren();
181 // in case "children" contains any NULL elements, remove them
182 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
183 it != children.end();) {
187 it = children.erase(it);
190 // 1. first empty editbox
191 for (gui::IGUIElement *it : children) {
192 if (it->getType() == gui::EGUIET_EDIT_BOX
193 && it->getText()[0] == 0) {
194 Environment->setFocus(it);
200 for (gui::IGUIElement *it : children) {
201 if (it->getType() == gui::EGUIET_EDIT_BOX) {
202 Environment->setFocus(it);
208 for (gui::IGUIElement *it : children) {
209 if (it->getTypeName() == std::string("GUITable")) {
210 Environment->setFocus(it);
216 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
217 it != children.end(); --it) {
218 if ((*it)->getType() == gui::EGUIET_BUTTON) {
219 Environment->setFocus(*it);
224 // 5. first focusable (not statictext, not tabheader)
225 for (gui::IGUIElement *it : children) {
226 if (it->getType() != gui::EGUIET_STATIC_TEXT &&
227 it->getType() != gui::EGUIET_TAB_CONTROL) {
228 Environment->setFocus(it);
233 // 6. first child element
234 if (children.empty())
235 Environment->setFocus(this);
237 Environment->setFocus(*(children.begin()));
240 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
242 for (auto &table : m_tables) {
243 if (tablename == table.first.fname)
249 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
251 for (auto &dropdown : m_dropdowns) {
252 if (name == dropdown.first.fname)
253 return &dropdown.second;
258 v2s32 GUIFormSpecMenu::getElementBasePos(bool absolute,
259 const std::vector<std::string> *v_pos)
263 pos += AbsoluteRect.UpperLeftCorner;
265 v2f32 pos_f = v2f32(pos.X, pos.Y) + pos_offset * spacing;
267 pos_f.X += stof((*v_pos)[0]) * spacing.X;
268 pos_f.Y += stof((*v_pos)[1]) * spacing.Y;
270 return v2s32(pos_f.X, pos_f.Y);
273 void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
275 std::vector<std::string> parts = split(element,',');
277 if (((parts.size() == 2) || parts.size() == 3) ||
278 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
280 if (parts[1].find(';') != std::string::npos)
281 parts[1] = parts[1].substr(0,parts[1].find(';'));
283 data->invsize.X = MYMAX(0, stof(parts[0]));
284 data->invsize.Y = MYMAX(0, stof(parts[1]));
288 if (parts.size() == 3) {
289 if (parts[2] == "true") {
290 lockSize(true,v2u32(800,600));
294 data->explicit_size = true;
297 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
300 void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
302 std::vector<std::string> parts = split(element, ',');
304 if (parts.size() >= 2) {
305 if (parts[1].find(';') != std::string::npos)
306 parts[1] = parts[1].substr(0, parts[1].find(';'));
308 container_stack.push(pos_offset);
309 pos_offset.X += MYMAX(0, stof(parts[0]));
310 pos_offset.Y += MYMAX(0, stof(parts[1]));
313 errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
316 void GUIFormSpecMenu::parseContainerEnd(parserData* data)
318 if (container_stack.empty()) {
319 errorstream<< "Invalid container end element, no matching container start element" << std::endl;
321 pos_offset = container_stack.top();
322 container_stack.pop();
326 void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
329 warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
333 std::vector<std::string> parts = split(element,';');
335 if (((parts.size() == 4) || (parts.size() == 5)) ||
336 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
338 std::string location = parts[0];
339 std::string listname = parts[1];
340 std::vector<std::string> v_pos = split(parts[2],',');
341 std::vector<std::string> v_geom = split(parts[3],',');
342 std::string startindex;
343 if (parts.size() == 5)
344 startindex = parts[4];
346 MY_CHECKPOS("list",2);
347 MY_CHECKGEOM("list",3);
349 InventoryLocation loc;
351 if(location == "context" || location == "current_name")
352 loc = m_current_inventory_location;
354 loc.deSerialize(location);
356 v2s32 pos = getElementBasePos(true, &v_pos);
358 geom.X = stoi(v_geom[0]);
359 geom.Y = stoi(v_geom[1]);
362 if (!startindex.empty())
363 start_i = stoi(startindex);
365 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
366 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
370 if(!data->explicit_size)
371 warningstream<<"invalid use of list without a size[] element"<<std::endl;
372 m_inventorylists.emplace_back(loc, listname, pos, geom, start_i);
375 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
378 void GUIFormSpecMenu::parseListRing(parserData* data, const std::string &element)
381 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
385 std::vector<std::string> parts = split(element, ';');
387 if (parts.size() == 2) {
388 std::string location = parts[0];
389 std::string listname = parts[1];
391 InventoryLocation loc;
393 if (location == "context" || location == "current_name")
394 loc = m_current_inventory_location;
396 loc.deSerialize(location);
398 m_inventory_rings.emplace_back(loc, listname);
402 if (element.empty() && m_inventorylists.size() > 1) {
403 size_t siz = m_inventorylists.size();
404 // insert the last two inv list elements into the list ring
405 const ListDrawSpec &spa = m_inventorylists[siz - 2];
406 const ListDrawSpec &spb = m_inventorylists[siz - 1];
407 m_inventory_rings.emplace_back(spa.inventoryloc, spa.listname);
408 m_inventory_rings.emplace_back(spb.inventoryloc, spb.listname);
412 errorstream<< "Invalid list ring element(" << parts.size() << ", "
413 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
416 void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
418 std::vector<std::string> parts = split(element,';');
420 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
421 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
423 std::vector<std::string> v_pos = split(parts[0],',');
424 std::string name = parts[1];
425 std::string label = parts[2];
426 std::string selected;
428 if (parts.size() >= 4)
431 MY_CHECKPOS("checkbox",0);
433 v2s32 pos = getElementBasePos(false, &v_pos);
435 bool fselected = false;
437 if (selected == "true")
440 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
441 s32 spacing = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH) + 7;
443 core::rect<s32> rect = core::rect<s32>(
444 pos.X, pos.Y + ((imgsize.Y / 2) - m_btn_height),
445 pos.X + m_font->getDimension(wlabel.c_str()).Width + spacing,
446 pos.Y + ((imgsize.Y / 2) + m_btn_height));
450 wlabel, //Needed for displaying text on MSVC
455 spec.ftype = f_CheckBox;
457 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
458 spec.fid, spec.flabel.c_str());
460 if (spec.fname == data->focused_fieldname) {
461 Environment->setFocus(e);
464 m_checkboxes.emplace_back(spec,e);
465 m_fields.push_back(spec);
468 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
471 void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
473 std::vector<std::string> parts = split(element,';');
475 if (parts.size() >= 5) {
476 std::vector<std::string> v_pos = split(parts[0],',');
477 std::vector<std::string> v_dim = split(parts[1],',');
478 std::string name = parts[3];
479 std::string value = parts[4];
481 MY_CHECKPOS("scrollbar",0);
483 v2s32 pos = getElementBasePos(false, &v_pos);
485 if (v_dim.size() != 2) {
486 errorstream<< "Invalid size for element " << "scrollbar"
487 << "specified: \"" << parts[1] << "\"" << std::endl;
492 dim.X = stof(v_dim[0]) * spacing.X;
493 dim.Y = stof(v_dim[1]) * spacing.Y;
495 core::rect<s32> rect =
496 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
505 bool is_horizontal = true;
507 if (parts[2] == "vertical")
508 is_horizontal = false;
510 spec.ftype = f_ScrollBar;
512 gui::IGUIScrollBar* e =
513 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
517 e->setPos(stoi(parts[4]));
519 e->setLargeStep(100);
521 m_scrollbars.emplace_back(spec,e);
522 m_fields.push_back(spec);
525 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
528 void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
530 std::vector<std::string> parts = split(element,';');
532 if ((parts.size() == 3) ||
533 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
535 std::vector<std::string> v_pos = split(parts[0],',');
536 std::vector<std::string> v_geom = split(parts[1],',');
537 std::string name = unescape_string(parts[2]);
539 MY_CHECKPOS("image", 0);
540 MY_CHECKGEOM("image", 1);
542 v2s32 pos = getElementBasePos(true, &v_pos);
544 geom.X = stof(v_geom[0]) * (float)imgsize.X;
545 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
547 if (!data->explicit_size)
548 warningstream<<"invalid use of image without a size[] element"<<std::endl;
549 m_images.emplace_back(name, pos, geom);
553 if (parts.size() == 2) {
554 std::vector<std::string> v_pos = split(parts[0],',');
555 std::string name = unescape_string(parts[1]);
557 MY_CHECKPOS("image", 0);
559 v2s32 pos = getElementBasePos(true, &v_pos);
561 if (!data->explicit_size)
562 warningstream<<"invalid use of image without a size[] element"<<std::endl;
563 m_images.emplace_back(name, pos);
566 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
569 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
571 std::vector<std::string> parts = split(element,';');
573 if ((parts.size() == 3) ||
574 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
576 std::vector<std::string> v_pos = split(parts[0],',');
577 std::vector<std::string> v_geom = split(parts[1],',');
578 std::string name = parts[2];
580 MY_CHECKPOS("itemimage",0);
581 MY_CHECKGEOM("itemimage",1);
583 v2s32 pos = getElementBasePos(true, &v_pos);
585 geom.X = stof(v_geom[0]) * (float)imgsize.X;
586 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
588 if(!data->explicit_size)
589 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
590 m_itemimages.emplace_back("", name, pos, geom);
593 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
596 void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
597 const std::string &type)
599 std::vector<std::string> parts = split(element,';');
601 if ((parts.size() == 4) ||
602 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
604 std::vector<std::string> v_pos = split(parts[0],',');
605 std::vector<std::string> v_geom = split(parts[1],',');
606 std::string name = parts[2];
607 std::string label = parts[3];
609 MY_CHECKPOS("button",0);
610 MY_CHECKGEOM("button",1);
612 v2s32 pos = getElementBasePos(false, &v_pos);
614 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
615 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
617 core::rect<s32> rect =
618 core::rect<s32>(pos.X, pos.Y - m_btn_height,
619 pos.X + geom.X, pos.Y + m_btn_height);
621 if(!data->explicit_size)
622 warningstream<<"invalid use of button without a size[] element"<<std::endl;
624 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
632 spec.ftype = f_Button;
633 if(type == "button_exit")
635 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
636 spec.flabel.c_str());
638 if (spec.fname == data->focused_fieldname) {
639 Environment->setFocus(e);
642 m_fields.push_back(spec);
645 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
648 void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
650 std::vector<std::string> parts = split(element,';');
652 if (((parts.size() == 3) || (parts.size() == 4)) ||
653 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
655 std::vector<std::string> v_pos = split(parts[0],',');
656 std::vector<std::string> v_geom = split(parts[1],',');
657 std::string name = unescape_string(parts[2]);
659 MY_CHECKPOS("background",0);
660 MY_CHECKGEOM("background",1);
662 v2s32 pos = getElementBasePos(true, &v_pos);
663 pos.X -= (spacing.X - (float)imgsize.X) / 2;
664 pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
667 geom.X = stof(v_geom[0]) * spacing.X;
668 geom.Y = stof(v_geom[1]) * spacing.Y;
671 if (parts.size() == 4 && is_yes(parts[3])) {
672 pos.X = stoi(v_pos[0]); //acts as offset
673 pos.Y = stoi(v_pos[1]); //acts as offset
677 if (!data->explicit_size && !clip)
678 warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
680 m_backgrounds.emplace_back(name, pos, geom, clip);
684 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
687 void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
689 std::vector<std::string> parts = split(element,';');
691 data->table_options.clear();
692 for (const std::string &part : parts) {
693 // Parse table option
694 std::string opt = unescape_string(part);
695 data->table_options.push_back(GUITable::splitOption(opt));
699 void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
701 std::vector<std::string> parts = split(element,';');
703 data->table_columns.clear();
704 for (const std::string &part : parts) {
705 std::vector<std::string> col_parts = split(part,',');
706 GUITable::TableColumn column;
708 if (!col_parts.empty())
709 column.type = col_parts[0];
710 // Parse column options
711 for (size_t j = 1; j < col_parts.size(); ++j) {
712 std::string opt = unescape_string(col_parts[j]);
713 column.options.push_back(GUITable::splitOption(opt));
715 data->table_columns.push_back(column);
719 void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
721 std::vector<std::string> parts = split(element,';');
723 if (((parts.size() == 4) || (parts.size() == 5)) ||
724 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
726 std::vector<std::string> v_pos = split(parts[0],',');
727 std::vector<std::string> v_geom = split(parts[1],',');
728 std::string name = parts[2];
729 std::vector<std::string> items = split(parts[3],',');
730 std::string str_initial_selection;
731 std::string str_transparent = "false";
733 if (parts.size() >= 5)
734 str_initial_selection = parts[4];
736 MY_CHECKPOS("table",0);
737 MY_CHECKGEOM("table",1);
739 v2s32 pos = getElementBasePos(false, &v_pos);
741 geom.X = stof(v_geom[0]) * spacing.X;
742 geom.Y = stof(v_geom[1]) * spacing.Y;
744 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
753 spec.ftype = f_Table;
755 for (std::string &item : items) {
756 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
759 //now really show table
760 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
763 if (spec.fname == data->focused_fieldname) {
764 Environment->setFocus(e);
767 e->setTable(data->table_options, data->table_columns, items);
769 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
770 e->setDynamicData(data->table_dyndata[name]);
773 if (!str_initial_selection.empty() && str_initial_selection != "0")
774 e->setSelected(stoi(str_initial_selection));
776 m_tables.emplace_back(spec, e);
777 m_fields.push_back(spec);
780 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
783 void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
785 std::vector<std::string> parts = split(element,';');
787 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
788 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
790 std::vector<std::string> v_pos = split(parts[0],',');
791 std::vector<std::string> v_geom = split(parts[1],',');
792 std::string name = parts[2];
793 std::vector<std::string> items = split(parts[3],',');
794 std::string str_initial_selection;
795 std::string str_transparent = "false";
797 if (parts.size() >= 5)
798 str_initial_selection = parts[4];
800 if (parts.size() >= 6)
801 str_transparent = parts[5];
803 MY_CHECKPOS("textlist",0);
804 MY_CHECKGEOM("textlist",1);
806 v2s32 pos = getElementBasePos(false, &v_pos);
808 geom.X = stof(v_geom[0]) * spacing.X;
809 geom.Y = stof(v_geom[1]) * spacing.Y;
812 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
821 spec.ftype = f_Table;
823 for (std::string &item : items) {
824 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
827 //now really show list
828 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
831 if (spec.fname == data->focused_fieldname) {
832 Environment->setFocus(e);
835 e->setTextList(items, is_yes(str_transparent));
837 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
838 e->setDynamicData(data->table_dyndata[name]);
841 if (!str_initial_selection.empty() && str_initial_selection != "0")
842 e->setSelected(stoi(str_initial_selection));
844 m_tables.emplace_back(spec, e);
845 m_fields.push_back(spec);
848 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
852 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
854 std::vector<std::string> parts = split(element,';');
856 if ((parts.size() == 5) ||
857 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
859 std::vector<std::string> v_pos = split(parts[0],',');
860 std::string name = parts[2];
861 std::vector<std::string> items = split(parts[3],',');
862 std::string str_initial_selection;
863 str_initial_selection = parts[4];
865 MY_CHECKPOS("dropdown",0);
867 v2s32 pos = getElementBasePos(false, &v_pos);
869 s32 width = stof(parts[1]) * spacing.Y;
871 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
872 pos.X + width, pos.Y + (m_btn_height * 2));
881 spec.ftype = f_DropDown;
884 //now really show list
885 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
887 if (spec.fname == data->focused_fieldname) {
888 Environment->setFocus(e);
891 for (const std::string &item : items) {
892 e->addItem(unescape_translate(unescape_string(
893 utf8_to_wide(item))).c_str());
896 if (!str_initial_selection.empty())
897 e->setSelected(stoi(str_initial_selection)-1);
899 m_fields.push_back(spec);
901 m_dropdowns.emplace_back(spec, std::vector<std::string>());
902 std::vector<std::string> &values = m_dropdowns.back().second;
903 for (const std::string &item : items) {
904 values.push_back(unescape_string(item));
909 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
910 << element << "'" << std::endl;
913 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
915 std::vector<std::string> parts = split(element,';');
916 if (parts.size() == 2 ||
917 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
918 field_close_on_enter[parts[0]] = is_yes(parts[1]);
922 void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
924 std::vector<std::string> parts = split(element,';');
926 if ((parts.size() == 4) || (parts.size() == 5) ||
927 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
929 std::vector<std::string> v_pos = split(parts[0],',');
930 std::vector<std::string> v_geom = split(parts[1],',');
931 std::string name = parts[2];
932 std::string label = parts[3];
934 MY_CHECKPOS("pwdfield",0);
935 MY_CHECKGEOM("pwdfield",1);
937 v2s32 pos = getElementBasePos(false, &v_pos);
941 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
943 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
944 pos.Y -= m_btn_height;
945 geom.Y = m_btn_height*2;
947 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
949 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
959 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
961 if (spec.fname == data->focused_fieldname) {
962 Environment->setFocus(e);
965 if (label.length() >= 1) {
966 int font_height = g_fontengine->getTextHeight();
967 rect.UpperLeftCorner.Y -= font_height;
968 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
969 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
973 e->setPasswordBox(true,L'*');
976 evt.EventType = EET_KEY_INPUT_EVENT;
977 evt.KeyInput.Key = KEY_END;
978 evt.KeyInput.Char = 0;
979 evt.KeyInput.Control = false;
980 evt.KeyInput.Shift = false;
981 evt.KeyInput.PressedDown = true;
984 if (parts.size() >= 5) {
985 // TODO: remove after 2016-11-03
986 warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
987 " instead of the 5th param" << std::endl;
988 field_close_on_enter[name] = is_yes(parts[4]);
991 m_fields.push_back(spec);
994 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
997 void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
998 core::rect<s32> &rect, bool is_multiline)
1000 bool is_editable = !spec.fname.empty();
1001 if (!is_editable && !is_multiline) {
1002 // spec field id to 0, this stops submit searching for a value that isn't there
1003 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1010 } else if (is_multiline &&
1011 spec.fdefault.empty() && !spec.flabel.empty()) {
1012 // Multiline textareas: swap default and label for backwards compat
1013 spec.flabel.swap(spec.fdefault);
1016 gui::IGUIEditBox *e = nullptr;
1017 static constexpr bool use_intl_edit_box = USE_FREETYPE &&
1018 IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9;
1020 if (use_intl_edit_box && g_settings->getBool("freetype")) {
1021 e = new gui::intlGUIEditBox(spec.fdefault.c_str(),
1022 true, Environment, this, spec.fid, rect, is_editable, is_multiline);
1026 e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true,
1027 Environment, this, spec.fid, rect, is_editable, true);
1028 else if (is_editable)
1029 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
1034 if (is_editable && spec.fname == data->focused_fieldname)
1035 Environment->setFocus(e);
1038 e->setMultiLine(true);
1039 e->setWordWrap(true);
1040 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1043 evt.EventType = EET_KEY_INPUT_EVENT;
1044 evt.KeyInput.Key = KEY_END;
1045 evt.KeyInput.Char = 0;
1046 evt.KeyInput.Control = 0;
1047 evt.KeyInput.Shift = 0;
1048 evt.KeyInput.PressedDown = true;
1053 if (!spec.flabel.empty()) {
1054 int font_height = g_fontengine->getTextHeight();
1055 rect.UpperLeftCorner.Y -= font_height;
1056 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1057 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1062 void GUIFormSpecMenu::parseSimpleField(parserData* data,
1063 std::vector<std::string> &parts)
1065 std::string name = parts[0];
1066 std::string label = parts[1];
1067 std::string default_val = parts[2];
1069 core::rect<s32> rect;
1071 if(data->explicit_size)
1072 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
1074 v2s32 pos = getElementBasePos(false, nullptr);
1075 pos.Y = ((m_fields.size()+2)*60);
1076 v2s32 size = DesiredRect.getSize();
1078 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
1079 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
1083 default_val = m_form_src->resolveText(default_val);
1086 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1091 utf8_to_wide(unescape_string(default_val)),
1095 createTextField(data, spec, rect, false);
1097 if (parts.size() >= 4) {
1098 // TODO: remove after 2016-11-03
1099 warningstream << "field/simple: use field_close_on_enter[name, enabled]" <<
1100 " instead of the 4th param" << std::endl;
1101 field_close_on_enter[name] = is_yes(parts[3]);
1104 m_fields.push_back(spec);
1107 void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
1108 const std::string &type)
1111 std::vector<std::string> v_pos = split(parts[0],',');
1112 std::vector<std::string> v_geom = split(parts[1],',');
1113 std::string name = parts[2];
1114 std::string label = parts[3];
1115 std::string default_val = parts[4];
1117 MY_CHECKPOS(type,0);
1118 MY_CHECKGEOM(type,1);
1120 v2s32 pos = getElementBasePos(false, &v_pos);
1125 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1127 if (type == "textarea")
1129 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1130 pos.Y += m_btn_height;
1134 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1135 pos.Y -= m_btn_height;
1136 geom.Y = m_btn_height*2;
1139 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1141 if(!data->explicit_size)
1142 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1145 default_val = m_form_src->resolveText(default_val);
1148 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1153 utf8_to_wide(unescape_string(default_val)),
1157 createTextField(data, spec, rect, type == "textarea");
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 = getElementBasePos(false, nullptr);
1201 pos.X += stof(v_pos[0]) * spacing.X;
1202 pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * 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 = gui::StaticText::add(Environment,
1232 spec.flabel.c_str(), rect, false, false, this, spec.fid);
1233 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
1234 m_fields.push_back(spec);
1239 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1242 void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
1244 std::vector<std::string> parts = split(element,';');
1246 if ((parts.size() == 2) ||
1247 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1249 std::vector<std::string> v_pos = split(parts[0],',');
1250 std::wstring text = unescape_translate(
1251 unescape_string(utf8_to_wide(parts[1])));
1253 MY_CHECKPOS("vertlabel",1);
1255 v2s32 pos = getElementBasePos(false, &v_pos);
1257 core::rect<s32> rect = core::rect<s32>(
1258 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1260 font_line_height(m_font)
1262 +((imgsize.Y/2)- m_btn_height));
1263 //actually text.length() would be correct but adding +1 avoids to break all mods
1265 if(!data->explicit_size)
1266 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1270 for (wchar_t i : text) {
1281 gui::IGUIStaticText *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
1282 rect, false, false, this, spec.fid);
1283 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1284 m_fields.push_back(spec);
1287 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1290 void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
1291 const std::string &type)
1293 std::vector<std::string> parts = split(element,';');
1295 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1296 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1298 std::vector<std::string> v_pos = split(parts[0],',');
1299 std::vector<std::string> v_geom = split(parts[1],',');
1300 std::string image_name = parts[2];
1301 std::string name = parts[3];
1302 std::string label = parts[4];
1304 MY_CHECKPOS("imagebutton",0);
1305 MY_CHECKGEOM("imagebutton",1);
1307 v2s32 pos = getElementBasePos(false, &v_pos);
1309 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1310 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1312 bool noclip = false;
1313 bool drawborder = true;
1314 std::string pressed_image_name;
1316 if (parts.size() >= 7) {
1317 if (parts[5] == "true")
1319 if (parts[6] == "false")
1323 if (parts.size() >= 8) {
1324 pressed_image_name = parts[7];
1327 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1329 if(!data->explicit_size)
1330 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1332 image_name = unescape_string(image_name);
1333 pressed_image_name = unescape_string(pressed_image_name);
1335 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1340 utf8_to_wide(image_name),
1343 spec.ftype = f_Button;
1344 if(type == "image_button_exit")
1345 spec.is_exit = true;
1347 video::ITexture *texture = 0;
1348 video::ITexture *pressed_texture = 0;
1349 texture = m_tsrc->getTexture(image_name);
1350 if (!pressed_image_name.empty())
1351 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1353 pressed_texture = texture;
1355 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1357 if (spec.fname == data->focused_fieldname) {
1358 Environment->setFocus(e);
1361 e->setUseAlphaChannel(true);
1362 e->setImage(guiScalingImageButton(
1363 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1364 e->setPressedImage(guiScalingImageButton(
1365 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1366 e->setScaleImage(true);
1367 e->setNotClipped(noclip);
1368 e->setDrawBorder(drawborder);
1370 m_fields.push_back(spec);
1374 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1377 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
1379 std::vector<std::string> parts = split(element,';');
1381 if (((parts.size() == 4) || (parts.size() == 6)) ||
1382 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1384 std::vector<std::string> v_pos = split(parts[0],',');
1385 std::string name = parts[1];
1386 std::vector<std::string> buttons = split(parts[2],',');
1387 std::string str_index = parts[3];
1388 bool show_background = true;
1389 bool show_border = true;
1390 int tab_index = stoi(str_index) -1;
1392 MY_CHECKPOS("tabheader",0);
1394 if (parts.size() == 6) {
1395 if (parts[4] == "true")
1396 show_background = false;
1397 if (parts[5] == "false")
1398 show_border = false;
1408 spec.ftype = f_TabHeader;
1412 v2f32 pos_f = pos_offset * spacing;
1413 pos_f.X += stof(v_pos[0]) * spacing.X;
1414 pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
1415 pos = v2s32(pos_f.X, pos_f.Y);
1418 geom.X = DesiredRect.getWidth();
1419 geom.Y = m_btn_height*2;
1421 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1424 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1425 show_background, show_border, spec.fid);
1426 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1427 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1428 e->setTabHeight(m_btn_height*2);
1430 if (spec.fname == data->focused_fieldname) {
1431 Environment->setFocus(e);
1434 e->setNotClipped(true);
1436 for (const std::string &button : buttons) {
1437 e->addTab(unescape_translate(unescape_string(
1438 utf8_to_wide(button))).c_str(), -1);
1441 if ((tab_index >= 0) &&
1442 (buttons.size() < INT_MAX) &&
1443 (tab_index < (int) buttons.size()))
1444 e->setActiveTab(tab_index);
1446 m_fields.push_back(spec);
1449 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1450 << element << "'" << std::endl;
1453 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
1456 if (m_client == 0) {
1457 warningstream << "invalid use of item_image_button with m_client==0"
1462 std::vector<std::string> parts = split(element,';');
1464 if ((parts.size() == 5) ||
1465 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1467 std::vector<std::string> v_pos = split(parts[0],',');
1468 std::vector<std::string> v_geom = split(parts[1],',');
1469 std::string item_name = parts[2];
1470 std::string name = parts[3];
1471 std::string label = parts[4];
1473 label = unescape_string(label);
1474 item_name = unescape_string(item_name);
1476 MY_CHECKPOS("itemimagebutton",0);
1477 MY_CHECKGEOM("itemimagebutton",1);
1479 v2s32 pos = getElementBasePos(false, &v_pos);
1481 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1482 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1484 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1486 if(!data->explicit_size)
1487 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1489 IItemDefManager *idef = m_client->idef();
1491 item.deSerialize(item_name, idef);
1494 TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
1495 m_default_tooltip_bgcolor,
1496 m_default_tooltip_color);
1500 utf8_to_wide(label),
1501 utf8_to_wide(item_name),
1502 258 + m_fields.size()
1505 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1507 if (spec.fname == data->focused_fieldname) {
1508 Environment->setFocus(e);
1511 spec.ftype = f_Button;
1512 rect+=data->basepos-padding;
1514 m_fields.push_back(spec);
1516 pos = getElementBasePos(true, &v_pos);
1517 m_itemimages.emplace_back("", item_name, e, pos, geom);
1518 m_static_texts.emplace_back(utf8_to_wide(label), rect, e);
1521 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1524 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
1526 std::vector<std::string> parts = split(element,';');
1528 if ((parts.size() == 3) ||
1529 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1531 std::vector<std::string> v_pos = split(parts[0],',');
1532 std::vector<std::string> v_geom = split(parts[1],',');
1534 MY_CHECKPOS("box",0);
1535 MY_CHECKGEOM("box",1);
1537 v2s32 pos = getElementBasePos(true, &v_pos);
1539 geom.X = stof(v_geom[0]) * spacing.X;
1540 geom.Y = stof(v_geom[1]) * spacing.Y;
1542 video::SColor tmp_color;
1544 if (parseColorString(parts[2], tmp_color, false, 0x8C)) {
1545 BoxDrawSpec spec(pos, geom, tmp_color);
1547 m_boxes.push_back(spec);
1550 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1554 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1557 void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
1559 std::vector<std::string> parts = split(element,';');
1561 if (((parts.size() == 1) || (parts.size() == 2)) ||
1562 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION))) {
1563 parseColorString(parts[0], m_bgcolor, false);
1565 if (parts.size() == 2) {
1566 std::string fullscreen = parts[1];
1567 m_bgfullscreen = is_yes(fullscreen);
1573 errorstream << "Invalid bgcolor element(" << parts.size() << "): '" << element << "'"
1577 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
1579 std::vector<std::string> parts = split(element,';');
1581 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1582 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1584 parseColorString(parts[0], m_slotbg_n, false);
1585 parseColorString(parts[1], m_slotbg_h, false);
1587 if (parts.size() >= 3) {
1588 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1589 m_slotborder = true;
1592 if (parts.size() == 5) {
1593 video::SColor tmp_color;
1595 if (parseColorString(parts[3], tmp_color, false))
1596 m_default_tooltip_bgcolor = tmp_color;
1597 if (parseColorString(parts[4], tmp_color, false))
1598 m_default_tooltip_color = tmp_color;
1602 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1605 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
1607 std::vector<std::string> parts = split(element,';');
1608 if (parts.size() < 2) {
1609 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
1610 << element << "'" << std::endl;
1614 // Get mode and check size
1615 bool rect_mode = parts[0].find(',') != std::string::npos;
1616 size_t base_size = rect_mode ? 3 : 2;
1617 if (parts.size() != base_size && parts.size() != base_size + 2) {
1618 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
1619 << element << "'" << std::endl;
1624 video::SColor bgcolor = m_default_tooltip_bgcolor;
1625 video::SColor color = m_default_tooltip_color;
1626 if (parts.size() == base_size + 2 &&
1627 (!parseColorString(parts[base_size], bgcolor, false) ||
1628 !parseColorString(parts[base_size + 1], color, false))) {
1629 errorstream << "Invalid color in tooltip element(" << parts.size()
1630 << "): '" << element << "'" << std::endl;
1634 // Make tooltip spec
1635 std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
1636 TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
1640 std::vector<std::string> v_pos = split(parts[0], ',');
1641 std::vector<std::string> v_geom = split(parts[1], ',');
1643 MY_CHECKPOS("tooltip", 0);
1644 MY_CHECKGEOM("tooltip", 1);
1646 v2s32 pos = getElementBasePos(true, &v_pos);
1648 geom.X = stof(v_geom[0]) * spacing.X;
1649 geom.Y = stof(v_geom[1]) * spacing.Y;
1651 irr::core::rect<s32> rect(pos, pos + geom);
1652 m_tooltip_rects.emplace_back(rect, spec);
1654 m_tooltips[parts[0]] = spec;
1658 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
1664 std::vector<std::string> parts = split(data,'[');
1666 if (parts.size() < 2) {
1670 if (parts[0] != "formspec_version") {
1674 if (is_number(parts[1])) {
1675 m_formspec_version = mystoi(parts[1]);
1682 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
1684 if (element.empty())
1687 std::vector<std::string> parts = split(element,'[');
1689 if (parts.size() < 2)
1692 std::string type = trim(parts[0]);
1693 std::string description = trim(parts[1]);
1695 if (type != "size" && type != "invsize")
1698 if (type == "invsize")
1699 log_deprecated("Deprecated formspec element \"invsize\" is used");
1701 parseSize(data, description);
1706 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
1708 if (element.empty())
1711 std::vector<std::string> parts = split(element, '[');
1713 if (parts.size() != 2)
1716 std::string type = trim(parts[0]);
1717 std::string description = trim(parts[1]);
1719 if (type != "position")
1722 parsePosition(data, description);
1727 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
1729 std::vector<std::string> parts = split(element, ',');
1731 if (parts.size() == 2) {
1732 data->offset.X = stof(parts[0]);
1733 data->offset.Y = stof(parts[1]);
1737 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
1740 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
1742 if (element.empty())
1745 std::vector<std::string> parts = split(element, '[');
1747 if (parts.size() != 2)
1750 std::string type = trim(parts[0]);
1751 std::string description = trim(parts[1]);
1753 if (type != "anchor")
1756 parseAnchor(data, description);
1761 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
1763 std::vector<std::string> parts = split(element, ',');
1765 if (parts.size() == 2) {
1766 data->anchor.X = stof(parts[0]);
1767 data->anchor.Y = stof(parts[1]);
1771 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
1772 << "'" << std::endl;
1775 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
1778 if (element.empty())
1781 std::vector<std::string> parts = split(element,'[');
1783 // ugly workaround to keep compatibility
1784 if (parts.size() > 2) {
1785 if (trim(parts[0]) == "image") {
1786 for (unsigned int i=2;i< parts.size(); i++) {
1787 parts[1] += "[" + parts[i];
1793 if (parts.size() < 2) {
1797 std::string type = trim(parts[0]);
1798 std::string description = trim(parts[1]);
1800 if (type == "container") {
1801 parseContainer(data, description);
1805 if (type == "container_end") {
1806 parseContainerEnd(data);
1810 if (type == "list") {
1811 parseList(data, description);
1815 if (type == "listring") {
1816 parseListRing(data, description);
1820 if (type == "checkbox") {
1821 parseCheckbox(data, description);
1825 if (type == "image") {
1826 parseImage(data, description);
1830 if (type == "item_image") {
1831 parseItemImage(data, description);
1835 if (type == "button" || type == "button_exit") {
1836 parseButton(data, description, type);
1840 if (type == "background") {
1841 parseBackground(data,description);
1845 if (type == "tableoptions"){
1846 parseTableOptions(data,description);
1850 if (type == "tablecolumns"){
1851 parseTableColumns(data,description);
1855 if (type == "table"){
1856 parseTable(data,description);
1860 if (type == "textlist"){
1861 parseTextList(data,description);
1865 if (type == "dropdown"){
1866 parseDropDown(data,description);
1870 if (type == "field_close_on_enter") {
1871 parseFieldCloseOnEnter(data, description);
1875 if (type == "pwdfield") {
1876 parsePwdField(data,description);
1880 if ((type == "field") || (type == "textarea")){
1881 parseField(data,description,type);
1885 if (type == "label") {
1886 parseLabel(data,description);
1890 if (type == "vertlabel") {
1891 parseVertLabel(data,description);
1895 if (type == "item_image_button") {
1896 parseItemImageButton(data,description);
1900 if ((type == "image_button") || (type == "image_button_exit")) {
1901 parseImageButton(data,description,type);
1905 if (type == "tabheader") {
1906 parseTabHeader(data,description);
1910 if (type == "box") {
1911 parseBox(data,description);
1915 if (type == "bgcolor") {
1916 parseBackgroundColor(data,description);
1920 if (type == "listcolors") {
1921 parseListColors(data,description);
1925 if (type == "tooltip") {
1926 parseTooltip(data,description);
1930 if (type == "scrollbar") {
1931 parseScrollBar(data, description);
1936 infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
1940 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1942 /* useless to regenerate without a screensize */
1943 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1950 for (auto &m_table : m_tables) {
1951 std::string tablename = m_table.first.fname;
1952 GUITable *table = m_table.second;
1953 mydata.table_dyndata[tablename] = table->getDynamicData();
1957 if (!m_focused_element.empty())
1958 mydata.focused_fieldname = m_focused_element;
1961 gui::IGUIElement *focused_element = Environment->getFocus();
1962 if (focused_element && focused_element->getParent() == this) {
1963 s32 focused_id = focused_element->getID();
1964 if (focused_id > 257) {
1965 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
1966 if (field.fid == focused_id) {
1967 mydata.focused_fieldname = field.fname;
1977 for (auto &table_it : m_tables) {
1978 table_it.second->drop();
1981 mydata.size= v2s32(100,100);
1982 mydata.screensize = screensize;
1983 mydata.offset = v2f32(0.5f, 0.5f);
1984 mydata.anchor = v2f32(0.5f, 0.5f);
1986 // Base position of contents of form
1987 mydata.basepos = getBasePos();
1989 /* Convert m_init_draw_spec to m_inventorylists */
1991 m_inventorylists.clear();
1993 m_backgrounds.clear();
1994 m_itemimages.clear();
1996 m_checkboxes.clear();
1997 m_scrollbars.clear();
2001 m_tooltip_rects.clear();
2002 m_inventory_rings.clear();
2003 m_static_texts.clear();
2004 m_dropdowns.clear();
2006 m_bgfullscreen = false;
2009 v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
2010 m_bgcolor = video::SColor(
2011 (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
2012 clamp_u8(myround(formspec_bgcolor.X)),
2013 clamp_u8(myround(formspec_bgcolor.Y)),
2014 clamp_u8(myround(formspec_bgcolor.Z))
2019 v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
2020 m_fullscreen_bgcolor = video::SColor(
2021 (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
2022 clamp_u8(myround(formspec_bgcolor.X)),
2023 clamp_u8(myround(formspec_bgcolor.Y)),
2024 clamp_u8(myround(formspec_bgcolor.Z))
2028 m_slotbg_n = video::SColor(255,128,128,128);
2029 m_slotbg_h = video::SColor(255,192,192,192);
2031 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
2032 m_default_tooltip_color = video::SColor(255,255,255,255);
2034 m_slotbordercolor = video::SColor(200,0,0,0);
2035 m_slotborder = false;
2039 assert(!m_tooltip_element);
2040 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
2041 m_tooltip_element = gui::StaticText::add(Environment, L"",
2042 core::rect<s32>(0, 0, 110, 18));
2043 m_tooltip_element->enableOverrideColor(true);
2044 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2045 m_tooltip_element->setDrawBackground(true);
2046 m_tooltip_element->setDrawBorder(true);
2047 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2048 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
2049 m_tooltip_element->setWordWrap(false);
2050 //we're not parent so no autograb for this one!
2051 m_tooltip_element->grab();
2054 std::vector<std::string> elements = split(m_formspec_string,']');
2057 /* try to read version from first element only */
2058 if (!elements.empty()) {
2059 if (parseVersionDirect(elements[0])) {
2064 /* we need size first in order to calculate image scale */
2065 mydata.explicit_size = false;
2066 for (; i< elements.size(); i++) {
2067 if (!parseSizeDirect(&mydata, elements[i])) {
2072 /* "position" element is always after "size" element if it used */
2073 for (; i< elements.size(); i++) {
2074 if (!parsePositionDirect(&mydata, elements[i])) {
2079 /* "anchor" element is always after "position" (or "size" element) if it used */
2080 for (; i< elements.size(); i++) {
2081 if (!parseAnchorDirect(&mydata, elements[i])) {
2086 /* "no_prepend" element is always after "position" (or "size" element) if it used */
2087 bool enable_prepends = true;
2088 for (; i < elements.size(); i++) {
2089 if (elements[i].empty())
2092 std::vector<std::string> parts = split(elements[i], '[');
2093 if (trim(parts[0]) == "no_prepend")
2094 enable_prepends = false;
2099 if (mydata.explicit_size) {
2100 // compute scaling for specified form size
2102 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
2103 v2u32 delta = current_screensize - m_lockscreensize;
2105 if (current_screensize.Y > m_lockscreensize.Y)
2110 if (current_screensize.X > m_lockscreensize.X)
2115 offset = v2s32(delta.X,delta.Y);
2117 mydata.screensize = m_lockscreensize;
2119 offset = v2s32(0,0);
2122 double gui_scaling = g_settings->getFloat("gui_scaling");
2123 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2127 // In fixed-size mode, inventory image size
2128 // is 0.53 inch multiplied by the gui_scaling
2129 // config parameter. This magic size is chosen
2130 // to make the main menu (15.5 inventory images
2131 // wide, including border) just fit into the
2132 // default window (800 pixels wide) at 96 DPI
2133 // and default scaling (1.00).
2134 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
2136 // In variable-size mode, we prefer to make the
2137 // inventory image size 1/15 of screen height,
2138 // multiplied by the gui_scaling config parameter.
2139 // If the preferred size won't fit the whole
2140 // form on the screen, either horizontally or
2141 // vertically, then we scale it down to fit.
2142 // (The magic numbers in the computation of what
2143 // fits arise from the scaling factors in the
2144 // following stanza, including the form border,
2145 // help text space, and 0.1 inventory slot spare.)
2146 // However, a minimum size is also set, that
2147 // the image size can't be less than 0.3 inch
2148 // multiplied by gui_scaling, even if this means
2149 // the form doesn't fit the screen.
2151 // For mobile devices these magic numbers are
2152 // different and forms should always use the
2153 // maximum screen space available.
2154 double prefer_imgsize = mydata.screensize.Y / 10 * gui_scaling;
2155 double fitx_imgsize = mydata.screensize.X /
2156 ((12.0 / 8.0) * (0.5 + mydata.invsize.X));
2157 double fity_imgsize = mydata.screensize.Y /
2158 ((15.0 / 11.0) * (0.85 + mydata.invsize.Y));
2159 use_imgsize = MYMIN(prefer_imgsize,
2160 MYMIN(fitx_imgsize, fity_imgsize));
2162 double prefer_imgsize = mydata.screensize.Y / 15 * gui_scaling;
2163 double fitx_imgsize = mydata.screensize.X /
2164 ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
2165 double fity_imgsize = mydata.screensize.Y /
2166 ((15.0 / 13.0) * (0.85 * mydata.invsize.Y));
2167 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2168 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
2169 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2170 MYMIN(fitx_imgsize, fity_imgsize)));
2174 // Everything else is scaled in proportion to the
2175 // inventory image size. The inventory slot spacing
2176 // is 5/4 image size horizontally and 15/13 image size
2177 // vertically. The padding around the form (incorporating
2178 // the border of the outer inventory slots) is 3/8
2179 // image size. Font height (baseline to baseline)
2180 // is 2/5 vertical inventory slot spacing, and button
2181 // half-height is 7/8 of font height.
2182 imgsize = v2s32(use_imgsize, use_imgsize);
2183 spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2184 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2185 m_btn_height = use_imgsize*15.0/13 * 0.35;
2187 m_font = g_fontengine->getFont();
2189 mydata.size = v2s32(
2190 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2191 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2193 DesiredRect = mydata.rect = core::rect<s32>(
2194 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
2195 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
2196 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
2197 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
2200 // Non-size[] form must consist only of text fields and
2201 // implicit "Proceed" button. Use default font, and
2202 // temporary form size which will be recalculated below.
2203 m_font = g_fontengine->getFont();
2204 m_btn_height = font_line_height(m_font) * 0.875;
2205 DesiredRect = core::rect<s32>(
2206 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
2207 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
2208 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
2209 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
2212 recalculateAbsolutePosition(false);
2213 mydata.basepos = getBasePos();
2214 m_tooltip_element->setOverrideFont(m_font);
2216 gui::IGUISkin *skin = Environment->getSkin();
2218 gui::IGUIFont *old_font = skin->getFont();
2219 skin->setFont(m_font);
2221 pos_offset = v2f32();
2223 if (enable_prepends) {
2224 std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
2225 for (const auto &element : prepend_elements)
2226 parseElement(&mydata, element);
2229 for (; i< elements.size(); i++) {
2230 parseElement(&mydata, elements[i]);
2233 if (!container_stack.empty()) {
2234 errorstream << "Invalid formspec string: container was never closed!"
2238 // If there are fields without explicit size[], add a "Proceed"
2239 // button and adjust size to fit all the fields.
2240 if (!m_fields.empty() && !mydata.explicit_size) {
2241 mydata.rect = core::rect<s32>(
2242 mydata.screensize.X/2 - 580/2,
2243 mydata.screensize.Y/2 - 300/2,
2244 mydata.screensize.X/2 + 580/2,
2245 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2247 DesiredRect = mydata.rect;
2248 recalculateAbsolutePosition(false);
2249 mydata.basepos = getBasePos();
2252 v2s32 pos = mydata.basepos;
2253 pos.Y = ((m_fields.size()+2)*60);
2255 v2s32 size = DesiredRect.getSize();
2257 core::rect<s32>(size.X/2-70, pos.Y,
2258 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2259 const wchar_t *text = wgettext("Proceed");
2260 Environment->addButton(mydata.rect, this, 257, text);
2266 //set initial focus if parser didn't set it
2267 focused_element = Environment->getFocus();
2268 if (!focused_element
2269 || !isMyChild(focused_element)
2270 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2273 skin->setFont(old_font);
2277 bool GUIFormSpecMenu::getAndroidUIInput()
2279 if (!hasAndroidUIInput())
2282 std::string fieldname = m_jni_field_name;
2283 m_jni_field_name.clear();
2285 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2286 iter != m_fields.end(); ++iter) {
2288 if (iter->fname != fieldname) {
2291 IGUIElement* tochange = getElementFromId(iter->fid);
2293 if (tochange == 0) {
2297 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2301 std::string text = porting::getInputDialogValue();
2303 ((gui::IGUIEditBox *)tochange)->setText(utf8_to_wide(text).c_str());
2309 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2311 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2313 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
2314 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2315 s32 item_i = i + s.start_item_i;
2316 s32 x = (i%s.geom.X) * spacing.X;
2317 s32 y = (i/s.geom.X) * spacing.Y;
2319 core::rect<s32> rect = imgrect + s.pos + p0;
2320 if(rect.isPointInside(p))
2322 return ItemSpec(s.inventoryloc, s.listname, item_i);
2327 return ItemSpec(InventoryLocation(), "", -1);
2330 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int layer,
2333 video::IVideoDriver* driver = Environment->getVideoDriver();
2335 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2337 warningstream<<"GUIFormSpecMenu::drawList(): "
2338 <<"The inventory location "
2339 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2343 InventoryList *ilist = inv->getList(s.listname);
2345 warningstream<<"GUIFormSpecMenu::drawList(): "
2346 <<"The inventory list \""<<s.listname<<"\" @ \""
2347 <<s.inventoryloc.dump()<<"\" doesn't exist"
2352 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2354 for (s32 i = 0; i < s.geom.X * s.geom.Y; i++) {
2355 s32 item_i = i + s.start_item_i;
2356 if (item_i >= (s32)ilist->getSize())
2359 s32 x = (i%s.geom.X) * spacing.X;
2360 s32 y = (i/s.geom.X) * spacing.Y;
2362 core::rect<s32> rect = imgrect + s.pos + p;
2363 ItemStack item = ilist->getItem(item_i);
2365 bool selected = m_selected_item
2366 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2367 && m_selected_item->listname == s.listname
2368 && m_selected_item->i == item_i;
2369 bool hovering = rect.isPointInside(m_pointer);
2370 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2371 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2375 item_hovered = true;
2376 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2378 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2382 //Draw inv slot borders
2384 s32 x1 = rect.UpperLeftCorner.X;
2385 s32 y1 = rect.UpperLeftCorner.Y;
2386 s32 x2 = rect.LowerRightCorner.X;
2387 s32 y2 = rect.LowerRightCorner.Y;
2389 driver->draw2DRectangle(m_slotbordercolor,
2390 core::rect<s32>(v2s32(x1 - border, y1 - border),
2391 v2s32(x2 + border, y1)), NULL);
2392 driver->draw2DRectangle(m_slotbordercolor,
2393 core::rect<s32>(v2s32(x1 - border, y2),
2394 v2s32(x2 + border, y2 + border)), NULL);
2395 driver->draw2DRectangle(m_slotbordercolor,
2396 core::rect<s32>(v2s32(x1 - border, y1),
2397 v2s32(x1, y2)), NULL);
2398 driver->draw2DRectangle(m_slotbordercolor,
2399 core::rect<s32>(v2s32(x2, y1),
2400 v2s32(x2 + border, y2)), NULL);
2406 item.takeItem(m_selected_amount);
2408 if (!item.empty()) {
2409 drawItemStack(driver, m_font, item,
2410 rect, &AbsoluteClippingRect, m_client,
2415 std::wstring tooltip_text;
2416 if (hovering && !m_selected_item) {
2417 const std::string &desc = item.metadata.getString("description");
2420 utf8_to_wide(item.getDefinition(m_client->idef()).description);
2422 tooltip_text = utf8_to_wide(desc);
2424 if (!item.name.empty()) {
2425 if (tooltip_text.empty())
2426 tooltip_text = utf8_to_wide(item.name);
2427 if (m_tooltip_append_itemname)
2428 tooltip_text += utf8_to_wide(" [" + item.name + "]");
2431 if (!tooltip_text.empty()) {
2432 showTooltip(tooltip_text, m_default_tooltip_color,
2433 m_default_tooltip_bgcolor);
2439 void GUIFormSpecMenu::drawSelectedItem()
2441 video::IVideoDriver* driver = Environment->getVideoDriver();
2443 if (!m_selected_item) {
2444 drawItemStack(driver, m_font, ItemStack(),
2445 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2446 NULL, m_client, IT_ROT_DRAGGED);
2450 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2452 InventoryList *list = inv->getList(m_selected_item->listname);
2454 ItemStack stack = list->getItem(m_selected_item->i);
2455 stack.count = m_selected_amount;
2457 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2458 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2459 rect.constrainTo(driver->getViewPort());
2460 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
2463 void GUIFormSpecMenu::drawMenu()
2466 const std::string &newform = m_form_src->getForm();
2467 if (newform != m_formspec_string) {
2468 m_formspec_string = newform;
2469 regenerateGui(m_screensize_old);
2473 gui::IGUISkin* skin = Environment->getSkin();
2474 sanity_check(skin != NULL);
2475 gui::IGUIFont *old_font = skin->getFont();
2476 skin->setFont(m_font);
2478 updateSelectedItem();
2480 video::IVideoDriver* driver = Environment->getVideoDriver();
2482 v2u32 screenSize = driver->getScreenSize();
2483 core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
2486 driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
2488 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2490 m_tooltip_element->setVisible(false);
2492 for (const auto &pair : m_tooltip_rects) {
2493 if (pair.first.isPointInside(m_pointer)) {
2494 const std::wstring &text = pair.second.tooltip;
2495 if (!text.empty()) {
2496 showTooltip(text, pair.second.color, pair.second.bgcolor);
2505 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_backgrounds) {
2506 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2509 // Image size on screen
2510 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2511 // Image rectangle on screen
2512 core::rect<s32> rect = imgrect + spec.pos;
2515 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2516 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2517 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2518 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2519 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2522 const video::SColor color(255,255,255,255);
2523 const video::SColor colors[] = {color,color,color,color};
2524 draw2DImageFilterScaled(driver, texture, rect,
2525 core::rect<s32>(core::position2d<s32>(0,0),
2526 core::dimension2di(texture->getOriginalSize())),
2527 NULL/*&AbsoluteClippingRect*/, colors, true);
2529 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2530 errorstream << "\t" << spec.name << std::endl;
2537 for (const GUIFormSpecMenu::BoxDrawSpec &spec : m_boxes) {
2538 irr::video::SColor todraw = spec.color;
2540 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2541 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2543 driver->draw2DRectangle(todraw, rect, 0);
2549 gui::IGUIElement::draw();
2554 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_images) {
2555 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2558 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2559 // Image size on screen
2560 core::rect<s32> imgrect;
2563 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2566 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2568 // Image rectangle on screen
2569 core::rect<s32> rect = imgrect + spec.pos;
2570 const video::SColor color(255,255,255,255);
2571 const video::SColor colors[] = {color,color,color,color};
2572 draw2DImageFilterScaled(driver, texture, rect,
2573 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2574 NULL/*&AbsoluteClippingRect*/, colors, true);
2577 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2578 errorstream << "\t" << spec.name << std::endl;
2585 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_itemimages) {
2589 IItemDefManager *idef = m_client->idef();
2591 item.deSerialize(spec.item_name, idef);
2592 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2593 // Viewport rectangle on screen
2594 core::rect<s32> rect = imgrect + spec.pos;
2595 if (spec.parent_button && spec.parent_button->isPressed()) {
2596 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2597 rect += core::dimension2d<s32>(
2598 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2600 rect += core::dimension2d<s32>(
2601 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2602 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2605 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2606 m_client, IT_ROT_NONE);
2611 Layer 0: Item slot rectangles
2612 Layer 1: Item images; prepare tooltip
2614 bool item_hovered = false;
2615 for (int layer = 0; layer < 2; layer++) {
2616 for (const GUIFormSpecMenu::ListDrawSpec &spec : m_inventorylists) {
2617 drawList(spec, layer, item_hovered);
2620 if (!item_hovered) {
2621 drawItemStack(driver, m_font, ItemStack(),
2622 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2623 NULL, m_client, IT_ROT_HOVERED);
2626 /* TODO find way to show tooltips on touchscreen */
2627 #ifndef HAVE_TOUCHSCREENGUI
2628 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
2632 Draw static text elements
2634 for (const GUIFormSpecMenu::StaticTextSpec &spec : m_static_texts) {
2635 core::rect<s32> rect = spec.rect;
2636 if (spec.parent_button && spec.parent_button->isPressed()) {
2637 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2638 rect += core::dimension2d<s32>(
2639 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2641 // Use image offset instead of text's because its a bit smaller
2642 // and fits better, also TEXT_OFFSET_X is always 0
2643 rect += core::dimension2d<s32>(
2644 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2645 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2648 video::SColor color(255, 255, 255, 255);
2649 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
2653 Draw fields/buttons tooltips
2655 gui::IGUIElement *hovered =
2656 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2658 if (hovered != NULL) {
2659 s32 id = hovered->getID();
2663 m_old_tooltip_id = id;
2665 if (id == m_old_tooltip_id) {
2666 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
2668 m_hovered_time = porting::getTimeMs();
2669 m_old_tooltip_id = id;
2673 // Find and update the current tooltip
2674 if (id != -1 && delta >= m_tooltip_show_delay) {
2675 for (const FieldSpec &field : m_fields) {
2677 if (field.fid != id)
2680 const std::wstring &text = m_tooltips[field.fname].tooltip;
2682 showTooltip(text, m_tooltips[field.fname].color,
2683 m_tooltips[field.fname].bgcolor);
2690 m_tooltip_element->draw();
2693 Draw dragged item stack
2697 skin->setFont(old_font);
2701 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
2702 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
2704 const std::wstring ntext = translate_string(text);
2705 m_tooltip_element->setOverrideColor(color);
2706 m_tooltip_element->setBackgroundColor(bgcolor);
2707 setStaticText(m_tooltip_element, ntext.c_str());
2709 // Tooltip size and offset
2710 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2711 #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
2712 std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
2713 s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
2715 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2717 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
2718 int tooltip_offset_x = m_btn_height;
2719 int tooltip_offset_y = m_btn_height;
2721 tooltip_offset_x *= 3;
2722 tooltip_offset_y = 0;
2723 if (m_pointer.X > (s32)screenSize.X / 2)
2724 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
2727 // Calculate and set the tooltip position
2728 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2729 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2730 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2731 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2732 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2733 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2735 m_tooltip_element->setRelativePosition(
2737 core::position2d<s32>(tooltip_x, tooltip_y),
2738 core::dimension2d<s32>(tooltip_width, tooltip_height)
2742 // Display the tooltip
2743 m_tooltip_element->setVisible(true);
2744 bringToFront(m_tooltip_element);
2747 void GUIFormSpecMenu::updateSelectedItem()
2749 verifySelectedItem();
2751 // If craftresult is nonempty and nothing else is selected, select it now.
2752 if (!m_selected_item) {
2753 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
2754 if (s.listname != "craftpreview")
2757 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2761 InventoryList *list = inv->getList("craftresult");
2763 if (!list || list->getSize() == 0)
2766 const ItemStack &item = list->getItem(0);
2770 // Grab selected item from the crafting result list
2771 m_selected_item = new ItemSpec;
2772 m_selected_item->inventoryloc = s.inventoryloc;
2773 m_selected_item->listname = "craftresult";
2774 m_selected_item->i = 0;
2775 m_selected_amount = item.count;
2776 m_selected_dragging = false;
2781 // If craftresult is selected, keep the whole stack selected
2782 if (m_selected_item && m_selected_item->listname == "craftresult")
2783 m_selected_amount = verifySelectedItem().count;
2786 ItemStack GUIFormSpecMenu::verifySelectedItem()
2788 // If the selected stack has become empty for some reason, deselect it.
2789 // If the selected stack has become inaccessible, deselect it.
2790 // If the selected stack has become smaller, adjust m_selected_amount.
2791 // Return the selected stack.
2795 if(m_selected_item->isValid())
2797 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2800 InventoryList *list = inv->getList(m_selected_item->listname);
2801 if(list && (u32) m_selected_item->i < list->getSize())
2803 ItemStack stack = list->getItem(m_selected_item->i);
2804 if (!m_selected_swap.empty()) {
2805 if (m_selected_swap.name == stack.name &&
2806 m_selected_swap.count == stack.count)
2807 m_selected_swap.clear();
2809 m_selected_amount = std::min(m_selected_amount, stack.count);
2818 // selection was not valid
2819 delete m_selected_item;
2820 m_selected_item = NULL;
2821 m_selected_amount = 0;
2822 m_selected_dragging = false;
2827 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2833 if (quitmode == quit_mode_accept) {
2834 fields["quit"] = "true";
2837 if (quitmode == quit_mode_cancel) {
2838 fields["quit"] = "true";
2839 m_text_dst->gotText(fields);
2843 if (current_keys_pending.key_down) {
2844 fields["key_down"] = "true";
2845 current_keys_pending.key_down = false;
2848 if (current_keys_pending.key_up) {
2849 fields["key_up"] = "true";
2850 current_keys_pending.key_up = false;
2853 if (current_keys_pending.key_enter) {
2854 fields["key_enter"] = "true";
2855 current_keys_pending.key_enter = false;
2858 if (!current_field_enter_pending.empty()) {
2859 fields["key_enter_field"] = current_field_enter_pending;
2860 current_field_enter_pending = "";
2863 if (current_keys_pending.key_escape) {
2864 fields["key_escape"] = "true";
2865 current_keys_pending.key_escape = false;
2868 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
2870 std::string name = s.fname;
2871 if (s.ftype == f_Button) {
2872 fields[name] = wide_to_utf8(s.flabel);
2873 } else if (s.ftype == f_Table) {
2874 GUITable *table = getTable(s.fname);
2876 fields[name] = table->checkEvent();
2879 else if(s.ftype == f_DropDown) {
2880 // no dynamic cast possible due to some distributions shipped
2881 // without rtti support in irrlicht
2882 IGUIElement * element = getElementFromId(s.fid);
2883 gui::IGUIComboBox *e = NULL;
2884 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2885 e = static_cast<gui::IGUIComboBox*>(element);
2887 s32 selected = e->getSelected();
2888 if (selected >= 0) {
2889 std::vector<std::string> *dropdown_values =
2890 getDropDownValues(s.fname);
2891 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2892 fields[name] = (*dropdown_values)[selected];
2896 else if (s.ftype == f_TabHeader) {
2897 // no dynamic cast possible due to some distributions shipped
2898 // without rttzi support in irrlicht
2899 IGUIElement * element = getElementFromId(s.fid);
2900 gui::IGUITabControl *e = NULL;
2901 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2902 e = static_cast<gui::IGUITabControl *>(element);
2906 std::stringstream ss;
2907 ss << (e->getActiveTab() +1);
2908 fields[name] = ss.str();
2911 else if (s.ftype == f_CheckBox) {
2912 // no dynamic cast possible due to some distributions shipped
2913 // without rtti support in irrlicht
2914 IGUIElement * element = getElementFromId(s.fid);
2915 gui::IGUICheckBox *e = NULL;
2916 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2917 e = static_cast<gui::IGUICheckBox*>(element);
2922 fields[name] = "true";
2924 fields[name] = "false";
2927 else if (s.ftype == f_ScrollBar) {
2928 // no dynamic cast possible due to some distributions shipped
2929 // without rtti support in irrlicht
2930 IGUIElement * element = getElementFromId(s.fid);
2931 gui::IGUIScrollBar *e = NULL;
2932 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2933 e = static_cast<gui::IGUIScrollBar*>(element);
2937 std::stringstream os;
2939 if (s.fdefault == L"Changed")
2940 fields[name] = "CHG:" + os.str();
2942 fields[name] = "VAL:" + os.str();
2947 IGUIElement* e = getElementFromId(s.fid);
2949 fields[name] = wide_to_utf8(e->getText());
2955 m_text_dst->gotText(fields);
2959 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2961 while(tocheck != NULL) {
2962 if (tocheck == parent) {
2965 tocheck = tocheck->getParent();
2970 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2972 // The IGUITabControl renders visually using the skin's selected
2973 // font, which we override for the duration of form drawing,
2974 // but computes tab hotspots based on how it would have rendered
2975 // using the font that is selected at the time of button release.
2976 // To make these two consistent, temporarily override the skin's
2977 // font while the IGUITabControl is processing the event.
2978 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2979 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2980 s32 x = event.MouseInput.X;
2981 s32 y = event.MouseInput.Y;
2982 gui::IGUIElement *hovered =
2983 Environment->getRootGUIElement()->getElementFromPoint(
2984 core::position2d<s32>(x, y));
2985 if (hovered && isMyChild(hovered) &&
2986 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2987 gui::IGUISkin* skin = Environment->getSkin();
2988 sanity_check(skin != NULL);
2989 gui::IGUIFont *old_font = skin->getFont();
2990 skin->setFont(m_font);
2991 bool retval = hovered->OnEvent(event);
2992 skin->setFont(old_font);
2997 // Fix Esc/Return key being eaten by checkboxen and tables
2998 if(event.EventType==EET_KEY_INPUT_EVENT) {
2999 KeyPress kp(event.KeyInput);
3000 if (kp == EscapeKey || kp == CancelKey
3001 || kp == getKeySetting("keymap_inventory")
3002 || event.KeyInput.Key==KEY_RETURN) {
3003 gui::IGUIElement *focused = Environment->getFocus();
3004 if (focused && isMyChild(focused) &&
3005 (focused->getType() == gui::EGUIET_LIST_BOX ||
3006 focused->getType() == gui::EGUIET_CHECK_BOX) &&
3007 (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
3008 event.KeyInput.Key != KEY_RETURN)) {
3014 // Mouse wheel events: send to hovered element instead of focused
3015 if(event.EventType==EET_MOUSE_INPUT_EVENT
3016 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
3017 s32 x = event.MouseInput.X;
3018 s32 y = event.MouseInput.Y;
3019 gui::IGUIElement *hovered =
3020 Environment->getRootGUIElement()->getElementFromPoint(
3021 core::position2d<s32>(x, y));
3022 if (hovered && isMyChild(hovered)) {
3023 hovered->OnEvent(event);
3028 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
3029 s32 x = event.MouseInput.X;
3030 s32 y = event.MouseInput.Y;
3031 gui::IGUIElement *hovered =
3032 Environment->getRootGUIElement()->getElementFromPoint(
3033 core::position2d<s32>(x, y));
3034 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3035 m_old_tooltip_id = -1;
3037 if (!isChild(hovered,this)) {
3038 if (DoubleClickDetection(event)) {
3044 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3045 /* TODO add a check like:
3046 if (event.JoystickEvent != joystick_we_listen_for)
3049 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3051 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3053 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3055 acceptInput(quit_mode_accept);
3063 return GUIModalMenu::preprocessEvent(event);
3066 /******************************************************************************/
3067 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3069 /* The following code is for capturing double-clicks of the mouse button
3070 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3071 * -- which closes the form -- under some circumstances.
3073 * There have been many github issues reporting this as a bug even though it
3074 * was an intended feature. For this reason, remapping the double-click as
3075 * an ESC must be explicitly set when creating this class via the
3076 * /p remap_dbl_click parameter of the constructor.
3079 if (!m_remap_dbl_click)
3082 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3083 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3084 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3086 m_doubleclickdetect[1].pos = m_pointer;
3087 m_doubleclickdetect[1].time = porting::getTimeMs();
3089 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3090 u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
3095 double squaredistance =
3096 m_doubleclickdetect[0].pos
3097 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3099 if (squaredistance > (30*30)) {
3103 SEvent* translated = new SEvent();
3104 assert(translated != 0);
3105 //translate doubleclick to escape
3106 memset(translated, 0, sizeof(SEvent));
3107 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3108 translated->KeyInput.Key = KEY_ESCAPE;
3109 translated->KeyInput.Control = false;
3110 translated->KeyInput.Shift = false;
3111 translated->KeyInput.PressedDown = true;
3112 translated->KeyInput.Char = 0;
3113 OnEvent(*translated);
3115 // no need to send the key up event as we're already deleted
3116 // and no one else did notice this event
3124 void GUIFormSpecMenu::tryClose()
3128 acceptInput(quit_mode_cancel);
3131 m_text_dst->gotText(L"MenuQuit");
3135 enum ButtonEventType : u8
3148 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3150 if (event.EventType==EET_KEY_INPUT_EVENT) {
3151 KeyPress kp(event.KeyInput);
3152 if (event.KeyInput.PressedDown && (
3153 (kp == EscapeKey) || (kp == CancelKey) ||
3154 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
3159 if (m_client != NULL && event.KeyInput.PressedDown &&
3160 (kp == getKeySetting("keymap_screenshot"))) {
3161 m_client->makeScreenshot();
3163 if (event.KeyInput.PressedDown &&
3164 (event.KeyInput.Key==KEY_RETURN ||
3165 event.KeyInput.Key==KEY_UP ||
3166 event.KeyInput.Key==KEY_DOWN)
3168 switch (event.KeyInput.Key) {
3170 current_keys_pending.key_enter = true;
3173 current_keys_pending.key_up = true;
3176 current_keys_pending.key_down = true;
3180 //can't happen at all!
3181 FATAL_ERROR("Reached a source line that can't ever been reached");
3184 if (current_keys_pending.key_enter && m_allowclose) {
3185 acceptInput(quit_mode_accept);
3195 /* Mouse event other than movement, or crossing the border of inventory
3196 field while holding right mouse button
3198 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3199 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3200 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3201 event.MouseInput.isRightPressed() &&
3202 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3204 // Get selected item and hovered/clicked item (s)
3206 m_old_tooltip_id = -1;
3207 updateSelectedItem();
3208 ItemSpec s = getItemAtPos(m_pointer);
3210 Inventory *inv_selected = NULL;
3211 Inventory *inv_s = NULL;
3212 InventoryList *list_s = NULL;
3214 if (m_selected_item) {
3215 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3216 sanity_check(inv_selected);
3217 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3224 inv_s = m_invmgr->getInventory(s.inventoryloc);
3227 errorstream << "InventoryMenu: The selected inventory location "
3228 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3230 s.i = -1; // make it invalid again
3234 list_s = inv_s->getList(s.listname);
3235 if (list_s == NULL) {
3236 verbosestream << "InventoryMenu: The selected inventory list \""
3237 << s.listname << "\" does not exist" << std::endl;
3238 s.i = -1; // make it invalid again
3242 if ((u32)s.i >= list_s->getSize()) {
3243 infostream << "InventoryMenu: The selected inventory list \""
3244 << s.listname << "\" is too small (i=" << s.i << ", size="
3245 << list_s->getSize() << ")" << std::endl;
3246 s.i = -1; // make it invalid again
3250 s_count = list_s->getItem(s.i).count;
3253 bool identical = m_selected_item && s.isValid() &&
3254 (inv_selected == inv_s) &&
3255 (m_selected_item->listname == s.listname) &&
3256 (m_selected_item->i == s.i);
3258 ButtonEventType button = BET_LEFT;
3259 ButtonEventType updown = BET_OTHER;
3260 switch (event.MouseInput.Event) {
3261 case EMIE_LMOUSE_PRESSED_DOWN:
3262 button = BET_LEFT; updown = BET_DOWN;
3264 case EMIE_RMOUSE_PRESSED_DOWN:
3265 button = BET_RIGHT; updown = BET_DOWN;
3267 case EMIE_MMOUSE_PRESSED_DOWN:
3268 button = BET_MIDDLE; updown = BET_DOWN;
3270 case EMIE_MOUSE_WHEEL:
3271 button = (event.MouseInput.Wheel > 0) ?
3272 BET_WHEEL_UP : BET_WHEEL_DOWN;
3275 case EMIE_LMOUSE_LEFT_UP:
3276 button = BET_LEFT; updown = BET_UP;
3278 case EMIE_RMOUSE_LEFT_UP:
3279 button = BET_RIGHT; updown = BET_UP;
3281 case EMIE_MMOUSE_LEFT_UP:
3282 button = BET_MIDDLE; updown = BET_UP;
3284 case EMIE_MOUSE_MOVED:
3291 // Set this number to a positive value to generate a move action
3292 // from m_selected_item to s.
3293 u32 move_amount = 0;
3295 // Set this number to a positive value to generate a move action
3296 // from s to the next inventory ring.
3297 u32 shift_move_amount = 0;
3299 // Set this number to a positive value to generate a drop action
3300 // from m_selected_item.
3301 u32 drop_amount = 0;
3303 // Set this number to a positive value to generate a craft action at s.
3304 u32 craft_amount = 0;
3308 // Some mouse button has been pressed
3310 //infostream<<"Mouse button "<<button<<" pressed at p=("
3311 // <<p.X<<","<<p.Y<<")"<<std::endl;
3313 m_selected_dragging = false;
3315 if (s.isValid() && s.listname == "craftpreview") {
3316 // Craft preview has been clicked: craft
3317 craft_amount = (button == BET_MIDDLE ? 10 : 1);
3318 } else if (!m_selected_item) {
3319 if (s_count && button != BET_WHEEL_UP) {
3320 // Non-empty stack has been clicked: select or shift-move it
3321 m_selected_item = new ItemSpec(s);
3324 if (button == BET_RIGHT)
3325 count = (s_count + 1) / 2;
3326 else if (button == BET_MIDDLE)
3327 count = MYMIN(s_count, 10);
3328 else if (button == BET_WHEEL_DOWN)
3333 if (!event.MouseInput.Shift) {
3334 // no shift: select item
3335 m_selected_amount = count;
3336 m_selected_dragging = button != BET_WHEEL_DOWN;
3337 m_auto_place = false;
3339 // shift pressed: move item, right click moves 1
3340 shift_move_amount = button == BET_RIGHT ? 1 : count;
3343 } else { // m_selected_item != NULL
3344 assert(m_selected_amount >= 1);
3347 // Clicked a slot: move
3348 if (button == BET_RIGHT || button == BET_WHEEL_UP)
3350 else if (button == BET_MIDDLE)
3351 move_amount = MYMIN(m_selected_amount, 10);
3352 else if (button == BET_LEFT)
3353 move_amount = m_selected_amount;
3357 if (button == BET_WHEEL_DOWN) {
3358 if (m_selected_amount < s_count)
3359 ++m_selected_amount;
3361 if (move_amount >= m_selected_amount)
3362 m_selected_amount = 0;
3364 m_selected_amount -= move_amount;
3368 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
3369 && button != BET_WHEEL_DOWN) {
3370 // Clicked outside of the window: drop
3371 if (button == BET_RIGHT || button == BET_WHEEL_UP)
3373 else if (button == BET_MIDDLE)
3374 drop_amount = MYMIN(m_selected_amount, 10);
3376 drop_amount = m_selected_amount;
3381 // Some mouse button has been released
3383 //infostream<<"Mouse button "<<button<<" released at p=("
3384 // <<p.X<<","<<p.Y<<")"<<std::endl;
3386 if (m_selected_dragging && m_selected_item) {
3389 // Dragged to different slot: move all selected
3390 move_amount = m_selected_amount;
3392 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3393 // Dragged outside of window: drop all selected
3394 drop_amount = m_selected_amount;
3398 m_selected_dragging = false;
3399 // Keep track of whether the mouse button be released
3400 // One click is drag without dropping. Click + release
3401 // + click changes to drop item when moved mode
3402 if (m_selected_item)
3403 m_auto_place = true;
3406 // Mouse has been moved and rmb is down and mouse pointer just
3407 // entered a new inventory field (checked in the entry-if, this
3408 // is the only action here that is generated by mouse movement)
3409 if (m_selected_item && s.isValid()) {
3411 // TODO: middle mouse to move 10 items might be handy
3413 // Only move an item if the destination slot is empty
3414 // or contains the same item type as what is going to be
3416 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3417 InventoryList *list_to = list_s;
3418 assert(list_from && list_to);
3419 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3420 ItemStack stack_to = list_to->getItem(s.i);
3421 if (stack_to.empty() || stack_to.name == stack_from.name)
3430 // Possibly send inventory action to server
3431 if (move_amount > 0) {
3432 // Send IAction::Move
3434 assert(m_selected_item && m_selected_item->isValid());
3435 assert(s.isValid());
3437 assert(inv_selected && inv_s);
3438 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3439 InventoryList *list_to = list_s;
3440 assert(list_from && list_to);
3441 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3442 ItemStack stack_to = list_to->getItem(s.i);
3444 // Check how many items can be moved
3445 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3446 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
3448 // If source stack cannot be added to destination stack at all,
3450 if (leftover.count == stack_from.count &&
3451 leftover.name == stack_from.name) {
3453 if (m_selected_swap.empty()) {
3454 m_selected_amount = stack_to.count;
3455 m_selected_dragging = false;
3457 // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
3458 // Skip next validation checks due async inventory calls
3459 m_selected_swap = stack_to;
3464 // Source stack goes fully into destination stack
3465 else if (leftover.empty()) {
3466 m_selected_amount -= move_amount;
3468 // Source stack goes partly into destination stack
3470 move_amount -= leftover.count;
3471 m_selected_amount -= move_amount;
3475 infostream << "Handing IAction::Move to manager" << std::endl;
3476 IMoveAction *a = new IMoveAction();
3477 a->count = move_amount;
3478 a->from_inv = m_selected_item->inventoryloc;
3479 a->from_list = m_selected_item->listname;
3480 a->from_i = m_selected_item->i;
3481 a->to_inv = s.inventoryloc;
3482 a->to_list = s.listname;
3484 m_invmgr->inventoryAction(a);
3486 } else if (shift_move_amount > 0) {
3487 u32 mis = m_inventory_rings.size();
3489 for (; i < mis; i++) {
3490 const ListRingSpec &sp = m_inventory_rings[i];
3491 if (sp.inventoryloc == s.inventoryloc
3492 && sp.listname == s.listname)
3496 if (i >= mis) // if not found
3498 u32 to_inv_ind = (i + 1) % mis;
3499 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3500 InventoryList *list_from = list_s;
3503 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3506 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3509 ItemStack stack_from = list_from->getItem(s.i);
3510 assert(shift_move_amount <= stack_from.count);
3512 infostream << "Handing IAction::Move to manager" << std::endl;
3513 IMoveAction *a = new IMoveAction();
3514 a->count = shift_move_amount;
3515 a->from_inv = s.inventoryloc;
3516 a->from_list = s.listname;
3518 a->to_inv = to_inv_sp.inventoryloc;
3519 a->to_list = to_inv_sp.listname;
3520 a->move_somewhere = true;
3521 m_invmgr->inventoryAction(a);
3523 } else if (drop_amount > 0) {
3524 // Send IAction::Drop
3526 assert(m_selected_item && m_selected_item->isValid());
3527 assert(inv_selected);
3528 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3530 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3532 // Check how many items can be dropped
3533 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3534 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3535 m_selected_amount -= drop_amount;
3537 infostream << "Handing IAction::Drop to manager" << std::endl;
3538 IDropAction *a = new IDropAction();
3539 a->count = drop_amount;
3540 a->from_inv = m_selected_item->inventoryloc;
3541 a->from_list = m_selected_item->listname;
3542 a->from_i = m_selected_item->i;
3543 m_invmgr->inventoryAction(a);
3544 } else if (craft_amount > 0) {
3545 assert(s.isValid());
3547 // if there are no items selected or the selected item
3548 // belongs to craftresult list, proceed with crafting
3549 if (m_selected_item == NULL ||
3550 !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
3554 // Send IACTION_CRAFT
3555 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3556 ICraftAction *a = new ICraftAction();
3557 a->count = craft_amount;
3558 a->craft_inv = s.inventoryloc;
3559 m_invmgr->inventoryAction(a);
3563 // If m_selected_amount has been decreased to zero, deselect
3564 if (m_selected_amount == 0) {
3565 m_selected_swap.clear();
3566 delete m_selected_item;
3567 m_selected_item = NULL;
3568 m_selected_amount = 0;
3569 m_selected_dragging = false;
3571 m_old_pointer = m_pointer;
3573 if (event.EventType == EET_GUI_EVENT) {
3575 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3577 // find the element that was clicked
3578 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3579 if ((s.ftype == f_TabHeader) &&
3580 (s.fid == event.GUIEvent.Caller->getID())) {
3588 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3590 if (!canTakeFocus(event.GUIEvent.Element)) {
3591 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3593 // Returning true disables focus change
3597 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3598 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3599 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3600 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3601 unsigned int btn_id = event.GUIEvent.Caller->getID();
3603 if (btn_id == 257) {
3605 acceptInput(quit_mode_accept);
3609 m_text_dst->gotText(L"ExitButton");
3611 // quitMenu deallocates menu
3615 // find the element that was clicked
3616 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3617 // if its a button, set the send field so
3618 // lua knows which button was pressed
3619 if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
3620 s.fid == event.GUIEvent.Caller->getID()) {
3624 acceptInput(quit_mode_accept);
3627 m_text_dst->gotText(L"ExitButton");
3632 acceptInput(quit_mode_no);
3636 } else if ((s.ftype == f_DropDown) &&
3637 (s.fid == event.GUIEvent.Caller->getID())) {
3638 // only send the changed dropdown
3639 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
3640 if (s2.ftype == f_DropDown) {
3645 acceptInput(quit_mode_no);
3647 // revert configuration to make sure dropdowns are sent on
3648 // regular button click
3649 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
3650 if (s2.ftype == f_DropDown) {
3655 } else if ((s.ftype == f_ScrollBar) &&
3656 (s.fid == event.GUIEvent.Caller->getID())) {
3657 s.fdefault = L"Changed";
3658 acceptInput(quit_mode_no);
3664 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3665 if (event.GUIEvent.Caller->getID() > 257) {
3666 bool close_on_enter = true;
3667 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3668 if (s.ftype == f_Unknown &&
3669 s.fid == event.GUIEvent.Caller->getID()) {
3670 current_field_enter_pending = s.fname;
3671 std::unordered_map<std::string, bool>::const_iterator it =
3672 field_close_on_enter.find(s.fname);
3673 if (it != field_close_on_enter.end())
3674 close_on_enter = (*it).second;
3680 if (m_allowclose && close_on_enter) {
3681 current_keys_pending.key_enter = true;
3682 acceptInput(quit_mode_accept);
3685 current_keys_pending.key_enter = true;
3688 // quitMenu deallocates menu
3693 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3694 int current_id = event.GUIEvent.Caller->getID();
3695 if (current_id > 257) {
3696 // find the element that was clicked
3697 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
3698 // if it's a table, set the send field
3699 // so lua knows which table was changed
3700 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3711 return Parent ? Parent->OnEvent(event) : false;
3715 * get name of element by element id
3716 * @param id of element
3717 * @return name string or empty string
3719 std::string GUIFormSpecMenu::getNameByID(s32 id)
3721 for (FieldSpec &spec : m_fields) {
3722 if (spec.fid == id) {
3730 * get label of element by id
3731 * @param id of element
3732 * @return label string or empty string
3734 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3736 for (FieldSpec &spec : m_fields) {
3737 if (spec.fid == id) {