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"
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 "guiscalingfilter.h"
55 #define MY_CHECKPOS(a,b) \
56 if (v_pos.size() != 2) { \
57 errorstream<< "Invalid pos for element " << a << "specified: \"" \
58 << parts[b] << "\"" << std::endl; \
62 #define MY_CHECKGEOM(a,b) \
63 if (v_geom.size() != 2) { \
64 errorstream<< "Invalid pos for element " << a << "specified: \"" \
65 << parts[b] << "\"" << std::endl; \
71 static unsigned int font_line_height(gui::IGUIFont *font)
73 return font->getDimension(L"Ay").Height + font->getKerningHeight();
76 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
77 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
78 InventoryManager *invmgr, IGameDef *gamedef,
79 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
80 Client* client, bool remap_dbl_click) :
81 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
87 m_selected_item(NULL),
89 m_selected_dragging(false),
90 m_tooltip_element(NULL),
93 m_rmouse_auto_place(false),
98 m_formspec_version(0),
99 m_focused_element(L""),
101 m_remap_dbl_click(remap_dbl_click)
103 ,m_JavaDialogFieldName(L"")
106 current_keys_pending.key_down = false;
107 current_keys_pending.key_up = false;
108 current_keys_pending.key_enter = false;
109 current_keys_pending.key_escape = false;
111 m_doubleclickdetect[0].time = 0;
112 m_doubleclickdetect[1].time = 0;
114 m_doubleclickdetect[0].pos = v2s32(0, 0);
115 m_doubleclickdetect[1].pos = v2s32(0, 0);
117 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
120 GUIFormSpecMenu::~GUIFormSpecMenu()
124 for (u32 i = 0; i < m_tables.size(); ++i) {
125 GUITable *table = m_tables[i].second;
129 delete m_selected_item;
131 if (m_form_src != NULL) {
134 if (m_text_dst != NULL) {
139 void GUIFormSpecMenu::removeChildren()
141 const core::list<gui::IGUIElement*> &children = getChildren();
143 while(!children.empty()) {
144 (*children.getLast())->remove();
147 if(m_tooltip_element) {
148 m_tooltip_element->remove();
149 m_tooltip_element->drop();
150 m_tooltip_element = NULL;
155 void GUIFormSpecMenu::setInitialFocus()
157 // Set initial focus according to following order of precedence:
158 // 1. first empty editbox
162 // 5. first focusable (not statictext, not tabheader)
163 // 6. first child element
165 core::list<gui::IGUIElement*> children = getChildren();
167 // in case "children" contains any NULL elements, remove them
168 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
169 it != children.end();) {
173 it = children.erase(it);
176 // 1. first empty editbox
177 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
178 it != children.end(); ++it) {
179 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
180 && (*it)->getText()[0] == 0) {
181 Environment->setFocus(*it);
187 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
188 it != children.end(); ++it) {
189 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
190 Environment->setFocus(*it);
196 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
197 it != children.end(); ++it) {
198 if ((*it)->getTypeName() == std::string("GUITable")) {
199 Environment->setFocus(*it);
205 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
206 it != children.end(); --it) {
207 if ((*it)->getType() == gui::EGUIET_BUTTON) {
208 Environment->setFocus(*it);
213 // 5. first focusable (not statictext, not tabheader)
214 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
215 it != children.end(); ++it) {
216 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
217 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
218 Environment->setFocus(*it);
223 // 6. first child element
224 if (children.empty())
225 Environment->setFocus(this);
227 Environment->setFocus(*(children.begin()));
230 GUITable* GUIFormSpecMenu::getTable(std::wstring tablename)
232 for (u32 i = 0; i < m_tables.size(); ++i) {
233 if (tablename == m_tables[i].first.fname)
234 return m_tables[i].second;
239 static std::vector<std::string> split(const std::string &s, char delim)
241 std::vector<std::string> tokens;
243 std::string current = "";
244 bool last_was_escape = false;
245 for (unsigned int i = 0; i < s.size(); i++) {
246 char si = s.c_str()[i];
247 if (last_was_escape) {
250 last_was_escape = false;
253 tokens.push_back(current);
255 last_was_escape = false;
256 } else if (si == '\\') {
257 last_was_escape = true;
260 last_was_escape = false;
265 tokens.push_back(current);
270 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
272 std::vector<std::string> parts = split(element,',');
274 if (((parts.size() == 2) || parts.size() == 3) ||
275 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
277 if (parts[1].find(';') != std::string::npos)
278 parts[1] = parts[1].substr(0,parts[1].find(';'));
280 data->invsize.X = MYMAX(0, stof(parts[0]));
281 data->invsize.Y = MYMAX(0, stof(parts[1]));
284 if (parts.size() == 3) {
285 if (parts[2] == "true") {
286 lockSize(true,v2u32(800,600));
290 data->explicit_size = true;
293 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
296 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
298 if (m_gamedef == 0) {
299 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
303 std::vector<std::string> parts = split(element,';');
305 if (((parts.size() == 4) || (parts.size() == 5)) ||
306 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
308 std::string location = parts[0];
309 std::string listname = parts[1];
310 std::vector<std::string> v_pos = split(parts[2],',');
311 std::vector<std::string> v_geom = split(parts[3],',');
312 std::string startindex = "";
313 if (parts.size() == 5)
314 startindex = parts[4];
316 MY_CHECKPOS("list",2);
317 MY_CHECKGEOM("list",3);
319 InventoryLocation loc;
321 if(location == "context" || location == "current_name")
322 loc = m_current_inventory_location;
324 loc.deSerialize(location);
326 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
327 pos.X += stof(v_pos[0]) * (float)spacing.X;
328 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
331 geom.X = stoi(v_geom[0]);
332 geom.Y = stoi(v_geom[1]);
336 start_i = stoi(startindex);
338 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
339 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
343 if(!data->explicit_size)
344 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
345 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
348 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
351 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
353 std::vector<std::string> parts = split(element,';');
355 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
356 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
358 std::vector<std::string> v_pos = split(parts[0],',');
359 std::string name = parts[1];
360 std::string label = parts[2];
361 std::string selected = "";
363 if (parts.size() >= 4)
366 MY_CHECKPOS("checkbox",0);
369 pos.X += stof(v_pos[0]) * (float) spacing.X;
370 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
372 bool fselected = false;
374 if (selected == "true")
377 std::wstring wlabel = narrow_to_wide(label);
379 core::rect<s32> rect = core::rect<s32>(
380 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
381 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
382 pos.Y + ((imgsize.Y/2) + m_btn_height));
385 narrow_to_wide(name),
386 wlabel, //Needed for displaying text on MSVC
391 spec.ftype = f_CheckBox;
393 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
394 spec.fid, spec.flabel.c_str());
396 if (spec.fname == data->focused_fieldname) {
397 Environment->setFocus(e);
400 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
401 m_fields.push_back(spec);
404 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
407 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
409 std::vector<std::string> parts = split(element,';');
411 if (parts.size() >= 5) {
412 std::vector<std::string> v_pos = split(parts[0],',');
413 std::vector<std::string> v_dim = split(parts[1],',');
414 std::string name = parts[2];
415 std::string value = parts[4];
417 MY_CHECKPOS("scrollbar",0);
420 pos.X += stof(v_pos[0]) * (float) spacing.X;
421 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
423 if (v_dim.size() != 2) {
424 errorstream<< "Invalid size for element " << "scrollbar"
425 << "specified: \"" << parts[1] << "\"" << std::endl;
430 dim.X = stof(v_dim[0]) * (float) spacing.X;
431 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
433 core::rect<s32> rect =
434 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
437 narrow_to_wide(name),
443 bool is_horizontal = true;
445 if (parts[2] == "vertical")
446 is_horizontal = false;
448 spec.ftype = f_ScrollBar;
450 gui::IGUIScrollBar* e =
451 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
455 e->setPos(stoi(parts[4]));
457 e->setLargeStep(100);
459 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
460 m_fields.push_back(spec);
463 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
466 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
468 std::vector<std::string> parts = split(element,';');
470 if ((parts.size() == 3) ||
471 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
473 std::vector<std::string> v_pos = split(parts[0],',');
474 std::vector<std::string> v_geom = split(parts[1],',');
475 std::string name = unescape_string(parts[2]);
477 MY_CHECKPOS("image",0);
478 MY_CHECKGEOM("image",1);
480 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
481 pos.X += stof(v_pos[0]) * (float) spacing.X;
482 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
485 geom.X = stof(v_geom[0]) * (float)imgsize.X;
486 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
488 if(!data->explicit_size)
489 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
490 m_images.push_back(ImageDrawSpec(name, pos, geom));
494 if (parts.size() == 2) {
495 std::vector<std::string> v_pos = split(parts[0],',');
496 std::string name = unescape_string(parts[1]);
498 MY_CHECKPOS("image",0);
500 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
501 pos.X += stof(v_pos[0]) * (float) spacing.X;
502 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
504 if(!data->explicit_size)
505 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
506 m_images.push_back(ImageDrawSpec(name, pos));
509 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
512 void GUIFormSpecMenu::parseItemImage(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 = parts[2];
523 MY_CHECKPOS("itemimage",0);
524 MY_CHECKGEOM("itemimage",1);
526 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
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 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
536 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
539 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
542 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
545 std::vector<std::string> parts = split(element,';');
547 if ((parts.size() == 4) ||
548 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
550 std::vector<std::string> v_pos = split(parts[0],',');
551 std::vector<std::string> v_geom = split(parts[1],',');
552 std::string name = parts[2];
553 std::string label = parts[3];
555 MY_CHECKPOS("button",0);
556 MY_CHECKGEOM("button",1);
559 pos.X += stof(v_pos[0]) * (float)spacing.X;
560 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
563 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
564 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
566 core::rect<s32> rect =
567 core::rect<s32>(pos.X, pos.Y - m_btn_height,
568 pos.X + geom.X, pos.Y + m_btn_height);
570 if(!data->explicit_size)
571 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
573 label = unescape_string(label);
575 std::wstring wlabel = narrow_to_wide(label);
578 narrow_to_wide(name),
583 spec.ftype = f_Button;
584 if(type == "button_exit")
586 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
587 spec.flabel.c_str());
589 if (spec.fname == data->focused_fieldname) {
590 Environment->setFocus(e);
593 m_fields.push_back(spec);
596 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
599 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
601 std::vector<std::string> parts = split(element,';');
603 if (((parts.size() == 3) || (parts.size() == 4)) ||
604 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
606 std::vector<std::string> v_pos = split(parts[0],',');
607 std::vector<std::string> v_geom = split(parts[1],',');
608 std::string name = unescape_string(parts[2]);
610 MY_CHECKPOS("background",0);
611 MY_CHECKGEOM("background",1);
613 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
614 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
615 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
618 geom.X = stof(v_geom[0]) * (float)spacing.X;
619 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
621 if (parts.size() == 4) {
622 m_clipbackground = is_yes(parts[3]);
623 if (m_clipbackground) {
624 pos.X = stoi(v_pos[0]); //acts as offset
625 pos.Y = stoi(v_pos[1]); //acts as offset
629 if(!data->explicit_size)
630 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
631 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
634 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
637 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
639 std::vector<std::string> parts = split(element,';');
641 data->table_options.clear();
642 for (size_t i = 0; i < parts.size(); ++i) {
643 // Parse table option
644 std::string opt = unescape_string(parts[i]);
645 data->table_options.push_back(GUITable::splitOption(opt));
649 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
651 std::vector<std::string> parts = split(element,';');
653 data->table_columns.clear();
654 for (size_t i = 0; i < parts.size(); ++i) {
655 std::vector<std::string> col_parts = split(parts[i],',');
656 GUITable::TableColumn column;
658 if (!col_parts.empty())
659 column.type = col_parts[0];
660 // Parse column options
661 for (size_t j = 1; j < col_parts.size(); ++j) {
662 std::string opt = unescape_string(col_parts[j]);
663 column.options.push_back(GUITable::splitOption(opt));
665 data->table_columns.push_back(column);
669 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
671 std::vector<std::string> parts = split(element,';');
673 if (((parts.size() == 4) || (parts.size() == 5)) ||
674 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
676 std::vector<std::string> v_pos = split(parts[0],',');
677 std::vector<std::string> v_geom = split(parts[1],',');
678 std::string name = parts[2];
679 std::vector<std::string> items = split(parts[3],',');
680 std::string str_initial_selection = "";
681 std::string str_transparent = "false";
683 if (parts.size() >= 5)
684 str_initial_selection = parts[4];
686 MY_CHECKPOS("table",0);
687 MY_CHECKGEOM("table",1);
690 pos.X += stof(v_pos[0]) * (float)spacing.X;
691 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
694 geom.X = stof(v_geom[0]) * (float)spacing.X;
695 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
698 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
700 std::wstring fname_w = narrow_to_wide(name);
709 spec.ftype = f_Table;
711 for (unsigned int i = 0; i < items.size(); ++i) {
712 items[i] = unescape_string(items[i]);
715 //now really show table
716 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
719 if (spec.fname == data->focused_fieldname) {
720 Environment->setFocus(e);
723 e->setTable(data->table_options, data->table_columns, items);
725 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
726 e->setDynamicData(data->table_dyndata[fname_w]);
729 if ((str_initial_selection != "") &&
730 (str_initial_selection != "0"))
731 e->setSelected(stoi(str_initial_selection.c_str()));
733 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
734 m_fields.push_back(spec);
737 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
740 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
742 std::vector<std::string> parts = split(element,';');
744 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
745 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
747 std::vector<std::string> v_pos = split(parts[0],',');
748 std::vector<std::string> v_geom = split(parts[1],',');
749 std::string name = parts[2];
750 std::vector<std::string> items = split(parts[3],',');
751 std::string str_initial_selection = "";
752 std::string str_transparent = "false";
754 if (parts.size() >= 5)
755 str_initial_selection = parts[4];
757 if (parts.size() >= 6)
758 str_transparent = parts[5];
760 MY_CHECKPOS("textlist",0);
761 MY_CHECKGEOM("textlist",1);
764 pos.X += stof(v_pos[0]) * (float)spacing.X;
765 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
768 geom.X = stof(v_geom[0]) * (float)spacing.X;
769 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
772 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
774 std::wstring fname_w = narrow_to_wide(name);
783 spec.ftype = f_Table;
785 for (unsigned int i = 0; i < items.size(); ++i) {
786 items[i] = unescape_string(items[i]);
789 //now really show list
790 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
793 if (spec.fname == data->focused_fieldname) {
794 Environment->setFocus(e);
797 e->setTextList(items, is_yes(str_transparent));
799 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
800 e->setDynamicData(data->table_dyndata[fname_w]);
803 if ((str_initial_selection != "") &&
804 (str_initial_selection != "0"))
805 e->setSelected(stoi(str_initial_selection.c_str()));
807 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
808 m_fields.push_back(spec);
811 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
815 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
817 std::vector<std::string> parts = split(element,';');
819 if ((parts.size() == 5) ||
820 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
822 std::vector<std::string> v_pos = split(parts[0],',');
823 std::string name = parts[2];
824 std::vector<std::string> items = split(parts[3],',');
825 std::string str_initial_selection = "";
826 str_initial_selection = parts[4];
828 MY_CHECKPOS("dropdown",0);
831 pos.X += stof(v_pos[0]) * (float)spacing.X;
832 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
834 s32 width = stof(parts[1]) * (float)spacing.Y;
836 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
837 pos.X + width, pos.Y + (m_btn_height * 2));
839 std::wstring fname_w = narrow_to_wide(name);
848 spec.ftype = f_DropDown;
851 //now really show list
852 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
854 if (spec.fname == data->focused_fieldname) {
855 Environment->setFocus(e);
858 for (unsigned int i=0; i < items.size(); i++) {
859 e->addItem(narrow_to_wide(items[i]).c_str());
862 if (str_initial_selection != "")
863 e->setSelected(stoi(str_initial_selection.c_str())-1);
865 m_fields.push_back(spec);
868 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
869 << element << "'" << std::endl;
872 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
874 std::vector<std::string> parts = split(element,';');
876 if ((parts.size() == 4) ||
877 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
879 std::vector<std::string> v_pos = split(parts[0],',');
880 std::vector<std::string> v_geom = split(parts[1],',');
881 std::string name = parts[2];
882 std::string label = parts[3];
884 MY_CHECKPOS("pwdfield",0);
885 MY_CHECKGEOM("pwdfield",1);
888 pos.X += stof(v_pos[0]) * (float)spacing.X;
889 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
892 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
894 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
895 pos.Y -= m_btn_height;
896 geom.Y = m_btn_height*2;
898 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
900 label = unescape_string(label);
902 std::wstring wlabel = narrow_to_wide(label);
905 narrow_to_wide(name),
912 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
914 if (spec.fname == data->focused_fieldname) {
915 Environment->setFocus(e);
918 if (label.length() >= 1)
920 int font_height = g_fontengine->getTextHeight();
921 rect.UpperLeftCorner.Y -= font_height;
922 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
923 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
926 e->setPasswordBox(true,L'*');
929 evt.EventType = EET_KEY_INPUT_EVENT;
930 evt.KeyInput.Key = KEY_END;
931 evt.KeyInput.Char = 0;
932 evt.KeyInput.Control = 0;
933 evt.KeyInput.Shift = 0;
934 evt.KeyInput.PressedDown = true;
936 m_fields.push_back(spec);
939 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
942 void GUIFormSpecMenu::parseSimpleField(parserData* data,
943 std::vector<std::string> &parts)
945 std::string name = parts[0];
946 std::string label = parts[1];
947 std::string default_val = parts[2];
949 core::rect<s32> rect;
951 if(data->explicit_size)
952 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
954 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
955 pos.Y = ((m_fields.size()+2)*60);
956 v2s32 size = DesiredRect.getSize();
958 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
959 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
963 default_val = m_form_src->resolveText(default_val);
965 default_val = unescape_string(default_val);
966 label = unescape_string(label);
968 std::wstring wlabel = narrow_to_wide(label);
971 narrow_to_wide(name),
973 narrow_to_wide(default_val),
979 // spec field id to 0, this stops submit searching for a value that isn't there
980 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
985 gui::IGUIEditBox *e =
986 Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
988 if (spec.fname == data->focused_fieldname) {
989 Environment->setFocus(e);
993 evt.EventType = EET_KEY_INPUT_EVENT;
994 evt.KeyInput.Key = KEY_END;
995 evt.KeyInput.Char = 0;
996 evt.KeyInput.Control = 0;
997 evt.KeyInput.Shift = 0;
998 evt.KeyInput.PressedDown = true;
1001 if (label.length() >= 1)
1003 int font_height = g_fontengine->getTextHeight();
1004 rect.UpperLeftCorner.Y -= font_height;
1005 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1006 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1010 m_fields.push_back(spec);
1013 void GUIFormSpecMenu::parseTextArea(parserData* data,
1014 std::vector<std::string>& parts,std::string type)
1017 std::vector<std::string> v_pos = split(parts[0],',');
1018 std::vector<std::string> v_geom = split(parts[1],',');
1019 std::string name = parts[2];
1020 std::string label = parts[3];
1021 std::string default_val = parts[4];
1023 MY_CHECKPOS(type,0);
1024 MY_CHECKGEOM(type,1);
1027 pos.X = stof(v_pos[0]) * (float) spacing.X;
1028 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1032 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1034 if (type == "textarea")
1036 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1037 pos.Y += m_btn_height;
1041 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1042 pos.Y -= m_btn_height;
1043 geom.Y = m_btn_height*2;
1046 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1048 if(!data->explicit_size)
1049 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1052 default_val = m_form_src->resolveText(default_val);
1055 default_val = unescape_string(default_val);
1056 label = unescape_string(label);
1058 std::wstring wlabel = narrow_to_wide(label);
1061 narrow_to_wide(name),
1063 narrow_to_wide(default_val),
1069 // spec field id to 0, this stops submit searching for a value that isn't there
1070 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1075 gui::IGUIEditBox *e =
1076 Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1078 if (spec.fname == data->focused_fieldname) {
1079 Environment->setFocus(e);
1082 if (type == "textarea")
1084 e->setMultiLine(true);
1085 e->setWordWrap(true);
1086 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1089 evt.EventType = EET_KEY_INPUT_EVENT;
1090 evt.KeyInput.Key = KEY_END;
1091 evt.KeyInput.Char = 0;
1092 evt.KeyInput.Control = 0;
1093 evt.KeyInput.Shift = 0;
1094 evt.KeyInput.PressedDown = true;
1098 if (label.length() >= 1)
1100 int font_height = g_fontengine->getTextHeight();
1101 rect.UpperLeftCorner.Y -= font_height;
1102 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1103 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1106 m_fields.push_back(spec);
1109 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1112 std::vector<std::string> parts = split(element,';');
1114 if (parts.size() == 3 || parts.size() == 4) {
1115 parseSimpleField(data,parts);
1119 if ((parts.size() == 5) ||
1120 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1122 parseTextArea(data,parts,type);
1125 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1128 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1130 std::vector<std::string> parts = split(element,';');
1132 if ((parts.size() == 2) ||
1133 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1135 std::vector<std::string> v_pos = split(parts[0],',');
1136 std::string text = parts[1];
1138 MY_CHECKPOS("label",0);
1140 v2s32 pos = padding;
1141 pos.X += stof(v_pos[0]) * (float)spacing.X;
1142 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1144 if(!data->explicit_size)
1145 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1147 text = unescape_string(text);
1148 std::vector<std::string> lines = split(text, '\n');
1150 for (unsigned int i = 0; i != lines.size(); i++) {
1151 // Lines are spaced at the nominal distance of
1152 // 2/5 inventory slot, even if the font doesn't
1153 // quite match that. This provides consistent
1154 // form layout, at the expense of sometimes
1155 // having sub-optimal spacing for the font.
1156 // We multiply by 2 and then divide by 5, rather
1157 // than multiply by 0.4, to get exact results
1158 // in the integer cases: 0.4 is not exactly
1159 // representable in binary floating point.
1160 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1161 std::wstring wlabel = narrow_to_wide(lines[i]);
1162 core::rect<s32> rect = core::rect<s32>(
1163 pos.X, posy - m_btn_height,
1164 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1165 posy + m_btn_height);
1172 gui::IGUIStaticText *e =
1173 Environment->addStaticText(spec.flabel.c_str(),
1174 rect, false, false, this, spec.fid);
1175 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1177 m_fields.push_back(spec);
1182 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1185 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1187 std::vector<std::string> parts = split(element,';');
1189 if ((parts.size() == 2) ||
1190 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1192 std::vector<std::string> v_pos = split(parts[0],',');
1193 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1195 MY_CHECKPOS("vertlabel",1);
1197 v2s32 pos = padding;
1198 pos.X += stof(v_pos[0]) * (float)spacing.X;
1199 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1201 core::rect<s32> rect = core::rect<s32>(
1202 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1204 font_line_height(m_font)
1206 +((imgsize.Y/2)- m_btn_height));
1207 //actually text.length() would be correct but adding +1 avoids to break all mods
1209 if(!data->explicit_size)
1210 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1212 std::wstring label = L"";
1214 for (unsigned int i=0; i < text.length(); i++) {
1225 gui::IGUIStaticText *t =
1226 Environment->addStaticText(spec.flabel.c_str(), rect, false, false, this, spec.fid);
1227 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1228 m_fields.push_back(spec);
1231 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1234 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1237 std::vector<std::string> parts = split(element,';');
1239 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1240 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1242 std::vector<std::string> v_pos = split(parts[0],',');
1243 std::vector<std::string> v_geom = split(parts[1],',');
1244 std::string image_name = parts[2];
1245 std::string name = parts[3];
1246 std::string label = parts[4];
1248 MY_CHECKPOS("imagebutton",0);
1249 MY_CHECKGEOM("imagebutton",1);
1251 v2s32 pos = padding;
1252 pos.X += stof(v_pos[0]) * (float)spacing.X;
1253 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1255 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1256 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1258 bool noclip = false;
1259 bool drawborder = true;
1260 std::string pressed_image_name = "";
1262 if (parts.size() >= 7) {
1263 if (parts[5] == "true")
1265 if (parts[6] == "false")
1269 if (parts.size() >= 8) {
1270 pressed_image_name = parts[7];
1273 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1275 if(!data->explicit_size)
1276 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1278 image_name = unescape_string(image_name);
1279 pressed_image_name = unescape_string(pressed_image_name);
1280 label = unescape_string(label);
1282 std::wstring wlabel = narrow_to_wide(label);
1285 narrow_to_wide(name),
1287 narrow_to_wide(image_name),
1290 spec.ftype = f_Button;
1291 if(type == "image_button_exit")
1292 spec.is_exit = true;
1294 video::ITexture *texture = 0;
1295 video::ITexture *pressed_texture = 0;
1296 texture = m_tsrc->getTexture(image_name);
1297 if (pressed_image_name != "")
1298 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1300 pressed_texture = texture;
1302 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1304 if (spec.fname == data->focused_fieldname) {
1305 Environment->setFocus(e);
1308 e->setUseAlphaChannel(true);
1309 e->setImage(guiScalingImageButton(
1310 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1311 e->setPressedImage(guiScalingImageButton(
1312 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1313 e->setScaleImage(true);
1314 e->setNotClipped(noclip);
1315 e->setDrawBorder(drawborder);
1317 m_fields.push_back(spec);
1321 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1324 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1326 std::vector<std::string> parts = split(element,';');
1328 if (((parts.size() == 4) || (parts.size() == 6)) ||
1329 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1331 std::vector<std::string> v_pos = split(parts[0],',');
1332 std::string name = parts[1];
1333 std::vector<std::string> buttons = split(parts[2],',');
1334 std::string str_index = parts[3];
1335 bool show_background = true;
1336 bool show_border = true;
1337 int tab_index = stoi(str_index) -1;
1339 MY_CHECKPOS("tabheader",0);
1341 if (parts.size() == 6) {
1342 if (parts[4] == "true")
1343 show_background = false;
1344 if (parts[5] == "false")
1345 show_border = false;
1349 narrow_to_wide(name),
1355 spec.ftype = f_TabHeader;
1358 pos.X += stof(v_pos[0]) * (float)spacing.X;
1359 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1361 geom.X = DesiredRect.getWidth();
1362 geom.Y = m_btn_height*2;
1364 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1367 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1368 show_background, show_border, spec.fid);
1369 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1370 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1371 e->setTabHeight(m_btn_height*2);
1373 if (spec.fname == data->focused_fieldname) {
1374 Environment->setFocus(e);
1377 e->setNotClipped(true);
1379 for (unsigned int i=0; i< buttons.size(); i++) {
1380 e->addTab(narrow_to_wide(buttons[i]).c_str(), -1);
1383 if ((tab_index >= 0) &&
1384 (buttons.size() < INT_MAX) &&
1385 (tab_index < (int) buttons.size()))
1386 e->setActiveTab(tab_index);
1388 m_fields.push_back(spec);
1391 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1392 << element << "'" << std::endl;
1395 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1398 if (m_gamedef == 0) {
1400 "WARNING: invalid use of item_image_button with m_gamedef==0"
1405 std::vector<std::string> parts = split(element,';');
1407 if ((parts.size() == 5) ||
1408 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1410 std::vector<std::string> v_pos = split(parts[0],',');
1411 std::vector<std::string> v_geom = split(parts[1],',');
1412 std::string item_name = parts[2];
1413 std::string name = parts[3];
1414 std::string label = parts[4];
1416 MY_CHECKPOS("itemimagebutton",0);
1417 MY_CHECKGEOM("itemimagebutton",1);
1419 v2s32 pos = padding;
1420 pos.X += stof(v_pos[0]) * (float)spacing.X;
1421 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1423 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1424 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1426 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1428 if(!data->explicit_size)
1429 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1431 IItemDefManager *idef = m_gamedef->idef();
1433 item.deSerialize(item_name, idef);
1434 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1436 m_tooltips[narrow_to_wide(name)] =
1437 TooltipSpec(item.getDefinition(idef).description,
1438 m_default_tooltip_bgcolor,
1439 m_default_tooltip_color);
1441 label = unescape_string(label);
1443 narrow_to_wide(name),
1444 narrow_to_wide(label),
1445 narrow_to_wide(item_name),
1449 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1451 if (spec.fname == data->focused_fieldname) {
1452 Environment->setFocus(e);
1455 e->setUseAlphaChannel(true);
1456 e->setImage(guiScalingImageButton(Environment->getVideoDriver(), texture, geom.X, geom.Y));
1457 e->setPressedImage(guiScalingImageButton(Environment->getVideoDriver(), texture, geom.X, geom.Y));
1458 e->setScaleImage(true);
1459 spec.ftype = f_Button;
1460 rect+=data->basepos-padding;
1462 m_fields.push_back(spec);
1465 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1468 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1470 std::vector<std::string> parts = split(element,';');
1472 if ((parts.size() == 3) ||
1473 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1475 std::vector<std::string> v_pos = split(parts[0],',');
1476 std::vector<std::string> v_geom = split(parts[1],',');
1478 MY_CHECKPOS("box",0);
1479 MY_CHECKGEOM("box",1);
1481 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1482 pos.X += stof(v_pos[0]) * (float) spacing.X;
1483 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1486 geom.X = stof(v_geom[0]) * (float)spacing.X;
1487 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1489 video::SColor tmp_color;
1491 if (parseColorString(parts[2], tmp_color, false)) {
1492 BoxDrawSpec spec(pos, geom, tmp_color);
1494 m_boxes.push_back(spec);
1497 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1501 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1504 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1506 std::vector<std::string> parts = split(element,';');
1508 if (((parts.size() == 1) || (parts.size() == 2)) ||
1509 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1511 parseColorString(parts[0],m_bgcolor,false);
1513 if (parts.size() == 2) {
1514 std::string fullscreen = parts[1];
1515 m_bgfullscreen = is_yes(fullscreen);
1519 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1522 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1524 std::vector<std::string> parts = split(element,';');
1526 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1527 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1529 parseColorString(parts[0], m_slotbg_n, false);
1530 parseColorString(parts[1], m_slotbg_h, false);
1532 if (parts.size() >= 3) {
1533 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1534 m_slotborder = true;
1537 if (parts.size() == 5) {
1538 video::SColor tmp_color;
1540 if (parseColorString(parts[3], tmp_color, false))
1541 m_default_tooltip_bgcolor = tmp_color;
1542 if (parseColorString(parts[4], tmp_color, false))
1543 m_default_tooltip_color = tmp_color;
1547 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1550 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1552 std::vector<std::string> parts = split(element,';');
1553 if (parts.size() == 2) {
1554 std::string name = parts[0];
1555 m_tooltips[narrow_to_wide(name)] = TooltipSpec(unescape_string(parts[1]),
1556 m_default_tooltip_bgcolor, m_default_tooltip_color);
1558 } else if (parts.size() == 4) {
1559 std::string name = parts[0];
1560 video::SColor tmp_color1, tmp_color2;
1561 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1562 m_tooltips[narrow_to_wide(name)] = TooltipSpec(unescape_string(parts[1]),
1563 tmp_color1, tmp_color2);
1567 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1570 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1576 std::vector<std::string> parts = split(data,'[');
1578 if (parts.size() < 2) {
1582 if (parts[0] != "formspec_version") {
1586 if (is_number(parts[1])) {
1587 m_formspec_version = mystoi(parts[1]);
1594 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1599 std::vector<std::string> parts = split(element,'[');
1601 if (parts.size() < 2)
1604 std::string type = trim(parts[0]);
1605 std::string description = trim(parts[1]);
1607 if (type != "size" && type != "invsize")
1610 if (type == "invsize")
1611 log_deprecated("Deprecated formspec element \"invsize\" is used");
1613 parseSize(data, description);
1618 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1624 std::vector<std::string> parts = split(element,'[');
1626 // ugly workaround to keep compatibility
1627 if (parts.size() > 2) {
1628 if (trim(parts[0]) == "image") {
1629 for (unsigned int i=2;i< parts.size(); i++) {
1630 parts[1] += "[" + parts[i];
1636 if (parts.size() < 2) {
1640 std::string type = trim(parts[0]);
1641 std::string description = trim(parts[1]);
1643 if (type == "list") {
1644 parseList(data,description);
1648 if (type == "checkbox") {
1649 parseCheckbox(data,description);
1653 if (type == "image") {
1654 parseImage(data,description);
1658 if (type == "item_image") {
1659 parseItemImage(data,description);
1663 if ((type == "button") || (type == "button_exit")) {
1664 parseButton(data,description,type);
1668 if (type == "background") {
1669 parseBackground(data,description);
1673 if (type == "tableoptions"){
1674 parseTableOptions(data,description);
1678 if (type == "tablecolumns"){
1679 parseTableColumns(data,description);
1683 if (type == "table"){
1684 parseTable(data,description);
1688 if (type == "textlist"){
1689 parseTextList(data,description);
1693 if (type == "dropdown"){
1694 parseDropDown(data,description);
1698 if (type == "pwdfield") {
1699 parsePwdField(data,description);
1703 if ((type == "field") || (type == "textarea")){
1704 parseField(data,description,type);
1708 if (type == "label") {
1709 parseLabel(data,description);
1713 if (type == "vertlabel") {
1714 parseVertLabel(data,description);
1718 if (type == "item_image_button") {
1719 parseItemImageButton(data,description);
1723 if ((type == "image_button") || (type == "image_button_exit")) {
1724 parseImageButton(data,description,type);
1728 if (type == "tabheader") {
1729 parseTabHeader(data,description);
1733 if (type == "box") {
1734 parseBox(data,description);
1738 if (type == "bgcolor") {
1739 parseBackgroundColor(data,description);
1743 if (type == "listcolors") {
1744 parseListColors(data,description);
1748 if (type == "tooltip") {
1749 parseTooltip(data,description);
1753 if (type == "scrollbar") {
1754 parseScrollBar(data, description);
1760 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1764 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1766 /* useless to regenerate without a screensize */
1767 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1774 for (u32 i = 0; i < m_tables.size(); ++i) {
1775 std::wstring tablename = m_tables[i].first.fname;
1776 GUITable *table = m_tables[i].second;
1777 mydata.table_dyndata[tablename] = table->getDynamicData();
1781 if (!m_focused_element.empty())
1782 mydata.focused_fieldname = m_focused_element;
1785 gui::IGUIElement *focused_element = Environment->getFocus();
1786 if (focused_element && focused_element->getParent() == this) {
1787 s32 focused_id = focused_element->getID();
1788 if (focused_id > 257) {
1789 for (u32 i=0; i<m_fields.size(); i++) {
1790 if (m_fields[i].fid == focused_id) {
1791 mydata.focused_fieldname =
1802 for (u32 i = 0; i < m_tables.size(); ++i) {
1803 GUITable *table = m_tables[i].second;
1807 mydata.size= v2s32(100,100);
1808 mydata.screensize = screensize;
1810 // Base position of contents of form
1811 mydata.basepos = getBasePos();
1813 /* Convert m_init_draw_spec to m_inventorylists */
1815 m_inventorylists.clear();
1817 m_backgrounds.clear();
1818 m_itemimages.clear();
1820 m_checkboxes.clear();
1821 m_scrollbars.clear();
1826 // Set default values (fits old formspec values)
1827 m_bgcolor = video::SColor(140,0,0,0);
1828 m_bgfullscreen = false;
1830 m_slotbg_n = video::SColor(255,128,128,128);
1831 m_slotbg_h = video::SColor(255,192,192,192);
1833 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1834 m_default_tooltip_color = video::SColor(255,255,255,255);
1836 m_slotbordercolor = video::SColor(200,0,0,0);
1837 m_slotborder = false;
1839 m_clipbackground = false;
1842 assert(m_tooltip_element == NULL);
1843 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1844 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1845 m_tooltip_element->enableOverrideColor(true);
1846 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1847 m_tooltip_element->setDrawBackground(true);
1848 m_tooltip_element->setDrawBorder(true);
1849 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1850 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1851 m_tooltip_element->setWordWrap(false);
1852 //we're not parent so no autograb for this one!
1853 m_tooltip_element->grab();
1856 std::vector<std::string> elements = split(m_formspec_string,']');
1859 /* try to read version from first element only */
1860 if (elements.size() >= 1) {
1861 if ( parseVersionDirect(elements[0]) ) {
1866 /* we need size first in order to calculate image scale */
1867 mydata.explicit_size = false;
1868 for (; i< elements.size(); i++) {
1869 if (!parseSizeDirect(&mydata, elements[i])) {
1874 if (mydata.explicit_size) {
1875 // compute scaling for specified form size
1877 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1878 v2u32 delta = current_screensize - m_lockscreensize;
1880 if (current_screensize.Y > m_lockscreensize.Y)
1885 if (current_screensize.X > m_lockscreensize.X)
1890 offset = v2s32(delta.X,delta.Y);
1892 mydata.screensize = m_lockscreensize;
1894 offset = v2s32(0,0);
1897 double gui_scaling = g_settings->getFloat("gui_scaling");
1898 double screen_dpi = porting::getDisplayDensity() * 96;
1902 // In fixed-size mode, inventory image size
1903 // is 0.53 inch multiplied by the gui_scaling
1904 // config parameter. This magic size is chosen
1905 // to make the main menu (15.5 inventory images
1906 // wide, including border) just fit into the
1907 // default window (800 pixels wide) at 96 DPI
1908 // and default scaling (1.00).
1909 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1911 // In variable-size mode, we prefer to make the
1912 // inventory image size 1/15 of screen height,
1913 // multiplied by the gui_scaling config parameter.
1914 // If the preferred size won't fit the whole
1915 // form on the screen, either horizontally or
1916 // vertically, then we scale it down to fit.
1917 // (The magic numbers in the computation of what
1918 // fits arise from the scaling factors in the
1919 // following stanza, including the form border,
1920 // help text space, and 0.1 inventory slot spare.)
1921 // However, a minimum size is also set, that
1922 // the image size can't be less than 0.3 inch
1923 // multiplied by gui_scaling, even if this means
1924 // the form doesn't fit the screen.
1925 double prefer_imgsize = mydata.screensize.Y / 15 *
1927 double fitx_imgsize = mydata.screensize.X /
1928 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1929 double fity_imgsize = mydata.screensize.Y /
1930 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1931 double screen_dpi = porting::getDisplayDensity() * 96;
1932 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1933 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1934 MYMIN(fitx_imgsize, fity_imgsize)));
1937 // Everything else is scaled in proportion to the
1938 // inventory image size. The inventory slot spacing
1939 // is 5/4 image size horizontally and 15/13 image size
1940 // vertically. The padding around the form (incorporating
1941 // the border of the outer inventory slots) is 3/8
1942 // image size. Font height (baseline to baseline)
1943 // is 2/5 vertical inventory slot spacing, and button
1944 // half-height is 7/8 of font height.
1945 imgsize = v2s32(use_imgsize, use_imgsize);
1946 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
1947 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
1948 m_btn_height = use_imgsize*15.0/13 * 0.35;
1950 m_font = g_fontengine->getFont();
1952 mydata.size = v2s32(
1953 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
1954 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
1956 DesiredRect = mydata.rect = core::rect<s32>(
1957 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
1958 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
1959 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
1960 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
1963 // Non-size[] form must consist only of text fields and
1964 // implicit "Proceed" button. Use default font, and
1965 // temporary form size which will be recalculated below.
1966 m_font = g_fontengine->getFont();
1967 m_btn_height = font_line_height(m_font) * 0.875;
1968 DesiredRect = core::rect<s32>(
1969 mydata.screensize.X/2 - 580/2,
1970 mydata.screensize.Y/2 - 300/2,
1971 mydata.screensize.X/2 + 580/2,
1972 mydata.screensize.Y/2 + 300/2
1975 recalculateAbsolutePosition(false);
1976 mydata.basepos = getBasePos();
1977 m_tooltip_element->setOverrideFont(m_font);
1979 gui::IGUISkin* skin = Environment->getSkin();
1980 sanity_check(skin != NULL);
1981 gui::IGUIFont *old_font = skin->getFont();
1982 skin->setFont(m_font);
1984 for (; i< elements.size(); i++) {
1985 parseElement(&mydata, elements[i]);
1988 // If there are fields without explicit size[], add a "Proceed"
1989 // button and adjust size to fit all the fields.
1990 if (m_fields.size() && !mydata.explicit_size) {
1991 mydata.rect = core::rect<s32>(
1992 mydata.screensize.X/2 - 580/2,
1993 mydata.screensize.Y/2 - 300/2,
1994 mydata.screensize.X/2 + 580/2,
1995 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1997 DesiredRect = mydata.rect;
1998 recalculateAbsolutePosition(false);
1999 mydata.basepos = getBasePos();
2002 v2s32 pos = mydata.basepos;
2003 pos.Y = ((m_fields.size()+2)*60);
2005 v2s32 size = DesiredRect.getSize();
2007 core::rect<s32>(size.X/2-70, pos.Y,
2008 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2009 const wchar_t *text = wgettext("Proceed");
2010 Environment->addButton(mydata.rect, this, 257, text);
2016 //set initial focus if parser didn't set it
2017 focused_element = Environment->getFocus();
2018 if (!focused_element
2019 || !isMyChild(focused_element)
2020 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2023 skin->setFont(old_font);
2027 bool GUIFormSpecMenu::getAndroidUIInput()
2029 /* no dialog shown */
2030 if (m_JavaDialogFieldName == L"") {
2035 if (porting::getInputDialogState() == -1) {
2039 std::wstring fieldname = m_JavaDialogFieldName;
2040 m_JavaDialogFieldName = L"";
2042 /* no value abort dialog processing */
2043 if (porting::getInputDialogState() != 0) {
2047 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2048 iter != m_fields.end(); iter++) {
2050 if (iter->fname != fieldname) {
2053 IGUIElement* tochange = getElementFromId(iter->fid);
2055 if (tochange == 0) {
2059 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2063 std::string text = porting::getInputDialogValue();
2065 ((gui::IGUIEditBox*) tochange)->
2066 setText(narrow_to_wide(text).c_str());
2072 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2074 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2076 for(u32 i=0; i<m_inventorylists.size(); i++)
2078 const ListDrawSpec &s = m_inventorylists[i];
2080 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2081 s32 item_i = i + s.start_item_i;
2082 s32 x = (i%s.geom.X) * spacing.X;
2083 s32 y = (i/s.geom.X) * spacing.Y;
2085 core::rect<s32> rect = imgrect + s.pos + p0;
2086 if(rect.isPointInside(p))
2088 return ItemSpec(s.inventoryloc, s.listname, item_i);
2093 return ItemSpec(InventoryLocation(), "", -1);
2096 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
2098 video::IVideoDriver* driver = Environment->getVideoDriver();
2100 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2102 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2103 <<"The inventory location "
2104 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2108 InventoryList *ilist = inv->getList(s.listname);
2110 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2111 <<"The inventory list \""<<s.listname<<"\" @ \""
2112 <<s.inventoryloc.dump()<<"\" doesn't exist"
2117 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2119 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2121 s32 item_i = i + s.start_item_i;
2122 if(item_i >= (s32) ilist->getSize())
2124 s32 x = (i%s.geom.X) * spacing.X;
2125 s32 y = (i/s.geom.X) * spacing.Y;
2127 core::rect<s32> rect = imgrect + s.pos + p;
2130 item = ilist->getItem(item_i);
2132 bool selected = m_selected_item
2133 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2134 && m_selected_item->listname == s.listname
2135 && m_selected_item->i == item_i;
2136 bool hovering = rect.isPointInside(m_pointer);
2141 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2143 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2146 //Draw inv slot borders
2148 s32 x1 = rect.UpperLeftCorner.X;
2149 s32 y1 = rect.UpperLeftCorner.Y;
2150 s32 x2 = rect.LowerRightCorner.X;
2151 s32 y2 = rect.LowerRightCorner.Y;
2153 driver->draw2DRectangle(m_slotbordercolor,
2154 core::rect<s32>(v2s32(x1 - border, y1 - border),
2155 v2s32(x2 + border, y1)), NULL);
2156 driver->draw2DRectangle(m_slotbordercolor,
2157 core::rect<s32>(v2s32(x1 - border, y2),
2158 v2s32(x2 + border, y2 + border)), NULL);
2159 driver->draw2DRectangle(m_slotbordercolor,
2160 core::rect<s32>(v2s32(x1 - border, y1),
2161 v2s32(x1, y2)), NULL);
2162 driver->draw2DRectangle(m_slotbordercolor,
2163 core::rect<s32>(v2s32(x2, y1),
2164 v2s32(x2 + border, y2)), NULL);
2172 item.takeItem(m_selected_amount);
2176 drawItemStack(driver, m_font, item,
2177 rect, &AbsoluteClippingRect, m_gamedef);
2181 std::string tooltip_text = "";
2182 if (hovering && !m_selected_item)
2183 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
2184 if (tooltip_text != "") {
2185 std::vector<std::string> tt_rows = str_split(tooltip_text, '\n');
2186 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2187 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2188 m_tooltip_element->setVisible(true);
2189 this->bringToFront(m_tooltip_element);
2190 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
2191 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2192 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2193 v2u32 screenSize = driver->getScreenSize();
2194 int tooltip_offset_x = m_btn_height;
2195 int tooltip_offset_y = m_btn_height;
2197 tooltip_offset_x *= 3;
2198 tooltip_offset_y = 0;
2199 if (m_pointer.X > (s32)screenSize.X / 2)
2200 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2202 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2203 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2204 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2205 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2206 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2207 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2208 m_tooltip_element->setRelativePosition(core::rect<s32>(
2209 core::position2d<s32>(tooltip_x, tooltip_y),
2210 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2216 void GUIFormSpecMenu::drawSelectedItem()
2218 if(!m_selected_item)
2221 video::IVideoDriver* driver = Environment->getVideoDriver();
2223 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2225 InventoryList *list = inv->getList(m_selected_item->listname);
2227 ItemStack stack = list->getItem(m_selected_item->i);
2228 stack.count = m_selected_amount;
2230 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2231 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2232 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef);
2235 void GUIFormSpecMenu::drawMenu()
2238 std::string newform = m_form_src->getForm();
2239 if(newform != m_formspec_string){
2240 m_formspec_string = newform;
2241 regenerateGui(m_screensize_old);
2245 gui::IGUISkin* skin = Environment->getSkin();
2246 sanity_check(skin != NULL);
2247 gui::IGUIFont *old_font = skin->getFont();
2248 skin->setFont(m_font);
2250 updateSelectedItem();
2252 video::IVideoDriver* driver = Environment->getVideoDriver();
2254 v2u32 screenSize = driver->getScreenSize();
2255 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2257 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2259 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2261 m_tooltip_element->setVisible(false);
2266 for(u32 i=0; i<m_backgrounds.size(); i++)
2268 const ImageDrawSpec &spec = m_backgrounds[i];
2269 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2272 // Image size on screen
2273 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2274 // Image rectangle on screen
2275 core::rect<s32> rect = imgrect + spec.pos;
2277 if (m_clipbackground) {
2278 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2279 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2280 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2281 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2282 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2285 const video::SColor color(255,255,255,255);
2286 const video::SColor colors[] = {color,color,color,color};
2287 draw2DImageFilterScaled(driver, texture, rect,
2288 core::rect<s32>(core::position2d<s32>(0,0),
2289 core::dimension2di(texture->getOriginalSize())),
2290 NULL/*&AbsoluteClippingRect*/, colors, true);
2293 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2294 errorstream << "\t" << spec.name << std::endl;
2301 for(u32 i=0; i<m_boxes.size(); i++)
2303 const BoxDrawSpec &spec = m_boxes[i];
2305 irr::video::SColor todraw = spec.color;
2307 todraw.setAlpha(140);
2309 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2310 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2312 driver->draw2DRectangle(todraw, rect, 0);
2317 for(u32 i=0; i<m_images.size(); i++)
2319 const ImageDrawSpec &spec = m_images[i];
2320 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2323 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2324 // Image size on screen
2325 core::rect<s32> imgrect;
2328 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2331 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2333 // Image rectangle on screen
2334 core::rect<s32> rect = imgrect + spec.pos;
2335 const video::SColor color(255,255,255,255);
2336 const video::SColor colors[] = {color,color,color,color};
2337 draw2DImageFilterScaled(driver, texture, rect,
2338 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2339 NULL/*&AbsoluteClippingRect*/, colors, true);
2342 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2343 errorstream << "\t" << spec.name << std::endl;
2350 for(u32 i=0; i<m_itemimages.size(); i++)
2355 const ImageDrawSpec &spec = m_itemimages[i];
2356 IItemDefManager *idef = m_gamedef->idef();
2358 item.deSerialize(spec.name, idef);
2359 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
2360 // Image size on screen
2361 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2362 // Image rectangle on screen
2363 core::rect<s32> rect = imgrect + spec.pos;
2364 const video::SColor color(255,255,255,255);
2365 const video::SColor colors[] = {color,color,color,color};
2366 draw2DImageFilterScaled(driver, texture, rect,
2367 core::rect<s32>(core::position2d<s32>(0,0),
2368 core::dimension2di(texture->getOriginalSize())),
2369 NULL/*&AbsoluteClippingRect*/, colors, true);
2374 Phase 0: Item slot rectangles
2375 Phase 1: Item images; prepare tooltip
2378 for(int phase=start_phase; phase<=1; phase++)
2379 for(u32 i=0; i<m_inventorylists.size(); i++)
2381 drawList(m_inventorylists[i], phase);
2387 gui::IGUIElement::draw();
2389 /* TODO find way to show tooltips on touchscreen */
2390 #ifndef HAVE_TOUCHSCREENGUI
2391 m_pointer = m_device->getCursorControl()->getPosition();
2395 Draw fields/buttons tooltips
2397 gui::IGUIElement *hovered =
2398 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2400 if (hovered != NULL) {
2401 s32 id = hovered->getID();
2405 m_old_tooltip_id = id;
2408 if (id == m_old_tooltip_id) {
2409 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2411 m_hovered_time = getTimeMs();
2412 m_old_tooltip_id = id;
2416 if (id != -1 && delta >= m_tooltip_show_delay) {
2417 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2418 iter != m_fields.end(); iter++) {
2419 if ( (iter->fid == id) && (m_tooltips[iter->fname].tooltip != "") ){
2420 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2421 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2422 m_tooltip_element->setText(narrow_to_wide(m_tooltips[iter->fname].tooltip).c_str());
2423 std::vector<std::string> tt_rows = str_split(m_tooltips[iter->fname].tooltip, '\n');
2424 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2425 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2426 int tooltip_offset_x = m_btn_height;
2427 int tooltip_offset_y = m_btn_height;
2429 tooltip_offset_x *= 3;
2430 tooltip_offset_y = 0;
2431 if (m_pointer.X > (s32)screenSize.X / 2)
2432 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2434 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2435 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2436 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2437 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2438 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2439 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2440 m_tooltip_element->setRelativePosition(core::rect<s32>(
2441 core::position2d<s32>(tooltip_x, tooltip_y),
2442 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2444 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2445 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2446 m_tooltip_element->setVisible(true);
2447 this->bringToFront(m_tooltip_element);
2455 Draw dragged item stack
2459 skin->setFont(old_font);
2462 void GUIFormSpecMenu::updateSelectedItem()
2464 // If the selected stack has become empty for some reason, deselect it.
2465 // If the selected stack has become inaccessible, deselect it.
2466 // If the selected stack has become smaller, adjust m_selected_amount.
2467 ItemStack selected = verifySelectedItem();
2469 // WARNING: BLACK MAGIC
2470 // See if there is a stack suited for our current guess.
2471 // If such stack does not exist, clear the guess.
2472 if(m_selected_content_guess.name != "" &&
2473 selected.name == m_selected_content_guess.name &&
2474 selected.count == m_selected_content_guess.count){
2475 // Selected item fits the guess. Skip the black magic.
2477 else if(m_selected_content_guess.name != ""){
2479 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2480 const ListDrawSpec &s = m_inventorylists[i];
2481 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2484 InventoryList *list = inv->getList(s.listname);
2487 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2488 u32 item_i = i + s.start_item_i;
2489 if(item_i >= list->getSize())
2491 ItemStack stack = list->getItem(item_i);
2492 if(stack.name == m_selected_content_guess.name &&
2493 stack.count == m_selected_content_guess.count){
2495 infostream<<"Client: Changing selected content guess to "
2496 <<s.inventoryloc.dump()<<" "<<s.listname
2497 <<" "<<item_i<<std::endl;
2498 delete m_selected_item;
2499 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2500 m_selected_amount = stack.count;
2505 infostream<<"Client: Discarding selected content guess: "
2506 <<m_selected_content_guess.getItemString()<<std::endl;
2507 m_selected_content_guess.name = "";
2511 // If craftresult is nonempty and nothing else is selected, select it now.
2512 if(!m_selected_item)
2514 for(u32 i=0; i<m_inventorylists.size(); i++)
2516 const ListDrawSpec &s = m_inventorylists[i];
2517 if(s.listname == "craftpreview")
2519 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2520 InventoryList *list = inv->getList("craftresult");
2521 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2523 m_selected_item = new ItemSpec;
2524 m_selected_item->inventoryloc = s.inventoryloc;
2525 m_selected_item->listname = "craftresult";
2526 m_selected_item->i = 0;
2527 m_selected_amount = 0;
2528 m_selected_dragging = false;
2535 // If craftresult is selected, keep the whole stack selected
2536 if(m_selected_item && m_selected_item->listname == "craftresult")
2538 m_selected_amount = verifySelectedItem().count;
2542 ItemStack GUIFormSpecMenu::verifySelectedItem()
2544 // If the selected stack has become empty for some reason, deselect it.
2545 // If the selected stack has become inaccessible, deselect it.
2546 // If the selected stack has become smaller, adjust m_selected_amount.
2547 // Return the selected stack.
2551 if(m_selected_item->isValid())
2553 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2556 InventoryList *list = inv->getList(m_selected_item->listname);
2557 if(list && (u32) m_selected_item->i < list->getSize())
2559 ItemStack stack = list->getItem(m_selected_item->i);
2560 if(m_selected_amount > stack.count)
2561 m_selected_amount = stack.count;
2568 // selection was not valid
2569 delete m_selected_item;
2570 m_selected_item = NULL;
2571 m_selected_amount = 0;
2572 m_selected_dragging = false;
2577 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2583 if (quitmode == quit_mode_accept) {
2584 fields["quit"] = "true";
2587 if (quitmode == quit_mode_cancel) {
2588 fields["quit"] = "true";
2589 m_text_dst->gotText(fields);
2593 if (current_keys_pending.key_down) {
2594 fields["key_down"] = "true";
2595 current_keys_pending.key_down = false;
2598 if (current_keys_pending.key_up) {
2599 fields["key_up"] = "true";
2600 current_keys_pending.key_up = false;
2603 if (current_keys_pending.key_enter) {
2604 fields["key_enter"] = "true";
2605 current_keys_pending.key_enter = false;
2608 if (current_keys_pending.key_escape) {
2609 fields["key_escape"] = "true";
2610 current_keys_pending.key_escape = false;
2613 for(unsigned int i=0; i<m_fields.size(); i++) {
2614 const FieldSpec &s = m_fields[i];
2616 std::string name = wide_to_narrow(s.fname);
2617 if(s.ftype == f_Button) {
2618 fields[name] = wide_to_narrow(s.flabel);
2620 else if(s.ftype == f_Table) {
2621 GUITable *table = getTable(s.fname);
2623 fields[name] = table->checkEvent();
2626 else if(s.ftype == f_DropDown) {
2627 // no dynamic cast possible due to some distributions shipped
2628 // without rtti support in irrlicht
2629 IGUIElement * element = getElementFromId(s.fid);
2630 gui::IGUIComboBox *e = NULL;
2631 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2632 e = static_cast<gui::IGUIComboBox*>(element);
2634 s32 selected = e->getSelected();
2635 if (selected >= 0) {
2637 wide_to_narrow(e->getItem(selected));
2640 else if (s.ftype == f_TabHeader) {
2641 // no dynamic cast possible due to some distributions shipped
2642 // without rtti support in irrlicht
2643 IGUIElement * element = getElementFromId(s.fid);
2644 gui::IGUITabControl *e = NULL;
2645 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2646 e = static_cast<gui::IGUITabControl*>(element);
2650 std::stringstream ss;
2651 ss << (e->getActiveTab() +1);
2652 fields[name] = ss.str();
2655 else if (s.ftype == f_CheckBox) {
2656 // no dynamic cast possible due to some distributions shipped
2657 // without rtti support in irrlicht
2658 IGUIElement * element = getElementFromId(s.fid);
2659 gui::IGUICheckBox *e = NULL;
2660 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2661 e = static_cast<gui::IGUICheckBox*>(element);
2666 fields[name] = "true";
2668 fields[name] = "false";
2671 else if (s.ftype == f_ScrollBar) {
2672 // no dynamic cast possible due to some distributions shipped
2673 // without rtti support in irrlicht
2674 IGUIElement * element = getElementFromId(s.fid);
2675 gui::IGUIScrollBar *e = NULL;
2676 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2677 e = static_cast<gui::IGUIScrollBar*>(element);
2681 std::stringstream os;
2683 if (s.fdefault == L"Changed")
2684 fields[name] = "CHG:" + os.str();
2686 fields[name] = "VAL:" + os.str();
2691 IGUIElement* e = getElementFromId(s.fid);
2693 fields[name] = wide_to_narrow(e->getText());
2699 m_text_dst->gotText(fields);
2703 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2705 while(tocheck != NULL) {
2706 if (tocheck == parent) {
2709 tocheck = tocheck->getParent();
2714 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2716 // The IGUITabControl renders visually using the skin's selected
2717 // font, which we override for the duration of form drawing,
2718 // but computes tab hotspots based on how it would have rendered
2719 // using the font that is selected at the time of button release.
2720 // To make these two consistent, temporarily override the skin's
2721 // font while the IGUITabControl is processing the event.
2722 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2723 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2724 s32 x = event.MouseInput.X;
2725 s32 y = event.MouseInput.Y;
2726 gui::IGUIElement *hovered =
2727 Environment->getRootGUIElement()->getElementFromPoint(
2728 core::position2d<s32>(x, y));
2729 if (hovered && isMyChild(hovered) &&
2730 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2731 gui::IGUISkin* skin = Environment->getSkin();
2732 sanity_check(skin != NULL);
2733 gui::IGUIFont *old_font = skin->getFont();
2734 skin->setFont(m_font);
2735 bool retval = hovered->OnEvent(event);
2736 skin->setFont(old_font);
2741 // Fix Esc/Return key being eaten by checkboxen and tables
2742 if(event.EventType==EET_KEY_INPUT_EVENT) {
2743 KeyPress kp(event.KeyInput);
2744 if (kp == EscapeKey || kp == CancelKey
2745 || kp == getKeySetting("keymap_inventory")
2746 || event.KeyInput.Key==KEY_RETURN) {
2747 gui::IGUIElement *focused = Environment->getFocus();
2748 if (focused && isMyChild(focused) &&
2749 (focused->getType() == gui::EGUIET_LIST_BOX ||
2750 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2756 // Mouse wheel events: send to hovered element instead of focused
2757 if(event.EventType==EET_MOUSE_INPUT_EVENT
2758 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2759 s32 x = event.MouseInput.X;
2760 s32 y = event.MouseInput.Y;
2761 gui::IGUIElement *hovered =
2762 Environment->getRootGUIElement()->getElementFromPoint(
2763 core::position2d<s32>(x, y));
2764 if (hovered && isMyChild(hovered)) {
2765 hovered->OnEvent(event);
2770 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2771 s32 x = event.MouseInput.X;
2772 s32 y = event.MouseInput.Y;
2773 gui::IGUIElement *hovered =
2774 Environment->getRootGUIElement()->getElementFromPoint(
2775 core::position2d<s32>(x, y));
2776 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2777 m_old_tooltip_id = -1;
2780 if (!isChild(hovered,this)) {
2781 if (DoubleClickDetection(event)) {
2788 // display software keyboard when clicking edit boxes
2789 if (event.EventType == EET_MOUSE_INPUT_EVENT
2790 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2791 gui::IGUIElement *hovered =
2792 Environment->getRootGUIElement()->getElementFromPoint(
2793 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2794 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2795 bool retval = hovered->OnEvent(event);
2797 Environment->setFocus(hovered);
2799 m_JavaDialogFieldName = getNameByID(hovered->getID());
2800 std::string message = gettext("Enter ");
2801 std::string label = wide_to_narrow(getLabelByID(hovered->getID()));
2805 message += gettext(label) + ":";
2807 /* single line text input */
2810 /* multi line text input */
2811 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2815 /* passwords are always single line */
2816 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2820 porting::showInputDialog(gettext("ok"), "",
2821 wide_to_narrow(((gui::IGUIEditBox*) hovered)->getText()),
2827 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2830 memset(&translated, 0, sizeof(SEvent));
2831 translated.EventType = EET_MOUSE_INPUT_EVENT;
2832 gui::IGUIElement* root = Environment->getRootGUIElement();
2836 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2840 gui::IGUIElement* hovered = root->getElementFromPoint(
2841 core::position2d<s32>(
2843 event.TouchInput.Y));
2845 translated.MouseInput.X = event.TouchInput.X;
2846 translated.MouseInput.Y = event.TouchInput.Y;
2847 translated.MouseInput.Control = false;
2849 bool dont_send_event = false;
2851 if (event.TouchInput.touchedCount == 1) {
2852 switch (event.TouchInput.Event) {
2853 case ETIE_PRESSED_DOWN:
2854 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2855 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2856 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2857 m_down_pos = m_pointer;
2860 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2861 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2862 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2865 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2866 translated.MouseInput.ButtonStates = 0;
2867 hovered = root->getElementFromPoint(m_down_pos);
2868 /* we don't have a valid pointer element use last
2869 * known pointer pos */
2870 translated.MouseInput.X = m_pointer.X;
2871 translated.MouseInput.Y = m_pointer.Y;
2873 /* reset down pos */
2874 m_down_pos = v2s32(0,0);
2877 dont_send_event = true;
2878 //this is not supposed to happen
2880 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2881 << event.TouchInput.Event << std::endl;
2883 } else if ( (event.TouchInput.touchedCount == 2) &&
2884 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2885 hovered = root->getElementFromPoint(m_down_pos);
2887 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2888 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2889 translated.MouseInput.X = m_pointer.X;
2890 translated.MouseInput.Y = m_pointer.Y;
2893 hovered->OnEvent(translated);
2896 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
2897 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2901 hovered->OnEvent(translated);
2903 dont_send_event = true;
2905 /* ignore unhandled 2 touch events ... accidental moving for example */
2906 else if (event.TouchInput.touchedCount == 2) {
2907 dont_send_event = true;
2909 else if (event.TouchInput.touchedCount > 2) {
2911 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
2912 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
2915 if (dont_send_event) {
2919 /* check if translated event needs to be preprocessed again */
2920 if (preprocessEvent(translated)) {
2925 bool retval = hovered->OnEvent(translated);
2927 if (event.TouchInput.Event == ETIE_LEFT_UP) {
2929 m_pointer = v2s32(0,0);
2940 /******************************************************************************/
2941 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
2943 /* The following code is for capturing double-clicks of the mouse button
2944 * and translating the double-click into an EET_KEY_INPUT_EVENT event
2945 * -- which closes the form -- under some circumstances.
2947 * There have been many github issues reporting this as a bug even though it
2948 * was an intended feature. For this reason, remapping the double-click as
2949 * an ESC must be explicitly set when creating this class via the
2950 * /p remap_dbl_click parameter of the constructor.
2953 if (!m_remap_dbl_click)
2956 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2957 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
2958 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
2960 m_doubleclickdetect[1].pos = m_pointer;
2961 m_doubleclickdetect[1].time = getTimeMs();
2963 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2964 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
2969 double squaredistance =
2970 m_doubleclickdetect[0].pos
2971 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
2973 if (squaredistance > (30*30)) {
2977 SEvent* translated = new SEvent();
2978 assert(translated != 0);
2979 //translate doubleclick to escape
2980 memset(translated, 0, sizeof(SEvent));
2981 translated->EventType = irr::EET_KEY_INPUT_EVENT;
2982 translated->KeyInput.Key = KEY_ESCAPE;
2983 translated->KeyInput.Control = false;
2984 translated->KeyInput.Shift = false;
2985 translated->KeyInput.PressedDown = true;
2986 translated->KeyInput.Char = 0;
2987 OnEvent(*translated);
2989 // no need to send the key up event as we're already deleted
2990 // and no one else did notice this event
2998 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3000 if(event.EventType==EET_KEY_INPUT_EVENT) {
3001 KeyPress kp(event.KeyInput);
3002 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3003 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3006 acceptInput(quit_mode_cancel);
3009 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
3012 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3013 (kp == getKeySetting("keymap_screenshot"))) {
3014 m_client->makeScreenshot(m_device);
3016 if (event.KeyInput.PressedDown &&
3017 (event.KeyInput.Key==KEY_RETURN ||
3018 event.KeyInput.Key==KEY_UP ||
3019 event.KeyInput.Key==KEY_DOWN)
3021 switch (event.KeyInput.Key) {
3023 current_keys_pending.key_enter = true;
3026 current_keys_pending.key_up = true;
3029 current_keys_pending.key_down = true;
3033 //can't happen at all!
3034 FATAL_ERROR("Reached a source line that can't ever been reached");
3037 if (current_keys_pending.key_enter && m_allowclose) {
3038 acceptInput(quit_mode_accept);
3048 /* Mouse event other than movement, or crossing the border of inventory
3049 field while holding right mouse button
3051 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3052 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3053 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3054 event.MouseInput.isRightPressed() &&
3055 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3057 // Get selected item and hovered/clicked item (s)
3059 m_old_tooltip_id = -1;
3060 updateSelectedItem();
3061 ItemSpec s = getItemAtPos(m_pointer);
3063 Inventory *inv_selected = NULL;
3064 Inventory *inv_s = NULL;
3066 if(m_selected_item) {
3067 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3068 sanity_check(inv_selected);
3069 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3076 inv_s = m_invmgr->getInventory(s.inventoryloc);
3079 errorstream<<"InventoryMenu: The selected inventory location "
3080 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
3082 s.i = -1; // make it invalid again
3086 InventoryList *list = inv_s->getList(s.listname);
3088 verbosestream<<"InventoryMenu: The selected inventory list \""
3089 <<s.listname<<"\" does not exist"<<std::endl;
3090 s.i = -1; // make it invalid again
3094 if((u32)s.i >= list->getSize()) {
3095 infostream<<"InventoryMenu: The selected inventory list \""
3096 <<s.listname<<"\" is too small (i="<<s.i<<", size="
3097 <<list->getSize()<<")"<<std::endl;
3098 s.i = -1; // make it invalid again
3102 s_count = list->getItem(s.i).count;
3105 bool identical = (m_selected_item != NULL) && s.isValid() &&
3106 (inv_selected == inv_s) &&
3107 (m_selected_item->listname == s.listname) &&
3108 (m_selected_item->i == s.i);
3110 // buttons: 0 = left, 1 = right, 2 = middle
3111 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3114 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3115 { button = 0; updown = 0; }
3116 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3117 { button = 1; updown = 0; }
3118 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3119 { button = 2; updown = 0; }
3120 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3121 { button = 0; updown = 1; }
3122 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3123 { button = 1; updown = 1; }
3124 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3125 { button = 2; updown = 1; }
3126 else if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
3129 // Set this number to a positive value to generate a move action
3130 // from m_selected_item to s.
3131 u32 move_amount = 0;
3133 // Set this number to a positive value to generate a drop action
3134 // from m_selected_item.
3135 u32 drop_amount = 0;
3137 // Set this number to a positive value to generate a craft action at s.
3138 u32 craft_amount = 0;
3141 // Some mouse button has been pressed
3143 //infostream<<"Mouse button "<<button<<" pressed at p=("
3144 // <<p.X<<","<<p.Y<<")"<<std::endl;
3146 m_selected_dragging = false;
3148 if(s.isValid() && s.listname == "craftpreview") {
3149 // Craft preview has been clicked: craft
3150 craft_amount = (button == 2 ? 10 : 1);
3152 else if(m_selected_item == NULL) {
3154 // Non-empty stack has been clicked: select it
3155 m_selected_item = new ItemSpec(s);
3157 if(button == 1) // right
3158 m_selected_amount = (s_count + 1) / 2;
3159 else if(button == 2) // middle
3160 m_selected_amount = MYMIN(s_count, 10);
3162 m_selected_amount = s_count;
3164 m_selected_dragging = true;
3165 m_rmouse_auto_place = false;
3168 else { // m_selected_item != NULL
3169 assert(m_selected_amount >= 1);
3172 // Clicked a slot: move
3173 if(button == 1) // right
3175 else if(button == 2) // middle
3176 move_amount = MYMIN(m_selected_amount, 10);
3178 move_amount = m_selected_amount;
3181 if(move_amount >= m_selected_amount)
3182 m_selected_amount = 0;
3184 m_selected_amount -= move_amount;
3188 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3189 // Clicked outside of the window: drop
3190 if(button == 1) // right
3192 else if(button == 2) // middle
3193 drop_amount = MYMIN(m_selected_amount, 10);
3195 drop_amount = m_selected_amount;
3199 else if(updown == 1) {
3200 // Some mouse button has been released
3202 //infostream<<"Mouse button "<<button<<" released at p=("
3203 // <<p.X<<","<<p.Y<<")"<<std::endl;
3205 if(m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3207 // Dragged to different slot: move all selected
3208 move_amount = m_selected_amount;
3211 else if(m_selected_item != NULL && m_selected_dragging &&
3212 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3213 // Dragged outside of window: drop all selected
3214 drop_amount = m_selected_amount;
3217 m_selected_dragging = false;
3218 // Keep count of how many times right mouse button has been
3219 // clicked. One click is drag without dropping. Click + release
3220 // + click changes to drop one item when moved mode
3221 if(button == 1 && m_selected_item != NULL)
3222 m_rmouse_auto_place = !m_rmouse_auto_place;
3224 else if(updown == -1) {
3225 // Mouse has been moved and rmb is down and mouse pointer just
3226 // entered a new inventory field (checked in the entry-if, this
3227 // is the only action here that is generated by mouse movement)
3228 if(m_selected_item != NULL && s.isValid()){
3230 // TODO: middle mouse to move 10 items might be handy
3231 if (m_rmouse_auto_place) {
3232 // Only move an item if the destination slot is empty
3233 // or contains the same item type as what is going to be
3235 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3236 InventoryList *list_to = inv_s->getList(s.listname);
3237 assert(list_from && list_to);
3238 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3239 ItemStack stack_to = list_to->getItem(s.i);
3240 if (stack_to.empty() || stack_to.name == stack_from.name)
3246 // Possibly send inventory action to server
3249 // Send IACTION_MOVE
3251 assert(m_selected_item && m_selected_item->isValid());
3252 assert(s.isValid());
3254 assert(inv_selected && inv_s);
3255 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3256 InventoryList *list_to = inv_s->getList(s.listname);
3257 assert(list_from && list_to);
3258 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3259 ItemStack stack_to = list_to->getItem(s.i);
3261 // Check how many items can be moved
3262 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3263 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3264 // If source stack cannot be added to destination stack at all,
3266 if ((leftover.count == stack_from.count) &&
3267 (leftover.name == stack_from.name)) {
3268 m_selected_amount = stack_to.count;
3269 // In case the server doesn't directly swap them but instead
3270 // moves stack_to somewhere else, set this
3271 m_selected_content_guess = stack_to;
3272 m_selected_content_guess_inventory = s.inventoryloc;
3274 // Source stack goes fully into destination stack
3275 else if(leftover.empty()) {
3276 m_selected_amount -= move_amount;
3277 m_selected_content_guess = ItemStack(); // Clear
3279 // Source stack goes partly into destination stack
3281 move_amount -= leftover.count;
3282 m_selected_amount -= move_amount;
3283 m_selected_content_guess = ItemStack(); // Clear
3286 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
3287 IMoveAction *a = new IMoveAction();
3288 a->count = move_amount;
3289 a->from_inv = m_selected_item->inventoryloc;
3290 a->from_list = m_selected_item->listname;
3291 a->from_i = m_selected_item->i;
3292 a->to_inv = s.inventoryloc;
3293 a->to_list = s.listname;
3295 m_invmgr->inventoryAction(a);
3297 else if(drop_amount > 0) {
3298 m_selected_content_guess = ItemStack(); // Clear
3300 // Send IACTION_DROP
3302 assert(m_selected_item && m_selected_item->isValid());
3303 assert(inv_selected);
3304 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3306 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3308 // Check how many items can be dropped
3309 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3310 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3311 m_selected_amount -= drop_amount;
3313 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
3314 IDropAction *a = new IDropAction();
3315 a->count = drop_amount;
3316 a->from_inv = m_selected_item->inventoryloc;
3317 a->from_list = m_selected_item->listname;
3318 a->from_i = m_selected_item->i;
3319 m_invmgr->inventoryAction(a);
3321 else if(craft_amount > 0) {
3322 m_selected_content_guess = ItemStack(); // Clear
3324 // Send IACTION_CRAFT
3326 assert(s.isValid());
3329 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
3330 ICraftAction *a = new ICraftAction();
3331 a->count = craft_amount;
3332 a->craft_inv = s.inventoryloc;
3333 m_invmgr->inventoryAction(a);
3336 // If m_selected_amount has been decreased to zero, deselect
3337 if(m_selected_amount == 0) {
3338 delete m_selected_item;
3339 m_selected_item = NULL;
3340 m_selected_amount = 0;
3341 m_selected_dragging = false;
3342 m_selected_content_guess = ItemStack();
3344 m_old_pointer = m_pointer;
3346 if(event.EventType==EET_GUI_EVENT) {
3348 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
3350 // find the element that was clicked
3351 for(unsigned int i=0; i<m_fields.size(); i++) {
3352 FieldSpec &s = m_fields[i];
3353 if ((s.ftype == f_TabHeader) &&
3354 (s.fid == event.GUIEvent.Caller->getID())) {
3362 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
3364 if(!canTakeFocus(event.GUIEvent.Element)) {
3365 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3367 // Returning true disables focus change
3371 if((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3372 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3373 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3374 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3375 unsigned int btn_id = event.GUIEvent.Caller->getID();
3377 if (btn_id == 257) {
3379 acceptInput(quit_mode_accept);
3383 m_text_dst->gotText(narrow_to_wide("ExitButton"));
3385 // quitMenu deallocates menu
3389 // find the element that was clicked
3390 for(u32 i=0; i<m_fields.size(); i++) {
3391 FieldSpec &s = m_fields[i];
3392 // if its a button, set the send field so
3393 // lua knows which button was pressed
3394 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3395 (s.fid == event.GUIEvent.Caller->getID())) {
3399 acceptInput(quit_mode_accept);
3402 m_text_dst->gotText(narrow_to_wide("ExitButton"));
3406 acceptInput(quit_mode_no);
3411 else if ((s.ftype == f_DropDown) &&
3412 (s.fid == event.GUIEvent.Caller->getID())) {
3413 // only send the changed dropdown
3414 for(u32 i=0; i<m_fields.size(); i++) {
3415 FieldSpec &s2 = m_fields[i];
3416 if (s2.ftype == f_DropDown) {
3421 acceptInput(quit_mode_no);
3423 // revert configuration to make sure dropdowns are sent on
3424 // regular button click
3425 for(u32 i=0; i<m_fields.size(); i++) {
3426 FieldSpec &s2 = m_fields[i];
3427 if (s2.ftype == f_DropDown) {
3433 else if ((s.ftype == f_ScrollBar) &&
3434 (s.fid == event.GUIEvent.Caller->getID()))
3436 s.fdefault = L"Changed";
3437 acceptInput(quit_mode_no);
3443 if(event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3444 if(event.GUIEvent.Caller->getID() > 257) {
3447 acceptInput(quit_mode_accept);
3450 current_keys_pending.key_enter = true;
3453 // quitMenu deallocates menu
3458 if(event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3459 int current_id = event.GUIEvent.Caller->getID();
3460 if(current_id > 257) {
3461 // find the element that was clicked
3462 for(u32 i=0; i<m_fields.size(); i++) {
3463 FieldSpec &s = m_fields[i];
3464 // if it's a table, set the send field
3465 // so lua knows which table was changed
3466 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3477 return Parent ? Parent->OnEvent(event) : false;
3481 * get name of element by element id
3482 * @param id of element
3483 * @return name string or empty string
3485 std::wstring GUIFormSpecMenu::getNameByID(s32 id)
3487 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3488 iter != m_fields.end(); iter++) {
3489 if (iter->fid == id) {
3497 * get label of element by id
3498 * @param id of element
3499 * @return label string or empty string
3501 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3503 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3504 iter != m_fields.end(); iter++) {
3505 if (iter->fid == id) {
3506 return iter->flabel;