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 (parts.size() == 4) {
641 m_clipbackground = is_yes(parts[3]);
642 if (m_clipbackground) {
643 pos.X = stoi(v_pos[0]); //acts as offset
644 pos.Y = stoi(v_pos[1]); //acts as offset
648 if(!data->explicit_size)
649 warningstream<<"invalid use of background without a size[] element"<<std::endl;
650 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
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;
1895 m_clipbackground = false;
1898 assert(m_tooltip_element == NULL);
1899 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1900 m_tooltip_element = addStaticText(Environment, L"",core::rect<s32>(0,0,110,18));
1901 m_tooltip_element->enableOverrideColor(true);
1902 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1903 m_tooltip_element->setDrawBackground(true);
1904 m_tooltip_element->setDrawBorder(true);
1905 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1906 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1907 m_tooltip_element->setWordWrap(false);
1908 //we're not parent so no autograb for this one!
1909 m_tooltip_element->grab();
1912 std::vector<std::string> elements = split(m_formspec_string,']');
1915 /* try to read version from first element only */
1916 if (elements.size() >= 1) {
1917 if ( parseVersionDirect(elements[0]) ) {
1922 /* we need size first in order to calculate image scale */
1923 mydata.explicit_size = false;
1924 for (; i< elements.size(); i++) {
1925 if (!parseSizeDirect(&mydata, elements[i])) {
1930 if (mydata.explicit_size) {
1931 // compute scaling for specified form size
1933 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1934 v2u32 delta = current_screensize - m_lockscreensize;
1936 if (current_screensize.Y > m_lockscreensize.Y)
1941 if (current_screensize.X > m_lockscreensize.X)
1946 offset = v2s32(delta.X,delta.Y);
1948 mydata.screensize = m_lockscreensize;
1950 offset = v2s32(0,0);
1953 double gui_scaling = g_settings->getFloat("gui_scaling");
1954 double screen_dpi = porting::getDisplayDensity() * 96;
1958 // In fixed-size mode, inventory image size
1959 // is 0.53 inch multiplied by the gui_scaling
1960 // config parameter. This magic size is chosen
1961 // to make the main menu (15.5 inventory images
1962 // wide, including border) just fit into the
1963 // default window (800 pixels wide) at 96 DPI
1964 // and default scaling (1.00).
1965 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1967 // In variable-size mode, we prefer to make the
1968 // inventory image size 1/15 of screen height,
1969 // multiplied by the gui_scaling config parameter.
1970 // If the preferred size won't fit the whole
1971 // form on the screen, either horizontally or
1972 // vertically, then we scale it down to fit.
1973 // (The magic numbers in the computation of what
1974 // fits arise from the scaling factors in the
1975 // following stanza, including the form border,
1976 // help text space, and 0.1 inventory slot spare.)
1977 // However, a minimum size is also set, that
1978 // the image size can't be less than 0.3 inch
1979 // multiplied by gui_scaling, even if this means
1980 // the form doesn't fit the screen.
1981 double prefer_imgsize = mydata.screensize.Y / 15 *
1983 double fitx_imgsize = mydata.screensize.X /
1984 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1985 double fity_imgsize = mydata.screensize.Y /
1986 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1987 double screen_dpi = porting::getDisplayDensity() * 96;
1988 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1989 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1990 MYMIN(fitx_imgsize, fity_imgsize)));
1993 // Everything else is scaled in proportion to the
1994 // inventory image size. The inventory slot spacing
1995 // is 5/4 image size horizontally and 15/13 image size
1996 // vertically. The padding around the form (incorporating
1997 // the border of the outer inventory slots) is 3/8
1998 // image size. Font height (baseline to baseline)
1999 // is 2/5 vertical inventory slot spacing, and button
2000 // half-height is 7/8 of font height.
2001 imgsize = v2s32(use_imgsize, use_imgsize);
2002 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
2003 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
2004 m_btn_height = use_imgsize*15.0/13 * 0.35;
2006 m_font = g_fontengine->getFont();
2008 mydata.size = v2s32(
2009 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2010 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2012 DesiredRect = mydata.rect = core::rect<s32>(
2013 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
2014 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
2015 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
2016 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
2019 // Non-size[] form must consist only of text fields and
2020 // implicit "Proceed" button. Use default font, and
2021 // temporary form size which will be recalculated below.
2022 m_font = g_fontengine->getFont();
2023 m_btn_height = font_line_height(m_font) * 0.875;
2024 DesiredRect = core::rect<s32>(
2025 mydata.screensize.X/2 - 580/2,
2026 mydata.screensize.Y/2 - 300/2,
2027 mydata.screensize.X/2 + 580/2,
2028 mydata.screensize.Y/2 + 300/2
2031 recalculateAbsolutePosition(false);
2032 mydata.basepos = getBasePos();
2033 m_tooltip_element->setOverrideFont(m_font);
2035 gui::IGUISkin* skin = Environment->getSkin();
2036 sanity_check(skin != NULL);
2037 gui::IGUIFont *old_font = skin->getFont();
2038 skin->setFont(m_font);
2040 for (; i< elements.size(); i++) {
2041 parseElement(&mydata, elements[i]);
2044 // If there are fields without explicit size[], add a "Proceed"
2045 // button and adjust size to fit all the fields.
2046 if (m_fields.size() && !mydata.explicit_size) {
2047 mydata.rect = core::rect<s32>(
2048 mydata.screensize.X/2 - 580/2,
2049 mydata.screensize.Y/2 - 300/2,
2050 mydata.screensize.X/2 + 580/2,
2051 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2053 DesiredRect = mydata.rect;
2054 recalculateAbsolutePosition(false);
2055 mydata.basepos = getBasePos();
2058 v2s32 pos = mydata.basepos;
2059 pos.Y = ((m_fields.size()+2)*60);
2061 v2s32 size = DesiredRect.getSize();
2063 core::rect<s32>(size.X/2-70, pos.Y,
2064 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2065 const wchar_t *text = wgettext("Proceed");
2066 Environment->addButton(mydata.rect, this, 257, text);
2072 //set initial focus if parser didn't set it
2073 focused_element = Environment->getFocus();
2074 if (!focused_element
2075 || !isMyChild(focused_element)
2076 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2079 skin->setFont(old_font);
2083 bool GUIFormSpecMenu::getAndroidUIInput()
2085 /* no dialog shown */
2086 if (m_JavaDialogFieldName == "") {
2091 if (porting::getInputDialogState() == -1) {
2095 std::string fieldname = m_JavaDialogFieldName;
2096 m_JavaDialogFieldName = "";
2098 /* no value abort dialog processing */
2099 if (porting::getInputDialogState() != 0) {
2103 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2104 iter != m_fields.end(); ++iter) {
2106 if (iter->fname != fieldname) {
2109 IGUIElement* tochange = getElementFromId(iter->fid);
2111 if (tochange == 0) {
2115 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2119 std::string text = porting::getInputDialogValue();
2121 ((gui::IGUIEditBox*) tochange)->
2122 setText(utf8_to_wide(text).c_str());
2128 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2130 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2132 for(u32 i=0; i<m_inventorylists.size(); i++)
2134 const ListDrawSpec &s = m_inventorylists[i];
2136 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2137 s32 item_i = i + s.start_item_i;
2138 s32 x = (i%s.geom.X) * spacing.X;
2139 s32 y = (i/s.geom.X) * spacing.Y;
2141 core::rect<s32> rect = imgrect + s.pos + p0;
2142 if(rect.isPointInside(p))
2144 return ItemSpec(s.inventoryloc, s.listname, item_i);
2149 return ItemSpec(InventoryLocation(), "", -1);
2152 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
2155 video::IVideoDriver* driver = Environment->getVideoDriver();
2157 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2159 warningstream<<"GUIFormSpecMenu::drawList(): "
2160 <<"The inventory location "
2161 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2165 InventoryList *ilist = inv->getList(s.listname);
2167 warningstream<<"GUIFormSpecMenu::drawList(): "
2168 <<"The inventory list \""<<s.listname<<"\" @ \""
2169 <<s.inventoryloc.dump()<<"\" doesn't exist"
2174 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2176 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2178 s32 item_i = i + s.start_item_i;
2179 if(item_i >= (s32) ilist->getSize())
2181 s32 x = (i%s.geom.X) * spacing.X;
2182 s32 y = (i/s.geom.X) * spacing.Y;
2184 core::rect<s32> rect = imgrect + s.pos + p;
2187 item = ilist->getItem(item_i);
2189 bool selected = m_selected_item
2190 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2191 && m_selected_item->listname == s.listname
2192 && m_selected_item->i == item_i;
2193 bool hovering = rect.isPointInside(m_pointer);
2194 ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2195 (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
2199 item_hovered = true;
2200 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2202 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2206 //Draw inv slot borders
2208 s32 x1 = rect.UpperLeftCorner.X;
2209 s32 y1 = rect.UpperLeftCorner.Y;
2210 s32 x2 = rect.LowerRightCorner.X;
2211 s32 y2 = rect.LowerRightCorner.Y;
2213 driver->draw2DRectangle(m_slotbordercolor,
2214 core::rect<s32>(v2s32(x1 - border, y1 - border),
2215 v2s32(x2 + border, y1)), NULL);
2216 driver->draw2DRectangle(m_slotbordercolor,
2217 core::rect<s32>(v2s32(x1 - border, y2),
2218 v2s32(x2 + border, y2 + border)), NULL);
2219 driver->draw2DRectangle(m_slotbordercolor,
2220 core::rect<s32>(v2s32(x1 - border, y1),
2221 v2s32(x1, y2)), NULL);
2222 driver->draw2DRectangle(m_slotbordercolor,
2223 core::rect<s32>(v2s32(x2, y1),
2224 v2s32(x2 + border, y2)), NULL);
2232 item.takeItem(m_selected_amount);
2236 drawItemStack(driver, m_font, item,
2237 rect, &AbsoluteClippingRect, m_gamedef,
2242 std::wstring tooltip_text = L"";
2243 if (hovering && !m_selected_item) {
2244 tooltip_text = utf8_to_wide(item.getDefinition(m_gamedef->idef()).description);
2246 if (tooltip_text != L"") {
2247 std::vector<std::wstring> tt_rows = str_split(tooltip_text, L'\n');
2248 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2249 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2250 m_tooltip_element->setVisible(true);
2251 this->bringToFront(m_tooltip_element);
2252 setStaticText(m_tooltip_element, tooltip_text.c_str());
2253 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2254 #if IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2
2255 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2257 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2259 v2u32 screenSize = driver->getScreenSize();
2260 int tooltip_offset_x = m_btn_height;
2261 int tooltip_offset_y = m_btn_height;
2263 tooltip_offset_x *= 3;
2264 tooltip_offset_y = 0;
2265 if (m_pointer.X > (s32)screenSize.X / 2)
2266 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2268 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2269 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2270 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2271 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2272 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2273 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2274 m_tooltip_element->setRelativePosition(core::rect<s32>(
2275 core::position2d<s32>(tooltip_x, tooltip_y),
2276 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2282 void GUIFormSpecMenu::drawSelectedItem()
2284 video::IVideoDriver* driver = Environment->getVideoDriver();
2286 if (!m_selected_item) {
2287 drawItemStack(driver, m_font, ItemStack(),
2288 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2289 NULL, m_gamedef, IT_ROT_DRAGGED);
2293 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2295 InventoryList *list = inv->getList(m_selected_item->listname);
2297 ItemStack stack = list->getItem(m_selected_item->i);
2298 stack.count = m_selected_amount;
2300 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2301 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2302 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, IT_ROT_DRAGGED);
2305 void GUIFormSpecMenu::drawMenu()
2308 std::string newform = m_form_src->getForm();
2309 if(newform != m_formspec_string){
2310 m_formspec_string = newform;
2311 regenerateGui(m_screensize_old);
2315 gui::IGUISkin* skin = Environment->getSkin();
2316 sanity_check(skin != NULL);
2317 gui::IGUIFont *old_font = skin->getFont();
2318 skin->setFont(m_font);
2320 updateSelectedItem();
2322 video::IVideoDriver* driver = Environment->getVideoDriver();
2324 v2u32 screenSize = driver->getScreenSize();
2325 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2327 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2329 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2331 m_tooltip_element->setVisible(false);
2336 for(u32 i=0; i<m_backgrounds.size(); i++)
2338 const ImageDrawSpec &spec = m_backgrounds[i];
2339 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2342 // Image size on screen
2343 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2344 // Image rectangle on screen
2345 core::rect<s32> rect = imgrect + spec.pos;
2347 if (m_clipbackground) {
2348 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2349 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2350 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2351 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2352 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2355 const video::SColor color(255,255,255,255);
2356 const video::SColor colors[] = {color,color,color,color};
2357 draw2DImageFilterScaled(driver, texture, rect,
2358 core::rect<s32>(core::position2d<s32>(0,0),
2359 core::dimension2di(texture->getOriginalSize())),
2360 NULL/*&AbsoluteClippingRect*/, colors, true);
2363 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2364 errorstream << "\t" << spec.name << std::endl;
2371 for(u32 i=0; i<m_boxes.size(); i++)
2373 const BoxDrawSpec &spec = m_boxes[i];
2375 irr::video::SColor todraw = spec.color;
2377 todraw.setAlpha(140);
2379 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2380 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2382 driver->draw2DRectangle(todraw, rect, 0);
2388 gui::IGUIElement::draw();
2393 for(u32 i=0; i<m_images.size(); i++)
2395 const ImageDrawSpec &spec = m_images[i];
2396 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2399 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2400 // Image size on screen
2401 core::rect<s32> imgrect;
2404 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2407 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2409 // Image rectangle on screen
2410 core::rect<s32> rect = imgrect + spec.pos;
2411 const video::SColor color(255,255,255,255);
2412 const video::SColor colors[] = {color,color,color,color};
2413 draw2DImageFilterScaled(driver, texture, rect,
2414 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2415 NULL/*&AbsoluteClippingRect*/, colors, true);
2418 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2419 errorstream << "\t" << spec.name << std::endl;
2426 for(u32 i=0; i<m_itemimages.size(); i++)
2431 const ImageDrawSpec &spec = m_itemimages[i];
2432 IItemDefManager *idef = m_gamedef->idef();
2434 item.deSerialize(spec.item_name, idef);
2435 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2436 // Viewport rectangle on screen
2437 core::rect<s32> rect = imgrect + spec.pos;
2438 if (spec.parent_button && spec.parent_button->isPressed()) {
2439 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2440 rect += core::dimension2d<s32>(
2441 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2443 rect += core::dimension2d<s32>(
2444 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2445 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2448 drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2449 m_gamedef, IT_ROT_NONE);
2454 Phase 0: Item slot rectangles
2455 Phase 1: Item images; prepare tooltip
2457 bool item_hovered = false;
2458 int start_phase = 0;
2459 for (int phase = start_phase; phase <= 1; phase++) {
2460 for (u32 i = 0; i < m_inventorylists.size(); i++) {
2461 drawList(m_inventorylists[i], phase, item_hovered);
2464 if (!item_hovered) {
2465 drawItemStack(driver, m_font, ItemStack(),
2466 core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2467 NULL, m_gamedef, IT_ROT_HOVERED);
2470 /* TODO find way to show tooltips on touchscreen */
2471 #ifndef HAVE_TOUCHSCREENGUI
2472 m_pointer = m_device->getCursorControl()->getPosition();
2476 Draw static text elements
2478 for (u32 i = 0; i < m_static_texts.size(); i++) {
2479 const StaticTextSpec &spec = m_static_texts[i];
2480 core::rect<s32> rect = spec.rect;
2481 if (spec.parent_button && spec.parent_button->isPressed()) {
2482 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
2483 rect += core::dimension2d<s32>(
2484 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
2486 // Use image offset instead of text's because its a bit smaller
2487 // and fits better, also TEXT_OFFSET_X is always 0
2488 rect += core::dimension2d<s32>(
2489 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
2490 skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
2493 video::SColor color(255, 255, 255, 255);
2494 m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
2498 Draw fields/buttons tooltips
2500 gui::IGUIElement *hovered =
2501 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2503 if (hovered != NULL) {
2504 s32 id = hovered->getID();
2508 m_old_tooltip_id = id;
2509 m_old_tooltip = L"";
2511 if (id == m_old_tooltip_id) {
2512 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2514 m_hovered_time = getTimeMs();
2515 m_old_tooltip_id = id;
2519 if (id != -1 && delta >= m_tooltip_show_delay) {
2520 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2521 iter != m_fields.end(); ++iter) {
2522 if (iter->fid == id && m_tooltips[iter->fname].tooltip != L"") {
2523 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2524 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2525 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2526 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2527 setStaticText(m_tooltip_element, m_tooltips[iter->fname].tooltip.c_str());
2528 std::vector<std::wstring> tt_rows = str_split(m_tooltips[iter->fname].tooltip, L'\n');
2529 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2530 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2531 int tooltip_offset_x = m_btn_height;
2532 int tooltip_offset_y = m_btn_height;
2534 tooltip_offset_x *= 3;
2535 tooltip_offset_y = 0;
2536 if (m_pointer.X > (s32)screenSize.X / 2)
2537 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2539 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2540 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2541 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2542 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2543 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2544 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2545 m_tooltip_element->setRelativePosition(core::rect<s32>(
2546 core::position2d<s32>(tooltip_x, tooltip_y),
2547 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2549 m_tooltip_element->setVisible(true);
2550 this->bringToFront(m_tooltip_element);
2557 m_tooltip_element->draw();
2560 Draw dragged item stack
2564 skin->setFont(old_font);
2567 void GUIFormSpecMenu::updateSelectedItem()
2569 // If the selected stack has become empty for some reason, deselect it.
2570 // If the selected stack has become inaccessible, deselect it.
2571 // If the selected stack has become smaller, adjust m_selected_amount.
2572 ItemStack selected = verifySelectedItem();
2574 // WARNING: BLACK MAGIC
2575 // See if there is a stack suited for our current guess.
2576 // If such stack does not exist, clear the guess.
2577 if(m_selected_content_guess.name != "" &&
2578 selected.name == m_selected_content_guess.name &&
2579 selected.count == m_selected_content_guess.count){
2580 // Selected item fits the guess. Skip the black magic.
2582 else if(m_selected_content_guess.name != ""){
2584 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2585 const ListDrawSpec &s = m_inventorylists[i];
2586 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2589 InventoryList *list = inv->getList(s.listname);
2592 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2593 u32 item_i = i + s.start_item_i;
2594 if(item_i >= list->getSize())
2596 ItemStack stack = list->getItem(item_i);
2597 if(stack.name == m_selected_content_guess.name &&
2598 stack.count == m_selected_content_guess.count){
2600 infostream<<"Client: Changing selected content guess to "
2601 <<s.inventoryloc.dump()<<" "<<s.listname
2602 <<" "<<item_i<<std::endl;
2603 delete m_selected_item;
2604 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2605 m_selected_amount = stack.count;
2610 infostream<<"Client: Discarding selected content guess: "
2611 <<m_selected_content_guess.getItemString()<<std::endl;
2612 m_selected_content_guess.name = "";
2616 // If craftresult is nonempty and nothing else is selected, select it now.
2617 if(!m_selected_item)
2619 for(u32 i=0; i<m_inventorylists.size(); i++)
2621 const ListDrawSpec &s = m_inventorylists[i];
2622 if(s.listname == "craftpreview")
2624 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2625 InventoryList *list = inv->getList("craftresult");
2626 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2628 m_selected_item = new ItemSpec;
2629 m_selected_item->inventoryloc = s.inventoryloc;
2630 m_selected_item->listname = "craftresult";
2631 m_selected_item->i = 0;
2632 m_selected_amount = 0;
2633 m_selected_dragging = false;
2640 // If craftresult is selected, keep the whole stack selected
2641 if(m_selected_item && m_selected_item->listname == "craftresult")
2643 m_selected_amount = verifySelectedItem().count;
2647 ItemStack GUIFormSpecMenu::verifySelectedItem()
2649 // If the selected stack has become empty for some reason, deselect it.
2650 // If the selected stack has become inaccessible, deselect it.
2651 // If the selected stack has become smaller, adjust m_selected_amount.
2652 // Return the selected stack.
2656 if(m_selected_item->isValid())
2658 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2661 InventoryList *list = inv->getList(m_selected_item->listname);
2662 if(list && (u32) m_selected_item->i < list->getSize())
2664 ItemStack stack = list->getItem(m_selected_item->i);
2665 if(m_selected_amount > stack.count)
2666 m_selected_amount = stack.count;
2673 // selection was not valid
2674 delete m_selected_item;
2675 m_selected_item = NULL;
2676 m_selected_amount = 0;
2677 m_selected_dragging = false;
2682 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2688 if (quitmode == quit_mode_accept) {
2689 fields["quit"] = "true";
2692 if (quitmode == quit_mode_cancel) {
2693 fields["quit"] = "true";
2694 m_text_dst->gotText(fields);
2698 if (current_keys_pending.key_down) {
2699 fields["key_down"] = "true";
2700 current_keys_pending.key_down = false;
2703 if (current_keys_pending.key_up) {
2704 fields["key_up"] = "true";
2705 current_keys_pending.key_up = false;
2708 if (current_keys_pending.key_enter) {
2709 fields["key_enter"] = "true";
2710 current_keys_pending.key_enter = false;
2713 if (!current_field_enter_pending.empty()) {
2714 fields["key_enter_field"] = current_field_enter_pending;
2715 current_field_enter_pending = "";
2718 if (current_keys_pending.key_escape) {
2719 fields["key_escape"] = "true";
2720 current_keys_pending.key_escape = false;
2723 for(unsigned int i=0; i<m_fields.size(); i++) {
2724 const FieldSpec &s = m_fields[i];
2726 std::string name = s.fname;
2727 if (s.ftype == f_Button) {
2728 fields[name] = wide_to_utf8(s.flabel);
2729 } else if (s.ftype == f_Table) {
2730 GUITable *table = getTable(s.fname);
2732 fields[name] = table->checkEvent();
2735 else if(s.ftype == f_DropDown) {
2736 // no dynamic cast possible due to some distributions shipped
2737 // without rtti support in irrlicht
2738 IGUIElement * element = getElementFromId(s.fid);
2739 gui::IGUIComboBox *e = NULL;
2740 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2741 e = static_cast<gui::IGUIComboBox*>(element);
2743 s32 selected = e->getSelected();
2744 if (selected >= 0) {
2745 std::vector<std::string> *dropdown_values =
2746 getDropDownValues(s.fname);
2747 if (dropdown_values && selected < (s32)dropdown_values->size()) {
2748 fields[name] = (*dropdown_values)[selected];
2752 else if (s.ftype == f_TabHeader) {
2753 // no dynamic cast possible due to some distributions shipped
2754 // without rtti support in irrlicht
2755 IGUIElement * element = getElementFromId(s.fid);
2756 gui::IGUITabControl *e = NULL;
2757 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2758 e = static_cast<gui::IGUITabControl*>(element);
2762 std::stringstream ss;
2763 ss << (e->getActiveTab() +1);
2764 fields[name] = ss.str();
2767 else if (s.ftype == f_CheckBox) {
2768 // no dynamic cast possible due to some distributions shipped
2769 // without rtti support in irrlicht
2770 IGUIElement * element = getElementFromId(s.fid);
2771 gui::IGUICheckBox *e = NULL;
2772 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2773 e = static_cast<gui::IGUICheckBox*>(element);
2778 fields[name] = "true";
2780 fields[name] = "false";
2783 else if (s.ftype == f_ScrollBar) {
2784 // no dynamic cast possible due to some distributions shipped
2785 // without rtti support in irrlicht
2786 IGUIElement * element = getElementFromId(s.fid);
2787 gui::IGUIScrollBar *e = NULL;
2788 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2789 e = static_cast<gui::IGUIScrollBar*>(element);
2793 std::stringstream os;
2795 if (s.fdefault == L"Changed")
2796 fields[name] = "CHG:" + os.str();
2798 fields[name] = "VAL:" + os.str();
2803 IGUIElement* e = getElementFromId(s.fid);
2805 fields[name] = wide_to_utf8(e->getText());
2811 m_text_dst->gotText(fields);
2815 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2817 while(tocheck != NULL) {
2818 if (tocheck == parent) {
2821 tocheck = tocheck->getParent();
2826 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2828 // The IGUITabControl renders visually using the skin's selected
2829 // font, which we override for the duration of form drawing,
2830 // but computes tab hotspots based on how it would have rendered
2831 // using the font that is selected at the time of button release.
2832 // To make these two consistent, temporarily override the skin's
2833 // font while the IGUITabControl is processing the event.
2834 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2835 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2836 s32 x = event.MouseInput.X;
2837 s32 y = event.MouseInput.Y;
2838 gui::IGUIElement *hovered =
2839 Environment->getRootGUIElement()->getElementFromPoint(
2840 core::position2d<s32>(x, y));
2841 if (hovered && isMyChild(hovered) &&
2842 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2843 gui::IGUISkin* skin = Environment->getSkin();
2844 sanity_check(skin != NULL);
2845 gui::IGUIFont *old_font = skin->getFont();
2846 skin->setFont(m_font);
2847 bool retval = hovered->OnEvent(event);
2848 skin->setFont(old_font);
2853 // Fix Esc/Return key being eaten by checkboxen and tables
2854 if(event.EventType==EET_KEY_INPUT_EVENT) {
2855 KeyPress kp(event.KeyInput);
2856 if (kp == EscapeKey || kp == CancelKey
2857 || kp == getKeySetting("keymap_inventory")
2858 || event.KeyInput.Key==KEY_RETURN) {
2859 gui::IGUIElement *focused = Environment->getFocus();
2860 if (focused && isMyChild(focused) &&
2861 (focused->getType() == gui::EGUIET_LIST_BOX ||
2862 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2868 // Mouse wheel events: send to hovered element instead of focused
2869 if(event.EventType==EET_MOUSE_INPUT_EVENT
2870 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2871 s32 x = event.MouseInput.X;
2872 s32 y = event.MouseInput.Y;
2873 gui::IGUIElement *hovered =
2874 Environment->getRootGUIElement()->getElementFromPoint(
2875 core::position2d<s32>(x, y));
2876 if (hovered && isMyChild(hovered)) {
2877 hovered->OnEvent(event);
2882 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2883 s32 x = event.MouseInput.X;
2884 s32 y = event.MouseInput.Y;
2885 gui::IGUIElement *hovered =
2886 Environment->getRootGUIElement()->getElementFromPoint(
2887 core::position2d<s32>(x, y));
2888 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2889 m_old_tooltip_id = -1;
2890 m_old_tooltip = L"";
2892 if (!isChild(hovered,this)) {
2893 if (DoubleClickDetection(event)) {
2900 // display software keyboard when clicking edit boxes
2901 if (event.EventType == EET_MOUSE_INPUT_EVENT
2902 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2903 gui::IGUIElement *hovered =
2904 Environment->getRootGUIElement()->getElementFromPoint(
2905 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2906 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2907 bool retval = hovered->OnEvent(event);
2909 Environment->setFocus(hovered);
2911 m_JavaDialogFieldName = getNameByID(hovered->getID());
2912 std::string message = gettext("Enter ");
2913 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
2917 message += gettext(label) + ":";
2919 /* single line text input */
2922 /* multi line text input */
2923 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2927 /* passwords are always single line */
2928 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2932 porting::showInputDialog(gettext("ok"), "",
2933 wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
2939 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2942 memset(&translated, 0, sizeof(SEvent));
2943 translated.EventType = EET_MOUSE_INPUT_EVENT;
2944 gui::IGUIElement* root = Environment->getRootGUIElement();
2948 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2952 gui::IGUIElement* hovered = root->getElementFromPoint(
2953 core::position2d<s32>(
2955 event.TouchInput.Y));
2957 translated.MouseInput.X = event.TouchInput.X;
2958 translated.MouseInput.Y = event.TouchInput.Y;
2959 translated.MouseInput.Control = false;
2961 bool dont_send_event = false;
2963 if (event.TouchInput.touchedCount == 1) {
2964 switch (event.TouchInput.Event) {
2965 case ETIE_PRESSED_DOWN:
2966 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2967 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2968 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2969 m_down_pos = m_pointer;
2972 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2973 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2974 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2977 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2978 translated.MouseInput.ButtonStates = 0;
2979 hovered = root->getElementFromPoint(m_down_pos);
2980 /* we don't have a valid pointer element use last
2981 * known pointer pos */
2982 translated.MouseInput.X = m_pointer.X;
2983 translated.MouseInput.Y = m_pointer.Y;
2985 /* reset down pos */
2986 m_down_pos = v2s32(0,0);
2989 dont_send_event = true;
2990 //this is not supposed to happen
2992 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2993 << event.TouchInput.Event << std::endl;
2995 } else if ( (event.TouchInput.touchedCount == 2) &&
2996 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2997 hovered = root->getElementFromPoint(m_down_pos);
2999 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
3000 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
3001 translated.MouseInput.X = m_pointer.X;
3002 translated.MouseInput.Y = m_pointer.Y;
3005 hovered->OnEvent(translated);
3008 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
3009 translated.MouseInput.ButtonStates = EMBSM_LEFT;
3013 hovered->OnEvent(translated);
3015 dont_send_event = true;
3017 /* ignore unhandled 2 touch events ... accidental moving for example */
3018 else if (event.TouchInput.touchedCount == 2) {
3019 dont_send_event = true;
3021 else if (event.TouchInput.touchedCount > 2) {
3023 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
3024 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
3027 if (dont_send_event) {
3031 /* check if translated event needs to be preprocessed again */
3032 if (preprocessEvent(translated)) {
3037 bool retval = hovered->OnEvent(translated);
3039 if (event.TouchInput.Event == ETIE_LEFT_UP) {
3041 m_pointer = v2s32(0,0);
3049 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
3050 /* TODO add a check like:
3051 if (event.JoystickEvent != joystick_we_listen_for)
3054 bool handled = m_joystick->handleEvent(event.JoystickEvent);
3056 if (m_joystick->wasKeyDown(KeyType::ESC)) {
3058 } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
3060 acceptInput(quit_mode_accept);
3071 /******************************************************************************/
3072 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
3074 /* The following code is for capturing double-clicks of the mouse button
3075 * and translating the double-click into an EET_KEY_INPUT_EVENT event
3076 * -- which closes the form -- under some circumstances.
3078 * There have been many github issues reporting this as a bug even though it
3079 * was an intended feature. For this reason, remapping the double-click as
3080 * an ESC must be explicitly set when creating this class via the
3081 * /p remap_dbl_click parameter of the constructor.
3084 if (!m_remap_dbl_click)
3087 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
3088 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
3089 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
3091 m_doubleclickdetect[1].pos = m_pointer;
3092 m_doubleclickdetect[1].time = getTimeMs();
3094 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
3095 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
3100 double squaredistance =
3101 m_doubleclickdetect[0].pos
3102 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
3104 if (squaredistance > (30*30)) {
3108 SEvent* translated = new SEvent();
3109 assert(translated != 0);
3110 //translate doubleclick to escape
3111 memset(translated, 0, sizeof(SEvent));
3112 translated->EventType = irr::EET_KEY_INPUT_EVENT;
3113 translated->KeyInput.Key = KEY_ESCAPE;
3114 translated->KeyInput.Control = false;
3115 translated->KeyInput.Shift = false;
3116 translated->KeyInput.PressedDown = true;
3117 translated->KeyInput.Char = 0;
3118 OnEvent(*translated);
3120 // no need to send the key up event as we're already deleted
3121 // and no one else did notice this event
3129 void GUIFormSpecMenu::tryClose()
3133 acceptInput(quit_mode_cancel);
3136 m_text_dst->gotText(L"MenuQuit");
3140 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3142 if (event.EventType==EET_KEY_INPUT_EVENT) {
3143 KeyPress kp(event.KeyInput);
3144 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3145 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3148 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3149 (kp == getKeySetting("keymap_screenshot"))) {
3150 m_client->makeScreenshot(m_device);
3152 if (event.KeyInput.PressedDown &&
3153 (event.KeyInput.Key==KEY_RETURN ||
3154 event.KeyInput.Key==KEY_UP ||
3155 event.KeyInput.Key==KEY_DOWN)
3157 switch (event.KeyInput.Key) {
3159 current_keys_pending.key_enter = true;
3162 current_keys_pending.key_up = true;
3165 current_keys_pending.key_down = true;
3169 //can't happen at all!
3170 FATAL_ERROR("Reached a source line that can't ever been reached");
3173 if (current_keys_pending.key_enter && m_allowclose) {
3174 acceptInput(quit_mode_accept);
3184 /* Mouse event other than movement, or crossing the border of inventory
3185 field while holding right mouse button
3187 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3188 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3189 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3190 event.MouseInput.isRightPressed() &&
3191 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3193 // Get selected item and hovered/clicked item (s)
3195 m_old_tooltip_id = -1;
3196 updateSelectedItem();
3197 ItemSpec s = getItemAtPos(m_pointer);
3199 Inventory *inv_selected = NULL;
3200 Inventory *inv_s = NULL;
3201 InventoryList *list_s = NULL;
3203 if (m_selected_item) {
3204 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3205 sanity_check(inv_selected);
3206 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3213 inv_s = m_invmgr->getInventory(s.inventoryloc);
3216 errorstream << "InventoryMenu: The selected inventory location "
3217 << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
3219 s.i = -1; // make it invalid again
3223 list_s = inv_s->getList(s.listname);
3224 if (list_s == NULL) {
3225 verbosestream << "InventoryMenu: The selected inventory list \""
3226 << s.listname << "\" does not exist" << std::endl;
3227 s.i = -1; // make it invalid again
3231 if ((u32)s.i >= list_s->getSize()) {
3232 infostream << "InventoryMenu: The selected inventory list \""
3233 << s.listname << "\" is too small (i=" << s.i << ", size="
3234 << list_s->getSize() << ")" << std::endl;
3235 s.i = -1; // make it invalid again
3239 s_count = list_s->getItem(s.i).count;
3242 bool identical = (m_selected_item != NULL) && s.isValid() &&
3243 (inv_selected == inv_s) &&
3244 (m_selected_item->listname == s.listname) &&
3245 (m_selected_item->i == s.i);
3247 // buttons: 0 = left, 1 = right, 2 = middle
3248 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3251 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3252 { button = 0; updown = 0; }
3253 else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3254 { button = 1; updown = 0; }
3255 else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3256 { button = 2; updown = 0; }
3257 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3258 { button = 0; updown = 1; }
3259 else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3260 { button = 1; updown = 1; }
3261 else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3262 { button = 2; updown = 1; }
3263 else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
3266 // Set this number to a positive value to generate a move action
3267 // from m_selected_item to s.
3268 u32 move_amount = 0;
3270 // Set this number to a positive value to generate a move action
3271 // from s to the next inventory ring.
3272 u32 shift_move_amount = 0;
3274 // Set this number to a positive value to generate a drop action
3275 // from m_selected_item.
3276 u32 drop_amount = 0;
3278 // Set this number to a positive value to generate a craft action at s.
3279 u32 craft_amount = 0;
3282 // Some mouse button has been pressed
3284 //infostream<<"Mouse button "<<button<<" pressed at p=("
3285 // <<p.X<<","<<p.Y<<")"<<std::endl;
3287 m_selected_dragging = false;
3289 if (s.isValid() && s.listname == "craftpreview") {
3290 // Craft preview has been clicked: craft
3291 craft_amount = (button == 2 ? 10 : 1);
3292 } else if (m_selected_item == NULL) {
3294 // Non-empty stack has been clicked: select or shift-move it
3295 m_selected_item = new ItemSpec(s);
3298 if (button == 1) // right
3299 count = (s_count + 1) / 2;
3300 else if (button == 2) // middle
3301 count = MYMIN(s_count, 10);
3305 if (!event.MouseInput.Shift) {
3306 // no shift: select item
3307 m_selected_amount = count;
3308 m_selected_dragging = true;
3309 m_rmouse_auto_place = false;
3311 // shift pressed: move item
3313 shift_move_amount = count;
3314 else // count of 1 at left click like after drag & drop
3315 shift_move_amount = 1;
3318 } else { // m_selected_item != NULL
3319 assert(m_selected_amount >= 1);
3322 // Clicked a slot: move
3323 if (button == 1) // right
3325 else if (button == 2) // middle
3326 move_amount = MYMIN(m_selected_amount, 10);
3328 move_amount = m_selected_amount;
3331 if (move_amount >= m_selected_amount)
3332 m_selected_amount = 0;
3334 m_selected_amount -= move_amount;
3338 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3339 // Clicked outside of the window: drop
3340 if (button == 1) // right
3342 else if (button == 2) // middle
3343 drop_amount = MYMIN(m_selected_amount, 10);
3345 drop_amount = m_selected_amount;
3349 else if (updown == 1) {
3350 // Some mouse button has been released
3352 //infostream<<"Mouse button "<<button<<" released at p=("
3353 // <<p.X<<","<<p.Y<<")"<<std::endl;
3355 if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3357 // Dragged to different slot: move all selected
3358 move_amount = m_selected_amount;
3360 } else if (m_selected_item != NULL && m_selected_dragging &&
3361 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3362 // Dragged outside of window: drop all selected
3363 drop_amount = m_selected_amount;
3366 m_selected_dragging = false;
3367 // Keep count of how many times right mouse button has been
3368 // clicked. One click is drag without dropping. Click + release
3369 // + click changes to drop one item when moved mode
3370 if (button == 1 && m_selected_item != NULL)
3371 m_rmouse_auto_place = !m_rmouse_auto_place;
3372 } else if (updown == -1) {
3373 // Mouse has been moved and rmb is down and mouse pointer just
3374 // entered a new inventory field (checked in the entry-if, this
3375 // is the only action here that is generated by mouse movement)
3376 if (m_selected_item != NULL && s.isValid()) {
3378 // TODO: middle mouse to move 10 items might be handy
3379 if (m_rmouse_auto_place) {
3380 // Only move an item if the destination slot is empty
3381 // or contains the same item type as what is going to be
3383 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3384 InventoryList *list_to = list_s;
3385 assert(list_from && list_to);
3386 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3387 ItemStack stack_to = list_to->getItem(s.i);
3388 if (stack_to.empty() || stack_to.name == stack_from.name)
3394 // Possibly send inventory action to server
3395 if (move_amount > 0) {
3396 // Send IACTION_MOVE
3398 assert(m_selected_item && m_selected_item->isValid());
3399 assert(s.isValid());
3401 assert(inv_selected && inv_s);
3402 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3403 InventoryList *list_to = list_s;
3404 assert(list_from && list_to);
3405 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3406 ItemStack stack_to = list_to->getItem(s.i);
3408 // Check how many items can be moved
3409 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3410 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3411 // If source stack cannot be added to destination stack at all,
3413 if ((leftover.count == stack_from.count) &&
3414 (leftover.name == stack_from.name)) {
3415 m_selected_amount = stack_to.count;
3416 // In case the server doesn't directly swap them but instead
3417 // moves stack_to somewhere else, set this
3418 m_selected_content_guess = stack_to;
3419 m_selected_content_guess_inventory = s.inventoryloc;
3421 // Source stack goes fully into destination stack
3422 else if (leftover.empty()) {
3423 m_selected_amount -= move_amount;
3424 m_selected_content_guess = ItemStack(); // Clear
3426 // Source stack goes partly into destination stack
3428 move_amount -= leftover.count;
3429 m_selected_amount -= move_amount;
3430 m_selected_content_guess = ItemStack(); // Clear
3433 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3434 IMoveAction *a = new IMoveAction();
3435 a->count = move_amount;
3436 a->from_inv = m_selected_item->inventoryloc;
3437 a->from_list = m_selected_item->listname;
3438 a->from_i = m_selected_item->i;
3439 a->to_inv = s.inventoryloc;
3440 a->to_list = s.listname;
3442 m_invmgr->inventoryAction(a);
3443 } else if (shift_move_amount > 0) {
3444 u32 mis = m_inventory_rings.size();
3446 for (; i < mis; i++) {
3447 const ListRingSpec &sp = m_inventory_rings[i];
3448 if (sp.inventoryloc == s.inventoryloc
3449 && sp.listname == s.listname)
3453 if (i >= mis) // if not found
3455 u32 to_inv_ind = (i + 1) % mis;
3456 const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
3457 InventoryList *list_from = list_s;
3460 Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
3463 InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
3466 ItemStack stack_from = list_from->getItem(s.i);
3467 assert(shift_move_amount <= stack_from.count);
3468 if (m_client->getProtoVersion() >= 25) {
3469 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3470 IMoveAction *a = new IMoveAction();
3471 a->count = shift_move_amount;
3472 a->from_inv = s.inventoryloc;
3473 a->from_list = s.listname;
3475 a->to_inv = to_inv_sp.inventoryloc;
3476 a->to_list = to_inv_sp.listname;
3477 a->move_somewhere = true;
3478 m_invmgr->inventoryAction(a);
3480 // find a place (or more than one) to add the new item
3481 u32 ilt_size = list_to->getSize();
3483 for (u32 slot_to = 0; slot_to < ilt_size
3484 && shift_move_amount > 0; slot_to++) {
3485 list_to->itemFits(slot_to, stack_from, &leftover);
3486 if (leftover.count < stack_from.count) {
3487 infostream << "Handing IACTION_MOVE to manager" << std::endl;
3488 IMoveAction *a = new IMoveAction();
3489 a->count = MYMIN(shift_move_amount,
3490 (u32) (stack_from.count - leftover.count));
3491 shift_move_amount -= a->count;
3492 a->from_inv = s.inventoryloc;
3493 a->from_list = s.listname;
3495 a->to_inv = to_inv_sp.inventoryloc;
3496 a->to_list = to_inv_sp.listname;
3498 m_invmgr->inventoryAction(a);
3499 stack_from = leftover;
3504 } else if (drop_amount > 0) {
3505 m_selected_content_guess = ItemStack(); // Clear
3507 // Send IACTION_DROP
3509 assert(m_selected_item && m_selected_item->isValid());
3510 assert(inv_selected);
3511 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3513 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3515 // Check how many items can be dropped
3516 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3517 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3518 m_selected_amount -= drop_amount;
3520 infostream << "Handing IACTION_DROP to manager" << std::endl;
3521 IDropAction *a = new IDropAction();
3522 a->count = drop_amount;
3523 a->from_inv = m_selected_item->inventoryloc;
3524 a->from_list = m_selected_item->listname;
3525 a->from_i = m_selected_item->i;
3526 m_invmgr->inventoryAction(a);
3527 } else if (craft_amount > 0) {
3528 m_selected_content_guess = ItemStack(); // Clear
3530 // Send IACTION_CRAFT
3532 assert(s.isValid());
3535 infostream << "Handing IACTION_CRAFT to manager" << std::endl;
3536 ICraftAction *a = new ICraftAction();
3537 a->count = craft_amount;
3538 a->craft_inv = s.inventoryloc;
3539 m_invmgr->inventoryAction(a);
3542 // If m_selected_amount has been decreased to zero, deselect
3543 if (m_selected_amount == 0) {
3544 delete m_selected_item;
3545 m_selected_item = NULL;
3546 m_selected_amount = 0;
3547 m_selected_dragging = false;
3548 m_selected_content_guess = ItemStack();
3550 m_old_pointer = m_pointer;
3552 if (event.EventType == EET_GUI_EVENT) {
3554 if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
3556 // find the element that was clicked
3557 for (unsigned int i=0; i<m_fields.size(); i++) {
3558 FieldSpec &s = m_fields[i];
3559 if ((s.ftype == f_TabHeader) &&
3560 (s.fid == event.GUIEvent.Caller->getID())) {
3568 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
3570 if (!canTakeFocus(event.GUIEvent.Element)) {
3571 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3573 // Returning true disables focus change
3577 if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3578 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3579 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3580 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3581 unsigned int btn_id = event.GUIEvent.Caller->getID();
3583 if (btn_id == 257) {
3585 acceptInput(quit_mode_accept);
3589 m_text_dst->gotText(L"ExitButton");
3591 // quitMenu deallocates menu
3595 // find the element that was clicked
3596 for (u32 i = 0; i < m_fields.size(); i++) {
3597 FieldSpec &s = m_fields[i];
3598 // if its a button, set the send field so
3599 // lua knows which button was pressed
3600 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3601 (s.fid == event.GUIEvent.Caller->getID())) {
3605 acceptInput(quit_mode_accept);
3608 m_text_dst->gotText(L"ExitButton");
3612 acceptInput(quit_mode_no);
3616 } else if ((s.ftype == f_DropDown) &&
3617 (s.fid == event.GUIEvent.Caller->getID())) {
3618 // only send the changed dropdown
3619 for (u32 i = 0; i < m_fields.size(); i++) {
3620 FieldSpec &s2 = m_fields[i];
3621 if (s2.ftype == f_DropDown) {
3626 acceptInput(quit_mode_no);
3628 // revert configuration to make sure dropdowns are sent on
3629 // regular button click
3630 for (u32 i = 0; i < m_fields.size(); i++) {
3631 FieldSpec &s2 = m_fields[i];
3632 if (s2.ftype == f_DropDown) {
3637 } else if ((s.ftype == f_ScrollBar) &&
3638 (s.fid == event.GUIEvent.Caller->getID())) {
3639 s.fdefault = L"Changed";
3640 acceptInput(quit_mode_no);
3646 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3647 if (event.GUIEvent.Caller->getID() > 257) {
3648 bool close_on_enter = true;
3649 for (u32 i = 0; i < m_fields.size(); i++) {
3650 FieldSpec &s = m_fields[i];
3651 if (s.ftype == f_Unknown &&
3652 s.fid == event.GUIEvent.Caller->getID()) {
3653 current_field_enter_pending = s.fname;
3654 close_on_enter = s.close_on_enter;
3659 if (m_allowclose && close_on_enter) {
3660 current_keys_pending.key_enter = true;
3661 acceptInput(quit_mode_accept);
3664 current_keys_pending.key_enter = true;
3667 // quitMenu deallocates menu
3672 if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3673 int current_id = event.GUIEvent.Caller->getID();
3674 if (current_id > 257) {
3675 // find the element that was clicked
3676 for (u32 i = 0; i < m_fields.size(); i++) {
3677 FieldSpec &s = m_fields[i];
3678 // if it's a table, set the send field
3679 // so lua knows which table was changed
3680 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3691 return Parent ? Parent->OnEvent(event) : false;
3695 * get name of element by element id
3696 * @param id of element
3697 * @return name string or empty string
3699 std::string GUIFormSpecMenu::getNameByID(s32 id)
3701 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3702 iter != m_fields.end(); ++iter) {
3703 if (iter->fid == id) {
3711 * get label of element by id
3712 * @param id of element
3713 * @return label string or empty string
3715 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3717 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3718 iter != m_fields.end(); ++iter) {
3719 if (iter->fid == id) {
3720 return iter->flabel;