3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guiFormSpecMenu.h"
28 #include "constants.h"
31 #include "util/strfnd.h"
32 #include <IGUICheckBox.h>
33 #include <IGUIEditBox.h>
34 #include <IGUIButton.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include <IGUIComboBox.h>
40 #include "client/tile.h" // ITextureSource
41 #include "hud.h" // drawItemStack
45 #include "scripting_game.h"
49 #include "fontengine.h"
51 #include "util/numeric.h"
52 #include "util/string.h" // for parseColorString()
53 #include "irrlicht_changes/static_text.h"
54 #include "guiscalingfilter.h"
56 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
57 #include "intlGUIEditBox.h"
60 #define MY_CHECKPOS(a,b) \
61 if (v_pos.size() != 2) { \
62 errorstream<< "Invalid pos for element " << a << "specified: \"" \
63 << parts[b] << "\"" << std::endl; \
67 #define MY_CHECKGEOM(a,b) \
68 if (v_geom.size() != 2) { \
69 errorstream<< "Invalid pos for element " << a << "specified: \"" \
70 << parts[b] << "\"" << std::endl; \
76 static unsigned int font_line_height(gui::IGUIFont *font)
78 return font->getDimension(L"Ay").Height + font->getKerningHeight();
81 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
82 JoystickController *joystick,
83 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
85 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
86 bool remap_dbl_click) :
87 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
92 m_selected_item(NULL),
94 m_selected_dragging(false),
95 m_tooltip_element(NULL),
98 m_rmouse_auto_place(false),
103 m_formspec_version(0),
104 m_focused_element(""),
105 m_joystick(joystick),
106 current_field_enter_pending(""),
108 m_remap_dbl_click(remap_dbl_click)
110 , m_JavaDialogFieldName("")
113 current_keys_pending.key_down = false;
114 current_keys_pending.key_up = false;
115 current_keys_pending.key_enter = false;
116 current_keys_pending.key_escape = false;
118 m_doubleclickdetect[0].time = 0;
119 m_doubleclickdetect[1].time = 0;
121 m_doubleclickdetect[0].pos = v2s32(0, 0);
122 m_doubleclickdetect[1].pos = v2s32(0, 0);
124 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
127 GUIFormSpecMenu::~GUIFormSpecMenu()
131 for (u32 i = 0; i < m_tables.size(); ++i) {
132 GUITable *table = m_tables[i].second;
136 delete m_selected_item;
138 if (m_form_src != NULL) {
141 if (m_text_dst != NULL) {
146 void GUIFormSpecMenu::removeChildren()
148 const core::list<gui::IGUIElement*> &children = getChildren();
150 while(!children.empty()) {
151 (*children.getLast())->remove();
154 if(m_tooltip_element) {
155 m_tooltip_element->remove();
156 m_tooltip_element->drop();
157 m_tooltip_element = NULL;
162 void GUIFormSpecMenu::setInitialFocus()
164 // Set initial focus according to following order of precedence:
165 // 1. first empty editbox
169 // 5. first focusable (not statictext, not tabheader)
170 // 6. first child element
172 core::list<gui::IGUIElement*> children = getChildren();
174 // in case "children" contains any NULL elements, remove them
175 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
176 it != children.end();) {
180 it = children.erase(it);
183 // 1. first empty editbox
184 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
185 it != children.end(); ++it) {
186 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
187 && (*it)->getText()[0] == 0) {
188 Environment->setFocus(*it);
194 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
195 it != children.end(); ++it) {
196 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
197 Environment->setFocus(*it);
203 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
204 it != children.end(); ++it) {
205 if ((*it)->getTypeName() == std::string("GUITable")) {
206 Environment->setFocus(*it);
212 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
213 it != children.end(); --it) {
214 if ((*it)->getType() == gui::EGUIET_BUTTON) {
215 Environment->setFocus(*it);
220 // 5. first focusable (not statictext, not tabheader)
221 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
222 it != children.end(); ++it) {
223 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
224 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
225 Environment->setFocus(*it);
230 // 6. first child element
231 if (children.empty())
232 Environment->setFocus(this);
234 Environment->setFocus(*(children.begin()));
237 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
239 for (u32 i = 0; i < m_tables.size(); ++i) {
240 if (tablename == m_tables[i].first.fname)
241 return m_tables[i].second;
246 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
248 for (u32 i = 0; i < m_dropdowns.size(); ++i) {
249 if (name == m_dropdowns[i].first.fname)
250 return &m_dropdowns[i].second;
255 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
257 std::vector<std::string> parts = split(element,',');
259 if (((parts.size() == 2) || parts.size() == 3) ||
260 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
262 if (parts[1].find(';') != std::string::npos)
263 parts[1] = parts[1].substr(0,parts[1].find(';'));
265 data->invsize.X = MYMAX(0, stof(parts[0]));
266 data->invsize.Y = MYMAX(0, stof(parts[1]));
269 if (parts.size() == 3) {
270 if (parts[2] == "true") {
271 lockSize(true,v2u32(800,600));
275 data->explicit_size = true;
278 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
281 void GUIFormSpecMenu::parseContainer(parserData* data, std::string element)
283 std::vector<std::string> parts = split(element, ',');
285 if (parts.size() >= 2) {
286 if (parts[1].find(';') != std::string::npos)
287 parts[1] = parts[1].substr(0, parts[1].find(';'));
289 container_stack.push(pos_offset);
290 pos_offset.X += MYMAX(0, stof(parts[0]));
291 pos_offset.Y += MYMAX(0, stof(parts[1]));
294 errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
297 void GUIFormSpecMenu::parseContainerEnd(parserData* data)
299 if (container_stack.empty()) {
300 errorstream<< "Invalid container end element, no matching container start element" << std::endl;
302 pos_offset = container_stack.top();
303 container_stack.pop();
307 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
310 warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
314 std::vector<std::string> parts = split(element,';');
316 if (((parts.size() == 4) || (parts.size() == 5)) ||
317 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
319 std::string location = parts[0];
320 std::string listname = parts[1];
321 std::vector<std::string> v_pos = split(parts[2],',');
322 std::vector<std::string> v_geom = split(parts[3],',');
323 std::string startindex = "";
324 if (parts.size() == 5)
325 startindex = parts[4];
327 MY_CHECKPOS("list",2);
328 MY_CHECKGEOM("list",3);
330 InventoryLocation loc;
332 if(location == "context" || location == "current_name")
333 loc = m_current_inventory_location;
335 loc.deSerialize(location);
337 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
338 pos.X += stof(v_pos[0]) * (float)spacing.X;
339 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
342 geom.X = stoi(v_geom[0]);
343 geom.Y = stoi(v_geom[1]);
347 start_i = stoi(startindex);
349 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
350 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
354 if(!data->explicit_size)
355 warningstream<<"invalid use of list without a size[] element"<<std::endl;
356 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
359 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
362 void GUIFormSpecMenu::parseListRing(parserData* data, std::string element)
365 errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
369 std::vector<std::string> parts = split(element, ';');
371 if (parts.size() == 2) {
372 std::string location = parts[0];
373 std::string listname = parts[1];
375 InventoryLocation loc;
377 if (location == "context" || location == "current_name")
378 loc = m_current_inventory_location;
380 loc.deSerialize(location);
382 m_inventory_rings.push_back(ListRingSpec(loc, listname));
384 } else if ((element == "") && (m_inventorylists.size() > 1)) {
385 size_t siz = m_inventorylists.size();
386 // insert the last two inv list elements into the list ring
387 const ListDrawSpec &spa = m_inventorylists[siz - 2];
388 const ListDrawSpec &spb = m_inventorylists[siz - 1];
389 m_inventory_rings.push_back(ListRingSpec(spa.inventoryloc, spa.listname));
390 m_inventory_rings.push_back(ListRingSpec(spb.inventoryloc, spb.listname));
393 errorstream<< "Invalid list ring element(" << parts.size() << ", "
394 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
397 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
399 std::vector<std::string> parts = split(element,';');
401 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
402 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
404 std::vector<std::string> v_pos = split(parts[0],',');
405 std::string name = parts[1];
406 std::string label = parts[2];
407 std::string selected = "";
409 if (parts.size() >= 4)
412 MY_CHECKPOS("checkbox",0);
414 v2s32 pos = padding + pos_offset * spacing;
415 pos.X += stof(v_pos[0]) * (float) spacing.X;
416 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
418 bool fselected = false;
420 if (selected == "true")
423 std::wstring wlabel = utf8_to_wide(unescape_string(label));
425 core::rect<s32> rect = core::rect<s32>(
426 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
427 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
428 pos.Y + ((imgsize.Y/2) + m_btn_height));
432 wlabel, //Needed for displaying text on MSVC
437 spec.ftype = f_CheckBox;
439 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
440 spec.fid, spec.flabel.c_str());
442 if (spec.fname == data->focused_fieldname) {
443 Environment->setFocus(e);
446 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
447 m_fields.push_back(spec);
450 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
453 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
455 std::vector<std::string> parts = split(element,';');
457 if (parts.size() >= 5) {
458 std::vector<std::string> v_pos = split(parts[0],',');
459 std::vector<std::string> v_dim = split(parts[1],',');
460 std::string name = parts[3];
461 std::string value = parts[4];
463 MY_CHECKPOS("scrollbar",0);
465 v2s32 pos = padding + pos_offset * spacing;
466 pos.X += stof(v_pos[0]) * (float) spacing.X;
467 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
469 if (v_dim.size() != 2) {
470 errorstream<< "Invalid size for element " << "scrollbar"
471 << "specified: \"" << parts[1] << "\"" << std::endl;
476 dim.X = stof(v_dim[0]) * (float) spacing.X;
477 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
479 core::rect<s32> rect =
480 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
489 bool is_horizontal = true;
491 if (parts[2] == "vertical")
492 is_horizontal = false;
494 spec.ftype = f_ScrollBar;
496 gui::IGUIScrollBar* e =
497 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
501 e->setPos(stoi(parts[4]));
503 e->setLargeStep(100);
505 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
506 m_fields.push_back(spec);
509 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
512 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
514 std::vector<std::string> parts = split(element,';');
516 if ((parts.size() == 3) ||
517 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
519 std::vector<std::string> v_pos = split(parts[0],',');
520 std::vector<std::string> v_geom = split(parts[1],',');
521 std::string name = unescape_string(parts[2]);
523 MY_CHECKPOS("image", 0);
524 MY_CHECKGEOM("image", 1);
526 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
527 pos.X += stof(v_pos[0]) * (float) spacing.X;
528 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
531 geom.X = stof(v_geom[0]) * (float)imgsize.X;
532 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
534 if (!data->explicit_size)
535 warningstream<<"invalid use of image without a size[] element"<<std::endl;
536 m_images.push_back(ImageDrawSpec(name, pos, geom));
538 } else if (parts.size() == 2) {
539 std::vector<std::string> v_pos = split(parts[0],',');
540 std::string name = unescape_string(parts[1]);
542 MY_CHECKPOS("image", 0);
544 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
545 pos.X += stof(v_pos[0]) * (float) spacing.X;
546 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
548 if (!data->explicit_size)
549 warningstream<<"invalid use of image without a size[] element"<<std::endl;
550 m_images.push_back(ImageDrawSpec(name, pos));
553 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
556 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
558 std::vector<std::string> parts = split(element,';');
560 if ((parts.size() == 3) ||
561 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
563 std::vector<std::string> v_pos = split(parts[0],',');
564 std::vector<std::string> v_geom = split(parts[1],',');
565 std::string name = parts[2];
567 MY_CHECKPOS("itemimage",0);
568 MY_CHECKGEOM("itemimage",1);
570 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
571 pos.X += stof(v_pos[0]) * (float) spacing.X;
572 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
575 geom.X = stof(v_geom[0]) * (float)imgsize.X;
576 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
578 if(!data->explicit_size)
579 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
580 m_itemimages.push_back(ImageDrawSpec("", name, pos, geom));
583 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
586 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
589 std::vector<std::string> parts = split(element,';');
591 if ((parts.size() == 4) ||
592 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
594 std::vector<std::string> v_pos = split(parts[0],',');
595 std::vector<std::string> v_geom = split(parts[1],',');
596 std::string name = parts[2];
597 std::string label = parts[3];
599 MY_CHECKPOS("button",0);
600 MY_CHECKGEOM("button",1);
602 v2s32 pos = padding + pos_offset * spacing;
603 pos.X += stof(v_pos[0]) * (float)spacing.X;
604 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
607 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
608 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
610 core::rect<s32> rect =
611 core::rect<s32>(pos.X, pos.Y - m_btn_height,
612 pos.X + geom.X, pos.Y + m_btn_height);
614 if(!data->explicit_size)
615 warningstream<<"invalid use of button without a size[] element"<<std::endl;
617 std::wstring wlabel = utf8_to_wide(unescape_string(label));
625 spec.ftype = f_Button;
626 if(type == "button_exit")
628 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
629 spec.flabel.c_str());
631 if (spec.fname == data->focused_fieldname) {
632 Environment->setFocus(e);
635 m_fields.push_back(spec);
638 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
641 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
643 std::vector<std::string> parts = split(element,';');
645 if (((parts.size() == 3) || (parts.size() == 4)) ||
646 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
648 std::vector<std::string> v_pos = split(parts[0],',');
649 std::vector<std::string> v_geom = split(parts[1],',');
650 std::string name = unescape_string(parts[2]);
652 MY_CHECKPOS("background",0);
653 MY_CHECKGEOM("background",1);
655 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
656 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X - (float)imgsize.X)/2;
657 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y - (float)imgsize.Y)/2;
660 geom.X = stof(v_geom[0]) * (float)spacing.X;
661 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
663 if (!data->explicit_size)
664 warningstream<<"invalid use of background without a size[] element"<<std::endl;
667 if (parts.size() == 4 && is_yes(parts[3])) {
668 pos.X = stoi(v_pos[0]); //acts as offset
669 pos.Y = stoi(v_pos[1]); //acts as offset
672 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom, clip));
676 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
679 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
681 std::vector<std::string> parts = split(element,';');
683 data->table_options.clear();
684 for (size_t i = 0; i < parts.size(); ++i) {
685 // Parse table option
686 std::string opt = unescape_string(parts[i]);
687 data->table_options.push_back(GUITable::splitOption(opt));
691 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
693 std::vector<std::string> parts = split(element,';');
695 data->table_columns.clear();
696 for (size_t i = 0; i < parts.size(); ++i) {
697 std::vector<std::string> col_parts = split(parts[i],',');
698 GUITable::TableColumn column;
700 if (!col_parts.empty())
701 column.type = col_parts[0];
702 // Parse column options
703 for (size_t j = 1; j < col_parts.size(); ++j) {
704 std::string opt = unescape_string(col_parts[j]);
705 column.options.push_back(GUITable::splitOption(opt));
707 data->table_columns.push_back(column);
711 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
713 std::vector<std::string> parts = split(element,';');
715 if (((parts.size() == 4) || (parts.size() == 5)) ||
716 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
718 std::vector<std::string> v_pos = split(parts[0],',');
719 std::vector<std::string> v_geom = split(parts[1],',');
720 std::string name = parts[2];
721 std::vector<std::string> items = split(parts[3],',');
722 std::string str_initial_selection = "";
723 std::string str_transparent = "false";
725 if (parts.size() >= 5)
726 str_initial_selection = parts[4];
728 MY_CHECKPOS("table",0);
729 MY_CHECKGEOM("table",1);
731 v2s32 pos = padding + pos_offset * spacing;
732 pos.X += stof(v_pos[0]) * (float)spacing.X;
733 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
736 geom.X = stof(v_geom[0]) * (float)spacing.X;
737 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
739 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
748 spec.ftype = f_Table;
750 for (unsigned int i = 0; i < items.size(); ++i) {
751 items[i] = unescape_enriched(unescape_string(items[i]));
754 //now really show table
755 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
758 if (spec.fname == data->focused_fieldname) {
759 Environment->setFocus(e);
762 e->setTable(data->table_options, data->table_columns, items);
764 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
765 e->setDynamicData(data->table_dyndata[name]);
768 if ((str_initial_selection != "") &&
769 (str_initial_selection != "0"))
770 e->setSelected(stoi(str_initial_selection.c_str()));
772 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
773 m_fields.push_back(spec);
776 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
779 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
781 std::vector<std::string> parts = split(element,';');
783 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
784 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
786 std::vector<std::string> v_pos = split(parts[0],',');
787 std::vector<std::string> v_geom = split(parts[1],',');
788 std::string name = parts[2];
789 std::vector<std::string> items = split(parts[3],',');
790 std::string str_initial_selection = "";
791 std::string str_transparent = "false";
793 if (parts.size() >= 5)
794 str_initial_selection = parts[4];
796 if (parts.size() >= 6)
797 str_transparent = parts[5];
799 MY_CHECKPOS("textlist",0);
800 MY_CHECKGEOM("textlist",1);
802 v2s32 pos = padding + pos_offset * spacing;
803 pos.X += stof(v_pos[0]) * (float)spacing.X;
804 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
807 geom.X = stof(v_geom[0]) * (float)spacing.X;
808 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
811 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
820 spec.ftype = f_Table;
822 for (unsigned int i = 0; i < items.size(); ++i) {
823 items[i] = unescape_enriched(unescape_string(items[i]));
826 //now really show list
827 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
830 if (spec.fname == data->focused_fieldname) {
831 Environment->setFocus(e);
834 e->setTextList(items, is_yes(str_transparent));
836 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
837 e->setDynamicData(data->table_dyndata[name]);
840 if ((str_initial_selection != "") &&
841 (str_initial_selection != "0"))
842 e->setSelected(stoi(str_initial_selection.c_str()));
844 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
845 m_fields.push_back(spec);
848 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
852 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
854 std::vector<std::string> parts = split(element,';');
856 if ((parts.size() == 5) ||
857 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
859 std::vector<std::string> v_pos = split(parts[0],',');
860 std::string name = parts[2];
861 std::vector<std::string> items = split(parts[3],',');
862 std::string str_initial_selection = "";
863 str_initial_selection = parts[4];
865 MY_CHECKPOS("dropdown",0);
867 v2s32 pos = padding + pos_offset * spacing;
868 pos.X += stof(v_pos[0]) * (float)spacing.X;
869 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
871 s32 width = stof(parts[1]) * (float)spacing.Y;
873 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
874 pos.X + width, pos.Y + (m_btn_height * 2));
883 spec.ftype = f_DropDown;
886 //now really show list
887 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
889 if (spec.fname == data->focused_fieldname) {
890 Environment->setFocus(e);
893 for (unsigned int i=0; i < items.size(); i++) {
894 e->addItem(unescape_enriched(unescape_string(
895 utf8_to_wide(items[i]))).c_str());
898 if (str_initial_selection != "")
899 e->setSelected(stoi(str_initial_selection.c_str())-1);
901 m_fields.push_back(spec);
903 m_dropdowns.push_back(std::pair<FieldSpec,
904 std::vector<std::string> >(spec, std::vector<std::string>()));
905 std::vector<std::string> &values = m_dropdowns.back().second;
906 for (unsigned int i = 0; i < items.size(); i++) {
907 values.push_back(unescape_string(items[i]));
912 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
913 << element << "'" << std::endl;
916 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data,
917 const std::string &element)
919 std::vector<std::string> parts = split(element,';');
920 if (parts.size() == 2 ||
921 (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
922 field_close_on_enter[parts[0]] = is_yes(parts[1]);
926 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
928 std::vector<std::string> parts = split(element,';');
930 if ((parts.size() == 4) || (parts.size() == 5) ||
931 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
933 std::vector<std::string> v_pos = split(parts[0],',');
934 std::vector<std::string> v_geom = split(parts[1],',');
935 std::string name = parts[2];
936 std::string label = parts[3];
938 MY_CHECKPOS("pwdfield",0);
939 MY_CHECKGEOM("pwdfield",1);
941 v2s32 pos = pos_offset * spacing;
942 pos.X += stof(v_pos[0]) * (float)spacing.X;
943 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
946 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
948 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
949 pos.Y -= m_btn_height;
950 geom.Y = m_btn_height*2;
952 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
954 std::wstring wlabel = utf8_to_wide(unescape_string(label));
964 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
966 if (spec.fname == data->focused_fieldname) {
967 Environment->setFocus(e);
970 if (label.length() >= 1)
972 int font_height = g_fontengine->getTextHeight();
973 rect.UpperLeftCorner.Y -= font_height;
974 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
975 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
978 e->setPasswordBox(true,L'*');
981 evt.EventType = EET_KEY_INPUT_EVENT;
982 evt.KeyInput.Key = KEY_END;
983 evt.KeyInput.Char = 0;
984 evt.KeyInput.Control = 0;
985 evt.KeyInput.Shift = 0;
986 evt.KeyInput.PressedDown = true;
989 if (parts.size() >= 5) {
990 // TODO: remove after 2016-11-03
991 warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
992 " instead of the 5th param" << std::endl;
993 field_close_on_enter[name] = is_yes(parts[4]);
996 m_fields.push_back(spec);
999 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
1002 void GUIFormSpecMenu::parseSimpleField(parserData* data,
1003 std::vector<std::string> &parts)
1005 std::string name = parts[0];
1006 std::string label = parts[1];
1007 std::string default_val = parts[2];
1009 core::rect<s32> rect;
1011 if(data->explicit_size)
1012 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
1014 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
1015 pos.Y = ((m_fields.size()+2)*60);
1016 v2s32 size = DesiredRect.getSize();
1018 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
1019 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
1023 default_val = m_form_src->resolveText(default_val);
1026 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1031 utf8_to_wide(unescape_string(default_val)),
1037 // spec field id to 0, this stops submit searching for a value that isn't there
1038 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1043 gui::IGUIElement *e;
1044 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1045 if (g_settings->getBool("freetype")) {
1046 e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1047 true, Environment, this, spec.fid, rect);
1053 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1055 if (spec.fname == data->focused_fieldname) {
1056 Environment->setFocus(e);
1060 evt.EventType = EET_KEY_INPUT_EVENT;
1061 evt.KeyInput.Key = KEY_END;
1062 evt.KeyInput.Char = 0;
1063 evt.KeyInput.Control = 0;
1064 evt.KeyInput.Shift = 0;
1065 evt.KeyInput.PressedDown = true;
1068 if (label.length() >= 1)
1070 int font_height = g_fontengine->getTextHeight();
1071 rect.UpperLeftCorner.Y -= font_height;
1072 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1073 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1077 if (parts.size() >= 4) {
1078 // TODO: remove after 2016-11-03
1079 warningstream << "field/simple: use field_close_on_enter[name, enabled]" <<
1080 " instead of the 4th param" << std::endl;
1081 field_close_on_enter[name] = is_yes(parts[3]);
1084 m_fields.push_back(spec);
1087 void GUIFormSpecMenu::parseTextArea(parserData* data,
1088 std::vector<std::string>& parts,std::string type)
1091 std::vector<std::string> v_pos = split(parts[0],',');
1092 std::vector<std::string> v_geom = split(parts[1],',');
1093 std::string name = parts[2];
1094 std::string label = parts[3];
1095 std::string default_val = parts[4];
1097 MY_CHECKPOS(type,0);
1098 MY_CHECKGEOM(type,1);
1100 v2s32 pos = pos_offset * spacing;
1101 pos.X += stof(v_pos[0]) * (float) spacing.X;
1102 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1106 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1108 if (type == "textarea")
1110 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1111 pos.Y += m_btn_height;
1115 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1116 pos.Y -= m_btn_height;
1117 geom.Y = m_btn_height*2;
1120 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1122 if(!data->explicit_size)
1123 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1126 default_val = m_form_src->resolveText(default_val);
1129 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1134 utf8_to_wide(unescape_string(default_val)),
1140 // spec field id to 0, this stops submit searching for a value that isn't there
1141 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1147 gui::IGUIEditBox *e;
1148 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1149 if (g_settings->getBool("freetype")) {
1150 e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1151 true, Environment, this, spec.fid, rect);
1157 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1160 if (spec.fname == data->focused_fieldname) {
1161 Environment->setFocus(e);
1164 if (type == "textarea")
1166 e->setMultiLine(true);
1167 e->setWordWrap(true);
1168 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1171 evt.EventType = EET_KEY_INPUT_EVENT;
1172 evt.KeyInput.Key = KEY_END;
1173 evt.KeyInput.Char = 0;
1174 evt.KeyInput.Control = 0;
1175 evt.KeyInput.Shift = 0;
1176 evt.KeyInput.PressedDown = true;
1180 if (label.length() >= 1)
1182 int font_height = g_fontengine->getTextHeight();
1183 rect.UpperLeftCorner.Y -= font_height;
1184 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1185 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1189 if (parts.size() >= 6) {
1190 // TODO: remove after 2016-11-03
1191 warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
1192 " instead of the 6th param" << std::endl;
1193 field_close_on_enter[name] = is_yes(parts[5]);
1196 m_fields.push_back(spec);
1199 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1202 std::vector<std::string> parts = split(element,';');
1204 if (parts.size() == 3 || parts.size() == 4) {
1205 parseSimpleField(data,parts);
1209 if ((parts.size() == 5) || (parts.size() == 6) ||
1210 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1212 parseTextArea(data,parts,type);
1215 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1218 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1220 std::vector<std::string> parts = split(element,';');
1222 if ((parts.size() == 2) ||
1223 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1225 std::vector<std::string> v_pos = split(parts[0],',');
1226 std::string text = parts[1];
1228 MY_CHECKPOS("label",0);
1230 v2s32 pos = padding + pos_offset * spacing;
1231 pos.X += stof(v_pos[0]) * (float)spacing.X;
1232 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1234 if(!data->explicit_size)
1235 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1237 std::vector<std::string> lines = split(text, '\n');
1239 for (unsigned int i = 0; i != lines.size(); i++) {
1240 // Lines are spaced at the nominal distance of
1241 // 2/5 inventory slot, even if the font doesn't
1242 // quite match that. This provides consistent
1243 // form layout, at the expense of sometimes
1244 // having sub-optimal spacing for the font.
1245 // We multiply by 2 and then divide by 5, rather
1246 // than multiply by 0.4, to get exact results
1247 // in the integer cases: 0.4 is not exactly
1248 // representable in binary floating point.
1249 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1250 std::wstring wlabel = utf8_to_wide(unescape_string(lines[i]));
1251 core::rect<s32> rect = core::rect<s32>(
1252 pos.X, posy - m_btn_height,
1253 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1254 posy + m_btn_height);
1261 gui::IGUIStaticText *e =
1262 addStaticText(Environment, spec.flabel.c_str(),
1263 rect, false, false, this, spec.fid);
1264 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1266 m_fields.push_back(spec);
1271 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1274 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1276 std::vector<std::string> parts = split(element,';');
1278 if ((parts.size() == 2) ||
1279 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1281 std::vector<std::string> v_pos = split(parts[0],',');
1282 std::wstring text = unescape_enriched(
1283 unescape_string(utf8_to_wide(parts[1])));
1285 MY_CHECKPOS("vertlabel",1);
1287 v2s32 pos = padding + pos_offset * spacing;
1288 pos.X += stof(v_pos[0]) * (float)spacing.X;
1289 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1291 core::rect<s32> rect = core::rect<s32>(
1292 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1294 font_line_height(m_font)
1296 +((imgsize.Y/2)- m_btn_height));
1297 //actually text.length() would be correct but adding +1 avoids to break all mods
1299 if(!data->explicit_size)
1300 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1302 std::wstring label = L"";
1304 for (unsigned int i=0; i < text.length(); i++) {
1315 gui::IGUIStaticText *t =
1316 addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid);
1317 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1318 m_fields.push_back(spec);
1321 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1324 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1327 std::vector<std::string> parts = split(element,';');
1329 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1330 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1332 std::vector<std::string> v_pos = split(parts[0],',');
1333 std::vector<std::string> v_geom = split(parts[1],',');
1334 std::string image_name = parts[2];
1335 std::string name = parts[3];
1336 std::string label = parts[4];
1338 MY_CHECKPOS("imagebutton",0);
1339 MY_CHECKGEOM("imagebutton",1);
1341 v2s32 pos = padding + pos_offset * spacing;
1342 pos.X += stof(v_pos[0]) * (float)spacing.X;
1343 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1345 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1346 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1348 bool noclip = false;
1349 bool drawborder = true;
1350 std::string pressed_image_name = "";
1352 if (parts.size() >= 7) {
1353 if (parts[5] == "true")
1355 if (parts[6] == "false")
1359 if (parts.size() >= 8) {
1360 pressed_image_name = parts[7];
1363 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1365 if(!data->explicit_size)
1366 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1368 image_name = unescape_string(image_name);
1369 pressed_image_name = unescape_string(pressed_image_name);
1371 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1376 utf8_to_wide(image_name),
1379 spec.ftype = f_Button;
1380 if(type == "image_button_exit")
1381 spec.is_exit = true;
1383 video::ITexture *texture = 0;
1384 video::ITexture *pressed_texture = 0;
1385 texture = m_tsrc->getTexture(image_name);
1386 if (pressed_image_name != "")
1387 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1389 pressed_texture = texture;
1391 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1393 if (spec.fname == data->focused_fieldname) {
1394 Environment->setFocus(e);
1397 e->setUseAlphaChannel(true);
1398 e->setImage(guiScalingImageButton(
1399 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1400 e->setPressedImage(guiScalingImageButton(
1401 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1402 e->setScaleImage(true);
1403 e->setNotClipped(noclip);
1404 e->setDrawBorder(drawborder);
1406 m_fields.push_back(spec);
1410 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1413 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1415 std::vector<std::string> parts = split(element,';');
1417 if (((parts.size() == 4) || (parts.size() == 6)) ||
1418 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1420 std::vector<std::string> v_pos = split(parts[0],',');
1421 std::string name = parts[1];
1422 std::vector<std::string> buttons = split(parts[2],',');
1423 std::string str_index = parts[3];
1424 bool show_background = true;
1425 bool show_border = true;
1426 int tab_index = stoi(str_index) -1;
1428 MY_CHECKPOS("tabheader",0);
1430 if (parts.size() == 6) {
1431 if (parts[4] == "true")
1432 show_background = false;
1433 if (parts[5] == "false")
1434 show_border = false;
1444 spec.ftype = f_TabHeader;
1446 v2s32 pos = pos_offset * spacing;
1447 pos.X += stof(v_pos[0]) * (float)spacing.X;
1448 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1450 geom.X = DesiredRect.getWidth();
1451 geom.Y = m_btn_height*2;
1453 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1456 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1457 show_background, show_border, spec.fid);
1458 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1459 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1460 e->setTabHeight(m_btn_height*2);
1462 if (spec.fname == data->focused_fieldname) {
1463 Environment->setFocus(e);
1466 e->setNotClipped(true);
1468 for (unsigned int i = 0; i < buttons.size(); i++) {
1469 e->addTab(unescape_enriched(unescape_string(
1470 utf8_to_wide(buttons[i]))).c_str(), -1);
1473 if ((tab_index >= 0) &&
1474 (buttons.size() < INT_MAX) &&
1475 (tab_index < (int) buttons.size()))
1476 e->setActiveTab(tab_index);
1478 m_fields.push_back(spec);
1481 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1482 << element << "'" << std::endl;
1485 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1488 if (m_client == 0) {
1489 warningstream << "invalid use of item_image_button with m_client==0"
1494 std::vector<std::string> parts = split(element,';');
1496 if ((parts.size() == 5) ||
1497 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1499 std::vector<std::string> v_pos = split(parts[0],',');
1500 std::vector<std::string> v_geom = split(parts[1],',');
1501 std::string item_name = parts[2];
1502 std::string name = parts[3];
1503 std::string label = parts[4];
1505 label = unescape_string(label);
1506 item_name = unescape_string(item_name);
1508 MY_CHECKPOS("itemimagebutton",0);
1509 MY_CHECKGEOM("itemimagebutton",1);
1511 v2s32 pos = padding + pos_offset * spacing;
1512 pos.X += stof(v_pos[0]) * (float)spacing.X;
1513 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1515 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1516 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1518 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1520 if(!data->explicit_size)
1521 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1523 IItemDefManager *idef = m_client->idef();
1525 item.deSerialize(item_name, idef);
1528 TooltipSpec(item.getDefinition(idef).description,
1529 m_default_tooltip_bgcolor,
1530 m_default_tooltip_color);
1534 utf8_to_wide(label),
1535 utf8_to_wide(item_name),
1536 258 + m_fields.size()
1539 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1541 if (spec.fname == data->focused_fieldname) {
1542 Environment->setFocus(e);
1545 spec.ftype = f_Button;
1546 rect+=data->basepos-padding;
1548 m_fields.push_back(spec);
1549 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
1550 pos.X += stof(v_pos[0]) * (float) spacing.X;
1551 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1552 m_itemimages.push_back(ImageDrawSpec("", item_name, e, pos, geom));
1553 m_static_texts.push_back(StaticTextSpec(utf8_to_wide(label), rect, e));
1556 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1559 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1561 std::vector<std::string> parts = split(element,';');
1563 if ((parts.size() == 3) ||
1564 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1566 std::vector<std::string> v_pos = split(parts[0],',');
1567 std::vector<std::string> v_geom = split(parts[1],',');
1569 MY_CHECKPOS("box",0);
1570 MY_CHECKGEOM("box",1);
1572 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
1573 pos.X += stof(v_pos[0]) * (float) spacing.X;
1574 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1577 geom.X = stof(v_geom[0]) * (float)spacing.X;
1578 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1580 video::SColor tmp_color;
1582 if (parseColorString(parts[2], tmp_color, false)) {
1583 BoxDrawSpec spec(pos, geom, tmp_color);
1585 m_boxes.push_back(spec);
1588 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1592 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1595 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1597 std::vector<std::string> parts = split(element,';');
1599 if (((parts.size() == 1) || (parts.size() == 2)) ||
1600 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1602 parseColorString(parts[0],m_bgcolor,false);
1604 if (parts.size() == 2) {
1605 std::string fullscreen = parts[1];
1606 m_bgfullscreen = is_yes(fullscreen);
1610 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1613 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1615 std::vector<std::string> parts = split(element,';');
1617 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1618 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1620 parseColorString(parts[0], m_slotbg_n, false);
1621 parseColorString(parts[1], m_slotbg_h, false);
1623 if (parts.size() >= 3) {
1624 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1625 m_slotborder = true;
1628 if (parts.size() == 5) {
1629 video::SColor tmp_color;
1631 if (parseColorString(parts[3], tmp_color, false))
1632 m_default_tooltip_bgcolor = tmp_color;
1633 if (parseColorString(parts[4], tmp_color, false))
1634 m_default_tooltip_color = tmp_color;
1638 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1641 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1643 std::vector<std::string> parts = split(element,';');
1644 if (parts.size() == 2) {
1645 std::string name = parts[0];
1646 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1647 m_default_tooltip_bgcolor, m_default_tooltip_color);
1649 } else if (parts.size() == 4) {
1650 std::string name = parts[0];
1651 video::SColor tmp_color1, tmp_color2;
1652 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1653 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1654 tmp_color1, tmp_color2);
1658 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1661 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1667 std::vector<std::string> parts = split(data,'[');
1669 if (parts.size() < 2) {
1673 if (parts[0] != "formspec_version") {
1677 if (is_number(parts[1])) {
1678 m_formspec_version = mystoi(parts[1]);
1685 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1690 std::vector<std::string> parts = split(element,'[');
1692 if (parts.size() < 2)
1695 std::string type = trim(parts[0]);
1696 std::string description = trim(parts[1]);
1698 if (type != "size" && type != "invsize")
1701 if (type == "invsize")
1702 log_deprecated("Deprecated formspec element \"invsize\" is used");
1704 parseSize(data, description);
1709 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1715 std::vector<std::string> parts = split(element,'[');
1717 // ugly workaround to keep compatibility
1718 if (parts.size() > 2) {
1719 if (trim(parts[0]) == "image") {
1720 for (unsigned int i=2;i< parts.size(); i++) {
1721 parts[1] += "[" + parts[i];
1727 if (parts.size() < 2) {
1731 std::string type = trim(parts[0]);
1732 std::string description = trim(parts[1]);
1734 if (type == "container") {
1735 parseContainer(data, description);
1739 if (type == "container_end") {
1740 parseContainerEnd(data);
1744 if (type == "list") {
1745 parseList(data, description);
1749 if (type == "listring") {
1750 parseListRing(data, description);
1754 if (type == "checkbox") {
1755 parseCheckbox(data, description);
1759 if (type == "image") {
1760 parseImage(data, description);
1764 if (type == "item_image") {
1765 parseItemImage(data, description);
1769 if (type == "button" || type == "button_exit") {
1770 parseButton(data, description, type);
1774 if (type == "background") {
1775 parseBackground(data,description);
1779 if (type == "tableoptions"){
1780 parseTableOptions(data,description);
1784 if (type == "tablecolumns"){
1785 parseTableColumns(data,description);
1789 if (type == "table"){
1790 parseTable(data,description);
1794 if (type == "textlist"){
1795 parseTextList(data,description);
1799 if (type == "dropdown"){
1800 parseDropDown(data,description);
1804 if (type == "field_close_on_enter") {
1805 parseFieldCloseOnEnter(data, description);
1809 if (type == "pwdfield") {
1810 parsePwdField(data,description);
1814 if ((type == "field") || (type == "textarea")){
1815 parseField(data,description,type);
1819 if (type == "label") {
1820 parseLabel(data,description);
1824 if (type == "vertlabel") {
1825 parseVertLabel(data,description);
1829 if (type == "item_image_button") {
1830 parseItemImageButton(data,description);
1834 if ((type == "image_button") || (type == "image_button_exit")) {
1835 parseImageButton(data,description,type);
1839 if (type == "tabheader") {
1840 parseTabHeader(data,description);
1844 if (type == "box") {
1845 parseBox(data,description);
1849 if (type == "bgcolor") {
1850 parseBackgroundColor(data,description);
1854 if (type == "listcolors") {
1855 parseListColors(data,description);
1859 if (type == "tooltip") {
1860 parseTooltip(data,description);
1864 if (type == "scrollbar") {
1865 parseScrollBar(data, description);
1871 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1875 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1877 /* useless to regenerate without a screensize */
1878 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1885 for (u32 i = 0; i < m_tables.size(); ++i) {
1886 std::string tablename = m_tables[i].first.fname;
1887 GUITable *table = m_tables[i].second;
1888 mydata.table_dyndata[tablename] = table->getDynamicData();
1892 if (!m_focused_element.empty())
1893 mydata.focused_fieldname = m_focused_element;
1896 gui::IGUIElement *focused_element = Environment->getFocus();
1897 if (focused_element && focused_element->getParent() == this) {
1898 s32 focused_id = focused_element->getID();
1899 if (focused_id > 257) {
1900 for (u32 i=0; i<m_fields.size(); i++) {
1901 if (m_fields[i].fid == focused_id) {
1902 mydata.focused_fieldname =
1913 for (u32 i = 0; i < m_tables.size(); ++i) {
1914 GUITable *table = m_tables[i].second;
1918 mydata.size= v2s32(100,100);
1919 mydata.screensize = screensize;
1921 // Base position of contents of form
1922 mydata.basepos = getBasePos();
1924 /* Convert m_init_draw_spec to m_inventorylists */
1926 m_inventorylists.clear();
1928 m_backgrounds.clear();
1929 m_itemimages.clear();
1931 m_checkboxes.clear();
1932 m_scrollbars.clear();
1936 m_inventory_rings.clear();
1937 m_static_texts.clear();
1939 // Set default values (fits old formspec values)
1940 m_bgcolor = video::SColor(140,0,0,0);
1941 m_bgfullscreen = false;
1943 m_slotbg_n = video::SColor(255,128,128,128);
1944 m_slotbg_h = video::SColor(255,192,192,192);
1946 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1947 m_default_tooltip_color = video::SColor(255,255,255,255);
1949 m_slotbordercolor = video::SColor(200,0,0,0);
1950 m_slotborder = false;
1954 assert(m_tooltip_element == NULL);
1955 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1956 m_tooltip_element = addStaticText(Environment, L"",core::rect<s32>(0,0,110,18));
1957 m_tooltip_element->enableOverrideColor(true);
1958 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1959 m_tooltip_element->setDrawBackground(true);
1960 m_tooltip_element->setDrawBorder(true);
1961 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1962 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1963 m_tooltip_element->setWordWrap(false);
1964 //we're not parent so no autograb for this one!
1965 m_tooltip_element->grab();
1968 std::vector<std::string> elements = split(m_formspec_string,']');
1971 /* try to read version from first element only */
1972 if (elements.size() >= 1) {
1973 if ( parseVersionDirect(elements[0]) ) {
1978 /* we need size first in order to calculate image scale */
1979 mydata.explicit_size = false;
1980 for (; i< elements.size(); i++) {
1981 if (!parseSizeDirect(&mydata, elements[i])) {
1986 if (mydata.explicit_size) {
1987 // compute scaling for specified form size
1989 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1990 v2u32 delta = current_screensize - m_lockscreensize;
1992 if (current_screensize.Y > m_lockscreensize.Y)
1997 if (current_screensize.X > m_lockscreensize.X)
2002 offset = v2s32(delta.X,delta.Y);
2004 mydata.screensize = m_lockscreensize;
2006 offset = v2s32(0,0);
2009 double gui_scaling = g_settings->getFloat("gui_scaling");
2010 double screen_dpi = porting::getDisplayDensity() * 96;
2014 // In fixed-size mode, inventory image size
2015 // is 0.53 inch multiplied by the gui_scaling
2016 // config parameter. This magic size is chosen
2017 // to make the main menu (15.5 inventory images
2018 // wide, including border) just fit into the
2019 // default window (800 pixels wide) at 96 DPI
2020 // and default scaling (1.00).
2021 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
2023 // In variable-size mode, we prefer to make the
2024 // inventory image size 1/15 of screen height,
2025 // multiplied by the gui_scaling config parameter.
2026 // If the preferred size won't fit the whole
2027 // form on the screen, either horizontally or
2028 // vertically, then we scale it down to fit.
2029 // (The magic numbers in the computation of what
2030 // fits arise from the scaling factors in the
2031 // following stanza, including the form border,
2032 // help text space, and 0.1 inventory slot spare.)
2033 // However, a minimum size is also set, that
2034 // the image size can't be less than 0.3 inch
2035 // multiplied by gui_scaling, even if this means
2036 // the form doesn't fit the screen.
2037 double prefer_imgsize = mydata.screensize.Y / 15 *
2039 double fitx_imgsize = mydata.screensize.X /
2040 ((5.0/4.0) * (0.5 + mydata.invsize.X));
2041 double fity_imgsize = mydata.screensize.Y /
2042 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
2043 double screen_dpi = porting::getDisplayDensity() * 96;
2044 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
2045 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2046 MYMIN(fitx_imgsize, fity_imgsize)));
2049 // Everything else is scaled in proportion to the
2050 // inventory image size. The inventory slot spacing
2051 // is 5/4 image size horizontally and 15/13 image size
2052 // vertically. The padding around the form (incorporating
2053 // the border of the outer inventory slots) is 3/8
2054 // image size. Font height (baseline to baseline)
2055 // is 2/5 vertical inventory slot spacing, and button
2056 // half-height is 7/8 of font height.
2057 imgsize = v2s32(use_imgsize, use_imgsize);
2058 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2059 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2060 m_btn_height = use_imgsize*15.0/13 * 0.35;
2062 m_font = g_fontengine->getFont();
2064 mydata.size = v2s32(
2065 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2066 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2068 DesiredRect = mydata.rect = core::rect<s32>(
2069 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
2070 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
2071 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
2072 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
2075 // Non-size[] form must consist only of text fields and
2076 // implicit "Proceed" button. Use default font, and
2077 // temporary form size which will be recalculated below.
2078 m_font = g_fontengine->getFont();
2079 m_btn_height = font_line_height(m_font) * 0.875;
2080 DesiredRect = core::rect<s32>(
2081 mydata.screensize.X/2 - 580/2,
2082 mydata.screensize.Y/2 - 300/2,
2083 mydata.screensize.X/2 + 580/2,
2084 mydata.screensize.Y/2 + 300/2
2087 recalculateAbsolutePosition(false);
2088 mydata.basepos = getBasePos();
2089 m_tooltip_element->setOverrideFont(m_font);
2091 gui::IGUISkin* skin = Environment->getSkin();
2092 sanity_check(skin != NULL);
2093 gui::IGUIFont *old_font = skin->getFont();
2094 skin->setFont(m_font);
2096 pos_offset = v2s32();
2097 for (; i< elements.size(); i++) {
2098 parseElement(&mydata, elements[i]);
2101 if (!container_stack.empty()) {
2102 errorstream << "Invalid formspec string: container was never closed!"
2106 // If there are fields without explicit size[], add a "Proceed"
2107 // button and adjust size to fit all the fields.
2108 if (m_fields.size() && !mydata.explicit_size) {
2109 mydata.rect = core::rect<s32>(
2110 mydata.screensize.X/2 - 580/2,
2111 mydata.screensize.Y/2 - 300/2,
2112 mydata.screensize.X/2 + 580/2,
2113 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2115 DesiredRect = mydata.rect;
2116 recalculateAbsolutePosition(false);
2117 mydata.basepos = getBasePos();
2120 v2s32 pos = mydata.basepos;
2121 pos.Y = ((m_fields.size()+2)*60);
2123 v2s32 size = DesiredRect.getSize();
2125 core::rect<s32>(size.X/2-70, pos.Y,
2126 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2127 const wchar_t *text = wgettext("Proceed");
2128 Environment->addButton(mydata.rect, this, 257, text);
2134 //set initial focus if parser didn't set it
2135 focused_element = Environment->getFocus();
2136 if (!focused_element
2137 || !isMyChild(focused_element)
2138 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2141 skin->setFont(old_font);
2145 bool GUIFormSpecMenu::getAndroidUIInput()
2147 /* no dialog shown */
2148 if (m_JavaDialogFieldName == "") {
2153 if (porting::getInputDialogState() == -1) {
2157 std::string fieldname = m_JavaDialogFieldName;
2158 m_JavaDialogFieldName = "";
2160 /* no value abort dialog processing */
2161 if (porting::getInputDialogState() != 0) {
2165 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2166 iter != m_fields.end(); ++iter) {
2168 if (iter->fname != fieldname) {
2171 IGUIElement* tochange = getElementFromId(iter->fid);
2173 if (tochange == 0) {
2177 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2181 std::string text = porting::getInputDialogValue();
2183 ((gui::IGUIEditBox*) tochange)->
2184 setText(utf8_to_wide(text).c_str());
2190 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2192 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2194 for(u32 i=0; i<m_inventorylists.size(); i++)
2196 const ListDrawSpec &s = m_inventorylists[i];
2198 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2199 s32 item_i = i + s.start_item_i;
2200 s32 x = (i%s.geom.X) * spacing.X;
2201 s32 y = (i/s.geom.X) * spacing.Y;
2203 core::rect<s32> rect = imgrect + s.pos + p0;
2204 if(rect.isPointInside(p))
2206 return ItemSpec(s.inventoryloc, s.listname, item_i);
2211 return ItemSpec(InventoryLocation(), "", -1);
2214 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
2217 video::IVideoDriver* driver = Environment->getVideoDriver();
2219 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2221 warningstream<<"GUIFormSpecMenu::drawList(): "
2222 <<"The inventory location "
2223 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2227 InventoryList *ilist = inv->getList(s.listname);
2229 warningstream<<"GUIFormSpecMenu::drawList(): "
2230 <<"The inventory list \""<<s.listname<<"\" @ \""
2231 <<s.inventoryloc.dump()<<"\" doesn't exist"
2236 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2238 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2240 s32 item_i = i + s.start_item_i;
2241 if(item_i >= (s32) ilist->getSize())
2243 s32 x = (i%s.geom.X) * spacing.X;
2244 s32 y = (i/s.geom.X) * spacing.Y;
2246 core::rect<s32> rect = imgrect + s.pos + p;
2249 item = ilist->getItem(item_i);
2251 bool selected = m_selected_item
2252 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2253 && m_selected_item->listname == s.listname
2254 && m_selected_item->i == item_i;
2255 bool hovering = rect.isPointInside(m_pointer);
2256 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2257 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2261 item_hovered = true;
2262 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2264 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2268 //Draw inv slot borders
2270 s32 x1 = rect.UpperLeftCorner.X;
2271 s32 y1 = rect.UpperLeftCorner.Y;
2272 s32 x2 = rect.LowerRightCorner.X;
2273 s32 y2 = rect.LowerRightCorner.Y;
2275 driver->draw2DRectangle(m_slotbordercolor,
2276 core::rect<s32>(v2s32(x1 - border, y1 - border),
2277 v2s32(x2 + border, y1)), NULL);
2278 driver->draw2DRectangle(m_slotbordercolor,
2279 core::rect<s32>(v2s32(x1 - border, y2),
2280 v2s32(x2 + border, y2 + border)), NULL);
2281 driver->draw2DRectangle(m_slotbordercolor,
2282 core::rect<s32>(v2s32(x1 - border, y1),
2283 v2s32(x1, y2)), NULL);
2284 driver->draw2DRectangle(m_slotbordercolor,
2285 core::rect<s32>(v2s32(x2, y1),
2286 v2s32(x2 + border, y2)), NULL);
2294 item.takeItem(m_selected_amount);
2298 drawItemStack(driver, m_font, item,
2299 rect, &AbsoluteClippingRect, m_client,
2304 std::wstring tooltip_text = L"";
2305 if (hovering && !m_selected_item) {
2306 const std::string &desc = item.metadata.getString("description");
2309 utf8_to_wide(item.getDefinition(m_client->idef()).description);
2311 tooltip_text = utf8_to_wide(desc);
2313 if (tooltip_text != L"") {
2314 std::vector<std::wstring> tt_rows = str_split(tooltip_text, L'\n');
2315 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2316 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2317 m_tooltip_element->setVisible(true);
2318 this->bringToFront(m_tooltip_element);
2319 setStaticText(m_tooltip_element, tooltip_text.c_str());
2320 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2321 #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
2322 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2324 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2326 v2u32 screenSize = driver->getScreenSize();
2327 int tooltip_offset_x = m_btn_height;
2328 int tooltip_offset_y = m_btn_height;
2330 tooltip_offset_x *= 3;
2331 tooltip_offset_y = 0;
2332 if (m_pointer.X > (s32)screenSize.X / 2)
2333 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2335 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2336 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2337 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2338 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2339 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2340 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2341 m_tooltip_element->setRelativePosition(core::rect<s32>(
2342 core::position2d<s32>(tooltip_x, tooltip_y),
2343 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2349 void GUIFormSpecMenu::drawSelectedItem()
2351 video::IVideoDriver* driver = Environment->getVideoDriver();
2353 if (!m_selected_item) {
2354 drawItemStack(driver, m_font, ItemStack(),
2355 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2356 NULL, m_client, IT_ROT_DRAGGED);
2360 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2362 InventoryList *list = inv->getList(m_selected_item->listname);
2364 ItemStack stack = list->getItem(m_selected_item->i);
2365 stack.count = m_selected_amount;
2367 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2368 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2369 rect.constrainTo(driver->getViewPort());
2370 drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
2373 void GUIFormSpecMenu::drawMenu()
2376 std::string newform = m_form_src->getForm();
2377 if(newform != m_formspec_string){
2378 m_formspec_string = newform;
2379 regenerateGui(m_screensize_old);
2383 gui::IGUISkin* skin = Environment->getSkin();
2384 sanity_check(skin != NULL);
2385 gui::IGUIFont *old_font = skin->getFont();
2386 skin->setFont(m_font);
2388 updateSelectedItem();
2390 video::IVideoDriver* driver = Environment->getVideoDriver();
2392 v2u32 screenSize = driver->getScreenSize();
2393 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2395 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2397 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2399 m_tooltip_element->setVisible(false);
2404 for(u32 i=0; i<m_backgrounds.size(); i++)
2406 const ImageDrawSpec &spec = m_backgrounds[i];
2407 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2410 // Image size on screen
2411 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2412 // Image rectangle on screen
2413 core::rect<s32> rect = imgrect + spec.pos;
2416 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2417 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2418 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2419 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2420 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2423 const video::SColor color(255,255,255,255);
2424 const video::SColor colors[] = {color,color,color,color};
2425 draw2DImageFilterScaled(driver, texture, rect,
2426 core::rect<s32>(core::position2d<s32>(0,0),
2427 core::dimension2di(texture->getOriginalSize())),
2428 NULL/*&AbsoluteClippingRect*/, colors, true);
2430 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2431 errorstream << "\t" << spec.name << std::endl;
2438 for(u32 i=0; i<m_boxes.size(); i++)
2440 const BoxDrawSpec &spec = m_boxes[i];
2442 irr::video::SColor todraw = spec.color;
2444 todraw.setAlpha(140);
2446 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2447 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2449 driver->draw2DRectangle(todraw, rect, 0);
2455 gui::IGUIElement::draw();
2460 for(u32 i=0; i<m_images.size(); i++)
2462 const ImageDrawSpec &spec = m_images[i];
2463 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2466 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2467 // Image size on screen
2468 core::rect<s32> imgrect;
2471 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2474 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2476 // Image rectangle on screen
2477 core::rect<s32> rect = imgrect + spec.pos;
2478 const video::SColor color(255,255,255,255);
2479 const video::SColor colors[] = {color,color,color,color};
2480 draw2DImageFilterScaled(driver, texture, rect,
2481 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2482 NULL/*&AbsoluteClippingRect*/, colors, true);
2485 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2486 errorstream << "\t" << spec.name << std::endl;
2493 for(u32 i=0; i<m_itemimages.size(); i++)
2498 const ImageDrawSpec &spec = m_itemimages[i];
2499 IItemDefManager *idef = m_client->idef();
2501 item.deSerialize(spec.item_name, idef);
2502 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2503 // Viewport rectangle on screen
2504 core::rect<s32> rect = imgrect + spec.pos;
2505 if (spec.parent_button && spec.parent_button->isPressed()) {
2506 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2507 rect += core::dimension2d<s32>(
2508 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2510 rect += core::dimension2d<s32>(
2511 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2512 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2515 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2516 m_client, IT_ROT_NONE);
2521 Phase 0: Item slot rectangles
2522 Phase 1: Item images; prepare tooltip
2524 bool item_hovered = false;
2525 int start_phase = 0;
2526 for (int phase = start_phase; phase <= 1; phase++) {
2527 for (u32 i = 0; i < m_inventorylists.size(); i++) {
2528 drawList(m_inventorylists[i], phase, item_hovered);
2531 if (!item_hovered) {
2532 drawItemStack(driver, m_font, ItemStack(),
2533 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2534 NULL, m_client, IT_ROT_HOVERED);
2537 /* TODO find way to show tooltips on touchscreen */
2538 #ifndef HAVE_TOUCHSCREENGUI
2539 m_pointer = m_device->getCursorControl()->getPosition();
2543 Draw static text elements
2545 for (u32 i = 0; i < m_static_texts.size(); i++) {
2546 const StaticTextSpec &spec = m_static_texts[i];
2547 core::rect<s32> rect = spec.rect;
2548 if (spec.parent_button && spec.parent_button->isPressed()) {
2549 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2550 rect += core::dimension2d<s32>(
2551 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2553 // Use image offset instead of text's because its a bit smaller
2554 // and fits better, also TEXT_OFFSET_X is always 0
2555 rect += core::dimension2d<s32>(
2556 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2557 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2560 video::SColor color(255, 255, 255, 255);
2561 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
2565 Draw fields/buttons tooltips
2567 gui::IGUIElement *hovered =
2568 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2570 if (hovered != NULL) {
2571 s32 id = hovered->getID();
2575 m_old_tooltip_id = id;
2576 m_old_tooltip = L"";
2578 if (id == m_old_tooltip_id) {
2579 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2581 m_hovered_time = getTimeMs();
2582 m_old_tooltip_id = id;
2586 if (id != -1 && delta >= m_tooltip_show_delay) {
2587 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2588 iter != m_fields.end(); ++iter) {
2589 if (iter->fid == id && m_tooltips[iter->fname].tooltip != L"") {
2590 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2591 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2592 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2593 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2594 setStaticText(m_tooltip_element, m_tooltips[iter->fname].tooltip.c_str());
2595 std::vector<std::wstring> tt_rows = str_split(m_tooltips[iter->fname].tooltip, L'\n');
2596 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2597 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2598 int tooltip_offset_x = m_btn_height;
2599 int tooltip_offset_y = m_btn_height;
2601 tooltip_offset_x *= 3;
2602 tooltip_offset_y = 0;
2603 if (m_pointer.X > (s32)screenSize.X / 2)
2604 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2606 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2607 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2608 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2609 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2610 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2611 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2612 m_tooltip_element->setRelativePosition(core::rect<s32>(
2613 core::position2d<s32>(tooltip_x, tooltip_y),
2614 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2616 m_tooltip_element->setVisible(true);
2617 this->bringToFront(m_tooltip_element);
2624 m_tooltip_element->draw();
2627 Draw dragged item stack
2631 skin->setFont(old_font);
2634 void GUIFormSpecMenu::updateSelectedItem()
2636 // If the selected stack has become empty for some reason, deselect it.
2637 // If the selected stack has become inaccessible, deselect it.
2638 // If the selected stack has become smaller, adjust m_selected_amount.
2639 ItemStack selected = verifySelectedItem();
2641 // WARNING: BLACK MAGIC
2642 // See if there is a stack suited for our current guess.
2643 // If such stack does not exist, clear the guess.
2644 if(m_selected_content_guess.name != "" &&
2645 selected.name == m_selected_content_guess.name &&
2646 selected.count == m_selected_content_guess.count){
2647 // Selected item fits the guess. Skip the black magic.
2649 else if(m_selected_content_guess.name != ""){
2651 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2652 const ListDrawSpec &s = m_inventorylists[i];
2653 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2656 InventoryList *list = inv->getList(s.listname);
2659 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2660 u32 item_i = i + s.start_item_i;
2661 if(item_i >= list->getSize())
2663 ItemStack stack = list->getItem(item_i);
2664 if(stack.name == m_selected_content_guess.name &&
2665 stack.count == m_selected_content_guess.count){
2667 infostream<<"Client: Changing selected content guess to "
2668 <<s.inventoryloc.dump()<<" "<<s.listname
2669 <<" "<<item_i<<std::endl;
2670 delete m_selected_item;
2671 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2672 m_selected_amount = stack.count;
2677 infostream<<"Client: Discarding selected content guess: "
2678 <<m_selected_content_guess.getItemString()<<std::endl;
2679 m_selected_content_guess.name = "";
2683 // If craftresult is nonempty and nothing else is selected, select it now.
2684 if(!m_selected_item)
2686 for(u32 i=0; i<m_inventorylists.size(); i++)
2688 const ListDrawSpec &s = m_inventorylists[i];
2689 if(s.listname == "craftpreview")
2691 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2692 InventoryList *list = inv->getList("craftresult");
2693 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2695 m_selected_item = new ItemSpec;
2696 m_selected_item->inventoryloc = s.inventoryloc;
2697 m_selected_item->listname = "craftresult";
2698 m_selected_item->i = 0;
2699 m_selected_amount = 0;
2700 m_selected_dragging = false;
2707 // If craftresult is selected, keep the whole stack selected
2708 if(m_selected_item && m_selected_item->listname == "craftresult")
2710 m_selected_amount = verifySelectedItem().count;
2714 ItemStack GUIFormSpecMenu::verifySelectedItem()
2716 // If the selected stack has become empty for some reason, deselect it.
2717 // If the selected stack has become inaccessible, deselect it.
2718 // If the selected stack has become smaller, adjust m_selected_amount.
2719 // Return the selected stack.
2723 if(m_selected_item->isValid())
2725 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2728 InventoryList *list = inv->getList(m_selected_item->listname);
2729 if(list && (u32) m_selected_item->i < list->getSize())
2731 ItemStack stack = list->getItem(m_selected_item->i);
2732 if(m_selected_amount > stack.count)
2733 m_selected_amount = stack.count;
2740 // selection was not valid
2741 delete m_selected_item;
2742 m_selected_item = NULL;
2743 m_selected_amount = 0;
2744 m_selected_dragging = false;
2749 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2755 if (quitmode == quit_mode_accept) {
2756 fields["quit"] = "true";
2759 if (quitmode == quit_mode_cancel) {
2760 fields["quit"] = "true";
2761 m_text_dst->gotText(fields);
2765 if (current_keys_pending.key_down) {
2766 fields["key_down"] = "true";
2767 current_keys_pending.key_down = false;
2770 if (current_keys_pending.key_up) {
2771 fields["key_up"] = "true";
2772 current_keys_pending.key_up = false;
2775 if (current_keys_pending.key_enter) {
2776 fields["key_enter"] = "true";
2777 current_keys_pending.key_enter = false;
2780 if (!current_field_enter_pending.empty()) {
2781 fields["key_enter_field"] = current_field_enter_pending;
2782 current_field_enter_pending = "";
2785 if (current_keys_pending.key_escape) {
2786 fields["key_escape"] = "true";
2787 current_keys_pending.key_escape = false;
2790 for(unsigned int i=0; i<m_fields.size(); i++) {
2791 const FieldSpec &s = m_fields[i];
2793 std::string name = s.fname;
2794 if (s.ftype == f_Button) {
2795 fields[name] = wide_to_utf8(s.flabel);
2796 } else if (s.ftype == f_Table) {
2797 GUITable *table = getTable(s.fname);
2799 fields[name] = table->checkEvent();
2802 else if(s.ftype == f_DropDown) {
2803 // no dynamic cast possible due to some distributions shipped
2804 // without rtti support in irrlicht
2805 IGUIElement * element = getElementFromId(s.fid);
2806 gui::IGUIComboBox *e = NULL;
2807 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2808 e = static_cast<gui::IGUIComboBox*>(element);
2810 s32 selected = e->getSelected();
2811 if (selected >= 0) {
2812 std::vector<std::string> *dropdown_values =
2813 getDropDownValues(s.fname);
2814 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2815 fields[name] = (*dropdown_values)[selected];
2819 else if (s.ftype == f_TabHeader) {
2820 // no dynamic cast possible due to some distributions shipped
2821 // without rtti support in irrlicht
2822 IGUIElement * element = getElementFromId(s.fid);
2823 gui::IGUITabControl *e = NULL;
2824 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2825 e = static_cast<gui::IGUITabControl*>(element);
2829 std::stringstream ss;
2830 ss << (e->getActiveTab() +1);
2831 fields[name] = ss.str();
2834 else if (s.ftype == f_CheckBox) {
2835 // no dynamic cast possible due to some distributions shipped
2836 // without rtti support in irrlicht
2837 IGUIElement * element = getElementFromId(s.fid);
2838 gui::IGUICheckBox *e = NULL;
2839 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2840 e = static_cast<gui::IGUICheckBox*>(element);
2845 fields[name] = "true";
2847 fields[name] = "false";
2850 else if (s.ftype == f_ScrollBar) {
2851 // no dynamic cast possible due to some distributions shipped
2852 // without rtti support in irrlicht
2853 IGUIElement * element = getElementFromId(s.fid);
2854 gui::IGUIScrollBar *e = NULL;
2855 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2856 e = static_cast<gui::IGUIScrollBar*>(element);
2860 std::stringstream os;
2862 if (s.fdefault == L"Changed")
2863 fields[name] = "CHG:" + os.str();
2865 fields[name] = "VAL:" + os.str();
2870 IGUIElement* e = getElementFromId(s.fid);
2872 fields[name] = wide_to_utf8(e->getText());
2878 m_text_dst->gotText(fields);
2882 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2884 while(tocheck != NULL) {
2885 if (tocheck == parent) {
2888 tocheck = tocheck->getParent();
2893 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2895 // The IGUITabControl renders visually using the skin's selected
2896 // font, which we override for the duration of form drawing,
2897 // but computes tab hotspots based on how it would have rendered
2898 // using the font that is selected at the time of button release.
2899 // To make these two consistent, temporarily override the skin's
2900 // font while the IGUITabControl is processing the event.
2901 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2902 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2903 s32 x = event.MouseInput.X;
2904 s32 y = event.MouseInput.Y;
2905 gui::IGUIElement *hovered =
2906 Environment->getRootGUIElement()->getElementFromPoint(
2907 core::position2d<s32>(x, y));
2908 if (hovered && isMyChild(hovered) &&
2909 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2910 gui::IGUISkin* skin = Environment->getSkin();
2911 sanity_check(skin != NULL);
2912 gui::IGUIFont *old_font = skin->getFont();
2913 skin->setFont(m_font);
2914 bool retval = hovered->OnEvent(event);
2915 skin->setFont(old_font);
2920 // Fix Esc/Return key being eaten by checkboxen and tables
2921 if(event.EventType==EET_KEY_INPUT_EVENT) {
2922 KeyPress kp(event.KeyInput);
2923 if (kp == EscapeKey || kp == CancelKey
2924 || kp == getKeySetting("keymap_inventory")
2925 || event.KeyInput.Key==KEY_RETURN) {
2926 gui::IGUIElement *focused = Environment->getFocus();
2927 if (focused && isMyChild(focused) &&
2928 (focused->getType() == gui::EGUIET_LIST_BOX ||
2929 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2935 // Mouse wheel events: send to hovered element instead of focused
2936 if(event.EventType==EET_MOUSE_INPUT_EVENT
2937 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2938 s32 x = event.MouseInput.X;
2939 s32 y = event.MouseInput.Y;
2940 gui::IGUIElement *hovered =
2941 Environment->getRootGUIElement()->getElementFromPoint(
2942 core::position2d<s32>(x, y));
2943 if (hovered && isMyChild(hovered)) {
2944 hovered->OnEvent(event);
2949 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2950 s32 x = event.MouseInput.X;
2951 s32 y = event.MouseInput.Y;
2952 gui::IGUIElement *hovered =
2953 Environment->getRootGUIElement()->getElementFromPoint(
2954 core::position2d<s32>(x, y));
2955 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2956 m_old_tooltip_id = -1;
2957 m_old_tooltip = L"";
2959 if (!isChild(hovered,this)) {
2960 if (DoubleClickDetection(event)) {
2967 // display software keyboard when clicking edit boxes
2968 if (event.EventType == EET_MOUSE_INPUT_EVENT
2969 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2970 gui::IGUIElement *hovered =
2971 Environment->getRootGUIElement()->getElementFromPoint(
2972 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2973 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2974 bool retval = hovered->OnEvent(event);
2976 Environment->setFocus(hovered);
2978 m_JavaDialogFieldName = getNameByID(hovered->getID());
2979 std::string message = gettext("Enter ");
2980 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
2984 message += gettext(label) + ":";
2986 /* single line text input */
2989 /* multi line text input */
2990 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2994 /* passwords are always single line */
2995 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2999 porting::showInputDialog(gettext("ok"), "",
3000 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
3006 if (event.EventType == EET_TOUCH_INPUT_EVENT)
3009 memset(&translated, 0, sizeof(SEvent));
3010 translated.EventType = EET_MOUSE_INPUT_EVENT;
3011 gui::IGUIElement* root = Environment->getRootGUIElement();
3015 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
3019 gui::IGUIElement* hovered = root->getElementFromPoint(
3020 core::position2d<s32>(
3022 event.TouchInput.Y));
3024 translated.MouseInput.X = event.TouchInput.X;
3025 translated.MouseInput.Y = event.TouchInput.Y;
3026 translated.MouseInput.Control = false;
3028 bool dont_send_event = false;
3030 if (event.TouchInput.touchedCount == 1) {
3031 switch (event.TouchInput.Event) {
3032 case ETIE_PRESSED_DOWN:
3033 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
3034 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
3035 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3036 m_down_pos = m_pointer;
3039 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
3040 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
3041 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3044 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
3045 translated.MouseInput.ButtonStates = 0;
3046 hovered = root->getElementFromPoint(m_down_pos);
3047 /* we don't have a valid pointer element use last
3048 * known pointer pos */
3049 translated.MouseInput.X = m_pointer.X;
3050 translated.MouseInput.Y = m_pointer.Y;
3052 /* reset down pos */
3053 m_down_pos = v2s32(0,0);
3056 dont_send_event = true;
3057 //this is not supposed to happen
3059 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
3060 << event.TouchInput.Event << std::endl;
3062 } else if ( (event.TouchInput.touchedCount == 2) &&
3063 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
3064 hovered = root->getElementFromPoint(m_down_pos);
3066 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
3067 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
3068 translated.MouseInput.X = m_pointer.X;
3069 translated.MouseInput.Y = m_pointer.Y;
3072 hovered->OnEvent(translated);
3075 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
3076 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3080 hovered->OnEvent(translated);
3082 dont_send_event = true;
3084 /* ignore unhandled 2 touch events ... accidental moving for example */
3085 else if (event.TouchInput.touchedCount == 2) {
3086 dont_send_event = true;
3088 else if (event.TouchInput.touchedCount > 2) {
3090 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
3091 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3094 if (dont_send_event) {
3098 /* check if translated event needs to be preprocessed again */
3099 if (preprocessEvent(translated)) {
3104 bool retval = hovered->OnEvent(translated);
3106 if (event.TouchInput.Event == ETIE_LEFT_UP) {
3108 m_pointer = v2s32(0,0);
3116 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3117 /* TODO add a check like:
3118 if (event.JoystickEvent != joystick_we_listen_for)
3121 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3123 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3125 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3127 acceptInput(quit_mode_accept);
3138 /******************************************************************************/
3139 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3141 /* The following code is for capturing double-clicks of the mouse button
3142 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3143 * -- which closes the form -- under some circumstances.
3145 * There have been many github issues reporting this as a bug even though it
3146 * was an intended feature. For this reason, remapping the double-click as
3147 * an ESC must be explicitly set when creating this class via the
3148 * /p remap_dbl_click parameter of the constructor.
3151 if (!m_remap_dbl_click)
3154 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3155 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3156 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3158 m_doubleclickdetect[1].pos = m_pointer;
3159 m_doubleclickdetect[1].time = getTimeMs();
3161 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3162 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
3167 double squaredistance =
3168 m_doubleclickdetect[0].pos
3169 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3171 if (squaredistance > (30*30)) {
3175 SEvent* translated = new SEvent();
3176 assert(translated != 0);
3177 //translate doubleclick to escape
3178 memset(translated, 0, sizeof(SEvent));
3179 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3180 translated->KeyInput.Key = KEY_ESCAPE;
3181 translated->KeyInput.Control = false;
3182 translated->KeyInput.Shift = false;
3183 translated->KeyInput.PressedDown = true;
3184 translated->KeyInput.Char = 0;
3185 OnEvent(*translated);
3187 // no need to send the key up event as we're already deleted
3188 // and no one else did notice this event
3196 void GUIFormSpecMenu::tryClose()
3200 acceptInput(quit_mode_cancel);
3203 m_text_dst->gotText(L"MenuQuit");
3207 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3209 if (event.EventType==EET_KEY_INPUT_EVENT) {
3210 KeyPress kp(event.KeyInput);
3211 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3212 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3215 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3216 (kp == getKeySetting("keymap_screenshot"))) {
3217 m_client->makeScreenshot(m_device);
3219 if (event.KeyInput.PressedDown &&
3220 (event.KeyInput.Key==KEY_RETURN ||
3221 event.KeyInput.Key==KEY_UP ||
3222 event.KeyInput.Key==KEY_DOWN)
3224 switch (event.KeyInput.Key) {
3226 current_keys_pending.key_enter = true;
3229 current_keys_pending.key_up = true;
3232 current_keys_pending.key_down = true;
3236 //can't happen at all!
3237 FATAL_ERROR("Reached a source line that can't ever been reached");
3240 if (current_keys_pending.key_enter && m_allowclose) {
3241 acceptInput(quit_mode_accept);
3251 /* Mouse event other than movement, or crossing the border of inventory
3252 field while holding right mouse button
3254 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3255 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3256 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3257 event.MouseInput.isRightPressed() &&
3258 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3260 // Get selected item and hovered/clicked item (s)
3262 m_old_tooltip_id = -1;
3263 updateSelectedItem();
3264 ItemSpec s = getItemAtPos(m_pointer);
3266 Inventory *inv_selected = NULL;
3267 Inventory *inv_s = NULL;
3268 InventoryList *list_s = NULL;
3270 if (m_selected_item) {
3271 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3272 sanity_check(inv_selected);
3273 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3280 inv_s = m_invmgr->getInventory(s.inventoryloc);
3283 errorstream << "InventoryMenu: The selected inventory location "
3284 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3286 s.i = -1; // make it invalid again
3290 list_s = inv_s->getList(s.listname);
3291 if (list_s == NULL) {
3292 verbosestream << "InventoryMenu: The selected inventory list \""
3293 << s.listname << "\" does not exist" << std::endl;
3294 s.i = -1; // make it invalid again
3298 if ((u32)s.i >= list_s->getSize()) {
3299 infostream << "InventoryMenu: The selected inventory list \""
3300 << s.listname << "\" is too small (i=" << s.i << ", size="
3301 << list_s->getSize() << ")" << std::endl;
3302 s.i = -1; // make it invalid again
3306 s_count = list_s->getItem(s.i).count;
3309 bool identical = (m_selected_item != NULL) && s.isValid() &&
3310 (inv_selected == inv_s) &&
3311 (m_selected_item->listname == s.listname) &&
3312 (m_selected_item->i == s.i);
3314 // buttons: 0 = left, 1 = right, 2 = middle
3315 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3318 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3319 { button = 0; updown = 0; }
3320 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3321 { button = 1; updown = 0; }
3322 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3323 { button = 2; updown = 0; }
3324 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3325 { button = 0; updown = 1; }
3326 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3327 { button = 1; updown = 1; }
3328 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3329 { button = 2; updown = 1; }
3330 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3333 // Set this number to a positive value to generate a move action
3334 // from m_selected_item to s.
3335 u32 move_amount = 0;
3337 // Set this number to a positive value to generate a move action
3338 // from s to the next inventory ring.
3339 u32 shift_move_amount = 0;
3341 // Set this number to a positive value to generate a drop action
3342 // from m_selected_item.
3343 u32 drop_amount = 0;
3345 // Set this number to a positive value to generate a craft action at s.
3346 u32 craft_amount = 0;
3349 // Some mouse button has been pressed
3351 //infostream<<"Mouse button "<<button<<" pressed at p=("
3352 // <<p.X<<","<<p.Y<<")"<<std::endl;
3354 m_selected_dragging = false;
3356 if (s.isValid() && s.listname == "craftpreview") {
3357 // Craft preview has been clicked: craft
3358 craft_amount = (button == 2 ? 10 : 1);
3359 } else if (m_selected_item == NULL) {
3361 // Non-empty stack has been clicked: select or shift-move it
3362 m_selected_item = new ItemSpec(s);
3365 if (button == 1) // right
3366 count = (s_count + 1) / 2;
3367 else if (button == 2) // middle
3368 count = MYMIN(s_count, 10);
3372 if (!event.MouseInput.Shift) {
3373 // no shift: select item
3374 m_selected_amount = count;
3375 m_selected_dragging = true;
3376 m_rmouse_auto_place = false;
3378 // shift pressed: move item
3380 shift_move_amount = count;
3381 else // count of 1 at left click like after drag & drop
3382 shift_move_amount = 1;
3385 } else { // m_selected_item != NULL
3386 assert(m_selected_amount >= 1);
3389 // Clicked a slot: move
3390 if (button == 1) // right
3392 else if (button == 2) // middle
3393 move_amount = MYMIN(m_selected_amount, 10);
3395 move_amount = m_selected_amount;
3398 if (move_amount >= m_selected_amount)
3399 m_selected_amount = 0;
3401 m_selected_amount -= move_amount;
3405 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3406 // Clicked outside of the window: drop
3407 if (button == 1) // right
3409 else if (button == 2) // middle
3410 drop_amount = MYMIN(m_selected_amount, 10);
3412 drop_amount = m_selected_amount;
3416 else if (updown == 1) {
3417 // Some mouse button has been released
3419 //infostream<<"Mouse button "<<button<<" released at p=("
3420 // <<p.X<<","<<p.Y<<")"<<std::endl;
3422 if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3424 // Dragged to different slot: move all selected
3425 move_amount = m_selected_amount;
3427 } else if (m_selected_item != NULL && m_selected_dragging &&
3428 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3429 // Dragged outside of window: drop all selected
3430 drop_amount = m_selected_amount;
3433 m_selected_dragging = false;
3434 // Keep count of how many times right mouse button has been
3435 // clicked. One click is drag without dropping. Click + release
3436 // + click changes to drop one item when moved mode
3437 if (button == 1 && m_selected_item != NULL)
3438 m_rmouse_auto_place = !m_rmouse_auto_place;
3439 } else if (updown == -1) {
3440 // Mouse has been moved and rmb is down and mouse pointer just
3441 // entered a new inventory field (checked in the entry-if, this
3442 // is the only action here that is generated by mouse movement)
3443 if (m_selected_item != NULL && s.isValid()) {
3445 // TODO: middle mouse to move 10 items might be handy
3446 if (m_rmouse_auto_place) {
3447 // Only move an item if the destination slot is empty
3448 // or contains the same item type as what is going to be
3450 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3451 InventoryList *list_to = list_s;
3452 assert(list_from && list_to);
3453 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3454 ItemStack stack_to = list_to->getItem(s.i);
3455 if (stack_to.empty() || stack_to.name == stack_from.name)
3461 // Possibly send inventory action to server
3462 if (move_amount > 0) {
3463 // Send IACTION_MOVE
3465 assert(m_selected_item && m_selected_item->isValid());
3466 assert(s.isValid());
3468 assert(inv_selected && inv_s);
3469 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3470 InventoryList *list_to = list_s;
3471 assert(list_from && list_to);
3472 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3473 ItemStack stack_to = list_to->getItem(s.i);
3475 // Check how many items can be moved
3476 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3477 ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
3478 // If source stack cannot be added to destination stack at all,
3480 if ((leftover.count == stack_from.count) &&
3481 (leftover.name == stack_from.name)) {
3482 m_selected_amount = stack_to.count;
3483 // In case the server doesn't directly swap them but instead
3484 // moves stack_to somewhere else, set this
3485 m_selected_content_guess = stack_to;
3486 m_selected_content_guess_inventory = s.inventoryloc;
3488 // Source stack goes fully into destination stack
3489 else if (leftover.empty()) {
3490 m_selected_amount -= move_amount;
3491 m_selected_content_guess = ItemStack(); // Clear
3493 // Source stack goes partly into destination stack
3495 move_amount -= leftover.count;
3496 m_selected_amount -= move_amount;
3497 m_selected_content_guess = ItemStack(); // Clear
3500 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3501 IMoveAction *a = new IMoveAction();
3502 a->count = move_amount;
3503 a->from_inv = m_selected_item->inventoryloc;
3504 a->from_list = m_selected_item->listname;
3505 a->from_i = m_selected_item->i;
3506 a->to_inv = s.inventoryloc;
3507 a->to_list = s.listname;
3509 m_invmgr->inventoryAction(a);
3510 } else if (shift_move_amount > 0) {
3511 u32 mis = m_inventory_rings.size();
3513 for (; i < mis; i++) {
3514 const ListRingSpec &sp = m_inventory_rings[i];
3515 if (sp.inventoryloc == s.inventoryloc
3516 && sp.listname == s.listname)
3520 if (i >= mis) // if not found
3522 u32 to_inv_ind = (i + 1) % mis;
3523 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3524 InventoryList *list_from = list_s;
3527 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3530 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3533 ItemStack stack_from = list_from->getItem(s.i);
3534 assert(shift_move_amount <= stack_from.count);
3535 if (m_client->getProtoVersion() >= 25) {
3536 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3537 IMoveAction *a = new IMoveAction();
3538 a->count = shift_move_amount;
3539 a->from_inv = s.inventoryloc;
3540 a->from_list = s.listname;
3542 a->to_inv = to_inv_sp.inventoryloc;
3543 a->to_list = to_inv_sp.listname;
3544 a->move_somewhere = true;
3545 m_invmgr->inventoryAction(a);
3547 // find a place (or more than one) to add the new item
3548 u32 ilt_size = list_to->getSize();
3550 for (u32 slot_to = 0; slot_to < ilt_size
3551 && shift_move_amount > 0; slot_to++) {
3552 list_to->itemFits(slot_to, stack_from, &leftover);
3553 if (leftover.count < stack_from.count) {
3554 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3555 IMoveAction *a = new IMoveAction();
3556 a->count = MYMIN(shift_move_amount,
3557 (u32) (stack_from.count - leftover.count));
3558 shift_move_amount -= a->count;
3559 a->from_inv = s.inventoryloc;
3560 a->from_list = s.listname;
3562 a->to_inv = to_inv_sp.inventoryloc;
3563 a->to_list = to_inv_sp.listname;
3565 m_invmgr->inventoryAction(a);
3566 stack_from = leftover;
3571 } else if (drop_amount > 0) {
3572 m_selected_content_guess = ItemStack(); // Clear
3574 // Send IACTION_DROP
3576 assert(m_selected_item && m_selected_item->isValid());
3577 assert(inv_selected);
3578 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3580 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3582 // Check how many items can be dropped
3583 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3584 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3585 m_selected_amount -= drop_amount;
3587 infostream << "Handing IACTION_DROP to manager" << std::endl;
3588 IDropAction *a = new IDropAction();
3589 a->count = drop_amount;
3590 a->from_inv = m_selected_item->inventoryloc;
3591 a->from_list = m_selected_item->listname;
3592 a->from_i = m_selected_item->i;
3593 m_invmgr->inventoryAction(a);
3594 } else if (craft_amount > 0) {
3595 m_selected_content_guess = ItemStack(); // Clear
3597 // Send IACTION_CRAFT
3599 assert(s.isValid());
3602 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3603 ICraftAction *a = new ICraftAction();
3604 a->count = craft_amount;
3605 a->craft_inv = s.inventoryloc;
3606 m_invmgr->inventoryAction(a);
3609 // If m_selected_amount has been decreased to zero, deselect
3610 if (m_selected_amount == 0) {
3611 delete m_selected_item;
3612 m_selected_item = NULL;
3613 m_selected_amount = 0;
3614 m_selected_dragging = false;
3615 m_selected_content_guess = ItemStack();
3617 m_old_pointer = m_pointer;
3619 if (event.EventType == EET_GUI_EVENT) {
3621 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3623 // find the element that was clicked
3624 for (unsigned int i=0; i<m_fields.size(); i++) {
3625 FieldSpec &s = m_fields[i];
3626 if ((s.ftype == f_TabHeader) &&
3627 (s.fid == event.GUIEvent.Caller->getID())) {
3635 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3637 if (!canTakeFocus(event.GUIEvent.Element)) {
3638 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3640 // Returning true disables focus change
3644 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3645 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3646 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3647 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3648 unsigned int btn_id = event.GUIEvent.Caller->getID();
3650 if (btn_id == 257) {
3652 acceptInput(quit_mode_accept);
3656 m_text_dst->gotText(L"ExitButton");
3658 // quitMenu deallocates menu
3662 // find the element that was clicked
3663 for (u32 i = 0; i < m_fields.size(); i++) {
3664 FieldSpec &s = m_fields[i];
3665 // if its a button, set the send field so
3666 // lua knows which button was pressed
3667 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3668 (s.fid == event.GUIEvent.Caller->getID())) {
3672 acceptInput(quit_mode_accept);
3675 m_text_dst->gotText(L"ExitButton");
3679 acceptInput(quit_mode_no);
3683 } else if ((s.ftype == f_DropDown) &&
3684 (s.fid == event.GUIEvent.Caller->getID())) {
3685 // only send the changed dropdown
3686 for (u32 i = 0; i < m_fields.size(); i++) {
3687 FieldSpec &s2 = m_fields[i];
3688 if (s2.ftype == f_DropDown) {
3693 acceptInput(quit_mode_no);
3695 // revert configuration to make sure dropdowns are sent on
3696 // regular button click
3697 for (u32 i = 0; i < m_fields.size(); i++) {
3698 FieldSpec &s2 = m_fields[i];
3699 if (s2.ftype == f_DropDown) {
3704 } else if ((s.ftype == f_ScrollBar) &&
3705 (s.fid == event.GUIEvent.Caller->getID())) {
3706 s.fdefault = L"Changed";
3707 acceptInput(quit_mode_no);
3713 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3714 if (event.GUIEvent.Caller->getID() > 257) {
3715 bool close_on_enter = true;
3716 for (u32 i = 0; i < m_fields.size(); i++) {
3717 FieldSpec &s = m_fields[i];
3718 if (s.ftype == f_Unknown &&
3719 s.fid == event.GUIEvent.Caller->getID()) {
3720 current_field_enter_pending = s.fname;
3721 UNORDERED_MAP<std::string, bool>::const_iterator it =
3722 field_close_on_enter.find(s.fname);
3723 if (it != field_close_on_enter.end())
3724 close_on_enter = (*it).second;
3730 if (m_allowclose && close_on_enter) {
3731 current_keys_pending.key_enter = true;
3732 acceptInput(quit_mode_accept);
3735 current_keys_pending.key_enter = true;
3738 // quitMenu deallocates menu
3743 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3744 int current_id = event.GUIEvent.Caller->getID();
3745 if (current_id > 257) {
3746 // find the element that was clicked
3747 for (u32 i = 0; i < m_fields.size(); i++) {
3748 FieldSpec &s = m_fields[i];
3749 // if it's a table, set the send field
3750 // so lua knows which table was changed
3751 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3762 return Parent ? Parent->OnEvent(event) : false;
3766 * get name of element by element id
3767 * @param id of element
3768 * @return name string or empty string
3770 std::string GUIFormSpecMenu::getNameByID(s32 id)
3772 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3773 iter != m_fields.end(); ++iter) {
3774 if (iter->fid == id) {
3782 * get label of element by id
3783 * @param id of element
3784 * @return label string or empty string
3786 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3788 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3789 iter != m_fields.end(); ++iter) {
3790 if (iter->fid == id) {
3791 return iter->flabel;