3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guiFormSpecMenu.h"
28 #include "constants.h"
32 #include <IGUICheckBox.h>
33 #include <IGUIEditBox.h>
34 #include <IGUIButton.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include <IGUIComboBox.h>
40 #include "client/tile.h" // ITextureSource
41 #include "hud.h" // drawItemStack
45 #include "scripting_game.h"
49 #include "fontengine.h"
51 #include "util/numeric.h"
52 #include "util/string.h" // for parseColorString()
53 #include "guiscalingfilter.h"
55 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
56 #include "intlGUIEditBox.h"
59 #define MY_CHECKPOS(a,b) \
60 if (v_pos.size() != 2) { \
61 errorstream<< "Invalid pos for element " << a << "specified: \"" \
62 << parts[b] << "\"" << std::endl; \
66 #define MY_CHECKGEOM(a,b) \
67 if (v_geom.size() != 2) { \
68 errorstream<< "Invalid pos for element " << a << "specified: \"" \
69 << parts[b] << "\"" << std::endl; \
75 static unsigned int font_line_height(gui::IGUIFont *font)
77 return font->getDimension(L"Ay").Height + font->getKerningHeight();
80 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
81 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
82 InventoryManager *invmgr, IGameDef *gamedef,
83 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
84 Client* client, bool remap_dbl_click) :
85 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
91 m_selected_item(NULL),
93 m_selected_dragging(false),
94 m_tooltip_element(NULL),
97 m_rmouse_auto_place(false),
102 m_formspec_version(0),
103 m_focused_element(""),
105 m_remap_dbl_click(remap_dbl_click)
107 , m_JavaDialogFieldName("")
110 current_keys_pending.key_down = false;
111 current_keys_pending.key_up = false;
112 current_keys_pending.key_enter = false;
113 current_keys_pending.key_escape = false;
115 m_doubleclickdetect[0].time = 0;
116 m_doubleclickdetect[1].time = 0;
118 m_doubleclickdetect[0].pos = v2s32(0, 0);
119 m_doubleclickdetect[1].pos = v2s32(0, 0);
121 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
124 GUIFormSpecMenu::~GUIFormSpecMenu()
128 for (u32 i = 0; i < m_tables.size(); ++i) {
129 GUITable *table = m_tables[i].second;
133 delete m_selected_item;
135 if (m_form_src != NULL) {
138 if (m_text_dst != NULL) {
143 void GUIFormSpecMenu::removeChildren()
145 const core::list<gui::IGUIElement*> &children = getChildren();
147 while(!children.empty()) {
148 (*children.getLast())->remove();
151 if(m_tooltip_element) {
152 m_tooltip_element->remove();
153 m_tooltip_element->drop();
154 m_tooltip_element = NULL;
159 void GUIFormSpecMenu::setInitialFocus()
161 // Set initial focus according to following order of precedence:
162 // 1. first empty editbox
166 // 5. first focusable (not statictext, not tabheader)
167 // 6. first child element
169 core::list<gui::IGUIElement*> children = getChildren();
171 // in case "children" contains any NULL elements, remove them
172 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
173 it != children.end();) {
177 it = children.erase(it);
180 // 1. first empty editbox
181 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
182 it != children.end(); ++it) {
183 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
184 && (*it)->getText()[0] == 0) {
185 Environment->setFocus(*it);
191 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
192 it != children.end(); ++it) {
193 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
194 Environment->setFocus(*it);
200 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
201 it != children.end(); ++it) {
202 if ((*it)->getTypeName() == std::string("GUITable")) {
203 Environment->setFocus(*it);
209 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
210 it != children.end(); --it) {
211 if ((*it)->getType() == gui::EGUIET_BUTTON) {
212 Environment->setFocus(*it);
217 // 5. first focusable (not statictext, not tabheader)
218 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
219 it != children.end(); ++it) {
220 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
221 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
222 Environment->setFocus(*it);
227 // 6. first child element
228 if (children.empty())
229 Environment->setFocus(this);
231 Environment->setFocus(*(children.begin()));
234 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
236 for (u32 i = 0; i < m_tables.size(); ++i) {
237 if (tablename == m_tables[i].first.fname)
238 return m_tables[i].second;
243 static std::vector<std::string> split(const std::string &s, char delim)
245 std::vector<std::string> tokens;
247 std::string current = "";
248 bool last_was_escape = false;
249 for (unsigned int i = 0; i < s.size(); i++) {
250 char si = s.c_str()[i];
251 if (last_was_escape) {
254 last_was_escape = false;
257 tokens.push_back(current);
259 last_was_escape = false;
260 } else if (si == '\\') {
261 last_was_escape = true;
264 last_was_escape = false;
269 tokens.push_back(current);
274 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
276 std::vector<std::string> parts = split(element,',');
278 if (((parts.size() == 2) || parts.size() == 3) ||
279 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
281 if (parts[1].find(';') != std::string::npos)
282 parts[1] = parts[1].substr(0,parts[1].find(';'));
284 data->invsize.X = MYMAX(0, stof(parts[0]));
285 data->invsize.Y = MYMAX(0, stof(parts[1]));
288 if (parts.size() == 3) {
289 if (parts[2] == "true") {
290 lockSize(true,v2u32(800,600));
294 data->explicit_size = true;
297 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
300 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
302 if (m_gamedef == 0) {
303 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
307 std::vector<std::string> parts = split(element,';');
309 if (((parts.size() == 4) || (parts.size() == 5)) ||
310 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
312 std::string location = parts[0];
313 std::string listname = parts[1];
314 std::vector<std::string> v_pos = split(parts[2],',');
315 std::vector<std::string> v_geom = split(parts[3],',');
316 std::string startindex = "";
317 if (parts.size() == 5)
318 startindex = parts[4];
320 MY_CHECKPOS("list",2);
321 MY_CHECKGEOM("list",3);
323 InventoryLocation loc;
325 if(location == "context" || location == "current_name")
326 loc = m_current_inventory_location;
328 loc.deSerialize(location);
330 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
331 pos.X += stof(v_pos[0]) * (float)spacing.X;
332 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
335 geom.X = stoi(v_geom[0]);
336 geom.Y = stoi(v_geom[1]);
340 start_i = stoi(startindex);
342 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
343 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
347 if(!data->explicit_size)
348 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
349 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
352 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
355 void GUIFormSpecMenu::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[2];
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 errorstream<<"WARNING: 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 errorstream<<"WARNING: 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 errorstream<<"WARNING: 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 errorstream<<"WARNING: 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 errorstream<<"WARNING: 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 errorstream<<"WARNING: 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 errorstream<<"WARNING: 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 errorstream<<"WARNING: 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 errorstream<<"WARNING: 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 errorstream<<"WARNING: 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) {
1453 "WARNING: invalid use of item_image_button with m_gamedef==0"
1458 std::vector<std::string> parts = split(element,';');
1460 if ((parts.size() == 5) ||
1461 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1463 std::vector<std::string> v_pos = split(parts[0],',');
1464 std::vector<std::string> v_geom = split(parts[1],',');
1465 std::string item_name = parts[2];
1466 std::string name = parts[3];
1467 std::string label = parts[4];
1469 MY_CHECKPOS("itemimagebutton",0);
1470 MY_CHECKGEOM("itemimagebutton",1);
1472 v2s32 pos = padding;
1473 pos.X += stof(v_pos[0]) * (float)spacing.X;
1474 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1476 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1477 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1479 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1481 if(!data->explicit_size)
1482 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1484 IItemDefManager *idef = m_gamedef->idef();
1486 item.deSerialize(item_name, idef);
1487 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1490 TooltipSpec(item.getDefinition(idef).description,
1491 m_default_tooltip_bgcolor,
1492 m_default_tooltip_color);
1494 label = unescape_string(label);
1497 utf8_to_wide(label),
1498 utf8_to_wide(item_name),
1499 258 + m_fields.size()
1502 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1504 if (spec.fname == data->focused_fieldname) {
1505 Environment->setFocus(e);
1508 e->setUseAlphaChannel(true);
1509 e->setImage(guiScalingImageButton(Environment->getVideoDriver(), texture, geom.X, geom.Y));
1510 e->setPressedImage(guiScalingImageButton(Environment->getVideoDriver(), texture, geom.X, geom.Y));
1511 e->setScaleImage(true);
1512 spec.ftype = f_Button;
1513 rect+=data->basepos-padding;
1515 m_fields.push_back(spec);
1518 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1521 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1523 std::vector<std::string> parts = split(element,';');
1525 if ((parts.size() == 3) ||
1526 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1528 std::vector<std::string> v_pos = split(parts[0],',');
1529 std::vector<std::string> v_geom = split(parts[1],',');
1531 MY_CHECKPOS("box",0);
1532 MY_CHECKGEOM("box",1);
1534 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1535 pos.X += stof(v_pos[0]) * (float) spacing.X;
1536 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1539 geom.X = stof(v_geom[0]) * (float)spacing.X;
1540 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1542 video::SColor tmp_color;
1544 if (parseColorString(parts[2], tmp_color, false)) {
1545 BoxDrawSpec spec(pos, geom, tmp_color);
1547 m_boxes.push_back(spec);
1550 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1554 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1557 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1559 std::vector<std::string> parts = split(element,';');
1561 if (((parts.size() == 1) || (parts.size() == 2)) ||
1562 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1564 parseColorString(parts[0],m_bgcolor,false);
1566 if (parts.size() == 2) {
1567 std::string fullscreen = parts[1];
1568 m_bgfullscreen = is_yes(fullscreen);
1572 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1575 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1577 std::vector<std::string> parts = split(element,';');
1579 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1580 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1582 parseColorString(parts[0], m_slotbg_n, false);
1583 parseColorString(parts[1], m_slotbg_h, false);
1585 if (parts.size() >= 3) {
1586 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1587 m_slotborder = true;
1590 if (parts.size() == 5) {
1591 video::SColor tmp_color;
1593 if (parseColorString(parts[3], tmp_color, false))
1594 m_default_tooltip_bgcolor = tmp_color;
1595 if (parseColorString(parts[4], tmp_color, false))
1596 m_default_tooltip_color = tmp_color;
1600 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1603 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1605 std::vector<std::string> parts = split(element,';');
1606 if (parts.size() == 2) {
1607 std::string name = parts[0];
1608 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1609 m_default_tooltip_bgcolor, m_default_tooltip_color);
1611 } else if (parts.size() == 4) {
1612 std::string name = parts[0];
1613 video::SColor tmp_color1, tmp_color2;
1614 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1615 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1616 tmp_color1, tmp_color2);
1620 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1623 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1629 std::vector<std::string> parts = split(data,'[');
1631 if (parts.size() < 2) {
1635 if (parts[0] != "formspec_version") {
1639 if (is_number(parts[1])) {
1640 m_formspec_version = mystoi(parts[1]);
1647 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1652 std::vector<std::string> parts = split(element,'[');
1654 if (parts.size() < 2)
1657 std::string type = trim(parts[0]);
1658 std::string description = trim(parts[1]);
1660 if (type != "size" && type != "invsize")
1663 if (type == "invsize")
1664 log_deprecated("Deprecated formspec element \"invsize\" is used");
1666 parseSize(data, description);
1671 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1677 std::vector<std::string> parts = split(element,'[');
1679 // ugly workaround to keep compatibility
1680 if (parts.size() > 2) {
1681 if (trim(parts[0]) == "image") {
1682 for (unsigned int i=2;i< parts.size(); i++) {
1683 parts[1] += "[" + parts[i];
1689 if (parts.size() < 2) {
1693 std::string type = trim(parts[0]);
1694 std::string description = trim(parts[1]);
1696 if (type == "list") {
1697 parseList(data,description);
1701 if (type == "listring") {
1702 parseListRing(data, description);
1706 if (type == "checkbox") {
1707 parseCheckbox(data,description);
1711 if (type == "image") {
1712 parseImage(data,description);
1716 if (type == "item_image") {
1717 parseItemImage(data,description);
1721 if ((type == "button") || (type == "button_exit")) {
1722 parseButton(data,description,type);
1726 if (type == "background") {
1727 parseBackground(data,description);
1731 if (type == "tableoptions"){
1732 parseTableOptions(data,description);
1736 if (type == "tablecolumns"){
1737 parseTableColumns(data,description);
1741 if (type == "table"){
1742 parseTable(data,description);
1746 if (type == "textlist"){
1747 parseTextList(data,description);
1751 if (type == "dropdown"){
1752 parseDropDown(data,description);
1756 if (type == "pwdfield") {
1757 parsePwdField(data,description);
1761 if ((type == "field") || (type == "textarea")){
1762 parseField(data,description,type);
1766 if (type == "label") {
1767 parseLabel(data,description);
1771 if (type == "vertlabel") {
1772 parseVertLabel(data,description);
1776 if (type == "item_image_button") {
1777 parseItemImageButton(data,description);
1781 if ((type == "image_button") || (type == "image_button_exit")) {
1782 parseImageButton(data,description,type);
1786 if (type == "tabheader") {
1787 parseTabHeader(data,description);
1791 if (type == "box") {
1792 parseBox(data,description);
1796 if (type == "bgcolor") {
1797 parseBackgroundColor(data,description);
1801 if (type == "listcolors") {
1802 parseListColors(data,description);
1806 if (type == "tooltip") {
1807 parseTooltip(data,description);
1811 if (type == "scrollbar") {
1812 parseScrollBar(data, description);
1818 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1822 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1824 /* useless to regenerate without a screensize */
1825 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1832 for (u32 i = 0; i < m_tables.size(); ++i) {
1833 std::string tablename = m_tables[i].first.fname;
1834 GUITable *table = m_tables[i].second;
1835 mydata.table_dyndata[tablename] = table->getDynamicData();
1839 if (!m_focused_element.empty())
1840 mydata.focused_fieldname = m_focused_element;
1843 gui::IGUIElement *focused_element = Environment->getFocus();
1844 if (focused_element && focused_element->getParent() == this) {
1845 s32 focused_id = focused_element->getID();
1846 if (focused_id > 257) {
1847 for (u32 i=0; i<m_fields.size(); i++) {
1848 if (m_fields[i].fid == focused_id) {
1849 mydata.focused_fieldname =
1860 for (u32 i = 0; i < m_tables.size(); ++i) {
1861 GUITable *table = m_tables[i].second;
1865 mydata.size= v2s32(100,100);
1866 mydata.screensize = screensize;
1868 // Base position of contents of form
1869 mydata.basepos = getBasePos();
1871 /* Convert m_init_draw_spec to m_inventorylists */
1873 m_inventorylists.clear();
1875 m_backgrounds.clear();
1876 m_itemimages.clear();
1878 m_checkboxes.clear();
1879 m_scrollbars.clear();
1883 m_inventory_rings.clear();
1885 // Set default values (fits old formspec values)
1886 m_bgcolor = video::SColor(140,0,0,0);
1887 m_bgfullscreen = false;
1889 m_slotbg_n = video::SColor(255,128,128,128);
1890 m_slotbg_h = video::SColor(255,192,192,192);
1892 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1893 m_default_tooltip_color = video::SColor(255,255,255,255);
1895 m_slotbordercolor = video::SColor(200,0,0,0);
1896 m_slotborder = false;
1898 m_clipbackground = false;
1901 assert(m_tooltip_element == NULL);
1902 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1903 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1904 m_tooltip_element->enableOverrideColor(true);
1905 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1906 m_tooltip_element->setDrawBackground(true);
1907 m_tooltip_element->setDrawBorder(true);
1908 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1909 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1910 m_tooltip_element->setWordWrap(false);
1911 //we're not parent so no autograb for this one!
1912 m_tooltip_element->grab();
1915 std::vector<std::string> elements = split(m_formspec_string,']');
1918 /* try to read version from first element only */
1919 if (elements.size() >= 1) {
1920 if ( parseVersionDirect(elements[0]) ) {
1925 /* we need size first in order to calculate image scale */
1926 mydata.explicit_size = false;
1927 for (; i< elements.size(); i++) {
1928 if (!parseSizeDirect(&mydata, elements[i])) {
1933 if (mydata.explicit_size) {
1934 // compute scaling for specified form size
1936 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1937 v2u32 delta = current_screensize - m_lockscreensize;
1939 if (current_screensize.Y > m_lockscreensize.Y)
1944 if (current_screensize.X > m_lockscreensize.X)
1949 offset = v2s32(delta.X,delta.Y);
1951 mydata.screensize = m_lockscreensize;
1953 offset = v2s32(0,0);
1956 double gui_scaling = g_settings->getFloat("gui_scaling");
1957 double screen_dpi = porting::getDisplayDensity() * 96;
1961 // In fixed-size mode, inventory image size
1962 // is 0.53 inch multiplied by the gui_scaling
1963 // config parameter. This magic size is chosen
1964 // to make the main menu (15.5 inventory images
1965 // wide, including border) just fit into the
1966 // default window (800 pixels wide) at 96 DPI
1967 // and default scaling (1.00).
1968 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1970 // In variable-size mode, we prefer to make the
1971 // inventory image size 1/15 of screen height,
1972 // multiplied by the gui_scaling config parameter.
1973 // If the preferred size won't fit the whole
1974 // form on the screen, either horizontally or
1975 // vertically, then we scale it down to fit.
1976 // (The magic numbers in the computation of what
1977 // fits arise from the scaling factors in the
1978 // following stanza, including the form border,
1979 // help text space, and 0.1 inventory slot spare.)
1980 // However, a minimum size is also set, that
1981 // the image size can't be less than 0.3 inch
1982 // multiplied by gui_scaling, even if this means
1983 // the form doesn't fit the screen.
1984 double prefer_imgsize = mydata.screensize.Y / 15 *
1986 double fitx_imgsize = mydata.screensize.X /
1987 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1988 double fity_imgsize = mydata.screensize.Y /
1989 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1990 double screen_dpi = porting::getDisplayDensity() * 96;
1991 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1992 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1993 MYMIN(fitx_imgsize, fity_imgsize)));
1996 // Everything else is scaled in proportion to the
1997 // inventory image size. The inventory slot spacing
1998 // is 5/4 image size horizontally and 15/13 image size
1999 // vertically. The padding around the form (incorporating
2000 // the border of the outer inventory slots) is 3/8
2001 // image size. Font height (baseline to baseline)
2002 // is 2/5 vertical inventory slot spacing, and button
2003 // half-height is 7/8 of font height.
2004 imgsize = v2s32(use_imgsize, use_imgsize);
2005 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2006 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2007 m_btn_height = use_imgsize*15.0/13 * 0.35;
2009 m_font = g_fontengine->getFont();
2011 mydata.size = v2s32(
2012 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2013 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2015 DesiredRect = mydata.rect = core::rect<s32>(
2016 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
2017 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
2018 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
2019 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
2022 // Non-size[] form must consist only of text fields and
2023 // implicit "Proceed" button. Use default font, and
2024 // temporary form size which will be recalculated below.
2025 m_font = g_fontengine->getFont();
2026 m_btn_height = font_line_height(m_font) * 0.875;
2027 DesiredRect = core::rect<s32>(
2028 mydata.screensize.X/2 - 580/2,
2029 mydata.screensize.Y/2 - 300/2,
2030 mydata.screensize.X/2 + 580/2,
2031 mydata.screensize.Y/2 + 300/2
2034 recalculateAbsolutePosition(false);
2035 mydata.basepos = getBasePos();
2036 m_tooltip_element->setOverrideFont(m_font);
2038 gui::IGUISkin* skin = Environment->getSkin();
2039 sanity_check(skin != NULL);
2040 gui::IGUIFont *old_font = skin->getFont();
2041 skin->setFont(m_font);
2043 for (; i< elements.size(); i++) {
2044 parseElement(&mydata, elements[i]);
2047 // If there are fields without explicit size[], add a "Proceed"
2048 // button and adjust size to fit all the fields.
2049 if (m_fields.size() && !mydata.explicit_size) {
2050 mydata.rect = core::rect<s32>(
2051 mydata.screensize.X/2 - 580/2,
2052 mydata.screensize.Y/2 - 300/2,
2053 mydata.screensize.X/2 + 580/2,
2054 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2056 DesiredRect = mydata.rect;
2057 recalculateAbsolutePosition(false);
2058 mydata.basepos = getBasePos();
2061 v2s32 pos = mydata.basepos;
2062 pos.Y = ((m_fields.size()+2)*60);
2064 v2s32 size = DesiredRect.getSize();
2066 core::rect<s32>(size.X/2-70, pos.Y,
2067 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2068 const wchar_t *text = wgettext("Proceed");
2069 Environment->addButton(mydata.rect, this, 257, text);
2075 //set initial focus if parser didn't set it
2076 focused_element = Environment->getFocus();
2077 if (!focused_element
2078 || !isMyChild(focused_element)
2079 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2082 skin->setFont(old_font);
2086 bool GUIFormSpecMenu::getAndroidUIInput()
2088 /* no dialog shown */
2089 if (m_JavaDialogFieldName == "") {
2094 if (porting::getInputDialogState() == -1) {
2098 std::string fieldname = m_JavaDialogFieldName;
2099 m_JavaDialogFieldName = "";
2101 /* no value abort dialog processing */
2102 if (porting::getInputDialogState() != 0) {
2106 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2107 iter != m_fields.end(); ++iter) {
2109 if (iter->fname != fieldname) {
2112 IGUIElement* tochange = getElementFromId(iter->fid);
2114 if (tochange == 0) {
2118 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2122 std::string text = porting::getInputDialogValue();
2124 ((gui::IGUIEditBox*) tochange)->
2125 setText(utf8_to_wide(text).c_str());
2131 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2133 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2135 for(u32 i=0; i<m_inventorylists.size(); i++)
2137 const ListDrawSpec &s = m_inventorylists[i];
2139 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2140 s32 item_i = i + s.start_item_i;
2141 s32 x = (i%s.geom.X) * spacing.X;
2142 s32 y = (i/s.geom.X) * spacing.Y;
2144 core::rect<s32> rect = imgrect + s.pos + p0;
2145 if(rect.isPointInside(p))
2147 return ItemSpec(s.inventoryloc, s.listname, item_i);
2152 return ItemSpec(InventoryLocation(), "", -1);
2155 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
2157 video::IVideoDriver* driver = Environment->getVideoDriver();
2159 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2161 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2162 <<"The inventory location "
2163 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2167 InventoryList *ilist = inv->getList(s.listname);
2169 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2170 <<"The inventory list \""<<s.listname<<"\" @ \""
2171 <<s.inventoryloc.dump()<<"\" doesn't exist"
2176 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2178 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2180 s32 item_i = i + s.start_item_i;
2181 if(item_i >= (s32) ilist->getSize())
2183 s32 x = (i%s.geom.X) * spacing.X;
2184 s32 y = (i/s.geom.X) * spacing.Y;
2186 core::rect<s32> rect = imgrect + s.pos + p;
2189 item = ilist->getItem(item_i);
2191 bool selected = m_selected_item
2192 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2193 && m_selected_item->listname == s.listname
2194 && m_selected_item->i == item_i;
2195 bool hovering = rect.isPointInside(m_pointer);
2200 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2202 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2205 //Draw inv slot borders
2207 s32 x1 = rect.UpperLeftCorner.X;
2208 s32 y1 = rect.UpperLeftCorner.Y;
2209 s32 x2 = rect.LowerRightCorner.X;
2210 s32 y2 = rect.LowerRightCorner.Y;
2212 driver->draw2DRectangle(m_slotbordercolor,
2213 core::rect<s32>(v2s32(x1 - border, y1 - border),
2214 v2s32(x2 + border, y1)), NULL);
2215 driver->draw2DRectangle(m_slotbordercolor,
2216 core::rect<s32>(v2s32(x1 - border, y2),
2217 v2s32(x2 + border, y2 + border)), NULL);
2218 driver->draw2DRectangle(m_slotbordercolor,
2219 core::rect<s32>(v2s32(x1 - border, y1),
2220 v2s32(x1, y2)), NULL);
2221 driver->draw2DRectangle(m_slotbordercolor,
2222 core::rect<s32>(v2s32(x2, y1),
2223 v2s32(x2 + border, y2)), NULL);
2231 item.takeItem(m_selected_amount);
2235 drawItemStack(driver, m_font, item,
2236 rect, &AbsoluteClippingRect, m_gamedef);
2240 std::string tooltip_text = "";
2241 if (hovering && !m_selected_item)
2242 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
2243 if (tooltip_text != "") {
2244 std::vector<std::string> tt_rows = str_split(tooltip_text, '\n');
2245 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2246 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2247 m_tooltip_element->setVisible(true);
2248 this->bringToFront(m_tooltip_element);
2249 m_tooltip_element->setText(utf8_to_wide(tooltip_text).c_str());
2250 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2251 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2252 v2u32 screenSize = driver->getScreenSize();
2253 int tooltip_offset_x = m_btn_height;
2254 int tooltip_offset_y = m_btn_height;
2256 tooltip_offset_x *= 3;
2257 tooltip_offset_y = 0;
2258 if (m_pointer.X > (s32)screenSize.X / 2)
2259 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2261 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2262 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2263 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2264 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2265 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2266 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2267 m_tooltip_element->setRelativePosition(core::rect<s32>(
2268 core::position2d<s32>(tooltip_x, tooltip_y),
2269 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2275 void GUIFormSpecMenu::drawSelectedItem()
2277 if(!m_selected_item)
2280 video::IVideoDriver* driver = Environment->getVideoDriver();
2282 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2284 InventoryList *list = inv->getList(m_selected_item->listname);
2286 ItemStack stack = list->getItem(m_selected_item->i);
2287 stack.count = m_selected_amount;
2289 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2290 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2291 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef);
2294 void GUIFormSpecMenu::drawMenu()
2297 std::string newform = m_form_src->getForm();
2298 if(newform != m_formspec_string){
2299 m_formspec_string = newform;
2300 regenerateGui(m_screensize_old);
2304 gui::IGUISkin* skin = Environment->getSkin();
2305 sanity_check(skin != NULL);
2306 gui::IGUIFont *old_font = skin->getFont();
2307 skin->setFont(m_font);
2309 updateSelectedItem();
2311 video::IVideoDriver* driver = Environment->getVideoDriver();
2313 v2u32 screenSize = driver->getScreenSize();
2314 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2316 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2318 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2320 m_tooltip_element->setVisible(false);
2325 for(u32 i=0; i<m_backgrounds.size(); i++)
2327 const ImageDrawSpec &spec = m_backgrounds[i];
2328 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2331 // Image size on screen
2332 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2333 // Image rectangle on screen
2334 core::rect<s32> rect = imgrect + spec.pos;
2336 if (m_clipbackground) {
2337 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2338 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2339 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2340 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2341 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2344 const video::SColor color(255,255,255,255);
2345 const video::SColor colors[] = {color,color,color,color};
2346 draw2DImageFilterScaled(driver, texture, rect,
2347 core::rect<s32>(core::position2d<s32>(0,0),
2348 core::dimension2di(texture->getOriginalSize())),
2349 NULL/*&AbsoluteClippingRect*/, colors, true);
2352 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2353 errorstream << "\t" << spec.name << std::endl;
2360 for(u32 i=0; i<m_boxes.size(); i++)
2362 const BoxDrawSpec &spec = m_boxes[i];
2364 irr::video::SColor todraw = spec.color;
2366 todraw.setAlpha(140);
2368 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2369 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2371 driver->draw2DRectangle(todraw, rect, 0);
2376 for(u32 i=0; i<m_images.size(); i++)
2378 const ImageDrawSpec &spec = m_images[i];
2379 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2382 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2383 // Image size on screen
2384 core::rect<s32> imgrect;
2387 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2390 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2392 // Image rectangle on screen
2393 core::rect<s32> rect = imgrect + spec.pos;
2394 const video::SColor color(255,255,255,255);
2395 const video::SColor colors[] = {color,color,color,color};
2396 draw2DImageFilterScaled(driver, texture, rect,
2397 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2398 NULL/*&AbsoluteClippingRect*/, colors, true);
2401 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2402 errorstream << "\t" << spec.name << std::endl;
2409 for(u32 i=0; i<m_itemimages.size(); i++)
2414 const ImageDrawSpec &spec = m_itemimages[i];
2415 IItemDefManager *idef = m_gamedef->idef();
2417 item.deSerialize(spec.name, idef);
2418 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
2419 // Image size on screen
2420 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2421 // Image rectangle on screen
2422 core::rect<s32> rect = imgrect + spec.pos;
2423 const video::SColor color(255,255,255,255);
2424 const video::SColor colors[] = {color,color,color,color};
2425 draw2DImageFilterScaled(driver, texture, rect,
2426 core::rect<s32>(core::position2d<s32>(0,0),
2427 core::dimension2di(texture->getOriginalSize())),
2428 NULL/*&AbsoluteClippingRect*/, colors, true);
2433 Phase 0: Item slot rectangles
2434 Phase 1: Item images; prepare tooltip
2437 for(int phase=start_phase; phase<=1; phase++)
2438 for(u32 i=0; i<m_inventorylists.size(); i++)
2440 drawList(m_inventorylists[i], phase);
2446 gui::IGUIElement::draw();
2448 /* TODO find way to show tooltips on touchscreen */
2449 #ifndef HAVE_TOUCHSCREENGUI
2450 m_pointer = m_device->getCursorControl()->getPosition();
2454 Draw fields/buttons tooltips
2456 gui::IGUIElement *hovered =
2457 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2459 if (hovered != NULL) {
2460 s32 id = hovered->getID();
2464 m_old_tooltip_id = id;
2467 if (id == m_old_tooltip_id) {
2468 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2470 m_hovered_time = getTimeMs();
2471 m_old_tooltip_id = id;
2475 if (id != -1 && delta >= m_tooltip_show_delay) {
2476 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2477 iter != m_fields.end(); ++iter) {
2478 if ( (iter->fid == id) && (m_tooltips[iter->fname].tooltip != "") ){
2479 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2480 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2481 m_tooltip_element->setText(utf8_to_wide(m_tooltips[iter->fname].tooltip).c_str());
2482 std::vector<std::string> tt_rows = str_split(m_tooltips[iter->fname].tooltip, '\n');
2483 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2484 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2485 int tooltip_offset_x = m_btn_height;
2486 int tooltip_offset_y = m_btn_height;
2488 tooltip_offset_x *= 3;
2489 tooltip_offset_y = 0;
2490 if (m_pointer.X > (s32)screenSize.X / 2)
2491 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2493 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2494 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2495 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2496 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2497 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2498 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2499 m_tooltip_element->setRelativePosition(core::rect<s32>(
2500 core::position2d<s32>(tooltip_x, tooltip_y),
2501 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2503 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2504 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2505 m_tooltip_element->setVisible(true);
2506 this->bringToFront(m_tooltip_element);
2514 Draw dragged item stack
2518 skin->setFont(old_font);
2521 void GUIFormSpecMenu::updateSelectedItem()
2523 // If the selected stack has become empty for some reason, deselect it.
2524 // If the selected stack has become inaccessible, deselect it.
2525 // If the selected stack has become smaller, adjust m_selected_amount.
2526 ItemStack selected = verifySelectedItem();
2528 // WARNING: BLACK MAGIC
2529 // See if there is a stack suited for our current guess.
2530 // If such stack does not exist, clear the guess.
2531 if(m_selected_content_guess.name != "" &&
2532 selected.name == m_selected_content_guess.name &&
2533 selected.count == m_selected_content_guess.count){
2534 // Selected item fits the guess. Skip the black magic.
2536 else if(m_selected_content_guess.name != ""){
2538 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2539 const ListDrawSpec &s = m_inventorylists[i];
2540 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2543 InventoryList *list = inv->getList(s.listname);
2546 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2547 u32 item_i = i + s.start_item_i;
2548 if(item_i >= list->getSize())
2550 ItemStack stack = list->getItem(item_i);
2551 if(stack.name == m_selected_content_guess.name &&
2552 stack.count == m_selected_content_guess.count){
2554 infostream<<"Client: Changing selected content guess to "
2555 <<s.inventoryloc.dump()<<" "<<s.listname
2556 <<" "<<item_i<<std::endl;
2557 delete m_selected_item;
2558 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2559 m_selected_amount = stack.count;
2564 infostream<<"Client: Discarding selected content guess: "
2565 <<m_selected_content_guess.getItemString()<<std::endl;
2566 m_selected_content_guess.name = "";
2570 // If craftresult is nonempty and nothing else is selected, select it now.
2571 if(!m_selected_item)
2573 for(u32 i=0; i<m_inventorylists.size(); i++)
2575 const ListDrawSpec &s = m_inventorylists[i];
2576 if(s.listname == "craftpreview")
2578 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2579 InventoryList *list = inv->getList("craftresult");
2580 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2582 m_selected_item = new ItemSpec;
2583 m_selected_item->inventoryloc = s.inventoryloc;
2584 m_selected_item->listname = "craftresult";
2585 m_selected_item->i = 0;
2586 m_selected_amount = 0;
2587 m_selected_dragging = false;
2594 // If craftresult is selected, keep the whole stack selected
2595 if(m_selected_item && m_selected_item->listname == "craftresult")
2597 m_selected_amount = verifySelectedItem().count;
2601 ItemStack GUIFormSpecMenu::verifySelectedItem()
2603 // If the selected stack has become empty for some reason, deselect it.
2604 // If the selected stack has become inaccessible, deselect it.
2605 // If the selected stack has become smaller, adjust m_selected_amount.
2606 // Return the selected stack.
2610 if(m_selected_item->isValid())
2612 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2615 InventoryList *list = inv->getList(m_selected_item->listname);
2616 if(list && (u32) m_selected_item->i < list->getSize())
2618 ItemStack stack = list->getItem(m_selected_item->i);
2619 if(m_selected_amount > stack.count)
2620 m_selected_amount = stack.count;
2627 // selection was not valid
2628 delete m_selected_item;
2629 m_selected_item = NULL;
2630 m_selected_amount = 0;
2631 m_selected_dragging = false;
2636 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2642 if (quitmode == quit_mode_accept) {
2643 fields["quit"] = "true";
2646 if (quitmode == quit_mode_cancel) {
2647 fields["quit"] = "true";
2648 m_text_dst->gotText(fields);
2652 if (current_keys_pending.key_down) {
2653 fields["key_down"] = "true";
2654 current_keys_pending.key_down = false;
2657 if (current_keys_pending.key_up) {
2658 fields["key_up"] = "true";
2659 current_keys_pending.key_up = false;
2662 if (current_keys_pending.key_enter) {
2663 fields["key_enter"] = "true";
2664 current_keys_pending.key_enter = false;
2667 if (current_keys_pending.key_escape) {
2668 fields["key_escape"] = "true";
2669 current_keys_pending.key_escape = false;
2672 for(unsigned int i=0; i<m_fields.size(); i++) {
2673 const FieldSpec &s = m_fields[i];
2675 std::string name = s.fname;
2676 if (s.ftype == f_Button) {
2677 fields[name] = wide_to_utf8(s.flabel);
2678 } else if (s.ftype == f_Table) {
2679 GUITable *table = getTable(s.fname);
2681 fields[name] = table->checkEvent();
2684 else if(s.ftype == f_DropDown) {
2685 // no dynamic cast possible due to some distributions shipped
2686 // without rtti support in irrlicht
2687 IGUIElement * element = getElementFromId(s.fid);
2688 gui::IGUIComboBox *e = NULL;
2689 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2690 e = static_cast<gui::IGUIComboBox*>(element);
2692 s32 selected = e->getSelected();
2693 if (selected >= 0) {
2695 wide_to_utf8(e->getItem(selected));
2698 else if (s.ftype == f_TabHeader) {
2699 // no dynamic cast possible due to some distributions shipped
2700 // without rtti support in irrlicht
2701 IGUIElement * element = getElementFromId(s.fid);
2702 gui::IGUITabControl *e = NULL;
2703 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2704 e = static_cast<gui::IGUITabControl*>(element);
2708 std::stringstream ss;
2709 ss << (e->getActiveTab() +1);
2710 fields[name] = ss.str();
2713 else if (s.ftype == f_CheckBox) {
2714 // no dynamic cast possible due to some distributions shipped
2715 // without rtti support in irrlicht
2716 IGUIElement * element = getElementFromId(s.fid);
2717 gui::IGUICheckBox *e = NULL;
2718 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2719 e = static_cast<gui::IGUICheckBox*>(element);
2724 fields[name] = "true";
2726 fields[name] = "false";
2729 else if (s.ftype == f_ScrollBar) {
2730 // no dynamic cast possible due to some distributions shipped
2731 // without rtti support in irrlicht
2732 IGUIElement * element = getElementFromId(s.fid);
2733 gui::IGUIScrollBar *e = NULL;
2734 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2735 e = static_cast<gui::IGUIScrollBar*>(element);
2739 std::stringstream os;
2741 if (s.fdefault == L"Changed")
2742 fields[name] = "CHG:" + os.str();
2744 fields[name] = "VAL:" + os.str();
2749 IGUIElement* e = getElementFromId(s.fid);
2751 fields[name] = wide_to_utf8(e->getText());
2757 m_text_dst->gotText(fields);
2761 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2763 while(tocheck != NULL) {
2764 if (tocheck == parent) {
2767 tocheck = tocheck->getParent();
2772 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2774 // The IGUITabControl renders visually using the skin's selected
2775 // font, which we override for the duration of form drawing,
2776 // but computes tab hotspots based on how it would have rendered
2777 // using the font that is selected at the time of button release.
2778 // To make these two consistent, temporarily override the skin's
2779 // font while the IGUITabControl is processing the event.
2780 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2781 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2782 s32 x = event.MouseInput.X;
2783 s32 y = event.MouseInput.Y;
2784 gui::IGUIElement *hovered =
2785 Environment->getRootGUIElement()->getElementFromPoint(
2786 core::position2d<s32>(x, y));
2787 if (hovered && isMyChild(hovered) &&
2788 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2789 gui::IGUISkin* skin = Environment->getSkin();
2790 sanity_check(skin != NULL);
2791 gui::IGUIFont *old_font = skin->getFont();
2792 skin->setFont(m_font);
2793 bool retval = hovered->OnEvent(event);
2794 skin->setFont(old_font);
2799 // Fix Esc/Return key being eaten by checkboxen and tables
2800 if(event.EventType==EET_KEY_INPUT_EVENT) {
2801 KeyPress kp(event.KeyInput);
2802 if (kp == EscapeKey || kp == CancelKey
2803 || kp == getKeySetting("keymap_inventory")
2804 || event.KeyInput.Key==KEY_RETURN) {
2805 gui::IGUIElement *focused = Environment->getFocus();
2806 if (focused && isMyChild(focused) &&
2807 (focused->getType() == gui::EGUIET_LIST_BOX ||
2808 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2814 // Mouse wheel events: send to hovered element instead of focused
2815 if(event.EventType==EET_MOUSE_INPUT_EVENT
2816 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2817 s32 x = event.MouseInput.X;
2818 s32 y = event.MouseInput.Y;
2819 gui::IGUIElement *hovered =
2820 Environment->getRootGUIElement()->getElementFromPoint(
2821 core::position2d<s32>(x, y));
2822 if (hovered && isMyChild(hovered)) {
2823 hovered->OnEvent(event);
2828 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2829 s32 x = event.MouseInput.X;
2830 s32 y = event.MouseInput.Y;
2831 gui::IGUIElement *hovered =
2832 Environment->getRootGUIElement()->getElementFromPoint(
2833 core::position2d<s32>(x, y));
2834 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2835 m_old_tooltip_id = -1;
2838 if (!isChild(hovered,this)) {
2839 if (DoubleClickDetection(event)) {
2846 // display software keyboard when clicking edit boxes
2847 if (event.EventType == EET_MOUSE_INPUT_EVENT
2848 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2849 gui::IGUIElement *hovered =
2850 Environment->getRootGUIElement()->getElementFromPoint(
2851 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2852 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2853 bool retval = hovered->OnEvent(event);
2855 Environment->setFocus(hovered);
2857 m_JavaDialogFieldName = getNameByID(hovered->getID());
2858 std::string message = gettext("Enter ");
2859 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
2863 message += gettext(label) + ":";
2865 /* single line text input */
2868 /* multi line text input */
2869 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2873 /* passwords are always single line */
2874 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2878 porting::showInputDialog(gettext("ok"), "",
2879 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
2885 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2888 memset(&translated, 0, sizeof(SEvent));
2889 translated.EventType = EET_MOUSE_INPUT_EVENT;
2890 gui::IGUIElement* root = Environment->getRootGUIElement();
2894 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2898 gui::IGUIElement* hovered = root->getElementFromPoint(
2899 core::position2d<s32>(
2901 event.TouchInput.Y));
2903 translated.MouseInput.X = event.TouchInput.X;
2904 translated.MouseInput.Y = event.TouchInput.Y;
2905 translated.MouseInput.Control = false;
2907 bool dont_send_event = false;
2909 if (event.TouchInput.touchedCount == 1) {
2910 switch (event.TouchInput.Event) {
2911 case ETIE_PRESSED_DOWN:
2912 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2913 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2914 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2915 m_down_pos = m_pointer;
2918 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2919 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2920 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2923 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2924 translated.MouseInput.ButtonStates = 0;
2925 hovered = root->getElementFromPoint(m_down_pos);
2926 /* we don't have a valid pointer element use last
2927 * known pointer pos */
2928 translated.MouseInput.X = m_pointer.X;
2929 translated.MouseInput.Y = m_pointer.Y;
2931 /* reset down pos */
2932 m_down_pos = v2s32(0,0);
2935 dont_send_event = true;
2936 //this is not supposed to happen
2938 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2939 << event.TouchInput.Event << std::endl;
2941 } else if ( (event.TouchInput.touchedCount == 2) &&
2942 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2943 hovered = root->getElementFromPoint(m_down_pos);
2945 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2946 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2947 translated.MouseInput.X = m_pointer.X;
2948 translated.MouseInput.Y = m_pointer.Y;
2951 hovered->OnEvent(translated);
2954 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
2955 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2959 hovered->OnEvent(translated);
2961 dont_send_event = true;
2963 /* ignore unhandled 2 touch events ... accidental moving for example */
2964 else if (event.TouchInput.touchedCount == 2) {
2965 dont_send_event = true;
2967 else if (event.TouchInput.touchedCount > 2) {
2969 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
2970 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
2973 if (dont_send_event) {
2977 /* check if translated event needs to be preprocessed again */
2978 if (preprocessEvent(translated)) {
2983 bool retval = hovered->OnEvent(translated);
2985 if (event.TouchInput.Event == ETIE_LEFT_UP) {
2987 m_pointer = v2s32(0,0);
2998 /******************************************************************************/
2999 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3001 /* The following code is for capturing double-clicks of the mouse button
3002 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3003 * -- which closes the form -- under some circumstances.
3005 * There have been many github issues reporting this as a bug even though it
3006 * was an intended feature. For this reason, remapping the double-click as
3007 * an ESC must be explicitly set when creating this class via the
3008 * /p remap_dbl_click parameter of the constructor.
3011 if (!m_remap_dbl_click)
3014 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3015 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3016 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3018 m_doubleclickdetect[1].pos = m_pointer;
3019 m_doubleclickdetect[1].time = getTimeMs();
3021 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3022 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
3027 double squaredistance =
3028 m_doubleclickdetect[0].pos
3029 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3031 if (squaredistance > (30*30)) {
3035 SEvent* translated = new SEvent();
3036 assert(translated != 0);
3037 //translate doubleclick to escape
3038 memset(translated, 0, sizeof(SEvent));
3039 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3040 translated->KeyInput.Key = KEY_ESCAPE;
3041 translated->KeyInput.Control = false;
3042 translated->KeyInput.Shift = false;
3043 translated->KeyInput.PressedDown = true;
3044 translated->KeyInput.Char = 0;
3045 OnEvent(*translated);
3047 // no need to send the key up event as we're already deleted
3048 // and no one else did notice this event
3056 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3058 if (event.EventType==EET_KEY_INPUT_EVENT) {
3059 KeyPress kp(event.KeyInput);
3060 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3061 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3064 acceptInput(quit_mode_cancel);
3067 m_text_dst->gotText(L"MenuQuit");
3070 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3071 (kp == getKeySetting("keymap_screenshot"))) {
3072 m_client->makeScreenshot(m_device);
3074 if (event.KeyInput.PressedDown &&
3075 (event.KeyInput.Key==KEY_RETURN ||
3076 event.KeyInput.Key==KEY_UP ||
3077 event.KeyInput.Key==KEY_DOWN)
3079 switch (event.KeyInput.Key) {
3081 current_keys_pending.key_enter = true;
3084 current_keys_pending.key_up = true;
3087 current_keys_pending.key_down = true;
3091 //can't happen at all!
3092 FATAL_ERROR("Reached a source line that can't ever been reached");
3095 if (current_keys_pending.key_enter && m_allowclose) {
3096 acceptInput(quit_mode_accept);
3106 /* Mouse event other than movement, or crossing the border of inventory
3107 field while holding right mouse button
3109 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3110 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3111 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3112 event.MouseInput.isRightPressed() &&
3113 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3115 // Get selected item and hovered/clicked item (s)
3117 m_old_tooltip_id = -1;
3118 updateSelectedItem();
3119 ItemSpec s = getItemAtPos(m_pointer);
3121 Inventory *inv_selected = NULL;
3122 Inventory *inv_s = NULL;
3123 InventoryList *list_s = NULL;
3125 if (m_selected_item) {
3126 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3127 sanity_check(inv_selected);
3128 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3135 inv_s = m_invmgr->getInventory(s.inventoryloc);
3138 errorstream << "InventoryMenu: The selected inventory location "
3139 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3141 s.i = -1; // make it invalid again
3145 list_s = inv_s->getList(s.listname);
3146 if (list_s == NULL) {
3147 verbosestream << "InventoryMenu: The selected inventory list \""
3148 << s.listname << "\" does not exist" << std::endl;
3149 s.i = -1; // make it invalid again
3153 if ((u32)s.i >= list_s->getSize()) {
3154 infostream << "InventoryMenu: The selected inventory list \""
3155 << s.listname << "\" is too small (i=" << s.i << ", size="
3156 << list_s->getSize() << ")" << std::endl;
3157 s.i = -1; // make it invalid again
3161 s_count = list_s->getItem(s.i).count;
3164 bool identical = (m_selected_item != NULL) && s.isValid() &&
3165 (inv_selected == inv_s) &&
3166 (m_selected_item->listname == s.listname) &&
3167 (m_selected_item->i == s.i);
3169 // buttons: 0 = left, 1 = right, 2 = middle
3170 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3173 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3174 { button = 0; updown = 0; }
3175 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3176 { button = 1; updown = 0; }
3177 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3178 { button = 2; updown = 0; }
3179 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3180 { button = 0; updown = 1; }
3181 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3182 { button = 1; updown = 1; }
3183 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3184 { button = 2; updown = 1; }
3185 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3188 // Set this number to a positive value to generate a move action
3189 // from m_selected_item to s.
3190 u32 move_amount = 0;
3192 // Set this number to a positive value to generate a move action
3193 // from s to the next inventory ring.
3194 u32 shift_move_amount = 0;
3196 // Set this number to a positive value to generate a drop action
3197 // from m_selected_item.
3198 u32 drop_amount = 0;
3200 // Set this number to a positive value to generate a craft action at s.
3201 u32 craft_amount = 0;
3204 // Some mouse button has been pressed
3206 //infostream<<"Mouse button "<<button<<" pressed at p=("
3207 // <<p.X<<","<<p.Y<<")"<<std::endl;
3209 m_selected_dragging = false;
3211 if (s.isValid() && s.listname == "craftpreview") {
3212 // Craft preview has been clicked: craft
3213 craft_amount = (button == 2 ? 10 : 1);
3214 } else if (m_selected_item == NULL) {
3216 // Non-empty stack has been clicked: select or shift-move it
3217 m_selected_item = new ItemSpec(s);
3220 if (button == 1) // right
3221 count = (s_count + 1) / 2;
3222 else if (button == 2) // middle
3223 count = MYMIN(s_count, 10);
3227 if (!event.MouseInput.Shift) {
3228 // no shift: select item
3229 m_selected_amount = count;
3230 m_selected_dragging = true;
3231 m_rmouse_auto_place = false;
3233 // shift pressed: move item
3235 shift_move_amount = count;
3236 else // count of 1 at left click like after drag & drop
3237 shift_move_amount = 1;
3240 } else { // m_selected_item != NULL
3241 assert(m_selected_amount >= 1);
3244 // Clicked a slot: move
3245 if (button == 1) // right
3247 else if (button == 2) // middle
3248 move_amount = MYMIN(m_selected_amount, 10);
3250 move_amount = m_selected_amount;
3253 if (move_amount >= m_selected_amount)
3254 m_selected_amount = 0;
3256 m_selected_amount -= move_amount;
3260 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3261 // Clicked outside of the window: drop
3262 if (button == 1) // right
3264 else if (button == 2) // middle
3265 drop_amount = MYMIN(m_selected_amount, 10);
3267 drop_amount = m_selected_amount;
3271 else if (updown == 1) {
3272 // Some mouse button has been released
3274 //infostream<<"Mouse button "<<button<<" released at p=("
3275 // <<p.X<<","<<p.Y<<")"<<std::endl;
3277 if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3279 // Dragged to different slot: move all selected
3280 move_amount = m_selected_amount;
3282 } else if (m_selected_item != NULL && m_selected_dragging &&
3283 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3284 // Dragged outside of window: drop all selected
3285 drop_amount = m_selected_amount;
3288 m_selected_dragging = false;
3289 // Keep count of how many times right mouse button has been
3290 // clicked. One click is drag without dropping. Click + release
3291 // + click changes to drop one item when moved mode
3292 if (button == 1 && m_selected_item != NULL)
3293 m_rmouse_auto_place = !m_rmouse_auto_place;
3294 } else if (updown == -1) {
3295 // Mouse has been moved and rmb is down and mouse pointer just
3296 // entered a new inventory field (checked in the entry-if, this
3297 // is the only action here that is generated by mouse movement)
3298 if (m_selected_item != NULL && s.isValid()) {
3300 // TODO: middle mouse to move 10 items might be handy
3301 if (m_rmouse_auto_place) {
3302 // Only move an item if the destination slot is empty
3303 // or contains the same item type as what is going to be
3305 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3306 InventoryList *list_to = list_s;
3307 assert(list_from && list_to);
3308 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3309 ItemStack stack_to = list_to->getItem(s.i);
3310 if (stack_to.empty() || stack_to.name == stack_from.name)
3316 // Possibly send inventory action to server
3317 if (move_amount > 0) {
3318 // Send IACTION_MOVE
3320 assert(m_selected_item && m_selected_item->isValid());
3321 assert(s.isValid());
3323 assert(inv_selected && inv_s);
3324 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3325 InventoryList *list_to = list_s;
3326 assert(list_from && list_to);
3327 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3328 ItemStack stack_to = list_to->getItem(s.i);
3330 // Check how many items can be moved
3331 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3332 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3333 // If source stack cannot be added to destination stack at all,
3335 if ((leftover.count == stack_from.count) &&
3336 (leftover.name == stack_from.name)) {
3337 m_selected_amount = stack_to.count;
3338 // In case the server doesn't directly swap them but instead
3339 // moves stack_to somewhere else, set this
3340 m_selected_content_guess = stack_to;
3341 m_selected_content_guess_inventory = s.inventoryloc;
3343 // Source stack goes fully into destination stack
3344 else if (leftover.empty()) {
3345 m_selected_amount -= move_amount;
3346 m_selected_content_guess = ItemStack(); // Clear
3348 // Source stack goes partly into destination stack
3350 move_amount -= leftover.count;
3351 m_selected_amount -= move_amount;
3352 m_selected_content_guess = ItemStack(); // Clear
3355 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3356 IMoveAction *a = new IMoveAction();
3357 a->count = move_amount;
3358 a->from_inv = m_selected_item->inventoryloc;
3359 a->from_list = m_selected_item->listname;
3360 a->from_i = m_selected_item->i;
3361 a->to_inv = s.inventoryloc;
3362 a->to_list = s.listname;
3364 m_invmgr->inventoryAction(a);
3365 } else if (shift_move_amount > 0) {
3366 u32 mis = m_inventory_rings.size();
3368 for (; i < mis; i++) {
3369 const ListRingSpec &sp = m_inventory_rings[i];
3370 if (sp.inventoryloc == s.inventoryloc
3371 && sp.listname == s.listname)
3375 if (i >= mis) // if not found
3377 u32 to_inv_ind = (i + 1) % mis;
3378 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3379 InventoryList *list_from = list_s;
3382 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3385 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3388 ItemStack stack_from = list_from->getItem(s.i);
3389 assert(shift_move_amount <= stack_from.count);
3390 if (m_client->getProtoVersion() >= 25) {
3391 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3392 IMoveAction *a = new IMoveAction();
3393 a->count = shift_move_amount;
3394 a->from_inv = s.inventoryloc;
3395 a->from_list = s.listname;
3397 a->to_inv = to_inv_sp.inventoryloc;
3398 a->to_list = to_inv_sp.listname;
3399 a->move_somewhere = true;
3400 m_invmgr->inventoryAction(a);
3402 // find a place (or more than one) to add the new item
3403 u32 ilt_size = list_to->getSize();
3405 for (u32 slot_to = 0; slot_to < ilt_size
3406 && shift_move_amount > 0; slot_to++) {
3407 list_to->itemFits(slot_to, stack_from, &leftover);
3408 if (leftover.count < stack_from.count) {
3409 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3410 IMoveAction *a = new IMoveAction();
3411 a->count = MYMIN(shift_move_amount,
3412 (u32) (stack_from.count - leftover.count));
3413 shift_move_amount -= a->count;
3414 a->from_inv = s.inventoryloc;
3415 a->from_list = s.listname;
3417 a->to_inv = to_inv_sp.inventoryloc;
3418 a->to_list = to_inv_sp.listname;
3420 m_invmgr->inventoryAction(a);
3421 stack_from = leftover;
3426 } else if (drop_amount > 0) {
3427 m_selected_content_guess = ItemStack(); // Clear
3429 // Send IACTION_DROP
3431 assert(m_selected_item && m_selected_item->isValid());
3432 assert(inv_selected);
3433 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3435 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3437 // Check how many items can be dropped
3438 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3439 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3440 m_selected_amount -= drop_amount;
3442 infostream << "Handing IACTION_DROP to manager" << std::endl;
3443 IDropAction *a = new IDropAction();
3444 a->count = drop_amount;
3445 a->from_inv = m_selected_item->inventoryloc;
3446 a->from_list = m_selected_item->listname;
3447 a->from_i = m_selected_item->i;
3448 m_invmgr->inventoryAction(a);
3449 } else if (craft_amount > 0) {
3450 m_selected_content_guess = ItemStack(); // Clear
3452 // Send IACTION_CRAFT
3454 assert(s.isValid());
3457 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3458 ICraftAction *a = new ICraftAction();
3459 a->count = craft_amount;
3460 a->craft_inv = s.inventoryloc;
3461 m_invmgr->inventoryAction(a);
3464 // If m_selected_amount has been decreased to zero, deselect
3465 if (m_selected_amount == 0) {
3466 delete m_selected_item;
3467 m_selected_item = NULL;
3468 m_selected_amount = 0;
3469 m_selected_dragging = false;
3470 m_selected_content_guess = ItemStack();
3472 m_old_pointer = m_pointer;
3474 if (event.EventType == EET_GUI_EVENT) {
3476 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3478 // find the element that was clicked
3479 for (unsigned int i=0; i<m_fields.size(); i++) {
3480 FieldSpec &s = m_fields[i];
3481 if ((s.ftype == f_TabHeader) &&
3482 (s.fid == event.GUIEvent.Caller->getID())) {
3490 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3492 if (!canTakeFocus(event.GUIEvent.Element)) {
3493 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3495 // Returning true disables focus change
3499 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3500 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3501 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3502 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3503 unsigned int btn_id = event.GUIEvent.Caller->getID();
3505 if (btn_id == 257) {
3507 acceptInput(quit_mode_accept);
3511 m_text_dst->gotText(L"ExitButton");
3513 // quitMenu deallocates menu
3517 // find the element that was clicked
3518 for (u32 i = 0; i < m_fields.size(); i++) {
3519 FieldSpec &s = m_fields[i];
3520 // if its a button, set the send field so
3521 // lua knows which button was pressed
3522 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3523 (s.fid == event.GUIEvent.Caller->getID())) {
3527 acceptInput(quit_mode_accept);
3530 m_text_dst->gotText(L"ExitButton");
3534 acceptInput(quit_mode_no);
3538 } else if ((s.ftype == f_DropDown) &&
3539 (s.fid == event.GUIEvent.Caller->getID())) {
3540 // only send the changed dropdown
3541 for (u32 i = 0; i < m_fields.size(); i++) {
3542 FieldSpec &s2 = m_fields[i];
3543 if (s2.ftype == f_DropDown) {
3548 acceptInput(quit_mode_no);
3550 // revert configuration to make sure dropdowns are sent on
3551 // regular button click
3552 for (u32 i = 0; i < m_fields.size(); i++) {
3553 FieldSpec &s2 = m_fields[i];
3554 if (s2.ftype == f_DropDown) {
3559 } else if ((s.ftype == f_ScrollBar) &&
3560 (s.fid == event.GUIEvent.Caller->getID())) {
3561 s.fdefault = L"Changed";
3562 acceptInput(quit_mode_no);
3568 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3569 if (event.GUIEvent.Caller->getID() > 257) {
3572 acceptInput(quit_mode_accept);
3575 current_keys_pending.key_enter = true;
3578 // quitMenu deallocates menu
3583 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3584 int current_id = event.GUIEvent.Caller->getID();
3585 if (current_id > 257) {
3586 // find the element that was clicked
3587 for (u32 i = 0; i < m_fields.size(); i++) {
3588 FieldSpec &s = m_fields[i];
3589 // if it's a table, set the send field
3590 // so lua knows which table was changed
3591 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3602 return Parent ? Parent->OnEvent(event) : false;
3606 * get name of element by element id
3607 * @param id of element
3608 * @return name string or empty string
3610 std::string GUIFormSpecMenu::getNameByID(s32 id)
3612 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3613 iter != m_fields.end(); ++iter) {
3614 if (iter->fid == id) {
3622 * get label of element by id
3623 * @param id of element
3624 * @return label string or empty string
3626 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3628 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3629 iter != m_fields.end(); ++iter) {
3630 if (iter->fid == id) {
3631 return iter->flabel;