3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guiFormSpecMenu.h"
28 #include "constants.h"
31 #include "util/strfnd.h"
32 #include <IGUICheckBox.h>
33 #include <IGUIEditBox.h>
34 #include <IGUIButton.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include <IGUIComboBox.h>
40 #include "client/tile.h" // ITextureSource
41 #include "hud.h" // drawItemStack
45 #include "scripting_game.h"
49 #include "fontengine.h"
51 #include "util/numeric.h"
52 #include "util/string.h" // for parseColorString()
53 #include "irrlicht_changes/static_text.h"
54 #include "guiscalingfilter.h"
56 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
57 #include "intlGUIEditBox.h"
60 #define MY_CHECKPOS(a,b) \
61 if (v_pos.size() != 2) { \
62 errorstream<< "Invalid pos for element " << a << "specified: \"" \
63 << parts[b] << "\"" << std::endl; \
67 #define MY_CHECKGEOM(a,b) \
68 if (v_geom.size() != 2) { \
69 errorstream<< "Invalid pos for element " << a << "specified: \"" \
70 << parts[b] << "\"" << std::endl; \
76 static unsigned int font_line_height(gui::IGUIFont *font)
78 return font->getDimension(L"Ay").Height + font->getKerningHeight();
81 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
82 JoystickController *joystick,
83 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
84 InventoryManager *invmgr, IGameDef *gamedef,
85 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
86 Client* client, bool remap_dbl_click) :
87 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
93 m_selected_item(NULL),
95 m_selected_dragging(false),
96 m_tooltip_element(NULL),
99 m_rmouse_auto_place(false),
104 m_formspec_version(0),
105 m_focused_element(""),
106 m_joystick(joystick),
107 current_field_enter_pending(""),
109 m_remap_dbl_click(remap_dbl_click)
111 , m_JavaDialogFieldName("")
114 current_keys_pending.key_down = false;
115 current_keys_pending.key_up = false;
116 current_keys_pending.key_enter = false;
117 current_keys_pending.key_escape = false;
119 m_doubleclickdetect[0].time = 0;
120 m_doubleclickdetect[1].time = 0;
122 m_doubleclickdetect[0].pos = v2s32(0, 0);
123 m_doubleclickdetect[1].pos = v2s32(0, 0);
125 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
128 GUIFormSpecMenu::~GUIFormSpecMenu()
132 for (u32 i = 0; i < m_tables.size(); ++i) {
133 GUITable *table = m_tables[i].second;
137 delete m_selected_item;
139 if (m_form_src != NULL) {
142 if (m_text_dst != NULL) {
147 void GUIFormSpecMenu::removeChildren()
149 const core::list<gui::IGUIElement*> &children = getChildren();
151 while(!children.empty()) {
152 (*children.getLast())->remove();
155 if(m_tooltip_element) {
156 m_tooltip_element->remove();
157 m_tooltip_element->drop();
158 m_tooltip_element = NULL;
163 void GUIFormSpecMenu::setInitialFocus()
165 // Set initial focus according to following order of precedence:
166 // 1. first empty editbox
170 // 5. first focusable (not statictext, not tabheader)
171 // 6. first child element
173 core::list<gui::IGUIElement*> children = getChildren();
175 // in case "children" contains any NULL elements, remove them
176 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
177 it != children.end();) {
181 it = children.erase(it);
184 // 1. first empty editbox
185 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
186 it != children.end(); ++it) {
187 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
188 && (*it)->getText()[0] == 0) {
189 Environment->setFocus(*it);
195 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
196 it != children.end(); ++it) {
197 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
198 Environment->setFocus(*it);
204 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
205 it != children.end(); ++it) {
206 if ((*it)->getTypeName() == std::string("GUITable")) {
207 Environment->setFocus(*it);
213 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
214 it != children.end(); --it) {
215 if ((*it)->getType() == gui::EGUIET_BUTTON) {
216 Environment->setFocus(*it);
221 // 5. first focusable (not statictext, not tabheader)
222 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
223 it != children.end(); ++it) {
224 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
225 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
226 Environment->setFocus(*it);
231 // 6. first child element
232 if (children.empty())
233 Environment->setFocus(this);
235 Environment->setFocus(*(children.begin()));
238 GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
240 for (u32 i = 0; i < m_tables.size(); ++i) {
241 if (tablename == m_tables[i].first.fname)
242 return m_tables[i].second;
247 std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
249 for (u32 i = 0; i < m_dropdowns.size(); ++i) {
250 if (name == m_dropdowns[i].first.fname)
251 return &m_dropdowns[i].second;
256 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
258 std::vector<std::string> parts = split(element,',');
260 if (((parts.size() == 2) || parts.size() == 3) ||
261 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
263 if (parts[1].find(';') != std::string::npos)
264 parts[1] = parts[1].substr(0,parts[1].find(';'));
266 data->invsize.X = MYMAX(0, stof(parts[0]));
267 data->invsize.Y = MYMAX(0, stof(parts[1]));
270 if (parts.size() == 3) {
271 if (parts[2] == "true") {
272 lockSize(true,v2u32(800,600));
276 data->explicit_size = true;
279 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
282 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
284 if (m_gamedef == 0) {
285 warningstream<<"invalid use of 'list' with m_gamedef==0"<<std::endl;
289 std::vector<std::string> parts = split(element,';');
291 if (((parts.size() == 4) || (parts.size() == 5)) ||
292 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
294 std::string location = parts[0];
295 std::string listname = parts[1];
296 std::vector<std::string> v_pos = split(parts[2],',');
297 std::vector<std::string> v_geom = split(parts[3],',');
298 std::string startindex = "";
299 if (parts.size() == 5)
300 startindex = parts[4];
302 MY_CHECKPOS("list",2);
303 MY_CHECKGEOM("list",3);
305 InventoryLocation loc;
307 if(location == "context" || location == "current_name")
308 loc = m_current_inventory_location;
310 loc.deSerialize(location);
312 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
313 pos.X += stof(v_pos[0]) * (float)spacing.X;
314 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
317 geom.X = stoi(v_geom[0]);
318 geom.Y = stoi(v_geom[1]);
322 start_i = stoi(startindex);
324 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
325 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
329 if(!data->explicit_size)
330 warningstream<<"invalid use of list without a size[] element"<<std::endl;
331 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
334 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
337 void GUIFormSpecMenu::parseListRing(parserData* data, std::string element)
339 if (m_gamedef == 0) {
340 errorstream << "WARNING: invalid use of 'listring' with m_gamedef==0" << std::endl;
344 std::vector<std::string> parts = split(element, ';');
346 if (parts.size() == 2) {
347 std::string location = parts[0];
348 std::string listname = parts[1];
350 InventoryLocation loc;
352 if (location == "context" || location == "current_name")
353 loc = m_current_inventory_location;
355 loc.deSerialize(location);
357 m_inventory_rings.push_back(ListRingSpec(loc, listname));
359 } else if ((element == "") && (m_inventorylists.size() > 1)) {
360 size_t siz = m_inventorylists.size();
361 // insert the last two inv list elements into the list ring
362 const ListDrawSpec &spa = m_inventorylists[siz - 2];
363 const ListDrawSpec &spb = m_inventorylists[siz - 1];
364 m_inventory_rings.push_back(ListRingSpec(spa.inventoryloc, spa.listname));
365 m_inventory_rings.push_back(ListRingSpec(spb.inventoryloc, spb.listname));
368 errorstream<< "Invalid list ring element(" << parts.size() << ", "
369 << m_inventorylists.size() << "): '" << element << "'" << std::endl;
372 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
374 std::vector<std::string> parts = split(element,';');
376 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
377 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
379 std::vector<std::string> v_pos = split(parts[0],',');
380 std::string name = parts[1];
381 std::string label = parts[2];
382 std::string selected = "";
384 if (parts.size() >= 4)
387 MY_CHECKPOS("checkbox",0);
390 pos.X += stof(v_pos[0]) * (float) spacing.X;
391 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
393 bool fselected = false;
395 if (selected == "true")
398 std::wstring wlabel = utf8_to_wide(unescape_string(label));
400 core::rect<s32> rect = core::rect<s32>(
401 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
402 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
403 pos.Y + ((imgsize.Y/2) + m_btn_height));
407 wlabel, //Needed for displaying text on MSVC
412 spec.ftype = f_CheckBox;
414 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
415 spec.fid, spec.flabel.c_str());
417 if (spec.fname == data->focused_fieldname) {
418 Environment->setFocus(e);
421 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
422 m_fields.push_back(spec);
425 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
428 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
430 std::vector<std::string> parts = split(element,';');
432 if (parts.size() >= 5) {
433 std::vector<std::string> v_pos = split(parts[0],',');
434 std::vector<std::string> v_dim = split(parts[1],',');
435 std::string name = parts[3];
436 std::string value = parts[4];
438 MY_CHECKPOS("scrollbar",0);
441 pos.X += stof(v_pos[0]) * (float) spacing.X;
442 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
444 if (v_dim.size() != 2) {
445 errorstream<< "Invalid size for element " << "scrollbar"
446 << "specified: \"" << parts[1] << "\"" << std::endl;
451 dim.X = stof(v_dim[0]) * (float) spacing.X;
452 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
454 core::rect<s32> rect =
455 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
464 bool is_horizontal = true;
466 if (parts[2] == "vertical")
467 is_horizontal = false;
469 spec.ftype = f_ScrollBar;
471 gui::IGUIScrollBar* e =
472 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
476 e->setPos(stoi(parts[4]));
478 e->setLargeStep(100);
480 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
481 m_fields.push_back(spec);
484 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
487 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
489 std::vector<std::string> parts = split(element,';');
491 if ((parts.size() == 3) ||
492 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
494 std::vector<std::string> v_pos = split(parts[0],',');
495 std::vector<std::string> v_geom = split(parts[1],',');
496 std::string name = unescape_string(parts[2]);
498 MY_CHECKPOS("image",0);
499 MY_CHECKGEOM("image",1);
501 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
502 pos.X += stof(v_pos[0]) * (float) spacing.X;
503 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
506 geom.X = stof(v_geom[0]) * (float)imgsize.X;
507 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
509 if(!data->explicit_size)
510 warningstream<<"invalid use of image without a size[] element"<<std::endl;
511 m_images.push_back(ImageDrawSpec(name, pos, geom));
515 if (parts.size() == 2) {
516 std::vector<std::string> v_pos = split(parts[0],',');
517 std::string name = unescape_string(parts[1]);
519 MY_CHECKPOS("image",0);
521 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
522 pos.X += stof(v_pos[0]) * (float) spacing.X;
523 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
525 if(!data->explicit_size)
526 warningstream<<"invalid use of image without a size[] element"<<std::endl;
527 m_images.push_back(ImageDrawSpec(name, pos));
530 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
533 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
535 std::vector<std::string> parts = split(element,';');
537 if ((parts.size() == 3) ||
538 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
540 std::vector<std::string> v_pos = split(parts[0],',');
541 std::vector<std::string> v_geom = split(parts[1],',');
542 std::string name = parts[2];
544 MY_CHECKPOS("itemimage",0);
545 MY_CHECKGEOM("itemimage",1);
547 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
548 pos.X += stof(v_pos[0]) * (float) spacing.X;
549 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
552 geom.X = stof(v_geom[0]) * (float)imgsize.X;
553 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
555 if(!data->explicit_size)
556 warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
557 m_itemimages.push_back(ImageDrawSpec("", name, pos, geom));
560 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
563 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
566 std::vector<std::string> parts = split(element,';');
568 if ((parts.size() == 4) ||
569 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
571 std::vector<std::string> v_pos = split(parts[0],',');
572 std::vector<std::string> v_geom = split(parts[1],',');
573 std::string name = parts[2];
574 std::string label = parts[3];
576 MY_CHECKPOS("button",0);
577 MY_CHECKGEOM("button",1);
580 pos.X += stof(v_pos[0]) * (float)spacing.X;
581 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
584 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
585 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
587 core::rect<s32> rect =
588 core::rect<s32>(pos.X, pos.Y - m_btn_height,
589 pos.X + geom.X, pos.Y + m_btn_height);
591 if(!data->explicit_size)
592 warningstream<<"invalid use of button without a size[] element"<<std::endl;
594 std::wstring wlabel = utf8_to_wide(unescape_string(label));
602 spec.ftype = f_Button;
603 if(type == "button_exit")
605 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
606 spec.flabel.c_str());
608 if (spec.fname == data->focused_fieldname) {
609 Environment->setFocus(e);
612 m_fields.push_back(spec);
615 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
618 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
620 std::vector<std::string> parts = split(element,';');
622 if (((parts.size() == 3) || (parts.size() == 4)) ||
623 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
625 std::vector<std::string> v_pos = split(parts[0],',');
626 std::vector<std::string> v_geom = split(parts[1],',');
627 std::string name = unescape_string(parts[2]);
629 MY_CHECKPOS("background",0);
630 MY_CHECKGEOM("background",1);
632 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
633 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
634 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
637 geom.X = stof(v_geom[0]) * (float)spacing.X;
638 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
640 if (!data->explicit_size)
641 warningstream<<"invalid use of background without a size[] element"<<std::endl;
644 if (parts.size() == 4 && is_yes(parts[3])) {
645 pos.X = stoi(v_pos[0]); //acts as offset
646 pos.Y = stoi(v_pos[1]); //acts as offset
649 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom, clip));
653 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
656 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
658 std::vector<std::string> parts = split(element,';');
660 data->table_options.clear();
661 for (size_t i = 0; i < parts.size(); ++i) {
662 // Parse table option
663 std::string opt = unescape_string(parts[i]);
664 data->table_options.push_back(GUITable::splitOption(opt));
668 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
670 std::vector<std::string> parts = split(element,';');
672 data->table_columns.clear();
673 for (size_t i = 0; i < parts.size(); ++i) {
674 std::vector<std::string> col_parts = split(parts[i],',');
675 GUITable::TableColumn column;
677 if (!col_parts.empty())
678 column.type = col_parts[0];
679 // Parse column options
680 for (size_t j = 1; j < col_parts.size(); ++j) {
681 std::string opt = unescape_string(col_parts[j]);
682 column.options.push_back(GUITable::splitOption(opt));
684 data->table_columns.push_back(column);
688 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
690 std::vector<std::string> parts = split(element,';');
692 if (((parts.size() == 4) || (parts.size() == 5)) ||
693 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
695 std::vector<std::string> v_pos = split(parts[0],',');
696 std::vector<std::string> v_geom = split(parts[1],',');
697 std::string name = parts[2];
698 std::vector<std::string> items = split(parts[3],',');
699 std::string str_initial_selection = "";
700 std::string str_transparent = "false";
702 if (parts.size() >= 5)
703 str_initial_selection = parts[4];
705 MY_CHECKPOS("table",0);
706 MY_CHECKGEOM("table",1);
709 pos.X += stof(v_pos[0]) * (float)spacing.X;
710 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
713 geom.X = stof(v_geom[0]) * (float)spacing.X;
714 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
716 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
725 spec.ftype = f_Table;
727 for (unsigned int i = 0; i < items.size(); ++i) {
728 items[i] = unescape_enriched(unescape_string(items[i]));
731 //now really show table
732 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
735 if (spec.fname == data->focused_fieldname) {
736 Environment->setFocus(e);
739 e->setTable(data->table_options, data->table_columns, items);
741 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
742 e->setDynamicData(data->table_dyndata[name]);
745 if ((str_initial_selection != "") &&
746 (str_initial_selection != "0"))
747 e->setSelected(stoi(str_initial_selection.c_str()));
749 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
750 m_fields.push_back(spec);
753 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
756 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
758 std::vector<std::string> parts = split(element,';');
760 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
761 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
763 std::vector<std::string> v_pos = split(parts[0],',');
764 std::vector<std::string> v_geom = split(parts[1],',');
765 std::string name = parts[2];
766 std::vector<std::string> items = split(parts[3],',');
767 std::string str_initial_selection = "";
768 std::string str_transparent = "false";
770 if (parts.size() >= 5)
771 str_initial_selection = parts[4];
773 if (parts.size() >= 6)
774 str_transparent = parts[5];
776 MY_CHECKPOS("textlist",0);
777 MY_CHECKGEOM("textlist",1);
780 pos.X += stof(v_pos[0]) * (float)spacing.X;
781 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
784 geom.X = stof(v_geom[0]) * (float)spacing.X;
785 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
788 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
797 spec.ftype = f_Table;
799 for (unsigned int i = 0; i < items.size(); ++i) {
800 items[i] = unescape_enriched(unescape_string(items[i]));
803 //now really show list
804 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
807 if (spec.fname == data->focused_fieldname) {
808 Environment->setFocus(e);
811 e->setTextList(items, is_yes(str_transparent));
813 if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
814 e->setDynamicData(data->table_dyndata[name]);
817 if ((str_initial_selection != "") &&
818 (str_initial_selection != "0"))
819 e->setSelected(stoi(str_initial_selection.c_str()));
821 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
822 m_fields.push_back(spec);
825 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
829 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
831 std::vector<std::string> parts = split(element,';');
833 if ((parts.size() == 5) ||
834 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
836 std::vector<std::string> v_pos = split(parts[0],',');
837 std::string name = parts[2];
838 std::vector<std::string> items = split(parts[3],',');
839 std::string str_initial_selection = "";
840 str_initial_selection = parts[4];
842 MY_CHECKPOS("dropdown",0);
845 pos.X += stof(v_pos[0]) * (float)spacing.X;
846 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
848 s32 width = stof(parts[1]) * (float)spacing.Y;
850 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
851 pos.X + width, pos.Y + (m_btn_height * 2));
860 spec.ftype = f_DropDown;
863 //now really show list
864 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
866 if (spec.fname == data->focused_fieldname) {
867 Environment->setFocus(e);
870 for (unsigned int i=0; i < items.size(); i++) {
871 e->addItem(unescape_enriched(unescape_string(
872 utf8_to_wide(items[i]))).c_str());
875 if (str_initial_selection != "")
876 e->setSelected(stoi(str_initial_selection.c_str())-1);
878 m_fields.push_back(spec);
880 m_dropdowns.push_back(std::pair<FieldSpec,
881 std::vector<std::string> >(spec, std::vector<std::string>()));
882 std::vector<std::string> &values = m_dropdowns.back().second;
883 for (unsigned int i = 0; i < items.size(); i++) {
884 values.push_back(unescape_string(items[i]));
889 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
890 << element << "'" << std::endl;
893 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
895 std::vector<std::string> parts = split(element,';');
897 if ((parts.size() == 4) || (parts.size() == 5) ||
898 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
900 std::vector<std::string> v_pos = split(parts[0],',');
901 std::vector<std::string> v_geom = split(parts[1],',');
902 std::string name = parts[2];
903 std::string label = parts[3];
905 MY_CHECKPOS("pwdfield",0);
906 MY_CHECKGEOM("pwdfield",1);
909 pos.X += stof(v_pos[0]) * (float)spacing.X;
910 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
913 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
915 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
916 pos.Y -= m_btn_height;
917 geom.Y = m_btn_height*2;
919 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
921 std::wstring wlabel = utf8_to_wide(unescape_string(label));
931 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
933 if (spec.fname == data->focused_fieldname) {
934 Environment->setFocus(e);
937 if (label.length() >= 1)
939 int font_height = g_fontengine->getTextHeight();
940 rect.UpperLeftCorner.Y -= font_height;
941 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
942 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
945 e->setPasswordBox(true,L'*');
948 evt.EventType = EET_KEY_INPUT_EVENT;
949 evt.KeyInput.Key = KEY_END;
950 evt.KeyInput.Char = 0;
951 evt.KeyInput.Control = 0;
952 evt.KeyInput.Shift = 0;
953 evt.KeyInput.PressedDown = true;
956 if (parts.size() >= 5 && !is_yes(parts[4])) {
957 spec.close_on_enter = false;
960 m_fields.push_back(spec);
963 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
966 void GUIFormSpecMenu::parseSimpleField(parserData* data,
967 std::vector<std::string> &parts)
969 std::string name = parts[0];
970 std::string label = parts[1];
971 std::string default_val = parts[2];
973 core::rect<s32> rect;
975 if(data->explicit_size)
976 warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
978 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
979 pos.Y = ((m_fields.size()+2)*60);
980 v2s32 size = DesiredRect.getSize();
982 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
983 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
987 default_val = m_form_src->resolveText(default_val);
990 std::wstring wlabel = utf8_to_wide(unescape_string(label));
995 utf8_to_wide(unescape_string(default_val)),
1001 // spec field id to 0, this stops submit searching for a value that isn't there
1002 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1007 gui::IGUIElement *e;
1008 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1009 if (g_settings->getBool("freetype")) {
1010 e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1011 true, Environment, this, spec.fid, rect);
1017 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1019 if (spec.fname == data->focused_fieldname) {
1020 Environment->setFocus(e);
1024 evt.EventType = EET_KEY_INPUT_EVENT;
1025 evt.KeyInput.Key = KEY_END;
1026 evt.KeyInput.Char = 0;
1027 evt.KeyInput.Control = 0;
1028 evt.KeyInput.Shift = 0;
1029 evt.KeyInput.PressedDown = true;
1032 if (label.length() >= 1)
1034 int font_height = g_fontengine->getTextHeight();
1035 rect.UpperLeftCorner.Y -= font_height;
1036 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1037 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1041 if (parts.size() >= 4 && !is_yes(parts[3])) {
1042 spec.close_on_enter = false;
1045 m_fields.push_back(spec);
1048 void GUIFormSpecMenu::parseTextArea(parserData* data,
1049 std::vector<std::string>& parts,std::string type)
1052 std::vector<std::string> v_pos = split(parts[0],',');
1053 std::vector<std::string> v_geom = split(parts[1],',');
1054 std::string name = parts[2];
1055 std::string label = parts[3];
1056 std::string default_val = parts[4];
1058 MY_CHECKPOS(type,0);
1059 MY_CHECKGEOM(type,1);
1062 pos.X = stof(v_pos[0]) * (float) spacing.X;
1063 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1067 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1069 if (type == "textarea")
1071 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1072 pos.Y += m_btn_height;
1076 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1077 pos.Y -= m_btn_height;
1078 geom.Y = m_btn_height*2;
1081 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1083 if(!data->explicit_size)
1084 warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1087 default_val = m_form_src->resolveText(default_val);
1090 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1095 utf8_to_wide(unescape_string(default_val)),
1101 // spec field id to 0, this stops submit searching for a value that isn't there
1102 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1108 gui::IGUIEditBox *e;
1109 #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1110 if (g_settings->getBool("freetype")) {
1111 e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1112 true, Environment, this, spec.fid, rect);
1118 e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1121 if (spec.fname == data->focused_fieldname) {
1122 Environment->setFocus(e);
1125 if (type == "textarea")
1127 e->setMultiLine(true);
1128 e->setWordWrap(true);
1129 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1132 evt.EventType = EET_KEY_INPUT_EVENT;
1133 evt.KeyInput.Key = KEY_END;
1134 evt.KeyInput.Char = 0;
1135 evt.KeyInput.Control = 0;
1136 evt.KeyInput.Shift = 0;
1137 evt.KeyInput.PressedDown = true;
1141 if (label.length() >= 1)
1143 int font_height = g_fontengine->getTextHeight();
1144 rect.UpperLeftCorner.Y -= font_height;
1145 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1146 addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1150 if (parts.size() >= 6 && !is_yes(parts[5])) {
1151 spec.close_on_enter = false;
1154 m_fields.push_back(spec);
1157 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1160 std::vector<std::string> parts = split(element,';');
1162 if (parts.size() == 3 || parts.size() == 4) {
1163 parseSimpleField(data,parts);
1167 if ((parts.size() == 5) || (parts.size() == 6) ||
1168 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1170 parseTextArea(data,parts,type);
1173 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1176 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1178 std::vector<std::string> parts = split(element,';');
1180 if ((parts.size() == 2) ||
1181 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1183 std::vector<std::string> v_pos = split(parts[0],',');
1184 std::string text = parts[1];
1186 MY_CHECKPOS("label",0);
1188 v2s32 pos = padding;
1189 pos.X += stof(v_pos[0]) * (float)spacing.X;
1190 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1192 if(!data->explicit_size)
1193 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1195 std::vector<std::string> lines = split(text, '\n');
1197 for (unsigned int i = 0; i != lines.size(); i++) {
1198 // Lines are spaced at the nominal distance of
1199 // 2/5 inventory slot, even if the font doesn't
1200 // quite match that. This provides consistent
1201 // form layout, at the expense of sometimes
1202 // having sub-optimal spacing for the font.
1203 // We multiply by 2 and then divide by 5, rather
1204 // than multiply by 0.4, to get exact results
1205 // in the integer cases: 0.4 is not exactly
1206 // representable in binary floating point.
1207 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1208 std::wstring wlabel = utf8_to_wide(unescape_string(lines[i]));
1209 core::rect<s32> rect = core::rect<s32>(
1210 pos.X, posy - m_btn_height,
1211 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1212 posy + m_btn_height);
1219 gui::IGUIStaticText *e =
1220 addStaticText(Environment, spec.flabel.c_str(),
1221 rect, false, false, this, spec.fid);
1222 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1224 m_fields.push_back(spec);
1229 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1232 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1234 std::vector<std::string> parts = split(element,';');
1236 if ((parts.size() == 2) ||
1237 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1239 std::vector<std::string> v_pos = split(parts[0],',');
1240 std::wstring text = unescape_enriched(
1241 unescape_string(utf8_to_wide(parts[1])));
1243 MY_CHECKPOS("vertlabel",1);
1245 v2s32 pos = padding;
1246 pos.X += stof(v_pos[0]) * (float)spacing.X;
1247 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1249 core::rect<s32> rect = core::rect<s32>(
1250 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1252 font_line_height(m_font)
1254 +((imgsize.Y/2)- m_btn_height));
1255 //actually text.length() would be correct but adding +1 avoids to break all mods
1257 if(!data->explicit_size)
1258 warningstream<<"invalid use of label without a size[] element"<<std::endl;
1260 std::wstring label = L"";
1262 for (unsigned int i=0; i < text.length(); i++) {
1273 gui::IGUIStaticText *t =
1274 addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid);
1275 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1276 m_fields.push_back(spec);
1279 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1282 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1285 std::vector<std::string> parts = split(element,';');
1287 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1288 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1290 std::vector<std::string> v_pos = split(parts[0],',');
1291 std::vector<std::string> v_geom = split(parts[1],',');
1292 std::string image_name = parts[2];
1293 std::string name = parts[3];
1294 std::string label = parts[4];
1296 MY_CHECKPOS("imagebutton",0);
1297 MY_CHECKGEOM("imagebutton",1);
1299 v2s32 pos = padding;
1300 pos.X += stof(v_pos[0]) * (float)spacing.X;
1301 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1303 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1304 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1306 bool noclip = false;
1307 bool drawborder = true;
1308 std::string pressed_image_name = "";
1310 if (parts.size() >= 7) {
1311 if (parts[5] == "true")
1313 if (parts[6] == "false")
1317 if (parts.size() >= 8) {
1318 pressed_image_name = parts[7];
1321 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1323 if(!data->explicit_size)
1324 warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
1326 image_name = unescape_string(image_name);
1327 pressed_image_name = unescape_string(pressed_image_name);
1329 std::wstring wlabel = utf8_to_wide(unescape_string(label));
1334 utf8_to_wide(image_name),
1337 spec.ftype = f_Button;
1338 if(type == "image_button_exit")
1339 spec.is_exit = true;
1341 video::ITexture *texture = 0;
1342 video::ITexture *pressed_texture = 0;
1343 texture = m_tsrc->getTexture(image_name);
1344 if (pressed_image_name != "")
1345 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1347 pressed_texture = texture;
1349 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1351 if (spec.fname == data->focused_fieldname) {
1352 Environment->setFocus(e);
1355 e->setUseAlphaChannel(true);
1356 e->setImage(guiScalingImageButton(
1357 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1358 e->setPressedImage(guiScalingImageButton(
1359 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1360 e->setScaleImage(true);
1361 e->setNotClipped(noclip);
1362 e->setDrawBorder(drawborder);
1364 m_fields.push_back(spec);
1368 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1371 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1373 std::vector<std::string> parts = split(element,';');
1375 if (((parts.size() == 4) || (parts.size() == 6)) ||
1376 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1378 std::vector<std::string> v_pos = split(parts[0],',');
1379 std::string name = parts[1];
1380 std::vector<std::string> buttons = split(parts[2],',');
1381 std::string str_index = parts[3];
1382 bool show_background = true;
1383 bool show_border = true;
1384 int tab_index = stoi(str_index) -1;
1386 MY_CHECKPOS("tabheader",0);
1388 if (parts.size() == 6) {
1389 if (parts[4] == "true")
1390 show_background = false;
1391 if (parts[5] == "false")
1392 show_border = false;
1402 spec.ftype = f_TabHeader;
1405 pos.X += stof(v_pos[0]) * (float)spacing.X;
1406 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1408 geom.X = DesiredRect.getWidth();
1409 geom.Y = m_btn_height*2;
1411 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1414 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1415 show_background, show_border, spec.fid);
1416 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1417 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1418 e->setTabHeight(m_btn_height*2);
1420 if (spec.fname == data->focused_fieldname) {
1421 Environment->setFocus(e);
1424 e->setNotClipped(true);
1426 for (unsigned int i = 0; i < buttons.size(); i++) {
1427 e->addTab(unescape_enriched(unescape_string(
1428 utf8_to_wide(buttons[i]))).c_str(), -1);
1431 if ((tab_index >= 0) &&
1432 (buttons.size() < INT_MAX) &&
1433 (tab_index < (int) buttons.size()))
1434 e->setActiveTab(tab_index);
1436 m_fields.push_back(spec);
1439 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1440 << element << "'" << std::endl;
1443 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1446 if (m_gamedef == 0) {
1447 warningstream << "invalid use of item_image_button with m_gamedef==0"
1452 std::vector<std::string> parts = split(element,';');
1454 if ((parts.size() == 5) ||
1455 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1457 std::vector<std::string> v_pos = split(parts[0],',');
1458 std::vector<std::string> v_geom = split(parts[1],',');
1459 std::string item_name = parts[2];
1460 std::string name = parts[3];
1461 std::string label = parts[4];
1463 label = unescape_string(label);
1464 item_name = unescape_string(item_name);
1466 MY_CHECKPOS("itemimagebutton",0);
1467 MY_CHECKGEOM("itemimagebutton",1);
1469 v2s32 pos = padding;
1470 pos.X += stof(v_pos[0]) * (float)spacing.X;
1471 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1473 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1474 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1476 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1478 if(!data->explicit_size)
1479 warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
1481 IItemDefManager *idef = m_gamedef->idef();
1483 item.deSerialize(item_name, idef);
1486 TooltipSpec(item.getDefinition(idef).description,
1487 m_default_tooltip_bgcolor,
1488 m_default_tooltip_color);
1492 utf8_to_wide(label),
1493 utf8_to_wide(item_name),
1494 258 + m_fields.size()
1497 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
1499 if (spec.fname == data->focused_fieldname) {
1500 Environment->setFocus(e);
1503 spec.ftype = f_Button;
1504 rect+=data->basepos-padding;
1506 m_fields.push_back(spec);
1507 pos = padding + AbsoluteRect.UpperLeftCorner;
1508 pos.X += stof(v_pos[0]) * (float) spacing.X;
1509 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1510 m_itemimages.push_back(ImageDrawSpec("", item_name, e, pos, geom));
1511 m_static_texts.push_back(StaticTextSpec(utf8_to_wide(label), rect, e));
1514 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1517 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1519 std::vector<std::string> parts = split(element,';');
1521 if ((parts.size() == 3) ||
1522 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1524 std::vector<std::string> v_pos = split(parts[0],',');
1525 std::vector<std::string> v_geom = split(parts[1],',');
1527 MY_CHECKPOS("box",0);
1528 MY_CHECKGEOM("box",1);
1530 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1531 pos.X += stof(v_pos[0]) * (float) spacing.X;
1532 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1535 geom.X = stof(v_geom[0]) * (float)spacing.X;
1536 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1538 video::SColor tmp_color;
1540 if (parseColorString(parts[2], tmp_color, false)) {
1541 BoxDrawSpec spec(pos, geom, tmp_color);
1543 m_boxes.push_back(spec);
1546 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1550 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1553 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1555 std::vector<std::string> parts = split(element,';');
1557 if (((parts.size() == 1) || (parts.size() == 2)) ||
1558 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1560 parseColorString(parts[0],m_bgcolor,false);
1562 if (parts.size() == 2) {
1563 std::string fullscreen = parts[1];
1564 m_bgfullscreen = is_yes(fullscreen);
1568 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1571 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1573 std::vector<std::string> parts = split(element,';');
1575 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1576 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1578 parseColorString(parts[0], m_slotbg_n, false);
1579 parseColorString(parts[1], m_slotbg_h, false);
1581 if (parts.size() >= 3) {
1582 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1583 m_slotborder = true;
1586 if (parts.size() == 5) {
1587 video::SColor tmp_color;
1589 if (parseColorString(parts[3], tmp_color, false))
1590 m_default_tooltip_bgcolor = tmp_color;
1591 if (parseColorString(parts[4], tmp_color, false))
1592 m_default_tooltip_color = tmp_color;
1596 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1599 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1601 std::vector<std::string> parts = split(element,';');
1602 if (parts.size() == 2) {
1603 std::string name = parts[0];
1604 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1605 m_default_tooltip_bgcolor, m_default_tooltip_color);
1607 } else if (parts.size() == 4) {
1608 std::string name = parts[0];
1609 video::SColor tmp_color1, tmp_color2;
1610 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1611 m_tooltips[name] = TooltipSpec(unescape_string(parts[1]),
1612 tmp_color1, tmp_color2);
1616 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1619 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1625 std::vector<std::string> parts = split(data,'[');
1627 if (parts.size() < 2) {
1631 if (parts[0] != "formspec_version") {
1635 if (is_number(parts[1])) {
1636 m_formspec_version = mystoi(parts[1]);
1643 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1648 std::vector<std::string> parts = split(element,'[');
1650 if (parts.size() < 2)
1653 std::string type = trim(parts[0]);
1654 std::string description = trim(parts[1]);
1656 if (type != "size" && type != "invsize")
1659 if (type == "invsize")
1660 log_deprecated("Deprecated formspec element \"invsize\" is used");
1662 parseSize(data, description);
1667 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1673 std::vector<std::string> parts = split(element,'[');
1675 // ugly workaround to keep compatibility
1676 if (parts.size() > 2) {
1677 if (trim(parts[0]) == "image") {
1678 for (unsigned int i=2;i< parts.size(); i++) {
1679 parts[1] += "[" + parts[i];
1685 if (parts.size() < 2) {
1689 std::string type = trim(parts[0]);
1690 std::string description = trim(parts[1]);
1692 if (type == "list") {
1693 parseList(data,description);
1697 if (type == "listring") {
1698 parseListRing(data, description);
1702 if (type == "checkbox") {
1703 parseCheckbox(data,description);
1707 if (type == "image") {
1708 parseImage(data,description);
1712 if (type == "item_image") {
1713 parseItemImage(data,description);
1717 if ((type == "button") || (type == "button_exit")) {
1718 parseButton(data,description,type);
1722 if (type == "background") {
1723 parseBackground(data,description);
1727 if (type == "tableoptions"){
1728 parseTableOptions(data,description);
1732 if (type == "tablecolumns"){
1733 parseTableColumns(data,description);
1737 if (type == "table"){
1738 parseTable(data,description);
1742 if (type == "textlist"){
1743 parseTextList(data,description);
1747 if (type == "dropdown"){
1748 parseDropDown(data,description);
1752 if (type == "pwdfield") {
1753 parsePwdField(data,description);
1757 if ((type == "field") || (type == "textarea")){
1758 parseField(data,description,type);
1762 if (type == "label") {
1763 parseLabel(data,description);
1767 if (type == "vertlabel") {
1768 parseVertLabel(data,description);
1772 if (type == "item_image_button") {
1773 parseItemImageButton(data,description);
1777 if ((type == "image_button") || (type == "image_button_exit")) {
1778 parseImageButton(data,description,type);
1782 if (type == "tabheader") {
1783 parseTabHeader(data,description);
1787 if (type == "box") {
1788 parseBox(data,description);
1792 if (type == "bgcolor") {
1793 parseBackgroundColor(data,description);
1797 if (type == "listcolors") {
1798 parseListColors(data,description);
1802 if (type == "tooltip") {
1803 parseTooltip(data,description);
1807 if (type == "scrollbar") {
1808 parseScrollBar(data, description);
1814 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1818 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1820 /* useless to regenerate without a screensize */
1821 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1828 for (u32 i = 0; i < m_tables.size(); ++i) {
1829 std::string tablename = m_tables[i].first.fname;
1830 GUITable *table = m_tables[i].second;
1831 mydata.table_dyndata[tablename] = table->getDynamicData();
1835 if (!m_focused_element.empty())
1836 mydata.focused_fieldname = m_focused_element;
1839 gui::IGUIElement *focused_element = Environment->getFocus();
1840 if (focused_element && focused_element->getParent() == this) {
1841 s32 focused_id = focused_element->getID();
1842 if (focused_id > 257) {
1843 for (u32 i=0; i<m_fields.size(); i++) {
1844 if (m_fields[i].fid == focused_id) {
1845 mydata.focused_fieldname =
1856 for (u32 i = 0; i < m_tables.size(); ++i) {
1857 GUITable *table = m_tables[i].second;
1861 mydata.size= v2s32(100,100);
1862 mydata.screensize = screensize;
1864 // Base position of contents of form
1865 mydata.basepos = getBasePos();
1867 /* Convert m_init_draw_spec to m_inventorylists */
1869 m_inventorylists.clear();
1871 m_backgrounds.clear();
1872 m_itemimages.clear();
1874 m_checkboxes.clear();
1875 m_scrollbars.clear();
1879 m_inventory_rings.clear();
1880 m_static_texts.clear();
1882 // Set default values (fits old formspec values)
1883 m_bgcolor = video::SColor(140,0,0,0);
1884 m_bgfullscreen = false;
1886 m_slotbg_n = video::SColor(255,128,128,128);
1887 m_slotbg_h = video::SColor(255,192,192,192);
1889 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1890 m_default_tooltip_color = video::SColor(255,255,255,255);
1892 m_slotbordercolor = video::SColor(200,0,0,0);
1893 m_slotborder = false;
1897 assert(m_tooltip_element == NULL);
1898 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1899 m_tooltip_element = addStaticText(Environment, L"",core::rect<s32>(0,0,110,18));
1900 m_tooltip_element->enableOverrideColor(true);
1901 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1902 m_tooltip_element->setDrawBackground(true);
1903 m_tooltip_element->setDrawBorder(true);
1904 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1905 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1906 m_tooltip_element->setWordWrap(false);
1907 //we're not parent so no autograb for this one!
1908 m_tooltip_element->grab();
1911 std::vector<std::string> elements = split(m_formspec_string,']');
1914 /* try to read version from first element only */
1915 if (elements.size() >= 1) {
1916 if ( parseVersionDirect(elements[0]) ) {
1921 /* we need size first in order to calculate image scale */
1922 mydata.explicit_size = false;
1923 for (; i< elements.size(); i++) {
1924 if (!parseSizeDirect(&mydata, elements[i])) {
1929 if (mydata.explicit_size) {
1930 // compute scaling for specified form size
1932 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1933 v2u32 delta = current_screensize - m_lockscreensize;
1935 if (current_screensize.Y > m_lockscreensize.Y)
1940 if (current_screensize.X > m_lockscreensize.X)
1945 offset = v2s32(delta.X,delta.Y);
1947 mydata.screensize = m_lockscreensize;
1949 offset = v2s32(0,0);
1952 double gui_scaling = g_settings->getFloat("gui_scaling");
1953 double screen_dpi = porting::getDisplayDensity() * 96;
1957 // In fixed-size mode, inventory image size
1958 // is 0.53 inch multiplied by the gui_scaling
1959 // config parameter. This magic size is chosen
1960 // to make the main menu (15.5 inventory images
1961 // wide, including border) just fit into the
1962 // default window (800 pixels wide) at 96 DPI
1963 // and default scaling (1.00).
1964 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1966 // In variable-size mode, we prefer to make the
1967 // inventory image size 1/15 of screen height,
1968 // multiplied by the gui_scaling config parameter.
1969 // If the preferred size won't fit the whole
1970 // form on the screen, either horizontally or
1971 // vertically, then we scale it down to fit.
1972 // (The magic numbers in the computation of what
1973 // fits arise from the scaling factors in the
1974 // following stanza, including the form border,
1975 // help text space, and 0.1 inventory slot spare.)
1976 // However, a minimum size is also set, that
1977 // the image size can't be less than 0.3 inch
1978 // multiplied by gui_scaling, even if this means
1979 // the form doesn't fit the screen.
1980 double prefer_imgsize = mydata.screensize.Y / 15 *
1982 double fitx_imgsize = mydata.screensize.X /
1983 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1984 double fity_imgsize = mydata.screensize.Y /
1985 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1986 double screen_dpi = porting::getDisplayDensity() * 96;
1987 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1988 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1989 MYMIN(fitx_imgsize, fity_imgsize)));
1992 // Everything else is scaled in proportion to the
1993 // inventory image size. The inventory slot spacing
1994 // is 5/4 image size horizontally and 15/13 image size
1995 // vertically. The padding around the form (incorporating
1996 // the border of the outer inventory slots) is 3/8
1997 // image size. Font height (baseline to baseline)
1998 // is 2/5 vertical inventory slot spacing, and button
1999 // half-height is 7/8 of font height.
2000 imgsize = v2s32(use_imgsize, use_imgsize);
2001 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2002 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2003 m_btn_height = use_imgsize*15.0/13 * 0.35;
2005 m_font = g_fontengine->getFont();
2007 mydata.size = v2s32(
2008 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2009 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2011 DesiredRect = mydata.rect = core::rect<s32>(
2012 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
2013 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
2014 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
2015 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
2018 // Non-size[] form must consist only of text fields and
2019 // implicit "Proceed" button. Use default font, and
2020 // temporary form size which will be recalculated below.
2021 m_font = g_fontengine->getFont();
2022 m_btn_height = font_line_height(m_font) * 0.875;
2023 DesiredRect = core::rect<s32>(
2024 mydata.screensize.X/2 - 580/2,
2025 mydata.screensize.Y/2 - 300/2,
2026 mydata.screensize.X/2 + 580/2,
2027 mydata.screensize.Y/2 + 300/2
2030 recalculateAbsolutePosition(false);
2031 mydata.basepos = getBasePos();
2032 m_tooltip_element->setOverrideFont(m_font);
2034 gui::IGUISkin* skin = Environment->getSkin();
2035 sanity_check(skin != NULL);
2036 gui::IGUIFont *old_font = skin->getFont();
2037 skin->setFont(m_font);
2039 for (; i< elements.size(); i++) {
2040 parseElement(&mydata, elements[i]);
2043 // If there are fields without explicit size[], add a "Proceed"
2044 // button and adjust size to fit all the fields.
2045 if (m_fields.size() && !mydata.explicit_size) {
2046 mydata.rect = core::rect<s32>(
2047 mydata.screensize.X/2 - 580/2,
2048 mydata.screensize.Y/2 - 300/2,
2049 mydata.screensize.X/2 + 580/2,
2050 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2052 DesiredRect = mydata.rect;
2053 recalculateAbsolutePosition(false);
2054 mydata.basepos = getBasePos();
2057 v2s32 pos = mydata.basepos;
2058 pos.Y = ((m_fields.size()+2)*60);
2060 v2s32 size = DesiredRect.getSize();
2062 core::rect<s32>(size.X/2-70, pos.Y,
2063 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2064 const wchar_t *text = wgettext("Proceed");
2065 Environment->addButton(mydata.rect, this, 257, text);
2071 //set initial focus if parser didn't set it
2072 focused_element = Environment->getFocus();
2073 if (!focused_element
2074 || !isMyChild(focused_element)
2075 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2078 skin->setFont(old_font);
2082 bool GUIFormSpecMenu::getAndroidUIInput()
2084 /* no dialog shown */
2085 if (m_JavaDialogFieldName == "") {
2090 if (porting::getInputDialogState() == -1) {
2094 std::string fieldname = m_JavaDialogFieldName;
2095 m_JavaDialogFieldName = "";
2097 /* no value abort dialog processing */
2098 if (porting::getInputDialogState() != 0) {
2102 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2103 iter != m_fields.end(); ++iter) {
2105 if (iter->fname != fieldname) {
2108 IGUIElement* tochange = getElementFromId(iter->fid);
2110 if (tochange == 0) {
2114 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2118 std::string text = porting::getInputDialogValue();
2120 ((gui::IGUIEditBox*) tochange)->
2121 setText(utf8_to_wide(text).c_str());
2127 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2129 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2131 for(u32 i=0; i<m_inventorylists.size(); i++)
2133 const ListDrawSpec &s = m_inventorylists[i];
2135 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2136 s32 item_i = i + s.start_item_i;
2137 s32 x = (i%s.geom.X) * spacing.X;
2138 s32 y = (i/s.geom.X) * spacing.Y;
2140 core::rect<s32> rect = imgrect + s.pos + p0;
2141 if(rect.isPointInside(p))
2143 return ItemSpec(s.inventoryloc, s.listname, item_i);
2148 return ItemSpec(InventoryLocation(), "", -1);
2151 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
2154 video::IVideoDriver* driver = Environment->getVideoDriver();
2156 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2158 warningstream<<"GUIFormSpecMenu::drawList(): "
2159 <<"The inventory location "
2160 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2164 InventoryList *ilist = inv->getList(s.listname);
2166 warningstream<<"GUIFormSpecMenu::drawList(): "
2167 <<"The inventory list \""<<s.listname<<"\" @ \""
2168 <<s.inventoryloc.dump()<<"\" doesn't exist"
2173 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2175 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2177 s32 item_i = i + s.start_item_i;
2178 if(item_i >= (s32) ilist->getSize())
2180 s32 x = (i%s.geom.X) * spacing.X;
2181 s32 y = (i/s.geom.X) * spacing.Y;
2183 core::rect<s32> rect = imgrect + s.pos + p;
2186 item = ilist->getItem(item_i);
2188 bool selected = m_selected_item
2189 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2190 && m_selected_item->listname == s.listname
2191 && m_selected_item->i == item_i;
2192 bool hovering = rect.isPointInside(m_pointer);
2193 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2194 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2198 item_hovered = true;
2199 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2201 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2205 //Draw inv slot borders
2207 s32 x1 = rect.UpperLeftCorner.X;
2208 s32 y1 = rect.UpperLeftCorner.Y;
2209 s32 x2 = rect.LowerRightCorner.X;
2210 s32 y2 = rect.LowerRightCorner.Y;
2212 driver->draw2DRectangle(m_slotbordercolor,
2213 core::rect<s32>(v2s32(x1 - border, y1 - border),
2214 v2s32(x2 + border, y1)), NULL);
2215 driver->draw2DRectangle(m_slotbordercolor,
2216 core::rect<s32>(v2s32(x1 - border, y2),
2217 v2s32(x2 + border, y2 + border)), NULL);
2218 driver->draw2DRectangle(m_slotbordercolor,
2219 core::rect<s32>(v2s32(x1 - border, y1),
2220 v2s32(x1, y2)), NULL);
2221 driver->draw2DRectangle(m_slotbordercolor,
2222 core::rect<s32>(v2s32(x2, y1),
2223 v2s32(x2 + border, y2)), NULL);
2231 item.takeItem(m_selected_amount);
2235 drawItemStack(driver, m_font, item,
2236 rect, &AbsoluteClippingRect, m_gamedef,
2241 std::wstring tooltip_text = L"";
2242 if (hovering && !m_selected_item) {
2243 tooltip_text = utf8_to_wide(item.getDefinition(m_gamedef->idef()).description);
2245 if (tooltip_text != L"") {
2246 std::vector<std::wstring> tt_rows = str_split(tooltip_text, L'\n');
2247 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2248 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2249 m_tooltip_element->setVisible(true);
2250 this->bringToFront(m_tooltip_element);
2251 setStaticText(m_tooltip_element, tooltip_text.c_str());
2252 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2253 #if IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2
2254 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2256 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2258 v2u32 screenSize = driver->getScreenSize();
2259 int tooltip_offset_x = m_btn_height;
2260 int tooltip_offset_y = m_btn_height;
2262 tooltip_offset_x *= 3;
2263 tooltip_offset_y = 0;
2264 if (m_pointer.X > (s32)screenSize.X / 2)
2265 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2267 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2268 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2269 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2270 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2271 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2272 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2273 m_tooltip_element->setRelativePosition(core::rect<s32>(
2274 core::position2d<s32>(tooltip_x, tooltip_y),
2275 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2281 void GUIFormSpecMenu::drawSelectedItem()
2283 video::IVideoDriver* driver = Environment->getVideoDriver();
2285 if (!m_selected_item) {
2286 drawItemStack(driver, m_font, ItemStack(),
2287 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2288 NULL, m_gamedef, IT_ROT_DRAGGED);
2292 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2294 InventoryList *list = inv->getList(m_selected_item->listname);
2296 ItemStack stack = list->getItem(m_selected_item->i);
2297 stack.count = m_selected_amount;
2299 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2300 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2301 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, IT_ROT_DRAGGED);
2304 void GUIFormSpecMenu::drawMenu()
2307 std::string newform = m_form_src->getForm();
2308 if(newform != m_formspec_string){
2309 m_formspec_string = newform;
2310 regenerateGui(m_screensize_old);
2314 gui::IGUISkin* skin = Environment->getSkin();
2315 sanity_check(skin != NULL);
2316 gui::IGUIFont *old_font = skin->getFont();
2317 skin->setFont(m_font);
2319 updateSelectedItem();
2321 video::IVideoDriver* driver = Environment->getVideoDriver();
2323 v2u32 screenSize = driver->getScreenSize();
2324 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2326 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2328 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2330 m_tooltip_element->setVisible(false);
2335 for(u32 i=0; i<m_backgrounds.size(); i++)
2337 const ImageDrawSpec &spec = m_backgrounds[i];
2338 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2341 // Image size on screen
2342 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2343 // Image rectangle on screen
2344 core::rect<s32> rect = imgrect + spec.pos;
2347 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2348 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2349 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2350 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2351 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2354 const video::SColor color(255,255,255,255);
2355 const video::SColor colors[] = {color,color,color,color};
2356 draw2DImageFilterScaled(driver, texture, rect,
2357 core::rect<s32>(core::position2d<s32>(0,0),
2358 core::dimension2di(texture->getOriginalSize())),
2359 NULL/*&AbsoluteClippingRect*/, colors, true);
2361 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2362 errorstream << "\t" << spec.name << std::endl;
2369 for(u32 i=0; i<m_boxes.size(); i++)
2371 const BoxDrawSpec &spec = m_boxes[i];
2373 irr::video::SColor todraw = spec.color;
2375 todraw.setAlpha(140);
2377 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2378 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2380 driver->draw2DRectangle(todraw, rect, 0);
2386 gui::IGUIElement::draw();
2391 for(u32 i=0; i<m_images.size(); i++)
2393 const ImageDrawSpec &spec = m_images[i];
2394 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2397 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2398 // Image size on screen
2399 core::rect<s32> imgrect;
2402 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2405 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2407 // Image rectangle on screen
2408 core::rect<s32> rect = imgrect + spec.pos;
2409 const video::SColor color(255,255,255,255);
2410 const video::SColor colors[] = {color,color,color,color};
2411 draw2DImageFilterScaled(driver, texture, rect,
2412 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2413 NULL/*&AbsoluteClippingRect*/, colors, true);
2416 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2417 errorstream << "\t" << spec.name << std::endl;
2424 for(u32 i=0; i<m_itemimages.size(); i++)
2429 const ImageDrawSpec &spec = m_itemimages[i];
2430 IItemDefManager *idef = m_gamedef->idef();
2432 item.deSerialize(spec.item_name, idef);
2433 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2434 // Viewport rectangle on screen
2435 core::rect<s32> rect = imgrect + spec.pos;
2436 if (spec.parent_button && spec.parent_button->isPressed()) {
2437 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2438 rect += core::dimension2d<s32>(
2439 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2441 rect += core::dimension2d<s32>(
2442 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2443 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2446 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2447 m_gamedef, IT_ROT_NONE);
2452 Phase 0: Item slot rectangles
2453 Phase 1: Item images; prepare tooltip
2455 bool item_hovered = false;
2456 int start_phase = 0;
2457 for (int phase = start_phase; phase <= 1; phase++) {
2458 for (u32 i = 0; i < m_inventorylists.size(); i++) {
2459 drawList(m_inventorylists[i], phase, item_hovered);
2462 if (!item_hovered) {
2463 drawItemStack(driver, m_font, ItemStack(),
2464 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2465 NULL, m_gamedef, IT_ROT_HOVERED);
2468 /* TODO find way to show tooltips on touchscreen */
2469 #ifndef HAVE_TOUCHSCREENGUI
2470 m_pointer = m_device->getCursorControl()->getPosition();
2474 Draw static text elements
2476 for (u32 i = 0; i < m_static_texts.size(); i++) {
2477 const StaticTextSpec &spec = m_static_texts[i];
2478 core::rect<s32> rect = spec.rect;
2479 if (spec.parent_button && spec.parent_button->isPressed()) {
2480 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2481 rect += core::dimension2d<s32>(
2482 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2484 // Use image offset instead of text's because its a bit smaller
2485 // and fits better, also TEXT_OFFSET_X is always 0
2486 rect += core::dimension2d<s32>(
2487 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2488 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2491 video::SColor color(255, 255, 255, 255);
2492 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
2496 Draw fields/buttons tooltips
2498 gui::IGUIElement *hovered =
2499 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2501 if (hovered != NULL) {
2502 s32 id = hovered->getID();
2506 m_old_tooltip_id = id;
2507 m_old_tooltip = L"";
2509 if (id == m_old_tooltip_id) {
2510 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2512 m_hovered_time = getTimeMs();
2513 m_old_tooltip_id = id;
2517 if (id != -1 && delta >= m_tooltip_show_delay) {
2518 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2519 iter != m_fields.end(); ++iter) {
2520 if (iter->fid == id && m_tooltips[iter->fname].tooltip != L"") {
2521 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2522 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2523 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2524 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2525 setStaticText(m_tooltip_element, m_tooltips[iter->fname].tooltip.c_str());
2526 std::vector<std::wstring> tt_rows = str_split(m_tooltips[iter->fname].tooltip, L'\n');
2527 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2528 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2529 int tooltip_offset_x = m_btn_height;
2530 int tooltip_offset_y = m_btn_height;
2532 tooltip_offset_x *= 3;
2533 tooltip_offset_y = 0;
2534 if (m_pointer.X > (s32)screenSize.X / 2)
2535 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2537 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2538 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2539 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2540 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2541 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2542 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2543 m_tooltip_element->setRelativePosition(core::rect<s32>(
2544 core::position2d<s32>(tooltip_x, tooltip_y),
2545 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2547 m_tooltip_element->setVisible(true);
2548 this->bringToFront(m_tooltip_element);
2555 m_tooltip_element->draw();
2558 Draw dragged item stack
2562 skin->setFont(old_font);
2565 void GUIFormSpecMenu::updateSelectedItem()
2567 // If the selected stack has become empty for some reason, deselect it.
2568 // If the selected stack has become inaccessible, deselect it.
2569 // If the selected stack has become smaller, adjust m_selected_amount.
2570 ItemStack selected = verifySelectedItem();
2572 // WARNING: BLACK MAGIC
2573 // See if there is a stack suited for our current guess.
2574 // If such stack does not exist, clear the guess.
2575 if(m_selected_content_guess.name != "" &&
2576 selected.name == m_selected_content_guess.name &&
2577 selected.count == m_selected_content_guess.count){
2578 // Selected item fits the guess. Skip the black magic.
2580 else if(m_selected_content_guess.name != ""){
2582 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2583 const ListDrawSpec &s = m_inventorylists[i];
2584 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2587 InventoryList *list = inv->getList(s.listname);
2590 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2591 u32 item_i = i + s.start_item_i;
2592 if(item_i >= list->getSize())
2594 ItemStack stack = list->getItem(item_i);
2595 if(stack.name == m_selected_content_guess.name &&
2596 stack.count == m_selected_content_guess.count){
2598 infostream<<"Client: Changing selected content guess to "
2599 <<s.inventoryloc.dump()<<" "<<s.listname
2600 <<" "<<item_i<<std::endl;
2601 delete m_selected_item;
2602 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2603 m_selected_amount = stack.count;
2608 infostream<<"Client: Discarding selected content guess: "
2609 <<m_selected_content_guess.getItemString()<<std::endl;
2610 m_selected_content_guess.name = "";
2614 // If craftresult is nonempty and nothing else is selected, select it now.
2615 if(!m_selected_item)
2617 for(u32 i=0; i<m_inventorylists.size(); i++)
2619 const ListDrawSpec &s = m_inventorylists[i];
2620 if(s.listname == "craftpreview")
2622 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2623 InventoryList *list = inv->getList("craftresult");
2624 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2626 m_selected_item = new ItemSpec;
2627 m_selected_item->inventoryloc = s.inventoryloc;
2628 m_selected_item->listname = "craftresult";
2629 m_selected_item->i = 0;
2630 m_selected_amount = 0;
2631 m_selected_dragging = false;
2638 // If craftresult is selected, keep the whole stack selected
2639 if(m_selected_item && m_selected_item->listname == "craftresult")
2641 m_selected_amount = verifySelectedItem().count;
2645 ItemStack GUIFormSpecMenu::verifySelectedItem()
2647 // If the selected stack has become empty for some reason, deselect it.
2648 // If the selected stack has become inaccessible, deselect it.
2649 // If the selected stack has become smaller, adjust m_selected_amount.
2650 // Return the selected stack.
2654 if(m_selected_item->isValid())
2656 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2659 InventoryList *list = inv->getList(m_selected_item->listname);
2660 if(list && (u32) m_selected_item->i < list->getSize())
2662 ItemStack stack = list->getItem(m_selected_item->i);
2663 if(m_selected_amount > stack.count)
2664 m_selected_amount = stack.count;
2671 // selection was not valid
2672 delete m_selected_item;
2673 m_selected_item = NULL;
2674 m_selected_amount = 0;
2675 m_selected_dragging = false;
2680 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2686 if (quitmode == quit_mode_accept) {
2687 fields["quit"] = "true";
2690 if (quitmode == quit_mode_cancel) {
2691 fields["quit"] = "true";
2692 m_text_dst->gotText(fields);
2696 if (current_keys_pending.key_down) {
2697 fields["key_down"] = "true";
2698 current_keys_pending.key_down = false;
2701 if (current_keys_pending.key_up) {
2702 fields["key_up"] = "true";
2703 current_keys_pending.key_up = false;
2706 if (current_keys_pending.key_enter) {
2707 fields["key_enter"] = "true";
2708 current_keys_pending.key_enter = false;
2711 if (!current_field_enter_pending.empty()) {
2712 fields["key_enter_field"] = current_field_enter_pending;
2713 current_field_enter_pending = "";
2716 if (current_keys_pending.key_escape) {
2717 fields["key_escape"] = "true";
2718 current_keys_pending.key_escape = false;
2721 for(unsigned int i=0; i<m_fields.size(); i++) {
2722 const FieldSpec &s = m_fields[i];
2724 std::string name = s.fname;
2725 if (s.ftype == f_Button) {
2726 fields[name] = wide_to_utf8(s.flabel);
2727 } else if (s.ftype == f_Table) {
2728 GUITable *table = getTable(s.fname);
2730 fields[name] = table->checkEvent();
2733 else if(s.ftype == f_DropDown) {
2734 // no dynamic cast possible due to some distributions shipped
2735 // without rtti support in irrlicht
2736 IGUIElement * element = getElementFromId(s.fid);
2737 gui::IGUIComboBox *e = NULL;
2738 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2739 e = static_cast<gui::IGUIComboBox*>(element);
2741 s32 selected = e->getSelected();
2742 if (selected >= 0) {
2743 std::vector<std::string> *dropdown_values =
2744 getDropDownValues(s.fname);
2745 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2746 fields[name] = (*dropdown_values)[selected];
2750 else if (s.ftype == f_TabHeader) {
2751 // no dynamic cast possible due to some distributions shipped
2752 // without rtti support in irrlicht
2753 IGUIElement * element = getElementFromId(s.fid);
2754 gui::IGUITabControl *e = NULL;
2755 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2756 e = static_cast<gui::IGUITabControl*>(element);
2760 std::stringstream ss;
2761 ss << (e->getActiveTab() +1);
2762 fields[name] = ss.str();
2765 else if (s.ftype == f_CheckBox) {
2766 // no dynamic cast possible due to some distributions shipped
2767 // without rtti support in irrlicht
2768 IGUIElement * element = getElementFromId(s.fid);
2769 gui::IGUICheckBox *e = NULL;
2770 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2771 e = static_cast<gui::IGUICheckBox*>(element);
2776 fields[name] = "true";
2778 fields[name] = "false";
2781 else if (s.ftype == f_ScrollBar) {
2782 // no dynamic cast possible due to some distributions shipped
2783 // without rtti support in irrlicht
2784 IGUIElement * element = getElementFromId(s.fid);
2785 gui::IGUIScrollBar *e = NULL;
2786 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2787 e = static_cast<gui::IGUIScrollBar*>(element);
2791 std::stringstream os;
2793 if (s.fdefault == L"Changed")
2794 fields[name] = "CHG:" + os.str();
2796 fields[name] = "VAL:" + os.str();
2801 IGUIElement* e = getElementFromId(s.fid);
2803 fields[name] = wide_to_utf8(e->getText());
2809 m_text_dst->gotText(fields);
2813 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2815 while(tocheck != NULL) {
2816 if (tocheck == parent) {
2819 tocheck = tocheck->getParent();
2824 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2826 // The IGUITabControl renders visually using the skin's selected
2827 // font, which we override for the duration of form drawing,
2828 // but computes tab hotspots based on how it would have rendered
2829 // using the font that is selected at the time of button release.
2830 // To make these two consistent, temporarily override the skin's
2831 // font while the IGUITabControl is processing the event.
2832 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2833 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2834 s32 x = event.MouseInput.X;
2835 s32 y = event.MouseInput.Y;
2836 gui::IGUIElement *hovered =
2837 Environment->getRootGUIElement()->getElementFromPoint(
2838 core::position2d<s32>(x, y));
2839 if (hovered && isMyChild(hovered) &&
2840 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2841 gui::IGUISkin* skin = Environment->getSkin();
2842 sanity_check(skin != NULL);
2843 gui::IGUIFont *old_font = skin->getFont();
2844 skin->setFont(m_font);
2845 bool retval = hovered->OnEvent(event);
2846 skin->setFont(old_font);
2851 // Fix Esc/Return key being eaten by checkboxen and tables
2852 if(event.EventType==EET_KEY_INPUT_EVENT) {
2853 KeyPress kp(event.KeyInput);
2854 if (kp == EscapeKey || kp == CancelKey
2855 || kp == getKeySetting("keymap_inventory")
2856 || event.KeyInput.Key==KEY_RETURN) {
2857 gui::IGUIElement *focused = Environment->getFocus();
2858 if (focused && isMyChild(focused) &&
2859 (focused->getType() == gui::EGUIET_LIST_BOX ||
2860 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2866 // Mouse wheel events: send to hovered element instead of focused
2867 if(event.EventType==EET_MOUSE_INPUT_EVENT
2868 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2869 s32 x = event.MouseInput.X;
2870 s32 y = event.MouseInput.Y;
2871 gui::IGUIElement *hovered =
2872 Environment->getRootGUIElement()->getElementFromPoint(
2873 core::position2d<s32>(x, y));
2874 if (hovered && isMyChild(hovered)) {
2875 hovered->OnEvent(event);
2880 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2881 s32 x = event.MouseInput.X;
2882 s32 y = event.MouseInput.Y;
2883 gui::IGUIElement *hovered =
2884 Environment->getRootGUIElement()->getElementFromPoint(
2885 core::position2d<s32>(x, y));
2886 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2887 m_old_tooltip_id = -1;
2888 m_old_tooltip = L"";
2890 if (!isChild(hovered,this)) {
2891 if (DoubleClickDetection(event)) {
2898 // display software keyboard when clicking edit boxes
2899 if (event.EventType == EET_MOUSE_INPUT_EVENT
2900 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2901 gui::IGUIElement *hovered =
2902 Environment->getRootGUIElement()->getElementFromPoint(
2903 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2904 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2905 bool retval = hovered->OnEvent(event);
2907 Environment->setFocus(hovered);
2909 m_JavaDialogFieldName = getNameByID(hovered->getID());
2910 std::string message = gettext("Enter ");
2911 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
2915 message += gettext(label) + ":";
2917 /* single line text input */
2920 /* multi line text input */
2921 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2925 /* passwords are always single line */
2926 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2930 porting::showInputDialog(gettext("ok"), "",
2931 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
2937 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2940 memset(&translated, 0, sizeof(SEvent));
2941 translated.EventType = EET_MOUSE_INPUT_EVENT;
2942 gui::IGUIElement* root = Environment->getRootGUIElement();
2946 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2950 gui::IGUIElement* hovered = root->getElementFromPoint(
2951 core::position2d<s32>(
2953 event.TouchInput.Y));
2955 translated.MouseInput.X = event.TouchInput.X;
2956 translated.MouseInput.Y = event.TouchInput.Y;
2957 translated.MouseInput.Control = false;
2959 bool dont_send_event = false;
2961 if (event.TouchInput.touchedCount == 1) {
2962 switch (event.TouchInput.Event) {
2963 case ETIE_PRESSED_DOWN:
2964 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2965 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2966 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2967 m_down_pos = m_pointer;
2970 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2971 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2972 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2975 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2976 translated.MouseInput.ButtonStates = 0;
2977 hovered = root->getElementFromPoint(m_down_pos);
2978 /* we don't have a valid pointer element use last
2979 * known pointer pos */
2980 translated.MouseInput.X = m_pointer.X;
2981 translated.MouseInput.Y = m_pointer.Y;
2983 /* reset down pos */
2984 m_down_pos = v2s32(0,0);
2987 dont_send_event = true;
2988 //this is not supposed to happen
2990 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2991 << event.TouchInput.Event << std::endl;
2993 } else if ( (event.TouchInput.touchedCount == 2) &&
2994 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2995 hovered = root->getElementFromPoint(m_down_pos);
2997 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2998 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2999 translated.MouseInput.X = m_pointer.X;
3000 translated.MouseInput.Y = m_pointer.Y;
3003 hovered->OnEvent(translated);
3006 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
3007 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3011 hovered->OnEvent(translated);
3013 dont_send_event = true;
3015 /* ignore unhandled 2 touch events ... accidental moving for example */
3016 else if (event.TouchInput.touchedCount == 2) {
3017 dont_send_event = true;
3019 else if (event.TouchInput.touchedCount > 2) {
3021 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
3022 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3025 if (dont_send_event) {
3029 /* check if translated event needs to be preprocessed again */
3030 if (preprocessEvent(translated)) {
3035 bool retval = hovered->OnEvent(translated);
3037 if (event.TouchInput.Event == ETIE_LEFT_UP) {
3039 m_pointer = v2s32(0,0);
3047 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3048 /* TODO add a check like:
3049 if (event.JoystickEvent != joystick_we_listen_for)
3052 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3054 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3056 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3058 acceptInput(quit_mode_accept);
3069 /******************************************************************************/
3070 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3072 /* The following code is for capturing double-clicks of the mouse button
3073 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3074 * -- which closes the form -- under some circumstances.
3076 * There have been many github issues reporting this as a bug even though it
3077 * was an intended feature. For this reason, remapping the double-click as
3078 * an ESC must be explicitly set when creating this class via the
3079 * /p remap_dbl_click parameter of the constructor.
3082 if (!m_remap_dbl_click)
3085 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3086 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3087 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3089 m_doubleclickdetect[1].pos = m_pointer;
3090 m_doubleclickdetect[1].time = getTimeMs();
3092 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3093 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
3098 double squaredistance =
3099 m_doubleclickdetect[0].pos
3100 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3102 if (squaredistance > (30*30)) {
3106 SEvent* translated = new SEvent();
3107 assert(translated != 0);
3108 //translate doubleclick to escape
3109 memset(translated, 0, sizeof(SEvent));
3110 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3111 translated->KeyInput.Key = KEY_ESCAPE;
3112 translated->KeyInput.Control = false;
3113 translated->KeyInput.Shift = false;
3114 translated->KeyInput.PressedDown = true;
3115 translated->KeyInput.Char = 0;
3116 OnEvent(*translated);
3118 // no need to send the key up event as we're already deleted
3119 // and no one else did notice this event
3127 void GUIFormSpecMenu::tryClose()
3131 acceptInput(quit_mode_cancel);
3134 m_text_dst->gotText(L"MenuQuit");
3138 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3140 if (event.EventType==EET_KEY_INPUT_EVENT) {
3141 KeyPress kp(event.KeyInput);
3142 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3143 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3146 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3147 (kp == getKeySetting("keymap_screenshot"))) {
3148 m_client->makeScreenshot(m_device);
3150 if (event.KeyInput.PressedDown &&
3151 (event.KeyInput.Key==KEY_RETURN ||
3152 event.KeyInput.Key==KEY_UP ||
3153 event.KeyInput.Key==KEY_DOWN)
3155 switch (event.KeyInput.Key) {
3157 current_keys_pending.key_enter = true;
3160 current_keys_pending.key_up = true;
3163 current_keys_pending.key_down = true;
3167 //can't happen at all!
3168 FATAL_ERROR("Reached a source line that can't ever been reached");
3171 if (current_keys_pending.key_enter && m_allowclose) {
3172 acceptInput(quit_mode_accept);
3182 /* Mouse event other than movement, or crossing the border of inventory
3183 field while holding right mouse button
3185 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3186 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3187 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3188 event.MouseInput.isRightPressed() &&
3189 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3191 // Get selected item and hovered/clicked item (s)
3193 m_old_tooltip_id = -1;
3194 updateSelectedItem();
3195 ItemSpec s = getItemAtPos(m_pointer);
3197 Inventory *inv_selected = NULL;
3198 Inventory *inv_s = NULL;
3199 InventoryList *list_s = NULL;
3201 if (m_selected_item) {
3202 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3203 sanity_check(inv_selected);
3204 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3211 inv_s = m_invmgr->getInventory(s.inventoryloc);
3214 errorstream << "InventoryMenu: The selected inventory location "
3215 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3217 s.i = -1; // make it invalid again
3221 list_s = inv_s->getList(s.listname);
3222 if (list_s == NULL) {
3223 verbosestream << "InventoryMenu: The selected inventory list \""
3224 << s.listname << "\" does not exist" << std::endl;
3225 s.i = -1; // make it invalid again
3229 if ((u32)s.i >= list_s->getSize()) {
3230 infostream << "InventoryMenu: The selected inventory list \""
3231 << s.listname << "\" is too small (i=" << s.i << ", size="
3232 << list_s->getSize() << ")" << std::endl;
3233 s.i = -1; // make it invalid again
3237 s_count = list_s->getItem(s.i).count;
3240 bool identical = (m_selected_item != NULL) && s.isValid() &&
3241 (inv_selected == inv_s) &&
3242 (m_selected_item->listname == s.listname) &&
3243 (m_selected_item->i == s.i);
3245 // buttons: 0 = left, 1 = right, 2 = middle
3246 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3249 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3250 { button = 0; updown = 0; }
3251 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3252 { button = 1; updown = 0; }
3253 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3254 { button = 2; updown = 0; }
3255 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3256 { button = 0; updown = 1; }
3257 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3258 { button = 1; updown = 1; }
3259 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3260 { button = 2; updown = 1; }
3261 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3264 // Set this number to a positive value to generate a move action
3265 // from m_selected_item to s.
3266 u32 move_amount = 0;
3268 // Set this number to a positive value to generate a move action
3269 // from s to the next inventory ring.
3270 u32 shift_move_amount = 0;
3272 // Set this number to a positive value to generate a drop action
3273 // from m_selected_item.
3274 u32 drop_amount = 0;
3276 // Set this number to a positive value to generate a craft action at s.
3277 u32 craft_amount = 0;
3280 // Some mouse button has been pressed
3282 //infostream<<"Mouse button "<<button<<" pressed at p=("
3283 // <<p.X<<","<<p.Y<<")"<<std::endl;
3285 m_selected_dragging = false;
3287 if (s.isValid() && s.listname == "craftpreview") {
3288 // Craft preview has been clicked: craft
3289 craft_amount = (button == 2 ? 10 : 1);
3290 } else if (m_selected_item == NULL) {
3292 // Non-empty stack has been clicked: select or shift-move it
3293 m_selected_item = new ItemSpec(s);
3296 if (button == 1) // right
3297 count = (s_count + 1) / 2;
3298 else if (button == 2) // middle
3299 count = MYMIN(s_count, 10);
3303 if (!event.MouseInput.Shift) {
3304 // no shift: select item
3305 m_selected_amount = count;
3306 m_selected_dragging = true;
3307 m_rmouse_auto_place = false;
3309 // shift pressed: move item
3311 shift_move_amount = count;
3312 else // count of 1 at left click like after drag & drop
3313 shift_move_amount = 1;
3316 } else { // m_selected_item != NULL
3317 assert(m_selected_amount >= 1);
3320 // Clicked a slot: move
3321 if (button == 1) // right
3323 else if (button == 2) // middle
3324 move_amount = MYMIN(m_selected_amount, 10);
3326 move_amount = m_selected_amount;
3329 if (move_amount >= m_selected_amount)
3330 m_selected_amount = 0;
3332 m_selected_amount -= move_amount;
3336 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3337 // Clicked outside of the window: drop
3338 if (button == 1) // right
3340 else if (button == 2) // middle
3341 drop_amount = MYMIN(m_selected_amount, 10);
3343 drop_amount = m_selected_amount;
3347 else if (updown == 1) {
3348 // Some mouse button has been released
3350 //infostream<<"Mouse button "<<button<<" released at p=("
3351 // <<p.X<<","<<p.Y<<")"<<std::endl;
3353 if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3355 // Dragged to different slot: move all selected
3356 move_amount = m_selected_amount;
3358 } else if (m_selected_item != NULL && m_selected_dragging &&
3359 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3360 // Dragged outside of window: drop all selected
3361 drop_amount = m_selected_amount;
3364 m_selected_dragging = false;
3365 // Keep count of how many times right mouse button has been
3366 // clicked. One click is drag without dropping. Click + release
3367 // + click changes to drop one item when moved mode
3368 if (button == 1 && m_selected_item != NULL)
3369 m_rmouse_auto_place = !m_rmouse_auto_place;
3370 } else if (updown == -1) {
3371 // Mouse has been moved and rmb is down and mouse pointer just
3372 // entered a new inventory field (checked in the entry-if, this
3373 // is the only action here that is generated by mouse movement)
3374 if (m_selected_item != NULL && s.isValid()) {
3376 // TODO: middle mouse to move 10 items might be handy
3377 if (m_rmouse_auto_place) {
3378 // Only move an item if the destination slot is empty
3379 // or contains the same item type as what is going to be
3381 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3382 InventoryList *list_to = list_s;
3383 assert(list_from && list_to);
3384 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3385 ItemStack stack_to = list_to->getItem(s.i);
3386 if (stack_to.empty() || stack_to.name == stack_from.name)
3392 // Possibly send inventory action to server
3393 if (move_amount > 0) {
3394 // Send IACTION_MOVE
3396 assert(m_selected_item && m_selected_item->isValid());
3397 assert(s.isValid());
3399 assert(inv_selected && inv_s);
3400 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3401 InventoryList *list_to = list_s;
3402 assert(list_from && list_to);
3403 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3404 ItemStack stack_to = list_to->getItem(s.i);
3406 // Check how many items can be moved
3407 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3408 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3409 // If source stack cannot be added to destination stack at all,
3411 if ((leftover.count == stack_from.count) &&
3412 (leftover.name == stack_from.name)) {
3413 m_selected_amount = stack_to.count;
3414 // In case the server doesn't directly swap them but instead
3415 // moves stack_to somewhere else, set this
3416 m_selected_content_guess = stack_to;
3417 m_selected_content_guess_inventory = s.inventoryloc;
3419 // Source stack goes fully into destination stack
3420 else if (leftover.empty()) {
3421 m_selected_amount -= move_amount;
3422 m_selected_content_guess = ItemStack(); // Clear
3424 // Source stack goes partly into destination stack
3426 move_amount -= leftover.count;
3427 m_selected_amount -= move_amount;
3428 m_selected_content_guess = ItemStack(); // Clear
3431 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3432 IMoveAction *a = new IMoveAction();
3433 a->count = move_amount;
3434 a->from_inv = m_selected_item->inventoryloc;
3435 a->from_list = m_selected_item->listname;
3436 a->from_i = m_selected_item->i;
3437 a->to_inv = s.inventoryloc;
3438 a->to_list = s.listname;
3440 m_invmgr->inventoryAction(a);
3441 } else if (shift_move_amount > 0) {
3442 u32 mis = m_inventory_rings.size();
3444 for (; i < mis; i++) {
3445 const ListRingSpec &sp = m_inventory_rings[i];
3446 if (sp.inventoryloc == s.inventoryloc
3447 && sp.listname == s.listname)
3451 if (i >= mis) // if not found
3453 u32 to_inv_ind = (i + 1) % mis;
3454 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3455 InventoryList *list_from = list_s;
3458 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3461 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3464 ItemStack stack_from = list_from->getItem(s.i);
3465 assert(shift_move_amount <= stack_from.count);
3466 if (m_client->getProtoVersion() >= 25) {
3467 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3468 IMoveAction *a = new IMoveAction();
3469 a->count = shift_move_amount;
3470 a->from_inv = s.inventoryloc;
3471 a->from_list = s.listname;
3473 a->to_inv = to_inv_sp.inventoryloc;
3474 a->to_list = to_inv_sp.listname;
3475 a->move_somewhere = true;
3476 m_invmgr->inventoryAction(a);
3478 // find a place (or more than one) to add the new item
3479 u32 ilt_size = list_to->getSize();
3481 for (u32 slot_to = 0; slot_to < ilt_size
3482 && shift_move_amount > 0; slot_to++) {
3483 list_to->itemFits(slot_to, stack_from, &leftover);
3484 if (leftover.count < stack_from.count) {
3485 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3486 IMoveAction *a = new IMoveAction();
3487 a->count = MYMIN(shift_move_amount,
3488 (u32) (stack_from.count - leftover.count));
3489 shift_move_amount -= a->count;
3490 a->from_inv = s.inventoryloc;
3491 a->from_list = s.listname;
3493 a->to_inv = to_inv_sp.inventoryloc;
3494 a->to_list = to_inv_sp.listname;
3496 m_invmgr->inventoryAction(a);
3497 stack_from = leftover;
3502 } else if (drop_amount > 0) {
3503 m_selected_content_guess = ItemStack(); // Clear
3505 // Send IACTION_DROP
3507 assert(m_selected_item && m_selected_item->isValid());
3508 assert(inv_selected);
3509 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3511 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3513 // Check how many items can be dropped
3514 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3515 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3516 m_selected_amount -= drop_amount;
3518 infostream << "Handing IACTION_DROP to manager" << std::endl;
3519 IDropAction *a = new IDropAction();
3520 a->count = drop_amount;
3521 a->from_inv = m_selected_item->inventoryloc;
3522 a->from_list = m_selected_item->listname;
3523 a->from_i = m_selected_item->i;
3524 m_invmgr->inventoryAction(a);
3525 } else if (craft_amount > 0) {
3526 m_selected_content_guess = ItemStack(); // Clear
3528 // Send IACTION_CRAFT
3530 assert(s.isValid());
3533 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3534 ICraftAction *a = new ICraftAction();
3535 a->count = craft_amount;
3536 a->craft_inv = s.inventoryloc;
3537 m_invmgr->inventoryAction(a);
3540 // If m_selected_amount has been decreased to zero, deselect
3541 if (m_selected_amount == 0) {
3542 delete m_selected_item;
3543 m_selected_item = NULL;
3544 m_selected_amount = 0;
3545 m_selected_dragging = false;
3546 m_selected_content_guess = ItemStack();
3548 m_old_pointer = m_pointer;
3550 if (event.EventType == EET_GUI_EVENT) {
3552 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3554 // find the element that was clicked
3555 for (unsigned int i=0; i<m_fields.size(); i++) {
3556 FieldSpec &s = m_fields[i];
3557 if ((s.ftype == f_TabHeader) &&
3558 (s.fid == event.GUIEvent.Caller->getID())) {
3566 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3568 if (!canTakeFocus(event.GUIEvent.Element)) {
3569 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3571 // Returning true disables focus change
3575 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3576 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3577 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3578 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3579 unsigned int btn_id = event.GUIEvent.Caller->getID();
3581 if (btn_id == 257) {
3583 acceptInput(quit_mode_accept);
3587 m_text_dst->gotText(L"ExitButton");
3589 // quitMenu deallocates menu
3593 // find the element that was clicked
3594 for (u32 i = 0; i < m_fields.size(); i++) {
3595 FieldSpec &s = m_fields[i];
3596 // if its a button, set the send field so
3597 // lua knows which button was pressed
3598 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3599 (s.fid == event.GUIEvent.Caller->getID())) {
3603 acceptInput(quit_mode_accept);
3606 m_text_dst->gotText(L"ExitButton");
3610 acceptInput(quit_mode_no);
3614 } else if ((s.ftype == f_DropDown) &&
3615 (s.fid == event.GUIEvent.Caller->getID())) {
3616 // only send the changed dropdown
3617 for (u32 i = 0; i < m_fields.size(); i++) {
3618 FieldSpec &s2 = m_fields[i];
3619 if (s2.ftype == f_DropDown) {
3624 acceptInput(quit_mode_no);
3626 // revert configuration to make sure dropdowns are sent on
3627 // regular button click
3628 for (u32 i = 0; i < m_fields.size(); i++) {
3629 FieldSpec &s2 = m_fields[i];
3630 if (s2.ftype == f_DropDown) {
3635 } else if ((s.ftype == f_ScrollBar) &&
3636 (s.fid == event.GUIEvent.Caller->getID())) {
3637 s.fdefault = L"Changed";
3638 acceptInput(quit_mode_no);
3644 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3645 if (event.GUIEvent.Caller->getID() > 257) {
3646 bool close_on_enter = true;
3647 for (u32 i = 0; i < m_fields.size(); i++) {
3648 FieldSpec &s = m_fields[i];
3649 if (s.ftype == f_Unknown &&
3650 s.fid == event.GUIEvent.Caller->getID()) {
3651 current_field_enter_pending = s.fname;
3652 close_on_enter = s.close_on_enter;
3657 if (m_allowclose && close_on_enter) {
3658 current_keys_pending.key_enter = true;
3659 acceptInput(quit_mode_accept);
3662 current_keys_pending.key_enter = true;
3665 // quitMenu deallocates menu
3670 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3671 int current_id = event.GUIEvent.Caller->getID();
3672 if (current_id > 257) {
3673 // find the element that was clicked
3674 for (u32 i = 0; i < m_fields.size(); i++) {
3675 FieldSpec &s = m_fields[i];
3676 // if it's a table, set the send field
3677 // so lua knows which table was changed
3678 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3689 return Parent ? Parent->OnEvent(event) : false;
3693 * get name of element by element id
3694 * @param id of element
3695 * @return name string or empty string
3697 std::string GUIFormSpecMenu::getNameByID(s32 id)
3699 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3700 iter != m_fields.end(); ++iter) {
3701 if (iter->fid == id) {
3709 * get label of element by id
3710 * @param id of element
3711 * @return label string or empty string
3713 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3715 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3716 iter != m_fields.end(); ++iter) {
3717 if (iter->fid == id) {
3718 return iter->flabel;