3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guiFormSpecMenu.h"
28 #include "constants.h"
31 #include "util/strfnd.h"
32 #include <IGUICheckBox.h>
33 #include <IGUIEditBox.h>
34 #include <IGUIButton.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include <IGUIComboBox.h>
40 #include "client/tile.h" // ITextureSource
41 #include "hud.h" // drawItemStack
45 #include "scripting_game.h"
49 #include "fontengine.h"
51 #include "util/numeric.h"
52 #include "util/string.h" // for parseColorString()
53 #include "irrlicht_changes/static_text.h"
54 #include "guiscalingfilter.h"
56 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
57 #include "intlGUIEditBox.h"
60 #define MY_CHECKPOS(a,b) \
61 if (v_pos.size() != 2) { \
62 errorstream<< "Invalid pos for element " << a << "specified: \"" \
63 << parts[b] << "\"" << std::endl; \
67 #define MY_CHECKGEOM(a,b) \
68 if (v_geom.size() != 2) { \
69 errorstream<< "Invalid pos for element " << a << "specified: \"" \
70 << parts[b] << "\"" << std::endl; \
76 static unsigned int font_line_height(gui::IGUIFont *font)
78 return font->getDimension(L"Ay").Height + font->getKerningHeight();
81 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
82 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
83 InventoryManager *invmgr, IGameDef *gamedef,
84 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
85 Client* client, bool remap_dbl_click) :
86 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
92 m_selected_item(NULL),
94 m_selected_dragging(false),
95 m_tooltip_element(NULL),
98 m_rmouse_auto_place(false),
103 m_formspec_version(0),
104 m_focused_element(""),
106 m_remap_dbl_click(remap_dbl_click)
108 , m_JavaDialogFieldName("")
111 current_keys_pending.key_down = false;
112 current_keys_pending.key_up = false;
113 current_keys_pending.key_enter = false;
114 current_keys_pending.key_escape = false;
116 m_doubleclickdetect[0].time = 0;
117 m_doubleclickdetect[1].time = 0;
119 m_doubleclickdetect[0].pos = v2s32(0, 0);
120 m_doubleclickdetect[1].pos = v2s32(0, 0);
122 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
125 GUIFormSpecMenu::~GUIFormSpecMenu()
129 for (u32 i = 0; i < m_tables.size(); ++i) {
130 GUITable *table = m_tables[i].second;
134 delete m_selected_item;
136 if (m_form_src != NULL) {
139 if (m_text_dst != NULL) {
144 void GUIFormSpecMenu::removeChildren()
146 const core::list<gui::IGUIElement*> &children = getChildren();
148 while(!children.empty()) {
149 (*children.getLast())->remove();
152 if(m_tooltip_element) {
153 m_tooltip_element->remove();
154 m_tooltip_element->drop();
155 m_tooltip_element = NULL;
160 void GUIFormSpecMenu::setInitialFocus()
162 // Set initial focus according to following order of precedence:
163 // 1. first empty editbox
167 // 5. first focusable (not statictext, not tabheader)
168 // 6. first child element
170 core::list<gui::IGUIElement*> children = getChildren();
172 // in case "children" contains any NULL elements, remove them
173 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
174 it != children.end();) {
178 it = children.erase(it);
181 // 1. first empty editbox
182 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
183 it != children.end(); ++it) {
184 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
185 && (*it)->getText()[0] == 0) {
186 Environment->setFocus(*it);
192 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
193 it != children.end(); ++it) {
194 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
195 Environment->setFocus(*it);
201 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
202 it != children.end(); ++it) {
203 if ((*it)->getTypeName() == std::string("GUITable")) {
204 Environment->setFocus(*it);
210 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
211 it != children.end(); --it) {
212 if ((*it)->getType() == gui::EGUIET_BUTTON) {
213 Environment->setFocus(*it);
218 // 5. first focusable (not statictext, not tabheader)
219 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
220 it != children.end(); ++it) {
221 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
222 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
223 Environment->setFocus(*it);
228 // 6. first child element
229 if (children.empty())
230 Environment->setFocus(this);
232 Environment->setFocus(*(children.begin()));
235 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
237 for (u32 i = 0; i < m_tables.size(); ++i) {
238 if (tablename == m_tables[i].first.fname)
239 return m_tables[i].second;
244 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
246 for (u32 i = 0; i < m_dropdowns.size(); ++i) {
247 if (name == m_dropdowns[i].first.fname)
248 return &m_dropdowns[i].second;
253 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
255 std::vector<std::string> parts = split(element,',');
257 if (((parts.size() == 2) || parts.size() == 3) ||
258 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
260 if (parts[1].find(';') != std::string::npos)
261 parts[1] = parts[1].substr(0,parts[1].find(';'));
263 data->invsize.X = MYMAX(0, stof(parts[0]));
264 data->invsize.Y = MYMAX(0, stof(parts[1]));
267 if (parts.size() == 3) {
268 if (parts[2] == "true") {
269 lockSize(true,v2u32(800,600));
273 data->explicit_size = true;
276 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
279 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
281 if (m_gamedef == 0) {
282 warningstream<<"invalid use of 'list' with m_gamedef==0"<<std::endl;
286 std::vector<std::string> parts = split(element,';');
288 if (((parts.size() == 4) || (parts.size() == 5)) ||
289 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
291 std::string location = parts[0];
292 std::string listname = parts[1];
293 std::vector<std::string> v_pos = split(parts[2],',');
294 std::vector<std::string> v_geom = split(parts[3],',');
295 std::string startindex = "";
296 if (parts.size() == 5)
297 startindex = parts[4];
299 MY_CHECKPOS("list",2);
300 MY_CHECKGEOM("list",3);
302 InventoryLocation loc;
304 if(location == "context" || location == "current_name")
305 loc = m_current_inventory_location;
307 loc.deSerialize(location);
309 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
310 pos.X += stof(v_pos[0]) * (float)spacing.X;
311 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
314 geom.X = stoi(v_geom[0]);
315 geom.Y = stoi(v_geom[1]);
319 start_i = stoi(startindex);
321 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
322 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
326 if(!data->explicit_size)
327 warningstream<<"invalid use of list without a size[] element"<<std::endl;
328 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
331 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
334 void GUIFormSpecMenu::parseListRing(parserData* data, std::string element)
336 if (m_gamedef == 0) {
337 errorstream << "WARNING: invalid use of 'listring' with m_gamedef==0" << std::endl;
341 std::vector<std::string> parts = split(element, ';');
343 if (parts.size() == 2) {
344 std::string location = parts[0];
345 std::string listname = parts[1];
347 InventoryLocation loc;
349 if (location == "context" || location == "current_name")
350 loc = m_current_inventory_location;
352 loc.deSerialize(location);
354 m_inventory_rings.push_back(ListRingSpec(loc, listname));
356 } else if ((element == "") && (m_inventorylists.size() > 1)) {
357 size_t siz = m_inventorylists.size();
358 // insert the last two inv list elements into the list ring
359 const ListDrawSpec &spa = m_inventorylists[siz - 2];
360 const ListDrawSpec &spb = m_inventorylists[siz - 1];
361 m_inventory_rings.push_back(ListRingSpec(spa.inventoryloc, spa.listname));
362 m_inventory_rings.push_back(ListRingSpec(spb.inventoryloc, spb.listname));
365 errorstream<< "Invalid list ring element(" << parts.size() << ", "
366 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
369 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
371 std::vector<std::string> parts = split(element,';');
373 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
374 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
376 std::vector<std::string> v_pos = split(parts[0],',');
377 std::string name = parts[1];
378 std::string label = parts[2];
379 std::string selected = "";
381 if (parts.size() >= 4)
384 MY_CHECKPOS("checkbox",0);
387 pos.X += stof(v_pos[0]) * (float) spacing.X;
388 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
390 bool fselected = false;
392 if (selected == "true")
395 std::wstring wlabel = utf8_to_wide(unescape_string(label));
397 core::rect<s32> rect = core::rect<s32>(
398 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
399 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
400 pos.Y + ((imgsize.Y/2) + m_btn_height));
404 wlabel, //Needed for displaying text on MSVC
409 spec.ftype = f_CheckBox;
411 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
412 spec.fid, spec.flabel.c_str());
414 if (spec.fname == data->focused_fieldname) {
415 Environment->setFocus(e);
418 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
419 m_fields.push_back(spec);
422 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
425 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
427 std::vector<std::string> parts = split(element,';');
429 if (parts.size() >= 5) {
430 std::vector<std::string> v_pos = split(parts[0],',');
431 std::vector<std::string> v_dim = split(parts[1],',');
432 std::string name = parts[3];
433 std::string value = parts[4];
435 MY_CHECKPOS("scrollbar",0);
438 pos.X += stof(v_pos[0]) * (float) spacing.X;
439 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
441 if (v_dim.size() != 2) {
442 errorstream<< "Invalid size for element " << "scrollbar"
443 << "specified: \"" << parts[1] << "\"" << std::endl;
448 dim.X = stof(v_dim[0]) * (float) spacing.X;
449 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
451 core::rect<s32> rect =
452 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
461 bool is_horizontal = true;
463 if (parts[2] == "vertical")
464 is_horizontal = false;
466 spec.ftype = f_ScrollBar;
468 gui::IGUIScrollBar* e =
469 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
473 e->setPos(stoi(parts[4]));
475 e->setLargeStep(100);
477 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
478 m_fields.push_back(spec);
481 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
484 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
486 std::vector<std::string> parts = split(element,';');
488 if ((parts.size() == 3) ||
489 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
491 std::vector<std::string> v_pos = split(parts[0],',');
492 std::vector<std::string> v_geom = split(parts[1],',');
493 std::string name = unescape_string(parts[2]);
495 MY_CHECKPOS("image",0);
496 MY_CHECKGEOM("image",1);
498 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
499 pos.X += stof(v_pos[0]) * (float) spacing.X;
500 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
503 geom.X = stof(v_geom[0]) * (float)imgsize.X;
504 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
506 if(!data->explicit_size)
507 warningstream<<"invalid use of image without a size[] element"<<std::endl;
508 m_images.push_back(ImageDrawSpec(name, pos, geom));
512 if (parts.size() == 2) {
513 std::vector<std::string> v_pos = split(parts[0],',');
514 std::string name = unescape_string(parts[1]);
516 MY_CHECKPOS("image",0);
518 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
519 pos.X += stof(v_pos[0]) * (float) spacing.X;
520 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
522 if(!data->explicit_size)
523 warningstream<<"invalid use of image without a size[] element"<<std::endl;
524 m_images.push_back(ImageDrawSpec(name, pos));
527 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
530 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
532 std::vector<std::string> parts = split(element,';');
534 if ((parts.size() == 3) ||
535 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
537 std::vector<std::string> v_pos = split(parts[0],',');
538 std::vector<std::string> v_geom = split(parts[1],',');
539 std::string name = parts[2];
541 MY_CHECKPOS("itemimage",0);
542 MY_CHECKGEOM("itemimage",1);
544 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
545 pos.X += stof(v_pos[0]) * (float) spacing.X;
546 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
549 geom.X = stof(v_geom[0]) * (float)imgsize.X;
550 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
552 if(!data->explicit_size)
553 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
554 m_itemimages.push_back(ImageDrawSpec("", name, pos, geom));
557 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
560 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
563 std::vector<std::string> parts = split(element,';');
565 if ((parts.size() == 4) ||
566 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
568 std::vector<std::string> v_pos = split(parts[0],',');
569 std::vector<std::string> v_geom = split(parts[1],',');
570 std::string name = parts[2];
571 std::string label = parts[3];
573 MY_CHECKPOS("button",0);
574 MY_CHECKGEOM("button",1);
577 pos.X += stof(v_pos[0]) * (float)spacing.X;
578 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
581 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
582 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
584 core::rect<s32> rect =
585 core::rect<s32>(pos.X, pos.Y - m_btn_height,
586 pos.X + geom.X, pos.Y + m_btn_height);
588 if(!data->explicit_size)
589 warningstream<<"invalid use of button without a size[] element"<<std::endl;
591 std::wstring wlabel = utf8_to_wide(unescape_string(label));
599 spec.ftype = f_Button;
600 if(type == "button_exit")
602 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
603 spec.flabel.c_str());
605 if (spec.fname == data->focused_fieldname) {
606 Environment->setFocus(e);
609 m_fields.push_back(spec);
612 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
615 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
617 std::vector<std::string> parts = split(element,';');
619 if (((parts.size() == 3) || (parts.size() == 4)) ||
620 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
622 std::vector<std::string> v_pos = split(parts[0],',');
623 std::vector<std::string> v_geom = split(parts[1],',');
624 std::string name = unescape_string(parts[2]);
626 MY_CHECKPOS("background",0);
627 MY_CHECKGEOM("background",1);
629 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
630 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
631 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
634 geom.X = stof(v_geom[0]) * (float)spacing.X;
635 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
637 if (parts.size() == 4) {
638 m_clipbackground = is_yes(parts[3]);
639 if (m_clipbackground) {
640 pos.X = stoi(v_pos[0]); //acts as offset
641 pos.Y = stoi(v_pos[1]); //acts as offset
645 if(!data->explicit_size)
646 warningstream<<"invalid use of background without a size[] element"<<std::endl;
647 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
650 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
653 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
655 std::vector<std::string> parts = split(element,';');
657 data->table_options.clear();
658 for (size_t i = 0; i < parts.size(); ++i) {
659 // Parse table option
660 std::string opt = unescape_string(parts[i]);
661 data->table_options.push_back(GUITable::splitOption(opt));
665 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
667 std::vector<std::string> parts = split(element,';');
669 data->table_columns.clear();
670 for (size_t i = 0; i < parts.size(); ++i) {
671 std::vector<std::string> col_parts = split(parts[i],',');
672 GUITable::TableColumn column;
674 if (!col_parts.empty())
675 column.type = col_parts[0];
676 // Parse column options
677 for (size_t j = 1; j < col_parts.size(); ++j) {
678 std::string opt = unescape_string(col_parts[j]);
679 column.options.push_back(GUITable::splitOption(opt));
681 data->table_columns.push_back(column);
685 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
687 std::vector<std::string> parts = split(element,';');
689 if (((parts.size() == 4) || (parts.size() == 5)) ||
690 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
692 std::vector<std::string> v_pos = split(parts[0],',');
693 std::vector<std::string> v_geom = split(parts[1],',');
694 std::string name = parts[2];
695 std::vector<std::string> items = split(parts[3],',');
696 std::string str_initial_selection = "";
697 std::string str_transparent = "false";
699 if (parts.size() >= 5)
700 str_initial_selection = parts[4];
702 MY_CHECKPOS("table",0);
703 MY_CHECKGEOM("table",1);
706 pos.X += stof(v_pos[0]) * (float)spacing.X;
707 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
710 geom.X = stof(v_geom[0]) * (float)spacing.X;
711 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
713 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
722 spec.ftype = f_Table;
724 for (unsigned int i = 0; i < items.size(); ++i) {
725 items[i] = unescape_enriched(unescape_string(items[i]));
728 //now really show table
729 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
732 if (spec.fname == data->focused_fieldname) {
733 Environment->setFocus(e);
736 e->setTable(data->table_options, data->table_columns, items);
738 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
739 e->setDynamicData(data->table_dyndata[name]);
742 if ((str_initial_selection != "") &&
743 (str_initial_selection != "0"))
744 e->setSelected(stoi(str_initial_selection.c_str()));
746 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
747 m_fields.push_back(spec);
750 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
753 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
755 std::vector<std::string> parts = split(element,';');
757 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
758 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
760 std::vector<std::string> v_pos = split(parts[0],',');
761 std::vector<std::string> v_geom = split(parts[1],',');
762 std::string name = parts[2];
763 std::vector<std::string> items = split(parts[3],',');
764 std::string str_initial_selection = "";
765 std::string str_transparent = "false";
767 if (parts.size() >= 5)
768 str_initial_selection = parts[4];
770 if (parts.size() >= 6)
771 str_transparent = parts[5];
773 MY_CHECKPOS("textlist",0);
774 MY_CHECKGEOM("textlist",1);
777 pos.X += stof(v_pos[0]) * (float)spacing.X;
778 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
781 geom.X = stof(v_geom[0]) * (float)spacing.X;
782 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
785 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
794 spec.ftype = f_Table;
796 for (unsigned int i = 0; i < items.size(); ++i) {
797 items[i] = unescape_enriched(unescape_string(items[i]));
800 //now really show list
801 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
804 if (spec.fname == data->focused_fieldname) {
805 Environment->setFocus(e);
808 e->setTextList(items, is_yes(str_transparent));
810 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
811 e->setDynamicData(data->table_dyndata[name]);
814 if ((str_initial_selection != "") &&
815 (str_initial_selection != "0"))
816 e->setSelected(stoi(str_initial_selection.c_str()));
818 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
819 m_fields.push_back(spec);
822 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
826 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
828 std::vector<std::string> parts = split(element,';');
830 if ((parts.size() == 5) ||
831 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
833 std::vector<std::string> v_pos = split(parts[0],',');
834 std::string name = parts[2];
835 std::vector<std::string> items = split(parts[3],',');
836 std::string str_initial_selection = "";
837 str_initial_selection = parts[4];
839 MY_CHECKPOS("dropdown",0);
842 pos.X += stof(v_pos[0]) * (float)spacing.X;
843 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
845 s32 width = stof(parts[1]) * (float)spacing.Y;
847 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
848 pos.X + width, pos.Y + (m_btn_height * 2));
857 spec.ftype = f_DropDown;
860 //now really show list
861 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
863 if (spec.fname == data->focused_fieldname) {
864 Environment->setFocus(e);
867 for (unsigned int i=0; i < items.size(); i++) {
868 e->addItem(unescape_enriched(unescape_string(
869 utf8_to_wide(items[i]))).c_str());
872 if (str_initial_selection != "")
873 e->setSelected(stoi(str_initial_selection.c_str())-1);
875 m_fields.push_back(spec);
877 m_dropdowns.push_back(std::pair<FieldSpec,
878 std::vector<std::string> >(spec, std::vector<std::string>()));
879 std::vector<std::string> &values = m_dropdowns.back().second;
880 for (unsigned int i = 0; i < items.size(); i++) {
881 values.push_back(unescape_string(items[i]));
886 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
887 << element << "'" << std::endl;
890 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
892 std::vector<std::string> parts = split(element,';');
894 if ((parts.size() == 4) ||
895 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
897 std::vector<std::string> v_pos = split(parts[0],',');
898 std::vector<std::string> v_geom = split(parts[1],',');
899 std::string name = parts[2];
900 std::string label = parts[3];
902 MY_CHECKPOS("pwdfield",0);
903 MY_CHECKGEOM("pwdfield",1);
906 pos.X += stof(v_pos[0]) * (float)spacing.X;
907 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
910 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
912 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
913 pos.Y -= m_btn_height;
914 geom.Y = m_btn_height*2;
916 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
918 std::wstring wlabel = utf8_to_wide(unescape_string(label));
928 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
930 if (spec.fname == data->focused_fieldname) {
931 Environment->setFocus(e);
934 if (label.length() >= 1)
936 int font_height = g_fontengine->getTextHeight();
937 rect.UpperLeftCorner.Y -= font_height;
938 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
939 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
942 e->setPasswordBox(true,L'*');
945 evt.EventType = EET_KEY_INPUT_EVENT;
946 evt.KeyInput.Key = KEY_END;
947 evt.KeyInput.Char = 0;
948 evt.KeyInput.Control = 0;
949 evt.KeyInput.Shift = 0;
950 evt.KeyInput.PressedDown = true;
952 m_fields.push_back(spec);
955 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
958 void GUIFormSpecMenu::parseSimpleField(parserData* data,
959 std::vector<std::string> &parts)
961 std::string name = parts[0];
962 std::string label = parts[1];
963 std::string default_val = parts[2];
965 core::rect<s32> rect;
967 if(data->explicit_size)
968 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
970 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
971 pos.Y = ((m_fields.size()+2)*60);
972 v2s32 size = DesiredRect.getSize();
974 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
975 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
979 default_val = m_form_src->resolveText(default_val);
982 std::wstring wlabel = utf8_to_wide(unescape_string(label));
987 utf8_to_wide(unescape_string(default_val)),
993 // spec field id to 0, this stops submit searching for a value that isn't there
994 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1000 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1001 if (g_settings->getBool("freetype")) {
1002 e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1003 true, Environment, this, spec.fid, rect);
1009 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1011 if (spec.fname == data->focused_fieldname) {
1012 Environment->setFocus(e);
1016 evt.EventType = EET_KEY_INPUT_EVENT;
1017 evt.KeyInput.Key = KEY_END;
1018 evt.KeyInput.Char = 0;
1019 evt.KeyInput.Control = 0;
1020 evt.KeyInput.Shift = 0;
1021 evt.KeyInput.PressedDown = true;
1024 if (label.length() >= 1)
1026 int font_height = g_fontengine->getTextHeight();
1027 rect.UpperLeftCorner.Y -= font_height;
1028 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1029 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1033 m_fields.push_back(spec);
1036 void GUIFormSpecMenu::parseTextArea(parserData* data,
1037 std::vector<std::string>& parts,std::string type)
1040 std::vector<std::string> v_pos = split(parts[0],',');
1041 std::vector<std::string> v_geom = split(parts[1],',');
1042 std::string name = parts[2];
1043 std::string label = parts[3];
1044 std::string default_val = parts[4];
1046 MY_CHECKPOS(type,0);
1047 MY_CHECKGEOM(type,1);
1050 pos.X = stof(v_pos[0]) * (float) spacing.X;
1051 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1055 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1057 if (type == "textarea")
1059 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1060 pos.Y += m_btn_height;
1064 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1065 pos.Y -= m_btn_height;
1066 geom.Y = m_btn_height*2;
1069 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1071 if(!data->explicit_size)
1072 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1075 default_val = m_form_src->resolveText(default_val);
1078 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1083 utf8_to_wide(unescape_string(default_val)),
1089 // spec field id to 0, this stops submit searching for a value that isn't there
1090 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1096 gui::IGUIEditBox *e;
1097 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1098 if (g_settings->getBool("freetype")) {
1099 e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1100 true, Environment, this, spec.fid, rect);
1106 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1109 if (spec.fname == data->focused_fieldname) {
1110 Environment->setFocus(e);
1113 if (type == "textarea")
1115 e->setMultiLine(true);
1116 e->setWordWrap(true);
1117 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1120 evt.EventType = EET_KEY_INPUT_EVENT;
1121 evt.KeyInput.Key = KEY_END;
1122 evt.KeyInput.Char = 0;
1123 evt.KeyInput.Control = 0;
1124 evt.KeyInput.Shift = 0;
1125 evt.KeyInput.PressedDown = true;
1129 if (label.length() >= 1)
1131 int font_height = g_fontengine->getTextHeight();
1132 rect.UpperLeftCorner.Y -= font_height;
1133 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1134 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1137 m_fields.push_back(spec);
1140 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1143 std::vector<std::string> parts = split(element,';');
1145 if (parts.size() == 3 || parts.size() == 4) {
1146 parseSimpleField(data,parts);
1150 if ((parts.size() == 5) ||
1151 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1153 parseTextArea(data,parts,type);
1156 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1159 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1161 std::vector<std::string> parts = split(element,';');
1163 if ((parts.size() == 2) ||
1164 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1166 std::vector<std::string> v_pos = split(parts[0],',');
1167 std::string text = parts[1];
1169 MY_CHECKPOS("label",0);
1171 v2s32 pos = padding;
1172 pos.X += stof(v_pos[0]) * (float)spacing.X;
1173 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1175 if(!data->explicit_size)
1176 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1178 std::vector<std::string> lines = split(text, '\n');
1180 for (unsigned int i = 0; i != lines.size(); i++) {
1181 // Lines are spaced at the nominal distance of
1182 // 2/5 inventory slot, even if the font doesn't
1183 // quite match that. This provides consistent
1184 // form layout, at the expense of sometimes
1185 // having sub-optimal spacing for the font.
1186 // We multiply by 2 and then divide by 5, rather
1187 // than multiply by 0.4, to get exact results
1188 // in the integer cases: 0.4 is not exactly
1189 // representable in binary floating point.
1190 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1191 std::wstring wlabel = utf8_to_wide(unescape_string(lines[i]));
1192 core::rect<s32> rect = core::rect<s32>(
1193 pos.X, posy - m_btn_height,
1194 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1195 posy + m_btn_height);
1202 gui::IGUIStaticText *e =
1203 addStaticText(Environment, spec.flabel.c_str(),
1204 rect, false, false, this, spec.fid);
1205 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1207 m_fields.push_back(spec);
1212 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1215 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1217 std::vector<std::string> parts = split(element,';');
1219 if ((parts.size() == 2) ||
1220 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1222 std::vector<std::string> v_pos = split(parts[0],',');
1223 std::wstring text = unescape_enriched(
1224 unescape_string(utf8_to_wide(parts[1])));
1226 MY_CHECKPOS("vertlabel",1);
1228 v2s32 pos = padding;
1229 pos.X += stof(v_pos[0]) * (float)spacing.X;
1230 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1232 core::rect<s32> rect = core::rect<s32>(
1233 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1235 font_line_height(m_font)
1237 +((imgsize.Y/2)- m_btn_height));
1238 //actually text.length() would be correct but adding +1 avoids to break all mods
1240 if(!data->explicit_size)
1241 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1243 std::wstring label = L"";
1245 for (unsigned int i=0; i < text.length(); i++) {
1256 gui::IGUIStaticText *t =
1257 addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid);
1258 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1259 m_fields.push_back(spec);
1262 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1265 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1268 std::vector<std::string> parts = split(element,';');
1270 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1271 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1273 std::vector<std::string> v_pos = split(parts[0],',');
1274 std::vector<std::string> v_geom = split(parts[1],',');
1275 std::string image_name = parts[2];
1276 std::string name = parts[3];
1277 std::string label = parts[4];
1279 MY_CHECKPOS("imagebutton",0);
1280 MY_CHECKGEOM("imagebutton",1);
1282 v2s32 pos = padding;
1283 pos.X += stof(v_pos[0]) * (float)spacing.X;
1284 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1286 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1287 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1289 bool noclip = false;
1290 bool drawborder = true;
1291 std::string pressed_image_name = "";
1293 if (parts.size() >= 7) {
1294 if (parts[5] == "true")
1296 if (parts[6] == "false")
1300 if (parts.size() >= 8) {
1301 pressed_image_name = parts[7];
1304 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1306 if(!data->explicit_size)
1307 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1309 image_name = unescape_string(image_name);
1310 pressed_image_name = unescape_string(pressed_image_name);
1312 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1317 utf8_to_wide(image_name),
1320 spec.ftype = f_Button;
1321 if(type == "image_button_exit")
1322 spec.is_exit = true;
1324 video::ITexture *texture = 0;
1325 video::ITexture *pressed_texture = 0;
1326 texture = m_tsrc->getTexture(image_name);
1327 if (pressed_image_name != "")
1328 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1330 pressed_texture = texture;
1332 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1334 if (spec.fname == data->focused_fieldname) {
1335 Environment->setFocus(e);
1338 e->setUseAlphaChannel(true);
1339 e->setImage(guiScalingImageButton(
1340 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1341 e->setPressedImage(guiScalingImageButton(
1342 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1343 e->setScaleImage(true);
1344 e->setNotClipped(noclip);
1345 e->setDrawBorder(drawborder);
1347 m_fields.push_back(spec);
1351 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1354 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1356 std::vector<std::string> parts = split(element,';');
1358 if (((parts.size() == 4) || (parts.size() == 6)) ||
1359 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1361 std::vector<std::string> v_pos = split(parts[0],',');
1362 std::string name = parts[1];
1363 std::vector<std::string> buttons = split(parts[2],',');
1364 std::string str_index = parts[3];
1365 bool show_background = true;
1366 bool show_border = true;
1367 int tab_index = stoi(str_index) -1;
1369 MY_CHECKPOS("tabheader",0);
1371 if (parts.size() == 6) {
1372 if (parts[4] == "true")
1373 show_background = false;
1374 if (parts[5] == "false")
1375 show_border = false;
1385 spec.ftype = f_TabHeader;
1388 pos.X += stof(v_pos[0]) * (float)spacing.X;
1389 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1391 geom.X = DesiredRect.getWidth();
1392 geom.Y = m_btn_height*2;
1394 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1397 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1398 show_background, show_border, spec.fid);
1399 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1400 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1401 e->setTabHeight(m_btn_height*2);
1403 if (spec.fname == data->focused_fieldname) {
1404 Environment->setFocus(e);
1407 e->setNotClipped(true);
1409 for (unsigned int i = 0; i < buttons.size(); i++) {
1410 e->addTab(unescape_enriched(unescape_string(
1411 utf8_to_wide(buttons[i]))).c_str(), -1);
1414 if ((tab_index >= 0) &&
1415 (buttons.size() < INT_MAX) &&
1416 (tab_index < (int) buttons.size()))
1417 e->setActiveTab(tab_index);
1419 m_fields.push_back(spec);
1422 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1423 << element << "'" << std::endl;
1426 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1429 if (m_gamedef == 0) {
1430 warningstream << "invalid use of item_image_button with m_gamedef==0"
1435 std::vector<std::string> parts = split(element,';');
1437 if ((parts.size() == 5) ||
1438 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1440 std::vector<std::string> v_pos = split(parts[0],',');
1441 std::vector<std::string> v_geom = split(parts[1],',');
1442 std::string item_name = parts[2];
1443 std::string name = parts[3];
1444 std::string label = parts[4];
1446 label = unescape_string(label);
1447 item_name = unescape_string(item_name);
1449 MY_CHECKPOS("itemimagebutton",0);
1450 MY_CHECKGEOM("itemimagebutton",1);
1452 v2s32 pos = padding;
1453 pos.X += stof(v_pos[0]) * (float)spacing.X;
1454 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1456 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1457 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1459 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1461 if(!data->explicit_size)
1462 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1464 IItemDefManager *idef = m_gamedef->idef();
1466 item.deSerialize(item_name, idef);
1469 TooltipSpec(item.getDefinition(idef).description,
1470 m_default_tooltip_bgcolor,
1471 m_default_tooltip_color);
1475 utf8_to_wide(label),
1476 utf8_to_wide(item_name),
1477 258 + m_fields.size()
1480 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1482 if (spec.fname == data->focused_fieldname) {
1483 Environment->setFocus(e);
1486 spec.ftype = f_Button;
1487 rect+=data->basepos-padding;
1489 m_fields.push_back(spec);
1490 pos = padding + AbsoluteRect.UpperLeftCorner;
1491 pos.X += stof(v_pos[0]) * (float) spacing.X;
1492 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1493 m_itemimages.push_back(ImageDrawSpec("", item_name, e, pos, geom));
1494 m_static_texts.push_back(StaticTextSpec(utf8_to_wide(label), rect, e));
1497 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1500 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1502 std::vector<std::string> parts = split(element,';');
1504 if ((parts.size() == 3) ||
1505 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1507 std::vector<std::string> v_pos = split(parts[0],',');
1508 std::vector<std::string> v_geom = split(parts[1],',');
1510 MY_CHECKPOS("box",0);
1511 MY_CHECKGEOM("box",1);
1513 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1514 pos.X += stof(v_pos[0]) * (float) spacing.X;
1515 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1518 geom.X = stof(v_geom[0]) * (float)spacing.X;
1519 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1521 video::SColor tmp_color;
1523 if (parseColorString(parts[2], tmp_color, false)) {
1524 BoxDrawSpec spec(pos, geom, tmp_color);
1526 m_boxes.push_back(spec);
1529 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1533 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1536 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1538 std::vector<std::string> parts = split(element,';');
1540 if (((parts.size() == 1) || (parts.size() == 2)) ||
1541 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1543 parseColorString(parts[0],m_bgcolor,false);
1545 if (parts.size() == 2) {
1546 std::string fullscreen = parts[1];
1547 m_bgfullscreen = is_yes(fullscreen);
1551 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1554 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1556 std::vector<std::string> parts = split(element,';');
1558 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1559 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1561 parseColorString(parts[0], m_slotbg_n, false);
1562 parseColorString(parts[1], m_slotbg_h, false);
1564 if (parts.size() >= 3) {
1565 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1566 m_slotborder = true;
1569 if (parts.size() == 5) {
1570 video::SColor tmp_color;
1572 if (parseColorString(parts[3], tmp_color, false))
1573 m_default_tooltip_bgcolor = tmp_color;
1574 if (parseColorString(parts[4], tmp_color, false))
1575 m_default_tooltip_color = tmp_color;
1579 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1582 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1584 std::vector<std::string> parts = split(element,';');
1585 if (parts.size() == 2) {
1586 std::string name = parts[0];
1587 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1588 m_default_tooltip_bgcolor, m_default_tooltip_color);
1590 } else if (parts.size() == 4) {
1591 std::string name = parts[0];
1592 video::SColor tmp_color1, tmp_color2;
1593 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1594 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1595 tmp_color1, tmp_color2);
1599 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1602 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1608 std::vector<std::string> parts = split(data,'[');
1610 if (parts.size() < 2) {
1614 if (parts[0] != "formspec_version") {
1618 if (is_number(parts[1])) {
1619 m_formspec_version = mystoi(parts[1]);
1626 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1631 std::vector<std::string> parts = split(element,'[');
1633 if (parts.size() < 2)
1636 std::string type = trim(parts[0]);
1637 std::string description = trim(parts[1]);
1639 if (type != "size" && type != "invsize")
1642 if (type == "invsize")
1643 log_deprecated("Deprecated formspec element \"invsize\" is used");
1645 parseSize(data, description);
1650 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1656 std::vector<std::string> parts = split(element,'[');
1658 // ugly workaround to keep compatibility
1659 if (parts.size() > 2) {
1660 if (trim(parts[0]) == "image") {
1661 for (unsigned int i=2;i< parts.size(); i++) {
1662 parts[1] += "[" + parts[i];
1668 if (parts.size() < 2) {
1672 std::string type = trim(parts[0]);
1673 std::string description = trim(parts[1]);
1675 if (type == "list") {
1676 parseList(data,description);
1680 if (type == "listring") {
1681 parseListRing(data, description);
1685 if (type == "checkbox") {
1686 parseCheckbox(data,description);
1690 if (type == "image") {
1691 parseImage(data,description);
1695 if (type == "item_image") {
1696 parseItemImage(data,description);
1700 if ((type == "button") || (type == "button_exit")) {
1701 parseButton(data,description,type);
1705 if (type == "background") {
1706 parseBackground(data,description);
1710 if (type == "tableoptions"){
1711 parseTableOptions(data,description);
1715 if (type == "tablecolumns"){
1716 parseTableColumns(data,description);
1720 if (type == "table"){
1721 parseTable(data,description);
1725 if (type == "textlist"){
1726 parseTextList(data,description);
1730 if (type == "dropdown"){
1731 parseDropDown(data,description);
1735 if (type == "pwdfield") {
1736 parsePwdField(data,description);
1740 if ((type == "field") || (type == "textarea")){
1741 parseField(data,description,type);
1745 if (type == "label") {
1746 parseLabel(data,description);
1750 if (type == "vertlabel") {
1751 parseVertLabel(data,description);
1755 if (type == "item_image_button") {
1756 parseItemImageButton(data,description);
1760 if ((type == "image_button") || (type == "image_button_exit")) {
1761 parseImageButton(data,description,type);
1765 if (type == "tabheader") {
1766 parseTabHeader(data,description);
1770 if (type == "box") {
1771 parseBox(data,description);
1775 if (type == "bgcolor") {
1776 parseBackgroundColor(data,description);
1780 if (type == "listcolors") {
1781 parseListColors(data,description);
1785 if (type == "tooltip") {
1786 parseTooltip(data,description);
1790 if (type == "scrollbar") {
1791 parseScrollBar(data, description);
1797 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1801 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1803 /* useless to regenerate without a screensize */
1804 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1811 for (u32 i = 0; i < m_tables.size(); ++i) {
1812 std::string tablename = m_tables[i].first.fname;
1813 GUITable *table = m_tables[i].second;
1814 mydata.table_dyndata[tablename] = table->getDynamicData();
1818 if (!m_focused_element.empty())
1819 mydata.focused_fieldname = m_focused_element;
1822 gui::IGUIElement *focused_element = Environment->getFocus();
1823 if (focused_element && focused_element->getParent() == this) {
1824 s32 focused_id = focused_element->getID();
1825 if (focused_id > 257) {
1826 for (u32 i=0; i<m_fields.size(); i++) {
1827 if (m_fields[i].fid == focused_id) {
1828 mydata.focused_fieldname =
1839 for (u32 i = 0; i < m_tables.size(); ++i) {
1840 GUITable *table = m_tables[i].second;
1844 mydata.size= v2s32(100,100);
1845 mydata.screensize = screensize;
1847 // Base position of contents of form
1848 mydata.basepos = getBasePos();
1850 /* Convert m_init_draw_spec to m_inventorylists */
1852 m_inventorylists.clear();
1854 m_backgrounds.clear();
1855 m_itemimages.clear();
1857 m_checkboxes.clear();
1858 m_scrollbars.clear();
1862 m_inventory_rings.clear();
1863 m_static_texts.clear();
1865 // Set default values (fits old formspec values)
1866 m_bgcolor = video::SColor(140,0,0,0);
1867 m_bgfullscreen = false;
1869 m_slotbg_n = video::SColor(255,128,128,128);
1870 m_slotbg_h = video::SColor(255,192,192,192);
1872 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1873 m_default_tooltip_color = video::SColor(255,255,255,255);
1875 m_slotbordercolor = video::SColor(200,0,0,0);
1876 m_slotborder = false;
1878 m_clipbackground = false;
1881 assert(m_tooltip_element == NULL);
1882 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1883 m_tooltip_element = addStaticText(Environment, L"",core::rect<s32>(0,0,110,18));
1884 m_tooltip_element->enableOverrideColor(true);
1885 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1886 m_tooltip_element->setDrawBackground(true);
1887 m_tooltip_element->setDrawBorder(true);
1888 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1889 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1890 m_tooltip_element->setWordWrap(false);
1891 //we're not parent so no autograb for this one!
1892 m_tooltip_element->grab();
1895 std::vector<std::string> elements = split(m_formspec_string,']');
1898 /* try to read version from first element only */
1899 if (elements.size() >= 1) {
1900 if ( parseVersionDirect(elements[0]) ) {
1905 /* we need size first in order to calculate image scale */
1906 mydata.explicit_size = false;
1907 for (; i< elements.size(); i++) {
1908 if (!parseSizeDirect(&mydata, elements[i])) {
1913 if (mydata.explicit_size) {
1914 // compute scaling for specified form size
1916 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1917 v2u32 delta = current_screensize - m_lockscreensize;
1919 if (current_screensize.Y > m_lockscreensize.Y)
1924 if (current_screensize.X > m_lockscreensize.X)
1929 offset = v2s32(delta.X,delta.Y);
1931 mydata.screensize = m_lockscreensize;
1933 offset = v2s32(0,0);
1936 double gui_scaling = g_settings->getFloat("gui_scaling");
1937 double screen_dpi = porting::getDisplayDensity() * 96;
1941 // In fixed-size mode, inventory image size
1942 // is 0.53 inch multiplied by the gui_scaling
1943 // config parameter. This magic size is chosen
1944 // to make the main menu (15.5 inventory images
1945 // wide, including border) just fit into the
1946 // default window (800 pixels wide) at 96 DPI
1947 // and default scaling (1.00).
1948 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1950 // In variable-size mode, we prefer to make the
1951 // inventory image size 1/15 of screen height,
1952 // multiplied by the gui_scaling config parameter.
1953 // If the preferred size won't fit the whole
1954 // form on the screen, either horizontally or
1955 // vertically, then we scale it down to fit.
1956 // (The magic numbers in the computation of what
1957 // fits arise from the scaling factors in the
1958 // following stanza, including the form border,
1959 // help text space, and 0.1 inventory slot spare.)
1960 // However, a minimum size is also set, that
1961 // the image size can't be less than 0.3 inch
1962 // multiplied by gui_scaling, even if this means
1963 // the form doesn't fit the screen.
1964 double prefer_imgsize = mydata.screensize.Y / 15 *
1966 double fitx_imgsize = mydata.screensize.X /
1967 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1968 double fity_imgsize = mydata.screensize.Y /
1969 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1970 double screen_dpi = porting::getDisplayDensity() * 96;
1971 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1972 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1973 MYMIN(fitx_imgsize, fity_imgsize)));
1976 // Everything else is scaled in proportion to the
1977 // inventory image size. The inventory slot spacing
1978 // is 5/4 image size horizontally and 15/13 image size
1979 // vertically. The padding around the form (incorporating
1980 // the border of the outer inventory slots) is 3/8
1981 // image size. Font height (baseline to baseline)
1982 // is 2/5 vertical inventory slot spacing, and button
1983 // half-height is 7/8 of font height.
1984 imgsize = v2s32(use_imgsize, use_imgsize);
1985 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
1986 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
1987 m_btn_height = use_imgsize*15.0/13 * 0.35;
1989 m_font = g_fontengine->getFont();
1991 mydata.size = v2s32(
1992 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
1993 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
1995 DesiredRect = mydata.rect = core::rect<s32>(
1996 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
1997 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
1998 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
1999 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
2002 // Non-size[] form must consist only of text fields and
2003 // implicit "Proceed" button. Use default font, and
2004 // temporary form size which will be recalculated below.
2005 m_font = g_fontengine->getFont();
2006 m_btn_height = font_line_height(m_font) * 0.875;
2007 DesiredRect = core::rect<s32>(
2008 mydata.screensize.X/2 - 580/2,
2009 mydata.screensize.Y/2 - 300/2,
2010 mydata.screensize.X/2 + 580/2,
2011 mydata.screensize.Y/2 + 300/2
2014 recalculateAbsolutePosition(false);
2015 mydata.basepos = getBasePos();
2016 m_tooltip_element->setOverrideFont(m_font);
2018 gui::IGUISkin* skin = Environment->getSkin();
2019 sanity_check(skin != NULL);
2020 gui::IGUIFont *old_font = skin->getFont();
2021 skin->setFont(m_font);
2023 for (; i< elements.size(); i++) {
2024 parseElement(&mydata, elements[i]);
2027 // If there are fields without explicit size[], add a "Proceed"
2028 // button and adjust size to fit all the fields.
2029 if (m_fields.size() && !mydata.explicit_size) {
2030 mydata.rect = core::rect<s32>(
2031 mydata.screensize.X/2 - 580/2,
2032 mydata.screensize.Y/2 - 300/2,
2033 mydata.screensize.X/2 + 580/2,
2034 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2036 DesiredRect = mydata.rect;
2037 recalculateAbsolutePosition(false);
2038 mydata.basepos = getBasePos();
2041 v2s32 pos = mydata.basepos;
2042 pos.Y = ((m_fields.size()+2)*60);
2044 v2s32 size = DesiredRect.getSize();
2046 core::rect<s32>(size.X/2-70, pos.Y,
2047 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2048 const wchar_t *text = wgettext("Proceed");
2049 Environment->addButton(mydata.rect, this, 257, text);
2055 //set initial focus if parser didn't set it
2056 focused_element = Environment->getFocus();
2057 if (!focused_element
2058 || !isMyChild(focused_element)
2059 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2062 skin->setFont(old_font);
2066 bool GUIFormSpecMenu::getAndroidUIInput()
2068 /* no dialog shown */
2069 if (m_JavaDialogFieldName == "") {
2074 if (porting::getInputDialogState() == -1) {
2078 std::string fieldname = m_JavaDialogFieldName;
2079 m_JavaDialogFieldName = "";
2081 /* no value abort dialog processing */
2082 if (porting::getInputDialogState() != 0) {
2086 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2087 iter != m_fields.end(); ++iter) {
2089 if (iter->fname != fieldname) {
2092 IGUIElement* tochange = getElementFromId(iter->fid);
2094 if (tochange == 0) {
2098 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2102 std::string text = porting::getInputDialogValue();
2104 ((gui::IGUIEditBox*) tochange)->
2105 setText(utf8_to_wide(text).c_str());
2111 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2113 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2115 for(u32 i=0; i<m_inventorylists.size(); i++)
2117 const ListDrawSpec &s = m_inventorylists[i];
2119 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2120 s32 item_i = i + s.start_item_i;
2121 s32 x = (i%s.geom.X) * spacing.X;
2122 s32 y = (i/s.geom.X) * spacing.Y;
2124 core::rect<s32> rect = imgrect + s.pos + p0;
2125 if(rect.isPointInside(p))
2127 return ItemSpec(s.inventoryloc, s.listname, item_i);
2132 return ItemSpec(InventoryLocation(), "", -1);
2135 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
2138 video::IVideoDriver* driver = Environment->getVideoDriver();
2140 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2142 warningstream<<"GUIFormSpecMenu::drawList(): "
2143 <<"The inventory location "
2144 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2148 InventoryList *ilist = inv->getList(s.listname);
2150 warningstream<<"GUIFormSpecMenu::drawList(): "
2151 <<"The inventory list \""<<s.listname<<"\" @ \""
2152 <<s.inventoryloc.dump()<<"\" doesn't exist"
2157 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2159 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2161 s32 item_i = i + s.start_item_i;
2162 if(item_i >= (s32) ilist->getSize())
2164 s32 x = (i%s.geom.X) * spacing.X;
2165 s32 y = (i/s.geom.X) * spacing.Y;
2167 core::rect<s32> rect = imgrect + s.pos + p;
2170 item = ilist->getItem(item_i);
2172 bool selected = m_selected_item
2173 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2174 && m_selected_item->listname == s.listname
2175 && m_selected_item->i == item_i;
2176 bool hovering = rect.isPointInside(m_pointer);
2177 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2178 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2182 item_hovered = true;
2183 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2185 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2189 //Draw inv slot borders
2191 s32 x1 = rect.UpperLeftCorner.X;
2192 s32 y1 = rect.UpperLeftCorner.Y;
2193 s32 x2 = rect.LowerRightCorner.X;
2194 s32 y2 = rect.LowerRightCorner.Y;
2196 driver->draw2DRectangle(m_slotbordercolor,
2197 core::rect<s32>(v2s32(x1 - border, y1 - border),
2198 v2s32(x2 + border, y1)), NULL);
2199 driver->draw2DRectangle(m_slotbordercolor,
2200 core::rect<s32>(v2s32(x1 - border, y2),
2201 v2s32(x2 + border, y2 + border)), NULL);
2202 driver->draw2DRectangle(m_slotbordercolor,
2203 core::rect<s32>(v2s32(x1 - border, y1),
2204 v2s32(x1, y2)), NULL);
2205 driver->draw2DRectangle(m_slotbordercolor,
2206 core::rect<s32>(v2s32(x2, y1),
2207 v2s32(x2 + border, y2)), NULL);
2215 item.takeItem(m_selected_amount);
2219 drawItemStack(driver, m_font, item,
2220 rect, &AbsoluteClippingRect, m_gamedef,
2225 std::wstring tooltip_text = L"";
2226 if (hovering && !m_selected_item) {
2227 tooltip_text = utf8_to_wide(item.getDefinition(m_gamedef->idef()).description);
2229 if (tooltip_text != L"") {
2230 std::vector<std::wstring> tt_rows = str_split(tooltip_text, L'\n');
2231 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2232 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2233 m_tooltip_element->setVisible(true);
2234 this->bringToFront(m_tooltip_element);
2235 setStaticText(m_tooltip_element, tooltip_text.c_str());
2236 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2237 #if IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2
2238 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2240 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2242 v2u32 screenSize = driver->getScreenSize();
2243 int tooltip_offset_x = m_btn_height;
2244 int tooltip_offset_y = m_btn_height;
2246 tooltip_offset_x *= 3;
2247 tooltip_offset_y = 0;
2248 if (m_pointer.X > (s32)screenSize.X / 2)
2249 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2251 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2252 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2253 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2254 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2255 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2256 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2257 m_tooltip_element->setRelativePosition(core::rect<s32>(
2258 core::position2d<s32>(tooltip_x, tooltip_y),
2259 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2265 void GUIFormSpecMenu::drawSelectedItem()
2267 video::IVideoDriver* driver = Environment->getVideoDriver();
2269 if (!m_selected_item) {
2270 drawItemStack(driver, m_font, ItemStack(),
2271 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2272 NULL, m_gamedef, IT_ROT_DRAGGED);
2276 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2278 InventoryList *list = inv->getList(m_selected_item->listname);
2280 ItemStack stack = list->getItem(m_selected_item->i);
2281 stack.count = m_selected_amount;
2283 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2284 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2285 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, IT_ROT_DRAGGED);
2288 void GUIFormSpecMenu::drawMenu()
2291 std::string newform = m_form_src->getForm();
2292 if(newform != m_formspec_string){
2293 m_formspec_string = newform;
2294 regenerateGui(m_screensize_old);
2298 gui::IGUISkin* skin = Environment->getSkin();
2299 sanity_check(skin != NULL);
2300 gui::IGUIFont *old_font = skin->getFont();
2301 skin->setFont(m_font);
2303 updateSelectedItem();
2305 video::IVideoDriver* driver = Environment->getVideoDriver();
2307 v2u32 screenSize = driver->getScreenSize();
2308 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2310 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2312 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2314 m_tooltip_element->setVisible(false);
2319 for(u32 i=0; i<m_backgrounds.size(); i++)
2321 const ImageDrawSpec &spec = m_backgrounds[i];
2322 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2325 // Image size on screen
2326 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2327 // Image rectangle on screen
2328 core::rect<s32> rect = imgrect + spec.pos;
2330 if (m_clipbackground) {
2331 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2332 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2333 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2334 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2335 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2338 const video::SColor color(255,255,255,255);
2339 const video::SColor colors[] = {color,color,color,color};
2340 draw2DImageFilterScaled(driver, texture, rect,
2341 core::rect<s32>(core::position2d<s32>(0,0),
2342 core::dimension2di(texture->getOriginalSize())),
2343 NULL/*&AbsoluteClippingRect*/, colors, true);
2346 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2347 errorstream << "\t" << spec.name << std::endl;
2354 for(u32 i=0; i<m_boxes.size(); i++)
2356 const BoxDrawSpec &spec = m_boxes[i];
2358 irr::video::SColor todraw = spec.color;
2360 todraw.setAlpha(140);
2362 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2363 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2365 driver->draw2DRectangle(todraw, rect, 0);
2371 gui::IGUIElement::draw();
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.item_name, idef);
2418 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2419 // Viewport rectangle on screen
2420 core::rect<s32> rect = imgrect + spec.pos;
2421 if (spec.parent_button && spec.parent_button->isPressed()) {
2422 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2423 rect += core::dimension2d<s32>(
2424 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2426 rect += core::dimension2d<s32>(
2427 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2428 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2431 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2432 m_gamedef, IT_ROT_NONE);
2437 Phase 0: Item slot rectangles
2438 Phase 1: Item images; prepare tooltip
2440 bool item_hovered = false;
2441 int start_phase = 0;
2442 for (int phase = start_phase; phase <= 1; phase++) {
2443 for (u32 i = 0; i < m_inventorylists.size(); i++) {
2444 drawList(m_inventorylists[i], phase, item_hovered);
2447 if (!item_hovered) {
2448 drawItemStack(driver, m_font, ItemStack(),
2449 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2450 NULL, m_gamedef, IT_ROT_HOVERED);
2453 /* TODO find way to show tooltips on touchscreen */
2454 #ifndef HAVE_TOUCHSCREENGUI
2455 m_pointer = m_device->getCursorControl()->getPosition();
2459 Draw static text elements
2461 for (u32 i = 0; i < m_static_texts.size(); i++) {
2462 const StaticTextSpec &spec = m_static_texts[i];
2463 core::rect<s32> rect = spec.rect;
2464 if (spec.parent_button && spec.parent_button->isPressed()) {
2465 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2466 rect += core::dimension2d<s32>(
2467 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2469 // Use image offset instead of text's because its a bit smaller
2470 // and fits better, also TEXT_OFFSET_X is always 0
2471 rect += core::dimension2d<s32>(
2472 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2473 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2476 video::SColor color(255, 255, 255, 255);
2477 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
2481 Draw fields/buttons tooltips
2483 gui::IGUIElement *hovered =
2484 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2486 if (hovered != NULL) {
2487 s32 id = hovered->getID();
2491 m_old_tooltip_id = id;
2492 m_old_tooltip = L"";
2494 if (id == m_old_tooltip_id) {
2495 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2497 m_hovered_time = getTimeMs();
2498 m_old_tooltip_id = id;
2502 if (id != -1 && delta >= m_tooltip_show_delay) {
2503 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2504 iter != m_fields.end(); ++iter) {
2505 if (iter->fid == id && m_tooltips[iter->fname].tooltip != L"") {
2506 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2507 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2508 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2509 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2510 setStaticText(m_tooltip_element, m_tooltips[iter->fname].tooltip.c_str());
2511 std::vector<std::wstring> tt_rows = str_split(m_tooltips[iter->fname].tooltip, L'\n');
2512 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2513 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2514 int tooltip_offset_x = m_btn_height;
2515 int tooltip_offset_y = m_btn_height;
2517 tooltip_offset_x *= 3;
2518 tooltip_offset_y = 0;
2519 if (m_pointer.X > (s32)screenSize.X / 2)
2520 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2522 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2523 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2524 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2525 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2526 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2527 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2528 m_tooltip_element->setRelativePosition(core::rect<s32>(
2529 core::position2d<s32>(tooltip_x, tooltip_y),
2530 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2532 m_tooltip_element->setVisible(true);
2533 this->bringToFront(m_tooltip_element);
2540 m_tooltip_element->draw();
2543 Draw dragged item stack
2547 skin->setFont(old_font);
2550 void GUIFormSpecMenu::updateSelectedItem()
2552 // If the selected stack has become empty for some reason, deselect it.
2553 // If the selected stack has become inaccessible, deselect it.
2554 // If the selected stack has become smaller, adjust m_selected_amount.
2555 ItemStack selected = verifySelectedItem();
2557 // WARNING: BLACK MAGIC
2558 // See if there is a stack suited for our current guess.
2559 // If such stack does not exist, clear the guess.
2560 if(m_selected_content_guess.name != "" &&
2561 selected.name == m_selected_content_guess.name &&
2562 selected.count == m_selected_content_guess.count){
2563 // Selected item fits the guess. Skip the black magic.
2565 else if(m_selected_content_guess.name != ""){
2567 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2568 const ListDrawSpec &s = m_inventorylists[i];
2569 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2572 InventoryList *list = inv->getList(s.listname);
2575 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2576 u32 item_i = i + s.start_item_i;
2577 if(item_i >= list->getSize())
2579 ItemStack stack = list->getItem(item_i);
2580 if(stack.name == m_selected_content_guess.name &&
2581 stack.count == m_selected_content_guess.count){
2583 infostream<<"Client: Changing selected content guess to "
2584 <<s.inventoryloc.dump()<<" "<<s.listname
2585 <<" "<<item_i<<std::endl;
2586 delete m_selected_item;
2587 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2588 m_selected_amount = stack.count;
2593 infostream<<"Client: Discarding selected content guess: "
2594 <<m_selected_content_guess.getItemString()<<std::endl;
2595 m_selected_content_guess.name = "";
2599 // If craftresult is nonempty and nothing else is selected, select it now.
2600 if(!m_selected_item)
2602 for(u32 i=0; i<m_inventorylists.size(); i++)
2604 const ListDrawSpec &s = m_inventorylists[i];
2605 if(s.listname == "craftpreview")
2607 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2608 InventoryList *list = inv->getList("craftresult");
2609 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2611 m_selected_item = new ItemSpec;
2612 m_selected_item->inventoryloc = s.inventoryloc;
2613 m_selected_item->listname = "craftresult";
2614 m_selected_item->i = 0;
2615 m_selected_amount = 0;
2616 m_selected_dragging = false;
2623 // If craftresult is selected, keep the whole stack selected
2624 if(m_selected_item && m_selected_item->listname == "craftresult")
2626 m_selected_amount = verifySelectedItem().count;
2630 ItemStack GUIFormSpecMenu::verifySelectedItem()
2632 // If the selected stack has become empty for some reason, deselect it.
2633 // If the selected stack has become inaccessible, deselect it.
2634 // If the selected stack has become smaller, adjust m_selected_amount.
2635 // Return the selected stack.
2639 if(m_selected_item->isValid())
2641 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2644 InventoryList *list = inv->getList(m_selected_item->listname);
2645 if(list && (u32) m_selected_item->i < list->getSize())
2647 ItemStack stack = list->getItem(m_selected_item->i);
2648 if(m_selected_amount > stack.count)
2649 m_selected_amount = stack.count;
2656 // selection was not valid
2657 delete m_selected_item;
2658 m_selected_item = NULL;
2659 m_selected_amount = 0;
2660 m_selected_dragging = false;
2665 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2671 if (quitmode == quit_mode_accept) {
2672 fields["quit"] = "true";
2675 if (quitmode == quit_mode_cancel) {
2676 fields["quit"] = "true";
2677 m_text_dst->gotText(fields);
2681 if (current_keys_pending.key_down) {
2682 fields["key_down"] = "true";
2683 current_keys_pending.key_down = false;
2686 if (current_keys_pending.key_up) {
2687 fields["key_up"] = "true";
2688 current_keys_pending.key_up = false;
2691 if (current_keys_pending.key_enter) {
2692 fields["key_enter"] = "true";
2693 current_keys_pending.key_enter = false;
2696 if (current_keys_pending.key_escape) {
2697 fields["key_escape"] = "true";
2698 current_keys_pending.key_escape = false;
2701 for(unsigned int i=0; i<m_fields.size(); i++) {
2702 const FieldSpec &s = m_fields[i];
2704 std::string name = s.fname;
2705 if (s.ftype == f_Button) {
2706 fields[name] = wide_to_utf8(s.flabel);
2707 } else if (s.ftype == f_Table) {
2708 GUITable *table = getTable(s.fname);
2710 fields[name] = table->checkEvent();
2713 else if(s.ftype == f_DropDown) {
2714 // no dynamic cast possible due to some distributions shipped
2715 // without rtti support in irrlicht
2716 IGUIElement * element = getElementFromId(s.fid);
2717 gui::IGUIComboBox *e = NULL;
2718 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2719 e = static_cast<gui::IGUIComboBox*>(element);
2721 s32 selected = e->getSelected();
2722 if (selected >= 0) {
2723 std::vector<std::string> *dropdown_values =
2724 getDropDownValues(s.fname);
2725 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2726 fields[name] = (*dropdown_values)[selected];
2730 else if (s.ftype == f_TabHeader) {
2731 // no dynamic cast possible due to some distributions shipped
2732 // without rtti support in irrlicht
2733 IGUIElement * element = getElementFromId(s.fid);
2734 gui::IGUITabControl *e = NULL;
2735 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2736 e = static_cast<gui::IGUITabControl*>(element);
2740 std::stringstream ss;
2741 ss << (e->getActiveTab() +1);
2742 fields[name] = ss.str();
2745 else if (s.ftype == f_CheckBox) {
2746 // no dynamic cast possible due to some distributions shipped
2747 // without rtti support in irrlicht
2748 IGUIElement * element = getElementFromId(s.fid);
2749 gui::IGUICheckBox *e = NULL;
2750 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2751 e = static_cast<gui::IGUICheckBox*>(element);
2756 fields[name] = "true";
2758 fields[name] = "false";
2761 else if (s.ftype == f_ScrollBar) {
2762 // no dynamic cast possible due to some distributions shipped
2763 // without rtti support in irrlicht
2764 IGUIElement * element = getElementFromId(s.fid);
2765 gui::IGUIScrollBar *e = NULL;
2766 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2767 e = static_cast<gui::IGUIScrollBar*>(element);
2771 std::stringstream os;
2773 if (s.fdefault == L"Changed")
2774 fields[name] = "CHG:" + os.str();
2776 fields[name] = "VAL:" + os.str();
2781 IGUIElement* e = getElementFromId(s.fid);
2783 fields[name] = wide_to_utf8(e->getText());
2789 m_text_dst->gotText(fields);
2793 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2795 while(tocheck != NULL) {
2796 if (tocheck == parent) {
2799 tocheck = tocheck->getParent();
2804 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2806 // The IGUITabControl renders visually using the skin's selected
2807 // font, which we override for the duration of form drawing,
2808 // but computes tab hotspots based on how it would have rendered
2809 // using the font that is selected at the time of button release.
2810 // To make these two consistent, temporarily override the skin's
2811 // font while the IGUITabControl is processing the event.
2812 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2813 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2814 s32 x = event.MouseInput.X;
2815 s32 y = event.MouseInput.Y;
2816 gui::IGUIElement *hovered =
2817 Environment->getRootGUIElement()->getElementFromPoint(
2818 core::position2d<s32>(x, y));
2819 if (hovered && isMyChild(hovered) &&
2820 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2821 gui::IGUISkin* skin = Environment->getSkin();
2822 sanity_check(skin != NULL);
2823 gui::IGUIFont *old_font = skin->getFont();
2824 skin->setFont(m_font);
2825 bool retval = hovered->OnEvent(event);
2826 skin->setFont(old_font);
2831 // Fix Esc/Return key being eaten by checkboxen and tables
2832 if(event.EventType==EET_KEY_INPUT_EVENT) {
2833 KeyPress kp(event.KeyInput);
2834 if (kp == EscapeKey || kp == CancelKey
2835 || kp == getKeySetting("keymap_inventory")
2836 || event.KeyInput.Key==KEY_RETURN) {
2837 gui::IGUIElement *focused = Environment->getFocus();
2838 if (focused && isMyChild(focused) &&
2839 (focused->getType() == gui::EGUIET_LIST_BOX ||
2840 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2846 // Mouse wheel events: send to hovered element instead of focused
2847 if(event.EventType==EET_MOUSE_INPUT_EVENT
2848 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2849 s32 x = event.MouseInput.X;
2850 s32 y = event.MouseInput.Y;
2851 gui::IGUIElement *hovered =
2852 Environment->getRootGUIElement()->getElementFromPoint(
2853 core::position2d<s32>(x, y));
2854 if (hovered && isMyChild(hovered)) {
2855 hovered->OnEvent(event);
2860 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2861 s32 x = event.MouseInput.X;
2862 s32 y = event.MouseInput.Y;
2863 gui::IGUIElement *hovered =
2864 Environment->getRootGUIElement()->getElementFromPoint(
2865 core::position2d<s32>(x, y));
2866 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2867 m_old_tooltip_id = -1;
2868 m_old_tooltip = L"";
2870 if (!isChild(hovered,this)) {
2871 if (DoubleClickDetection(event)) {
2878 // display software keyboard when clicking edit boxes
2879 if (event.EventType == EET_MOUSE_INPUT_EVENT
2880 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2881 gui::IGUIElement *hovered =
2882 Environment->getRootGUIElement()->getElementFromPoint(
2883 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2884 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2885 bool retval = hovered->OnEvent(event);
2887 Environment->setFocus(hovered);
2889 m_JavaDialogFieldName = getNameByID(hovered->getID());
2890 std::string message = gettext("Enter ");
2891 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
2895 message += gettext(label) + ":";
2897 /* single line text input */
2900 /* multi line text input */
2901 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2905 /* passwords are always single line */
2906 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2910 porting::showInputDialog(gettext("ok"), "",
2911 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
2917 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2920 memset(&translated, 0, sizeof(SEvent));
2921 translated.EventType = EET_MOUSE_INPUT_EVENT;
2922 gui::IGUIElement* root = Environment->getRootGUIElement();
2926 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2930 gui::IGUIElement* hovered = root->getElementFromPoint(
2931 core::position2d<s32>(
2933 event.TouchInput.Y));
2935 translated.MouseInput.X = event.TouchInput.X;
2936 translated.MouseInput.Y = event.TouchInput.Y;
2937 translated.MouseInput.Control = false;
2939 bool dont_send_event = false;
2941 if (event.TouchInput.touchedCount == 1) {
2942 switch (event.TouchInput.Event) {
2943 case ETIE_PRESSED_DOWN:
2944 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2945 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2946 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2947 m_down_pos = m_pointer;
2950 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2951 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2952 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2955 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2956 translated.MouseInput.ButtonStates = 0;
2957 hovered = root->getElementFromPoint(m_down_pos);
2958 /* we don't have a valid pointer element use last
2959 * known pointer pos */
2960 translated.MouseInput.X = m_pointer.X;
2961 translated.MouseInput.Y = m_pointer.Y;
2963 /* reset down pos */
2964 m_down_pos = v2s32(0,0);
2967 dont_send_event = true;
2968 //this is not supposed to happen
2970 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2971 << event.TouchInput.Event << std::endl;
2973 } else if ( (event.TouchInput.touchedCount == 2) &&
2974 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2975 hovered = root->getElementFromPoint(m_down_pos);
2977 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2978 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2979 translated.MouseInput.X = m_pointer.X;
2980 translated.MouseInput.Y = m_pointer.Y;
2983 hovered->OnEvent(translated);
2986 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
2987 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2991 hovered->OnEvent(translated);
2993 dont_send_event = true;
2995 /* ignore unhandled 2 touch events ... accidental moving for example */
2996 else if (event.TouchInput.touchedCount == 2) {
2997 dont_send_event = true;
2999 else if (event.TouchInput.touchedCount > 2) {
3001 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
3002 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3005 if (dont_send_event) {
3009 /* check if translated event needs to be preprocessed again */
3010 if (preprocessEvent(translated)) {
3015 bool retval = hovered->OnEvent(translated);
3017 if (event.TouchInput.Event == ETIE_LEFT_UP) {
3019 m_pointer = v2s32(0,0);
3030 /******************************************************************************/
3031 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3033 /* The following code is for capturing double-clicks of the mouse button
3034 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3035 * -- which closes the form -- under some circumstances.
3037 * There have been many github issues reporting this as a bug even though it
3038 * was an intended feature. For this reason, remapping the double-click as
3039 * an ESC must be explicitly set when creating this class via the
3040 * /p remap_dbl_click parameter of the constructor.
3043 if (!m_remap_dbl_click)
3046 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3047 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3048 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3050 m_doubleclickdetect[1].pos = m_pointer;
3051 m_doubleclickdetect[1].time = getTimeMs();
3053 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3054 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
3059 double squaredistance =
3060 m_doubleclickdetect[0].pos
3061 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3063 if (squaredistance > (30*30)) {
3067 SEvent* translated = new SEvent();
3068 assert(translated != 0);
3069 //translate doubleclick to escape
3070 memset(translated, 0, sizeof(SEvent));
3071 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3072 translated->KeyInput.Key = KEY_ESCAPE;
3073 translated->KeyInput.Control = false;
3074 translated->KeyInput.Shift = false;
3075 translated->KeyInput.PressedDown = true;
3076 translated->KeyInput.Char = 0;
3077 OnEvent(*translated);
3079 // no need to send the key up event as we're already deleted
3080 // and no one else did notice this event
3088 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3090 if (event.EventType==EET_KEY_INPUT_EVENT) {
3091 KeyPress kp(event.KeyInput);
3092 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3093 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3096 acceptInput(quit_mode_cancel);
3099 m_text_dst->gotText(L"MenuQuit");
3102 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3103 (kp == getKeySetting("keymap_screenshot"))) {
3104 m_client->makeScreenshot(m_device);
3106 if (event.KeyInput.PressedDown &&
3107 (event.KeyInput.Key==KEY_RETURN ||
3108 event.KeyInput.Key==KEY_UP ||
3109 event.KeyInput.Key==KEY_DOWN)
3111 switch (event.KeyInput.Key) {
3113 current_keys_pending.key_enter = true;
3116 current_keys_pending.key_up = true;
3119 current_keys_pending.key_down = true;
3123 //can't happen at all!
3124 FATAL_ERROR("Reached a source line that can't ever been reached");
3127 if (current_keys_pending.key_enter && m_allowclose) {
3128 acceptInput(quit_mode_accept);
3138 /* Mouse event other than movement, or crossing the border of inventory
3139 field while holding right mouse button
3141 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3142 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3143 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3144 event.MouseInput.isRightPressed() &&
3145 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3147 // Get selected item and hovered/clicked item (s)
3149 m_old_tooltip_id = -1;
3150 updateSelectedItem();
3151 ItemSpec s = getItemAtPos(m_pointer);
3153 Inventory *inv_selected = NULL;
3154 Inventory *inv_s = NULL;
3155 InventoryList *list_s = NULL;
3157 if (m_selected_item) {
3158 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3159 sanity_check(inv_selected);
3160 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3167 inv_s = m_invmgr->getInventory(s.inventoryloc);
3170 errorstream << "InventoryMenu: The selected inventory location "
3171 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3173 s.i = -1; // make it invalid again
3177 list_s = inv_s->getList(s.listname);
3178 if (list_s == NULL) {
3179 verbosestream << "InventoryMenu: The selected inventory list \""
3180 << s.listname << "\" does not exist" << std::endl;
3181 s.i = -1; // make it invalid again
3185 if ((u32)s.i >= list_s->getSize()) {
3186 infostream << "InventoryMenu: The selected inventory list \""
3187 << s.listname << "\" is too small (i=" << s.i << ", size="
3188 << list_s->getSize() << ")" << std::endl;
3189 s.i = -1; // make it invalid again
3193 s_count = list_s->getItem(s.i).count;
3196 bool identical = (m_selected_item != NULL) && s.isValid() &&
3197 (inv_selected == inv_s) &&
3198 (m_selected_item->listname == s.listname) &&
3199 (m_selected_item->i == s.i);
3201 // buttons: 0 = left, 1 = right, 2 = middle
3202 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3205 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3206 { button = 0; updown = 0; }
3207 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3208 { button = 1; updown = 0; }
3209 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3210 { button = 2; updown = 0; }
3211 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3212 { button = 0; updown = 1; }
3213 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3214 { button = 1; updown = 1; }
3215 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3216 { button = 2; updown = 1; }
3217 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3220 // Set this number to a positive value to generate a move action
3221 // from m_selected_item to s.
3222 u32 move_amount = 0;
3224 // Set this number to a positive value to generate a move action
3225 // from s to the next inventory ring.
3226 u32 shift_move_amount = 0;
3228 // Set this number to a positive value to generate a drop action
3229 // from m_selected_item.
3230 u32 drop_amount = 0;
3232 // Set this number to a positive value to generate a craft action at s.
3233 u32 craft_amount = 0;
3236 // Some mouse button has been pressed
3238 //infostream<<"Mouse button "<<button<<" pressed at p=("
3239 // <<p.X<<","<<p.Y<<")"<<std::endl;
3241 m_selected_dragging = false;
3243 if (s.isValid() && s.listname == "craftpreview") {
3244 // Craft preview has been clicked: craft
3245 craft_amount = (button == 2 ? 10 : 1);
3246 } else if (m_selected_item == NULL) {
3248 // Non-empty stack has been clicked: select or shift-move it
3249 m_selected_item = new ItemSpec(s);
3252 if (button == 1) // right
3253 count = (s_count + 1) / 2;
3254 else if (button == 2) // middle
3255 count = MYMIN(s_count, 10);
3259 if (!event.MouseInput.Shift) {
3260 // no shift: select item
3261 m_selected_amount = count;
3262 m_selected_dragging = true;
3263 m_rmouse_auto_place = false;
3265 // shift pressed: move item
3267 shift_move_amount = count;
3268 else // count of 1 at left click like after drag & drop
3269 shift_move_amount = 1;
3272 } else { // m_selected_item != NULL
3273 assert(m_selected_amount >= 1);
3276 // Clicked a slot: move
3277 if (button == 1) // right
3279 else if (button == 2) // middle
3280 move_amount = MYMIN(m_selected_amount, 10);
3282 move_amount = m_selected_amount;
3285 if (move_amount >= m_selected_amount)
3286 m_selected_amount = 0;
3288 m_selected_amount -= move_amount;
3292 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3293 // Clicked outside of the window: drop
3294 if (button == 1) // right
3296 else if (button == 2) // middle
3297 drop_amount = MYMIN(m_selected_amount, 10);
3299 drop_amount = m_selected_amount;
3303 else if (updown == 1) {
3304 // Some mouse button has been released
3306 //infostream<<"Mouse button "<<button<<" released at p=("
3307 // <<p.X<<","<<p.Y<<")"<<std::endl;
3309 if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3311 // Dragged to different slot: move all selected
3312 move_amount = m_selected_amount;
3314 } else if (m_selected_item != NULL && m_selected_dragging &&
3315 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3316 // Dragged outside of window: drop all selected
3317 drop_amount = m_selected_amount;
3320 m_selected_dragging = false;
3321 // Keep count of how many times right mouse button has been
3322 // clicked. One click is drag without dropping. Click + release
3323 // + click changes to drop one item when moved mode
3324 if (button == 1 && m_selected_item != NULL)
3325 m_rmouse_auto_place = !m_rmouse_auto_place;
3326 } else if (updown == -1) {
3327 // Mouse has been moved and rmb is down and mouse pointer just
3328 // entered a new inventory field (checked in the entry-if, this
3329 // is the only action here that is generated by mouse movement)
3330 if (m_selected_item != NULL && s.isValid()) {
3332 // TODO: middle mouse to move 10 items might be handy
3333 if (m_rmouse_auto_place) {
3334 // Only move an item if the destination slot is empty
3335 // or contains the same item type as what is going to be
3337 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3338 InventoryList *list_to = list_s;
3339 assert(list_from && list_to);
3340 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3341 ItemStack stack_to = list_to->getItem(s.i);
3342 if (stack_to.empty() || stack_to.name == stack_from.name)
3348 // Possibly send inventory action to server
3349 if (move_amount > 0) {
3350 // Send IACTION_MOVE
3352 assert(m_selected_item && m_selected_item->isValid());
3353 assert(s.isValid());
3355 assert(inv_selected && inv_s);
3356 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3357 InventoryList *list_to = list_s;
3358 assert(list_from && list_to);
3359 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3360 ItemStack stack_to = list_to->getItem(s.i);
3362 // Check how many items can be moved
3363 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3364 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3365 // If source stack cannot be added to destination stack at all,
3367 if ((leftover.count == stack_from.count) &&
3368 (leftover.name == stack_from.name)) {
3369 m_selected_amount = stack_to.count;
3370 // In case the server doesn't directly swap them but instead
3371 // moves stack_to somewhere else, set this
3372 m_selected_content_guess = stack_to;
3373 m_selected_content_guess_inventory = s.inventoryloc;
3375 // Source stack goes fully into destination stack
3376 else if (leftover.empty()) {
3377 m_selected_amount -= move_amount;
3378 m_selected_content_guess = ItemStack(); // Clear
3380 // Source stack goes partly into destination stack
3382 move_amount -= leftover.count;
3383 m_selected_amount -= move_amount;
3384 m_selected_content_guess = ItemStack(); // Clear
3387 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3388 IMoveAction *a = new IMoveAction();
3389 a->count = move_amount;
3390 a->from_inv = m_selected_item->inventoryloc;
3391 a->from_list = m_selected_item->listname;
3392 a->from_i = m_selected_item->i;
3393 a->to_inv = s.inventoryloc;
3394 a->to_list = s.listname;
3396 m_invmgr->inventoryAction(a);
3397 } else if (shift_move_amount > 0) {
3398 u32 mis = m_inventory_rings.size();
3400 for (; i < mis; i++) {
3401 const ListRingSpec &sp = m_inventory_rings[i];
3402 if (sp.inventoryloc == s.inventoryloc
3403 && sp.listname == s.listname)
3407 if (i >= mis) // if not found
3409 u32 to_inv_ind = (i + 1) % mis;
3410 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3411 InventoryList *list_from = list_s;
3414 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3417 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3420 ItemStack stack_from = list_from->getItem(s.i);
3421 assert(shift_move_amount <= stack_from.count);
3422 if (m_client->getProtoVersion() >= 25) {
3423 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3424 IMoveAction *a = new IMoveAction();
3425 a->count = shift_move_amount;
3426 a->from_inv = s.inventoryloc;
3427 a->from_list = s.listname;
3429 a->to_inv = to_inv_sp.inventoryloc;
3430 a->to_list = to_inv_sp.listname;
3431 a->move_somewhere = true;
3432 m_invmgr->inventoryAction(a);
3434 // find a place (or more than one) to add the new item
3435 u32 ilt_size = list_to->getSize();
3437 for (u32 slot_to = 0; slot_to < ilt_size
3438 && shift_move_amount > 0; slot_to++) {
3439 list_to->itemFits(slot_to, stack_from, &leftover);
3440 if (leftover.count < stack_from.count) {
3441 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3442 IMoveAction *a = new IMoveAction();
3443 a->count = MYMIN(shift_move_amount,
3444 (u32) (stack_from.count - leftover.count));
3445 shift_move_amount -= a->count;
3446 a->from_inv = s.inventoryloc;
3447 a->from_list = s.listname;
3449 a->to_inv = to_inv_sp.inventoryloc;
3450 a->to_list = to_inv_sp.listname;
3452 m_invmgr->inventoryAction(a);
3453 stack_from = leftover;
3458 } else if (drop_amount > 0) {
3459 m_selected_content_guess = ItemStack(); // Clear
3461 // Send IACTION_DROP
3463 assert(m_selected_item && m_selected_item->isValid());
3464 assert(inv_selected);
3465 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3467 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3469 // Check how many items can be dropped
3470 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3471 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3472 m_selected_amount -= drop_amount;
3474 infostream << "Handing IACTION_DROP to manager" << std::endl;
3475 IDropAction *a = new IDropAction();
3476 a->count = drop_amount;
3477 a->from_inv = m_selected_item->inventoryloc;
3478 a->from_list = m_selected_item->listname;
3479 a->from_i = m_selected_item->i;
3480 m_invmgr->inventoryAction(a);
3481 } else if (craft_amount > 0) {
3482 m_selected_content_guess = ItemStack(); // Clear
3484 // Send IACTION_CRAFT
3486 assert(s.isValid());
3489 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3490 ICraftAction *a = new ICraftAction();
3491 a->count = craft_amount;
3492 a->craft_inv = s.inventoryloc;
3493 m_invmgr->inventoryAction(a);
3496 // If m_selected_amount has been decreased to zero, deselect
3497 if (m_selected_amount == 0) {
3498 delete m_selected_item;
3499 m_selected_item = NULL;
3500 m_selected_amount = 0;
3501 m_selected_dragging = false;
3502 m_selected_content_guess = ItemStack();
3504 m_old_pointer = m_pointer;
3506 if (event.EventType == EET_GUI_EVENT) {
3508 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3510 // find the element that was clicked
3511 for (unsigned int i=0; i<m_fields.size(); i++) {
3512 FieldSpec &s = m_fields[i];
3513 if ((s.ftype == f_TabHeader) &&
3514 (s.fid == event.GUIEvent.Caller->getID())) {
3522 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3524 if (!canTakeFocus(event.GUIEvent.Element)) {
3525 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3527 // Returning true disables focus change
3531 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3532 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3533 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3534 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3535 unsigned int btn_id = event.GUIEvent.Caller->getID();
3537 if (btn_id == 257) {
3539 acceptInput(quit_mode_accept);
3543 m_text_dst->gotText(L"ExitButton");
3545 // quitMenu deallocates menu
3549 // find the element that was clicked
3550 for (u32 i = 0; i < m_fields.size(); i++) {
3551 FieldSpec &s = m_fields[i];
3552 // if its a button, set the send field so
3553 // lua knows which button was pressed
3554 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3555 (s.fid == event.GUIEvent.Caller->getID())) {
3559 acceptInput(quit_mode_accept);
3562 m_text_dst->gotText(L"ExitButton");
3566 acceptInput(quit_mode_no);
3570 } else if ((s.ftype == f_DropDown) &&
3571 (s.fid == event.GUIEvent.Caller->getID())) {
3572 // only send the changed dropdown
3573 for (u32 i = 0; i < m_fields.size(); i++) {
3574 FieldSpec &s2 = m_fields[i];
3575 if (s2.ftype == f_DropDown) {
3580 acceptInput(quit_mode_no);
3582 // revert configuration to make sure dropdowns are sent on
3583 // regular button click
3584 for (u32 i = 0; i < m_fields.size(); i++) {
3585 FieldSpec &s2 = m_fields[i];
3586 if (s2.ftype == f_DropDown) {
3591 } else if ((s.ftype == f_ScrollBar) &&
3592 (s.fid == event.GUIEvent.Caller->getID())) {
3593 s.fdefault = L"Changed";
3594 acceptInput(quit_mode_no);
3600 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3601 if (event.GUIEvent.Caller->getID() > 257) {
3604 acceptInput(quit_mode_accept);
3607 current_keys_pending.key_enter = true;
3610 // quitMenu deallocates menu
3615 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3616 int current_id = event.GUIEvent.Caller->getID();
3617 if (current_id > 257) {
3618 // find the element that was clicked
3619 for (u32 i = 0; i < m_fields.size(); i++) {
3620 FieldSpec &s = m_fields[i];
3621 // if it's a table, set the send field
3622 // so lua knows which table was changed
3623 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3634 return Parent ? Parent->OnEvent(event) : false;
3638 * get name of element by element id
3639 * @param id of element
3640 * @return name string or empty string
3642 std::string GUIFormSpecMenu::getNameByID(s32 id)
3644 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3645 iter != m_fields.end(); ++iter) {
3646 if (iter->fid == id) {
3654 * get label of element by id
3655 * @param id of element
3656 * @return label string or empty string
3658 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3660 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3661 iter != m_fields.end(); ++iter) {
3662 if (iter->fid == id) {
3663 return iter->flabel;