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"
50 #include "fontengine.h"
52 #include "util/numeric.h"
53 #include "util/string.h" // for parseColorString()
55 #define MY_CHECKPOS(a,b) \
56 if (v_pos.size() != 2) { \
57 errorstream<< "Invalid pos for element " << a << "specified: \"" \
58 << parts[b] << "\"" << std::endl; \
62 #define MY_CHECKGEOM(a,b) \
63 if (v_geom.size() != 2) { \
64 errorstream<< "Invalid pos for element " << a << "specified: \"" \
65 << parts[b] << "\"" << std::endl; \
71 static unsigned int font_line_height(gui::IGUIFont *font)
73 return font->getDimension(L"Ay").Height + font->getKerningHeight();
76 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
77 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
78 InventoryManager *invmgr, IGameDef *gamedef,
79 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
80 Client* client, bool remap_dbl_click) :
81 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
87 m_selected_item(NULL),
89 m_selected_dragging(false),
90 m_tooltip_element(NULL),
93 m_rmouse_auto_place(false),
98 m_formspec_version(0),
99 m_focused_element(L""),
101 m_remap_dbl_click(remap_dbl_click)
103 ,m_JavaDialogFieldName(L"")
106 current_keys_pending.key_down = false;
107 current_keys_pending.key_up = false;
108 current_keys_pending.key_enter = false;
109 current_keys_pending.key_escape = false;
111 m_doubleclickdetect[0].time = 0;
112 m_doubleclickdetect[1].time = 0;
114 m_doubleclickdetect[0].pos = v2s32(0, 0);
115 m_doubleclickdetect[1].pos = v2s32(0, 0);
117 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
120 GUIFormSpecMenu::~GUIFormSpecMenu()
124 for (u32 i = 0; i < m_tables.size(); ++i) {
125 GUITable *table = m_tables[i].second;
129 delete m_selected_item;
131 if (m_form_src != NULL) {
134 if (m_text_dst != NULL) {
139 void GUIFormSpecMenu::removeChildren()
141 const core::list<gui::IGUIElement*> &children = getChildren();
143 while(!children.empty()) {
144 (*children.getLast())->remove();
147 if(m_tooltip_element) {
148 m_tooltip_element->remove();
149 m_tooltip_element->drop();
150 m_tooltip_element = NULL;
155 void GUIFormSpecMenu::setInitialFocus()
157 // Set initial focus according to following order of precedence:
158 // 1. first empty editbox
162 // 5. first focusable (not statictext, not tabheader)
163 // 6. first child element
165 core::list<gui::IGUIElement*> children = getChildren();
167 // in case "children" contains any NULL elements, remove them
168 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
169 it != children.end();) {
173 it = children.erase(it);
176 // 1. first empty editbox
177 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
178 it != children.end(); ++it) {
179 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
180 && (*it)->getText()[0] == 0) {
181 Environment->setFocus(*it);
187 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
188 it != children.end(); ++it) {
189 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
190 Environment->setFocus(*it);
196 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
197 it != children.end(); ++it) {
198 if ((*it)->getTypeName() == std::string("GUITable")) {
199 Environment->setFocus(*it);
205 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
206 it != children.end(); --it) {
207 if ((*it)->getType() == gui::EGUIET_BUTTON) {
208 Environment->setFocus(*it);
213 // 5. first focusable (not statictext, not tabheader)
214 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
215 it != children.end(); ++it) {
216 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
217 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
218 Environment->setFocus(*it);
223 // 6. first child element
224 if (children.empty())
225 Environment->setFocus(this);
227 Environment->setFocus(*(children.begin()));
230 GUITable* GUIFormSpecMenu::getTable(std::wstring tablename)
232 for (u32 i = 0; i < m_tables.size(); ++i) {
233 if (tablename == m_tables[i].first.fname)
234 return m_tables[i].second;
239 std::vector<std::string> split(const std::string &s, char delim) {
240 std::vector<std::string> tokens;
242 std::string current = "";
243 bool last_was_escape = false;
244 for(unsigned int i=0; i < s.size(); i++) {
245 if (last_was_escape) {
247 current += s.c_str()[i];
248 last_was_escape = false;
251 if (s.c_str()[i] == delim) {
252 tokens.push_back(current);
254 last_was_escape = false;
256 else if (s.c_str()[i] == '\\'){
257 last_was_escape = true;
260 current += s.c_str()[i];
261 last_was_escape = false;
266 tokens.push_back(current);
271 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
273 std::vector<std::string> parts = split(element,',');
275 if (((parts.size() == 2) || parts.size() == 3) ||
276 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
278 if (parts[1].find(';') != std::string::npos)
279 parts[1] = parts[1].substr(0,parts[1].find(';'));
281 data->invsize.X = MYMAX(0, stof(parts[0]));
282 data->invsize.Y = MYMAX(0, stof(parts[1]));
285 if (parts.size() == 3) {
286 if (parts[2] == "true") {
287 lockSize(true,v2u32(800,600));
291 data->explicit_size = true;
294 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
297 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
299 if (m_gamedef == 0) {
300 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
304 std::vector<std::string> parts = split(element,';');
306 if (((parts.size() == 4) || (parts.size() == 5)) ||
307 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
309 std::string location = parts[0];
310 std::string listname = parts[1];
311 std::vector<std::string> v_pos = split(parts[2],',');
312 std::vector<std::string> v_geom = split(parts[3],',');
313 std::string startindex = "";
314 if (parts.size() == 5)
315 startindex = parts[4];
317 MY_CHECKPOS("list",2);
318 MY_CHECKGEOM("list",3);
320 InventoryLocation loc;
322 if(location == "context" || location == "current_name")
323 loc = m_current_inventory_location;
325 loc.deSerialize(location);
327 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
328 pos.X += stof(v_pos[0]) * (float)spacing.X;
329 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
332 geom.X = stoi(v_geom[0]);
333 geom.Y = stoi(v_geom[1]);
337 start_i = stoi(startindex);
339 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
340 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
344 if(!data->explicit_size)
345 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
346 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
349 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
352 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
354 std::vector<std::string> parts = split(element,';');
356 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
357 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
359 std::vector<std::string> v_pos = split(parts[0],',');
360 std::string name = parts[1];
361 std::string label = parts[2];
362 std::string selected = "";
364 if (parts.size() >= 4)
367 MY_CHECKPOS("checkbox",0);
370 pos.X += stof(v_pos[0]) * (float) spacing.X;
371 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
373 bool fselected = false;
375 if (selected == "true")
378 std::wstring wlabel = narrow_to_wide(label);
380 core::rect<s32> rect = core::rect<s32>(
381 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
382 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
383 pos.Y + ((imgsize.Y/2) + m_btn_height));
386 narrow_to_wide(name),
387 wlabel, //Needed for displaying text on MSVC
392 spec.ftype = f_CheckBox;
394 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
395 spec.fid, spec.flabel.c_str());
397 if (spec.fname == data->focused_fieldname) {
398 Environment->setFocus(e);
401 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
402 m_fields.push_back(spec);
405 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
408 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
410 std::vector<std::string> parts = split(element,';');
412 if (parts.size() >= 5) {
413 std::vector<std::string> v_pos = split(parts[0],',');
414 std::vector<std::string> v_dim = split(parts[1],',');
415 std::string name = parts[2];
416 std::string value = parts[4];
418 MY_CHECKPOS("scrollbar",0);
421 pos.X += stof(v_pos[0]) * (float) spacing.X;
422 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
424 if (v_dim.size() != 2) {
425 errorstream<< "Invalid size for element " << "scrollbar"
426 << "specified: \"" << parts[1] << "\"" << std::endl;
431 dim.X = stof(v_dim[0]) * (float) spacing.X;
432 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
434 core::rect<s32> rect =
435 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
438 narrow_to_wide(name),
444 bool is_horizontal = true;
446 if (parts[2] == "vertical")
447 is_horizontal = false;
449 spec.ftype = f_ScrollBar;
451 gui::IGUIScrollBar* e =
452 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
456 e->setPos(stoi(parts[4]));
458 e->setLargeStep(100);
460 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
461 m_fields.push_back(spec);
464 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
467 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
469 std::vector<std::string> parts = split(element,';');
471 if ((parts.size() == 3) ||
472 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
474 std::vector<std::string> v_pos = split(parts[0],',');
475 std::vector<std::string> v_geom = split(parts[1],',');
476 std::string name = unescape_string(parts[2]);
478 MY_CHECKPOS("image",0);
479 MY_CHECKGEOM("image",1);
481 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
482 pos.X += stof(v_pos[0]) * (float) spacing.X;
483 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
486 geom.X = stof(v_geom[0]) * (float)imgsize.X;
487 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
489 if(!data->explicit_size)
490 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
491 m_images.push_back(ImageDrawSpec(name, pos, geom));
495 if (parts.size() == 2) {
496 std::vector<std::string> v_pos = split(parts[0],',');
497 std::string name = unescape_string(parts[1]);
499 MY_CHECKPOS("image",0);
501 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
502 pos.X += stof(v_pos[0]) * (float) spacing.X;
503 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
505 if(!data->explicit_size)
506 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
507 m_images.push_back(ImageDrawSpec(name, pos));
510 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
513 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
515 std::vector<std::string> parts = split(element,';');
517 if ((parts.size() == 3) ||
518 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
520 std::vector<std::string> v_pos = split(parts[0],',');
521 std::vector<std::string> v_geom = split(parts[1],',');
522 std::string name = parts[2];
524 MY_CHECKPOS("itemimage",0);
525 MY_CHECKGEOM("itemimage",1);
527 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
528 pos.X += stof(v_pos[0]) * (float) spacing.X;
529 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
532 geom.X = stof(v_geom[0]) * (float)imgsize.X;
533 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
535 if(!data->explicit_size)
536 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
537 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
540 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
543 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
546 std::vector<std::string> parts = split(element,';');
548 if ((parts.size() == 4) ||
549 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
551 std::vector<std::string> v_pos = split(parts[0],',');
552 std::vector<std::string> v_geom = split(parts[1],',');
553 std::string name = parts[2];
554 std::string label = parts[3];
556 MY_CHECKPOS("button",0);
557 MY_CHECKGEOM("button",1);
560 pos.X += stof(v_pos[0]) * (float)spacing.X;
561 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
564 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
565 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
567 core::rect<s32> rect =
568 core::rect<s32>(pos.X, pos.Y - m_btn_height,
569 pos.X + geom.X, pos.Y + m_btn_height);
571 if(!data->explicit_size)
572 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
574 label = unescape_string(label);
576 std::wstring wlabel = narrow_to_wide(label);
579 narrow_to_wide(name),
584 spec.ftype = f_Button;
585 if(type == "button_exit")
587 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
588 spec.flabel.c_str());
590 if (spec.fname == data->focused_fieldname) {
591 Environment->setFocus(e);
594 m_fields.push_back(spec);
597 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
600 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
602 std::vector<std::string> parts = split(element,';');
604 if (((parts.size() == 3) || (parts.size() == 4)) ||
605 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
607 std::vector<std::string> v_pos = split(parts[0],',');
608 std::vector<std::string> v_geom = split(parts[1],',');
609 std::string name = unescape_string(parts[2]);
611 MY_CHECKPOS("background",0);
612 MY_CHECKGEOM("background",1);
614 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
615 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
616 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
619 geom.X = stof(v_geom[0]) * (float)spacing.X;
620 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
622 if (parts.size() == 4) {
623 m_clipbackground = is_yes(parts[3]);
624 if (m_clipbackground) {
625 pos.X = stoi(v_pos[0]); //acts as offset
626 pos.Y = stoi(v_pos[1]); //acts as offset
630 if(!data->explicit_size)
631 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
632 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
635 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
638 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
640 std::vector<std::string> parts = split(element,';');
642 data->table_options.clear();
643 for (size_t i = 0; i < parts.size(); ++i) {
644 // Parse table option
645 std::string opt = unescape_string(parts[i]);
646 data->table_options.push_back(GUITable::splitOption(opt));
650 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
652 std::vector<std::string> parts = split(element,';');
654 data->table_columns.clear();
655 for (size_t i = 0; i < parts.size(); ++i) {
656 std::vector<std::string> col_parts = split(parts[i],',');
657 GUITable::TableColumn column;
659 if (!col_parts.empty())
660 column.type = col_parts[0];
661 // Parse column options
662 for (size_t j = 1; j < col_parts.size(); ++j) {
663 std::string opt = unescape_string(col_parts[j]);
664 column.options.push_back(GUITable::splitOption(opt));
666 data->table_columns.push_back(column);
670 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
672 std::vector<std::string> parts = split(element,';');
674 if (((parts.size() == 4) || (parts.size() == 5)) ||
675 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
677 std::vector<std::string> v_pos = split(parts[0],',');
678 std::vector<std::string> v_geom = split(parts[1],',');
679 std::string name = parts[2];
680 std::vector<std::string> items = split(parts[3],',');
681 std::string str_initial_selection = "";
682 std::string str_transparent = "false";
684 if (parts.size() >= 5)
685 str_initial_selection = parts[4];
687 MY_CHECKPOS("table",0);
688 MY_CHECKGEOM("table",1);
691 pos.X += stof(v_pos[0]) * (float)spacing.X;
692 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
695 geom.X = stof(v_geom[0]) * (float)spacing.X;
696 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
699 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
701 std::wstring fname_w = narrow_to_wide(name);
710 spec.ftype = f_Table;
712 for (unsigned int i = 0; i < items.size(); ++i) {
713 items[i] = unescape_string(items[i]);
716 //now really show table
717 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
720 if (spec.fname == data->focused_fieldname) {
721 Environment->setFocus(e);
724 e->setTable(data->table_options, data->table_columns, items);
726 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
727 e->setDynamicData(data->table_dyndata[fname_w]);
730 if ((str_initial_selection != "") &&
731 (str_initial_selection != "0"))
732 e->setSelected(stoi(str_initial_selection.c_str()));
734 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
735 m_fields.push_back(spec);
738 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
741 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
743 std::vector<std::string> parts = split(element,';');
745 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
746 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
748 std::vector<std::string> v_pos = split(parts[0],',');
749 std::vector<std::string> v_geom = split(parts[1],',');
750 std::string name = parts[2];
751 std::vector<std::string> items = split(parts[3],',');
752 std::string str_initial_selection = "";
753 std::string str_transparent = "false";
755 if (parts.size() >= 5)
756 str_initial_selection = parts[4];
758 if (parts.size() >= 6)
759 str_transparent = parts[5];
761 MY_CHECKPOS("textlist",0);
762 MY_CHECKGEOM("textlist",1);
765 pos.X += stof(v_pos[0]) * (float)spacing.X;
766 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
769 geom.X = stof(v_geom[0]) * (float)spacing.X;
770 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
773 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
775 std::wstring fname_w = narrow_to_wide(name);
784 spec.ftype = f_Table;
786 for (unsigned int i = 0; i < items.size(); ++i) {
787 items[i] = unescape_string(items[i]);
790 //now really show list
791 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
794 if (spec.fname == data->focused_fieldname) {
795 Environment->setFocus(e);
798 e->setTextList(items, is_yes(str_transparent));
800 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
801 e->setDynamicData(data->table_dyndata[fname_w]);
804 if ((str_initial_selection != "") &&
805 (str_initial_selection != "0"))
806 e->setSelected(stoi(str_initial_selection.c_str()));
808 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
809 m_fields.push_back(spec);
812 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
816 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
818 std::vector<std::string> parts = split(element,';');
820 if ((parts.size() == 5) ||
821 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
823 std::vector<std::string> v_pos = split(parts[0],',');
824 std::string name = parts[2];
825 std::vector<std::string> items = split(parts[3],',');
826 std::string str_initial_selection = "";
827 str_initial_selection = parts[4];
829 MY_CHECKPOS("dropdown",0);
832 pos.X += stof(v_pos[0]) * (float)spacing.X;
833 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
835 s32 width = stof(parts[1]) * (float)spacing.Y;
837 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
838 pos.X + width, pos.Y + (m_btn_height * 2));
840 std::wstring fname_w = narrow_to_wide(name);
849 spec.ftype = f_DropDown;
852 //now really show list
853 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
855 if (spec.fname == data->focused_fieldname) {
856 Environment->setFocus(e);
859 for (unsigned int i=0; i < items.size(); i++) {
860 e->addItem(narrow_to_wide(items[i]).c_str());
863 if (str_initial_selection != "")
864 e->setSelected(stoi(str_initial_selection.c_str())-1);
866 m_fields.push_back(spec);
869 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
870 << element << "'" << std::endl;
873 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
875 std::vector<std::string> parts = split(element,';');
877 if ((parts.size() == 4) ||
878 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
880 std::vector<std::string> v_pos = split(parts[0],',');
881 std::vector<std::string> v_geom = split(parts[1],',');
882 std::string name = parts[2];
883 std::string label = parts[3];
885 MY_CHECKPOS("pwdfield",0);
886 MY_CHECKGEOM("pwdfield",1);
889 pos.X += stof(v_pos[0]) * (float)spacing.X;
890 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
893 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
895 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
896 pos.Y -= m_btn_height;
897 geom.Y = m_btn_height*2;
899 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
901 label = unescape_string(label);
903 std::wstring wlabel = narrow_to_wide(label);
906 narrow_to_wide(name),
913 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
915 if (spec.fname == data->focused_fieldname) {
916 Environment->setFocus(e);
919 if (label.length() >= 1)
921 int font_height = g_fontengine->getTextHeight();
922 rect.UpperLeftCorner.Y -= font_height;
923 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
924 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
927 e->setPasswordBox(true,L'*');
930 evt.EventType = EET_KEY_INPUT_EVENT;
931 evt.KeyInput.Key = KEY_END;
932 evt.KeyInput.Char = 0;
933 evt.KeyInput.Control = 0;
934 evt.KeyInput.Shift = 0;
935 evt.KeyInput.PressedDown = true;
937 m_fields.push_back(spec);
940 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
943 void GUIFormSpecMenu::parseSimpleField(parserData* data,
944 std::vector<std::string> &parts)
946 std::string name = parts[0];
947 std::string label = parts[1];
948 std::string default_val = parts[2];
950 core::rect<s32> rect;
952 if(data->explicit_size)
953 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
955 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
956 pos.Y = ((m_fields.size()+2)*60);
957 v2s32 size = DesiredRect.getSize();
959 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
960 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
964 default_val = m_form_src->resolveText(default_val);
966 default_val = unescape_string(default_val);
967 label = unescape_string(label);
969 std::wstring wlabel = narrow_to_wide(label);
972 narrow_to_wide(name),
974 narrow_to_wide(default_val),
980 // spec field id to 0, this stops submit searching for a value that isn't there
981 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
986 gui::IGUIEditBox *e =
987 Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
989 if (spec.fname == data->focused_fieldname) {
990 Environment->setFocus(e);
994 evt.EventType = EET_KEY_INPUT_EVENT;
995 evt.KeyInput.Key = KEY_END;
996 evt.KeyInput.Char = 0;
997 evt.KeyInput.Control = 0;
998 evt.KeyInput.Shift = 0;
999 evt.KeyInput.PressedDown = true;
1002 if (label.length() >= 1)
1004 int font_height = g_fontengine->getTextHeight();
1005 rect.UpperLeftCorner.Y -= font_height;
1006 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1007 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1011 m_fields.push_back(spec);
1014 void GUIFormSpecMenu::parseTextArea(parserData* data,
1015 std::vector<std::string>& parts,std::string type)
1018 std::vector<std::string> v_pos = split(parts[0],',');
1019 std::vector<std::string> v_geom = split(parts[1],',');
1020 std::string name = parts[2];
1021 std::string label = parts[3];
1022 std::string default_val = parts[4];
1024 MY_CHECKPOS(type,0);
1025 MY_CHECKGEOM(type,1);
1028 pos.X = stof(v_pos[0]) * (float) spacing.X;
1029 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1033 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1035 if (type == "textarea")
1037 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1038 pos.Y += m_btn_height;
1042 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1043 pos.Y -= m_btn_height;
1044 geom.Y = m_btn_height*2;
1047 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1049 if(!data->explicit_size)
1050 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1053 default_val = m_form_src->resolveText(default_val);
1056 default_val = unescape_string(default_val);
1057 label = unescape_string(label);
1059 std::wstring wlabel = narrow_to_wide(label);
1062 narrow_to_wide(name),
1064 narrow_to_wide(default_val),
1070 // spec field id to 0, this stops submit searching for a value that isn't there
1071 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1076 gui::IGUIEditBox *e =
1077 Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1079 if (spec.fname == data->focused_fieldname) {
1080 Environment->setFocus(e);
1083 if (type == "textarea")
1085 e->setMultiLine(true);
1086 e->setWordWrap(true);
1087 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1090 evt.EventType = EET_KEY_INPUT_EVENT;
1091 evt.KeyInput.Key = KEY_END;
1092 evt.KeyInput.Char = 0;
1093 evt.KeyInput.Control = 0;
1094 evt.KeyInput.Shift = 0;
1095 evt.KeyInput.PressedDown = true;
1099 if (label.length() >= 1)
1101 int font_height = g_fontengine->getTextHeight();
1102 rect.UpperLeftCorner.Y -= font_height;
1103 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1104 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1107 m_fields.push_back(spec);
1110 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1113 std::vector<std::string> parts = split(element,';');
1115 if (parts.size() == 3 || parts.size() == 4) {
1116 parseSimpleField(data,parts);
1120 if ((parts.size() == 5) ||
1121 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1123 parseTextArea(data,parts,type);
1126 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1129 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1131 std::vector<std::string> parts = split(element,';');
1133 if ((parts.size() == 2) ||
1134 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1136 std::vector<std::string> v_pos = split(parts[0],',');
1137 std::string text = parts[1];
1139 MY_CHECKPOS("label",0);
1141 v2s32 pos = padding;
1142 pos.X += stof(v_pos[0]) * (float)spacing.X;
1143 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1145 if(!data->explicit_size)
1146 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1148 text = unescape_string(text);
1149 std::vector<std::string> lines = split(text, '\n');
1151 for (unsigned int i = 0; i != lines.size(); i++) {
1152 // Lines are spaced at the nominal distance of
1153 // 2/5 inventory slot, even if the font doesn't
1154 // quite match that. This provides consistent
1155 // form layout, at the expense of sometimes
1156 // having sub-optimal spacing for the font.
1157 // We multiply by 2 and then divide by 5, rather
1158 // than multiply by 0.4, to get exact results
1159 // in the integer cases: 0.4 is not exactly
1160 // representable in binary floating point.
1161 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1162 std::wstring wlabel = narrow_to_wide(lines[i]);
1163 core::rect<s32> rect = core::rect<s32>(
1164 pos.X, posy - m_btn_height,
1165 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1166 posy + m_btn_height);
1173 gui::IGUIStaticText *e =
1174 Environment->addStaticText(spec.flabel.c_str(),
1175 rect, false, false, this, spec.fid);
1176 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1178 m_fields.push_back(spec);
1183 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1186 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1188 std::vector<std::string> parts = split(element,';');
1190 if ((parts.size() == 2) ||
1191 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1193 std::vector<std::string> v_pos = split(parts[0],',');
1194 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1196 MY_CHECKPOS("vertlabel",1);
1198 v2s32 pos = padding;
1199 pos.X += stof(v_pos[0]) * (float)spacing.X;
1200 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1202 core::rect<s32> rect = core::rect<s32>(
1203 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1205 font_line_height(m_font)
1207 +((imgsize.Y/2)- m_btn_height));
1208 //actually text.length() would be correct but adding +1 avoids to break all mods
1210 if(!data->explicit_size)
1211 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1213 std::wstring label = L"";
1215 for (unsigned int i=0; i < text.length(); i++) {
1226 gui::IGUIStaticText *t =
1227 Environment->addStaticText(spec.flabel.c_str(), rect, false, false, this, spec.fid);
1228 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1229 m_fields.push_back(spec);
1232 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1235 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1238 std::vector<std::string> parts = split(element,';');
1240 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1241 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1243 std::vector<std::string> v_pos = split(parts[0],',');
1244 std::vector<std::string> v_geom = split(parts[1],',');
1245 std::string image_name = parts[2];
1246 std::string name = parts[3];
1247 std::string label = parts[4];
1249 MY_CHECKPOS("imagebutton",0);
1250 MY_CHECKGEOM("imagebutton",1);
1252 v2s32 pos = padding;
1253 pos.X += stof(v_pos[0]) * (float)spacing.X;
1254 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1256 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1257 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1259 bool noclip = false;
1260 bool drawborder = true;
1261 std::string pressed_image_name = "";
1263 if (parts.size() >= 7) {
1264 if (parts[5] == "true")
1266 if (parts[6] == "false")
1270 if (parts.size() >= 8) {
1271 pressed_image_name = parts[7];
1274 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1276 if(!data->explicit_size)
1277 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1279 image_name = unescape_string(image_name);
1280 pressed_image_name = unescape_string(pressed_image_name);
1281 label = unescape_string(label);
1283 std::wstring wlabel = narrow_to_wide(label);
1286 narrow_to_wide(name),
1288 narrow_to_wide(image_name),
1291 spec.ftype = f_Button;
1292 if(type == "image_button_exit")
1293 spec.is_exit = true;
1295 video::ITexture *texture = 0;
1296 video::ITexture *pressed_texture = 0;
1297 texture = m_tsrc->getTexture(image_name);
1298 if (pressed_image_name != "")
1299 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1301 pressed_texture = texture;
1303 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1305 if (spec.fname == data->focused_fieldname) {
1306 Environment->setFocus(e);
1309 e->setUseAlphaChannel(true);
1310 e->setImage(texture);
1311 e->setPressedImage(pressed_texture);
1312 e->setScaleImage(true);
1313 e->setNotClipped(noclip);
1314 e->setDrawBorder(drawborder);
1316 m_fields.push_back(spec);
1320 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1323 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1325 std::vector<std::string> parts = split(element,';');
1327 if (((parts.size() == 4) || (parts.size() == 6)) ||
1328 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1330 std::vector<std::string> v_pos = split(parts[0],',');
1331 std::string name = parts[1];
1332 std::vector<std::string> buttons = split(parts[2],',');
1333 std::string str_index = parts[3];
1334 bool show_background = true;
1335 bool show_border = true;
1336 int tab_index = stoi(str_index) -1;
1338 MY_CHECKPOS("tabheader",0);
1340 if (parts.size() == 6) {
1341 if (parts[4] == "true")
1342 show_background = false;
1343 if (parts[5] == "false")
1344 show_border = false;
1348 narrow_to_wide(name),
1354 spec.ftype = f_TabHeader;
1357 pos.X += stof(v_pos[0]) * (float)spacing.X;
1358 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1360 geom.X = DesiredRect.getWidth();
1361 geom.Y = m_btn_height*2;
1363 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1366 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1367 show_background, show_border, spec.fid);
1368 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1369 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1370 e->setTabHeight(m_btn_height*2);
1372 if (spec.fname == data->focused_fieldname) {
1373 Environment->setFocus(e);
1376 e->setNotClipped(true);
1378 for (unsigned int i=0; i< buttons.size(); i++) {
1379 e->addTab(narrow_to_wide(buttons[i]).c_str(), -1);
1382 if ((tab_index >= 0) &&
1383 (buttons.size() < INT_MAX) &&
1384 (tab_index < (int) buttons.size()))
1385 e->setActiveTab(tab_index);
1387 m_fields.push_back(spec);
1390 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1391 << element << "'" << std::endl;
1394 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1397 if (m_gamedef == 0) {
1399 "WARNING: invalid use of item_image_button with m_gamedef==0"
1404 std::vector<std::string> parts = split(element,';');
1406 if ((parts.size() == 5) ||
1407 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1409 std::vector<std::string> v_pos = split(parts[0],',');
1410 std::vector<std::string> v_geom = split(parts[1],',');
1411 std::string item_name = parts[2];
1412 std::string name = parts[3];
1413 std::string label = parts[4];
1415 MY_CHECKPOS("itemimagebutton",0);
1416 MY_CHECKGEOM("itemimagebutton",1);
1418 v2s32 pos = padding;
1419 pos.X += stof(v_pos[0]) * (float)spacing.X;
1420 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1422 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1423 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1425 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1427 if(!data->explicit_size)
1428 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1430 IItemDefManager *idef = m_gamedef->idef();
1432 item.deSerialize(item_name, idef);
1433 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1435 m_tooltips[narrow_to_wide(name)] =
1436 TooltipSpec(item.getDefinition(idef).description,
1437 m_default_tooltip_bgcolor,
1438 m_default_tooltip_color);
1440 label = unescape_string(label);
1442 narrow_to_wide(name),
1443 narrow_to_wide(label),
1444 narrow_to_wide(item_name),
1448 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1450 if (spec.fname == data->focused_fieldname) {
1451 Environment->setFocus(e);
1454 e->setUseAlphaChannel(true);
1455 e->setImage(texture);
1456 e->setPressedImage(texture);
1457 e->setScaleImage(true);
1458 spec.ftype = f_Button;
1459 rect+=data->basepos-padding;
1461 m_fields.push_back(spec);
1464 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1467 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1469 std::vector<std::string> parts = split(element,';');
1471 if ((parts.size() == 3) ||
1472 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1474 std::vector<std::string> v_pos = split(parts[0],',');
1475 std::vector<std::string> v_geom = split(parts[1],',');
1477 MY_CHECKPOS("box",0);
1478 MY_CHECKGEOM("box",1);
1480 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1481 pos.X += stof(v_pos[0]) * (float) spacing.X;
1482 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1485 geom.X = stof(v_geom[0]) * (float)spacing.X;
1486 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1488 video::SColor tmp_color;
1490 if (parseColorString(parts[2], tmp_color, false)) {
1491 BoxDrawSpec spec(pos, geom, tmp_color);
1493 m_boxes.push_back(spec);
1496 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1500 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1503 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1505 std::vector<std::string> parts = split(element,';');
1507 if (((parts.size() == 1) || (parts.size() == 2)) ||
1508 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1510 parseColorString(parts[0],m_bgcolor,false);
1512 if (parts.size() == 2) {
1513 std::string fullscreen = parts[1];
1514 m_bgfullscreen = is_yes(fullscreen);
1518 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1521 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1523 std::vector<std::string> parts = split(element,';');
1525 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1526 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1528 parseColorString(parts[0], m_slotbg_n, false);
1529 parseColorString(parts[1], m_slotbg_h, false);
1531 if (parts.size() >= 3) {
1532 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1533 m_slotborder = true;
1536 if (parts.size() == 5) {
1537 video::SColor tmp_color;
1539 if (parseColorString(parts[3], tmp_color, false))
1540 m_default_tooltip_bgcolor = tmp_color;
1541 if (parseColorString(parts[4], tmp_color, false))
1542 m_default_tooltip_color = tmp_color;
1546 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1549 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1551 std::vector<std::string> parts = split(element,';');
1552 if (parts.size() == 2) {
1553 std::string name = parts[0];
1554 m_tooltips[narrow_to_wide(name)] = TooltipSpec(unescape_string(parts[1]),
1555 m_default_tooltip_bgcolor, m_default_tooltip_color);
1557 } else if (parts.size() == 4) {
1558 std::string name = parts[0];
1559 video::SColor tmp_color1, tmp_color2;
1560 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1561 m_tooltips[narrow_to_wide(name)] = TooltipSpec(unescape_string(parts[1]),
1562 tmp_color1, tmp_color2);
1566 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1569 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1575 std::vector<std::string> parts = split(data,'[');
1577 if (parts.size() < 2) {
1581 if (parts[0] != "formspec_version") {
1585 if (is_number(parts[1])) {
1586 m_formspec_version = mystoi(parts[1]);
1593 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1598 std::vector<std::string> parts = split(element,'[');
1600 if (parts.size() < 2)
1603 std::string type = trim(parts[0]);
1604 std::string description = trim(parts[1]);
1606 if (type != "size" && type != "invsize")
1609 if (type == "invsize")
1610 log_deprecated("Deprecated formspec element \"invsize\" is used");
1612 parseSize(data, description);
1617 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1623 std::vector<std::string> parts = split(element,'[');
1625 // ugly workaround to keep compatibility
1626 if (parts.size() > 2) {
1627 if (trim(parts[0]) == "image") {
1628 for (unsigned int i=2;i< parts.size(); i++) {
1629 parts[1] += "[" + parts[i];
1635 if (parts.size() < 2) {
1639 std::string type = trim(parts[0]);
1640 std::string description = trim(parts[1]);
1642 if (type == "list") {
1643 parseList(data,description);
1647 if (type == "checkbox") {
1648 parseCheckbox(data,description);
1652 if (type == "image") {
1653 parseImage(data,description);
1657 if (type == "item_image") {
1658 parseItemImage(data,description);
1662 if ((type == "button") || (type == "button_exit")) {
1663 parseButton(data,description,type);
1667 if (type == "background") {
1668 parseBackground(data,description);
1672 if (type == "tableoptions"){
1673 parseTableOptions(data,description);
1677 if (type == "tablecolumns"){
1678 parseTableColumns(data,description);
1682 if (type == "table"){
1683 parseTable(data,description);
1687 if (type == "textlist"){
1688 parseTextList(data,description);
1692 if (type == "dropdown"){
1693 parseDropDown(data,description);
1697 if (type == "pwdfield") {
1698 parsePwdField(data,description);
1702 if ((type == "field") || (type == "textarea")){
1703 parseField(data,description,type);
1707 if (type == "label") {
1708 parseLabel(data,description);
1712 if (type == "vertlabel") {
1713 parseVertLabel(data,description);
1717 if (type == "item_image_button") {
1718 parseItemImageButton(data,description);
1722 if ((type == "image_button") || (type == "image_button_exit")) {
1723 parseImageButton(data,description,type);
1727 if (type == "tabheader") {
1728 parseTabHeader(data,description);
1732 if (type == "box") {
1733 parseBox(data,description);
1737 if (type == "bgcolor") {
1738 parseBackgroundColor(data,description);
1742 if (type == "listcolors") {
1743 parseListColors(data,description);
1747 if (type == "tooltip") {
1748 parseTooltip(data,description);
1752 if (type == "scrollbar") {
1753 parseScrollBar(data, description);
1759 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1763 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1765 /* useless to regenerate without a screensize */
1766 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1773 for (u32 i = 0; i < m_tables.size(); ++i) {
1774 std::wstring tablename = m_tables[i].first.fname;
1775 GUITable *table = m_tables[i].second;
1776 mydata.table_dyndata[tablename] = table->getDynamicData();
1780 if (!m_focused_element.empty())
1781 mydata.focused_fieldname = m_focused_element;
1784 gui::IGUIElement *focused_element = Environment->getFocus();
1785 if (focused_element && focused_element->getParent() == this) {
1786 s32 focused_id = focused_element->getID();
1787 if (focused_id > 257) {
1788 for (u32 i=0; i<m_fields.size(); i++) {
1789 if (m_fields[i].fid == focused_id) {
1790 mydata.focused_fieldname =
1801 for (u32 i = 0; i < m_tables.size(); ++i) {
1802 GUITable *table = m_tables[i].second;
1806 mydata.size= v2s32(100,100);
1807 mydata.screensize = screensize;
1809 // Base position of contents of form
1810 mydata.basepos = getBasePos();
1812 /* Convert m_init_draw_spec to m_inventorylists */
1814 m_inventorylists.clear();
1816 m_backgrounds.clear();
1817 m_itemimages.clear();
1819 m_checkboxes.clear();
1820 m_scrollbars.clear();
1825 // Set default values (fits old formspec values)
1826 m_bgcolor = video::SColor(140,0,0,0);
1827 m_bgfullscreen = false;
1829 m_slotbg_n = video::SColor(255,128,128,128);
1830 m_slotbg_h = video::SColor(255,192,192,192);
1832 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1833 m_default_tooltip_color = video::SColor(255,255,255,255);
1835 m_slotbordercolor = video::SColor(200,0,0,0);
1836 m_slotborder = false;
1838 m_clipbackground = false;
1841 assert(m_tooltip_element == NULL);
1842 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1843 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1844 m_tooltip_element->enableOverrideColor(true);
1845 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1846 m_tooltip_element->setDrawBackground(true);
1847 m_tooltip_element->setDrawBorder(true);
1848 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1849 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1850 m_tooltip_element->setWordWrap(false);
1851 //we're not parent so no autograb for this one!
1852 m_tooltip_element->grab();
1855 std::vector<std::string> elements = split(m_formspec_string,']');
1858 /* try to read version from first element only */
1859 if (elements.size() >= 1) {
1860 if ( parseVersionDirect(elements[0]) ) {
1865 /* we need size first in order to calculate image scale */
1866 mydata.explicit_size = false;
1867 for (; i< elements.size(); i++) {
1868 if (!parseSizeDirect(&mydata, elements[i])) {
1873 if (mydata.explicit_size) {
1874 // compute scaling for specified form size
1876 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1877 v2u32 delta = current_screensize - m_lockscreensize;
1879 if (current_screensize.Y > m_lockscreensize.Y)
1884 if (current_screensize.X > m_lockscreensize.X)
1889 offset = v2s32(delta.X,delta.Y);
1891 mydata.screensize = m_lockscreensize;
1893 offset = v2s32(0,0);
1896 double gui_scaling = g_settings->getFloat("gui_scaling");
1897 double screen_dpi = porting::getDisplayDensity() * 96;
1901 // In fixed-size mode, inventory image size
1902 // is 0.53 inch multiplied by the gui_scaling
1903 // config parameter. This magic size is chosen
1904 // to make the main menu (15.5 inventory images
1905 // wide, including border) just fit into the
1906 // default window (800 pixels wide) at 96 DPI
1907 // and default scaling (1.00).
1908 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1910 // In variable-size mode, we prefer to make the
1911 // inventory image size 1/15 of screen height,
1912 // multiplied by the gui_scaling config parameter.
1913 // If the preferred size won't fit the whole
1914 // form on the screen, either horizontally or
1915 // vertically, then we scale it down to fit.
1916 // (The magic numbers in the computation of what
1917 // fits arise from the scaling factors in the
1918 // following stanza, including the form border,
1919 // help text space, and 0.1 inventory slot spare.)
1920 // However, a minimum size is also set, that
1921 // the image size can't be less than 0.3 inch
1922 // multiplied by gui_scaling, even if this means
1923 // the form doesn't fit the screen.
1924 double prefer_imgsize = mydata.screensize.Y / 15 *
1926 double fitx_imgsize = mydata.screensize.X /
1927 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1928 double fity_imgsize = mydata.screensize.Y /
1929 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1930 double screen_dpi = porting::getDisplayDensity() * 96;
1931 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1932 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1933 MYMIN(fitx_imgsize, fity_imgsize)));
1936 // Everything else is scaled in proportion to the
1937 // inventory image size. The inventory slot spacing
1938 // is 5/4 image size horizontally and 15/13 image size
1939 // vertically. The padding around the form (incorporating
1940 // the border of the outer inventory slots) is 3/8
1941 // image size. Font height (baseline to baseline)
1942 // is 2/5 vertical inventory slot spacing, and button
1943 // half-height is 7/8 of font height.
1944 imgsize = v2s32(use_imgsize, use_imgsize);
1945 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
1946 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
1947 m_btn_height = use_imgsize*15.0/13 * 0.35;
1949 m_font = g_fontengine->getFont();
1951 mydata.size = v2s32(
1952 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
1953 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
1955 DesiredRect = mydata.rect = core::rect<s32>(
1956 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
1957 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
1958 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
1959 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
1962 // Non-size[] form must consist only of text fields and
1963 // implicit "Proceed" button. Use default font, and
1964 // temporary form size which will be recalculated below.
1965 m_font = g_fontengine->getFont();
1966 m_btn_height = font_line_height(m_font) * 0.875;
1967 DesiredRect = core::rect<s32>(
1968 mydata.screensize.X/2 - 580/2,
1969 mydata.screensize.Y/2 - 300/2,
1970 mydata.screensize.X/2 + 580/2,
1971 mydata.screensize.Y/2 + 300/2
1974 recalculateAbsolutePosition(false);
1975 mydata.basepos = getBasePos();
1976 m_tooltip_element->setOverrideFont(m_font);
1978 gui::IGUISkin* skin = Environment->getSkin();
1979 sanity_check(skin != NULL);
1980 gui::IGUIFont *old_font = skin->getFont();
1981 skin->setFont(m_font);
1983 for (; i< elements.size(); i++) {
1984 parseElement(&mydata, elements[i]);
1987 // If there are fields without explicit size[], add a "Proceed"
1988 // button and adjust size to fit all the fields.
1989 if (m_fields.size() && !mydata.explicit_size) {
1990 mydata.rect = core::rect<s32>(
1991 mydata.screensize.X/2 - 580/2,
1992 mydata.screensize.Y/2 - 300/2,
1993 mydata.screensize.X/2 + 580/2,
1994 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1996 DesiredRect = mydata.rect;
1997 recalculateAbsolutePosition(false);
1998 mydata.basepos = getBasePos();
2001 v2s32 pos = mydata.basepos;
2002 pos.Y = ((m_fields.size()+2)*60);
2004 v2s32 size = DesiredRect.getSize();
2006 core::rect<s32>(size.X/2-70, pos.Y,
2007 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2008 const wchar_t *text = wgettext("Proceed");
2009 Environment->addButton(mydata.rect, this, 257, text);
2015 //set initial focus if parser didn't set it
2016 focused_element = Environment->getFocus();
2017 if (!focused_element
2018 || !isMyChild(focused_element)
2019 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2022 skin->setFont(old_font);
2026 bool GUIFormSpecMenu::getAndroidUIInput()
2028 /* no dialog shown */
2029 if (m_JavaDialogFieldName == L"") {
2034 if (porting::getInputDialogState() == -1) {
2038 std::wstring fieldname = m_JavaDialogFieldName;
2039 m_JavaDialogFieldName = L"";
2041 /* no value abort dialog processing */
2042 if (porting::getInputDialogState() != 0) {
2046 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2047 iter != m_fields.end(); iter++) {
2049 if (iter->fname != fieldname) {
2052 IGUIElement* tochange = getElementFromId(iter->fid);
2054 if (tochange == 0) {
2058 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2062 std::string text = porting::getInputDialogValue();
2064 ((gui::IGUIEditBox*) tochange)->
2065 setText(narrow_to_wide(text).c_str());
2071 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2073 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2075 for(u32 i=0; i<m_inventorylists.size(); i++)
2077 const ListDrawSpec &s = m_inventorylists[i];
2079 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2080 s32 item_i = i + s.start_item_i;
2081 s32 x = (i%s.geom.X) * spacing.X;
2082 s32 y = (i/s.geom.X) * spacing.Y;
2084 core::rect<s32> rect = imgrect + s.pos + p0;
2085 if(rect.isPointInside(p))
2087 return ItemSpec(s.inventoryloc, s.listname, item_i);
2092 return ItemSpec(InventoryLocation(), "", -1);
2095 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
2097 video::IVideoDriver* driver = Environment->getVideoDriver();
2099 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2101 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2102 <<"The inventory location "
2103 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2107 InventoryList *ilist = inv->getList(s.listname);
2109 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2110 <<"The inventory list \""<<s.listname<<"\" @ \""
2111 <<s.inventoryloc.dump()<<"\" doesn't exist"
2116 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2118 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2120 s32 item_i = i + s.start_item_i;
2121 if(item_i >= (s32) ilist->getSize())
2123 s32 x = (i%s.geom.X) * spacing.X;
2124 s32 y = (i/s.geom.X) * spacing.Y;
2126 core::rect<s32> rect = imgrect + s.pos + p;
2129 item = ilist->getItem(item_i);
2131 bool selected = m_selected_item
2132 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2133 && m_selected_item->listname == s.listname
2134 && m_selected_item->i == item_i;
2135 bool hovering = rect.isPointInside(m_pointer);
2140 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2142 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2145 //Draw inv slot borders
2147 s32 x1 = rect.UpperLeftCorner.X;
2148 s32 y1 = rect.UpperLeftCorner.Y;
2149 s32 x2 = rect.LowerRightCorner.X;
2150 s32 y2 = rect.LowerRightCorner.Y;
2152 driver->draw2DRectangle(m_slotbordercolor,
2153 core::rect<s32>(v2s32(x1 - border, y1 - border),
2154 v2s32(x2 + border, y1)), NULL);
2155 driver->draw2DRectangle(m_slotbordercolor,
2156 core::rect<s32>(v2s32(x1 - border, y2),
2157 v2s32(x2 + border, y2 + border)), NULL);
2158 driver->draw2DRectangle(m_slotbordercolor,
2159 core::rect<s32>(v2s32(x1 - border, y1),
2160 v2s32(x1, y2)), NULL);
2161 driver->draw2DRectangle(m_slotbordercolor,
2162 core::rect<s32>(v2s32(x2, y1),
2163 v2s32(x2 + border, y2)), NULL);
2171 item.takeItem(m_selected_amount);
2175 drawItemStack(driver, m_font, item,
2176 rect, &AbsoluteClippingRect, m_gamedef);
2180 std::string tooltip_text = "";
2181 if (hovering && !m_selected_item)
2182 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
2183 if (tooltip_text != "") {
2184 std::vector<std::string> tt_rows = str_split(tooltip_text, '\n');
2185 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2186 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2187 m_tooltip_element->setVisible(true);
2188 this->bringToFront(m_tooltip_element);
2189 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
2190 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2191 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2192 v2u32 screenSize = driver->getScreenSize();
2193 int tooltip_offset_x = m_btn_height;
2194 int tooltip_offset_y = m_btn_height;
2196 tooltip_offset_x *= 3;
2197 tooltip_offset_y = 0;
2198 if (m_pointer.X > (s32)screenSize.X / 2)
2199 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2201 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2202 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2203 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2204 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2205 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2206 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2207 m_tooltip_element->setRelativePosition(core::rect<s32>(
2208 core::position2d<s32>(tooltip_x, tooltip_y),
2209 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2215 void GUIFormSpecMenu::drawSelectedItem()
2217 if(!m_selected_item)
2220 video::IVideoDriver* driver = Environment->getVideoDriver();
2222 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2224 InventoryList *list = inv->getList(m_selected_item->listname);
2226 ItemStack stack = list->getItem(m_selected_item->i);
2227 stack.count = m_selected_amount;
2229 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2230 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2231 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef);
2234 void GUIFormSpecMenu::drawMenu()
2237 std::string newform = m_form_src->getForm();
2238 if(newform != m_formspec_string){
2239 m_formspec_string = newform;
2240 regenerateGui(m_screensize_old);
2244 gui::IGUISkin* skin = Environment->getSkin();
2245 sanity_check(skin != NULL);
2246 gui::IGUIFont *old_font = skin->getFont();
2247 skin->setFont(m_font);
2249 updateSelectedItem();
2251 video::IVideoDriver* driver = Environment->getVideoDriver();
2253 v2u32 screenSize = driver->getScreenSize();
2254 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2256 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2258 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2260 m_tooltip_element->setVisible(false);
2265 for(u32 i=0; i<m_backgrounds.size(); i++)
2267 const ImageDrawSpec &spec = m_backgrounds[i];
2268 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2271 // Image size on screen
2272 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2273 // Image rectangle on screen
2274 core::rect<s32> rect = imgrect + spec.pos;
2276 if (m_clipbackground) {
2277 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2278 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2279 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2280 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2281 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2284 const video::SColor color(255,255,255,255);
2285 const video::SColor colors[] = {color,color,color,color};
2286 driver->draw2DImage(texture, rect,
2287 core::rect<s32>(core::position2d<s32>(0,0),
2288 core::dimension2di(texture->getOriginalSize())),
2289 NULL/*&AbsoluteClippingRect*/, colors, true);
2292 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2293 errorstream << "\t" << spec.name << std::endl;
2300 for(u32 i=0; i<m_boxes.size(); i++)
2302 const BoxDrawSpec &spec = m_boxes[i];
2304 irr::video::SColor todraw = spec.color;
2306 todraw.setAlpha(140);
2308 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2309 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2311 driver->draw2DRectangle(todraw, rect, 0);
2316 for(u32 i=0; i<m_images.size(); i++)
2318 const ImageDrawSpec &spec = m_images[i];
2319 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2322 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2323 // Image size on screen
2324 core::rect<s32> imgrect;
2327 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2330 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2332 // Image rectangle on screen
2333 core::rect<s32> rect = imgrect + spec.pos;
2334 const video::SColor color(255,255,255,255);
2335 const video::SColor colors[] = {color,color,color,color};
2336 driver->draw2DImage(texture, rect,
2337 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2338 NULL/*&AbsoluteClippingRect*/, colors, true);
2341 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2342 errorstream << "\t" << spec.name << std::endl;
2349 for(u32 i=0; i<m_itemimages.size(); i++)
2354 const ImageDrawSpec &spec = m_itemimages[i];
2355 IItemDefManager *idef = m_gamedef->idef();
2357 item.deSerialize(spec.name, idef);
2358 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
2359 // Image size on screen
2360 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2361 // Image rectangle on screen
2362 core::rect<s32> rect = imgrect + spec.pos;
2363 const video::SColor color(255,255,255,255);
2364 const video::SColor colors[] = {color,color,color,color};
2365 driver->draw2DImage(texture, rect,
2366 core::rect<s32>(core::position2d<s32>(0,0),
2367 core::dimension2di(texture->getOriginalSize())),
2368 NULL/*&AbsoluteClippingRect*/, colors, true);
2373 Phase 0: Item slot rectangles
2374 Phase 1: Item images; prepare tooltip
2377 for(int phase=start_phase; phase<=1; phase++)
2378 for(u32 i=0; i<m_inventorylists.size(); i++)
2380 drawList(m_inventorylists[i], phase);
2386 gui::IGUIElement::draw();
2388 /* TODO find way to show tooltips on touchscreen */
2389 #ifndef HAVE_TOUCHSCREENGUI
2390 m_pointer = m_device->getCursorControl()->getPosition();
2394 Draw fields/buttons tooltips
2396 gui::IGUIElement *hovered =
2397 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2399 if (hovered != NULL) {
2400 s32 id = hovered->getID();
2404 m_old_tooltip_id = id;
2407 if (id == m_old_tooltip_id) {
2408 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2410 m_hovered_time = getTimeMs();
2411 m_old_tooltip_id = id;
2415 if (id != -1 && delta >= m_tooltip_show_delay) {
2416 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2417 iter != m_fields.end(); iter++) {
2418 if ( (iter->fid == id) && (m_tooltips[iter->fname].tooltip != "") ){
2419 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2420 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2421 m_tooltip_element->setText(narrow_to_wide(m_tooltips[iter->fname].tooltip).c_str());
2422 std::vector<std::string> tt_rows = str_split(m_tooltips[iter->fname].tooltip, '\n');
2423 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2424 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2425 int tooltip_offset_x = m_btn_height;
2426 int tooltip_offset_y = m_btn_height;
2428 tooltip_offset_x *= 3;
2429 tooltip_offset_y = 0;
2430 if (m_pointer.X > (s32)screenSize.X / 2)
2431 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2433 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2434 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2435 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2436 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2437 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2438 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2439 m_tooltip_element->setRelativePosition(core::rect<s32>(
2440 core::position2d<s32>(tooltip_x, tooltip_y),
2441 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2443 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2444 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2445 m_tooltip_element->setVisible(true);
2446 this->bringToFront(m_tooltip_element);
2454 Draw dragged item stack
2458 skin->setFont(old_font);
2461 void GUIFormSpecMenu::updateSelectedItem()
2463 // If the selected stack has become empty for some reason, deselect it.
2464 // If the selected stack has become inaccessible, deselect it.
2465 // If the selected stack has become smaller, adjust m_selected_amount.
2466 ItemStack selected = verifySelectedItem();
2468 // WARNING: BLACK MAGIC
2469 // See if there is a stack suited for our current guess.
2470 // If such stack does not exist, clear the guess.
2471 if(m_selected_content_guess.name != "" &&
2472 selected.name == m_selected_content_guess.name &&
2473 selected.count == m_selected_content_guess.count){
2474 // Selected item fits the guess. Skip the black magic.
2476 else if(m_selected_content_guess.name != ""){
2478 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2479 const ListDrawSpec &s = m_inventorylists[i];
2480 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2483 InventoryList *list = inv->getList(s.listname);
2486 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2487 u32 item_i = i + s.start_item_i;
2488 if(item_i >= list->getSize())
2490 ItemStack stack = list->getItem(item_i);
2491 if(stack.name == m_selected_content_guess.name &&
2492 stack.count == m_selected_content_guess.count){
2494 infostream<<"Client: Changing selected content guess to "
2495 <<s.inventoryloc.dump()<<" "<<s.listname
2496 <<" "<<item_i<<std::endl;
2497 delete m_selected_item;
2498 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2499 m_selected_amount = stack.count;
2504 infostream<<"Client: Discarding selected content guess: "
2505 <<m_selected_content_guess.getItemString()<<std::endl;
2506 m_selected_content_guess.name = "";
2510 // If craftresult is nonempty and nothing else is selected, select it now.
2511 if(!m_selected_item)
2513 for(u32 i=0; i<m_inventorylists.size(); i++)
2515 const ListDrawSpec &s = m_inventorylists[i];
2516 if(s.listname == "craftpreview")
2518 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2519 InventoryList *list = inv->getList("craftresult");
2520 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2522 m_selected_item = new ItemSpec;
2523 m_selected_item->inventoryloc = s.inventoryloc;
2524 m_selected_item->listname = "craftresult";
2525 m_selected_item->i = 0;
2526 m_selected_amount = 0;
2527 m_selected_dragging = false;
2534 // If craftresult is selected, keep the whole stack selected
2535 if(m_selected_item && m_selected_item->listname == "craftresult")
2537 m_selected_amount = verifySelectedItem().count;
2541 ItemStack GUIFormSpecMenu::verifySelectedItem()
2543 // If the selected stack has become empty for some reason, deselect it.
2544 // If the selected stack has become inaccessible, deselect it.
2545 // If the selected stack has become smaller, adjust m_selected_amount.
2546 // Return the selected stack.
2550 if(m_selected_item->isValid())
2552 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2555 InventoryList *list = inv->getList(m_selected_item->listname);
2556 if(list && (u32) m_selected_item->i < list->getSize())
2558 ItemStack stack = list->getItem(m_selected_item->i);
2559 if(m_selected_amount > stack.count)
2560 m_selected_amount = stack.count;
2567 // selection was not valid
2568 delete m_selected_item;
2569 m_selected_item = NULL;
2570 m_selected_amount = 0;
2571 m_selected_dragging = false;
2576 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2580 std::map<std::string, std::string> fields;
2582 if (quitmode == quit_mode_accept) {
2583 fields["quit"] = "true";
2586 if (quitmode == quit_mode_cancel) {
2587 fields["quit"] = "true";
2588 m_text_dst->gotText(fields);
2592 if (current_keys_pending.key_down) {
2593 fields["key_down"] = "true";
2594 current_keys_pending.key_down = false;
2597 if (current_keys_pending.key_up) {
2598 fields["key_up"] = "true";
2599 current_keys_pending.key_up = false;
2602 if (current_keys_pending.key_enter) {
2603 fields["key_enter"] = "true";
2604 current_keys_pending.key_enter = false;
2607 if (current_keys_pending.key_escape) {
2608 fields["key_escape"] = "true";
2609 current_keys_pending.key_escape = false;
2612 for(unsigned int i=0; i<m_fields.size(); i++) {
2613 const FieldSpec &s = m_fields[i];
2615 std::string name = wide_to_narrow(s.fname);
2616 if(s.ftype == f_Button) {
2617 fields[name] = wide_to_narrow(s.flabel);
2619 else if(s.ftype == f_Table) {
2620 GUITable *table = getTable(s.fname);
2622 fields[name] = table->checkEvent();
2625 else if(s.ftype == f_DropDown) {
2626 // no dynamic cast possible due to some distributions shipped
2627 // without rtti support in irrlicht
2628 IGUIElement * element = getElementFromId(s.fid);
2629 gui::IGUIComboBox *e = NULL;
2630 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2631 e = static_cast<gui::IGUIComboBox*>(element);
2633 s32 selected = e->getSelected();
2634 if (selected >= 0) {
2636 wide_to_narrow(e->getItem(selected));
2639 else if (s.ftype == f_TabHeader) {
2640 // no dynamic cast possible due to some distributions shipped
2641 // without rtti support in irrlicht
2642 IGUIElement * element = getElementFromId(s.fid);
2643 gui::IGUITabControl *e = NULL;
2644 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2645 e = static_cast<gui::IGUITabControl*>(element);
2649 std::stringstream ss;
2650 ss << (e->getActiveTab() +1);
2651 fields[name] = ss.str();
2654 else if (s.ftype == f_CheckBox) {
2655 // no dynamic cast possible due to some distributions shipped
2656 // without rtti support in irrlicht
2657 IGUIElement * element = getElementFromId(s.fid);
2658 gui::IGUICheckBox *e = NULL;
2659 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2660 e = static_cast<gui::IGUICheckBox*>(element);
2665 fields[name] = "true";
2667 fields[name] = "false";
2670 else if (s.ftype == f_ScrollBar) {
2671 // no dynamic cast possible due to some distributions shipped
2672 // without rtti support in irrlicht
2673 IGUIElement * element = getElementFromId(s.fid);
2674 gui::IGUIScrollBar *e = NULL;
2675 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2676 e = static_cast<gui::IGUIScrollBar*>(element);
2680 std::stringstream os;
2682 if (s.fdefault == L"Changed")
2683 fields[name] = "CHG:" + os.str();
2685 fields[name] = "VAL:" + os.str();
2690 IGUIElement* e = getElementFromId(s.fid);
2692 fields[name] = wide_to_narrow(e->getText());
2698 m_text_dst->gotText(fields);
2702 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2704 while(tocheck != NULL) {
2705 if (tocheck == parent) {
2708 tocheck = tocheck->getParent();
2713 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2715 // The IGUITabControl renders visually using the skin's selected
2716 // font, which we override for the duration of form drawing,
2717 // but computes tab hotspots based on how it would have rendered
2718 // using the font that is selected at the time of button release.
2719 // To make these two consistent, temporarily override the skin's
2720 // font while the IGUITabControl is processing the event.
2721 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2722 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2723 s32 x = event.MouseInput.X;
2724 s32 y = event.MouseInput.Y;
2725 gui::IGUIElement *hovered =
2726 Environment->getRootGUIElement()->getElementFromPoint(
2727 core::position2d<s32>(x, y));
2728 if (hovered && isMyChild(hovered) &&
2729 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2730 gui::IGUISkin* skin = Environment->getSkin();
2731 sanity_check(skin != NULL);
2732 gui::IGUIFont *old_font = skin->getFont();
2733 skin->setFont(m_font);
2734 bool retval = hovered->OnEvent(event);
2735 skin->setFont(old_font);
2740 // Fix Esc/Return key being eaten by checkboxen and tables
2741 if(event.EventType==EET_KEY_INPUT_EVENT) {
2742 KeyPress kp(event.KeyInput);
2743 if (kp == EscapeKey || kp == CancelKey
2744 || kp == getKeySetting("keymap_inventory")
2745 || event.KeyInput.Key==KEY_RETURN) {
2746 gui::IGUIElement *focused = Environment->getFocus();
2747 if (focused && isMyChild(focused) &&
2748 (focused->getType() == gui::EGUIET_LIST_BOX ||
2749 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2755 // Mouse wheel events: send to hovered element instead of focused
2756 if(event.EventType==EET_MOUSE_INPUT_EVENT
2757 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2758 s32 x = event.MouseInput.X;
2759 s32 y = event.MouseInput.Y;
2760 gui::IGUIElement *hovered =
2761 Environment->getRootGUIElement()->getElementFromPoint(
2762 core::position2d<s32>(x, y));
2763 if (hovered && isMyChild(hovered)) {
2764 hovered->OnEvent(event);
2769 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2770 s32 x = event.MouseInput.X;
2771 s32 y = event.MouseInput.Y;
2772 gui::IGUIElement *hovered =
2773 Environment->getRootGUIElement()->getElementFromPoint(
2774 core::position2d<s32>(x, y));
2775 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2776 m_old_tooltip_id = -1;
2779 if (!isChild(hovered,this)) {
2780 if (DoubleClickDetection(event)) {
2787 // display software keyboard when clicking edit boxes
2788 if (event.EventType == EET_MOUSE_INPUT_EVENT
2789 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2790 gui::IGUIElement *hovered =
2791 Environment->getRootGUIElement()->getElementFromPoint(
2792 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2793 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2794 bool retval = hovered->OnEvent(event);
2796 Environment->setFocus(hovered);
2798 m_JavaDialogFieldName = getNameByID(hovered->getID());
2799 std::string message = gettext("Enter ");
2800 std::string label = wide_to_narrow(getLabelByID(hovered->getID()));
2804 message += gettext(label) + ":";
2806 /* single line text input */
2809 /* multi line text input */
2810 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2814 /* passwords are always single line */
2815 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2819 porting::showInputDialog(gettext("ok"), "",
2820 wide_to_narrow(((gui::IGUIEditBox*) hovered)->getText()),
2826 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2829 memset(&translated, 0, sizeof(SEvent));
2830 translated.EventType = EET_MOUSE_INPUT_EVENT;
2831 gui::IGUIElement* root = Environment->getRootGUIElement();
2835 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2839 gui::IGUIElement* hovered = root->getElementFromPoint(
2840 core::position2d<s32>(
2842 event.TouchInput.Y));
2844 translated.MouseInput.X = event.TouchInput.X;
2845 translated.MouseInput.Y = event.TouchInput.Y;
2846 translated.MouseInput.Control = false;
2848 bool dont_send_event = false;
2850 if (event.TouchInput.touchedCount == 1) {
2851 switch (event.TouchInput.Event) {
2852 case ETIE_PRESSED_DOWN:
2853 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2854 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2855 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2856 m_down_pos = m_pointer;
2859 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2860 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2861 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2864 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2865 translated.MouseInput.ButtonStates = 0;
2866 hovered = root->getElementFromPoint(m_down_pos);
2867 /* we don't have a valid pointer element use last
2868 * known pointer pos */
2869 translated.MouseInput.X = m_pointer.X;
2870 translated.MouseInput.Y = m_pointer.Y;
2872 /* reset down pos */
2873 m_down_pos = v2s32(0,0);
2876 dont_send_event = true;
2877 //this is not supposed to happen
2879 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2880 << event.TouchInput.Event << std::endl;
2882 } else if ( (event.TouchInput.touchedCount == 2) &&
2883 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2884 hovered = root->getElementFromPoint(m_down_pos);
2886 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2887 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2888 translated.MouseInput.X = m_pointer.X;
2889 translated.MouseInput.Y = m_pointer.Y;
2892 hovered->OnEvent(translated);
2895 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
2896 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2900 hovered->OnEvent(translated);
2902 dont_send_event = true;
2904 /* ignore unhandled 2 touch events ... accidental moving for example */
2905 else if (event.TouchInput.touchedCount == 2) {
2906 dont_send_event = true;
2908 else if (event.TouchInput.touchedCount > 2) {
2910 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
2911 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
2914 if (dont_send_event) {
2918 /* check if translated event needs to be preprocessed again */
2919 if (preprocessEvent(translated)) {
2924 bool retval = hovered->OnEvent(translated);
2926 if (event.TouchInput.Event == ETIE_LEFT_UP) {
2928 m_pointer = v2s32(0,0);
2939 /******************************************************************************/
2940 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
2942 /* The following code is for capturing double-clicks of the mouse button
2943 * and translating the double-click into an EET_KEY_INPUT_EVENT event
2944 * -- which closes the form -- under some circumstances.
2946 * There have been many github issues reporting this as a bug even though it
2947 * was an intended feature. For this reason, remapping the double-click as
2948 * an ESC must be explicitly set when creating this class via the
2949 * /p remap_dbl_click parameter of the constructor.
2952 if (!m_remap_dbl_click)
2955 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2956 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
2957 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
2959 m_doubleclickdetect[1].pos = m_pointer;
2960 m_doubleclickdetect[1].time = getTimeMs();
2962 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2963 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
2968 double squaredistance =
2969 m_doubleclickdetect[0].pos
2970 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
2972 if (squaredistance > (30*30)) {
2976 SEvent* translated = new SEvent();
2977 assert(translated != 0);
2978 //translate doubleclick to escape
2979 memset(translated, 0, sizeof(SEvent));
2980 translated->EventType = irr::EET_KEY_INPUT_EVENT;
2981 translated->KeyInput.Key = KEY_ESCAPE;
2982 translated->KeyInput.Control = false;
2983 translated->KeyInput.Shift = false;
2984 translated->KeyInput.PressedDown = true;
2985 translated->KeyInput.Char = 0;
2986 OnEvent(*translated);
2988 // no need to send the key up event as we're already deleted
2989 // and no one else did notice this event
2997 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2999 if(event.EventType==EET_KEY_INPUT_EVENT) {
3000 KeyPress kp(event.KeyInput);
3001 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3002 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3005 acceptInput(quit_mode_cancel);
3008 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
3011 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3012 (kp == getKeySetting("keymap_screenshot"))) {
3013 m_client->makeScreenshot(m_device);
3015 if (event.KeyInput.PressedDown &&
3016 (event.KeyInput.Key==KEY_RETURN ||
3017 event.KeyInput.Key==KEY_UP ||
3018 event.KeyInput.Key==KEY_DOWN)
3020 switch (event.KeyInput.Key) {
3022 current_keys_pending.key_enter = true;
3025 current_keys_pending.key_up = true;
3028 current_keys_pending.key_down = true;
3032 //can't happen at all!
3033 FATAL_ERROR("Reached a source line that can't ever been reached");
3036 if (current_keys_pending.key_enter && m_allowclose) {
3037 acceptInput(quit_mode_accept);
3047 /* Mouse event other than movement, or crossing the border of inventory
3048 field while holding right mouse button
3050 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3051 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3052 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3053 event.MouseInput.isRightPressed() &&
3054 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3056 // Get selected item and hovered/clicked item (s)
3058 m_old_tooltip_id = -1;
3059 updateSelectedItem();
3060 ItemSpec s = getItemAtPos(m_pointer);
3062 Inventory *inv_selected = NULL;
3063 Inventory *inv_s = NULL;
3065 if(m_selected_item) {
3066 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3067 sanity_check(inv_selected);
3068 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3075 inv_s = m_invmgr->getInventory(s.inventoryloc);
3078 errorstream<<"InventoryMenu: The selected inventory location "
3079 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
3081 s.i = -1; // make it invalid again
3085 InventoryList *list = inv_s->getList(s.listname);
3087 verbosestream<<"InventoryMenu: The selected inventory list \""
3088 <<s.listname<<"\" does not exist"<<std::endl;
3089 s.i = -1; // make it invalid again
3093 if((u32)s.i >= list->getSize()) {
3094 infostream<<"InventoryMenu: The selected inventory list \""
3095 <<s.listname<<"\" is too small (i="<<s.i<<", size="
3096 <<list->getSize()<<")"<<std::endl;
3097 s.i = -1; // make it invalid again
3101 s_count = list->getItem(s.i).count;
3104 bool identical = (m_selected_item != NULL) && s.isValid() &&
3105 (inv_selected == inv_s) &&
3106 (m_selected_item->listname == s.listname) &&
3107 (m_selected_item->i == s.i);
3109 // buttons: 0 = left, 1 = right, 2 = middle
3110 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3113 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3114 { button = 0; updown = 0; }
3115 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3116 { button = 1; updown = 0; }
3117 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3118 { button = 2; updown = 0; }
3119 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3120 { button = 0; updown = 1; }
3121 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3122 { button = 1; updown = 1; }
3123 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3124 { button = 2; updown = 1; }
3125 else if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
3128 // Set this number to a positive value to generate a move action
3129 // from m_selected_item to s.
3130 u32 move_amount = 0;
3132 // Set this number to a positive value to generate a drop action
3133 // from m_selected_item.
3134 u32 drop_amount = 0;
3136 // Set this number to a positive value to generate a craft action at s.
3137 u32 craft_amount = 0;
3140 // Some mouse button has been pressed
3142 //infostream<<"Mouse button "<<button<<" pressed at p=("
3143 // <<p.X<<","<<p.Y<<")"<<std::endl;
3145 m_selected_dragging = false;
3147 if(s.isValid() && s.listname == "craftpreview") {
3148 // Craft preview has been clicked: craft
3149 craft_amount = (button == 2 ? 10 : 1);
3151 else if(m_selected_item == NULL) {
3153 // Non-empty stack has been clicked: select it
3154 m_selected_item = new ItemSpec(s);
3156 if(button == 1) // right
3157 m_selected_amount = (s_count + 1) / 2;
3158 else if(button == 2) // middle
3159 m_selected_amount = MYMIN(s_count, 10);
3161 m_selected_amount = s_count;
3163 m_selected_dragging = true;
3164 m_rmouse_auto_place = false;
3167 else { // m_selected_item != NULL
3168 assert(m_selected_amount >= 1);
3171 // Clicked a slot: move
3172 if(button == 1) // right
3174 else if(button == 2) // middle
3175 move_amount = MYMIN(m_selected_amount, 10);
3177 move_amount = m_selected_amount;
3180 if(move_amount >= m_selected_amount)
3181 m_selected_amount = 0;
3183 m_selected_amount -= move_amount;
3187 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3188 // Clicked outside of the window: drop
3189 if(button == 1) // right
3191 else if(button == 2) // middle
3192 drop_amount = MYMIN(m_selected_amount, 10);
3194 drop_amount = m_selected_amount;
3198 else if(updown == 1) {
3199 // Some mouse button has been released
3201 //infostream<<"Mouse button "<<button<<" released at p=("
3202 // <<p.X<<","<<p.Y<<")"<<std::endl;
3204 if(m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3206 // Dragged to different slot: move all selected
3207 move_amount = m_selected_amount;
3210 else if(m_selected_item != NULL && m_selected_dragging &&
3211 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3212 // Dragged outside of window: drop all selected
3213 drop_amount = m_selected_amount;
3216 m_selected_dragging = false;
3217 // Keep count of how many times right mouse button has been
3218 // clicked. One click is drag without dropping. Click + release
3219 // + click changes to drop one item when moved mode
3220 if(button == 1 && m_selected_item != NULL)
3221 m_rmouse_auto_place = !m_rmouse_auto_place;
3223 else if(updown == -1) {
3224 // Mouse has been moved and rmb is down and mouse pointer just
3225 // entered a new inventory field (checked in the entry-if, this
3226 // is the only action here that is generated by mouse movement)
3227 if(m_selected_item != NULL && s.isValid()){
3229 // TODO: middle mouse to move 10 items might be handy
3230 if (m_rmouse_auto_place) {
3231 // Only move an item if the destination slot is empty
3232 // or contains the same item type as what is going to be
3234 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3235 InventoryList *list_to = inv_s->getList(s.listname);
3236 assert(list_from && list_to);
3237 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3238 ItemStack stack_to = list_to->getItem(s.i);
3239 if (stack_to.empty() || stack_to.name == stack_from.name)
3245 // Possibly send inventory action to server
3248 // Send IACTION_MOVE
3250 assert(m_selected_item && m_selected_item->isValid());
3251 assert(s.isValid());
3253 assert(inv_selected && inv_s);
3254 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3255 InventoryList *list_to = inv_s->getList(s.listname);
3256 assert(list_from && list_to);
3257 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3258 ItemStack stack_to = list_to->getItem(s.i);
3260 // Check how many items can be moved
3261 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3262 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3263 // If source stack cannot be added to destination stack at all,
3265 if ((leftover.count == stack_from.count) &&
3266 (leftover.name == stack_from.name)) {
3267 m_selected_amount = stack_to.count;
3268 // In case the server doesn't directly swap them but instead
3269 // moves stack_to somewhere else, set this
3270 m_selected_content_guess = stack_to;
3271 m_selected_content_guess_inventory = s.inventoryloc;
3273 // Source stack goes fully into destination stack
3274 else if(leftover.empty()) {
3275 m_selected_amount -= move_amount;
3276 m_selected_content_guess = ItemStack(); // Clear
3278 // Source stack goes partly into destination stack
3280 move_amount -= leftover.count;
3281 m_selected_amount -= move_amount;
3282 m_selected_content_guess = ItemStack(); // Clear
3285 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
3286 IMoveAction *a = new IMoveAction();
3287 a->count = move_amount;
3288 a->from_inv = m_selected_item->inventoryloc;
3289 a->from_list = m_selected_item->listname;
3290 a->from_i = m_selected_item->i;
3291 a->to_inv = s.inventoryloc;
3292 a->to_list = s.listname;
3294 m_invmgr->inventoryAction(a);
3296 else if(drop_amount > 0) {
3297 m_selected_content_guess = ItemStack(); // Clear
3299 // Send IACTION_DROP
3301 assert(m_selected_item && m_selected_item->isValid());
3302 assert(inv_selected);
3303 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3305 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3307 // Check how many items can be dropped
3308 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3309 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3310 m_selected_amount -= drop_amount;
3312 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
3313 IDropAction *a = new IDropAction();
3314 a->count = drop_amount;
3315 a->from_inv = m_selected_item->inventoryloc;
3316 a->from_list = m_selected_item->listname;
3317 a->from_i = m_selected_item->i;
3318 m_invmgr->inventoryAction(a);
3320 else if(craft_amount > 0) {
3321 m_selected_content_guess = ItemStack(); // Clear
3323 // Send IACTION_CRAFT
3325 assert(s.isValid());
3328 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
3329 ICraftAction *a = new ICraftAction();
3330 a->count = craft_amount;
3331 a->craft_inv = s.inventoryloc;
3332 m_invmgr->inventoryAction(a);
3335 // If m_selected_amount has been decreased to zero, deselect
3336 if(m_selected_amount == 0) {
3337 delete m_selected_item;
3338 m_selected_item = NULL;
3339 m_selected_amount = 0;
3340 m_selected_dragging = false;
3341 m_selected_content_guess = ItemStack();
3343 m_old_pointer = m_pointer;
3345 if(event.EventType==EET_GUI_EVENT) {
3347 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
3349 // find the element that was clicked
3350 for(unsigned int i=0; i<m_fields.size(); i++) {
3351 FieldSpec &s = m_fields[i];
3352 if ((s.ftype == f_TabHeader) &&
3353 (s.fid == event.GUIEvent.Caller->getID())) {
3361 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
3363 if(!canTakeFocus(event.GUIEvent.Element)) {
3364 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3366 // Returning true disables focus change
3370 if((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3371 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3372 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3373 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3374 unsigned int btn_id = event.GUIEvent.Caller->getID();
3376 if (btn_id == 257) {
3378 acceptInput(quit_mode_accept);
3382 m_text_dst->gotText(narrow_to_wide("ExitButton"));
3384 // quitMenu deallocates menu
3388 // find the element that was clicked
3389 for(u32 i=0; i<m_fields.size(); i++) {
3390 FieldSpec &s = m_fields[i];
3391 // if its a button, set the send field so
3392 // lua knows which button was pressed
3393 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3394 (s.fid == event.GUIEvent.Caller->getID())) {
3398 acceptInput(quit_mode_accept);
3401 m_text_dst->gotText(narrow_to_wide("ExitButton"));
3405 acceptInput(quit_mode_no);
3410 else if ((s.ftype == f_DropDown) &&
3411 (s.fid == event.GUIEvent.Caller->getID())) {
3412 // only send the changed dropdown
3413 for(u32 i=0; i<m_fields.size(); i++) {
3414 FieldSpec &s2 = m_fields[i];
3415 if (s2.ftype == f_DropDown) {
3420 acceptInput(quit_mode_no);
3422 // revert configuration to make sure dropdowns are sent on
3423 // regular button click
3424 for(u32 i=0; i<m_fields.size(); i++) {
3425 FieldSpec &s2 = m_fields[i];
3426 if (s2.ftype == f_DropDown) {
3432 else if ((s.ftype == f_ScrollBar) &&
3433 (s.fid == event.GUIEvent.Caller->getID()))
3435 s.fdefault = L"Changed";
3436 acceptInput(quit_mode_no);
3442 if(event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3443 if(event.GUIEvent.Caller->getID() > 257) {
3446 acceptInput(quit_mode_accept);
3449 current_keys_pending.key_enter = true;
3452 // quitMenu deallocates menu
3457 if(event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3458 int current_id = event.GUIEvent.Caller->getID();
3459 if(current_id > 257) {
3460 // find the element that was clicked
3461 for(u32 i=0; i<m_fields.size(); i++) {
3462 FieldSpec &s = m_fields[i];
3463 // if it's a table, set the send field
3464 // so lua knows which table was changed
3465 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3476 return Parent ? Parent->OnEvent(event) : false;
3480 * get name of element by element id
3481 * @param id of element
3482 * @return name string or empty string
3484 std::wstring GUIFormSpecMenu::getNameByID(s32 id)
3486 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3487 iter != m_fields.end(); iter++) {
3488 if (iter->fid == id) {
3496 * get label of element by id
3497 * @param id of element
3498 * @return label string or empty string
3500 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3502 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3503 iter != m_fields.end(); iter++) {
3504 if (iter->fid == id) {
3505 return iter->flabel;