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 GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, this, spec.fid, spec.flabel.c_str());
1840 if (spec.fname == data->focused_fieldname) {
1841 Environment->setFocus(e);
1844 auto style = getStyleForElement("image_button", spec.fname);
1845 e->setFromStyle(style, m_tsrc);
1847 // We explicitly handle these arguments *after* the style properties in
1848 // order to override them if they are provided
1849 if (!image_name.empty())
1851 video::ITexture *texture = m_tsrc->getTexture(image_name);
1852 e->setForegroundImage(guiScalingImageButton(
1853 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1855 if (!pressed_image_name.empty()) {
1856 video::ITexture *pressed_texture = m_tsrc->getTexture(pressed_image_name);
1857 e->setPressedForegroundImage(guiScalingImageButton(
1858 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1860 e->setScaleImage(true);
1862 if (parts.size() >= 7) {
1863 e->setNotClipped(noclip);
1864 e->setDrawBorder(drawborder);
1867 m_fields.push_back(spec);
1871 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1874 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
1876 std::vector<std::string> parts = split(element, ';');
1878 if (((parts.size() == 4) || (parts.size() == 6)) || (parts.size() == 7 &&
1879 data->real_coordinates) || ((parts.size() > 6) &&
1880 (m_formspec_version > FORMSPEC_API_VERSION)))
1882 std::vector<std::string> v_pos = split(parts[0],',');
1884 // If we're using real coordinates, add an extra field for height.
1885 // Width is not here because tabs are the width of the text, and
1886 // there's no reason to change that.
1888 std::vector<std::string> v_geom = {"1", "0.75"}; // Dummy width and default height
1889 bool auto_width = true;
1890 if (parts.size() == 7) {
1893 v_geom = split(parts[1], ',');
1894 if (v_geom.size() == 1)
1895 v_geom.insert(v_geom.begin(), "1"); // Dummy value
1900 std::string name = parts[i+1];
1901 std::vector<std::string> buttons = split(parts[i+2], ',');
1902 std::string str_index = parts[i+3];
1903 bool show_background = true;
1904 bool show_border = true;
1905 int tab_index = stoi(str_index) - 1;
1907 MY_CHECKPOS("tabheader", 0);
1909 if (parts.size() == 6 + i) {
1910 if (parts[4+i] == "true")
1911 show_background = false;
1912 if (parts[5+i] == "false")
1913 show_border = false;
1920 258 + m_fields.size()
1923 spec.ftype = f_TabHeader;
1928 if (data->real_coordinates) {
1929 pos = getRealCoordinateBasePos(v_pos);
1931 geom = getRealCoordinateGeometry(v_geom);
1932 pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
1934 geom.X = DesiredRect.getWidth(); // Set automatic width
1936 MY_CHECKGEOM("tabheader", 1);
1938 v2f32 pos_f = pos_offset * spacing;
1939 pos_f.X += stof(v_pos[0]) * spacing.X;
1940 pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
1941 pos = v2s32(pos_f.X, pos_f.Y);
1943 geom.Y = m_btn_height * 2;
1944 geom.X = DesiredRect.getWidth();
1947 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1950 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1951 show_background, show_border, spec.fid);
1952 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1953 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1954 e->setTabHeight(geom.Y);
1956 if (spec.fname == data->focused_fieldname) {
1957 Environment->setFocus(e);
1960 auto style = getStyleForElement("tabheader", name);
1961 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
1963 for (const std::string &button : buttons) {
1964 auto tab = e->addTab(unescape_translate(unescape_string(
1965 utf8_to_wide(button))).c_str(), -1);
1966 if (style.isNotDefault(StyleSpec::BGCOLOR))
1967 tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
1969 tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1972 if ((tab_index >= 0) &&
1973 (buttons.size() < INT_MAX) &&
1974 (tab_index < (int) buttons.size()))
1975 e->setActiveTab(tab_index);
1977 m_fields.push_back(spec);
1980 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1981 << element << "'" << std::endl;
1984 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
1987 if (m_client == 0) {
1988 warningstream << "invalid use of item_image_button with m_client==0"
1993 std::vector<std::string> parts = split(element,';');
1995 if ((parts.size() == 5) ||
1996 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1998 std::vector<std::string> v_pos = split(parts[0],',');
1999 std::vector<std::string> v_geom = split(parts[1],',');
2000 std::string item_name = parts[2];
2001 std::string name = parts[3];
2002 std::string label = parts[4];
2004 label = unescape_string(label);
2005 item_name = unescape_string(item_name);
2007 MY_CHECKPOS("itemimagebutton",0);
2008 MY_CHECKGEOM("itemimagebutton",1);
2013 if (data->real_coordinates) {
2014 pos = getRealCoordinateBasePos(v_pos);
2015 geom = getRealCoordinateGeometry(v_geom);
2017 pos = getElementBasePos(&v_pos);
2018 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
2019 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
2022 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
2024 if(!data->explicit_size)
2025 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
2027 IItemDefManager *idef = m_client->idef();
2029 item.deSerialize(item_name, idef);
2032 TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
2033 m_default_tooltip_bgcolor,
2034 m_default_tooltip_color);
2036 // the spec for the button
2039 utf8_to_wide(label),
2040 utf8_to_wide(item_name),
2041 258 + m_fields.size(),
2045 GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment, rect, this, spec_btn.fid, spec_btn.flabel.c_str(), item_name, m_client);
2047 auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
2048 e_btn->setFromStyle(style, m_tsrc);
2050 if (spec_btn.fname == data->focused_fieldname) {
2051 Environment->setFocus(e_btn);
2054 spec_btn.ftype = f_Button;
2055 rect += data->basepos-padding;
2056 spec_btn.rect = rect;
2057 m_fields.push_back(spec_btn);
2060 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
2063 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
2065 std::vector<std::string> parts = split(element,';');
2067 if ((parts.size() == 3) ||
2068 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
2070 std::vector<std::string> v_pos = split(parts[0],',');
2071 std::vector<std::string> v_geom = split(parts[1],',');
2073 MY_CHECKPOS("box",0);
2074 MY_CHECKGEOM("box",1);
2079 if (data->real_coordinates) {
2080 pos = getRealCoordinateBasePos(v_pos);
2081 geom = getRealCoordinateGeometry(v_geom);
2083 pos = getElementBasePos(&v_pos);
2084 geom.X = stof(v_geom[0]) * spacing.X;
2085 geom.Y = stof(v_geom[1]) * spacing.Y;
2088 video::SColor tmp_color;
2090 if (parseColorString(parts[2], tmp_color, false, 0x8C)) {
2095 258 + m_fields.size(),
2100 core::rect<s32> rect(pos, pos + geom);
2102 GUIBox *e = new GUIBox(Environment, this, spec.fid, rect, tmp_color);
2104 e->setNotClipped(true);
2108 m_fields.push_back(spec);
2111 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
2115 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
2118 void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
2120 std::vector<std::string> parts = split(element,';');
2121 const u32 parameter_count = parts.size();
2123 if ((parameter_count > 2 && m_formspec_version < 3) ||
2124 (parameter_count > 3 && m_formspec_version <= FORMSPEC_API_VERSION)) {
2125 errorstream << "Invalid bgcolor element(" << parameter_count << "): '"
2126 << element << "'" << std::endl;
2131 if (parameter_count >= 1 && parts[0] != "")
2132 parseColorString(parts[0], m_bgcolor, false);
2135 if (parameter_count >= 2) {
2136 if (parts[1] == "both") {
2137 m_bgnonfullscreen = true;
2138 m_bgfullscreen = true;
2139 } else if (parts[1] == "neither") {
2140 m_bgnonfullscreen = false;
2141 m_bgfullscreen = false;
2142 } else if (parts[1] != "" || m_formspec_version < 3) {
2143 m_bgfullscreen = is_yes(parts[1]);
2144 m_bgnonfullscreen = !m_bgfullscreen;
2149 if (parameter_count >= 3 && parts[2] != "")
2150 parseColorString(parts[2], m_fullscreen_bgcolor, false);
2153 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
2155 std::vector<std::string> parts = split(element,';');
2157 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
2158 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2160 parseColorString(parts[0], m_slotbg_n, false);
2161 parseColorString(parts[1], m_slotbg_h, false);
2163 if (parts.size() >= 3) {
2164 if (parseColorString(parts[2], m_slotbordercolor, false)) {
2165 m_slotborder = true;
2168 if (parts.size() == 5) {
2169 video::SColor tmp_color;
2171 if (parseColorString(parts[3], tmp_color, false))
2172 m_default_tooltip_bgcolor = tmp_color;
2173 if (parseColorString(parts[4], tmp_color, false))
2174 m_default_tooltip_color = tmp_color;
2178 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
2181 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
2183 std::vector<std::string> parts = split(element,';');
2184 if (parts.size() < 2) {
2185 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2186 << element << "'" << std::endl;
2190 // Get mode and check size
2191 bool rect_mode = parts[0].find(',') != std::string::npos;
2192 size_t base_size = rect_mode ? 3 : 2;
2193 if (parts.size() != base_size && parts.size() != base_size + 2) {
2194 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2195 << element << "'" << std::endl;
2200 video::SColor bgcolor = m_default_tooltip_bgcolor;
2201 video::SColor color = m_default_tooltip_color;
2202 if (parts.size() == base_size + 2 &&
2203 (!parseColorString(parts[base_size], bgcolor, false) ||
2204 !parseColorString(parts[base_size + 1], color, false))) {
2205 errorstream << "Invalid color in tooltip element(" << parts.size()
2206 << "): '" << element << "'" << std::endl;
2210 // Make tooltip spec
2211 std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
2212 TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
2216 std::vector<std::string> v_pos = split(parts[0], ',');
2217 std::vector<std::string> v_geom = split(parts[1], ',');
2219 MY_CHECKPOS("tooltip", 0);
2220 MY_CHECKGEOM("tooltip", 1);
2225 if (data->real_coordinates) {
2226 pos = getRealCoordinateBasePos(v_pos);
2227 geom = getRealCoordinateGeometry(v_geom);
2229 pos = getElementBasePos(&v_pos);
2230 geom.X = stof(v_geom[0]) * spacing.X;
2231 geom.Y = stof(v_geom[1]) * spacing.Y;
2234 FieldSpec fieldspec(
2238 258 + m_fields.size()
2241 core::rect<s32> rect(pos, pos + geom);
2243 gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
2244 this, fieldspec.fid, rect);
2246 m_fields.push_back(fieldspec);
2247 m_tooltip_rects.emplace_back(e, spec);
2250 m_tooltips[parts[0]] = spec;
2254 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
2260 std::vector<std::string> parts = split(data,'[');
2262 if (parts.size() < 2) {
2266 if (trim(parts[0]) != "formspec_version") {
2270 if (is_number(parts[1])) {
2271 m_formspec_version = mystoi(parts[1]);
2278 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
2280 if (element.empty())
2283 std::vector<std::string> parts = split(element,'[');
2285 if (parts.size() < 2)
2288 std::string type = trim(parts[0]);
2289 std::string description = trim(parts[1]);
2291 if (type != "size" && type != "invsize")
2294 if (type == "invsize")
2295 log_deprecated("Deprecated formspec element \"invsize\" is used");
2297 parseSize(data, description);
2302 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
2304 if (element.empty())
2307 std::vector<std::string> parts = split(element, '[');
2309 if (parts.size() != 2)
2312 std::string type = trim(parts[0]);
2313 std::string description = trim(parts[1]);
2315 if (type != "position")
2318 parsePosition(data, description);
2323 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
2325 std::vector<std::string> parts = split(element, ',');
2327 if (parts.size() == 2) {
2328 data->offset.X = stof(parts[0]);
2329 data->offset.Y = stof(parts[1]);
2333 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
2336 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
2338 if (element.empty())
2341 std::vector<std::string> parts = split(element, '[');
2343 if (parts.size() != 2)
2346 std::string type = trim(parts[0]);
2347 std::string description = trim(parts[1]);
2349 if (type != "anchor")
2352 parseAnchor(data, description);
2357 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
2359 std::vector<std::string> parts = split(element, ',');
2361 if (parts.size() == 2) {
2362 data->anchor.X = stof(parts[0]);
2363 data->anchor.Y = stof(parts[1]);
2367 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
2368 << "'" << std::endl;
2371 bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type)
2373 std::vector<std::string> parts = split(element, ';');
2375 if (parts.size() < 2) {
2376 errorstream << "Invalid style element (" << parts.size() << "): '" << element
2377 << "'" << std::endl;
2381 std::string selector = trim(parts[0]);
2382 if (selector.empty()) {
2383 errorstream << "Invalid style element (Selector required): '" << element
2384 << "'" << std::endl;
2390 for (size_t i = 1; i < parts.size(); i++) {
2391 size_t equal_pos = parts[i].find('=');
2392 if (equal_pos == std::string::npos) {
2393 errorstream << "Invalid style element (Property missing value): '" << element
2394 << "'" << std::endl;
2398 std::string propname = trim(parts[i].substr(0, equal_pos));
2399 std::string value = trim(unescape_string(parts[i].substr(equal_pos + 1)));
2401 std::transform(propname.begin(), propname.end(), propname.begin(), ::tolower);
2403 StyleSpec::Property prop = StyleSpec::GetPropertyByName(propname);
2404 if (prop == StyleSpec::NONE) {
2405 if (property_warned.find(propname) != property_warned.end()) {
2406 warningstream << "Invalid style element (Unknown property " << propname << "): '"
2408 << "'" << std::endl;
2409 property_warned.insert(propname);
2414 spec.set(prop, value);
2418 theme_by_type[selector] |= spec;
2420 theme_by_name[selector] |= spec;
2426 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
2429 if (element.empty())
2432 if (parseVersionDirect(element))
2435 std::vector<std::string> parts = split(element,'[');
2437 // ugly workaround to keep compatibility
2438 if (parts.size() > 2) {
2439 if (trim(parts[0]) == "image") {
2440 for (unsigned int i=2;i< parts.size(); i++) {
2441 parts[1] += "[" + parts[i];
2447 if (parts.size() < 2) {
2451 std::string type = trim(parts[0]);
2452 std::string description = trim(parts[1]);
2454 if (type == "container") {
2455 parseContainer(data, description);
2459 if (type == "container_end") {
2460 parseContainerEnd(data);
2464 if (type == "list") {
2465 parseList(data, description);
2469 if (type == "listring") {
2470 parseListRing(data, description);
2474 if (type == "checkbox") {
2475 parseCheckbox(data, description);
2479 if (type == "image") {
2480 parseImage(data, description);
2484 if (type == "item_image") {
2485 parseItemImage(data, description);
2489 if (type == "button" || type == "button_exit") {
2490 parseButton(data, description, type);
2494 if (type == "background" || type == "background9") {
2495 parseBackground(data, description);
2499 if (type == "tableoptions"){
2500 parseTableOptions(data,description);
2504 if (type == "tablecolumns"){
2505 parseTableColumns(data,description);
2509 if (type == "table"){
2510 parseTable(data,description);
2514 if (type == "textlist"){
2515 parseTextList(data,description);
2519 if (type == "dropdown"){
2520 parseDropDown(data,description);
2524 if (type == "field_close_on_enter") {
2525 parseFieldCloseOnEnter(data, description);
2529 if (type == "pwdfield") {
2530 parsePwdField(data,description);
2534 if ((type == "field") || (type == "textarea")){
2535 parseField(data,description,type);
2539 if (type == "hypertext") {
2540 parseHyperText(data,description);
2544 if (type == "label") {
2545 parseLabel(data,description);
2549 if (type == "vertlabel") {
2550 parseVertLabel(data,description);
2554 if (type == "item_image_button") {
2555 parseItemImageButton(data,description);
2559 if ((type == "image_button") || (type == "image_button_exit")) {
2560 parseImageButton(data,description,type);
2564 if (type == "tabheader") {
2565 parseTabHeader(data,description);
2569 if (type == "box") {
2570 parseBox(data,description);
2574 if (type == "bgcolor") {
2575 parseBackgroundColor(data,description);
2579 if (type == "listcolors") {
2580 parseListColors(data,description);
2584 if (type == "tooltip") {
2585 parseTooltip(data,description);
2589 if (type == "scrollbar") {
2590 parseScrollBar(data, description);
2594 if (type == "real_coordinates") {
2595 data->real_coordinates = is_yes(description);
2599 if (type == "style") {
2600 parseStyle(data, description, false);
2604 if (type == "style_type") {
2605 parseStyle(data, description, true);
2609 if (type == "scrollbaroptions") {
2610 parseScrollBarOptions(data, description);
2615 infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
2619 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
2621 /* useless to regenerate without a screensize */
2622 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
2629 for (auto &m_table : m_tables) {
2630 std::string tablename = m_table.first.fname;
2631 GUITable *table = m_table.second;
2632 mydata.table_dyndata[tablename] = table->getDynamicData();
2636 if (!m_focused_element.empty())
2637 mydata.focused_fieldname = m_focused_element;
2640 gui::IGUIElement *focused_element = Environment->getFocus();
2641 if (focused_element && focused_element->getParent() == this) {
2642 s32 focused_id = focused_element->getID();
2643 if (focused_id > 257) {
2644 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
2645 if (field.fid == focused_id) {
2646 mydata.focused_fieldname = field.fname;
2656 for (auto &table_it : m_tables)
2657 table_it.second->drop();
2658 for (auto &inventorylist_it : m_inventorylists)
2659 inventorylist_it.e->drop();
2660 for (auto &checkbox_it : m_checkboxes)
2661 checkbox_it.second->drop();
2662 for (auto &scrollbar_it : m_scrollbars)
2663 scrollbar_it.second->drop();
2664 for (auto &background_it : m_backgrounds)
2665 background_it->drop();
2666 for (auto &tooltip_rect_it : m_tooltip_rects)
2667 tooltip_rect_it.first->drop();
2669 mydata.size= v2s32(100,100);
2670 mydata.screensize = screensize;
2671 mydata.offset = v2f32(0.5f, 0.5f);
2672 mydata.anchor = v2f32(0.5f, 0.5f);
2673 mydata.simple_field_count = 0;
2675 // Base position of contents of form
2676 mydata.basepos = getBasePos();
2678 /* Convert m_init_draw_spec to m_inventorylists */
2680 m_inventorylists.clear();
2681 m_backgrounds.clear();
2683 m_checkboxes.clear();
2684 m_scrollbars.clear();
2687 m_tooltip_rects.clear();
2688 m_inventory_rings.clear();
2689 m_dropdowns.clear();
2690 theme_by_name.clear();
2691 theme_by_type.clear();
2693 m_bgnonfullscreen = true;
2694 m_bgfullscreen = false;
2696 m_formspec_version = 1;
2699 v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
2700 m_bgcolor = video::SColor(
2701 (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
2702 clamp_u8(myround(formspec_bgcolor.X)),
2703 clamp_u8(myround(formspec_bgcolor.Y)),
2704 clamp_u8(myround(formspec_bgcolor.Z))
2709 v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
2710 m_fullscreen_bgcolor = video::SColor(
2711 (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
2712 clamp_u8(myround(formspec_bgcolor.X)),
2713 clamp_u8(myround(formspec_bgcolor.Y)),
2714 clamp_u8(myround(formspec_bgcolor.Z))
2718 m_slotbg_n = video::SColor(255,128,128,128);
2719 m_slotbg_h = video::SColor(255,192,192,192);
2721 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
2722 m_default_tooltip_color = video::SColor(255,255,255,255);
2724 m_slotbordercolor = video::SColor(200,0,0,0);
2725 m_slotborder = false;
2729 assert(!m_tooltip_element);
2730 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
2731 m_tooltip_element = gui::StaticText::add(Environment, L"",
2732 core::rect<s32>(0, 0, 110, 18));
2733 m_tooltip_element->enableOverrideColor(true);
2734 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2735 m_tooltip_element->setDrawBackground(true);
2736 m_tooltip_element->setDrawBorder(true);
2737 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2738 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
2739 m_tooltip_element->setWordWrap(false);
2740 //we're not parent so no autograb for this one!
2741 m_tooltip_element->grab();
2744 std::vector<std::string> elements = split(m_formspec_string,']');
2747 /* try to read version from first element only */
2748 if (!elements.empty()) {
2749 if (parseVersionDirect(elements[0])) {
2754 /* we need size first in order to calculate image scale */
2755 mydata.explicit_size = false;
2756 for (; i< elements.size(); i++) {
2757 if (!parseSizeDirect(&mydata, elements[i])) {
2762 /* "position" element is always after "size" element if it used */
2763 for (; i< elements.size(); i++) {
2764 if (!parsePositionDirect(&mydata, elements[i])) {
2769 /* "anchor" element is always after "position" (or "size" element) if it used */
2770 for (; i< elements.size(); i++) {
2771 if (!parseAnchorDirect(&mydata, elements[i])) {
2776 /* "no_prepend" element is always after "position" (or "size" element) if it used */
2777 bool enable_prepends = true;
2778 for (; i < elements.size(); i++) {
2779 if (elements[i].empty())
2782 std::vector<std::string> parts = split(elements[i], '[');
2783 if (trim(parts[0]) == "no_prepend")
2784 enable_prepends = false;
2789 /* Copy of the "real_coordinates" element for after the form size. */
2790 mydata.real_coordinates = m_formspec_version >= 2;
2791 for (; i < elements.size(); i++) {
2792 std::vector<std::string> parts = split(elements[i], '[');
2793 std::string name = trim(parts[0]);
2794 if (name != "real_coordinates" || parts.size() != 2)
2795 break; // Invalid format
2797 mydata.real_coordinates = is_yes(trim(parts[1]));
2800 if (mydata.explicit_size) {
2801 // compute scaling for specified form size
2803 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
2804 v2u32 delta = current_screensize - m_lockscreensize;
2806 if (current_screensize.Y > m_lockscreensize.Y)
2811 if (current_screensize.X > m_lockscreensize.X)
2816 offset = v2s32(delta.X,delta.Y);
2818 mydata.screensize = m_lockscreensize;
2820 offset = v2s32(0,0);
2823 double gui_scaling = g_settings->getFloat("gui_scaling");
2824 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2828 // In fixed-size mode, inventory image size
2829 // is 0.53 inch multiplied by the gui_scaling
2830 // config parameter. This magic size is chosen
2831 // to make the main menu (15.5 inventory images
2832 // wide, including border) just fit into the
2833 // default window (800 pixels wide) at 96 DPI
2834 // and default scaling (1.00).
2835 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
2837 // In variable-size mode, we prefer to make the
2838 // inventory image size 1/15 of screen height,
2839 // multiplied by the gui_scaling config parameter.
2840 // If the preferred size won't fit the whole
2841 // form on the screen, either horizontally or
2842 // vertically, then we scale it down to fit.
2843 // (The magic numbers in the computation of what
2844 // fits arise from the scaling factors in the
2845 // following stanza, including the form border,
2846 // help text space, and 0.1 inventory slot spare.)
2847 // However, a minimum size is also set, that
2848 // the image size can't be less than 0.3 inch
2849 // multiplied by gui_scaling, even if this means
2850 // the form doesn't fit the screen.
2852 // For mobile devices these magic numbers are
2853 // different and forms should always use the
2854 // maximum screen space available.
2855 double prefer_imgsize = mydata.screensize.Y / 10 * gui_scaling;
2856 double fitx_imgsize = mydata.screensize.X /
2857 ((12.0 / 8.0) * (0.5 + mydata.invsize.X));
2858 double fity_imgsize = mydata.screensize.Y /
2859 ((15.0 / 11.0) * (0.85 + mydata.invsize.Y));
2860 use_imgsize = MYMIN(prefer_imgsize,
2861 MYMIN(fitx_imgsize, fity_imgsize));
2863 double prefer_imgsize = mydata.screensize.Y / 15 * gui_scaling;
2864 double fitx_imgsize = mydata.screensize.X /
2865 ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
2866 double fity_imgsize = mydata.screensize.Y /
2867 ((15.0 / 13.0) * (0.85 * mydata.invsize.Y));
2868 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2869 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
2870 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2871 MYMIN(fitx_imgsize, fity_imgsize)));
2875 // Everything else is scaled in proportion to the
2876 // inventory image size. The inventory slot spacing
2877 // is 5/4 image size horizontally and 15/13 image size
2878 // vertically. The padding around the form (incorporating
2879 // the border of the outer inventory slots) is 3/8
2880 // image size. Font height (baseline to baseline)
2881 // is 2/5 vertical inventory slot spacing, and button
2882 // half-height is 7/8 of font height.
2883 imgsize = v2s32(use_imgsize, use_imgsize);
2884 spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2885 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2886 m_btn_height = use_imgsize*15.0/13 * 0.35;
2888 m_font = g_fontengine->getFont();
2890 if (mydata.real_coordinates) {
2891 mydata.size = v2s32(
2892 mydata.invsize.X*imgsize.X,
2893 mydata.invsize.Y*imgsize.Y
2896 mydata.size = v2s32(
2897 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2898 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2902 DesiredRect = mydata.rect = core::rect<s32>(
2903 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
2904 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
2905 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
2906 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
2909 // Non-size[] form must consist only of text fields and
2910 // implicit "Proceed" button. Use default font, and
2911 // temporary form size which will be recalculated below.
2912 m_font = g_fontengine->getFont();
2913 m_btn_height = font_line_height(m_font) * 0.875;
2914 DesiredRect = core::rect<s32>(
2915 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
2916 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
2917 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
2918 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
2921 recalculateAbsolutePosition(false);
2922 mydata.basepos = getBasePos();
2923 m_tooltip_element->setOverrideFont(m_font);
2925 gui::IGUISkin *skin = Environment->getSkin();
2927 gui::IGUIFont *old_font = skin->getFont();
2928 skin->setFont(m_font);
2930 pos_offset = v2f32();
2932 // used for formspec versions < 3
2933 core::list<IGUIElement *>::Iterator legacy_sort_start = Children.getLast();
2935 if (enable_prepends) {
2936 // Backup the coordinates so that prepends can use the coordinates of choice.
2937 bool rc_backup = mydata.real_coordinates;
2938 u16 version_backup = m_formspec_version;
2939 mydata.real_coordinates = false; // Old coordinates by default.
2941 std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
2942 for (const auto &element : prepend_elements)
2943 parseElement(&mydata, element);
2945 // legacy sorting for formspec versions < 3
2946 if (m_formspec_version >= 3)
2947 // prepends do not need to be reordered
2948 legacy_sort_start = Children.getLast();
2949 else if (version_backup >= 3)
2950 // only prepends elements have to be reordered
2951 legacySortElements(legacy_sort_start);
2953 m_formspec_version = version_backup;
2954 mydata.real_coordinates = rc_backup; // Restore coordinates
2957 for (; i< elements.size(); i++) {
2958 parseElement(&mydata, elements[i]);
2961 if (!container_stack.empty()) {
2962 errorstream << "Invalid formspec string: container was never closed!"
2966 // If there are fields without explicit size[], add a "Proceed"
2967 // button and adjust size to fit all the fields.
2968 if (mydata.simple_field_count > 0 && !mydata.explicit_size) {
2969 mydata.rect = core::rect<s32>(
2970 mydata.screensize.X / 2 - 580 / 2,
2971 mydata.screensize.Y / 2 - 300 / 2,
2972 mydata.screensize.X / 2 + 580 / 2,
2973 mydata.screensize.Y / 2 + 240 / 2 + mydata.simple_field_count * 60
2976 DesiredRect = mydata.rect;
2977 recalculateAbsolutePosition(false);
2978 mydata.basepos = getBasePos();
2981 v2s32 pos = mydata.basepos;
2982 pos.Y = (mydata.simple_field_count + 2) * 60;
2984 v2s32 size = DesiredRect.getSize();
2985 mydata.rect = core::rect<s32>(
2986 size.X / 2 - 70, pos.Y,
2987 size.X / 2 - 70 + 140, pos.Y + m_btn_height * 2
2989 const wchar_t *text = wgettext("Proceed");
2990 GUIButton::addButton(Environment, mydata.rect, this, 257, text);
2995 //set initial focus if parser didn't set it
2996 focused_element = Environment->getFocus();
2997 if (!focused_element
2998 || !isMyChild(focused_element)
2999 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
3002 skin->setFont(old_font);
3005 if (m_formspec_version < 3)
3006 legacySortElements(legacy_sort_start);
3009 void GUIFormSpecMenu::legacySortElements(core::list<IGUIElement *>::Iterator from)
3012 Draw order for formspec_version <= 2:
3016 0 All other elements
3018 2 item_image, item_image_button
3023 if (from == Children.end())
3024 from = Children.begin();
3028 core::list<IGUIElement *>::Iterator to = Children.end();
3029 // 1: Copy into a sortable container
3030 std::vector<IGUIElement *> elements;
3031 for (auto it = from; it != to; ++it)
3032 elements.emplace_back(*it);
3034 // 2: Sort the container
3035 std::stable_sort(elements.begin(), elements.end(),
3036 [this] (const IGUIElement *a, const IGUIElement *b) -> bool {
3037 const FieldSpec *spec_a = getSpecByID(a->getID());
3038 const FieldSpec *spec_b = getSpecByID(b->getID());
3039 return spec_a && spec_b &&
3040 spec_a->priority < spec_b->priority;
3043 // 3: Re-assign the pointers
3044 for (auto e : elements) {
3051 bool GUIFormSpecMenu::getAndroidUIInput()
3053 if (!hasAndroidUIInput())
3056 std::string fieldname = m_jni_field_name;
3057 m_jni_field_name.clear();
3059 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3060 iter != m_fields.end(); ++iter) {
3062 if (iter->fname != fieldname) {
3065 IGUIElement *tochange = getElementFromId(iter->fid, true);
3067 if (tochange == 0) {
3071 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
3075 std::string text = porting::getInputDialogValue();
3077 ((gui::IGUIEditBox *)tochange)->setText(utf8_to_wide(text).c_str());
3083 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
3085 core::rect<s32> imgrect(0, 0, imgsize.X, imgsize.Y);
3087 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
3088 core::rect<s32> clipping_rect = s.e->getAbsoluteClippingRect();
3089 v2s32 base_pos = s.e->getAbsolutePosition().UpperLeftCorner;
3090 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
3091 s32 item_i = i + s.start_item_i;
3095 if (s.real_coordinates) {
3096 x = (i%s.geom.X) * (imgsize.X * 1.25);
3097 y = (i/s.geom.X) * (imgsize.Y * 1.25);
3099 x = (i%s.geom.X) * spacing.X;
3100 y = (i/s.geom.X) * spacing.Y;
3103 core::rect<s32> rect = imgrect + base_pos + p0;
3104 rect.clipAgainst(clipping_rect);
3105 if (rect.getArea() > 0 && rect.isPointInside(p))
3106 return ItemSpec(s.inventoryloc, s.listname, item_i);
3110 return ItemSpec(InventoryLocation(), "", -1);
3113 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int layer,
3116 video::IVideoDriver* driver = Environment->getVideoDriver();
3118 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
3120 warningstream<<"GUIFormSpecMenu::drawList(): "
3121 << "The inventory location "
3122 << "\"" << s.inventoryloc.dump() << "\" doesn't exist anymore"
3126 InventoryList *ilist = inv->getList(s.listname);
3128 warningstream << "GUIFormSpecMenu::drawList(): "
3129 << "The inventory list \"" << s.listname << "\" @ \""
3130 << s.inventoryloc.dump() << "\" doesn't exist anymore"
3135 core::rect<s32> imgrect(0, 0, imgsize.X, imgsize.Y);
3136 core::rect<s32> clipping_rect = s.e->getAbsoluteClippingRect();
3137 v2s32 base_pos = s.e->getAbsolutePosition().UpperLeftCorner;
3139 for (s32 i = 0; i < s.geom.X * s.geom.Y; i++) {
3140 s32 item_i = i + s.start_item_i;
3141 if (item_i >= (s32)ilist->getSize())
3146 if (s.real_coordinates) {
3147 x = (i%s.geom.X) * (imgsize.X * 1.25);
3148 y = (i/s.geom.X) * (imgsize.Y * 1.25);
3150 x = (i%s.geom.X) * spacing.X;
3151 y = (i/s.geom.X) * spacing.Y;
3154 core::rect<s32> rect = imgrect + base_pos + p;
3155 ItemStack item = ilist->getItem(item_i);
3157 bool selected = m_selected_item
3158 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
3159 && m_selected_item->listname == s.listname
3160 && m_selected_item->i == item_i;
3161 core::rect<s32> clipped_rect(rect);
3162 clipped_rect.clipAgainst(clipping_rect);
3163 bool hovering = clipped_rect.getArea() > 0 &&
3164 clipped_rect.isPointInside(m_pointer);
3165 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
3166 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
3170 item_hovered = true;
3171 driver->draw2DRectangle(m_slotbg_h, rect, &clipping_rect);
3173 driver->draw2DRectangle(m_slotbg_n, rect, &clipping_rect);
3177 //Draw inv slot borders
3179 s32 x1 = rect.UpperLeftCorner.X;
3180 s32 y1 = rect.UpperLeftCorner.Y;
3181 s32 x2 = rect.LowerRightCorner.X;
3182 s32 y2 = rect.LowerRightCorner.Y;
3184 driver->draw2DRectangle(m_slotbordercolor,
3185 core::rect<s32>(v2s32(x1 - border, y1 - border),
3186 v2s32(x2 + border, y1)), &clipping_rect);
3187 driver->draw2DRectangle(m_slotbordercolor,
3188 core::rect<s32>(v2s32(x1 - border, y2),
3189 v2s32(x2 + border, y2 + border)), &clipping_rect);
3190 driver->draw2DRectangle(m_slotbordercolor,
3191 core::rect<s32>(v2s32(x1 - border, y1),
3192 v2s32(x1, y2)), &clipping_rect);
3193 driver->draw2DRectangle(m_slotbordercolor,
3194 core::rect<s32>(v2s32(x2, y1),
3195 v2s32(x2 + border, y2)), &clipping_rect);
3200 item.takeItem(m_selected_amount);
3202 if (!item.empty()) {
3204 drawItemStack(driver, m_font, item,
3205 rect, &clipping_rect, m_client, rotation_kind);
3207 if (hovering && !m_selected_item) {
3208 std::string tooltip = item.getDescription(m_client->idef());
3209 if (m_tooltip_append_itemname)
3210 tooltip += "\n[" + item.name + "]";
3211 showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
3212 m_default_tooltip_bgcolor);
3219 void GUIFormSpecMenu::drawSelectedItem()
3221 video::IVideoDriver* driver = Environment->getVideoDriver();
3223 if (!m_selected_item) {
3224 // reset rotation time
3225 drawItemStack(driver, m_font, ItemStack(),
3226 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)), NULL,
3227 m_client, IT_ROT_DRAGGED);
3231 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3233 InventoryList *list = inv->getList(m_selected_item->listname);
3235 ItemStack stack = list->getItem(m_selected_item->i);
3236 stack.count = m_selected_amount;
3238 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
3239 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
3240 rect.constrainTo(driver->getViewPort());
3241 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
3244 void GUIFormSpecMenu::drawMenu()
3247 const std::string &newform = m_form_src->getForm();
3248 if (newform != m_formspec_string) {
3249 m_formspec_string = newform;
3250 regenerateGui(m_screensize_old);
3254 gui::IGUISkin* skin = Environment->getSkin();
3255 sanity_check(skin != NULL);
3256 gui::IGUIFont *old_font = skin->getFont();
3257 skin->setFont(m_font);
3259 updateSelectedItem();
3261 video::IVideoDriver* driver = Environment->getVideoDriver();
3264 Draw background color
3266 v2u32 screenSize = driver->getScreenSize();
3267 core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
3270 driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
3271 if (m_bgnonfullscreen)
3272 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
3275 Draw rect_mode tooltip
3277 m_tooltip_element->setVisible(false);
3279 for (const auto &pair : m_tooltip_rects) {
3280 const core::rect<s32> &rect = pair.first->getAbsoluteClippingRect();
3281 if (rect.getArea() > 0 && rect.isPointInside(m_pointer)) {
3282 const std::wstring &text = pair.second.tooltip;
3283 if (!text.empty()) {
3284 showTooltip(text, pair.second.color, pair.second.bgcolor);
3293 for (gui::IGUIElement *e : m_backgrounds) {
3294 e->setVisible(true);
3296 e->setVisible(false);
3302 gui::IGUIElement::draw();
3306 Layer 0: Item slot rectangles
3307 Layer 1: Item images; prepare tooltip
3309 bool item_hovered = false;
3310 for (int layer = 0; layer < 2; layer++) {
3311 for (const GUIFormSpecMenu::ListDrawSpec &spec : m_inventorylists) {
3312 drawList(spec, layer, item_hovered);
3315 if (!item_hovered) {
3316 // reset rotation time
3317 drawItemStack(driver, m_font, ItemStack(),
3318 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
3319 NULL, m_client, IT_ROT_HOVERED);
3322 /* TODO find way to show tooltips on touchscreen */
3323 #ifndef HAVE_TOUCHSCREENGUI
3324 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
3328 Draw fields/buttons tooltips
3330 gui::IGUIElement *hovered =
3331 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
3333 if (hovered != NULL) {
3334 s32 id = hovered->getID();
3338 m_old_tooltip_id = id;
3340 if (id == m_old_tooltip_id) {
3341 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
3343 m_hovered_time = porting::getTimeMs();
3344 m_old_tooltip_id = id;
3348 // Find and update the current tooltip
3349 if (id != -1 && delta >= m_tooltip_show_delay) {
3350 for (const FieldSpec &field : m_fields) {
3352 if (field.fid != id)
3355 const std::wstring &text = m_tooltips[field.fname].tooltip;
3357 showTooltip(text, m_tooltips[field.fname].color,
3358 m_tooltips[field.fname].bgcolor);
3365 m_tooltip_element->draw();
3368 Draw dragged item stack
3372 skin->setFont(old_font);
3376 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
3377 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
3379 const std::wstring ntext = translate_string(text);
3380 m_tooltip_element->setOverrideColor(color);
3381 m_tooltip_element->setBackgroundColor(bgcolor);
3382 setStaticText(m_tooltip_element, ntext.c_str());
3384 // Tooltip size and offset
3385 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
3386 #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
3387 std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
3388 s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
3390 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
3392 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
3393 int tooltip_offset_x = m_btn_height;
3394 int tooltip_offset_y = m_btn_height;
3396 tooltip_offset_x *= 3;
3397 tooltip_offset_y = 0;
3398 if (m_pointer.X > (s32)screenSize.X / 2)
3399 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
3402 // Calculate and set the tooltip position
3403 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
3404 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
3405 if (tooltip_x + tooltip_width > (s32)screenSize.X)
3406 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
3407 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
3408 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
3410 m_tooltip_element->setRelativePosition(
3412 core::position2d<s32>(tooltip_x, tooltip_y),
3413 core::dimension2d<s32>(tooltip_width, tooltip_height)
3417 // Display the tooltip
3418 m_tooltip_element->setVisible(true);
3419 bringToFront(m_tooltip_element);
3422 void GUIFormSpecMenu::updateSelectedItem()
3424 verifySelectedItem();
3426 // If craftresult is nonempty and nothing else is selected, select it now.
3427 if (!m_selected_item) {
3428 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
3429 if (s.listname != "craftpreview")
3432 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
3436 InventoryList *list = inv->getList("craftresult");
3438 if (!list || list->getSize() == 0)
3441 const ItemStack &item = list->getItem(0);
3445 // Grab selected item from the crafting result list
3446 m_selected_item = new ItemSpec;
3447 m_selected_item->inventoryloc = s.inventoryloc;
3448 m_selected_item->listname = "craftresult";
3449 m_selected_item->i = 0;
3450 m_selected_amount = item.count;
3451 m_selected_dragging = false;
3456 // If craftresult is selected, keep the whole stack selected
3457 if (m_selected_item && m_selected_item->listname == "craftresult")
3458 m_selected_amount = verifySelectedItem().count;
3461 ItemStack GUIFormSpecMenu::verifySelectedItem()
3463 // If the selected stack has become empty for some reason, deselect it.
3464 // If the selected stack has become inaccessible, deselect it.
3465 // If the selected stack has become smaller, adjust m_selected_amount.
3466 // Return the selected stack.
3470 if(m_selected_item->isValid())
3472 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3475 InventoryList *list = inv->getList(m_selected_item->listname);
3476 if(list && (u32) m_selected_item->i < list->getSize())
3478 ItemStack stack = list->getItem(m_selected_item->i);
3479 if (!m_selected_swap.empty()) {
3480 if (m_selected_swap.name == stack.name &&
3481 m_selected_swap.count == stack.count)
3482 m_selected_swap.clear();
3484 m_selected_amount = std::min(m_selected_amount, stack.count);
3493 // selection was not valid
3494 delete m_selected_item;
3495 m_selected_item = NULL;
3496 m_selected_amount = 0;
3497 m_selected_dragging = false;
3502 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
3508 if (quitmode == quit_mode_accept) {
3509 fields["quit"] = "true";
3512 if (quitmode == quit_mode_cancel) {
3513 fields["quit"] = "true";
3514 m_text_dst->gotText(fields);
3518 if (current_keys_pending.key_down) {
3519 fields["key_down"] = "true";
3520 current_keys_pending.key_down = false;
3523 if (current_keys_pending.key_up) {
3524 fields["key_up"] = "true";
3525 current_keys_pending.key_up = false;
3528 if (current_keys_pending.key_enter) {
3529 fields["key_enter"] = "true";
3530 current_keys_pending.key_enter = false;
3533 if (!current_field_enter_pending.empty()) {
3534 fields["key_enter_field"] = current_field_enter_pending;
3535 current_field_enter_pending = "";
3538 if (current_keys_pending.key_escape) {
3539 fields["key_escape"] = "true";
3540 current_keys_pending.key_escape = false;
3543 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
3545 std::string name = s.fname;
3546 if (s.ftype == f_Button) {
3547 fields[name] = wide_to_utf8(s.flabel);
3548 } else if (s.ftype == f_Table) {
3549 GUITable *table = getTable(s.fname);
3551 fields[name] = table->checkEvent();
3554 else if(s.ftype == f_DropDown) {
3555 // no dynamic cast possible due to some distributions shipped
3556 // without rtti support in irrlicht
3557 IGUIElement *element = getElementFromId(s.fid, true);
3558 gui::IGUIComboBox *e = NULL;
3559 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
3560 e = static_cast<gui::IGUIComboBox*>(element);
3562 warningstream << "GUIFormSpecMenu::acceptInput: dropdown "
3563 << "field without dropdown element" << std::endl;
3566 s32 selected = e->getSelected();
3567 if (selected >= 0) {
3568 std::vector<std::string> *dropdown_values =
3569 getDropDownValues(s.fname);
3570 if (dropdown_values && selected < (s32)dropdown_values->size()) {
3571 fields[name] = (*dropdown_values)[selected];
3575 else if (s.ftype == f_TabHeader) {
3576 // no dynamic cast possible due to some distributions shipped
3577 // without rttzi support in irrlicht
3578 IGUIElement *element = getElementFromId(s.fid, true);
3579 gui::IGUITabControl *e = nullptr;
3580 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
3581 e = static_cast<gui::IGUITabControl *>(element);
3585 std::stringstream ss;
3586 ss << (e->getActiveTab() +1);
3587 fields[name] = ss.str();
3590 else if (s.ftype == f_CheckBox) {
3591 // no dynamic cast possible due to some distributions shipped
3592 // without rtti support in irrlicht
3593 IGUIElement *element = getElementFromId(s.fid, true);
3594 gui::IGUICheckBox *e = nullptr;
3595 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
3596 e = static_cast<gui::IGUICheckBox*>(element);
3601 fields[name] = "true";
3603 fields[name] = "false";
3606 else if (s.ftype == f_ScrollBar) {
3607 // no dynamic cast possible due to some distributions shipped
3608 // without rtti support in irrlicht
3609 IGUIElement *element = getElementFromId(s.fid, true);
3610 GUIScrollBar *e = nullptr;
3611 if (element && element->getType() == gui::EGUIET_ELEMENT)
3612 e = static_cast<GUIScrollBar *>(element);
3615 std::stringstream os;
3617 if (s.fdefault == L"Changed")
3618 fields[name] = "CHG:" + os.str();
3620 fields[name] = "VAL:" + os.str();
3624 IGUIElement *e = getElementFromId(s.fid, true);
3626 fields[name] = wide_to_utf8(e->getText());
3631 m_text_dst->gotText(fields);
3635 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
3637 while(tocheck != NULL) {
3638 if (tocheck == parent) {
3641 tocheck = tocheck->getParent();
3646 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
3648 // The IGUITabControl renders visually using the skin's selected
3649 // font, which we override for the duration of form drawing,
3650 // but computes tab hotspots based on how it would have rendered
3651 // using the font that is selected at the time of button release.
3652 // To make these two consistent, temporarily override the skin's
3653 // font while the IGUITabControl is processing the event.
3654 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3655 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3656 s32 x = event.MouseInput.X;
3657 s32 y = event.MouseInput.Y;
3658 gui::IGUIElement *hovered =
3659 Environment->getRootGUIElement()->getElementFromPoint(
3660 core::position2d<s32>(x, y));
3661 if (hovered && isMyChild(hovered) &&
3662 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
3663 gui::IGUISkin* skin = Environment->getSkin();
3664 sanity_check(skin != NULL);
3665 gui::IGUIFont *old_font = skin->getFont();
3666 skin->setFont(m_font);
3667 bool retval = hovered->OnEvent(event);
3668 skin->setFont(old_font);
3673 // Fix Esc/Return key being eaten by checkboxen and tables
3674 if(event.EventType==EET_KEY_INPUT_EVENT) {
3675 KeyPress kp(event.KeyInput);
3676 if (kp == EscapeKey || kp == CancelKey
3677 || kp == getKeySetting("keymap_inventory")
3678 || event.KeyInput.Key==KEY_RETURN) {
3679 gui::IGUIElement *focused = Environment->getFocus();
3680 if (focused && isMyChild(focused) &&
3681 (focused->getType() == gui::EGUIET_LIST_BOX ||
3682 focused->getType() == gui::EGUIET_CHECK_BOX) &&
3683 (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
3684 event.KeyInput.Key != KEY_RETURN)) {
3690 // Mouse wheel and move events: send to hovered element instead of focused
3691 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3692 (event.MouseInput.Event == EMIE_MOUSE_WHEEL ||
3693 event.MouseInput.Event == EMIE_MOUSE_MOVED)) {
3694 s32 x = event.MouseInput.X;
3695 s32 y = event.MouseInput.Y;
3696 gui::IGUIElement *hovered =
3697 Environment->getRootGUIElement()->getElementFromPoint(
3698 core::position2d<s32>(x, y));
3699 if (hovered && isMyChild(hovered)) {
3700 hovered->OnEvent(event);
3701 return event.MouseInput.Event == EMIE_MOUSE_WHEEL;
3705 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
3706 s32 x = event.MouseInput.X;
3707 s32 y = event.MouseInput.Y;
3708 gui::IGUIElement *hovered =
3709 Environment->getRootGUIElement()->getElementFromPoint(
3710 core::position2d<s32>(x, y));
3711 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3712 m_old_tooltip_id = -1;
3714 if (!isChild(hovered,this)) {
3715 if (DoubleClickDetection(event)) {
3721 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3722 /* TODO add a check like:
3723 if (event.JoystickEvent != joystick_we_listen_for)
3726 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3728 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3730 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3732 acceptInput(quit_mode_accept);
3740 return GUIModalMenu::preprocessEvent(event);
3743 /******************************************************************************/
3744 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3746 /* The following code is for capturing double-clicks of the mouse button
3747 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3748 * -- which closes the form -- under some circumstances.
3750 * There have been many github issues reporting this as a bug even though it
3751 * was an intended feature. For this reason, remapping the double-click as
3752 * an ESC must be explicitly set when creating this class via the
3753 * /p remap_dbl_click parameter of the constructor.
3756 if (!m_remap_dbl_click)
3759 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3760 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3761 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3763 m_doubleclickdetect[1].pos = m_pointer;
3764 m_doubleclickdetect[1].time = porting::getTimeMs();
3766 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3767 u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
3772 double squaredistance =
3773 m_doubleclickdetect[0].pos
3774 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3776 if (squaredistance > (30*30)) {
3780 SEvent* translated = new SEvent();
3781 assert(translated != 0);
3782 //translate doubleclick to escape
3783 memset(translated, 0, sizeof(SEvent));
3784 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3785 translated->KeyInput.Key = KEY_ESCAPE;
3786 translated->KeyInput.Control = false;
3787 translated->KeyInput.Shift = false;
3788 translated->KeyInput.PressedDown = true;
3789 translated->KeyInput.Char = 0;
3790 OnEvent(*translated);
3792 // no need to send the key up event as we're already deleted
3793 // and no one else did notice this event
3801 void GUIFormSpecMenu::tryClose()
3805 acceptInput(quit_mode_cancel);
3808 m_text_dst->gotText(L"MenuQuit");
3812 enum ButtonEventType : u8
3825 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3827 if (event.EventType==EET_KEY_INPUT_EVENT) {
3828 KeyPress kp(event.KeyInput);
3829 if (event.KeyInput.PressedDown && (
3830 (kp == EscapeKey) || (kp == CancelKey) ||
3831 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
3836 if (m_client != NULL && event.KeyInput.PressedDown &&
3837 (kp == getKeySetting("keymap_screenshot"))) {
3838 m_client->makeScreenshot();
3840 if (event.KeyInput.PressedDown &&
3841 (event.KeyInput.Key==KEY_RETURN ||
3842 event.KeyInput.Key==KEY_UP ||
3843 event.KeyInput.Key==KEY_DOWN)
3845 switch (event.KeyInput.Key) {
3847 current_keys_pending.key_enter = true;
3850 current_keys_pending.key_up = true;
3853 current_keys_pending.key_down = true;
3857 //can't happen at all!
3858 FATAL_ERROR("Reached a source line that can't ever been reached");
3861 if (current_keys_pending.key_enter && m_allowclose) {
3862 acceptInput(quit_mode_accept);
3872 /* Mouse event other than movement, or crossing the border of inventory
3873 field while holding right mouse button
3875 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3876 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3877 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3878 event.MouseInput.isRightPressed() &&
3879 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3881 // Get selected item and hovered/clicked item (s)
3883 m_old_tooltip_id = -1;
3884 updateSelectedItem();
3885 ItemSpec s = getItemAtPos(m_pointer);
3887 Inventory *inv_selected = NULL;
3888 Inventory *inv_s = NULL;
3889 InventoryList *list_s = NULL;
3891 if (m_selected_item) {
3892 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3893 sanity_check(inv_selected);
3894 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3901 inv_s = m_invmgr->getInventory(s.inventoryloc);
3904 errorstream << "InventoryMenu: The selected inventory location "
3905 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3907 s.i = -1; // make it invalid again
3911 list_s = inv_s->getList(s.listname);
3912 if (list_s == NULL) {
3913 verbosestream << "InventoryMenu: The selected inventory list \""
3914 << s.listname << "\" does not exist" << std::endl;
3915 s.i = -1; // make it invalid again
3919 if ((u32)s.i >= list_s->getSize()) {
3920 infostream << "InventoryMenu: The selected inventory list \""
3921 << s.listname << "\" is too small (i=" << s.i << ", size="
3922 << list_s->getSize() << ")" << std::endl;
3923 s.i = -1; // make it invalid again
3927 s_count = list_s->getItem(s.i).count;
3930 bool identical = m_selected_item && s.isValid() &&
3931 (inv_selected == inv_s) &&
3932 (m_selected_item->listname == s.listname) &&
3933 (m_selected_item->i == s.i);
3935 ButtonEventType button = BET_LEFT;
3936 ButtonEventType updown = BET_OTHER;
3937 switch (event.MouseInput.Event) {
3938 case EMIE_LMOUSE_PRESSED_DOWN:
3939 button = BET_LEFT; updown = BET_DOWN;
3941 case EMIE_RMOUSE_PRESSED_DOWN:
3942 button = BET_RIGHT; updown = BET_DOWN;
3944 case EMIE_MMOUSE_PRESSED_DOWN:
3945 button = BET_MIDDLE; updown = BET_DOWN;
3947 case EMIE_MOUSE_WHEEL:
3948 button = (event.MouseInput.Wheel > 0) ?
3949 BET_WHEEL_UP : BET_WHEEL_DOWN;
3952 case EMIE_LMOUSE_LEFT_UP:
3953 button = BET_LEFT; updown = BET_UP;
3955 case EMIE_RMOUSE_LEFT_UP:
3956 button = BET_RIGHT; updown = BET_UP;
3958 case EMIE_MMOUSE_LEFT_UP:
3959 button = BET_MIDDLE; updown = BET_UP;
3961 case EMIE_MOUSE_MOVED:
3968 // Set this number to a positive value to generate a move action
3969 // from m_selected_item to s.
3970 u32 move_amount = 0;
3972 // Set this number to a positive value to generate a move action
3973 // from s to the next inventory ring.
3974 u32 shift_move_amount = 0;
3976 // Set this number to a positive value to generate a drop action
3977 // from m_selected_item.
3978 u32 drop_amount = 0;
3980 // Set this number to a positive value to generate a craft action at s.
3981 u32 craft_amount = 0;
3985 // Some mouse button has been pressed
3987 //infostream<<"Mouse button "<<button<<" pressed at p=("
3988 // <<p.X<<","<<p.Y<<")"<<std::endl;
3990 m_selected_dragging = false;
3992 if (s.isValid() && s.listname == "craftpreview") {
3993 // Craft preview has been clicked: craft
3994 craft_amount = (button == BET_MIDDLE ? 10 : 1);
3995 } else if (!m_selected_item) {
3996 if (s_count && button != BET_WHEEL_UP) {
3997 // Non-empty stack has been clicked: select or shift-move it
3998 m_selected_item = new ItemSpec(s);
4001 if (button == BET_RIGHT)
4002 count = (s_count + 1) / 2;
4003 else if (button == BET_MIDDLE)
4004 count = MYMIN(s_count, 10);
4005 else if (button == BET_WHEEL_DOWN)
4010 if (!event.MouseInput.Shift) {
4011 // no shift: select item
4012 m_selected_amount = count;
4013 m_selected_dragging = button != BET_WHEEL_DOWN;
4014 m_auto_place = false;
4016 // shift pressed: move item, right click moves 1
4017 shift_move_amount = button == BET_RIGHT ? 1 : count;
4020 } else { // m_selected_item != NULL
4021 assert(m_selected_amount >= 1);
4024 // Clicked a slot: move
4025 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4027 else if (button == BET_MIDDLE)
4028 move_amount = MYMIN(m_selected_amount, 10);
4029 else if (button == BET_LEFT)
4030 move_amount = m_selected_amount;
4034 if (button == BET_WHEEL_DOWN) {
4035 if (m_selected_amount < s_count)
4036 ++m_selected_amount;
4038 if (move_amount >= m_selected_amount)
4039 m_selected_amount = 0;
4041 m_selected_amount -= move_amount;
4045 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
4046 && button != BET_WHEEL_DOWN) {
4047 // Clicked outside of the window: drop
4048 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4050 else if (button == BET_MIDDLE)
4051 drop_amount = MYMIN(m_selected_amount, 10);
4053 drop_amount = m_selected_amount;
4058 // Some mouse button has been released
4060 //infostream<<"Mouse button "<<button<<" released at p=("
4061 // <<p.X<<","<<p.Y<<")"<<std::endl;
4063 if (m_selected_dragging && m_selected_item) {
4066 // Dragged to different slot: move all selected
4067 move_amount = m_selected_amount;
4069 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
4070 // Dragged outside of window: drop all selected
4071 drop_amount = m_selected_amount;
4075 m_selected_dragging = false;
4076 // Keep track of whether the mouse button be released
4077 // One click is drag without dropping. Click + release
4078 // + click changes to drop item when moved mode
4079 if (m_selected_item)
4080 m_auto_place = true;
4083 // Mouse has been moved and rmb is down and mouse pointer just
4084 // entered a new inventory field (checked in the entry-if, this
4085 // is the only action here that is generated by mouse movement)
4086 if (m_selected_item && s.isValid() && s.listname != "craftpreview") {
4088 // TODO: middle mouse to move 10 items might be handy
4090 // Only move an item if the destination slot is empty
4091 // or contains the same item type as what is going to be
4093 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4094 InventoryList *list_to = list_s;
4095 assert(list_from && list_to);
4096 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4097 ItemStack stack_to = list_to->getItem(s.i);
4098 if (stack_to.empty() || stack_to.name == stack_from.name)
4107 // Possibly send inventory action to server
4108 if (move_amount > 0) {
4109 // Send IAction::Move
4111 assert(m_selected_item && m_selected_item->isValid());
4112 assert(s.isValid());
4114 assert(inv_selected && inv_s);
4115 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4116 InventoryList *list_to = list_s;
4117 assert(list_from && list_to);
4118 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4119 ItemStack stack_to = list_to->getItem(s.i);
4121 // Check how many items can be moved
4122 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
4123 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
4125 // If source stack cannot be added to destination stack at all,
4127 if (leftover.count == stack_from.count &&
4128 leftover.name == stack_from.name) {
4130 if (m_selected_swap.empty()) {
4131 m_selected_amount = stack_to.count;
4132 m_selected_dragging = false;
4134 // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
4135 // Skip next validation checks due async inventory calls
4136 m_selected_swap = stack_to;
4141 // Source stack goes fully into destination stack
4142 else if (leftover.empty()) {
4143 m_selected_amount -= move_amount;
4145 // Source stack goes partly into destination stack
4147 move_amount -= leftover.count;
4148 m_selected_amount -= move_amount;
4152 infostream << "Handing IAction::Move to manager" << std::endl;
4153 IMoveAction *a = new IMoveAction();
4154 a->count = move_amount;
4155 a->from_inv = m_selected_item->inventoryloc;
4156 a->from_list = m_selected_item->listname;
4157 a->from_i = m_selected_item->i;
4158 a->to_inv = s.inventoryloc;
4159 a->to_list = s.listname;
4161 m_invmgr->inventoryAction(a);
4163 } else if (shift_move_amount > 0) {
4164 u32 mis = m_inventory_rings.size();
4166 for (; i < mis; i++) {
4167 const ListRingSpec &sp = m_inventory_rings[i];
4168 if (sp.inventoryloc == s.inventoryloc
4169 && sp.listname == s.listname)
4173 if (i >= mis) // if not found
4175 u32 to_inv_ind = (i + 1) % mis;
4176 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
4177 InventoryList *list_from = list_s;
4180 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
4183 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
4186 ItemStack stack_from = list_from->getItem(s.i);
4187 assert(shift_move_amount <= stack_from.count);
4189 infostream << "Handing IAction::Move to manager" << std::endl;
4190 IMoveAction *a = new IMoveAction();
4191 a->count = shift_move_amount;
4192 a->from_inv = s.inventoryloc;
4193 a->from_list = s.listname;
4195 a->to_inv = to_inv_sp.inventoryloc;
4196 a->to_list = to_inv_sp.listname;
4197 a->move_somewhere = true;
4198 m_invmgr->inventoryAction(a);
4200 } else if (drop_amount > 0) {
4201 // Send IAction::Drop
4203 assert(m_selected_item && m_selected_item->isValid());
4204 assert(inv_selected);
4205 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4207 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4209 // Check how many items can be dropped
4210 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
4211 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
4212 m_selected_amount -= drop_amount;
4214 infostream << "Handing IAction::Drop to manager" << std::endl;
4215 IDropAction *a = new IDropAction();
4216 a->count = drop_amount;
4217 a->from_inv = m_selected_item->inventoryloc;
4218 a->from_list = m_selected_item->listname;
4219 a->from_i = m_selected_item->i;
4220 m_invmgr->inventoryAction(a);
4221 } else if (craft_amount > 0) {
4222 assert(s.isValid());
4224 // if there are no items selected or the selected item
4225 // belongs to craftresult list, proceed with crafting
4226 if (m_selected_item == NULL ||
4227 !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
4231 // Send IACTION_CRAFT
4232 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
4233 ICraftAction *a = new ICraftAction();
4234 a->count = craft_amount;
4235 a->craft_inv = s.inventoryloc;
4236 m_invmgr->inventoryAction(a);
4240 // If m_selected_amount has been decreased to zero, deselect
4241 if (m_selected_amount == 0) {
4242 m_selected_swap.clear();
4243 delete m_selected_item;
4244 m_selected_item = NULL;
4245 m_selected_amount = 0;
4246 m_selected_dragging = false;
4248 m_old_pointer = m_pointer;
4251 if (event.EventType == EET_GUI_EVENT) {
4252 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
4254 // find the element that was clicked
4255 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4256 if ((s.ftype == f_TabHeader) &&
4257 (s.fid == event.GUIEvent.Caller->getID())) {
4265 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
4267 if (!canTakeFocus(event.GUIEvent.Element)) {
4268 infostream<<"GUIFormSpecMenu: Not allowing focus change."
4270 // Returning true disables focus change
4274 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
4275 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
4276 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
4277 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
4278 unsigned int btn_id = event.GUIEvent.Caller->getID();
4280 if (btn_id == 257) {
4282 acceptInput(quit_mode_accept);
4286 m_text_dst->gotText(L"ExitButton");
4288 // quitMenu deallocates menu
4292 // find the element that was clicked
4293 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4294 // if its a button, set the send field so
4295 // lua knows which button was pressed
4296 if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
4297 s.fid == event.GUIEvent.Caller->getID()) {
4301 acceptInput(quit_mode_accept);
4304 m_text_dst->gotText(L"ExitButton");
4309 acceptInput(quit_mode_no);
4313 } else if ((s.ftype == f_DropDown) &&
4314 (s.fid == event.GUIEvent.Caller->getID())) {
4315 // only send the changed dropdown
4316 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4317 if (s2.ftype == f_DropDown) {
4322 acceptInput(quit_mode_no);
4324 // revert configuration to make sure dropdowns are sent on
4325 // regular button click
4326 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4327 if (s2.ftype == f_DropDown) {
4332 } else if ((s.ftype == f_ScrollBar) &&
4333 (s.fid == event.GUIEvent.Caller->getID())) {
4334 s.fdefault = L"Changed";
4335 acceptInput(quit_mode_no);
4337 } else if ((s.ftype == f_Unknown) &&
4338 (s.fid == event.GUIEvent.Caller->getID())) {
4346 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
4347 if (event.GUIEvent.Caller->getID() > 257) {
4348 bool close_on_enter = true;
4349 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4350 if (s.ftype == f_Unknown &&
4351 s.fid == event.GUIEvent.Caller->getID()) {
4352 current_field_enter_pending = s.fname;
4353 std::unordered_map<std::string, bool>::const_iterator it =
4354 field_close_on_enter.find(s.fname);
4355 if (it != field_close_on_enter.end())
4356 close_on_enter = (*it).second;
4362 if (m_allowclose && close_on_enter) {
4363 current_keys_pending.key_enter = true;
4364 acceptInput(quit_mode_accept);
4367 current_keys_pending.key_enter = true;
4370 // quitMenu deallocates menu
4375 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
4376 int current_id = event.GUIEvent.Caller->getID();
4377 if (current_id > 257) {
4378 // find the element that was clicked
4379 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4380 // if it's a table, set the send field
4381 // so lua knows which table was changed
4382 if ((s.ftype == f_Table) && (s.fid == current_id)) {
4393 return Parent ? Parent->OnEvent(event) : false;
4397 * get name of element by element id
4398 * @param id of element
4399 * @return name string or empty string
4401 std::string GUIFormSpecMenu::getNameByID(s32 id)
4403 for (FieldSpec &spec : m_fields) {
4411 const GUIFormSpecMenu::FieldSpec *GUIFormSpecMenu::getSpecByID(s32 id)
4413 for (FieldSpec &spec : m_fields) {
4421 * get label of element by id
4422 * @param id of element
4423 * @return label string or empty string
4425 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
4427 for (FieldSpec &spec : m_fields) {
4434 StyleSpec GUIFormSpecMenu::getStyleForElement(const std::string &type,
4435 const std::string &name, const std::string &parent_type) {
4438 if (!parent_type.empty()) {
4439 auto it = theme_by_type.find(parent_type);
4440 if (it != theme_by_type.end()) {
4445 auto it = theme_by_type.find(type);
4446 if (it != theme_by_type.end()) {
4450 it = theme_by_name.find(name);
4451 if (it != theme_by_name.end()) {