3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guiFormSpecMenu.h"
28 #include "constants.h"
31 #include "util/strfnd.h"
32 #include <IGUICheckBox.h>
33 #include <IGUIEditBox.h>
34 #include <IGUIButton.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include <IGUIComboBox.h>
40 #include "client/tile.h" // ITextureSource
41 #include "hud.h" // drawItemStack
45 #include "scripting_game.h"
49 #include "fontengine.h"
51 #include "util/numeric.h"
52 #include "util/string.h" // for parseColorString()
53 #include "irrlicht_changes/static_text.h"
54 #include "guiscalingfilter.h"
56 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
57 #include "intlGUIEditBox.h"
60 #define MY_CHECKPOS(a,b) \
61 if (v_pos.size() != 2) { \
62 errorstream<< "Invalid pos for element " << a << "specified: \"" \
63 << parts[b] << "\"" << std::endl; \
67 #define MY_CHECKGEOM(a,b) \
68 if (v_geom.size() != 2) { \
69 errorstream<< "Invalid pos for element " << a << "specified: \"" \
70 << parts[b] << "\"" << std::endl; \
76 static unsigned int font_line_height(gui::IGUIFont *font)
78 return font->getDimension(L"Ay").Height + font->getKerningHeight();
81 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
82 JoystickController *joystick,
83 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
84 InventoryManager *invmgr, IGameDef *gamedef,
85 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
86 Client* client, bool remap_dbl_click) :
87 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
93 m_selected_item(NULL),
95 m_selected_dragging(false),
96 m_tooltip_element(NULL),
99 m_rmouse_auto_place(false),
104 m_formspec_version(0),
105 m_focused_element(""),
106 m_joystick(joystick),
108 m_remap_dbl_click(remap_dbl_click)
110 , m_JavaDialogFieldName("")
113 current_keys_pending.key_down = false;
114 current_keys_pending.key_up = false;
115 current_keys_pending.key_enter = false;
116 current_keys_pending.key_escape = false;
118 m_doubleclickdetect[0].time = 0;
119 m_doubleclickdetect[1].time = 0;
121 m_doubleclickdetect[0].pos = v2s32(0, 0);
122 m_doubleclickdetect[1].pos = v2s32(0, 0);
124 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
127 GUIFormSpecMenu::~GUIFormSpecMenu()
131 for (u32 i = 0; i < m_tables.size(); ++i) {
132 GUITable *table = m_tables[i].second;
136 delete m_selected_item;
138 if (m_form_src != NULL) {
141 if (m_text_dst != NULL) {
146 void GUIFormSpecMenu::removeChildren()
148 const core::list<gui::IGUIElement*> &children = getChildren();
150 while(!children.empty()) {
151 (*children.getLast())->remove();
154 if(m_tooltip_element) {
155 m_tooltip_element->remove();
156 m_tooltip_element->drop();
157 m_tooltip_element = NULL;
162 void GUIFormSpecMenu::setInitialFocus()
164 // Set initial focus according to following order of precedence:
165 // 1. first empty editbox
169 // 5. first focusable (not statictext, not tabheader)
170 // 6. first child element
172 core::list<gui::IGUIElement*> children = getChildren();
174 // in case "children" contains any NULL elements, remove them
175 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
176 it != children.end();) {
180 it = children.erase(it);
183 // 1. first empty editbox
184 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
185 it != children.end(); ++it) {
186 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
187 && (*it)->getText()[0] == 0) {
188 Environment->setFocus(*it);
194 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
195 it != children.end(); ++it) {
196 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
197 Environment->setFocus(*it);
203 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
204 it != children.end(); ++it) {
205 if ((*it)->getTypeName() == std::string("GUITable")) {
206 Environment->setFocus(*it);
212 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
213 it != children.end(); --it) {
214 if ((*it)->getType() == gui::EGUIET_BUTTON) {
215 Environment->setFocus(*it);
220 // 5. first focusable (not statictext, not tabheader)
221 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
222 it != children.end(); ++it) {
223 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
224 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
225 Environment->setFocus(*it);
230 // 6. first child element
231 if (children.empty())
232 Environment->setFocus(this);
234 Environment->setFocus(*(children.begin()));
237 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
239 for (u32 i = 0; i < m_tables.size(); ++i) {
240 if (tablename == m_tables[i].first.fname)
241 return m_tables[i].second;
246 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
248 for (u32 i = 0; i < m_dropdowns.size(); ++i) {
249 if (name == m_dropdowns[i].first.fname)
250 return &m_dropdowns[i].second;
255 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
257 std::vector<std::string> parts = split(element,',');
259 if (((parts.size() == 2) || parts.size() == 3) ||
260 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
262 if (parts[1].find(';') != std::string::npos)
263 parts[1] = parts[1].substr(0,parts[1].find(';'));
265 data->invsize.X = MYMAX(0, stof(parts[0]));
266 data->invsize.Y = MYMAX(0, stof(parts[1]));
269 if (parts.size() == 3) {
270 if (parts[2] == "true") {
271 lockSize(true,v2u32(800,600));
275 data->explicit_size = true;
278 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
281 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
283 if (m_gamedef == 0) {
284 warningstream<<"invalid use of 'list' with m_gamedef==0"<<std::endl;
288 std::vector<std::string> parts = split(element,';');
290 if (((parts.size() == 4) || (parts.size() == 5)) ||
291 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
293 std::string location = parts[0];
294 std::string listname = parts[1];
295 std::vector<std::string> v_pos = split(parts[2],',');
296 std::vector<std::string> v_geom = split(parts[3],',');
297 std::string startindex = "";
298 if (parts.size() == 5)
299 startindex = parts[4];
301 MY_CHECKPOS("list",2);
302 MY_CHECKGEOM("list",3);
304 InventoryLocation loc;
306 if(location == "context" || location == "current_name")
307 loc = m_current_inventory_location;
309 loc.deSerialize(location);
311 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
312 pos.X += stof(v_pos[0]) * (float)spacing.X;
313 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
316 geom.X = stoi(v_geom[0]);
317 geom.Y = stoi(v_geom[1]);
321 start_i = stoi(startindex);
323 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
324 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
328 if(!data->explicit_size)
329 warningstream<<"invalid use of list without a size[] element"<<std::endl;
330 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
333 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
336 void GUIFormSpecMenu::parseListRing(parserData* data, std::string element)
338 if (m_gamedef == 0) {
339 errorstream << "WARNING: invalid use of 'listring' with m_gamedef==0" << std::endl;
343 std::vector<std::string> parts = split(element, ';');
345 if (parts.size() == 2) {
346 std::string location = parts[0];
347 std::string listname = parts[1];
349 InventoryLocation loc;
351 if (location == "context" || location == "current_name")
352 loc = m_current_inventory_location;
354 loc.deSerialize(location);
356 m_inventory_rings.push_back(ListRingSpec(loc, listname));
358 } else if ((element == "") && (m_inventorylists.size() > 1)) {
359 size_t siz = m_inventorylists.size();
360 // insert the last two inv list elements into the list ring
361 const ListDrawSpec &spa = m_inventorylists[siz - 2];
362 const ListDrawSpec &spb = m_inventorylists[siz - 1];
363 m_inventory_rings.push_back(ListRingSpec(spa.inventoryloc, spa.listname));
364 m_inventory_rings.push_back(ListRingSpec(spb.inventoryloc, spb.listname));
367 errorstream<< "Invalid list ring element(" << parts.size() << ", "
368 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
371 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
373 std::vector<std::string> parts = split(element,';');
375 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
376 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
378 std::vector<std::string> v_pos = split(parts[0],',');
379 std::string name = parts[1];
380 std::string label = parts[2];
381 std::string selected = "";
383 if (parts.size() >= 4)
386 MY_CHECKPOS("checkbox",0);
389 pos.X += stof(v_pos[0]) * (float) spacing.X;
390 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
392 bool fselected = false;
394 if (selected == "true")
397 std::wstring wlabel = utf8_to_wide(unescape_string(label));
399 core::rect<s32> rect = core::rect<s32>(
400 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
401 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
402 pos.Y + ((imgsize.Y/2) + m_btn_height));
406 wlabel, //Needed for displaying text on MSVC
411 spec.ftype = f_CheckBox;
413 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
414 spec.fid, spec.flabel.c_str());
416 if (spec.fname == data->focused_fieldname) {
417 Environment->setFocus(e);
420 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
421 m_fields.push_back(spec);
424 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
427 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
429 std::vector<std::string> parts = split(element,';');
431 if (parts.size() >= 5) {
432 std::vector<std::string> v_pos = split(parts[0],',');
433 std::vector<std::string> v_dim = split(parts[1],',');
434 std::string name = parts[3];
435 std::string value = parts[4];
437 MY_CHECKPOS("scrollbar",0);
440 pos.X += stof(v_pos[0]) * (float) spacing.X;
441 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
443 if (v_dim.size() != 2) {
444 errorstream<< "Invalid size for element " << "scrollbar"
445 << "specified: \"" << parts[1] << "\"" << std::endl;
450 dim.X = stof(v_dim[0]) * (float) spacing.X;
451 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
453 core::rect<s32> rect =
454 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
463 bool is_horizontal = true;
465 if (parts[2] == "vertical")
466 is_horizontal = false;
468 spec.ftype = f_ScrollBar;
470 gui::IGUIScrollBar* e =
471 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
475 e->setPos(stoi(parts[4]));
477 e->setLargeStep(100);
479 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
480 m_fields.push_back(spec);
483 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
486 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
488 std::vector<std::string> parts = split(element,';');
490 if ((parts.size() == 3) ||
491 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
493 std::vector<std::string> v_pos = split(parts[0],',');
494 std::vector<std::string> v_geom = split(parts[1],',');
495 std::string name = unescape_string(parts[2]);
497 MY_CHECKPOS("image",0);
498 MY_CHECKGEOM("image",1);
500 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
501 pos.X += stof(v_pos[0]) * (float) spacing.X;
502 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
505 geom.X = stof(v_geom[0]) * (float)imgsize.X;
506 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
508 if(!data->explicit_size)
509 warningstream<<"invalid use of image without a size[] element"<<std::endl;
510 m_images.push_back(ImageDrawSpec(name, pos, geom));
514 if (parts.size() == 2) {
515 std::vector<std::string> v_pos = split(parts[0],',');
516 std::string name = unescape_string(parts[1]);
518 MY_CHECKPOS("image",0);
520 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
521 pos.X += stof(v_pos[0]) * (float) spacing.X;
522 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
524 if(!data->explicit_size)
525 warningstream<<"invalid use of image without a size[] element"<<std::endl;
526 m_images.push_back(ImageDrawSpec(name, pos));
529 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
532 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
534 std::vector<std::string> parts = split(element,';');
536 if ((parts.size() == 3) ||
537 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
539 std::vector<std::string> v_pos = split(parts[0],',');
540 std::vector<std::string> v_geom = split(parts[1],',');
541 std::string name = parts[2];
543 MY_CHECKPOS("itemimage",0);
544 MY_CHECKGEOM("itemimage",1);
546 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
547 pos.X += stof(v_pos[0]) * (float) spacing.X;
548 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
551 geom.X = stof(v_geom[0]) * (float)imgsize.X;
552 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
554 if(!data->explicit_size)
555 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
556 m_itemimages.push_back(ImageDrawSpec("", name, pos, geom));
559 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
562 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
565 std::vector<std::string> parts = split(element,';');
567 if ((parts.size() == 4) ||
568 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
570 std::vector<std::string> v_pos = split(parts[0],',');
571 std::vector<std::string> v_geom = split(parts[1],',');
572 std::string name = parts[2];
573 std::string label = parts[3];
575 MY_CHECKPOS("button",0);
576 MY_CHECKGEOM("button",1);
579 pos.X += stof(v_pos[0]) * (float)spacing.X;
580 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
583 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
584 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
586 core::rect<s32> rect =
587 core::rect<s32>(pos.X, pos.Y - m_btn_height,
588 pos.X + geom.X, pos.Y + m_btn_height);
590 if(!data->explicit_size)
591 warningstream<<"invalid use of button without a size[] element"<<std::endl;
593 std::wstring wlabel = utf8_to_wide(unescape_string(label));
601 spec.ftype = f_Button;
602 if(type == "button_exit")
604 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
605 spec.flabel.c_str());
607 if (spec.fname == data->focused_fieldname) {
608 Environment->setFocus(e);
611 m_fields.push_back(spec);
614 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
617 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
619 std::vector<std::string> parts = split(element,';');
621 if (((parts.size() == 3) || (parts.size() == 4)) ||
622 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
624 std::vector<std::string> v_pos = split(parts[0],',');
625 std::vector<std::string> v_geom = split(parts[1],',');
626 std::string name = unescape_string(parts[2]);
628 MY_CHECKPOS("background",0);
629 MY_CHECKGEOM("background",1);
631 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
632 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
633 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
636 geom.X = stof(v_geom[0]) * (float)spacing.X;
637 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
639 if (parts.size() == 4) {
640 m_clipbackground = is_yes(parts[3]);
641 if (m_clipbackground) {
642 pos.X = stoi(v_pos[0]); //acts as offset
643 pos.Y = stoi(v_pos[1]); //acts as offset
647 if(!data->explicit_size)
648 warningstream<<"invalid use of background without a size[] element"<<std::endl;
649 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
652 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
655 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
657 std::vector<std::string> parts = split(element,';');
659 data->table_options.clear();
660 for (size_t i = 0; i < parts.size(); ++i) {
661 // Parse table option
662 std::string opt = unescape_string(parts[i]);
663 data->table_options.push_back(GUITable::splitOption(opt));
667 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
669 std::vector<std::string> parts = split(element,';');
671 data->table_columns.clear();
672 for (size_t i = 0; i < parts.size(); ++i) {
673 std::vector<std::string> col_parts = split(parts[i],',');
674 GUITable::TableColumn column;
676 if (!col_parts.empty())
677 column.type = col_parts[0];
678 // Parse column options
679 for (size_t j = 1; j < col_parts.size(); ++j) {
680 std::string opt = unescape_string(col_parts[j]);
681 column.options.push_back(GUITable::splitOption(opt));
683 data->table_columns.push_back(column);
687 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
689 std::vector<std::string> parts = split(element,';');
691 if (((parts.size() == 4) || (parts.size() == 5)) ||
692 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
694 std::vector<std::string> v_pos = split(parts[0],',');
695 std::vector<std::string> v_geom = split(parts[1],',');
696 std::string name = parts[2];
697 std::vector<std::string> items = split(parts[3],',');
698 std::string str_initial_selection = "";
699 std::string str_transparent = "false";
701 if (parts.size() >= 5)
702 str_initial_selection = parts[4];
704 MY_CHECKPOS("table",0);
705 MY_CHECKGEOM("table",1);
708 pos.X += stof(v_pos[0]) * (float)spacing.X;
709 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
712 geom.X = stof(v_geom[0]) * (float)spacing.X;
713 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
715 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
724 spec.ftype = f_Table;
726 for (unsigned int i = 0; i < items.size(); ++i) {
727 items[i] = unescape_enriched(unescape_string(items[i]));
730 //now really show table
731 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
734 if (spec.fname == data->focused_fieldname) {
735 Environment->setFocus(e);
738 e->setTable(data->table_options, data->table_columns, items);
740 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
741 e->setDynamicData(data->table_dyndata[name]);
744 if ((str_initial_selection != "") &&
745 (str_initial_selection != "0"))
746 e->setSelected(stoi(str_initial_selection.c_str()));
748 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
749 m_fields.push_back(spec);
752 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
755 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
757 std::vector<std::string> parts = split(element,';');
759 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
760 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
762 std::vector<std::string> v_pos = split(parts[0],',');
763 std::vector<std::string> v_geom = split(parts[1],',');
764 std::string name = parts[2];
765 std::vector<std::string> items = split(parts[3],',');
766 std::string str_initial_selection = "";
767 std::string str_transparent = "false";
769 if (parts.size() >= 5)
770 str_initial_selection = parts[4];
772 if (parts.size() >= 6)
773 str_transparent = parts[5];
775 MY_CHECKPOS("textlist",0);
776 MY_CHECKGEOM("textlist",1);
779 pos.X += stof(v_pos[0]) * (float)spacing.X;
780 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
783 geom.X = stof(v_geom[0]) * (float)spacing.X;
784 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
787 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
796 spec.ftype = f_Table;
798 for (unsigned int i = 0; i < items.size(); ++i) {
799 items[i] = unescape_enriched(unescape_string(items[i]));
802 //now really show list
803 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
806 if (spec.fname == data->focused_fieldname) {
807 Environment->setFocus(e);
810 e->setTextList(items, is_yes(str_transparent));
812 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
813 e->setDynamicData(data->table_dyndata[name]);
816 if ((str_initial_selection != "") &&
817 (str_initial_selection != "0"))
818 e->setSelected(stoi(str_initial_selection.c_str()));
820 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
821 m_fields.push_back(spec);
824 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
828 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
830 std::vector<std::string> parts = split(element,';');
832 if ((parts.size() == 5) ||
833 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
835 std::vector<std::string> v_pos = split(parts[0],',');
836 std::string name = parts[2];
837 std::vector<std::string> items = split(parts[3],',');
838 std::string str_initial_selection = "";
839 str_initial_selection = parts[4];
841 MY_CHECKPOS("dropdown",0);
844 pos.X += stof(v_pos[0]) * (float)spacing.X;
845 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
847 s32 width = stof(parts[1]) * (float)spacing.Y;
849 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
850 pos.X + width, pos.Y + (m_btn_height * 2));
859 spec.ftype = f_DropDown;
862 //now really show list
863 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
865 if (spec.fname == data->focused_fieldname) {
866 Environment->setFocus(e);
869 for (unsigned int i=0; i < items.size(); i++) {
870 e->addItem(unescape_enriched(unescape_string(
871 utf8_to_wide(items[i]))).c_str());
874 if (str_initial_selection != "")
875 e->setSelected(stoi(str_initial_selection.c_str())-1);
877 m_fields.push_back(spec);
879 m_dropdowns.push_back(std::pair<FieldSpec,
880 std::vector<std::string> >(spec, std::vector<std::string>()));
881 std::vector<std::string> &values = m_dropdowns.back().second;
882 for (unsigned int i = 0; i < items.size(); i++) {
883 values.push_back(unescape_string(items[i]));
888 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
889 << element << "'" << std::endl;
892 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
894 std::vector<std::string> parts = split(element,';');
896 if ((parts.size() == 4) ||
897 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
899 std::vector<std::string> v_pos = split(parts[0],',');
900 std::vector<std::string> v_geom = split(parts[1],',');
901 std::string name = parts[2];
902 std::string label = parts[3];
904 MY_CHECKPOS("pwdfield",0);
905 MY_CHECKGEOM("pwdfield",1);
908 pos.X += stof(v_pos[0]) * (float)spacing.X;
909 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
912 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
914 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
915 pos.Y -= m_btn_height;
916 geom.Y = m_btn_height*2;
918 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
920 std::wstring wlabel = utf8_to_wide(unescape_string(label));
930 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
932 if (spec.fname == data->focused_fieldname) {
933 Environment->setFocus(e);
936 if (label.length() >= 1)
938 int font_height = g_fontengine->getTextHeight();
939 rect.UpperLeftCorner.Y -= font_height;
940 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
941 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
944 e->setPasswordBox(true,L'*');
947 evt.EventType = EET_KEY_INPUT_EVENT;
948 evt.KeyInput.Key = KEY_END;
949 evt.KeyInput.Char = 0;
950 evt.KeyInput.Control = 0;
951 evt.KeyInput.Shift = 0;
952 evt.KeyInput.PressedDown = true;
954 m_fields.push_back(spec);
957 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
960 void GUIFormSpecMenu::parseSimpleField(parserData* data,
961 std::vector<std::string> &parts)
963 std::string name = parts[0];
964 std::string label = parts[1];
965 std::string default_val = parts[2];
967 core::rect<s32> rect;
969 if(data->explicit_size)
970 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
972 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
973 pos.Y = ((m_fields.size()+2)*60);
974 v2s32 size = DesiredRect.getSize();
976 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
977 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
981 default_val = m_form_src->resolveText(default_val);
984 std::wstring wlabel = utf8_to_wide(unescape_string(label));
989 utf8_to_wide(unescape_string(default_val)),
995 // spec field id to 0, this stops submit searching for a value that isn't there
996 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1001 gui::IGUIElement *e;
1002 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1003 if (g_settings->getBool("freetype")) {
1004 e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1005 true, Environment, this, spec.fid, rect);
1011 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1013 if (spec.fname == data->focused_fieldname) {
1014 Environment->setFocus(e);
1018 evt.EventType = EET_KEY_INPUT_EVENT;
1019 evt.KeyInput.Key = KEY_END;
1020 evt.KeyInput.Char = 0;
1021 evt.KeyInput.Control = 0;
1022 evt.KeyInput.Shift = 0;
1023 evt.KeyInput.PressedDown = true;
1026 if (label.length() >= 1)
1028 int font_height = g_fontengine->getTextHeight();
1029 rect.UpperLeftCorner.Y -= font_height;
1030 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1031 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1035 m_fields.push_back(spec);
1038 void GUIFormSpecMenu::parseTextArea(parserData* data,
1039 std::vector<std::string>& parts,std::string type)
1042 std::vector<std::string> v_pos = split(parts[0],',');
1043 std::vector<std::string> v_geom = split(parts[1],',');
1044 std::string name = parts[2];
1045 std::string label = parts[3];
1046 std::string default_val = parts[4];
1048 MY_CHECKPOS(type,0);
1049 MY_CHECKGEOM(type,1);
1052 pos.X = stof(v_pos[0]) * (float) spacing.X;
1053 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1057 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1059 if (type == "textarea")
1061 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1062 pos.Y += m_btn_height;
1066 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1067 pos.Y -= m_btn_height;
1068 geom.Y = m_btn_height*2;
1071 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1073 if(!data->explicit_size)
1074 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1077 default_val = m_form_src->resolveText(default_val);
1080 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1085 utf8_to_wide(unescape_string(default_val)),
1091 // spec field id to 0, this stops submit searching for a value that isn't there
1092 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1098 gui::IGUIEditBox *e;
1099 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1100 if (g_settings->getBool("freetype")) {
1101 e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1102 true, Environment, this, spec.fid, rect);
1108 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1111 if (spec.fname == data->focused_fieldname) {
1112 Environment->setFocus(e);
1115 if (type == "textarea")
1117 e->setMultiLine(true);
1118 e->setWordWrap(true);
1119 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1122 evt.EventType = EET_KEY_INPUT_EVENT;
1123 evt.KeyInput.Key = KEY_END;
1124 evt.KeyInput.Char = 0;
1125 evt.KeyInput.Control = 0;
1126 evt.KeyInput.Shift = 0;
1127 evt.KeyInput.PressedDown = true;
1131 if (label.length() >= 1)
1133 int font_height = g_fontengine->getTextHeight();
1134 rect.UpperLeftCorner.Y -= font_height;
1135 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1136 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1139 m_fields.push_back(spec);
1142 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1145 std::vector<std::string> parts = split(element,';');
1147 if (parts.size() == 3 || parts.size() == 4) {
1148 parseSimpleField(data,parts);
1152 if ((parts.size() == 5) ||
1153 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1155 parseTextArea(data,parts,type);
1158 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1161 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1163 std::vector<std::string> parts = split(element,';');
1165 if ((parts.size() == 2) ||
1166 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1168 std::vector<std::string> v_pos = split(parts[0],',');
1169 std::string text = parts[1];
1171 MY_CHECKPOS("label",0);
1173 v2s32 pos = padding;
1174 pos.X += stof(v_pos[0]) * (float)spacing.X;
1175 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1177 if(!data->explicit_size)
1178 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1180 std::vector<std::string> lines = split(text, '\n');
1182 for (unsigned int i = 0; i != lines.size(); i++) {
1183 // Lines are spaced at the nominal distance of
1184 // 2/5 inventory slot, even if the font doesn't
1185 // quite match that. This provides consistent
1186 // form layout, at the expense of sometimes
1187 // having sub-optimal spacing for the font.
1188 // We multiply by 2 and then divide by 5, rather
1189 // than multiply by 0.4, to get exact results
1190 // in the integer cases: 0.4 is not exactly
1191 // representable in binary floating point.
1192 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1193 std::wstring wlabel = utf8_to_wide(unescape_string(lines[i]));
1194 core::rect<s32> rect = core::rect<s32>(
1195 pos.X, posy - m_btn_height,
1196 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1197 posy + m_btn_height);
1204 gui::IGUIStaticText *e =
1205 addStaticText(Environment, spec.flabel.c_str(),
1206 rect, false, false, this, spec.fid);
1207 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1209 m_fields.push_back(spec);
1214 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1217 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1219 std::vector<std::string> parts = split(element,';');
1221 if ((parts.size() == 2) ||
1222 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1224 std::vector<std::string> v_pos = split(parts[0],',');
1225 std::wstring text = unescape_enriched(
1226 unescape_string(utf8_to_wide(parts[1])));
1228 MY_CHECKPOS("vertlabel",1);
1230 v2s32 pos = padding;
1231 pos.X += stof(v_pos[0]) * (float)spacing.X;
1232 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1234 core::rect<s32> rect = core::rect<s32>(
1235 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1237 font_line_height(m_font)
1239 +((imgsize.Y/2)- m_btn_height));
1240 //actually text.length() would be correct but adding +1 avoids to break all mods
1242 if(!data->explicit_size)
1243 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1245 std::wstring label = L"";
1247 for (unsigned int i=0; i < text.length(); i++) {
1258 gui::IGUIStaticText *t =
1259 addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid);
1260 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1261 m_fields.push_back(spec);
1264 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1267 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1270 std::vector<std::string> parts = split(element,';');
1272 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1273 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1275 std::vector<std::string> v_pos = split(parts[0],',');
1276 std::vector<std::string> v_geom = split(parts[1],',');
1277 std::string image_name = parts[2];
1278 std::string name = parts[3];
1279 std::string label = parts[4];
1281 MY_CHECKPOS("imagebutton",0);
1282 MY_CHECKGEOM("imagebutton",1);
1284 v2s32 pos = padding;
1285 pos.X += stof(v_pos[0]) * (float)spacing.X;
1286 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1288 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1289 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1291 bool noclip = false;
1292 bool drawborder = true;
1293 std::string pressed_image_name = "";
1295 if (parts.size() >= 7) {
1296 if (parts[5] == "true")
1298 if (parts[6] == "false")
1302 if (parts.size() >= 8) {
1303 pressed_image_name = parts[7];
1306 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1308 if(!data->explicit_size)
1309 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1311 image_name = unescape_string(image_name);
1312 pressed_image_name = unescape_string(pressed_image_name);
1314 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1319 utf8_to_wide(image_name),
1322 spec.ftype = f_Button;
1323 if(type == "image_button_exit")
1324 spec.is_exit = true;
1326 video::ITexture *texture = 0;
1327 video::ITexture *pressed_texture = 0;
1328 texture = m_tsrc->getTexture(image_name);
1329 if (pressed_image_name != "")
1330 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1332 pressed_texture = texture;
1334 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1336 if (spec.fname == data->focused_fieldname) {
1337 Environment->setFocus(e);
1340 e->setUseAlphaChannel(true);
1341 e->setImage(guiScalingImageButton(
1342 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1343 e->setPressedImage(guiScalingImageButton(
1344 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1345 e->setScaleImage(true);
1346 e->setNotClipped(noclip);
1347 e->setDrawBorder(drawborder);
1349 m_fields.push_back(spec);
1353 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1356 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1358 std::vector<std::string> parts = split(element,';');
1360 if (((parts.size() == 4) || (parts.size() == 6)) ||
1361 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1363 std::vector<std::string> v_pos = split(parts[0],',');
1364 std::string name = parts[1];
1365 std::vector<std::string> buttons = split(parts[2],',');
1366 std::string str_index = parts[3];
1367 bool show_background = true;
1368 bool show_border = true;
1369 int tab_index = stoi(str_index) -1;
1371 MY_CHECKPOS("tabheader",0);
1373 if (parts.size() == 6) {
1374 if (parts[4] == "true")
1375 show_background = false;
1376 if (parts[5] == "false")
1377 show_border = false;
1387 spec.ftype = f_TabHeader;
1390 pos.X += stof(v_pos[0]) * (float)spacing.X;
1391 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1393 geom.X = DesiredRect.getWidth();
1394 geom.Y = m_btn_height*2;
1396 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1399 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1400 show_background, show_border, spec.fid);
1401 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1402 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1403 e->setTabHeight(m_btn_height*2);
1405 if (spec.fname == data->focused_fieldname) {
1406 Environment->setFocus(e);
1409 e->setNotClipped(true);
1411 for (unsigned int i = 0; i < buttons.size(); i++) {
1412 e->addTab(unescape_enriched(unescape_string(
1413 utf8_to_wide(buttons[i]))).c_str(), -1);
1416 if ((tab_index >= 0) &&
1417 (buttons.size() < INT_MAX) &&
1418 (tab_index < (int) buttons.size()))
1419 e->setActiveTab(tab_index);
1421 m_fields.push_back(spec);
1424 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1425 << element << "'" << std::endl;
1428 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1431 if (m_gamedef == 0) {
1432 warningstream << "invalid use of item_image_button with m_gamedef==0"
1437 std::vector<std::string> parts = split(element,';');
1439 if ((parts.size() == 5) ||
1440 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1442 std::vector<std::string> v_pos = split(parts[0],',');
1443 std::vector<std::string> v_geom = split(parts[1],',');
1444 std::string item_name = parts[2];
1445 std::string name = parts[3];
1446 std::string label = parts[4];
1448 label = unescape_string(label);
1449 item_name = unescape_string(item_name);
1451 MY_CHECKPOS("itemimagebutton",0);
1452 MY_CHECKGEOM("itemimagebutton",1);
1454 v2s32 pos = padding;
1455 pos.X += stof(v_pos[0]) * (float)spacing.X;
1456 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1458 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1459 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1461 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1463 if(!data->explicit_size)
1464 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1466 IItemDefManager *idef = m_gamedef->idef();
1468 item.deSerialize(item_name, idef);
1471 TooltipSpec(item.getDefinition(idef).description,
1472 m_default_tooltip_bgcolor,
1473 m_default_tooltip_color);
1477 utf8_to_wide(label),
1478 utf8_to_wide(item_name),
1479 258 + m_fields.size()
1482 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1484 if (spec.fname == data->focused_fieldname) {
1485 Environment->setFocus(e);
1488 spec.ftype = f_Button;
1489 rect+=data->basepos-padding;
1491 m_fields.push_back(spec);
1492 pos = padding + AbsoluteRect.UpperLeftCorner;
1493 pos.X += stof(v_pos[0]) * (float) spacing.X;
1494 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1495 m_itemimages.push_back(ImageDrawSpec("", item_name, e, pos, geom));
1496 m_static_texts.push_back(StaticTextSpec(utf8_to_wide(label), rect, e));
1499 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1502 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1504 std::vector<std::string> parts = split(element,';');
1506 if ((parts.size() == 3) ||
1507 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1509 std::vector<std::string> v_pos = split(parts[0],',');
1510 std::vector<std::string> v_geom = split(parts[1],',');
1512 MY_CHECKPOS("box",0);
1513 MY_CHECKGEOM("box",1);
1515 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1516 pos.X += stof(v_pos[0]) * (float) spacing.X;
1517 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1520 geom.X = stof(v_geom[0]) * (float)spacing.X;
1521 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1523 video::SColor tmp_color;
1525 if (parseColorString(parts[2], tmp_color, false)) {
1526 BoxDrawSpec spec(pos, geom, tmp_color);
1528 m_boxes.push_back(spec);
1531 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1535 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1538 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1540 std::vector<std::string> parts = split(element,';');
1542 if (((parts.size() == 1) || (parts.size() == 2)) ||
1543 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1545 parseColorString(parts[0],m_bgcolor,false);
1547 if (parts.size() == 2) {
1548 std::string fullscreen = parts[1];
1549 m_bgfullscreen = is_yes(fullscreen);
1553 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1556 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1558 std::vector<std::string> parts = split(element,';');
1560 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1561 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1563 parseColorString(parts[0], m_slotbg_n, false);
1564 parseColorString(parts[1], m_slotbg_h, false);
1566 if (parts.size() >= 3) {
1567 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1568 m_slotborder = true;
1571 if (parts.size() == 5) {
1572 video::SColor tmp_color;
1574 if (parseColorString(parts[3], tmp_color, false))
1575 m_default_tooltip_bgcolor = tmp_color;
1576 if (parseColorString(parts[4], tmp_color, false))
1577 m_default_tooltip_color = tmp_color;
1581 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1584 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1586 std::vector<std::string> parts = split(element,';');
1587 if (parts.size() == 2) {
1588 std::string name = parts[0];
1589 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1590 m_default_tooltip_bgcolor, m_default_tooltip_color);
1592 } else if (parts.size() == 4) {
1593 std::string name = parts[0];
1594 video::SColor tmp_color1, tmp_color2;
1595 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1596 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1597 tmp_color1, tmp_color2);
1601 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1604 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1610 std::vector<std::string> parts = split(data,'[');
1612 if (parts.size() < 2) {
1616 if (parts[0] != "formspec_version") {
1620 if (is_number(parts[1])) {
1621 m_formspec_version = mystoi(parts[1]);
1628 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1633 std::vector<std::string> parts = split(element,'[');
1635 if (parts.size() < 2)
1638 std::string type = trim(parts[0]);
1639 std::string description = trim(parts[1]);
1641 if (type != "size" && type != "invsize")
1644 if (type == "invsize")
1645 log_deprecated("Deprecated formspec element \"invsize\" is used");
1647 parseSize(data, description);
1652 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1658 std::vector<std::string> parts = split(element,'[');
1660 // ugly workaround to keep compatibility
1661 if (parts.size() > 2) {
1662 if (trim(parts[0]) == "image") {
1663 for (unsigned int i=2;i< parts.size(); i++) {
1664 parts[1] += "[" + parts[i];
1670 if (parts.size() < 2) {
1674 std::string type = trim(parts[0]);
1675 std::string description = trim(parts[1]);
1677 if (type == "list") {
1678 parseList(data,description);
1682 if (type == "listring") {
1683 parseListRing(data, description);
1687 if (type == "checkbox") {
1688 parseCheckbox(data,description);
1692 if (type == "image") {
1693 parseImage(data,description);
1697 if (type == "item_image") {
1698 parseItemImage(data,description);
1702 if ((type == "button") || (type == "button_exit")) {
1703 parseButton(data,description,type);
1707 if (type == "background") {
1708 parseBackground(data,description);
1712 if (type == "tableoptions"){
1713 parseTableOptions(data,description);
1717 if (type == "tablecolumns"){
1718 parseTableColumns(data,description);
1722 if (type == "table"){
1723 parseTable(data,description);
1727 if (type == "textlist"){
1728 parseTextList(data,description);
1732 if (type == "dropdown"){
1733 parseDropDown(data,description);
1737 if (type == "pwdfield") {
1738 parsePwdField(data,description);
1742 if ((type == "field") || (type == "textarea")){
1743 parseField(data,description,type);
1747 if (type == "label") {
1748 parseLabel(data,description);
1752 if (type == "vertlabel") {
1753 parseVertLabel(data,description);
1757 if (type == "item_image_button") {
1758 parseItemImageButton(data,description);
1762 if ((type == "image_button") || (type == "image_button_exit")) {
1763 parseImageButton(data,description,type);
1767 if (type == "tabheader") {
1768 parseTabHeader(data,description);
1772 if (type == "box") {
1773 parseBox(data,description);
1777 if (type == "bgcolor") {
1778 parseBackgroundColor(data,description);
1782 if (type == "listcolors") {
1783 parseListColors(data,description);
1787 if (type == "tooltip") {
1788 parseTooltip(data,description);
1792 if (type == "scrollbar") {
1793 parseScrollBar(data, description);
1799 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1803 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1805 /* useless to regenerate without a screensize */
1806 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1813 for (u32 i = 0; i < m_tables.size(); ++i) {
1814 std::string tablename = m_tables[i].first.fname;
1815 GUITable *table = m_tables[i].second;
1816 mydata.table_dyndata[tablename] = table->getDynamicData();
1820 if (!m_focused_element.empty())
1821 mydata.focused_fieldname = m_focused_element;
1824 gui::IGUIElement *focused_element = Environment->getFocus();
1825 if (focused_element && focused_element->getParent() == this) {
1826 s32 focused_id = focused_element->getID();
1827 if (focused_id > 257) {
1828 for (u32 i=0; i<m_fields.size(); i++) {
1829 if (m_fields[i].fid == focused_id) {
1830 mydata.focused_fieldname =
1841 for (u32 i = 0; i < m_tables.size(); ++i) {
1842 GUITable *table = m_tables[i].second;
1846 mydata.size= v2s32(100,100);
1847 mydata.screensize = screensize;
1849 // Base position of contents of form
1850 mydata.basepos = getBasePos();
1852 /* Convert m_init_draw_spec to m_inventorylists */
1854 m_inventorylists.clear();
1856 m_backgrounds.clear();
1857 m_itemimages.clear();
1859 m_checkboxes.clear();
1860 m_scrollbars.clear();
1864 m_inventory_rings.clear();
1865 m_static_texts.clear();
1867 // Set default values (fits old formspec values)
1868 m_bgcolor = video::SColor(140,0,0,0);
1869 m_bgfullscreen = false;
1871 m_slotbg_n = video::SColor(255,128,128,128);
1872 m_slotbg_h = video::SColor(255,192,192,192);
1874 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1875 m_default_tooltip_color = video::SColor(255,255,255,255);
1877 m_slotbordercolor = video::SColor(200,0,0,0);
1878 m_slotborder = false;
1880 m_clipbackground = false;
1883 assert(m_tooltip_element == NULL);
1884 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1885 m_tooltip_element = addStaticText(Environment, L"",core::rect<s32>(0,0,110,18));
1886 m_tooltip_element->enableOverrideColor(true);
1887 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1888 m_tooltip_element->setDrawBackground(true);
1889 m_tooltip_element->setDrawBorder(true);
1890 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1891 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1892 m_tooltip_element->setWordWrap(false);
1893 //we're not parent so no autograb for this one!
1894 m_tooltip_element->grab();
1897 std::vector<std::string> elements = split(m_formspec_string,']');
1900 /* try to read version from first element only */
1901 if (elements.size() >= 1) {
1902 if ( parseVersionDirect(elements[0]) ) {
1907 /* we need size first in order to calculate image scale */
1908 mydata.explicit_size = false;
1909 for (; i< elements.size(); i++) {
1910 if (!parseSizeDirect(&mydata, elements[i])) {
1915 if (mydata.explicit_size) {
1916 // compute scaling for specified form size
1918 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1919 v2u32 delta = current_screensize - m_lockscreensize;
1921 if (current_screensize.Y > m_lockscreensize.Y)
1926 if (current_screensize.X > m_lockscreensize.X)
1931 offset = v2s32(delta.X,delta.Y);
1933 mydata.screensize = m_lockscreensize;
1935 offset = v2s32(0,0);
1938 double gui_scaling = g_settings->getFloat("gui_scaling");
1939 double screen_dpi = porting::getDisplayDensity() * 96;
1943 // In fixed-size mode, inventory image size
1944 // is 0.53 inch multiplied by the gui_scaling
1945 // config parameter. This magic size is chosen
1946 // to make the main menu (15.5 inventory images
1947 // wide, including border) just fit into the
1948 // default window (800 pixels wide) at 96 DPI
1949 // and default scaling (1.00).
1950 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1952 // In variable-size mode, we prefer to make the
1953 // inventory image size 1/15 of screen height,
1954 // multiplied by the gui_scaling config parameter.
1955 // If the preferred size won't fit the whole
1956 // form on the screen, either horizontally or
1957 // vertically, then we scale it down to fit.
1958 // (The magic numbers in the computation of what
1959 // fits arise from the scaling factors in the
1960 // following stanza, including the form border,
1961 // help text space, and 0.1 inventory slot spare.)
1962 // However, a minimum size is also set, that
1963 // the image size can't be less than 0.3 inch
1964 // multiplied by gui_scaling, even if this means
1965 // the form doesn't fit the screen.
1966 double prefer_imgsize = mydata.screensize.Y / 15 *
1968 double fitx_imgsize = mydata.screensize.X /
1969 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1970 double fity_imgsize = mydata.screensize.Y /
1971 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1972 double screen_dpi = porting::getDisplayDensity() * 96;
1973 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1974 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1975 MYMIN(fitx_imgsize, fity_imgsize)));
1978 // Everything else is scaled in proportion to the
1979 // inventory image size. The inventory slot spacing
1980 // is 5/4 image size horizontally and 15/13 image size
1981 // vertically. The padding around the form (incorporating
1982 // the border of the outer inventory slots) is 3/8
1983 // image size. Font height (baseline to baseline)
1984 // is 2/5 vertical inventory slot spacing, and button
1985 // half-height is 7/8 of font height.
1986 imgsize = v2s32(use_imgsize, use_imgsize);
1987 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
1988 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
1989 m_btn_height = use_imgsize*15.0/13 * 0.35;
1991 m_font = g_fontengine->getFont();
1993 mydata.size = v2s32(
1994 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
1995 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
1997 DesiredRect = mydata.rect = core::rect<s32>(
1998 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
1999 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
2000 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
2001 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
2004 // Non-size[] form must consist only of text fields and
2005 // implicit "Proceed" button. Use default font, and
2006 // temporary form size which will be recalculated below.
2007 m_font = g_fontengine->getFont();
2008 m_btn_height = font_line_height(m_font) * 0.875;
2009 DesiredRect = core::rect<s32>(
2010 mydata.screensize.X/2 - 580/2,
2011 mydata.screensize.Y/2 - 300/2,
2012 mydata.screensize.X/2 + 580/2,
2013 mydata.screensize.Y/2 + 300/2
2016 recalculateAbsolutePosition(false);
2017 mydata.basepos = getBasePos();
2018 m_tooltip_element->setOverrideFont(m_font);
2020 gui::IGUISkin* skin = Environment->getSkin();
2021 sanity_check(skin != NULL);
2022 gui::IGUIFont *old_font = skin->getFont();
2023 skin->setFont(m_font);
2025 for (; i< elements.size(); i++) {
2026 parseElement(&mydata, elements[i]);
2029 // If there are fields without explicit size[], add a "Proceed"
2030 // button and adjust size to fit all the fields.
2031 if (m_fields.size() && !mydata.explicit_size) {
2032 mydata.rect = core::rect<s32>(
2033 mydata.screensize.X/2 - 580/2,
2034 mydata.screensize.Y/2 - 300/2,
2035 mydata.screensize.X/2 + 580/2,
2036 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2038 DesiredRect = mydata.rect;
2039 recalculateAbsolutePosition(false);
2040 mydata.basepos = getBasePos();
2043 v2s32 pos = mydata.basepos;
2044 pos.Y = ((m_fields.size()+2)*60);
2046 v2s32 size = DesiredRect.getSize();
2048 core::rect<s32>(size.X/2-70, pos.Y,
2049 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2050 const wchar_t *text = wgettext("Proceed");
2051 Environment->addButton(mydata.rect, this, 257, text);
2057 //set initial focus if parser didn't set it
2058 focused_element = Environment->getFocus();
2059 if (!focused_element
2060 || !isMyChild(focused_element)
2061 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2064 skin->setFont(old_font);
2068 bool GUIFormSpecMenu::getAndroidUIInput()
2070 /* no dialog shown */
2071 if (m_JavaDialogFieldName == "") {
2076 if (porting::getInputDialogState() == -1) {
2080 std::string fieldname = m_JavaDialogFieldName;
2081 m_JavaDialogFieldName = "";
2083 /* no value abort dialog processing */
2084 if (porting::getInputDialogState() != 0) {
2088 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2089 iter != m_fields.end(); ++iter) {
2091 if (iter->fname != fieldname) {
2094 IGUIElement* tochange = getElementFromId(iter->fid);
2096 if (tochange == 0) {
2100 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2104 std::string text = porting::getInputDialogValue();
2106 ((gui::IGUIEditBox*) tochange)->
2107 setText(utf8_to_wide(text).c_str());
2113 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2115 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2117 for(u32 i=0; i<m_inventorylists.size(); i++)
2119 const ListDrawSpec &s = m_inventorylists[i];
2121 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2122 s32 item_i = i + s.start_item_i;
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 + p0;
2127 if(rect.isPointInside(p))
2129 return ItemSpec(s.inventoryloc, s.listname, item_i);
2134 return ItemSpec(InventoryLocation(), "", -1);
2137 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
2140 video::IVideoDriver* driver = Environment->getVideoDriver();
2142 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2144 warningstream<<"GUIFormSpecMenu::drawList(): "
2145 <<"The inventory location "
2146 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2150 InventoryList *ilist = inv->getList(s.listname);
2152 warningstream<<"GUIFormSpecMenu::drawList(): "
2153 <<"The inventory list \""<<s.listname<<"\" @ \""
2154 <<s.inventoryloc.dump()<<"\" doesn't exist"
2159 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2161 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2163 s32 item_i = i + s.start_item_i;
2164 if(item_i >= (s32) ilist->getSize())
2166 s32 x = (i%s.geom.X) * spacing.X;
2167 s32 y = (i/s.geom.X) * spacing.Y;
2169 core::rect<s32> rect = imgrect + s.pos + p;
2172 item = ilist->getItem(item_i);
2174 bool selected = m_selected_item
2175 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2176 && m_selected_item->listname == s.listname
2177 && m_selected_item->i == item_i;
2178 bool hovering = rect.isPointInside(m_pointer);
2179 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2180 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2184 item_hovered = true;
2185 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2187 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2191 //Draw inv slot borders
2193 s32 x1 = rect.UpperLeftCorner.X;
2194 s32 y1 = rect.UpperLeftCorner.Y;
2195 s32 x2 = rect.LowerRightCorner.X;
2196 s32 y2 = rect.LowerRightCorner.Y;
2198 driver->draw2DRectangle(m_slotbordercolor,
2199 core::rect<s32>(v2s32(x1 - border, y1 - border),
2200 v2s32(x2 + border, y1)), NULL);
2201 driver->draw2DRectangle(m_slotbordercolor,
2202 core::rect<s32>(v2s32(x1 - border, y2),
2203 v2s32(x2 + border, y2 + border)), NULL);
2204 driver->draw2DRectangle(m_slotbordercolor,
2205 core::rect<s32>(v2s32(x1 - border, y1),
2206 v2s32(x1, y2)), NULL);
2207 driver->draw2DRectangle(m_slotbordercolor,
2208 core::rect<s32>(v2s32(x2, y1),
2209 v2s32(x2 + border, y2)), NULL);
2217 item.takeItem(m_selected_amount);
2221 drawItemStack(driver, m_font, item,
2222 rect, &AbsoluteClippingRect, m_gamedef,
2227 std::wstring tooltip_text = L"";
2228 if (hovering && !m_selected_item) {
2229 tooltip_text = utf8_to_wide(item.getDefinition(m_gamedef->idef()).description);
2231 if (tooltip_text != L"") {
2232 std::vector<std::wstring> tt_rows = str_split(tooltip_text, L'\n');
2233 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2234 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2235 m_tooltip_element->setVisible(true);
2236 this->bringToFront(m_tooltip_element);
2237 setStaticText(m_tooltip_element, tooltip_text.c_str());
2238 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2239 #if IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2
2240 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2242 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2244 v2u32 screenSize = driver->getScreenSize();
2245 int tooltip_offset_x = m_btn_height;
2246 int tooltip_offset_y = m_btn_height;
2248 tooltip_offset_x *= 3;
2249 tooltip_offset_y = 0;
2250 if (m_pointer.X > (s32)screenSize.X / 2)
2251 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2253 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2254 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2255 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2256 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2257 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2258 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2259 m_tooltip_element->setRelativePosition(core::rect<s32>(
2260 core::position2d<s32>(tooltip_x, tooltip_y),
2261 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2267 void GUIFormSpecMenu::drawSelectedItem()
2269 video::IVideoDriver* driver = Environment->getVideoDriver();
2271 if (!m_selected_item) {
2272 drawItemStack(driver, m_font, ItemStack(),
2273 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2274 NULL, m_gamedef, IT_ROT_DRAGGED);
2278 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2280 InventoryList *list = inv->getList(m_selected_item->listname);
2282 ItemStack stack = list->getItem(m_selected_item->i);
2283 stack.count = m_selected_amount;
2285 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2286 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2287 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, IT_ROT_DRAGGED);
2290 void GUIFormSpecMenu::drawMenu()
2293 std::string newform = m_form_src->getForm();
2294 if(newform != m_formspec_string){
2295 m_formspec_string = newform;
2296 regenerateGui(m_screensize_old);
2300 gui::IGUISkin* skin = Environment->getSkin();
2301 sanity_check(skin != NULL);
2302 gui::IGUIFont *old_font = skin->getFont();
2303 skin->setFont(m_font);
2305 updateSelectedItem();
2307 video::IVideoDriver* driver = Environment->getVideoDriver();
2309 v2u32 screenSize = driver->getScreenSize();
2310 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2312 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2314 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2316 m_tooltip_element->setVisible(false);
2321 for(u32 i=0; i<m_backgrounds.size(); i++)
2323 const ImageDrawSpec &spec = m_backgrounds[i];
2324 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2327 // Image size on screen
2328 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2329 // Image rectangle on screen
2330 core::rect<s32> rect = imgrect + spec.pos;
2332 if (m_clipbackground) {
2333 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2334 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2335 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2336 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2337 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2340 const video::SColor color(255,255,255,255);
2341 const video::SColor colors[] = {color,color,color,color};
2342 draw2DImageFilterScaled(driver, texture, rect,
2343 core::rect<s32>(core::position2d<s32>(0,0),
2344 core::dimension2di(texture->getOriginalSize())),
2345 NULL/*&AbsoluteClippingRect*/, colors, true);
2348 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2349 errorstream << "\t" << spec.name << std::endl;
2356 for(u32 i=0; i<m_boxes.size(); i++)
2358 const BoxDrawSpec &spec = m_boxes[i];
2360 irr::video::SColor todraw = spec.color;
2362 todraw.setAlpha(140);
2364 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2365 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2367 driver->draw2DRectangle(todraw, rect, 0);
2373 gui::IGUIElement::draw();
2378 for(u32 i=0; i<m_images.size(); i++)
2380 const ImageDrawSpec &spec = m_images[i];
2381 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2384 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2385 // Image size on screen
2386 core::rect<s32> imgrect;
2389 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2392 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2394 // Image rectangle on screen
2395 core::rect<s32> rect = imgrect + spec.pos;
2396 const video::SColor color(255,255,255,255);
2397 const video::SColor colors[] = {color,color,color,color};
2398 draw2DImageFilterScaled(driver, texture, rect,
2399 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2400 NULL/*&AbsoluteClippingRect*/, colors, true);
2403 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2404 errorstream << "\t" << spec.name << std::endl;
2411 for(u32 i=0; i<m_itemimages.size(); i++)
2416 const ImageDrawSpec &spec = m_itemimages[i];
2417 IItemDefManager *idef = m_gamedef->idef();
2419 item.deSerialize(spec.item_name, idef);
2420 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2421 // Viewport rectangle on screen
2422 core::rect<s32> rect = imgrect + spec.pos;
2423 if (spec.parent_button && spec.parent_button->isPressed()) {
2424 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2425 rect += core::dimension2d<s32>(
2426 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2428 rect += core::dimension2d<s32>(
2429 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2430 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2433 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2434 m_gamedef, IT_ROT_NONE);
2439 Phase 0: Item slot rectangles
2440 Phase 1: Item images; prepare tooltip
2442 bool item_hovered = false;
2443 int start_phase = 0;
2444 for (int phase = start_phase; phase <= 1; phase++) {
2445 for (u32 i = 0; i < m_inventorylists.size(); i++) {
2446 drawList(m_inventorylists[i], phase, item_hovered);
2449 if (!item_hovered) {
2450 drawItemStack(driver, m_font, ItemStack(),
2451 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2452 NULL, m_gamedef, IT_ROT_HOVERED);
2455 /* TODO find way to show tooltips on touchscreen */
2456 #ifndef HAVE_TOUCHSCREENGUI
2457 m_pointer = m_device->getCursorControl()->getPosition();
2461 Draw static text elements
2463 for (u32 i = 0; i < m_static_texts.size(); i++) {
2464 const StaticTextSpec &spec = m_static_texts[i];
2465 core::rect<s32> rect = spec.rect;
2466 if (spec.parent_button && spec.parent_button->isPressed()) {
2467 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2468 rect += core::dimension2d<s32>(
2469 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2471 // Use image offset instead of text's because its a bit smaller
2472 // and fits better, also TEXT_OFFSET_X is always 0
2473 rect += core::dimension2d<s32>(
2474 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2475 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2478 video::SColor color(255, 255, 255, 255);
2479 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
2483 Draw fields/buttons tooltips
2485 gui::IGUIElement *hovered =
2486 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2488 if (hovered != NULL) {
2489 s32 id = hovered->getID();
2493 m_old_tooltip_id = id;
2494 m_old_tooltip = L"";
2496 if (id == m_old_tooltip_id) {
2497 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2499 m_hovered_time = getTimeMs();
2500 m_old_tooltip_id = id;
2504 if (id != -1 && delta >= m_tooltip_show_delay) {
2505 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2506 iter != m_fields.end(); ++iter) {
2507 if (iter->fid == id && m_tooltips[iter->fname].tooltip != L"") {
2508 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2509 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2510 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2511 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2512 setStaticText(m_tooltip_element, m_tooltips[iter->fname].tooltip.c_str());
2513 std::vector<std::wstring> tt_rows = str_split(m_tooltips[iter->fname].tooltip, L'\n');
2514 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2515 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2516 int tooltip_offset_x = m_btn_height;
2517 int tooltip_offset_y = m_btn_height;
2519 tooltip_offset_x *= 3;
2520 tooltip_offset_y = 0;
2521 if (m_pointer.X > (s32)screenSize.X / 2)
2522 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2524 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2525 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2526 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2527 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2528 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2529 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2530 m_tooltip_element->setRelativePosition(core::rect<s32>(
2531 core::position2d<s32>(tooltip_x, tooltip_y),
2532 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2534 m_tooltip_element->setVisible(true);
2535 this->bringToFront(m_tooltip_element);
2542 m_tooltip_element->draw();
2545 Draw dragged item stack
2549 skin->setFont(old_font);
2552 void GUIFormSpecMenu::updateSelectedItem()
2554 // If the selected stack has become empty for some reason, deselect it.
2555 // If the selected stack has become inaccessible, deselect it.
2556 // If the selected stack has become smaller, adjust m_selected_amount.
2557 ItemStack selected = verifySelectedItem();
2559 // WARNING: BLACK MAGIC
2560 // See if there is a stack suited for our current guess.
2561 // If such stack does not exist, clear the guess.
2562 if(m_selected_content_guess.name != "" &&
2563 selected.name == m_selected_content_guess.name &&
2564 selected.count == m_selected_content_guess.count){
2565 // Selected item fits the guess. Skip the black magic.
2567 else if(m_selected_content_guess.name != ""){
2569 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2570 const ListDrawSpec &s = m_inventorylists[i];
2571 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2574 InventoryList *list = inv->getList(s.listname);
2577 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2578 u32 item_i = i + s.start_item_i;
2579 if(item_i >= list->getSize())
2581 ItemStack stack = list->getItem(item_i);
2582 if(stack.name == m_selected_content_guess.name &&
2583 stack.count == m_selected_content_guess.count){
2585 infostream<<"Client: Changing selected content guess to "
2586 <<s.inventoryloc.dump()<<" "<<s.listname
2587 <<" "<<item_i<<std::endl;
2588 delete m_selected_item;
2589 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2590 m_selected_amount = stack.count;
2595 infostream<<"Client: Discarding selected content guess: "
2596 <<m_selected_content_guess.getItemString()<<std::endl;
2597 m_selected_content_guess.name = "";
2601 // If craftresult is nonempty and nothing else is selected, select it now.
2602 if(!m_selected_item)
2604 for(u32 i=0; i<m_inventorylists.size(); i++)
2606 const ListDrawSpec &s = m_inventorylists[i];
2607 if(s.listname == "craftpreview")
2609 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2610 InventoryList *list = inv->getList("craftresult");
2611 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2613 m_selected_item = new ItemSpec;
2614 m_selected_item->inventoryloc = s.inventoryloc;
2615 m_selected_item->listname = "craftresult";
2616 m_selected_item->i = 0;
2617 m_selected_amount = 0;
2618 m_selected_dragging = false;
2625 // If craftresult is selected, keep the whole stack selected
2626 if(m_selected_item && m_selected_item->listname == "craftresult")
2628 m_selected_amount = verifySelectedItem().count;
2632 ItemStack GUIFormSpecMenu::verifySelectedItem()
2634 // If the selected stack has become empty for some reason, deselect it.
2635 // If the selected stack has become inaccessible, deselect it.
2636 // If the selected stack has become smaller, adjust m_selected_amount.
2637 // Return the selected stack.
2641 if(m_selected_item->isValid())
2643 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2646 InventoryList *list = inv->getList(m_selected_item->listname);
2647 if(list && (u32) m_selected_item->i < list->getSize())
2649 ItemStack stack = list->getItem(m_selected_item->i);
2650 if(m_selected_amount > stack.count)
2651 m_selected_amount = stack.count;
2658 // selection was not valid
2659 delete m_selected_item;
2660 m_selected_item = NULL;
2661 m_selected_amount = 0;
2662 m_selected_dragging = false;
2667 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2673 if (quitmode == quit_mode_accept) {
2674 fields["quit"] = "true";
2677 if (quitmode == quit_mode_cancel) {
2678 fields["quit"] = "true";
2679 m_text_dst->gotText(fields);
2683 if (current_keys_pending.key_down) {
2684 fields["key_down"] = "true";
2685 current_keys_pending.key_down = false;
2688 if (current_keys_pending.key_up) {
2689 fields["key_up"] = "true";
2690 current_keys_pending.key_up = false;
2693 if (current_keys_pending.key_enter) {
2694 fields["key_enter"] = "true";
2695 current_keys_pending.key_enter = false;
2698 if (current_keys_pending.key_escape) {
2699 fields["key_escape"] = "true";
2700 current_keys_pending.key_escape = false;
2703 for(unsigned int i=0; i<m_fields.size(); i++) {
2704 const FieldSpec &s = m_fields[i];
2706 std::string name = s.fname;
2707 if (s.ftype == f_Button) {
2708 fields[name] = wide_to_utf8(s.flabel);
2709 } else if (s.ftype == f_Table) {
2710 GUITable *table = getTable(s.fname);
2712 fields[name] = table->checkEvent();
2715 else if(s.ftype == f_DropDown) {
2716 // no dynamic cast possible due to some distributions shipped
2717 // without rtti support in irrlicht
2718 IGUIElement * element = getElementFromId(s.fid);
2719 gui::IGUIComboBox *e = NULL;
2720 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2721 e = static_cast<gui::IGUIComboBox*>(element);
2723 s32 selected = e->getSelected();
2724 if (selected >= 0) {
2725 std::vector<std::string> *dropdown_values =
2726 getDropDownValues(s.fname);
2727 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2728 fields[name] = (*dropdown_values)[selected];
2732 else if (s.ftype == f_TabHeader) {
2733 // no dynamic cast possible due to some distributions shipped
2734 // without rtti support in irrlicht
2735 IGUIElement * element = getElementFromId(s.fid);
2736 gui::IGUITabControl *e = NULL;
2737 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2738 e = static_cast<gui::IGUITabControl*>(element);
2742 std::stringstream ss;
2743 ss << (e->getActiveTab() +1);
2744 fields[name] = ss.str();
2747 else if (s.ftype == f_CheckBox) {
2748 // no dynamic cast possible due to some distributions shipped
2749 // without rtti support in irrlicht
2750 IGUIElement * element = getElementFromId(s.fid);
2751 gui::IGUICheckBox *e = NULL;
2752 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2753 e = static_cast<gui::IGUICheckBox*>(element);
2758 fields[name] = "true";
2760 fields[name] = "false";
2763 else if (s.ftype == f_ScrollBar) {
2764 // no dynamic cast possible due to some distributions shipped
2765 // without rtti support in irrlicht
2766 IGUIElement * element = getElementFromId(s.fid);
2767 gui::IGUIScrollBar *e = NULL;
2768 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2769 e = static_cast<gui::IGUIScrollBar*>(element);
2773 std::stringstream os;
2775 if (s.fdefault == L"Changed")
2776 fields[name] = "CHG:" + os.str();
2778 fields[name] = "VAL:" + os.str();
2783 IGUIElement* e = getElementFromId(s.fid);
2785 fields[name] = wide_to_utf8(e->getText());
2791 m_text_dst->gotText(fields);
2795 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2797 while(tocheck != NULL) {
2798 if (tocheck == parent) {
2801 tocheck = tocheck->getParent();
2806 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2808 // The IGUITabControl renders visually using the skin's selected
2809 // font, which we override for the duration of form drawing,
2810 // but computes tab hotspots based on how it would have rendered
2811 // using the font that is selected at the time of button release.
2812 // To make these two consistent, temporarily override the skin's
2813 // font while the IGUITabControl is processing the event.
2814 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2815 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2816 s32 x = event.MouseInput.X;
2817 s32 y = event.MouseInput.Y;
2818 gui::IGUIElement *hovered =
2819 Environment->getRootGUIElement()->getElementFromPoint(
2820 core::position2d<s32>(x, y));
2821 if (hovered && isMyChild(hovered) &&
2822 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2823 gui::IGUISkin* skin = Environment->getSkin();
2824 sanity_check(skin != NULL);
2825 gui::IGUIFont *old_font = skin->getFont();
2826 skin->setFont(m_font);
2827 bool retval = hovered->OnEvent(event);
2828 skin->setFont(old_font);
2833 // Fix Esc/Return key being eaten by checkboxen and tables
2834 if(event.EventType==EET_KEY_INPUT_EVENT) {
2835 KeyPress kp(event.KeyInput);
2836 if (kp == EscapeKey || kp == CancelKey
2837 || kp == getKeySetting("keymap_inventory")
2838 || event.KeyInput.Key==KEY_RETURN) {
2839 gui::IGUIElement *focused = Environment->getFocus();
2840 if (focused && isMyChild(focused) &&
2841 (focused->getType() == gui::EGUIET_LIST_BOX ||
2842 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2848 // Mouse wheel events: send to hovered element instead of focused
2849 if(event.EventType==EET_MOUSE_INPUT_EVENT
2850 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2851 s32 x = event.MouseInput.X;
2852 s32 y = event.MouseInput.Y;
2853 gui::IGUIElement *hovered =
2854 Environment->getRootGUIElement()->getElementFromPoint(
2855 core::position2d<s32>(x, y));
2856 if (hovered && isMyChild(hovered)) {
2857 hovered->OnEvent(event);
2862 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2863 s32 x = event.MouseInput.X;
2864 s32 y = event.MouseInput.Y;
2865 gui::IGUIElement *hovered =
2866 Environment->getRootGUIElement()->getElementFromPoint(
2867 core::position2d<s32>(x, y));
2868 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2869 m_old_tooltip_id = -1;
2870 m_old_tooltip = L"";
2872 if (!isChild(hovered,this)) {
2873 if (DoubleClickDetection(event)) {
2880 // display software keyboard when clicking edit boxes
2881 if (event.EventType == EET_MOUSE_INPUT_EVENT
2882 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2883 gui::IGUIElement *hovered =
2884 Environment->getRootGUIElement()->getElementFromPoint(
2885 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2886 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2887 bool retval = hovered->OnEvent(event);
2889 Environment->setFocus(hovered);
2891 m_JavaDialogFieldName = getNameByID(hovered->getID());
2892 std::string message = gettext("Enter ");
2893 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
2897 message += gettext(label) + ":";
2899 /* single line text input */
2902 /* multi line text input */
2903 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2907 /* passwords are always single line */
2908 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2912 porting::showInputDialog(gettext("ok"), "",
2913 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
2919 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2922 memset(&translated, 0, sizeof(SEvent));
2923 translated.EventType = EET_MOUSE_INPUT_EVENT;
2924 gui::IGUIElement* root = Environment->getRootGUIElement();
2928 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2932 gui::IGUIElement* hovered = root->getElementFromPoint(
2933 core::position2d<s32>(
2935 event.TouchInput.Y));
2937 translated.MouseInput.X = event.TouchInput.X;
2938 translated.MouseInput.Y = event.TouchInput.Y;
2939 translated.MouseInput.Control = false;
2941 bool dont_send_event = false;
2943 if (event.TouchInput.touchedCount == 1) {
2944 switch (event.TouchInput.Event) {
2945 case ETIE_PRESSED_DOWN:
2946 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2947 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2948 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2949 m_down_pos = m_pointer;
2952 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2953 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2954 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2957 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2958 translated.MouseInput.ButtonStates = 0;
2959 hovered = root->getElementFromPoint(m_down_pos);
2960 /* we don't have a valid pointer element use last
2961 * known pointer pos */
2962 translated.MouseInput.X = m_pointer.X;
2963 translated.MouseInput.Y = m_pointer.Y;
2965 /* reset down pos */
2966 m_down_pos = v2s32(0,0);
2969 dont_send_event = true;
2970 //this is not supposed to happen
2972 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2973 << event.TouchInput.Event << std::endl;
2975 } else if ( (event.TouchInput.touchedCount == 2) &&
2976 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2977 hovered = root->getElementFromPoint(m_down_pos);
2979 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2980 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2981 translated.MouseInput.X = m_pointer.X;
2982 translated.MouseInput.Y = m_pointer.Y;
2985 hovered->OnEvent(translated);
2988 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
2989 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2993 hovered->OnEvent(translated);
2995 dont_send_event = true;
2997 /* ignore unhandled 2 touch events ... accidental moving for example */
2998 else if (event.TouchInput.touchedCount == 2) {
2999 dont_send_event = true;
3001 else if (event.TouchInput.touchedCount > 2) {
3003 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
3004 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3007 if (dont_send_event) {
3011 /* check if translated event needs to be preprocessed again */
3012 if (preprocessEvent(translated)) {
3017 bool retval = hovered->OnEvent(translated);
3019 if (event.TouchInput.Event == ETIE_LEFT_UP) {
3021 m_pointer = v2s32(0,0);
3029 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3030 /* TODO add a check like:
3031 if (event.JoystickEvent != joystick_we_listen_for)
3034 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3036 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3038 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3040 acceptInput(quit_mode_accept);
3051 /******************************************************************************/
3052 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3054 /* The following code is for capturing double-clicks of the mouse button
3055 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3056 * -- which closes the form -- under some circumstances.
3058 * There have been many github issues reporting this as a bug even though it
3059 * was an intended feature. For this reason, remapping the double-click as
3060 * an ESC must be explicitly set when creating this class via the
3061 * /p remap_dbl_click parameter of the constructor.
3064 if (!m_remap_dbl_click)
3067 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3068 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3069 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3071 m_doubleclickdetect[1].pos = m_pointer;
3072 m_doubleclickdetect[1].time = getTimeMs();
3074 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3075 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
3080 double squaredistance =
3081 m_doubleclickdetect[0].pos
3082 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3084 if (squaredistance > (30*30)) {
3088 SEvent* translated = new SEvent();
3089 assert(translated != 0);
3090 //translate doubleclick to escape
3091 memset(translated, 0, sizeof(SEvent));
3092 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3093 translated->KeyInput.Key = KEY_ESCAPE;
3094 translated->KeyInput.Control = false;
3095 translated->KeyInput.Shift = false;
3096 translated->KeyInput.PressedDown = true;
3097 translated->KeyInput.Char = 0;
3098 OnEvent(*translated);
3100 // no need to send the key up event as we're already deleted
3101 // and no one else did notice this event
3109 void GUIFormSpecMenu::tryClose()
3113 acceptInput(quit_mode_cancel);
3116 m_text_dst->gotText(L"MenuQuit");
3120 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3122 if (event.EventType==EET_KEY_INPUT_EVENT) {
3123 KeyPress kp(event.KeyInput);
3124 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3125 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3128 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3129 (kp == getKeySetting("keymap_screenshot"))) {
3130 m_client->makeScreenshot(m_device);
3132 if (event.KeyInput.PressedDown &&
3133 (event.KeyInput.Key==KEY_RETURN ||
3134 event.KeyInput.Key==KEY_UP ||
3135 event.KeyInput.Key==KEY_DOWN)
3137 switch (event.KeyInput.Key) {
3139 current_keys_pending.key_enter = true;
3142 current_keys_pending.key_up = true;
3145 current_keys_pending.key_down = true;
3149 //can't happen at all!
3150 FATAL_ERROR("Reached a source line that can't ever been reached");
3153 if (current_keys_pending.key_enter && m_allowclose) {
3154 acceptInput(quit_mode_accept);
3164 /* Mouse event other than movement, or crossing the border of inventory
3165 field while holding right mouse button
3167 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3168 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3169 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3170 event.MouseInput.isRightPressed() &&
3171 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3173 // Get selected item and hovered/clicked item (s)
3175 m_old_tooltip_id = -1;
3176 updateSelectedItem();
3177 ItemSpec s = getItemAtPos(m_pointer);
3179 Inventory *inv_selected = NULL;
3180 Inventory *inv_s = NULL;
3181 InventoryList *list_s = NULL;
3183 if (m_selected_item) {
3184 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3185 sanity_check(inv_selected);
3186 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3193 inv_s = m_invmgr->getInventory(s.inventoryloc);
3196 errorstream << "InventoryMenu: The selected inventory location "
3197 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3199 s.i = -1; // make it invalid again
3203 list_s = inv_s->getList(s.listname);
3204 if (list_s == NULL) {
3205 verbosestream << "InventoryMenu: The selected inventory list \""
3206 << s.listname << "\" does not exist" << std::endl;
3207 s.i = -1; // make it invalid again
3211 if ((u32)s.i >= list_s->getSize()) {
3212 infostream << "InventoryMenu: The selected inventory list \""
3213 << s.listname << "\" is too small (i=" << s.i << ", size="
3214 << list_s->getSize() << ")" << std::endl;
3215 s.i = -1; // make it invalid again
3219 s_count = list_s->getItem(s.i).count;
3222 bool identical = (m_selected_item != NULL) && s.isValid() &&
3223 (inv_selected == inv_s) &&
3224 (m_selected_item->listname == s.listname) &&
3225 (m_selected_item->i == s.i);
3227 // buttons: 0 = left, 1 = right, 2 = middle
3228 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3231 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3232 { button = 0; updown = 0; }
3233 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3234 { button = 1; updown = 0; }
3235 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3236 { button = 2; updown = 0; }
3237 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3238 { button = 0; updown = 1; }
3239 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3240 { button = 1; updown = 1; }
3241 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3242 { button = 2; updown = 1; }
3243 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3246 // Set this number to a positive value to generate a move action
3247 // from m_selected_item to s.
3248 u32 move_amount = 0;
3250 // Set this number to a positive value to generate a move action
3251 // from s to the next inventory ring.
3252 u32 shift_move_amount = 0;
3254 // Set this number to a positive value to generate a drop action
3255 // from m_selected_item.
3256 u32 drop_amount = 0;
3258 // Set this number to a positive value to generate a craft action at s.
3259 u32 craft_amount = 0;
3262 // Some mouse button has been pressed
3264 //infostream<<"Mouse button "<<button<<" pressed at p=("
3265 // <<p.X<<","<<p.Y<<")"<<std::endl;
3267 m_selected_dragging = false;
3269 if (s.isValid() && s.listname == "craftpreview") {
3270 // Craft preview has been clicked: craft
3271 craft_amount = (button == 2 ? 10 : 1);
3272 } else if (m_selected_item == NULL) {
3274 // Non-empty stack has been clicked: select or shift-move it
3275 m_selected_item = new ItemSpec(s);
3278 if (button == 1) // right
3279 count = (s_count + 1) / 2;
3280 else if (button == 2) // middle
3281 count = MYMIN(s_count, 10);
3285 if (!event.MouseInput.Shift) {
3286 // no shift: select item
3287 m_selected_amount = count;
3288 m_selected_dragging = true;
3289 m_rmouse_auto_place = false;
3291 // shift pressed: move item
3293 shift_move_amount = count;
3294 else // count of 1 at left click like after drag & drop
3295 shift_move_amount = 1;
3298 } else { // m_selected_item != NULL
3299 assert(m_selected_amount >= 1);
3302 // Clicked a slot: move
3303 if (button == 1) // right
3305 else if (button == 2) // middle
3306 move_amount = MYMIN(m_selected_amount, 10);
3308 move_amount = m_selected_amount;
3311 if (move_amount >= m_selected_amount)
3312 m_selected_amount = 0;
3314 m_selected_amount -= move_amount;
3318 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3319 // Clicked outside of the window: drop
3320 if (button == 1) // right
3322 else if (button == 2) // middle
3323 drop_amount = MYMIN(m_selected_amount, 10);
3325 drop_amount = m_selected_amount;
3329 else if (updown == 1) {
3330 // Some mouse button has been released
3332 //infostream<<"Mouse button "<<button<<" released at p=("
3333 // <<p.X<<","<<p.Y<<")"<<std::endl;
3335 if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3337 // Dragged to different slot: move all selected
3338 move_amount = m_selected_amount;
3340 } else if (m_selected_item != NULL && m_selected_dragging &&
3341 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3342 // Dragged outside of window: drop all selected
3343 drop_amount = m_selected_amount;
3346 m_selected_dragging = false;
3347 // Keep count of how many times right mouse button has been
3348 // clicked. One click is drag without dropping. Click + release
3349 // + click changes to drop one item when moved mode
3350 if (button == 1 && m_selected_item != NULL)
3351 m_rmouse_auto_place = !m_rmouse_auto_place;
3352 } else if (updown == -1) {
3353 // Mouse has been moved and rmb is down and mouse pointer just
3354 // entered a new inventory field (checked in the entry-if, this
3355 // is the only action here that is generated by mouse movement)
3356 if (m_selected_item != NULL && s.isValid()) {
3358 // TODO: middle mouse to move 10 items might be handy
3359 if (m_rmouse_auto_place) {
3360 // Only move an item if the destination slot is empty
3361 // or contains the same item type as what is going to be
3363 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3364 InventoryList *list_to = list_s;
3365 assert(list_from && list_to);
3366 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3367 ItemStack stack_to = list_to->getItem(s.i);
3368 if (stack_to.empty() || stack_to.name == stack_from.name)
3374 // Possibly send inventory action to server
3375 if (move_amount > 0) {
3376 // Send IACTION_MOVE
3378 assert(m_selected_item && m_selected_item->isValid());
3379 assert(s.isValid());
3381 assert(inv_selected && inv_s);
3382 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3383 InventoryList *list_to = list_s;
3384 assert(list_from && list_to);
3385 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3386 ItemStack stack_to = list_to->getItem(s.i);
3388 // Check how many items can be moved
3389 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3390 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3391 // If source stack cannot be added to destination stack at all,
3393 if ((leftover.count == stack_from.count) &&
3394 (leftover.name == stack_from.name)) {
3395 m_selected_amount = stack_to.count;
3396 // In case the server doesn't directly swap them but instead
3397 // moves stack_to somewhere else, set this
3398 m_selected_content_guess = stack_to;
3399 m_selected_content_guess_inventory = s.inventoryloc;
3401 // Source stack goes fully into destination stack
3402 else if (leftover.empty()) {
3403 m_selected_amount -= move_amount;
3404 m_selected_content_guess = ItemStack(); // Clear
3406 // Source stack goes partly into destination stack
3408 move_amount -= leftover.count;
3409 m_selected_amount -= move_amount;
3410 m_selected_content_guess = ItemStack(); // Clear
3413 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3414 IMoveAction *a = new IMoveAction();
3415 a->count = move_amount;
3416 a->from_inv = m_selected_item->inventoryloc;
3417 a->from_list = m_selected_item->listname;
3418 a->from_i = m_selected_item->i;
3419 a->to_inv = s.inventoryloc;
3420 a->to_list = s.listname;
3422 m_invmgr->inventoryAction(a);
3423 } else if (shift_move_amount > 0) {
3424 u32 mis = m_inventory_rings.size();
3426 for (; i < mis; i++) {
3427 const ListRingSpec &sp = m_inventory_rings[i];
3428 if (sp.inventoryloc == s.inventoryloc
3429 && sp.listname == s.listname)
3433 if (i >= mis) // if not found
3435 u32 to_inv_ind = (i + 1) % mis;
3436 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3437 InventoryList *list_from = list_s;
3440 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3443 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3446 ItemStack stack_from = list_from->getItem(s.i);
3447 assert(shift_move_amount <= stack_from.count);
3448 if (m_client->getProtoVersion() >= 25) {
3449 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3450 IMoveAction *a = new IMoveAction();
3451 a->count = shift_move_amount;
3452 a->from_inv = s.inventoryloc;
3453 a->from_list = s.listname;
3455 a->to_inv = to_inv_sp.inventoryloc;
3456 a->to_list = to_inv_sp.listname;
3457 a->move_somewhere = true;
3458 m_invmgr->inventoryAction(a);
3460 // find a place (or more than one) to add the new item
3461 u32 ilt_size = list_to->getSize();
3463 for (u32 slot_to = 0; slot_to < ilt_size
3464 && shift_move_amount > 0; slot_to++) {
3465 list_to->itemFits(slot_to, stack_from, &leftover);
3466 if (leftover.count < stack_from.count) {
3467 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3468 IMoveAction *a = new IMoveAction();
3469 a->count = MYMIN(shift_move_amount,
3470 (u32) (stack_from.count - leftover.count));
3471 shift_move_amount -= a->count;
3472 a->from_inv = s.inventoryloc;
3473 a->from_list = s.listname;
3475 a->to_inv = to_inv_sp.inventoryloc;
3476 a->to_list = to_inv_sp.listname;
3478 m_invmgr->inventoryAction(a);
3479 stack_from = leftover;
3484 } else if (drop_amount > 0) {
3485 m_selected_content_guess = ItemStack(); // Clear
3487 // Send IACTION_DROP
3489 assert(m_selected_item && m_selected_item->isValid());
3490 assert(inv_selected);
3491 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3493 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3495 // Check how many items can be dropped
3496 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3497 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3498 m_selected_amount -= drop_amount;
3500 infostream << "Handing IACTION_DROP to manager" << std::endl;
3501 IDropAction *a = new IDropAction();
3502 a->count = drop_amount;
3503 a->from_inv = m_selected_item->inventoryloc;
3504 a->from_list = m_selected_item->listname;
3505 a->from_i = m_selected_item->i;
3506 m_invmgr->inventoryAction(a);
3507 } else if (craft_amount > 0) {
3508 m_selected_content_guess = ItemStack(); // Clear
3510 // Send IACTION_CRAFT
3512 assert(s.isValid());
3515 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3516 ICraftAction *a = new ICraftAction();
3517 a->count = craft_amount;
3518 a->craft_inv = s.inventoryloc;
3519 m_invmgr->inventoryAction(a);
3522 // If m_selected_amount has been decreased to zero, deselect
3523 if (m_selected_amount == 0) {
3524 delete m_selected_item;
3525 m_selected_item = NULL;
3526 m_selected_amount = 0;
3527 m_selected_dragging = false;
3528 m_selected_content_guess = ItemStack();
3530 m_old_pointer = m_pointer;
3532 if (event.EventType == EET_GUI_EVENT) {
3534 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3536 // find the element that was clicked
3537 for (unsigned int i=0; i<m_fields.size(); i++) {
3538 FieldSpec &s = m_fields[i];
3539 if ((s.ftype == f_TabHeader) &&
3540 (s.fid == event.GUIEvent.Caller->getID())) {
3548 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3550 if (!canTakeFocus(event.GUIEvent.Element)) {
3551 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3553 // Returning true disables focus change
3557 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3558 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3559 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3560 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3561 unsigned int btn_id = event.GUIEvent.Caller->getID();
3563 if (btn_id == 257) {
3565 acceptInput(quit_mode_accept);
3569 m_text_dst->gotText(L"ExitButton");
3571 // quitMenu deallocates menu
3575 // find the element that was clicked
3576 for (u32 i = 0; i < m_fields.size(); i++) {
3577 FieldSpec &s = m_fields[i];
3578 // if its a button, set the send field so
3579 // lua knows which button was pressed
3580 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3581 (s.fid == event.GUIEvent.Caller->getID())) {
3585 acceptInput(quit_mode_accept);
3588 m_text_dst->gotText(L"ExitButton");
3592 acceptInput(quit_mode_no);
3596 } else if ((s.ftype == f_DropDown) &&
3597 (s.fid == event.GUIEvent.Caller->getID())) {
3598 // only send the changed dropdown
3599 for (u32 i = 0; i < m_fields.size(); i++) {
3600 FieldSpec &s2 = m_fields[i];
3601 if (s2.ftype == f_DropDown) {
3606 acceptInput(quit_mode_no);
3608 // revert configuration to make sure dropdowns are sent on
3609 // regular button click
3610 for (u32 i = 0; i < m_fields.size(); i++) {
3611 FieldSpec &s2 = m_fields[i];
3612 if (s2.ftype == f_DropDown) {
3617 } else if ((s.ftype == f_ScrollBar) &&
3618 (s.fid == event.GUIEvent.Caller->getID())) {
3619 s.fdefault = L"Changed";
3620 acceptInput(quit_mode_no);
3626 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3627 if (event.GUIEvent.Caller->getID() > 257) {
3630 acceptInput(quit_mode_accept);
3633 current_keys_pending.key_enter = true;
3636 // quitMenu deallocates menu
3641 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3642 int current_id = event.GUIEvent.Caller->getID();
3643 if (current_id > 257) {
3644 // find the element that was clicked
3645 for (u32 i = 0; i < m_fields.size(); i++) {
3646 FieldSpec &s = m_fields[i];
3647 // if it's a table, set the send field
3648 // so lua knows which table was changed
3649 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3660 return Parent ? Parent->OnEvent(event) : false;
3664 * get name of element by element id
3665 * @param id of element
3666 * @return name string or empty string
3668 std::string GUIFormSpecMenu::getNameByID(s32 id)
3670 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3671 iter != m_fields.end(); ++iter) {
3672 if (iter->fid == id) {
3680 * get label of element by id
3681 * @param id of element
3682 * @return label string or empty string
3684 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3686 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3687 iter != m_fields.end(); ++iter) {
3688 if (iter->fid == id) {
3689 return iter->flabel;