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 "guiscalingfilter.h"
55 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
56 #include "intlGUIEditBox.h"
59 #define MY_CHECKPOS(a,b) \
60 if (v_pos.size() != 2) { \
61 errorstream<< "Invalid pos for element " << a << "specified: \"" \
62 << parts[b] << "\"" << std::endl; \
66 #define MY_CHECKGEOM(a,b) \
67 if (v_geom.size() != 2) { \
68 errorstream<< "Invalid pos for element " << a << "specified: \"" \
69 << parts[b] << "\"" << std::endl; \
75 static unsigned int font_line_height(gui::IGUIFont *font)
77 return font->getDimension(L"Ay").Height + font->getKerningHeight();
80 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
81 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
82 InventoryManager *invmgr, IGameDef *gamedef,
83 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
84 Client* client, bool remap_dbl_click) :
85 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
91 m_selected_item(NULL),
93 m_selected_dragging(false),
94 m_tooltip_element(NULL),
97 m_rmouse_auto_place(false),
102 m_formspec_version(0),
103 m_focused_element(""),
105 m_remap_dbl_click(remap_dbl_click)
107 , m_JavaDialogFieldName("")
110 current_keys_pending.key_down = false;
111 current_keys_pending.key_up = false;
112 current_keys_pending.key_enter = false;
113 current_keys_pending.key_escape = false;
115 m_doubleclickdetect[0].time = 0;
116 m_doubleclickdetect[1].time = 0;
118 m_doubleclickdetect[0].pos = v2s32(0, 0);
119 m_doubleclickdetect[1].pos = v2s32(0, 0);
121 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
124 GUIFormSpecMenu::~GUIFormSpecMenu()
128 for (u32 i = 0; i < m_tables.size(); ++i) {
129 GUITable *table = m_tables[i].second;
133 delete m_selected_item;
135 if (m_form_src != NULL) {
138 if (m_text_dst != NULL) {
143 void GUIFormSpecMenu::removeChildren()
145 const core::list<gui::IGUIElement*> &children = getChildren();
147 while(!children.empty()) {
148 (*children.getLast())->remove();
151 if(m_tooltip_element) {
152 m_tooltip_element->remove();
153 m_tooltip_element->drop();
154 m_tooltip_element = NULL;
159 void GUIFormSpecMenu::setInitialFocus()
161 // Set initial focus according to following order of precedence:
162 // 1. first empty editbox
166 // 5. first focusable (not statictext, not tabheader)
167 // 6. first child element
169 core::list<gui::IGUIElement*> children = getChildren();
171 // in case "children" contains any NULL elements, remove them
172 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
173 it != children.end();) {
177 it = children.erase(it);
180 // 1. first empty editbox
181 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
182 it != children.end(); ++it) {
183 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
184 && (*it)->getText()[0] == 0) {
185 Environment->setFocus(*it);
191 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
192 it != children.end(); ++it) {
193 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
194 Environment->setFocus(*it);
200 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
201 it != children.end(); ++it) {
202 if ((*it)->getTypeName() == std::string("GUITable")) {
203 Environment->setFocus(*it);
209 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
210 it != children.end(); --it) {
211 if ((*it)->getType() == gui::EGUIET_BUTTON) {
212 Environment->setFocus(*it);
217 // 5. first focusable (not statictext, not tabheader)
218 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
219 it != children.end(); ++it) {
220 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
221 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
222 Environment->setFocus(*it);
227 // 6. first child element
228 if (children.empty())
229 Environment->setFocus(this);
231 Environment->setFocus(*(children.begin()));
234 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
236 for (u32 i = 0; i < m_tables.size(); ++i) {
237 if (tablename == m_tables[i].first.fname)
238 return m_tables[i].second;
243 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
245 for (u32 i = 0; i < m_dropdowns.size(); ++i) {
246 if (name == m_dropdowns[i].first.fname)
247 return &m_dropdowns[i].second;
252 static std::vector<std::string> split(const std::string &s, char delim)
254 std::vector<std::string> tokens;
256 std::string current = "";
257 bool last_was_escape = false;
258 for (unsigned int i = 0; i < s.size(); i++) {
259 char si = s.c_str()[i];
260 if (last_was_escape) {
263 last_was_escape = false;
266 tokens.push_back(current);
268 last_was_escape = false;
269 } else if (si == '\\') {
270 last_was_escape = true;
273 last_was_escape = false;
278 tokens.push_back(current);
283 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
285 std::vector<std::string> parts = split(element,',');
287 if (((parts.size() == 2) || parts.size() == 3) ||
288 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
290 if (parts[1].find(';') != std::string::npos)
291 parts[1] = parts[1].substr(0,parts[1].find(';'));
293 data->invsize.X = MYMAX(0, stof(parts[0]));
294 data->invsize.Y = MYMAX(0, stof(parts[1]));
297 if (parts.size() == 3) {
298 if (parts[2] == "true") {
299 lockSize(true,v2u32(800,600));
303 data->explicit_size = true;
306 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
309 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
311 if (m_gamedef == 0) {
312 warningstream<<"invalid use of 'list' with m_gamedef==0"<<std::endl;
316 std::vector<std::string> parts = split(element,';');
318 if (((parts.size() == 4) || (parts.size() == 5)) ||
319 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
321 std::string location = parts[0];
322 std::string listname = parts[1];
323 std::vector<std::string> v_pos = split(parts[2],',');
324 std::vector<std::string> v_geom = split(parts[3],',');
325 std::string startindex = "";
326 if (parts.size() == 5)
327 startindex = parts[4];
329 MY_CHECKPOS("list",2);
330 MY_CHECKGEOM("list",3);
332 InventoryLocation loc;
334 if(location == "context" || location == "current_name")
335 loc = m_current_inventory_location;
337 loc.deSerialize(location);
339 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
340 pos.X += stof(v_pos[0]) * (float)spacing.X;
341 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
344 geom.X = stoi(v_geom[0]);
345 geom.Y = stoi(v_geom[1]);
349 start_i = stoi(startindex);
351 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
352 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
356 if(!data->explicit_size)
357 warningstream<<"invalid use of list without a size[] element"<<std::endl;
358 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
361 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
364 void GUIFormSpecMenu::parseListRing(parserData* data, std::string element)
366 if (m_gamedef == 0) {
367 errorstream << "WARNING: invalid use of 'listring' with m_gamedef==0" << std::endl;
371 std::vector<std::string> parts = split(element, ';');
373 if (parts.size() == 2) {
374 std::string location = parts[0];
375 std::string listname = parts[1];
377 InventoryLocation loc;
379 if (location == "context" || location == "current_name")
380 loc = m_current_inventory_location;
382 loc.deSerialize(location);
384 m_inventory_rings.push_back(ListRingSpec(loc, listname));
386 } else if ((element == "") && (m_inventorylists.size() > 1)) {
387 size_t siz = m_inventorylists.size();
388 // insert the last two inv list elements into the list ring
389 const ListDrawSpec &spa = m_inventorylists[siz - 2];
390 const ListDrawSpec &spb = m_inventorylists[siz - 1];
391 m_inventory_rings.push_back(ListRingSpec(spa.inventoryloc, spa.listname));
392 m_inventory_rings.push_back(ListRingSpec(spb.inventoryloc, spb.listname));
395 errorstream<< "Invalid list ring element(" << parts.size() << ", "
396 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
399 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
401 std::vector<std::string> parts = split(element,';');
403 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
404 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
406 std::vector<std::string> v_pos = split(parts[0],',');
407 std::string name = parts[1];
408 std::string label = parts[2];
409 std::string selected = "";
411 if (parts.size() >= 4)
414 MY_CHECKPOS("checkbox",0);
417 pos.X += stof(v_pos[0]) * (float) spacing.X;
418 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
420 bool fselected = false;
422 if (selected == "true")
425 std::wstring wlabel = utf8_to_wide(label);
427 core::rect<s32> rect = core::rect<s32>(
428 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
429 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
430 pos.Y + ((imgsize.Y/2) + m_btn_height));
434 wlabel, //Needed for displaying text on MSVC
439 spec.ftype = f_CheckBox;
441 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
442 spec.fid, spec.flabel.c_str());
444 if (spec.fname == data->focused_fieldname) {
445 Environment->setFocus(e);
448 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
449 m_fields.push_back(spec);
452 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
455 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
457 std::vector<std::string> parts = split(element,';');
459 if (parts.size() >= 5) {
460 std::vector<std::string> v_pos = split(parts[0],',');
461 std::vector<std::string> v_dim = split(parts[1],',');
462 std::string name = parts[3];
463 std::string value = parts[4];
465 MY_CHECKPOS("scrollbar",0);
468 pos.X += stof(v_pos[0]) * (float) spacing.X;
469 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
471 if (v_dim.size() != 2) {
472 errorstream<< "Invalid size for element " << "scrollbar"
473 << "specified: \"" << parts[1] << "\"" << std::endl;
478 dim.X = stof(v_dim[0]) * (float) spacing.X;
479 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
481 core::rect<s32> rect =
482 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
491 bool is_horizontal = true;
493 if (parts[2] == "vertical")
494 is_horizontal = false;
496 spec.ftype = f_ScrollBar;
498 gui::IGUIScrollBar* e =
499 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
503 e->setPos(stoi(parts[4]));
505 e->setLargeStep(100);
507 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
508 m_fields.push_back(spec);
511 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
514 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
516 std::vector<std::string> parts = split(element,';');
518 if ((parts.size() == 3) ||
519 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
521 std::vector<std::string> v_pos = split(parts[0],',');
522 std::vector<std::string> v_geom = split(parts[1],',');
523 std::string name = unescape_string(parts[2]);
525 MY_CHECKPOS("image",0);
526 MY_CHECKGEOM("image",1);
528 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
529 pos.X += stof(v_pos[0]) * (float) spacing.X;
530 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
533 geom.X = stof(v_geom[0]) * (float)imgsize.X;
534 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
536 if(!data->explicit_size)
537 warningstream<<"invalid use of image without a size[] element"<<std::endl;
538 m_images.push_back(ImageDrawSpec(name, pos, geom));
542 if (parts.size() == 2) {
543 std::vector<std::string> v_pos = split(parts[0],',');
544 std::string name = unescape_string(parts[1]);
546 MY_CHECKPOS("image",0);
548 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
549 pos.X += stof(v_pos[0]) * (float) spacing.X;
550 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
552 if(!data->explicit_size)
553 warningstream<<"invalid use of image without a size[] element"<<std::endl;
554 m_images.push_back(ImageDrawSpec(name, pos));
557 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
560 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
562 std::vector<std::string> parts = split(element,';');
564 if ((parts.size() == 3) ||
565 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
567 std::vector<std::string> v_pos = split(parts[0],',');
568 std::vector<std::string> v_geom = split(parts[1],',');
569 std::string name = parts[2];
571 MY_CHECKPOS("itemimage",0);
572 MY_CHECKGEOM("itemimage",1);
574 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
575 pos.X += stof(v_pos[0]) * (float) spacing.X;
576 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
579 geom.X = stof(v_geom[0]) * (float)imgsize.X;
580 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
582 if(!data->explicit_size)
583 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
584 m_itemimages.push_back(ImageDrawSpec("", name, pos, geom));
587 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
590 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
593 std::vector<std::string> parts = split(element,';');
595 if ((parts.size() == 4) ||
596 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
598 std::vector<std::string> v_pos = split(parts[0],',');
599 std::vector<std::string> v_geom = split(parts[1],',');
600 std::string name = parts[2];
601 std::string label = parts[3];
603 MY_CHECKPOS("button",0);
604 MY_CHECKGEOM("button",1);
607 pos.X += stof(v_pos[0]) * (float)spacing.X;
608 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
611 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
612 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
614 core::rect<s32> rect =
615 core::rect<s32>(pos.X, pos.Y - m_btn_height,
616 pos.X + geom.X, pos.Y + m_btn_height);
618 if(!data->explicit_size)
619 warningstream<<"invalid use of button without a size[] element"<<std::endl;
621 std::wstring wlabel = utf8_to_wide(label);
629 spec.ftype = f_Button;
630 if(type == "button_exit")
632 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
633 spec.flabel.c_str());
635 if (spec.fname == data->focused_fieldname) {
636 Environment->setFocus(e);
639 m_fields.push_back(spec);
642 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
645 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
647 std::vector<std::string> parts = split(element,';');
649 if (((parts.size() == 3) || (parts.size() == 4)) ||
650 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
652 std::vector<std::string> v_pos = split(parts[0],',');
653 std::vector<std::string> v_geom = split(parts[1],',');
654 std::string name = unescape_string(parts[2]);
656 MY_CHECKPOS("background",0);
657 MY_CHECKGEOM("background",1);
659 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
660 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
661 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
664 geom.X = stof(v_geom[0]) * (float)spacing.X;
665 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
667 if (parts.size() == 4) {
668 m_clipbackground = is_yes(parts[3]);
669 if (m_clipbackground) {
670 pos.X = stoi(v_pos[0]); //acts as offset
671 pos.Y = stoi(v_pos[1]); //acts as offset
675 if(!data->explicit_size)
676 warningstream<<"invalid use of background without a size[] element"<<std::endl;
677 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
680 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
683 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
685 std::vector<std::string> parts = split(element,';');
687 data->table_options.clear();
688 for (size_t i = 0; i < parts.size(); ++i) {
689 // Parse table option
690 std::string opt = unescape_string(parts[i]);
691 data->table_options.push_back(GUITable::splitOption(opt));
695 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
697 std::vector<std::string> parts = split(element,';');
699 data->table_columns.clear();
700 for (size_t i = 0; i < parts.size(); ++i) {
701 std::vector<std::string> col_parts = split(parts[i],',');
702 GUITable::TableColumn column;
704 if (!col_parts.empty())
705 column.type = col_parts[0];
706 // Parse column options
707 for (size_t j = 1; j < col_parts.size(); ++j) {
708 std::string opt = unescape_string(col_parts[j]);
709 column.options.push_back(GUITable::splitOption(opt));
711 data->table_columns.push_back(column);
715 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
717 std::vector<std::string> parts = split(element,';');
719 if (((parts.size() == 4) || (parts.size() == 5)) ||
720 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
722 std::vector<std::string> v_pos = split(parts[0],',');
723 std::vector<std::string> v_geom = split(parts[1],',');
724 std::string name = parts[2];
725 std::vector<std::string> items = split(parts[3],',');
726 std::string str_initial_selection = "";
727 std::string str_transparent = "false";
729 if (parts.size() >= 5)
730 str_initial_selection = parts[4];
732 MY_CHECKPOS("table",0);
733 MY_CHECKGEOM("table",1);
736 pos.X += stof(v_pos[0]) * (float)spacing.X;
737 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
740 geom.X = stof(v_geom[0]) * (float)spacing.X;
741 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
743 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
752 spec.ftype = f_Table;
754 for (unsigned int i = 0; i < items.size(); ++i) {
755 items[i] = unescape_string(unescape_enriched(items[i]));
758 //now really show table
759 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
762 if (spec.fname == data->focused_fieldname) {
763 Environment->setFocus(e);
766 e->setTable(data->table_options, data->table_columns, items);
768 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
769 e->setDynamicData(data->table_dyndata[name]);
772 if ((str_initial_selection != "") &&
773 (str_initial_selection != "0"))
774 e->setSelected(stoi(str_initial_selection.c_str()));
776 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
777 m_fields.push_back(spec);
780 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
783 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
785 std::vector<std::string> parts = split(element,';');
787 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
788 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
790 std::vector<std::string> v_pos = split(parts[0],',');
791 std::vector<std::string> v_geom = split(parts[1],',');
792 std::string name = parts[2];
793 std::vector<std::string> items = split(parts[3],',');
794 std::string str_initial_selection = "";
795 std::string str_transparent = "false";
797 if (parts.size() >= 5)
798 str_initial_selection = parts[4];
800 if (parts.size() >= 6)
801 str_transparent = parts[5];
803 MY_CHECKPOS("textlist",0);
804 MY_CHECKGEOM("textlist",1);
807 pos.X += stof(v_pos[0]) * (float)spacing.X;
808 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
811 geom.X = stof(v_geom[0]) * (float)spacing.X;
812 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
815 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
824 spec.ftype = f_Table;
826 for (unsigned int i = 0; i < items.size(); ++i) {
827 items[i] = unescape_string(unescape_enriched(items[i]));
830 //now really show list
831 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
834 if (spec.fname == data->focused_fieldname) {
835 Environment->setFocus(e);
838 e->setTextList(items, is_yes(str_transparent));
840 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
841 e->setDynamicData(data->table_dyndata[name]);
844 if ((str_initial_selection != "") &&
845 (str_initial_selection != "0"))
846 e->setSelected(stoi(str_initial_selection.c_str()));
848 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
849 m_fields.push_back(spec);
852 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
856 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
858 std::vector<std::string> parts = split(element,';');
860 if ((parts.size() == 5) ||
861 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
863 std::vector<std::string> v_pos = split(parts[0],',');
864 std::string name = parts[2];
865 std::vector<std::string> items = split(parts[3],',');
866 std::string str_initial_selection = "";
867 str_initial_selection = parts[4];
869 MY_CHECKPOS("dropdown",0);
872 pos.X += stof(v_pos[0]) * (float)spacing.X;
873 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
875 s32 width = stof(parts[1]) * (float)spacing.Y;
877 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
878 pos.X + width, pos.Y + (m_btn_height * 2));
887 spec.ftype = f_DropDown;
890 //now really show list
891 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
893 if (spec.fname == data->focused_fieldname) {
894 Environment->setFocus(e);
897 for (unsigned int i=0; i < items.size(); i++) {
898 e->addItem(unescape_string(unescape_enriched(
899 utf8_to_wide(items[i]))).c_str());
902 if (str_initial_selection != "")
903 e->setSelected(stoi(str_initial_selection.c_str())-1);
905 m_fields.push_back(spec);
907 m_dropdowns.push_back(std::pair<FieldSpec,
908 std::vector<std::string> >(spec, std::vector<std::string>()));
909 std::vector<std::string> &values = m_dropdowns.back().second;
910 for (unsigned int i = 0; i < items.size(); i++) {
911 values.push_back(unescape_string(items[i]));
916 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
917 << element << "'" << std::endl;
920 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
922 std::vector<std::string> parts = split(element,';');
924 if ((parts.size() == 4) ||
925 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
927 std::vector<std::string> v_pos = split(parts[0],',');
928 std::vector<std::string> v_geom = split(parts[1],',');
929 std::string name = parts[2];
930 std::string label = parts[3];
932 MY_CHECKPOS("pwdfield",0);
933 MY_CHECKGEOM("pwdfield",1);
936 pos.X += stof(v_pos[0]) * (float)spacing.X;
937 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
940 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
942 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
943 pos.Y -= m_btn_height;
944 geom.Y = m_btn_height*2;
946 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
948 std::wstring wlabel = utf8_to_wide(label);
958 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
960 if (spec.fname == data->focused_fieldname) {
961 Environment->setFocus(e);
964 if (label.length() >= 1)
966 int font_height = g_fontengine->getTextHeight();
967 rect.UpperLeftCorner.Y -= font_height;
968 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
969 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
972 e->setPasswordBox(true,L'*');
975 evt.EventType = EET_KEY_INPUT_EVENT;
976 evt.KeyInput.Key = KEY_END;
977 evt.KeyInput.Char = 0;
978 evt.KeyInput.Control = 0;
979 evt.KeyInput.Shift = 0;
980 evt.KeyInput.PressedDown = true;
982 m_fields.push_back(spec);
985 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
988 void GUIFormSpecMenu::parseSimpleField(parserData* data,
989 std::vector<std::string> &parts)
991 std::string name = parts[0];
992 std::string label = parts[1];
993 std::string default_val = parts[2];
995 core::rect<s32> rect;
997 if(data->explicit_size)
998 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
1000 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1001 pos.Y = ((m_fields.size()+2)*60);
1002 v2s32 size = DesiredRect.getSize();
1004 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
1005 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
1009 default_val = m_form_src->resolveText(default_val);
1012 std::wstring wlabel = utf8_to_wide(label);
1017 utf8_to_wide(default_val),
1023 // spec field id to 0, this stops submit searching for a value that isn't there
1024 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1029 gui::IGUIElement *e;
1030 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1031 if (g_settings->getBool("freetype")) {
1032 e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1033 true, Environment, this, spec.fid, rect);
1039 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1041 if (spec.fname == data->focused_fieldname) {
1042 Environment->setFocus(e);
1046 evt.EventType = EET_KEY_INPUT_EVENT;
1047 evt.KeyInput.Key = KEY_END;
1048 evt.KeyInput.Char = 0;
1049 evt.KeyInput.Control = 0;
1050 evt.KeyInput.Shift = 0;
1051 evt.KeyInput.PressedDown = true;
1054 if (label.length() >= 1)
1056 int font_height = g_fontengine->getTextHeight();
1057 rect.UpperLeftCorner.Y -= font_height;
1058 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1059 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1063 m_fields.push_back(spec);
1066 void GUIFormSpecMenu::parseTextArea(parserData* data,
1067 std::vector<std::string>& parts,std::string type)
1070 std::vector<std::string> v_pos = split(parts[0],',');
1071 std::vector<std::string> v_geom = split(parts[1],',');
1072 std::string name = parts[2];
1073 std::string label = parts[3];
1074 std::string default_val = parts[4];
1076 MY_CHECKPOS(type,0);
1077 MY_CHECKGEOM(type,1);
1080 pos.X = stof(v_pos[0]) * (float) spacing.X;
1081 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1085 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1087 if (type == "textarea")
1089 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1090 pos.Y += m_btn_height;
1094 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1095 pos.Y -= m_btn_height;
1096 geom.Y = m_btn_height*2;
1099 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1101 if(!data->explicit_size)
1102 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1105 default_val = m_form_src->resolveText(default_val);
1108 std::wstring wlabel = utf8_to_wide(label);
1113 utf8_to_wide(default_val),
1119 // spec field id to 0, this stops submit searching for a value that isn't there
1120 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1126 gui::IGUIEditBox *e;
1127 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1128 if (g_settings->getBool("freetype")) {
1129 e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1130 true, Environment, this, spec.fid, rect);
1136 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1139 if (spec.fname == data->focused_fieldname) {
1140 Environment->setFocus(e);
1143 if (type == "textarea")
1145 e->setMultiLine(true);
1146 e->setWordWrap(true);
1147 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1150 evt.EventType = EET_KEY_INPUT_EVENT;
1151 evt.KeyInput.Key = KEY_END;
1152 evt.KeyInput.Char = 0;
1153 evt.KeyInput.Control = 0;
1154 evt.KeyInput.Shift = 0;
1155 evt.KeyInput.PressedDown = true;
1159 if (label.length() >= 1)
1161 int font_height = g_fontengine->getTextHeight();
1162 rect.UpperLeftCorner.Y -= font_height;
1163 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1164 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1167 m_fields.push_back(spec);
1170 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1173 std::vector<std::string> parts = split(element,';');
1175 if (parts.size() == 3 || parts.size() == 4) {
1176 parseSimpleField(data,parts);
1180 if ((parts.size() == 5) ||
1181 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1183 parseTextArea(data,parts,type);
1186 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1189 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1191 std::vector<std::string> parts = split(element,';');
1193 if ((parts.size() == 2) ||
1194 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1196 std::vector<std::string> v_pos = split(parts[0],',');
1197 std::string text = parts[1];
1199 MY_CHECKPOS("label",0);
1201 v2s32 pos = padding;
1202 pos.X += stof(v_pos[0]) * (float)spacing.X;
1203 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1205 if(!data->explicit_size)
1206 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1208 std::vector<std::string> lines = split(text, '\n');
1210 for (unsigned int i = 0; i != lines.size(); i++) {
1211 // Lines are spaced at the nominal distance of
1212 // 2/5 inventory slot, even if the font doesn't
1213 // quite match that. This provides consistent
1214 // form layout, at the expense of sometimes
1215 // having sub-optimal spacing for the font.
1216 // We multiply by 2 and then divide by 5, rather
1217 // than multiply by 0.4, to get exact results
1218 // in the integer cases: 0.4 is not exactly
1219 // representable in binary floating point.
1220 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1221 std::wstring wlabel = utf8_to_wide(lines[i]);
1222 core::rect<s32> rect = core::rect<s32>(
1223 pos.X, posy - m_btn_height,
1224 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1225 posy + m_btn_height);
1232 gui::IGUIStaticText *e =
1233 Environment->addStaticText(spec.flabel.c_str(),
1234 rect, false, false, this, spec.fid);
1235 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1237 m_fields.push_back(spec);
1242 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1245 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1247 std::vector<std::string> parts = split(element,';');
1249 if ((parts.size() == 2) ||
1250 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1252 std::vector<std::string> v_pos = split(parts[0],',');
1253 std::wstring text = unescape_string(
1254 unescape_enriched(utf8_to_wide(parts[1])));
1256 MY_CHECKPOS("vertlabel",1);
1258 v2s32 pos = padding;
1259 pos.X += stof(v_pos[0]) * (float)spacing.X;
1260 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1262 core::rect<s32> rect = core::rect<s32>(
1263 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1265 font_line_height(m_font)
1267 +((imgsize.Y/2)- m_btn_height));
1268 //actually text.length() would be correct but adding +1 avoids to break all mods
1270 if(!data->explicit_size)
1271 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1273 std::wstring label = L"";
1275 for (unsigned int i=0; i < text.length(); i++) {
1286 gui::IGUIStaticText *t =
1287 Environment->addStaticText(spec.flabel.c_str(), rect, false, false, this, spec.fid);
1288 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1289 m_fields.push_back(spec);
1292 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1295 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1298 std::vector<std::string> parts = split(element,';');
1300 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1301 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1303 std::vector<std::string> v_pos = split(parts[0],',');
1304 std::vector<std::string> v_geom = split(parts[1],',');
1305 std::string image_name = parts[2];
1306 std::string name = parts[3];
1307 std::string label = parts[4];
1309 MY_CHECKPOS("imagebutton",0);
1310 MY_CHECKGEOM("imagebutton",1);
1312 v2s32 pos = padding;
1313 pos.X += stof(v_pos[0]) * (float)spacing.X;
1314 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1316 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1317 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1319 bool noclip = false;
1320 bool drawborder = true;
1321 std::string pressed_image_name = "";
1323 if (parts.size() >= 7) {
1324 if (parts[5] == "true")
1326 if (parts[6] == "false")
1330 if (parts.size() >= 8) {
1331 pressed_image_name = parts[7];
1334 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1336 if(!data->explicit_size)
1337 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1339 image_name = unescape_string(image_name);
1340 pressed_image_name = unescape_string(pressed_image_name);
1342 std::wstring wlabel = utf8_to_wide(label);
1347 utf8_to_wide(image_name),
1350 spec.ftype = f_Button;
1351 if(type == "image_button_exit")
1352 spec.is_exit = true;
1354 video::ITexture *texture = 0;
1355 video::ITexture *pressed_texture = 0;
1356 texture = m_tsrc->getTexture(image_name);
1357 if (pressed_image_name != "")
1358 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1360 pressed_texture = texture;
1362 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1364 if (spec.fname == data->focused_fieldname) {
1365 Environment->setFocus(e);
1368 e->setUseAlphaChannel(true);
1369 e->setImage(guiScalingImageButton(
1370 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1371 e->setPressedImage(guiScalingImageButton(
1372 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1373 e->setScaleImage(true);
1374 e->setNotClipped(noclip);
1375 e->setDrawBorder(drawborder);
1377 m_fields.push_back(spec);
1381 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1384 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1386 std::vector<std::string> parts = split(element,';');
1388 if (((parts.size() == 4) || (parts.size() == 6)) ||
1389 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1391 std::vector<std::string> v_pos = split(parts[0],',');
1392 std::string name = parts[1];
1393 std::vector<std::string> buttons = split(parts[2],',');
1394 std::string str_index = parts[3];
1395 bool show_background = true;
1396 bool show_border = true;
1397 int tab_index = stoi(str_index) -1;
1399 MY_CHECKPOS("tabheader",0);
1401 if (parts.size() == 6) {
1402 if (parts[4] == "true")
1403 show_background = false;
1404 if (parts[5] == "false")
1405 show_border = false;
1415 spec.ftype = f_TabHeader;
1418 pos.X += stof(v_pos[0]) * (float)spacing.X;
1419 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1421 geom.X = DesiredRect.getWidth();
1422 geom.Y = m_btn_height*2;
1424 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1427 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1428 show_background, show_border, spec.fid);
1429 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1430 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1431 e->setTabHeight(m_btn_height*2);
1433 if (spec.fname == data->focused_fieldname) {
1434 Environment->setFocus(e);
1437 e->setNotClipped(true);
1439 for (unsigned int i = 0; i < buttons.size(); i++) {
1440 e->addTab(unescape_string(unescape_enriched(
1441 utf8_to_wide(buttons[i]))).c_str(), -1);
1444 if ((tab_index >= 0) &&
1445 (buttons.size() < INT_MAX) &&
1446 (tab_index < (int) buttons.size()))
1447 e->setActiveTab(tab_index);
1449 m_fields.push_back(spec);
1452 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1453 << element << "'" << std::endl;
1456 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1459 if (m_gamedef == 0) {
1460 warningstream << "invalid use of item_image_button with m_gamedef==0"
1465 std::vector<std::string> parts = split(element,';');
1467 if ((parts.size() == 5) ||
1468 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1470 std::vector<std::string> v_pos = split(parts[0],',');
1471 std::vector<std::string> v_geom = split(parts[1],',');
1472 std::string item_name = parts[2];
1473 std::string name = parts[3];
1474 std::string label = parts[4];
1476 MY_CHECKPOS("itemimagebutton",0);
1477 MY_CHECKGEOM("itemimagebutton",1);
1479 v2s32 pos = padding;
1480 pos.X += stof(v_pos[0]) * (float)spacing.X;
1481 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1483 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1484 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1486 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1488 if(!data->explicit_size)
1489 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1491 IItemDefManager *idef = m_gamedef->idef();
1493 item.deSerialize(item_name, idef);
1496 TooltipSpec(item.getDefinition(idef).description,
1497 m_default_tooltip_bgcolor,
1498 m_default_tooltip_color);
1502 utf8_to_wide(label),
1503 utf8_to_wide(item_name),
1504 258 + m_fields.size()
1507 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1509 if (spec.fname == data->focused_fieldname) {
1510 Environment->setFocus(e);
1513 spec.ftype = f_Button;
1514 rect+=data->basepos-padding;
1516 m_fields.push_back(spec);
1517 pos = padding + AbsoluteRect.UpperLeftCorner;
1518 pos.X += stof(v_pos[0]) * (float) spacing.X;
1519 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1520 m_itemimages.push_back(ImageDrawSpec("", item_name, e, pos, geom));
1521 m_static_texts.push_back(StaticTextSpec(utf8_to_wide(label), rect, e));
1524 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1527 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1529 std::vector<std::string> parts = split(element,';');
1531 if ((parts.size() == 3) ||
1532 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1534 std::vector<std::string> v_pos = split(parts[0],',');
1535 std::vector<std::string> v_geom = split(parts[1],',');
1537 MY_CHECKPOS("box",0);
1538 MY_CHECKGEOM("box",1);
1540 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1541 pos.X += stof(v_pos[0]) * (float) spacing.X;
1542 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1545 geom.X = stof(v_geom[0]) * (float)spacing.X;
1546 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1548 video::SColor tmp_color;
1550 if (parseColorString(parts[2], tmp_color, false)) {
1551 BoxDrawSpec spec(pos, geom, tmp_color);
1553 m_boxes.push_back(spec);
1556 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1560 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1563 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1565 std::vector<std::string> parts = split(element,';');
1567 if (((parts.size() == 1) || (parts.size() == 2)) ||
1568 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1570 parseColorString(parts[0],m_bgcolor,false);
1572 if (parts.size() == 2) {
1573 std::string fullscreen = parts[1];
1574 m_bgfullscreen = is_yes(fullscreen);
1578 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1581 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1583 std::vector<std::string> parts = split(element,';');
1585 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1586 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1588 parseColorString(parts[0], m_slotbg_n, false);
1589 parseColorString(parts[1], m_slotbg_h, false);
1591 if (parts.size() >= 3) {
1592 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1593 m_slotborder = true;
1596 if (parts.size() == 5) {
1597 video::SColor tmp_color;
1599 if (parseColorString(parts[3], tmp_color, false))
1600 m_default_tooltip_bgcolor = tmp_color;
1601 if (parseColorString(parts[4], tmp_color, false))
1602 m_default_tooltip_color = tmp_color;
1606 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1609 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1611 std::vector<std::string> parts = split(element,';');
1612 if (parts.size() == 2) {
1613 std::string name = parts[0];
1614 m_tooltips[name] = TooltipSpec(parts[1],
1615 m_default_tooltip_bgcolor, m_default_tooltip_color);
1617 } else if (parts.size() == 4) {
1618 std::string name = parts[0];
1619 video::SColor tmp_color1, tmp_color2;
1620 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1621 m_tooltips[name] = TooltipSpec(parts[1],
1622 tmp_color1, tmp_color2);
1626 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1629 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1635 std::vector<std::string> parts = split(data,'[');
1637 if (parts.size() < 2) {
1641 if (parts[0] != "formspec_version") {
1645 if (is_number(parts[1])) {
1646 m_formspec_version = mystoi(parts[1]);
1653 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1658 std::vector<std::string> parts = split(element,'[');
1660 if (parts.size() < 2)
1663 std::string type = trim(parts[0]);
1664 std::string description = trim(parts[1]);
1666 if (type != "size" && type != "invsize")
1669 if (type == "invsize")
1670 log_deprecated("Deprecated formspec element \"invsize\" is used");
1672 parseSize(data, description);
1677 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1683 std::vector<std::string> parts = split(element,'[');
1685 // ugly workaround to keep compatibility
1686 if (parts.size() > 2) {
1687 if (trim(parts[0]) == "image") {
1688 for (unsigned int i=2;i< parts.size(); i++) {
1689 parts[1] += "[" + parts[i];
1695 if (parts.size() < 2) {
1699 std::string type = trim(parts[0]);
1700 std::string description = trim(parts[1]);
1702 if (type == "list") {
1703 parseList(data,description);
1707 if (type == "listring") {
1708 parseListRing(data, description);
1712 if (type == "checkbox") {
1713 parseCheckbox(data,description);
1717 if (type == "image") {
1718 parseImage(data,description);
1722 if (type == "item_image") {
1723 parseItemImage(data,description);
1727 if ((type == "button") || (type == "button_exit")) {
1728 parseButton(data,description,type);
1732 if (type == "background") {
1733 parseBackground(data,description);
1737 if (type == "tableoptions"){
1738 parseTableOptions(data,description);
1742 if (type == "tablecolumns"){
1743 parseTableColumns(data,description);
1747 if (type == "table"){
1748 parseTable(data,description);
1752 if (type == "textlist"){
1753 parseTextList(data,description);
1757 if (type == "dropdown"){
1758 parseDropDown(data,description);
1762 if (type == "pwdfield") {
1763 parsePwdField(data,description);
1767 if ((type == "field") || (type == "textarea")){
1768 parseField(data,description,type);
1772 if (type == "label") {
1773 parseLabel(data,description);
1777 if (type == "vertlabel") {
1778 parseVertLabel(data,description);
1782 if (type == "item_image_button") {
1783 parseItemImageButton(data,description);
1787 if ((type == "image_button") || (type == "image_button_exit")) {
1788 parseImageButton(data,description,type);
1792 if (type == "tabheader") {
1793 parseTabHeader(data,description);
1797 if (type == "box") {
1798 parseBox(data,description);
1802 if (type == "bgcolor") {
1803 parseBackgroundColor(data,description);
1807 if (type == "listcolors") {
1808 parseListColors(data,description);
1812 if (type == "tooltip") {
1813 parseTooltip(data,description);
1817 if (type == "scrollbar") {
1818 parseScrollBar(data, description);
1824 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1828 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1830 /* useless to regenerate without a screensize */
1831 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1838 for (u32 i = 0; i < m_tables.size(); ++i) {
1839 std::string tablename = m_tables[i].first.fname;
1840 GUITable *table = m_tables[i].second;
1841 mydata.table_dyndata[tablename] = table->getDynamicData();
1845 if (!m_focused_element.empty())
1846 mydata.focused_fieldname = m_focused_element;
1849 gui::IGUIElement *focused_element = Environment->getFocus();
1850 if (focused_element && focused_element->getParent() == this) {
1851 s32 focused_id = focused_element->getID();
1852 if (focused_id > 257) {
1853 for (u32 i=0; i<m_fields.size(); i++) {
1854 if (m_fields[i].fid == focused_id) {
1855 mydata.focused_fieldname =
1866 for (u32 i = 0; i < m_tables.size(); ++i) {
1867 GUITable *table = m_tables[i].second;
1871 mydata.size= v2s32(100,100);
1872 mydata.screensize = screensize;
1874 // Base position of contents of form
1875 mydata.basepos = getBasePos();
1877 /* Convert m_init_draw_spec to m_inventorylists */
1879 m_inventorylists.clear();
1881 m_backgrounds.clear();
1882 m_itemimages.clear();
1884 m_checkboxes.clear();
1885 m_scrollbars.clear();
1889 m_inventory_rings.clear();
1890 m_static_texts.clear();
1892 // Set default values (fits old formspec values)
1893 m_bgcolor = video::SColor(140,0,0,0);
1894 m_bgfullscreen = false;
1896 m_slotbg_n = video::SColor(255,128,128,128);
1897 m_slotbg_h = video::SColor(255,192,192,192);
1899 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1900 m_default_tooltip_color = video::SColor(255,255,255,255);
1902 m_slotbordercolor = video::SColor(200,0,0,0);
1903 m_slotborder = false;
1905 m_clipbackground = false;
1908 assert(m_tooltip_element == NULL);
1909 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1910 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1911 m_tooltip_element->enableOverrideColor(true);
1912 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1913 m_tooltip_element->setDrawBackground(true);
1914 m_tooltip_element->setDrawBorder(true);
1915 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1916 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1917 m_tooltip_element->setWordWrap(false);
1918 //we're not parent so no autograb for this one!
1919 m_tooltip_element->grab();
1922 std::vector<std::string> elements = split(m_formspec_string,']');
1925 /* try to read version from first element only */
1926 if (elements.size() >= 1) {
1927 if ( parseVersionDirect(elements[0]) ) {
1932 /* we need size first in order to calculate image scale */
1933 mydata.explicit_size = false;
1934 for (; i< elements.size(); i++) {
1935 if (!parseSizeDirect(&mydata, elements[i])) {
1940 if (mydata.explicit_size) {
1941 // compute scaling for specified form size
1943 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1944 v2u32 delta = current_screensize - m_lockscreensize;
1946 if (current_screensize.Y > m_lockscreensize.Y)
1951 if (current_screensize.X > m_lockscreensize.X)
1956 offset = v2s32(delta.X,delta.Y);
1958 mydata.screensize = m_lockscreensize;
1960 offset = v2s32(0,0);
1963 double gui_scaling = g_settings->getFloat("gui_scaling");
1964 double screen_dpi = porting::getDisplayDensity() * 96;
1968 // In fixed-size mode, inventory image size
1969 // is 0.53 inch multiplied by the gui_scaling
1970 // config parameter. This magic size is chosen
1971 // to make the main menu (15.5 inventory images
1972 // wide, including border) just fit into the
1973 // default window (800 pixels wide) at 96 DPI
1974 // and default scaling (1.00).
1975 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1977 // In variable-size mode, we prefer to make the
1978 // inventory image size 1/15 of screen height,
1979 // multiplied by the gui_scaling config parameter.
1980 // If the preferred size won't fit the whole
1981 // form on the screen, either horizontally or
1982 // vertically, then we scale it down to fit.
1983 // (The magic numbers in the computation of what
1984 // fits arise from the scaling factors in the
1985 // following stanza, including the form border,
1986 // help text space, and 0.1 inventory slot spare.)
1987 // However, a minimum size is also set, that
1988 // the image size can't be less than 0.3 inch
1989 // multiplied by gui_scaling, even if this means
1990 // the form doesn't fit the screen.
1991 double prefer_imgsize = mydata.screensize.Y / 15 *
1993 double fitx_imgsize = mydata.screensize.X /
1994 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1995 double fity_imgsize = mydata.screensize.Y /
1996 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1997 double screen_dpi = porting::getDisplayDensity() * 96;
1998 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1999 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
2000 MYMIN(fitx_imgsize, fity_imgsize)));
2003 // Everything else is scaled in proportion to the
2004 // inventory image size. The inventory slot spacing
2005 // is 5/4 image size horizontally and 15/13 image size
2006 // vertically. The padding around the form (incorporating
2007 // the border of the outer inventory slots) is 3/8
2008 // image size. Font height (baseline to baseline)
2009 // is 2/5 vertical inventory slot spacing, and button
2010 // half-height is 7/8 of font height.
2011 imgsize = v2s32(use_imgsize, use_imgsize);
2012 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2013 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2014 m_btn_height = use_imgsize*15.0/13 * 0.35;
2016 m_font = g_fontengine->getFont();
2018 mydata.size = v2s32(
2019 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2020 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2022 DesiredRect = mydata.rect = core::rect<s32>(
2023 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
2024 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
2025 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
2026 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
2029 // Non-size[] form must consist only of text fields and
2030 // implicit "Proceed" button. Use default font, and
2031 // temporary form size which will be recalculated below.
2032 m_font = g_fontengine->getFont();
2033 m_btn_height = font_line_height(m_font) * 0.875;
2034 DesiredRect = core::rect<s32>(
2035 mydata.screensize.X/2 - 580/2,
2036 mydata.screensize.Y/2 - 300/2,
2037 mydata.screensize.X/2 + 580/2,
2038 mydata.screensize.Y/2 + 300/2
2041 recalculateAbsolutePosition(false);
2042 mydata.basepos = getBasePos();
2043 m_tooltip_element->setOverrideFont(m_font);
2045 gui::IGUISkin* skin = Environment->getSkin();
2046 sanity_check(skin != NULL);
2047 gui::IGUIFont *old_font = skin->getFont();
2048 skin->setFont(m_font);
2050 for (; i< elements.size(); i++) {
2051 parseElement(&mydata, elements[i]);
2054 // If there are fields without explicit size[], add a "Proceed"
2055 // button and adjust size to fit all the fields.
2056 if (m_fields.size() && !mydata.explicit_size) {
2057 mydata.rect = core::rect<s32>(
2058 mydata.screensize.X/2 - 580/2,
2059 mydata.screensize.Y/2 - 300/2,
2060 mydata.screensize.X/2 + 580/2,
2061 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2063 DesiredRect = mydata.rect;
2064 recalculateAbsolutePosition(false);
2065 mydata.basepos = getBasePos();
2068 v2s32 pos = mydata.basepos;
2069 pos.Y = ((m_fields.size()+2)*60);
2071 v2s32 size = DesiredRect.getSize();
2073 core::rect<s32>(size.X/2-70, pos.Y,
2074 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2075 const wchar_t *text = wgettext("Proceed");
2076 Environment->addButton(mydata.rect, this, 257, text);
2082 //set initial focus if parser didn't set it
2083 focused_element = Environment->getFocus();
2084 if (!focused_element
2085 || !isMyChild(focused_element)
2086 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2089 skin->setFont(old_font);
2093 bool GUIFormSpecMenu::getAndroidUIInput()
2095 /* no dialog shown */
2096 if (m_JavaDialogFieldName == "") {
2101 if (porting::getInputDialogState() == -1) {
2105 std::string fieldname = m_JavaDialogFieldName;
2106 m_JavaDialogFieldName = "";
2108 /* no value abort dialog processing */
2109 if (porting::getInputDialogState() != 0) {
2113 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2114 iter != m_fields.end(); ++iter) {
2116 if (iter->fname != fieldname) {
2119 IGUIElement* tochange = getElementFromId(iter->fid);
2121 if (tochange == 0) {
2125 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2129 std::string text = porting::getInputDialogValue();
2131 ((gui::IGUIEditBox*) tochange)->
2132 setText(utf8_to_wide(text).c_str());
2138 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2140 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2142 for(u32 i=0; i<m_inventorylists.size(); i++)
2144 const ListDrawSpec &s = m_inventorylists[i];
2146 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2147 s32 item_i = i + s.start_item_i;
2148 s32 x = (i%s.geom.X) * spacing.X;
2149 s32 y = (i/s.geom.X) * spacing.Y;
2151 core::rect<s32> rect = imgrect + s.pos + p0;
2152 if(rect.isPointInside(p))
2154 return ItemSpec(s.inventoryloc, s.listname, item_i);
2159 return ItemSpec(InventoryLocation(), "", -1);
2162 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
2165 video::IVideoDriver* driver = Environment->getVideoDriver();
2167 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2169 warningstream<<"GUIFormSpecMenu::drawList(): "
2170 <<"The inventory location "
2171 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2175 InventoryList *ilist = inv->getList(s.listname);
2177 warningstream<<"GUIFormSpecMenu::drawList(): "
2178 <<"The inventory list \""<<s.listname<<"\" @ \""
2179 <<s.inventoryloc.dump()<<"\" doesn't exist"
2184 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2186 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2188 s32 item_i = i + s.start_item_i;
2189 if(item_i >= (s32) ilist->getSize())
2191 s32 x = (i%s.geom.X) * spacing.X;
2192 s32 y = (i/s.geom.X) * spacing.Y;
2194 core::rect<s32> rect = imgrect + s.pos + p;
2197 item = ilist->getItem(item_i);
2199 bool selected = m_selected_item
2200 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2201 && m_selected_item->listname == s.listname
2202 && m_selected_item->i == item_i;
2203 bool hovering = rect.isPointInside(m_pointer);
2204 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2205 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2209 item_hovered = true;
2210 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2212 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2216 //Draw inv slot borders
2218 s32 x1 = rect.UpperLeftCorner.X;
2219 s32 y1 = rect.UpperLeftCorner.Y;
2220 s32 x2 = rect.LowerRightCorner.X;
2221 s32 y2 = rect.LowerRightCorner.Y;
2223 driver->draw2DRectangle(m_slotbordercolor,
2224 core::rect<s32>(v2s32(x1 - border, y1 - border),
2225 v2s32(x2 + border, y1)), NULL);
2226 driver->draw2DRectangle(m_slotbordercolor,
2227 core::rect<s32>(v2s32(x1 - border, y2),
2228 v2s32(x2 + border, y2 + border)), NULL);
2229 driver->draw2DRectangle(m_slotbordercolor,
2230 core::rect<s32>(v2s32(x1 - border, y1),
2231 v2s32(x1, y2)), NULL);
2232 driver->draw2DRectangle(m_slotbordercolor,
2233 core::rect<s32>(v2s32(x2, y1),
2234 v2s32(x2 + border, y2)), NULL);
2242 item.takeItem(m_selected_amount);
2246 drawItemStack(driver, m_font, item,
2247 rect, &AbsoluteClippingRect, m_gamedef,
2252 std::wstring tooltip_text = L"";
2253 if (hovering && !m_selected_item) {
2254 tooltip_text = utf8_to_wide(item.getDefinition(m_gamedef->idef()).description);
2255 tooltip_text = unescape_enriched(tooltip_text);
2257 if (tooltip_text != L"") {
2258 std::vector<std::wstring> tt_rows = str_split(tooltip_text, L'\n');
2259 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2260 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2261 m_tooltip_element->setVisible(true);
2262 this->bringToFront(m_tooltip_element);
2263 m_tooltip_element->setText(tooltip_text.c_str());
2264 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2265 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2266 v2u32 screenSize = driver->getScreenSize();
2267 int tooltip_offset_x = m_btn_height;
2268 int tooltip_offset_y = m_btn_height;
2270 tooltip_offset_x *= 3;
2271 tooltip_offset_y = 0;
2272 if (m_pointer.X > (s32)screenSize.X / 2)
2273 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2275 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2276 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2277 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2278 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2279 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2280 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2281 m_tooltip_element->setRelativePosition(core::rect<s32>(
2282 core::position2d<s32>(tooltip_x, tooltip_y),
2283 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2289 void GUIFormSpecMenu::drawSelectedItem()
2291 video::IVideoDriver* driver = Environment->getVideoDriver();
2293 if (!m_selected_item) {
2294 drawItemStack(driver, m_font, ItemStack(),
2295 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2296 NULL, m_gamedef, IT_ROT_DRAGGED);
2300 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2302 InventoryList *list = inv->getList(m_selected_item->listname);
2304 ItemStack stack = list->getItem(m_selected_item->i);
2305 stack.count = m_selected_amount;
2307 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2308 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2309 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, IT_ROT_DRAGGED);
2312 void GUIFormSpecMenu::drawMenu()
2315 std::string newform = m_form_src->getForm();
2316 if(newform != m_formspec_string){
2317 m_formspec_string = newform;
2318 regenerateGui(m_screensize_old);
2322 gui::IGUISkin* skin = Environment->getSkin();
2323 sanity_check(skin != NULL);
2324 gui::IGUIFont *old_font = skin->getFont();
2325 skin->setFont(m_font);
2327 updateSelectedItem();
2329 video::IVideoDriver* driver = Environment->getVideoDriver();
2331 v2u32 screenSize = driver->getScreenSize();
2332 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2334 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2336 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2338 m_tooltip_element->setVisible(false);
2343 for(u32 i=0; i<m_backgrounds.size(); i++)
2345 const ImageDrawSpec &spec = m_backgrounds[i];
2346 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2349 // Image size on screen
2350 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2351 // Image rectangle on screen
2352 core::rect<s32> rect = imgrect + spec.pos;
2354 if (m_clipbackground) {
2355 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2356 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2357 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2358 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2359 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2362 const video::SColor color(255,255,255,255);
2363 const video::SColor colors[] = {color,color,color,color};
2364 draw2DImageFilterScaled(driver, texture, rect,
2365 core::rect<s32>(core::position2d<s32>(0,0),
2366 core::dimension2di(texture->getOriginalSize())),
2367 NULL/*&AbsoluteClippingRect*/, colors, true);
2370 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2371 errorstream << "\t" << spec.name << std::endl;
2378 for(u32 i=0; i<m_boxes.size(); i++)
2380 const BoxDrawSpec &spec = m_boxes[i];
2382 irr::video::SColor todraw = spec.color;
2384 todraw.setAlpha(140);
2386 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2387 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2389 driver->draw2DRectangle(todraw, rect, 0);
2395 gui::IGUIElement::draw();
2400 for(u32 i=0; i<m_images.size(); i++)
2402 const ImageDrawSpec &spec = m_images[i];
2403 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2406 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2407 // Image size on screen
2408 core::rect<s32> imgrect;
2411 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2414 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2416 // Image rectangle on screen
2417 core::rect<s32> rect = imgrect + spec.pos;
2418 const video::SColor color(255,255,255,255);
2419 const video::SColor colors[] = {color,color,color,color};
2420 draw2DImageFilterScaled(driver, texture, rect,
2421 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2422 NULL/*&AbsoluteClippingRect*/, colors, true);
2425 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2426 errorstream << "\t" << spec.name << std::endl;
2433 for(u32 i=0; i<m_itemimages.size(); i++)
2438 const ImageDrawSpec &spec = m_itemimages[i];
2439 IItemDefManager *idef = m_gamedef->idef();
2441 item.deSerialize(spec.item_name, idef);
2442 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2443 // Viewport rectangle on screen
2444 core::rect<s32> rect = imgrect + spec.pos;
2445 if (spec.parent_button && spec.parent_button->isPressed()) {
2446 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2447 rect += core::dimension2d<s32>(
2448 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2450 rect += core::dimension2d<s32>(
2451 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2452 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2455 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2456 m_gamedef, IT_ROT_NONE);
2461 Phase 0: Item slot rectangles
2462 Phase 1: Item images; prepare tooltip
2464 bool item_hovered = false;
2465 int start_phase = 0;
2466 for (int phase = start_phase; phase <= 1; phase++) {
2467 for (u32 i = 0; i < m_inventorylists.size(); i++) {
2468 drawList(m_inventorylists[i], phase, item_hovered);
2471 if (!item_hovered) {
2472 drawItemStack(driver, m_font, ItemStack(),
2473 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2474 NULL, m_gamedef, IT_ROT_HOVERED);
2477 /* TODO find way to show tooltips on touchscreen */
2478 #ifndef HAVE_TOUCHSCREENGUI
2479 m_pointer = m_device->getCursorControl()->getPosition();
2483 Draw static text elements
2485 for (u32 i = 0; i < m_static_texts.size(); i++) {
2486 const StaticTextSpec &spec = m_static_texts[i];
2487 core::rect<s32> rect = spec.rect;
2488 if (spec.parent_button && spec.parent_button->isPressed()) {
2489 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2490 rect += core::dimension2d<s32>(
2491 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2493 // Use image offset instead of text's because its a bit smaller
2494 // and fits better, also TEXT_OFFSET_X is always 0
2495 rect += core::dimension2d<s32>(
2496 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2497 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2500 video::SColor color(255, 255, 255, 255);
2501 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
2505 Draw fields/buttons tooltips
2507 gui::IGUIElement *hovered =
2508 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2510 if (hovered != NULL) {
2511 s32 id = hovered->getID();
2515 m_old_tooltip_id = id;
2516 m_old_tooltip = L"";
2518 if (id == m_old_tooltip_id) {
2519 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2521 m_hovered_time = getTimeMs();
2522 m_old_tooltip_id = id;
2526 if (id != -1 && delta >= m_tooltip_show_delay) {
2527 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2528 iter != m_fields.end(); ++iter) {
2529 if (iter->fid == id && m_tooltips[iter->fname].tooltip != L"") {
2530 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2531 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2532 m_tooltip_element->setText(m_tooltips[iter->fname].tooltip.c_str());
2533 std::vector<std::wstring> tt_rows = str_split(m_tooltips[iter->fname].tooltip, L'\n');
2534 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2535 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2536 int tooltip_offset_x = m_btn_height;
2537 int tooltip_offset_y = m_btn_height;
2539 tooltip_offset_x *= 3;
2540 tooltip_offset_y = 0;
2541 if (m_pointer.X > (s32)screenSize.X / 2)
2542 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2544 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2545 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2546 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2547 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2548 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2549 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2550 m_tooltip_element->setRelativePosition(core::rect<s32>(
2551 core::position2d<s32>(tooltip_x, tooltip_y),
2552 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2554 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2555 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2556 m_tooltip_element->setVisible(true);
2557 this->bringToFront(m_tooltip_element);
2565 Draw dragged item stack
2569 skin->setFont(old_font);
2572 void GUIFormSpecMenu::updateSelectedItem()
2574 // If the selected stack has become empty for some reason, deselect it.
2575 // If the selected stack has become inaccessible, deselect it.
2576 // If the selected stack has become smaller, adjust m_selected_amount.
2577 ItemStack selected = verifySelectedItem();
2579 // WARNING: BLACK MAGIC
2580 // See if there is a stack suited for our current guess.
2581 // If such stack does not exist, clear the guess.
2582 if(m_selected_content_guess.name != "" &&
2583 selected.name == m_selected_content_guess.name &&
2584 selected.count == m_selected_content_guess.count){
2585 // Selected item fits the guess. Skip the black magic.
2587 else if(m_selected_content_guess.name != ""){
2589 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2590 const ListDrawSpec &s = m_inventorylists[i];
2591 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2594 InventoryList *list = inv->getList(s.listname);
2597 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2598 u32 item_i = i + s.start_item_i;
2599 if(item_i >= list->getSize())
2601 ItemStack stack = list->getItem(item_i);
2602 if(stack.name == m_selected_content_guess.name &&
2603 stack.count == m_selected_content_guess.count){
2605 infostream<<"Client: Changing selected content guess to "
2606 <<s.inventoryloc.dump()<<" "<<s.listname
2607 <<" "<<item_i<<std::endl;
2608 delete m_selected_item;
2609 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2610 m_selected_amount = stack.count;
2615 infostream<<"Client: Discarding selected content guess: "
2616 <<m_selected_content_guess.getItemString()<<std::endl;
2617 m_selected_content_guess.name = "";
2621 // If craftresult is nonempty and nothing else is selected, select it now.
2622 if(!m_selected_item)
2624 for(u32 i=0; i<m_inventorylists.size(); i++)
2626 const ListDrawSpec &s = m_inventorylists[i];
2627 if(s.listname == "craftpreview")
2629 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2630 InventoryList *list = inv->getList("craftresult");
2631 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2633 m_selected_item = new ItemSpec;
2634 m_selected_item->inventoryloc = s.inventoryloc;
2635 m_selected_item->listname = "craftresult";
2636 m_selected_item->i = 0;
2637 m_selected_amount = 0;
2638 m_selected_dragging = false;
2645 // If craftresult is selected, keep the whole stack selected
2646 if(m_selected_item && m_selected_item->listname == "craftresult")
2648 m_selected_amount = verifySelectedItem().count;
2652 ItemStack GUIFormSpecMenu::verifySelectedItem()
2654 // If the selected stack has become empty for some reason, deselect it.
2655 // If the selected stack has become inaccessible, deselect it.
2656 // If the selected stack has become smaller, adjust m_selected_amount.
2657 // Return the selected stack.
2661 if(m_selected_item->isValid())
2663 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2666 InventoryList *list = inv->getList(m_selected_item->listname);
2667 if(list && (u32) m_selected_item->i < list->getSize())
2669 ItemStack stack = list->getItem(m_selected_item->i);
2670 if(m_selected_amount > stack.count)
2671 m_selected_amount = stack.count;
2678 // selection was not valid
2679 delete m_selected_item;
2680 m_selected_item = NULL;
2681 m_selected_amount = 0;
2682 m_selected_dragging = false;
2687 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2693 if (quitmode == quit_mode_accept) {
2694 fields["quit"] = "true";
2697 if (quitmode == quit_mode_cancel) {
2698 fields["quit"] = "true";
2699 m_text_dst->gotText(fields);
2703 if (current_keys_pending.key_down) {
2704 fields["key_down"] = "true";
2705 current_keys_pending.key_down = false;
2708 if (current_keys_pending.key_up) {
2709 fields["key_up"] = "true";
2710 current_keys_pending.key_up = false;
2713 if (current_keys_pending.key_enter) {
2714 fields["key_enter"] = "true";
2715 current_keys_pending.key_enter = false;
2718 if (current_keys_pending.key_escape) {
2719 fields["key_escape"] = "true";
2720 current_keys_pending.key_escape = false;
2723 for(unsigned int i=0; i<m_fields.size(); i++) {
2724 const FieldSpec &s = m_fields[i];
2726 std::string name = s.fname;
2727 if (s.ftype == f_Button) {
2728 fields[name] = wide_to_utf8(s.flabel);
2729 } else if (s.ftype == f_Table) {
2730 GUITable *table = getTable(s.fname);
2732 fields[name] = table->checkEvent();
2735 else if(s.ftype == f_DropDown) {
2736 // no dynamic cast possible due to some distributions shipped
2737 // without rtti support in irrlicht
2738 IGUIElement * element = getElementFromId(s.fid);
2739 gui::IGUIComboBox *e = NULL;
2740 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2741 e = static_cast<gui::IGUIComboBox*>(element);
2743 s32 selected = e->getSelected();
2744 if (selected >= 0) {
2745 std::vector<std::string> *dropdown_values =
2746 getDropDownValues(s.fname);
2747 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2748 fields[name] = (*dropdown_values)[selected];
2752 else if (s.ftype == f_TabHeader) {
2753 // no dynamic cast possible due to some distributions shipped
2754 // without rtti support in irrlicht
2755 IGUIElement * element = getElementFromId(s.fid);
2756 gui::IGUITabControl *e = NULL;
2757 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2758 e = static_cast<gui::IGUITabControl*>(element);
2762 std::stringstream ss;
2763 ss << (e->getActiveTab() +1);
2764 fields[name] = ss.str();
2767 else if (s.ftype == f_CheckBox) {
2768 // no dynamic cast possible due to some distributions shipped
2769 // without rtti support in irrlicht
2770 IGUIElement * element = getElementFromId(s.fid);
2771 gui::IGUICheckBox *e = NULL;
2772 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2773 e = static_cast<gui::IGUICheckBox*>(element);
2778 fields[name] = "true";
2780 fields[name] = "false";
2783 else if (s.ftype == f_ScrollBar) {
2784 // no dynamic cast possible due to some distributions shipped
2785 // without rtti support in irrlicht
2786 IGUIElement * element = getElementFromId(s.fid);
2787 gui::IGUIScrollBar *e = NULL;
2788 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2789 e = static_cast<gui::IGUIScrollBar*>(element);
2793 std::stringstream os;
2795 if (s.fdefault == L"Changed")
2796 fields[name] = "CHG:" + os.str();
2798 fields[name] = "VAL:" + os.str();
2803 IGUIElement* e = getElementFromId(s.fid);
2805 fields[name] = wide_to_utf8(e->getText());
2811 m_text_dst->gotText(fields);
2815 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2817 while(tocheck != NULL) {
2818 if (tocheck == parent) {
2821 tocheck = tocheck->getParent();
2826 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2828 // The IGUITabControl renders visually using the skin's selected
2829 // font, which we override for the duration of form drawing,
2830 // but computes tab hotspots based on how it would have rendered
2831 // using the font that is selected at the time of button release.
2832 // To make these two consistent, temporarily override the skin's
2833 // font while the IGUITabControl is processing the event.
2834 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2835 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2836 s32 x = event.MouseInput.X;
2837 s32 y = event.MouseInput.Y;
2838 gui::IGUIElement *hovered =
2839 Environment->getRootGUIElement()->getElementFromPoint(
2840 core::position2d<s32>(x, y));
2841 if (hovered && isMyChild(hovered) &&
2842 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2843 gui::IGUISkin* skin = Environment->getSkin();
2844 sanity_check(skin != NULL);
2845 gui::IGUIFont *old_font = skin->getFont();
2846 skin->setFont(m_font);
2847 bool retval = hovered->OnEvent(event);
2848 skin->setFont(old_font);
2853 // Fix Esc/Return key being eaten by checkboxen and tables
2854 if(event.EventType==EET_KEY_INPUT_EVENT) {
2855 KeyPress kp(event.KeyInput);
2856 if (kp == EscapeKey || kp == CancelKey
2857 || kp == getKeySetting("keymap_inventory")
2858 || event.KeyInput.Key==KEY_RETURN) {
2859 gui::IGUIElement *focused = Environment->getFocus();
2860 if (focused && isMyChild(focused) &&
2861 (focused->getType() == gui::EGUIET_LIST_BOX ||
2862 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2868 // Mouse wheel events: send to hovered element instead of focused
2869 if(event.EventType==EET_MOUSE_INPUT_EVENT
2870 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2871 s32 x = event.MouseInput.X;
2872 s32 y = event.MouseInput.Y;
2873 gui::IGUIElement *hovered =
2874 Environment->getRootGUIElement()->getElementFromPoint(
2875 core::position2d<s32>(x, y));
2876 if (hovered && isMyChild(hovered)) {
2877 hovered->OnEvent(event);
2882 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2883 s32 x = event.MouseInput.X;
2884 s32 y = event.MouseInput.Y;
2885 gui::IGUIElement *hovered =
2886 Environment->getRootGUIElement()->getElementFromPoint(
2887 core::position2d<s32>(x, y));
2888 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2889 m_old_tooltip_id = -1;
2890 m_old_tooltip = L"";
2892 if (!isChild(hovered,this)) {
2893 if (DoubleClickDetection(event)) {
2900 // display software keyboard when clicking edit boxes
2901 if (event.EventType == EET_MOUSE_INPUT_EVENT
2902 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2903 gui::IGUIElement *hovered =
2904 Environment->getRootGUIElement()->getElementFromPoint(
2905 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2906 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2907 bool retval = hovered->OnEvent(event);
2909 Environment->setFocus(hovered);
2911 m_JavaDialogFieldName = getNameByID(hovered->getID());
2912 std::string message = gettext("Enter ");
2913 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
2917 message += gettext(label) + ":";
2919 /* single line text input */
2922 /* multi line text input */
2923 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2927 /* passwords are always single line */
2928 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2932 porting::showInputDialog(gettext("ok"), "",
2933 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
2939 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2942 memset(&translated, 0, sizeof(SEvent));
2943 translated.EventType = EET_MOUSE_INPUT_EVENT;
2944 gui::IGUIElement* root = Environment->getRootGUIElement();
2948 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2952 gui::IGUIElement* hovered = root->getElementFromPoint(
2953 core::position2d<s32>(
2955 event.TouchInput.Y));
2957 translated.MouseInput.X = event.TouchInput.X;
2958 translated.MouseInput.Y = event.TouchInput.Y;
2959 translated.MouseInput.Control = false;
2961 bool dont_send_event = false;
2963 if (event.TouchInput.touchedCount == 1) {
2964 switch (event.TouchInput.Event) {
2965 case ETIE_PRESSED_DOWN:
2966 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2967 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2968 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2969 m_down_pos = m_pointer;
2972 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2973 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2974 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2977 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2978 translated.MouseInput.ButtonStates = 0;
2979 hovered = root->getElementFromPoint(m_down_pos);
2980 /* we don't have a valid pointer element use last
2981 * known pointer pos */
2982 translated.MouseInput.X = m_pointer.X;
2983 translated.MouseInput.Y = m_pointer.Y;
2985 /* reset down pos */
2986 m_down_pos = v2s32(0,0);
2989 dont_send_event = true;
2990 //this is not supposed to happen
2992 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2993 << event.TouchInput.Event << std::endl;
2995 } else if ( (event.TouchInput.touchedCount == 2) &&
2996 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2997 hovered = root->getElementFromPoint(m_down_pos);
2999 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
3000 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
3001 translated.MouseInput.X = m_pointer.X;
3002 translated.MouseInput.Y = m_pointer.Y;
3005 hovered->OnEvent(translated);
3008 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
3009 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3013 hovered->OnEvent(translated);
3015 dont_send_event = true;
3017 /* ignore unhandled 2 touch events ... accidental moving for example */
3018 else if (event.TouchInput.touchedCount == 2) {
3019 dont_send_event = true;
3021 else if (event.TouchInput.touchedCount > 2) {
3023 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
3024 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3027 if (dont_send_event) {
3031 /* check if translated event needs to be preprocessed again */
3032 if (preprocessEvent(translated)) {
3037 bool retval = hovered->OnEvent(translated);
3039 if (event.TouchInput.Event == ETIE_LEFT_UP) {
3041 m_pointer = v2s32(0,0);
3052 /******************************************************************************/
3053 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3055 /* The following code is for capturing double-clicks of the mouse button
3056 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3057 * -- which closes the form -- under some circumstances.
3059 * There have been many github issues reporting this as a bug even though it
3060 * was an intended feature. For this reason, remapping the double-click as
3061 * an ESC must be explicitly set when creating this class via the
3062 * /p remap_dbl_click parameter of the constructor.
3065 if (!m_remap_dbl_click)
3068 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3069 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3070 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3072 m_doubleclickdetect[1].pos = m_pointer;
3073 m_doubleclickdetect[1].time = getTimeMs();
3075 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3076 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
3081 double squaredistance =
3082 m_doubleclickdetect[0].pos
3083 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3085 if (squaredistance > (30*30)) {
3089 SEvent* translated = new SEvent();
3090 assert(translated != 0);
3091 //translate doubleclick to escape
3092 memset(translated, 0, sizeof(SEvent));
3093 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3094 translated->KeyInput.Key = KEY_ESCAPE;
3095 translated->KeyInput.Control = false;
3096 translated->KeyInput.Shift = false;
3097 translated->KeyInput.PressedDown = true;
3098 translated->KeyInput.Char = 0;
3099 OnEvent(*translated);
3101 // no need to send the key up event as we're already deleted
3102 // and no one else did notice this event
3110 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3112 if (event.EventType==EET_KEY_INPUT_EVENT) {
3113 KeyPress kp(event.KeyInput);
3114 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3115 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3118 acceptInput(quit_mode_cancel);
3121 m_text_dst->gotText(L"MenuQuit");
3124 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3125 (kp == getKeySetting("keymap_screenshot"))) {
3126 m_client->makeScreenshot(m_device);
3128 if (event.KeyInput.PressedDown &&
3129 (event.KeyInput.Key==KEY_RETURN ||
3130 event.KeyInput.Key==KEY_UP ||
3131 event.KeyInput.Key==KEY_DOWN)
3133 switch (event.KeyInput.Key) {
3135 current_keys_pending.key_enter = true;
3138 current_keys_pending.key_up = true;
3141 current_keys_pending.key_down = true;
3145 //can't happen at all!
3146 FATAL_ERROR("Reached a source line that can't ever been reached");
3149 if (current_keys_pending.key_enter && m_allowclose) {
3150 acceptInput(quit_mode_accept);
3160 /* Mouse event other than movement, or crossing the border of inventory
3161 field while holding right mouse button
3163 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3164 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3165 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3166 event.MouseInput.isRightPressed() &&
3167 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3169 // Get selected item and hovered/clicked item (s)
3171 m_old_tooltip_id = -1;
3172 updateSelectedItem();
3173 ItemSpec s = getItemAtPos(m_pointer);
3175 Inventory *inv_selected = NULL;
3176 Inventory *inv_s = NULL;
3177 InventoryList *list_s = NULL;
3179 if (m_selected_item) {
3180 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3181 sanity_check(inv_selected);
3182 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3189 inv_s = m_invmgr->getInventory(s.inventoryloc);
3192 errorstream << "InventoryMenu: The selected inventory location "
3193 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3195 s.i = -1; // make it invalid again
3199 list_s = inv_s->getList(s.listname);
3200 if (list_s == NULL) {
3201 verbosestream << "InventoryMenu: The selected inventory list \""
3202 << s.listname << "\" does not exist" << std::endl;
3203 s.i = -1; // make it invalid again
3207 if ((u32)s.i >= list_s->getSize()) {
3208 infostream << "InventoryMenu: The selected inventory list \""
3209 << s.listname << "\" is too small (i=" << s.i << ", size="
3210 << list_s->getSize() << ")" << std::endl;
3211 s.i = -1; // make it invalid again
3215 s_count = list_s->getItem(s.i).count;
3218 bool identical = (m_selected_item != NULL) && s.isValid() &&
3219 (inv_selected == inv_s) &&
3220 (m_selected_item->listname == s.listname) &&
3221 (m_selected_item->i == s.i);
3223 // buttons: 0 = left, 1 = right, 2 = middle
3224 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3227 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3228 { button = 0; updown = 0; }
3229 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3230 { button = 1; updown = 0; }
3231 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3232 { button = 2; updown = 0; }
3233 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3234 { button = 0; updown = 1; }
3235 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3236 { button = 1; updown = 1; }
3237 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3238 { button = 2; updown = 1; }
3239 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3242 // Set this number to a positive value to generate a move action
3243 // from m_selected_item to s.
3244 u32 move_amount = 0;
3246 // Set this number to a positive value to generate a move action
3247 // from s to the next inventory ring.
3248 u32 shift_move_amount = 0;
3250 // Set this number to a positive value to generate a drop action
3251 // from m_selected_item.
3252 u32 drop_amount = 0;
3254 // Set this number to a positive value to generate a craft action at s.
3255 u32 craft_amount = 0;
3258 // Some mouse button has been pressed
3260 //infostream<<"Mouse button "<<button<<" pressed at p=("
3261 // <<p.X<<","<<p.Y<<")"<<std::endl;
3263 m_selected_dragging = false;
3265 if (s.isValid() && s.listname == "craftpreview") {
3266 // Craft preview has been clicked: craft
3267 craft_amount = (button == 2 ? 10 : 1);
3268 } else if (m_selected_item == NULL) {
3270 // Non-empty stack has been clicked: select or shift-move it
3271 m_selected_item = new ItemSpec(s);
3274 if (button == 1) // right
3275 count = (s_count + 1) / 2;
3276 else if (button == 2) // middle
3277 count = MYMIN(s_count, 10);
3281 if (!event.MouseInput.Shift) {
3282 // no shift: select item
3283 m_selected_amount = count;
3284 m_selected_dragging = true;
3285 m_rmouse_auto_place = false;
3287 // shift pressed: move item
3289 shift_move_amount = count;
3290 else // count of 1 at left click like after drag & drop
3291 shift_move_amount = 1;
3294 } else { // m_selected_item != NULL
3295 assert(m_selected_amount >= 1);
3298 // Clicked a slot: move
3299 if (button == 1) // right
3301 else if (button == 2) // middle
3302 move_amount = MYMIN(m_selected_amount, 10);
3304 move_amount = m_selected_amount;
3307 if (move_amount >= m_selected_amount)
3308 m_selected_amount = 0;
3310 m_selected_amount -= move_amount;
3314 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3315 // Clicked outside of the window: drop
3316 if (button == 1) // right
3318 else if (button == 2) // middle
3319 drop_amount = MYMIN(m_selected_amount, 10);
3321 drop_amount = m_selected_amount;
3325 else if (updown == 1) {
3326 // Some mouse button has been released
3328 //infostream<<"Mouse button "<<button<<" released at p=("
3329 // <<p.X<<","<<p.Y<<")"<<std::endl;
3331 if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3333 // Dragged to different slot: move all selected
3334 move_amount = m_selected_amount;
3336 } else if (m_selected_item != NULL && m_selected_dragging &&
3337 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3338 // Dragged outside of window: drop all selected
3339 drop_amount = m_selected_amount;
3342 m_selected_dragging = false;
3343 // Keep count of how many times right mouse button has been
3344 // clicked. One click is drag without dropping. Click + release
3345 // + click changes to drop one item when moved mode
3346 if (button == 1 && m_selected_item != NULL)
3347 m_rmouse_auto_place = !m_rmouse_auto_place;
3348 } else if (updown == -1) {
3349 // Mouse has been moved and rmb is down and mouse pointer just
3350 // entered a new inventory field (checked in the entry-if, this
3351 // is the only action here that is generated by mouse movement)
3352 if (m_selected_item != NULL && s.isValid()) {
3354 // TODO: middle mouse to move 10 items might be handy
3355 if (m_rmouse_auto_place) {
3356 // Only move an item if the destination slot is empty
3357 // or contains the same item type as what is going to be
3359 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3360 InventoryList *list_to = list_s;
3361 assert(list_from && list_to);
3362 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3363 ItemStack stack_to = list_to->getItem(s.i);
3364 if (stack_to.empty() || stack_to.name == stack_from.name)
3370 // Possibly send inventory action to server
3371 if (move_amount > 0) {
3372 // Send IACTION_MOVE
3374 assert(m_selected_item && m_selected_item->isValid());
3375 assert(s.isValid());
3377 assert(inv_selected && inv_s);
3378 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3379 InventoryList *list_to = list_s;
3380 assert(list_from && list_to);
3381 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3382 ItemStack stack_to = list_to->getItem(s.i);
3384 // Check how many items can be moved
3385 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3386 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3387 // If source stack cannot be added to destination stack at all,
3389 if ((leftover.count == stack_from.count) &&
3390 (leftover.name == stack_from.name)) {
3391 m_selected_amount = stack_to.count;
3392 // In case the server doesn't directly swap them but instead
3393 // moves stack_to somewhere else, set this
3394 m_selected_content_guess = stack_to;
3395 m_selected_content_guess_inventory = s.inventoryloc;
3397 // Source stack goes fully into destination stack
3398 else if (leftover.empty()) {
3399 m_selected_amount -= move_amount;
3400 m_selected_content_guess = ItemStack(); // Clear
3402 // Source stack goes partly into destination stack
3404 move_amount -= leftover.count;
3405 m_selected_amount -= move_amount;
3406 m_selected_content_guess = ItemStack(); // Clear
3409 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3410 IMoveAction *a = new IMoveAction();
3411 a->count = move_amount;
3412 a->from_inv = m_selected_item->inventoryloc;
3413 a->from_list = m_selected_item->listname;
3414 a->from_i = m_selected_item->i;
3415 a->to_inv = s.inventoryloc;
3416 a->to_list = s.listname;
3418 m_invmgr->inventoryAction(a);
3419 } else if (shift_move_amount > 0) {
3420 u32 mis = m_inventory_rings.size();
3422 for (; i < mis; i++) {
3423 const ListRingSpec &sp = m_inventory_rings[i];
3424 if (sp.inventoryloc == s.inventoryloc
3425 && sp.listname == s.listname)
3429 if (i >= mis) // if not found
3431 u32 to_inv_ind = (i + 1) % mis;
3432 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3433 InventoryList *list_from = list_s;
3436 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3439 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3442 ItemStack stack_from = list_from->getItem(s.i);
3443 assert(shift_move_amount <= stack_from.count);
3444 if (m_client->getProtoVersion() >= 25) {
3445 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3446 IMoveAction *a = new IMoveAction();
3447 a->count = shift_move_amount;
3448 a->from_inv = s.inventoryloc;
3449 a->from_list = s.listname;
3451 a->to_inv = to_inv_sp.inventoryloc;
3452 a->to_list = to_inv_sp.listname;
3453 a->move_somewhere = true;
3454 m_invmgr->inventoryAction(a);
3456 // find a place (or more than one) to add the new item
3457 u32 ilt_size = list_to->getSize();
3459 for (u32 slot_to = 0; slot_to < ilt_size
3460 && shift_move_amount > 0; slot_to++) {
3461 list_to->itemFits(slot_to, stack_from, &leftover);
3462 if (leftover.count < stack_from.count) {
3463 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3464 IMoveAction *a = new IMoveAction();
3465 a->count = MYMIN(shift_move_amount,
3466 (u32) (stack_from.count - leftover.count));
3467 shift_move_amount -= a->count;
3468 a->from_inv = s.inventoryloc;
3469 a->from_list = s.listname;
3471 a->to_inv = to_inv_sp.inventoryloc;
3472 a->to_list = to_inv_sp.listname;
3474 m_invmgr->inventoryAction(a);
3475 stack_from = leftover;
3480 } else if (drop_amount > 0) {
3481 m_selected_content_guess = ItemStack(); // Clear
3483 // Send IACTION_DROP
3485 assert(m_selected_item && m_selected_item->isValid());
3486 assert(inv_selected);
3487 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3489 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3491 // Check how many items can be dropped
3492 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3493 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3494 m_selected_amount -= drop_amount;
3496 infostream << "Handing IACTION_DROP to manager" << std::endl;
3497 IDropAction *a = new IDropAction();
3498 a->count = drop_amount;
3499 a->from_inv = m_selected_item->inventoryloc;
3500 a->from_list = m_selected_item->listname;
3501 a->from_i = m_selected_item->i;
3502 m_invmgr->inventoryAction(a);
3503 } else if (craft_amount > 0) {
3504 m_selected_content_guess = ItemStack(); // Clear
3506 // Send IACTION_CRAFT
3508 assert(s.isValid());
3511 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3512 ICraftAction *a = new ICraftAction();
3513 a->count = craft_amount;
3514 a->craft_inv = s.inventoryloc;
3515 m_invmgr->inventoryAction(a);
3518 // If m_selected_amount has been decreased to zero, deselect
3519 if (m_selected_amount == 0) {
3520 delete m_selected_item;
3521 m_selected_item = NULL;
3522 m_selected_amount = 0;
3523 m_selected_dragging = false;
3524 m_selected_content_guess = ItemStack();
3526 m_old_pointer = m_pointer;
3528 if (event.EventType == EET_GUI_EVENT) {
3530 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3532 // find the element that was clicked
3533 for (unsigned int i=0; i<m_fields.size(); i++) {
3534 FieldSpec &s = m_fields[i];
3535 if ((s.ftype == f_TabHeader) &&
3536 (s.fid == event.GUIEvent.Caller->getID())) {
3544 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3546 if (!canTakeFocus(event.GUIEvent.Element)) {
3547 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3549 // Returning true disables focus change
3553 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3554 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3555 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3556 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3557 unsigned int btn_id = event.GUIEvent.Caller->getID();
3559 if (btn_id == 257) {
3561 acceptInput(quit_mode_accept);
3565 m_text_dst->gotText(L"ExitButton");
3567 // quitMenu deallocates menu
3571 // find the element that was clicked
3572 for (u32 i = 0; i < m_fields.size(); i++) {
3573 FieldSpec &s = m_fields[i];
3574 // if its a button, set the send field so
3575 // lua knows which button was pressed
3576 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3577 (s.fid == event.GUIEvent.Caller->getID())) {
3581 acceptInput(quit_mode_accept);
3584 m_text_dst->gotText(L"ExitButton");
3588 acceptInput(quit_mode_no);
3592 } else if ((s.ftype == f_DropDown) &&
3593 (s.fid == event.GUIEvent.Caller->getID())) {
3594 // only send the changed dropdown
3595 for (u32 i = 0; i < m_fields.size(); i++) {
3596 FieldSpec &s2 = m_fields[i];
3597 if (s2.ftype == f_DropDown) {
3602 acceptInput(quit_mode_no);
3604 // revert configuration to make sure dropdowns are sent on
3605 // regular button click
3606 for (u32 i = 0; i < m_fields.size(); i++) {
3607 FieldSpec &s2 = m_fields[i];
3608 if (s2.ftype == f_DropDown) {
3613 } else if ((s.ftype == f_ScrollBar) &&
3614 (s.fid == event.GUIEvent.Caller->getID())) {
3615 s.fdefault = L"Changed";
3616 acceptInput(quit_mode_no);
3622 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3623 if (event.GUIEvent.Caller->getID() > 257) {
3626 acceptInput(quit_mode_accept);
3629 current_keys_pending.key_enter = true;
3632 // quitMenu deallocates menu
3637 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3638 int current_id = event.GUIEvent.Caller->getID();
3639 if (current_id > 257) {
3640 // find the element that was clicked
3641 for (u32 i = 0; i < m_fields.size(); i++) {
3642 FieldSpec &s = m_fields[i];
3643 // if it's a table, set the send field
3644 // so lua knows which table was changed
3645 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3656 return Parent ? Parent->OnEvent(event) : false;
3660 * get name of element by element id
3661 * @param id of element
3662 * @return name string or empty string
3664 std::string GUIFormSpecMenu::getNameByID(s32 id)
3666 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3667 iter != m_fields.end(); ++iter) {
3668 if (iter->fid == id) {
3676 * get label of element by id
3677 * @param id of element
3678 * @return label string or empty string
3680 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3682 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3683 iter != m_fields.end(); ++iter) {
3684 if (iter->fid == id) {
3685 return iter->flabel;