3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guiFormSpecMenu.h"
28 #include "constants.h"
32 #include <IGUICheckBox.h>
33 #include <IGUIEditBox.h>
34 #include <IGUIButton.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include <IGUIComboBox.h>
40 #include "client/tile.h" // ITextureSource
41 #include "hud.h" // drawItemStack
45 #include "scripting_game.h"
49 #include "fontengine.h"
51 #include "util/numeric.h"
52 #include "util/string.h" // for parseColorString()
53 #include "guiscalingfilter.h"
55 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
56 #include "intlGUIEditBox.h"
59 #define MY_CHECKPOS(a,b) \
60 if (v_pos.size() != 2) { \
61 errorstream<< "Invalid pos for element " << a << "specified: \"" \
62 << parts[b] << "\"" << std::endl; \
66 #define MY_CHECKGEOM(a,b) \
67 if (v_geom.size() != 2) { \
68 errorstream<< "Invalid pos for element " << a << "specified: \"" \
69 << parts[b] << "\"" << std::endl; \
75 static unsigned int font_line_height(gui::IGUIFont *font)
77 return font->getDimension(L"Ay").Height + font->getKerningHeight();
80 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
81 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
82 InventoryManager *invmgr, IGameDef *gamedef,
83 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
84 Client* client, bool remap_dbl_click) :
85 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
91 m_selected_item(NULL),
93 m_selected_dragging(false),
94 m_tooltip_element(NULL),
97 m_rmouse_auto_place(false),
102 m_formspec_version(0),
103 m_focused_element(""),
105 m_remap_dbl_click(remap_dbl_click)
107 , m_JavaDialogFieldName("")
110 current_keys_pending.key_down = false;
111 current_keys_pending.key_up = false;
112 current_keys_pending.key_enter = false;
113 current_keys_pending.key_escape = false;
115 m_doubleclickdetect[0].time = 0;
116 m_doubleclickdetect[1].time = 0;
118 m_doubleclickdetect[0].pos = v2s32(0, 0);
119 m_doubleclickdetect[1].pos = v2s32(0, 0);
121 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
124 GUIFormSpecMenu::~GUIFormSpecMenu()
128 for (u32 i = 0; i < m_tables.size(); ++i) {
129 GUITable *table = m_tables[i].second;
133 delete m_selected_item;
135 if (m_form_src != NULL) {
138 if (m_text_dst != NULL) {
143 void GUIFormSpecMenu::removeChildren()
145 const core::list<gui::IGUIElement*> &children = getChildren();
147 while(!children.empty()) {
148 (*children.getLast())->remove();
151 if(m_tooltip_element) {
152 m_tooltip_element->remove();
153 m_tooltip_element->drop();
154 m_tooltip_element = NULL;
159 void GUIFormSpecMenu::setInitialFocus()
161 // Set initial focus according to following order of precedence:
162 // 1. first empty editbox
166 // 5. first focusable (not statictext, not tabheader)
167 // 6. first child element
169 core::list<gui::IGUIElement*> children = getChildren();
171 // in case "children" contains any NULL elements, remove them
172 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
173 it != children.end();) {
177 it = children.erase(it);
180 // 1. first empty editbox
181 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
182 it != children.end(); ++it) {
183 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
184 && (*it)->getText()[0] == 0) {
185 Environment->setFocus(*it);
191 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
192 it != children.end(); ++it) {
193 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
194 Environment->setFocus(*it);
200 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
201 it != children.end(); ++it) {
202 if ((*it)->getTypeName() == std::string("GUITable")) {
203 Environment->setFocus(*it);
209 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
210 it != children.end(); --it) {
211 if ((*it)->getType() == gui::EGUIET_BUTTON) {
212 Environment->setFocus(*it);
217 // 5. first focusable (not statictext, not tabheader)
218 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
219 it != children.end(); ++it) {
220 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
221 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
222 Environment->setFocus(*it);
227 // 6. first child element
228 if (children.empty())
229 Environment->setFocus(this);
231 Environment->setFocus(*(children.begin()));
234 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
236 for (u32 i = 0; i < m_tables.size(); ++i) {
237 if (tablename == m_tables[i].first.fname)
238 return m_tables[i].second;
243 static std::vector<std::string> split(const std::string &s, char delim)
245 std::vector<std::string> tokens;
247 std::string current = "";
248 bool last_was_escape = false;
249 for (unsigned int i = 0; i < s.size(); i++) {
250 char si = s.c_str()[i];
251 if (last_was_escape) {
254 last_was_escape = false;
257 tokens.push_back(current);
259 last_was_escape = false;
260 } else if (si == '\\') {
261 last_was_escape = true;
264 last_was_escape = false;
269 tokens.push_back(current);
274 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
276 std::vector<std::string> parts = split(element,',');
278 if (((parts.size() == 2) || parts.size() == 3) ||
279 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
281 if (parts[1].find(';') != std::string::npos)
282 parts[1] = parts[1].substr(0,parts[1].find(';'));
284 data->invsize.X = MYMAX(0, stof(parts[0]));
285 data->invsize.Y = MYMAX(0, stof(parts[1]));
288 if (parts.size() == 3) {
289 if (parts[2] == "true") {
290 lockSize(true,v2u32(800,600));
294 data->explicit_size = true;
297 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
300 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
302 if (m_gamedef == 0) {
303 warningstream<<"invalid use of 'list' with m_gamedef==0"<<std::endl;
307 std::vector<std::string> parts = split(element,';');
309 if (((parts.size() == 4) || (parts.size() == 5)) ||
310 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
312 std::string location = parts[0];
313 std::string listname = parts[1];
314 std::vector<std::string> v_pos = split(parts[2],',');
315 std::vector<std::string> v_geom = split(parts[3],',');
316 std::string startindex = "";
317 if (parts.size() == 5)
318 startindex = parts[4];
320 MY_CHECKPOS("list",2);
321 MY_CHECKGEOM("list",3);
323 InventoryLocation loc;
325 if(location == "context" || location == "current_name")
326 loc = m_current_inventory_location;
328 loc.deSerialize(location);
330 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
331 pos.X += stof(v_pos[0]) * (float)spacing.X;
332 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
335 geom.X = stoi(v_geom[0]);
336 geom.Y = stoi(v_geom[1]);
340 start_i = stoi(startindex);
342 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
343 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
347 if(!data->explicit_size)
348 warningstream<<"invalid use of list without a size[] element"<<std::endl;
349 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
352 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
355 void GUIFormSpecMenu::parseListRing(parserData* data, std::string element)
357 if (m_gamedef == 0) {
358 errorstream << "WARNING: invalid use of 'listring' with m_gamedef==0" << std::endl;
362 std::vector<std::string> parts = split(element, ';');
364 if (parts.size() == 2) {
365 std::string location = parts[0];
366 std::string listname = parts[1];
368 InventoryLocation loc;
370 if (location == "context" || location == "current_name")
371 loc = m_current_inventory_location;
373 loc.deSerialize(location);
375 m_inventory_rings.push_back(ListRingSpec(loc, listname));
377 } else if ((element == "") && (m_inventorylists.size() > 1)) {
378 size_t siz = m_inventorylists.size();
379 // insert the last two inv list elements into the list ring
380 const ListDrawSpec &spa = m_inventorylists[siz - 2];
381 const ListDrawSpec &spb = m_inventorylists[siz - 1];
382 m_inventory_rings.push_back(ListRingSpec(spa.inventoryloc, spa.listname));
383 m_inventory_rings.push_back(ListRingSpec(spb.inventoryloc, spb.listname));
386 errorstream<< "Invalid list ring element(" << parts.size() << ", "
387 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
390 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
392 std::vector<std::string> parts = split(element,';');
394 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
395 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
397 std::vector<std::string> v_pos = split(parts[0],',');
398 std::string name = parts[1];
399 std::string label = parts[2];
400 std::string selected = "";
402 if (parts.size() >= 4)
405 MY_CHECKPOS("checkbox",0);
408 pos.X += stof(v_pos[0]) * (float) spacing.X;
409 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
411 bool fselected = false;
413 if (selected == "true")
416 std::wstring wlabel = utf8_to_wide(label);
418 core::rect<s32> rect = core::rect<s32>(
419 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
420 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
421 pos.Y + ((imgsize.Y/2) + m_btn_height));
425 wlabel, //Needed for displaying text on MSVC
430 spec.ftype = f_CheckBox;
432 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
433 spec.fid, spec.flabel.c_str());
435 if (spec.fname == data->focused_fieldname) {
436 Environment->setFocus(e);
439 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
440 m_fields.push_back(spec);
443 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
446 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
448 std::vector<std::string> parts = split(element,';');
450 if (parts.size() >= 5) {
451 std::vector<std::string> v_pos = split(parts[0],',');
452 std::vector<std::string> v_dim = split(parts[1],',');
453 std::string name = parts[3];
454 std::string value = parts[4];
456 MY_CHECKPOS("scrollbar",0);
459 pos.X += stof(v_pos[0]) * (float) spacing.X;
460 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
462 if (v_dim.size() != 2) {
463 errorstream<< "Invalid size for element " << "scrollbar"
464 << "specified: \"" << parts[1] << "\"" << std::endl;
469 dim.X = stof(v_dim[0]) * (float) spacing.X;
470 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
472 core::rect<s32> rect =
473 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
482 bool is_horizontal = true;
484 if (parts[2] == "vertical")
485 is_horizontal = false;
487 spec.ftype = f_ScrollBar;
489 gui::IGUIScrollBar* e =
490 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
494 e->setPos(stoi(parts[4]));
496 e->setLargeStep(100);
498 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
499 m_fields.push_back(spec);
502 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
505 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
507 std::vector<std::string> parts = split(element,';');
509 if ((parts.size() == 3) ||
510 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
512 std::vector<std::string> v_pos = split(parts[0],',');
513 std::vector<std::string> v_geom = split(parts[1],',');
514 std::string name = unescape_string(parts[2]);
516 MY_CHECKPOS("image",0);
517 MY_CHECKGEOM("image",1);
519 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
520 pos.X += stof(v_pos[0]) * (float) spacing.X;
521 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
524 geom.X = stof(v_geom[0]) * (float)imgsize.X;
525 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
527 if(!data->explicit_size)
528 warningstream<<"invalid use of image without a size[] element"<<std::endl;
529 m_images.push_back(ImageDrawSpec(name, pos, geom));
533 if (parts.size() == 2) {
534 std::vector<std::string> v_pos = split(parts[0],',');
535 std::string name = unescape_string(parts[1]);
537 MY_CHECKPOS("image",0);
539 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
540 pos.X += stof(v_pos[0]) * (float) spacing.X;
541 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
543 if(!data->explicit_size)
544 warningstream<<"invalid use of image without a size[] element"<<std::endl;
545 m_images.push_back(ImageDrawSpec(name, pos));
548 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
551 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
553 std::vector<std::string> parts = split(element,';');
555 if ((parts.size() == 3) ||
556 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
558 std::vector<std::string> v_pos = split(parts[0],',');
559 std::vector<std::string> v_geom = split(parts[1],',');
560 std::string name = parts[2];
562 MY_CHECKPOS("itemimage",0);
563 MY_CHECKGEOM("itemimage",1);
565 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
566 pos.X += stof(v_pos[0]) * (float) spacing.X;
567 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
570 geom.X = stof(v_geom[0]) * (float)imgsize.X;
571 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
573 if(!data->explicit_size)
574 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
575 m_itemimages.push_back(ImageDrawSpec("", name, pos, geom));
578 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
581 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
584 std::vector<std::string> parts = split(element,';');
586 if ((parts.size() == 4) ||
587 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
589 std::vector<std::string> v_pos = split(parts[0],',');
590 std::vector<std::string> v_geom = split(parts[1],',');
591 std::string name = parts[2];
592 std::string label = parts[3];
594 MY_CHECKPOS("button",0);
595 MY_CHECKGEOM("button",1);
598 pos.X += stof(v_pos[0]) * (float)spacing.X;
599 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
602 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
603 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
605 core::rect<s32> rect =
606 core::rect<s32>(pos.X, pos.Y - m_btn_height,
607 pos.X + geom.X, pos.Y + m_btn_height);
609 if(!data->explicit_size)
610 warningstream<<"invalid use of button without a size[] element"<<std::endl;
612 label = unescape_string(label);
614 std::wstring wlabel = utf8_to_wide(label);
622 spec.ftype = f_Button;
623 if(type == "button_exit")
625 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
626 spec.flabel.c_str());
628 if (spec.fname == data->focused_fieldname) {
629 Environment->setFocus(e);
632 m_fields.push_back(spec);
635 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
638 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
640 std::vector<std::string> parts = split(element,';');
642 if (((parts.size() == 3) || (parts.size() == 4)) ||
643 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
645 std::vector<std::string> v_pos = split(parts[0],',');
646 std::vector<std::string> v_geom = split(parts[1],',');
647 std::string name = unescape_string(parts[2]);
649 MY_CHECKPOS("background",0);
650 MY_CHECKGEOM("background",1);
652 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
653 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
654 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
657 geom.X = stof(v_geom[0]) * (float)spacing.X;
658 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
660 if (parts.size() == 4) {
661 m_clipbackground = is_yes(parts[3]);
662 if (m_clipbackground) {
663 pos.X = stoi(v_pos[0]); //acts as offset
664 pos.Y = stoi(v_pos[1]); //acts as offset
668 if(!data->explicit_size)
669 warningstream<<"invalid use of background without a size[] element"<<std::endl;
670 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
673 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
676 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
678 std::vector<std::string> parts = split(element,';');
680 data->table_options.clear();
681 for (size_t i = 0; i < parts.size(); ++i) {
682 // Parse table option
683 std::string opt = unescape_string(parts[i]);
684 data->table_options.push_back(GUITable::splitOption(opt));
688 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
690 std::vector<std::string> parts = split(element,';');
692 data->table_columns.clear();
693 for (size_t i = 0; i < parts.size(); ++i) {
694 std::vector<std::string> col_parts = split(parts[i],',');
695 GUITable::TableColumn column;
697 if (!col_parts.empty())
698 column.type = col_parts[0];
699 // Parse column options
700 for (size_t j = 1; j < col_parts.size(); ++j) {
701 std::string opt = unescape_string(col_parts[j]);
702 column.options.push_back(GUITable::splitOption(opt));
704 data->table_columns.push_back(column);
708 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
710 std::vector<std::string> parts = split(element,';');
712 if (((parts.size() == 4) || (parts.size() == 5)) ||
713 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
715 std::vector<std::string> v_pos = split(parts[0],',');
716 std::vector<std::string> v_geom = split(parts[1],',');
717 std::string name = parts[2];
718 std::vector<std::string> items = split(parts[3],',');
719 std::string str_initial_selection = "";
720 std::string str_transparent = "false";
722 if (parts.size() >= 5)
723 str_initial_selection = parts[4];
725 MY_CHECKPOS("table",0);
726 MY_CHECKGEOM("table",1);
729 pos.X += stof(v_pos[0]) * (float)spacing.X;
730 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
733 geom.X = stof(v_geom[0]) * (float)spacing.X;
734 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
737 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
746 spec.ftype = f_Table;
748 for (unsigned int i = 0; i < items.size(); ++i) {
749 items[i] = unescape_string(items[i]);
752 //now really show table
753 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
756 if (spec.fname == data->focused_fieldname) {
757 Environment->setFocus(e);
760 e->setTable(data->table_options, data->table_columns, items);
762 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
763 e->setDynamicData(data->table_dyndata[name]);
766 if ((str_initial_selection != "") &&
767 (str_initial_selection != "0"))
768 e->setSelected(stoi(str_initial_selection.c_str()));
770 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
771 m_fields.push_back(spec);
774 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
777 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
779 std::vector<std::string> parts = split(element,';');
781 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
782 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
784 std::vector<std::string> v_pos = split(parts[0],',');
785 std::vector<std::string> v_geom = split(parts[1],',');
786 std::string name = parts[2];
787 std::vector<std::string> items = split(parts[3],',');
788 std::string str_initial_selection = "";
789 std::string str_transparent = "false";
791 if (parts.size() >= 5)
792 str_initial_selection = parts[4];
794 if (parts.size() >= 6)
795 str_transparent = parts[5];
797 MY_CHECKPOS("textlist",0);
798 MY_CHECKGEOM("textlist",1);
801 pos.X += stof(v_pos[0]) * (float)spacing.X;
802 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
805 geom.X = stof(v_geom[0]) * (float)spacing.X;
806 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
809 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
818 spec.ftype = f_Table;
820 for (unsigned int i = 0; i < items.size(); ++i) {
821 items[i] = unescape_string(items[i]);
824 //now really show list
825 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
828 if (spec.fname == data->focused_fieldname) {
829 Environment->setFocus(e);
832 e->setTextList(items, is_yes(str_transparent));
834 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
835 e->setDynamicData(data->table_dyndata[name]);
838 if ((str_initial_selection != "") &&
839 (str_initial_selection != "0"))
840 e->setSelected(stoi(str_initial_selection.c_str()));
842 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
843 m_fields.push_back(spec);
846 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
850 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
852 std::vector<std::string> parts = split(element,';');
854 if ((parts.size() == 5) ||
855 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
857 std::vector<std::string> v_pos = split(parts[0],',');
858 std::string name = parts[2];
859 std::vector<std::string> items = split(parts[3],',');
860 std::string str_initial_selection = "";
861 str_initial_selection = parts[4];
863 MY_CHECKPOS("dropdown",0);
866 pos.X += stof(v_pos[0]) * (float)spacing.X;
867 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
869 s32 width = stof(parts[1]) * (float)spacing.Y;
871 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
872 pos.X + width, pos.Y + (m_btn_height * 2));
881 spec.ftype = f_DropDown;
884 //now really show list
885 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
887 if (spec.fname == data->focused_fieldname) {
888 Environment->setFocus(e);
891 for (unsigned int i=0; i < items.size(); i++) {
892 e->addItem(utf8_to_wide(items[i]).c_str());
895 if (str_initial_selection != "")
896 e->setSelected(stoi(str_initial_selection.c_str())-1);
898 m_fields.push_back(spec);
901 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
902 << element << "'" << std::endl;
905 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
907 std::vector<std::string> parts = split(element,';');
909 if ((parts.size() == 4) ||
910 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
912 std::vector<std::string> v_pos = split(parts[0],',');
913 std::vector<std::string> v_geom = split(parts[1],',');
914 std::string name = parts[2];
915 std::string label = parts[3];
917 MY_CHECKPOS("pwdfield",0);
918 MY_CHECKGEOM("pwdfield",1);
921 pos.X += stof(v_pos[0]) * (float)spacing.X;
922 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
925 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
927 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
928 pos.Y -= m_btn_height;
929 geom.Y = m_btn_height*2;
931 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
933 label = unescape_string(label);
935 std::wstring wlabel = utf8_to_wide(label);
945 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
947 if (spec.fname == data->focused_fieldname) {
948 Environment->setFocus(e);
951 if (label.length() >= 1)
953 int font_height = g_fontengine->getTextHeight();
954 rect.UpperLeftCorner.Y -= font_height;
955 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
956 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
959 e->setPasswordBox(true,L'*');
962 evt.EventType = EET_KEY_INPUT_EVENT;
963 evt.KeyInput.Key = KEY_END;
964 evt.KeyInput.Char = 0;
965 evt.KeyInput.Control = 0;
966 evt.KeyInput.Shift = 0;
967 evt.KeyInput.PressedDown = true;
969 m_fields.push_back(spec);
972 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
975 void GUIFormSpecMenu::parseSimpleField(parserData* data,
976 std::vector<std::string> &parts)
978 std::string name = parts[0];
979 std::string label = parts[1];
980 std::string default_val = parts[2];
982 core::rect<s32> rect;
984 if(data->explicit_size)
985 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
987 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
988 pos.Y = ((m_fields.size()+2)*60);
989 v2s32 size = DesiredRect.getSize();
991 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
992 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
996 default_val = m_form_src->resolveText(default_val);
998 default_val = unescape_string(default_val);
999 label = unescape_string(label);
1001 std::wstring wlabel = utf8_to_wide(label);
1006 utf8_to_wide(default_val),
1012 // spec field id to 0, this stops submit searching for a value that isn't there
1013 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1018 gui::IGUIElement *e;
1019 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1020 if (g_settings->getBool("freetype")) {
1021 e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1022 true, Environment, this, spec.fid, rect);
1028 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1030 if (spec.fname == data->focused_fieldname) {
1031 Environment->setFocus(e);
1035 evt.EventType = EET_KEY_INPUT_EVENT;
1036 evt.KeyInput.Key = KEY_END;
1037 evt.KeyInput.Char = 0;
1038 evt.KeyInput.Control = 0;
1039 evt.KeyInput.Shift = 0;
1040 evt.KeyInput.PressedDown = true;
1043 if (label.length() >= 1)
1045 int font_height = g_fontengine->getTextHeight();
1046 rect.UpperLeftCorner.Y -= font_height;
1047 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1048 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1052 m_fields.push_back(spec);
1055 void GUIFormSpecMenu::parseTextArea(parserData* data,
1056 std::vector<std::string>& parts,std::string type)
1059 std::vector<std::string> v_pos = split(parts[0],',');
1060 std::vector<std::string> v_geom = split(parts[1],',');
1061 std::string name = parts[2];
1062 std::string label = parts[3];
1063 std::string default_val = parts[4];
1065 MY_CHECKPOS(type,0);
1066 MY_CHECKGEOM(type,1);
1069 pos.X = stof(v_pos[0]) * (float) spacing.X;
1070 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1074 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1076 if (type == "textarea")
1078 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1079 pos.Y += m_btn_height;
1083 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1084 pos.Y -= m_btn_height;
1085 geom.Y = m_btn_height*2;
1088 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1090 if(!data->explicit_size)
1091 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1094 default_val = m_form_src->resolveText(default_val);
1097 default_val = unescape_string(default_val);
1098 label = unescape_string(label);
1100 std::wstring wlabel = utf8_to_wide(label);
1105 utf8_to_wide(default_val),
1111 // spec field id to 0, this stops submit searching for a value that isn't there
1112 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1118 gui::IGUIEditBox *e;
1119 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1120 if (g_settings->getBool("freetype")) {
1121 e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1122 true, Environment, this, spec.fid, rect);
1128 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1131 if (spec.fname == data->focused_fieldname) {
1132 Environment->setFocus(e);
1135 if (type == "textarea")
1137 e->setMultiLine(true);
1138 e->setWordWrap(true);
1139 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1142 evt.EventType = EET_KEY_INPUT_EVENT;
1143 evt.KeyInput.Key = KEY_END;
1144 evt.KeyInput.Char = 0;
1145 evt.KeyInput.Control = 0;
1146 evt.KeyInput.Shift = 0;
1147 evt.KeyInput.PressedDown = true;
1151 if (label.length() >= 1)
1153 int font_height = g_fontengine->getTextHeight();
1154 rect.UpperLeftCorner.Y -= font_height;
1155 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1156 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1159 m_fields.push_back(spec);
1162 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1165 std::vector<std::string> parts = split(element,';');
1167 if (parts.size() == 3 || parts.size() == 4) {
1168 parseSimpleField(data,parts);
1172 if ((parts.size() == 5) ||
1173 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1175 parseTextArea(data,parts,type);
1178 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1181 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1183 std::vector<std::string> parts = split(element,';');
1185 if ((parts.size() == 2) ||
1186 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1188 std::vector<std::string> v_pos = split(parts[0],',');
1189 std::string text = parts[1];
1191 MY_CHECKPOS("label",0);
1193 v2s32 pos = padding;
1194 pos.X += stof(v_pos[0]) * (float)spacing.X;
1195 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1197 if(!data->explicit_size)
1198 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1200 text = unescape_string(text);
1201 std::vector<std::string> lines = split(text, '\n');
1203 for (unsigned int i = 0; i != lines.size(); i++) {
1204 // Lines are spaced at the nominal distance of
1205 // 2/5 inventory slot, even if the font doesn't
1206 // quite match that. This provides consistent
1207 // form layout, at the expense of sometimes
1208 // having sub-optimal spacing for the font.
1209 // We multiply by 2 and then divide by 5, rather
1210 // than multiply by 0.4, to get exact results
1211 // in the integer cases: 0.4 is not exactly
1212 // representable in binary floating point.
1213 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1214 std::wstring wlabel = utf8_to_wide(lines[i]);
1215 core::rect<s32> rect = core::rect<s32>(
1216 pos.X, posy - m_btn_height,
1217 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1218 posy + m_btn_height);
1225 gui::IGUIStaticText *e =
1226 Environment->addStaticText(spec.flabel.c_str(),
1227 rect, false, false, this, spec.fid);
1228 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1230 m_fields.push_back(spec);
1235 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1238 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1240 std::vector<std::string> parts = split(element,';');
1242 if ((parts.size() == 2) ||
1243 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1245 std::vector<std::string> v_pos = split(parts[0],',');
1246 std::wstring text = utf8_to_wide(unescape_string(parts[1]));
1248 MY_CHECKPOS("vertlabel",1);
1250 v2s32 pos = padding;
1251 pos.X += stof(v_pos[0]) * (float)spacing.X;
1252 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1254 core::rect<s32> rect = core::rect<s32>(
1255 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1257 font_line_height(m_font)
1259 +((imgsize.Y/2)- m_btn_height));
1260 //actually text.length() would be correct but adding +1 avoids to break all mods
1262 if(!data->explicit_size)
1263 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1265 std::wstring label = L"";
1267 for (unsigned int i=0; i < text.length(); i++) {
1278 gui::IGUIStaticText *t =
1279 Environment->addStaticText(spec.flabel.c_str(), rect, false, false, this, spec.fid);
1280 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1281 m_fields.push_back(spec);
1284 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1287 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1290 std::vector<std::string> parts = split(element,';');
1292 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1293 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1295 std::vector<std::string> v_pos = split(parts[0],',');
1296 std::vector<std::string> v_geom = split(parts[1],',');
1297 std::string image_name = parts[2];
1298 std::string name = parts[3];
1299 std::string label = parts[4];
1301 MY_CHECKPOS("imagebutton",0);
1302 MY_CHECKGEOM("imagebutton",1);
1304 v2s32 pos = padding;
1305 pos.X += stof(v_pos[0]) * (float)spacing.X;
1306 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1308 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1309 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1311 bool noclip = false;
1312 bool drawborder = true;
1313 std::string pressed_image_name = "";
1315 if (parts.size() >= 7) {
1316 if (parts[5] == "true")
1318 if (parts[6] == "false")
1322 if (parts.size() >= 8) {
1323 pressed_image_name = parts[7];
1326 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1328 if(!data->explicit_size)
1329 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1331 image_name = unescape_string(image_name);
1332 pressed_image_name = unescape_string(pressed_image_name);
1333 label = unescape_string(label);
1335 std::wstring wlabel = utf8_to_wide(label);
1340 utf8_to_wide(image_name),
1343 spec.ftype = f_Button;
1344 if(type == "image_button_exit")
1345 spec.is_exit = true;
1347 video::ITexture *texture = 0;
1348 video::ITexture *pressed_texture = 0;
1349 texture = m_tsrc->getTexture(image_name);
1350 if (pressed_image_name != "")
1351 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1353 pressed_texture = texture;
1355 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1357 if (spec.fname == data->focused_fieldname) {
1358 Environment->setFocus(e);
1361 e->setUseAlphaChannel(true);
1362 e->setImage(guiScalingImageButton(
1363 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1364 e->setPressedImage(guiScalingImageButton(
1365 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1366 e->setScaleImage(true);
1367 e->setNotClipped(noclip);
1368 e->setDrawBorder(drawborder);
1370 m_fields.push_back(spec);
1374 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1377 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1379 std::vector<std::string> parts = split(element,';');
1381 if (((parts.size() == 4) || (parts.size() == 6)) ||
1382 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1384 std::vector<std::string> v_pos = split(parts[0],',');
1385 std::string name = parts[1];
1386 std::vector<std::string> buttons = split(parts[2],',');
1387 std::string str_index = parts[3];
1388 bool show_background = true;
1389 bool show_border = true;
1390 int tab_index = stoi(str_index) -1;
1392 MY_CHECKPOS("tabheader",0);
1394 if (parts.size() == 6) {
1395 if (parts[4] == "true")
1396 show_background = false;
1397 if (parts[5] == "false")
1398 show_border = false;
1408 spec.ftype = f_TabHeader;
1411 pos.X += stof(v_pos[0]) * (float)spacing.X;
1412 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1414 geom.X = DesiredRect.getWidth();
1415 geom.Y = m_btn_height*2;
1417 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1420 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1421 show_background, show_border, spec.fid);
1422 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1423 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1424 e->setTabHeight(m_btn_height*2);
1426 if (spec.fname == data->focused_fieldname) {
1427 Environment->setFocus(e);
1430 e->setNotClipped(true);
1432 for (unsigned int i = 0; i < buttons.size(); i++) {
1433 e->addTab(utf8_to_wide(buttons[i]).c_str(), -1);
1436 if ((tab_index >= 0) &&
1437 (buttons.size() < INT_MAX) &&
1438 (tab_index < (int) buttons.size()))
1439 e->setActiveTab(tab_index);
1441 m_fields.push_back(spec);
1444 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1445 << element << "'" << std::endl;
1448 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1451 if (m_gamedef == 0) {
1452 warningstream << "invalid use of item_image_button with m_gamedef==0"
1457 std::vector<std::string> parts = split(element,';');
1459 if ((parts.size() == 5) ||
1460 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1462 std::vector<std::string> v_pos = split(parts[0],',');
1463 std::vector<std::string> v_geom = split(parts[1],',');
1464 std::string item_name = parts[2];
1465 std::string name = parts[3];
1466 std::string label = parts[4];
1468 MY_CHECKPOS("itemimagebutton",0);
1469 MY_CHECKGEOM("itemimagebutton",1);
1471 v2s32 pos = padding;
1472 pos.X += stof(v_pos[0]) * (float)spacing.X;
1473 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1475 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1476 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1478 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1480 if(!data->explicit_size)
1481 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1483 IItemDefManager *idef = m_gamedef->idef();
1485 item.deSerialize(item_name, idef);
1488 TooltipSpec(item.getDefinition(idef).description,
1489 m_default_tooltip_bgcolor,
1490 m_default_tooltip_color);
1492 label = unescape_string(label);
1495 utf8_to_wide(label),
1496 utf8_to_wide(item_name),
1497 258 + m_fields.size()
1500 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1502 if (spec.fname == data->focused_fieldname) {
1503 Environment->setFocus(e);
1506 e->setUseAlphaChannel(true);
1507 e->setImage(guiScalingImageButton(Environment->getVideoDriver(), NULL, geom.X, geom.Y));
1508 e->setPressedImage(guiScalingImageButton(Environment->getVideoDriver(), NULL, geom.X, geom.Y));
1509 e->setScaleImage(true);
1510 spec.ftype = f_Button;
1511 rect+=data->basepos-padding;
1513 m_fields.push_back(spec);
1514 pos = padding + AbsoluteRect.UpperLeftCorner;
1515 pos.X += stof(v_pos[0]) * (float) spacing.X;
1516 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1517 m_itemimages.push_back(ImageDrawSpec("", item_name, pos, geom));
1519 StaticTextSpec label_spec(
1520 utf8_to_wide(label),
1523 m_static_texts.push_back(label_spec);
1526 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1529 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1531 std::vector<std::string> parts = split(element,';');
1533 if ((parts.size() == 3) ||
1534 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1536 std::vector<std::string> v_pos = split(parts[0],',');
1537 std::vector<std::string> v_geom = split(parts[1],',');
1539 MY_CHECKPOS("box",0);
1540 MY_CHECKGEOM("box",1);
1542 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1543 pos.X += stof(v_pos[0]) * (float) spacing.X;
1544 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1547 geom.X = stof(v_geom[0]) * (float)spacing.X;
1548 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1550 video::SColor tmp_color;
1552 if (parseColorString(parts[2], tmp_color, false)) {
1553 BoxDrawSpec spec(pos, geom, tmp_color);
1555 m_boxes.push_back(spec);
1558 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1562 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1565 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1567 std::vector<std::string> parts = split(element,';');
1569 if (((parts.size() == 1) || (parts.size() == 2)) ||
1570 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1572 parseColorString(parts[0],m_bgcolor,false);
1574 if (parts.size() == 2) {
1575 std::string fullscreen = parts[1];
1576 m_bgfullscreen = is_yes(fullscreen);
1580 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1583 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1585 std::vector<std::string> parts = split(element,';');
1587 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1588 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1590 parseColorString(parts[0], m_slotbg_n, false);
1591 parseColorString(parts[1], m_slotbg_h, false);
1593 if (parts.size() >= 3) {
1594 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1595 m_slotborder = true;
1598 if (parts.size() == 5) {
1599 video::SColor tmp_color;
1601 if (parseColorString(parts[3], tmp_color, false))
1602 m_default_tooltip_bgcolor = tmp_color;
1603 if (parseColorString(parts[4], tmp_color, false))
1604 m_default_tooltip_color = tmp_color;
1608 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1611 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1613 std::vector<std::string> parts = split(element,';');
1614 if (parts.size() == 2) {
1615 std::string name = parts[0];
1616 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1617 m_default_tooltip_bgcolor, m_default_tooltip_color);
1619 } else if (parts.size() == 4) {
1620 std::string name = parts[0];
1621 video::SColor tmp_color1, tmp_color2;
1622 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1623 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1624 tmp_color1, tmp_color2);
1628 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1631 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1637 std::vector<std::string> parts = split(data,'[');
1639 if (parts.size() < 2) {
1643 if (parts[0] != "formspec_version") {
1647 if (is_number(parts[1])) {
1648 m_formspec_version = mystoi(parts[1]);
1655 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1660 std::vector<std::string> parts = split(element,'[');
1662 if (parts.size() < 2)
1665 std::string type = trim(parts[0]);
1666 std::string description = trim(parts[1]);
1668 if (type != "size" && type != "invsize")
1671 if (type == "invsize")
1672 log_deprecated("Deprecated formspec element \"invsize\" is used");
1674 parseSize(data, description);
1679 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1685 std::vector<std::string> parts = split(element,'[');
1687 // ugly workaround to keep compatibility
1688 if (parts.size() > 2) {
1689 if (trim(parts[0]) == "image") {
1690 for (unsigned int i=2;i< parts.size(); i++) {
1691 parts[1] += "[" + parts[i];
1697 if (parts.size() < 2) {
1701 std::string type = trim(parts[0]);
1702 std::string description = trim(parts[1]);
1704 if (type == "list") {
1705 parseList(data,description);
1709 if (type == "listring") {
1710 parseListRing(data, description);
1714 if (type == "checkbox") {
1715 parseCheckbox(data,description);
1719 if (type == "image") {
1720 parseImage(data,description);
1724 if (type == "item_image") {
1725 parseItemImage(data,description);
1729 if ((type == "button") || (type == "button_exit")) {
1730 parseButton(data,description,type);
1734 if (type == "background") {
1735 parseBackground(data,description);
1739 if (type == "tableoptions"){
1740 parseTableOptions(data,description);
1744 if (type == "tablecolumns"){
1745 parseTableColumns(data,description);
1749 if (type == "table"){
1750 parseTable(data,description);
1754 if (type == "textlist"){
1755 parseTextList(data,description);
1759 if (type == "dropdown"){
1760 parseDropDown(data,description);
1764 if (type == "pwdfield") {
1765 parsePwdField(data,description);
1769 if ((type == "field") || (type == "textarea")){
1770 parseField(data,description,type);
1774 if (type == "label") {
1775 parseLabel(data,description);
1779 if (type == "vertlabel") {
1780 parseVertLabel(data,description);
1784 if (type == "item_image_button") {
1785 parseItemImageButton(data,description);
1789 if ((type == "image_button") || (type == "image_button_exit")) {
1790 parseImageButton(data,description,type);
1794 if (type == "tabheader") {
1795 parseTabHeader(data,description);
1799 if (type == "box") {
1800 parseBox(data,description);
1804 if (type == "bgcolor") {
1805 parseBackgroundColor(data,description);
1809 if (type == "listcolors") {
1810 parseListColors(data,description);
1814 if (type == "tooltip") {
1815 parseTooltip(data,description);
1819 if (type == "scrollbar") {
1820 parseScrollBar(data, description);
1826 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1830 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1832 /* useless to regenerate without a screensize */
1833 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1840 for (u32 i = 0; i < m_tables.size(); ++i) {
1841 std::string tablename = m_tables[i].first.fname;
1842 GUITable *table = m_tables[i].second;
1843 mydata.table_dyndata[tablename] = table->getDynamicData();
1847 if (!m_focused_element.empty())
1848 mydata.focused_fieldname = m_focused_element;
1851 gui::IGUIElement *focused_element = Environment->getFocus();
1852 if (focused_element && focused_element->getParent() == this) {
1853 s32 focused_id = focused_element->getID();
1854 if (focused_id > 257) {
1855 for (u32 i=0; i<m_fields.size(); i++) {
1856 if (m_fields[i].fid == focused_id) {
1857 mydata.focused_fieldname =
1868 for (u32 i = 0; i < m_tables.size(); ++i) {
1869 GUITable *table = m_tables[i].second;
1873 mydata.size= v2s32(100,100);
1874 mydata.screensize = screensize;
1876 // Base position of contents of form
1877 mydata.basepos = getBasePos();
1879 /* Convert m_init_draw_spec to m_inventorylists */
1881 m_inventorylists.clear();
1883 m_backgrounds.clear();
1884 m_itemimages.clear();
1886 m_checkboxes.clear();
1887 m_scrollbars.clear();
1891 m_inventory_rings.clear();
1892 m_static_texts.clear();
1894 // Set default values (fits old formspec values)
1895 m_bgcolor = video::SColor(140,0,0,0);
1896 m_bgfullscreen = false;
1898 m_slotbg_n = video::SColor(255,128,128,128);
1899 m_slotbg_h = video::SColor(255,192,192,192);
1901 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1902 m_default_tooltip_color = video::SColor(255,255,255,255);
1904 m_slotbordercolor = video::SColor(200,0,0,0);
1905 m_slotborder = false;
1907 m_clipbackground = false;
1910 assert(m_tooltip_element == NULL);
1911 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1912 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1913 m_tooltip_element->enableOverrideColor(true);
1914 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1915 m_tooltip_element->setDrawBackground(true);
1916 m_tooltip_element->setDrawBorder(true);
1917 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1918 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1919 m_tooltip_element->setWordWrap(false);
1920 //we're not parent so no autograb for this one!
1921 m_tooltip_element->grab();
1924 std::vector<std::string> elements = split(m_formspec_string,']');
1927 /* try to read version from first element only */
1928 if (elements.size() >= 1) {
1929 if ( parseVersionDirect(elements[0]) ) {
1934 /* we need size first in order to calculate image scale */
1935 mydata.explicit_size = false;
1936 for (; i< elements.size(); i++) {
1937 if (!parseSizeDirect(&mydata, elements[i])) {
1942 if (mydata.explicit_size) {
1943 // compute scaling for specified form size
1945 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1946 v2u32 delta = current_screensize - m_lockscreensize;
1948 if (current_screensize.Y > m_lockscreensize.Y)
1953 if (current_screensize.X > m_lockscreensize.X)
1958 offset = v2s32(delta.X,delta.Y);
1960 mydata.screensize = m_lockscreensize;
1962 offset = v2s32(0,0);
1965 double gui_scaling = g_settings->getFloat("gui_scaling");
1966 double screen_dpi = porting::getDisplayDensity() * 96;
1970 // In fixed-size mode, inventory image size
1971 // is 0.53 inch multiplied by the gui_scaling
1972 // config parameter. This magic size is chosen
1973 // to make the main menu (15.5 inventory images
1974 // wide, including border) just fit into the
1975 // default window (800 pixels wide) at 96 DPI
1976 // and default scaling (1.00).
1977 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1979 // In variable-size mode, we prefer to make the
1980 // inventory image size 1/15 of screen height,
1981 // multiplied by the gui_scaling config parameter.
1982 // If the preferred size won't fit the whole
1983 // form on the screen, either horizontally or
1984 // vertically, then we scale it down to fit.
1985 // (The magic numbers in the computation of what
1986 // fits arise from the scaling factors in the
1987 // following stanza, including the form border,
1988 // help text space, and 0.1 inventory slot spare.)
1989 // However, a minimum size is also set, that
1990 // the image size can't be less than 0.3 inch
1991 // multiplied by gui_scaling, even if this means
1992 // the form doesn't fit the screen.
1993 double prefer_imgsize = mydata.screensize.Y / 15 *
1995 double fitx_imgsize = mydata.screensize.X /
1996 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1997 double fity_imgsize = mydata.screensize.Y /
1998 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1999 double screen_dpi = porting::getDisplayDensity() * 96;
2000 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
2001 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2002 MYMIN(fitx_imgsize, fity_imgsize)));
2005 // Everything else is scaled in proportion to the
2006 // inventory image size. The inventory slot spacing
2007 // is 5/4 image size horizontally and 15/13 image size
2008 // vertically. The padding around the form (incorporating
2009 // the border of the outer inventory slots) is 3/8
2010 // image size. Font height (baseline to baseline)
2011 // is 2/5 vertical inventory slot spacing, and button
2012 // half-height is 7/8 of font height.
2013 imgsize = v2s32(use_imgsize, use_imgsize);
2014 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2015 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2016 m_btn_height = use_imgsize*15.0/13 * 0.35;
2018 m_font = g_fontengine->getFont();
2020 mydata.size = v2s32(
2021 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2022 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2024 DesiredRect = mydata.rect = core::rect<s32>(
2025 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
2026 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
2027 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
2028 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
2031 // Non-size[] form must consist only of text fields and
2032 // implicit "Proceed" button. Use default font, and
2033 // temporary form size which will be recalculated below.
2034 m_font = g_fontengine->getFont();
2035 m_btn_height = font_line_height(m_font) * 0.875;
2036 DesiredRect = core::rect<s32>(
2037 mydata.screensize.X/2 - 580/2,
2038 mydata.screensize.Y/2 - 300/2,
2039 mydata.screensize.X/2 + 580/2,
2040 mydata.screensize.Y/2 + 300/2
2043 recalculateAbsolutePosition(false);
2044 mydata.basepos = getBasePos();
2045 m_tooltip_element->setOverrideFont(m_font);
2047 gui::IGUISkin* skin = Environment->getSkin();
2048 sanity_check(skin != NULL);
2049 gui::IGUIFont *old_font = skin->getFont();
2050 skin->setFont(m_font);
2052 for (; i< elements.size(); i++) {
2053 parseElement(&mydata, elements[i]);
2056 // If there are fields without explicit size[], add a "Proceed"
2057 // button and adjust size to fit all the fields.
2058 if (m_fields.size() && !mydata.explicit_size) {
2059 mydata.rect = core::rect<s32>(
2060 mydata.screensize.X/2 - 580/2,
2061 mydata.screensize.Y/2 - 300/2,
2062 mydata.screensize.X/2 + 580/2,
2063 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2065 DesiredRect = mydata.rect;
2066 recalculateAbsolutePosition(false);
2067 mydata.basepos = getBasePos();
2070 v2s32 pos = mydata.basepos;
2071 pos.Y = ((m_fields.size()+2)*60);
2073 v2s32 size = DesiredRect.getSize();
2075 core::rect<s32>(size.X/2-70, pos.Y,
2076 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2077 const wchar_t *text = wgettext("Proceed");
2078 Environment->addButton(mydata.rect, this, 257, text);
2084 //set initial focus if parser didn't set it
2085 focused_element = Environment->getFocus();
2086 if (!focused_element
2087 || !isMyChild(focused_element)
2088 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2091 skin->setFont(old_font);
2095 bool GUIFormSpecMenu::getAndroidUIInput()
2097 /* no dialog shown */
2098 if (m_JavaDialogFieldName == "") {
2103 if (porting::getInputDialogState() == -1) {
2107 std::string fieldname = m_JavaDialogFieldName;
2108 m_JavaDialogFieldName = "";
2110 /* no value abort dialog processing */
2111 if (porting::getInputDialogState() != 0) {
2115 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2116 iter != m_fields.end(); ++iter) {
2118 if (iter->fname != fieldname) {
2121 IGUIElement* tochange = getElementFromId(iter->fid);
2123 if (tochange == 0) {
2127 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2131 std::string text = porting::getInputDialogValue();
2133 ((gui::IGUIEditBox*) tochange)->
2134 setText(utf8_to_wide(text).c_str());
2140 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2142 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2144 for(u32 i=0; i<m_inventorylists.size(); i++)
2146 const ListDrawSpec &s = m_inventorylists[i];
2148 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2149 s32 item_i = i + s.start_item_i;
2150 s32 x = (i%s.geom.X) * spacing.X;
2151 s32 y = (i/s.geom.X) * spacing.Y;
2153 core::rect<s32> rect = imgrect + s.pos + p0;
2154 if(rect.isPointInside(p))
2156 return ItemSpec(s.inventoryloc, s.listname, item_i);
2161 return ItemSpec(InventoryLocation(), "", -1);
2164 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
2167 video::IVideoDriver* driver = Environment->getVideoDriver();
2169 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2171 warningstream<<"GUIFormSpecMenu::drawList(): "
2172 <<"The inventory location "
2173 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2177 InventoryList *ilist = inv->getList(s.listname);
2179 warningstream<<"GUIFormSpecMenu::drawList(): "
2180 <<"The inventory list \""<<s.listname<<"\" @ \""
2181 <<s.inventoryloc.dump()<<"\" doesn't exist"
2186 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2188 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2190 s32 item_i = i + s.start_item_i;
2191 if(item_i >= (s32) ilist->getSize())
2193 s32 x = (i%s.geom.X) * spacing.X;
2194 s32 y = (i/s.geom.X) * spacing.Y;
2196 core::rect<s32> rect = imgrect + s.pos + p;
2199 item = ilist->getItem(item_i);
2201 bool selected = m_selected_item
2202 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2203 && m_selected_item->listname == s.listname
2204 && m_selected_item->i == item_i;
2205 bool hovering = rect.isPointInside(m_pointer);
2206 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2207 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2211 item_hovered = true;
2212 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2214 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2218 //Draw inv slot borders
2220 s32 x1 = rect.UpperLeftCorner.X;
2221 s32 y1 = rect.UpperLeftCorner.Y;
2222 s32 x2 = rect.LowerRightCorner.X;
2223 s32 y2 = rect.LowerRightCorner.Y;
2225 driver->draw2DRectangle(m_slotbordercolor,
2226 core::rect<s32>(v2s32(x1 - border, y1 - border),
2227 v2s32(x2 + border, y1)), NULL);
2228 driver->draw2DRectangle(m_slotbordercolor,
2229 core::rect<s32>(v2s32(x1 - border, y2),
2230 v2s32(x2 + border, y2 + border)), NULL);
2231 driver->draw2DRectangle(m_slotbordercolor,
2232 core::rect<s32>(v2s32(x1 - border, y1),
2233 v2s32(x1, y2)), NULL);
2234 driver->draw2DRectangle(m_slotbordercolor,
2235 core::rect<s32>(v2s32(x2, y1),
2236 v2s32(x2 + border, y2)), NULL);
2244 item.takeItem(m_selected_amount);
2248 drawItemStack(driver, m_font, item,
2249 rect, &AbsoluteClippingRect, m_gamedef,
2254 std::string tooltip_text = "";
2255 if (hovering && !m_selected_item)
2256 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
2257 if (tooltip_text != "") {
2258 std::vector<std::string> tt_rows = str_split(tooltip_text, '\n');
2259 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2260 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2261 m_tooltip_element->setVisible(true);
2262 this->bringToFront(m_tooltip_element);
2263 m_tooltip_element->setText(utf8_to_wide(tooltip_text).c_str());
2264 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2265 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2266 v2u32 screenSize = driver->getScreenSize();
2267 int tooltip_offset_x = m_btn_height;
2268 int tooltip_offset_y = m_btn_height;
2270 tooltip_offset_x *= 3;
2271 tooltip_offset_y = 0;
2272 if (m_pointer.X > (s32)screenSize.X / 2)
2273 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2275 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2276 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2277 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2278 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2279 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2280 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2281 m_tooltip_element->setRelativePosition(core::rect<s32>(
2282 core::position2d<s32>(tooltip_x, tooltip_y),
2283 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2289 void GUIFormSpecMenu::drawSelectedItem()
2291 video::IVideoDriver* driver = Environment->getVideoDriver();
2293 if (!m_selected_item) {
2294 drawItemStack(driver, m_font, ItemStack(),
2295 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2296 NULL, m_gamedef, IT_ROT_DRAGGED);
2300 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2302 InventoryList *list = inv->getList(m_selected_item->listname);
2304 ItemStack stack = list->getItem(m_selected_item->i);
2305 stack.count = m_selected_amount;
2307 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2308 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2309 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, IT_ROT_DRAGGED);
2312 void GUIFormSpecMenu::drawMenu()
2315 std::string newform = m_form_src->getForm();
2316 if(newform != m_formspec_string){
2317 m_formspec_string = newform;
2318 regenerateGui(m_screensize_old);
2322 gui::IGUISkin* skin = Environment->getSkin();
2323 sanity_check(skin != NULL);
2324 gui::IGUIFont *old_font = skin->getFont();
2325 skin->setFont(m_font);
2327 updateSelectedItem();
2329 video::IVideoDriver* driver = Environment->getVideoDriver();
2331 v2u32 screenSize = driver->getScreenSize();
2332 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2334 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2336 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2338 m_tooltip_element->setVisible(false);
2343 for(u32 i=0; i<m_backgrounds.size(); i++)
2345 const ImageDrawSpec &spec = m_backgrounds[i];
2346 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2349 // Image size on screen
2350 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2351 // Image rectangle on screen
2352 core::rect<s32> rect = imgrect + spec.pos;
2354 if (m_clipbackground) {
2355 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2356 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2357 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2358 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2359 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2362 const video::SColor color(255,255,255,255);
2363 const video::SColor colors[] = {color,color,color,color};
2364 draw2DImageFilterScaled(driver, texture, rect,
2365 core::rect<s32>(core::position2d<s32>(0,0),
2366 core::dimension2di(texture->getOriginalSize())),
2367 NULL/*&AbsoluteClippingRect*/, colors, true);
2370 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2371 errorstream << "\t" << spec.name << std::endl;
2378 for(u32 i=0; i<m_boxes.size(); i++)
2380 const BoxDrawSpec &spec = m_boxes[i];
2382 irr::video::SColor todraw = spec.color;
2384 todraw.setAlpha(140);
2386 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2387 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2389 driver->draw2DRectangle(todraw, rect, 0);
2395 gui::IGUIElement::draw();
2400 for(u32 i=0; i<m_images.size(); i++)
2402 const ImageDrawSpec &spec = m_images[i];
2403 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2406 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2407 // Image size on screen
2408 core::rect<s32> imgrect;
2411 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2414 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2416 // Image rectangle on screen
2417 core::rect<s32> rect = imgrect + spec.pos;
2418 const video::SColor color(255,255,255,255);
2419 const video::SColor colors[] = {color,color,color,color};
2420 draw2DImageFilterScaled(driver, texture, rect,
2421 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2422 NULL/*&AbsoluteClippingRect*/, colors, true);
2425 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2426 errorstream << "\t" << spec.name << std::endl;
2433 for(u32 i=0; i<m_itemimages.size(); i++)
2438 const ImageDrawSpec &spec = m_itemimages[i];
2439 IItemDefManager *idef = m_gamedef->idef();
2441 item.deSerialize(spec.item_name, idef);
2442 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2443 // Viewport rectangle on screen
2444 core::rect<s32> rect = imgrect + spec.pos;
2445 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2446 m_gamedef, IT_ROT_NONE);
2451 Phase 0: Item slot rectangles
2452 Phase 1: Item images; prepare tooltip
2454 bool item_hovered = false;
2455 int start_phase = 0;
2456 for (int phase = start_phase; phase <= 1; phase++) {
2457 for (u32 i = 0; i < m_inventorylists.size(); i++) {
2458 drawList(m_inventorylists[i], phase, item_hovered);
2461 if (!item_hovered) {
2462 drawItemStack(driver, m_font, ItemStack(),
2463 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2464 NULL, m_gamedef, IT_ROT_HOVERED);
2467 /* TODO find way to show tooltips on touchscreen */
2468 #ifndef HAVE_TOUCHSCREENGUI
2469 m_pointer = m_device->getCursorControl()->getPosition();
2473 Draw static text elements
2475 for (u32 i = 0; i < m_static_texts.size(); i++) {
2476 const StaticTextSpec &spec = m_static_texts[i];
2477 video::SColor color(255, 255, 255, 255);
2478 m_font->draw(spec.text.c_str(), spec.rect, color, true, true, &spec.rect);
2482 Draw fields/buttons tooltips
2484 gui::IGUIElement *hovered =
2485 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2487 if (hovered != NULL) {
2488 s32 id = hovered->getID();
2492 m_old_tooltip_id = id;
2495 if (id == m_old_tooltip_id) {
2496 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2498 m_hovered_time = getTimeMs();
2499 m_old_tooltip_id = id;
2503 if (id != -1 && delta >= m_tooltip_show_delay) {
2504 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2505 iter != m_fields.end(); ++iter) {
2506 if ( (iter->fid == id) && (m_tooltips[iter->fname].tooltip != "") ){
2507 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2508 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2509 m_tooltip_element->setText(utf8_to_wide(m_tooltips[iter->fname].tooltip).c_str());
2510 std::vector<std::string> tt_rows = str_split(m_tooltips[iter->fname].tooltip, '\n');
2511 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2512 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2513 int tooltip_offset_x = m_btn_height;
2514 int tooltip_offset_y = m_btn_height;
2516 tooltip_offset_x *= 3;
2517 tooltip_offset_y = 0;
2518 if (m_pointer.X > (s32)screenSize.X / 2)
2519 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2521 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2522 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2523 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2524 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2525 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2526 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2527 m_tooltip_element->setRelativePosition(core::rect<s32>(
2528 core::position2d<s32>(tooltip_x, tooltip_y),
2529 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2531 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2532 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2533 m_tooltip_element->setVisible(true);
2534 this->bringToFront(m_tooltip_element);
2542 Draw dragged item stack
2546 skin->setFont(old_font);
2549 void GUIFormSpecMenu::updateSelectedItem()
2551 // If the selected stack has become empty for some reason, deselect it.
2552 // If the selected stack has become inaccessible, deselect it.
2553 // If the selected stack has become smaller, adjust m_selected_amount.
2554 ItemStack selected = verifySelectedItem();
2556 // WARNING: BLACK MAGIC
2557 // See if there is a stack suited for our current guess.
2558 // If such stack does not exist, clear the guess.
2559 if(m_selected_content_guess.name != "" &&
2560 selected.name == m_selected_content_guess.name &&
2561 selected.count == m_selected_content_guess.count){
2562 // Selected item fits the guess. Skip the black magic.
2564 else if(m_selected_content_guess.name != ""){
2566 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2567 const ListDrawSpec &s = m_inventorylists[i];
2568 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2571 InventoryList *list = inv->getList(s.listname);
2574 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2575 u32 item_i = i + s.start_item_i;
2576 if(item_i >= list->getSize())
2578 ItemStack stack = list->getItem(item_i);
2579 if(stack.name == m_selected_content_guess.name &&
2580 stack.count == m_selected_content_guess.count){
2582 infostream<<"Client: Changing selected content guess to "
2583 <<s.inventoryloc.dump()<<" "<<s.listname
2584 <<" "<<item_i<<std::endl;
2585 delete m_selected_item;
2586 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2587 m_selected_amount = stack.count;
2592 infostream<<"Client: Discarding selected content guess: "
2593 <<m_selected_content_guess.getItemString()<<std::endl;
2594 m_selected_content_guess.name = "";
2598 // If craftresult is nonempty and nothing else is selected, select it now.
2599 if(!m_selected_item)
2601 for(u32 i=0; i<m_inventorylists.size(); i++)
2603 const ListDrawSpec &s = m_inventorylists[i];
2604 if(s.listname == "craftpreview")
2606 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2607 InventoryList *list = inv->getList("craftresult");
2608 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2610 m_selected_item = new ItemSpec;
2611 m_selected_item->inventoryloc = s.inventoryloc;
2612 m_selected_item->listname = "craftresult";
2613 m_selected_item->i = 0;
2614 m_selected_amount = 0;
2615 m_selected_dragging = false;
2622 // If craftresult is selected, keep the whole stack selected
2623 if(m_selected_item && m_selected_item->listname == "craftresult")
2625 m_selected_amount = verifySelectedItem().count;
2629 ItemStack GUIFormSpecMenu::verifySelectedItem()
2631 // If the selected stack has become empty for some reason, deselect it.
2632 // If the selected stack has become inaccessible, deselect it.
2633 // If the selected stack has become smaller, adjust m_selected_amount.
2634 // Return the selected stack.
2638 if(m_selected_item->isValid())
2640 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2643 InventoryList *list = inv->getList(m_selected_item->listname);
2644 if(list && (u32) m_selected_item->i < list->getSize())
2646 ItemStack stack = list->getItem(m_selected_item->i);
2647 if(m_selected_amount > stack.count)
2648 m_selected_amount = stack.count;
2655 // selection was not valid
2656 delete m_selected_item;
2657 m_selected_item = NULL;
2658 m_selected_amount = 0;
2659 m_selected_dragging = false;
2664 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2670 if (quitmode == quit_mode_accept) {
2671 fields["quit"] = "true";
2674 if (quitmode == quit_mode_cancel) {
2675 fields["quit"] = "true";
2676 m_text_dst->gotText(fields);
2680 if (current_keys_pending.key_down) {
2681 fields["key_down"] = "true";
2682 current_keys_pending.key_down = false;
2685 if (current_keys_pending.key_up) {
2686 fields["key_up"] = "true";
2687 current_keys_pending.key_up = false;
2690 if (current_keys_pending.key_enter) {
2691 fields["key_enter"] = "true";
2692 current_keys_pending.key_enter = false;
2695 if (current_keys_pending.key_escape) {
2696 fields["key_escape"] = "true";
2697 current_keys_pending.key_escape = false;
2700 for(unsigned int i=0; i<m_fields.size(); i++) {
2701 const FieldSpec &s = m_fields[i];
2703 std::string name = s.fname;
2704 if (s.ftype == f_Button) {
2705 fields[name] = wide_to_utf8(s.flabel);
2706 } else if (s.ftype == f_Table) {
2707 GUITable *table = getTable(s.fname);
2709 fields[name] = table->checkEvent();
2712 else if(s.ftype == f_DropDown) {
2713 // no dynamic cast possible due to some distributions shipped
2714 // without rtti support in irrlicht
2715 IGUIElement * element = getElementFromId(s.fid);
2716 gui::IGUIComboBox *e = NULL;
2717 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2718 e = static_cast<gui::IGUIComboBox*>(element);
2720 s32 selected = e->getSelected();
2721 if (selected >= 0) {
2723 wide_to_utf8(e->getItem(selected));
2726 else if (s.ftype == f_TabHeader) {
2727 // no dynamic cast possible due to some distributions shipped
2728 // without rtti support in irrlicht
2729 IGUIElement * element = getElementFromId(s.fid);
2730 gui::IGUITabControl *e = NULL;
2731 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2732 e = static_cast<gui::IGUITabControl*>(element);
2736 std::stringstream ss;
2737 ss << (e->getActiveTab() +1);
2738 fields[name] = ss.str();
2741 else if (s.ftype == f_CheckBox) {
2742 // no dynamic cast possible due to some distributions shipped
2743 // without rtti support in irrlicht
2744 IGUIElement * element = getElementFromId(s.fid);
2745 gui::IGUICheckBox *e = NULL;
2746 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2747 e = static_cast<gui::IGUICheckBox*>(element);
2752 fields[name] = "true";
2754 fields[name] = "false";
2757 else if (s.ftype == f_ScrollBar) {
2758 // no dynamic cast possible due to some distributions shipped
2759 // without rtti support in irrlicht
2760 IGUIElement * element = getElementFromId(s.fid);
2761 gui::IGUIScrollBar *e = NULL;
2762 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2763 e = static_cast<gui::IGUIScrollBar*>(element);
2767 std::stringstream os;
2769 if (s.fdefault == L"Changed")
2770 fields[name] = "CHG:" + os.str();
2772 fields[name] = "VAL:" + os.str();
2777 IGUIElement* e = getElementFromId(s.fid);
2779 fields[name] = wide_to_utf8(e->getText());
2785 m_text_dst->gotText(fields);
2789 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2791 while(tocheck != NULL) {
2792 if (tocheck == parent) {
2795 tocheck = tocheck->getParent();
2800 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2802 // The IGUITabControl renders visually using the skin's selected
2803 // font, which we override for the duration of form drawing,
2804 // but computes tab hotspots based on how it would have rendered
2805 // using the font that is selected at the time of button release.
2806 // To make these two consistent, temporarily override the skin's
2807 // font while the IGUITabControl is processing the event.
2808 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2809 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2810 s32 x = event.MouseInput.X;
2811 s32 y = event.MouseInput.Y;
2812 gui::IGUIElement *hovered =
2813 Environment->getRootGUIElement()->getElementFromPoint(
2814 core::position2d<s32>(x, y));
2815 if (hovered && isMyChild(hovered) &&
2816 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2817 gui::IGUISkin* skin = Environment->getSkin();
2818 sanity_check(skin != NULL);
2819 gui::IGUIFont *old_font = skin->getFont();
2820 skin->setFont(m_font);
2821 bool retval = hovered->OnEvent(event);
2822 skin->setFont(old_font);
2827 // Fix Esc/Return key being eaten by checkboxen and tables
2828 if(event.EventType==EET_KEY_INPUT_EVENT) {
2829 KeyPress kp(event.KeyInput);
2830 if (kp == EscapeKey || kp == CancelKey
2831 || kp == getKeySetting("keymap_inventory")
2832 || event.KeyInput.Key==KEY_RETURN) {
2833 gui::IGUIElement *focused = Environment->getFocus();
2834 if (focused && isMyChild(focused) &&
2835 (focused->getType() == gui::EGUIET_LIST_BOX ||
2836 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2842 // Mouse wheel events: send to hovered element instead of focused
2843 if(event.EventType==EET_MOUSE_INPUT_EVENT
2844 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2845 s32 x = event.MouseInput.X;
2846 s32 y = event.MouseInput.Y;
2847 gui::IGUIElement *hovered =
2848 Environment->getRootGUIElement()->getElementFromPoint(
2849 core::position2d<s32>(x, y));
2850 if (hovered && isMyChild(hovered)) {
2851 hovered->OnEvent(event);
2856 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2857 s32 x = event.MouseInput.X;
2858 s32 y = event.MouseInput.Y;
2859 gui::IGUIElement *hovered =
2860 Environment->getRootGUIElement()->getElementFromPoint(
2861 core::position2d<s32>(x, y));
2862 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2863 m_old_tooltip_id = -1;
2866 if (!isChild(hovered,this)) {
2867 if (DoubleClickDetection(event)) {
2874 // display software keyboard when clicking edit boxes
2875 if (event.EventType == EET_MOUSE_INPUT_EVENT
2876 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2877 gui::IGUIElement *hovered =
2878 Environment->getRootGUIElement()->getElementFromPoint(
2879 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2880 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2881 bool retval = hovered->OnEvent(event);
2883 Environment->setFocus(hovered);
2885 m_JavaDialogFieldName = getNameByID(hovered->getID());
2886 std::string message = gettext("Enter ");
2887 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
2891 message += gettext(label) + ":";
2893 /* single line text input */
2896 /* multi line text input */
2897 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2901 /* passwords are always single line */
2902 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2906 porting::showInputDialog(gettext("ok"), "",
2907 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
2913 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2916 memset(&translated, 0, sizeof(SEvent));
2917 translated.EventType = EET_MOUSE_INPUT_EVENT;
2918 gui::IGUIElement* root = Environment->getRootGUIElement();
2922 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2926 gui::IGUIElement* hovered = root->getElementFromPoint(
2927 core::position2d<s32>(
2929 event.TouchInput.Y));
2931 translated.MouseInput.X = event.TouchInput.X;
2932 translated.MouseInput.Y = event.TouchInput.Y;
2933 translated.MouseInput.Control = false;
2935 bool dont_send_event = false;
2937 if (event.TouchInput.touchedCount == 1) {
2938 switch (event.TouchInput.Event) {
2939 case ETIE_PRESSED_DOWN:
2940 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2941 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2942 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2943 m_down_pos = m_pointer;
2946 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2947 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2948 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2951 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2952 translated.MouseInput.ButtonStates = 0;
2953 hovered = root->getElementFromPoint(m_down_pos);
2954 /* we don't have a valid pointer element use last
2955 * known pointer pos */
2956 translated.MouseInput.X = m_pointer.X;
2957 translated.MouseInput.Y = m_pointer.Y;
2959 /* reset down pos */
2960 m_down_pos = v2s32(0,0);
2963 dont_send_event = true;
2964 //this is not supposed to happen
2966 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2967 << event.TouchInput.Event << std::endl;
2969 } else if ( (event.TouchInput.touchedCount == 2) &&
2970 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2971 hovered = root->getElementFromPoint(m_down_pos);
2973 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2974 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2975 translated.MouseInput.X = m_pointer.X;
2976 translated.MouseInput.Y = m_pointer.Y;
2979 hovered->OnEvent(translated);
2982 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
2983 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2987 hovered->OnEvent(translated);
2989 dont_send_event = true;
2991 /* ignore unhandled 2 touch events ... accidental moving for example */
2992 else if (event.TouchInput.touchedCount == 2) {
2993 dont_send_event = true;
2995 else if (event.TouchInput.touchedCount > 2) {
2997 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
2998 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3001 if (dont_send_event) {
3005 /* check if translated event needs to be preprocessed again */
3006 if (preprocessEvent(translated)) {
3011 bool retval = hovered->OnEvent(translated);
3013 if (event.TouchInput.Event == ETIE_LEFT_UP) {
3015 m_pointer = v2s32(0,0);
3026 /******************************************************************************/
3027 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3029 /* The following code is for capturing double-clicks of the mouse button
3030 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3031 * -- which closes the form -- under some circumstances.
3033 * There have been many github issues reporting this as a bug even though it
3034 * was an intended feature. For this reason, remapping the double-click as
3035 * an ESC must be explicitly set when creating this class via the
3036 * /p remap_dbl_click parameter of the constructor.
3039 if (!m_remap_dbl_click)
3042 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3043 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3044 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3046 m_doubleclickdetect[1].pos = m_pointer;
3047 m_doubleclickdetect[1].time = getTimeMs();
3049 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3050 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
3055 double squaredistance =
3056 m_doubleclickdetect[0].pos
3057 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3059 if (squaredistance > (30*30)) {
3063 SEvent* translated = new SEvent();
3064 assert(translated != 0);
3065 //translate doubleclick to escape
3066 memset(translated, 0, sizeof(SEvent));
3067 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3068 translated->KeyInput.Key = KEY_ESCAPE;
3069 translated->KeyInput.Control = false;
3070 translated->KeyInput.Shift = false;
3071 translated->KeyInput.PressedDown = true;
3072 translated->KeyInput.Char = 0;
3073 OnEvent(*translated);
3075 // no need to send the key up event as we're already deleted
3076 // and no one else did notice this event
3084 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3086 if (event.EventType==EET_KEY_INPUT_EVENT) {
3087 KeyPress kp(event.KeyInput);
3088 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3089 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3092 acceptInput(quit_mode_cancel);
3095 m_text_dst->gotText(L"MenuQuit");
3098 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3099 (kp == getKeySetting("keymap_screenshot"))) {
3100 m_client->makeScreenshot(m_device);
3102 if (event.KeyInput.PressedDown &&
3103 (event.KeyInput.Key==KEY_RETURN ||
3104 event.KeyInput.Key==KEY_UP ||
3105 event.KeyInput.Key==KEY_DOWN)
3107 switch (event.KeyInput.Key) {
3109 current_keys_pending.key_enter = true;
3112 current_keys_pending.key_up = true;
3115 current_keys_pending.key_down = true;
3119 //can't happen at all!
3120 FATAL_ERROR("Reached a source line that can't ever been reached");
3123 if (current_keys_pending.key_enter && m_allowclose) {
3124 acceptInput(quit_mode_accept);
3134 /* Mouse event other than movement, or crossing the border of inventory
3135 field while holding right mouse button
3137 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3138 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3139 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3140 event.MouseInput.isRightPressed() &&
3141 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3143 // Get selected item and hovered/clicked item (s)
3145 m_old_tooltip_id = -1;
3146 updateSelectedItem();
3147 ItemSpec s = getItemAtPos(m_pointer);
3149 Inventory *inv_selected = NULL;
3150 Inventory *inv_s = NULL;
3151 InventoryList *list_s = NULL;
3153 if (m_selected_item) {
3154 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3155 sanity_check(inv_selected);
3156 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3163 inv_s = m_invmgr->getInventory(s.inventoryloc);
3166 errorstream << "InventoryMenu: The selected inventory location "
3167 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3169 s.i = -1; // make it invalid again
3173 list_s = inv_s->getList(s.listname);
3174 if (list_s == NULL) {
3175 verbosestream << "InventoryMenu: The selected inventory list \""
3176 << s.listname << "\" does not exist" << std::endl;
3177 s.i = -1; // make it invalid again
3181 if ((u32)s.i >= list_s->getSize()) {
3182 infostream << "InventoryMenu: The selected inventory list \""
3183 << s.listname << "\" is too small (i=" << s.i << ", size="
3184 << list_s->getSize() << ")" << std::endl;
3185 s.i = -1; // make it invalid again
3189 s_count = list_s->getItem(s.i).count;
3192 bool identical = (m_selected_item != NULL) && s.isValid() &&
3193 (inv_selected == inv_s) &&
3194 (m_selected_item->listname == s.listname) &&
3195 (m_selected_item->i == s.i);
3197 // buttons: 0 = left, 1 = right, 2 = middle
3198 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3201 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3202 { button = 0; updown = 0; }
3203 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3204 { button = 1; updown = 0; }
3205 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3206 { button = 2; updown = 0; }
3207 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3208 { button = 0; updown = 1; }
3209 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3210 { button = 1; updown = 1; }
3211 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3212 { button = 2; updown = 1; }
3213 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3216 // Set this number to a positive value to generate a move action
3217 // from m_selected_item to s.
3218 u32 move_amount = 0;
3220 // Set this number to a positive value to generate a move action
3221 // from s to the next inventory ring.
3222 u32 shift_move_amount = 0;
3224 // Set this number to a positive value to generate a drop action
3225 // from m_selected_item.
3226 u32 drop_amount = 0;
3228 // Set this number to a positive value to generate a craft action at s.
3229 u32 craft_amount = 0;
3232 // Some mouse button has been pressed
3234 //infostream<<"Mouse button "<<button<<" pressed at p=("
3235 // <<p.X<<","<<p.Y<<")"<<std::endl;
3237 m_selected_dragging = false;
3239 if (s.isValid() && s.listname == "craftpreview") {
3240 // Craft preview has been clicked: craft
3241 craft_amount = (button == 2 ? 10 : 1);
3242 } else if (m_selected_item == NULL) {
3244 // Non-empty stack has been clicked: select or shift-move it
3245 m_selected_item = new ItemSpec(s);
3248 if (button == 1) // right
3249 count = (s_count + 1) / 2;
3250 else if (button == 2) // middle
3251 count = MYMIN(s_count, 10);
3255 if (!event.MouseInput.Shift) {
3256 // no shift: select item
3257 m_selected_amount = count;
3258 m_selected_dragging = true;
3259 m_rmouse_auto_place = false;
3261 // shift pressed: move item
3263 shift_move_amount = count;
3264 else // count of 1 at left click like after drag & drop
3265 shift_move_amount = 1;
3268 } else { // m_selected_item != NULL
3269 assert(m_selected_amount >= 1);
3272 // Clicked a slot: move
3273 if (button == 1) // right
3275 else if (button == 2) // middle
3276 move_amount = MYMIN(m_selected_amount, 10);
3278 move_amount = m_selected_amount;
3281 if (move_amount >= m_selected_amount)
3282 m_selected_amount = 0;
3284 m_selected_amount -= move_amount;
3288 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3289 // Clicked outside of the window: drop
3290 if (button == 1) // right
3292 else if (button == 2) // middle
3293 drop_amount = MYMIN(m_selected_amount, 10);
3295 drop_amount = m_selected_amount;
3299 else if (updown == 1) {
3300 // Some mouse button has been released
3302 //infostream<<"Mouse button "<<button<<" released at p=("
3303 // <<p.X<<","<<p.Y<<")"<<std::endl;
3305 if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3307 // Dragged to different slot: move all selected
3308 move_amount = m_selected_amount;
3310 } else if (m_selected_item != NULL && m_selected_dragging &&
3311 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3312 // Dragged outside of window: drop all selected
3313 drop_amount = m_selected_amount;
3316 m_selected_dragging = false;
3317 // Keep count of how many times right mouse button has been
3318 // clicked. One click is drag without dropping. Click + release
3319 // + click changes to drop one item when moved mode
3320 if (button == 1 && m_selected_item != NULL)
3321 m_rmouse_auto_place = !m_rmouse_auto_place;
3322 } else if (updown == -1) {
3323 // Mouse has been moved and rmb is down and mouse pointer just
3324 // entered a new inventory field (checked in the entry-if, this
3325 // is the only action here that is generated by mouse movement)
3326 if (m_selected_item != NULL && s.isValid()) {
3328 // TODO: middle mouse to move 10 items might be handy
3329 if (m_rmouse_auto_place) {
3330 // Only move an item if the destination slot is empty
3331 // or contains the same item type as what is going to be
3333 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3334 InventoryList *list_to = list_s;
3335 assert(list_from && list_to);
3336 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3337 ItemStack stack_to = list_to->getItem(s.i);
3338 if (stack_to.empty() || stack_to.name == stack_from.name)
3344 // Possibly send inventory action to server
3345 if (move_amount > 0) {
3346 // Send IACTION_MOVE
3348 assert(m_selected_item && m_selected_item->isValid());
3349 assert(s.isValid());
3351 assert(inv_selected && inv_s);
3352 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3353 InventoryList *list_to = list_s;
3354 assert(list_from && list_to);
3355 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3356 ItemStack stack_to = list_to->getItem(s.i);
3358 // Check how many items can be moved
3359 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3360 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3361 // If source stack cannot be added to destination stack at all,
3363 if ((leftover.count == stack_from.count) &&
3364 (leftover.name == stack_from.name)) {
3365 m_selected_amount = stack_to.count;
3366 // In case the server doesn't directly swap them but instead
3367 // moves stack_to somewhere else, set this
3368 m_selected_content_guess = stack_to;
3369 m_selected_content_guess_inventory = s.inventoryloc;
3371 // Source stack goes fully into destination stack
3372 else if (leftover.empty()) {
3373 m_selected_amount -= move_amount;
3374 m_selected_content_guess = ItemStack(); // Clear
3376 // Source stack goes partly into destination stack
3378 move_amount -= leftover.count;
3379 m_selected_amount -= move_amount;
3380 m_selected_content_guess = ItemStack(); // Clear
3383 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3384 IMoveAction *a = new IMoveAction();
3385 a->count = move_amount;
3386 a->from_inv = m_selected_item->inventoryloc;
3387 a->from_list = m_selected_item->listname;
3388 a->from_i = m_selected_item->i;
3389 a->to_inv = s.inventoryloc;
3390 a->to_list = s.listname;
3392 m_invmgr->inventoryAction(a);
3393 } else if (shift_move_amount > 0) {
3394 u32 mis = m_inventory_rings.size();
3396 for (; i < mis; i++) {
3397 const ListRingSpec &sp = m_inventory_rings[i];
3398 if (sp.inventoryloc == s.inventoryloc
3399 && sp.listname == s.listname)
3403 if (i >= mis) // if not found
3405 u32 to_inv_ind = (i + 1) % mis;
3406 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3407 InventoryList *list_from = list_s;
3410 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3413 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3416 ItemStack stack_from = list_from->getItem(s.i);
3417 assert(shift_move_amount <= stack_from.count);
3418 if (m_client->getProtoVersion() >= 25) {
3419 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3420 IMoveAction *a = new IMoveAction();
3421 a->count = shift_move_amount;
3422 a->from_inv = s.inventoryloc;
3423 a->from_list = s.listname;
3425 a->to_inv = to_inv_sp.inventoryloc;
3426 a->to_list = to_inv_sp.listname;
3427 a->move_somewhere = true;
3428 m_invmgr->inventoryAction(a);
3430 // find a place (or more than one) to add the new item
3431 u32 ilt_size = list_to->getSize();
3433 for (u32 slot_to = 0; slot_to < ilt_size
3434 && shift_move_amount > 0; slot_to++) {
3435 list_to->itemFits(slot_to, stack_from, &leftover);
3436 if (leftover.count < stack_from.count) {
3437 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3438 IMoveAction *a = new IMoveAction();
3439 a->count = MYMIN(shift_move_amount,
3440 (u32) (stack_from.count - leftover.count));
3441 shift_move_amount -= a->count;
3442 a->from_inv = s.inventoryloc;
3443 a->from_list = s.listname;
3445 a->to_inv = to_inv_sp.inventoryloc;
3446 a->to_list = to_inv_sp.listname;
3448 m_invmgr->inventoryAction(a);
3449 stack_from = leftover;
3454 } else if (drop_amount > 0) {
3455 m_selected_content_guess = ItemStack(); // Clear
3457 // Send IACTION_DROP
3459 assert(m_selected_item && m_selected_item->isValid());
3460 assert(inv_selected);
3461 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3463 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3465 // Check how many items can be dropped
3466 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3467 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3468 m_selected_amount -= drop_amount;
3470 infostream << "Handing IACTION_DROP to manager" << std::endl;
3471 IDropAction *a = new IDropAction();
3472 a->count = drop_amount;
3473 a->from_inv = m_selected_item->inventoryloc;
3474 a->from_list = m_selected_item->listname;
3475 a->from_i = m_selected_item->i;
3476 m_invmgr->inventoryAction(a);
3477 } else if (craft_amount > 0) {
3478 m_selected_content_guess = ItemStack(); // Clear
3480 // Send IACTION_CRAFT
3482 assert(s.isValid());
3485 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3486 ICraftAction *a = new ICraftAction();
3487 a->count = craft_amount;
3488 a->craft_inv = s.inventoryloc;
3489 m_invmgr->inventoryAction(a);
3492 // If m_selected_amount has been decreased to zero, deselect
3493 if (m_selected_amount == 0) {
3494 delete m_selected_item;
3495 m_selected_item = NULL;
3496 m_selected_amount = 0;
3497 m_selected_dragging = false;
3498 m_selected_content_guess = ItemStack();
3500 m_old_pointer = m_pointer;
3502 if (event.EventType == EET_GUI_EVENT) {
3504 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3506 // find the element that was clicked
3507 for (unsigned int i=0; i<m_fields.size(); i++) {
3508 FieldSpec &s = m_fields[i];
3509 if ((s.ftype == f_TabHeader) &&
3510 (s.fid == event.GUIEvent.Caller->getID())) {
3518 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3520 if (!canTakeFocus(event.GUIEvent.Element)) {
3521 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3523 // Returning true disables focus change
3527 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3528 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3529 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3530 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3531 unsigned int btn_id = event.GUIEvent.Caller->getID();
3533 if (btn_id == 257) {
3535 acceptInput(quit_mode_accept);
3539 m_text_dst->gotText(L"ExitButton");
3541 // quitMenu deallocates menu
3545 // find the element that was clicked
3546 for (u32 i = 0; i < m_fields.size(); i++) {
3547 FieldSpec &s = m_fields[i];
3548 // if its a button, set the send field so
3549 // lua knows which button was pressed
3550 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3551 (s.fid == event.GUIEvent.Caller->getID())) {
3555 acceptInput(quit_mode_accept);
3558 m_text_dst->gotText(L"ExitButton");
3562 acceptInput(quit_mode_no);
3566 } else if ((s.ftype == f_DropDown) &&
3567 (s.fid == event.GUIEvent.Caller->getID())) {
3568 // only send the changed dropdown
3569 for (u32 i = 0; i < m_fields.size(); i++) {
3570 FieldSpec &s2 = m_fields[i];
3571 if (s2.ftype == f_DropDown) {
3576 acceptInput(quit_mode_no);
3578 // revert configuration to make sure dropdowns are sent on
3579 // regular button click
3580 for (u32 i = 0; i < m_fields.size(); i++) {
3581 FieldSpec &s2 = m_fields[i];
3582 if (s2.ftype == f_DropDown) {
3587 } else if ((s.ftype == f_ScrollBar) &&
3588 (s.fid == event.GUIEvent.Caller->getID())) {
3589 s.fdefault = L"Changed";
3590 acceptInput(quit_mode_no);
3596 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3597 if (event.GUIEvent.Caller->getID() > 257) {
3600 acceptInput(quit_mode_accept);
3603 current_keys_pending.key_enter = true;
3606 // quitMenu deallocates menu
3611 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3612 int current_id = event.GUIEvent.Caller->getID();
3613 if (current_id > 257) {
3614 // find the element that was clicked
3615 for (u32 i = 0; i < m_fields.size(); i++) {
3616 FieldSpec &s = m_fields[i];
3617 // if it's a table, set the send field
3618 // so lua knows which table was changed
3619 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3630 return Parent ? Parent->OnEvent(event) : false;
3634 * get name of element by element id
3635 * @param id of element
3636 * @return name string or empty string
3638 std::string GUIFormSpecMenu::getNameByID(s32 id)
3640 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3641 iter != m_fields.end(); ++iter) {
3642 if (iter->fid == id) {
3650 * get label of element by id
3651 * @param id of element
3652 * @return label string or empty string
3654 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3656 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3657 iter != m_fields.end(); ++iter) {
3658 if (iter->fid == id) {
3659 return iter->flabel;