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 "guiButton.h"
27 #include "guiFormSpecMenu.h"
29 #include "constants.h"
31 #include "client/keycode.h"
32 #include "util/strfnd.h"
33 #include <IGUICheckBox.h>
34 #include <IGUIEditBox.h>
35 #include <IGUIButton.h>
36 #include <IGUIStaticText.h>
38 #include <IGUITabControl.h>
39 #include <IGUIComboBox.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 "guiEditBoxWithScrollbar.h"
59 #include "intlGUIEditBox.h"
61 #define MY_CHECKPOS(a,b) \
62 if (v_pos.size() != 2) { \
63 errorstream<< "Invalid pos for element " << a << "specified: \"" \
64 << parts[b] << "\"" << std::endl; \
68 #define MY_CHECKGEOM(a,b) \
69 if (v_geom.size() != 2) { \
70 errorstream<< "Invalid geometry for element " << a << \
71 "specified: \"" << parts[b] << "\"" << std::endl; \
77 static unsigned int font_line_height(gui::IGUIFont *font)
79 return font->getDimension(L"Ay").Height + font->getKerningHeight();
82 inline u32 clamp_u8(s32 value)
84 return (u32) MYMIN(MYMAX(value, 0), 255);
87 GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
88 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
89 Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst,
90 const std::string &formspecPrepend,
91 bool remap_dbl_click):
92 GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr),
96 m_formspec_prepend(formspecPrepend),
100 m_remap_dbl_click(remap_dbl_click)
102 current_keys_pending.key_down = false;
103 current_keys_pending.key_up = false;
104 current_keys_pending.key_enter = false;
105 current_keys_pending.key_escape = false;
107 m_doubleclickdetect[0].time = 0;
108 m_doubleclickdetect[1].time = 0;
110 m_doubleclickdetect[0].pos = v2s32(0, 0);
111 m_doubleclickdetect[1].pos = v2s32(0, 0);
113 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
114 m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
117 GUIFormSpecMenu::~GUIFormSpecMenu()
121 for (auto &table_it : m_tables) {
122 table_it.second->drop();
125 delete m_selected_item;
130 void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
131 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
132 const std::string &formspecPrepend)
134 if (cur_formspec == nullptr) {
135 cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
136 client, client->getTextureSource(), fs_src, txt_dest, formspecPrepend);
137 cur_formspec->doPause = false;
140 Caution: do not call (*cur_formspec)->drop() here --
141 the reference might outlive the menu, so we will
142 periodically check if *cur_formspec is the only
143 remaining reference (i.e. the menu was removed)
144 and delete it in that case.
148 cur_formspec->setFormspecPrepend(formspecPrepend);
149 cur_formspec->setFormSource(fs_src);
150 cur_formspec->setTextDest(txt_dest);
154 void GUIFormSpecMenu::removeChildren()
156 const core::list<gui::IGUIElement*> &children = getChildren();
158 while(!children.empty()) {
159 (*children.getLast())->remove();
162 if(m_tooltip_element) {
163 m_tooltip_element->remove();
164 m_tooltip_element->drop();
165 m_tooltip_element = NULL;
170 void GUIFormSpecMenu::setInitialFocus()
172 // Set initial focus according to following order of precedence:
173 // 1. first empty editbox
177 // 5. first focusable (not statictext, not tabheader)
178 // 6. first child element
180 core::list<gui::IGUIElement*> children = getChildren();
182 // in case "children" contains any NULL elements, remove them
183 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
184 it != children.end();) {
188 it = children.erase(it);
191 // 1. first empty editbox
192 for (gui::IGUIElement *it : children) {
193 if (it->getType() == gui::EGUIET_EDIT_BOX
194 && it->getText()[0] == 0) {
195 Environment->setFocus(it);
201 for (gui::IGUIElement *it : children) {
202 if (it->getType() == gui::EGUIET_EDIT_BOX) {
203 Environment->setFocus(it);
209 for (gui::IGUIElement *it : children) {
210 if (it->getTypeName() == std::string("GUITable")) {
211 Environment->setFocus(it);
217 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
218 it != children.end(); --it) {
219 if ((*it)->getType() == gui::EGUIET_BUTTON) {
220 Environment->setFocus(*it);
225 // 5. first focusable (not statictext, not tabheader)
226 for (gui::IGUIElement *it : children) {
227 if (it->getType() != gui::EGUIET_STATIC_TEXT &&
228 it->getType() != gui::EGUIET_TAB_CONTROL) {
229 Environment->setFocus(it);
234 // 6. first child element
235 if (children.empty())
236 Environment->setFocus(this);
238 Environment->setFocus(*(children.begin()));
241 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
243 for (auto &table : m_tables) {
244 if (tablename == table.first.fname)
250 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
252 for (auto &dropdown : m_dropdowns) {
253 if (name == dropdown.first.fname)
254 return &dropdown.second;
259 v2s32 GUIFormSpecMenu::getElementBasePos(bool absolute,
260 const std::vector<std::string> *v_pos)
264 pos += AbsoluteRect.UpperLeftCorner;
266 v2f32 pos_f = v2f32(pos.X, pos.Y) + pos_offset * spacing;
268 pos_f.X += stof((*v_pos)[0]) * spacing.X;
269 pos_f.Y += stof((*v_pos)[1]) * spacing.Y;
271 return v2s32(pos_f.X, pos_f.Y);
274 v2s32 GUIFormSpecMenu::getRealCoordinateBasePos(bool absolute,
275 const std::vector<std::string> &v_pos)
277 v2f32 pos_f = v2f32(0.0f, 0.0f);
279 pos_f.X += stof(v_pos[0]) + pos_offset.X;
280 pos_f.Y += stof(v_pos[1]) + pos_offset.Y;
283 return v2s32(pos_f.X * imgsize.X + AbsoluteRect.UpperLeftCorner.X,
284 pos_f.Y * imgsize.Y + AbsoluteRect.UpperLeftCorner.Y);
285 return v2s32(pos_f.X * imgsize.X, pos_f.Y * imgsize.Y);
288 v2s32 GUIFormSpecMenu::getRealCoordinateGeometry(const std::vector<std::string> &v_geom)
290 return v2s32(stof(v_geom[0]) * imgsize.X, stof(v_geom[1]) * imgsize.Y);
293 void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
295 std::vector<std::string> parts = split(element,',');
297 if (((parts.size() == 2) || parts.size() == 3) ||
298 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
300 if (parts[1].find(';') != std::string::npos)
301 parts[1] = parts[1].substr(0,parts[1].find(';'));
303 data->invsize.X = MYMAX(0, stof(parts[0]));
304 data->invsize.Y = MYMAX(0, stof(parts[1]));
308 if (parts.size() == 3) {
309 if (parts[2] == "true") {
310 lockSize(true,v2u32(800,600));
314 data->explicit_size = true;
317 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
320 void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
322 std::vector<std::string> parts = split(element, ',');
324 if (parts.size() >= 2) {
325 if (parts[1].find(';') != std::string::npos)
326 parts[1] = parts[1].substr(0, parts[1].find(';'));
328 container_stack.push(pos_offset);
329 pos_offset.X += stof(parts[0]);
330 pos_offset.Y += stof(parts[1]);
333 errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
336 void GUIFormSpecMenu::parseContainerEnd(parserData* data)
338 if (container_stack.empty()) {
339 errorstream<< "Invalid container end element, no matching container start element" << std::endl;
341 pos_offset = container_stack.top();
342 container_stack.pop();
346 void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
349 warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
353 std::vector<std::string> parts = split(element,';');
355 if (((parts.size() == 4) || (parts.size() == 5)) ||
356 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
358 std::string location = parts[0];
359 std::string listname = parts[1];
360 std::vector<std::string> v_pos = split(parts[2],',');
361 std::vector<std::string> v_geom = split(parts[3],',');
362 std::string startindex;
363 if (parts.size() == 5)
364 startindex = parts[4];
366 MY_CHECKPOS("list",2);
367 MY_CHECKGEOM("list",3);
369 InventoryLocation loc;
371 if (location == "context" || location == "current_name")
372 loc = m_current_inventory_location;
374 loc.deSerialize(location);
379 if (data->real_coordinates)
380 pos = getRealCoordinateBasePos(true, v_pos);
382 pos = getElementBasePos(true, &v_pos);
384 geom.X = stoi(v_geom[0]);
385 geom.Y = stoi(v_geom[1]);
388 if (!startindex.empty())
389 start_i = stoi(startindex);
391 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
392 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
396 if(!data->explicit_size)
397 warningstream<<"invalid use of list without a size[] element"<<std::endl;
398 m_inventorylists.emplace_back(loc, listname, pos, geom, start_i, data->real_coordinates);
401 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
404 void GUIFormSpecMenu::parseListRing(parserData* data, const std::string &element)
407 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
411 std::vector<std::string> parts = split(element, ';');
413 if (parts.size() == 2) {
414 std::string location = parts[0];
415 std::string listname = parts[1];
417 InventoryLocation loc;
419 if (location == "context" || location == "current_name")
420 loc = m_current_inventory_location;
422 loc.deSerialize(location);
424 m_inventory_rings.emplace_back(loc, listname);
428 if (element.empty() && m_inventorylists.size() > 1) {
429 size_t siz = m_inventorylists.size();
430 // insert the last two inv list elements into the list ring
431 const ListDrawSpec &spa = m_inventorylists[siz - 2];
432 const ListDrawSpec &spb = m_inventorylists[siz - 1];
433 m_inventory_rings.emplace_back(spa.inventoryloc, spa.listname);
434 m_inventory_rings.emplace_back(spb.inventoryloc, spb.listname);
438 errorstream<< "Invalid list ring element(" << parts.size() << ", "
439 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
442 void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
444 std::vector<std::string> parts = split(element,';');
446 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
447 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
449 std::vector<std::string> v_pos = split(parts[0],',');
450 std::string name = parts[1];
451 std::string label = parts[2];
452 std::string selected;
454 if (parts.size() >= 4)
457 MY_CHECKPOS("checkbox",0);
459 bool fselected = false;
461 if (selected == "true")
464 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
465 const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str());
466 s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH);
467 s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2;
470 core::rect<s32> rect;
472 if (data->real_coordinates) {
473 pos = getRealCoordinateBasePos(false, v_pos);
475 rect = core::rect<s32>(
478 pos.X + label_size.Width + cb_size + 7,
482 pos = getElementBasePos(false, &v_pos);
483 rect = core::rect<s32>(
485 pos.Y + imgsize.Y / 2 - y_center,
486 pos.X + label_size.Width + cb_size + 7,
487 pos.Y + imgsize.Y / 2 + y_center
493 wlabel, //Needed for displaying text on MSVC
498 spec.ftype = f_CheckBox;
500 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
501 spec.fid, spec.flabel.c_str());
503 auto style = getStyleForElement("checkbox", name);
504 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
506 if (spec.fname == data->focused_fieldname) {
507 Environment->setFocus(e);
510 m_checkboxes.emplace_back(spec,e);
511 m_fields.push_back(spec);
514 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
517 void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
519 std::vector<std::string> parts = split(element,';');
521 if (parts.size() >= 5) {
522 std::vector<std::string> v_pos = split(parts[0],',');
523 std::vector<std::string> v_geom = split(parts[1],',');
524 std::string name = parts[3];
525 std::string value = parts[4];
527 MY_CHECKPOS("scrollbar",0);
528 MY_CHECKGEOM("scrollbar",1);
533 if (data->real_coordinates) {
534 pos = getRealCoordinateBasePos(false, v_pos);
535 dim = getRealCoordinateGeometry(v_geom);
537 pos = getElementBasePos(false, &v_pos);
538 dim.X = stof(v_geom[0]) * spacing.X;
539 dim.Y = stof(v_geom[1]) * spacing.Y;
542 core::rect<s32> rect =
543 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
552 bool is_horizontal = true;
554 if (parts[2] == "vertical")
555 is_horizontal = false;
557 spec.ftype = f_ScrollBar;
559 gui::IGUIScrollBar* e =
560 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
562 auto style = getStyleForElement("scrollbar", name);
563 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
567 e->setPos(stoi(parts[4]));
569 e->setLargeStep(100);
571 m_scrollbars.emplace_back(spec,e);
572 m_fields.push_back(spec);
575 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
578 void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
580 std::vector<std::string> parts = split(element,';');
582 if ((parts.size() == 3) ||
583 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
585 std::vector<std::string> v_pos = split(parts[0],',');
586 std::vector<std::string> v_geom = split(parts[1],',');
587 std::string name = unescape_string(parts[2]);
589 MY_CHECKPOS("image", 0);
590 MY_CHECKGEOM("image", 1);
595 if (data->real_coordinates) {
596 pos = getRealCoordinateBasePos(true, v_pos);
597 geom = getRealCoordinateGeometry(v_geom);
599 pos = getElementBasePos(true, &v_pos);
600 geom.X = stof(v_geom[0]) * (float)imgsize.X;
601 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
604 if (!data->explicit_size)
605 warningstream<<"invalid use of image without a size[] element"<<std::endl;
606 m_images.emplace_back(name, pos, geom);
610 if (parts.size() == 2) {
611 std::vector<std::string> v_pos = split(parts[0],',');
612 std::string name = unescape_string(parts[1]);
614 MY_CHECKPOS("image", 0);
616 v2s32 pos = getElementBasePos(true, &v_pos);
618 if (!data->explicit_size)
619 warningstream<<"invalid use of image without a size[] element"<<std::endl;
620 m_images.emplace_back(name, pos);
623 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
626 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
628 std::vector<std::string> parts = split(element,';');
630 if ((parts.size() == 3) ||
631 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
633 std::vector<std::string> v_pos = split(parts[0],',');
634 std::vector<std::string> v_geom = split(parts[1],',');
635 std::string name = parts[2];
637 MY_CHECKPOS("itemimage",0);
638 MY_CHECKGEOM("itemimage",1);
643 if (data->real_coordinates) {
644 pos = getRealCoordinateBasePos(true, v_pos);
645 geom = getRealCoordinateGeometry(v_geom);
647 pos = getElementBasePos(true, &v_pos);
648 geom.X = stof(v_geom[0]) * (float)imgsize.X;
649 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
652 if(!data->explicit_size)
653 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
654 m_itemimages.emplace_back("", name, pos, geom);
657 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
660 void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
661 const std::string &type)
663 std::vector<std::string> parts = split(element,';');
665 if ((parts.size() == 4) ||
666 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
668 std::vector<std::string> v_pos = split(parts[0],',');
669 std::vector<std::string> v_geom = split(parts[1],',');
670 std::string name = parts[2];
671 std::string label = parts[3];
673 MY_CHECKPOS("button",0);
674 MY_CHECKGEOM("button",1);
678 core::rect<s32> rect;
680 if (data->real_coordinates) {
681 pos = getRealCoordinateBasePos(false, v_pos);
682 geom = getRealCoordinateGeometry(v_geom);
683 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
686 pos = getElementBasePos(false, &v_pos);
687 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
688 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
690 rect = core::rect<s32>(pos.X, pos.Y - m_btn_height,
691 pos.X + geom.X, pos.Y + m_btn_height);
694 if(!data->explicit_size)
695 warningstream<<"invalid use of button without a size[] element"<<std::endl;
697 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
705 spec.ftype = f_Button;
706 if(type == "button_exit")
709 GUIButton *e = GUIButton::addButton(Environment, rect, this, spec.fid, spec.flabel.c_str());
711 auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
712 if (style.isNotDefault(StyleSpec::BGCOLOR)) {
713 e->setColor(style.getColor(StyleSpec::BGCOLOR));
715 if (style.isNotDefault(StyleSpec::TEXTCOLOR)) {
716 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR));
718 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
719 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
721 if (style.isNotDefault(StyleSpec::BGIMG)) {
722 std::string image_name = style.get(StyleSpec::BGIMG, "");
723 std::string pressed_image_name = style.get(StyleSpec::BGIMG_PRESSED, "");
725 video::ITexture *texture = 0;
726 video::ITexture *pressed_texture = 0;
727 texture = m_tsrc->getTexture(image_name);
728 if (!pressed_image_name.empty())
729 pressed_texture = m_tsrc->getTexture(pressed_image_name);
731 pressed_texture = texture;
733 e->setUseAlphaChannel(style.getBool(StyleSpec::ALPHA, true));
734 e->setImage(guiScalingImageButton(
735 Environment->getVideoDriver(), texture, geom.X, geom.Y));
736 e->setPressedImage(guiScalingImageButton(
737 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
738 e->setScaleImage(true);
741 if (spec.fname == data->focused_fieldname) {
742 Environment->setFocus(e);
745 m_fields.push_back(spec);
748 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
751 void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
753 std::vector<std::string> parts = split(element,';');
755 if ((parts.size() >= 3 && parts.size() <= 5) ||
756 (parts.size() > 5 && m_formspec_version > FORMSPEC_API_VERSION)) {
757 std::vector<std::string> v_pos = split(parts[0],',');
758 std::vector<std::string> v_geom = split(parts[1],',');
759 std::string name = unescape_string(parts[2]);
761 MY_CHECKPOS("background",0);
762 MY_CHECKGEOM("background",1);
767 if (data->real_coordinates) {
768 pos = getRealCoordinateBasePos(true, v_pos);
769 geom = getRealCoordinateGeometry(v_geom);
771 pos = getElementBasePos(true, &v_pos);
772 pos.X -= (spacing.X - (float)imgsize.X) / 2;
773 pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
775 geom.X = stof(v_geom[0]) * spacing.X;
776 geom.Y = stof(v_geom[1]) * spacing.Y;
780 if (parts.size() >= 4 && is_yes(parts[3])) {
781 if (data->real_coordinates) {
782 pos = getRealCoordinateBasePos(false, v_pos) * -1;
785 pos.X = stoi(v_pos[0]); //acts as offset
786 pos.Y = stoi(v_pos[1]);
791 core::rect<s32> middle;
792 if (parts.size() >= 5) {
793 std::vector<std::string> v_middle = split(parts[4], ',');
794 if (v_middle.size() == 1) {
795 s32 x = stoi(v_middle[0]);
796 middle.UpperLeftCorner = core::vector2di(x, x);
797 middle.LowerRightCorner = core::vector2di(-x, -x);
798 } else if (v_middle.size() == 2) {
799 s32 x = stoi(v_middle[0]);
800 s32 y = stoi(v_middle[1]);
801 middle.UpperLeftCorner = core::vector2di(x, y);
802 middle.LowerRightCorner = core::vector2di(-x, -y);
803 // `-x` is interpreted as `w - x`
804 } else if (v_middle.size() == 4) {
805 middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1]));
806 middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3]));
808 warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl;
812 if (!data->explicit_size && !clip)
813 warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
815 m_backgrounds.emplace_back(name, pos, geom, middle, clip);
819 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
822 void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
824 std::vector<std::string> parts = split(element,';');
826 data->table_options.clear();
827 for (const std::string &part : parts) {
828 // Parse table option
829 std::string opt = unescape_string(part);
830 data->table_options.push_back(GUITable::splitOption(opt));
834 void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
836 std::vector<std::string> parts = split(element,';');
838 data->table_columns.clear();
839 for (const std::string &part : parts) {
840 std::vector<std::string> col_parts = split(part,',');
841 GUITable::TableColumn column;
843 if (!col_parts.empty())
844 column.type = col_parts[0];
845 // Parse column options
846 for (size_t j = 1; j < col_parts.size(); ++j) {
847 std::string opt = unescape_string(col_parts[j]);
848 column.options.push_back(GUITable::splitOption(opt));
850 data->table_columns.push_back(column);
854 void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
856 std::vector<std::string> parts = split(element,';');
858 if (((parts.size() == 4) || (parts.size() == 5)) ||
859 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
861 std::vector<std::string> v_pos = split(parts[0],',');
862 std::vector<std::string> v_geom = split(parts[1],',');
863 std::string name = parts[2];
864 std::vector<std::string> items = split(parts[3],',');
865 std::string str_initial_selection;
866 std::string str_transparent = "false";
868 if (parts.size() >= 5)
869 str_initial_selection = parts[4];
871 MY_CHECKPOS("table",0);
872 MY_CHECKGEOM("table",1);
877 if (data->real_coordinates) {
878 pos = getRealCoordinateBasePos(false, v_pos);
879 geom = getRealCoordinateGeometry(v_geom);
881 pos = getElementBasePos(false, &v_pos);
882 geom.X = stof(v_geom[0]) * spacing.X;
883 geom.Y = stof(v_geom[1]) * spacing.Y;
886 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
895 spec.ftype = f_Table;
897 for (std::string &item : items) {
898 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
901 //now really show table
902 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
905 if (spec.fname == data->focused_fieldname) {
906 Environment->setFocus(e);
909 e->setTable(data->table_options, data->table_columns, items);
911 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
912 e->setDynamicData(data->table_dyndata[name]);
915 if (!str_initial_selection.empty() && str_initial_selection != "0")
916 e->setSelected(stoi(str_initial_selection));
918 auto style = getStyleForElement("table", name);
919 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
921 m_tables.emplace_back(spec, e);
922 m_fields.push_back(spec);
925 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
928 void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
930 std::vector<std::string> parts = split(element,';');
932 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
933 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
935 std::vector<std::string> v_pos = split(parts[0],',');
936 std::vector<std::string> v_geom = split(parts[1],',');
937 std::string name = parts[2];
938 std::vector<std::string> items = split(parts[3],',');
939 std::string str_initial_selection;
940 std::string str_transparent = "false";
942 if (parts.size() >= 5)
943 str_initial_selection = parts[4];
945 if (parts.size() >= 6)
946 str_transparent = parts[5];
948 MY_CHECKPOS("textlist",0);
949 MY_CHECKGEOM("textlist",1);
954 if (data->real_coordinates) {
955 pos = getRealCoordinateBasePos(false, v_pos);
956 geom = getRealCoordinateGeometry(v_geom);
958 pos = getElementBasePos(false, &v_pos);
959 geom.X = stof(v_geom[0]) * spacing.X;
960 geom.Y = stof(v_geom[1]) * spacing.Y;
963 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
972 spec.ftype = f_Table;
974 for (std::string &item : items) {
975 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
978 //now really show list
979 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
982 if (spec.fname == data->focused_fieldname) {
983 Environment->setFocus(e);
986 e->setTextList(items, is_yes(str_transparent));
988 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
989 e->setDynamicData(data->table_dyndata[name]);
992 if (!str_initial_selection.empty() && str_initial_selection != "0")
993 e->setSelected(stoi(str_initial_selection));
995 auto style = getStyleForElement("textlist", name);
996 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
998 m_tables.emplace_back(spec, e);
999 m_fields.push_back(spec);
1002 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
1006 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
1008 std::vector<std::string> parts = split(element,';');
1010 if ((parts.size() == 5) ||
1011 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1013 std::vector<std::string> v_pos = split(parts[0],',');
1014 std::string name = parts[2];
1015 std::vector<std::string> items = split(parts[3],',');
1016 std::string str_initial_selection;
1017 str_initial_selection = parts[4];
1019 MY_CHECKPOS("dropdown",0);
1023 core::rect<s32> rect;
1025 if (data->real_coordinates) {
1026 std::vector<std::string> v_geom = split(parts[1],',');
1028 if (v_geom.size() == 1)
1029 v_geom.emplace_back("1");
1031 MY_CHECKGEOM("dropdown",1);
1033 pos = getRealCoordinateBasePos(false, v_pos);
1034 geom = getRealCoordinateGeometry(v_geom);
1035 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1037 pos = getElementBasePos(false, &v_pos);
1039 s32 width = stof(parts[1]) * spacing.Y;
1041 rect = core::rect<s32>(pos.X, pos.Y,
1042 pos.X + width, pos.Y + (m_btn_height * 2));
1052 spec.ftype = f_DropDown;
1055 //now really show list
1056 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
1058 if (spec.fname == data->focused_fieldname) {
1059 Environment->setFocus(e);
1062 for (const std::string &item : items) {
1063 e->addItem(unescape_translate(unescape_string(
1064 utf8_to_wide(item))).c_str());
1067 if (!str_initial_selection.empty())
1068 e->setSelected(stoi(str_initial_selection)-1);
1070 auto style = getStyleForElement("dropdown", name);
1071 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1073 m_fields.push_back(spec);
1075 m_dropdowns.emplace_back(spec, std::vector<std::string>());
1076 std::vector<std::string> &values = m_dropdowns.back().second;
1077 for (const std::string &item : items) {
1078 values.push_back(unescape_string(item));
1083 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
1084 << element << "'" << std::endl;
1087 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
1089 std::vector<std::string> parts = split(element,';');
1090 if (parts.size() == 2 ||
1091 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
1092 field_close_on_enter[parts[0]] = is_yes(parts[1]);
1096 void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
1098 std::vector<std::string> parts = split(element,';');
1100 if ((parts.size() == 4) || (parts.size() == 5) ||
1101 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1103 std::vector<std::string> v_pos = split(parts[0],',');
1104 std::vector<std::string> v_geom = split(parts[1],',');
1105 std::string name = parts[2];
1106 std::string label = parts[3];
1108 MY_CHECKPOS("pwdfield",0);
1109 MY_CHECKGEOM("pwdfield",1);
1114 if (data->real_coordinates) {
1115 pos = getRealCoordinateBasePos(false, v_pos);
1116 geom = getRealCoordinateGeometry(v_geom);
1118 pos = getElementBasePos(false, &v_pos);
1121 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1123 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1124 pos.Y -= m_btn_height;
1125 geom.Y = m_btn_height*2;
1128 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1130 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1140 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
1142 if (spec.fname == data->focused_fieldname) {
1143 Environment->setFocus(e);
1146 if (label.length() >= 1) {
1147 int font_height = g_fontengine->getTextHeight();
1148 rect.UpperLeftCorner.Y -= font_height;
1149 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1150 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1154 e->setPasswordBox(true,L'*');
1156 auto style = getStyleForElement("pwdfield", name, "field");
1157 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1158 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1159 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1162 evt.EventType = EET_KEY_INPUT_EVENT;
1163 evt.KeyInput.Key = KEY_END;
1164 evt.KeyInput.Char = 0;
1165 evt.KeyInput.Control = false;
1166 evt.KeyInput.Shift = false;
1167 evt.KeyInput.PressedDown = true;
1170 if (parts.size() >= 5) {
1171 // TODO: remove after 2016-11-03
1172 warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
1173 " instead of the 5th param" << std::endl;
1174 field_close_on_enter[name] = is_yes(parts[4]);
1177 m_fields.push_back(spec);
1180 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
1183 void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
1184 core::rect<s32> &rect, bool is_multiline)
1186 bool is_editable = !spec.fname.empty();
1187 if (!is_editable && !is_multiline) {
1188 // spec field id to 0, this stops submit searching for a value that isn't there
1189 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1196 } else if (is_multiline &&
1197 spec.fdefault.empty() && !spec.flabel.empty()) {
1198 // Multiline textareas: swap default and label for backwards compat
1199 spec.flabel.swap(spec.fdefault);
1202 gui::IGUIEditBox *e = nullptr;
1203 static constexpr bool use_intl_edit_box = USE_FREETYPE &&
1204 IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9;
1206 if (use_intl_edit_box && g_settings->getBool("freetype")) {
1207 e = new gui::intlGUIEditBox(spec.fdefault.c_str(),
1208 true, Environment, this, spec.fid, rect, is_editable, is_multiline);
1212 e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true,
1213 Environment, this, spec.fid, rect, is_editable, true);
1214 else if (is_editable)
1215 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
1220 if (is_editable && spec.fname == data->focused_fieldname)
1221 Environment->setFocus(e);
1224 e->setMultiLine(true);
1225 e->setWordWrap(true);
1226 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1229 evt.EventType = EET_KEY_INPUT_EVENT;
1230 evt.KeyInput.Key = KEY_END;
1231 evt.KeyInput.Char = 0;
1232 evt.KeyInput.Control = 0;
1233 evt.KeyInput.Shift = 0;
1234 evt.KeyInput.PressedDown = true;
1238 auto style = getStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
1239 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1240 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1241 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1242 if (style.get(StyleSpec::BGCOLOR, "") == "transparent") {
1243 e->setDrawBackground(false);
1247 if (!spec.flabel.empty()) {
1248 int font_height = g_fontengine->getTextHeight();
1249 rect.UpperLeftCorner.Y -= font_height;
1250 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1251 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1256 void GUIFormSpecMenu::parseSimpleField(parserData* data,
1257 std::vector<std::string> &parts)
1259 std::string name = parts[0];
1260 std::string label = parts[1];
1261 std::string default_val = parts[2];
1263 core::rect<s32> rect;
1265 if(data->explicit_size)
1266 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
1268 v2s32 pos = getElementBasePos(false, nullptr);
1269 pos.Y = ((m_fields.size()+2)*60);
1270 v2s32 size = DesiredRect.getSize();
1272 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
1273 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
1277 default_val = m_form_src->resolveText(default_val);
1280 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1285 utf8_to_wide(unescape_string(default_val)),
1289 createTextField(data, spec, rect, false);
1291 if (parts.size() >= 4) {
1292 // TODO: remove after 2016-11-03
1293 warningstream << "field/simple: use field_close_on_enter[name, enabled]" <<
1294 " instead of the 4th param" << std::endl;
1295 field_close_on_enter[name] = is_yes(parts[3]);
1298 m_fields.push_back(spec);
1301 void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
1302 const std::string &type)
1305 std::vector<std::string> v_pos = split(parts[0],',');
1306 std::vector<std::string> v_geom = split(parts[1],',');
1307 std::string name = parts[2];
1308 std::string label = parts[3];
1309 std::string default_val = parts[4];
1311 MY_CHECKPOS(type,0);
1312 MY_CHECKGEOM(type,1);
1317 if (data->real_coordinates) {
1318 pos = getRealCoordinateBasePos(false, v_pos);
1319 geom = getRealCoordinateGeometry(v_geom);
1321 pos = getElementBasePos(false, &v_pos);
1324 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1326 if (type == "textarea")
1328 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1329 pos.Y += m_btn_height;
1333 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1334 pos.Y -= m_btn_height;
1335 geom.Y = m_btn_height*2;
1339 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1341 if(!data->explicit_size)
1342 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1345 default_val = m_form_src->resolveText(default_val);
1348 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1353 utf8_to_wide(unescape_string(default_val)),
1357 createTextField(data, spec, rect, type == "textarea");
1359 if (parts.size() >= 6) {
1360 // TODO: remove after 2016-11-03
1361 warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
1362 " instead of the 6th param" << std::endl;
1363 field_close_on_enter[name] = is_yes(parts[5]);
1366 m_fields.push_back(spec);
1369 void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
1370 const std::string &type)
1372 std::vector<std::string> parts = split(element,';');
1374 if (parts.size() == 3 || parts.size() == 4) {
1375 parseSimpleField(data,parts);
1379 if ((parts.size() == 5) || (parts.size() == 6) ||
1380 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1382 parseTextArea(data,parts,type);
1385 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1388 void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
1390 std::vector<std::string> parts = split(element,';');
1392 if ((parts.size() == 2) ||
1393 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1395 std::vector<std::string> v_pos = split(parts[0],',');
1396 std::string text = parts[1];
1398 MY_CHECKPOS("label",0);
1400 if(!data->explicit_size)
1401 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1403 std::vector<std::string> lines = split(text, '\n');
1405 for (unsigned int i = 0; i != lines.size(); i++) {
1406 std::wstring wlabel = unescape_translate(unescape_string(
1407 utf8_to_wide(lines[i])));
1409 core::rect<s32> rect;
1411 if (data->real_coordinates) {
1412 // Lines are spaced at the distance of 1/2 imgsize.
1413 // This alows lines that line up with the new elements
1414 // easily without sacrificing good line distance. If
1415 // it was one whole imgsize, it would have too much
1417 v2s32 pos = getRealCoordinateBasePos(false, v_pos);
1419 // Labels are positioned by their center, not their top.
1420 pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2);
1422 rect = core::rect<s32>(
1424 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1428 // Lines are spaced at the nominal distance of
1429 // 2/5 inventory slot, even if the font doesn't
1430 // quite match that. This provides consistent
1431 // form layout, at the expense of sometimes
1432 // having sub-optimal spacing for the font.
1433 // We multiply by 2 and then divide by 5, rather
1434 // than multiply by 0.4, to get exact results
1435 // in the integer cases: 0.4 is not exactly
1436 // representable in binary floating point.
1438 v2s32 pos = getElementBasePos(false, nullptr);
1439 pos.X += stof(v_pos[0]) * spacing.X;
1440 pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
1442 pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0;
1444 rect = core::rect<s32>(
1445 pos.X, pos.Y - m_btn_height,
1446 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1447 pos.Y + m_btn_height);
1456 gui::IGUIStaticText *e = gui::StaticText::add(Environment,
1457 spec.flabel.c_str(), rect, false, false, this, spec.fid);
1458 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
1460 auto style = getStyleForElement("label", spec.fname);
1461 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1462 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1464 m_fields.push_back(spec);
1469 errorstream << "Invalid label element(" << parts.size() << "): '" << element
1470 << "'" << std::endl;
1473 void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
1475 std::vector<std::string> parts = split(element,';');
1477 if ((parts.size() == 2) ||
1478 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1480 std::vector<std::string> v_pos = split(parts[0],',');
1481 std::wstring text = unescape_translate(
1482 unescape_string(utf8_to_wide(parts[1])));
1484 MY_CHECKPOS("vertlabel",1);
1487 core::rect<s32> rect;
1489 if (data->real_coordinates) {
1490 pos = getRealCoordinateBasePos(false, v_pos);
1492 // Vertlabels are positioned by center, not left.
1493 pos.X -= imgsize.X / 2;
1495 // We use text.length + 1 because without it, the rect
1496 // isn't quite tall enough and cuts off the text.
1497 rect = core::rect<s32>(pos.X, pos.Y,
1499 pos.Y + font_line_height(m_font) *
1500 (text.length() + 1));
1503 pos = getElementBasePos(false, &v_pos);
1505 // As above, the length must be one longer. The width of
1506 // the rect (15 pixels) seems rather arbitrary, but
1507 // changing it might break something.
1508 rect = core::rect<s32>(
1509 pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
1511 font_line_height(m_font) *
1512 (text.length() + 1) +
1513 ((imgsize.Y/2) - m_btn_height));
1516 if(!data->explicit_size)
1517 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1521 for (wchar_t i : text) {
1532 gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
1533 rect, false, false, this, spec.fid);
1534 e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1536 auto style = getStyleForElement("vertlabel", spec.fname, "label");
1537 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1538 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1540 m_fields.push_back(spec);
1543 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1546 void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
1547 const std::string &type)
1549 std::vector<std::string> parts = split(element,';');
1551 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1552 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1554 std::vector<std::string> v_pos = split(parts[0],',');
1555 std::vector<std::string> v_geom = split(parts[1],',');
1556 std::string image_name = parts[2];
1557 std::string name = parts[3];
1558 std::string label = parts[4];
1560 MY_CHECKPOS("imagebutton",0);
1561 MY_CHECKGEOM("imagebutton",1);
1563 bool noclip = false;
1564 bool drawborder = true;
1565 std::string pressed_image_name;
1567 if (parts.size() >= 7) {
1568 if (parts[5] == "true")
1570 if (parts[6] == "false")
1574 if (parts.size() >= 8) {
1575 pressed_image_name = parts[7];
1581 if (data->real_coordinates) {
1582 pos = getRealCoordinateBasePos(false, v_pos);
1583 geom = getRealCoordinateGeometry(v_geom);
1585 pos = getElementBasePos(false, &v_pos);
1586 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1587 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1590 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1593 if (!data->explicit_size)
1594 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1596 image_name = unescape_string(image_name);
1597 pressed_image_name = unescape_string(pressed_image_name);
1599 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1604 utf8_to_wide(image_name),
1607 spec.ftype = f_Button;
1608 if (type == "image_button_exit")
1609 spec.is_exit = true;
1611 video::ITexture *texture = 0;
1612 video::ITexture *pressed_texture = 0;
1613 texture = m_tsrc->getTexture(image_name);
1614 if (!pressed_image_name.empty())
1615 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1617 pressed_texture = texture;
1619 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1621 if (spec.fname == data->focused_fieldname) {
1622 Environment->setFocus(e);
1625 auto style = getStyleForElement("image_button", spec.fname);
1627 e->setUseAlphaChannel(style.getBool(StyleSpec::ALPHA, true));
1628 e->setImage(guiScalingImageButton(
1629 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1630 e->setPressedImage(guiScalingImageButton(
1631 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1632 e->setScaleImage(true);
1633 if (parts.size() >= 7) {
1634 e->setNotClipped(noclip);
1635 e->setDrawBorder(drawborder);
1637 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1638 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1641 m_fields.push_back(spec);
1645 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1648 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
1650 std::vector<std::string> parts = split(element, ';');
1652 if (((parts.size() == 4) || (parts.size() == 6)) || (parts.size() == 7 &&
1653 data->real_coordinates) || ((parts.size() > 6) &&
1654 (m_formspec_version > FORMSPEC_API_VERSION)))
1656 std::vector<std::string> v_pos = split(parts[0],',');
1658 // If we're using real coordinates, add an extra field for height.
1659 // Width is not here because tabs are the width of the text, and
1660 // there's no reason to change that.
1662 std::vector<std::string> v_geom = {"1", "0.75"}; // Dummy width and default height
1663 bool auto_width = true;
1664 if (parts.size() == 7) {
1667 v_geom = split(parts[1], ',');
1668 if (v_geom.size() == 1)
1669 v_geom.insert(v_geom.begin(), "1"); // Dummy value
1674 std::string name = parts[i+1];
1675 std::vector<std::string> buttons = split(parts[i+2], ',');
1676 std::string str_index = parts[i+3];
1677 bool show_background = true;
1678 bool show_border = true;
1679 int tab_index = stoi(str_index) - 1;
1681 MY_CHECKPOS("tabheader", 0);
1683 if (parts.size() == 6 + i) {
1684 if (parts[4+i] == "true")
1685 show_background = false;
1686 if (parts[5+i] == "false")
1687 show_border = false;
1697 spec.ftype = f_TabHeader;
1702 if (data->real_coordinates) {
1703 pos = getRealCoordinateBasePos(false, v_pos);
1705 geom = getRealCoordinateGeometry(v_geom);
1706 pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
1708 geom.X = DesiredRect.getWidth(); // Set automatic width
1710 MY_CHECKGEOM("tabheader", 1);
1712 v2f32 pos_f = pos_offset * spacing;
1713 pos_f.X += stof(v_pos[0]) * spacing.X;
1714 pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
1715 pos = v2s32(pos_f.X, pos_f.Y);
1717 geom.Y = m_btn_height * 2;
1718 geom.X = DesiredRect.getWidth();
1721 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1724 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1725 show_background, show_border, spec.fid);
1726 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1727 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1728 e->setTabHeight(geom.Y);
1730 if (spec.fname == data->focused_fieldname) {
1731 Environment->setFocus(e);
1734 auto style = getStyleForElement("tabheader", name);
1735 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
1737 for (const std::string &button : buttons) {
1738 auto tab = e->addTab(unescape_translate(unescape_string(
1739 utf8_to_wide(button))).c_str(), -1);
1740 if (style.isNotDefault(StyleSpec::BGCOLOR))
1741 tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
1743 tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1746 if ((tab_index >= 0) &&
1747 (buttons.size() < INT_MAX) &&
1748 (tab_index < (int) buttons.size()))
1749 e->setActiveTab(tab_index);
1751 m_fields.push_back(spec);
1754 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1755 << element << "'" << std::endl;
1758 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
1761 if (m_client == 0) {
1762 warningstream << "invalid use of item_image_button with m_client==0"
1767 std::vector<std::string> parts = split(element,';');
1769 if ((parts.size() == 5) ||
1770 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1772 std::vector<std::string> v_pos = split(parts[0],',');
1773 std::vector<std::string> v_geom = split(parts[1],',');
1774 std::string item_name = parts[2];
1775 std::string name = parts[3];
1776 std::string label = parts[4];
1778 label = unescape_string(label);
1779 item_name = unescape_string(item_name);
1781 MY_CHECKPOS("itemimagebutton",0);
1782 MY_CHECKGEOM("itemimagebutton",1);
1787 if (data->real_coordinates) {
1788 pos = getRealCoordinateBasePos(false, v_pos);
1789 geom = getRealCoordinateGeometry(v_geom);
1791 pos = getElementBasePos(false, &v_pos);
1792 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1793 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1796 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1798 if(!data->explicit_size)
1799 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1801 IItemDefManager *idef = m_client->idef();
1803 item.deSerialize(item_name, idef);
1806 TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
1807 m_default_tooltip_bgcolor,
1808 m_default_tooltip_color);
1812 utf8_to_wide(label),
1813 utf8_to_wide(item_name),
1814 258 + m_fields.size()
1817 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1819 auto style = getStyleForElement("item_image_button", spec.fname, "image_button");
1820 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1821 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1823 if (spec.fname == data->focused_fieldname) {
1824 Environment->setFocus(e);
1827 spec.ftype = f_Button;
1828 rect+=data->basepos-padding;
1830 m_fields.push_back(spec);
1832 if (data->real_coordinates)
1833 pos = getRealCoordinateBasePos(true, v_pos);
1835 pos = getElementBasePos(true, &v_pos);
1837 m_itemimages.emplace_back("", item_name, e, pos, geom);
1838 m_static_texts.emplace_back(utf8_to_wide(label), rect, e);
1841 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1844 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
1846 std::vector<std::string> parts = split(element,';');
1848 if ((parts.size() == 3) ||
1849 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1851 std::vector<std::string> v_pos = split(parts[0],',');
1852 std::vector<std::string> v_geom = split(parts[1],',');
1854 MY_CHECKPOS("box",0);
1855 MY_CHECKGEOM("box",1);
1860 if (data->real_coordinates) {
1861 pos = getRealCoordinateBasePos(true, v_pos);
1862 geom = getRealCoordinateGeometry(v_geom);
1864 pos = getElementBasePos(true, &v_pos);
1865 geom.X = stof(v_geom[0]) * spacing.X;
1866 geom.Y = stof(v_geom[1]) * spacing.Y;
1869 video::SColor tmp_color;
1871 if (parseColorString(parts[2], tmp_color, false, 0x8C)) {
1872 BoxDrawSpec spec(pos, geom, tmp_color);
1874 m_boxes.push_back(spec);
1877 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1881 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1884 void GUIFormSpecMenu::parseBackgroundColor(parserData *data, const std::string &element)
1886 std::vector<std::string> parts = split(element,';');
1888 if (((parts.size() == 1) || (parts.size() == 2)) ||
1889 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION))) {
1890 if (parts.size() == 1) {
1891 parseColorString(parts[0], m_bgcolor, false);
1892 } else if (parts.size() == 2) {
1893 parseColorString(parts[0], m_fullscreen_bgcolor, false);
1894 m_bgfullscreen = is_yes(parts[1]);
1900 errorstream << "Invalid bgcolor element(" << parts.size() << "): '" << element << "'"
1904 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
1906 std::vector<std::string> parts = split(element,';');
1908 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1909 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1911 parseColorString(parts[0], m_slotbg_n, false);
1912 parseColorString(parts[1], m_slotbg_h, false);
1914 if (parts.size() >= 3) {
1915 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1916 m_slotborder = true;
1919 if (parts.size() == 5) {
1920 video::SColor tmp_color;
1922 if (parseColorString(parts[3], tmp_color, false))
1923 m_default_tooltip_bgcolor = tmp_color;
1924 if (parseColorString(parts[4], tmp_color, false))
1925 m_default_tooltip_color = tmp_color;
1929 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1932 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
1934 std::vector<std::string> parts = split(element,';');
1935 if (parts.size() < 2) {
1936 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
1937 << element << "'" << std::endl;
1941 // Get mode and check size
1942 bool rect_mode = parts[0].find(',') != std::string::npos;
1943 size_t base_size = rect_mode ? 3 : 2;
1944 if (parts.size() != base_size && parts.size() != base_size + 2) {
1945 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
1946 << element << "'" << std::endl;
1951 video::SColor bgcolor = m_default_tooltip_bgcolor;
1952 video::SColor color = m_default_tooltip_color;
1953 if (parts.size() == base_size + 2 &&
1954 (!parseColorString(parts[base_size], bgcolor, false) ||
1955 !parseColorString(parts[base_size + 1], color, false))) {
1956 errorstream << "Invalid color in tooltip element(" << parts.size()
1957 << "): '" << element << "'" << std::endl;
1961 // Make tooltip spec
1962 std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
1963 TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
1967 std::vector<std::string> v_pos = split(parts[0], ',');
1968 std::vector<std::string> v_geom = split(parts[1], ',');
1970 MY_CHECKPOS("tooltip", 0);
1971 MY_CHECKGEOM("tooltip", 1);
1976 if (data->real_coordinates) {
1977 pos = getRealCoordinateBasePos(true, v_pos);
1978 geom = getRealCoordinateGeometry(v_geom);
1980 pos = getElementBasePos(true, &v_pos);
1981 geom.X = stof(v_geom[0]) * spacing.X;
1982 geom.Y = stof(v_geom[1]) * spacing.Y;
1985 irr::core::rect<s32> rect(pos, pos + geom);
1986 m_tooltip_rects.emplace_back(rect, spec);
1988 m_tooltips[parts[0]] = spec;
1992 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
1998 std::vector<std::string> parts = split(data,'[');
2000 if (parts.size() < 2) {
2004 if (parts[0] != "formspec_version") {
2008 if (is_number(parts[1])) {
2009 m_formspec_version = mystoi(parts[1]);
2016 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
2018 if (element.empty())
2021 std::vector<std::string> parts = split(element,'[');
2023 if (parts.size() < 2)
2026 std::string type = trim(parts[0]);
2027 std::string description = trim(parts[1]);
2029 if (type != "size" && type != "invsize")
2032 if (type == "invsize")
2033 log_deprecated("Deprecated formspec element \"invsize\" is used");
2035 parseSize(data, description);
2040 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
2042 if (element.empty())
2045 std::vector<std::string> parts = split(element, '[');
2047 if (parts.size() != 2)
2050 std::string type = trim(parts[0]);
2051 std::string description = trim(parts[1]);
2053 if (type != "position")
2056 parsePosition(data, description);
2061 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
2063 std::vector<std::string> parts = split(element, ',');
2065 if (parts.size() == 2) {
2066 data->offset.X = stof(parts[0]);
2067 data->offset.Y = stof(parts[1]);
2071 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
2074 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
2076 if (element.empty())
2079 std::vector<std::string> parts = split(element, '[');
2081 if (parts.size() != 2)
2084 std::string type = trim(parts[0]);
2085 std::string description = trim(parts[1]);
2087 if (type != "anchor")
2090 parseAnchor(data, description);
2095 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
2097 std::vector<std::string> parts = split(element, ',');
2099 if (parts.size() == 2) {
2100 data->anchor.X = stof(parts[0]);
2101 data->anchor.Y = stof(parts[1]);
2105 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
2106 << "'" << std::endl;
2109 bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type)
2111 std::vector<std::string> parts = split(element, ';');
2113 if (parts.size() < 2) {
2114 errorstream << "Invalid style element (" << parts.size() << "): '" << element
2115 << "'" << std::endl;
2119 std::string selector = trim(parts[0]);
2120 if (selector.empty()) {
2121 errorstream << "Invalid style element (Selector required): '" << element
2122 << "'" << std::endl;
2128 for (size_t i = 1; i < parts.size(); i++) {
2129 size_t equal_pos = parts[i].find('=');
2130 if (equal_pos == std::string::npos) {
2131 errorstream << "Invalid style element (Property missing value): '" << element
2132 << "'" << std::endl;
2136 std::string propname = trim(parts[i].substr(0, equal_pos));
2137 std::string value = trim(unescape_string(parts[i].substr(equal_pos + 1)));
2139 std::transform(propname.begin(), propname.end(), propname.begin(), ::tolower);
2141 StyleSpec::Property prop = StyleSpec::GetPropertyByName(propname);
2142 if (prop == StyleSpec::NONE) {
2143 if (property_warned.find(propname) != property_warned.end()) {
2144 warningstream << "Invalid style element (Unknown property " << propname << "): '"
2146 << "'" << std::endl;
2147 property_warned.insert(propname);
2152 spec.set(prop, value);
2156 theme_by_type[selector] |= spec;
2158 theme_by_name[selector] |= spec;
2164 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
2167 if (element.empty())
2170 if (parseVersionDirect(element))
2173 std::vector<std::string> parts = split(element,'[');
2175 // ugly workaround to keep compatibility
2176 if (parts.size() > 2) {
2177 if (trim(parts[0]) == "image") {
2178 for (unsigned int i=2;i< parts.size(); i++) {
2179 parts[1] += "[" + parts[i];
2185 if (parts.size() < 2) {
2189 std::string type = trim(parts[0]);
2190 std::string description = trim(parts[1]);
2192 if (type == "container") {
2193 parseContainer(data, description);
2197 if (type == "container_end") {
2198 parseContainerEnd(data);
2202 if (type == "list") {
2203 parseList(data, description);
2207 if (type == "listring") {
2208 parseListRing(data, description);
2212 if (type == "checkbox") {
2213 parseCheckbox(data, description);
2217 if (type == "image") {
2218 parseImage(data, description);
2222 if (type == "item_image") {
2223 parseItemImage(data, description);
2227 if (type == "button" || type == "button_exit") {
2228 parseButton(data, description, type);
2232 if (type == "background") {
2233 parseBackground(data,description);
2237 if (type == "tableoptions"){
2238 parseTableOptions(data,description);
2242 if (type == "tablecolumns"){
2243 parseTableColumns(data,description);
2247 if (type == "table"){
2248 parseTable(data,description);
2252 if (type == "textlist"){
2253 parseTextList(data,description);
2257 if (type == "dropdown"){
2258 parseDropDown(data,description);
2262 if (type == "field_close_on_enter") {
2263 parseFieldCloseOnEnter(data, description);
2267 if (type == "pwdfield") {
2268 parsePwdField(data,description);
2272 if ((type == "field") || (type == "textarea")){
2273 parseField(data,description,type);
2277 if (type == "label") {
2278 parseLabel(data,description);
2282 if (type == "vertlabel") {
2283 parseVertLabel(data,description);
2287 if (type == "item_image_button") {
2288 parseItemImageButton(data,description);
2292 if ((type == "image_button") || (type == "image_button_exit")) {
2293 parseImageButton(data,description,type);
2297 if (type == "tabheader") {
2298 parseTabHeader(data,description);
2302 if (type == "box") {
2303 parseBox(data,description);
2307 if (type == "bgcolor") {
2308 parseBackgroundColor(data,description);
2312 if (type == "listcolors") {
2313 parseListColors(data,description);
2317 if (type == "tooltip") {
2318 parseTooltip(data,description);
2322 if (type == "scrollbar") {
2323 parseScrollBar(data, description);
2327 if (type == "real_coordinates") {
2328 data->real_coordinates = is_yes(description);
2332 if (type == "style") {
2333 parseStyle(data, description, false);
2337 if (type == "style_type") {
2338 parseStyle(data, description, true);
2343 infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
2347 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
2349 /* useless to regenerate without a screensize */
2350 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
2357 for (auto &m_table : m_tables) {
2358 std::string tablename = m_table.first.fname;
2359 GUITable *table = m_table.second;
2360 mydata.table_dyndata[tablename] = table->getDynamicData();
2364 if (!m_focused_element.empty())
2365 mydata.focused_fieldname = m_focused_element;
2368 gui::IGUIElement *focused_element = Environment->getFocus();
2369 if (focused_element && focused_element->getParent() == this) {
2370 s32 focused_id = focused_element->getID();
2371 if (focused_id > 257) {
2372 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
2373 if (field.fid == focused_id) {
2374 mydata.focused_fieldname = field.fname;
2384 for (auto &table_it : m_tables) {
2385 table_it.second->drop();
2388 mydata.size= v2s32(100,100);
2389 mydata.screensize = screensize;
2390 mydata.offset = v2f32(0.5f, 0.5f);
2391 mydata.anchor = v2f32(0.5f, 0.5f);
2393 // Base position of contents of form
2394 mydata.basepos = getBasePos();
2396 /* Convert m_init_draw_spec to m_inventorylists */
2398 m_inventorylists.clear();
2400 m_backgrounds.clear();
2401 m_itemimages.clear();
2403 m_checkboxes.clear();
2404 m_scrollbars.clear();
2408 m_tooltip_rects.clear();
2409 m_inventory_rings.clear();
2410 m_static_texts.clear();
2411 m_dropdowns.clear();
2412 theme_by_name.clear();
2413 theme_by_type.clear();
2415 m_bgfullscreen = false;
2418 v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
2419 m_bgcolor = video::SColor(
2420 (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
2421 clamp_u8(myround(formspec_bgcolor.X)),
2422 clamp_u8(myround(formspec_bgcolor.Y)),
2423 clamp_u8(myround(formspec_bgcolor.Z))
2428 v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
2429 m_fullscreen_bgcolor = video::SColor(
2430 (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
2431 clamp_u8(myround(formspec_bgcolor.X)),
2432 clamp_u8(myround(formspec_bgcolor.Y)),
2433 clamp_u8(myround(formspec_bgcolor.Z))
2437 m_slotbg_n = video::SColor(255,128,128,128);
2438 m_slotbg_h = video::SColor(255,192,192,192);
2440 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
2441 m_default_tooltip_color = video::SColor(255,255,255,255);
2443 m_slotbordercolor = video::SColor(200,0,0,0);
2444 m_slotborder = false;
2448 assert(!m_tooltip_element);
2449 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
2450 m_tooltip_element = gui::StaticText::add(Environment, L"",
2451 core::rect<s32>(0, 0, 110, 18));
2452 m_tooltip_element->enableOverrideColor(true);
2453 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2454 m_tooltip_element->setDrawBackground(true);
2455 m_tooltip_element->setDrawBorder(true);
2456 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2457 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
2458 m_tooltip_element->setWordWrap(false);
2459 //we're not parent so no autograb for this one!
2460 m_tooltip_element->grab();
2463 std::vector<std::string> elements = split(m_formspec_string,']');
2466 /* try to read version from first element only */
2467 if (!elements.empty()) {
2468 if (parseVersionDirect(elements[0])) {
2473 /* we need size first in order to calculate image scale */
2474 mydata.explicit_size = false;
2475 for (; i< elements.size(); i++) {
2476 if (!parseSizeDirect(&mydata, elements[i])) {
2481 /* "position" element is always after "size" element if it used */
2482 for (; i< elements.size(); i++) {
2483 if (!parsePositionDirect(&mydata, elements[i])) {
2488 /* "anchor" element is always after "position" (or "size" element) if it used */
2489 for (; i< elements.size(); i++) {
2490 if (!parseAnchorDirect(&mydata, elements[i])) {
2495 /* "no_prepend" element is always after "position" (or "size" element) if it used */
2496 bool enable_prepends = true;
2497 for (; i < elements.size(); i++) {
2498 if (elements[i].empty())
2501 std::vector<std::string> parts = split(elements[i], '[');
2502 if (trim(parts[0]) == "no_prepend")
2503 enable_prepends = false;
2508 /* Copy of the "real_coordinates" element for after the form size. */
2509 mydata.real_coordinates = m_formspec_version >= 2;
2510 for (; i < elements.size(); i++) {
2511 std::vector<std::string> parts = split(elements[i], '[');
2512 std::string name = trim(parts[0]);
2513 if (name != "real_coordinates" || parts.size() != 2)
2514 break; // Invalid format
2516 mydata.real_coordinates = is_yes(trim(parts[1]));
2519 if (mydata.explicit_size) {
2520 // compute scaling for specified form size
2522 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
2523 v2u32 delta = current_screensize - m_lockscreensize;
2525 if (current_screensize.Y > m_lockscreensize.Y)
2530 if (current_screensize.X > m_lockscreensize.X)
2535 offset = v2s32(delta.X,delta.Y);
2537 mydata.screensize = m_lockscreensize;
2539 offset = v2s32(0,0);
2542 double gui_scaling = g_settings->getFloat("gui_scaling");
2543 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2547 // In fixed-size mode, inventory image size
2548 // is 0.53 inch multiplied by the gui_scaling
2549 // config parameter. This magic size is chosen
2550 // to make the main menu (15.5 inventory images
2551 // wide, including border) just fit into the
2552 // default window (800 pixels wide) at 96 DPI
2553 // and default scaling (1.00).
2554 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
2556 // In variable-size mode, we prefer to make the
2557 // inventory image size 1/15 of screen height,
2558 // multiplied by the gui_scaling config parameter.
2559 // If the preferred size won't fit the whole
2560 // form on the screen, either horizontally or
2561 // vertically, then we scale it down to fit.
2562 // (The magic numbers in the computation of what
2563 // fits arise from the scaling factors in the
2564 // following stanza, including the form border,
2565 // help text space, and 0.1 inventory slot spare.)
2566 // However, a minimum size is also set, that
2567 // the image size can't be less than 0.3 inch
2568 // multiplied by gui_scaling, even if this means
2569 // the form doesn't fit the screen.
2571 // For mobile devices these magic numbers are
2572 // different and forms should always use the
2573 // maximum screen space available.
2574 double prefer_imgsize = mydata.screensize.Y / 10 * gui_scaling;
2575 double fitx_imgsize = mydata.screensize.X /
2576 ((12.0 / 8.0) * (0.5 + mydata.invsize.X));
2577 double fity_imgsize = mydata.screensize.Y /
2578 ((15.0 / 11.0) * (0.85 + mydata.invsize.Y));
2579 use_imgsize = MYMIN(prefer_imgsize,
2580 MYMIN(fitx_imgsize, fity_imgsize));
2582 double prefer_imgsize = mydata.screensize.Y / 15 * gui_scaling;
2583 double fitx_imgsize = mydata.screensize.X /
2584 ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
2585 double fity_imgsize = mydata.screensize.Y /
2586 ((15.0 / 13.0) * (0.85 * mydata.invsize.Y));
2587 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2588 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
2589 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2590 MYMIN(fitx_imgsize, fity_imgsize)));
2594 // Everything else is scaled in proportion to the
2595 // inventory image size. The inventory slot spacing
2596 // is 5/4 image size horizontally and 15/13 image size
2597 // vertically. The padding around the form (incorporating
2598 // the border of the outer inventory slots) is 3/8
2599 // image size. Font height (baseline to baseline)
2600 // is 2/5 vertical inventory slot spacing, and button
2601 // half-height is 7/8 of font height.
2602 imgsize = v2s32(use_imgsize, use_imgsize);
2603 spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2604 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2605 m_btn_height = use_imgsize*15.0/13 * 0.35;
2607 m_font = g_fontengine->getFont();
2609 if (mydata.real_coordinates) {
2610 mydata.size = v2s32(
2611 mydata.invsize.X*imgsize.X,
2612 mydata.invsize.Y*imgsize.Y
2615 mydata.size = v2s32(
2616 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2617 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2621 DesiredRect = mydata.rect = core::rect<s32>(
2622 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
2623 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
2624 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
2625 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
2628 // Non-size[] form must consist only of text fields and
2629 // implicit "Proceed" button. Use default font, and
2630 // temporary form size which will be recalculated below.
2631 m_font = g_fontengine->getFont();
2632 m_btn_height = font_line_height(m_font) * 0.875;
2633 DesiredRect = core::rect<s32>(
2634 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
2635 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
2636 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
2637 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
2640 recalculateAbsolutePosition(false);
2641 mydata.basepos = getBasePos();
2642 m_tooltip_element->setOverrideFont(m_font);
2644 gui::IGUISkin *skin = Environment->getSkin();
2646 gui::IGUIFont *old_font = skin->getFont();
2647 skin->setFont(m_font);
2649 pos_offset = v2f32();
2651 if (enable_prepends) {
2652 // Backup the coordinates so that prepends can use the coordinates of choice.
2653 bool rc_backup = mydata.real_coordinates;
2654 u16 version_backup = m_formspec_version;
2655 mydata.real_coordinates = false; // Old coordinates by default.
2657 std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
2658 for (const auto &element : prepend_elements)
2659 parseElement(&mydata, element);
2661 m_formspec_version = version_backup;
2662 mydata.real_coordinates = rc_backup; // Restore coordinates
2665 for (; i< elements.size(); i++) {
2666 parseElement(&mydata, elements[i]);
2669 if (!container_stack.empty()) {
2670 errorstream << "Invalid formspec string: container was never closed!"
2674 // If there are fields without explicit size[], add a "Proceed"
2675 // button and adjust size to fit all the fields.
2676 if (!m_fields.empty() && !mydata.explicit_size) {
2677 mydata.rect = core::rect<s32>(
2678 mydata.screensize.X/2 - 580/2,
2679 mydata.screensize.Y/2 - 300/2,
2680 mydata.screensize.X/2 + 580/2,
2681 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2683 DesiredRect = mydata.rect;
2684 recalculateAbsolutePosition(false);
2685 mydata.basepos = getBasePos();
2688 v2s32 pos = mydata.basepos;
2689 pos.Y = ((m_fields.size()+2)*60);
2691 v2s32 size = DesiredRect.getSize();
2693 core::rect<s32>(size.X/2-70, pos.Y,
2694 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2695 const wchar_t *text = wgettext("Proceed");
2696 Environment->addButton(mydata.rect, this, 257, text);
2702 //set initial focus if parser didn't set it
2703 focused_element = Environment->getFocus();
2704 if (!focused_element
2705 || !isMyChild(focused_element)
2706 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2709 skin->setFont(old_font);
2713 bool GUIFormSpecMenu::getAndroidUIInput()
2715 if (!hasAndroidUIInput())
2718 std::string fieldname = m_jni_field_name;
2719 m_jni_field_name.clear();
2721 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2722 iter != m_fields.end(); ++iter) {
2724 if (iter->fname != fieldname) {
2727 IGUIElement* tochange = getElementFromId(iter->fid);
2729 if (tochange == 0) {
2733 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2737 std::string text = porting::getInputDialogValue();
2739 ((gui::IGUIEditBox *)tochange)->setText(utf8_to_wide(text).c_str());
2745 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2747 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2749 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
2750 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2751 s32 item_i = i + s.start_item_i;
2755 if (s.real_coordinates) {
2756 x = (i%s.geom.X) * (imgsize.X * 1.25);
2757 y = (i/s.geom.X) * (imgsize.Y * 1.25);
2759 x = (i%s.geom.X) * spacing.X;
2760 y = (i/s.geom.X) * spacing.Y;
2763 core::rect<s32> rect = imgrect + s.pos + p0;
2764 if(rect.isPointInside(p))
2766 return ItemSpec(s.inventoryloc, s.listname, item_i);
2771 return ItemSpec(InventoryLocation(), "", -1);
2774 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int layer,
2777 video::IVideoDriver* driver = Environment->getVideoDriver();
2779 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2781 warningstream<<"GUIFormSpecMenu::drawList(): "
2782 <<"The inventory location "
2783 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2787 InventoryList *ilist = inv->getList(s.listname);
2789 warningstream<<"GUIFormSpecMenu::drawList(): "
2790 <<"The inventory list \""<<s.listname<<"\" @ \""
2791 <<s.inventoryloc.dump()<<"\" doesn't exist"
2796 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2798 for (s32 i = 0; i < s.geom.X * s.geom.Y; i++) {
2799 s32 item_i = i + s.start_item_i;
2800 if (item_i >= (s32)ilist->getSize())
2805 if (s.real_coordinates) {
2806 x = (i%s.geom.X) * (imgsize.X * 1.25);
2807 y = (i/s.geom.X) * (imgsize.Y * 1.25);
2809 x = (i%s.geom.X) * spacing.X;
2810 y = (i/s.geom.X) * spacing.Y;
2813 core::rect<s32> rect = imgrect + s.pos + p;
2814 ItemStack item = ilist->getItem(item_i);
2816 bool selected = m_selected_item
2817 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2818 && m_selected_item->listname == s.listname
2819 && m_selected_item->i == item_i;
2820 bool hovering = rect.isPointInside(m_pointer);
2821 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2822 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2826 item_hovered = true;
2827 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2829 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2833 //Draw inv slot borders
2835 s32 x1 = rect.UpperLeftCorner.X;
2836 s32 y1 = rect.UpperLeftCorner.Y;
2837 s32 x2 = rect.LowerRightCorner.X;
2838 s32 y2 = rect.LowerRightCorner.Y;
2840 driver->draw2DRectangle(m_slotbordercolor,
2841 core::rect<s32>(v2s32(x1 - border, y1 - border),
2842 v2s32(x2 + border, y1)), NULL);
2843 driver->draw2DRectangle(m_slotbordercolor,
2844 core::rect<s32>(v2s32(x1 - border, y2),
2845 v2s32(x2 + border, y2 + border)), NULL);
2846 driver->draw2DRectangle(m_slotbordercolor,
2847 core::rect<s32>(v2s32(x1 - border, y1),
2848 v2s32(x1, y2)), NULL);
2849 driver->draw2DRectangle(m_slotbordercolor,
2850 core::rect<s32>(v2s32(x2, y1),
2851 v2s32(x2 + border, y2)), NULL);
2856 item.takeItem(m_selected_amount);
2858 if (!item.empty()) {
2860 drawItemStack(driver, m_font, item,
2861 rect, &AbsoluteClippingRect, m_client,
2864 if (hovering && !m_selected_item) {
2865 std::string tooltip = item.getDescription(m_client->idef());
2866 if (m_tooltip_append_itemname)
2867 tooltip += "\n[" + item.name + "]";
2868 showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
2869 m_default_tooltip_bgcolor);
2876 void GUIFormSpecMenu::drawSelectedItem()
2878 video::IVideoDriver* driver = Environment->getVideoDriver();
2880 if (!m_selected_item) {
2881 drawItemStack(driver, m_font, ItemStack(),
2882 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2883 NULL, m_client, IT_ROT_DRAGGED);
2887 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2889 InventoryList *list = inv->getList(m_selected_item->listname);
2891 ItemStack stack = list->getItem(m_selected_item->i);
2892 stack.count = m_selected_amount;
2894 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2895 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2896 rect.constrainTo(driver->getViewPort());
2897 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
2900 void GUIFormSpecMenu::drawMenu()
2903 const std::string &newform = m_form_src->getForm();
2904 if (newform != m_formspec_string) {
2905 m_formspec_string = newform;
2906 regenerateGui(m_screensize_old);
2910 gui::IGUISkin* skin = Environment->getSkin();
2911 sanity_check(skin != NULL);
2912 gui::IGUIFont *old_font = skin->getFont();
2913 skin->setFont(m_font);
2915 updateSelectedItem();
2917 video::IVideoDriver* driver = Environment->getVideoDriver();
2919 v2u32 screenSize = driver->getScreenSize();
2920 core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
2923 driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
2924 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2926 m_tooltip_element->setVisible(false);
2928 for (const auto &pair : m_tooltip_rects) {
2929 if (pair.first.isPointInside(m_pointer)) {
2930 const std::wstring &text = pair.second.tooltip;
2931 if (!text.empty()) {
2932 showTooltip(text, pair.second.color, pair.second.bgcolor);
2941 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_backgrounds) {
2942 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2945 // Image size on screen
2946 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2947 // Image rectangle on screen
2948 core::rect<s32> rect = imgrect + spec.pos;
2949 // Middle rect for 9-slicing
2950 core::rect<s32> middle = spec.middle;
2953 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2954 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2955 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2956 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2957 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2960 if (middle.getArea() == 0) {
2961 const video::SColor color(255, 255, 255, 255);
2962 const video::SColor colors[] = {color, color, color, color};
2963 draw2DImageFilterScaled(driver, texture, rect,
2964 core::rect<s32>(core::position2d<s32>(0, 0),
2965 core::dimension2di(texture->getOriginalSize())),
2966 NULL/*&AbsoluteClippingRect*/, colors, true);
2968 // `-x` is interpreted as `w - x`
2969 if (middle.LowerRightCorner.X < 0) {
2970 middle.LowerRightCorner.X += texture->getOriginalSize().Width;
2972 if (middle.LowerRightCorner.Y < 0) {
2973 middle.LowerRightCorner.Y += texture->getOriginalSize().Height;
2975 draw2DImage9Slice(driver, texture, rect, middle);
2978 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2979 errorstream << "\t" << spec.name << std::endl;
2986 for (const GUIFormSpecMenu::BoxDrawSpec &spec : m_boxes) {
2987 irr::video::SColor todraw = spec.color;
2989 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2990 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2992 driver->draw2DRectangle(todraw, rect, 0);
2998 gui::IGUIElement::draw();
3003 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_images) {
3004 video::ITexture *texture = m_tsrc->getTexture(spec.name);
3007 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
3008 // Image size on screen
3009 core::rect<s32> imgrect;
3012 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
3015 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
3017 // Image rectangle on screen
3018 core::rect<s32> rect = imgrect + spec.pos;
3019 const video::SColor color(255,255,255,255);
3020 const video::SColor colors[] = {color,color,color,color};
3021 draw2DImageFilterScaled(driver, texture, rect,
3022 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
3023 NULL/*&AbsoluteClippingRect*/, colors, true);
3026 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
3027 errorstream << "\t" << spec.name << std::endl;
3034 for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_itemimages) {
3038 IItemDefManager *idef = m_client->idef();
3040 item.deSerialize(spec.item_name, idef);
3041 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
3042 // Viewport rectangle on screen
3043 core::rect<s32> rect = imgrect + spec.pos;
3044 if (spec.parent_button && spec.parent_button->isPressed()) {
3045 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
3046 rect += core::dimension2d<s32>(
3047 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
3049 rect += core::dimension2d<s32>(
3050 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
3051 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
3054 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
3055 m_client, IT_ROT_NONE);
3060 Layer 0: Item slot rectangles
3061 Layer 1: Item images; prepare tooltip
3063 bool item_hovered = false;
3064 for (int layer = 0; layer < 2; layer++) {
3065 for (const GUIFormSpecMenu::ListDrawSpec &spec : m_inventorylists) {
3066 drawList(spec, layer, item_hovered);
3069 if (!item_hovered) {
3070 drawItemStack(driver, m_font, ItemStack(),
3071 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
3072 NULL, m_client, IT_ROT_HOVERED);
3075 /* TODO find way to show tooltips on touchscreen */
3076 #ifndef HAVE_TOUCHSCREENGUI
3077 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
3081 Draw static text elements
3083 for (const GUIFormSpecMenu::StaticTextSpec &spec : m_static_texts) {
3084 core::rect<s32> rect = spec.rect;
3085 if (spec.parent_button && spec.parent_button->isPressed()) {
3086 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
3087 rect += core::dimension2d<s32>(
3088 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
3090 // Use image offset instead of text's because its a bit smaller
3091 // and fits better, also TEXT_OFFSET_X is always 0
3092 rect += core::dimension2d<s32>(
3093 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
3094 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
3097 video::SColor color(255, 255, 255, 255);
3098 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
3102 Draw fields/buttons tooltips
3104 gui::IGUIElement *hovered =
3105 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
3107 if (hovered != NULL) {
3108 s32 id = hovered->getID();
3112 m_old_tooltip_id = id;
3114 if (id == m_old_tooltip_id) {
3115 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
3117 m_hovered_time = porting::getTimeMs();
3118 m_old_tooltip_id = id;
3122 // Find and update the current tooltip
3123 if (id != -1 && delta >= m_tooltip_show_delay) {
3124 for (const FieldSpec &field : m_fields) {
3126 if (field.fid != id)
3129 const std::wstring &text = m_tooltips[field.fname].tooltip;
3131 showTooltip(text, m_tooltips[field.fname].color,
3132 m_tooltips[field.fname].bgcolor);
3139 m_tooltip_element->draw();
3142 Draw dragged item stack
3146 skin->setFont(old_font);
3150 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
3151 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
3153 const std::wstring ntext = translate_string(text);
3154 m_tooltip_element->setOverrideColor(color);
3155 m_tooltip_element->setBackgroundColor(bgcolor);
3156 setStaticText(m_tooltip_element, ntext.c_str());
3158 // Tooltip size and offset
3159 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
3160 #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
3161 std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
3162 s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
3164 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
3166 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
3167 int tooltip_offset_x = m_btn_height;
3168 int tooltip_offset_y = m_btn_height;
3170 tooltip_offset_x *= 3;
3171 tooltip_offset_y = 0;
3172 if (m_pointer.X > (s32)screenSize.X / 2)
3173 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
3176 // Calculate and set the tooltip position
3177 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
3178 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
3179 if (tooltip_x + tooltip_width > (s32)screenSize.X)
3180 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
3181 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
3182 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
3184 m_tooltip_element->setRelativePosition(
3186 core::position2d<s32>(tooltip_x, tooltip_y),
3187 core::dimension2d<s32>(tooltip_width, tooltip_height)
3191 // Display the tooltip
3192 m_tooltip_element->setVisible(true);
3193 bringToFront(m_tooltip_element);
3196 void GUIFormSpecMenu::updateSelectedItem()
3198 verifySelectedItem();
3200 // If craftresult is nonempty and nothing else is selected, select it now.
3201 if (!m_selected_item) {
3202 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
3203 if (s.listname != "craftpreview")
3206 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
3210 InventoryList *list = inv->getList("craftresult");
3212 if (!list || list->getSize() == 0)
3215 const ItemStack &item = list->getItem(0);
3219 // Grab selected item from the crafting result list
3220 m_selected_item = new ItemSpec;
3221 m_selected_item->inventoryloc = s.inventoryloc;
3222 m_selected_item->listname = "craftresult";
3223 m_selected_item->i = 0;
3224 m_selected_amount = item.count;
3225 m_selected_dragging = false;
3230 // If craftresult is selected, keep the whole stack selected
3231 if (m_selected_item && m_selected_item->listname == "craftresult")
3232 m_selected_amount = verifySelectedItem().count;
3235 ItemStack GUIFormSpecMenu::verifySelectedItem()
3237 // If the selected stack has become empty for some reason, deselect it.
3238 // If the selected stack has become inaccessible, deselect it.
3239 // If the selected stack has become smaller, adjust m_selected_amount.
3240 // Return the selected stack.
3244 if(m_selected_item->isValid())
3246 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3249 InventoryList *list = inv->getList(m_selected_item->listname);
3250 if(list && (u32) m_selected_item->i < list->getSize())
3252 ItemStack stack = list->getItem(m_selected_item->i);
3253 if (!m_selected_swap.empty()) {
3254 if (m_selected_swap.name == stack.name &&
3255 m_selected_swap.count == stack.count)
3256 m_selected_swap.clear();
3258 m_selected_amount = std::min(m_selected_amount, stack.count);
3267 // selection was not valid
3268 delete m_selected_item;
3269 m_selected_item = NULL;
3270 m_selected_amount = 0;
3271 m_selected_dragging = false;
3276 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
3282 if (quitmode == quit_mode_accept) {
3283 fields["quit"] = "true";
3286 if (quitmode == quit_mode_cancel) {
3287 fields["quit"] = "true";
3288 m_text_dst->gotText(fields);
3292 if (current_keys_pending.key_down) {
3293 fields["key_down"] = "true";
3294 current_keys_pending.key_down = false;
3297 if (current_keys_pending.key_up) {
3298 fields["key_up"] = "true";
3299 current_keys_pending.key_up = false;
3302 if (current_keys_pending.key_enter) {
3303 fields["key_enter"] = "true";
3304 current_keys_pending.key_enter = false;
3307 if (!current_field_enter_pending.empty()) {
3308 fields["key_enter_field"] = current_field_enter_pending;
3309 current_field_enter_pending = "";
3312 if (current_keys_pending.key_escape) {
3313 fields["key_escape"] = "true";
3314 current_keys_pending.key_escape = false;
3317 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
3319 std::string name = s.fname;
3320 if (s.ftype == f_Button) {
3321 fields[name] = wide_to_utf8(s.flabel);
3322 } else if (s.ftype == f_Table) {
3323 GUITable *table = getTable(s.fname);
3325 fields[name] = table->checkEvent();
3328 else if(s.ftype == f_DropDown) {
3329 // no dynamic cast possible due to some distributions shipped
3330 // without rtti support in irrlicht
3331 IGUIElement * element = getElementFromId(s.fid);
3332 gui::IGUIComboBox *e = NULL;
3333 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
3334 e = static_cast<gui::IGUIComboBox*>(element);
3336 s32 selected = e->getSelected();
3337 if (selected >= 0) {
3338 std::vector<std::string> *dropdown_values =
3339 getDropDownValues(s.fname);
3340 if (dropdown_values && selected < (s32)dropdown_values->size()) {
3341 fields[name] = (*dropdown_values)[selected];
3345 else if (s.ftype == f_TabHeader) {
3346 // no dynamic cast possible due to some distributions shipped
3347 // without rttzi support in irrlicht
3348 IGUIElement * element = getElementFromId(s.fid);
3349 gui::IGUITabControl *e = NULL;
3350 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
3351 e = static_cast<gui::IGUITabControl *>(element);
3355 std::stringstream ss;
3356 ss << (e->getActiveTab() +1);
3357 fields[name] = ss.str();
3360 else if (s.ftype == f_CheckBox) {
3361 // no dynamic cast possible due to some distributions shipped
3362 // without rtti support in irrlicht
3363 IGUIElement * element = getElementFromId(s.fid);
3364 gui::IGUICheckBox *e = NULL;
3365 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
3366 e = static_cast<gui::IGUICheckBox*>(element);
3371 fields[name] = "true";
3373 fields[name] = "false";
3376 else if (s.ftype == f_ScrollBar) {
3377 // no dynamic cast possible due to some distributions shipped
3378 // without rtti support in irrlicht
3379 IGUIElement * element = getElementFromId(s.fid);
3380 gui::IGUIScrollBar *e = NULL;
3381 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
3382 e = static_cast<gui::IGUIScrollBar*>(element);
3386 std::stringstream os;
3388 if (s.fdefault == L"Changed")
3389 fields[name] = "CHG:" + os.str();
3391 fields[name] = "VAL:" + os.str();
3396 IGUIElement* e = getElementFromId(s.fid);
3398 fields[name] = wide_to_utf8(e->getText());
3404 m_text_dst->gotText(fields);
3408 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
3410 while(tocheck != NULL) {
3411 if (tocheck == parent) {
3414 tocheck = tocheck->getParent();
3419 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
3421 // The IGUITabControl renders visually using the skin's selected
3422 // font, which we override for the duration of form drawing,
3423 // but computes tab hotspots based on how it would have rendered
3424 // using the font that is selected at the time of button release.
3425 // To make these two consistent, temporarily override the skin's
3426 // font while the IGUITabControl is processing the event.
3427 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3428 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3429 s32 x = event.MouseInput.X;
3430 s32 y = event.MouseInput.Y;
3431 gui::IGUIElement *hovered =
3432 Environment->getRootGUIElement()->getElementFromPoint(
3433 core::position2d<s32>(x, y));
3434 if (hovered && isMyChild(hovered) &&
3435 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
3436 gui::IGUISkin* skin = Environment->getSkin();
3437 sanity_check(skin != NULL);
3438 gui::IGUIFont *old_font = skin->getFont();
3439 skin->setFont(m_font);
3440 bool retval = hovered->OnEvent(event);
3441 skin->setFont(old_font);
3446 // Fix Esc/Return key being eaten by checkboxen and tables
3447 if(event.EventType==EET_KEY_INPUT_EVENT) {
3448 KeyPress kp(event.KeyInput);
3449 if (kp == EscapeKey || kp == CancelKey
3450 || kp == getKeySetting("keymap_inventory")
3451 || event.KeyInput.Key==KEY_RETURN) {
3452 gui::IGUIElement *focused = Environment->getFocus();
3453 if (focused && isMyChild(focused) &&
3454 (focused->getType() == gui::EGUIET_LIST_BOX ||
3455 focused->getType() == gui::EGUIET_CHECK_BOX) &&
3456 (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
3457 event.KeyInput.Key != KEY_RETURN)) {
3463 // Mouse wheel events: send to hovered element instead of focused
3464 if(event.EventType==EET_MOUSE_INPUT_EVENT
3465 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
3466 s32 x = event.MouseInput.X;
3467 s32 y = event.MouseInput.Y;
3468 gui::IGUIElement *hovered =
3469 Environment->getRootGUIElement()->getElementFromPoint(
3470 core::position2d<s32>(x, y));
3471 if (hovered && isMyChild(hovered)) {
3472 hovered->OnEvent(event);
3477 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
3478 s32 x = event.MouseInput.X;
3479 s32 y = event.MouseInput.Y;
3480 gui::IGUIElement *hovered =
3481 Environment->getRootGUIElement()->getElementFromPoint(
3482 core::position2d<s32>(x, y));
3483 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3484 m_old_tooltip_id = -1;
3486 if (!isChild(hovered,this)) {
3487 if (DoubleClickDetection(event)) {
3493 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3494 /* TODO add a check like:
3495 if (event.JoystickEvent != joystick_we_listen_for)
3498 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3500 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3502 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3504 acceptInput(quit_mode_accept);
3512 return GUIModalMenu::preprocessEvent(event);
3515 /******************************************************************************/
3516 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3518 /* The following code is for capturing double-clicks of the mouse button
3519 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3520 * -- which closes the form -- under some circumstances.
3522 * There have been many github issues reporting this as a bug even though it
3523 * was an intended feature. For this reason, remapping the double-click as
3524 * an ESC must be explicitly set when creating this class via the
3525 * /p remap_dbl_click parameter of the constructor.
3528 if (!m_remap_dbl_click)
3531 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3532 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3533 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3535 m_doubleclickdetect[1].pos = m_pointer;
3536 m_doubleclickdetect[1].time = porting::getTimeMs();
3538 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3539 u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
3544 double squaredistance =
3545 m_doubleclickdetect[0].pos
3546 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3548 if (squaredistance > (30*30)) {
3552 SEvent* translated = new SEvent();
3553 assert(translated != 0);
3554 //translate doubleclick to escape
3555 memset(translated, 0, sizeof(SEvent));
3556 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3557 translated->KeyInput.Key = KEY_ESCAPE;
3558 translated->KeyInput.Control = false;
3559 translated->KeyInput.Shift = false;
3560 translated->KeyInput.PressedDown = true;
3561 translated->KeyInput.Char = 0;
3562 OnEvent(*translated);
3564 // no need to send the key up event as we're already deleted
3565 // and no one else did notice this event
3573 void GUIFormSpecMenu::tryClose()
3577 acceptInput(quit_mode_cancel);
3580 m_text_dst->gotText(L"MenuQuit");
3584 enum ButtonEventType : u8
3597 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3599 if (event.EventType==EET_KEY_INPUT_EVENT) {
3600 KeyPress kp(event.KeyInput);
3601 if (event.KeyInput.PressedDown && (
3602 (kp == EscapeKey) || (kp == CancelKey) ||
3603 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
3608 if (m_client != NULL && event.KeyInput.PressedDown &&
3609 (kp == getKeySetting("keymap_screenshot"))) {
3610 m_client->makeScreenshot();
3612 if (event.KeyInput.PressedDown &&
3613 (event.KeyInput.Key==KEY_RETURN ||
3614 event.KeyInput.Key==KEY_UP ||
3615 event.KeyInput.Key==KEY_DOWN)
3617 switch (event.KeyInput.Key) {
3619 current_keys_pending.key_enter = true;
3622 current_keys_pending.key_up = true;
3625 current_keys_pending.key_down = true;
3629 //can't happen at all!
3630 FATAL_ERROR("Reached a source line that can't ever been reached");
3633 if (current_keys_pending.key_enter && m_allowclose) {
3634 acceptInput(quit_mode_accept);
3644 /* Mouse event other than movement, or crossing the border of inventory
3645 field while holding right mouse button
3647 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3648 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3649 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3650 event.MouseInput.isRightPressed() &&
3651 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3653 // Get selected item and hovered/clicked item (s)
3655 m_old_tooltip_id = -1;
3656 updateSelectedItem();
3657 ItemSpec s = getItemAtPos(m_pointer);
3659 Inventory *inv_selected = NULL;
3660 Inventory *inv_s = NULL;
3661 InventoryList *list_s = NULL;
3663 if (m_selected_item) {
3664 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3665 sanity_check(inv_selected);
3666 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3673 inv_s = m_invmgr->getInventory(s.inventoryloc);
3676 errorstream << "InventoryMenu: The selected inventory location "
3677 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3679 s.i = -1; // make it invalid again
3683 list_s = inv_s->getList(s.listname);
3684 if (list_s == NULL) {
3685 verbosestream << "InventoryMenu: The selected inventory list \""
3686 << s.listname << "\" does not exist" << std::endl;
3687 s.i = -1; // make it invalid again
3691 if ((u32)s.i >= list_s->getSize()) {
3692 infostream << "InventoryMenu: The selected inventory list \""
3693 << s.listname << "\" is too small (i=" << s.i << ", size="
3694 << list_s->getSize() << ")" << std::endl;
3695 s.i = -1; // make it invalid again
3699 s_count = list_s->getItem(s.i).count;
3702 bool identical = m_selected_item && s.isValid() &&
3703 (inv_selected == inv_s) &&
3704 (m_selected_item->listname == s.listname) &&
3705 (m_selected_item->i == s.i);
3707 ButtonEventType button = BET_LEFT;
3708 ButtonEventType updown = BET_OTHER;
3709 switch (event.MouseInput.Event) {
3710 case EMIE_LMOUSE_PRESSED_DOWN:
3711 button = BET_LEFT; updown = BET_DOWN;
3713 case EMIE_RMOUSE_PRESSED_DOWN:
3714 button = BET_RIGHT; updown = BET_DOWN;
3716 case EMIE_MMOUSE_PRESSED_DOWN:
3717 button = BET_MIDDLE; updown = BET_DOWN;
3719 case EMIE_MOUSE_WHEEL:
3720 button = (event.MouseInput.Wheel > 0) ?
3721 BET_WHEEL_UP : BET_WHEEL_DOWN;
3724 case EMIE_LMOUSE_LEFT_UP:
3725 button = BET_LEFT; updown = BET_UP;
3727 case EMIE_RMOUSE_LEFT_UP:
3728 button = BET_RIGHT; updown = BET_UP;
3730 case EMIE_MMOUSE_LEFT_UP:
3731 button = BET_MIDDLE; updown = BET_UP;
3733 case EMIE_MOUSE_MOVED:
3740 // Set this number to a positive value to generate a move action
3741 // from m_selected_item to s.
3742 u32 move_amount = 0;
3744 // Set this number to a positive value to generate a move action
3745 // from s to the next inventory ring.
3746 u32 shift_move_amount = 0;
3748 // Set this number to a positive value to generate a drop action
3749 // from m_selected_item.
3750 u32 drop_amount = 0;
3752 // Set this number to a positive value to generate a craft action at s.
3753 u32 craft_amount = 0;
3757 // Some mouse button has been pressed
3759 //infostream<<"Mouse button "<<button<<" pressed at p=("
3760 // <<p.X<<","<<p.Y<<")"<<std::endl;
3762 m_selected_dragging = false;
3764 if (s.isValid() && s.listname == "craftpreview") {
3765 // Craft preview has been clicked: craft
3766 craft_amount = (button == BET_MIDDLE ? 10 : 1);
3767 } else if (!m_selected_item) {
3768 if (s_count && button != BET_WHEEL_UP) {
3769 // Non-empty stack has been clicked: select or shift-move it
3770 m_selected_item = new ItemSpec(s);
3773 if (button == BET_RIGHT)
3774 count = (s_count + 1) / 2;
3775 else if (button == BET_MIDDLE)
3776 count = MYMIN(s_count, 10);
3777 else if (button == BET_WHEEL_DOWN)
3782 if (!event.MouseInput.Shift) {
3783 // no shift: select item
3784 m_selected_amount = count;
3785 m_selected_dragging = button != BET_WHEEL_DOWN;
3786 m_auto_place = false;
3788 // shift pressed: move item, right click moves 1
3789 shift_move_amount = button == BET_RIGHT ? 1 : count;
3792 } else { // m_selected_item != NULL
3793 assert(m_selected_amount >= 1);
3796 // Clicked a slot: move
3797 if (button == BET_RIGHT || button == BET_WHEEL_UP)
3799 else if (button == BET_MIDDLE)
3800 move_amount = MYMIN(m_selected_amount, 10);
3801 else if (button == BET_LEFT)
3802 move_amount = m_selected_amount;
3806 if (button == BET_WHEEL_DOWN) {
3807 if (m_selected_amount < s_count)
3808 ++m_selected_amount;
3810 if (move_amount >= m_selected_amount)
3811 m_selected_amount = 0;
3813 m_selected_amount -= move_amount;
3817 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
3818 && button != BET_WHEEL_DOWN) {
3819 // Clicked outside of the window: drop
3820 if (button == BET_RIGHT || button == BET_WHEEL_UP)
3822 else if (button == BET_MIDDLE)
3823 drop_amount = MYMIN(m_selected_amount, 10);
3825 drop_amount = m_selected_amount;
3830 // Some mouse button has been released
3832 //infostream<<"Mouse button "<<button<<" released at p=("
3833 // <<p.X<<","<<p.Y<<")"<<std::endl;
3835 if (m_selected_dragging && m_selected_item) {
3838 // Dragged to different slot: move all selected
3839 move_amount = m_selected_amount;
3841 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3842 // Dragged outside of window: drop all selected
3843 drop_amount = m_selected_amount;
3847 m_selected_dragging = false;
3848 // Keep track of whether the mouse button be released
3849 // One click is drag without dropping. Click + release
3850 // + click changes to drop item when moved mode
3851 if (m_selected_item)
3852 m_auto_place = true;
3855 // Mouse has been moved and rmb is down and mouse pointer just
3856 // entered a new inventory field (checked in the entry-if, this
3857 // is the only action here that is generated by mouse movement)
3858 if (m_selected_item && s.isValid() && s.listname != "craftpreview") {
3860 // TODO: middle mouse to move 10 items might be handy
3862 // Only move an item if the destination slot is empty
3863 // or contains the same item type as what is going to be
3865 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3866 InventoryList *list_to = list_s;
3867 assert(list_from && list_to);
3868 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3869 ItemStack stack_to = list_to->getItem(s.i);
3870 if (stack_to.empty() || stack_to.name == stack_from.name)
3879 // Possibly send inventory action to server
3880 if (move_amount > 0) {
3881 // Send IAction::Move
3883 assert(m_selected_item && m_selected_item->isValid());
3884 assert(s.isValid());
3886 assert(inv_selected && inv_s);
3887 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3888 InventoryList *list_to = list_s;
3889 assert(list_from && list_to);
3890 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3891 ItemStack stack_to = list_to->getItem(s.i);
3893 // Check how many items can be moved
3894 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3895 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
3897 // If source stack cannot be added to destination stack at all,
3899 if (leftover.count == stack_from.count &&
3900 leftover.name == stack_from.name) {
3902 if (m_selected_swap.empty()) {
3903 m_selected_amount = stack_to.count;
3904 m_selected_dragging = false;
3906 // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
3907 // Skip next validation checks due async inventory calls
3908 m_selected_swap = stack_to;
3913 // Source stack goes fully into destination stack
3914 else if (leftover.empty()) {
3915 m_selected_amount -= move_amount;
3917 // Source stack goes partly into destination stack
3919 move_amount -= leftover.count;
3920 m_selected_amount -= move_amount;
3924 infostream << "Handing IAction::Move to manager" << std::endl;
3925 IMoveAction *a = new IMoveAction();
3926 a->count = move_amount;
3927 a->from_inv = m_selected_item->inventoryloc;
3928 a->from_list = m_selected_item->listname;
3929 a->from_i = m_selected_item->i;
3930 a->to_inv = s.inventoryloc;
3931 a->to_list = s.listname;
3933 m_invmgr->inventoryAction(a);
3935 } else if (shift_move_amount > 0) {
3936 u32 mis = m_inventory_rings.size();
3938 for (; i < mis; i++) {
3939 const ListRingSpec &sp = m_inventory_rings[i];
3940 if (sp.inventoryloc == s.inventoryloc
3941 && sp.listname == s.listname)
3945 if (i >= mis) // if not found
3947 u32 to_inv_ind = (i + 1) % mis;
3948 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3949 InventoryList *list_from = list_s;
3952 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3955 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3958 ItemStack stack_from = list_from->getItem(s.i);
3959 assert(shift_move_amount <= stack_from.count);
3961 infostream << "Handing IAction::Move to manager" << std::endl;
3962 IMoveAction *a = new IMoveAction();
3963 a->count = shift_move_amount;
3964 a->from_inv = s.inventoryloc;
3965 a->from_list = s.listname;
3967 a->to_inv = to_inv_sp.inventoryloc;
3968 a->to_list = to_inv_sp.listname;
3969 a->move_somewhere = true;
3970 m_invmgr->inventoryAction(a);
3972 } else if (drop_amount > 0) {
3973 // Send IAction::Drop
3975 assert(m_selected_item && m_selected_item->isValid());
3976 assert(inv_selected);
3977 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3979 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3981 // Check how many items can be dropped
3982 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3983 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3984 m_selected_amount -= drop_amount;
3986 infostream << "Handing IAction::Drop to manager" << std::endl;
3987 IDropAction *a = new IDropAction();
3988 a->count = drop_amount;
3989 a->from_inv = m_selected_item->inventoryloc;
3990 a->from_list = m_selected_item->listname;
3991 a->from_i = m_selected_item->i;
3992 m_invmgr->inventoryAction(a);
3993 } else if (craft_amount > 0) {
3994 assert(s.isValid());
3996 // if there are no items selected or the selected item
3997 // belongs to craftresult list, proceed with crafting
3998 if (m_selected_item == NULL ||
3999 !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
4003 // Send IACTION_CRAFT
4004 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
4005 ICraftAction *a = new ICraftAction();
4006 a->count = craft_amount;
4007 a->craft_inv = s.inventoryloc;
4008 m_invmgr->inventoryAction(a);
4012 // If m_selected_amount has been decreased to zero, deselect
4013 if (m_selected_amount == 0) {
4014 m_selected_swap.clear();
4015 delete m_selected_item;
4016 m_selected_item = NULL;
4017 m_selected_amount = 0;
4018 m_selected_dragging = false;
4020 m_old_pointer = m_pointer;
4022 if (event.EventType == EET_GUI_EVENT) {
4024 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
4026 // find the element that was clicked
4027 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4028 if ((s.ftype == f_TabHeader) &&
4029 (s.fid == event.GUIEvent.Caller->getID())) {
4037 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
4039 if (!canTakeFocus(event.GUIEvent.Element)) {
4040 infostream<<"GUIFormSpecMenu: Not allowing focus change."
4042 // Returning true disables focus change
4046 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
4047 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
4048 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
4049 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
4050 unsigned int btn_id = event.GUIEvent.Caller->getID();
4052 if (btn_id == 257) {
4054 acceptInput(quit_mode_accept);
4058 m_text_dst->gotText(L"ExitButton");
4060 // quitMenu deallocates menu
4064 // find the element that was clicked
4065 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4066 // if its a button, set the send field so
4067 // lua knows which button was pressed
4068 if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
4069 s.fid == event.GUIEvent.Caller->getID()) {
4073 acceptInput(quit_mode_accept);
4076 m_text_dst->gotText(L"ExitButton");
4081 acceptInput(quit_mode_no);
4085 } else if ((s.ftype == f_DropDown) &&
4086 (s.fid == event.GUIEvent.Caller->getID())) {
4087 // only send the changed dropdown
4088 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4089 if (s2.ftype == f_DropDown) {
4094 acceptInput(quit_mode_no);
4096 // revert configuration to make sure dropdowns are sent on
4097 // regular button click
4098 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4099 if (s2.ftype == f_DropDown) {
4104 } else if ((s.ftype == f_ScrollBar) &&
4105 (s.fid == event.GUIEvent.Caller->getID())) {
4106 s.fdefault = L"Changed";
4107 acceptInput(quit_mode_no);
4113 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
4114 if (event.GUIEvent.Caller->getID() > 257) {
4115 bool close_on_enter = true;
4116 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4117 if (s.ftype == f_Unknown &&
4118 s.fid == event.GUIEvent.Caller->getID()) {
4119 current_field_enter_pending = s.fname;
4120 std::unordered_map<std::string, bool>::const_iterator it =
4121 field_close_on_enter.find(s.fname);
4122 if (it != field_close_on_enter.end())
4123 close_on_enter = (*it).second;
4129 if (m_allowclose && close_on_enter) {
4130 current_keys_pending.key_enter = true;
4131 acceptInput(quit_mode_accept);
4134 current_keys_pending.key_enter = true;
4137 // quitMenu deallocates menu
4142 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
4143 int current_id = event.GUIEvent.Caller->getID();
4144 if (current_id > 257) {
4145 // find the element that was clicked
4146 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4147 // if it's a table, set the send field
4148 // so lua knows which table was changed
4149 if ((s.ftype == f_Table) && (s.fid == current_id)) {
4160 return Parent ? Parent->OnEvent(event) : false;
4164 * get name of element by element id
4165 * @param id of element
4166 * @return name string or empty string
4168 std::string GUIFormSpecMenu::getNameByID(s32 id)
4170 for (FieldSpec &spec : m_fields) {
4171 if (spec.fid == id) {
4179 * get label of element by id
4180 * @param id of element
4181 * @return label string or empty string
4183 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
4185 for (FieldSpec &spec : m_fields) {
4186 if (spec.fid == id) {
4193 StyleSpec GUIFormSpecMenu::getStyleForElement(const std::string &type,
4194 const std::string &name, const std::string &parent_type) {
4197 if (!parent_type.empty()) {
4198 auto it = theme_by_type.find(parent_type);
4199 if (it != theme_by_type.end()) {
4204 auto it = theme_by_type.find(type);
4205 if (it != theme_by_type.end()) {
4209 it = theme_by_name.find(name);
4210 if (it != theme_by_name.end()) {