3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guiFormSpecMenu.h"
27 #include "guiScrollBar.h"
29 #include "constants.h"
31 #include "client/keycode.h"
32 #include "util/strfnd.h"
33 #include <IGUIButton.h>
34 #include <IGUICheckBox.h>
35 #include <IGUIComboBox.h>
36 #include <IGUIEditBox.h>
37 #include <IGUIStaticText.h>
39 #include <IGUITabControl.h>
40 #include "client/renderingengine.h"
42 #include "client/tile.h" // ITextureSource
43 #include "client/hud.h" // drawItemStack
47 #include "scripting_server.h"
48 #include "mainmenumanager.h"
51 #include "client/client.h"
52 #include "client/fontengine.h"
54 #include "util/numeric.h"
55 #include "util/string.h" // for parseColorString()
56 #include "irrlicht_changes/static_text.h"
57 #include "client/guiscalingfilter.h"
58 #include "guiBackgroundImage.h"
60 #include "guiButton.h"
61 #include "guiButtonImage.h"
62 #include "guiButtonItemImage.h"
63 #include "guiEditBoxWithScrollbar.h"
64 #include "guiItemImage.h"
65 #include "guiScrollBar.h"
67 #include "intlGUIEditBox.h"
68 #include "guiHyperText.h"
70 #define MY_CHECKPOS(a,b) \
71 if (v_pos.size() != 2) { \
72 errorstream<< "Invalid pos for element " << a << "specified: \"" \
73 << parts[b] << "\"" << std::endl; \
77 #define MY_CHECKGEOM(a,b) \
78 if (v_geom.size() != 2) { \
79 errorstream<< "Invalid geometry for element " << a << \
80 "specified: \"" << parts[b] << "\"" << std::endl; \
86 static unsigned int font_line_height(gui::IGUIFont *font)
88 return font->getDimension(L"Ay").Height + font->getKerningHeight();
91 inline u32 clamp_u8(s32 value)
93 return (u32) MYMIN(MYMAX(value, 0), 255);
96 GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
97 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
98 Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst,
99 const std::string &formspecPrepend,
100 bool remap_dbl_click):
101 GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr),
105 m_formspec_prepend(formspecPrepend),
108 m_joystick(joystick),
109 m_remap_dbl_click(remap_dbl_click)
111 current_keys_pending.key_down = false;
112 current_keys_pending.key_up = false;
113 current_keys_pending.key_enter = false;
114 current_keys_pending.key_escape = false;
116 m_doubleclickdetect[0].time = 0;
117 m_doubleclickdetect[1].time = 0;
119 m_doubleclickdetect[0].pos = v2s32(0, 0);
120 m_doubleclickdetect[1].pos = v2s32(0, 0);
122 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
123 m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
126 GUIFormSpecMenu::~GUIFormSpecMenu()
130 for (auto &table_it : m_tables)
131 table_it.second->drop();
132 for (auto &inventorylist_it : m_inventorylists)
133 inventorylist_it.e->drop();
134 for (auto &checkbox_it : m_checkboxes)
135 checkbox_it.second->drop();
136 for (auto &scrollbar_it : m_scrollbars)
137 scrollbar_it.second->drop();
138 for (auto &background_it : m_backgrounds)
139 background_it->drop();
140 for (auto &tooltip_rect_it : m_tooltip_rects)
141 tooltip_rect_it.first->drop();
143 delete m_selected_item;
148 void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
149 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
150 const std::string &formspecPrepend)
152 if (cur_formspec == nullptr) {
153 cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
154 client, client->getTextureSource(), fs_src, txt_dest, formspecPrepend);
155 cur_formspec->doPause = false;
158 Caution: do not call (*cur_formspec)->drop() here --
159 the reference might outlive the menu, so we will
160 periodically check if *cur_formspec is the only
161 remaining reference (i.e. the menu was removed)
162 and delete it in that case.
166 cur_formspec->setFormspecPrepend(formspecPrepend);
167 cur_formspec->setFormSource(fs_src);
168 cur_formspec->setTextDest(txt_dest);
172 void GUIFormSpecMenu::removeChildren()
174 const core::list<gui::IGUIElement*> &children = getChildren();
176 while (!children.empty()) {
177 (*children.getLast())->remove();
180 if (m_tooltip_element) {
181 m_tooltip_element->remove();
182 m_tooltip_element->drop();
183 m_tooltip_element = nullptr;
187 void GUIFormSpecMenu::setInitialFocus()
189 // Set initial focus according to following order of precedence:
190 // 1. first empty editbox
194 // 5. first focusable (not statictext, not tabheader)
195 // 6. first child element
197 core::list<gui::IGUIElement*> children = getChildren();
199 // in case "children" contains any NULL elements, remove them
200 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
201 it != children.end();) {
205 it = children.erase(it);
208 // 1. first empty editbox
209 for (gui::IGUIElement *it : children) {
210 if (it->getType() == gui::EGUIET_EDIT_BOX
211 && it->getText()[0] == 0) {
212 Environment->setFocus(it);
218 for (gui::IGUIElement *it : children) {
219 if (it->getType() == gui::EGUIET_EDIT_BOX) {
220 Environment->setFocus(it);
226 for (gui::IGUIElement *it : children) {
227 if (it->getTypeName() == std::string("GUITable")) {
228 Environment->setFocus(it);
234 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
235 it != children.end(); --it) {
236 if ((*it)->getType() == gui::EGUIET_BUTTON) {
237 Environment->setFocus(*it);
242 // 5. first focusable (not statictext, not tabheader)
243 for (gui::IGUIElement *it : children) {
244 if (it->getType() != gui::EGUIET_STATIC_TEXT &&
245 it->getType() != gui::EGUIET_TAB_CONTROL) {
246 Environment->setFocus(it);
251 // 6. first child element
252 if (children.empty())
253 Environment->setFocus(this);
255 Environment->setFocus(*(children.begin()));
258 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
260 for (auto &table : m_tables) {
261 if (tablename == table.first.fname)
267 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
269 for (auto &dropdown : m_dropdowns) {
270 if (name == dropdown.first.fname)
271 return &dropdown.second;
276 v2s32 GUIFormSpecMenu::getElementBasePos(const std::vector<std::string> *v_pos)
278 v2f32 pos_f = v2f32(padding.X, padding.Y) + pos_offset * spacing;
280 pos_f.X += stof((*v_pos)[0]) * spacing.X;
281 pos_f.Y += stof((*v_pos)[1]) * spacing.Y;
283 return v2s32(pos_f.X, pos_f.Y);
286 v2s32 GUIFormSpecMenu::getRealCoordinateBasePos(const std::vector<std::string> &v_pos)
288 return v2s32((stof(v_pos[0]) + pos_offset.X) * imgsize.X,
289 (stof(v_pos[1]) + pos_offset.Y) * imgsize.Y);
292 v2s32 GUIFormSpecMenu::getRealCoordinateGeometry(const std::vector<std::string> &v_geom)
294 return v2s32(stof(v_geom[0]) * imgsize.X, stof(v_geom[1]) * imgsize.Y);
297 void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
299 std::vector<std::string> parts = split(element,',');
301 if (((parts.size() == 2) || parts.size() == 3) ||
302 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
304 if (parts[1].find(';') != std::string::npos)
305 parts[1] = parts[1].substr(0,parts[1].find(';'));
307 data->invsize.X = MYMAX(0, stof(parts[0]));
308 data->invsize.Y = MYMAX(0, stof(parts[1]));
312 if (parts.size() == 3) {
313 if (parts[2] == "true") {
314 lockSize(true,v2u32(800,600));
318 data->explicit_size = true;
321 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
324 void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
326 std::vector<std::string> parts = split(element, ',');
328 if (parts.size() >= 2) {
329 if (parts[1].find(';') != std::string::npos)
330 parts[1] = parts[1].substr(0, parts[1].find(';'));
332 container_stack.push(pos_offset);
333 pos_offset.X += stof(parts[0]);
334 pos_offset.Y += stof(parts[1]);
337 errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
340 void GUIFormSpecMenu::parseContainerEnd(parserData* data)
342 if (container_stack.empty()) {
343 errorstream<< "Invalid container end element, no matching container start element" << std::endl;
345 pos_offset = container_stack.top();
346 container_stack.pop();
350 void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
353 warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
357 std::vector<std::string> parts = split(element,';');
359 if (((parts.size() == 4) || (parts.size() == 5)) ||
360 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
362 std::string location = parts[0];
363 std::string listname = parts[1];
364 std::vector<std::string> v_pos = split(parts[2],',');
365 std::vector<std::string> v_geom = split(parts[3],',');
366 std::string startindex;
367 if (parts.size() == 5)
368 startindex = parts[4];
370 MY_CHECKPOS("list",2);
371 MY_CHECKGEOM("list",3);
373 InventoryLocation loc;
375 if (location == "context" || location == "current_name")
376 loc = m_current_inventory_location;
378 loc.deSerialize(location);
381 geom.X = stoi(v_geom[0]);
382 geom.Y = stoi(v_geom[1]);
385 if (!startindex.empty())
386 start_i = stoi(startindex);
388 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
389 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
393 // check for the existence of inventory and list
394 Inventory *inv = m_invmgr->getInventory(loc);
396 warningstream << "GUIFormSpecMenu::parseList(): "
397 << "The inventory location "
398 << "\"" << loc.dump() << "\" doesn't exist"
402 InventoryList *ilist = inv->getList(listname);
404 warningstream << "GUIFormSpecMenu::parseList(): "
405 << "The inventory list \"" << listname << "\" "
406 << "@ \"" << loc.dump() << "\" doesn't exist"
411 // trim geom if it is larger than the actual inventory size
412 s32 list_size = (s32)ilist->getSize();
413 if (list_size < geom.X * geom.Y + start_i) {
414 list_size -= MYMAX(start_i, 0);
415 geom.Y = list_size / geom.X;
416 geom.Y += list_size % geom.X > 0 ? 1 : 0;
421 if (!data->explicit_size)
422 warningstream << "invalid use of list without a size[] element" << std::endl;
428 258 + m_fields.size(),
433 core::rect<s32> rect;
435 if (data->real_coordinates) {
436 pos = getRealCoordinateBasePos(v_pos);
437 rect = core::rect<s32>(pos.X, pos.Y,
438 pos.X + (geom.X - 1) * (imgsize.X * 1.25) + imgsize.X,
439 pos.Y + (geom.Y - 1) * (imgsize.Y * 1.25) + imgsize.Y);
441 pos = getElementBasePos(&v_pos);
442 rect = core::rect<s32>(pos.X, pos.Y,
443 pos.X + (geom.X - 1) * spacing.X + imgsize.X,
444 pos.Y + (geom.Y - 1) * spacing.Y + imgsize.Y);
447 gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
448 this, spec.fid, rect);
450 m_inventorylists.emplace_back(loc, listname, e, geom, start_i,
451 data->real_coordinates);
452 m_fields.push_back(spec);
455 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
458 void GUIFormSpecMenu::parseListRing(parserData* data, const std::string &element)
461 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
465 std::vector<std::string> parts = split(element, ';');
467 if (parts.size() == 2) {
468 std::string location = parts[0];
469 std::string listname = parts[1];
471 InventoryLocation loc;
473 if (location == "context" || location == "current_name")
474 loc = m_current_inventory_location;
476 loc.deSerialize(location);
478 m_inventory_rings.emplace_back(loc, listname);
482 if (element.empty() && m_inventorylists.size() > 1) {
483 size_t siz = m_inventorylists.size();
484 // insert the last two inv list elements into the list ring
485 const ListDrawSpec &spa = m_inventorylists[siz - 2];
486 const ListDrawSpec &spb = m_inventorylists[siz - 1];
487 m_inventory_rings.emplace_back(spa.inventoryloc, spa.listname);
488 m_inventory_rings.emplace_back(spb.inventoryloc, spb.listname);
492 errorstream<< "Invalid list ring element(" << parts.size() << ", "
493 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
496 void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
498 std::vector<std::string> parts = split(element,';');
500 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
501 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
503 std::vector<std::string> v_pos = split(parts[0],',');
504 std::string name = parts[1];
505 std::string label = parts[2];
506 std::string selected;
508 if (parts.size() >= 4)
511 MY_CHECKPOS("checkbox",0);
513 bool fselected = false;
515 if (selected == "true")
518 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
519 const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str());
520 s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH);
521 s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2;
524 core::rect<s32> rect;
526 if (data->real_coordinates) {
527 pos = getRealCoordinateBasePos(v_pos);
529 rect = core::rect<s32>(
532 pos.X + label_size.Width + cb_size + 7,
536 pos = getElementBasePos(&v_pos);
537 rect = core::rect<s32>(
539 pos.Y + imgsize.Y / 2 - y_center,
540 pos.X + label_size.Width + cb_size + 7,
541 pos.Y + imgsize.Y / 2 + y_center
547 wlabel, //Needed for displaying text on MSVC
552 spec.ftype = f_CheckBox;
554 gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect, this,
555 spec.fid, spec.flabel.c_str());
557 auto style = getStyleForElement("checkbox", name);
558 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
560 if (spec.fname == data->focused_fieldname) {
561 Environment->setFocus(e);
565 m_checkboxes.emplace_back(spec, e);
566 m_fields.push_back(spec);
569 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
572 void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
574 std::vector<std::string> parts = split(element,';');
576 if (parts.size() >= 5) {
577 std::vector<std::string> v_pos = split(parts[0],',');
578 std::vector<std::string> v_geom = split(parts[1],',');
579 std::string name = parts[3];
580 std::string value = parts[4];
582 MY_CHECKPOS("scrollbar",0);
583 MY_CHECKGEOM("scrollbar",1);
588 if (data->real_coordinates) {
589 pos = getRealCoordinateBasePos(v_pos);
590 dim = getRealCoordinateGeometry(v_geom);
592 pos = getElementBasePos(&v_pos);
593 dim.X = stof(v_geom[0]) * spacing.X;
594 dim.Y = stof(v_geom[1]) * spacing.Y;
597 core::rect<s32> rect =
598 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
607 bool is_horizontal = true;
609 if (parts[2] == "vertical")
610 is_horizontal = false;
612 spec.ftype = f_ScrollBar;
614 GUIScrollBar *e = new GUIScrollBar(Environment, this, spec.fid, rect,
615 is_horizontal, true);
617 auto style = getStyleForElement("scrollbar", name);
618 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
619 e->setArrowsVisible(data->scrollbar_options.arrow_visiblity);
621 s32 max = data->scrollbar_options.max;
622 s32 min = data->scrollbar_options.min;
627 e->setPos(stoi(parts[4]));
629 e->setSmallStep(data->scrollbar_options.small_step);
630 e->setLargeStep(data->scrollbar_options.large_step);
632 s32 scrollbar_size = is_horizontal ? dim.X : dim.Y;
634 e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size);
636 m_scrollbars.emplace_back(spec,e);
637 m_fields.push_back(spec);
640 errorstream << "Invalid scrollbar element(" << parts.size() << "): '" << element
644 void GUIFormSpecMenu::parseScrollBarOptions(parserData* data, const std::string &element)
646 std::vector<std::string> parts = split(element, ';');
648 if (parts.size() == 0) {
649 warningstream << "Invalid scrollbaroptions element(" << parts.size() << "): '" <<
650 element << "'" << std::endl;
654 for (const std::string &i : parts) {
655 std::vector<std::string> options = split(i, '=');
657 if (options.size() != 2) {
658 warningstream << "Invalid scrollbaroptions option syntax: '" <<
659 element << "'" << std::endl;
660 continue; // Go to next option
663 if (options[0] == "max") {
664 data->scrollbar_options.max = stoi(options[1]);
666 } else if (options[0] == "min") {
667 data->scrollbar_options.min = stoi(options[1]);
669 } else if (options[0] == "smallstep") {
670 int value = stoi(options[1]);
671 data->scrollbar_options.small_step = value < 0 ? 10 : value;
673 } else if (options[0] == "largestep") {
674 int value = stoi(options[1]);
675 data->scrollbar_options.large_step = value < 0 ? 100 : value;
677 } else if (options[0] == "thumbsize") {
678 int value = stoi(options[1]);
679 data->scrollbar_options.thumb_size = value <= 0 ? 1 : value;
681 } else if (options[0] == "arrows") {
682 std::string value = trim(options[1]);
684 data->scrollbar_options.arrow_visiblity = GUIScrollBar::HIDE;
685 else if (value == "show")
686 data->scrollbar_options.arrow_visiblity = GUIScrollBar::SHOW;
687 else // Auto hide/show
688 data->scrollbar_options.arrow_visiblity = GUIScrollBar::DEFAULT;
692 warningstream << "Invalid scrollbaroptions option(" << options[0] <<
693 "): '" << element << "'" << std::endl;
697 void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
699 std::vector<std::string> parts = split(element,';');
701 if ((parts.size() == 3) ||
702 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
704 std::vector<std::string> v_pos = split(parts[0],',');
705 std::vector<std::string> v_geom = split(parts[1],',');
706 std::string name = unescape_string(parts[2]);
708 MY_CHECKPOS("image", 0);
709 MY_CHECKGEOM("image", 1);
714 if (data->real_coordinates) {
715 pos = getRealCoordinateBasePos(v_pos);
716 geom = getRealCoordinateGeometry(v_geom);
718 pos = getElementBasePos(&v_pos);
719 geom.X = stof(v_geom[0]) * (float)imgsize.X;
720 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
723 if (!data->explicit_size)
724 warningstream<<"invalid use of image without a size[] element"<<std::endl;
726 video::ITexture *texture = m_tsrc->getTexture(name);
728 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
729 << std::endl << "\t" << name << std::endl;
737 258 + m_fields.size(),
740 core::rect<s32> rect(pos, pos + geom);
741 gui::IGUIImage *e = Environment->addImage(rect, this, spec.fid, 0, true);
742 e->setImage(texture);
743 e->setScaleImage(true);
744 e->setNotClipped(true);
745 m_fields.push_back(spec);
750 if (parts.size() == 2) {
751 std::vector<std::string> v_pos = split(parts[0],',');
752 std::string name = unescape_string(parts[1]);
754 MY_CHECKPOS("image", 0);
756 v2s32 pos = getElementBasePos(&v_pos);
758 if (!data->explicit_size)
759 warningstream<<"invalid use of image without a size[] element"<<std::endl;
761 video::ITexture *texture = m_tsrc->getTexture(name);
763 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
764 << std::endl << "\t" << name << std::endl;
772 258 + m_fields.size()
774 gui::IGUIImage *e = Environment->addImage(texture, pos, true, this,
776 e->setNotClipped(true);
777 m_fields.push_back(spec);
781 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
784 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
786 std::vector<std::string> parts = split(element,';');
788 if ((parts.size() == 3) ||
789 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
791 std::vector<std::string> v_pos = split(parts[0],',');
792 std::vector<std::string> v_geom = split(parts[1],',');
793 std::string name = parts[2];
795 MY_CHECKPOS("itemimage",0);
796 MY_CHECKGEOM("itemimage",1);
801 if (data->real_coordinates) {
802 pos = getRealCoordinateBasePos(v_pos);
803 geom = getRealCoordinateGeometry(v_geom);
805 pos = getElementBasePos(&v_pos);
806 geom.X = stof(v_geom[0]) * (float)imgsize.X;
807 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
810 if(!data->explicit_size)
811 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
817 258 + m_fields.size(),
820 spec.ftype = f_ItemImage;
822 GUIItemImage *e = new GUIItemImage(Environment, this, spec.fid,
823 core::rect<s32>(pos, pos + geom), name, m_font, m_client);
826 m_fields.push_back(spec);
829 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
832 void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
833 const std::string &type)
835 std::vector<std::string> parts = split(element,';');
837 if ((parts.size() == 4) ||
838 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
840 std::vector<std::string> v_pos = split(parts[0],',');
841 std::vector<std::string> v_geom = split(parts[1],',');
842 std::string name = parts[2];
843 std::string label = parts[3];
845 MY_CHECKPOS("button",0);
846 MY_CHECKGEOM("button",1);
850 core::rect<s32> rect;
852 if (data->real_coordinates) {
853 pos = getRealCoordinateBasePos(v_pos);
854 geom = getRealCoordinateGeometry(v_geom);
855 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
858 pos = getElementBasePos(&v_pos);
859 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
860 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
862 rect = core::rect<s32>(pos.X, pos.Y - m_btn_height,
863 pos.X + geom.X, pos.Y + m_btn_height);
866 if(!data->explicit_size)
867 warningstream<<"invalid use of button without a size[] element"<<std::endl;
869 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
875 258 + m_fields.size()
877 spec.ftype = f_Button;
878 if(type == "button_exit")
881 GUIButton *e = GUIButton::addButton(Environment, rect, this, spec.fid, spec.flabel.c_str());
883 auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
884 e->setFromStyle(style, m_tsrc);
886 if (spec.fname == data->focused_fieldname) {
887 Environment->setFocus(e);
890 m_fields.push_back(spec);
893 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
896 void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
898 std::vector<std::string> parts = split(element,';');
900 if ((parts.size() >= 3 && parts.size() <= 5) ||
901 (parts.size() > 5 && m_formspec_version > FORMSPEC_API_VERSION)) {
902 std::vector<std::string> v_pos = split(parts[0],',');
903 std::vector<std::string> v_geom = split(parts[1],',');
904 std::string name = unescape_string(parts[2]);
906 MY_CHECKPOS("background",0);
907 MY_CHECKGEOM("background",1);
912 if (data->real_coordinates) {
913 pos = getRealCoordinateBasePos(v_pos);
914 geom = getRealCoordinateGeometry(v_geom);
916 pos = getElementBasePos(&v_pos);
917 pos.X -= (spacing.X - (float)imgsize.X) / 2;
918 pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
920 geom.X = stof(v_geom[0]) * spacing.X;
921 geom.Y = stof(v_geom[1]) * spacing.Y;
925 if (parts.size() >= 4 && is_yes(parts[3])) {
926 if (data->real_coordinates) {
927 pos = getRealCoordinateBasePos(v_pos) * -1;
930 pos.X = stoi(v_pos[0]); //acts as offset
931 pos.Y = stoi(v_pos[1]);
936 core::rect<s32> middle;
937 if (parts.size() >= 5) {
938 std::vector<std::string> v_middle = split(parts[4], ',');
939 if (v_middle.size() == 1) {
940 s32 x = stoi(v_middle[0]);
941 middle.UpperLeftCorner = core::vector2di(x, x);
942 middle.LowerRightCorner = core::vector2di(-x, -x);
943 } else if (v_middle.size() == 2) {
944 s32 x = stoi(v_middle[0]);
945 s32 y = stoi(v_middle[1]);
946 middle.UpperLeftCorner = core::vector2di(x, y);
947 middle.LowerRightCorner = core::vector2di(-x, -y);
948 // `-x` is interpreted as `w - x`
949 } else if (v_middle.size() == 4) {
950 middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1]));
951 middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3]));
953 warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl;
957 if (!data->explicit_size && !clip)
958 warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
964 258 + m_fields.size()
967 core::rect<s32> rect;
969 // no auto_clip => position like normal image
970 rect = core::rect<s32>(pos, pos + geom);
972 // it will be auto-clipped when drawing
973 rect = core::rect<s32>(-pos, pos);
976 GUIBackgroundImage *e = new GUIBackgroundImage(Environment, this, spec.fid,
977 rect, name, middle, m_tsrc, clip);
979 FATAL_ERROR_IF(!e, "Failed to create background formspec element");
981 e->setNotClipped(true);
983 e->setVisible(false); // the element is drawn manually before all others
985 m_backgrounds.push_back(e);
986 m_fields.push_back(spec);
989 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
992 void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
994 std::vector<std::string> parts = split(element,';');
996 data->table_options.clear();
997 for (const std::string &part : parts) {
998 // Parse table option
999 std::string opt = unescape_string(part);
1000 data->table_options.push_back(GUITable::splitOption(opt));
1004 void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
1006 std::vector<std::string> parts = split(element,';');
1008 data->table_columns.clear();
1009 for (const std::string &part : parts) {
1010 std::vector<std::string> col_parts = split(part,',');
1011 GUITable::TableColumn column;
1012 // Parse column type
1013 if (!col_parts.empty())
1014 column.type = col_parts[0];
1015 // Parse column options
1016 for (size_t j = 1; j < col_parts.size(); ++j) {
1017 std::string opt = unescape_string(col_parts[j]);
1018 column.options.push_back(GUITable::splitOption(opt));
1020 data->table_columns.push_back(column);
1024 void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
1026 std::vector<std::string> parts = split(element,';');
1028 if (((parts.size() == 4) || (parts.size() == 5)) ||
1029 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1031 std::vector<std::string> v_pos = split(parts[0],',');
1032 std::vector<std::string> v_geom = split(parts[1],',');
1033 std::string name = parts[2];
1034 std::vector<std::string> items = split(parts[3],',');
1035 std::string str_initial_selection;
1036 std::string str_transparent = "false";
1038 if (parts.size() >= 5)
1039 str_initial_selection = parts[4];
1041 MY_CHECKPOS("table",0);
1042 MY_CHECKGEOM("table",1);
1047 if (data->real_coordinates) {
1048 pos = getRealCoordinateBasePos(v_pos);
1049 geom = getRealCoordinateGeometry(v_geom);
1051 pos = getElementBasePos(&v_pos);
1052 geom.X = stof(v_geom[0]) * spacing.X;
1053 geom.Y = stof(v_geom[1]) * spacing.Y;
1056 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1062 258 + m_fields.size()
1065 spec.ftype = f_Table;
1067 for (std::string &item : items) {
1068 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1071 //now really show table
1072 GUITable *e = new GUITable(Environment, this, spec.fid, rect, m_tsrc);
1074 if (spec.fname == data->focused_fieldname) {
1075 Environment->setFocus(e);
1078 e->setTable(data->table_options, data->table_columns, items);
1080 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1081 e->setDynamicData(data->table_dyndata[name]);
1084 if (!str_initial_selection.empty() && str_initial_selection != "0")
1085 e->setSelected(stoi(str_initial_selection));
1087 auto style = getStyleForElement("table", name);
1088 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1090 m_tables.emplace_back(spec, e);
1091 m_fields.push_back(spec);
1094 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
1097 void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
1099 std::vector<std::string> parts = split(element,';');
1101 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
1102 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1104 std::vector<std::string> v_pos = split(parts[0],',');
1105 std::vector<std::string> v_geom = split(parts[1],',');
1106 std::string name = parts[2];
1107 std::vector<std::string> items = split(parts[3],',');
1108 std::string str_initial_selection;
1109 std::string str_transparent = "false";
1111 if (parts.size() >= 5)
1112 str_initial_selection = parts[4];
1114 if (parts.size() >= 6)
1115 str_transparent = parts[5];
1117 MY_CHECKPOS("textlist",0);
1118 MY_CHECKGEOM("textlist",1);
1123 if (data->real_coordinates) {
1124 pos = getRealCoordinateBasePos(v_pos);
1125 geom = getRealCoordinateGeometry(v_geom);
1127 pos = getElementBasePos(&v_pos);
1128 geom.X = stof(v_geom[0]) * spacing.X;
1129 geom.Y = stof(v_geom[1]) * spacing.Y;
1132 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1138 258 + m_fields.size()
1141 spec.ftype = f_Table;
1143 for (std::string &item : items) {
1144 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1147 //now really show list
1148 GUITable *e = new GUITable(Environment, this, spec.fid, rect, m_tsrc);
1150 if (spec.fname == data->focused_fieldname) {
1151 Environment->setFocus(e);
1154 e->setTextList(items, is_yes(str_transparent));
1156 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1157 e->setDynamicData(data->table_dyndata[name]);
1160 if (!str_initial_selection.empty() && str_initial_selection != "0")
1161 e->setSelected(stoi(str_initial_selection));
1163 auto style = getStyleForElement("textlist", name);
1164 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1166 m_tables.emplace_back(spec, e);
1167 m_fields.push_back(spec);
1170 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
1174 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
1176 std::vector<std::string> parts = split(element,';');
1178 if ((parts.size() == 5) ||
1179 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1181 std::vector<std::string> v_pos = split(parts[0],',');
1182 std::string name = parts[2];
1183 std::vector<std::string> items = split(parts[3],',');
1184 std::string str_initial_selection;
1185 str_initial_selection = parts[4];
1187 MY_CHECKPOS("dropdown",0);
1191 core::rect<s32> rect;
1193 if (data->real_coordinates) {
1194 std::vector<std::string> v_geom = split(parts[1],',');
1196 if (v_geom.size() == 1)
1197 v_geom.emplace_back("1");
1199 MY_CHECKGEOM("dropdown",1);
1201 pos = getRealCoordinateBasePos(v_pos);
1202 geom = getRealCoordinateGeometry(v_geom);
1203 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1205 pos = getElementBasePos(&v_pos);
1207 s32 width = stof(parts[1]) * spacing.Y;
1209 rect = core::rect<s32>(pos.X, pos.Y,
1210 pos.X + width, pos.Y + (m_btn_height * 2));
1217 258 + m_fields.size()
1220 spec.ftype = f_DropDown;
1223 //now really show list
1224 gui::IGUIComboBox *e = Environment->addComboBox(rect, this, spec.fid);
1226 if (spec.fname == data->focused_fieldname) {
1227 Environment->setFocus(e);
1230 for (const std::string &item : items) {
1231 e->addItem(unescape_translate(unescape_string(
1232 utf8_to_wide(item))).c_str());
1235 if (!str_initial_selection.empty())
1236 e->setSelected(stoi(str_initial_selection)-1);
1238 auto style = getStyleForElement("dropdown", name);
1239 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1241 m_fields.push_back(spec);
1243 m_dropdowns.emplace_back(spec, std::vector<std::string>());
1244 std::vector<std::string> &values = m_dropdowns.back().second;
1245 for (const std::string &item : items) {
1246 values.push_back(unescape_string(item));
1251 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
1252 << element << "'" << std::endl;
1255 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
1257 std::vector<std::string> parts = split(element,';');
1258 if (parts.size() == 2 ||
1259 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
1260 field_close_on_enter[parts[0]] = is_yes(parts[1]);
1264 void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
1266 std::vector<std::string> parts = split(element,';');
1268 if ((parts.size() == 4) || (parts.size() == 5) ||
1269 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1271 std::vector<std::string> v_pos = split(parts[0],',');
1272 std::vector<std::string> v_geom = split(parts[1],',');
1273 std::string name = parts[2];
1274 std::string label = parts[3];
1276 MY_CHECKPOS("pwdfield",0);
1277 MY_CHECKGEOM("pwdfield",1);
1282 if (data->real_coordinates) {
1283 pos = getRealCoordinateBasePos(v_pos);
1284 geom = getRealCoordinateGeometry(v_geom);
1286 pos = getElementBasePos(&v_pos);
1289 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1291 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1292 pos.Y -= m_btn_height;
1293 geom.Y = m_btn_height*2;
1296 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1298 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1304 258 + m_fields.size()
1308 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
1310 if (spec.fname == data->focused_fieldname) {
1311 Environment->setFocus(e);
1314 if (label.length() >= 1) {
1315 int font_height = g_fontengine->getTextHeight();
1316 rect.UpperLeftCorner.Y -= font_height;
1317 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1318 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1322 e->setPasswordBox(true,L'*');
1324 auto style = getStyleForElement("pwdfield", name, "field");
1325 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1326 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1327 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1330 evt.EventType = EET_KEY_INPUT_EVENT;
1331 evt.KeyInput.Key = KEY_END;
1332 evt.KeyInput.Char = 0;
1333 evt.KeyInput.Control = false;
1334 evt.KeyInput.Shift = false;
1335 evt.KeyInput.PressedDown = true;
1338 if (parts.size() >= 5) {
1339 // TODO: remove after 2016-11-03
1340 warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
1341 " instead of the 5th param" << std::endl;
1342 field_close_on_enter[name] = is_yes(parts[4]);
1345 m_fields.push_back(spec);
1348 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
1351 void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
1352 core::rect<s32> &rect, bool is_multiline)
1354 bool is_editable = !spec.fname.empty();
1355 if (!is_editable && !is_multiline) {
1356 // spec field id to 0, this stops submit searching for a value that isn't there
1357 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1364 } else if (is_multiline &&
1365 spec.fdefault.empty() && !spec.flabel.empty()) {
1366 // Multiline textareas: swap default and label for backwards compat
1367 spec.flabel.swap(spec.fdefault);
1370 gui::IGUIEditBox *e = nullptr;
1371 static constexpr bool use_intl_edit_box = USE_FREETYPE &&
1372 IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9;
1374 if (use_intl_edit_box && g_settings->getBool("freetype")) {
1375 e = new gui::intlGUIEditBox(spec.fdefault.c_str(), true, Environment,
1376 this, spec.fid, rect, is_editable, is_multiline);
1379 e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true,
1380 Environment, this, spec.fid, rect, is_editable, true);
1381 } else if (is_editable) {
1382 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this,
1388 auto style = getStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
1391 if (is_editable && spec.fname == data->focused_fieldname)
1392 Environment->setFocus(e);
1395 e->setMultiLine(true);
1396 e->setWordWrap(true);
1397 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1400 evt.EventType = EET_KEY_INPUT_EVENT;
1401 evt.KeyInput.Key = KEY_END;
1402 evt.KeyInput.Char = 0;
1403 evt.KeyInput.Control = 0;
1404 evt.KeyInput.Shift = 0;
1405 evt.KeyInput.PressedDown = true;
1409 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1410 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1411 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1412 if (style.get(StyleSpec::BGCOLOR, "") == "transparent") {
1413 e->setDrawBackground(false);
1419 if (!spec.flabel.empty()) {
1420 int font_height = g_fontengine->getTextHeight();
1421 rect.UpperLeftCorner.Y -= font_height;
1422 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1423 IGUIElement *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
1424 rect, false, true, this, 0);
1427 t->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1431 void GUIFormSpecMenu::parseSimpleField(parserData *data,
1432 std::vector<std::string> &parts)
1434 std::string name = parts[0];
1435 std::string label = parts[1];
1436 std::string default_val = parts[2];
1438 core::rect<s32> rect;
1440 if (data->explicit_size)
1441 warningstream << "invalid use of unpositioned \"field\" in inventory" << std::endl;
1443 v2s32 pos = getElementBasePos(nullptr);
1444 pos.Y = (data->simple_field_count + 2) * 60;
1445 v2s32 size = DesiredRect.getSize();
1447 rect = core::rect<s32>(
1448 size.X / 2 - 150, pos.Y,
1449 size.X / 2 - 150 + 300, pos.Y + m_btn_height * 2
1454 default_val = m_form_src->resolveText(default_val);
1457 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1462 utf8_to_wide(unescape_string(default_val)),
1463 258 + m_fields.size()
1466 createTextField(data, spec, rect, false);
1468 m_fields.push_back(spec);
1470 data->simple_field_count++;
1473 void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
1474 const std::string &type)
1476 std::vector<std::string> v_pos = split(parts[0],',');
1477 std::vector<std::string> v_geom = split(parts[1],',');
1478 std::string name = parts[2];
1479 std::string label = parts[3];
1480 std::string default_val = parts[4];
1482 MY_CHECKPOS(type,0);
1483 MY_CHECKGEOM(type,1);
1488 if (data->real_coordinates) {
1489 pos = getRealCoordinateBasePos(v_pos);
1490 geom = getRealCoordinateGeometry(v_geom);
1492 pos = getElementBasePos(&v_pos);
1495 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1497 if (type == "textarea")
1499 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1500 pos.Y += m_btn_height;
1504 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1505 pos.Y -= m_btn_height;
1506 geom.Y = m_btn_height*2;
1510 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1512 if(!data->explicit_size)
1513 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1516 default_val = m_form_src->resolveText(default_val);
1519 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1524 utf8_to_wide(unescape_string(default_val)),
1525 258 + m_fields.size()
1528 createTextField(data, spec, rect, type == "textarea");
1530 if (parts.size() >= 6) {
1531 // TODO: remove after 2016-11-03
1532 warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
1533 " instead of the 6th param" << std::endl;
1534 field_close_on_enter[name] = is_yes(parts[5]);
1537 m_fields.push_back(spec);
1540 void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
1541 const std::string &type)
1543 std::vector<std::string> parts = split(element,';');
1545 if (parts.size() == 3 || parts.size() == 4) {
1546 parseSimpleField(data,parts);
1550 if ((parts.size() == 5) || (parts.size() == 6) ||
1551 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1553 parseTextArea(data,parts,type);
1556 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1559 void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &element)
1561 std::vector<std::string> parts = split(element, ';');
1563 if (parts.size() != 4 && m_formspec_version < FORMSPEC_API_VERSION) {
1564 errorstream << "Invalid text element(" << parts.size() << "): '" << element << "'" << std::endl;
1568 std::vector<std::string> v_pos = split(parts[0], ',');
1569 std::vector<std::string> v_geom = split(parts[1], ',');
1570 std::string name = parts[2];
1571 std::string text = parts[3];
1573 MY_CHECKPOS("hypertext", 0);
1574 MY_CHECKGEOM("hypertext", 1);
1579 if (data->real_coordinates) {
1580 pos = getRealCoordinateBasePos(v_pos);
1581 geom = getRealCoordinateGeometry(v_geom);
1583 pos = getElementBasePos(&v_pos);
1586 pos.X += stof(v_pos[0]) * spacing.X;
1587 pos.Y += stof(v_pos[1]) * spacing.Y + (m_btn_height * 2);
1589 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1590 geom.Y = (stof(v_geom[1]) * imgsize.Y) - (spacing.Y - imgsize.Y);
1593 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X + geom.X, pos.Y + geom.Y);
1596 text = m_form_src->resolveText(text);
1600 utf8_to_wide(unescape_string(text)),
1602 258 + m_fields.size()
1605 spec.ftype = f_Unknown;
1607 spec.flabel.c_str(), Environment, this, spec.fid, rect, m_client, m_tsrc);
1609 m_fields.push_back(spec);
1612 void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
1614 std::vector<std::string> parts = split(element,';');
1616 if ((parts.size() == 2) ||
1617 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1619 std::vector<std::string> v_pos = split(parts[0],',');
1620 std::string text = parts[1];
1622 MY_CHECKPOS("label",0);
1624 if(!data->explicit_size)
1625 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1627 std::vector<std::string> lines = split(text, '\n');
1629 for (unsigned int i = 0; i != lines.size(); i++) {
1630 std::wstring wlabel_colors = translate_string(
1631 utf8_to_wide(unescape_string(lines[i])));
1632 // Without color escapes to get the font dimensions
1633 std::wstring wlabel_plain = unescape_enriched(wlabel_colors);
1635 core::rect<s32> rect;
1637 if (data->real_coordinates) {
1638 // Lines are spaced at the distance of 1/2 imgsize.
1639 // This alows lines that line up with the new elements
1640 // easily without sacrificing good line distance. If
1641 // it was one whole imgsize, it would have too much
1643 v2s32 pos = getRealCoordinateBasePos(v_pos);
1645 // Labels are positioned by their center, not their top.
1646 pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2);
1648 rect = core::rect<s32>(
1650 pos.X + m_font->getDimension(wlabel_plain.c_str()).Width,
1654 // Lines are spaced at the nominal distance of
1655 // 2/5 inventory slot, even if the font doesn't
1656 // quite match that. This provides consistent
1657 // form layout, at the expense of sometimes
1658 // having sub-optimal spacing for the font.
1659 // We multiply by 2 and then divide by 5, rather
1660 // than multiply by 0.4, to get exact results
1661 // in the integer cases: 0.4 is not exactly
1662 // representable in binary floating point.
1664 v2s32 pos = getElementBasePos(nullptr);
1665 pos.X += stof(v_pos[0]) * spacing.X;
1666 pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
1668 pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0;
1670 rect = core::rect<s32>(
1671 pos.X, pos.Y - m_btn_height,
1672 pos.X + m_font->getDimension(wlabel_plain.c_str()).Width,
1673 pos.Y + m_btn_height);
1680 258 + m_fields.size(),
1683 gui::IGUIStaticText *e = gui::StaticText::add(Environment,
1684 spec.flabel.c_str(), rect, false, false, this, spec.fid);
1685 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
1687 auto style = getStyleForElement("label", spec.fname);
1688 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1689 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1691 m_fields.push_back(spec);
1696 errorstream << "Invalid label element(" << parts.size() << "): '" << element
1697 << "'" << std::endl;
1700 void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
1702 std::vector<std::string> parts = split(element,';');
1704 if ((parts.size() == 2) ||
1705 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1707 std::vector<std::string> v_pos = split(parts[0],',');
1708 std::wstring text = unescape_translate(
1709 unescape_string(utf8_to_wide(parts[1])));
1711 MY_CHECKPOS("vertlabel",1);
1714 core::rect<s32> rect;
1716 if (data->real_coordinates) {
1717 pos = getRealCoordinateBasePos(v_pos);
1719 // Vertlabels are positioned by center, not left.
1720 pos.X -= imgsize.X / 2;
1722 // We use text.length + 1 because without it, the rect
1723 // isn't quite tall enough and cuts off the text.
1724 rect = core::rect<s32>(pos.X, pos.Y,
1726 pos.Y + font_line_height(m_font) *
1727 (text.length() + 1));
1730 pos = getElementBasePos(&v_pos);
1732 // As above, the length must be one longer. The width of
1733 // the rect (15 pixels) seems rather arbitrary, but
1734 // changing it might break something.
1735 rect = core::rect<s32>(
1736 pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
1738 font_line_height(m_font) *
1739 (text.length() + 1) +
1740 ((imgsize.Y/2) - m_btn_height));
1743 if(!data->explicit_size)
1744 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1748 for (wchar_t i : text) {
1757 258 + m_fields.size()
1759 gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
1760 rect, false, false, this, spec.fid);
1761 e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1763 auto style = getStyleForElement("vertlabel", spec.fname, "label");
1764 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1765 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1767 m_fields.push_back(spec);
1770 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1773 void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
1774 const std::string &type)
1776 std::vector<std::string> parts = split(element,';');
1778 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1779 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1781 std::vector<std::string> v_pos = split(parts[0],',');
1782 std::vector<std::string> v_geom = split(parts[1],',');
1783 std::string image_name = parts[2];
1784 std::string name = parts[3];
1785 std::string label = parts[4];
1787 MY_CHECKPOS("imagebutton",0);
1788 MY_CHECKGEOM("imagebutton",1);
1790 bool noclip = false;
1791 bool drawborder = true;
1792 std::string pressed_image_name;
1794 if (parts.size() >= 7) {
1795 if (parts[5] == "true")
1797 if (parts[6] == "false")
1801 if (parts.size() >= 8) {
1802 pressed_image_name = parts[7];
1808 if (data->real_coordinates) {
1809 pos = getRealCoordinateBasePos(v_pos);
1810 geom = getRealCoordinateGeometry(v_geom);
1812 pos = getElementBasePos(&v_pos);
1813 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1814 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1817 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1820 if (!data->explicit_size)
1821 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1823 image_name = unescape_string(image_name);
1824 pressed_image_name = unescape_string(pressed_image_name);
1826 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1831 utf8_to_wide(image_name),
1832 258 + m_fields.size()
1834 spec.ftype = f_Button;
1835 if (type == "image_button_exit")
1836 spec.is_exit = true;
1838 video::ITexture *texture = 0;
1839 texture = m_tsrc->getTexture(image_name);
1841 GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, this, spec.fid, spec.flabel.c_str());
1843 if (spec.fname == data->focused_fieldname) {
1844 Environment->setFocus(e);
1847 auto style = getStyleForElement("image_button", spec.fname);
1848 e->setFromStyle(style, m_tsrc);
1850 // We explicitly handle these arguments *after* the style properties in
1851 // order to override them if they are provided
1852 e->setForegroundImage(guiScalingImageButton(
1853 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1854 if (!pressed_image_name.empty()) {
1855 video::ITexture *pressed_texture = m_tsrc->getTexture(pressed_image_name);
1856 e->setPressedForegroundImage(guiScalingImageButton(
1857 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1859 e->setScaleImage(true);
1861 if (parts.size() >= 7) {
1862 e->setNotClipped(noclip);
1863 e->setDrawBorder(drawborder);
1866 m_fields.push_back(spec);
1870 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1873 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
1875 std::vector<std::string> parts = split(element, ';');
1877 if (((parts.size() == 4) || (parts.size() == 6)) || (parts.size() == 7 &&
1878 data->real_coordinates) || ((parts.size() > 6) &&
1879 (m_formspec_version > FORMSPEC_API_VERSION)))
1881 std::vector<std::string> v_pos = split(parts[0],',');
1883 // If we're using real coordinates, add an extra field for height.
1884 // Width is not here because tabs are the width of the text, and
1885 // there's no reason to change that.
1887 std::vector<std::string> v_geom = {"1", "0.75"}; // Dummy width and default height
1888 bool auto_width = true;
1889 if (parts.size() == 7) {
1892 v_geom = split(parts[1], ',');
1893 if (v_geom.size() == 1)
1894 v_geom.insert(v_geom.begin(), "1"); // Dummy value
1899 std::string name = parts[i+1];
1900 std::vector<std::string> buttons = split(parts[i+2], ',');
1901 std::string str_index = parts[i+3];
1902 bool show_background = true;
1903 bool show_border = true;
1904 int tab_index = stoi(str_index) - 1;
1906 MY_CHECKPOS("tabheader", 0);
1908 if (parts.size() == 6 + i) {
1909 if (parts[4+i] == "true")
1910 show_background = false;
1911 if (parts[5+i] == "false")
1912 show_border = false;
1919 258 + m_fields.size()
1922 spec.ftype = f_TabHeader;
1927 if (data->real_coordinates) {
1928 pos = getRealCoordinateBasePos(v_pos);
1930 geom = getRealCoordinateGeometry(v_geom);
1931 pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
1933 geom.X = DesiredRect.getWidth(); // Set automatic width
1935 MY_CHECKGEOM("tabheader", 1);
1937 v2f32 pos_f = pos_offset * spacing;
1938 pos_f.X += stof(v_pos[0]) * spacing.X;
1939 pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
1940 pos = v2s32(pos_f.X, pos_f.Y);
1942 geom.Y = m_btn_height * 2;
1943 geom.X = DesiredRect.getWidth();
1946 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1949 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1950 show_background, show_border, spec.fid);
1951 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1952 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1953 e->setTabHeight(geom.Y);
1955 if (spec.fname == data->focused_fieldname) {
1956 Environment->setFocus(e);
1959 auto style = getStyleForElement("tabheader", name);
1960 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
1962 for (const std::string &button : buttons) {
1963 auto tab = e->addTab(unescape_translate(unescape_string(
1964 utf8_to_wide(button))).c_str(), -1);
1965 if (style.isNotDefault(StyleSpec::BGCOLOR))
1966 tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
1968 tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1971 if ((tab_index >= 0) &&
1972 (buttons.size() < INT_MAX) &&
1973 (tab_index < (int) buttons.size()))
1974 e->setActiveTab(tab_index);
1976 m_fields.push_back(spec);
1979 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1980 << element << "'" << std::endl;
1983 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
1986 if (m_client == 0) {
1987 warningstream << "invalid use of item_image_button with m_client==0"
1992 std::vector<std::string> parts = split(element,';');
1994 if ((parts.size() == 5) ||
1995 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1997 std::vector<std::string> v_pos = split(parts[0],',');
1998 std::vector<std::string> v_geom = split(parts[1],',');
1999 std::string item_name = parts[2];
2000 std::string name = parts[3];
2001 std::string label = parts[4];
2003 label = unescape_string(label);
2004 item_name = unescape_string(item_name);
2006 MY_CHECKPOS("itemimagebutton",0);
2007 MY_CHECKGEOM("itemimagebutton",1);
2012 if (data->real_coordinates) {
2013 pos = getRealCoordinateBasePos(v_pos);
2014 geom = getRealCoordinateGeometry(v_geom);
2016 pos = getElementBasePos(&v_pos);
2017 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
2018 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
2021 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
2023 if(!data->explicit_size)
2024 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
2026 IItemDefManager *idef = m_client->idef();
2028 item.deSerialize(item_name, idef);
2031 TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
2032 m_default_tooltip_bgcolor,
2033 m_default_tooltip_color);
2035 // the spec for the button
2038 utf8_to_wide(label),
2039 utf8_to_wide(item_name),
2040 258 + m_fields.size(),
2044 GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment, rect, this, spec_btn.fid, spec_btn.flabel.c_str(), item_name, m_client);
2046 auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
2047 e_btn->setFromStyle(style, m_tsrc);
2049 if (spec_btn.fname == data->focused_fieldname) {
2050 Environment->setFocus(e_btn);
2053 spec_btn.ftype = f_Button;
2054 rect += data->basepos-padding;
2055 spec_btn.rect = rect;
2056 m_fields.push_back(spec_btn);
2059 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
2062 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
2064 std::vector<std::string> parts = split(element,';');
2066 if ((parts.size() == 3) ||
2067 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
2069 std::vector<std::string> v_pos = split(parts[0],',');
2070 std::vector<std::string> v_geom = split(parts[1],',');
2072 MY_CHECKPOS("box",0);
2073 MY_CHECKGEOM("box",1);
2078 if (data->real_coordinates) {
2079 pos = getRealCoordinateBasePos(v_pos);
2080 geom = getRealCoordinateGeometry(v_geom);
2082 pos = getElementBasePos(&v_pos);
2083 geom.X = stof(v_geom[0]) * spacing.X;
2084 geom.Y = stof(v_geom[1]) * spacing.Y;
2087 video::SColor tmp_color;
2089 if (parseColorString(parts[2], tmp_color, false, 0x8C)) {
2094 258 + m_fields.size(),
2099 core::rect<s32> rect(pos, pos + geom);
2101 GUIBox *e = new GUIBox(Environment, this, spec.fid, rect, tmp_color);
2103 e->setNotClipped(true);
2107 m_fields.push_back(spec);
2110 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
2114 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
2117 void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
2119 std::vector<std::string> parts = split(element,';');
2120 const u32 parameter_count = parts.size();
2122 if ((parameter_count > 2 && m_formspec_version < 3) ||
2123 (parameter_count > 3 && m_formspec_version <= FORMSPEC_API_VERSION)) {
2124 errorstream << "Invalid bgcolor element(" << parameter_count << "): '"
2125 << element << "'" << std::endl;
2130 if (parameter_count >= 1 && parts[0] != "")
2131 parseColorString(parts[0], m_bgcolor, false);
2134 if (parameter_count >= 2) {
2135 if (parts[1] == "both") {
2136 m_bgnonfullscreen = true;
2137 m_bgfullscreen = true;
2138 } else if (parts[1] == "neither") {
2139 m_bgnonfullscreen = false;
2140 m_bgfullscreen = false;
2141 } else if (parts[1] != "" || m_formspec_version < 3) {
2142 m_bgfullscreen = is_yes(parts[1]);
2143 m_bgnonfullscreen = !m_bgfullscreen;
2148 if (parameter_count >= 3 && parts[2] != "")
2149 parseColorString(parts[2], m_fullscreen_bgcolor, false);
2152 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
2154 std::vector<std::string> parts = split(element,';');
2156 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
2157 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2159 parseColorString(parts[0], m_slotbg_n, false);
2160 parseColorString(parts[1], m_slotbg_h, false);
2162 if (parts.size() >= 3) {
2163 if (parseColorString(parts[2], m_slotbordercolor, false)) {
2164 m_slotborder = true;
2167 if (parts.size() == 5) {
2168 video::SColor tmp_color;
2170 if (parseColorString(parts[3], tmp_color, false))
2171 m_default_tooltip_bgcolor = tmp_color;
2172 if (parseColorString(parts[4], tmp_color, false))
2173 m_default_tooltip_color = tmp_color;
2177 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
2180 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
2182 std::vector<std::string> parts = split(element,';');
2183 if (parts.size() < 2) {
2184 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2185 << element << "'" << std::endl;
2189 // Get mode and check size
2190 bool rect_mode = parts[0].find(',') != std::string::npos;
2191 size_t base_size = rect_mode ? 3 : 2;
2192 if (parts.size() != base_size && parts.size() != base_size + 2) {
2193 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2194 << element << "'" << std::endl;
2199 video::SColor bgcolor = m_default_tooltip_bgcolor;
2200 video::SColor color = m_default_tooltip_color;
2201 if (parts.size() == base_size + 2 &&
2202 (!parseColorString(parts[base_size], bgcolor, false) ||
2203 !parseColorString(parts[base_size + 1], color, false))) {
2204 errorstream << "Invalid color in tooltip element(" << parts.size()
2205 << "): '" << element << "'" << std::endl;
2209 // Make tooltip spec
2210 std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
2211 TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
2215 std::vector<std::string> v_pos = split(parts[0], ',');
2216 std::vector<std::string> v_geom = split(parts[1], ',');
2218 MY_CHECKPOS("tooltip", 0);
2219 MY_CHECKGEOM("tooltip", 1);
2224 if (data->real_coordinates) {
2225 pos = getRealCoordinateBasePos(v_pos);
2226 geom = getRealCoordinateGeometry(v_geom);
2228 pos = getElementBasePos(&v_pos);
2229 geom.X = stof(v_geom[0]) * spacing.X;
2230 geom.Y = stof(v_geom[1]) * spacing.Y;
2233 FieldSpec fieldspec(
2237 258 + m_fields.size()
2240 core::rect<s32> rect(pos, pos + geom);
2242 gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
2243 this, fieldspec.fid, rect);
2245 m_fields.push_back(fieldspec);
2246 m_tooltip_rects.emplace_back(e, spec);
2249 m_tooltips[parts[0]] = spec;
2253 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
2259 std::vector<std::string> parts = split(data,'[');
2261 if (parts.size() < 2) {
2265 if (parts[0] != "formspec_version") {
2269 if (is_number(parts[1])) {
2270 m_formspec_version = mystoi(parts[1]);
2277 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
2279 if (element.empty())
2282 std::vector<std::string> parts = split(element,'[');
2284 if (parts.size() < 2)
2287 std::string type = trim(parts[0]);
2288 std::string description = trim(parts[1]);
2290 if (type != "size" && type != "invsize")
2293 if (type == "invsize")
2294 log_deprecated("Deprecated formspec element \"invsize\" is used");
2296 parseSize(data, description);
2301 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
2303 if (element.empty())
2306 std::vector<std::string> parts = split(element, '[');
2308 if (parts.size() != 2)
2311 std::string type = trim(parts[0]);
2312 std::string description = trim(parts[1]);
2314 if (type != "position")
2317 parsePosition(data, description);
2322 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
2324 std::vector<std::string> parts = split(element, ',');
2326 if (parts.size() == 2) {
2327 data->offset.X = stof(parts[0]);
2328 data->offset.Y = stof(parts[1]);
2332 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
2335 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
2337 if (element.empty())
2340 std::vector<std::string> parts = split(element, '[');
2342 if (parts.size() != 2)
2345 std::string type = trim(parts[0]);
2346 std::string description = trim(parts[1]);
2348 if (type != "anchor")
2351 parseAnchor(data, description);
2356 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
2358 std::vector<std::string> parts = split(element, ',');
2360 if (parts.size() == 2) {
2361 data->anchor.X = stof(parts[0]);
2362 data->anchor.Y = stof(parts[1]);
2366 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
2367 << "'" << std::endl;
2370 bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type)
2372 std::vector<std::string> parts = split(element, ';');
2374 if (parts.size() < 2) {
2375 errorstream << "Invalid style element (" << parts.size() << "): '" << element
2376 << "'" << std::endl;
2380 std::string selector = trim(parts[0]);
2381 if (selector.empty()) {
2382 errorstream << "Invalid style element (Selector required): '" << element
2383 << "'" << std::endl;
2389 for (size_t i = 1; i < parts.size(); i++) {
2390 size_t equal_pos = parts[i].find('=');
2391 if (equal_pos == std::string::npos) {
2392 errorstream << "Invalid style element (Property missing value): '" << element
2393 << "'" << std::endl;
2397 std::string propname = trim(parts[i].substr(0, equal_pos));
2398 std::string value = trim(unescape_string(parts[i].substr(equal_pos + 1)));
2400 std::transform(propname.begin(), propname.end(), propname.begin(), ::tolower);
2402 StyleSpec::Property prop = StyleSpec::GetPropertyByName(propname);
2403 if (prop == StyleSpec::NONE) {
2404 if (property_warned.find(propname) != property_warned.end()) {
2405 warningstream << "Invalid style element (Unknown property " << propname << "): '"
2407 << "'" << std::endl;
2408 property_warned.insert(propname);
2413 spec.set(prop, value);
2417 theme_by_type[selector] |= spec;
2419 theme_by_name[selector] |= spec;
2425 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
2428 if (element.empty())
2431 if (parseVersionDirect(element))
2434 std::vector<std::string> parts = split(element,'[');
2436 // ugly workaround to keep compatibility
2437 if (parts.size() > 2) {
2438 if (trim(parts[0]) == "image") {
2439 for (unsigned int i=2;i< parts.size(); i++) {
2440 parts[1] += "[" + parts[i];
2446 if (parts.size() < 2) {
2450 std::string type = trim(parts[0]);
2451 std::string description = trim(parts[1]);
2453 if (type == "container") {
2454 parseContainer(data, description);
2458 if (type == "container_end") {
2459 parseContainerEnd(data);
2463 if (type == "list") {
2464 parseList(data, description);
2468 if (type == "listring") {
2469 parseListRing(data, description);
2473 if (type == "checkbox") {
2474 parseCheckbox(data, description);
2478 if (type == "image") {
2479 parseImage(data, description);
2483 if (type == "item_image") {
2484 parseItemImage(data, description);
2488 if (type == "button" || type == "button_exit") {
2489 parseButton(data, description, type);
2493 if (type == "background" || type == "background9") {
2494 parseBackground(data, description);
2498 if (type == "tableoptions"){
2499 parseTableOptions(data,description);
2503 if (type == "tablecolumns"){
2504 parseTableColumns(data,description);
2508 if (type == "table"){
2509 parseTable(data,description);
2513 if (type == "textlist"){
2514 parseTextList(data,description);
2518 if (type == "dropdown"){
2519 parseDropDown(data,description);
2523 if (type == "field_close_on_enter") {
2524 parseFieldCloseOnEnter(data, description);
2528 if (type == "pwdfield") {
2529 parsePwdField(data,description);
2533 if ((type == "field") || (type == "textarea")){
2534 parseField(data,description,type);
2538 if (type == "hypertext") {
2539 parseHyperText(data,description);
2543 if (type == "label") {
2544 parseLabel(data,description);
2548 if (type == "vertlabel") {
2549 parseVertLabel(data,description);
2553 if (type == "item_image_button") {
2554 parseItemImageButton(data,description);
2558 if ((type == "image_button") || (type == "image_button_exit")) {
2559 parseImageButton(data,description,type);
2563 if (type == "tabheader") {
2564 parseTabHeader(data,description);
2568 if (type == "box") {
2569 parseBox(data,description);
2573 if (type == "bgcolor") {
2574 parseBackgroundColor(data,description);
2578 if (type == "listcolors") {
2579 parseListColors(data,description);
2583 if (type == "tooltip") {
2584 parseTooltip(data,description);
2588 if (type == "scrollbar") {
2589 parseScrollBar(data, description);
2593 if (type == "real_coordinates") {
2594 data->real_coordinates = is_yes(description);
2598 if (type == "style") {
2599 parseStyle(data, description, false);
2603 if (type == "style_type") {
2604 parseStyle(data, description, true);
2608 if (type == "scrollbaroptions") {
2609 parseScrollBarOptions(data, description);
2614 infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
2618 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
2620 /* useless to regenerate without a screensize */
2621 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
2628 for (auto &m_table : m_tables) {
2629 std::string tablename = m_table.first.fname;
2630 GUITable *table = m_table.second;
2631 mydata.table_dyndata[tablename] = table->getDynamicData();
2635 if (!m_focused_element.empty())
2636 mydata.focused_fieldname = m_focused_element;
2639 gui::IGUIElement *focused_element = Environment->getFocus();
2640 if (focused_element && focused_element->getParent() == this) {
2641 s32 focused_id = focused_element->getID();
2642 if (focused_id > 257) {
2643 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
2644 if (field.fid == focused_id) {
2645 mydata.focused_fieldname = field.fname;
2655 for (auto &table_it : m_tables)
2656 table_it.second->drop();
2657 for (auto &inventorylist_it : m_inventorylists)
2658 inventorylist_it.e->drop();
2659 for (auto &checkbox_it : m_checkboxes)
2660 checkbox_it.second->drop();
2661 for (auto &scrollbar_it : m_scrollbars)
2662 scrollbar_it.second->drop();
2663 for (auto &background_it : m_backgrounds)
2664 background_it->drop();
2665 for (auto &tooltip_rect_it : m_tooltip_rects)
2666 tooltip_rect_it.first->drop();
2668 mydata.size= v2s32(100,100);
2669 mydata.screensize = screensize;
2670 mydata.offset = v2f32(0.5f, 0.5f);
2671 mydata.anchor = v2f32(0.5f, 0.5f);
2672 mydata.simple_field_count = 0;
2674 // Base position of contents of form
2675 mydata.basepos = getBasePos();
2677 /* Convert m_init_draw_spec to m_inventorylists */
2679 m_inventorylists.clear();
2680 m_backgrounds.clear();
2682 m_checkboxes.clear();
2683 m_scrollbars.clear();
2686 m_tooltip_rects.clear();
2687 m_inventory_rings.clear();
2688 m_dropdowns.clear();
2689 theme_by_name.clear();
2690 theme_by_type.clear();
2692 m_bgnonfullscreen = true;
2693 m_bgfullscreen = false;
2695 m_formspec_version = 1;
2698 v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
2699 m_bgcolor = video::SColor(
2700 (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
2701 clamp_u8(myround(formspec_bgcolor.X)),
2702 clamp_u8(myround(formspec_bgcolor.Y)),
2703 clamp_u8(myround(formspec_bgcolor.Z))
2708 v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
2709 m_fullscreen_bgcolor = video::SColor(
2710 (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
2711 clamp_u8(myround(formspec_bgcolor.X)),
2712 clamp_u8(myround(formspec_bgcolor.Y)),
2713 clamp_u8(myround(formspec_bgcolor.Z))
2717 m_slotbg_n = video::SColor(255,128,128,128);
2718 m_slotbg_h = video::SColor(255,192,192,192);
2720 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
2721 m_default_tooltip_color = video::SColor(255,255,255,255);
2723 m_slotbordercolor = video::SColor(200,0,0,0);
2724 m_slotborder = false;
2728 assert(!m_tooltip_element);
2729 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
2730 m_tooltip_element = gui::StaticText::add(Environment, L"",
2731 core::rect<s32>(0, 0, 110, 18));
2732 m_tooltip_element->enableOverrideColor(true);
2733 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2734 m_tooltip_element->setDrawBackground(true);
2735 m_tooltip_element->setDrawBorder(true);
2736 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2737 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
2738 m_tooltip_element->setWordWrap(false);
2739 //we're not parent so no autograb for this one!
2740 m_tooltip_element->grab();
2743 std::vector<std::string> elements = split(m_formspec_string,']');
2746 /* try to read version from first element only */
2747 if (!elements.empty()) {
2748 if (parseVersionDirect(elements[0])) {
2753 /* we need size first in order to calculate image scale */
2754 mydata.explicit_size = false;
2755 for (; i< elements.size(); i++) {
2756 if (!parseSizeDirect(&mydata, elements[i])) {
2761 /* "position" element is always after "size" element if it used */
2762 for (; i< elements.size(); i++) {
2763 if (!parsePositionDirect(&mydata, elements[i])) {
2768 /* "anchor" element is always after "position" (or "size" element) if it used */
2769 for (; i< elements.size(); i++) {
2770 if (!parseAnchorDirect(&mydata, elements[i])) {
2775 /* "no_prepend" element is always after "position" (or "size" element) if it used */
2776 bool enable_prepends = true;
2777 for (; i < elements.size(); i++) {
2778 if (elements[i].empty())
2781 std::vector<std::string> parts = split(elements[i], '[');
2782 if (trim(parts[0]) == "no_prepend")
2783 enable_prepends = false;
2788 /* Copy of the "real_coordinates" element for after the form size. */
2789 mydata.real_coordinates = m_formspec_version >= 2;
2790 for (; i < elements.size(); i++) {
2791 std::vector<std::string> parts = split(elements[i], '[');
2792 std::string name = trim(parts[0]);
2793 if (name != "real_coordinates" || parts.size() != 2)
2794 break; // Invalid format
2796 mydata.real_coordinates = is_yes(trim(parts[1]));
2799 if (mydata.explicit_size) {
2800 // compute scaling for specified form size
2802 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
2803 v2u32 delta = current_screensize - m_lockscreensize;
2805 if (current_screensize.Y > m_lockscreensize.Y)
2810 if (current_screensize.X > m_lockscreensize.X)
2815 offset = v2s32(delta.X,delta.Y);
2817 mydata.screensize = m_lockscreensize;
2819 offset = v2s32(0,0);
2822 double gui_scaling = g_settings->getFloat("gui_scaling");
2823 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2827 // In fixed-size mode, inventory image size
2828 // is 0.53 inch multiplied by the gui_scaling
2829 // config parameter. This magic size is chosen
2830 // to make the main menu (15.5 inventory images
2831 // wide, including border) just fit into the
2832 // default window (800 pixels wide) at 96 DPI
2833 // and default scaling (1.00).
2834 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
2836 // In variable-size mode, we prefer to make the
2837 // inventory image size 1/15 of screen height,
2838 // multiplied by the gui_scaling config parameter.
2839 // If the preferred size won't fit the whole
2840 // form on the screen, either horizontally or
2841 // vertically, then we scale it down to fit.
2842 // (The magic numbers in the computation of what
2843 // fits arise from the scaling factors in the
2844 // following stanza, including the form border,
2845 // help text space, and 0.1 inventory slot spare.)
2846 // However, a minimum size is also set, that
2847 // the image size can't be less than 0.3 inch
2848 // multiplied by gui_scaling, even if this means
2849 // the form doesn't fit the screen.
2851 // For mobile devices these magic numbers are
2852 // different and forms should always use the
2853 // maximum screen space available.
2854 double prefer_imgsize = mydata.screensize.Y / 10 * gui_scaling;
2855 double fitx_imgsize = mydata.screensize.X /
2856 ((12.0 / 8.0) * (0.5 + mydata.invsize.X));
2857 double fity_imgsize = mydata.screensize.Y /
2858 ((15.0 / 11.0) * (0.85 + mydata.invsize.Y));
2859 use_imgsize = MYMIN(prefer_imgsize,
2860 MYMIN(fitx_imgsize, fity_imgsize));
2862 double prefer_imgsize = mydata.screensize.Y / 15 * gui_scaling;
2863 double fitx_imgsize = mydata.screensize.X /
2864 ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
2865 double fity_imgsize = mydata.screensize.Y /
2866 ((15.0 / 13.0) * (0.85 * mydata.invsize.Y));
2867 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2868 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
2869 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2870 MYMIN(fitx_imgsize, fity_imgsize)));
2874 // Everything else is scaled in proportion to the
2875 // inventory image size. The inventory slot spacing
2876 // is 5/4 image size horizontally and 15/13 image size
2877 // vertically. The padding around the form (incorporating
2878 // the border of the outer inventory slots) is 3/8
2879 // image size. Font height (baseline to baseline)
2880 // is 2/5 vertical inventory slot spacing, and button
2881 // half-height is 7/8 of font height.
2882 imgsize = v2s32(use_imgsize, use_imgsize);
2883 spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2884 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2885 m_btn_height = use_imgsize*15.0/13 * 0.35;
2887 m_font = g_fontengine->getFont();
2889 if (mydata.real_coordinates) {
2890 mydata.size = v2s32(
2891 mydata.invsize.X*imgsize.X,
2892 mydata.invsize.Y*imgsize.Y
2895 mydata.size = v2s32(
2896 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2897 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2901 DesiredRect = mydata.rect = core::rect<s32>(
2902 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
2903 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
2904 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
2905 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
2908 // Non-size[] form must consist only of text fields and
2909 // implicit "Proceed" button. Use default font, and
2910 // temporary form size which will be recalculated below.
2911 m_font = g_fontengine->getFont();
2912 m_btn_height = font_line_height(m_font) * 0.875;
2913 DesiredRect = core::rect<s32>(
2914 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
2915 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
2916 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
2917 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
2920 recalculateAbsolutePosition(false);
2921 mydata.basepos = getBasePos();
2922 m_tooltip_element->setOverrideFont(m_font);
2924 gui::IGUISkin *skin = Environment->getSkin();
2926 gui::IGUIFont *old_font = skin->getFont();
2927 skin->setFont(m_font);
2929 pos_offset = v2f32();
2931 // used for formspec versions < 3
2932 core::list<IGUIElement *>::Iterator legacy_sort_start = Children.getLast();
2934 if (enable_prepends) {
2935 // Backup the coordinates so that prepends can use the coordinates of choice.
2936 bool rc_backup = mydata.real_coordinates;
2937 u16 version_backup = m_formspec_version;
2938 mydata.real_coordinates = false; // Old coordinates by default.
2940 std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
2941 for (const auto &element : prepend_elements)
2942 parseElement(&mydata, element);
2944 // legacy sorting for formspec versions < 3
2945 if (m_formspec_version >= 3)
2946 // prepends do not need to be reordered
2947 legacy_sort_start = Children.getLast();
2948 else if (version_backup >= 3)
2949 // only prepends elements have to be reordered
2950 legacySortElements(legacy_sort_start);
2952 m_formspec_version = version_backup;
2953 mydata.real_coordinates = rc_backup; // Restore coordinates
2956 for (; i< elements.size(); i++) {
2957 parseElement(&mydata, elements[i]);
2960 if (!container_stack.empty()) {
2961 errorstream << "Invalid formspec string: container was never closed!"
2965 // If there are fields without explicit size[], add a "Proceed"
2966 // button and adjust size to fit all the fields.
2967 if (mydata.simple_field_count > 0 && !mydata.explicit_size) {
2968 mydata.rect = core::rect<s32>(
2969 mydata.screensize.X / 2 - 580 / 2,
2970 mydata.screensize.Y / 2 - 300 / 2,
2971 mydata.screensize.X / 2 + 580 / 2,
2972 mydata.screensize.Y / 2 + 240 / 2 + mydata.simple_field_count * 60
2975 DesiredRect = mydata.rect;
2976 recalculateAbsolutePosition(false);
2977 mydata.basepos = getBasePos();
2980 v2s32 pos = mydata.basepos;
2981 pos.Y = (mydata.simple_field_count + 2) * 60;
2983 v2s32 size = DesiredRect.getSize();
2984 mydata.rect = core::rect<s32>(
2985 size.X / 2 - 70, pos.Y,
2986 size.X / 2 - 70 + 140, pos.Y + m_btn_height * 2
2988 const wchar_t *text = wgettext("Proceed");
2989 GUIButton::addButton(Environment, mydata.rect, this, 257, text);
2994 //set initial focus if parser didn't set it
2995 focused_element = Environment->getFocus();
2996 if (!focused_element
2997 || !isMyChild(focused_element)
2998 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
3001 skin->setFont(old_font);
3004 if (m_formspec_version < 3)
3005 legacySortElements(legacy_sort_start);
3008 void GUIFormSpecMenu::legacySortElements(core::list<IGUIElement *>::Iterator from)
3011 Draw order for formspec_version <= 2:
3015 0 All other elements
3017 2 item_image, item_image_button
3022 if (from == Children.end())
3023 from = Children.begin();
3027 core::list<IGUIElement *>::Iterator to = Children.end();
3028 // 1: Copy into a sortable container
3029 std::vector<IGUIElement *> elements;
3030 for (auto it = from; it != to; ++it)
3031 elements.emplace_back(*it);
3033 // 2: Sort the container
3034 std::sort(elements.begin(), elements.end(),
3035 [this] (const IGUIElement *a, const IGUIElement *b) -> bool {
3036 const FieldSpec *spec_a = getSpecByID(a->getID());
3037 const FieldSpec *spec_b = getSpecByID(b->getID());
3038 return spec_a && spec_b &&
3039 spec_a->priority < spec_b->priority;
3042 // 3: Re-assign the pointers
3043 for (auto e : elements) {
3050 bool GUIFormSpecMenu::getAndroidUIInput()
3052 if (!hasAndroidUIInput())
3055 std::string fieldname = m_jni_field_name;
3056 m_jni_field_name.clear();
3058 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3059 iter != m_fields.end(); ++iter) {
3061 if (iter->fname != fieldname) {
3064 IGUIElement *tochange = getElementFromId(iter->fid, true);
3066 if (tochange == 0) {
3070 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
3074 std::string text = porting::getInputDialogValue();
3076 ((gui::IGUIEditBox *)tochange)->setText(utf8_to_wide(text).c_str());
3082 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
3084 core::rect<s32> imgrect(0, 0, imgsize.X, imgsize.Y);
3086 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
3087 core::rect<s32> clipping_rect = s.e->getAbsoluteClippingRect();
3088 v2s32 base_pos = s.e->getAbsolutePosition().UpperLeftCorner;
3089 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
3090 s32 item_i = i + s.start_item_i;
3094 if (s.real_coordinates) {
3095 x = (i%s.geom.X) * (imgsize.X * 1.25);
3096 y = (i/s.geom.X) * (imgsize.Y * 1.25);
3098 x = (i%s.geom.X) * spacing.X;
3099 y = (i/s.geom.X) * spacing.Y;
3102 core::rect<s32> rect = imgrect + base_pos + p0;
3103 rect.clipAgainst(clipping_rect);
3104 if (rect.getArea() > 0 && rect.isPointInside(p))
3105 return ItemSpec(s.inventoryloc, s.listname, item_i);
3109 return ItemSpec(InventoryLocation(), "", -1);
3112 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int layer,
3115 video::IVideoDriver* driver = Environment->getVideoDriver();
3117 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
3119 warningstream<<"GUIFormSpecMenu::drawList(): "
3120 << "The inventory location "
3121 << "\"" << s.inventoryloc.dump() << "\" doesn't exist anymore"
3125 InventoryList *ilist = inv->getList(s.listname);
3127 warningstream << "GUIFormSpecMenu::drawList(): "
3128 << "The inventory list \"" << s.listname << "\" @ \""
3129 << s.inventoryloc.dump() << "\" doesn't exist anymore"
3134 core::rect<s32> imgrect(0, 0, imgsize.X, imgsize.Y);
3135 core::rect<s32> clipping_rect = s.e->getAbsoluteClippingRect();
3136 v2s32 base_pos = s.e->getAbsolutePosition().UpperLeftCorner;
3138 for (s32 i = 0; i < s.geom.X * s.geom.Y; i++) {
3139 s32 item_i = i + s.start_item_i;
3140 if (item_i >= (s32)ilist->getSize())
3145 if (s.real_coordinates) {
3146 x = (i%s.geom.X) * (imgsize.X * 1.25);
3147 y = (i/s.geom.X) * (imgsize.Y * 1.25);
3149 x = (i%s.geom.X) * spacing.X;
3150 y = (i/s.geom.X) * spacing.Y;
3153 core::rect<s32> rect = imgrect + base_pos + p;
3154 ItemStack item = ilist->getItem(item_i);
3156 bool selected = m_selected_item
3157 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
3158 && m_selected_item->listname == s.listname
3159 && m_selected_item->i == item_i;
3160 core::rect<s32> clipped_rect(rect);
3161 clipped_rect.clipAgainst(clipping_rect);
3162 bool hovering = clipped_rect.getArea() > 0 &&
3163 clipped_rect.isPointInside(m_pointer);
3164 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
3165 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
3169 item_hovered = true;
3170 driver->draw2DRectangle(m_slotbg_h, rect, &clipping_rect);
3172 driver->draw2DRectangle(m_slotbg_n, rect, &clipping_rect);
3176 //Draw inv slot borders
3178 s32 x1 = rect.UpperLeftCorner.X;
3179 s32 y1 = rect.UpperLeftCorner.Y;
3180 s32 x2 = rect.LowerRightCorner.X;
3181 s32 y2 = rect.LowerRightCorner.Y;
3183 driver->draw2DRectangle(m_slotbordercolor,
3184 core::rect<s32>(v2s32(x1 - border, y1 - border),
3185 v2s32(x2 + border, y1)), &clipping_rect);
3186 driver->draw2DRectangle(m_slotbordercolor,
3187 core::rect<s32>(v2s32(x1 - border, y2),
3188 v2s32(x2 + border, y2 + border)), &clipping_rect);
3189 driver->draw2DRectangle(m_slotbordercolor,
3190 core::rect<s32>(v2s32(x1 - border, y1),
3191 v2s32(x1, y2)), &clipping_rect);
3192 driver->draw2DRectangle(m_slotbordercolor,
3193 core::rect<s32>(v2s32(x2, y1),
3194 v2s32(x2 + border, y2)), &clipping_rect);
3199 item.takeItem(m_selected_amount);
3201 if (!item.empty()) {
3203 drawItemStack(driver, m_font, item,
3204 rect, &clipping_rect, m_client, rotation_kind);
3206 if (hovering && !m_selected_item) {
3207 std::string tooltip = item.getDescription(m_client->idef());
3208 if (m_tooltip_append_itemname)
3209 tooltip += "\n[" + item.name + "]";
3210 showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
3211 m_default_tooltip_bgcolor);
3218 void GUIFormSpecMenu::drawSelectedItem()
3220 video::IVideoDriver* driver = Environment->getVideoDriver();
3222 if (!m_selected_item) {
3223 // reset rotation time
3224 drawItemStack(driver, m_font, ItemStack(),
3225 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)), NULL,
3226 m_client, IT_ROT_DRAGGED);
3230 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3232 InventoryList *list = inv->getList(m_selected_item->listname);
3234 ItemStack stack = list->getItem(m_selected_item->i);
3235 stack.count = m_selected_amount;
3237 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
3238 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
3239 rect.constrainTo(driver->getViewPort());
3240 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
3243 void GUIFormSpecMenu::drawMenu()
3246 const std::string &newform = m_form_src->getForm();
3247 if (newform != m_formspec_string) {
3248 m_formspec_string = newform;
3249 regenerateGui(m_screensize_old);
3253 gui::IGUISkin* skin = Environment->getSkin();
3254 sanity_check(skin != NULL);
3255 gui::IGUIFont *old_font = skin->getFont();
3256 skin->setFont(m_font);
3258 updateSelectedItem();
3260 video::IVideoDriver* driver = Environment->getVideoDriver();
3263 Draw background color
3265 v2u32 screenSize = driver->getScreenSize();
3266 core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
3269 driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
3270 if (m_bgnonfullscreen)
3271 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
3274 Draw rect_mode tooltip
3276 m_tooltip_element->setVisible(false);
3278 for (const auto &pair : m_tooltip_rects) {
3279 const core::rect<s32> &rect = pair.first->getAbsoluteClippingRect();
3280 if (rect.getArea() > 0 && rect.isPointInside(m_pointer)) {
3281 const std::wstring &text = pair.second.tooltip;
3282 if (!text.empty()) {
3283 showTooltip(text, pair.second.color, pair.second.bgcolor);
3292 for (gui::IGUIElement *e : m_backgrounds) {
3293 e->setVisible(true);
3295 e->setVisible(false);
3301 gui::IGUIElement::draw();
3305 Layer 0: Item slot rectangles
3306 Layer 1: Item images; prepare tooltip
3308 bool item_hovered = false;
3309 for (int layer = 0; layer < 2; layer++) {
3310 for (const GUIFormSpecMenu::ListDrawSpec &spec : m_inventorylists) {
3311 drawList(spec, layer, item_hovered);
3314 if (!item_hovered) {
3315 // reset rotation time
3316 drawItemStack(driver, m_font, ItemStack(),
3317 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
3318 NULL, m_client, IT_ROT_HOVERED);
3321 /* TODO find way to show tooltips on touchscreen */
3322 #ifndef HAVE_TOUCHSCREENGUI
3323 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
3327 Draw fields/buttons tooltips
3329 gui::IGUIElement *hovered =
3330 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
3332 if (hovered != NULL) {
3333 s32 id = hovered->getID();
3337 m_old_tooltip_id = id;
3339 if (id == m_old_tooltip_id) {
3340 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
3342 m_hovered_time = porting::getTimeMs();
3343 m_old_tooltip_id = id;
3347 // Find and update the current tooltip
3348 if (id != -1 && delta >= m_tooltip_show_delay) {
3349 for (const FieldSpec &field : m_fields) {
3351 if (field.fid != id)
3354 const std::wstring &text = m_tooltips[field.fname].tooltip;
3356 showTooltip(text, m_tooltips[field.fname].color,
3357 m_tooltips[field.fname].bgcolor);
3364 m_tooltip_element->draw();
3367 Draw dragged item stack
3371 skin->setFont(old_font);
3375 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
3376 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
3378 const std::wstring ntext = translate_string(text);
3379 m_tooltip_element->setOverrideColor(color);
3380 m_tooltip_element->setBackgroundColor(bgcolor);
3381 setStaticText(m_tooltip_element, ntext.c_str());
3383 // Tooltip size and offset
3384 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
3385 #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
3386 std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
3387 s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
3389 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
3391 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
3392 int tooltip_offset_x = m_btn_height;
3393 int tooltip_offset_y = m_btn_height;
3395 tooltip_offset_x *= 3;
3396 tooltip_offset_y = 0;
3397 if (m_pointer.X > (s32)screenSize.X / 2)
3398 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
3401 // Calculate and set the tooltip position
3402 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
3403 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
3404 if (tooltip_x + tooltip_width > (s32)screenSize.X)
3405 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
3406 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
3407 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
3409 m_tooltip_element->setRelativePosition(
3411 core::position2d<s32>(tooltip_x, tooltip_y),
3412 core::dimension2d<s32>(tooltip_width, tooltip_height)
3416 // Display the tooltip
3417 m_tooltip_element->setVisible(true);
3418 bringToFront(m_tooltip_element);
3421 void GUIFormSpecMenu::updateSelectedItem()
3423 verifySelectedItem();
3425 // If craftresult is nonempty and nothing else is selected, select it now.
3426 if (!m_selected_item) {
3427 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
3428 if (s.listname != "craftpreview")
3431 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
3435 InventoryList *list = inv->getList("craftresult");
3437 if (!list || list->getSize() == 0)
3440 const ItemStack &item = list->getItem(0);
3444 // Grab selected item from the crafting result list
3445 m_selected_item = new ItemSpec;
3446 m_selected_item->inventoryloc = s.inventoryloc;
3447 m_selected_item->listname = "craftresult";
3448 m_selected_item->i = 0;
3449 m_selected_amount = item.count;
3450 m_selected_dragging = false;
3455 // If craftresult is selected, keep the whole stack selected
3456 if (m_selected_item && m_selected_item->listname == "craftresult")
3457 m_selected_amount = verifySelectedItem().count;
3460 ItemStack GUIFormSpecMenu::verifySelectedItem()
3462 // If the selected stack has become empty for some reason, deselect it.
3463 // If the selected stack has become inaccessible, deselect it.
3464 // If the selected stack has become smaller, adjust m_selected_amount.
3465 // Return the selected stack.
3469 if(m_selected_item->isValid())
3471 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3474 InventoryList *list = inv->getList(m_selected_item->listname);
3475 if(list && (u32) m_selected_item->i < list->getSize())
3477 ItemStack stack = list->getItem(m_selected_item->i);
3478 if (!m_selected_swap.empty()) {
3479 if (m_selected_swap.name == stack.name &&
3480 m_selected_swap.count == stack.count)
3481 m_selected_swap.clear();
3483 m_selected_amount = std::min(m_selected_amount, stack.count);
3492 // selection was not valid
3493 delete m_selected_item;
3494 m_selected_item = NULL;
3495 m_selected_amount = 0;
3496 m_selected_dragging = false;
3501 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
3507 if (quitmode == quit_mode_accept) {
3508 fields["quit"] = "true";
3511 if (quitmode == quit_mode_cancel) {
3512 fields["quit"] = "true";
3513 m_text_dst->gotText(fields);
3517 if (current_keys_pending.key_down) {
3518 fields["key_down"] = "true";
3519 current_keys_pending.key_down = false;
3522 if (current_keys_pending.key_up) {
3523 fields["key_up"] = "true";
3524 current_keys_pending.key_up = false;
3527 if (current_keys_pending.key_enter) {
3528 fields["key_enter"] = "true";
3529 current_keys_pending.key_enter = false;
3532 if (!current_field_enter_pending.empty()) {
3533 fields["key_enter_field"] = current_field_enter_pending;
3534 current_field_enter_pending = "";
3537 if (current_keys_pending.key_escape) {
3538 fields["key_escape"] = "true";
3539 current_keys_pending.key_escape = false;
3542 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
3544 std::string name = s.fname;
3545 if (s.ftype == f_Button) {
3546 fields[name] = wide_to_utf8(s.flabel);
3547 } else if (s.ftype == f_Table) {
3548 GUITable *table = getTable(s.fname);
3550 fields[name] = table->checkEvent();
3553 else if(s.ftype == f_DropDown) {
3554 // no dynamic cast possible due to some distributions shipped
3555 // without rtti support in irrlicht
3556 IGUIElement *element = getElementFromId(s.fid, true);
3557 gui::IGUIComboBox *e = NULL;
3558 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
3559 e = static_cast<gui::IGUIComboBox*>(element);
3561 warningstream << "GUIFormSpecMenu::acceptInput: dropdown "
3562 << "field without dropdown element" << std::endl;
3565 s32 selected = e->getSelected();
3566 if (selected >= 0) {
3567 std::vector<std::string> *dropdown_values =
3568 getDropDownValues(s.fname);
3569 if (dropdown_values && selected < (s32)dropdown_values->size()) {
3570 fields[name] = (*dropdown_values)[selected];
3574 else if (s.ftype == f_TabHeader) {
3575 // no dynamic cast possible due to some distributions shipped
3576 // without rttzi support in irrlicht
3577 IGUIElement *element = getElementFromId(s.fid, true);
3578 gui::IGUITabControl *e = nullptr;
3579 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
3580 e = static_cast<gui::IGUITabControl *>(element);
3584 std::stringstream ss;
3585 ss << (e->getActiveTab() +1);
3586 fields[name] = ss.str();
3589 else if (s.ftype == f_CheckBox) {
3590 // no dynamic cast possible due to some distributions shipped
3591 // without rtti support in irrlicht
3592 IGUIElement *element = getElementFromId(s.fid, true);
3593 gui::IGUICheckBox *e = nullptr;
3594 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
3595 e = static_cast<gui::IGUICheckBox*>(element);
3600 fields[name] = "true";
3602 fields[name] = "false";
3605 else if (s.ftype == f_ScrollBar) {
3606 // no dynamic cast possible due to some distributions shipped
3607 // without rtti support in irrlicht
3608 IGUIElement *element = getElementFromId(s.fid, true);
3609 GUIScrollBar *e = nullptr;
3610 if (element && element->getType() == gui::EGUIET_ELEMENT)
3611 e = static_cast<GUIScrollBar *>(element);
3614 std::stringstream os;
3616 if (s.fdefault == L"Changed")
3617 fields[name] = "CHG:" + os.str();
3619 fields[name] = "VAL:" + os.str();
3623 IGUIElement *e = getElementFromId(s.fid, true);
3625 fields[name] = wide_to_utf8(e->getText());
3630 m_text_dst->gotText(fields);
3634 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
3636 while(tocheck != NULL) {
3637 if (tocheck == parent) {
3640 tocheck = tocheck->getParent();
3645 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
3647 // The IGUITabControl renders visually using the skin's selected
3648 // font, which we override for the duration of form drawing,
3649 // but computes tab hotspots based on how it would have rendered
3650 // using the font that is selected at the time of button release.
3651 // To make these two consistent, temporarily override the skin's
3652 // font while the IGUITabControl is processing the event.
3653 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3654 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3655 s32 x = event.MouseInput.X;
3656 s32 y = event.MouseInput.Y;
3657 gui::IGUIElement *hovered =
3658 Environment->getRootGUIElement()->getElementFromPoint(
3659 core::position2d<s32>(x, y));
3660 if (hovered && isMyChild(hovered) &&
3661 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
3662 gui::IGUISkin* skin = Environment->getSkin();
3663 sanity_check(skin != NULL);
3664 gui::IGUIFont *old_font = skin->getFont();
3665 skin->setFont(m_font);
3666 bool retval = hovered->OnEvent(event);
3667 skin->setFont(old_font);
3672 // Fix Esc/Return key being eaten by checkboxen and tables
3673 if(event.EventType==EET_KEY_INPUT_EVENT) {
3674 KeyPress kp(event.KeyInput);
3675 if (kp == EscapeKey || kp == CancelKey
3676 || kp == getKeySetting("keymap_inventory")
3677 || event.KeyInput.Key==KEY_RETURN) {
3678 gui::IGUIElement *focused = Environment->getFocus();
3679 if (focused && isMyChild(focused) &&
3680 (focused->getType() == gui::EGUIET_LIST_BOX ||
3681 focused->getType() == gui::EGUIET_CHECK_BOX) &&
3682 (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
3683 event.KeyInput.Key != KEY_RETURN)) {
3689 // Mouse wheel and move events: send to hovered element instead of focused
3690 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3691 (event.MouseInput.Event == EMIE_MOUSE_WHEEL ||
3692 event.MouseInput.Event == EMIE_MOUSE_MOVED)) {
3693 s32 x = event.MouseInput.X;
3694 s32 y = event.MouseInput.Y;
3695 gui::IGUIElement *hovered =
3696 Environment->getRootGUIElement()->getElementFromPoint(
3697 core::position2d<s32>(x, y));
3698 if (hovered && isMyChild(hovered)) {
3699 hovered->OnEvent(event);
3700 return event.MouseInput.Event == EMIE_MOUSE_WHEEL;
3704 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
3705 s32 x = event.MouseInput.X;
3706 s32 y = event.MouseInput.Y;
3707 gui::IGUIElement *hovered =
3708 Environment->getRootGUIElement()->getElementFromPoint(
3709 core::position2d<s32>(x, y));
3710 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3711 m_old_tooltip_id = -1;
3713 if (!isChild(hovered,this)) {
3714 if (DoubleClickDetection(event)) {
3720 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3721 /* TODO add a check like:
3722 if (event.JoystickEvent != joystick_we_listen_for)
3725 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3727 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3729 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3731 acceptInput(quit_mode_accept);
3739 return GUIModalMenu::preprocessEvent(event);
3742 /******************************************************************************/
3743 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3745 /* The following code is for capturing double-clicks of the mouse button
3746 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3747 * -- which closes the form -- under some circumstances.
3749 * There have been many github issues reporting this as a bug even though it
3750 * was an intended feature. For this reason, remapping the double-click as
3751 * an ESC must be explicitly set when creating this class via the
3752 * /p remap_dbl_click parameter of the constructor.
3755 if (!m_remap_dbl_click)
3758 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3759 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3760 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3762 m_doubleclickdetect[1].pos = m_pointer;
3763 m_doubleclickdetect[1].time = porting::getTimeMs();
3765 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3766 u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
3771 double squaredistance =
3772 m_doubleclickdetect[0].pos
3773 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3775 if (squaredistance > (30*30)) {
3779 SEvent* translated = new SEvent();
3780 assert(translated != 0);
3781 //translate doubleclick to escape
3782 memset(translated, 0, sizeof(SEvent));
3783 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3784 translated->KeyInput.Key = KEY_ESCAPE;
3785 translated->KeyInput.Control = false;
3786 translated->KeyInput.Shift = false;
3787 translated->KeyInput.PressedDown = true;
3788 translated->KeyInput.Char = 0;
3789 OnEvent(*translated);
3791 // no need to send the key up event as we're already deleted
3792 // and no one else did notice this event
3800 void GUIFormSpecMenu::tryClose()
3804 acceptInput(quit_mode_cancel);
3807 m_text_dst->gotText(L"MenuQuit");
3811 enum ButtonEventType : u8
3824 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3826 if (event.EventType==EET_KEY_INPUT_EVENT) {
3827 KeyPress kp(event.KeyInput);
3828 if (event.KeyInput.PressedDown && (
3829 (kp == EscapeKey) || (kp == CancelKey) ||
3830 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
3835 if (m_client != NULL && event.KeyInput.PressedDown &&
3836 (kp == getKeySetting("keymap_screenshot"))) {
3837 m_client->makeScreenshot();
3839 if (event.KeyInput.PressedDown &&
3840 (event.KeyInput.Key==KEY_RETURN ||
3841 event.KeyInput.Key==KEY_UP ||
3842 event.KeyInput.Key==KEY_DOWN)
3844 switch (event.KeyInput.Key) {
3846 current_keys_pending.key_enter = true;
3849 current_keys_pending.key_up = true;
3852 current_keys_pending.key_down = true;
3856 //can't happen at all!
3857 FATAL_ERROR("Reached a source line that can't ever been reached");
3860 if (current_keys_pending.key_enter && m_allowclose) {
3861 acceptInput(quit_mode_accept);
3871 /* Mouse event other than movement, or crossing the border of inventory
3872 field while holding right mouse button
3874 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3875 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3876 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3877 event.MouseInput.isRightPressed() &&
3878 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3880 // Get selected item and hovered/clicked item (s)
3882 m_old_tooltip_id = -1;
3883 updateSelectedItem();
3884 ItemSpec s = getItemAtPos(m_pointer);
3886 Inventory *inv_selected = NULL;
3887 Inventory *inv_s = NULL;
3888 InventoryList *list_s = NULL;
3890 if (m_selected_item) {
3891 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3892 sanity_check(inv_selected);
3893 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3900 inv_s = m_invmgr->getInventory(s.inventoryloc);
3903 errorstream << "InventoryMenu: The selected inventory location "
3904 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3906 s.i = -1; // make it invalid again
3910 list_s = inv_s->getList(s.listname);
3911 if (list_s == NULL) {
3912 verbosestream << "InventoryMenu: The selected inventory list \""
3913 << s.listname << "\" does not exist" << std::endl;
3914 s.i = -1; // make it invalid again
3918 if ((u32)s.i >= list_s->getSize()) {
3919 infostream << "InventoryMenu: The selected inventory list \""
3920 << s.listname << "\" is too small (i=" << s.i << ", size="
3921 << list_s->getSize() << ")" << std::endl;
3922 s.i = -1; // make it invalid again
3926 s_count = list_s->getItem(s.i).count;
3929 bool identical = m_selected_item && s.isValid() &&
3930 (inv_selected == inv_s) &&
3931 (m_selected_item->listname == s.listname) &&
3932 (m_selected_item->i == s.i);
3934 ButtonEventType button = BET_LEFT;
3935 ButtonEventType updown = BET_OTHER;
3936 switch (event.MouseInput.Event) {
3937 case EMIE_LMOUSE_PRESSED_DOWN:
3938 button = BET_LEFT; updown = BET_DOWN;
3940 case EMIE_RMOUSE_PRESSED_DOWN:
3941 button = BET_RIGHT; updown = BET_DOWN;
3943 case EMIE_MMOUSE_PRESSED_DOWN:
3944 button = BET_MIDDLE; updown = BET_DOWN;
3946 case EMIE_MOUSE_WHEEL:
3947 button = (event.MouseInput.Wheel > 0) ?
3948 BET_WHEEL_UP : BET_WHEEL_DOWN;
3951 case EMIE_LMOUSE_LEFT_UP:
3952 button = BET_LEFT; updown = BET_UP;
3954 case EMIE_RMOUSE_LEFT_UP:
3955 button = BET_RIGHT; updown = BET_UP;
3957 case EMIE_MMOUSE_LEFT_UP:
3958 button = BET_MIDDLE; updown = BET_UP;
3960 case EMIE_MOUSE_MOVED:
3967 // Set this number to a positive value to generate a move action
3968 // from m_selected_item to s.
3969 u32 move_amount = 0;
3971 // Set this number to a positive value to generate a move action
3972 // from s to the next inventory ring.
3973 u32 shift_move_amount = 0;
3975 // Set this number to a positive value to generate a drop action
3976 // from m_selected_item.
3977 u32 drop_amount = 0;
3979 // Set this number to a positive value to generate a craft action at s.
3980 u32 craft_amount = 0;
3984 // Some mouse button has been pressed
3986 //infostream<<"Mouse button "<<button<<" pressed at p=("
3987 // <<p.X<<","<<p.Y<<")"<<std::endl;
3989 m_selected_dragging = false;
3991 if (s.isValid() && s.listname == "craftpreview") {
3992 // Craft preview has been clicked: craft
3993 craft_amount = (button == BET_MIDDLE ? 10 : 1);
3994 } else if (!m_selected_item) {
3995 if (s_count && button != BET_WHEEL_UP) {
3996 // Non-empty stack has been clicked: select or shift-move it
3997 m_selected_item = new ItemSpec(s);
4000 if (button == BET_RIGHT)
4001 count = (s_count + 1) / 2;
4002 else if (button == BET_MIDDLE)
4003 count = MYMIN(s_count, 10);
4004 else if (button == BET_WHEEL_DOWN)
4009 if (!event.MouseInput.Shift) {
4010 // no shift: select item
4011 m_selected_amount = count;
4012 m_selected_dragging = button != BET_WHEEL_DOWN;
4013 m_auto_place = false;
4015 // shift pressed: move item, right click moves 1
4016 shift_move_amount = button == BET_RIGHT ? 1 : count;
4019 } else { // m_selected_item != NULL
4020 assert(m_selected_amount >= 1);
4023 // Clicked a slot: move
4024 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4026 else if (button == BET_MIDDLE)
4027 move_amount = MYMIN(m_selected_amount, 10);
4028 else if (button == BET_LEFT)
4029 move_amount = m_selected_amount;
4033 if (button == BET_WHEEL_DOWN) {
4034 if (m_selected_amount < s_count)
4035 ++m_selected_amount;
4037 if (move_amount >= m_selected_amount)
4038 m_selected_amount = 0;
4040 m_selected_amount -= move_amount;
4044 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
4045 && button != BET_WHEEL_DOWN) {
4046 // Clicked outside of the window: drop
4047 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4049 else if (button == BET_MIDDLE)
4050 drop_amount = MYMIN(m_selected_amount, 10);
4052 drop_amount = m_selected_amount;
4057 // Some mouse button has been released
4059 //infostream<<"Mouse button "<<button<<" released at p=("
4060 // <<p.X<<","<<p.Y<<")"<<std::endl;
4062 if (m_selected_dragging && m_selected_item) {
4065 // Dragged to different slot: move all selected
4066 move_amount = m_selected_amount;
4068 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
4069 // Dragged outside of window: drop all selected
4070 drop_amount = m_selected_amount;
4074 m_selected_dragging = false;
4075 // Keep track of whether the mouse button be released
4076 // One click is drag without dropping. Click + release
4077 // + click changes to drop item when moved mode
4078 if (m_selected_item)
4079 m_auto_place = true;
4082 // Mouse has been moved and rmb is down and mouse pointer just
4083 // entered a new inventory field (checked in the entry-if, this
4084 // is the only action here that is generated by mouse movement)
4085 if (m_selected_item && s.isValid() && s.listname != "craftpreview") {
4087 // TODO: middle mouse to move 10 items might be handy
4089 // Only move an item if the destination slot is empty
4090 // or contains the same item type as what is going to be
4092 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4093 InventoryList *list_to = list_s;
4094 assert(list_from && list_to);
4095 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4096 ItemStack stack_to = list_to->getItem(s.i);
4097 if (stack_to.empty() || stack_to.name == stack_from.name)
4106 // Possibly send inventory action to server
4107 if (move_amount > 0) {
4108 // Send IAction::Move
4110 assert(m_selected_item && m_selected_item->isValid());
4111 assert(s.isValid());
4113 assert(inv_selected && inv_s);
4114 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4115 InventoryList *list_to = list_s;
4116 assert(list_from && list_to);
4117 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4118 ItemStack stack_to = list_to->getItem(s.i);
4120 // Check how many items can be moved
4121 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
4122 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
4124 // If source stack cannot be added to destination stack at all,
4126 if (leftover.count == stack_from.count &&
4127 leftover.name == stack_from.name) {
4129 if (m_selected_swap.empty()) {
4130 m_selected_amount = stack_to.count;
4131 m_selected_dragging = false;
4133 // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
4134 // Skip next validation checks due async inventory calls
4135 m_selected_swap = stack_to;
4140 // Source stack goes fully into destination stack
4141 else if (leftover.empty()) {
4142 m_selected_amount -= move_amount;
4144 // Source stack goes partly into destination stack
4146 move_amount -= leftover.count;
4147 m_selected_amount -= move_amount;
4151 infostream << "Handing IAction::Move to manager" << std::endl;
4152 IMoveAction *a = new IMoveAction();
4153 a->count = move_amount;
4154 a->from_inv = m_selected_item->inventoryloc;
4155 a->from_list = m_selected_item->listname;
4156 a->from_i = m_selected_item->i;
4157 a->to_inv = s.inventoryloc;
4158 a->to_list = s.listname;
4160 m_invmgr->inventoryAction(a);
4162 } else if (shift_move_amount > 0) {
4163 u32 mis = m_inventory_rings.size();
4165 for (; i < mis; i++) {
4166 const ListRingSpec &sp = m_inventory_rings[i];
4167 if (sp.inventoryloc == s.inventoryloc
4168 && sp.listname == s.listname)
4172 if (i >= mis) // if not found
4174 u32 to_inv_ind = (i + 1) % mis;
4175 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
4176 InventoryList *list_from = list_s;
4179 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
4182 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
4185 ItemStack stack_from = list_from->getItem(s.i);
4186 assert(shift_move_amount <= stack_from.count);
4188 infostream << "Handing IAction::Move to manager" << std::endl;
4189 IMoveAction *a = new IMoveAction();
4190 a->count = shift_move_amount;
4191 a->from_inv = s.inventoryloc;
4192 a->from_list = s.listname;
4194 a->to_inv = to_inv_sp.inventoryloc;
4195 a->to_list = to_inv_sp.listname;
4196 a->move_somewhere = true;
4197 m_invmgr->inventoryAction(a);
4199 } else if (drop_amount > 0) {
4200 // Send IAction::Drop
4202 assert(m_selected_item && m_selected_item->isValid());
4203 assert(inv_selected);
4204 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4206 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4208 // Check how many items can be dropped
4209 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
4210 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
4211 m_selected_amount -= drop_amount;
4213 infostream << "Handing IAction::Drop to manager" << std::endl;
4214 IDropAction *a = new IDropAction();
4215 a->count = drop_amount;
4216 a->from_inv = m_selected_item->inventoryloc;
4217 a->from_list = m_selected_item->listname;
4218 a->from_i = m_selected_item->i;
4219 m_invmgr->inventoryAction(a);
4220 } else if (craft_amount > 0) {
4221 assert(s.isValid());
4223 // if there are no items selected or the selected item
4224 // belongs to craftresult list, proceed with crafting
4225 if (m_selected_item == NULL ||
4226 !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
4230 // Send IACTION_CRAFT
4231 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
4232 ICraftAction *a = new ICraftAction();
4233 a->count = craft_amount;
4234 a->craft_inv = s.inventoryloc;
4235 m_invmgr->inventoryAction(a);
4239 // If m_selected_amount has been decreased to zero, deselect
4240 if (m_selected_amount == 0) {
4241 m_selected_swap.clear();
4242 delete m_selected_item;
4243 m_selected_item = NULL;
4244 m_selected_amount = 0;
4245 m_selected_dragging = false;
4247 m_old_pointer = m_pointer;
4250 if (event.EventType == EET_GUI_EVENT) {
4251 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
4253 // find the element that was clicked
4254 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4255 if ((s.ftype == f_TabHeader) &&
4256 (s.fid == event.GUIEvent.Caller->getID())) {
4264 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
4266 if (!canTakeFocus(event.GUIEvent.Element)) {
4267 infostream<<"GUIFormSpecMenu: Not allowing focus change."
4269 // Returning true disables focus change
4273 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
4274 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
4275 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
4276 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
4277 unsigned int btn_id = event.GUIEvent.Caller->getID();
4279 if (btn_id == 257) {
4281 acceptInput(quit_mode_accept);
4285 m_text_dst->gotText(L"ExitButton");
4287 // quitMenu deallocates menu
4291 // find the element that was clicked
4292 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4293 // if its a button, set the send field so
4294 // lua knows which button was pressed
4295 if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
4296 s.fid == event.GUIEvent.Caller->getID()) {
4300 acceptInput(quit_mode_accept);
4303 m_text_dst->gotText(L"ExitButton");
4308 acceptInput(quit_mode_no);
4312 } else if ((s.ftype == f_DropDown) &&
4313 (s.fid == event.GUIEvent.Caller->getID())) {
4314 // only send the changed dropdown
4315 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4316 if (s2.ftype == f_DropDown) {
4321 acceptInput(quit_mode_no);
4323 // revert configuration to make sure dropdowns are sent on
4324 // regular button click
4325 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4326 if (s2.ftype == f_DropDown) {
4331 } else if ((s.ftype == f_ScrollBar) &&
4332 (s.fid == event.GUIEvent.Caller->getID())) {
4333 s.fdefault = L"Changed";
4334 acceptInput(quit_mode_no);
4336 } else if ((s.ftype == f_Unknown) &&
4337 (s.fid == event.GUIEvent.Caller->getID())) {
4345 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
4346 if (event.GUIEvent.Caller->getID() > 257) {
4347 bool close_on_enter = true;
4348 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4349 if (s.ftype == f_Unknown &&
4350 s.fid == event.GUIEvent.Caller->getID()) {
4351 current_field_enter_pending = s.fname;
4352 std::unordered_map<std::string, bool>::const_iterator it =
4353 field_close_on_enter.find(s.fname);
4354 if (it != field_close_on_enter.end())
4355 close_on_enter = (*it).second;
4361 if (m_allowclose && close_on_enter) {
4362 current_keys_pending.key_enter = true;
4363 acceptInput(quit_mode_accept);
4366 current_keys_pending.key_enter = true;
4369 // quitMenu deallocates menu
4374 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
4375 int current_id = event.GUIEvent.Caller->getID();
4376 if (current_id > 257) {
4377 // find the element that was clicked
4378 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4379 // if it's a table, set the send field
4380 // so lua knows which table was changed
4381 if ((s.ftype == f_Table) && (s.fid == current_id)) {
4392 return Parent ? Parent->OnEvent(event) : false;
4396 * get name of element by element id
4397 * @param id of element
4398 * @return name string or empty string
4400 std::string GUIFormSpecMenu::getNameByID(s32 id)
4402 for (FieldSpec &spec : m_fields) {
4410 const GUIFormSpecMenu::FieldSpec *GUIFormSpecMenu::getSpecByID(s32 id)
4412 for (FieldSpec &spec : m_fields) {
4420 * get label of element by id
4421 * @param id of element
4422 * @return label string or empty string
4424 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
4426 for (FieldSpec &spec : m_fields) {
4433 StyleSpec GUIFormSpecMenu::getStyleForElement(const std::string &type,
4434 const std::string &name, const std::string &parent_type) {
4437 if (!parent_type.empty()) {
4438 auto it = theme_by_type.find(parent_type);
4439 if (it != theme_by_type.end()) {
4444 auto it = theme_by_type.find(type);
4445 if (it != theme_by_type.end()) {
4449 it = theme_by_name.find(name);
4450 if (it != theme_by_name.end()) {