3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guiFormSpecMenu.h"
27 #include "constants.h"
29 #include "client/keycode.h"
30 #include "util/strfnd.h"
31 #include <IGUIButton.h>
32 #include <IGUICheckBox.h>
33 #include <IGUIComboBox.h>
34 #include <IGUIEditBox.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include "client/renderingengine.h"
40 #include "client/tile.h" // ITextureSource
41 #include "client/hud.h" // drawItemStack
45 #include "scripting_server.h"
46 #include "mainmenumanager.h"
49 #include "client/client.h"
50 #include "client/fontengine.h"
52 #include "util/numeric.h"
53 #include "util/string.h" // for parseColorString()
54 #include "irrlicht_changes/static_text.h"
55 #include "client/guiscalingfilter.h"
56 #include "guiBackgroundImage.h"
58 #include "guiButton.h"
59 #include "guiEditBoxWithScrollbar.h"
60 #include "guiItemImage.h"
61 #include "guiScrollBar.h"
63 #include "intlGUIEditBox.h"
64 #include "guiHyperText.h"
66 #define MY_CHECKPOS(a,b) \
67 if (v_pos.size() != 2) { \
68 errorstream<< "Invalid pos for element " << a << "specified: \"" \
69 << parts[b] << "\"" << std::endl; \
73 #define MY_CHECKGEOM(a,b) \
74 if (v_geom.size() != 2) { \
75 errorstream<< "Invalid geometry for element " << a << \
76 "specified: \"" << parts[b] << "\"" << std::endl; \
82 static unsigned int font_line_height(gui::IGUIFont *font)
84 return font->getDimension(L"Ay").Height + font->getKerningHeight();
87 inline u32 clamp_u8(s32 value)
89 return (u32) MYMIN(MYMAX(value, 0), 255);
92 GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
93 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
94 Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst,
95 const std::string &formspecPrepend,
96 bool remap_dbl_click):
97 GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr),
101 m_formspec_prepend(formspecPrepend),
104 m_joystick(joystick),
105 m_remap_dbl_click(remap_dbl_click)
107 current_keys_pending.key_down = false;
108 current_keys_pending.key_up = false;
109 current_keys_pending.key_enter = false;
110 current_keys_pending.key_escape = false;
112 m_doubleclickdetect[0].time = 0;
113 m_doubleclickdetect[1].time = 0;
115 m_doubleclickdetect[0].pos = v2s32(0, 0);
116 m_doubleclickdetect[1].pos = v2s32(0, 0);
118 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
119 m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
122 GUIFormSpecMenu::~GUIFormSpecMenu()
126 for (auto &table_it : m_tables) {
127 table_it.second->drop();
129 for (auto &inventorylist_it : m_inventorylists) {
130 inventorylist_it.e->drop();
132 for (auto &checkbox_it : m_checkboxes) {
133 checkbox_it.second->drop();
135 for (auto &scrollbar_it : m_scrollbars) {
136 scrollbar_it.second->drop();
138 for (auto &background_it : m_backgrounds) {
139 background_it->drop();
141 for (auto &tooltip_rect_it : m_tooltip_rects) {
142 tooltip_rect_it.first->drop();
145 delete m_selected_item;
150 void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
151 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
152 const std::string &formspecPrepend)
154 if (cur_formspec == nullptr) {
155 cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
156 client, client->getTextureSource(), fs_src, txt_dest, formspecPrepend);
157 cur_formspec->doPause = false;
160 Caution: do not call (*cur_formspec)->drop() here --
161 the reference might outlive the menu, so we will
162 periodically check if *cur_formspec is the only
163 remaining reference (i.e. the menu was removed)
164 and delete it in that case.
168 cur_formspec->setFormspecPrepend(formspecPrepend);
169 cur_formspec->setFormSource(fs_src);
170 cur_formspec->setTextDest(txt_dest);
174 void GUIFormSpecMenu::removeChildren()
176 const core::list<gui::IGUIElement*> &children = getChildren();
178 while (!children.empty()) {
179 (*children.getLast())->remove();
182 if (m_tooltip_element) {
183 m_tooltip_element->remove();
184 m_tooltip_element->drop();
185 m_tooltip_element = nullptr;
189 void GUIFormSpecMenu::setInitialFocus()
191 // Set initial focus according to following order of precedence:
192 // 1. first empty editbox
196 // 5. first focusable (not statictext, not tabheader)
197 // 6. first child element
199 core::list<gui::IGUIElement*> children = getChildren();
201 // in case "children" contains any NULL elements, remove them
202 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
203 it != children.end();) {
207 it = children.erase(it);
210 // 1. first empty editbox
211 for (gui::IGUIElement *it : children) {
212 if (it->getType() == gui::EGUIET_EDIT_BOX
213 && it->getText()[0] == 0) {
214 Environment->setFocus(it);
220 for (gui::IGUIElement *it : children) {
221 if (it->getType() == gui::EGUIET_EDIT_BOX) {
222 Environment->setFocus(it);
228 for (gui::IGUIElement *it : children) {
229 if (it->getTypeName() == std::string("GUITable")) {
230 Environment->setFocus(it);
236 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
237 it != children.end(); --it) {
238 if ((*it)->getType() == gui::EGUIET_BUTTON) {
239 Environment->setFocus(*it);
244 // 5. first focusable (not statictext, not tabheader)
245 for (gui::IGUIElement *it : children) {
246 if (it->getType() != gui::EGUIET_STATIC_TEXT &&
247 it->getType() != gui::EGUIET_TAB_CONTROL) {
248 Environment->setFocus(it);
253 // 6. first child element
254 if (children.empty())
255 Environment->setFocus(this);
257 Environment->setFocus(*(children.begin()));
260 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
262 for (auto &table : m_tables) {
263 if (tablename == table.first.fname)
269 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
271 for (auto &dropdown : m_dropdowns) {
272 if (name == dropdown.first.fname)
273 return &dropdown.second;
278 v2s32 GUIFormSpecMenu::getElementBasePos(const std::vector<std::string> *v_pos)
280 v2f32 pos_f = v2f32(padding.X, padding.Y) + pos_offset * spacing;
282 pos_f.X += stof((*v_pos)[0]) * spacing.X;
283 pos_f.Y += stof((*v_pos)[1]) * spacing.Y;
285 return v2s32(pos_f.X, pos_f.Y);
288 v2s32 GUIFormSpecMenu::getRealCoordinateBasePos(const std::vector<std::string> &v_pos)
290 return v2s32((stof(v_pos[0]) + pos_offset.X) * imgsize.X,
291 (stof(v_pos[1]) + pos_offset.Y) * imgsize.Y);
294 v2s32 GUIFormSpecMenu::getRealCoordinateGeometry(const std::vector<std::string> &v_geom)
296 return v2s32(stof(v_geom[0]) * imgsize.X, stof(v_geom[1]) * imgsize.Y);
299 void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
301 std::vector<std::string> parts = split(element,',');
303 if (((parts.size() == 2) || parts.size() == 3) ||
304 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
306 if (parts[1].find(';') != std::string::npos)
307 parts[1] = parts[1].substr(0,parts[1].find(';'));
309 data->invsize.X = MYMAX(0, stof(parts[0]));
310 data->invsize.Y = MYMAX(0, stof(parts[1]));
314 if (parts.size() == 3) {
315 if (parts[2] == "true") {
316 lockSize(true,v2u32(800,600));
320 data->explicit_size = true;
323 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
326 void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
328 std::vector<std::string> parts = split(element, ',');
330 if (parts.size() >= 2) {
331 if (parts[1].find(';') != std::string::npos)
332 parts[1] = parts[1].substr(0, parts[1].find(';'));
334 container_stack.push(pos_offset);
335 pos_offset.X += stof(parts[0]);
336 pos_offset.Y += stof(parts[1]);
339 errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
342 void GUIFormSpecMenu::parseContainerEnd(parserData* data)
344 if (container_stack.empty()) {
345 errorstream<< "Invalid container end element, no matching container start element" << std::endl;
347 pos_offset = container_stack.top();
348 container_stack.pop();
352 void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
355 warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
359 std::vector<std::string> parts = split(element,';');
361 if (((parts.size() == 4) || (parts.size() == 5)) ||
362 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
364 std::string location = parts[0];
365 std::string listname = parts[1];
366 std::vector<std::string> v_pos = split(parts[2],',');
367 std::vector<std::string> v_geom = split(parts[3],',');
368 std::string startindex;
369 if (parts.size() == 5)
370 startindex = parts[4];
372 MY_CHECKPOS("list",2);
373 MY_CHECKGEOM("list",3);
375 InventoryLocation loc;
377 if (location == "context" || location == "current_name")
378 loc = m_current_inventory_location;
380 loc.deSerialize(location);
383 geom.X = stoi(v_geom[0]);
384 geom.Y = stoi(v_geom[1]);
387 if (!startindex.empty())
388 start_i = stoi(startindex);
390 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
391 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
395 // check for the existence of inventory and list
396 Inventory *inv = m_invmgr->getInventory(loc);
398 warningstream << "GUIFormSpecMenu::parseList(): "
399 << "The inventory location "
400 << "\"" << loc.dump() << "\" doesn't exist"
404 InventoryList *ilist = inv->getList(listname);
406 warningstream << "GUIFormSpecMenu::parseList(): "
407 << "The inventory list \"" << listname << "\" "
408 << "@ \"" << loc.dump() << "\" doesn't exist"
413 // trim geom if it is larger than the actual inventory size
414 s32 list_size = (s32)ilist->getSize();
415 if (list_size < geom.X * geom.Y + start_i) {
416 list_size -= MYMAX(start_i, 0);
417 geom.Y = list_size / geom.X;
418 geom.Y += list_size % geom.X > 0 ? 1 : 0;
423 if (!data->explicit_size)
424 warningstream << "invalid use of list without a size[] element" << std::endl;
430 258 + m_fields.size()
434 core::rect<s32> rect;
436 if (data->real_coordinates) {
437 pos = getRealCoordinateBasePos(v_pos);
438 rect = core::rect<s32>(pos.X, pos.Y,
439 pos.X + (geom.X - 1) * (imgsize.X * 1.25) + imgsize.X,
440 pos.Y + (geom.Y - 1) * (imgsize.Y * 1.25) + imgsize.Y);
442 pos = getElementBasePos(&v_pos);
443 rect = core::rect<s32>(pos.X, pos.Y,
444 pos.X + (geom.X - 1) * spacing.X + imgsize.X,
445 pos.Y + (geom.Y - 1) * spacing.Y + imgsize.Y);
448 gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
449 this, spec.fid, rect);
451 m_inventorylists.emplace_back(loc, listname, e, geom, start_i,
452 data->real_coordinates);
453 m_fields.push_back(spec);
456 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
459 void GUIFormSpecMenu::parseListRing(parserData* data, const std::string &element)
462 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
466 std::vector<std::string> parts = split(element, ';');
468 if (parts.size() == 2) {
469 std::string location = parts[0];
470 std::string listname = parts[1];
472 InventoryLocation loc;
474 if (location == "context" || location == "current_name")
475 loc = m_current_inventory_location;
477 loc.deSerialize(location);
479 m_inventory_rings.emplace_back(loc, listname);
483 if (element.empty() && m_inventorylists.size() > 1) {
484 size_t siz = m_inventorylists.size();
485 // insert the last two inv list elements into the list ring
486 const ListDrawSpec &spa = m_inventorylists[siz - 2];
487 const ListDrawSpec &spb = m_inventorylists[siz - 1];
488 m_inventory_rings.emplace_back(spa.inventoryloc, spa.listname);
489 m_inventory_rings.emplace_back(spb.inventoryloc, spb.listname);
493 errorstream<< "Invalid list ring element(" << parts.size() << ", "
494 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
497 void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
499 std::vector<std::string> parts = split(element,';');
501 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
502 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
504 std::vector<std::string> v_pos = split(parts[0],',');
505 std::string name = parts[1];
506 std::string label = parts[2];
507 std::string selected;
509 if (parts.size() >= 4)
512 MY_CHECKPOS("checkbox",0);
514 bool fselected = false;
516 if (selected == "true")
519 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
520 const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str());
521 s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH);
522 s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2;
525 core::rect<s32> rect;
527 if (data->real_coordinates) {
528 pos = getRealCoordinateBasePos(v_pos);
530 rect = core::rect<s32>(
533 pos.X + label_size.Width + cb_size + 7,
537 pos = getElementBasePos(&v_pos);
538 rect = core::rect<s32>(
540 pos.Y + imgsize.Y / 2 - y_center,
541 pos.X + label_size.Width + cb_size + 7,
542 pos.Y + imgsize.Y / 2 + y_center
548 wlabel, //Needed for displaying text on MSVC
553 spec.ftype = f_CheckBox;
555 gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect, this,
556 spec.fid, spec.flabel.c_str());
558 auto style = getStyleForElement("checkbox", name);
559 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
561 if (spec.fname == data->focused_fieldname) {
562 Environment->setFocus(e);
566 m_checkboxes.emplace_back(spec, e);
567 m_fields.push_back(spec);
570 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
573 void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
575 std::vector<std::string> parts = split(element,';');
577 if (parts.size() >= 5) {
578 std::vector<std::string> v_pos = split(parts[0],',');
579 std::vector<std::string> v_geom = split(parts[1],',');
580 std::string name = parts[3];
581 std::string value = parts[4];
583 MY_CHECKPOS("scrollbar",0);
584 MY_CHECKGEOM("scrollbar",1);
589 if (data->real_coordinates) {
590 pos = getRealCoordinateBasePos(v_pos);
591 dim = getRealCoordinateGeometry(v_geom);
593 pos = getElementBasePos(&v_pos);
594 dim.X = stof(v_geom[0]) * spacing.X;
595 dim.Y = stof(v_geom[1]) * spacing.Y;
598 core::rect<s32> rect =
599 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
608 bool is_horizontal = true;
610 if (parts[2] == "vertical")
611 is_horizontal = false;
613 spec.ftype = f_ScrollBar;
615 GUIScrollBar *e = new GUIScrollBar(Environment, this, spec.fid, rect,
616 is_horizontal, false);
618 auto style = getStyleForElement("scrollbar", name);
619 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
623 e->setPos(stoi(parts[4]));
625 e->setLargeStep(100);
627 m_scrollbars.emplace_back(spec,e);
628 m_fields.push_back(spec);
631 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
634 void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
636 std::vector<std::string> parts = split(element,';');
638 if ((parts.size() == 3) ||
639 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
641 std::vector<std::string> v_pos = split(parts[0],',');
642 std::vector<std::string> v_geom = split(parts[1],',');
643 std::string name = unescape_string(parts[2]);
645 MY_CHECKPOS("image", 0);
646 MY_CHECKGEOM("image", 1);
651 if (data->real_coordinates) {
652 pos = getRealCoordinateBasePos(v_pos);
653 geom = getRealCoordinateGeometry(v_geom);
655 pos = getElementBasePos(&v_pos);
656 geom.X = stof(v_geom[0]) * (float)imgsize.X;
657 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
660 if (!data->explicit_size)
661 warningstream<<"invalid use of image without a size[] element"<<std::endl;
663 video::ITexture *texture = m_tsrc->getTexture(name);
665 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
666 << std::endl << "\t" << name << std::endl;
674 258 + m_fields.size()
676 core::rect<s32> rect(pos, pos + geom);
677 gui::IGUIImage *e = Environment->addImage(rect, this, spec.fid, 0, true);
678 e->setImage(texture);
679 e->setScaleImage(true);
680 e->setNotClipped(true);
681 m_fields.push_back(spec);
686 if (parts.size() == 2) {
687 std::vector<std::string> v_pos = split(parts[0],',');
688 std::string name = unescape_string(parts[1]);
690 MY_CHECKPOS("image", 0);
692 v2s32 pos = getElementBasePos(&v_pos);
694 if (!data->explicit_size)
695 warningstream<<"invalid use of image without a size[] element"<<std::endl;
697 video::ITexture *texture = m_tsrc->getTexture(name);
699 errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
700 << std::endl << "\t" << name << std::endl;
708 258 + m_fields.size()
710 gui::IGUIImage *e = Environment->addImage(texture, pos, true, this,
712 e->setNotClipped(true);
713 m_fields.push_back(spec);
717 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
720 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
722 std::vector<std::string> parts = split(element,';');
724 if ((parts.size() == 3) ||
725 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
727 std::vector<std::string> v_pos = split(parts[0],',');
728 std::vector<std::string> v_geom = split(parts[1],',');
729 std::string name = parts[2];
731 MY_CHECKPOS("itemimage",0);
732 MY_CHECKGEOM("itemimage",1);
737 if (data->real_coordinates) {
738 pos = getRealCoordinateBasePos(v_pos);
739 geom = getRealCoordinateGeometry(v_geom);
741 pos = getElementBasePos(&v_pos);
742 geom.X = stof(v_geom[0]) * (float)imgsize.X;
743 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
746 if(!data->explicit_size)
747 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
753 258 + m_fields.size()
755 spec.ftype = f_ItemImage;
757 GUIItemImage *e = new GUIItemImage(Environment, this, spec.fid,
758 core::rect<s32>(pos, pos + geom), name, m_font, m_client);
761 m_fields.push_back(spec);
764 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
767 void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
768 const std::string &type)
770 std::vector<std::string> parts = split(element,';');
772 if ((parts.size() == 4) ||
773 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
775 std::vector<std::string> v_pos = split(parts[0],',');
776 std::vector<std::string> v_geom = split(parts[1],',');
777 std::string name = parts[2];
778 std::string label = parts[3];
780 MY_CHECKPOS("button",0);
781 MY_CHECKGEOM("button",1);
785 core::rect<s32> rect;
787 if (data->real_coordinates) {
788 pos = getRealCoordinateBasePos(v_pos);
789 geom = getRealCoordinateGeometry(v_geom);
790 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
793 pos = getElementBasePos(&v_pos);
794 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
795 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
797 rect = core::rect<s32>(pos.X, pos.Y - m_btn_height,
798 pos.X + geom.X, pos.Y + m_btn_height);
801 if(!data->explicit_size)
802 warningstream<<"invalid use of button without a size[] element"<<std::endl;
804 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
810 258 + m_fields.size()
812 spec.ftype = f_Button;
813 if(type == "button_exit")
816 GUIButton *e = GUIButton::addButton(Environment, rect, this, spec.fid, spec.flabel.c_str());
818 auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
819 if (style.isNotDefault(StyleSpec::BGCOLOR)) {
820 e->setColor(style.getColor(StyleSpec::BGCOLOR));
822 if (style.isNotDefault(StyleSpec::BGCOLOR_HOVERED)) {
823 e->setHoveredColor(style.getColor(StyleSpec::BGCOLOR_HOVERED));
825 if (style.isNotDefault(StyleSpec::BGCOLOR_PRESSED)) {
826 e->setPressedColor(style.getColor(StyleSpec::BGCOLOR_PRESSED));
829 if (style.isNotDefault(StyleSpec::TEXTCOLOR)) {
830 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR));
832 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
833 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
835 if (style.isNotDefault(StyleSpec::BGIMG)) {
836 std::string image_name = style.get(StyleSpec::BGIMG, "");
837 std::string hovered_image_name = style.get(StyleSpec::BGIMG_HOVERED, "");
838 std::string pressed_image_name = style.get(StyleSpec::BGIMG_PRESSED, "");
840 video::ITexture *texture = 0;
841 video::ITexture *hovered_texture = 0;
842 video::ITexture *pressed_texture = 0;
843 texture = m_tsrc->getTexture(image_name);
844 if (!hovered_image_name.empty())
845 hovered_texture = m_tsrc->getTexture(hovered_image_name);
847 hovered_texture = texture;
848 if (!pressed_image_name.empty())
849 pressed_texture = m_tsrc->getTexture(pressed_image_name);
851 pressed_texture = texture;
853 e->setUseAlphaChannel(style.getBool(StyleSpec::ALPHA, true));
854 e->setImage(guiScalingImageButton(
855 Environment->getVideoDriver(), texture, geom.X, geom.Y));
856 e->setHoveredImage(guiScalingImageButton(
857 Environment->getVideoDriver(), hovered_texture, geom.X, geom.Y));
858 e->setPressedImage(guiScalingImageButton(
859 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
860 e->setScaleImage(true);
863 if (spec.fname == data->focused_fieldname) {
864 Environment->setFocus(e);
867 m_fields.push_back(spec);
870 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
873 void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
875 std::vector<std::string> parts = split(element,';');
877 if ((parts.size() >= 3 && parts.size() <= 5) ||
878 (parts.size() > 5 && m_formspec_version > FORMSPEC_API_VERSION)) {
879 std::vector<std::string> v_pos = split(parts[0],',');
880 std::vector<std::string> v_geom = split(parts[1],',');
881 std::string name = unescape_string(parts[2]);
883 MY_CHECKPOS("background",0);
884 MY_CHECKGEOM("background",1);
889 if (data->real_coordinates) {
890 pos = getRealCoordinateBasePos(v_pos);
891 geom = getRealCoordinateGeometry(v_geom);
893 pos = getElementBasePos(&v_pos);
894 pos.X -= (spacing.X - (float)imgsize.X) / 2;
895 pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
897 geom.X = stof(v_geom[0]) * spacing.X;
898 geom.Y = stof(v_geom[1]) * spacing.Y;
902 if (parts.size() >= 4 && is_yes(parts[3])) {
903 if (data->real_coordinates) {
904 pos = getRealCoordinateBasePos(v_pos) * -1;
907 pos.X = stoi(v_pos[0]); //acts as offset
908 pos.Y = stoi(v_pos[1]);
913 core::rect<s32> middle;
914 if (parts.size() >= 5) {
915 std::vector<std::string> v_middle = split(parts[4], ',');
916 if (v_middle.size() == 1) {
917 s32 x = stoi(v_middle[0]);
918 middle.UpperLeftCorner = core::vector2di(x, x);
919 middle.LowerRightCorner = core::vector2di(-x, -x);
920 } else if (v_middle.size() == 2) {
921 s32 x = stoi(v_middle[0]);
922 s32 y = stoi(v_middle[1]);
923 middle.UpperLeftCorner = core::vector2di(x, y);
924 middle.LowerRightCorner = core::vector2di(-x, -y);
925 // `-x` is interpreted as `w - x`
926 } else if (v_middle.size() == 4) {
927 middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1]));
928 middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3]));
930 warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl;
934 if (!data->explicit_size && !clip)
935 warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
941 258 + m_fields.size()
944 core::rect<s32> rect;
946 // no auto_clip => position like normal image
947 rect = core::rect<s32>(pos, pos + geom);
949 // it will be auto-clipped when drawing
950 rect = core::rect<s32>(-pos, pos);
953 GUIBackgroundImage *e = new GUIBackgroundImage(Environment, this, spec.fid,
954 rect, name, middle, m_tsrc, clip);
956 FATAL_ERROR_IF(!e, "Failed to create background formspec element");
958 e->setNotClipped(true);
960 e->setVisible(false); // the element is drawn manually before all others
962 m_backgrounds.push_back(e);
963 m_fields.push_back(spec);
966 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
969 void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
971 std::vector<std::string> parts = split(element,';');
973 data->table_options.clear();
974 for (const std::string &part : parts) {
975 // Parse table option
976 std::string opt = unescape_string(part);
977 data->table_options.push_back(GUITable::splitOption(opt));
981 void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
983 std::vector<std::string> parts = split(element,';');
985 data->table_columns.clear();
986 for (const std::string &part : parts) {
987 std::vector<std::string> col_parts = split(part,',');
988 GUITable::TableColumn column;
990 if (!col_parts.empty())
991 column.type = col_parts[0];
992 // Parse column options
993 for (size_t j = 1; j < col_parts.size(); ++j) {
994 std::string opt = unescape_string(col_parts[j]);
995 column.options.push_back(GUITable::splitOption(opt));
997 data->table_columns.push_back(column);
1001 void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
1003 std::vector<std::string> parts = split(element,';');
1005 if (((parts.size() == 4) || (parts.size() == 5)) ||
1006 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1008 std::vector<std::string> v_pos = split(parts[0],',');
1009 std::vector<std::string> v_geom = split(parts[1],',');
1010 std::string name = parts[2];
1011 std::vector<std::string> items = split(parts[3],',');
1012 std::string str_initial_selection;
1013 std::string str_transparent = "false";
1015 if (parts.size() >= 5)
1016 str_initial_selection = parts[4];
1018 MY_CHECKPOS("table",0);
1019 MY_CHECKGEOM("table",1);
1024 if (data->real_coordinates) {
1025 pos = getRealCoordinateBasePos(v_pos);
1026 geom = getRealCoordinateGeometry(v_geom);
1028 pos = getElementBasePos(&v_pos);
1029 geom.X = stof(v_geom[0]) * spacing.X;
1030 geom.Y = stof(v_geom[1]) * spacing.Y;
1033 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1039 258 + m_fields.size()
1042 spec.ftype = f_Table;
1044 for (std::string &item : items) {
1045 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1048 //now really show table
1049 GUITable *e = new GUITable(Environment, this, spec.fid, rect, m_tsrc);
1051 if (spec.fname == data->focused_fieldname) {
1052 Environment->setFocus(e);
1055 e->setTable(data->table_options, data->table_columns, items);
1057 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1058 e->setDynamicData(data->table_dyndata[name]);
1061 if (!str_initial_selection.empty() && str_initial_selection != "0")
1062 e->setSelected(stoi(str_initial_selection));
1064 auto style = getStyleForElement("table", name);
1065 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1067 m_tables.emplace_back(spec, e);
1068 m_fields.push_back(spec);
1071 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
1074 void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
1076 std::vector<std::string> parts = split(element,';');
1078 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
1079 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1081 std::vector<std::string> v_pos = split(parts[0],',');
1082 std::vector<std::string> v_geom = split(parts[1],',');
1083 std::string name = parts[2];
1084 std::vector<std::string> items = split(parts[3],',');
1085 std::string str_initial_selection;
1086 std::string str_transparent = "false";
1088 if (parts.size() >= 5)
1089 str_initial_selection = parts[4];
1091 if (parts.size() >= 6)
1092 str_transparent = parts[5];
1094 MY_CHECKPOS("textlist",0);
1095 MY_CHECKGEOM("textlist",1);
1100 if (data->real_coordinates) {
1101 pos = getRealCoordinateBasePos(v_pos);
1102 geom = getRealCoordinateGeometry(v_geom);
1104 pos = getElementBasePos(&v_pos);
1105 geom.X = stof(v_geom[0]) * spacing.X;
1106 geom.Y = stof(v_geom[1]) * spacing.Y;
1109 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1115 258 + m_fields.size()
1118 spec.ftype = f_Table;
1120 for (std::string &item : items) {
1121 item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
1124 //now really show list
1125 GUITable *e = new GUITable(Environment, this, spec.fid, rect, m_tsrc);
1127 if (spec.fname == data->focused_fieldname) {
1128 Environment->setFocus(e);
1131 e->setTextList(items, is_yes(str_transparent));
1133 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
1134 e->setDynamicData(data->table_dyndata[name]);
1137 if (!str_initial_selection.empty() && str_initial_selection != "0")
1138 e->setSelected(stoi(str_initial_selection));
1140 auto style = getStyleForElement("textlist", name);
1141 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1143 m_tables.emplace_back(spec, e);
1144 m_fields.push_back(spec);
1147 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
1151 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
1153 std::vector<std::string> parts = split(element,';');
1155 if ((parts.size() == 5) ||
1156 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1158 std::vector<std::string> v_pos = split(parts[0],',');
1159 std::string name = parts[2];
1160 std::vector<std::string> items = split(parts[3],',');
1161 std::string str_initial_selection;
1162 str_initial_selection = parts[4];
1164 MY_CHECKPOS("dropdown",0);
1168 core::rect<s32> rect;
1170 if (data->real_coordinates) {
1171 std::vector<std::string> v_geom = split(parts[1],',');
1173 if (v_geom.size() == 1)
1174 v_geom.emplace_back("1");
1176 MY_CHECKGEOM("dropdown",1);
1178 pos = getRealCoordinateBasePos(v_pos);
1179 geom = getRealCoordinateGeometry(v_geom);
1180 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1182 pos = getElementBasePos(&v_pos);
1184 s32 width = stof(parts[1]) * spacing.Y;
1186 rect = core::rect<s32>(pos.X, pos.Y,
1187 pos.X + width, pos.Y + (m_btn_height * 2));
1194 258 + m_fields.size()
1197 spec.ftype = f_DropDown;
1200 //now really show list
1201 gui::IGUIComboBox *e = Environment->addComboBox(rect, this, spec.fid);
1203 if (spec.fname == data->focused_fieldname) {
1204 Environment->setFocus(e);
1207 for (const std::string &item : items) {
1208 e->addItem(unescape_translate(unescape_string(
1209 utf8_to_wide(item))).c_str());
1212 if (!str_initial_selection.empty())
1213 e->setSelected(stoi(str_initial_selection)-1);
1215 auto style = getStyleForElement("dropdown", name);
1216 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1218 m_fields.push_back(spec);
1220 m_dropdowns.emplace_back(spec, std::vector<std::string>());
1221 std::vector<std::string> &values = m_dropdowns.back().second;
1222 for (const std::string &item : items) {
1223 values.push_back(unescape_string(item));
1228 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
1229 << element << "'" << std::endl;
1232 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
1234 std::vector<std::string> parts = split(element,';');
1235 if (parts.size() == 2 ||
1236 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
1237 field_close_on_enter[parts[0]] = is_yes(parts[1]);
1241 void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
1243 std::vector<std::string> parts = split(element,';');
1245 if ((parts.size() == 4) || (parts.size() == 5) ||
1246 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1248 std::vector<std::string> v_pos = split(parts[0],',');
1249 std::vector<std::string> v_geom = split(parts[1],',');
1250 std::string name = parts[2];
1251 std::string label = parts[3];
1253 MY_CHECKPOS("pwdfield",0);
1254 MY_CHECKGEOM("pwdfield",1);
1259 if (data->real_coordinates) {
1260 pos = getRealCoordinateBasePos(v_pos);
1261 geom = getRealCoordinateGeometry(v_geom);
1263 pos = getElementBasePos(&v_pos);
1266 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1268 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1269 pos.Y -= m_btn_height;
1270 geom.Y = m_btn_height*2;
1273 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1275 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1281 258 + m_fields.size()
1285 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
1287 if (spec.fname == data->focused_fieldname) {
1288 Environment->setFocus(e);
1291 if (label.length() >= 1) {
1292 int font_height = g_fontengine->getTextHeight();
1293 rect.UpperLeftCorner.Y -= font_height;
1294 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1295 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1299 e->setPasswordBox(true,L'*');
1301 auto style = getStyleForElement("pwdfield", name, "field");
1302 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1303 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1304 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1307 evt.EventType = EET_KEY_INPUT_EVENT;
1308 evt.KeyInput.Key = KEY_END;
1309 evt.KeyInput.Char = 0;
1310 evt.KeyInput.Control = false;
1311 evt.KeyInput.Shift = false;
1312 evt.KeyInput.PressedDown = true;
1315 if (parts.size() >= 5) {
1316 // TODO: remove after 2016-11-03
1317 warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
1318 " instead of the 5th param" << std::endl;
1319 field_close_on_enter[name] = is_yes(parts[4]);
1322 m_fields.push_back(spec);
1325 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
1328 void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
1329 core::rect<s32> &rect, bool is_multiline)
1331 bool is_editable = !spec.fname.empty();
1332 if (!is_editable && !is_multiline) {
1333 // spec field id to 0, this stops submit searching for a value that isn't there
1334 gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1341 } else if (is_multiline &&
1342 spec.fdefault.empty() && !spec.flabel.empty()) {
1343 // Multiline textareas: swap default and label for backwards compat
1344 spec.flabel.swap(spec.fdefault);
1347 gui::IGUIEditBox *e = nullptr;
1348 static constexpr bool use_intl_edit_box = USE_FREETYPE &&
1349 IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9;
1351 if (use_intl_edit_box && g_settings->getBool("freetype")) {
1352 e = new gui::intlGUIEditBox(spec.fdefault.c_str(), true, Environment,
1353 this, spec.fid, rect, is_editable, is_multiline);
1356 e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true,
1357 Environment, this, spec.fid, rect, is_editable, true);
1358 } else if (is_editable) {
1359 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this,
1365 auto style = getStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
1368 if (is_editable && spec.fname == data->focused_fieldname)
1369 Environment->setFocus(e);
1372 e->setMultiLine(true);
1373 e->setWordWrap(true);
1374 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1377 evt.EventType = EET_KEY_INPUT_EVENT;
1378 evt.KeyInput.Key = KEY_END;
1379 evt.KeyInput.Char = 0;
1380 evt.KeyInput.Control = 0;
1381 evt.KeyInput.Shift = 0;
1382 evt.KeyInput.PressedDown = true;
1386 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1387 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1388 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1389 if (style.get(StyleSpec::BGCOLOR, "") == "transparent") {
1390 e->setDrawBackground(false);
1396 if (!spec.flabel.empty()) {
1397 int font_height = g_fontengine->getTextHeight();
1398 rect.UpperLeftCorner.Y -= font_height;
1399 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1400 IGUIElement *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
1401 rect, false, true, this, 0);
1404 t->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1408 void GUIFormSpecMenu::parseSimpleField(parserData *data,
1409 std::vector<std::string> &parts)
1411 std::string name = parts[0];
1412 std::string label = parts[1];
1413 std::string default_val = parts[2];
1415 core::rect<s32> rect;
1417 if (data->explicit_size)
1418 warningstream << "invalid use of unpositioned \"field\" in inventory" << std::endl;
1420 v2s32 pos = getElementBasePos(nullptr);
1421 pos.Y = (data->simple_field_count + 2) * 60;
1422 v2s32 size = DesiredRect.getSize();
1424 rect = core::rect<s32>(
1425 size.X / 2 - 150, pos.Y,
1426 size.X / 2 - 150 + 300, pos.Y + m_btn_height * 2
1431 default_val = m_form_src->resolveText(default_val);
1434 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1439 utf8_to_wide(unescape_string(default_val)),
1440 258 + m_fields.size()
1443 createTextField(data, spec, rect, false);
1445 m_fields.push_back(spec);
1447 data->simple_field_count++;
1450 void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
1451 const std::string &type)
1453 std::vector<std::string> v_pos = split(parts[0],',');
1454 std::vector<std::string> v_geom = split(parts[1],',');
1455 std::string name = parts[2];
1456 std::string label = parts[3];
1457 std::string default_val = parts[4];
1459 MY_CHECKPOS(type,0);
1460 MY_CHECKGEOM(type,1);
1465 if (data->real_coordinates) {
1466 pos = getRealCoordinateBasePos(v_pos);
1467 geom = getRealCoordinateGeometry(v_geom);
1469 pos = getElementBasePos(&v_pos);
1472 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1474 if (type == "textarea")
1476 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1477 pos.Y += m_btn_height;
1481 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1482 pos.Y -= m_btn_height;
1483 geom.Y = m_btn_height*2;
1487 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1489 if(!data->explicit_size)
1490 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1493 default_val = m_form_src->resolveText(default_val);
1496 std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
1501 utf8_to_wide(unescape_string(default_val)),
1502 258 + m_fields.size()
1505 createTextField(data, spec, rect, type == "textarea");
1507 if (parts.size() >= 6) {
1508 // TODO: remove after 2016-11-03
1509 warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
1510 " instead of the 6th param" << std::endl;
1511 field_close_on_enter[name] = is_yes(parts[5]);
1514 m_fields.push_back(spec);
1517 void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
1518 const std::string &type)
1520 std::vector<std::string> parts = split(element,';');
1522 if (parts.size() == 3 || parts.size() == 4) {
1523 parseSimpleField(data,parts);
1527 if ((parts.size() == 5) || (parts.size() == 6) ||
1528 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1530 parseTextArea(data,parts,type);
1533 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1536 void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &element)
1538 std::vector<std::string> parts = split(element, ';');
1540 if (parts.size() != 4 && m_formspec_version < FORMSPEC_API_VERSION) {
1541 errorstream << "Invalid text element(" << parts.size() << "): '" << element << "'" << std::endl;
1545 std::vector<std::string> v_pos = split(parts[0], ',');
1546 std::vector<std::string> v_geom = split(parts[1], ',');
1547 std::string name = parts[2];
1548 std::string text = parts[3];
1550 MY_CHECKPOS("hypertext", 0);
1551 MY_CHECKGEOM("hypertext", 1);
1556 if (data->real_coordinates) {
1557 pos = getRealCoordinateBasePos(v_pos);
1558 geom = getRealCoordinateGeometry(v_geom);
1560 pos = getElementBasePos(&v_pos);
1563 pos.X += stof(v_pos[0]) * spacing.X;
1564 pos.Y += stof(v_pos[1]) * spacing.Y + (m_btn_height * 2);
1566 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1567 geom.Y = (stof(v_geom[1]) * imgsize.Y) - (spacing.Y - imgsize.Y);
1570 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X + geom.X, pos.Y + geom.Y);
1573 text = m_form_src->resolveText(text);
1577 utf8_to_wide(unescape_string(text)),
1579 258 + m_fields.size()
1582 spec.ftype = f_Unknown;
1584 spec.flabel.c_str(), Environment, this, spec.fid, rect, m_client, m_tsrc);
1586 m_fields.push_back(spec);
1589 void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
1591 std::vector<std::string> parts = split(element,';');
1593 if ((parts.size() == 2) ||
1594 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1596 std::vector<std::string> v_pos = split(parts[0],',');
1597 std::string text = parts[1];
1599 MY_CHECKPOS("label",0);
1601 if(!data->explicit_size)
1602 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1604 std::vector<std::string> lines = split(text, '\n');
1606 for (unsigned int i = 0; i != lines.size(); i++) {
1607 std::wstring wlabel_colors = translate_string(
1608 utf8_to_wide(unescape_string(lines[i])));
1609 // Without color escapes to get the font dimensions
1610 std::wstring wlabel_plain = unescape_enriched(wlabel_colors);
1612 core::rect<s32> rect;
1614 if (data->real_coordinates) {
1615 // Lines are spaced at the distance of 1/2 imgsize.
1616 // This alows lines that line up with the new elements
1617 // easily without sacrificing good line distance. If
1618 // it was one whole imgsize, it would have too much
1620 v2s32 pos = getRealCoordinateBasePos(v_pos);
1622 // Labels are positioned by their center, not their top.
1623 pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2);
1625 rect = core::rect<s32>(
1627 pos.X + m_font->getDimension(wlabel_plain.c_str()).Width,
1631 // Lines are spaced at the nominal distance of
1632 // 2/5 inventory slot, even if the font doesn't
1633 // quite match that. This provides consistent
1634 // form layout, at the expense of sometimes
1635 // having sub-optimal spacing for the font.
1636 // We multiply by 2 and then divide by 5, rather
1637 // than multiply by 0.4, to get exact results
1638 // in the integer cases: 0.4 is not exactly
1639 // representable in binary floating point.
1641 v2s32 pos = getElementBasePos(nullptr);
1642 pos.X += stof(v_pos[0]) * spacing.X;
1643 pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
1645 pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0;
1647 rect = core::rect<s32>(
1648 pos.X, pos.Y - m_btn_height,
1649 pos.X + m_font->getDimension(wlabel_plain.c_str()).Width,
1650 pos.Y + m_btn_height);
1657 258 + m_fields.size()
1659 gui::IGUIStaticText *e = gui::StaticText::add(Environment,
1660 spec.flabel.c_str(), rect, false, false, this, spec.fid);
1661 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
1663 auto style = getStyleForElement("label", spec.fname);
1664 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1665 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1667 m_fields.push_back(spec);
1672 errorstream << "Invalid label element(" << parts.size() << "): '" << element
1673 << "'" << std::endl;
1676 void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
1678 std::vector<std::string> parts = split(element,';');
1680 if ((parts.size() == 2) ||
1681 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1683 std::vector<std::string> v_pos = split(parts[0],',');
1684 std::wstring text = unescape_translate(
1685 unescape_string(utf8_to_wide(parts[1])));
1687 MY_CHECKPOS("vertlabel",1);
1690 core::rect<s32> rect;
1692 if (data->real_coordinates) {
1693 pos = getRealCoordinateBasePos(v_pos);
1695 // Vertlabels are positioned by center, not left.
1696 pos.X -= imgsize.X / 2;
1698 // We use text.length + 1 because without it, the rect
1699 // isn't quite tall enough and cuts off the text.
1700 rect = core::rect<s32>(pos.X, pos.Y,
1702 pos.Y + font_line_height(m_font) *
1703 (text.length() + 1));
1706 pos = getElementBasePos(&v_pos);
1708 // As above, the length must be one longer. The width of
1709 // the rect (15 pixels) seems rather arbitrary, but
1710 // changing it might break something.
1711 rect = core::rect<s32>(
1712 pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
1714 font_line_height(m_font) *
1715 (text.length() + 1) +
1716 ((imgsize.Y/2) - m_btn_height));
1719 if(!data->explicit_size)
1720 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1724 for (wchar_t i : text) {
1733 258 + m_fields.size()
1735 gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
1736 rect, false, false, this, spec.fid);
1737 e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1739 auto style = getStyleForElement("vertlabel", spec.fname, "label");
1740 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1741 e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1743 m_fields.push_back(spec);
1746 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1749 void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
1750 const std::string &type)
1752 std::vector<std::string> parts = split(element,';');
1754 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1755 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1757 std::vector<std::string> v_pos = split(parts[0],',');
1758 std::vector<std::string> v_geom = split(parts[1],',');
1759 std::string image_name = parts[2];
1760 std::string name = parts[3];
1761 std::string label = parts[4];
1763 MY_CHECKPOS("imagebutton",0);
1764 MY_CHECKGEOM("imagebutton",1);
1766 bool noclip = false;
1767 bool drawborder = true;
1768 std::string pressed_image_name;
1770 if (parts.size() >= 7) {
1771 if (parts[5] == "true")
1773 if (parts[6] == "false")
1777 if (parts.size() >= 8) {
1778 pressed_image_name = parts[7];
1784 if (data->real_coordinates) {
1785 pos = getRealCoordinateBasePos(v_pos);
1786 geom = getRealCoordinateGeometry(v_geom);
1788 pos = getElementBasePos(&v_pos);
1789 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1790 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1793 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1796 if (!data->explicit_size)
1797 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1799 image_name = unescape_string(image_name);
1800 pressed_image_name = unescape_string(pressed_image_name);
1802 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1807 utf8_to_wide(image_name),
1808 258 + m_fields.size()
1810 spec.ftype = f_Button;
1811 if (type == "image_button_exit")
1812 spec.is_exit = true;
1814 video::ITexture *texture = 0;
1815 video::ITexture *pressed_texture = 0;
1816 texture = m_tsrc->getTexture(image_name);
1817 if (!pressed_image_name.empty())
1818 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1820 pressed_texture = texture;
1822 GUIButton *e = GUIButton::addButton(Environment, rect, this, spec.fid, spec.flabel.c_str());
1824 if (spec.fname == data->focused_fieldname) {
1825 Environment->setFocus(e);
1828 auto style = getStyleForElement("image_button", spec.fname);
1830 e->setUseAlphaChannel(style.getBool(StyleSpec::ALPHA, true));
1831 e->setImage(guiScalingImageButton(
1832 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1833 e->setPressedImage(guiScalingImageButton(
1834 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1835 e->setScaleImage(true);
1836 if (parts.size() >= 7) {
1837 e->setNotClipped(noclip);
1838 e->setDrawBorder(drawborder);
1840 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
1841 e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
1844 m_fields.push_back(spec);
1848 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1851 void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
1853 std::vector<std::string> parts = split(element, ';');
1855 if (((parts.size() == 4) || (parts.size() == 6)) || (parts.size() == 7 &&
1856 data->real_coordinates) || ((parts.size() > 6) &&
1857 (m_formspec_version > FORMSPEC_API_VERSION)))
1859 std::vector<std::string> v_pos = split(parts[0],',');
1861 // If we're using real coordinates, add an extra field for height.
1862 // Width is not here because tabs are the width of the text, and
1863 // there's no reason to change that.
1865 std::vector<std::string> v_geom = {"1", "0.75"}; // Dummy width and default height
1866 bool auto_width = true;
1867 if (parts.size() == 7) {
1870 v_geom = split(parts[1], ',');
1871 if (v_geom.size() == 1)
1872 v_geom.insert(v_geom.begin(), "1"); // Dummy value
1877 std::string name = parts[i+1];
1878 std::vector<std::string> buttons = split(parts[i+2], ',');
1879 std::string str_index = parts[i+3];
1880 bool show_background = true;
1881 bool show_border = true;
1882 int tab_index = stoi(str_index) - 1;
1884 MY_CHECKPOS("tabheader", 0);
1886 if (parts.size() == 6 + i) {
1887 if (parts[4+i] == "true")
1888 show_background = false;
1889 if (parts[5+i] == "false")
1890 show_border = false;
1897 258 + m_fields.size()
1900 spec.ftype = f_TabHeader;
1905 if (data->real_coordinates) {
1906 pos = getRealCoordinateBasePos(v_pos);
1908 geom = getRealCoordinateGeometry(v_geom);
1909 pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
1911 geom.X = DesiredRect.getWidth(); // Set automatic width
1913 MY_CHECKGEOM("tabheader", 1);
1915 v2f32 pos_f = pos_offset * spacing;
1916 pos_f.X += stof(v_pos[0]) * spacing.X;
1917 pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
1918 pos = v2s32(pos_f.X, pos_f.Y);
1920 geom.Y = m_btn_height * 2;
1921 geom.X = DesiredRect.getWidth();
1924 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1927 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1928 show_background, show_border, spec.fid);
1929 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1930 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1931 e->setTabHeight(geom.Y);
1933 if (spec.fname == data->focused_fieldname) {
1934 Environment->setFocus(e);
1937 auto style = getStyleForElement("tabheader", name);
1938 e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
1940 for (const std::string &button : buttons) {
1941 auto tab = e->addTab(unescape_translate(unescape_string(
1942 utf8_to_wide(button))).c_str(), -1);
1943 if (style.isNotDefault(StyleSpec::BGCOLOR))
1944 tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
1946 tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
1949 if ((tab_index >= 0) &&
1950 (buttons.size() < INT_MAX) &&
1951 (tab_index < (int) buttons.size()))
1952 e->setActiveTab(tab_index);
1954 m_fields.push_back(spec);
1957 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1958 << element << "'" << std::endl;
1961 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
1964 if (m_client == 0) {
1965 warningstream << "invalid use of item_image_button with m_client==0"
1970 std::vector<std::string> parts = split(element,';');
1972 if ((parts.size() == 5) ||
1973 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1975 std::vector<std::string> v_pos = split(parts[0],',');
1976 std::vector<std::string> v_geom = split(parts[1],',');
1977 std::string item_name = parts[2];
1978 std::string name = parts[3];
1979 std::string label = parts[4];
1981 label = unescape_string(label);
1982 item_name = unescape_string(item_name);
1984 MY_CHECKPOS("itemimagebutton",0);
1985 MY_CHECKGEOM("itemimagebutton",1);
1990 if (data->real_coordinates) {
1991 pos = getRealCoordinateBasePos(v_pos);
1992 geom = getRealCoordinateGeometry(v_geom);
1994 pos = getElementBasePos(&v_pos);
1995 geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
1996 geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
1999 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
2001 if(!data->explicit_size)
2002 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
2004 IItemDefManager *idef = m_client->idef();
2006 item.deSerialize(item_name, idef);
2009 TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
2010 m_default_tooltip_bgcolor,
2011 m_default_tooltip_color);
2013 // the spec for the button
2016 utf8_to_wide(label),
2017 utf8_to_wide(item_name),
2018 258 + m_fields.size()
2021 gui::IGUIButton *e_btn = GUIButton::addButton(Environment, rect, this, spec_btn.fid, L"");
2023 auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
2024 e_btn->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
2025 e_btn->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
2027 if (spec_btn.fname == data->focused_fieldname) {
2028 Environment->setFocus(e_btn);
2031 spec_btn.ftype = f_Button;
2032 rect += data->basepos-padding;
2033 spec_btn.rect = rect;
2034 m_fields.push_back(spec_btn);
2036 // the spec for the item-image
2041 258 + m_fields.size()
2044 GUIItemImage *e_img = new GUIItemImage(Environment, e_btn, spec_img.fid,
2045 core::rect<s32>(0, 0, geom.X, geom.Y), item_name, m_font, m_client);
2047 e_img->setText(utf8_to_wide(label).c_str());
2051 m_fields.push_back(spec_img);
2054 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
2057 void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
2059 std::vector<std::string> parts = split(element,';');
2061 if ((parts.size() == 3) ||
2062 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
2064 std::vector<std::string> v_pos = split(parts[0],',');
2065 std::vector<std::string> v_geom = split(parts[1],',');
2067 MY_CHECKPOS("box",0);
2068 MY_CHECKGEOM("box",1);
2073 if (data->real_coordinates) {
2074 pos = getRealCoordinateBasePos(v_pos);
2075 geom = getRealCoordinateGeometry(v_geom);
2077 pos = getElementBasePos(&v_pos);
2078 geom.X = stof(v_geom[0]) * spacing.X;
2079 geom.Y = stof(v_geom[1]) * spacing.Y;
2082 video::SColor tmp_color;
2084 if (parseColorString(parts[2], tmp_color, false, 0x8C)) {
2089 258 + m_fields.size()
2093 core::rect<s32> rect(pos, pos + geom);
2095 GUIBox *e = new GUIBox(Environment, this, spec.fid, rect, tmp_color);
2097 e->setNotClipped(true);
2101 m_fields.push_back(spec);
2104 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
2108 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
2111 void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
2113 std::vector<std::string> parts = split(element,';');
2115 if (((parts.size() == 1) || (parts.size() == 2)) ||
2116 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION))) {
2117 parseColorString(parts[0], m_bgcolor, false);
2119 if (parts.size() == 2) {
2120 std::string fullscreen = parts[1];
2121 m_bgfullscreen = is_yes(fullscreen);
2127 errorstream << "Invalid bgcolor element(" << parts.size() << "): '" << element << "'"
2131 void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
2133 std::vector<std::string> parts = split(element,';');
2135 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
2136 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
2138 parseColorString(parts[0], m_slotbg_n, false);
2139 parseColorString(parts[1], m_slotbg_h, false);
2141 if (parts.size() >= 3) {
2142 if (parseColorString(parts[2], m_slotbordercolor, false)) {
2143 m_slotborder = true;
2146 if (parts.size() == 5) {
2147 video::SColor tmp_color;
2149 if (parseColorString(parts[3], tmp_color, false))
2150 m_default_tooltip_bgcolor = tmp_color;
2151 if (parseColorString(parts[4], tmp_color, false))
2152 m_default_tooltip_color = tmp_color;
2156 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
2159 void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
2161 std::vector<std::string> parts = split(element,';');
2162 if (parts.size() < 2) {
2163 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2164 << element << "'" << std::endl;
2168 // Get mode and check size
2169 bool rect_mode = parts[0].find(',') != std::string::npos;
2170 size_t base_size = rect_mode ? 3 : 2;
2171 if (parts.size() != base_size && parts.size() != base_size + 2) {
2172 errorstream << "Invalid tooltip element(" << parts.size() << "): '"
2173 << element << "'" << std::endl;
2178 video::SColor bgcolor = m_default_tooltip_bgcolor;
2179 video::SColor color = m_default_tooltip_color;
2180 if (parts.size() == base_size + 2 &&
2181 (!parseColorString(parts[base_size], bgcolor, false) ||
2182 !parseColorString(parts[base_size + 1], color, false))) {
2183 errorstream << "Invalid color in tooltip element(" << parts.size()
2184 << "): '" << element << "'" << std::endl;
2188 // Make tooltip spec
2189 std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
2190 TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
2194 std::vector<std::string> v_pos = split(parts[0], ',');
2195 std::vector<std::string> v_geom = split(parts[1], ',');
2197 MY_CHECKPOS("tooltip", 0);
2198 MY_CHECKGEOM("tooltip", 1);
2203 if (data->real_coordinates) {
2204 pos = getRealCoordinateBasePos(v_pos);
2205 geom = getRealCoordinateGeometry(v_geom);
2207 pos = getElementBasePos(&v_pos);
2208 geom.X = stof(v_geom[0]) * spacing.X;
2209 geom.Y = stof(v_geom[1]) * spacing.Y;
2212 FieldSpec fieldspec(
2216 258 + m_fields.size()
2219 core::rect<s32> rect(pos, pos + geom);
2221 gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
2222 this, fieldspec.fid, rect);
2224 m_fields.push_back(fieldspec);
2225 m_tooltip_rects.emplace_back(e, spec);
2228 m_tooltips[parts[0]] = spec;
2232 bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
2238 std::vector<std::string> parts = split(data,'[');
2240 if (parts.size() < 2) {
2244 if (parts[0] != "formspec_version") {
2248 if (is_number(parts[1])) {
2249 m_formspec_version = mystoi(parts[1]);
2256 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
2258 if (element.empty())
2261 std::vector<std::string> parts = split(element,'[');
2263 if (parts.size() < 2)
2266 std::string type = trim(parts[0]);
2267 std::string description = trim(parts[1]);
2269 if (type != "size" && type != "invsize")
2272 if (type == "invsize")
2273 log_deprecated("Deprecated formspec element \"invsize\" is used");
2275 parseSize(data, description);
2280 bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
2282 if (element.empty())
2285 std::vector<std::string> parts = split(element, '[');
2287 if (parts.size() != 2)
2290 std::string type = trim(parts[0]);
2291 std::string description = trim(parts[1]);
2293 if (type != "position")
2296 parsePosition(data, description);
2301 void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
2303 std::vector<std::string> parts = split(element, ',');
2305 if (parts.size() == 2) {
2306 data->offset.X = stof(parts[0]);
2307 data->offset.Y = stof(parts[1]);
2311 errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
2314 bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
2316 if (element.empty())
2319 std::vector<std::string> parts = split(element, '[');
2321 if (parts.size() != 2)
2324 std::string type = trim(parts[0]);
2325 std::string description = trim(parts[1]);
2327 if (type != "anchor")
2330 parseAnchor(data, description);
2335 void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
2337 std::vector<std::string> parts = split(element, ',');
2339 if (parts.size() == 2) {
2340 data->anchor.X = stof(parts[0]);
2341 data->anchor.Y = stof(parts[1]);
2345 errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
2346 << "'" << std::endl;
2349 bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type)
2351 std::vector<std::string> parts = split(element, ';');
2353 if (parts.size() < 2) {
2354 errorstream << "Invalid style element (" << parts.size() << "): '" << element
2355 << "'" << std::endl;
2359 std::string selector = trim(parts[0]);
2360 if (selector.empty()) {
2361 errorstream << "Invalid style element (Selector required): '" << element
2362 << "'" << std::endl;
2368 for (size_t i = 1; i < parts.size(); i++) {
2369 size_t equal_pos = parts[i].find('=');
2370 if (equal_pos == std::string::npos) {
2371 errorstream << "Invalid style element (Property missing value): '" << element
2372 << "'" << std::endl;
2376 std::string propname = trim(parts[i].substr(0, equal_pos));
2377 std::string value = trim(unescape_string(parts[i].substr(equal_pos + 1)));
2379 std::transform(propname.begin(), propname.end(), propname.begin(), ::tolower);
2381 StyleSpec::Property prop = StyleSpec::GetPropertyByName(propname);
2382 if (prop == StyleSpec::NONE) {
2383 if (property_warned.find(propname) != property_warned.end()) {
2384 warningstream << "Invalid style element (Unknown property " << propname << "): '"
2386 << "'" << std::endl;
2387 property_warned.insert(propname);
2392 spec.set(prop, value);
2396 theme_by_type[selector] |= spec;
2398 theme_by_name[selector] |= spec;
2404 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
2407 if (element.empty())
2410 if (parseVersionDirect(element))
2413 std::vector<std::string> parts = split(element,'[');
2415 // ugly workaround to keep compatibility
2416 if (parts.size() > 2) {
2417 if (trim(parts[0]) == "image") {
2418 for (unsigned int i=2;i< parts.size(); i++) {
2419 parts[1] += "[" + parts[i];
2425 if (parts.size() < 2) {
2429 std::string type = trim(parts[0]);
2430 std::string description = trim(parts[1]);
2432 if (type == "container") {
2433 parseContainer(data, description);
2437 if (type == "container_end") {
2438 parseContainerEnd(data);
2442 if (type == "list") {
2443 parseList(data, description);
2447 if (type == "listring") {
2448 parseListRing(data, description);
2452 if (type == "checkbox") {
2453 parseCheckbox(data, description);
2457 if (type == "image") {
2458 parseImage(data, description);
2462 if (type == "item_image") {
2463 parseItemImage(data, description);
2467 if (type == "button" || type == "button_exit") {
2468 parseButton(data, description, type);
2472 if (type == "background" || type == "background9") {
2473 parseBackground(data, description);
2477 if (type == "tableoptions"){
2478 parseTableOptions(data,description);
2482 if (type == "tablecolumns"){
2483 parseTableColumns(data,description);
2487 if (type == "table"){
2488 parseTable(data,description);
2492 if (type == "textlist"){
2493 parseTextList(data,description);
2497 if (type == "dropdown"){
2498 parseDropDown(data,description);
2502 if (type == "field_close_on_enter") {
2503 parseFieldCloseOnEnter(data, description);
2507 if (type == "pwdfield") {
2508 parsePwdField(data,description);
2512 if ((type == "field") || (type == "textarea")){
2513 parseField(data,description,type);
2517 if (type == "hypertext") {
2518 parseHyperText(data,description);
2522 if (type == "label") {
2523 parseLabel(data,description);
2527 if (type == "vertlabel") {
2528 parseVertLabel(data,description);
2532 if (type == "item_image_button") {
2533 parseItemImageButton(data,description);
2537 if ((type == "image_button") || (type == "image_button_exit")) {
2538 parseImageButton(data,description,type);
2542 if (type == "tabheader") {
2543 parseTabHeader(data,description);
2547 if (type == "box") {
2548 parseBox(data,description);
2552 if (type == "bgcolor") {
2553 parseBackgroundColor(data,description);
2557 if (type == "listcolors") {
2558 parseListColors(data,description);
2562 if (type == "tooltip") {
2563 parseTooltip(data,description);
2567 if (type == "scrollbar") {
2568 parseScrollBar(data, description);
2572 if (type == "real_coordinates") {
2573 data->real_coordinates = is_yes(description);
2577 if (type == "style") {
2578 parseStyle(data, description, false);
2582 if (type == "style_type") {
2583 parseStyle(data, description, true);
2588 infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
2592 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
2594 /* useless to regenerate without a screensize */
2595 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
2602 for (auto &m_table : m_tables) {
2603 std::string tablename = m_table.first.fname;
2604 GUITable *table = m_table.second;
2605 mydata.table_dyndata[tablename] = table->getDynamicData();
2609 if (!m_focused_element.empty())
2610 mydata.focused_fieldname = m_focused_element;
2613 gui::IGUIElement *focused_element = Environment->getFocus();
2614 if (focused_element && focused_element->getParent() == this) {
2615 s32 focused_id = focused_element->getID();
2616 if (focused_id > 257) {
2617 for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
2618 if (field.fid == focused_id) {
2619 mydata.focused_fieldname = field.fname;
2629 for (auto &table_it : m_tables) {
2630 table_it.second->drop();
2632 for (auto &inventorylist_it : m_inventorylists) {
2633 inventorylist_it.e->drop();
2635 for (auto &checkbox_it : m_checkboxes) {
2636 checkbox_it.second->drop();
2638 for (auto &scrollbar_it : m_scrollbars) {
2639 scrollbar_it.second->drop();
2641 for (auto &background_it : m_backgrounds) {
2642 background_it->drop();
2644 for (auto &tooltip_rect_it : m_tooltip_rects) {
2645 tooltip_rect_it.first->drop();
2648 mydata.size= v2s32(100,100);
2649 mydata.screensize = screensize;
2650 mydata.offset = v2f32(0.5f, 0.5f);
2651 mydata.anchor = v2f32(0.5f, 0.5f);
2652 mydata.simple_field_count = 0;
2654 // Base position of contents of form
2655 mydata.basepos = getBasePos();
2657 /* Convert m_init_draw_spec to m_inventorylists */
2659 m_inventorylists.clear();
2660 m_backgrounds.clear();
2662 m_checkboxes.clear();
2663 m_scrollbars.clear();
2666 m_tooltip_rects.clear();
2667 m_inventory_rings.clear();
2668 m_dropdowns.clear();
2669 theme_by_name.clear();
2670 theme_by_type.clear();
2672 m_bgfullscreen = false;
2674 m_formspec_version = 1;
2677 v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
2678 m_bgcolor = video::SColor(
2679 (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
2680 clamp_u8(myround(formspec_bgcolor.X)),
2681 clamp_u8(myround(formspec_bgcolor.Y)),
2682 clamp_u8(myround(formspec_bgcolor.Z))
2687 v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
2688 m_fullscreen_bgcolor = video::SColor(
2689 (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
2690 clamp_u8(myround(formspec_bgcolor.X)),
2691 clamp_u8(myround(formspec_bgcolor.Y)),
2692 clamp_u8(myround(formspec_bgcolor.Z))
2696 m_slotbg_n = video::SColor(255,128,128,128);
2697 m_slotbg_h = video::SColor(255,192,192,192);
2699 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
2700 m_default_tooltip_color = video::SColor(255,255,255,255);
2702 m_slotbordercolor = video::SColor(200,0,0,0);
2703 m_slotborder = false;
2707 assert(!m_tooltip_element);
2708 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
2709 m_tooltip_element = gui::StaticText::add(Environment, L"",
2710 core::rect<s32>(0, 0, 110, 18));
2711 m_tooltip_element->enableOverrideColor(true);
2712 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2713 m_tooltip_element->setDrawBackground(true);
2714 m_tooltip_element->setDrawBorder(true);
2715 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2716 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
2717 m_tooltip_element->setWordWrap(false);
2718 //we're not parent so no autograb for this one!
2719 m_tooltip_element->grab();
2722 std::vector<std::string> elements = split(m_formspec_string,']');
2725 /* try to read version from first element only */
2726 if (!elements.empty()) {
2727 if (parseVersionDirect(elements[0])) {
2732 /* we need size first in order to calculate image scale */
2733 mydata.explicit_size = false;
2734 for (; i< elements.size(); i++) {
2735 if (!parseSizeDirect(&mydata, elements[i])) {
2740 /* "position" element is always after "size" element if it used */
2741 for (; i< elements.size(); i++) {
2742 if (!parsePositionDirect(&mydata, elements[i])) {
2747 /* "anchor" element is always after "position" (or "size" element) if it used */
2748 for (; i< elements.size(); i++) {
2749 if (!parseAnchorDirect(&mydata, elements[i])) {
2754 /* "no_prepend" element is always after "position" (or "size" element) if it used */
2755 bool enable_prepends = true;
2756 for (; i < elements.size(); i++) {
2757 if (elements[i].empty())
2760 std::vector<std::string> parts = split(elements[i], '[');
2761 if (trim(parts[0]) == "no_prepend")
2762 enable_prepends = false;
2767 /* Copy of the "real_coordinates" element for after the form size. */
2768 mydata.real_coordinates = m_formspec_version >= 2;
2769 for (; i < elements.size(); i++) {
2770 std::vector<std::string> parts = split(elements[i], '[');
2771 std::string name = trim(parts[0]);
2772 if (name != "real_coordinates" || parts.size() != 2)
2773 break; // Invalid format
2775 mydata.real_coordinates = is_yes(trim(parts[1]));
2778 if (mydata.explicit_size) {
2779 // compute scaling for specified form size
2781 v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
2782 v2u32 delta = current_screensize - m_lockscreensize;
2784 if (current_screensize.Y > m_lockscreensize.Y)
2789 if (current_screensize.X > m_lockscreensize.X)
2794 offset = v2s32(delta.X,delta.Y);
2796 mydata.screensize = m_lockscreensize;
2798 offset = v2s32(0,0);
2801 double gui_scaling = g_settings->getFloat("gui_scaling");
2802 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2806 // In fixed-size mode, inventory image size
2807 // is 0.53 inch multiplied by the gui_scaling
2808 // config parameter. This magic size is chosen
2809 // to make the main menu (15.5 inventory images
2810 // wide, including border) just fit into the
2811 // default window (800 pixels wide) at 96 DPI
2812 // and default scaling (1.00).
2813 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
2815 // In variable-size mode, we prefer to make the
2816 // inventory image size 1/15 of screen height,
2817 // multiplied by the gui_scaling config parameter.
2818 // If the preferred size won't fit the whole
2819 // form on the screen, either horizontally or
2820 // vertically, then we scale it down to fit.
2821 // (The magic numbers in the computation of what
2822 // fits arise from the scaling factors in the
2823 // following stanza, including the form border,
2824 // help text space, and 0.1 inventory slot spare.)
2825 // However, a minimum size is also set, that
2826 // the image size can't be less than 0.3 inch
2827 // multiplied by gui_scaling, even if this means
2828 // the form doesn't fit the screen.
2830 // For mobile devices these magic numbers are
2831 // different and forms should always use the
2832 // maximum screen space available.
2833 double prefer_imgsize = mydata.screensize.Y / 10 * gui_scaling;
2834 double fitx_imgsize = mydata.screensize.X /
2835 ((12.0 / 8.0) * (0.5 + mydata.invsize.X));
2836 double fity_imgsize = mydata.screensize.Y /
2837 ((15.0 / 11.0) * (0.85 + mydata.invsize.Y));
2838 use_imgsize = MYMIN(prefer_imgsize,
2839 MYMIN(fitx_imgsize, fity_imgsize));
2841 double prefer_imgsize = mydata.screensize.Y / 15 * gui_scaling;
2842 double fitx_imgsize = mydata.screensize.X /
2843 ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
2844 double fity_imgsize = mydata.screensize.Y /
2845 ((15.0 / 13.0) * (0.85 * mydata.invsize.Y));
2846 double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
2847 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
2848 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2849 MYMIN(fitx_imgsize, fity_imgsize)));
2853 // Everything else is scaled in proportion to the
2854 // inventory image size. The inventory slot spacing
2855 // is 5/4 image size horizontally and 15/13 image size
2856 // vertically. The padding around the form (incorporating
2857 // the border of the outer inventory slots) is 3/8
2858 // image size. Font height (baseline to baseline)
2859 // is 2/5 vertical inventory slot spacing, and button
2860 // half-height is 7/8 of font height.
2861 imgsize = v2s32(use_imgsize, use_imgsize);
2862 spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2863 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2864 m_btn_height = use_imgsize*15.0/13 * 0.35;
2866 m_font = g_fontengine->getFont();
2868 if (mydata.real_coordinates) {
2869 mydata.size = v2s32(
2870 mydata.invsize.X*imgsize.X,
2871 mydata.invsize.Y*imgsize.Y
2874 mydata.size = v2s32(
2875 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2876 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2880 DesiredRect = mydata.rect = core::rect<s32>(
2881 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
2882 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
2883 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
2884 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
2887 // Non-size[] form must consist only of text fields and
2888 // implicit "Proceed" button. Use default font, and
2889 // temporary form size which will be recalculated below.
2890 m_font = g_fontengine->getFont();
2891 m_btn_height = font_line_height(m_font) * 0.875;
2892 DesiredRect = core::rect<s32>(
2893 (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
2894 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
2895 (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
2896 (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
2899 recalculateAbsolutePosition(false);
2900 mydata.basepos = getBasePos();
2901 m_tooltip_element->setOverrideFont(m_font);
2903 gui::IGUISkin *skin = Environment->getSkin();
2905 gui::IGUIFont *old_font = skin->getFont();
2906 skin->setFont(m_font);
2908 pos_offset = v2f32();
2910 // used for formspec versions < 3
2911 core::list<IGUIElement *>::Iterator legacy_sort_start = Children.getLast();
2913 if (enable_prepends) {
2914 // Backup the coordinates so that prepends can use the coordinates of choice.
2915 bool rc_backup = mydata.real_coordinates;
2916 u16 version_backup = m_formspec_version;
2917 mydata.real_coordinates = false; // Old coordinates by default.
2919 std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
2920 for (const auto &element : prepend_elements)
2921 parseElement(&mydata, element);
2923 // legacy sorting for formspec versions < 3
2924 if (m_formspec_version >= 3)
2925 // prepends do not need to be reordered
2926 legacy_sort_start = Children.getLast();
2927 else if (version_backup >= 3)
2928 // only prepends elements have to be reordered
2929 legacySortElements(legacy_sort_start);
2931 m_formspec_version = version_backup;
2932 mydata.real_coordinates = rc_backup; // Restore coordinates
2935 for (; i< elements.size(); i++) {
2936 parseElement(&mydata, elements[i]);
2939 if (!container_stack.empty()) {
2940 errorstream << "Invalid formspec string: container was never closed!"
2944 // If there are fields without explicit size[], add a "Proceed"
2945 // button and adjust size to fit all the fields.
2946 if (mydata.simple_field_count > 0 && !mydata.explicit_size) {
2947 mydata.rect = core::rect<s32>(
2948 mydata.screensize.X / 2 - 580 / 2,
2949 mydata.screensize.Y / 2 - 300 / 2,
2950 mydata.screensize.X / 2 + 580 / 2,
2951 mydata.screensize.Y / 2 + 240 / 2 + mydata.simple_field_count * 60
2954 DesiredRect = mydata.rect;
2955 recalculateAbsolutePosition(false);
2956 mydata.basepos = getBasePos();
2959 v2s32 pos = mydata.basepos;
2960 pos.Y = (mydata.simple_field_count + 2) * 60;
2962 v2s32 size = DesiredRect.getSize();
2963 mydata.rect = core::rect<s32>(
2964 size.X / 2 - 70, pos.Y,
2965 size.X / 2 - 70 + 140, pos.Y + m_btn_height * 2
2967 const wchar_t *text = wgettext("Proceed");
2968 GUIButton::addButton(Environment, mydata.rect, this, 257, text);
2973 //set initial focus if parser didn't set it
2974 focused_element = Environment->getFocus();
2975 if (!focused_element
2976 || !isMyChild(focused_element)
2977 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2980 skin->setFont(old_font);
2983 if (m_formspec_version < 3)
2984 legacySortElements(legacy_sort_start);
2987 void GUIFormSpecMenu::legacySortElements(core::list<IGUIElement *>::Iterator from)
2991 * (0. background colors and backgrounds)
2993 * 2. all normal elements (like buttons)
2996 * 5. item lists (TODO)
2999 if (from == Children.end())
3000 from = Children.begin();
3004 core::list<IGUIElement *>::Iterator to = Children.end();
3006 // step 1: move all boxes to back and all images to front
3007 // (drawn in back = drawn first = in front of list)
3008 for (auto i = from; i != to;) {
3009 if (getTypeByID((*i)->getID()) == f_Box && i != from) {
3011 i = Children.erase(i);
3012 Children.insert_before(from, e);
3013 } else if ((*i)->hasType(gui::EGUIET_IMAGE)) {
3015 i = Children.erase(i);
3016 Children.push_back(e);
3017 if (to == Children.end())
3018 to = Children.getLast(); // do not touch sorted back
3019 if (i == Children.end())
3026 // step 2: move all item images to front
3027 for (auto i = from; i != to;) {
3028 if (getTypeByID((*i)->getID()) == f_ItemImage) {
3030 i = Children.erase(i);
3031 Children.push_back(e);
3032 if (to == Children.end())
3033 to = Children.getLast(); // do not touch sorted back
3034 if (i == Children.end())
3041 // step 3: move all item lists to front
3042 // TODO when item lists are drawn in order
3046 bool GUIFormSpecMenu::getAndroidUIInput()
3048 if (!hasAndroidUIInput())
3051 std::string fieldname = m_jni_field_name;
3052 m_jni_field_name.clear();
3054 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3055 iter != m_fields.end(); ++iter) {
3057 if (iter->fname != fieldname) {
3060 IGUIElement *tochange = getElementFromId(iter->fid, true);
3062 if (tochange == 0) {
3066 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
3070 std::string text = porting::getInputDialogValue();
3072 ((gui::IGUIEditBox *)tochange)->setText(utf8_to_wide(text).c_str());
3078 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
3080 core::rect<s32> imgrect(0, 0, imgsize.X, imgsize.Y);
3082 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
3083 core::rect<s32> clipping_rect = s.e->getAbsoluteClippingRect();
3084 v2s32 base_pos = s.e->getAbsolutePosition().UpperLeftCorner;
3085 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
3086 s32 item_i = i + s.start_item_i;
3090 if (s.real_coordinates) {
3091 x = (i%s.geom.X) * (imgsize.X * 1.25);
3092 y = (i/s.geom.X) * (imgsize.Y * 1.25);
3094 x = (i%s.geom.X) * spacing.X;
3095 y = (i/s.geom.X) * spacing.Y;
3098 core::rect<s32> rect = imgrect + base_pos + p0;
3099 rect.clipAgainst(clipping_rect);
3100 if (rect.getArea() > 0 && rect.isPointInside(p))
3101 return ItemSpec(s.inventoryloc, s.listname, item_i);
3105 return ItemSpec(InventoryLocation(), "", -1);
3108 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int layer,
3111 video::IVideoDriver* driver = Environment->getVideoDriver();
3113 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
3115 warningstream<<"GUIFormSpecMenu::drawList(): "
3116 << "The inventory location "
3117 << "\"" << s.inventoryloc.dump() << "\" doesn't exist anymore"
3121 InventoryList *ilist = inv->getList(s.listname);
3123 warningstream << "GUIFormSpecMenu::drawList(): "
3124 << "The inventory list \"" << s.listname << "\" @ \""
3125 << s.inventoryloc.dump() << "\" doesn't exist anymore"
3130 core::rect<s32> imgrect(0, 0, imgsize.X, imgsize.Y);
3131 core::rect<s32> clipping_rect = s.e->getAbsoluteClippingRect();
3132 v2s32 base_pos = s.e->getAbsolutePosition().UpperLeftCorner;
3134 for (s32 i = 0; i < s.geom.X * s.geom.Y; i++) {
3135 s32 item_i = i + s.start_item_i;
3136 if (item_i >= (s32)ilist->getSize())
3141 if (s.real_coordinates) {
3142 x = (i%s.geom.X) * (imgsize.X * 1.25);
3143 y = (i/s.geom.X) * (imgsize.Y * 1.25);
3145 x = (i%s.geom.X) * spacing.X;
3146 y = (i/s.geom.X) * spacing.Y;
3149 core::rect<s32> rect = imgrect + base_pos + p;
3150 ItemStack item = ilist->getItem(item_i);
3152 bool selected = m_selected_item
3153 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
3154 && m_selected_item->listname == s.listname
3155 && m_selected_item->i == item_i;
3156 core::rect<s32> clipped_rect(rect);
3157 clipped_rect.clipAgainst(clipping_rect);
3158 bool hovering = clipped_rect.getArea() > 0 &&
3159 clipped_rect.isPointInside(m_pointer);
3160 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
3161 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
3165 item_hovered = true;
3166 driver->draw2DRectangle(m_slotbg_h, rect, &clipping_rect);
3168 driver->draw2DRectangle(m_slotbg_n, rect, &clipping_rect);
3172 //Draw inv slot borders
3174 s32 x1 = rect.UpperLeftCorner.X;
3175 s32 y1 = rect.UpperLeftCorner.Y;
3176 s32 x2 = rect.LowerRightCorner.X;
3177 s32 y2 = rect.LowerRightCorner.Y;
3179 driver->draw2DRectangle(m_slotbordercolor,
3180 core::rect<s32>(v2s32(x1 - border, y1 - border),
3181 v2s32(x2 + border, y1)), &clipping_rect);
3182 driver->draw2DRectangle(m_slotbordercolor,
3183 core::rect<s32>(v2s32(x1 - border, y2),
3184 v2s32(x2 + border, y2 + border)), &clipping_rect);
3185 driver->draw2DRectangle(m_slotbordercolor,
3186 core::rect<s32>(v2s32(x1 - border, y1),
3187 v2s32(x1, y2)), &clipping_rect);
3188 driver->draw2DRectangle(m_slotbordercolor,
3189 core::rect<s32>(v2s32(x2, y1),
3190 v2s32(x2 + border, y2)), &clipping_rect);
3195 item.takeItem(m_selected_amount);
3197 if (!item.empty()) {
3199 drawItemStack(driver, m_font, item,
3200 rect, &clipping_rect, m_client, rotation_kind);
3202 if (hovering && !m_selected_item) {
3203 std::string tooltip = item.getDescription(m_client->idef());
3204 if (m_tooltip_append_itemname)
3205 tooltip += "\n[" + item.name + "]";
3206 showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
3207 m_default_tooltip_bgcolor);
3214 void GUIFormSpecMenu::drawSelectedItem()
3216 video::IVideoDriver* driver = Environment->getVideoDriver();
3218 if (!m_selected_item) {
3219 // reset rotation time
3220 drawItemStack(driver, m_font, ItemStack(),
3221 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)), NULL,
3222 m_client, IT_ROT_DRAGGED);
3226 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3228 InventoryList *list = inv->getList(m_selected_item->listname);
3230 ItemStack stack = list->getItem(m_selected_item->i);
3231 stack.count = m_selected_amount;
3233 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
3234 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
3235 rect.constrainTo(driver->getViewPort());
3236 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
3239 void GUIFormSpecMenu::drawMenu()
3242 const std::string &newform = m_form_src->getForm();
3243 if (newform != m_formspec_string) {
3244 m_formspec_string = newform;
3245 regenerateGui(m_screensize_old);
3249 gui::IGUISkin* skin = Environment->getSkin();
3250 sanity_check(skin != NULL);
3251 gui::IGUIFont *old_font = skin->getFont();
3252 skin->setFont(m_font);
3254 updateSelectedItem();
3256 video::IVideoDriver* driver = Environment->getVideoDriver();
3259 Draw background color
3261 v2u32 screenSize = driver->getScreenSize();
3262 core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
3265 driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
3267 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
3270 Draw rect_mode tooltip
3272 m_tooltip_element->setVisible(false);
3274 for (const auto &pair : m_tooltip_rects) {
3275 const core::rect<s32> &rect = pair.first->getAbsoluteClippingRect();
3276 if (rect.getArea() > 0 && rect.isPointInside(m_pointer)) {
3277 const std::wstring &text = pair.second.tooltip;
3278 if (!text.empty()) {
3279 showTooltip(text, pair.second.color, pair.second.bgcolor);
3288 for (gui::IGUIElement *e : m_backgrounds) {
3289 e->setVisible(true);
3291 e->setVisible(false);
3297 gui::IGUIElement::draw();
3301 Layer 0: Item slot rectangles
3302 Layer 1: Item images; prepare tooltip
3304 bool item_hovered = false;
3305 for (int layer = 0; layer < 2; layer++) {
3306 for (const GUIFormSpecMenu::ListDrawSpec &spec : m_inventorylists) {
3307 drawList(spec, layer, item_hovered);
3310 if (!item_hovered) {
3311 // reset rotation time
3312 drawItemStack(driver, m_font, ItemStack(),
3313 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
3314 NULL, m_client, IT_ROT_HOVERED);
3317 /* TODO find way to show tooltips on touchscreen */
3318 #ifndef HAVE_TOUCHSCREENGUI
3319 m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
3323 Draw fields/buttons tooltips
3325 gui::IGUIElement *hovered =
3326 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
3328 if (hovered != NULL) {
3329 s32 id = hovered->getID();
3333 m_old_tooltip_id = id;
3335 if (id == m_old_tooltip_id) {
3336 delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
3338 m_hovered_time = porting::getTimeMs();
3339 m_old_tooltip_id = id;
3343 // Find and update the current tooltip
3344 if (id != -1 && delta >= m_tooltip_show_delay) {
3345 for (const FieldSpec &field : m_fields) {
3347 if (field.fid != id)
3350 const std::wstring &text = m_tooltips[field.fname].tooltip;
3352 showTooltip(text, m_tooltips[field.fname].color,
3353 m_tooltips[field.fname].bgcolor);
3360 m_tooltip_element->draw();
3363 Draw dragged item stack
3367 skin->setFont(old_font);
3371 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
3372 const irr::video::SColor &color, const irr::video::SColor &bgcolor)
3374 const std::wstring ntext = translate_string(text);
3375 m_tooltip_element->setOverrideColor(color);
3376 m_tooltip_element->setBackgroundColor(bgcolor);
3377 setStaticText(m_tooltip_element, ntext.c_str());
3379 // Tooltip size and offset
3380 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
3381 #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
3382 std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
3383 s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
3385 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
3387 v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
3388 int tooltip_offset_x = m_btn_height;
3389 int tooltip_offset_y = m_btn_height;
3391 tooltip_offset_x *= 3;
3392 tooltip_offset_y = 0;
3393 if (m_pointer.X > (s32)screenSize.X / 2)
3394 tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
3397 // Calculate and set the tooltip position
3398 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
3399 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
3400 if (tooltip_x + tooltip_width > (s32)screenSize.X)
3401 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
3402 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
3403 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
3405 m_tooltip_element->setRelativePosition(
3407 core::position2d<s32>(tooltip_x, tooltip_y),
3408 core::dimension2d<s32>(tooltip_width, tooltip_height)
3412 // Display the tooltip
3413 m_tooltip_element->setVisible(true);
3414 bringToFront(m_tooltip_element);
3417 void GUIFormSpecMenu::updateSelectedItem()
3419 verifySelectedItem();
3421 // If craftresult is nonempty and nothing else is selected, select it now.
3422 if (!m_selected_item) {
3423 for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
3424 if (s.listname != "craftpreview")
3427 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
3431 InventoryList *list = inv->getList("craftresult");
3433 if (!list || list->getSize() == 0)
3436 const ItemStack &item = list->getItem(0);
3440 // Grab selected item from the crafting result list
3441 m_selected_item = new ItemSpec;
3442 m_selected_item->inventoryloc = s.inventoryloc;
3443 m_selected_item->listname = "craftresult";
3444 m_selected_item->i = 0;
3445 m_selected_amount = item.count;
3446 m_selected_dragging = false;
3451 // If craftresult is selected, keep the whole stack selected
3452 if (m_selected_item && m_selected_item->listname == "craftresult")
3453 m_selected_amount = verifySelectedItem().count;
3456 ItemStack GUIFormSpecMenu::verifySelectedItem()
3458 // If the selected stack has become empty for some reason, deselect it.
3459 // If the selected stack has become inaccessible, deselect it.
3460 // If the selected stack has become smaller, adjust m_selected_amount.
3461 // Return the selected stack.
3465 if(m_selected_item->isValid())
3467 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
3470 InventoryList *list = inv->getList(m_selected_item->listname);
3471 if(list && (u32) m_selected_item->i < list->getSize())
3473 ItemStack stack = list->getItem(m_selected_item->i);
3474 if (!m_selected_swap.empty()) {
3475 if (m_selected_swap.name == stack.name &&
3476 m_selected_swap.count == stack.count)
3477 m_selected_swap.clear();
3479 m_selected_amount = std::min(m_selected_amount, stack.count);
3488 // selection was not valid
3489 delete m_selected_item;
3490 m_selected_item = NULL;
3491 m_selected_amount = 0;
3492 m_selected_dragging = false;
3497 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
3503 if (quitmode == quit_mode_accept) {
3504 fields["quit"] = "true";
3507 if (quitmode == quit_mode_cancel) {
3508 fields["quit"] = "true";
3509 m_text_dst->gotText(fields);
3513 if (current_keys_pending.key_down) {
3514 fields["key_down"] = "true";
3515 current_keys_pending.key_down = false;
3518 if (current_keys_pending.key_up) {
3519 fields["key_up"] = "true";
3520 current_keys_pending.key_up = false;
3523 if (current_keys_pending.key_enter) {
3524 fields["key_enter"] = "true";
3525 current_keys_pending.key_enter = false;
3528 if (!current_field_enter_pending.empty()) {
3529 fields["key_enter_field"] = current_field_enter_pending;
3530 current_field_enter_pending = "";
3533 if (current_keys_pending.key_escape) {
3534 fields["key_escape"] = "true";
3535 current_keys_pending.key_escape = false;
3538 for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
3540 std::string name = s.fname;
3541 if (s.ftype == f_Button) {
3542 fields[name] = wide_to_utf8(s.flabel);
3543 } else if (s.ftype == f_Table) {
3544 GUITable *table = getTable(s.fname);
3546 fields[name] = table->checkEvent();
3549 else if(s.ftype == f_DropDown) {
3550 // no dynamic cast possible due to some distributions shipped
3551 // without rtti support in irrlicht
3552 IGUIElement *element = getElementFromId(s.fid, true);
3553 gui::IGUIComboBox *e = NULL;
3554 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
3555 e = static_cast<gui::IGUIComboBox*>(element);
3557 warningstream << "GUIFormSpecMenu::acceptInput: dropdown "
3558 << "field without dropdown element" << std::endl;
3561 s32 selected = e->getSelected();
3562 if (selected >= 0) {
3563 std::vector<std::string> *dropdown_values =
3564 getDropDownValues(s.fname);
3565 if (dropdown_values && selected < (s32)dropdown_values->size()) {
3566 fields[name] = (*dropdown_values)[selected];
3570 else if (s.ftype == f_TabHeader) {
3571 // no dynamic cast possible due to some distributions shipped
3572 // without rttzi support in irrlicht
3573 IGUIElement *element = getElementFromId(s.fid, true);
3574 gui::IGUITabControl *e = nullptr;
3575 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
3576 e = static_cast<gui::IGUITabControl *>(element);
3580 std::stringstream ss;
3581 ss << (e->getActiveTab() +1);
3582 fields[name] = ss.str();
3585 else if (s.ftype == f_CheckBox) {
3586 // no dynamic cast possible due to some distributions shipped
3587 // without rtti support in irrlicht
3588 IGUIElement *element = getElementFromId(s.fid, true);
3589 gui::IGUICheckBox *e = nullptr;
3590 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
3591 e = static_cast<gui::IGUICheckBox*>(element);
3596 fields[name] = "true";
3598 fields[name] = "false";
3601 else if (s.ftype == f_ScrollBar) {
3602 // no dynamic cast possible due to some distributions shipped
3603 // without rtti support in irrlicht
3604 IGUIElement *element = getElementFromId(s.fid, true);
3605 GUIScrollBar *e = nullptr;
3606 if (element && element->getType() == gui::EGUIET_ELEMENT)
3607 e = static_cast<GUIScrollBar *>(element);
3610 std::stringstream os;
3612 if (s.fdefault == L"Changed")
3613 fields[name] = "CHG:" + os.str();
3615 fields[name] = "VAL:" + os.str();
3619 IGUIElement *e = getElementFromId(s.fid, true);
3621 fields[name] = wide_to_utf8(e->getText());
3626 m_text_dst->gotText(fields);
3630 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
3632 while(tocheck != NULL) {
3633 if (tocheck == parent) {
3636 tocheck = tocheck->getParent();
3641 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
3643 // The IGUITabControl renders visually using the skin's selected
3644 // font, which we override for the duration of form drawing,
3645 // but computes tab hotspots based on how it would have rendered
3646 // using the font that is selected at the time of button release.
3647 // To make these two consistent, temporarily override the skin's
3648 // font while the IGUITabControl is processing the event.
3649 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3650 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3651 s32 x = event.MouseInput.X;
3652 s32 y = event.MouseInput.Y;
3653 gui::IGUIElement *hovered =
3654 Environment->getRootGUIElement()->getElementFromPoint(
3655 core::position2d<s32>(x, y));
3656 if (hovered && isMyChild(hovered) &&
3657 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
3658 gui::IGUISkin* skin = Environment->getSkin();
3659 sanity_check(skin != NULL);
3660 gui::IGUIFont *old_font = skin->getFont();
3661 skin->setFont(m_font);
3662 bool retval = hovered->OnEvent(event);
3663 skin->setFont(old_font);
3668 // Fix Esc/Return key being eaten by checkboxen and tables
3669 if(event.EventType==EET_KEY_INPUT_EVENT) {
3670 KeyPress kp(event.KeyInput);
3671 if (kp == EscapeKey || kp == CancelKey
3672 || kp == getKeySetting("keymap_inventory")
3673 || event.KeyInput.Key==KEY_RETURN) {
3674 gui::IGUIElement *focused = Environment->getFocus();
3675 if (focused && isMyChild(focused) &&
3676 (focused->getType() == gui::EGUIET_LIST_BOX ||
3677 focused->getType() == gui::EGUIET_CHECK_BOX) &&
3678 (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
3679 event.KeyInput.Key != KEY_RETURN)) {
3685 // Mouse wheel and move events: send to hovered element instead of focused
3686 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3687 (event.MouseInput.Event == EMIE_MOUSE_WHEEL ||
3688 event.MouseInput.Event == EMIE_MOUSE_MOVED)) {
3689 s32 x = event.MouseInput.X;
3690 s32 y = event.MouseInput.Y;
3691 gui::IGUIElement *hovered =
3692 Environment->getRootGUIElement()->getElementFromPoint(
3693 core::position2d<s32>(x, y));
3694 if (hovered && isMyChild(hovered)) {
3695 hovered->OnEvent(event);
3696 return event.MouseInput.Event == EMIE_MOUSE_WHEEL;
3700 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
3701 s32 x = event.MouseInput.X;
3702 s32 y = event.MouseInput.Y;
3703 gui::IGUIElement *hovered =
3704 Environment->getRootGUIElement()->getElementFromPoint(
3705 core::position2d<s32>(x, y));
3706 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3707 m_old_tooltip_id = -1;
3709 if (!isChild(hovered,this)) {
3710 if (DoubleClickDetection(event)) {
3716 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3717 /* TODO add a check like:
3718 if (event.JoystickEvent != joystick_we_listen_for)
3721 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3723 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3725 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3727 acceptInput(quit_mode_accept);
3735 return GUIModalMenu::preprocessEvent(event);
3738 /******************************************************************************/
3739 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3741 /* The following code is for capturing double-clicks of the mouse button
3742 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3743 * -- which closes the form -- under some circumstances.
3745 * There have been many github issues reporting this as a bug even though it
3746 * was an intended feature. For this reason, remapping the double-click as
3747 * an ESC must be explicitly set when creating this class via the
3748 * /p remap_dbl_click parameter of the constructor.
3751 if (!m_remap_dbl_click)
3754 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3755 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3756 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3758 m_doubleclickdetect[1].pos = m_pointer;
3759 m_doubleclickdetect[1].time = porting::getTimeMs();
3761 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3762 u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
3767 double squaredistance =
3768 m_doubleclickdetect[0].pos
3769 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3771 if (squaredistance > (30*30)) {
3775 SEvent* translated = new SEvent();
3776 assert(translated != 0);
3777 //translate doubleclick to escape
3778 memset(translated, 0, sizeof(SEvent));
3779 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3780 translated->KeyInput.Key = KEY_ESCAPE;
3781 translated->KeyInput.Control = false;
3782 translated->KeyInput.Shift = false;
3783 translated->KeyInput.PressedDown = true;
3784 translated->KeyInput.Char = 0;
3785 OnEvent(*translated);
3787 // no need to send the key up event as we're already deleted
3788 // and no one else did notice this event
3796 void GUIFormSpecMenu::tryClose()
3800 acceptInput(quit_mode_cancel);
3803 m_text_dst->gotText(L"MenuQuit");
3807 enum ButtonEventType : u8
3820 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3822 if (event.EventType==EET_KEY_INPUT_EVENT) {
3823 KeyPress kp(event.KeyInput);
3824 if (event.KeyInput.PressedDown && (
3825 (kp == EscapeKey) || (kp == CancelKey) ||
3826 ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
3831 if (m_client != NULL && event.KeyInput.PressedDown &&
3832 (kp == getKeySetting("keymap_screenshot"))) {
3833 m_client->makeScreenshot();
3835 if (event.KeyInput.PressedDown &&
3836 (event.KeyInput.Key==KEY_RETURN ||
3837 event.KeyInput.Key==KEY_UP ||
3838 event.KeyInput.Key==KEY_DOWN)
3840 switch (event.KeyInput.Key) {
3842 current_keys_pending.key_enter = true;
3845 current_keys_pending.key_up = true;
3848 current_keys_pending.key_down = true;
3852 //can't happen at all!
3853 FATAL_ERROR("Reached a source line that can't ever been reached");
3856 if (current_keys_pending.key_enter && m_allowclose) {
3857 acceptInput(quit_mode_accept);
3867 /* Mouse event other than movement, or crossing the border of inventory
3868 field while holding right mouse button
3870 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3871 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3872 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3873 event.MouseInput.isRightPressed() &&
3874 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3876 // Get selected item and hovered/clicked item (s)
3878 m_old_tooltip_id = -1;
3879 updateSelectedItem();
3880 ItemSpec s = getItemAtPos(m_pointer);
3882 Inventory *inv_selected = NULL;
3883 Inventory *inv_s = NULL;
3884 InventoryList *list_s = NULL;
3886 if (m_selected_item) {
3887 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3888 sanity_check(inv_selected);
3889 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3896 inv_s = m_invmgr->getInventory(s.inventoryloc);
3899 errorstream << "InventoryMenu: The selected inventory location "
3900 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3902 s.i = -1; // make it invalid again
3906 list_s = inv_s->getList(s.listname);
3907 if (list_s == NULL) {
3908 verbosestream << "InventoryMenu: The selected inventory list \""
3909 << s.listname << "\" does not exist" << std::endl;
3910 s.i = -1; // make it invalid again
3914 if ((u32)s.i >= list_s->getSize()) {
3915 infostream << "InventoryMenu: The selected inventory list \""
3916 << s.listname << "\" is too small (i=" << s.i << ", size="
3917 << list_s->getSize() << ")" << std::endl;
3918 s.i = -1; // make it invalid again
3922 s_count = list_s->getItem(s.i).count;
3925 bool identical = m_selected_item && s.isValid() &&
3926 (inv_selected == inv_s) &&
3927 (m_selected_item->listname == s.listname) &&
3928 (m_selected_item->i == s.i);
3930 ButtonEventType button = BET_LEFT;
3931 ButtonEventType updown = BET_OTHER;
3932 switch (event.MouseInput.Event) {
3933 case EMIE_LMOUSE_PRESSED_DOWN:
3934 button = BET_LEFT; updown = BET_DOWN;
3936 case EMIE_RMOUSE_PRESSED_DOWN:
3937 button = BET_RIGHT; updown = BET_DOWN;
3939 case EMIE_MMOUSE_PRESSED_DOWN:
3940 button = BET_MIDDLE; updown = BET_DOWN;
3942 case EMIE_MOUSE_WHEEL:
3943 button = (event.MouseInput.Wheel > 0) ?
3944 BET_WHEEL_UP : BET_WHEEL_DOWN;
3947 case EMIE_LMOUSE_LEFT_UP:
3948 button = BET_LEFT; updown = BET_UP;
3950 case EMIE_RMOUSE_LEFT_UP:
3951 button = BET_RIGHT; updown = BET_UP;
3953 case EMIE_MMOUSE_LEFT_UP:
3954 button = BET_MIDDLE; updown = BET_UP;
3956 case EMIE_MOUSE_MOVED:
3963 // Set this number to a positive value to generate a move action
3964 // from m_selected_item to s.
3965 u32 move_amount = 0;
3967 // Set this number to a positive value to generate a move action
3968 // from s to the next inventory ring.
3969 u32 shift_move_amount = 0;
3971 // Set this number to a positive value to generate a drop action
3972 // from m_selected_item.
3973 u32 drop_amount = 0;
3975 // Set this number to a positive value to generate a craft action at s.
3976 u32 craft_amount = 0;
3980 // Some mouse button has been pressed
3982 //infostream<<"Mouse button "<<button<<" pressed at p=("
3983 // <<p.X<<","<<p.Y<<")"<<std::endl;
3985 m_selected_dragging = false;
3987 if (s.isValid() && s.listname == "craftpreview") {
3988 // Craft preview has been clicked: craft
3989 craft_amount = (button == BET_MIDDLE ? 10 : 1);
3990 } else if (!m_selected_item) {
3991 if (s_count && button != BET_WHEEL_UP) {
3992 // Non-empty stack has been clicked: select or shift-move it
3993 m_selected_item = new ItemSpec(s);
3996 if (button == BET_RIGHT)
3997 count = (s_count + 1) / 2;
3998 else if (button == BET_MIDDLE)
3999 count = MYMIN(s_count, 10);
4000 else if (button == BET_WHEEL_DOWN)
4005 if (!event.MouseInput.Shift) {
4006 // no shift: select item
4007 m_selected_amount = count;
4008 m_selected_dragging = button != BET_WHEEL_DOWN;
4009 m_auto_place = false;
4011 // shift pressed: move item, right click moves 1
4012 shift_move_amount = button == BET_RIGHT ? 1 : count;
4015 } else { // m_selected_item != NULL
4016 assert(m_selected_amount >= 1);
4019 // Clicked a slot: move
4020 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4022 else if (button == BET_MIDDLE)
4023 move_amount = MYMIN(m_selected_amount, 10);
4024 else if (button == BET_LEFT)
4025 move_amount = m_selected_amount;
4029 if (button == BET_WHEEL_DOWN) {
4030 if (m_selected_amount < s_count)
4031 ++m_selected_amount;
4033 if (move_amount >= m_selected_amount)
4034 m_selected_amount = 0;
4036 m_selected_amount -= move_amount;
4040 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
4041 && button != BET_WHEEL_DOWN) {
4042 // Clicked outside of the window: drop
4043 if (button == BET_RIGHT || button == BET_WHEEL_UP)
4045 else if (button == BET_MIDDLE)
4046 drop_amount = MYMIN(m_selected_amount, 10);
4048 drop_amount = m_selected_amount;
4053 // Some mouse button has been released
4055 //infostream<<"Mouse button "<<button<<" released at p=("
4056 // <<p.X<<","<<p.Y<<")"<<std::endl;
4058 if (m_selected_dragging && m_selected_item) {
4061 // Dragged to different slot: move all selected
4062 move_amount = m_selected_amount;
4064 } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
4065 // Dragged outside of window: drop all selected
4066 drop_amount = m_selected_amount;
4070 m_selected_dragging = false;
4071 // Keep track of whether the mouse button be released
4072 // One click is drag without dropping. Click + release
4073 // + click changes to drop item when moved mode
4074 if (m_selected_item)
4075 m_auto_place = true;
4078 // Mouse has been moved and rmb is down and mouse pointer just
4079 // entered a new inventory field (checked in the entry-if, this
4080 // is the only action here that is generated by mouse movement)
4081 if (m_selected_item && s.isValid() && s.listname != "craftpreview") {
4083 // TODO: middle mouse to move 10 items might be handy
4085 // Only move an item if the destination slot is empty
4086 // or contains the same item type as what is going to be
4088 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4089 InventoryList *list_to = list_s;
4090 assert(list_from && list_to);
4091 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4092 ItemStack stack_to = list_to->getItem(s.i);
4093 if (stack_to.empty() || stack_to.name == stack_from.name)
4102 // Possibly send inventory action to server
4103 if (move_amount > 0) {
4104 // Send IAction::Move
4106 assert(m_selected_item && m_selected_item->isValid());
4107 assert(s.isValid());
4109 assert(inv_selected && inv_s);
4110 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4111 InventoryList *list_to = list_s;
4112 assert(list_from && list_to);
4113 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4114 ItemStack stack_to = list_to->getItem(s.i);
4116 // Check how many items can be moved
4117 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
4118 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
4120 // If source stack cannot be added to destination stack at all,
4122 if (leftover.count == stack_from.count &&
4123 leftover.name == stack_from.name) {
4125 if (m_selected_swap.empty()) {
4126 m_selected_amount = stack_to.count;
4127 m_selected_dragging = false;
4129 // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
4130 // Skip next validation checks due async inventory calls
4131 m_selected_swap = stack_to;
4136 // Source stack goes fully into destination stack
4137 else if (leftover.empty()) {
4138 m_selected_amount -= move_amount;
4140 // Source stack goes partly into destination stack
4142 move_amount -= leftover.count;
4143 m_selected_amount -= move_amount;
4147 infostream << "Handing IAction::Move to manager" << std::endl;
4148 IMoveAction *a = new IMoveAction();
4149 a->count = move_amount;
4150 a->from_inv = m_selected_item->inventoryloc;
4151 a->from_list = m_selected_item->listname;
4152 a->from_i = m_selected_item->i;
4153 a->to_inv = s.inventoryloc;
4154 a->to_list = s.listname;
4156 m_invmgr->inventoryAction(a);
4158 } else if (shift_move_amount > 0) {
4159 u32 mis = m_inventory_rings.size();
4161 for (; i < mis; i++) {
4162 const ListRingSpec &sp = m_inventory_rings[i];
4163 if (sp.inventoryloc == s.inventoryloc
4164 && sp.listname == s.listname)
4168 if (i >= mis) // if not found
4170 u32 to_inv_ind = (i + 1) % mis;
4171 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
4172 InventoryList *list_from = list_s;
4175 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
4178 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
4181 ItemStack stack_from = list_from->getItem(s.i);
4182 assert(shift_move_amount <= stack_from.count);
4184 infostream << "Handing IAction::Move to manager" << std::endl;
4185 IMoveAction *a = new IMoveAction();
4186 a->count = shift_move_amount;
4187 a->from_inv = s.inventoryloc;
4188 a->from_list = s.listname;
4190 a->to_inv = to_inv_sp.inventoryloc;
4191 a->to_list = to_inv_sp.listname;
4192 a->move_somewhere = true;
4193 m_invmgr->inventoryAction(a);
4195 } else if (drop_amount > 0) {
4196 // Send IAction::Drop
4198 assert(m_selected_item && m_selected_item->isValid());
4199 assert(inv_selected);
4200 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
4202 ItemStack stack_from = list_from->getItem(m_selected_item->i);
4204 // Check how many items can be dropped
4205 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
4206 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
4207 m_selected_amount -= drop_amount;
4209 infostream << "Handing IAction::Drop to manager" << std::endl;
4210 IDropAction *a = new IDropAction();
4211 a->count = drop_amount;
4212 a->from_inv = m_selected_item->inventoryloc;
4213 a->from_list = m_selected_item->listname;
4214 a->from_i = m_selected_item->i;
4215 m_invmgr->inventoryAction(a);
4216 } else if (craft_amount > 0) {
4217 assert(s.isValid());
4219 // if there are no items selected or the selected item
4220 // belongs to craftresult list, proceed with crafting
4221 if (m_selected_item == NULL ||
4222 !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
4226 // Send IACTION_CRAFT
4227 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
4228 ICraftAction *a = new ICraftAction();
4229 a->count = craft_amount;
4230 a->craft_inv = s.inventoryloc;
4231 m_invmgr->inventoryAction(a);
4235 // If m_selected_amount has been decreased to zero, deselect
4236 if (m_selected_amount == 0) {
4237 m_selected_swap.clear();
4238 delete m_selected_item;
4239 m_selected_item = NULL;
4240 m_selected_amount = 0;
4241 m_selected_dragging = false;
4243 m_old_pointer = m_pointer;
4246 if (event.EventType == EET_GUI_EVENT) {
4247 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
4249 // find the element that was clicked
4250 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4251 if ((s.ftype == f_TabHeader) &&
4252 (s.fid == event.GUIEvent.Caller->getID())) {
4260 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
4262 if (!canTakeFocus(event.GUIEvent.Element)) {
4263 infostream<<"GUIFormSpecMenu: Not allowing focus change."
4265 // Returning true disables focus change
4269 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
4270 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
4271 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
4272 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
4273 unsigned int btn_id = event.GUIEvent.Caller->getID();
4275 if (btn_id == 257) {
4277 acceptInput(quit_mode_accept);
4281 m_text_dst->gotText(L"ExitButton");
4283 // quitMenu deallocates menu
4287 // find the element that was clicked
4288 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4289 // if its a button, set the send field so
4290 // lua knows which button was pressed
4291 if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
4292 s.fid == event.GUIEvent.Caller->getID()) {
4296 acceptInput(quit_mode_accept);
4299 m_text_dst->gotText(L"ExitButton");
4304 acceptInput(quit_mode_no);
4308 } else if ((s.ftype == f_DropDown) &&
4309 (s.fid == event.GUIEvent.Caller->getID())) {
4310 // only send the changed dropdown
4311 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4312 if (s2.ftype == f_DropDown) {
4317 acceptInput(quit_mode_no);
4319 // revert configuration to make sure dropdowns are sent on
4320 // regular button click
4321 for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
4322 if (s2.ftype == f_DropDown) {
4327 } else if ((s.ftype == f_ScrollBar) &&
4328 (s.fid == event.GUIEvent.Caller->getID())) {
4329 s.fdefault = L"Changed";
4330 acceptInput(quit_mode_no);
4332 } else if ((s.ftype == f_Unknown) &&
4333 (s.fid == event.GUIEvent.Caller->getID())) {
4341 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
4342 if (event.GUIEvent.Caller->getID() > 257) {
4343 bool close_on_enter = true;
4344 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4345 if (s.ftype == f_Unknown &&
4346 s.fid == event.GUIEvent.Caller->getID()) {
4347 current_field_enter_pending = s.fname;
4348 std::unordered_map<std::string, bool>::const_iterator it =
4349 field_close_on_enter.find(s.fname);
4350 if (it != field_close_on_enter.end())
4351 close_on_enter = (*it).second;
4357 if (m_allowclose && close_on_enter) {
4358 current_keys_pending.key_enter = true;
4359 acceptInput(quit_mode_accept);
4362 current_keys_pending.key_enter = true;
4365 // quitMenu deallocates menu
4370 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
4371 int current_id = event.GUIEvent.Caller->getID();
4372 if (current_id > 257) {
4373 // find the element that was clicked
4374 for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
4375 // if it's a table, set the send field
4376 // so lua knows which table was changed
4377 if ((s.ftype == f_Table) && (s.fid == current_id)) {
4388 return Parent ? Parent->OnEvent(event) : false;
4392 * get name of element by element id
4393 * @param id of element
4394 * @return name string or empty string
4396 std::string GUIFormSpecMenu::getNameByID(s32 id)
4398 for (FieldSpec &spec : m_fields) {
4406 FormspecFieldType GUIFormSpecMenu::getTypeByID(s32 id)
4408 for (FieldSpec &spec : m_fields) {
4416 * get label of element by id
4417 * @param id of element
4418 * @return label string or empty string
4420 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
4422 for (FieldSpec &spec : m_fields) {
4429 StyleSpec GUIFormSpecMenu::getStyleForElement(const std::string &type,
4430 const std::string &name, const std::string &parent_type) {
4433 if (!parent_type.empty()) {
4434 auto it = theme_by_type.find(parent_type);
4435 if (it != theme_by_type.end()) {
4440 auto it = theme_by_type.find(type);
4441 if (it != theme_by_type.end()) {
4445 it = theme_by_name.find(name);
4446 if (it != theme_by_name.end()) {