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 "tile.h" // ITextureSource
41 #include "hud.h" // drawItemStack
43 #include "util/string.h"
44 #include "util/numeric.h"
48 #include "scripting_game.h"
53 #include "util/string.h" // for parseColorString()
54 #include "fontengine.h"
56 #define MY_CHECKPOS(a,b) \
57 if (v_pos.size() != 2) { \
58 errorstream<< "Invalid pos for element " << a << "specified: \"" \
59 << parts[b] << "\"" << std::endl; \
63 #define MY_CHECKGEOM(a,b) \
64 if (v_geom.size() != 2) { \
65 errorstream<< "Invalid pos for element " << a << "specified: \"" \
66 << parts[b] << "\"" << std::endl; \
72 static unsigned int font_line_height(gui::IGUIFont *font)
74 return font->getDimension(L"Ay").Height + font->getKerningHeight();
77 static gui::IGUIFont *select_font_by_line_height(double target_line_height)
79 // We don't get to directly select a font according to its
80 // baseline-to-baseline height. Rather, we select by em size.
81 // The ratio between these varies between fonts. The font
82 // engine also takes its size parameter not specified in pixels,
83 // as we want, but scaled by display density and gui_scaling,
84 // so invert that scaling here. Use a binary search among
85 // requested sizes to find the right font. Our starting bounds
86 // are an em height of 1 (being careful not to request size 0,
87 // which crashes the freetype system) and an em height of the
88 // target baseline-to-baseline height.
89 unsigned int loreq = ceil(1 / porting::getDisplayDensity()
90 / g_settings->getFloat("gui_scaling"));
91 unsigned int hireq = ceil(target_line_height
92 / porting::getDisplayDensity()
93 / g_settings->getFloat("gui_scaling"));
94 unsigned int lohgt = font_line_height(glb_fontengine->getFont(loreq));
95 unsigned int hihgt = font_line_height(glb_fontengine->getFont(hireq));
96 while(hireq - loreq > 1 && lohgt != hihgt) {
97 unsigned int nureq = (loreq + hireq) >> 1;
98 unsigned int nuhgt = font_line_height(glb_fontengine->getFont(nureq));
99 if(nuhgt < target_line_height) {
107 return glb_fontengine->getFont(target_line_height - lohgt < hihgt - target_line_height ? loreq : hireq);
110 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
111 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
112 InventoryManager *invmgr, IGameDef *gamedef,
113 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
115 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
121 m_selected_item(NULL),
122 m_selected_amount(0),
123 m_selected_dragging(false),
124 m_tooltip_element(NULL),
126 m_old_tooltip_id(-1),
131 m_formspec_version(0),
134 ,m_JavaDialogFieldName(L"")
137 current_keys_pending.key_down = false;
138 current_keys_pending.key_up = false;
139 current_keys_pending.key_enter = false;
140 current_keys_pending.key_escape = false;
142 m_doubleclickdetect[0].time = 0;
143 m_doubleclickdetect[1].time = 0;
145 m_doubleclickdetect[0].pos = v2s32(0, 0);
146 m_doubleclickdetect[1].pos = v2s32(0, 0);
148 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
151 GUIFormSpecMenu::~GUIFormSpecMenu()
155 for (u32 i = 0; i < m_tables.size(); ++i) {
156 GUITable *table = m_tables[i].second;
160 delete m_selected_item;
162 if (m_form_src != NULL) {
165 if (m_text_dst != NULL) {
170 void GUIFormSpecMenu::removeChildren()
172 const core::list<gui::IGUIElement*> &children = getChildren();
174 while(!children.empty()) {
175 (*children.getLast())->remove();
178 if(m_tooltip_element) {
179 m_tooltip_element->remove();
180 m_tooltip_element->drop();
181 m_tooltip_element = NULL;
186 void GUIFormSpecMenu::setInitialFocus()
188 // Set initial focus according to following order of precedence:
189 // 1. first empty editbox
193 // 5. first focusable (not statictext, not tabheader)
194 // 6. first child element
196 core::list<gui::IGUIElement*> children = getChildren();
198 // in case "children" contains any NULL elements, remove them
199 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
200 it != children.end();) {
204 it = children.erase(it);
207 // 1. first empty editbox
208 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
209 it != children.end(); ++it) {
210 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
211 && (*it)->getText()[0] == 0) {
212 Environment->setFocus(*it);
218 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
219 it != children.end(); ++it) {
220 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
221 Environment->setFocus(*it);
227 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
228 it != children.end(); ++it) {
229 if ((*it)->getTypeName() == std::string("GUITable")) {
230 Environment->setFocus(*it);
236 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
237 it != children.end(); --it) {
238 if ((*it)->getType() == gui::EGUIET_BUTTON) {
239 Environment->setFocus(*it);
244 // 5. first focusable (not statictext, not tabheader)
245 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
246 it != children.end(); ++it) {
247 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
248 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
249 Environment->setFocus(*it);
254 // 6. first child element
255 if (children.empty())
256 Environment->setFocus(this);
258 Environment->setFocus(*(children.begin()));
261 GUITable* GUIFormSpecMenu::getTable(std::wstring tablename)
263 for (u32 i = 0; i < m_tables.size(); ++i) {
264 if (tablename == m_tables[i].first.fname)
265 return m_tables[i].second;
270 std::vector<std::string> split(const std::string &s, char delim) {
271 std::vector<std::string> tokens;
273 std::string current = "";
274 bool last_was_escape = false;
275 for(unsigned int i=0; i < s.size(); i++) {
276 if (last_was_escape) {
278 current += s.c_str()[i];
279 last_was_escape = false;
282 if (s.c_str()[i] == delim) {
283 tokens.push_back(current);
285 last_was_escape = false;
287 else if (s.c_str()[i] == '\\'){
288 last_was_escape = true;
291 current += s.c_str()[i];
292 last_was_escape = false;
297 tokens.push_back(current);
302 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
304 std::vector<std::string> parts = split(element,',');
306 if (((parts.size() == 2) || parts.size() == 3) ||
307 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
309 if (parts[1].find(';') != std::string::npos)
310 parts[1] = parts[1].substr(0,parts[1].find(';'));
312 data->invsize.X = MYMAX(0, stof(parts[0]));
313 data->invsize.Y = MYMAX(0, stof(parts[1]));
316 if (parts.size() == 3) {
317 if (parts[2] == "true") {
318 lockSize(true,v2u32(800,600));
322 data->explicit_size = true;
325 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
328 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
330 if (m_gamedef == 0) {
331 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
335 std::vector<std::string> parts = split(element,';');
337 if (((parts.size() == 4) || (parts.size() == 5)) ||
338 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
340 std::string location = parts[0];
341 std::string listname = parts[1];
342 std::vector<std::string> v_pos = split(parts[2],',');
343 std::vector<std::string> v_geom = split(parts[3],',');
344 std::string startindex = "";
345 if (parts.size() == 5)
346 startindex = parts[4];
348 MY_CHECKPOS("list",2);
349 MY_CHECKGEOM("list",3);
351 InventoryLocation loc;
353 if(location == "context" || location == "current_name")
354 loc = m_current_inventory_location;
356 loc.deSerialize(location);
358 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
359 pos.X += stof(v_pos[0]) * (float)spacing.X;
360 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
363 geom.X = stoi(v_geom[0]);
364 geom.Y = stoi(v_geom[1]);
368 start_i = stoi(startindex);
370 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
371 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
375 if(!data->explicit_size)
376 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
377 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
380 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
383 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
385 std::vector<std::string> parts = split(element,';');
387 if (((parts.size() >= 3) || (parts.size() <= 4)) ||
388 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
390 std::vector<std::string> v_pos = split(parts[0],',');
391 std::string name = parts[1];
392 std::string label = parts[2];
393 std::string selected = "";
395 if (parts.size() >= 4)
398 MY_CHECKPOS("checkbox",0);
401 pos.X += stof(v_pos[0]) * (float) spacing.X;
402 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
404 bool fselected = false;
406 if (selected == "true")
409 std::wstring wlabel = narrow_to_wide(label.c_str());
411 core::rect<s32> rect = core::rect<s32>(
412 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
413 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
414 pos.Y + ((imgsize.Y/2) + m_btn_height));
417 narrow_to_wide(name.c_str()),
418 wlabel, //Needed for displaying text on MSVC
423 spec.ftype = f_CheckBox;
425 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
426 spec.fid, spec.flabel.c_str());
428 if (spec.fname == data->focused_fieldname) {
429 Environment->setFocus(e);
432 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
433 m_fields.push_back(spec);
436 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
439 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
441 std::vector<std::string> parts = split(element,';');
443 if (parts.size() >= 5) {
444 std::vector<std::string> v_pos = split(parts[0],',');
445 std::vector<std::string> v_dim = split(parts[1],',');
446 std::string name = parts[2];
447 std::string value = parts[4];
449 MY_CHECKPOS("scrollbar",0);
452 pos.X += stof(v_pos[0]) * (float) spacing.X;
453 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
455 if (v_dim.size() != 2) {
456 errorstream<< "Invalid size for element " << "scrollbar"
457 << "specified: \"" << parts[1] << "\"" << std::endl;
462 dim.X = stof(v_dim[0]) * (float) spacing.X;
463 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
465 core::rect<s32> rect =
466 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
469 narrow_to_wide(name.c_str()),
475 bool is_horizontal = true;
477 if (parts[2] == "vertical")
478 is_horizontal = false;
480 spec.ftype = f_ScrollBar;
482 gui::IGUIScrollBar* e =
483 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
487 e->setPos(stoi(parts[4]));
489 e->setLargeStep(100);
491 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
492 m_fields.push_back(spec);
495 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
498 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
500 std::vector<std::string> parts = split(element,';');
502 if ((parts.size() == 3) ||
503 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
505 std::vector<std::string> v_pos = split(parts[0],',');
506 std::vector<std::string> v_geom = split(parts[1],',');
507 std::string name = unescape_string(parts[2]);
509 MY_CHECKPOS("image",0);
510 MY_CHECKGEOM("image",1);
512 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
513 pos.X += stof(v_pos[0]) * (float) spacing.X;
514 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
517 geom.X = stof(v_geom[0]) * (float)imgsize.X;
518 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
520 if(!data->explicit_size)
521 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
522 m_images.push_back(ImageDrawSpec(name, pos, geom));
526 if (parts.size() == 2) {
527 std::vector<std::string> v_pos = split(parts[0],',');
528 std::string name = unescape_string(parts[1]);
530 MY_CHECKPOS("image",0);
532 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
533 pos.X += stof(v_pos[0]) * (float) spacing.X;
534 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
536 if(!data->explicit_size)
537 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
538 m_images.push_back(ImageDrawSpec(name, pos));
541 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
544 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
546 std::vector<std::string> parts = split(element,';');
548 if ((parts.size() == 3) ||
549 ((parts.size() > 3) && (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];
555 MY_CHECKPOS("itemimage",0);
556 MY_CHECKGEOM("itemimage",1);
558 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
559 pos.X += stof(v_pos[0]) * (float) spacing.X;
560 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
563 geom.X = stof(v_geom[0]) * (float)imgsize.X;
564 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
566 if(!data->explicit_size)
567 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
568 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
571 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
574 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
577 std::vector<std::string> parts = split(element,';');
579 if ((parts.size() == 4) ||
580 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
582 std::vector<std::string> v_pos = split(parts[0],',');
583 std::vector<std::string> v_geom = split(parts[1],',');
584 std::string name = parts[2];
585 std::string label = parts[3];
587 MY_CHECKPOS("button",0);
588 MY_CHECKGEOM("button",1);
591 pos.X += stof(v_pos[0]) * (float)spacing.X;
592 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
595 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
596 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
598 core::rect<s32> rect =
599 core::rect<s32>(pos.X, pos.Y - m_btn_height,
600 pos.X + geom.X, pos.Y + m_btn_height);
602 if(!data->explicit_size)
603 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
605 label = unescape_string(label);
607 std::wstring wlabel = narrow_to_wide(label.c_str());
610 narrow_to_wide(name.c_str()),
615 spec.ftype = f_Button;
616 if(type == "button_exit")
618 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
619 spec.flabel.c_str());
621 if (spec.fname == data->focused_fieldname) {
622 Environment->setFocus(e);
625 m_fields.push_back(spec);
628 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
631 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
633 std::vector<std::string> parts = split(element,';');
635 if (((parts.size() == 3) || (parts.size() == 4)) ||
636 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
638 std::vector<std::string> v_pos = split(parts[0],',');
639 std::vector<std::string> v_geom = split(parts[1],',');
640 std::string name = unescape_string(parts[2]);
642 MY_CHECKPOS("background",0);
643 MY_CHECKGEOM("background",1);
645 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
646 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
647 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
650 geom.X = stof(v_geom[0]) * (float)spacing.X;
651 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
653 if (parts.size() == 4) {
654 m_clipbackground = is_yes(parts[3]);
655 if (m_clipbackground) {
656 pos.X = stoi(v_pos[0]); //acts as offset
657 pos.Y = stoi(v_pos[1]); //acts as offset
661 if(!data->explicit_size)
662 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
663 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
666 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
669 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
671 std::vector<std::string> parts = split(element,';');
673 data->table_options.clear();
674 for (size_t i = 0; i < parts.size(); ++i) {
675 // Parse table option
676 std::string opt = unescape_string(parts[i]);
677 data->table_options.push_back(GUITable::splitOption(opt));
681 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
683 std::vector<std::string> parts = split(element,';');
685 data->table_columns.clear();
686 for (size_t i = 0; i < parts.size(); ++i) {
687 std::vector<std::string> col_parts = split(parts[i],',');
688 GUITable::TableColumn column;
690 if (!col_parts.empty())
691 column.type = col_parts[0];
692 // Parse column options
693 for (size_t j = 1; j < col_parts.size(); ++j) {
694 std::string opt = unescape_string(col_parts[j]);
695 column.options.push_back(GUITable::splitOption(opt));
697 data->table_columns.push_back(column);
701 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
703 std::vector<std::string> parts = split(element,';');
705 if (((parts.size() == 4) || (parts.size() == 5)) ||
706 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
708 std::vector<std::string> v_pos = split(parts[0],',');
709 std::vector<std::string> v_geom = split(parts[1],',');
710 std::string name = parts[2];
711 std::vector<std::string> items = split(parts[3],',');
712 std::string str_initial_selection = "";
713 std::string str_transparent = "false";
715 if (parts.size() >= 5)
716 str_initial_selection = parts[4];
718 MY_CHECKPOS("table",0);
719 MY_CHECKGEOM("table",1);
722 pos.X += stof(v_pos[0]) * (float)spacing.X;
723 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
726 geom.X = stof(v_geom[0]) * (float)spacing.X;
727 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
730 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
732 std::wstring fname_w = narrow_to_wide(name.c_str());
741 spec.ftype = f_Table;
743 for (unsigned int i = 0; i < items.size(); ++i) {
744 items[i] = unescape_string(items[i]);
747 //now really show table
748 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
751 if (spec.fname == data->focused_fieldname) {
752 Environment->setFocus(e);
755 e->setTable(data->table_options, data->table_columns, items);
757 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
758 e->setDynamicData(data->table_dyndata[fname_w]);
761 if ((str_initial_selection != "") &&
762 (str_initial_selection != "0"))
763 e->setSelected(stoi(str_initial_selection.c_str()));
765 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
766 m_fields.push_back(spec);
769 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
772 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
774 std::vector<std::string> parts = split(element,';');
776 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
777 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
779 std::vector<std::string> v_pos = split(parts[0],',');
780 std::vector<std::string> v_geom = split(parts[1],',');
781 std::string name = parts[2];
782 std::vector<std::string> items = split(parts[3],',');
783 std::string str_initial_selection = "";
784 std::string str_transparent = "false";
786 if (parts.size() >= 5)
787 str_initial_selection = parts[4];
789 if (parts.size() >= 6)
790 str_transparent = parts[5];
792 MY_CHECKPOS("textlist",0);
793 MY_CHECKGEOM("textlist",1);
796 pos.X += stof(v_pos[0]) * (float)spacing.X;
797 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
800 geom.X = stof(v_geom[0]) * (float)spacing.X;
801 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
804 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
806 std::wstring fname_w = narrow_to_wide(name.c_str());
815 spec.ftype = f_Table;
817 for (unsigned int i = 0; i < items.size(); ++i) {
818 items[i] = unescape_string(items[i]);
821 //now really show list
822 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
825 if (spec.fname == data->focused_fieldname) {
826 Environment->setFocus(e);
829 e->setTextList(items, is_yes(str_transparent));
831 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
832 e->setDynamicData(data->table_dyndata[fname_w]);
835 if ((str_initial_selection != "") &&
836 (str_initial_selection != "0"))
837 e->setSelected(stoi(str_initial_selection.c_str()));
839 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
840 m_fields.push_back(spec);
843 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
847 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
849 std::vector<std::string> parts = split(element,';');
851 if ((parts.size() == 5) ||
852 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
854 std::vector<std::string> v_pos = split(parts[0],',');
855 std::string name = parts[2];
856 std::vector<std::string> items = split(parts[3],',');
857 std::string str_initial_selection = "";
858 str_initial_selection = parts[4];
860 MY_CHECKPOS("dropdown",0);
863 pos.X += stof(v_pos[0]) * (float)spacing.X;
864 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
866 s32 width = stof(parts[1]) * (float)spacing.Y;
868 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
869 pos.X + width, pos.Y + (m_btn_height * 2));
871 std::wstring fname_w = narrow_to_wide(name.c_str());
880 spec.ftype = f_DropDown;
883 //now really show list
884 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
886 if (spec.fname == data->focused_fieldname) {
887 Environment->setFocus(e);
890 for (unsigned int i=0; i < items.size(); i++) {
891 e->addItem(narrow_to_wide(items[i]).c_str());
894 if (str_initial_selection != "")
895 e->setSelected(stoi(str_initial_selection.c_str())-1);
897 m_fields.push_back(spec);
900 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
901 << element << "'" << std::endl;
904 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
906 std::vector<std::string> parts = split(element,';');
908 if ((parts.size() == 4) ||
909 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
911 std::vector<std::string> v_pos = split(parts[0],',');
912 std::vector<std::string> v_geom = split(parts[1],',');
913 std::string name = parts[2];
914 std::string label = parts[3];
916 MY_CHECKPOS("pwdfield",0);
917 MY_CHECKGEOM("pwdfield",1);
920 pos.X += stof(v_pos[0]) * (float)spacing.X;
921 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
924 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
926 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
927 pos.Y -= m_btn_height;
928 geom.Y = m_btn_height*2;
930 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
932 label = unescape_string(label);
934 std::wstring wlabel = narrow_to_wide(label.c_str());
937 narrow_to_wide(name.c_str()),
944 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
946 if (spec.fname == data->focused_fieldname) {
947 Environment->setFocus(e);
950 if (label.length() >= 1)
952 int font_height = font_line_height(m_font);
953 rect.UpperLeftCorner.Y -= font_height;
954 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
955 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
958 e->setPasswordBox(true,L'*');
961 evt.EventType = EET_KEY_INPUT_EVENT;
962 evt.KeyInput.Key = KEY_END;
963 evt.KeyInput.Char = 0;
964 evt.KeyInput.Control = 0;
965 evt.KeyInput.Shift = 0;
966 evt.KeyInput.PressedDown = true;
968 m_fields.push_back(spec);
971 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
974 void GUIFormSpecMenu::parseSimpleField(parserData* data,
975 std::vector<std::string> &parts)
977 std::string name = parts[0];
978 std::string label = parts[1];
979 std::string default_val = parts[2];
981 core::rect<s32> rect;
983 if(data->explicit_size)
984 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
986 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
987 pos.Y = ((m_fields.size()+2)*60);
988 v2s32 size = DesiredRect.getSize();
990 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
991 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
995 default_val = m_form_src->resolveText(default_val);
997 default_val = unescape_string(default_val);
998 label = unescape_string(label);
1000 std::wstring wlabel = narrow_to_wide(label.c_str());
1003 narrow_to_wide(name.c_str()),
1005 narrow_to_wide(default_val.c_str()),
1011 // spec field id to 0, this stops submit searching for a value that isn't there
1012 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1017 gui::IGUIEditBox *e =
1018 Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1020 if (spec.fname == data->focused_fieldname) {
1021 Environment->setFocus(e);
1025 evt.EventType = EET_KEY_INPUT_EVENT;
1026 evt.KeyInput.Key = KEY_END;
1027 evt.KeyInput.Char = 0;
1028 evt.KeyInput.Control = 0;
1029 evt.KeyInput.Shift = 0;
1030 evt.KeyInput.PressedDown = true;
1033 if (label.length() >= 1)
1035 int font_height = font_line_height(m_font);
1036 rect.UpperLeftCorner.Y -= font_height;
1037 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1038 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1042 m_fields.push_back(spec);
1045 void GUIFormSpecMenu::parseTextArea(parserData* data,
1046 std::vector<std::string>& parts,std::string type)
1049 std::vector<std::string> v_pos = split(parts[0],',');
1050 std::vector<std::string> v_geom = split(parts[1],',');
1051 std::string name = parts[2];
1052 std::string label = parts[3];
1053 std::string default_val = parts[4];
1055 MY_CHECKPOS(type,0);
1056 MY_CHECKGEOM(type,1);
1059 pos.X = stof(v_pos[0]) * (float) spacing.X;
1060 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1064 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1066 if (type == "textarea")
1068 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1069 pos.Y += m_btn_height;
1073 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1074 pos.Y -= m_btn_height;
1075 geom.Y = m_btn_height*2;
1078 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1080 if(!data->explicit_size)
1081 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1084 default_val = m_form_src->resolveText(default_val);
1087 default_val = unescape_string(default_val);
1088 label = unescape_string(label);
1090 std::wstring wlabel = narrow_to_wide(label.c_str());
1093 narrow_to_wide(name.c_str()),
1095 narrow_to_wide(default_val.c_str()),
1101 // spec field id to 0, this stops submit searching for a value that isn't there
1102 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1107 gui::IGUIEditBox *e =
1108 Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1110 if (spec.fname == data->focused_fieldname) {
1111 Environment->setFocus(e);
1114 if (type == "textarea")
1116 e->setMultiLine(true);
1117 e->setWordWrap(true);
1118 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1121 evt.EventType = EET_KEY_INPUT_EVENT;
1122 evt.KeyInput.Key = KEY_END;
1123 evt.KeyInput.Char = 0;
1124 evt.KeyInput.Control = 0;
1125 evt.KeyInput.Shift = 0;
1126 evt.KeyInput.PressedDown = true;
1130 if (label.length() >= 1)
1132 int font_height = font_line_height(m_font);
1133 rect.UpperLeftCorner.Y -= font_height;
1134 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1135 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1138 m_fields.push_back(spec);
1141 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1144 std::vector<std::string> parts = split(element,';');
1146 if (parts.size() == 3 || parts.size() == 4) {
1147 parseSimpleField(data,parts);
1151 if ((parts.size() == 5) ||
1152 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1154 parseTextArea(data,parts,type);
1157 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1160 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1162 std::vector<std::string> parts = split(element,';');
1164 if ((parts.size() == 2) ||
1165 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1167 std::vector<std::string> v_pos = split(parts[0],',');
1168 std::string text = parts[1];
1170 MY_CHECKPOS("label",0);
1172 v2s32 pos = padding;
1173 pos.X += stof(v_pos[0]) * (float)spacing.X;
1174 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1176 if(!data->explicit_size)
1177 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1179 int font_height = font_line_height(m_font);
1181 text = unescape_string(text);
1182 std::vector<std::string> lines = split(text, '\n');
1184 for (unsigned int i = 0; i != lines.size(); i++) {
1185 // Lines are spaced at the nominal distance of
1186 // 2/5 inventory slot, even if the font doesn't
1187 // quite match that. This provides consistent
1188 // form layout, at the expense of sometimes
1189 // having sub-optimal spacing for the font.
1190 // We multiply by 2 and then divide by 5, rather
1191 // than multiply by 0.4, to get exact results
1192 // in the integer cases: 0.4 is not exactly
1193 // representable in binary floating point.
1194 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1195 std::wstring wlabel = narrow_to_wide(lines[i].c_str());
1196 core::rect<s32> rect = core::rect<s32>(
1197 pos.X, posy - font_height,
1198 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1199 posy + font_height);
1206 gui::IGUIStaticText *e =
1207 Environment->addStaticText(spec.flabel.c_str(),
1208 rect, false, false, this, spec.fid);
1209 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1211 m_fields.push_back(spec);
1216 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1219 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1221 std::vector<std::string> parts = split(element,';');
1223 if ((parts.size() == 2) ||
1224 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1226 std::vector<std::string> v_pos = split(parts[0],',');
1227 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1229 MY_CHECKPOS("vertlabel",1);
1231 v2s32 pos = padding;
1232 pos.X += stof(v_pos[0]) * (float)spacing.X;
1233 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1235 core::rect<s32> rect = core::rect<s32>(
1236 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1238 font_line_height(m_font)
1240 +((imgsize.Y/2)- m_btn_height));
1241 //actually text.length() would be correct but adding +1 avoids to break all mods
1243 if(!data->explicit_size)
1244 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1246 std::wstring label = L"";
1248 for (unsigned int i=0; i < text.length(); i++) {
1259 gui::IGUIStaticText *t =
1260 Environment->addStaticText(spec.flabel.c_str(), rect, false, false, this, spec.fid);
1261 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1262 m_fields.push_back(spec);
1265 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1268 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1271 std::vector<std::string> parts = split(element,';');
1273 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1274 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1276 std::vector<std::string> v_pos = split(parts[0],',');
1277 std::vector<std::string> v_geom = split(parts[1],',');
1278 std::string image_name = parts[2];
1279 std::string name = parts[3];
1280 std::string label = parts[4];
1282 MY_CHECKPOS("imagebutton",0);
1283 MY_CHECKGEOM("imagebutton",1);
1285 v2s32 pos = padding;
1286 pos.X += stof(v_pos[0]) * (float)spacing.X;
1287 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1289 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1290 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1292 bool noclip = false;
1293 bool drawborder = true;
1294 std::string pressed_image_name = "";
1296 if (parts.size() >= 7) {
1297 if (parts[5] == "true")
1299 if (parts[6] == "false")
1303 if (parts.size() >= 8) {
1304 pressed_image_name = parts[7];
1307 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1309 if(!data->explicit_size)
1310 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1312 image_name = unescape_string(image_name);
1313 pressed_image_name = unescape_string(pressed_image_name);
1314 label = unescape_string(label);
1316 std::wstring wlabel = narrow_to_wide(label.c_str());
1319 narrow_to_wide(name.c_str()),
1321 narrow_to_wide(image_name.c_str()),
1324 spec.ftype = f_Button;
1325 if(type == "image_button_exit")
1326 spec.is_exit = true;
1328 video::ITexture *texture = 0;
1329 video::ITexture *pressed_texture = 0;
1330 texture = m_tsrc->getTexture(image_name);
1331 if (pressed_image_name != "")
1332 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1334 pressed_texture = texture;
1336 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1338 if (spec.fname == data->focused_fieldname) {
1339 Environment->setFocus(e);
1342 e->setUseAlphaChannel(true);
1343 e->setImage(texture);
1344 e->setPressedImage(pressed_texture);
1345 e->setScaleImage(true);
1346 e->setNotClipped(noclip);
1347 e->setDrawBorder(drawborder);
1349 m_fields.push_back(spec);
1353 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1356 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1358 std::vector<std::string> parts = split(element,';');
1360 if (((parts.size() == 4) || (parts.size() == 6)) ||
1361 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1363 std::vector<std::string> v_pos = split(parts[0],',');
1364 std::string name = parts[1];
1365 std::vector<std::string> buttons = split(parts[2],',');
1366 std::string str_index = parts[3];
1367 bool show_background = true;
1368 bool show_border = true;
1369 int tab_index = stoi(str_index) -1;
1371 MY_CHECKPOS("tabheader",0);
1373 if (parts.size() == 6) {
1374 if (parts[4] == "true")
1375 show_background = false;
1376 if (parts[5] == "false")
1377 show_border = false;
1381 narrow_to_wide(name.c_str()),
1387 spec.ftype = f_TabHeader;
1390 pos.X += stof(v_pos[0]) * (float)spacing.X;
1391 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1393 geom.X = DesiredRect.getWidth();
1394 geom.Y = m_btn_height*2;
1396 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1399 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1400 show_background, show_border, spec.fid);
1401 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1402 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1403 e->setTabHeight(m_btn_height*2);
1405 if (spec.fname == data->focused_fieldname) {
1406 Environment->setFocus(e);
1409 e->setNotClipped(true);
1411 for (unsigned int i=0; i< buttons.size(); i++) {
1412 e->addTab(narrow_to_wide(buttons[i]).c_str(), -1);
1415 if ((tab_index >= 0) &&
1416 (buttons.size() < INT_MAX) &&
1417 (tab_index < (int) buttons.size()))
1418 e->setActiveTab(tab_index);
1420 m_fields.push_back(spec);
1423 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1424 << element << "'" << std::endl;
1427 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1430 if (m_gamedef == 0) {
1432 "WARNING: invalid use of item_image_button with m_gamedef==0"
1437 std::vector<std::string> parts = split(element,';');
1439 if ((parts.size() == 5) ||
1440 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1442 std::vector<std::string> v_pos = split(parts[0],',');
1443 std::vector<std::string> v_geom = split(parts[1],',');
1444 std::string item_name = parts[2];
1445 std::string name = parts[3];
1446 std::string label = parts[4];
1448 MY_CHECKPOS("itemimagebutton",0);
1449 MY_CHECKGEOM("itemimagebutton",1);
1451 v2s32 pos = padding;
1452 pos.X += stof(v_pos[0]) * (float)spacing.X;
1453 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1455 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1456 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1458 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1460 if(!data->explicit_size)
1461 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1463 IItemDefManager *idef = m_gamedef->idef();
1465 item.deSerialize(item_name, idef);
1466 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1468 m_tooltips[narrow_to_wide(name.c_str())] =
1469 TooltipSpec (item.getDefinition(idef).description,
1470 m_default_tooltip_bgcolor,
1471 m_default_tooltip_color);
1473 label = unescape_string(label);
1475 narrow_to_wide(name.c_str()),
1476 narrow_to_wide(label.c_str()),
1477 narrow_to_wide(item_name.c_str()),
1481 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1483 if (spec.fname == data->focused_fieldname) {
1484 Environment->setFocus(e);
1487 e->setUseAlphaChannel(true);
1488 e->setImage(texture);
1489 e->setPressedImage(texture);
1490 e->setScaleImage(true);
1491 spec.ftype = f_Button;
1492 rect+=data->basepos-padding;
1494 m_fields.push_back(spec);
1497 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1500 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1502 std::vector<std::string> parts = split(element,';');
1504 if ((parts.size() == 3) ||
1505 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1507 std::vector<std::string> v_pos = split(parts[0],',');
1508 std::vector<std::string> v_geom = split(parts[1],',');
1510 MY_CHECKPOS("box",0);
1511 MY_CHECKGEOM("box",1);
1513 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1514 pos.X += stof(v_pos[0]) * (float) spacing.X;
1515 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1518 geom.X = stof(v_geom[0]) * (float)spacing.X;
1519 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1521 video::SColor tmp_color;
1523 if (parseColorString(parts[2], tmp_color, false)) {
1524 BoxDrawSpec spec(pos, geom, tmp_color);
1526 m_boxes.push_back(spec);
1529 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1533 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1536 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1538 std::vector<std::string> parts = split(element,';');
1540 if (((parts.size() == 1) || (parts.size() == 2)) ||
1541 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1543 parseColorString(parts[0],m_bgcolor,false);
1545 if (parts.size() == 2) {
1546 std::string fullscreen = parts[1];
1547 m_bgfullscreen = is_yes(fullscreen);
1551 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1554 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1556 std::vector<std::string> parts = split(element,';');
1558 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1559 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1561 parseColorString(parts[0], m_slotbg_n, false);
1562 parseColorString(parts[1], m_slotbg_h, false);
1564 if (parts.size() >= 3) {
1565 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1566 m_slotborder = true;
1569 if (parts.size() == 5) {
1570 video::SColor tmp_color;
1572 if (parseColorString(parts[3], tmp_color, false))
1573 m_default_tooltip_bgcolor = tmp_color;
1574 if (parseColorString(parts[4], tmp_color, false))
1575 m_default_tooltip_color = tmp_color;
1579 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1582 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1584 std::vector<std::string> parts = split(element,';');
1585 if (parts.size() == 2) {
1586 std::string name = parts[0];
1587 m_tooltips[narrow_to_wide(name.c_str())] = TooltipSpec (parts[1], m_default_tooltip_bgcolor, m_default_tooltip_color);
1589 } else if (parts.size() == 4) {
1590 std::string name = parts[0];
1591 video::SColor tmp_color1, tmp_color2;
1592 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1593 m_tooltips[narrow_to_wide(name.c_str())] = TooltipSpec (parts[1], tmp_color1, tmp_color2);
1597 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1600 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1606 std::vector<std::string> parts = split(data,'[');
1608 if (parts.size() < 2) {
1612 if (parts[0] != "formspec_version") {
1616 if (is_number(parts[1])) {
1617 m_formspec_version = mystoi(parts[1]);
1624 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1629 std::vector<std::string> parts = split(element,'[');
1631 if (parts.size() < 2)
1634 std::string type = trim(parts[0]);
1635 std::string description = trim(parts[1]);
1637 if (type != "size" && type != "invsize")
1640 if (type == "invsize")
1641 log_deprecated("Deprecated formspec element \"invsize\" is used");
1643 parseSize(data, description);
1648 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1654 std::vector<std::string> parts = split(element,'[');
1656 // ugly workaround to keep compatibility
1657 if (parts.size() > 2) {
1658 if (trim(parts[0]) == "image") {
1659 for (unsigned int i=2;i< parts.size(); i++) {
1660 parts[1] += "[" + parts[i];
1666 if (parts.size() < 2) {
1670 std::string type = trim(parts[0]);
1671 std::string description = trim(parts[1]);
1673 if (type == "list") {
1674 parseList(data,description);
1678 if (type == "checkbox") {
1679 parseCheckbox(data,description);
1683 if (type == "image") {
1684 parseImage(data,description);
1688 if (type == "item_image") {
1689 parseItemImage(data,description);
1693 if ((type == "button") || (type == "button_exit")) {
1694 parseButton(data,description,type);
1698 if (type == "background") {
1699 parseBackground(data,description);
1703 if (type == "tableoptions"){
1704 parseTableOptions(data,description);
1708 if (type == "tablecolumns"){
1709 parseTableColumns(data,description);
1713 if (type == "table"){
1714 parseTable(data,description);
1718 if (type == "textlist"){
1719 parseTextList(data,description);
1723 if (type == "dropdown"){
1724 parseDropDown(data,description);
1728 if (type == "pwdfield") {
1729 parsePwdField(data,description);
1733 if ((type == "field") || (type == "textarea")){
1734 parseField(data,description,type);
1738 if (type == "label") {
1739 parseLabel(data,description);
1743 if (type == "vertlabel") {
1744 parseVertLabel(data,description);
1748 if (type == "item_image_button") {
1749 parseItemImageButton(data,description);
1753 if ((type == "image_button") || (type == "image_button_exit")) {
1754 parseImageButton(data,description,type);
1758 if (type == "tabheader") {
1759 parseTabHeader(data,description);
1763 if (type == "box") {
1764 parseBox(data,description);
1768 if (type == "bgcolor") {
1769 parseBackgroundColor(data,description);
1773 if (type == "listcolors") {
1774 parseListColors(data,description);
1778 if (type == "tooltip") {
1779 parseTooltip(data,description);
1783 if (type == "scrollbar") {
1784 parseScrollBar(data, description);
1790 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1796 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1798 /* useless to regenerate without a screensize */
1799 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1806 for (u32 i = 0; i < m_tables.size(); ++i) {
1807 std::wstring tablename = m_tables[i].first.fname;
1808 GUITable *table = m_tables[i].second;
1809 mydata.table_dyndata[tablename] = table->getDynamicData();
1813 gui::IGUIElement *focused_element = Environment->getFocus();
1814 if (focused_element && focused_element->getParent() == this) {
1815 s32 focused_id = focused_element->getID();
1816 if (focused_id > 257) {
1817 for (u32 i=0; i<m_fields.size(); i++) {
1818 if (m_fields[i].fid == focused_id) {
1819 mydata.focused_fieldname =
1830 for (u32 i = 0; i < m_tables.size(); ++i) {
1831 GUITable *table = m_tables[i].second;
1835 mydata.size= v2s32(100,100);
1836 mydata.screensize = screensize;
1838 // Base position of contents of form
1839 mydata.basepos = getBasePos();
1841 /* Convert m_init_draw_spec to m_inventorylists */
1843 m_inventorylists.clear();
1845 m_backgrounds.clear();
1846 m_itemimages.clear();
1848 m_checkboxes.clear();
1849 m_scrollbars.clear();
1854 // Set default values (fits old formspec values)
1855 m_bgcolor = video::SColor(140,0,0,0);
1856 m_bgfullscreen = false;
1858 m_slotbg_n = video::SColor(255,128,128,128);
1859 m_slotbg_h = video::SColor(255,192,192,192);
1861 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1862 m_default_tooltip_color = video::SColor(255,255,255,255);
1864 m_slotbordercolor = video::SColor(200,0,0,0);
1865 m_slotborder = false;
1867 m_clipbackground = false;
1870 assert(m_tooltip_element == NULL);
1871 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1872 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1873 m_tooltip_element->enableOverrideColor(true);
1874 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1875 m_tooltip_element->setDrawBackground(true);
1876 m_tooltip_element->setDrawBorder(true);
1877 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1878 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1879 m_tooltip_element->setWordWrap(false);
1880 //we're not parent so no autograb for this one!
1881 m_tooltip_element->grab();
1884 std::vector<std::string> elements = split(m_formspec_string,']');
1887 /* try to read version from first element only */
1888 if (elements.size() >= 1) {
1889 if ( parseVersionDirect(elements[0]) ) {
1894 /* we need size first in order to calculate image scale */
1895 mydata.explicit_size = false;
1896 for (; i< elements.size(); i++) {
1897 if (!parseSizeDirect(&mydata, elements[i])) {
1902 if (mydata.explicit_size) {
1903 // compute scaling for specified form size
1905 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1906 v2u32 delta = current_screensize - m_lockscreensize;
1908 if (current_screensize.Y > m_lockscreensize.Y)
1913 if (current_screensize.X > m_lockscreensize.X)
1918 offset = v2s32(delta.X,delta.Y);
1920 mydata.screensize = m_lockscreensize;
1922 offset = v2s32(0,0);
1925 double gui_scaling = g_settings->getFloat("gui_scaling");
1926 double screen_dpi = porting::getDisplayDensity() * 96;
1930 // In fixed-size mode, inventory image size
1931 // is 0.53 inch multiplied by the gui_scaling
1932 // config parameter. This magic size is chosen
1933 // to make the main menu (15.5 inventory images
1934 // wide, including border) just fit into the
1935 // default window (800 pixels wide) at 96 DPI
1936 // and default scaling (1.00).
1937 use_imgsize = 0.53 * screen_dpi * gui_scaling;
1939 // In variable-size mode, we prefer to make the
1940 // inventory image size 1/15 of screen height,
1941 // multiplied by the gui_scaling config parameter.
1942 // If the preferred size won't fit the whole
1943 // form on the screen, either horizontally or
1944 // vertically, then we scale it down to fit.
1945 // (The magic numbers in the computation of what
1946 // fits arise from the scaling factors in the
1947 // following stanza, including the form border,
1948 // help text space, and 0.1 inventory slot spare.)
1949 // However, a minimum size is also set, that
1950 // the image size can't be less than 0.3 inch
1951 // multiplied by gui_scaling, even if this means
1952 // the form doesn't fit the screen.
1953 double prefer_imgsize = mydata.screensize.Y / 15 *
1955 double fitx_imgsize = mydata.screensize.X /
1956 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1957 double fity_imgsize = mydata.screensize.Y /
1958 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1959 double screen_dpi = porting::getDisplayDensity() * 96;
1960 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1961 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1962 MYMIN(fitx_imgsize, fity_imgsize)));
1965 // Everything else is scaled in proportion to the
1966 // inventory image size. The inventory slot spacing
1967 // is 5/4 image size horizontally and 15/13 image size
1968 // vertically. The padding around the form (incorporating
1969 // the border of the outer inventory slots) is 3/8
1970 // image size. Font height (baseline to baseline)
1971 // is 2/5 vertical inventory slot spacing, and button
1972 // half-height is 7/8 of font height.
1973 imgsize = v2s32(use_imgsize, use_imgsize);
1974 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
1975 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
1976 double target_font_height = use_imgsize*15.0/13 * 0.4;
1977 m_btn_height = use_imgsize*15.0/13 * 0.35;
1979 m_font = select_font_by_line_height(target_font_height);
1981 mydata.size = v2s32(
1982 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
1983 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
1985 DesiredRect = mydata.rect = core::rect<s32>(
1986 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
1987 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
1988 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
1989 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
1992 // Non-size[] form must consist only of text fields and
1993 // implicit "Proceed" button. Use default font, and
1994 // temporary form size which will be recalculated below.
1995 m_font = glb_fontengine->getFont();
1996 m_btn_height = font_line_height(m_font) * 0.875;
1997 DesiredRect = core::rect<s32>(
1998 mydata.screensize.X/2 - 580/2,
1999 mydata.screensize.Y/2 - 300/2,
2000 mydata.screensize.X/2 + 580/2,
2001 mydata.screensize.Y/2 + 300/2
2004 recalculateAbsolutePosition(false);
2005 mydata.basepos = getBasePos();
2006 m_tooltip_element->setOverrideFont(m_font);
2008 gui::IGUISkin* skin = Environment->getSkin();
2009 assert(skin != NULL);
2010 gui::IGUIFont *old_font = skin->getFont();
2011 skin->setFont(m_font);
2013 for (; i< elements.size(); i++) {
2014 parseElement(&mydata, elements[i]);
2017 // If there are fields without explicit size[], add a "Proceed"
2018 // button and adjust size to fit all the fields.
2019 if (m_fields.size() && !mydata.explicit_size) {
2020 mydata.rect = core::rect<s32>(
2021 mydata.screensize.X/2 - 580/2,
2022 mydata.screensize.Y/2 - 300/2,
2023 mydata.screensize.X/2 + 580/2,
2024 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2026 DesiredRect = mydata.rect;
2027 recalculateAbsolutePosition(false);
2028 mydata.basepos = getBasePos();
2031 v2s32 pos = mydata.basepos;
2032 pos.Y = ((m_fields.size()+2)*60);
2034 v2s32 size = DesiredRect.getSize();
2036 core::rect<s32>(size.X/2-70, pos.Y,
2037 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2038 wchar_t* text = wgettext("Proceed");
2039 Environment->addButton(mydata.rect, this, 257, text);
2045 //set initial focus if parser didn't set it
2046 focused_element = Environment->getFocus();
2047 if (!focused_element
2048 || !isMyChild(focused_element)
2049 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2052 skin->setFont(old_font);
2056 bool GUIFormSpecMenu::getAndroidUIInput()
2058 /* no dialog shown */
2059 if (m_JavaDialogFieldName == L"") {
2064 if (porting::getInputDialogState() == -1) {
2068 std::wstring fieldname = m_JavaDialogFieldName;
2069 m_JavaDialogFieldName = L"";
2071 /* no value abort dialog processing */
2072 if (porting::getInputDialogState() != 0) {
2076 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2077 iter != m_fields.end(); iter++) {
2079 if (iter->fname != fieldname) {
2082 IGUIElement* tochange = getElementFromId(iter->fid);
2084 if (tochange == 0) {
2088 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2092 std::string text = porting::getInputDialogValue();
2094 ((gui::IGUIEditBox*) tochange)->
2095 setText(narrow_to_wide(text).c_str());
2101 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2103 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2105 for(u32 i=0; i<m_inventorylists.size(); i++)
2107 const ListDrawSpec &s = m_inventorylists[i];
2109 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2110 s32 item_i = i + s.start_item_i;
2111 s32 x = (i%s.geom.X) * spacing.X;
2112 s32 y = (i/s.geom.X) * spacing.Y;
2114 core::rect<s32> rect = imgrect + s.pos + p0;
2115 if(rect.isPointInside(p))
2117 return ItemSpec(s.inventoryloc, s.listname, item_i);
2122 return ItemSpec(InventoryLocation(), "", -1);
2125 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
2127 video::IVideoDriver* driver = Environment->getVideoDriver();
2129 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2131 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2132 <<"The inventory location "
2133 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2137 InventoryList *ilist = inv->getList(s.listname);
2139 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2140 <<"The inventory list \""<<s.listname<<"\" @ \""
2141 <<s.inventoryloc.dump()<<"\" doesn't exist"
2146 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2148 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2150 s32 item_i = i + s.start_item_i;
2151 if(item_i >= (s32) ilist->getSize())
2153 s32 x = (i%s.geom.X) * spacing.X;
2154 s32 y = (i/s.geom.X) * spacing.Y;
2156 core::rect<s32> rect = imgrect + s.pos + p;
2159 item = ilist->getItem(item_i);
2161 bool selected = m_selected_item
2162 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2163 && m_selected_item->listname == s.listname
2164 && m_selected_item->i == item_i;
2165 bool hovering = rect.isPointInside(m_pointer);
2170 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2172 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2175 //Draw inv slot borders
2177 s32 x1 = rect.UpperLeftCorner.X;
2178 s32 y1 = rect.UpperLeftCorner.Y;
2179 s32 x2 = rect.LowerRightCorner.X;
2180 s32 y2 = rect.LowerRightCorner.Y;
2182 driver->draw2DRectangle(m_slotbordercolor,
2183 core::rect<s32>(v2s32(x1 - border, y1 - border),
2184 v2s32(x2 + border, y1)), NULL);
2185 driver->draw2DRectangle(m_slotbordercolor,
2186 core::rect<s32>(v2s32(x1 - border, y2),
2187 v2s32(x2 + border, y2 + border)), NULL);
2188 driver->draw2DRectangle(m_slotbordercolor,
2189 core::rect<s32>(v2s32(x1 - border, y1),
2190 v2s32(x1, y2)), NULL);
2191 driver->draw2DRectangle(m_slotbordercolor,
2192 core::rect<s32>(v2s32(x2, y1),
2193 v2s32(x2 + border, y2)), NULL);
2201 item.takeItem(m_selected_amount);
2205 drawItemStack(driver, m_font, item,
2206 rect, &AbsoluteClippingRect, m_gamedef);
2210 std::string tooltip_text = "";
2211 if (hovering && !m_selected_item)
2212 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
2213 if (tooltip_text != "") {
2214 std::vector<std::string> tt_rows = str_split(tooltip_text, '\n');
2215 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2216 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2217 m_tooltip_element->setVisible(true);
2218 this->bringToFront(m_tooltip_element);
2219 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
2220 s32 tooltip_x = m_pointer.X + m_btn_height;
2221 s32 tooltip_y = m_pointer.Y + m_btn_height;
2222 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2223 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2224 m_tooltip_element->setRelativePosition(core::rect<s32>(
2225 core::position2d<s32>(tooltip_x, tooltip_y),
2226 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2232 void GUIFormSpecMenu::drawSelectedItem()
2234 if(!m_selected_item)
2237 video::IVideoDriver* driver = Environment->getVideoDriver();
2239 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2241 InventoryList *list = inv->getList(m_selected_item->listname);
2243 ItemStack stack = list->getItem(m_selected_item->i);
2244 stack.count = m_selected_amount;
2246 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2247 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2248 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef);
2251 void GUIFormSpecMenu::drawMenu()
2254 std::string newform = m_form_src->getForm();
2255 if(newform != m_formspec_string){
2256 m_formspec_string = newform;
2257 regenerateGui(m_screensize_old);
2261 gui::IGUISkin* skin = Environment->getSkin();
2262 assert(skin != NULL);
2263 gui::IGUIFont *old_font = skin->getFont();
2264 skin->setFont(m_font);
2266 updateSelectedItem();
2268 video::IVideoDriver* driver = Environment->getVideoDriver();
2270 v2u32 screenSize = driver->getScreenSize();
2271 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2273 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2275 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2277 m_tooltip_element->setVisible(false);
2282 for(u32 i=0; i<m_backgrounds.size(); i++)
2284 const ImageDrawSpec &spec = m_backgrounds[i];
2285 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2288 // Image size on screen
2289 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2290 // Image rectangle on screen
2291 core::rect<s32> rect = imgrect + spec.pos;
2293 if (m_clipbackground) {
2294 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2295 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2296 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2297 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2298 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2301 const video::SColor color(255,255,255,255);
2302 const video::SColor colors[] = {color,color,color,color};
2303 driver->draw2DImage(texture, rect,
2304 core::rect<s32>(core::position2d<s32>(0,0),
2305 core::dimension2di(texture->getOriginalSize())),
2306 NULL/*&AbsoluteClippingRect*/, colors, true);
2309 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2310 errorstream << "\t" << spec.name << std::endl;
2317 for(u32 i=0; i<m_boxes.size(); i++)
2319 const BoxDrawSpec &spec = m_boxes[i];
2321 irr::video::SColor todraw = spec.color;
2323 todraw.setAlpha(140);
2325 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2326 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2328 driver->draw2DRectangle(todraw, rect, 0);
2333 for(u32 i=0; i<m_images.size(); i++)
2335 const ImageDrawSpec &spec = m_images[i];
2336 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2339 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2340 // Image size on screen
2341 core::rect<s32> imgrect;
2344 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2347 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2349 // Image rectangle on screen
2350 core::rect<s32> rect = imgrect + spec.pos;
2351 const video::SColor color(255,255,255,255);
2352 const video::SColor colors[] = {color,color,color,color};
2353 driver->draw2DImage(texture, rect,
2354 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2355 NULL/*&AbsoluteClippingRect*/, colors, true);
2358 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2359 errorstream << "\t" << spec.name << std::endl;
2366 for(u32 i=0; i<m_itemimages.size(); i++)
2371 const ImageDrawSpec &spec = m_itemimages[i];
2372 IItemDefManager *idef = m_gamedef->idef();
2374 item.deSerialize(spec.name, idef);
2375 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
2376 // Image size on screen
2377 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2378 // Image rectangle on screen
2379 core::rect<s32> rect = imgrect + spec.pos;
2380 const video::SColor color(255,255,255,255);
2381 const video::SColor colors[] = {color,color,color,color};
2382 driver->draw2DImage(texture, rect,
2383 core::rect<s32>(core::position2d<s32>(0,0),
2384 core::dimension2di(texture->getOriginalSize())),
2385 NULL/*&AbsoluteClippingRect*/, colors, true);
2390 Phase 0: Item slot rectangles
2391 Phase 1: Item images; prepare tooltip
2394 for(int phase=start_phase; phase<=1; phase++)
2395 for(u32 i=0; i<m_inventorylists.size(); i++)
2397 drawList(m_inventorylists[i], phase);
2403 gui::IGUIElement::draw();
2405 /* TODO find way to show tooltips on touchscreen */
2406 #ifndef HAVE_TOUCHSCREENGUI
2407 m_pointer = m_device->getCursorControl()->getPosition();
2411 Draw fields/buttons tooltips
2413 gui::IGUIElement *hovered =
2414 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2416 if (hovered != NULL) {
2417 s32 id = hovered->getID();
2421 m_old_tooltip_id = id;
2424 if (id == m_old_tooltip_id) {
2425 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2427 m_hovered_time = getTimeMs();
2428 m_old_tooltip_id = id;
2432 if (id != -1 && delta >= m_tooltip_show_delay) {
2433 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2434 iter != m_fields.end(); iter++) {
2435 if ( (iter->fid == id) && (m_tooltips[iter->fname].tooltip != "") ){
2436 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2437 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2438 m_tooltip_element->setText(narrow_to_wide(m_tooltips[iter->fname].tooltip).c_str());
2439 s32 tooltip_x = m_pointer.X + m_btn_height;
2440 s32 tooltip_y = m_pointer.Y + m_btn_height;
2441 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2442 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2443 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2444 std::vector<std::string> tt_rows = str_split(m_tooltips[iter->fname].tooltip, '\n');
2445 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2446 m_tooltip_element->setRelativePosition(core::rect<s32>(
2447 core::position2d<s32>(tooltip_x, tooltip_y),
2448 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2450 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2451 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2452 m_tooltip_element->setVisible(true);
2453 this->bringToFront(m_tooltip_element);
2461 Draw dragged item stack
2465 skin->setFont(old_font);
2468 void GUIFormSpecMenu::updateSelectedItem()
2470 // If the selected stack has become empty for some reason, deselect it.
2471 // If the selected stack has become inaccessible, deselect it.
2472 // If the selected stack has become smaller, adjust m_selected_amount.
2473 ItemStack selected = verifySelectedItem();
2475 // WARNING: BLACK MAGIC
2476 // See if there is a stack suited for our current guess.
2477 // If such stack does not exist, clear the guess.
2478 if(m_selected_content_guess.name != "" &&
2479 selected.name == m_selected_content_guess.name &&
2480 selected.count == m_selected_content_guess.count){
2481 // Selected item fits the guess. Skip the black magic.
2483 else if(m_selected_content_guess.name != ""){
2485 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2486 const ListDrawSpec &s = m_inventorylists[i];
2487 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2490 InventoryList *list = inv->getList(s.listname);
2493 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2494 u32 item_i = i + s.start_item_i;
2495 if(item_i >= list->getSize())
2497 ItemStack stack = list->getItem(item_i);
2498 if(stack.name == m_selected_content_guess.name &&
2499 stack.count == m_selected_content_guess.count){
2501 infostream<<"Client: Changing selected content guess to "
2502 <<s.inventoryloc.dump()<<" "<<s.listname
2503 <<" "<<item_i<<std::endl;
2504 delete m_selected_item;
2505 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2506 m_selected_amount = stack.count;
2511 infostream<<"Client: Discarding selected content guess: "
2512 <<m_selected_content_guess.getItemString()<<std::endl;
2513 m_selected_content_guess.name = "";
2517 // If craftresult is nonempty and nothing else is selected, select it now.
2518 if(!m_selected_item)
2520 for(u32 i=0; i<m_inventorylists.size(); i++)
2522 const ListDrawSpec &s = m_inventorylists[i];
2523 if(s.listname == "craftpreview")
2525 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2526 InventoryList *list = inv->getList("craftresult");
2527 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2529 m_selected_item = new ItemSpec;
2530 m_selected_item->inventoryloc = s.inventoryloc;
2531 m_selected_item->listname = "craftresult";
2532 m_selected_item->i = 0;
2533 m_selected_amount = 0;
2534 m_selected_dragging = false;
2541 // If craftresult is selected, keep the whole stack selected
2542 if(m_selected_item && m_selected_item->listname == "craftresult")
2544 m_selected_amount = verifySelectedItem().count;
2548 ItemStack GUIFormSpecMenu::verifySelectedItem()
2550 // If the selected stack has become empty for some reason, deselect it.
2551 // If the selected stack has become inaccessible, deselect it.
2552 // If the selected stack has become smaller, adjust m_selected_amount.
2553 // Return the selected stack.
2557 if(m_selected_item->isValid())
2559 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2562 InventoryList *list = inv->getList(m_selected_item->listname);
2563 if(list && (u32) m_selected_item->i < list->getSize())
2565 ItemStack stack = list->getItem(m_selected_item->i);
2566 if(m_selected_amount > stack.count)
2567 m_selected_amount = stack.count;
2574 // selection was not valid
2575 delete m_selected_item;
2576 m_selected_item = NULL;
2577 m_selected_amount = 0;
2578 m_selected_dragging = false;
2583 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2587 std::map<std::string, std::string> fields;
2589 if (quitmode == quit_mode_accept) {
2590 fields["quit"] = "true";
2593 if (quitmode == quit_mode_cancel) {
2594 fields["quit"] = "true";
2595 m_text_dst->gotText(fields);
2599 if (current_keys_pending.key_down) {
2600 fields["key_down"] = "true";
2601 current_keys_pending.key_down = false;
2604 if (current_keys_pending.key_up) {
2605 fields["key_up"] = "true";
2606 current_keys_pending.key_up = false;
2609 if (current_keys_pending.key_enter) {
2610 fields["key_enter"] = "true";
2611 current_keys_pending.key_enter = false;
2614 if (current_keys_pending.key_escape) {
2615 fields["key_escape"] = "true";
2616 current_keys_pending.key_escape = false;
2619 for(unsigned int i=0; i<m_fields.size(); i++) {
2620 const FieldSpec &s = m_fields[i];
2622 std::string name = wide_to_narrow(s.fname);
2623 if(s.ftype == f_Button) {
2624 fields[name] = wide_to_narrow(s.flabel);
2626 else if(s.ftype == f_Table) {
2627 GUITable *table = getTable(s.fname);
2629 fields[name] = table->checkEvent();
2632 else if(s.ftype == f_DropDown) {
2633 // no dynamic cast possible due to some distributions shipped
2634 // without rtti support in irrlicht
2635 IGUIElement * element = getElementFromId(s.fid);
2636 gui::IGUIComboBox *e = NULL;
2637 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2638 e = static_cast<gui::IGUIComboBox*>(element);
2640 s32 selected = e->getSelected();
2641 if (selected >= 0) {
2643 wide_to_narrow(e->getItem(selected));
2646 else if (s.ftype == f_TabHeader) {
2647 // no dynamic cast possible due to some distributions shipped
2648 // without rtti support in irrlicht
2649 IGUIElement * element = getElementFromId(s.fid);
2650 gui::IGUITabControl *e = NULL;
2651 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2652 e = static_cast<gui::IGUITabControl*>(element);
2656 std::stringstream ss;
2657 ss << (e->getActiveTab() +1);
2658 fields[name] = ss.str();
2661 else if (s.ftype == f_CheckBox) {
2662 // no dynamic cast possible due to some distributions shipped
2663 // without rtti support in irrlicht
2664 IGUIElement * element = getElementFromId(s.fid);
2665 gui::IGUICheckBox *e = NULL;
2666 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2667 e = static_cast<gui::IGUICheckBox*>(element);
2672 fields[name] = "true";
2674 fields[name] = "false";
2677 else if (s.ftype == f_ScrollBar) {
2678 // no dynamic cast possible due to some distributions shipped
2679 // without rtti support in irrlicht
2680 IGUIElement * element = getElementFromId(s.fid);
2681 gui::IGUIScrollBar *e = NULL;
2682 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2683 e = static_cast<gui::IGUIScrollBar*>(element);
2687 std::stringstream os;
2689 if (s.fdefault == L"Changed")
2690 fields[name] = "CHG:" + os.str();
2692 fields[name] = "VAL:" + os.str();
2697 IGUIElement* e = getElementFromId(s.fid);
2699 fields[name] = wide_to_narrow(e->getText());
2705 m_text_dst->gotText(fields);
2709 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2711 while(tocheck != NULL) {
2712 if (tocheck == parent) {
2715 tocheck = tocheck->getParent();
2720 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2722 // The IGUITabControl renders visually using the skin's selected
2723 // font, which we override for the duration of form drawing,
2724 // but computes tab hotspots based on how it would have rendered
2725 // using the font that is selected at the time of button release.
2726 // To make these two consistent, temporarily override the skin's
2727 // font while the IGUITabControl is processing the event.
2728 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2729 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2730 s32 x = event.MouseInput.X;
2731 s32 y = event.MouseInput.Y;
2732 gui::IGUIElement *hovered =
2733 Environment->getRootGUIElement()->getElementFromPoint(
2734 core::position2d<s32>(x, y));
2735 if (hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2736 gui::IGUISkin* skin = Environment->getSkin();
2737 assert(skin != NULL);
2738 gui::IGUIFont *old_font = skin->getFont();
2739 skin->setFont(m_font);
2740 bool retval = hovered->OnEvent(event);
2741 skin->setFont(old_font);
2746 // Fix Esc/Return key being eaten by checkboxen and tables
2747 if(event.EventType==EET_KEY_INPUT_EVENT) {
2748 KeyPress kp(event.KeyInput);
2749 if (kp == EscapeKey || kp == CancelKey
2750 || kp == getKeySetting("keymap_inventory")
2751 || event.KeyInput.Key==KEY_RETURN) {
2752 gui::IGUIElement *focused = Environment->getFocus();
2753 if (focused && isMyChild(focused) &&
2754 (focused->getType() == gui::EGUIET_LIST_BOX ||
2755 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2761 // Mouse wheel events: send to hovered element instead of focused
2762 if(event.EventType==EET_MOUSE_INPUT_EVENT
2763 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2764 s32 x = event.MouseInput.X;
2765 s32 y = event.MouseInput.Y;
2766 gui::IGUIElement *hovered =
2767 Environment->getRootGUIElement()->getElementFromPoint(
2768 core::position2d<s32>(x, y));
2769 if (hovered && isMyChild(hovered)) {
2770 hovered->OnEvent(event);
2775 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2776 s32 x = event.MouseInput.X;
2777 s32 y = event.MouseInput.Y;
2778 gui::IGUIElement *hovered =
2779 Environment->getRootGUIElement()->getElementFromPoint(
2780 core::position2d<s32>(x, y));
2781 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2782 m_old_tooltip_id = -1;
2785 if (!isChild(hovered,this)) {
2786 if (DoubleClickDetection(event)) {
2793 // display software keyboard when clicking edit boxes
2794 if (event.EventType == EET_MOUSE_INPUT_EVENT
2795 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2796 gui::IGUIElement *hovered =
2797 Environment->getRootGUIElement()->getElementFromPoint(
2798 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2799 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2800 bool retval = hovered->OnEvent(event);
2802 Environment->setFocus(hovered);
2804 m_JavaDialogFieldName = getNameByID(hovered->getID());
2805 std::string message = gettext("Enter ");
2806 std::string label = wide_to_narrow(getLabelByID(hovered->getID()));
2810 message += gettext(label) + ":";
2812 /* single line text input */
2815 /* multi line text input */
2816 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2820 /* passwords are always single line */
2821 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2825 porting::showInputDialog(gettext("ok"), "",
2826 wide_to_narrow(((gui::IGUIEditBox*) hovered)->getText()),
2832 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2835 memset(&translated, 0, sizeof(SEvent));
2836 translated.EventType = EET_MOUSE_INPUT_EVENT;
2837 gui::IGUIElement* root = Environment->getRootGUIElement();
2841 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2845 gui::IGUIElement* hovered = root->getElementFromPoint(
2846 core::position2d<s32>(
2848 event.TouchInput.Y));
2850 translated.MouseInput.X = event.TouchInput.X;
2851 translated.MouseInput.Y = event.TouchInput.Y;
2852 translated.MouseInput.Control = false;
2854 bool dont_send_event = false;
2856 if (event.TouchInput.touchedCount == 1) {
2857 switch (event.TouchInput.Event) {
2858 case ETIE_PRESSED_DOWN:
2859 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2860 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2861 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2862 m_down_pos = m_pointer;
2865 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2866 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2867 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2870 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2871 translated.MouseInput.ButtonStates = 0;
2872 hovered = root->getElementFromPoint(m_down_pos);
2873 /* we don't have a valid pointer element use last
2874 * known pointer pos */
2875 translated.MouseInput.X = m_pointer.X;
2876 translated.MouseInput.Y = m_pointer.Y;
2878 /* reset down pos */
2879 m_down_pos = v2s32(0,0);
2882 dont_send_event = true;
2883 //this is not supposed to happen
2885 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2886 << event.TouchInput.Event << std::endl;
2888 } else if ( (event.TouchInput.touchedCount == 2) &&
2889 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2890 hovered = root->getElementFromPoint(m_down_pos);
2892 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2893 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2894 translated.MouseInput.X = m_pointer.X;
2895 translated.MouseInput.Y = m_pointer.Y;
2898 hovered->OnEvent(translated);
2901 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
2902 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2906 hovered->OnEvent(translated);
2908 dont_send_event = true;
2910 /* ignore unhandled 2 touch events ... accidental moving for example */
2911 else if (event.TouchInput.touchedCount == 2) {
2912 dont_send_event = true;
2914 else if (event.TouchInput.touchedCount > 2) {
2916 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
2917 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
2920 if (dont_send_event) {
2924 /* check if translated event needs to be preprocessed again */
2925 if (preprocessEvent(translated)) {
2930 bool retval = hovered->OnEvent(translated);
2932 if (event.TouchInput.Event == ETIE_LEFT_UP) {
2934 m_pointer = v2s32(0,0);
2945 /******************************************************************************/
2946 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
2948 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2949 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
2950 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
2952 m_doubleclickdetect[1].pos = m_pointer;
2953 m_doubleclickdetect[1].time = getTimeMs();
2955 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2956 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
2961 double squaredistance =
2962 m_doubleclickdetect[0].pos
2963 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
2965 if (squaredistance > (30*30)) {
2969 SEvent* translated = new SEvent();
2970 assert(translated != 0);
2971 //translate doubleclick to escape
2972 memset(translated, 0, sizeof(SEvent));
2973 translated->EventType = irr::EET_KEY_INPUT_EVENT;
2974 translated->KeyInput.Key = KEY_ESCAPE;
2975 translated->KeyInput.Control = false;
2976 translated->KeyInput.Shift = false;
2977 translated->KeyInput.PressedDown = true;
2978 translated->KeyInput.Char = 0;
2979 OnEvent(*translated);
2981 // no need to send the key up event as we're already deleted
2982 // and no one else did notice this event
2989 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2991 if(event.EventType==EET_KEY_INPUT_EVENT) {
2992 KeyPress kp(event.KeyInput);
2993 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
2994 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
2997 acceptInput(quit_mode_cancel);
3000 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
3003 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3004 (kp == getKeySetting("keymap_screenshot"))) {
3005 m_client->makeScreenshot(m_device);
3007 if (event.KeyInput.PressedDown &&
3008 (event.KeyInput.Key==KEY_RETURN ||
3009 event.KeyInput.Key==KEY_UP ||
3010 event.KeyInput.Key==KEY_DOWN)
3012 switch (event.KeyInput.Key) {
3014 current_keys_pending.key_enter = true;
3017 current_keys_pending.key_up = true;
3020 current_keys_pending.key_down = true;
3024 //can't happen at all!
3025 assert("reached a source line that can't ever been reached" == 0);
3028 if (current_keys_pending.key_enter && m_allowclose) {
3029 acceptInput(quit_mode_accept);
3039 /* Mouse event other than movement, or crossing the border of inventory
3040 field while holding right mouse button
3042 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3043 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3044 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3045 event.MouseInput.isRightPressed() &&
3046 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3048 // Get selected item and hovered/clicked item (s)
3050 m_old_tooltip_id = -1;
3051 updateSelectedItem();
3052 ItemSpec s = getItemAtPos(m_pointer);
3054 Inventory *inv_selected = NULL;
3055 Inventory *inv_s = NULL;
3057 if(m_selected_item) {
3058 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3059 assert(inv_selected);
3060 assert(inv_selected->getList(m_selected_item->listname) != NULL);
3067 inv_s = m_invmgr->getInventory(s.inventoryloc);
3070 errorstream<<"InventoryMenu: The selected inventory location "
3071 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
3073 s.i = -1; // make it invalid again
3077 InventoryList *list = inv_s->getList(s.listname);
3079 verbosestream<<"InventoryMenu: The selected inventory list \""
3080 <<s.listname<<"\" does not exist"<<std::endl;
3081 s.i = -1; // make it invalid again
3085 if((u32)s.i >= list->getSize()) {
3086 infostream<<"InventoryMenu: The selected inventory list \""
3087 <<s.listname<<"\" is too small (i="<<s.i<<", size="
3088 <<list->getSize()<<")"<<std::endl;
3089 s.i = -1; // make it invalid again
3093 s_count = list->getItem(s.i).count;
3096 bool identical = (m_selected_item != NULL) && s.isValid() &&
3097 (inv_selected == inv_s) &&
3098 (m_selected_item->listname == s.listname) &&
3099 (m_selected_item->i == s.i);
3101 // buttons: 0 = left, 1 = right, 2 = middle
3102 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3105 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3106 { button = 0; updown = 0; }
3107 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3108 { button = 1; updown = 0; }
3109 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3110 { button = 2; updown = 0; }
3111 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3112 { button = 0; updown = 1; }
3113 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3114 { button = 1; updown = 1; }
3115 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3116 { button = 2; updown = 1; }
3117 else if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
3120 // Set this number to a positive value to generate a move action
3121 // from m_selected_item to s.
3122 u32 move_amount = 0;
3124 // Set this number to a positive value to generate a drop action
3125 // from m_selected_item.
3126 u32 drop_amount = 0;
3128 // Set this number to a positive value to generate a craft action at s.
3129 u32 craft_amount = 0;
3132 // Some mouse button has been pressed
3134 //infostream<<"Mouse button "<<button<<" pressed at p=("
3135 // <<p.X<<","<<p.Y<<")"<<std::endl;
3137 m_selected_dragging = false;
3139 if(s.isValid() && s.listname == "craftpreview") {
3140 // Craft preview has been clicked: craft
3141 craft_amount = (button == 2 ? 10 : 1);
3143 else if(m_selected_item == NULL) {
3145 // Non-empty stack has been clicked: select it
3146 m_selected_item = new ItemSpec(s);
3148 if(button == 1) // right
3149 m_selected_amount = (s_count + 1) / 2;
3150 else if(button == 2) // middle
3151 m_selected_amount = MYMIN(s_count, 10);
3153 m_selected_amount = s_count;
3155 m_selected_dragging = true;
3158 else { // m_selected_item != NULL
3159 assert(m_selected_amount >= 1);
3162 // Clicked a slot: move
3163 if(button == 1) // right
3165 else if(button == 2) // middle
3166 move_amount = MYMIN(m_selected_amount, 10);
3168 move_amount = m_selected_amount;
3171 if(move_amount >= m_selected_amount)
3172 m_selected_amount = 0;
3174 m_selected_amount -= move_amount;
3178 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3179 // Clicked outside of the window: drop
3180 if(button == 1) // right
3182 else if(button == 2) // middle
3183 drop_amount = MYMIN(m_selected_amount, 10);
3185 drop_amount = m_selected_amount;
3189 else if(updown == 1) {
3190 // Some mouse button has been released
3192 //infostream<<"Mouse button "<<button<<" released at p=("
3193 // <<p.X<<","<<p.Y<<")"<<std::endl;
3195 if(m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3197 // Dragged to different slot: move all selected
3198 move_amount = m_selected_amount;
3201 else if(m_selected_item != NULL && m_selected_dragging &&
3202 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3203 // Dragged outside of window: drop all selected
3204 drop_amount = m_selected_amount;
3207 m_selected_dragging = false;
3209 else if(updown == -1) {
3210 // Mouse has been moved and rmb is down and mouse pointer just
3211 // entered a new inventory field (checked in the entry-if, this
3212 // is the only action here that is generated by mouse movement)
3213 if(m_selected_item != NULL && s.isValid()){
3215 // TODO: middle mouse to move 10 items might be handy
3220 // Possibly send inventory action to server
3223 // Send IACTION_MOVE
3225 assert(m_selected_item && m_selected_item->isValid());
3226 assert(s.isValid());
3228 assert(inv_selected && inv_s);
3229 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3230 InventoryList *list_to = inv_s->getList(s.listname);
3231 assert(list_from && list_to);
3232 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3233 ItemStack stack_to = list_to->getItem(s.i);
3235 // Check how many items can be moved
3236 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3237 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3238 // If source stack cannot be added to destination stack at all,
3240 if ((leftover.count == stack_from.count) &&
3241 (leftover.name == stack_from.name)) {
3242 m_selected_amount = stack_to.count;
3243 // In case the server doesn't directly swap them but instead
3244 // moves stack_to somewhere else, set this
3245 m_selected_content_guess = stack_to;
3246 m_selected_content_guess_inventory = s.inventoryloc;
3248 // Source stack goes fully into destination stack
3249 else if(leftover.empty()) {
3250 m_selected_amount -= move_amount;
3251 m_selected_content_guess = ItemStack(); // Clear
3253 // Source stack goes partly into destination stack
3255 move_amount -= leftover.count;
3256 m_selected_amount -= move_amount;
3257 m_selected_content_guess = ItemStack(); // Clear
3260 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
3261 IMoveAction *a = new IMoveAction();
3262 a->count = move_amount;
3263 a->from_inv = m_selected_item->inventoryloc;
3264 a->from_list = m_selected_item->listname;
3265 a->from_i = m_selected_item->i;
3266 a->to_inv = s.inventoryloc;
3267 a->to_list = s.listname;
3269 m_invmgr->inventoryAction(a);
3271 else if(drop_amount > 0) {
3272 m_selected_content_guess = ItemStack(); // Clear
3274 // Send IACTION_DROP
3276 assert(m_selected_item && m_selected_item->isValid());
3277 assert(inv_selected);
3278 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3280 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3282 // Check how many items can be dropped
3283 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3284 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3285 m_selected_amount -= drop_amount;
3287 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
3288 IDropAction *a = new IDropAction();
3289 a->count = drop_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 m_invmgr->inventoryAction(a);
3295 else if(craft_amount > 0) {
3296 m_selected_content_guess = ItemStack(); // Clear
3298 // Send IACTION_CRAFT
3300 assert(s.isValid());
3303 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
3304 ICraftAction *a = new ICraftAction();
3305 a->count = craft_amount;
3306 a->craft_inv = s.inventoryloc;
3307 m_invmgr->inventoryAction(a);
3310 // If m_selected_amount has been decreased to zero, deselect
3311 if(m_selected_amount == 0) {
3312 delete m_selected_item;
3313 m_selected_item = NULL;
3314 m_selected_amount = 0;
3315 m_selected_dragging = false;
3316 m_selected_content_guess = ItemStack();
3318 m_old_pointer = m_pointer;
3320 if(event.EventType==EET_GUI_EVENT) {
3322 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
3324 // find the element that was clicked
3325 for(unsigned int i=0; i<m_fields.size(); i++) {
3326 FieldSpec &s = m_fields[i];
3327 if ((s.ftype == f_TabHeader) &&
3328 (s.fid == event.GUIEvent.Caller->getID())) {
3336 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
3338 if(!canTakeFocus(event.GUIEvent.Element)) {
3339 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3341 // Returning true disables focus change
3345 if((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3346 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3347 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3348 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3349 unsigned int btn_id = event.GUIEvent.Caller->getID();
3351 if (btn_id == 257) {
3353 acceptInput(quit_mode_accept);
3357 m_text_dst->gotText(narrow_to_wide("ExitButton"));
3359 // quitMenu deallocates menu
3363 // find the element that was clicked
3364 for(u32 i=0; i<m_fields.size(); i++) {
3365 FieldSpec &s = m_fields[i];
3366 // if its a button, set the send field so
3367 // lua knows which button was pressed
3368 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3369 (s.fid == event.GUIEvent.Caller->getID())) {
3373 acceptInput(quit_mode_accept);
3376 m_text_dst->gotText(narrow_to_wide("ExitButton"));
3380 acceptInput(quit_mode_no);
3385 else if ((s.ftype == f_DropDown) &&
3386 (s.fid == event.GUIEvent.Caller->getID())) {
3387 // only send the changed dropdown
3388 for(u32 i=0; i<m_fields.size(); i++) {
3389 FieldSpec &s2 = m_fields[i];
3390 if (s2.ftype == f_DropDown) {
3395 acceptInput(quit_mode_no);
3397 // revert configuration to make sure dropdowns are sent on
3398 // regular button click
3399 for(u32 i=0; i<m_fields.size(); i++) {
3400 FieldSpec &s2 = m_fields[i];
3401 if (s2.ftype == f_DropDown) {
3407 else if ((s.ftype == f_ScrollBar) &&
3408 (s.fid == event.GUIEvent.Caller->getID()))
3410 s.fdefault = L"Changed";
3411 acceptInput(quit_mode_no);
3417 if(event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3418 if(event.GUIEvent.Caller->getID() > 257) {
3421 acceptInput(quit_mode_accept);
3424 current_keys_pending.key_enter = true;
3427 // quitMenu deallocates menu
3432 if(event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3433 int current_id = event.GUIEvent.Caller->getID();
3434 if(current_id > 257) {
3435 // find the element that was clicked
3436 for(u32 i=0; i<m_fields.size(); i++) {
3437 FieldSpec &s = m_fields[i];
3438 // if it's a table, set the send field
3439 // so lua knows which table was changed
3440 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3451 return Parent ? Parent->OnEvent(event) : false;
3455 * get name of element by element id
3456 * @param id of element
3457 * @return name string or empty string
3459 std::wstring GUIFormSpecMenu::getNameByID(s32 id)
3461 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3462 iter != m_fields.end(); iter++) {
3463 if (iter->fid == id) {
3471 * get label of element by id
3472 * @param id of element
3473 * @return label string or empty string
3475 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3477 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3478 iter != m_fields.end(); iter++) {
3479 if (iter->fid == id) {
3480 return iter->flabel;