3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guiFormSpecMenu.h"
28 #include "constants.h"
32 #include <IGUICheckBox.h>
33 #include <IGUIEditBox.h>
34 #include <IGUIButton.h>
35 #include <IGUIStaticText.h>
37 #include <IGUITabControl.h>
38 #include <IGUIComboBox.h>
40 #include "client/tile.h" // ITextureSource
41 #include "hud.h" // drawItemStack
45 #include "scripting_game.h"
49 #include "fontengine.h"
51 #include "util/numeric.h"
52 #include "util/string.h" // for parseColorString()
53 #include "guiscalingfilter.h"
55 #define MY_CHECKPOS(a,b) \
56 if (v_pos.size() != 2) { \
57 errorstream<< "Invalid pos for element " << a << "specified: \"" \
58 << parts[b] << "\"" << std::endl; \
62 #define MY_CHECKGEOM(a,b) \
63 if (v_geom.size() != 2) { \
64 errorstream<< "Invalid pos for element " << a << "specified: \"" \
65 << parts[b] << "\"" << std::endl; \
71 static unsigned int font_line_height(gui::IGUIFont *font)
73 return font->getDimension(L"Ay").Height + font->getKerningHeight();
76 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
77 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
78 InventoryManager *invmgr, IGameDef *gamedef,
79 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
80 Client* client, bool remap_dbl_click) :
81 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
87 m_selected_item(NULL),
89 m_selected_dragging(false),
90 m_tooltip_element(NULL),
93 m_rmouse_auto_place(false),
98 m_formspec_version(0),
99 m_focused_element(L""),
101 m_remap_dbl_click(remap_dbl_click)
103 ,m_JavaDialogFieldName(L"")
106 current_keys_pending.key_down = false;
107 current_keys_pending.key_up = false;
108 current_keys_pending.key_enter = false;
109 current_keys_pending.key_escape = false;
111 m_doubleclickdetect[0].time = 0;
112 m_doubleclickdetect[1].time = 0;
114 m_doubleclickdetect[0].pos = v2s32(0, 0);
115 m_doubleclickdetect[1].pos = v2s32(0, 0);
117 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
120 GUIFormSpecMenu::~GUIFormSpecMenu()
124 for (u32 i = 0; i < m_tables.size(); ++i) {
125 GUITable *table = m_tables[i].second;
129 delete m_selected_item;
131 if (m_form_src != NULL) {
134 if (m_text_dst != NULL) {
139 void GUIFormSpecMenu::removeChildren()
141 const core::list<gui::IGUIElement*> &children = getChildren();
143 while(!children.empty()) {
144 (*children.getLast())->remove();
147 if(m_tooltip_element) {
148 m_tooltip_element->remove();
149 m_tooltip_element->drop();
150 m_tooltip_element = NULL;
155 void GUIFormSpecMenu::setInitialFocus()
157 // Set initial focus according to following order of precedence:
158 // 1. first empty editbox
162 // 5. first focusable (not statictext, not tabheader)
163 // 6. first child element
165 core::list<gui::IGUIElement*> children = getChildren();
167 // in case "children" contains any NULL elements, remove them
168 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
169 it != children.end();) {
173 it = children.erase(it);
176 // 1. first empty editbox
177 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
178 it != children.end(); ++it) {
179 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
180 && (*it)->getText()[0] == 0) {
181 Environment->setFocus(*it);
187 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
188 it != children.end(); ++it) {
189 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
190 Environment->setFocus(*it);
196 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
197 it != children.end(); ++it) {
198 if ((*it)->getTypeName() == std::string("GUITable")) {
199 Environment->setFocus(*it);
205 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
206 it != children.end(); --it) {
207 if ((*it)->getType() == gui::EGUIET_BUTTON) {
208 Environment->setFocus(*it);
213 // 5. first focusable (not statictext, not tabheader)
214 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
215 it != children.end(); ++it) {
216 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
217 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
218 Environment->setFocus(*it);
223 // 6. first child element
224 if (children.empty())
225 Environment->setFocus(this);
227 Environment->setFocus(*(children.begin()));
230 GUITable* GUIFormSpecMenu::getTable(std::wstring tablename)
232 for (u32 i = 0; i < m_tables.size(); ++i) {
233 if (tablename == m_tables[i].first.fname)
234 return m_tables[i].second;
239 std::vector<std::string> split(const std::string &s, char delim) {
240 std::vector<std::string> tokens;
242 std::string current = "";
243 bool last_was_escape = false;
244 for(unsigned int i=0; i < s.size(); i++) {
245 if (last_was_escape) {
247 current += s.c_str()[i];
248 last_was_escape = false;
251 if (s.c_str()[i] == delim) {
252 tokens.push_back(current);
254 last_was_escape = false;
256 else if (s.c_str()[i] == '\\'){
257 last_was_escape = true;
260 current += s.c_str()[i];
261 last_was_escape = false;
266 tokens.push_back(current);
271 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
273 std::vector<std::string> parts = split(element,',');
275 if (((parts.size() == 2) || parts.size() == 3) ||
276 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
278 if (parts[1].find(';') != std::string::npos)
279 parts[1] = parts[1].substr(0,parts[1].find(';'));
281 data->invsize.X = MYMAX(0, stof(parts[0]));
282 data->invsize.Y = MYMAX(0, stof(parts[1]));
285 if (parts.size() == 3) {
286 if (parts[2] == "true") {
287 lockSize(true,v2u32(800,600));
291 data->explicit_size = true;
294 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
297 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
299 if (m_gamedef == 0) {
300 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
304 std::vector<std::string> parts = split(element,';');
306 if (((parts.size() == 4) || (parts.size() == 5)) ||
307 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
309 std::string location = parts[0];
310 std::string listname = parts[1];
311 std::vector<std::string> v_pos = split(parts[2],',');
312 std::vector<std::string> v_geom = split(parts[3],',');
313 std::string startindex = "";
314 if (parts.size() == 5)
315 startindex = parts[4];
317 MY_CHECKPOS("list",2);
318 MY_CHECKGEOM("list",3);
320 InventoryLocation loc;
322 if(location == "context" || location == "current_name")
323 loc = m_current_inventory_location;
325 loc.deSerialize(location);
327 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
328 pos.X += stof(v_pos[0]) * (float)spacing.X;
329 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
332 geom.X = stoi(v_geom[0]);
333 geom.Y = stoi(v_geom[1]);
337 start_i = stoi(startindex);
339 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
340 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
344 if(!data->explicit_size)
345 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
346 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
349 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
352 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
354 std::vector<std::string> parts = split(element,';');
356 if (((parts.size() >= 3) && (parts.size() <= 4)) ||
357 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
359 std::vector<std::string> v_pos = split(parts[0],',');
360 std::string name = parts[1];
361 std::string label = parts[2];
362 std::string selected = "";
364 if (parts.size() >= 4)
367 MY_CHECKPOS("checkbox",0);
370 pos.X += stof(v_pos[0]) * (float) spacing.X;
371 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
373 bool fselected = false;
375 if (selected == "true")
378 std::wstring wlabel = narrow_to_wide(label);
380 core::rect<s32> rect = core::rect<s32>(
381 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
382 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
383 pos.Y + ((imgsize.Y/2) + m_btn_height));
386 narrow_to_wide(name),
387 wlabel, //Needed for displaying text on MSVC
392 spec.ftype = f_CheckBox;
394 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
395 spec.fid, spec.flabel.c_str());
397 if (spec.fname == data->focused_fieldname) {
398 Environment->setFocus(e);
401 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
402 m_fields.push_back(spec);
405 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
408 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
410 std::vector<std::string> parts = split(element,';');
412 if (parts.size() >= 5) {
413 std::vector<std::string> v_pos = split(parts[0],',');
414 std::vector<std::string> v_dim = split(parts[1],',');
415 std::string name = parts[2];
416 std::string value = parts[4];
418 MY_CHECKPOS("scrollbar",0);
421 pos.X += stof(v_pos[0]) * (float) spacing.X;
422 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
424 if (v_dim.size() != 2) {
425 errorstream<< "Invalid size for element " << "scrollbar"
426 << "specified: \"" << parts[1] << "\"" << std::endl;
431 dim.X = stof(v_dim[0]) * (float) spacing.X;
432 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
434 core::rect<s32> rect =
435 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
438 narrow_to_wide(name),
444 bool is_horizontal = true;
446 if (parts[2] == "vertical")
447 is_horizontal = false;
449 spec.ftype = f_ScrollBar;
451 gui::IGUIScrollBar* e =
452 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
456 e->setPos(stoi(parts[4]));
458 e->setLargeStep(100);
460 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
461 m_fields.push_back(spec);
464 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
467 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
469 std::vector<std::string> parts = split(element,';');
471 if ((parts.size() == 3) ||
472 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
474 std::vector<std::string> v_pos = split(parts[0],',');
475 std::vector<std::string> v_geom = split(parts[1],',');
476 std::string name = unescape_string(parts[2]);
478 MY_CHECKPOS("image",0);
479 MY_CHECKGEOM("image",1);
481 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
482 pos.X += stof(v_pos[0]) * (float) spacing.X;
483 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
486 geom.X = stof(v_geom[0]) * (float)imgsize.X;
487 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
489 if(!data->explicit_size)
490 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
491 m_images.push_back(ImageDrawSpec(name, pos, geom));
495 if (parts.size() == 2) {
496 std::vector<std::string> v_pos = split(parts[0],',');
497 std::string name = unescape_string(parts[1]);
499 MY_CHECKPOS("image",0);
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;
505 if(!data->explicit_size)
506 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
507 m_images.push_back(ImageDrawSpec(name, pos));
510 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
513 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
515 std::vector<std::string> parts = split(element,';');
517 if ((parts.size() == 3) ||
518 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
520 std::vector<std::string> v_pos = split(parts[0],',');
521 std::vector<std::string> v_geom = split(parts[1],',');
522 std::string name = parts[2];
524 MY_CHECKPOS("itemimage",0);
525 MY_CHECKGEOM("itemimage",1);
527 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
528 pos.X += stof(v_pos[0]) * (float) spacing.X;
529 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
532 geom.X = stof(v_geom[0]) * (float)imgsize.X;
533 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
535 if(!data->explicit_size)
536 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
537 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
540 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
543 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
546 std::vector<std::string> parts = split(element,';');
548 if ((parts.size() == 4) ||
549 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
551 std::vector<std::string> v_pos = split(parts[0],',');
552 std::vector<std::string> v_geom = split(parts[1],',');
553 std::string name = parts[2];
554 std::string label = parts[3];
556 MY_CHECKPOS("button",0);
557 MY_CHECKGEOM("button",1);
560 pos.X += stof(v_pos[0]) * (float)spacing.X;
561 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
564 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
565 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
567 core::rect<s32> rect =
568 core::rect<s32>(pos.X, pos.Y - m_btn_height,
569 pos.X + geom.X, pos.Y + m_btn_height);
571 if(!data->explicit_size)
572 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
574 label = unescape_string(label);
576 std::wstring wlabel = narrow_to_wide(label);
579 narrow_to_wide(name),
584 spec.ftype = f_Button;
585 if(type == "button_exit")
587 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
588 spec.flabel.c_str());
590 if (spec.fname == data->focused_fieldname) {
591 Environment->setFocus(e);
594 m_fields.push_back(spec);
597 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
600 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
602 std::vector<std::string> parts = split(element,';');
604 if (((parts.size() == 3) || (parts.size() == 4)) ||
605 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
607 std::vector<std::string> v_pos = split(parts[0],',');
608 std::vector<std::string> v_geom = split(parts[1],',');
609 std::string name = unescape_string(parts[2]);
611 MY_CHECKPOS("background",0);
612 MY_CHECKGEOM("background",1);
614 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
615 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
616 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
619 geom.X = stof(v_geom[0]) * (float)spacing.X;
620 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
622 if (parts.size() == 4) {
623 m_clipbackground = is_yes(parts[3]);
624 if (m_clipbackground) {
625 pos.X = stoi(v_pos[0]); //acts as offset
626 pos.Y = stoi(v_pos[1]); //acts as offset
630 if(!data->explicit_size)
631 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
632 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
635 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
638 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
640 std::vector<std::string> parts = split(element,';');
642 data->table_options.clear();
643 for (size_t i = 0; i < parts.size(); ++i) {
644 // Parse table option
645 std::string opt = unescape_string(parts[i]);
646 data->table_options.push_back(GUITable::splitOption(opt));
650 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
652 std::vector<std::string> parts = split(element,';');
654 data->table_columns.clear();
655 for (size_t i = 0; i < parts.size(); ++i) {
656 std::vector<std::string> col_parts = split(parts[i],',');
657 GUITable::TableColumn column;
659 if (!col_parts.empty())
660 column.type = col_parts[0];
661 // Parse column options
662 for (size_t j = 1; j < col_parts.size(); ++j) {
663 std::string opt = unescape_string(col_parts[j]);
664 column.options.push_back(GUITable::splitOption(opt));
666 data->table_columns.push_back(column);
670 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
672 std::vector<std::string> parts = split(element,';');
674 if (((parts.size() == 4) || (parts.size() == 5)) ||
675 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
677 std::vector<std::string> v_pos = split(parts[0],',');
678 std::vector<std::string> v_geom = split(parts[1],',');
679 std::string name = parts[2];
680 std::vector<std::string> items = split(parts[3],',');
681 std::string str_initial_selection = "";
682 std::string str_transparent = "false";
684 if (parts.size() >= 5)
685 str_initial_selection = parts[4];
687 MY_CHECKPOS("table",0);
688 MY_CHECKGEOM("table",1);
691 pos.X += stof(v_pos[0]) * (float)spacing.X;
692 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
695 geom.X = stof(v_geom[0]) * (float)spacing.X;
696 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
699 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
701 std::wstring fname_w = narrow_to_wide(name);
710 spec.ftype = f_Table;
712 for (unsigned int i = 0; i < items.size(); ++i) {
713 items[i] = unescape_string(items[i]);
716 //now really show table
717 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
720 if (spec.fname == data->focused_fieldname) {
721 Environment->setFocus(e);
724 e->setTable(data->table_options, data->table_columns, items);
726 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
727 e->setDynamicData(data->table_dyndata[fname_w]);
730 if ((str_initial_selection != "") &&
731 (str_initial_selection != "0"))
732 e->setSelected(stoi(str_initial_selection.c_str()));
734 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
735 m_fields.push_back(spec);
738 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
741 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
743 std::vector<std::string> parts = split(element,';');
745 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
746 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
748 std::vector<std::string> v_pos = split(parts[0],',');
749 std::vector<std::string> v_geom = split(parts[1],',');
750 std::string name = parts[2];
751 std::vector<std::string> items = split(parts[3],',');
752 std::string str_initial_selection = "";
753 std::string str_transparent = "false";
755 if (parts.size() >= 5)
756 str_initial_selection = parts[4];
758 if (parts.size() >= 6)
759 str_transparent = parts[5];
761 MY_CHECKPOS("textlist",0);
762 MY_CHECKGEOM("textlist",1);
765 pos.X += stof(v_pos[0]) * (float)spacing.X;
766 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
769 geom.X = stof(v_geom[0]) * (float)spacing.X;
770 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
773 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
775 std::wstring fname_w = narrow_to_wide(name);
784 spec.ftype = f_Table;
786 for (unsigned int i = 0; i < items.size(); ++i) {
787 items[i] = unescape_string(items[i]);
790 //now really show list
791 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
794 if (spec.fname == data->focused_fieldname) {
795 Environment->setFocus(e);
798 e->setTextList(items, is_yes(str_transparent));
800 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
801 e->setDynamicData(data->table_dyndata[fname_w]);
804 if ((str_initial_selection != "") &&
805 (str_initial_selection != "0"))
806 e->setSelected(stoi(str_initial_selection.c_str()));
808 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
809 m_fields.push_back(spec);
812 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
816 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
818 std::vector<std::string> parts = split(element,';');
820 if ((parts.size() == 5) ||
821 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
823 std::vector<std::string> v_pos = split(parts[0],',');
824 std::string name = parts[2];
825 std::vector<std::string> items = split(parts[3],',');
826 std::string str_initial_selection = "";
827 str_initial_selection = parts[4];
829 MY_CHECKPOS("dropdown",0);
832 pos.X += stof(v_pos[0]) * (float)spacing.X;
833 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
835 s32 width = stof(parts[1]) * (float)spacing.Y;
837 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
838 pos.X + width, pos.Y + (m_btn_height * 2));
840 std::wstring fname_w = narrow_to_wide(name);
849 spec.ftype = f_DropDown;
852 //now really show list
853 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
855 if (spec.fname == data->focused_fieldname) {
856 Environment->setFocus(e);
859 for (unsigned int i=0; i < items.size(); i++) {
860 e->addItem(narrow_to_wide(items[i]).c_str());
863 if (str_initial_selection != "")
864 e->setSelected(stoi(str_initial_selection.c_str())-1);
866 m_fields.push_back(spec);
869 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
870 << element << "'" << std::endl;
873 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
875 std::vector<std::string> parts = split(element,';');
877 if ((parts.size() == 4) ||
878 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
880 std::vector<std::string> v_pos = split(parts[0],',');
881 std::vector<std::string> v_geom = split(parts[1],',');
882 std::string name = parts[2];
883 std::string label = parts[3];
885 MY_CHECKPOS("pwdfield",0);
886 MY_CHECKGEOM("pwdfield",1);
889 pos.X += stof(v_pos[0]) * (float)spacing.X;
890 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
893 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
895 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
896 pos.Y -= m_btn_height;
897 geom.Y = m_btn_height*2;
899 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
901 label = unescape_string(label);
903 std::wstring wlabel = narrow_to_wide(label);
906 narrow_to_wide(name),
913 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
915 if (spec.fname == data->focused_fieldname) {
916 Environment->setFocus(e);
919 if (label.length() >= 1)
921 int font_height = g_fontengine->getTextHeight();
922 rect.UpperLeftCorner.Y -= font_height;
923 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
924 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
927 e->setPasswordBox(true,L'*');
930 evt.EventType = EET_KEY_INPUT_EVENT;
931 evt.KeyInput.Key = KEY_END;
932 evt.KeyInput.Char = 0;
933 evt.KeyInput.Control = 0;
934 evt.KeyInput.Shift = 0;
935 evt.KeyInput.PressedDown = true;
937 m_fields.push_back(spec);
940 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
943 void GUIFormSpecMenu::parseSimpleField(parserData* data,
944 std::vector<std::string> &parts)
946 std::string name = parts[0];
947 std::string label = parts[1];
948 std::string default_val = parts[2];
950 core::rect<s32> rect;
952 if(data->explicit_size)
953 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
955 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
956 pos.Y = ((m_fields.size()+2)*60);
957 v2s32 size = DesiredRect.getSize();
959 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
960 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
964 default_val = m_form_src->resolveText(default_val);
966 default_val = unescape_string(default_val);
967 label = unescape_string(label);
969 std::wstring wlabel = narrow_to_wide(label);
972 narrow_to_wide(name),
974 narrow_to_wide(default_val),
980 // spec field id to 0, this stops submit searching for a value that isn't there
981 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
986 gui::IGUIEditBox *e =
987 Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
989 if (spec.fname == data->focused_fieldname) {
990 Environment->setFocus(e);
994 evt.EventType = EET_KEY_INPUT_EVENT;
995 evt.KeyInput.Key = KEY_END;
996 evt.KeyInput.Char = 0;
997 evt.KeyInput.Control = 0;
998 evt.KeyInput.Shift = 0;
999 evt.KeyInput.PressedDown = true;
1002 if (label.length() >= 1)
1004 int font_height = g_fontengine->getTextHeight();
1005 rect.UpperLeftCorner.Y -= font_height;
1006 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1007 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1011 m_fields.push_back(spec);
1014 void GUIFormSpecMenu::parseTextArea(parserData* data,
1015 std::vector<std::string>& parts,std::string type)
1018 std::vector<std::string> v_pos = split(parts[0],',');
1019 std::vector<std::string> v_geom = split(parts[1],',');
1020 std::string name = parts[2];
1021 std::string label = parts[3];
1022 std::string default_val = parts[4];
1024 MY_CHECKPOS(type,0);
1025 MY_CHECKGEOM(type,1);
1028 pos.X = stof(v_pos[0]) * (float) spacing.X;
1029 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1033 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1035 if (type == "textarea")
1037 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1038 pos.Y += m_btn_height;
1042 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1043 pos.Y -= m_btn_height;
1044 geom.Y = m_btn_height*2;
1047 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1049 if(!data->explicit_size)
1050 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1053 default_val = m_form_src->resolveText(default_val);
1056 default_val = unescape_string(default_val);
1057 label = unescape_string(label);
1059 std::wstring wlabel = narrow_to_wide(label);
1062 narrow_to_wide(name),
1064 narrow_to_wide(default_val),
1070 // spec field id to 0, this stops submit searching for a value that isn't there
1071 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1076 gui::IGUIEditBox *e =
1077 Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1079 if (spec.fname == data->focused_fieldname) {
1080 Environment->setFocus(e);
1083 if (type == "textarea")
1085 e->setMultiLine(true);
1086 e->setWordWrap(true);
1087 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1090 evt.EventType = EET_KEY_INPUT_EVENT;
1091 evt.KeyInput.Key = KEY_END;
1092 evt.KeyInput.Char = 0;
1093 evt.KeyInput.Control = 0;
1094 evt.KeyInput.Shift = 0;
1095 evt.KeyInput.PressedDown = true;
1099 if (label.length() >= 1)
1101 int font_height = g_fontengine->getTextHeight();
1102 rect.UpperLeftCorner.Y -= font_height;
1103 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1104 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1107 m_fields.push_back(spec);
1110 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1113 std::vector<std::string> parts = split(element,';');
1115 if (parts.size() == 3 || parts.size() == 4) {
1116 parseSimpleField(data,parts);
1120 if ((parts.size() == 5) ||
1121 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1123 parseTextArea(data,parts,type);
1126 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1129 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1131 std::vector<std::string> parts = split(element,';');
1133 if ((parts.size() == 2) ||
1134 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1136 std::vector<std::string> v_pos = split(parts[0],',');
1137 std::string text = parts[1];
1139 MY_CHECKPOS("label",0);
1141 v2s32 pos = padding;
1142 pos.X += stof(v_pos[0]) * (float)spacing.X;
1143 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1145 if(!data->explicit_size)
1146 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1148 text = unescape_string(text);
1149 std::vector<std::string> lines = split(text, '\n');
1151 for (unsigned int i = 0; i != lines.size(); i++) {
1152 // Lines are spaced at the nominal distance of
1153 // 2/5 inventory slot, even if the font doesn't
1154 // quite match that. This provides consistent
1155 // form layout, at the expense of sometimes
1156 // having sub-optimal spacing for the font.
1157 // We multiply by 2 and then divide by 5, rather
1158 // than multiply by 0.4, to get exact results
1159 // in the integer cases: 0.4 is not exactly
1160 // representable in binary floating point.
1161 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1162 std::wstring wlabel = narrow_to_wide(lines[i]);
1163 core::rect<s32> rect = core::rect<s32>(
1164 pos.X, posy - m_btn_height,
1165 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1166 posy + m_btn_height);
1173 gui::IGUIStaticText *e =
1174 Environment->addStaticText(spec.flabel.c_str(),
1175 rect, false, false, this, spec.fid);
1176 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1178 m_fields.push_back(spec);
1183 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1186 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1188 std::vector<std::string> parts = split(element,';');
1190 if ((parts.size() == 2) ||
1191 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1193 std::vector<std::string> v_pos = split(parts[0],',');
1194 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1196 MY_CHECKPOS("vertlabel",1);
1198 v2s32 pos = padding;
1199 pos.X += stof(v_pos[0]) * (float)spacing.X;
1200 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1202 core::rect<s32> rect = core::rect<s32>(
1203 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1205 font_line_height(m_font)
1207 +((imgsize.Y/2)- m_btn_height));
1208 //actually text.length() would be correct but adding +1 avoids to break all mods
1210 if(!data->explicit_size)
1211 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1213 std::wstring label = L"";
1215 for (unsigned int i=0; i < text.length(); i++) {
1226 gui::IGUIStaticText *t =
1227 Environment->addStaticText(spec.flabel.c_str(), rect, false, false, this, spec.fid);
1228 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1229 m_fields.push_back(spec);
1232 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1235 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1238 std::vector<std::string> parts = split(element,';');
1240 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1241 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1243 std::vector<std::string> v_pos = split(parts[0],',');
1244 std::vector<std::string> v_geom = split(parts[1],',');
1245 std::string image_name = parts[2];
1246 std::string name = parts[3];
1247 std::string label = parts[4];
1249 MY_CHECKPOS("imagebutton",0);
1250 MY_CHECKGEOM("imagebutton",1);
1252 v2s32 pos = padding;
1253 pos.X += stof(v_pos[0]) * (float)spacing.X;
1254 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1256 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1257 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1259 bool noclip = false;
1260 bool drawborder = true;
1261 std::string pressed_image_name = "";
1263 if (parts.size() >= 7) {
1264 if (parts[5] == "true")
1266 if (parts[6] == "false")
1270 if (parts.size() >= 8) {
1271 pressed_image_name = parts[7];
1274 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1276 if(!data->explicit_size)
1277 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1279 image_name = unescape_string(image_name);
1280 pressed_image_name = unescape_string(pressed_image_name);
1281 label = unescape_string(label);
1283 std::wstring wlabel = narrow_to_wide(label);
1286 narrow_to_wide(name),
1288 narrow_to_wide(image_name),
1291 spec.ftype = f_Button;
1292 if(type == "image_button_exit")
1293 spec.is_exit = true;
1295 video::ITexture *texture = 0;
1296 video::ITexture *pressed_texture = 0;
1297 texture = m_tsrc->getTexture(image_name);
1298 if (pressed_image_name != "")
1299 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1301 pressed_texture = texture;
1303 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1305 if (spec.fname == data->focused_fieldname) {
1306 Environment->setFocus(e);
1309 e->setUseAlphaChannel(true);
1310 e->setImage(guiScalingImageButton(
1311 Environment->getVideoDriver(), texture, geom.X, geom.Y));
1312 e->setPressedImage(guiScalingImageButton(
1313 Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
1314 e->setScaleImage(true);
1315 e->setNotClipped(noclip);
1316 e->setDrawBorder(drawborder);
1318 m_fields.push_back(spec);
1322 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1325 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1327 std::vector<std::string> parts = split(element,';');
1329 if (((parts.size() == 4) || (parts.size() == 6)) ||
1330 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1332 std::vector<std::string> v_pos = split(parts[0],',');
1333 std::string name = parts[1];
1334 std::vector<std::string> buttons = split(parts[2],',');
1335 std::string str_index = parts[3];
1336 bool show_background = true;
1337 bool show_border = true;
1338 int tab_index = stoi(str_index) -1;
1340 MY_CHECKPOS("tabheader",0);
1342 if (parts.size() == 6) {
1343 if (parts[4] == "true")
1344 show_background = false;
1345 if (parts[5] == "false")
1346 show_border = false;
1350 narrow_to_wide(name),
1356 spec.ftype = f_TabHeader;
1359 pos.X += stof(v_pos[0]) * (float)spacing.X;
1360 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1362 geom.X = DesiredRect.getWidth();
1363 geom.Y = m_btn_height*2;
1365 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1368 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1369 show_background, show_border, spec.fid);
1370 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1371 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1372 e->setTabHeight(m_btn_height*2);
1374 if (spec.fname == data->focused_fieldname) {
1375 Environment->setFocus(e);
1378 e->setNotClipped(true);
1380 for (unsigned int i=0; i< buttons.size(); i++) {
1381 e->addTab(narrow_to_wide(buttons[i]).c_str(), -1);
1384 if ((tab_index >= 0) &&
1385 (buttons.size() < INT_MAX) &&
1386 (tab_index < (int) buttons.size()))
1387 e->setActiveTab(tab_index);
1389 m_fields.push_back(spec);
1392 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1393 << element << "'" << std::endl;
1396 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1399 if (m_gamedef == 0) {
1401 "WARNING: invalid use of item_image_button with m_gamedef==0"
1406 std::vector<std::string> parts = split(element,';');
1408 if ((parts.size() == 5) ||
1409 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1411 std::vector<std::string> v_pos = split(parts[0],',');
1412 std::vector<std::string> v_geom = split(parts[1],',');
1413 std::string item_name = parts[2];
1414 std::string name = parts[3];
1415 std::string label = parts[4];
1417 MY_CHECKPOS("itemimagebutton",0);
1418 MY_CHECKGEOM("itemimagebutton",1);
1420 v2s32 pos = padding;
1421 pos.X += stof(v_pos[0]) * (float)spacing.X;
1422 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1424 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1425 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1427 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1429 if(!data->explicit_size)
1430 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1432 IItemDefManager *idef = m_gamedef->idef();
1434 item.deSerialize(item_name, idef);
1435 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1437 m_tooltips[narrow_to_wide(name)] =
1438 TooltipSpec(item.getDefinition(idef).description,
1439 m_default_tooltip_bgcolor,
1440 m_default_tooltip_color);
1442 label = unescape_string(label);
1444 narrow_to_wide(name),
1445 narrow_to_wide(label),
1446 narrow_to_wide(item_name),
1450 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1452 if (spec.fname == data->focused_fieldname) {
1453 Environment->setFocus(e);
1456 e->setUseAlphaChannel(true);
1457 e->setImage(guiScalingImageButton(Environment->getVideoDriver(), texture, geom.X, geom.Y));
1458 e->setPressedImage(guiScalingImageButton(Environment->getVideoDriver(), texture, geom.X, geom.Y));
1459 e->setScaleImage(true);
1460 spec.ftype = f_Button;
1461 rect+=data->basepos-padding;
1463 m_fields.push_back(spec);
1466 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1469 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1471 std::vector<std::string> parts = split(element,';');
1473 if ((parts.size() == 3) ||
1474 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1476 std::vector<std::string> v_pos = split(parts[0],',');
1477 std::vector<std::string> v_geom = split(parts[1],',');
1479 MY_CHECKPOS("box",0);
1480 MY_CHECKGEOM("box",1);
1482 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1483 pos.X += stof(v_pos[0]) * (float) spacing.X;
1484 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1487 geom.X = stof(v_geom[0]) * (float)spacing.X;
1488 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1490 video::SColor tmp_color;
1492 if (parseColorString(parts[2], tmp_color, false)) {
1493 BoxDrawSpec spec(pos, geom, tmp_color);
1495 m_boxes.push_back(spec);
1498 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1502 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1505 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1507 std::vector<std::string> parts = split(element,';');
1509 if (((parts.size() == 1) || (parts.size() == 2)) ||
1510 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1512 parseColorString(parts[0],m_bgcolor,false);
1514 if (parts.size() == 2) {
1515 std::string fullscreen = parts[1];
1516 m_bgfullscreen = is_yes(fullscreen);
1520 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1523 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1525 std::vector<std::string> parts = split(element,';');
1527 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1528 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1530 parseColorString(parts[0], m_slotbg_n, false);
1531 parseColorString(parts[1], m_slotbg_h, false);
1533 if (parts.size() >= 3) {
1534 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1535 m_slotborder = true;
1538 if (parts.size() == 5) {
1539 video::SColor tmp_color;
1541 if (parseColorString(parts[3], tmp_color, false))
1542 m_default_tooltip_bgcolor = tmp_color;
1543 if (parseColorString(parts[4], tmp_color, false))
1544 m_default_tooltip_color = tmp_color;
1548 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1551 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1553 std::vector<std::string> parts = split(element,';');
1554 if (parts.size() == 2) {
1555 std::string name = parts[0];
1556 m_tooltips[narrow_to_wide(name)] = TooltipSpec(unescape_string(parts[1]),
1557 m_default_tooltip_bgcolor, m_default_tooltip_color);
1559 } else if (parts.size() == 4) {
1560 std::string name = parts[0];
1561 video::SColor tmp_color1, tmp_color2;
1562 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1563 m_tooltips[narrow_to_wide(name)] = TooltipSpec(unescape_string(parts[1]),
1564 tmp_color1, tmp_color2);
1568 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1571 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1577 std::vector<std::string> parts = split(data,'[');
1579 if (parts.size() < 2) {
1583 if (parts[0] != "formspec_version") {
1587 if (is_number(parts[1])) {
1588 m_formspec_version = mystoi(parts[1]);
1595 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1600 std::vector<std::string> parts = split(element,'[');
1602 if (parts.size() < 2)
1605 std::string type = trim(parts[0]);
1606 std::string description = trim(parts[1]);
1608 if (type != "size" && type != "invsize")
1611 if (type == "invsize")
1612 log_deprecated("Deprecated formspec element \"invsize\" is used");
1614 parseSize(data, description);
1619 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1625 std::vector<std::string> parts = split(element,'[');
1627 // ugly workaround to keep compatibility
1628 if (parts.size() > 2) {
1629 if (trim(parts[0]) == "image") {
1630 for (unsigned int i=2;i< parts.size(); i++) {
1631 parts[1] += "[" + parts[i];
1637 if (parts.size() < 2) {
1641 std::string type = trim(parts[0]);
1642 std::string description = trim(parts[1]);
1644 if (type == "list") {
1645 parseList(data,description);
1649 if (type == "checkbox") {
1650 parseCheckbox(data,description);
1654 if (type == "image") {
1655 parseImage(data,description);
1659 if (type == "item_image") {
1660 parseItemImage(data,description);
1664 if ((type == "button") || (type == "button_exit")) {
1665 parseButton(data,description,type);
1669 if (type == "background") {
1670 parseBackground(data,description);
1674 if (type == "tableoptions"){
1675 parseTableOptions(data,description);
1679 if (type == "tablecolumns"){
1680 parseTableColumns(data,description);
1684 if (type == "table"){
1685 parseTable(data,description);
1689 if (type == "textlist"){
1690 parseTextList(data,description);
1694 if (type == "dropdown"){
1695 parseDropDown(data,description);
1699 if (type == "pwdfield") {
1700 parsePwdField(data,description);
1704 if ((type == "field") || (type == "textarea")){
1705 parseField(data,description,type);
1709 if (type == "label") {
1710 parseLabel(data,description);
1714 if (type == "vertlabel") {
1715 parseVertLabel(data,description);
1719 if (type == "item_image_button") {
1720 parseItemImageButton(data,description);
1724 if ((type == "image_button") || (type == "image_button_exit")) {
1725 parseImageButton(data,description,type);
1729 if (type == "tabheader") {
1730 parseTabHeader(data,description);
1734 if (type == "box") {
1735 parseBox(data,description);
1739 if (type == "bgcolor") {
1740 parseBackgroundColor(data,description);
1744 if (type == "listcolors") {
1745 parseListColors(data,description);
1749 if (type == "tooltip") {
1750 parseTooltip(data,description);
1754 if (type == "scrollbar") {
1755 parseScrollBar(data, description);
1761 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1765 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1767 /* useless to regenerate without a screensize */
1768 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1775 for (u32 i = 0; i < m_tables.size(); ++i) {
1776 std::wstring tablename = m_tables[i].first.fname;
1777 GUITable *table = m_tables[i].second;
1778 mydata.table_dyndata[tablename] = table->getDynamicData();
1782 if (!m_focused_element.empty())
1783 mydata.focused_fieldname = m_focused_element;
1786 gui::IGUIElement *focused_element = Environment->getFocus();
1787 if (focused_element && focused_element->getParent() == this) {
1788 s32 focused_id = focused_element->getID();
1789 if (focused_id > 257) {
1790 for (u32 i=0; i<m_fields.size(); i++) {
1791 if (m_fields[i].fid == focused_id) {
1792 mydata.focused_fieldname =
1803 for (u32 i = 0; i < m_tables.size(); ++i) {
1804 GUITable *table = m_tables[i].second;
1808 mydata.size= v2s32(100,100);
1809 mydata.screensize = screensize;
1811 // Base position of contents of form
1812 mydata.basepos = getBasePos();
1814 /* Convert m_init_draw_spec to m_inventorylists */
1816 m_inventorylists.clear();
1818 m_backgrounds.clear();
1819 m_itemimages.clear();
1821 m_checkboxes.clear();
1822 m_scrollbars.clear();
1827 // Set default values (fits old formspec values)
1828 m_bgcolor = video::SColor(140,0,0,0);
1829 m_bgfullscreen = false;
1831 m_slotbg_n = video::SColor(255,128,128,128);
1832 m_slotbg_h = video::SColor(255,192,192,192);
1834 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1835 m_default_tooltip_color = video::SColor(255,255,255,255);
1837 m_slotbordercolor = video::SColor(200,0,0,0);
1838 m_slotborder = false;
1840 m_clipbackground = false;
1843 assert(m_tooltip_element == NULL);
1844 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1845 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1846 m_tooltip_element->enableOverrideColor(true);
1847 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1848 m_tooltip_element->setDrawBackground(true);
1849 m_tooltip_element->setDrawBorder(true);
1850 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1851 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1852 m_tooltip_element->setWordWrap(false);
1853 //we're not parent so no autograb for this one!
1854 m_tooltip_element->grab();
1857 std::vector<std::string> elements = split(m_formspec_string,']');
1860 /* try to read version from first element only */
1861 if (elements.size() >= 1) {
1862 if ( parseVersionDirect(elements[0]) ) {
1867 /* we need size first in order to calculate image scale */
1868 mydata.explicit_size = false;
1869 for (; i< elements.size(); i++) {
1870 if (!parseSizeDirect(&mydata, elements[i])) {
1875 if (mydata.explicit_size) {
1876 // compute scaling for specified form size
1878 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1879 v2u32 delta = current_screensize - m_lockscreensize;
1881 if (current_screensize.Y > m_lockscreensize.Y)
1886 if (current_screensize.X > m_lockscreensize.X)
1891 offset = v2s32(delta.X,delta.Y);
1893 mydata.screensize = m_lockscreensize;
1895 offset = v2s32(0,0);
1898 double gui_scaling = g_settings->getFloat("gui_scaling");
1899 double screen_dpi = porting::getDisplayDensity() * 96;
1903 // In fixed-size mode, inventory image size
1904 // is 0.53 inch multiplied by the gui_scaling
1905 // config parameter. This magic size is chosen
1906 // to make the main menu (15.5 inventory images
1907 // wide, including border) just fit into the
1908 // default window (800 pixels wide) at 96 DPI
1909 // and default scaling (1.00).
1910 use_imgsize = 0.5555 * screen_dpi * gui_scaling;
1912 // In variable-size mode, we prefer to make the
1913 // inventory image size 1/15 of screen height,
1914 // multiplied by the gui_scaling config parameter.
1915 // If the preferred size won't fit the whole
1916 // form on the screen, either horizontally or
1917 // vertically, then we scale it down to fit.
1918 // (The magic numbers in the computation of what
1919 // fits arise from the scaling factors in the
1920 // following stanza, including the form border,
1921 // help text space, and 0.1 inventory slot spare.)
1922 // However, a minimum size is also set, that
1923 // the image size can't be less than 0.3 inch
1924 // multiplied by gui_scaling, even if this means
1925 // the form doesn't fit the screen.
1926 double prefer_imgsize = mydata.screensize.Y / 15 *
1928 double fitx_imgsize = mydata.screensize.X /
1929 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1930 double fity_imgsize = mydata.screensize.Y /
1931 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1932 double screen_dpi = porting::getDisplayDensity() * 96;
1933 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1934 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1935 MYMIN(fitx_imgsize, fity_imgsize)));
1938 // Everything else is scaled in proportion to the
1939 // inventory image size. The inventory slot spacing
1940 // is 5/4 image size horizontally and 15/13 image size
1941 // vertically. The padding around the form (incorporating
1942 // the border of the outer inventory slots) is 3/8
1943 // image size. Font height (baseline to baseline)
1944 // is 2/5 vertical inventory slot spacing, and button
1945 // half-height is 7/8 of font height.
1946 imgsize = v2s32(use_imgsize, use_imgsize);
1947 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
1948 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
1949 m_btn_height = use_imgsize*15.0/13 * 0.35;
1951 m_font = g_fontengine->getFont();
1953 mydata.size = v2s32(
1954 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
1955 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
1957 DesiredRect = mydata.rect = core::rect<s32>(
1958 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
1959 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
1960 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
1961 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
1964 // Non-size[] form must consist only of text fields and
1965 // implicit "Proceed" button. Use default font, and
1966 // temporary form size which will be recalculated below.
1967 m_font = g_fontengine->getFont();
1968 m_btn_height = font_line_height(m_font) * 0.875;
1969 DesiredRect = core::rect<s32>(
1970 mydata.screensize.X/2 - 580/2,
1971 mydata.screensize.Y/2 - 300/2,
1972 mydata.screensize.X/2 + 580/2,
1973 mydata.screensize.Y/2 + 300/2
1976 recalculateAbsolutePosition(false);
1977 mydata.basepos = getBasePos();
1978 m_tooltip_element->setOverrideFont(m_font);
1980 gui::IGUISkin* skin = Environment->getSkin();
1981 sanity_check(skin != NULL);
1982 gui::IGUIFont *old_font = skin->getFont();
1983 skin->setFont(m_font);
1985 for (; i< elements.size(); i++) {
1986 parseElement(&mydata, elements[i]);
1989 // If there are fields without explicit size[], add a "Proceed"
1990 // button and adjust size to fit all the fields.
1991 if (m_fields.size() && !mydata.explicit_size) {
1992 mydata.rect = core::rect<s32>(
1993 mydata.screensize.X/2 - 580/2,
1994 mydata.screensize.Y/2 - 300/2,
1995 mydata.screensize.X/2 + 580/2,
1996 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1998 DesiredRect = mydata.rect;
1999 recalculateAbsolutePosition(false);
2000 mydata.basepos = getBasePos();
2003 v2s32 pos = mydata.basepos;
2004 pos.Y = ((m_fields.size()+2)*60);
2006 v2s32 size = DesiredRect.getSize();
2008 core::rect<s32>(size.X/2-70, pos.Y,
2009 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2010 const wchar_t *text = wgettext("Proceed");
2011 Environment->addButton(mydata.rect, this, 257, text);
2017 //set initial focus if parser didn't set it
2018 focused_element = Environment->getFocus();
2019 if (!focused_element
2020 || !isMyChild(focused_element)
2021 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2024 skin->setFont(old_font);
2028 bool GUIFormSpecMenu::getAndroidUIInput()
2030 /* no dialog shown */
2031 if (m_JavaDialogFieldName == L"") {
2036 if (porting::getInputDialogState() == -1) {
2040 std::wstring fieldname = m_JavaDialogFieldName;
2041 m_JavaDialogFieldName = L"";
2043 /* no value abort dialog processing */
2044 if (porting::getInputDialogState() != 0) {
2048 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2049 iter != m_fields.end(); iter++) {
2051 if (iter->fname != fieldname) {
2054 IGUIElement* tochange = getElementFromId(iter->fid);
2056 if (tochange == 0) {
2060 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2064 std::string text = porting::getInputDialogValue();
2066 ((gui::IGUIEditBox*) tochange)->
2067 setText(narrow_to_wide(text).c_str());
2073 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2075 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2077 for(u32 i=0; i<m_inventorylists.size(); i++)
2079 const ListDrawSpec &s = m_inventorylists[i];
2081 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2082 s32 item_i = i + s.start_item_i;
2083 s32 x = (i%s.geom.X) * spacing.X;
2084 s32 y = (i/s.geom.X) * spacing.Y;
2086 core::rect<s32> rect = imgrect + s.pos + p0;
2087 if(rect.isPointInside(p))
2089 return ItemSpec(s.inventoryloc, s.listname, item_i);
2094 return ItemSpec(InventoryLocation(), "", -1);
2097 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
2099 video::IVideoDriver* driver = Environment->getVideoDriver();
2101 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2103 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2104 <<"The inventory location "
2105 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2109 InventoryList *ilist = inv->getList(s.listname);
2111 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2112 <<"The inventory list \""<<s.listname<<"\" @ \""
2113 <<s.inventoryloc.dump()<<"\" doesn't exist"
2118 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2120 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2122 s32 item_i = i + s.start_item_i;
2123 if(item_i >= (s32) ilist->getSize())
2125 s32 x = (i%s.geom.X) * spacing.X;
2126 s32 y = (i/s.geom.X) * spacing.Y;
2128 core::rect<s32> rect = imgrect + s.pos + p;
2131 item = ilist->getItem(item_i);
2133 bool selected = m_selected_item
2134 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2135 && m_selected_item->listname == s.listname
2136 && m_selected_item->i == item_i;
2137 bool hovering = rect.isPointInside(m_pointer);
2142 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2144 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2147 //Draw inv slot borders
2149 s32 x1 = rect.UpperLeftCorner.X;
2150 s32 y1 = rect.UpperLeftCorner.Y;
2151 s32 x2 = rect.LowerRightCorner.X;
2152 s32 y2 = rect.LowerRightCorner.Y;
2154 driver->draw2DRectangle(m_slotbordercolor,
2155 core::rect<s32>(v2s32(x1 - border, y1 - border),
2156 v2s32(x2 + border, y1)), NULL);
2157 driver->draw2DRectangle(m_slotbordercolor,
2158 core::rect<s32>(v2s32(x1 - border, y2),
2159 v2s32(x2 + border, y2 + border)), NULL);
2160 driver->draw2DRectangle(m_slotbordercolor,
2161 core::rect<s32>(v2s32(x1 - border, y1),
2162 v2s32(x1, y2)), NULL);
2163 driver->draw2DRectangle(m_slotbordercolor,
2164 core::rect<s32>(v2s32(x2, y1),
2165 v2s32(x2 + border, y2)), NULL);
2173 item.takeItem(m_selected_amount);
2177 drawItemStack(driver, m_font, item,
2178 rect, &AbsoluteClippingRect, m_gamedef);
2182 std::string tooltip_text = "";
2183 if (hovering && !m_selected_item)
2184 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
2185 if (tooltip_text != "") {
2186 std::vector<std::string> tt_rows = str_split(tooltip_text, '\n');
2187 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2188 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2189 m_tooltip_element->setVisible(true);
2190 this->bringToFront(m_tooltip_element);
2191 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
2192 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2193 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2194 v2u32 screenSize = driver->getScreenSize();
2195 int tooltip_offset_x = m_btn_height;
2196 int tooltip_offset_y = m_btn_height;
2198 tooltip_offset_x *= 3;
2199 tooltip_offset_y = 0;
2200 if (m_pointer.X > (s32)screenSize.X / 2)
2201 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2203 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2204 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2205 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2206 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2207 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2208 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2209 m_tooltip_element->setRelativePosition(core::rect<s32>(
2210 core::position2d<s32>(tooltip_x, tooltip_y),
2211 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2217 void GUIFormSpecMenu::drawSelectedItem()
2219 if(!m_selected_item)
2222 video::IVideoDriver* driver = Environment->getVideoDriver();
2224 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2226 InventoryList *list = inv->getList(m_selected_item->listname);
2228 ItemStack stack = list->getItem(m_selected_item->i);
2229 stack.count = m_selected_amount;
2231 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2232 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2233 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef);
2236 void GUIFormSpecMenu::drawMenu()
2239 std::string newform = m_form_src->getForm();
2240 if(newform != m_formspec_string){
2241 m_formspec_string = newform;
2242 regenerateGui(m_screensize_old);
2246 gui::IGUISkin* skin = Environment->getSkin();
2247 sanity_check(skin != NULL);
2248 gui::IGUIFont *old_font = skin->getFont();
2249 skin->setFont(m_font);
2251 updateSelectedItem();
2253 video::IVideoDriver* driver = Environment->getVideoDriver();
2255 v2u32 screenSize = driver->getScreenSize();
2256 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2258 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2260 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2262 m_tooltip_element->setVisible(false);
2267 for(u32 i=0; i<m_backgrounds.size(); i++)
2269 const ImageDrawSpec &spec = m_backgrounds[i];
2270 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2273 // Image size on screen
2274 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2275 // Image rectangle on screen
2276 core::rect<s32> rect = imgrect + spec.pos;
2278 if (m_clipbackground) {
2279 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2280 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2281 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2282 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2283 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2286 const video::SColor color(255,255,255,255);
2287 const video::SColor colors[] = {color,color,color,color};
2288 draw2DImageFilterScaled(driver, texture, rect,
2289 core::rect<s32>(core::position2d<s32>(0,0),
2290 core::dimension2di(texture->getOriginalSize())),
2291 NULL/*&AbsoluteClippingRect*/, colors, true);
2294 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2295 errorstream << "\t" << spec.name << std::endl;
2302 for(u32 i=0; i<m_boxes.size(); i++)
2304 const BoxDrawSpec &spec = m_boxes[i];
2306 irr::video::SColor todraw = spec.color;
2308 todraw.setAlpha(140);
2310 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2311 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2313 driver->draw2DRectangle(todraw, rect, 0);
2318 for(u32 i=0; i<m_images.size(); i++)
2320 const ImageDrawSpec &spec = m_images[i];
2321 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2324 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2325 // Image size on screen
2326 core::rect<s32> imgrect;
2329 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2332 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2334 // Image rectangle on screen
2335 core::rect<s32> rect = imgrect + spec.pos;
2336 const video::SColor color(255,255,255,255);
2337 const video::SColor colors[] = {color,color,color,color};
2338 draw2DImageFilterScaled(driver, texture, rect,
2339 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2340 NULL/*&AbsoluteClippingRect*/, colors, true);
2343 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2344 errorstream << "\t" << spec.name << std::endl;
2351 for(u32 i=0; i<m_itemimages.size(); i++)
2356 const ImageDrawSpec &spec = m_itemimages[i];
2357 IItemDefManager *idef = m_gamedef->idef();
2359 item.deSerialize(spec.name, idef);
2360 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
2361 // Image size on screen
2362 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2363 // Image rectangle on screen
2364 core::rect<s32> rect = imgrect + spec.pos;
2365 const video::SColor color(255,255,255,255);
2366 const video::SColor colors[] = {color,color,color,color};
2367 draw2DImageFilterScaled(driver, texture, rect,
2368 core::rect<s32>(core::position2d<s32>(0,0),
2369 core::dimension2di(texture->getOriginalSize())),
2370 NULL/*&AbsoluteClippingRect*/, colors, true);
2375 Phase 0: Item slot rectangles
2376 Phase 1: Item images; prepare tooltip
2379 for(int phase=start_phase; phase<=1; phase++)
2380 for(u32 i=0; i<m_inventorylists.size(); i++)
2382 drawList(m_inventorylists[i], phase);
2388 gui::IGUIElement::draw();
2390 /* TODO find way to show tooltips on touchscreen */
2391 #ifndef HAVE_TOUCHSCREENGUI
2392 m_pointer = m_device->getCursorControl()->getPosition();
2396 Draw fields/buttons tooltips
2398 gui::IGUIElement *hovered =
2399 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2401 if (hovered != NULL) {
2402 s32 id = hovered->getID();
2406 m_old_tooltip_id = id;
2409 if (id == m_old_tooltip_id) {
2410 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2412 m_hovered_time = getTimeMs();
2413 m_old_tooltip_id = id;
2417 if (id != -1 && delta >= m_tooltip_show_delay) {
2418 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2419 iter != m_fields.end(); iter++) {
2420 if ( (iter->fid == id) && (m_tooltips[iter->fname].tooltip != "") ){
2421 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2422 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2423 m_tooltip_element->setText(narrow_to_wide(m_tooltips[iter->fname].tooltip).c_str());
2424 std::vector<std::string> tt_rows = str_split(m_tooltips[iter->fname].tooltip, '\n');
2425 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2426 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2427 int tooltip_offset_x = m_btn_height;
2428 int tooltip_offset_y = m_btn_height;
2430 tooltip_offset_x *= 3;
2431 tooltip_offset_y = 0;
2432 if (m_pointer.X > (s32)screenSize.X / 2)
2433 tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2435 s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2436 s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2437 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2438 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2439 if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2440 tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2441 m_tooltip_element->setRelativePosition(core::rect<s32>(
2442 core::position2d<s32>(tooltip_x, tooltip_y),
2443 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2445 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2446 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2447 m_tooltip_element->setVisible(true);
2448 this->bringToFront(m_tooltip_element);
2456 Draw dragged item stack
2460 skin->setFont(old_font);
2463 void GUIFormSpecMenu::updateSelectedItem()
2465 // If the selected stack has become empty for some reason, deselect it.
2466 // If the selected stack has become inaccessible, deselect it.
2467 // If the selected stack has become smaller, adjust m_selected_amount.
2468 ItemStack selected = verifySelectedItem();
2470 // WARNING: BLACK MAGIC
2471 // See if there is a stack suited for our current guess.
2472 // If such stack does not exist, clear the guess.
2473 if(m_selected_content_guess.name != "" &&
2474 selected.name == m_selected_content_guess.name &&
2475 selected.count == m_selected_content_guess.count){
2476 // Selected item fits the guess. Skip the black magic.
2478 else if(m_selected_content_guess.name != ""){
2480 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2481 const ListDrawSpec &s = m_inventorylists[i];
2482 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2485 InventoryList *list = inv->getList(s.listname);
2488 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2489 u32 item_i = i + s.start_item_i;
2490 if(item_i >= list->getSize())
2492 ItemStack stack = list->getItem(item_i);
2493 if(stack.name == m_selected_content_guess.name &&
2494 stack.count == m_selected_content_guess.count){
2496 infostream<<"Client: Changing selected content guess to "
2497 <<s.inventoryloc.dump()<<" "<<s.listname
2498 <<" "<<item_i<<std::endl;
2499 delete m_selected_item;
2500 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2501 m_selected_amount = stack.count;
2506 infostream<<"Client: Discarding selected content guess: "
2507 <<m_selected_content_guess.getItemString()<<std::endl;
2508 m_selected_content_guess.name = "";
2512 // If craftresult is nonempty and nothing else is selected, select it now.
2513 if(!m_selected_item)
2515 for(u32 i=0; i<m_inventorylists.size(); i++)
2517 const ListDrawSpec &s = m_inventorylists[i];
2518 if(s.listname == "craftpreview")
2520 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2521 InventoryList *list = inv->getList("craftresult");
2522 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2524 m_selected_item = new ItemSpec;
2525 m_selected_item->inventoryloc = s.inventoryloc;
2526 m_selected_item->listname = "craftresult";
2527 m_selected_item->i = 0;
2528 m_selected_amount = 0;
2529 m_selected_dragging = false;
2536 // If craftresult is selected, keep the whole stack selected
2537 if(m_selected_item && m_selected_item->listname == "craftresult")
2539 m_selected_amount = verifySelectedItem().count;
2543 ItemStack GUIFormSpecMenu::verifySelectedItem()
2545 // If the selected stack has become empty for some reason, deselect it.
2546 // If the selected stack has become inaccessible, deselect it.
2547 // If the selected stack has become smaller, adjust m_selected_amount.
2548 // Return the selected stack.
2552 if(m_selected_item->isValid())
2554 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2557 InventoryList *list = inv->getList(m_selected_item->listname);
2558 if(list && (u32) m_selected_item->i < list->getSize())
2560 ItemStack stack = list->getItem(m_selected_item->i);
2561 if(m_selected_amount > stack.count)
2562 m_selected_amount = stack.count;
2569 // selection was not valid
2570 delete m_selected_item;
2571 m_selected_item = NULL;
2572 m_selected_amount = 0;
2573 m_selected_dragging = false;
2578 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2582 std::map<std::string, std::string> fields;
2584 if (quitmode == quit_mode_accept) {
2585 fields["quit"] = "true";
2588 if (quitmode == quit_mode_cancel) {
2589 fields["quit"] = "true";
2590 m_text_dst->gotText(fields);
2594 if (current_keys_pending.key_down) {
2595 fields["key_down"] = "true";
2596 current_keys_pending.key_down = false;
2599 if (current_keys_pending.key_up) {
2600 fields["key_up"] = "true";
2601 current_keys_pending.key_up = false;
2604 if (current_keys_pending.key_enter) {
2605 fields["key_enter"] = "true";
2606 current_keys_pending.key_enter = false;
2609 if (current_keys_pending.key_escape) {
2610 fields["key_escape"] = "true";
2611 current_keys_pending.key_escape = false;
2614 for(unsigned int i=0; i<m_fields.size(); i++) {
2615 const FieldSpec &s = m_fields[i];
2617 std::string name = wide_to_narrow(s.fname);
2618 if(s.ftype == f_Button) {
2619 fields[name] = wide_to_narrow(s.flabel);
2621 else if(s.ftype == f_Table) {
2622 GUITable *table = getTable(s.fname);
2624 fields[name] = table->checkEvent();
2627 else if(s.ftype == f_DropDown) {
2628 // no dynamic cast possible due to some distributions shipped
2629 // without rtti support in irrlicht
2630 IGUIElement * element = getElementFromId(s.fid);
2631 gui::IGUIComboBox *e = NULL;
2632 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2633 e = static_cast<gui::IGUIComboBox*>(element);
2635 s32 selected = e->getSelected();
2636 if (selected >= 0) {
2638 wide_to_narrow(e->getItem(selected));
2641 else if (s.ftype == f_TabHeader) {
2642 // no dynamic cast possible due to some distributions shipped
2643 // without rtti support in irrlicht
2644 IGUIElement * element = getElementFromId(s.fid);
2645 gui::IGUITabControl *e = NULL;
2646 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2647 e = static_cast<gui::IGUITabControl*>(element);
2651 std::stringstream ss;
2652 ss << (e->getActiveTab() +1);
2653 fields[name] = ss.str();
2656 else if (s.ftype == f_CheckBox) {
2657 // no dynamic cast possible due to some distributions shipped
2658 // without rtti support in irrlicht
2659 IGUIElement * element = getElementFromId(s.fid);
2660 gui::IGUICheckBox *e = NULL;
2661 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2662 e = static_cast<gui::IGUICheckBox*>(element);
2667 fields[name] = "true";
2669 fields[name] = "false";
2672 else if (s.ftype == f_ScrollBar) {
2673 // no dynamic cast possible due to some distributions shipped
2674 // without rtti support in irrlicht
2675 IGUIElement * element = getElementFromId(s.fid);
2676 gui::IGUIScrollBar *e = NULL;
2677 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2678 e = static_cast<gui::IGUIScrollBar*>(element);
2682 std::stringstream os;
2684 if (s.fdefault == L"Changed")
2685 fields[name] = "CHG:" + os.str();
2687 fields[name] = "VAL:" + os.str();
2692 IGUIElement* e = getElementFromId(s.fid);
2694 fields[name] = wide_to_narrow(e->getText());
2700 m_text_dst->gotText(fields);
2704 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2706 while(tocheck != NULL) {
2707 if (tocheck == parent) {
2710 tocheck = tocheck->getParent();
2715 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2717 // The IGUITabControl renders visually using the skin's selected
2718 // font, which we override for the duration of form drawing,
2719 // but computes tab hotspots based on how it would have rendered
2720 // using the font that is selected at the time of button release.
2721 // To make these two consistent, temporarily override the skin's
2722 // font while the IGUITabControl is processing the event.
2723 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2724 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2725 s32 x = event.MouseInput.X;
2726 s32 y = event.MouseInput.Y;
2727 gui::IGUIElement *hovered =
2728 Environment->getRootGUIElement()->getElementFromPoint(
2729 core::position2d<s32>(x, y));
2730 if (hovered && isMyChild(hovered) &&
2731 hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2732 gui::IGUISkin* skin = Environment->getSkin();
2733 sanity_check(skin != NULL);
2734 gui::IGUIFont *old_font = skin->getFont();
2735 skin->setFont(m_font);
2736 bool retval = hovered->OnEvent(event);
2737 skin->setFont(old_font);
2742 // Fix Esc/Return key being eaten by checkboxen and tables
2743 if(event.EventType==EET_KEY_INPUT_EVENT) {
2744 KeyPress kp(event.KeyInput);
2745 if (kp == EscapeKey || kp == CancelKey
2746 || kp == getKeySetting("keymap_inventory")
2747 || event.KeyInput.Key==KEY_RETURN) {
2748 gui::IGUIElement *focused = Environment->getFocus();
2749 if (focused && isMyChild(focused) &&
2750 (focused->getType() == gui::EGUIET_LIST_BOX ||
2751 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2757 // Mouse wheel events: send to hovered element instead of focused
2758 if(event.EventType==EET_MOUSE_INPUT_EVENT
2759 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2760 s32 x = event.MouseInput.X;
2761 s32 y = event.MouseInput.Y;
2762 gui::IGUIElement *hovered =
2763 Environment->getRootGUIElement()->getElementFromPoint(
2764 core::position2d<s32>(x, y));
2765 if (hovered && isMyChild(hovered)) {
2766 hovered->OnEvent(event);
2771 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2772 s32 x = event.MouseInput.X;
2773 s32 y = event.MouseInput.Y;
2774 gui::IGUIElement *hovered =
2775 Environment->getRootGUIElement()->getElementFromPoint(
2776 core::position2d<s32>(x, y));
2777 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2778 m_old_tooltip_id = -1;
2781 if (!isChild(hovered,this)) {
2782 if (DoubleClickDetection(event)) {
2789 // display software keyboard when clicking edit boxes
2790 if (event.EventType == EET_MOUSE_INPUT_EVENT
2791 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2792 gui::IGUIElement *hovered =
2793 Environment->getRootGUIElement()->getElementFromPoint(
2794 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2795 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2796 bool retval = hovered->OnEvent(event);
2798 Environment->setFocus(hovered);
2800 m_JavaDialogFieldName = getNameByID(hovered->getID());
2801 std::string message = gettext("Enter ");
2802 std::string label = wide_to_narrow(getLabelByID(hovered->getID()));
2806 message += gettext(label) + ":";
2808 /* single line text input */
2811 /* multi line text input */
2812 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2816 /* passwords are always single line */
2817 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2821 porting::showInputDialog(gettext("ok"), "",
2822 wide_to_narrow(((gui::IGUIEditBox*) hovered)->getText()),
2828 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2831 memset(&translated, 0, sizeof(SEvent));
2832 translated.EventType = EET_MOUSE_INPUT_EVENT;
2833 gui::IGUIElement* root = Environment->getRootGUIElement();
2837 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2841 gui::IGUIElement* hovered = root->getElementFromPoint(
2842 core::position2d<s32>(
2844 event.TouchInput.Y));
2846 translated.MouseInput.X = event.TouchInput.X;
2847 translated.MouseInput.Y = event.TouchInput.Y;
2848 translated.MouseInput.Control = false;
2850 bool dont_send_event = false;
2852 if (event.TouchInput.touchedCount == 1) {
2853 switch (event.TouchInput.Event) {
2854 case ETIE_PRESSED_DOWN:
2855 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2856 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2857 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2858 m_down_pos = m_pointer;
2861 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2862 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2863 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2866 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2867 translated.MouseInput.ButtonStates = 0;
2868 hovered = root->getElementFromPoint(m_down_pos);
2869 /* we don't have a valid pointer element use last
2870 * known pointer pos */
2871 translated.MouseInput.X = m_pointer.X;
2872 translated.MouseInput.Y = m_pointer.Y;
2874 /* reset down pos */
2875 m_down_pos = v2s32(0,0);
2878 dont_send_event = true;
2879 //this is not supposed to happen
2881 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2882 << event.TouchInput.Event << std::endl;
2884 } else if ( (event.TouchInput.touchedCount == 2) &&
2885 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2886 hovered = root->getElementFromPoint(m_down_pos);
2888 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2889 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2890 translated.MouseInput.X = m_pointer.X;
2891 translated.MouseInput.Y = m_pointer.Y;
2894 hovered->OnEvent(translated);
2897 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
2898 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2902 hovered->OnEvent(translated);
2904 dont_send_event = true;
2906 /* ignore unhandled 2 touch events ... accidental moving for example */
2907 else if (event.TouchInput.touchedCount == 2) {
2908 dont_send_event = true;
2910 else if (event.TouchInput.touchedCount > 2) {
2912 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
2913 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
2916 if (dont_send_event) {
2920 /* check if translated event needs to be preprocessed again */
2921 if (preprocessEvent(translated)) {
2926 bool retval = hovered->OnEvent(translated);
2928 if (event.TouchInput.Event == ETIE_LEFT_UP) {
2930 m_pointer = v2s32(0,0);
2941 /******************************************************************************/
2942 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
2944 /* The following code is for capturing double-clicks of the mouse button
2945 * and translating the double-click into an EET_KEY_INPUT_EVENT event
2946 * -- which closes the form -- under some circumstances.
2948 * There have been many github issues reporting this as a bug even though it
2949 * was an intended feature. For this reason, remapping the double-click as
2950 * an ESC must be explicitly set when creating this class via the
2951 * /p remap_dbl_click parameter of the constructor.
2954 if (!m_remap_dbl_click)
2957 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2958 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
2959 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
2961 m_doubleclickdetect[1].pos = m_pointer;
2962 m_doubleclickdetect[1].time = getTimeMs();
2964 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2965 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
2970 double squaredistance =
2971 m_doubleclickdetect[0].pos
2972 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
2974 if (squaredistance > (30*30)) {
2978 SEvent* translated = new SEvent();
2979 assert(translated != 0);
2980 //translate doubleclick to escape
2981 memset(translated, 0, sizeof(SEvent));
2982 translated->EventType = irr::EET_KEY_INPUT_EVENT;
2983 translated->KeyInput.Key = KEY_ESCAPE;
2984 translated->KeyInput.Control = false;
2985 translated->KeyInput.Shift = false;
2986 translated->KeyInput.PressedDown = true;
2987 translated->KeyInput.Char = 0;
2988 OnEvent(*translated);
2990 // no need to send the key up event as we're already deleted
2991 // and no one else did notice this event
2999 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3001 if(event.EventType==EET_KEY_INPUT_EVENT) {
3002 KeyPress kp(event.KeyInput);
3003 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3004 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3007 acceptInput(quit_mode_cancel);
3010 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
3013 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3014 (kp == getKeySetting("keymap_screenshot"))) {
3015 m_client->makeScreenshot(m_device);
3017 if (event.KeyInput.PressedDown &&
3018 (event.KeyInput.Key==KEY_RETURN ||
3019 event.KeyInput.Key==KEY_UP ||
3020 event.KeyInput.Key==KEY_DOWN)
3022 switch (event.KeyInput.Key) {
3024 current_keys_pending.key_enter = true;
3027 current_keys_pending.key_up = true;
3030 current_keys_pending.key_down = true;
3034 //can't happen at all!
3035 FATAL_ERROR("Reached a source line that can't ever been reached");
3038 if (current_keys_pending.key_enter && m_allowclose) {
3039 acceptInput(quit_mode_accept);
3049 /* Mouse event other than movement, or crossing the border of inventory
3050 field while holding right mouse button
3052 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3053 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3054 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3055 event.MouseInput.isRightPressed() &&
3056 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3058 // Get selected item and hovered/clicked item (s)
3060 m_old_tooltip_id = -1;
3061 updateSelectedItem();
3062 ItemSpec s = getItemAtPos(m_pointer);
3064 Inventory *inv_selected = NULL;
3065 Inventory *inv_s = NULL;
3067 if(m_selected_item) {
3068 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3069 sanity_check(inv_selected);
3070 sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
3077 inv_s = m_invmgr->getInventory(s.inventoryloc);
3080 errorstream<<"InventoryMenu: The selected inventory location "
3081 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
3083 s.i = -1; // make it invalid again
3087 InventoryList *list = inv_s->getList(s.listname);
3089 verbosestream<<"InventoryMenu: The selected inventory list \""
3090 <<s.listname<<"\" does not exist"<<std::endl;
3091 s.i = -1; // make it invalid again
3095 if((u32)s.i >= list->getSize()) {
3096 infostream<<"InventoryMenu: The selected inventory list \""
3097 <<s.listname<<"\" is too small (i="<<s.i<<", size="
3098 <<list->getSize()<<")"<<std::endl;
3099 s.i = -1; // make it invalid again
3103 s_count = list->getItem(s.i).count;
3106 bool identical = (m_selected_item != NULL) && s.isValid() &&
3107 (inv_selected == inv_s) &&
3108 (m_selected_item->listname == s.listname) &&
3109 (m_selected_item->i == s.i);
3111 // buttons: 0 = left, 1 = right, 2 = middle
3112 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3115 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3116 { button = 0; updown = 0; }
3117 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3118 { button = 1; updown = 0; }
3119 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3120 { button = 2; updown = 0; }
3121 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3122 { button = 0; updown = 1; }
3123 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3124 { button = 1; updown = 1; }
3125 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3126 { button = 2; updown = 1; }
3127 else if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
3130 // Set this number to a positive value to generate a move action
3131 // from m_selected_item to s.
3132 u32 move_amount = 0;
3134 // Set this number to a positive value to generate a drop action
3135 // from m_selected_item.
3136 u32 drop_amount = 0;
3138 // Set this number to a positive value to generate a craft action at s.
3139 u32 craft_amount = 0;
3142 // Some mouse button has been pressed
3144 //infostream<<"Mouse button "<<button<<" pressed at p=("
3145 // <<p.X<<","<<p.Y<<")"<<std::endl;
3147 m_selected_dragging = false;
3149 if(s.isValid() && s.listname == "craftpreview") {
3150 // Craft preview has been clicked: craft
3151 craft_amount = (button == 2 ? 10 : 1);
3153 else if(m_selected_item == NULL) {
3155 // Non-empty stack has been clicked: select it
3156 m_selected_item = new ItemSpec(s);
3158 if(button == 1) // right
3159 m_selected_amount = (s_count + 1) / 2;
3160 else if(button == 2) // middle
3161 m_selected_amount = MYMIN(s_count, 10);
3163 m_selected_amount = s_count;
3165 m_selected_dragging = true;
3166 m_rmouse_auto_place = false;
3169 else { // m_selected_item != NULL
3170 assert(m_selected_amount >= 1);
3173 // Clicked a slot: move
3174 if(button == 1) // right
3176 else if(button == 2) // middle
3177 move_amount = MYMIN(m_selected_amount, 10);
3179 move_amount = m_selected_amount;
3182 if(move_amount >= m_selected_amount)
3183 m_selected_amount = 0;
3185 m_selected_amount -= move_amount;
3189 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3190 // Clicked outside of the window: drop
3191 if(button == 1) // right
3193 else if(button == 2) // middle
3194 drop_amount = MYMIN(m_selected_amount, 10);
3196 drop_amount = m_selected_amount;
3200 else if(updown == 1) {
3201 // Some mouse button has been released
3203 //infostream<<"Mouse button "<<button<<" released at p=("
3204 // <<p.X<<","<<p.Y<<")"<<std::endl;
3206 if(m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3208 // Dragged to different slot: move all selected
3209 move_amount = m_selected_amount;
3212 else if(m_selected_item != NULL && m_selected_dragging &&
3213 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3214 // Dragged outside of window: drop all selected
3215 drop_amount = m_selected_amount;
3218 m_selected_dragging = false;
3219 // Keep count of how many times right mouse button has been
3220 // clicked. One click is drag without dropping. Click + release
3221 // + click changes to drop one item when moved mode
3222 if(button == 1 && m_selected_item != NULL)
3223 m_rmouse_auto_place = !m_rmouse_auto_place;
3225 else if(updown == -1) {
3226 // Mouse has been moved and rmb is down and mouse pointer just
3227 // entered a new inventory field (checked in the entry-if, this
3228 // is the only action here that is generated by mouse movement)
3229 if(m_selected_item != NULL && s.isValid()){
3231 // TODO: middle mouse to move 10 items might be handy
3232 if (m_rmouse_auto_place) {
3233 // Only move an item if the destination slot is empty
3234 // or contains the same item type as what is going to be
3236 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3237 InventoryList *list_to = inv_s->getList(s.listname);
3238 assert(list_from && list_to);
3239 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3240 ItemStack stack_to = list_to->getItem(s.i);
3241 if (stack_to.empty() || stack_to.name == stack_from.name)
3247 // Possibly send inventory action to server
3250 // Send IACTION_MOVE
3252 assert(m_selected_item && m_selected_item->isValid());
3253 assert(s.isValid());
3255 assert(inv_selected && inv_s);
3256 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3257 InventoryList *list_to = inv_s->getList(s.listname);
3258 assert(list_from && list_to);
3259 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3260 ItemStack stack_to = list_to->getItem(s.i);
3262 // Check how many items can be moved
3263 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3264 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3265 // If source stack cannot be added to destination stack at all,
3267 if ((leftover.count == stack_from.count) &&
3268 (leftover.name == stack_from.name)) {
3269 m_selected_amount = stack_to.count;
3270 // In case the server doesn't directly swap them but instead
3271 // moves stack_to somewhere else, set this
3272 m_selected_content_guess = stack_to;
3273 m_selected_content_guess_inventory = s.inventoryloc;
3275 // Source stack goes fully into destination stack
3276 else if(leftover.empty()) {
3277 m_selected_amount -= move_amount;
3278 m_selected_content_guess = ItemStack(); // Clear
3280 // Source stack goes partly into destination stack
3282 move_amount -= leftover.count;
3283 m_selected_amount -= move_amount;
3284 m_selected_content_guess = ItemStack(); // Clear
3287 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
3288 IMoveAction *a = new IMoveAction();
3289 a->count = move_amount;
3290 a->from_inv = m_selected_item->inventoryloc;
3291 a->from_list = m_selected_item->listname;
3292 a->from_i = m_selected_item->i;
3293 a->to_inv = s.inventoryloc;
3294 a->to_list = s.listname;
3296 m_invmgr->inventoryAction(a);
3298 else if(drop_amount > 0) {
3299 m_selected_content_guess = ItemStack(); // Clear
3301 // Send IACTION_DROP
3303 assert(m_selected_item && m_selected_item->isValid());
3304 assert(inv_selected);
3305 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3307 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3309 // Check how many items can be dropped
3310 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3311 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3312 m_selected_amount -= drop_amount;
3314 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
3315 IDropAction *a = new IDropAction();
3316 a->count = drop_amount;
3317 a->from_inv = m_selected_item->inventoryloc;
3318 a->from_list = m_selected_item->listname;
3319 a->from_i = m_selected_item->i;
3320 m_invmgr->inventoryAction(a);
3322 else if(craft_amount > 0) {
3323 m_selected_content_guess = ItemStack(); // Clear
3325 // Send IACTION_CRAFT
3327 assert(s.isValid());
3330 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
3331 ICraftAction *a = new ICraftAction();
3332 a->count = craft_amount;
3333 a->craft_inv = s.inventoryloc;
3334 m_invmgr->inventoryAction(a);
3337 // If m_selected_amount has been decreased to zero, deselect
3338 if(m_selected_amount == 0) {
3339 delete m_selected_item;
3340 m_selected_item = NULL;
3341 m_selected_amount = 0;
3342 m_selected_dragging = false;
3343 m_selected_content_guess = ItemStack();
3345 m_old_pointer = m_pointer;
3347 if(event.EventType==EET_GUI_EVENT) {
3349 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
3351 // find the element that was clicked
3352 for(unsigned int i=0; i<m_fields.size(); i++) {
3353 FieldSpec &s = m_fields[i];
3354 if ((s.ftype == f_TabHeader) &&
3355 (s.fid == event.GUIEvent.Caller->getID())) {
3363 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
3365 if(!canTakeFocus(event.GUIEvent.Element)) {
3366 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3368 // Returning true disables focus change
3372 if((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3373 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3374 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3375 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3376 unsigned int btn_id = event.GUIEvent.Caller->getID();
3378 if (btn_id == 257) {
3380 acceptInput(quit_mode_accept);
3384 m_text_dst->gotText(narrow_to_wide("ExitButton"));
3386 // quitMenu deallocates menu
3390 // find the element that was clicked
3391 for(u32 i=0; i<m_fields.size(); i++) {
3392 FieldSpec &s = m_fields[i];
3393 // if its a button, set the send field so
3394 // lua knows which button was pressed
3395 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3396 (s.fid == event.GUIEvent.Caller->getID())) {
3400 acceptInput(quit_mode_accept);
3403 m_text_dst->gotText(narrow_to_wide("ExitButton"));
3407 acceptInput(quit_mode_no);
3412 else if ((s.ftype == f_DropDown) &&
3413 (s.fid == event.GUIEvent.Caller->getID())) {
3414 // only send the changed dropdown
3415 for(u32 i=0; i<m_fields.size(); i++) {
3416 FieldSpec &s2 = m_fields[i];
3417 if (s2.ftype == f_DropDown) {
3422 acceptInput(quit_mode_no);
3424 // revert configuration to make sure dropdowns are sent on
3425 // regular button click
3426 for(u32 i=0; i<m_fields.size(); i++) {
3427 FieldSpec &s2 = m_fields[i];
3428 if (s2.ftype == f_DropDown) {
3434 else if ((s.ftype == f_ScrollBar) &&
3435 (s.fid == event.GUIEvent.Caller->getID()))
3437 s.fdefault = L"Changed";
3438 acceptInput(quit_mode_no);
3444 if(event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3445 if(event.GUIEvent.Caller->getID() > 257) {
3448 acceptInput(quit_mode_accept);
3451 current_keys_pending.key_enter = true;
3454 // quitMenu deallocates menu
3459 if(event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3460 int current_id = event.GUIEvent.Caller->getID();
3461 if(current_id > 257) {
3462 // find the element that was clicked
3463 for(u32 i=0; i<m_fields.size(); i++) {
3464 FieldSpec &s = m_fields[i];
3465 // if it's a table, set the send field
3466 // so lua knows which table was changed
3467 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3478 return Parent ? Parent->OnEvent(event) : false;
3482 * get name of element by element id
3483 * @param id of element
3484 * @return name string or empty string
3486 std::wstring GUIFormSpecMenu::getNameByID(s32 id)
3488 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3489 iter != m_fields.end(); iter++) {
3490 if (iter->fid == id) {
3498 * get label of element by id
3499 * @param id of element
3500 * @return label string or empty string
3502 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3504 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3505 iter != m_fields.end(); iter++) {
3506 if (iter->fid == id) {
3507 return iter->flabel;