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 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
56 #include "intlGUIEditBox.h"
59 #define MY_CHECKPOS(a,b) \
60 if (v_pos.size() != 2) { \
61 errorstream<< "Invalid pos for element " << a << "specified: \"" \
62 << parts[b] << "\"" << std::endl; \
66 #define MY_CHECKGEOM(a,b) \
67 if (v_geom.size() != 2) { \
68 errorstream<< "Invalid pos for element " << a << "specified: \"" \
69 << parts[b] << "\"" << std::endl; \
75 static unsigned int font_line_height(gui::IGUIFont *font)
77 return font->getDimension(L"Ay").Height + font->getKerningHeight();
80 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
81 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
82 InventoryManager *invmgr, IGameDef *gamedef,
83 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
84 Client* client, bool remap_dbl_click) :
85 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
91 m_selected_item(NULL),
93 m_selected_dragging(false),
94 m_tooltip_element(NULL),
97 m_rmouse_auto_place(false),
102 m_formspec_version(0),
103 m_focused_element(""),
105 m_remap_dbl_click(remap_dbl_click)
107 , m_JavaDialogFieldName("")
110 current_keys_pending.key_down = false;
111 current_keys_pending.key_up = false;
112 current_keys_pending.key_enter = false;
113 current_keys_pending.key_escape = false;
115 m_doubleclickdetect[0].time = 0;
116 m_doubleclickdetect[1].time = 0;
118 m_doubleclickdetect[0].pos = v2s32(0, 0);
119 m_doubleclickdetect[1].pos = v2s32(0, 0);
121 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
124 GUIFormSpecMenu::~GUIFormSpecMenu()
128 for (u32 i = 0; i < m_tables.size(); ++i) {
129 GUITable *table = m_tables[i].second;
133 delete m_selected_item;
135 if (m_form_src != NULL) {
138 if (m_text_dst != NULL) {
143 void GUIFormSpecMenu::removeChildren()
145 const core::list<gui::IGUIElement*> &children = getChildren();
147 while(!children.empty()) {
148 (*children.getLast())->remove();
151 if(m_tooltip_element) {
152 m_tooltip_element->remove();
153 m_tooltip_element->drop();
154 m_tooltip_element = NULL;
159 void GUIFormSpecMenu::setInitialFocus()
161 // Set initial focus according to following order of precedence:
162 // 1. first empty editbox
166 // 5. first focusable (not statictext, not tabheader)
167 // 6. first child element
169 core::list<gui::IGUIElement*> children = getChildren();
171 // in case "children" contains any NULL elements, remove them
172 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
173 it != children.end();) {
177 it = children.erase(it);
180 // 1. first empty editbox
181 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
182 it != children.end(); ++it) {
183 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
184 && (*it)->getText()[0] == 0) {
185 Environment->setFocus(*it);
191 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
192 it != children.end(); ++it) {
193 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
194 Environment->setFocus(*it);
200 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
201 it != children.end(); ++it) {
202 if ((*it)->getTypeName() == std::string("GUITable")) {
203 Environment->setFocus(*it);
209 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
210 it != children.end(); --it) {
211 if ((*it)->getType() == gui::EGUIET_BUTTON) {
212 Environment->setFocus(*it);
217 // 5. first focusable (not statictext, not tabheader)
218 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
219 it != children.end(); ++it) {
220 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
221 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
222 Environment->setFocus(*it);
227 // 6. first child element
228 if (children.empty())
229 Environment->setFocus(this);
231 Environment->setFocus(*(children.begin()));
234 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
236 for (u32 i = 0; i < m_tables.size(); ++i) {
237 if (tablename == m_tables[i].first.fname)
238 return m_tables[i].second;
243 static std::vector<std::string> split(const std::string &s, char delim)
245 std::vector<std::string> tokens;
247 std::string current = "";
248 bool last_was_escape = false;
249 for (unsigned int i = 0; i < s.size(); i++) {
250 char si = s.c_str()[i];
251 if (last_was_escape) {
254 last_was_escape = false;
257 tokens.push_back(current);
259 last_was_escape = false;
260 } else if (si == '\\') {
261 last_was_escape = true;
264 last_was_escape = false;
269 tokens.push_back(current);
274 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
276 std::vector<std::string> parts = split(element,',');
278 if (((parts.size() == 2) || parts.size() == 3) ||
279 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
281 if (parts[1].find(';') != std::string::npos)
282 parts[1] = parts[1].substr(0,parts[1].find(';'));
284 data->invsize.X = MYMAX(0, stof(parts[0]));
285 data->invsize.Y = MYMAX(0, stof(parts[1]));
288 if (parts.size() == 3) {
289 if (parts[2] == "true") {
290 lockSize(true,v2u32(800,600));
294 data->explicit_size = true;
297 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
300 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
302 if (m_gamedef == 0) {
303 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
307 std::vector<std::string> parts = split(element,';');
309 if (((parts.size() == 4) || (parts.size() == 5)) ||
310 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
312 std::string location = parts[0];
313 std::string listname = parts[1];
314 std::vector<std::string> v_pos = split(parts[2],',');
315 std::vector<std::string> v_geom = split(parts[3],',');
316 std::string startindex = "";
317 if (parts.size() == 5)
318 startindex = parts[4];
320 MY_CHECKPOS("list",2);
321 MY_CHECKGEOM("list",3);
323 InventoryLocation loc;
325 if(location == "context" || location == "current_name")
326 loc = m_current_inventory_location;
328 loc.deSerialize(location);
330 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
331 pos.X += stof(v_pos[0]) * (float)spacing.X;
332 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
335 geom.X = stoi(v_geom[0]);
336 geom.Y = stoi(v_geom[1]);
340 start_i = stoi(startindex);
342 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
343 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
347 if(!data->explicit_size)
348 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
349 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
352 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
355 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
357 std::vector<std::string> parts = split(element,';');
359 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
360 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
362 std::vector<std::string> v_pos = split(parts[0],',');
363 std::string name = parts[1];
364 std::string label = parts[2];
365 std::string selected = "";
367 if (parts.size() >= 4)
370 MY_CHECKPOS("checkbox",0);
373 pos.X += stof(v_pos[0]) * (float) spacing.X;
374 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
376 bool fselected = false;
378 if (selected == "true")
381 std::wstring wlabel = utf8_to_wide(label);
383 core::rect<s32> rect = core::rect<s32>(
384 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
385 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
386 pos.Y + ((imgsize.Y/2) + m_btn_height));
390 wlabel, //Needed for displaying text on MSVC
395 spec.ftype = f_CheckBox;
397 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
398 spec.fid, spec.flabel.c_str());
400 if (spec.fname == data->focused_fieldname) {
401 Environment->setFocus(e);
404 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
405 m_fields.push_back(spec);
408 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
411 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
413 std::vector<std::string> parts = split(element,';');
415 if (parts.size() >= 5) {
416 std::vector<std::string> v_pos = split(parts[0],',');
417 std::vector<std::string> v_dim = split(parts[1],',');
418 std::string name = parts[2];
419 std::string value = parts[4];
421 MY_CHECKPOS("scrollbar",0);
424 pos.X += stof(v_pos[0]) * (float) spacing.X;
425 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
427 if (v_dim.size() != 2) {
428 errorstream<< "Invalid size for element " << "scrollbar"
429 << "specified: \"" << parts[1] << "\"" << std::endl;
434 dim.X = stof(v_dim[0]) * (float) spacing.X;
435 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
437 core::rect<s32> rect =
438 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
447 bool is_horizontal = true;
449 if (parts[2] == "vertical")
450 is_horizontal = false;
452 spec.ftype = f_ScrollBar;
454 gui::IGUIScrollBar* e =
455 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
459 e->setPos(stoi(parts[4]));
461 e->setLargeStep(100);
463 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
464 m_fields.push_back(spec);
467 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
470 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
472 std::vector<std::string> parts = split(element,';');
474 if ((parts.size() == 3) ||
475 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
477 std::vector<std::string> v_pos = split(parts[0],',');
478 std::vector<std::string> v_geom = split(parts[1],',');
479 std::string name = unescape_string(parts[2]);
481 MY_CHECKPOS("image",0);
482 MY_CHECKGEOM("image",1);
484 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
485 pos.X += stof(v_pos[0]) * (float) spacing.X;
486 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
489 geom.X = stof(v_geom[0]) * (float)imgsize.X;
490 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
492 if(!data->explicit_size)
493 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
494 m_images.push_back(ImageDrawSpec(name, pos, geom));
498 if (parts.size() == 2) {
499 std::vector<std::string> v_pos = split(parts[0],',');
500 std::string name = unescape_string(parts[1]);
502 MY_CHECKPOS("image",0);
504 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
505 pos.X += stof(v_pos[0]) * (float) spacing.X;
506 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
508 if(!data->explicit_size)
509 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
510 m_images.push_back(ImageDrawSpec(name, pos));
513 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
516 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
518 std::vector<std::string> parts = split(element,';');
520 if ((parts.size() == 3) ||
521 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
523 std::vector<std::string> v_pos = split(parts[0],',');
524 std::vector<std::string> v_geom = split(parts[1],',');
525 std::string name = parts[2];
527 MY_CHECKPOS("itemimage",0);
528 MY_CHECKGEOM("itemimage",1);
530 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
531 pos.X += stof(v_pos[0]) * (float) spacing.X;
532 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
535 geom.X = stof(v_geom[0]) * (float)imgsize.X;
536 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
538 if(!data->explicit_size)
539 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
540 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
543 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
546 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
549 std::vector<std::string> parts = split(element,';');
551 if ((parts.size() == 4) ||
552 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
554 std::vector<std::string> v_pos = split(parts[0],',');
555 std::vector<std::string> v_geom = split(parts[1],',');
556 std::string name = parts[2];
557 std::string label = parts[3];
559 MY_CHECKPOS("button",0);
560 MY_CHECKGEOM("button",1);
563 pos.X += stof(v_pos[0]) * (float)spacing.X;
564 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
567 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
568 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
570 core::rect<s32> rect =
571 core::rect<s32>(pos.X, pos.Y - m_btn_height,
572 pos.X + geom.X, pos.Y + m_btn_height);
574 if(!data->explicit_size)
575 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
577 label = unescape_string(label);
579 std::wstring wlabel = utf8_to_wide(label);
587 spec.ftype = f_Button;
588 if(type == "button_exit")
590 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
591 spec.flabel.c_str());
593 if (spec.fname == data->focused_fieldname) {
594 Environment->setFocus(e);
597 m_fields.push_back(spec);
600 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
603 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
605 std::vector<std::string> parts = split(element,';');
607 if (((parts.size() == 3) || (parts.size() == 4)) ||
608 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
610 std::vector<std::string> v_pos = split(parts[0],',');
611 std::vector<std::string> v_geom = split(parts[1],',');
612 std::string name = unescape_string(parts[2]);
614 MY_CHECKPOS("background",0);
615 MY_CHECKGEOM("background",1);
617 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
618 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
619 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
622 geom.X = stof(v_geom[0]) * (float)spacing.X;
623 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
625 if (parts.size() == 4) {
626 m_clipbackground = is_yes(parts[3]);
627 if (m_clipbackground) {
628 pos.X = stoi(v_pos[0]); //acts as offset
629 pos.Y = stoi(v_pos[1]); //acts as offset
633 if(!data->explicit_size)
634 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
635 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
638 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
641 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
643 std::vector<std::string> parts = split(element,';');
645 data->table_options.clear();
646 for (size_t i = 0; i < parts.size(); ++i) {
647 // Parse table option
648 std::string opt = unescape_string(parts[i]);
649 data->table_options.push_back(GUITable::splitOption(opt));
653 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
655 std::vector<std::string> parts = split(element,';');
657 data->table_columns.clear();
658 for (size_t i = 0; i < parts.size(); ++i) {
659 std::vector<std::string> col_parts = split(parts[i],',');
660 GUITable::TableColumn column;
662 if (!col_parts.empty())
663 column.type = col_parts[0];
664 // Parse column options
665 for (size_t j = 1; j < col_parts.size(); ++j) {
666 std::string opt = unescape_string(col_parts[j]);
667 column.options.push_back(GUITable::splitOption(opt));
669 data->table_columns.push_back(column);
673 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
675 std::vector<std::string> parts = split(element,';');
677 if (((parts.size() == 4) || (parts.size() == 5)) ||
678 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
680 std::vector<std::string> v_pos = split(parts[0],',');
681 std::vector<std::string> v_geom = split(parts[1],',');
682 std::string name = parts[2];
683 std::vector<std::string> items = split(parts[3],',');
684 std::string str_initial_selection = "";
685 std::string str_transparent = "false";
687 if (parts.size() >= 5)
688 str_initial_selection = parts[4];
690 MY_CHECKPOS("table",0);
691 MY_CHECKGEOM("table",1);
694 pos.X += stof(v_pos[0]) * (float)spacing.X;
695 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
698 geom.X = stof(v_geom[0]) * (float)spacing.X;
699 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
702 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
711 spec.ftype = f_Table;
713 for (unsigned int i = 0; i < items.size(); ++i) {
714 items[i] = unescape_string(items[i]);
717 //now really show table
718 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
721 if (spec.fname == data->focused_fieldname) {
722 Environment->setFocus(e);
725 e->setTable(data->table_options, data->table_columns, items);
727 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
728 e->setDynamicData(data->table_dyndata[name]);
731 if ((str_initial_selection != "") &&
732 (str_initial_selection != "0"))
733 e->setSelected(stoi(str_initial_selection.c_str()));
735 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
736 m_fields.push_back(spec);
739 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
742 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
744 std::vector<std::string> parts = split(element,';');
746 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
747 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
749 std::vector<std::string> v_pos = split(parts[0],',');
750 std::vector<std::string> v_geom = split(parts[1],',');
751 std::string name = parts[2];
752 std::vector<std::string> items = split(parts[3],',');
753 std::string str_initial_selection = "";
754 std::string str_transparent = "false";
756 if (parts.size() >= 5)
757 str_initial_selection = parts[4];
759 if (parts.size() >= 6)
760 str_transparent = parts[5];
762 MY_CHECKPOS("textlist",0);
763 MY_CHECKGEOM("textlist",1);
766 pos.X += stof(v_pos[0]) * (float)spacing.X;
767 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
770 geom.X = stof(v_geom[0]) * (float)spacing.X;
771 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
774 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
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(name) != data->table_dyndata.end()) {
800 e->setDynamicData(data->table_dyndata[name]);
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));
846 spec.ftype = f_DropDown;
849 //now really show list
850 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
852 if (spec.fname == data->focused_fieldname) {
853 Environment->setFocus(e);
856 for (unsigned int i=0; i < items.size(); i++) {
857 e->addItem(utf8_to_wide(items[i]).c_str());
860 if (str_initial_selection != "")
861 e->setSelected(stoi(str_initial_selection.c_str())-1);
863 m_fields.push_back(spec);
866 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
867 << element << "'" << std::endl;
870 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
872 std::vector<std::string> parts = split(element,';');
874 if ((parts.size() == 4) ||
875 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
877 std::vector<std::string> v_pos = split(parts[0],',');
878 std::vector<std::string> v_geom = split(parts[1],',');
879 std::string name = parts[2];
880 std::string label = parts[3];
882 MY_CHECKPOS("pwdfield",0);
883 MY_CHECKGEOM("pwdfield",1);
886 pos.X += stof(v_pos[0]) * (float)spacing.X;
887 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
890 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
892 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
893 pos.Y -= m_btn_height;
894 geom.Y = m_btn_height*2;
896 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
898 label = unescape_string(label);
900 std::wstring wlabel = utf8_to_wide(label);
910 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
912 if (spec.fname == data->focused_fieldname) {
913 Environment->setFocus(e);
916 if (label.length() >= 1)
918 int font_height = g_fontengine->getTextHeight();
919 rect.UpperLeftCorner.Y -= font_height;
920 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
921 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
924 e->setPasswordBox(true,L'*');
927 evt.EventType = EET_KEY_INPUT_EVENT;
928 evt.KeyInput.Key = KEY_END;
929 evt.KeyInput.Char = 0;
930 evt.KeyInput.Control = 0;
931 evt.KeyInput.Shift = 0;
932 evt.KeyInput.PressedDown = true;
934 m_fields.push_back(spec);
937 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
940 void GUIFormSpecMenu::parseSimpleField(parserData* data,
941 std::vector<std::string> &parts)
943 std::string name = parts[0];
944 std::string label = parts[1];
945 std::string default_val = parts[2];
947 core::rect<s32> rect;
949 if(data->explicit_size)
950 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
952 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
953 pos.Y = ((m_fields.size()+2)*60);
954 v2s32 size = DesiredRect.getSize();
956 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
957 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
961 default_val = m_form_src->resolveText(default_val);
963 default_val = unescape_string(default_val);
964 label = unescape_string(label);
966 std::wstring wlabel = utf8_to_wide(label);
971 utf8_to_wide(default_val),
977 // spec field id to 0, this stops submit searching for a value that isn't there
978 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
984 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
985 if (g_settings->getBool("freetype")) {
986 e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
987 true, Environment, this, spec.fid, rect);
992 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
994 if (spec.fname == data->focused_fieldname) {
995 Environment->setFocus(e);
999 evt.EventType = EET_KEY_INPUT_EVENT;
1000 evt.KeyInput.Key = KEY_END;
1001 evt.KeyInput.Char = 0;
1002 evt.KeyInput.Control = 0;
1003 evt.KeyInput.Shift = 0;
1004 evt.KeyInput.PressedDown = true;
1007 if (label.length() >= 1)
1009 int font_height = g_fontengine->getTextHeight();
1010 rect.UpperLeftCorner.Y -= font_height;
1011 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1012 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1016 m_fields.push_back(spec);
1019 void GUIFormSpecMenu::parseTextArea(parserData* data,
1020 std::vector<std::string>& parts,std::string type)
1023 std::vector<std::string> v_pos = split(parts[0],',');
1024 std::vector<std::string> v_geom = split(parts[1],',');
1025 std::string name = parts[2];
1026 std::string label = parts[3];
1027 std::string default_val = parts[4];
1029 MY_CHECKPOS(type,0);
1030 MY_CHECKGEOM(type,1);
1033 pos.X = stof(v_pos[0]) * (float) spacing.X;
1034 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1038 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1040 if (type == "textarea")
1042 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1043 pos.Y += m_btn_height;
1047 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1048 pos.Y -= m_btn_height;
1049 geom.Y = m_btn_height*2;
1052 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1054 if(!data->explicit_size)
1055 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1058 default_val = m_form_src->resolveText(default_val);
1061 default_val = unescape_string(default_val);
1062 label = unescape_string(label);
1064 std::wstring wlabel = utf8_to_wide(label);
1069 utf8_to_wide(default_val),
1075 // spec field id to 0, this stops submit searching for a value that isn't there
1076 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1082 gui::IGUIEditBox *e;
1083 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1084 if (g_settings->getBool("freetype")) {
1085 e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1086 true, Environment, this, spec.fid, rect);
1091 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1094 if (spec.fname == data->focused_fieldname) {
1095 Environment->setFocus(e);
1098 if (type == "textarea")
1100 e->setMultiLine(true);
1101 e->setWordWrap(true);
1102 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1105 evt.EventType = EET_KEY_INPUT_EVENT;
1106 evt.KeyInput.Key = KEY_END;
1107 evt.KeyInput.Char = 0;
1108 evt.KeyInput.Control = 0;
1109 evt.KeyInput.Shift = 0;
1110 evt.KeyInput.PressedDown = true;
1114 if (label.length() >= 1)
1116 int font_height = g_fontengine->getTextHeight();
1117 rect.UpperLeftCorner.Y -= font_height;
1118 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1119 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1122 m_fields.push_back(spec);
1125 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1128 std::vector<std::string> parts = split(element,';');
1130 if (parts.size() == 3 || parts.size() == 4) {
1131 parseSimpleField(data,parts);
1135 if ((parts.size() == 5) ||
1136 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1138 parseTextArea(data,parts,type);
1141 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1144 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1146 std::vector<std::string> parts = split(element,';');
1148 if ((parts.size() == 2) ||
1149 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1151 std::vector<std::string> v_pos = split(parts[0],',');
1152 std::string text = parts[1];
1154 MY_CHECKPOS("label",0);
1156 v2s32 pos = padding;
1157 pos.X += stof(v_pos[0]) * (float)spacing.X;
1158 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1160 if(!data->explicit_size)
1161 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1163 text = unescape_string(text);
1164 std::vector<std::string> lines = split(text, '\n');
1166 for (unsigned int i = 0; i != lines.size(); i++) {
1167 // Lines are spaced at the nominal distance of
1168 // 2/5 inventory slot, even if the font doesn't
1169 // quite match that. This provides consistent
1170 // form layout, at the expense of sometimes
1171 // having sub-optimal spacing for the font.
1172 // We multiply by 2 and then divide by 5, rather
1173 // than multiply by 0.4, to get exact results
1174 // in the integer cases: 0.4 is not exactly
1175 // representable in binary floating point.
1176 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1177 std::wstring wlabel = utf8_to_wide(lines[i]);
1178 core::rect<s32> rect = core::rect<s32>(
1179 pos.X, posy - m_btn_height,
1180 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1181 posy + m_btn_height);
1188 gui::IGUIStaticText *e =
1189 Environment->addStaticText(spec.flabel.c_str(),
1190 rect, false, false, this, spec.fid);
1191 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1193 m_fields.push_back(spec);
1198 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1201 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1203 std::vector<std::string> parts = split(element,';');
1205 if ((parts.size() == 2) ||
1206 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1208 std::vector<std::string> v_pos = split(parts[0],',');
1209 std::wstring text = utf8_to_wide(unescape_string(parts[1]));
1211 MY_CHECKPOS("vertlabel",1);
1213 v2s32 pos = padding;
1214 pos.X += stof(v_pos[0]) * (float)spacing.X;
1215 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1217 core::rect<s32> rect = core::rect<s32>(
1218 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1220 font_line_height(m_font)
1222 +((imgsize.Y/2)- m_btn_height));
1223 //actually text.length() would be correct but adding +1 avoids to break all mods
1225 if(!data->explicit_size)
1226 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1228 std::wstring label = L"";
1230 for (unsigned int i=0; i < text.length(); i++) {
1241 gui::IGUIStaticText *t =
1242 Environment->addStaticText(spec.flabel.c_str(), rect, false, false, this, spec.fid);
1243 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1244 m_fields.push_back(spec);
1247 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1250 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1253 std::vector<std::string> parts = split(element,';');
1255 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1256 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1258 std::vector<std::string> v_pos = split(parts[0],',');
1259 std::vector<std::string> v_geom = split(parts[1],',');
1260 std::string image_name = parts[2];
1261 std::string name = parts[3];
1262 std::string label = parts[4];
1264 MY_CHECKPOS("imagebutton",0);
1265 MY_CHECKGEOM("imagebutton",1);
1267 v2s32 pos = padding;
1268 pos.X += stof(v_pos[0]) * (float)spacing.X;
1269 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1271 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1272 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1274 bool noclip = false;
1275 bool drawborder = true;
1276 std::string pressed_image_name = "";
1278 if (parts.size() >= 7) {
1279 if (parts[5] == "true")
1281 if (parts[6] == "false")
1285 if (parts.size() >= 8) {
1286 pressed_image_name = parts[7];
1289 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1291 if(!data->explicit_size)
1292 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1294 image_name = unescape_string(image_name);
1295 pressed_image_name = unescape_string(pressed_image_name);
1296 label = unescape_string(label);
1298 std::wstring wlabel = utf8_to_wide(label);
1303 utf8_to_wide(image_name),
1306 spec.ftype = f_Button;
1307 if(type == "image_button_exit")
1308 spec.is_exit = true;
1310 video::ITexture *texture = 0;
1311 video::ITexture *pressed_texture = 0;
1312 texture = m_tsrc->getTexture(image_name);
1313 if (pressed_image_name != "")
1314 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1316 pressed_texture = texture;
1318 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1320 if (spec.fname == data->focused_fieldname) {
1321 Environment->setFocus(e);
1324 e->setUseAlphaChannel(true);
1325 e->setImage(guiScalingImageButton(
1326 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1327 e->setPressedImage(guiScalingImageButton(
1328 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1329 e->setScaleImage(true);
1330 e->setNotClipped(noclip);
1331 e->setDrawBorder(drawborder);
1333 m_fields.push_back(spec);
1337 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1340 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1342 std::vector<std::string> parts = split(element,';');
1344 if (((parts.size() == 4) || (parts.size() == 6)) ||
1345 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1347 std::vector<std::string> v_pos = split(parts[0],',');
1348 std::string name = parts[1];
1349 std::vector<std::string> buttons = split(parts[2],',');
1350 std::string str_index = parts[3];
1351 bool show_background = true;
1352 bool show_border = true;
1353 int tab_index = stoi(str_index) -1;
1355 MY_CHECKPOS("tabheader",0);
1357 if (parts.size() == 6) {
1358 if (parts[4] == "true")
1359 show_background = false;
1360 if (parts[5] == "false")
1361 show_border = false;
1371 spec.ftype = f_TabHeader;
1374 pos.X += stof(v_pos[0]) * (float)spacing.X;
1375 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1377 geom.X = DesiredRect.getWidth();
1378 geom.Y = m_btn_height*2;
1380 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1383 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1384 show_background, show_border, spec.fid);
1385 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1386 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1387 e->setTabHeight(m_btn_height*2);
1389 if (spec.fname == data->focused_fieldname) {
1390 Environment->setFocus(e);
1393 e->setNotClipped(true);
1395 for (unsigned int i = 0; i < buttons.size(); i++) {
1396 e->addTab(utf8_to_wide(buttons[i]).c_str(), -1);
1399 if ((tab_index >= 0) &&
1400 (buttons.size() < INT_MAX) &&
1401 (tab_index < (int) buttons.size()))
1402 e->setActiveTab(tab_index);
1404 m_fields.push_back(spec);
1407 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1408 << element << "'" << std::endl;
1411 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1414 if (m_gamedef == 0) {
1416 "WARNING: invalid use of item_image_button with m_gamedef==0"
1421 std::vector<std::string> parts = split(element,';');
1423 if ((parts.size() == 5) ||
1424 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1426 std::vector<std::string> v_pos = split(parts[0],',');
1427 std::vector<std::string> v_geom = split(parts[1],',');
1428 std::string item_name = parts[2];
1429 std::string name = parts[3];
1430 std::string label = parts[4];
1432 MY_CHECKPOS("itemimagebutton",0);
1433 MY_CHECKGEOM("itemimagebutton",1);
1435 v2s32 pos = padding;
1436 pos.X += stof(v_pos[0]) * (float)spacing.X;
1437 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1439 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1440 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1442 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1444 if(!data->explicit_size)
1445 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1447 IItemDefManager *idef = m_gamedef->idef();
1449 item.deSerialize(item_name, idef);
1450 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1453 TooltipSpec(item.getDefinition(idef).description,
1454 m_default_tooltip_bgcolor,
1455 m_default_tooltip_color);
1457 label = unescape_string(label);
1460 utf8_to_wide(label),
1461 utf8_to_wide(item_name),
1462 258 + m_fields.size()
1465 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1467 if (spec.fname == data->focused_fieldname) {
1468 Environment->setFocus(e);
1471 e->setUseAlphaChannel(true);
1472 e->setImage(guiScalingImageButton(Environment->getVideoDriver(), texture, geom.X, geom.Y));
1473 e->setPressedImage(guiScalingImageButton(Environment->getVideoDriver(), texture, geom.X, geom.Y));
1474 e->setScaleImage(true);
1475 spec.ftype = f_Button;
1476 rect+=data->basepos-padding;
1478 m_fields.push_back(spec);
1481 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1484 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1486 std::vector<std::string> parts = split(element,';');
1488 if ((parts.size() == 3) ||
1489 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1491 std::vector<std::string> v_pos = split(parts[0],',');
1492 std::vector<std::string> v_geom = split(parts[1],',');
1494 MY_CHECKPOS("box",0);
1495 MY_CHECKGEOM("box",1);
1497 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1498 pos.X += stof(v_pos[0]) * (float) spacing.X;
1499 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1502 geom.X = stof(v_geom[0]) * (float)spacing.X;
1503 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1505 video::SColor tmp_color;
1507 if (parseColorString(parts[2], tmp_color, false)) {
1508 BoxDrawSpec spec(pos, geom, tmp_color);
1510 m_boxes.push_back(spec);
1513 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1517 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1520 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1522 std::vector<std::string> parts = split(element,';');
1524 if (((parts.size() == 1) || (parts.size() == 2)) ||
1525 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1527 parseColorString(parts[0],m_bgcolor,false);
1529 if (parts.size() == 2) {
1530 std::string fullscreen = parts[1];
1531 m_bgfullscreen = is_yes(fullscreen);
1535 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1538 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1540 std::vector<std::string> parts = split(element,';');
1542 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1543 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1545 parseColorString(parts[0], m_slotbg_n, false);
1546 parseColorString(parts[1], m_slotbg_h, false);
1548 if (parts.size() >= 3) {
1549 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1550 m_slotborder = true;
1553 if (parts.size() == 5) {
1554 video::SColor tmp_color;
1556 if (parseColorString(parts[3], tmp_color, false))
1557 m_default_tooltip_bgcolor = tmp_color;
1558 if (parseColorString(parts[4], tmp_color, false))
1559 m_default_tooltip_color = tmp_color;
1563 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1566 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1568 std::vector<std::string> parts = split(element,';');
1569 if (parts.size() == 2) {
1570 std::string name = parts[0];
1571 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1572 m_default_tooltip_bgcolor, m_default_tooltip_color);
1574 } else if (parts.size() == 4) {
1575 std::string name = parts[0];
1576 video::SColor tmp_color1, tmp_color2;
1577 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1578 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1579 tmp_color1, tmp_color2);
1583 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1586 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1592 std::vector<std::string> parts = split(data,'[');
1594 if (parts.size() < 2) {
1598 if (parts[0] != "formspec_version") {
1602 if (is_number(parts[1])) {
1603 m_formspec_version = mystoi(parts[1]);
1610 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1615 std::vector<std::string> parts = split(element,'[');
1617 if (parts.size() < 2)
1620 std::string type = trim(parts[0]);
1621 std::string description = trim(parts[1]);
1623 if (type != "size" && type != "invsize")
1626 if (type == "invsize")
1627 log_deprecated("Deprecated formspec element \"invsize\" is used");
1629 parseSize(data, description);
1634 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1640 std::vector<std::string> parts = split(element,'[');
1642 // ugly workaround to keep compatibility
1643 if (parts.size() > 2) {
1644 if (trim(parts[0]) == "image") {
1645 for (unsigned int i=2;i< parts.size(); i++) {
1646 parts[1] += "[" + parts[i];
1652 if (parts.size() < 2) {
1656 std::string type = trim(parts[0]);
1657 std::string description = trim(parts[1]);
1659 if (type == "list") {
1660 parseList(data,description);
1664 if (type == "checkbox") {
1665 parseCheckbox(data,description);
1669 if (type == "image") {
1670 parseImage(data,description);
1674 if (type == "item_image") {
1675 parseItemImage(data,description);
1679 if ((type == "button") || (type == "button_exit")) {
1680 parseButton(data,description,type);
1684 if (type == "background") {
1685 parseBackground(data,description);
1689 if (type == "tableoptions"){
1690 parseTableOptions(data,description);
1694 if (type == "tablecolumns"){
1695 parseTableColumns(data,description);
1699 if (type == "table"){
1700 parseTable(data,description);
1704 if (type == "textlist"){
1705 parseTextList(data,description);
1709 if (type == "dropdown"){
1710 parseDropDown(data,description);
1714 if (type == "pwdfield") {
1715 parsePwdField(data,description);
1719 if ((type == "field") || (type == "textarea")){
1720 parseField(data,description,type);
1724 if (type == "label") {
1725 parseLabel(data,description);
1729 if (type == "vertlabel") {
1730 parseVertLabel(data,description);
1734 if (type == "item_image_button") {
1735 parseItemImageButton(data,description);
1739 if ((type == "image_button") || (type == "image_button_exit")) {
1740 parseImageButton(data,description,type);
1744 if (type == "tabheader") {
1745 parseTabHeader(data,description);
1749 if (type == "box") {
1750 parseBox(data,description);
1754 if (type == "bgcolor") {
1755 parseBackgroundColor(data,description);
1759 if (type == "listcolors") {
1760 parseListColors(data,description);
1764 if (type == "tooltip") {
1765 parseTooltip(data,description);
1769 if (type == "scrollbar") {
1770 parseScrollBar(data, description);
1776 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1780 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1782 /* useless to regenerate without a screensize */
1783 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1790 for (u32 i = 0; i < m_tables.size(); ++i) {
1791 std::string tablename = m_tables[i].first.fname;
1792 GUITable *table = m_tables[i].second;
1793 mydata.table_dyndata[tablename] = table->getDynamicData();
1797 if (!m_focused_element.empty())
1798 mydata.focused_fieldname = m_focused_element;
1801 gui::IGUIElement *focused_element = Environment->getFocus();
1802 if (focused_element && focused_element->getParent() == this) {
1803 s32 focused_id = focused_element->getID();
1804 if (focused_id > 257) {
1805 for (u32 i=0; i<m_fields.size(); i++) {
1806 if (m_fields[i].fid == focused_id) {
1807 mydata.focused_fieldname =
1818 for (u32 i = 0; i < m_tables.size(); ++i) {
1819 GUITable *table = m_tables[i].second;
1823 mydata.size= v2s32(100,100);
1824 mydata.screensize = screensize;
1826 // Base position of contents of form
1827 mydata.basepos = getBasePos();
1829 /* Convert m_init_draw_spec to m_inventorylists */
1831 m_inventorylists.clear();
1833 m_backgrounds.clear();
1834 m_itemimages.clear();
1836 m_checkboxes.clear();
1837 m_scrollbars.clear();
1842 // Set default values (fits old formspec values)
1843 m_bgcolor = video::SColor(140,0,0,0);
1844 m_bgfullscreen = false;
1846 m_slotbg_n = video::SColor(255,128,128,128);
1847 m_slotbg_h = video::SColor(255,192,192,192);
1849 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1850 m_default_tooltip_color = video::SColor(255,255,255,255);
1852 m_slotbordercolor = video::SColor(200,0,0,0);
1853 m_slotborder = false;
1855 m_clipbackground = false;
1858 assert(m_tooltip_element == NULL);
1859 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1860 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1861 m_tooltip_element->enableOverrideColor(true);
1862 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1863 m_tooltip_element->setDrawBackground(true);
1864 m_tooltip_element->setDrawBorder(true);
1865 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1866 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1867 m_tooltip_element->setWordWrap(false);
1868 //we're not parent so no autograb for this one!
1869 m_tooltip_element->grab();
1872 std::vector<std::string> elements = split(m_formspec_string,']');
1875 /* try to read version from first element only */
1876 if (elements.size() >= 1) {
1877 if ( parseVersionDirect(elements[0]) ) {
1882 /* we need size first in order to calculate image scale */
1883 mydata.explicit_size = false;
1884 for (; i< elements.size(); i++) {
1885 if (!parseSizeDirect(&mydata, elements[i])) {
1890 if (mydata.explicit_size) {
1891 // compute scaling for specified form size
1893 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1894 v2u32 delta = current_screensize - m_lockscreensize;
1896 if (current_screensize.Y > m_lockscreensize.Y)
1901 if (current_screensize.X > m_lockscreensize.X)
1906 offset = v2s32(delta.X,delta.Y);
1908 mydata.screensize = m_lockscreensize;
1910 offset = v2s32(0,0);
1913 double gui_scaling = g_settings->getFloat("gui_scaling");
1914 double screen_dpi = porting::getDisplayDensity() * 96;
1918 // In fixed-size mode, inventory image size
1919 // is 0.53 inch multiplied by the gui_scaling
1920 // config parameter. This magic size is chosen
1921 // to make the main menu (15.5 inventory images
1922 // wide, including border) just fit into the
1923 // default window (800 pixels wide) at 96 DPI
1924 // and default scaling (1.00).
1925 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1927 // In variable-size mode, we prefer to make the
1928 // inventory image size 1/15 of screen height,
1929 // multiplied by the gui_scaling config parameter.
1930 // If the preferred size won't fit the whole
1931 // form on the screen, either horizontally or
1932 // vertically, then we scale it down to fit.
1933 // (The magic numbers in the computation of what
1934 // fits arise from the scaling factors in the
1935 // following stanza, including the form border,
1936 // help text space, and 0.1 inventory slot spare.)
1937 // However, a minimum size is also set, that
1938 // the image size can't be less than 0.3 inch
1939 // multiplied by gui_scaling, even if this means
1940 // the form doesn't fit the screen.
1941 double prefer_imgsize = mydata.screensize.Y / 15 *
1943 double fitx_imgsize = mydata.screensize.X /
1944 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1945 double fity_imgsize = mydata.screensize.Y /
1946 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1947 double screen_dpi = porting::getDisplayDensity() * 96;
1948 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1949 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1950 MYMIN(fitx_imgsize, fity_imgsize)));
1953 // Everything else is scaled in proportion to the
1954 // inventory image size. The inventory slot spacing
1955 // is 5/4 image size horizontally and 15/13 image size
1956 // vertically. The padding around the form (incorporating
1957 // the border of the outer inventory slots) is 3/8
1958 // image size. Font height (baseline to baseline)
1959 // is 2/5 vertical inventory slot spacing, and button
1960 // half-height is 7/8 of font height.
1961 imgsize = v2s32(use_imgsize, use_imgsize);
1962 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
1963 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
1964 m_btn_height = use_imgsize*15.0/13 * 0.35;
1966 m_font = g_fontengine->getFont();
1968 mydata.size = v2s32(
1969 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
1970 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
1972 DesiredRect = mydata.rect = core::rect<s32>(
1973 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
1974 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
1975 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
1976 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
1979 // Non-size[] form must consist only of text fields and
1980 // implicit "Proceed" button. Use default font, and
1981 // temporary form size which will be recalculated below.
1982 m_font = g_fontengine->getFont();
1983 m_btn_height = font_line_height(m_font) * 0.875;
1984 DesiredRect = core::rect<s32>(
1985 mydata.screensize.X/2 - 580/2,
1986 mydata.screensize.Y/2 - 300/2,
1987 mydata.screensize.X/2 + 580/2,
1988 mydata.screensize.Y/2 + 300/2
1991 recalculateAbsolutePosition(false);
1992 mydata.basepos = getBasePos();
1993 m_tooltip_element->setOverrideFont(m_font);
1995 gui::IGUISkin* skin = Environment->getSkin();
1996 sanity_check(skin != NULL);
1997 gui::IGUIFont *old_font = skin->getFont();
1998 skin->setFont(m_font);
2000 for (; i< elements.size(); i++) {
2001 parseElement(&mydata, elements[i]);
2004 // If there are fields without explicit size[], add a "Proceed"
2005 // button and adjust size to fit all the fields.
2006 if (m_fields.size() && !mydata.explicit_size) {
2007 mydata.rect = core::rect<s32>(
2008 mydata.screensize.X/2 - 580/2,
2009 mydata.screensize.Y/2 - 300/2,
2010 mydata.screensize.X/2 + 580/2,
2011 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2013 DesiredRect = mydata.rect;
2014 recalculateAbsolutePosition(false);
2015 mydata.basepos = getBasePos();
2018 v2s32 pos = mydata.basepos;
2019 pos.Y = ((m_fields.size()+2)*60);
2021 v2s32 size = DesiredRect.getSize();
2023 core::rect<s32>(size.X/2-70, pos.Y,
2024 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2025 const wchar_t *text = wgettext("Proceed");
2026 Environment->addButton(mydata.rect, this, 257, text);
2032 //set initial focus if parser didn't set it
2033 focused_element = Environment->getFocus();
2034 if (!focused_element
2035 || !isMyChild(focused_element)
2036 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2039 skin->setFont(old_font);
2043 bool GUIFormSpecMenu::getAndroidUIInput()
2045 /* no dialog shown */
2046 if (m_JavaDialogFieldName == "") {
2051 if (porting::getInputDialogState() == -1) {
2055 std::string fieldname = m_JavaDialogFieldName;
2056 m_JavaDialogFieldName = "";
2058 /* no value abort dialog processing */
2059 if (porting::getInputDialogState() != 0) {
2063 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2064 iter != m_fields.end(); iter++) {
2066 if (iter->fname != fieldname) {
2069 IGUIElement* tochange = getElementFromId(iter->fid);
2071 if (tochange == 0) {
2075 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2079 std::string text = porting::getInputDialogValue();
2081 ((gui::IGUIEditBox*) tochange)->
2082 setText(utf8_to_wide(text).c_str());
2088 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2090 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2092 for(u32 i=0; i<m_inventorylists.size(); i++)
2094 const ListDrawSpec &s = m_inventorylists[i];
2096 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2097 s32 item_i = i + s.start_item_i;
2098 s32 x = (i%s.geom.X) * spacing.X;
2099 s32 y = (i/s.geom.X) * spacing.Y;
2101 core::rect<s32> rect = imgrect + s.pos + p0;
2102 if(rect.isPointInside(p))
2104 return ItemSpec(s.inventoryloc, s.listname, item_i);
2109 return ItemSpec(InventoryLocation(), "", -1);
2112 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
2114 video::IVideoDriver* driver = Environment->getVideoDriver();
2116 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2118 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2119 <<"The inventory location "
2120 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2124 InventoryList *ilist = inv->getList(s.listname);
2126 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2127 <<"The inventory list \""<<s.listname<<"\" @ \""
2128 <<s.inventoryloc.dump()<<"\" doesn't exist"
2133 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2135 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2137 s32 item_i = i + s.start_item_i;
2138 if(item_i >= (s32) ilist->getSize())
2140 s32 x = (i%s.geom.X) * spacing.X;
2141 s32 y = (i/s.geom.X) * spacing.Y;
2143 core::rect<s32> rect = imgrect + s.pos + p;
2146 item = ilist->getItem(item_i);
2148 bool selected = m_selected_item
2149 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2150 && m_selected_item->listname == s.listname
2151 && m_selected_item->i == item_i;
2152 bool hovering = rect.isPointInside(m_pointer);
2157 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2159 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2162 //Draw inv slot borders
2164 s32 x1 = rect.UpperLeftCorner.X;
2165 s32 y1 = rect.UpperLeftCorner.Y;
2166 s32 x2 = rect.LowerRightCorner.X;
2167 s32 y2 = rect.LowerRightCorner.Y;
2169 driver->draw2DRectangle(m_slotbordercolor,
2170 core::rect<s32>(v2s32(x1 - border, y1 - border),
2171 v2s32(x2 + border, y1)), NULL);
2172 driver->draw2DRectangle(m_slotbordercolor,
2173 core::rect<s32>(v2s32(x1 - border, y2),
2174 v2s32(x2 + border, y2 + border)), NULL);
2175 driver->draw2DRectangle(m_slotbordercolor,
2176 core::rect<s32>(v2s32(x1 - border, y1),
2177 v2s32(x1, y2)), NULL);
2178 driver->draw2DRectangle(m_slotbordercolor,
2179 core::rect<s32>(v2s32(x2, y1),
2180 v2s32(x2 + border, y2)), NULL);
2188 item.takeItem(m_selected_amount);
2192 drawItemStack(driver, m_font, item,
2193 rect, &AbsoluteClippingRect, m_gamedef);
2197 std::string tooltip_text = "";
2198 if (hovering && !m_selected_item)
2199 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
2200 if (tooltip_text != "") {
2201 std::vector<std::string> tt_rows = str_split(tooltip_text, '\n');
2202 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2203 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2204 m_tooltip_element->setVisible(true);
2205 this->bringToFront(m_tooltip_element);
2206 m_tooltip_element->setText(utf8_to_wide(tooltip_text).c_str());
2207 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2208 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2209 v2u32 screenSize = driver->getScreenSize();
2210 int tooltip_offset_x = m_btn_height;
2211 int tooltip_offset_y = m_btn_height;
2213 tooltip_offset_x *= 3;
2214 tooltip_offset_y = 0;
2215 if (m_pointer.X > (s32)screenSize.X / 2)
2216 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2218 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2219 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2220 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2221 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2222 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2223 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2224 m_tooltip_element->setRelativePosition(core::rect<s32>(
2225 core::position2d<s32>(tooltip_x, tooltip_y),
2226 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2232 void GUIFormSpecMenu::drawSelectedItem()
2234 if(!m_selected_item)
2237 video::IVideoDriver* driver = Environment->getVideoDriver();
2239 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2241 InventoryList *list = inv->getList(m_selected_item->listname);
2243 ItemStack stack = list->getItem(m_selected_item->i);
2244 stack.count = m_selected_amount;
2246 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2247 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2248 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef);
2251 void GUIFormSpecMenu::drawMenu()
2254 std::string newform = m_form_src->getForm();
2255 if(newform != m_formspec_string){
2256 m_formspec_string = newform;
2257 regenerateGui(m_screensize_old);
2261 gui::IGUISkin* skin = Environment->getSkin();
2262 sanity_check(skin != NULL);
2263 gui::IGUIFont *old_font = skin->getFont();
2264 skin->setFont(m_font);
2266 updateSelectedItem();
2268 video::IVideoDriver* driver = Environment->getVideoDriver();
2270 v2u32 screenSize = driver->getScreenSize();
2271 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2273 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2275 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2277 m_tooltip_element->setVisible(false);
2282 for(u32 i=0; i<m_backgrounds.size(); i++)
2284 const ImageDrawSpec &spec = m_backgrounds[i];
2285 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2288 // Image size on screen
2289 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2290 // Image rectangle on screen
2291 core::rect<s32> rect = imgrect + spec.pos;
2293 if (m_clipbackground) {
2294 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2295 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2296 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2297 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2298 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2301 const video::SColor color(255,255,255,255);
2302 const video::SColor colors[] = {color,color,color,color};
2303 draw2DImageFilterScaled(driver, texture, rect,
2304 core::rect<s32>(core::position2d<s32>(0,0),
2305 core::dimension2di(texture->getOriginalSize())),
2306 NULL/*&AbsoluteClippingRect*/, colors, true);
2309 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2310 errorstream << "\t" << spec.name << std::endl;
2317 for(u32 i=0; i<m_boxes.size(); i++)
2319 const BoxDrawSpec &spec = m_boxes[i];
2321 irr::video::SColor todraw = spec.color;
2323 todraw.setAlpha(140);
2325 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2326 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2328 driver->draw2DRectangle(todraw, rect, 0);
2333 for(u32 i=0; i<m_images.size(); i++)
2335 const ImageDrawSpec &spec = m_images[i];
2336 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2339 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2340 // Image size on screen
2341 core::rect<s32> imgrect;
2344 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2347 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2349 // Image rectangle on screen
2350 core::rect<s32> rect = imgrect + spec.pos;
2351 const video::SColor color(255,255,255,255);
2352 const video::SColor colors[] = {color,color,color,color};
2353 draw2DImageFilterScaled(driver, texture, rect,
2354 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2355 NULL/*&AbsoluteClippingRect*/, colors, true);
2358 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2359 errorstream << "\t" << spec.name << std::endl;
2366 for(u32 i=0; i<m_itemimages.size(); i++)
2371 const ImageDrawSpec &spec = m_itemimages[i];
2372 IItemDefManager *idef = m_gamedef->idef();
2374 item.deSerialize(spec.name, idef);
2375 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
2376 // Image size on screen
2377 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2378 // Image rectangle on screen
2379 core::rect<s32> rect = imgrect + spec.pos;
2380 const video::SColor color(255,255,255,255);
2381 const video::SColor colors[] = {color,color,color,color};
2382 draw2DImageFilterScaled(driver, texture, rect,
2383 core::rect<s32>(core::position2d<s32>(0,0),
2384 core::dimension2di(texture->getOriginalSize())),
2385 NULL/*&AbsoluteClippingRect*/, colors, true);
2390 Phase 0: Item slot rectangles
2391 Phase 1: Item images; prepare tooltip
2394 for(int phase=start_phase; phase<=1; phase++)
2395 for(u32 i=0; i<m_inventorylists.size(); i++)
2397 drawList(m_inventorylists[i], phase);
2403 gui::IGUIElement::draw();
2405 /* TODO find way to show tooltips on touchscreen */
2406 #ifndef HAVE_TOUCHSCREENGUI
2407 m_pointer = m_device->getCursorControl()->getPosition();
2411 Draw fields/buttons tooltips
2413 gui::IGUIElement *hovered =
2414 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2416 if (hovered != NULL) {
2417 s32 id = hovered->getID();
2421 m_old_tooltip_id = id;
2424 if (id == m_old_tooltip_id) {
2425 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2427 m_hovered_time = getTimeMs();
2428 m_old_tooltip_id = id;
2432 if (id != -1 && delta >= m_tooltip_show_delay) {
2433 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2434 iter != m_fields.end(); iter++) {
2435 if ( (iter->fid == id) && (m_tooltips[iter->fname].tooltip != "") ){
2436 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2437 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2438 m_tooltip_element->setText(utf8_to_wide(m_tooltips[iter->fname].tooltip).c_str());
2439 std::vector<std::string> tt_rows = str_split(m_tooltips[iter->fname].tooltip, '\n');
2440 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2441 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2442 int tooltip_offset_x = m_btn_height;
2443 int tooltip_offset_y = m_btn_height;
2445 tooltip_offset_x *= 3;
2446 tooltip_offset_y = 0;
2447 if (m_pointer.X > (s32)screenSize.X / 2)
2448 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2450 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2451 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2452 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2453 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2454 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2455 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2456 m_tooltip_element->setRelativePosition(core::rect<s32>(
2457 core::position2d<s32>(tooltip_x, tooltip_y),
2458 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2460 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2461 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2462 m_tooltip_element->setVisible(true);
2463 this->bringToFront(m_tooltip_element);
2471 Draw dragged item stack
2475 skin->setFont(old_font);
2478 void GUIFormSpecMenu::updateSelectedItem()
2480 // If the selected stack has become empty for some reason, deselect it.
2481 // If the selected stack has become inaccessible, deselect it.
2482 // If the selected stack has become smaller, adjust m_selected_amount.
2483 ItemStack selected = verifySelectedItem();
2485 // WARNING: BLACK MAGIC
2486 // See if there is a stack suited for our current guess.
2487 // If such stack does not exist, clear the guess.
2488 if(m_selected_content_guess.name != "" &&
2489 selected.name == m_selected_content_guess.name &&
2490 selected.count == m_selected_content_guess.count){
2491 // Selected item fits the guess. Skip the black magic.
2493 else if(m_selected_content_guess.name != ""){
2495 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2496 const ListDrawSpec &s = m_inventorylists[i];
2497 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2500 InventoryList *list = inv->getList(s.listname);
2503 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2504 u32 item_i = i + s.start_item_i;
2505 if(item_i >= list->getSize())
2507 ItemStack stack = list->getItem(item_i);
2508 if(stack.name == m_selected_content_guess.name &&
2509 stack.count == m_selected_content_guess.count){
2511 infostream<<"Client: Changing selected content guess to "
2512 <<s.inventoryloc.dump()<<" "<<s.listname
2513 <<" "<<item_i<<std::endl;
2514 delete m_selected_item;
2515 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2516 m_selected_amount = stack.count;
2521 infostream<<"Client: Discarding selected content guess: "
2522 <<m_selected_content_guess.getItemString()<<std::endl;
2523 m_selected_content_guess.name = "";
2527 // If craftresult is nonempty and nothing else is selected, select it now.
2528 if(!m_selected_item)
2530 for(u32 i=0; i<m_inventorylists.size(); i++)
2532 const ListDrawSpec &s = m_inventorylists[i];
2533 if(s.listname == "craftpreview")
2535 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2536 InventoryList *list = inv->getList("craftresult");
2537 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2539 m_selected_item = new ItemSpec;
2540 m_selected_item->inventoryloc = s.inventoryloc;
2541 m_selected_item->listname = "craftresult";
2542 m_selected_item->i = 0;
2543 m_selected_amount = 0;
2544 m_selected_dragging = false;
2551 // If craftresult is selected, keep the whole stack selected
2552 if(m_selected_item && m_selected_item->listname == "craftresult")
2554 m_selected_amount = verifySelectedItem().count;
2558 ItemStack GUIFormSpecMenu::verifySelectedItem()
2560 // If the selected stack has become empty for some reason, deselect it.
2561 // If the selected stack has become inaccessible, deselect it.
2562 // If the selected stack has become smaller, adjust m_selected_amount.
2563 // Return the selected stack.
2567 if(m_selected_item->isValid())
2569 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2572 InventoryList *list = inv->getList(m_selected_item->listname);
2573 if(list && (u32) m_selected_item->i < list->getSize())
2575 ItemStack stack = list->getItem(m_selected_item->i);
2576 if(m_selected_amount > stack.count)
2577 m_selected_amount = stack.count;
2584 // selection was not valid
2585 delete m_selected_item;
2586 m_selected_item = NULL;
2587 m_selected_amount = 0;
2588 m_selected_dragging = false;
2593 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2599 if (quitmode == quit_mode_accept) {
2600 fields["quit"] = "true";
2603 if (quitmode == quit_mode_cancel) {
2604 fields["quit"] = "true";
2605 m_text_dst->gotText(fields);
2609 if (current_keys_pending.key_down) {
2610 fields["key_down"] = "true";
2611 current_keys_pending.key_down = false;
2614 if (current_keys_pending.key_up) {
2615 fields["key_up"] = "true";
2616 current_keys_pending.key_up = false;
2619 if (current_keys_pending.key_enter) {
2620 fields["key_enter"] = "true";
2621 current_keys_pending.key_enter = false;
2624 if (current_keys_pending.key_escape) {
2625 fields["key_escape"] = "true";
2626 current_keys_pending.key_escape = false;
2629 for(unsigned int i=0; i<m_fields.size(); i++) {
2630 const FieldSpec &s = m_fields[i];
2632 std::string name = s.fname;
2633 if (s.ftype == f_Button) {
2634 fields[name] = wide_to_utf8(s.flabel);
2635 } else if (s.ftype == f_Table) {
2636 GUITable *table = getTable(s.fname);
2638 fields[name] = table->checkEvent();
2641 else if(s.ftype == f_DropDown) {
2642 // no dynamic cast possible due to some distributions shipped
2643 // without rtti support in irrlicht
2644 IGUIElement * element = getElementFromId(s.fid);
2645 gui::IGUIComboBox *e = NULL;
2646 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2647 e = static_cast<gui::IGUIComboBox*>(element);
2649 s32 selected = e->getSelected();
2650 if (selected >= 0) {
2652 wide_to_utf8(e->getItem(selected));
2655 else if (s.ftype == f_TabHeader) {
2656 // no dynamic cast possible due to some distributions shipped
2657 // without rtti support in irrlicht
2658 IGUIElement * element = getElementFromId(s.fid);
2659 gui::IGUITabControl *e = NULL;
2660 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2661 e = static_cast<gui::IGUITabControl*>(element);
2665 std::stringstream ss;
2666 ss << (e->getActiveTab() +1);
2667 fields[name] = ss.str();
2670 else if (s.ftype == f_CheckBox) {
2671 // no dynamic cast possible due to some distributions shipped
2672 // without rtti support in irrlicht
2673 IGUIElement * element = getElementFromId(s.fid);
2674 gui::IGUICheckBox *e = NULL;
2675 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2676 e = static_cast<gui::IGUICheckBox*>(element);
2681 fields[name] = "true";
2683 fields[name] = "false";
2686 else if (s.ftype == f_ScrollBar) {
2687 // no dynamic cast possible due to some distributions shipped
2688 // without rtti support in irrlicht
2689 IGUIElement * element = getElementFromId(s.fid);
2690 gui::IGUIScrollBar *e = NULL;
2691 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2692 e = static_cast<gui::IGUIScrollBar*>(element);
2696 std::stringstream os;
2698 if (s.fdefault == L"Changed")
2699 fields[name] = "CHG:" + os.str();
2701 fields[name] = "VAL:" + os.str();
2706 IGUIElement* e = getElementFromId(s.fid);
2708 fields[name] = wide_to_utf8(e->getText());
2714 m_text_dst->gotText(fields);
2718 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2720 while(tocheck != NULL) {
2721 if (tocheck == parent) {
2724 tocheck = tocheck->getParent();
2729 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2731 // The IGUITabControl renders visually using the skin's selected
2732 // font, which we override for the duration of form drawing,
2733 // but computes tab hotspots based on how it would have rendered
2734 // using the font that is selected at the time of button release.
2735 // To make these two consistent, temporarily override the skin's
2736 // font while the IGUITabControl is processing the event.
2737 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2738 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2739 s32 x = event.MouseInput.X;
2740 s32 y = event.MouseInput.Y;
2741 gui::IGUIElement *hovered =
2742 Environment->getRootGUIElement()->getElementFromPoint(
2743 core::position2d<s32>(x, y));
2744 if (hovered && isMyChild(hovered) &&
2745 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2746 gui::IGUISkin* skin = Environment->getSkin();
2747 sanity_check(skin != NULL);
2748 gui::IGUIFont *old_font = skin->getFont();
2749 skin->setFont(m_font);
2750 bool retval = hovered->OnEvent(event);
2751 skin->setFont(old_font);
2756 // Fix Esc/Return key being eaten by checkboxen and tables
2757 if(event.EventType==EET_KEY_INPUT_EVENT) {
2758 KeyPress kp(event.KeyInput);
2759 if (kp == EscapeKey || kp == CancelKey
2760 || kp == getKeySetting("keymap_inventory")
2761 || event.KeyInput.Key==KEY_RETURN) {
2762 gui::IGUIElement *focused = Environment->getFocus();
2763 if (focused && isMyChild(focused) &&
2764 (focused->getType() == gui::EGUIET_LIST_BOX ||
2765 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2771 // Mouse wheel events: send to hovered element instead of focused
2772 if(event.EventType==EET_MOUSE_INPUT_EVENT
2773 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2774 s32 x = event.MouseInput.X;
2775 s32 y = event.MouseInput.Y;
2776 gui::IGUIElement *hovered =
2777 Environment->getRootGUIElement()->getElementFromPoint(
2778 core::position2d<s32>(x, y));
2779 if (hovered && isMyChild(hovered)) {
2780 hovered->OnEvent(event);
2785 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2786 s32 x = event.MouseInput.X;
2787 s32 y = event.MouseInput.Y;
2788 gui::IGUIElement *hovered =
2789 Environment->getRootGUIElement()->getElementFromPoint(
2790 core::position2d<s32>(x, y));
2791 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2792 m_old_tooltip_id = -1;
2795 if (!isChild(hovered,this)) {
2796 if (DoubleClickDetection(event)) {
2803 // display software keyboard when clicking edit boxes
2804 if (event.EventType == EET_MOUSE_INPUT_EVENT
2805 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2806 gui::IGUIElement *hovered =
2807 Environment->getRootGUIElement()->getElementFromPoint(
2808 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2809 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2810 bool retval = hovered->OnEvent(event);
2812 Environment->setFocus(hovered);
2814 m_JavaDialogFieldName = getNameByID(hovered->getID());
2815 std::string message = gettext("Enter ");
2816 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
2820 message += gettext(label) + ":";
2822 /* single line text input */
2825 /* multi line text input */
2826 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2830 /* passwords are always single line */
2831 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2835 porting::showInputDialog(gettext("ok"), "",
2836 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
2842 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2845 memset(&translated, 0, sizeof(SEvent));
2846 translated.EventType = EET_MOUSE_INPUT_EVENT;
2847 gui::IGUIElement* root = Environment->getRootGUIElement();
2851 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2855 gui::IGUIElement* hovered = root->getElementFromPoint(
2856 core::position2d<s32>(
2858 event.TouchInput.Y));
2860 translated.MouseInput.X = event.TouchInput.X;
2861 translated.MouseInput.Y = event.TouchInput.Y;
2862 translated.MouseInput.Control = false;
2864 bool dont_send_event = false;
2866 if (event.TouchInput.touchedCount == 1) {
2867 switch (event.TouchInput.Event) {
2868 case ETIE_PRESSED_DOWN:
2869 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2870 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2871 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2872 m_down_pos = m_pointer;
2875 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2876 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2877 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2880 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2881 translated.MouseInput.ButtonStates = 0;
2882 hovered = root->getElementFromPoint(m_down_pos);
2883 /* we don't have a valid pointer element use last
2884 * known pointer pos */
2885 translated.MouseInput.X = m_pointer.X;
2886 translated.MouseInput.Y = m_pointer.Y;
2888 /* reset down pos */
2889 m_down_pos = v2s32(0,0);
2892 dont_send_event = true;
2893 //this is not supposed to happen
2895 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2896 << event.TouchInput.Event << std::endl;
2898 } else if ( (event.TouchInput.touchedCount == 2) &&
2899 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2900 hovered = root->getElementFromPoint(m_down_pos);
2902 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2903 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2904 translated.MouseInput.X = m_pointer.X;
2905 translated.MouseInput.Y = m_pointer.Y;
2908 hovered->OnEvent(translated);
2911 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
2912 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2916 hovered->OnEvent(translated);
2918 dont_send_event = true;
2920 /* ignore unhandled 2 touch events ... accidental moving for example */
2921 else if (event.TouchInput.touchedCount == 2) {
2922 dont_send_event = true;
2924 else if (event.TouchInput.touchedCount > 2) {
2926 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
2927 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
2930 if (dont_send_event) {
2934 /* check if translated event needs to be preprocessed again */
2935 if (preprocessEvent(translated)) {
2940 bool retval = hovered->OnEvent(translated);
2942 if (event.TouchInput.Event == ETIE_LEFT_UP) {
2944 m_pointer = v2s32(0,0);
2955 /******************************************************************************/
2956 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
2958 /* The following code is for capturing double-clicks of the mouse button
2959 * and translating the double-click into an EET_KEY_INPUT_EVENT event
2960 * -- which closes the form -- under some circumstances.
2962 * There have been many github issues reporting this as a bug even though it
2963 * was an intended feature. For this reason, remapping the double-click as
2964 * an ESC must be explicitly set when creating this class via the
2965 * /p remap_dbl_click parameter of the constructor.
2968 if (!m_remap_dbl_click)
2971 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2972 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
2973 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
2975 m_doubleclickdetect[1].pos = m_pointer;
2976 m_doubleclickdetect[1].time = getTimeMs();
2978 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2979 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
2984 double squaredistance =
2985 m_doubleclickdetect[0].pos
2986 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
2988 if (squaredistance > (30*30)) {
2992 SEvent* translated = new SEvent();
2993 assert(translated != 0);
2994 //translate doubleclick to escape
2995 memset(translated, 0, sizeof(SEvent));
2996 translated->EventType = irr::EET_KEY_INPUT_EVENT;
2997 translated->KeyInput.Key = KEY_ESCAPE;
2998 translated->KeyInput.Control = false;
2999 translated->KeyInput.Shift = false;
3000 translated->KeyInput.PressedDown = true;
3001 translated->KeyInput.Char = 0;
3002 OnEvent(*translated);
3004 // no need to send the key up event as we're already deleted
3005 // and no one else did notice this event
3013 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3015 if(event.EventType==EET_KEY_INPUT_EVENT) {
3016 KeyPress kp(event.KeyInput);
3017 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3018 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3021 acceptInput(quit_mode_cancel);
3024 m_text_dst->gotText(L"MenuQuit");
3027 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3028 (kp == getKeySetting("keymap_screenshot"))) {
3029 m_client->makeScreenshot(m_device);
3031 if (event.KeyInput.PressedDown &&
3032 (event.KeyInput.Key==KEY_RETURN ||
3033 event.KeyInput.Key==KEY_UP ||
3034 event.KeyInput.Key==KEY_DOWN)
3036 switch (event.KeyInput.Key) {
3038 current_keys_pending.key_enter = true;
3041 current_keys_pending.key_up = true;
3044 current_keys_pending.key_down = true;
3048 //can't happen at all!
3049 FATAL_ERROR("Reached a source line that can't ever been reached");
3052 if (current_keys_pending.key_enter && m_allowclose) {
3053 acceptInput(quit_mode_accept);
3063 /* Mouse event other than movement, or crossing the border of inventory
3064 field while holding right mouse button
3066 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3067 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3068 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3069 event.MouseInput.isRightPressed() &&
3070 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3072 // Get selected item and hovered/clicked item (s)
3074 m_old_tooltip_id = -1;
3075 updateSelectedItem();
3076 ItemSpec s = getItemAtPos(m_pointer);
3078 Inventory *inv_selected = NULL;
3079 Inventory *inv_s = NULL;
3081 if(m_selected_item) {
3082 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3083 sanity_check(inv_selected);
3084 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3091 inv_s = m_invmgr->getInventory(s.inventoryloc);
3094 errorstream<<"InventoryMenu: The selected inventory location "
3095 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
3097 s.i = -1; // make it invalid again
3101 InventoryList *list = inv_s->getList(s.listname);
3103 verbosestream<<"InventoryMenu: The selected inventory list \""
3104 <<s.listname<<"\" does not exist"<<std::endl;
3105 s.i = -1; // make it invalid again
3109 if((u32)s.i >= list->getSize()) {
3110 infostream<<"InventoryMenu: The selected inventory list \""
3111 <<s.listname<<"\" is too small (i="<<s.i<<", size="
3112 <<list->getSize()<<")"<<std::endl;
3113 s.i = -1; // make it invalid again
3117 s_count = list->getItem(s.i).count;
3120 bool identical = (m_selected_item != NULL) && s.isValid() &&
3121 (inv_selected == inv_s) &&
3122 (m_selected_item->listname == s.listname) &&
3123 (m_selected_item->i == s.i);
3125 // buttons: 0 = left, 1 = right, 2 = middle
3126 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3129 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3130 { button = 0; updown = 0; }
3131 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3132 { button = 1; updown = 0; }
3133 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3134 { button = 2; updown = 0; }
3135 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3136 { button = 0; updown = 1; }
3137 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3138 { button = 1; updown = 1; }
3139 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3140 { button = 2; updown = 1; }
3141 else if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
3144 // Set this number to a positive value to generate a move action
3145 // from m_selected_item to s.
3146 u32 move_amount = 0;
3148 // Set this number to a positive value to generate a drop action
3149 // from m_selected_item.
3150 u32 drop_amount = 0;
3152 // Set this number to a positive value to generate a craft action at s.
3153 u32 craft_amount = 0;
3156 // Some mouse button has been pressed
3158 //infostream<<"Mouse button "<<button<<" pressed at p=("
3159 // <<p.X<<","<<p.Y<<")"<<std::endl;
3161 m_selected_dragging = false;
3163 if(s.isValid() && s.listname == "craftpreview") {
3164 // Craft preview has been clicked: craft
3165 craft_amount = (button == 2 ? 10 : 1);
3167 else if(m_selected_item == NULL) {
3169 // Non-empty stack has been clicked: select it
3170 m_selected_item = new ItemSpec(s);
3172 if(button == 1) // right
3173 m_selected_amount = (s_count + 1) / 2;
3174 else if(button == 2) // middle
3175 m_selected_amount = MYMIN(s_count, 10);
3177 m_selected_amount = s_count;
3179 m_selected_dragging = true;
3180 m_rmouse_auto_place = false;
3183 else { // m_selected_item != NULL
3184 assert(m_selected_amount >= 1);
3187 // Clicked a slot: move
3188 if(button == 1) // right
3190 else if(button == 2) // middle
3191 move_amount = MYMIN(m_selected_amount, 10);
3193 move_amount = m_selected_amount;
3196 if(move_amount >= m_selected_amount)
3197 m_selected_amount = 0;
3199 m_selected_amount -= move_amount;
3203 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3204 // Clicked outside of the window: drop
3205 if(button == 1) // right
3207 else if(button == 2) // middle
3208 drop_amount = MYMIN(m_selected_amount, 10);
3210 drop_amount = m_selected_amount;
3214 else if(updown == 1) {
3215 // Some mouse button has been released
3217 //infostream<<"Mouse button "<<button<<" released at p=("
3218 // <<p.X<<","<<p.Y<<")"<<std::endl;
3220 if(m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3222 // Dragged to different slot: move all selected
3223 move_amount = m_selected_amount;
3226 else if(m_selected_item != NULL && m_selected_dragging &&
3227 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3228 // Dragged outside of window: drop all selected
3229 drop_amount = m_selected_amount;
3232 m_selected_dragging = false;
3233 // Keep count of how many times right mouse button has been
3234 // clicked. One click is drag without dropping. Click + release
3235 // + click changes to drop one item when moved mode
3236 if(button == 1 && m_selected_item != NULL)
3237 m_rmouse_auto_place = !m_rmouse_auto_place;
3239 else if(updown == -1) {
3240 // Mouse has been moved and rmb is down and mouse pointer just
3241 // entered a new inventory field (checked in the entry-if, this
3242 // is the only action here that is generated by mouse movement)
3243 if(m_selected_item != NULL && s.isValid()){
3245 // TODO: middle mouse to move 10 items might be handy
3246 if (m_rmouse_auto_place) {
3247 // Only move an item if the destination slot is empty
3248 // or contains the same item type as what is going to be
3250 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3251 InventoryList *list_to = inv_s->getList(s.listname);
3252 assert(list_from && list_to);
3253 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3254 ItemStack stack_to = list_to->getItem(s.i);
3255 if (stack_to.empty() || stack_to.name == stack_from.name)
3261 // Possibly send inventory action to server
3264 // Send IACTION_MOVE
3266 assert(m_selected_item && m_selected_item->isValid());
3267 assert(s.isValid());
3269 assert(inv_selected && inv_s);
3270 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3271 InventoryList *list_to = inv_s->getList(s.listname);
3272 assert(list_from && list_to);
3273 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3274 ItemStack stack_to = list_to->getItem(s.i);
3276 // Check how many items can be moved
3277 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3278 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3279 // If source stack cannot be added to destination stack at all,
3281 if ((leftover.count == stack_from.count) &&
3282 (leftover.name == stack_from.name)) {
3283 m_selected_amount = stack_to.count;
3284 // In case the server doesn't directly swap them but instead
3285 // moves stack_to somewhere else, set this
3286 m_selected_content_guess = stack_to;
3287 m_selected_content_guess_inventory = s.inventoryloc;
3289 // Source stack goes fully into destination stack
3290 else if(leftover.empty()) {
3291 m_selected_amount -= move_amount;
3292 m_selected_content_guess = ItemStack(); // Clear
3294 // Source stack goes partly into destination stack
3296 move_amount -= leftover.count;
3297 m_selected_amount -= move_amount;
3298 m_selected_content_guess = ItemStack(); // Clear
3301 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
3302 IMoveAction *a = new IMoveAction();
3303 a->count = move_amount;
3304 a->from_inv = m_selected_item->inventoryloc;
3305 a->from_list = m_selected_item->listname;
3306 a->from_i = m_selected_item->i;
3307 a->to_inv = s.inventoryloc;
3308 a->to_list = s.listname;
3310 m_invmgr->inventoryAction(a);
3312 else if(drop_amount > 0) {
3313 m_selected_content_guess = ItemStack(); // Clear
3315 // Send IACTION_DROP
3317 assert(m_selected_item && m_selected_item->isValid());
3318 assert(inv_selected);
3319 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3321 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3323 // Check how many items can be dropped
3324 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3325 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3326 m_selected_amount -= drop_amount;
3328 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
3329 IDropAction *a = new IDropAction();
3330 a->count = drop_amount;
3331 a->from_inv = m_selected_item->inventoryloc;
3332 a->from_list = m_selected_item->listname;
3333 a->from_i = m_selected_item->i;
3334 m_invmgr->inventoryAction(a);
3336 else if(craft_amount > 0) {
3337 m_selected_content_guess = ItemStack(); // Clear
3339 // Send IACTION_CRAFT
3341 assert(s.isValid());
3344 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
3345 ICraftAction *a = new ICraftAction();
3346 a->count = craft_amount;
3347 a->craft_inv = s.inventoryloc;
3348 m_invmgr->inventoryAction(a);
3351 // If m_selected_amount has been decreased to zero, deselect
3352 if(m_selected_amount == 0) {
3353 delete m_selected_item;
3354 m_selected_item = NULL;
3355 m_selected_amount = 0;
3356 m_selected_dragging = false;
3357 m_selected_content_guess = ItemStack();
3359 m_old_pointer = m_pointer;
3361 if(event.EventType==EET_GUI_EVENT) {
3363 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
3365 // find the element that was clicked
3366 for(unsigned int i=0; i<m_fields.size(); i++) {
3367 FieldSpec &s = m_fields[i];
3368 if ((s.ftype == f_TabHeader) &&
3369 (s.fid == event.GUIEvent.Caller->getID())) {
3377 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
3379 if(!canTakeFocus(event.GUIEvent.Element)) {
3380 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3382 // Returning true disables focus change
3386 if((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3387 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3388 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3389 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3390 unsigned int btn_id = event.GUIEvent.Caller->getID();
3392 if (btn_id == 257) {
3394 acceptInput(quit_mode_accept);
3398 m_text_dst->gotText(L"ExitButton");
3400 // quitMenu deallocates menu
3404 // find the element that was clicked
3405 for(u32 i=0; i<m_fields.size(); i++) {
3406 FieldSpec &s = m_fields[i];
3407 // if its a button, set the send field so
3408 // lua knows which button was pressed
3409 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3410 (s.fid == event.GUIEvent.Caller->getID())) {
3414 acceptInput(quit_mode_accept);
3417 m_text_dst->gotText(L"ExitButton");
3421 acceptInput(quit_mode_no);
3426 else if ((s.ftype == f_DropDown) &&
3427 (s.fid == event.GUIEvent.Caller->getID())) {
3428 // only send the changed dropdown
3429 for(u32 i=0; i<m_fields.size(); i++) {
3430 FieldSpec &s2 = m_fields[i];
3431 if (s2.ftype == f_DropDown) {
3436 acceptInput(quit_mode_no);
3438 // revert configuration to make sure dropdowns are sent on
3439 // regular button click
3440 for(u32 i=0; i<m_fields.size(); i++) {
3441 FieldSpec &s2 = m_fields[i];
3442 if (s2.ftype == f_DropDown) {
3448 else if ((s.ftype == f_ScrollBar) &&
3449 (s.fid == event.GUIEvent.Caller->getID()))
3451 s.fdefault = L"Changed";
3452 acceptInput(quit_mode_no);
3458 if(event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3459 if(event.GUIEvent.Caller->getID() > 257) {
3462 acceptInput(quit_mode_accept);
3465 current_keys_pending.key_enter = true;
3468 // quitMenu deallocates menu
3473 if(event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3474 int current_id = event.GUIEvent.Caller->getID();
3475 if(current_id > 257) {
3476 // find the element that was clicked
3477 for(u32 i=0; i<m_fields.size(); i++) {
3478 FieldSpec &s = m_fields[i];
3479 // if it's a table, set the send field
3480 // so lua knows which table was changed
3481 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3492 return Parent ? Parent->OnEvent(event) : false;
3496 * get name of element by element id
3497 * @param id of element
3498 * @return name string or empty string
3500 std::string GUIFormSpecMenu::getNameByID(s32 id)
3502 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3503 iter != m_fields.end(); iter++) {
3504 if (iter->fid == id) {
3512 * get label of element by id
3513 * @param id of element
3514 * @return label string or empty string
3516 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3518 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3519 iter != m_fields.end(); iter++) {
3520 if (iter->fid == id) {
3521 return iter->flabel;