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 return g_fontengine->getFont();
81 /* I have no idea what this is trying to achieve, but scaling the font according
82 * to the size of a formspec/dialog does not seem to be a standard (G)UI
83 * design and AFAIK no existing nor proposed GUI does this. Besides that it:
84 * a) breaks most (current) formspec layouts
85 * b) font sizes change depending on the size of the formspec/dialog (see above)
86 * meaning that there is no UI consistency
87 * c) the chosen fonts are, in general, probably too large
94 // We don't get to directly select a font according to its
95 // baseline-to-baseline height. Rather, we select by em size.
96 // The ratio between these varies between fonts. The font
97 // engine also takes its size parameter not specified in pixels,
98 // as we want, but scaled by display density and gui_scaling,
99 // so invert that scaling here. Use a binary search among
100 // requested sizes to find the right font. Our starting bounds
101 // are an em height of 1 (being careful not to request size 0,
102 // which crashes the freetype system) and an em height of the
103 // target baseline-to-baseline height.
104 unsigned int loreq = ceil(1 / porting::getDisplayDensity()
105 / g_settings->getFloat("gui_scaling"));
106 unsigned int hireq = ceil(target_line_height
107 / porting::getDisplayDensity()
108 / g_settings->getFloat("gui_scaling"));
109 unsigned int lohgt = font_line_height(g_fontengine->getFont(loreq));
110 unsigned int hihgt = font_line_height(g_fontengine->getFont(hireq));
111 while(hireq - loreq > 1 && lohgt != hihgt) {
112 unsigned int nureq = (loreq + hireq) >> 1;
113 unsigned int nuhgt = font_line_height(g_fontengine->getFont(nureq));
114 if(nuhgt < target_line_height) {
122 return g_fontengine->getFont(target_line_height - lohgt < hihgt - target_line_height ? loreq : hireq);
126 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
127 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
128 InventoryManager *invmgr, IGameDef *gamedef,
129 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
131 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
137 m_selected_item(NULL),
138 m_selected_amount(0),
139 m_selected_dragging(false),
140 m_tooltip_element(NULL),
142 m_old_tooltip_id(-1),
143 m_rmouse_auto_place(false),
148 m_formspec_version(0),
151 ,m_JavaDialogFieldName(L"")
154 current_keys_pending.key_down = false;
155 current_keys_pending.key_up = false;
156 current_keys_pending.key_enter = false;
157 current_keys_pending.key_escape = false;
159 m_doubleclickdetect[0].time = 0;
160 m_doubleclickdetect[1].time = 0;
162 m_doubleclickdetect[0].pos = v2s32(0, 0);
163 m_doubleclickdetect[1].pos = v2s32(0, 0);
165 m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
168 GUIFormSpecMenu::~GUIFormSpecMenu()
172 for (u32 i = 0; i < m_tables.size(); ++i) {
173 GUITable *table = m_tables[i].second;
177 delete m_selected_item;
179 if (m_form_src != NULL) {
182 if (m_text_dst != NULL) {
187 void GUIFormSpecMenu::removeChildren()
189 const core::list<gui::IGUIElement*> &children = getChildren();
191 while(!children.empty()) {
192 (*children.getLast())->remove();
195 if(m_tooltip_element) {
196 m_tooltip_element->remove();
197 m_tooltip_element->drop();
198 m_tooltip_element = NULL;
203 void GUIFormSpecMenu::setInitialFocus()
205 // Set initial focus according to following order of precedence:
206 // 1. first empty editbox
210 // 5. first focusable (not statictext, not tabheader)
211 // 6. first child element
213 core::list<gui::IGUIElement*> children = getChildren();
215 // in case "children" contains any NULL elements, remove them
216 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
217 it != children.end();) {
221 it = children.erase(it);
224 // 1. first empty editbox
225 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
226 it != children.end(); ++it) {
227 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
228 && (*it)->getText()[0] == 0) {
229 Environment->setFocus(*it);
235 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
236 it != children.end(); ++it) {
237 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
238 Environment->setFocus(*it);
244 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
245 it != children.end(); ++it) {
246 if ((*it)->getTypeName() == std::string("GUITable")) {
247 Environment->setFocus(*it);
253 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
254 it != children.end(); --it) {
255 if ((*it)->getType() == gui::EGUIET_BUTTON) {
256 Environment->setFocus(*it);
261 // 5. first focusable (not statictext, not tabheader)
262 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
263 it != children.end(); ++it) {
264 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
265 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
266 Environment->setFocus(*it);
271 // 6. first child element
272 if (children.empty())
273 Environment->setFocus(this);
275 Environment->setFocus(*(children.begin()));
278 GUITable* GUIFormSpecMenu::getTable(std::wstring tablename)
280 for (u32 i = 0; i < m_tables.size(); ++i) {
281 if (tablename == m_tables[i].first.fname)
282 return m_tables[i].second;
287 std::vector<std::string> split(const std::string &s, char delim) {
288 std::vector<std::string> tokens;
290 std::string current = "";
291 bool last_was_escape = false;
292 for(unsigned int i=0; i < s.size(); i++) {
293 if (last_was_escape) {
295 current += s.c_str()[i];
296 last_was_escape = false;
299 if (s.c_str()[i] == delim) {
300 tokens.push_back(current);
302 last_was_escape = false;
304 else if (s.c_str()[i] == '\\'){
305 last_was_escape = true;
308 current += s.c_str()[i];
309 last_was_escape = false;
314 tokens.push_back(current);
319 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
321 std::vector<std::string> parts = split(element,',');
323 if (((parts.size() == 2) || parts.size() == 3) ||
324 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
326 if (parts[1].find(';') != std::string::npos)
327 parts[1] = parts[1].substr(0,parts[1].find(';'));
329 data->invsize.X = MYMAX(0, stof(parts[0]));
330 data->invsize.Y = MYMAX(0, stof(parts[1]));
333 if (parts.size() == 3) {
334 if (parts[2] == "true") {
335 lockSize(true,v2u32(800,600));
339 data->explicit_size = true;
342 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
345 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
347 if (m_gamedef == 0) {
348 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
352 std::vector<std::string> parts = split(element,';');
354 if (((parts.size() == 4) || (parts.size() == 5)) ||
355 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
357 std::string location = parts[0];
358 std::string listname = parts[1];
359 std::vector<std::string> v_pos = split(parts[2],',');
360 std::vector<std::string> v_geom = split(parts[3],',');
361 std::string startindex = "";
362 if (parts.size() == 5)
363 startindex = parts[4];
365 MY_CHECKPOS("list",2);
366 MY_CHECKGEOM("list",3);
368 InventoryLocation loc;
370 if(location == "context" || location == "current_name")
371 loc = m_current_inventory_location;
373 loc.deSerialize(location);
375 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
376 pos.X += stof(v_pos[0]) * (float)spacing.X;
377 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
380 geom.X = stoi(v_geom[0]);
381 geom.Y = stoi(v_geom[1]);
385 start_i = stoi(startindex);
387 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
388 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
392 if(!data->explicit_size)
393 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
394 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
397 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
400 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
402 std::vector<std::string> parts = split(element,';');
404 if (((parts.size() >= 3) || (parts.size() <= 4)) ||
405 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
407 std::vector<std::string> v_pos = split(parts[0],',');
408 std::string name = parts[1];
409 std::string label = parts[2];
410 std::string selected = "";
412 if (parts.size() >= 4)
415 MY_CHECKPOS("checkbox",0);
418 pos.X += stof(v_pos[0]) * (float) spacing.X;
419 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
421 bool fselected = false;
423 if (selected == "true")
426 std::wstring wlabel = narrow_to_wide(label.c_str());
428 core::rect<s32> rect = core::rect<s32>(
429 pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
430 pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
431 pos.Y + ((imgsize.Y/2) + m_btn_height));
434 narrow_to_wide(name.c_str()),
435 wlabel, //Needed for displaying text on MSVC
440 spec.ftype = f_CheckBox;
442 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
443 spec.fid, spec.flabel.c_str());
445 if (spec.fname == data->focused_fieldname) {
446 Environment->setFocus(e);
449 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
450 m_fields.push_back(spec);
453 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
456 void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element)
458 std::vector<std::string> parts = split(element,';');
460 if (parts.size() >= 5) {
461 std::vector<std::string> v_pos = split(parts[0],',');
462 std::vector<std::string> v_dim = split(parts[1],',');
463 std::string name = parts[2];
464 std::string value = parts[4];
466 MY_CHECKPOS("scrollbar",0);
469 pos.X += stof(v_pos[0]) * (float) spacing.X;
470 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
472 if (v_dim.size() != 2) {
473 errorstream<< "Invalid size for element " << "scrollbar"
474 << "specified: \"" << parts[1] << "\"" << std::endl;
479 dim.X = stof(v_dim[0]) * (float) spacing.X;
480 dim.Y = stof(v_dim[1]) * (float) spacing.Y;
482 core::rect<s32> rect =
483 core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
486 narrow_to_wide(name.c_str()),
492 bool is_horizontal = true;
494 if (parts[2] == "vertical")
495 is_horizontal = false;
497 spec.ftype = f_ScrollBar;
499 gui::IGUIScrollBar* e =
500 Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
504 e->setPos(stoi(parts[4]));
506 e->setLargeStep(100);
508 m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
509 m_fields.push_back(spec);
512 errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
515 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
517 std::vector<std::string> parts = split(element,';');
519 if ((parts.size() == 3) ||
520 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
522 std::vector<std::string> v_pos = split(parts[0],',');
523 std::vector<std::string> v_geom = split(parts[1],',');
524 std::string name = unescape_string(parts[2]);
526 MY_CHECKPOS("image",0);
527 MY_CHECKGEOM("image",1);
529 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
530 pos.X += stof(v_pos[0]) * (float) spacing.X;
531 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
534 geom.X = stof(v_geom[0]) * (float)imgsize.X;
535 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
537 if(!data->explicit_size)
538 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
539 m_images.push_back(ImageDrawSpec(name, pos, geom));
543 if (parts.size() == 2) {
544 std::vector<std::string> v_pos = split(parts[0],',');
545 std::string name = unescape_string(parts[1]);
547 MY_CHECKPOS("image",0);
549 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
550 pos.X += stof(v_pos[0]) * (float) spacing.X;
551 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
553 if(!data->explicit_size)
554 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
555 m_images.push_back(ImageDrawSpec(name, pos));
558 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
561 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
563 std::vector<std::string> parts = split(element,';');
565 if ((parts.size() == 3) ||
566 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
568 std::vector<std::string> v_pos = split(parts[0],',');
569 std::vector<std::string> v_geom = split(parts[1],',');
570 std::string name = parts[2];
572 MY_CHECKPOS("itemimage",0);
573 MY_CHECKGEOM("itemimage",1);
575 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
576 pos.X += stof(v_pos[0]) * (float) spacing.X;
577 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
580 geom.X = stof(v_geom[0]) * (float)imgsize.X;
581 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
583 if(!data->explicit_size)
584 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
585 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
588 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
591 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
594 std::vector<std::string> parts = split(element,';');
596 if ((parts.size() == 4) ||
597 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
599 std::vector<std::string> v_pos = split(parts[0],',');
600 std::vector<std::string> v_geom = split(parts[1],',');
601 std::string name = parts[2];
602 std::string label = parts[3];
604 MY_CHECKPOS("button",0);
605 MY_CHECKGEOM("button",1);
608 pos.X += stof(v_pos[0]) * (float)spacing.X;
609 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
612 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
613 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
615 core::rect<s32> rect =
616 core::rect<s32>(pos.X, pos.Y - m_btn_height,
617 pos.X + geom.X, pos.Y + m_btn_height);
619 if(!data->explicit_size)
620 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
622 label = unescape_string(label);
624 std::wstring wlabel = narrow_to_wide(label.c_str());
627 narrow_to_wide(name.c_str()),
632 spec.ftype = f_Button;
633 if(type == "button_exit")
635 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
636 spec.flabel.c_str());
638 if (spec.fname == data->focused_fieldname) {
639 Environment->setFocus(e);
642 m_fields.push_back(spec);
645 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
648 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
650 std::vector<std::string> parts = split(element,';');
652 if (((parts.size() == 3) || (parts.size() == 4)) ||
653 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
655 std::vector<std::string> v_pos = split(parts[0],',');
656 std::vector<std::string> v_geom = split(parts[1],',');
657 std::string name = unescape_string(parts[2]);
659 MY_CHECKPOS("background",0);
660 MY_CHECKGEOM("background",1);
662 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
663 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
664 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
667 geom.X = stof(v_geom[0]) * (float)spacing.X;
668 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
670 if (parts.size() == 4) {
671 m_clipbackground = is_yes(parts[3]);
672 if (m_clipbackground) {
673 pos.X = stoi(v_pos[0]); //acts as offset
674 pos.Y = stoi(v_pos[1]); //acts as offset
678 if(!data->explicit_size)
679 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
680 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
683 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
686 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
688 std::vector<std::string> parts = split(element,';');
690 data->table_options.clear();
691 for (size_t i = 0; i < parts.size(); ++i) {
692 // Parse table option
693 std::string opt = unescape_string(parts[i]);
694 data->table_options.push_back(GUITable::splitOption(opt));
698 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
700 std::vector<std::string> parts = split(element,';');
702 data->table_columns.clear();
703 for (size_t i = 0; i < parts.size(); ++i) {
704 std::vector<std::string> col_parts = split(parts[i],',');
705 GUITable::TableColumn column;
707 if (!col_parts.empty())
708 column.type = col_parts[0];
709 // Parse column options
710 for (size_t j = 1; j < col_parts.size(); ++j) {
711 std::string opt = unescape_string(col_parts[j]);
712 column.options.push_back(GUITable::splitOption(opt));
714 data->table_columns.push_back(column);
718 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
720 std::vector<std::string> parts = split(element,';');
722 if (((parts.size() == 4) || (parts.size() == 5)) ||
723 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
725 std::vector<std::string> v_pos = split(parts[0],',');
726 std::vector<std::string> v_geom = split(parts[1],',');
727 std::string name = parts[2];
728 std::vector<std::string> items = split(parts[3],',');
729 std::string str_initial_selection = "";
730 std::string str_transparent = "false";
732 if (parts.size() >= 5)
733 str_initial_selection = parts[4];
735 MY_CHECKPOS("table",0);
736 MY_CHECKGEOM("table",1);
739 pos.X += stof(v_pos[0]) * (float)spacing.X;
740 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
743 geom.X = stof(v_geom[0]) * (float)spacing.X;
744 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
747 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
749 std::wstring fname_w = narrow_to_wide(name.c_str());
758 spec.ftype = f_Table;
760 for (unsigned int i = 0; i < items.size(); ++i) {
761 items[i] = unescape_string(items[i]);
764 //now really show table
765 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
768 if (spec.fname == data->focused_fieldname) {
769 Environment->setFocus(e);
772 e->setTable(data->table_options, data->table_columns, items);
774 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
775 e->setDynamicData(data->table_dyndata[fname_w]);
778 if ((str_initial_selection != "") &&
779 (str_initial_selection != "0"))
780 e->setSelected(stoi(str_initial_selection.c_str()));
782 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
783 m_fields.push_back(spec);
786 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
789 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
791 std::vector<std::string> parts = split(element,';');
793 if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
794 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
796 std::vector<std::string> v_pos = split(parts[0],',');
797 std::vector<std::string> v_geom = split(parts[1],',');
798 std::string name = parts[2];
799 std::vector<std::string> items = split(parts[3],',');
800 std::string str_initial_selection = "";
801 std::string str_transparent = "false";
803 if (parts.size() >= 5)
804 str_initial_selection = parts[4];
806 if (parts.size() >= 6)
807 str_transparent = parts[5];
809 MY_CHECKPOS("textlist",0);
810 MY_CHECKGEOM("textlist",1);
813 pos.X += stof(v_pos[0]) * (float)spacing.X;
814 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
817 geom.X = stof(v_geom[0]) * (float)spacing.X;
818 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
821 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
823 std::wstring fname_w = narrow_to_wide(name.c_str());
832 spec.ftype = f_Table;
834 for (unsigned int i = 0; i < items.size(); ++i) {
835 items[i] = unescape_string(items[i]);
838 //now really show list
839 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
842 if (spec.fname == data->focused_fieldname) {
843 Environment->setFocus(e);
846 e->setTextList(items, is_yes(str_transparent));
848 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
849 e->setDynamicData(data->table_dyndata[fname_w]);
852 if ((str_initial_selection != "") &&
853 (str_initial_selection != "0"))
854 e->setSelected(stoi(str_initial_selection.c_str()));
856 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
857 m_fields.push_back(spec);
860 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
864 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
866 std::vector<std::string> parts = split(element,';');
868 if ((parts.size() == 5) ||
869 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
871 std::vector<std::string> v_pos = split(parts[0],',');
872 std::string name = parts[2];
873 std::vector<std::string> items = split(parts[3],',');
874 std::string str_initial_selection = "";
875 str_initial_selection = parts[4];
877 MY_CHECKPOS("dropdown",0);
880 pos.X += stof(v_pos[0]) * (float)spacing.X;
881 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
883 s32 width = stof(parts[1]) * (float)spacing.Y;
885 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
886 pos.X + width, pos.Y + (m_btn_height * 2));
888 std::wstring fname_w = narrow_to_wide(name.c_str());
897 spec.ftype = f_DropDown;
900 //now really show list
901 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
903 if (spec.fname == data->focused_fieldname) {
904 Environment->setFocus(e);
907 for (unsigned int i=0; i < items.size(); i++) {
908 e->addItem(narrow_to_wide(items[i]).c_str());
911 if (str_initial_selection != "")
912 e->setSelected(stoi(str_initial_selection.c_str())-1);
914 m_fields.push_back(spec);
917 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
918 << element << "'" << std::endl;
921 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
923 std::vector<std::string> parts = split(element,';');
925 if ((parts.size() == 4) ||
926 ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
928 std::vector<std::string> v_pos = split(parts[0],',');
929 std::vector<std::string> v_geom = split(parts[1],',');
930 std::string name = parts[2];
931 std::string label = parts[3];
933 MY_CHECKPOS("pwdfield",0);
934 MY_CHECKGEOM("pwdfield",1);
937 pos.X += stof(v_pos[0]) * (float)spacing.X;
938 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
941 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
943 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
944 pos.Y -= m_btn_height;
945 geom.Y = m_btn_height*2;
947 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
949 label = unescape_string(label);
951 std::wstring wlabel = narrow_to_wide(label.c_str());
954 narrow_to_wide(name.c_str()),
961 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
963 if (spec.fname == data->focused_fieldname) {
964 Environment->setFocus(e);
967 if (label.length() >= 1)
969 int font_height = font_line_height(m_font);
970 rect.UpperLeftCorner.Y -= font_height;
971 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
972 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
975 e->setPasswordBox(true,L'*');
978 evt.EventType = EET_KEY_INPUT_EVENT;
979 evt.KeyInput.Key = KEY_END;
980 evt.KeyInput.Char = 0;
981 evt.KeyInput.Control = 0;
982 evt.KeyInput.Shift = 0;
983 evt.KeyInput.PressedDown = true;
985 m_fields.push_back(spec);
988 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
991 void GUIFormSpecMenu::parseSimpleField(parserData* data,
992 std::vector<std::string> &parts)
994 std::string name = parts[0];
995 std::string label = parts[1];
996 std::string default_val = parts[2];
998 core::rect<s32> rect;
1000 if(data->explicit_size)
1001 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
1003 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1004 pos.Y = ((m_fields.size()+2)*60);
1005 v2s32 size = DesiredRect.getSize();
1007 rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
1008 (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
1012 default_val = m_form_src->resolveText(default_val);
1014 default_val = unescape_string(default_val);
1015 label = unescape_string(label);
1017 std::wstring wlabel = narrow_to_wide(label.c_str());
1020 narrow_to_wide(name.c_str()),
1022 narrow_to_wide(default_val.c_str()),
1028 // spec field id to 0, this stops submit searching for a value that isn't there
1029 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1034 gui::IGUIEditBox *e =
1035 Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1037 if (spec.fname == data->focused_fieldname) {
1038 Environment->setFocus(e);
1042 evt.EventType = EET_KEY_INPUT_EVENT;
1043 evt.KeyInput.Key = KEY_END;
1044 evt.KeyInput.Char = 0;
1045 evt.KeyInput.Control = 0;
1046 evt.KeyInput.Shift = 0;
1047 evt.KeyInput.PressedDown = true;
1050 if (label.length() >= 1)
1052 int font_height = font_line_height(m_font);
1053 rect.UpperLeftCorner.Y -= font_height;
1054 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1055 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1059 m_fields.push_back(spec);
1062 void GUIFormSpecMenu::parseTextArea(parserData* data,
1063 std::vector<std::string>& parts,std::string type)
1066 std::vector<std::string> v_pos = split(parts[0],',');
1067 std::vector<std::string> v_geom = split(parts[1],',');
1068 std::string name = parts[2];
1069 std::string label = parts[3];
1070 std::string default_val = parts[4];
1072 MY_CHECKPOS(type,0);
1073 MY_CHECKGEOM(type,1);
1076 pos.X = stof(v_pos[0]) * (float) spacing.X;
1077 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
1081 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1083 if (type == "textarea")
1085 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
1086 pos.Y += m_btn_height;
1090 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1091 pos.Y -= m_btn_height;
1092 geom.Y = m_btn_height*2;
1095 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1097 if(!data->explicit_size)
1098 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1101 default_val = m_form_src->resolveText(default_val);
1104 default_val = unescape_string(default_val);
1105 label = unescape_string(label);
1107 std::wstring wlabel = narrow_to_wide(label.c_str());
1110 narrow_to_wide(name.c_str()),
1112 narrow_to_wide(default_val.c_str()),
1118 // spec field id to 0, this stops submit searching for a value that isn't there
1119 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1124 gui::IGUIEditBox *e =
1125 Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1127 if (spec.fname == data->focused_fieldname) {
1128 Environment->setFocus(e);
1131 if (type == "textarea")
1133 e->setMultiLine(true);
1134 e->setWordWrap(true);
1135 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1138 evt.EventType = EET_KEY_INPUT_EVENT;
1139 evt.KeyInput.Key = KEY_END;
1140 evt.KeyInput.Char = 0;
1141 evt.KeyInput.Control = 0;
1142 evt.KeyInput.Shift = 0;
1143 evt.KeyInput.PressedDown = true;
1147 if (label.length() >= 1)
1149 int font_height = font_line_height(m_font);
1150 rect.UpperLeftCorner.Y -= font_height;
1151 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1152 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1155 m_fields.push_back(spec);
1158 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1161 std::vector<std::string> parts = split(element,';');
1163 if (parts.size() == 3 || parts.size() == 4) {
1164 parseSimpleField(data,parts);
1168 if ((parts.size() == 5) ||
1169 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1171 parseTextArea(data,parts,type);
1174 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1177 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1179 std::vector<std::string> parts = split(element,';');
1181 if ((parts.size() == 2) ||
1182 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1184 std::vector<std::string> v_pos = split(parts[0],',');
1185 std::string text = parts[1];
1187 MY_CHECKPOS("label",0);
1189 v2s32 pos = padding;
1190 pos.X += stof(v_pos[0]) * (float)spacing.X;
1191 pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
1193 if(!data->explicit_size)
1194 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1196 int font_height = font_line_height(m_font);
1198 text = unescape_string(text);
1199 std::vector<std::string> lines = split(text, '\n');
1201 for (unsigned int i = 0; i != lines.size(); i++) {
1202 // Lines are spaced at the nominal distance of
1203 // 2/5 inventory slot, even if the font doesn't
1204 // quite match that. This provides consistent
1205 // form layout, at the expense of sometimes
1206 // having sub-optimal spacing for the font.
1207 // We multiply by 2 and then divide by 5, rather
1208 // than multiply by 0.4, to get exact results
1209 // in the integer cases: 0.4 is not exactly
1210 // representable in binary floating point.
1211 s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
1212 std::wstring wlabel = narrow_to_wide(lines[i].c_str());
1213 core::rect<s32> rect = core::rect<s32>(
1214 pos.X, posy - font_height,
1215 pos.X + m_font->getDimension(wlabel.c_str()).Width,
1216 posy + font_height);
1223 gui::IGUIStaticText *e =
1224 Environment->addStaticText(spec.flabel.c_str(),
1225 rect, false, false, this, spec.fid);
1226 e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1228 m_fields.push_back(spec);
1233 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1236 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1238 std::vector<std::string> parts = split(element,';');
1240 if ((parts.size() == 2) ||
1241 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1243 std::vector<std::string> v_pos = split(parts[0],',');
1244 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1246 MY_CHECKPOS("vertlabel",1);
1248 v2s32 pos = padding;
1249 pos.X += stof(v_pos[0]) * (float)spacing.X;
1250 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1252 core::rect<s32> rect = core::rect<s32>(
1253 pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
1255 font_line_height(m_font)
1257 +((imgsize.Y/2)- m_btn_height));
1258 //actually text.length() would be correct but adding +1 avoids to break all mods
1260 if(!data->explicit_size)
1261 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1263 std::wstring label = L"";
1265 for (unsigned int i=0; i < text.length(); i++) {
1276 gui::IGUIStaticText *t =
1277 Environment->addStaticText(spec.flabel.c_str(), rect, false, false, this, spec.fid);
1278 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1279 m_fields.push_back(spec);
1282 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1285 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1288 std::vector<std::string> parts = split(element,';');
1290 if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
1291 ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
1293 std::vector<std::string> v_pos = split(parts[0],',');
1294 std::vector<std::string> v_geom = split(parts[1],',');
1295 std::string image_name = parts[2];
1296 std::string name = parts[3];
1297 std::string label = parts[4];
1299 MY_CHECKPOS("imagebutton",0);
1300 MY_CHECKGEOM("imagebutton",1);
1302 v2s32 pos = padding;
1303 pos.X += stof(v_pos[0]) * (float)spacing.X;
1304 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1306 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1307 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1309 bool noclip = false;
1310 bool drawborder = true;
1311 std::string pressed_image_name = "";
1313 if (parts.size() >= 7) {
1314 if (parts[5] == "true")
1316 if (parts[6] == "false")
1320 if (parts.size() >= 8) {
1321 pressed_image_name = parts[7];
1324 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1326 if(!data->explicit_size)
1327 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1329 image_name = unescape_string(image_name);
1330 pressed_image_name = unescape_string(pressed_image_name);
1331 label = unescape_string(label);
1333 std::wstring wlabel = narrow_to_wide(label.c_str());
1336 narrow_to_wide(name.c_str()),
1338 narrow_to_wide(image_name.c_str()),
1341 spec.ftype = f_Button;
1342 if(type == "image_button_exit")
1343 spec.is_exit = true;
1345 video::ITexture *texture = 0;
1346 video::ITexture *pressed_texture = 0;
1347 texture = m_tsrc->getTexture(image_name);
1348 if (pressed_image_name != "")
1349 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1351 pressed_texture = texture;
1353 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1355 if (spec.fname == data->focused_fieldname) {
1356 Environment->setFocus(e);
1359 e->setUseAlphaChannel(true);
1360 e->setImage(texture);
1361 e->setPressedImage(pressed_texture);
1362 e->setScaleImage(true);
1363 e->setNotClipped(noclip);
1364 e->setDrawBorder(drawborder);
1366 m_fields.push_back(spec);
1370 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1373 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1375 std::vector<std::string> parts = split(element,';');
1377 if (((parts.size() == 4) || (parts.size() == 6)) ||
1378 ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
1380 std::vector<std::string> v_pos = split(parts[0],',');
1381 std::string name = parts[1];
1382 std::vector<std::string> buttons = split(parts[2],',');
1383 std::string str_index = parts[3];
1384 bool show_background = true;
1385 bool show_border = true;
1386 int tab_index = stoi(str_index) -1;
1388 MY_CHECKPOS("tabheader",0);
1390 if (parts.size() == 6) {
1391 if (parts[4] == "true")
1392 show_background = false;
1393 if (parts[5] == "false")
1394 show_border = false;
1398 narrow_to_wide(name.c_str()),
1404 spec.ftype = f_TabHeader;
1407 pos.X += stof(v_pos[0]) * (float)spacing.X;
1408 pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
1410 geom.X = DesiredRect.getWidth();
1411 geom.Y = m_btn_height*2;
1413 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
1416 gui::IGUITabControl *e = Environment->addTabControl(rect, this,
1417 show_background, show_border, spec.fid);
1418 e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
1419 irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
1420 e->setTabHeight(m_btn_height*2);
1422 if (spec.fname == data->focused_fieldname) {
1423 Environment->setFocus(e);
1426 e->setNotClipped(true);
1428 for (unsigned int i=0; i< buttons.size(); i++) {
1429 e->addTab(narrow_to_wide(buttons[i]).c_str(), -1);
1432 if ((tab_index >= 0) &&
1433 (buttons.size() < INT_MAX) &&
1434 (tab_index < (int) buttons.size()))
1435 e->setActiveTab(tab_index);
1437 m_fields.push_back(spec);
1440 errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
1441 << element << "'" << std::endl;
1444 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1447 if (m_gamedef == 0) {
1449 "WARNING: invalid use of item_image_button with m_gamedef==0"
1454 std::vector<std::string> parts = split(element,';');
1456 if ((parts.size() == 5) ||
1457 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1459 std::vector<std::string> v_pos = split(parts[0],',');
1460 std::vector<std::string> v_geom = split(parts[1],',');
1461 std::string item_name = parts[2];
1462 std::string name = parts[3];
1463 std::string label = parts[4];
1465 MY_CHECKPOS("itemimagebutton",0);
1466 MY_CHECKGEOM("itemimagebutton",1);
1468 v2s32 pos = padding;
1469 pos.X += stof(v_pos[0]) * (float)spacing.X;
1470 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1472 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1473 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1475 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1477 if(!data->explicit_size)
1478 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1480 IItemDefManager *idef = m_gamedef->idef();
1482 item.deSerialize(item_name, idef);
1483 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1485 m_tooltips[narrow_to_wide(name.c_str())] =
1486 TooltipSpec (item.getDefinition(idef).description,
1487 m_default_tooltip_bgcolor,
1488 m_default_tooltip_color);
1490 label = unescape_string(label);
1492 narrow_to_wide(name.c_str()),
1493 narrow_to_wide(label.c_str()),
1494 narrow_to_wide(item_name.c_str()),
1498 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1500 if (spec.fname == data->focused_fieldname) {
1501 Environment->setFocus(e);
1504 e->setUseAlphaChannel(true);
1505 e->setImage(texture);
1506 e->setPressedImage(texture);
1507 e->setScaleImage(true);
1508 spec.ftype = f_Button;
1509 rect+=data->basepos-padding;
1511 m_fields.push_back(spec);
1514 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1517 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1519 std::vector<std::string> parts = split(element,';');
1521 if ((parts.size() == 3) ||
1522 ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
1524 std::vector<std::string> v_pos = split(parts[0],',');
1525 std::vector<std::string> v_geom = split(parts[1],',');
1527 MY_CHECKPOS("box",0);
1528 MY_CHECKGEOM("box",1);
1530 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1531 pos.X += stof(v_pos[0]) * (float) spacing.X;
1532 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1535 geom.X = stof(v_geom[0]) * (float)spacing.X;
1536 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1538 video::SColor tmp_color;
1540 if (parseColorString(parts[2], tmp_color, false)) {
1541 BoxDrawSpec spec(pos, geom, tmp_color);
1543 m_boxes.push_back(spec);
1546 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1550 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1553 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1555 std::vector<std::string> parts = split(element,';');
1557 if (((parts.size() == 1) || (parts.size() == 2)) ||
1558 ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
1560 parseColorString(parts[0],m_bgcolor,false);
1562 if (parts.size() == 2) {
1563 std::string fullscreen = parts[1];
1564 m_bgfullscreen = is_yes(fullscreen);
1568 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1571 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1573 std::vector<std::string> parts = split(element,';');
1575 if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
1576 ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1578 parseColorString(parts[0], m_slotbg_n, false);
1579 parseColorString(parts[1], m_slotbg_h, false);
1581 if (parts.size() >= 3) {
1582 if (parseColorString(parts[2], m_slotbordercolor, false)) {
1583 m_slotborder = true;
1586 if (parts.size() == 5) {
1587 video::SColor tmp_color;
1589 if (parseColorString(parts[3], tmp_color, false))
1590 m_default_tooltip_bgcolor = tmp_color;
1591 if (parseColorString(parts[4], tmp_color, false))
1592 m_default_tooltip_color = tmp_color;
1596 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1599 void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element)
1601 std::vector<std::string> parts = split(element,';');
1602 if (parts.size() == 2) {
1603 std::string name = parts[0];
1604 m_tooltips[narrow_to_wide(name.c_str())] = TooltipSpec (parts[1], m_default_tooltip_bgcolor, m_default_tooltip_color);
1606 } else if (parts.size() == 4) {
1607 std::string name = parts[0];
1608 video::SColor tmp_color1, tmp_color2;
1609 if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
1610 m_tooltips[narrow_to_wide(name.c_str())] = TooltipSpec (parts[1], tmp_color1, tmp_color2);
1614 errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
1617 bool GUIFormSpecMenu::parseVersionDirect(std::string data)
1623 std::vector<std::string> parts = split(data,'[');
1625 if (parts.size() < 2) {
1629 if (parts[0] != "formspec_version") {
1633 if (is_number(parts[1])) {
1634 m_formspec_version = mystoi(parts[1]);
1641 bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element)
1646 std::vector<std::string> parts = split(element,'[');
1648 if (parts.size() < 2)
1651 std::string type = trim(parts[0]);
1652 std::string description = trim(parts[1]);
1654 if (type != "size" && type != "invsize")
1657 if (type == "invsize")
1658 log_deprecated("Deprecated formspec element \"invsize\" is used");
1660 parseSize(data, description);
1665 void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
1671 std::vector<std::string> parts = split(element,'[');
1673 // ugly workaround to keep compatibility
1674 if (parts.size() > 2) {
1675 if (trim(parts[0]) == "image") {
1676 for (unsigned int i=2;i< parts.size(); i++) {
1677 parts[1] += "[" + parts[i];
1683 if (parts.size() < 2) {
1687 std::string type = trim(parts[0]);
1688 std::string description = trim(parts[1]);
1690 if (type == "list") {
1691 parseList(data,description);
1695 if (type == "checkbox") {
1696 parseCheckbox(data,description);
1700 if (type == "image") {
1701 parseImage(data,description);
1705 if (type == "item_image") {
1706 parseItemImage(data,description);
1710 if ((type == "button") || (type == "button_exit")) {
1711 parseButton(data,description,type);
1715 if (type == "background") {
1716 parseBackground(data,description);
1720 if (type == "tableoptions"){
1721 parseTableOptions(data,description);
1725 if (type == "tablecolumns"){
1726 parseTableColumns(data,description);
1730 if (type == "table"){
1731 parseTable(data,description);
1735 if (type == "textlist"){
1736 parseTextList(data,description);
1740 if (type == "dropdown"){
1741 parseDropDown(data,description);
1745 if (type == "pwdfield") {
1746 parsePwdField(data,description);
1750 if ((type == "field") || (type == "textarea")){
1751 parseField(data,description,type);
1755 if (type == "label") {
1756 parseLabel(data,description);
1760 if (type == "vertlabel") {
1761 parseVertLabel(data,description);
1765 if (type == "item_image_button") {
1766 parseItemImageButton(data,description);
1770 if ((type == "image_button") || (type == "image_button_exit")) {
1771 parseImageButton(data,description,type);
1775 if (type == "tabheader") {
1776 parseTabHeader(data,description);
1780 if (type == "box") {
1781 parseBox(data,description);
1785 if (type == "bgcolor") {
1786 parseBackgroundColor(data,description);
1790 if (type == "listcolors") {
1791 parseListColors(data,description);
1795 if (type == "tooltip") {
1796 parseTooltip(data,description);
1800 if (type == "scrollbar") {
1801 parseScrollBar(data, description);
1807 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1813 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1815 /* useless to regenerate without a screensize */
1816 if ((screensize.X <= 0) || (screensize.Y <= 0)) {
1823 for (u32 i = 0; i < m_tables.size(); ++i) {
1824 std::wstring tablename = m_tables[i].first.fname;
1825 GUITable *table = m_tables[i].second;
1826 mydata.table_dyndata[tablename] = table->getDynamicData();
1830 gui::IGUIElement *focused_element = Environment->getFocus();
1831 if (focused_element && focused_element->getParent() == this) {
1832 s32 focused_id = focused_element->getID();
1833 if (focused_id > 257) {
1834 for (u32 i=0; i<m_fields.size(); i++) {
1835 if (m_fields[i].fid == focused_id) {
1836 mydata.focused_fieldname =
1847 for (u32 i = 0; i < m_tables.size(); ++i) {
1848 GUITable *table = m_tables[i].second;
1852 mydata.size= v2s32(100,100);
1853 mydata.screensize = screensize;
1855 // Base position of contents of form
1856 mydata.basepos = getBasePos();
1858 /* Convert m_init_draw_spec to m_inventorylists */
1860 m_inventorylists.clear();
1862 m_backgrounds.clear();
1863 m_itemimages.clear();
1865 m_checkboxes.clear();
1866 m_scrollbars.clear();
1871 // Set default values (fits old formspec values)
1872 m_bgcolor = video::SColor(140,0,0,0);
1873 m_bgfullscreen = false;
1875 m_slotbg_n = video::SColor(255,128,128,128);
1876 m_slotbg_h = video::SColor(255,192,192,192);
1878 m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
1879 m_default_tooltip_color = video::SColor(255,255,255,255);
1881 m_slotbordercolor = video::SColor(200,0,0,0);
1882 m_slotborder = false;
1884 m_clipbackground = false;
1887 assert(m_tooltip_element == NULL);
1888 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1889 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1890 m_tooltip_element->enableOverrideColor(true);
1891 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
1892 m_tooltip_element->setDrawBackground(true);
1893 m_tooltip_element->setDrawBorder(true);
1894 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
1895 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1896 m_tooltip_element->setWordWrap(false);
1897 //we're not parent so no autograb for this one!
1898 m_tooltip_element->grab();
1901 std::vector<std::string> elements = split(m_formspec_string,']');
1904 /* try to read version from first element only */
1905 if (elements.size() >= 1) {
1906 if ( parseVersionDirect(elements[0]) ) {
1911 /* we need size first in order to calculate image scale */
1912 mydata.explicit_size = false;
1913 for (; i< elements.size(); i++) {
1914 if (!parseSizeDirect(&mydata, elements[i])) {
1919 if (mydata.explicit_size) {
1920 // compute scaling for specified form size
1922 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
1923 v2u32 delta = current_screensize - m_lockscreensize;
1925 if (current_screensize.Y > m_lockscreensize.Y)
1930 if (current_screensize.X > m_lockscreensize.X)
1935 offset = v2s32(delta.X,delta.Y);
1937 mydata.screensize = m_lockscreensize;
1939 offset = v2s32(0,0);
1942 double gui_scaling = g_settings->getFloat("gui_scaling");
1943 double screen_dpi = porting::getDisplayDensity() * 96;
1947 // In fixed-size mode, inventory image size
1948 // is 0.53 inch multiplied by the gui_scaling
1949 // config parameter. This magic size is chosen
1950 // to make the main menu (15.5 inventory images
1951 // wide, including border) just fit into the
1952 // default window (800 pixels wide) at 96 DPI
1953 // and default scaling (1.00).
1954 use_imgsize = 0.53 * screen_dpi * gui_scaling;
1956 // In variable-size mode, we prefer to make the
1957 // inventory image size 1/15 of screen height,
1958 // multiplied by the gui_scaling config parameter.
1959 // If the preferred size won't fit the whole
1960 // form on the screen, either horizontally or
1961 // vertically, then we scale it down to fit.
1962 // (The magic numbers in the computation of what
1963 // fits arise from the scaling factors in the
1964 // following stanza, including the form border,
1965 // help text space, and 0.1 inventory slot spare.)
1966 // However, a minimum size is also set, that
1967 // the image size can't be less than 0.3 inch
1968 // multiplied by gui_scaling, even if this means
1969 // the form doesn't fit the screen.
1970 double prefer_imgsize = mydata.screensize.Y / 15 *
1972 double fitx_imgsize = mydata.screensize.X /
1973 ((5.0/4.0) * (0.5 + mydata.invsize.X));
1974 double fity_imgsize = mydata.screensize.Y /
1975 ((15.0/13.0) * (0.85 * mydata.invsize.Y));
1976 double screen_dpi = porting::getDisplayDensity() * 96;
1977 double min_imgsize = 0.3 * screen_dpi * gui_scaling;
1978 use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
1979 MYMIN(fitx_imgsize, fity_imgsize)));
1982 // Everything else is scaled in proportion to the
1983 // inventory image size. The inventory slot spacing
1984 // is 5/4 image size horizontally and 15/13 image size
1985 // vertically. The padding around the form (incorporating
1986 // the border of the outer inventory slots) is 3/8
1987 // image size. Font height (baseline to baseline)
1988 // is 2/5 vertical inventory slot spacing, and button
1989 // half-height is 7/8 of font height.
1990 imgsize = v2s32(use_imgsize, use_imgsize);
1991 spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
1992 padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
1993 double target_font_height = use_imgsize*15.0/13 * 0.4;
1994 m_btn_height = use_imgsize*15.0/13 * 0.35;
1996 m_font = select_font_by_line_height(target_font_height);
1998 mydata.size = v2s32(
1999 padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
2000 padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
2002 DesiredRect = mydata.rect = core::rect<s32>(
2003 mydata.screensize.X/2 - mydata.size.X/2 + offset.X,
2004 mydata.screensize.Y/2 - mydata.size.Y/2 + offset.Y,
2005 mydata.screensize.X/2 + mydata.size.X/2 + offset.X,
2006 mydata.screensize.Y/2 + mydata.size.Y/2 + offset.Y
2009 // Non-size[] form must consist only of text fields and
2010 // implicit "Proceed" button. Use default font, and
2011 // temporary form size which will be recalculated below.
2012 m_font = g_fontengine->getFont();
2013 m_btn_height = font_line_height(m_font) * 0.875;
2014 DesiredRect = core::rect<s32>(
2015 mydata.screensize.X/2 - 580/2,
2016 mydata.screensize.Y/2 - 300/2,
2017 mydata.screensize.X/2 + 580/2,
2018 mydata.screensize.Y/2 + 300/2
2021 recalculateAbsolutePosition(false);
2022 mydata.basepos = getBasePos();
2023 m_tooltip_element->setOverrideFont(m_font);
2025 gui::IGUISkin* skin = Environment->getSkin();
2026 assert(skin != NULL);
2027 gui::IGUIFont *old_font = skin->getFont();
2028 skin->setFont(m_font);
2030 for (; i< elements.size(); i++) {
2031 parseElement(&mydata, elements[i]);
2034 // If there are fields without explicit size[], add a "Proceed"
2035 // button and adjust size to fit all the fields.
2036 if (m_fields.size() && !mydata.explicit_size) {
2037 mydata.rect = core::rect<s32>(
2038 mydata.screensize.X/2 - 580/2,
2039 mydata.screensize.Y/2 - 300/2,
2040 mydata.screensize.X/2 + 580/2,
2041 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
2043 DesiredRect = mydata.rect;
2044 recalculateAbsolutePosition(false);
2045 mydata.basepos = getBasePos();
2048 v2s32 pos = mydata.basepos;
2049 pos.Y = ((m_fields.size()+2)*60);
2051 v2s32 size = DesiredRect.getSize();
2053 core::rect<s32>(size.X/2-70, pos.Y,
2054 (size.X/2-70)+140, pos.Y + (m_btn_height*2));
2055 wchar_t* text = wgettext("Proceed");
2056 Environment->addButton(mydata.rect, this, 257, text);
2062 //set initial focus if parser didn't set it
2063 focused_element = Environment->getFocus();
2064 if (!focused_element
2065 || !isMyChild(focused_element)
2066 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
2069 skin->setFont(old_font);
2073 bool GUIFormSpecMenu::getAndroidUIInput()
2075 /* no dialog shown */
2076 if (m_JavaDialogFieldName == L"") {
2081 if (porting::getInputDialogState() == -1) {
2085 std::wstring fieldname = m_JavaDialogFieldName;
2086 m_JavaDialogFieldName = L"";
2088 /* no value abort dialog processing */
2089 if (porting::getInputDialogState() != 0) {
2093 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2094 iter != m_fields.end(); iter++) {
2096 if (iter->fname != fieldname) {
2099 IGUIElement* tochange = getElementFromId(iter->fid);
2101 if (tochange == 0) {
2105 if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
2109 std::string text = porting::getInputDialogValue();
2111 ((gui::IGUIEditBox*) tochange)->
2112 setText(narrow_to_wide(text).c_str());
2118 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
2120 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2122 for(u32 i=0; i<m_inventorylists.size(); i++)
2124 const ListDrawSpec &s = m_inventorylists[i];
2126 for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
2127 s32 item_i = i + s.start_item_i;
2128 s32 x = (i%s.geom.X) * spacing.X;
2129 s32 y = (i/s.geom.X) * spacing.Y;
2131 core::rect<s32> rect = imgrect + s.pos + p0;
2132 if(rect.isPointInside(p))
2134 return ItemSpec(s.inventoryloc, s.listname, item_i);
2139 return ItemSpec(InventoryLocation(), "", -1);
2142 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
2144 video::IVideoDriver* driver = Environment->getVideoDriver();
2146 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2148 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2149 <<"The inventory location "
2150 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2154 InventoryList *ilist = inv->getList(s.listname);
2156 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
2157 <<"The inventory list \""<<s.listname<<"\" @ \""
2158 <<s.inventoryloc.dump()<<"\" doesn't exist"
2163 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2165 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
2167 s32 item_i = i + s.start_item_i;
2168 if(item_i >= (s32) ilist->getSize())
2170 s32 x = (i%s.geom.X) * spacing.X;
2171 s32 y = (i/s.geom.X) * spacing.Y;
2173 core::rect<s32> rect = imgrect + s.pos + p;
2176 item = ilist->getItem(item_i);
2178 bool selected = m_selected_item
2179 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
2180 && m_selected_item->listname == s.listname
2181 && m_selected_item->i == item_i;
2182 bool hovering = rect.isPointInside(m_pointer);
2187 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
2189 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
2192 //Draw inv slot borders
2194 s32 x1 = rect.UpperLeftCorner.X;
2195 s32 y1 = rect.UpperLeftCorner.Y;
2196 s32 x2 = rect.LowerRightCorner.X;
2197 s32 y2 = rect.LowerRightCorner.Y;
2199 driver->draw2DRectangle(m_slotbordercolor,
2200 core::rect<s32>(v2s32(x1 - border, y1 - border),
2201 v2s32(x2 + border, y1)), NULL);
2202 driver->draw2DRectangle(m_slotbordercolor,
2203 core::rect<s32>(v2s32(x1 - border, y2),
2204 v2s32(x2 + border, y2 + border)), NULL);
2205 driver->draw2DRectangle(m_slotbordercolor,
2206 core::rect<s32>(v2s32(x1 - border, y1),
2207 v2s32(x1, y2)), NULL);
2208 driver->draw2DRectangle(m_slotbordercolor,
2209 core::rect<s32>(v2s32(x2, y1),
2210 v2s32(x2 + border, y2)), NULL);
2218 item.takeItem(m_selected_amount);
2222 drawItemStack(driver, m_font, item,
2223 rect, &AbsoluteClippingRect, m_gamedef);
2227 std::string tooltip_text = "";
2228 if (hovering && !m_selected_item)
2229 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
2230 if (tooltip_text != "") {
2231 std::vector<std::string> tt_rows = str_split(tooltip_text, '\n');
2232 m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2233 m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2234 m_tooltip_element->setVisible(true);
2235 this->bringToFront(m_tooltip_element);
2236 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
2237 s32 tooltip_x = m_pointer.X + m_btn_height;
2238 s32 tooltip_y = m_pointer.Y + m_btn_height;
2239 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2240 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2241 m_tooltip_element->setRelativePosition(core::rect<s32>(
2242 core::position2d<s32>(tooltip_x, tooltip_y),
2243 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2249 void GUIFormSpecMenu::drawSelectedItem()
2251 if(!m_selected_item)
2254 video::IVideoDriver* driver = Environment->getVideoDriver();
2256 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2258 InventoryList *list = inv->getList(m_selected_item->listname);
2260 ItemStack stack = list->getItem(m_selected_item->i);
2261 stack.count = m_selected_amount;
2263 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
2264 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2265 drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef);
2268 void GUIFormSpecMenu::drawMenu()
2271 std::string newform = m_form_src->getForm();
2272 if(newform != m_formspec_string){
2273 m_formspec_string = newform;
2274 regenerateGui(m_screensize_old);
2278 gui::IGUISkin* skin = Environment->getSkin();
2279 assert(skin != NULL);
2280 gui::IGUIFont *old_font = skin->getFont();
2281 skin->setFont(m_font);
2283 updateSelectedItem();
2285 video::IVideoDriver* driver = Environment->getVideoDriver();
2287 v2u32 screenSize = driver->getScreenSize();
2288 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
2290 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
2292 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
2294 m_tooltip_element->setVisible(false);
2299 for(u32 i=0; i<m_backgrounds.size(); i++)
2301 const ImageDrawSpec &spec = m_backgrounds[i];
2302 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2305 // Image size on screen
2306 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2307 // Image rectangle on screen
2308 core::rect<s32> rect = imgrect + spec.pos;
2310 if (m_clipbackground) {
2311 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
2312 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
2313 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
2314 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
2315 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
2318 const video::SColor color(255,255,255,255);
2319 const video::SColor colors[] = {color,color,color,color};
2320 driver->draw2DImage(texture, rect,
2321 core::rect<s32>(core::position2d<s32>(0,0),
2322 core::dimension2di(texture->getOriginalSize())),
2323 NULL/*&AbsoluteClippingRect*/, colors, true);
2326 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
2327 errorstream << "\t" << spec.name << std::endl;
2334 for(u32 i=0; i<m_boxes.size(); i++)
2336 const BoxDrawSpec &spec = m_boxes[i];
2338 irr::video::SColor todraw = spec.color;
2340 todraw.setAlpha(140);
2342 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
2343 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
2345 driver->draw2DRectangle(todraw, rect, 0);
2350 for(u32 i=0; i<m_images.size(); i++)
2352 const ImageDrawSpec &spec = m_images[i];
2353 video::ITexture *texture = m_tsrc->getTexture(spec.name);
2356 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
2357 // Image size on screen
2358 core::rect<s32> imgrect;
2361 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
2364 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
2366 // Image rectangle on screen
2367 core::rect<s32> rect = imgrect + spec.pos;
2368 const video::SColor color(255,255,255,255);
2369 const video::SColor colors[] = {color,color,color,color};
2370 driver->draw2DImage(texture, rect,
2371 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
2372 NULL/*&AbsoluteClippingRect*/, colors, true);
2375 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
2376 errorstream << "\t" << spec.name << std::endl;
2383 for(u32 i=0; i<m_itemimages.size(); i++)
2388 const ImageDrawSpec &spec = m_itemimages[i];
2389 IItemDefManager *idef = m_gamedef->idef();
2391 item.deSerialize(spec.name, idef);
2392 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
2393 // Image size on screen
2394 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2395 // Image rectangle on screen
2396 core::rect<s32> rect = imgrect + spec.pos;
2397 const video::SColor color(255,255,255,255);
2398 const video::SColor colors[] = {color,color,color,color};
2399 driver->draw2DImage(texture, rect,
2400 core::rect<s32>(core::position2d<s32>(0,0),
2401 core::dimension2di(texture->getOriginalSize())),
2402 NULL/*&AbsoluteClippingRect*/, colors, true);
2407 Phase 0: Item slot rectangles
2408 Phase 1: Item images; prepare tooltip
2411 for(int phase=start_phase; phase<=1; phase++)
2412 for(u32 i=0; i<m_inventorylists.size(); i++)
2414 drawList(m_inventorylists[i], phase);
2420 gui::IGUIElement::draw();
2422 /* TODO find way to show tooltips on touchscreen */
2423 #ifndef HAVE_TOUCHSCREENGUI
2424 m_pointer = m_device->getCursorControl()->getPosition();
2428 Draw fields/buttons tooltips
2430 gui::IGUIElement *hovered =
2431 Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2433 if (hovered != NULL) {
2434 s32 id = hovered->getID();
2438 m_old_tooltip_id = id;
2441 if (id == m_old_tooltip_id) {
2442 delta = porting::getDeltaMs(m_hovered_time, getTimeMs());
2444 m_hovered_time = getTimeMs();
2445 m_old_tooltip_id = id;
2449 if (id != -1 && delta >= m_tooltip_show_delay) {
2450 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2451 iter != m_fields.end(); iter++) {
2452 if ( (iter->fid == id) && (m_tooltips[iter->fname].tooltip != "") ){
2453 if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2454 m_old_tooltip = m_tooltips[iter->fname].tooltip;
2455 m_tooltip_element->setText(narrow_to_wide(m_tooltips[iter->fname].tooltip).c_str());
2456 s32 tooltip_x = m_pointer.X + m_btn_height;
2457 s32 tooltip_y = m_pointer.Y + m_btn_height;
2458 s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2459 if (tooltip_x + tooltip_width > (s32)screenSize.X)
2460 tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2461 std::vector<std::string> tt_rows = str_split(m_tooltips[iter->fname].tooltip, '\n');
2462 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2463 m_tooltip_element->setRelativePosition(core::rect<s32>(
2464 core::position2d<s32>(tooltip_x, tooltip_y),
2465 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2467 m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2468 m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2469 m_tooltip_element->setVisible(true);
2470 this->bringToFront(m_tooltip_element);
2478 Draw dragged item stack
2482 skin->setFont(old_font);
2485 void GUIFormSpecMenu::updateSelectedItem()
2487 // If the selected stack has become empty for some reason, deselect it.
2488 // If the selected stack has become inaccessible, deselect it.
2489 // If the selected stack has become smaller, adjust m_selected_amount.
2490 ItemStack selected = verifySelectedItem();
2492 // WARNING: BLACK MAGIC
2493 // See if there is a stack suited for our current guess.
2494 // If such stack does not exist, clear the guess.
2495 if(m_selected_content_guess.name != "" &&
2496 selected.name == m_selected_content_guess.name &&
2497 selected.count == m_selected_content_guess.count){
2498 // Selected item fits the guess. Skip the black magic.
2500 else if(m_selected_content_guess.name != ""){
2502 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2503 const ListDrawSpec &s = m_inventorylists[i];
2504 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2507 InventoryList *list = inv->getList(s.listname);
2510 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2511 u32 item_i = i + s.start_item_i;
2512 if(item_i >= list->getSize())
2514 ItemStack stack = list->getItem(item_i);
2515 if(stack.name == m_selected_content_guess.name &&
2516 stack.count == m_selected_content_guess.count){
2518 infostream<<"Client: Changing selected content guess to "
2519 <<s.inventoryloc.dump()<<" "<<s.listname
2520 <<" "<<item_i<<std::endl;
2521 delete m_selected_item;
2522 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2523 m_selected_amount = stack.count;
2528 infostream<<"Client: Discarding selected content guess: "
2529 <<m_selected_content_guess.getItemString()<<std::endl;
2530 m_selected_content_guess.name = "";
2534 // If craftresult is nonempty and nothing else is selected, select it now.
2535 if(!m_selected_item)
2537 for(u32 i=0; i<m_inventorylists.size(); i++)
2539 const ListDrawSpec &s = m_inventorylists[i];
2540 if(s.listname == "craftpreview")
2542 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2543 InventoryList *list = inv->getList("craftresult");
2544 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2546 m_selected_item = new ItemSpec;
2547 m_selected_item->inventoryloc = s.inventoryloc;
2548 m_selected_item->listname = "craftresult";
2549 m_selected_item->i = 0;
2550 m_selected_amount = 0;
2551 m_selected_dragging = false;
2558 // If craftresult is selected, keep the whole stack selected
2559 if(m_selected_item && m_selected_item->listname == "craftresult")
2561 m_selected_amount = verifySelectedItem().count;
2565 ItemStack GUIFormSpecMenu::verifySelectedItem()
2567 // If the selected stack has become empty for some reason, deselect it.
2568 // If the selected stack has become inaccessible, deselect it.
2569 // If the selected stack has become smaller, adjust m_selected_amount.
2570 // Return the selected stack.
2574 if(m_selected_item->isValid())
2576 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2579 InventoryList *list = inv->getList(m_selected_item->listname);
2580 if(list && (u32) m_selected_item->i < list->getSize())
2582 ItemStack stack = list->getItem(m_selected_item->i);
2583 if(m_selected_amount > stack.count)
2584 m_selected_amount = stack.count;
2591 // selection was not valid
2592 delete m_selected_item;
2593 m_selected_item = NULL;
2594 m_selected_amount = 0;
2595 m_selected_dragging = false;
2600 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2604 std::map<std::string, std::string> fields;
2606 if (quitmode == quit_mode_accept) {
2607 fields["quit"] = "true";
2610 if (quitmode == quit_mode_cancel) {
2611 fields["quit"] = "true";
2612 m_text_dst->gotText(fields);
2616 if (current_keys_pending.key_down) {
2617 fields["key_down"] = "true";
2618 current_keys_pending.key_down = false;
2621 if (current_keys_pending.key_up) {
2622 fields["key_up"] = "true";
2623 current_keys_pending.key_up = false;
2626 if (current_keys_pending.key_enter) {
2627 fields["key_enter"] = "true";
2628 current_keys_pending.key_enter = false;
2631 if (current_keys_pending.key_escape) {
2632 fields["key_escape"] = "true";
2633 current_keys_pending.key_escape = false;
2636 for(unsigned int i=0; i<m_fields.size(); i++) {
2637 const FieldSpec &s = m_fields[i];
2639 std::string name = wide_to_narrow(s.fname);
2640 if(s.ftype == f_Button) {
2641 fields[name] = wide_to_narrow(s.flabel);
2643 else if(s.ftype == f_Table) {
2644 GUITable *table = getTable(s.fname);
2646 fields[name] = table->checkEvent();
2649 else if(s.ftype == f_DropDown) {
2650 // no dynamic cast possible due to some distributions shipped
2651 // without rtti support in irrlicht
2652 IGUIElement * element = getElementFromId(s.fid);
2653 gui::IGUIComboBox *e = NULL;
2654 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2655 e = static_cast<gui::IGUIComboBox*>(element);
2657 s32 selected = e->getSelected();
2658 if (selected >= 0) {
2660 wide_to_narrow(e->getItem(selected));
2663 else if (s.ftype == f_TabHeader) {
2664 // no dynamic cast possible due to some distributions shipped
2665 // without rtti support in irrlicht
2666 IGUIElement * element = getElementFromId(s.fid);
2667 gui::IGUITabControl *e = NULL;
2668 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2669 e = static_cast<gui::IGUITabControl*>(element);
2673 std::stringstream ss;
2674 ss << (e->getActiveTab() +1);
2675 fields[name] = ss.str();
2678 else if (s.ftype == f_CheckBox) {
2679 // no dynamic cast possible due to some distributions shipped
2680 // without rtti support in irrlicht
2681 IGUIElement * element = getElementFromId(s.fid);
2682 gui::IGUICheckBox *e = NULL;
2683 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2684 e = static_cast<gui::IGUICheckBox*>(element);
2689 fields[name] = "true";
2691 fields[name] = "false";
2694 else if (s.ftype == f_ScrollBar) {
2695 // no dynamic cast possible due to some distributions shipped
2696 // without rtti support in irrlicht
2697 IGUIElement * element = getElementFromId(s.fid);
2698 gui::IGUIScrollBar *e = NULL;
2699 if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
2700 e = static_cast<gui::IGUIScrollBar*>(element);
2704 std::stringstream os;
2706 if (s.fdefault == L"Changed")
2707 fields[name] = "CHG:" + os.str();
2709 fields[name] = "VAL:" + os.str();
2714 IGUIElement* e = getElementFromId(s.fid);
2716 fields[name] = wide_to_narrow(e->getText());
2722 m_text_dst->gotText(fields);
2726 static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
2728 while(tocheck != NULL) {
2729 if (tocheck == parent) {
2732 tocheck = tocheck->getParent();
2737 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2739 // The IGUITabControl renders visually using the skin's selected
2740 // font, which we override for the duration of form drawing,
2741 // but computes tab hotspots based on how it would have rendered
2742 // using the font that is selected at the time of button release.
2743 // To make these two consistent, temporarily override the skin's
2744 // font while the IGUITabControl is processing the event.
2745 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
2746 event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2747 s32 x = event.MouseInput.X;
2748 s32 y = event.MouseInput.Y;
2749 gui::IGUIElement *hovered =
2750 Environment->getRootGUIElement()->getElementFromPoint(
2751 core::position2d<s32>(x, y));
2752 if (hovered->getType() == gui::EGUIET_TAB_CONTROL) {
2753 gui::IGUISkin* skin = Environment->getSkin();
2754 assert(skin != NULL);
2755 gui::IGUIFont *old_font = skin->getFont();
2756 skin->setFont(m_font);
2757 bool retval = hovered->OnEvent(event);
2758 skin->setFont(old_font);
2763 // Fix Esc/Return key being eaten by checkboxen and tables
2764 if(event.EventType==EET_KEY_INPUT_EVENT) {
2765 KeyPress kp(event.KeyInput);
2766 if (kp == EscapeKey || kp == CancelKey
2767 || kp == getKeySetting("keymap_inventory")
2768 || event.KeyInput.Key==KEY_RETURN) {
2769 gui::IGUIElement *focused = Environment->getFocus();
2770 if (focused && isMyChild(focused) &&
2771 (focused->getType() == gui::EGUIET_LIST_BOX ||
2772 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2778 // Mouse wheel events: send to hovered element instead of focused
2779 if(event.EventType==EET_MOUSE_INPUT_EVENT
2780 && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
2781 s32 x = event.MouseInput.X;
2782 s32 y = event.MouseInput.Y;
2783 gui::IGUIElement *hovered =
2784 Environment->getRootGUIElement()->getElementFromPoint(
2785 core::position2d<s32>(x, y));
2786 if (hovered && isMyChild(hovered)) {
2787 hovered->OnEvent(event);
2792 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
2793 s32 x = event.MouseInput.X;
2794 s32 y = event.MouseInput.Y;
2795 gui::IGUIElement *hovered =
2796 Environment->getRootGUIElement()->getElementFromPoint(
2797 core::position2d<s32>(x, y));
2798 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2799 m_old_tooltip_id = -1;
2802 if (!isChild(hovered,this)) {
2803 if (DoubleClickDetection(event)) {
2810 // display software keyboard when clicking edit boxes
2811 if (event.EventType == EET_MOUSE_INPUT_EVENT
2812 && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2813 gui::IGUIElement *hovered =
2814 Environment->getRootGUIElement()->getElementFromPoint(
2815 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
2816 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
2817 bool retval = hovered->OnEvent(event);
2819 Environment->setFocus(hovered);
2821 m_JavaDialogFieldName = getNameByID(hovered->getID());
2822 std::string message = gettext("Enter ");
2823 std::string label = wide_to_narrow(getLabelByID(hovered->getID()));
2827 message += gettext(label) + ":";
2829 /* single line text input */
2832 /* multi line text input */
2833 if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
2837 /* passwords are always single line */
2838 if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
2842 porting::showInputDialog(gettext("ok"), "",
2843 wide_to_narrow(((gui::IGUIEditBox*) hovered)->getText()),
2849 if (event.EventType == EET_TOUCH_INPUT_EVENT)
2852 memset(&translated, 0, sizeof(SEvent));
2853 translated.EventType = EET_MOUSE_INPUT_EVENT;
2854 gui::IGUIElement* root = Environment->getRootGUIElement();
2858 << "GUIFormSpecMenu::preprocessEvent unable to get root element"
2862 gui::IGUIElement* hovered = root->getElementFromPoint(
2863 core::position2d<s32>(
2865 event.TouchInput.Y));
2867 translated.MouseInput.X = event.TouchInput.X;
2868 translated.MouseInput.Y = event.TouchInput.Y;
2869 translated.MouseInput.Control = false;
2871 bool dont_send_event = false;
2873 if (event.TouchInput.touchedCount == 1) {
2874 switch (event.TouchInput.Event) {
2875 case ETIE_PRESSED_DOWN:
2876 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2877 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
2878 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2879 m_down_pos = m_pointer;
2882 m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
2883 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
2884 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2887 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
2888 translated.MouseInput.ButtonStates = 0;
2889 hovered = root->getElementFromPoint(m_down_pos);
2890 /* we don't have a valid pointer element use last
2891 * known pointer pos */
2892 translated.MouseInput.X = m_pointer.X;
2893 translated.MouseInput.Y = m_pointer.Y;
2895 /* reset down pos */
2896 m_down_pos = v2s32(0,0);
2899 dont_send_event = true;
2900 //this is not supposed to happen
2902 << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
2903 << event.TouchInput.Event << std::endl;
2905 } else if ( (event.TouchInput.touchedCount == 2) &&
2906 (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
2907 hovered = root->getElementFromPoint(m_down_pos);
2909 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
2910 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
2911 translated.MouseInput.X = m_pointer.X;
2912 translated.MouseInput.Y = m_pointer.Y;
2915 hovered->OnEvent(translated);
2918 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
2919 translated.MouseInput.ButtonStates = EMBSM_LEFT;
2923 hovered->OnEvent(translated);
2925 dont_send_event = true;
2927 /* ignore unhandled 2 touch events ... accidental moving for example */
2928 else if (event.TouchInput.touchedCount == 2) {
2929 dont_send_event = true;
2931 else if (event.TouchInput.touchedCount > 2) {
2933 << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
2934 << event.TouchInput.touchedCount << " ignoring them" << std::endl;
2937 if (dont_send_event) {
2941 /* check if translated event needs to be preprocessed again */
2942 if (preprocessEvent(translated)) {
2947 bool retval = hovered->OnEvent(translated);
2949 if (event.TouchInput.Event == ETIE_LEFT_UP) {
2951 m_pointer = v2s32(0,0);
2962 /******************************************************************************/
2963 bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
2965 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
2966 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
2967 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
2969 m_doubleclickdetect[1].pos = m_pointer;
2970 m_doubleclickdetect[1].time = getTimeMs();
2972 else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
2973 u32 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, getTimeMs());
2978 double squaredistance =
2979 m_doubleclickdetect[0].pos
2980 .getDistanceFromSQ(m_doubleclickdetect[1].pos);
2982 if (squaredistance > (30*30)) {
2986 SEvent* translated = new SEvent();
2987 assert(translated != 0);
2988 //translate doubleclick to escape
2989 memset(translated, 0, sizeof(SEvent));
2990 translated->EventType = irr::EET_KEY_INPUT_EVENT;
2991 translated->KeyInput.Key = KEY_ESCAPE;
2992 translated->KeyInput.Control = false;
2993 translated->KeyInput.Shift = false;
2994 translated->KeyInput.PressedDown = true;
2995 translated->KeyInput.Char = 0;
2996 OnEvent(*translated);
2998 // no need to send the key up event as we're already deleted
2999 // and no one else did notice this event
3006 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
3008 if(event.EventType==EET_KEY_INPUT_EVENT) {
3009 KeyPress kp(event.KeyInput);
3010 if (event.KeyInput.PressedDown && ( (kp == EscapeKey) ||
3011 (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) {
3014 acceptInput(quit_mode_cancel);
3017 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
3020 } else if (m_client != NULL && event.KeyInput.PressedDown &&
3021 (kp == getKeySetting("keymap_screenshot"))) {
3022 m_client->makeScreenshot(m_device);
3024 if (event.KeyInput.PressedDown &&
3025 (event.KeyInput.Key==KEY_RETURN ||
3026 event.KeyInput.Key==KEY_UP ||
3027 event.KeyInput.Key==KEY_DOWN)
3029 switch (event.KeyInput.Key) {
3031 current_keys_pending.key_enter = true;
3034 current_keys_pending.key_up = true;
3037 current_keys_pending.key_down = true;
3041 //can't happen at all!
3042 assert("reached a source line that can't ever been reached" == 0);
3045 if (current_keys_pending.key_enter && m_allowclose) {
3046 acceptInput(quit_mode_accept);
3056 /* Mouse event other than movement, or crossing the border of inventory
3057 field while holding right mouse button
3059 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
3060 (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
3061 (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
3062 event.MouseInput.isRightPressed() &&
3063 getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
3065 // Get selected item and hovered/clicked item (s)
3067 m_old_tooltip_id = -1;
3068 updateSelectedItem();
3069 ItemSpec s = getItemAtPos(m_pointer);
3071 Inventory *inv_selected = NULL;
3072 Inventory *inv_s = NULL;
3074 if(m_selected_item) {
3075 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
3076 assert(inv_selected);
3077 assert(inv_selected->getList(m_selected_item->listname) != NULL);
3084 inv_s = m_invmgr->getInventory(s.inventoryloc);
3087 errorstream<<"InventoryMenu: The selected inventory location "
3088 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
3090 s.i = -1; // make it invalid again
3094 InventoryList *list = inv_s->getList(s.listname);
3096 verbosestream<<"InventoryMenu: The selected inventory list \""
3097 <<s.listname<<"\" does not exist"<<std::endl;
3098 s.i = -1; // make it invalid again
3102 if((u32)s.i >= list->getSize()) {
3103 infostream<<"InventoryMenu: The selected inventory list \""
3104 <<s.listname<<"\" is too small (i="<<s.i<<", size="
3105 <<list->getSize()<<")"<<std::endl;
3106 s.i = -1; // make it invalid again
3110 s_count = list->getItem(s.i).count;
3113 bool identical = (m_selected_item != NULL) && s.isValid() &&
3114 (inv_selected == inv_s) &&
3115 (m_selected_item->listname == s.listname) &&
3116 (m_selected_item->i == s.i);
3118 // buttons: 0 = left, 1 = right, 2 = middle
3119 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
3122 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
3123 { button = 0; updown = 0; }
3124 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
3125 { button = 1; updown = 0; }
3126 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
3127 { button = 2; updown = 0; }
3128 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
3129 { button = 0; updown = 1; }
3130 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
3131 { button = 1; updown = 1; }
3132 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
3133 { button = 2; updown = 1; }
3134 else if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
3137 // Set this number to a positive value to generate a move action
3138 // from m_selected_item to s.
3139 u32 move_amount = 0;
3141 // Set this number to a positive value to generate a drop action
3142 // from m_selected_item.
3143 u32 drop_amount = 0;
3145 // Set this number to a positive value to generate a craft action at s.
3146 u32 craft_amount = 0;
3149 // Some mouse button has been pressed
3151 //infostream<<"Mouse button "<<button<<" pressed at p=("
3152 // <<p.X<<","<<p.Y<<")"<<std::endl;
3154 m_selected_dragging = false;
3156 if(s.isValid() && s.listname == "craftpreview") {
3157 // Craft preview has been clicked: craft
3158 craft_amount = (button == 2 ? 10 : 1);
3160 else if(m_selected_item == NULL) {
3162 // Non-empty stack has been clicked: select it
3163 m_selected_item = new ItemSpec(s);
3165 if(button == 1) // right
3166 m_selected_amount = (s_count + 1) / 2;
3167 else if(button == 2) // middle
3168 m_selected_amount = MYMIN(s_count, 10);
3170 m_selected_amount = s_count;
3172 m_selected_dragging = true;
3173 m_rmouse_auto_place = false;
3176 else { // m_selected_item != NULL
3177 assert(m_selected_amount >= 1);
3180 // Clicked a slot: move
3181 if(button == 1) // right
3183 else if(button == 2) // middle
3184 move_amount = MYMIN(m_selected_amount, 10);
3186 move_amount = m_selected_amount;
3189 if(move_amount >= m_selected_amount)
3190 m_selected_amount = 0;
3192 m_selected_amount -= move_amount;
3196 else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
3197 // Clicked outside of the window: drop
3198 if(button == 1) // right
3200 else if(button == 2) // middle
3201 drop_amount = MYMIN(m_selected_amount, 10);
3203 drop_amount = m_selected_amount;
3207 else if(updown == 1) {
3208 // Some mouse button has been released
3210 //infostream<<"Mouse button "<<button<<" released at p=("
3211 // <<p.X<<","<<p.Y<<")"<<std::endl;
3213 if(m_selected_item != NULL && m_selected_dragging && s.isValid()) {
3215 // Dragged to different slot: move all selected
3216 move_amount = m_selected_amount;
3219 else if(m_selected_item != NULL && m_selected_dragging &&
3220 !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
3221 // Dragged outside of window: drop all selected
3222 drop_amount = m_selected_amount;
3225 m_selected_dragging = false;
3226 // Keep count of how many times right mouse button has been
3227 // clicked. One click is drag without dropping. Click + release
3228 // + click changes to drop one item when moved mode
3229 if(button == 1 && m_selected_item != NULL)
3230 m_rmouse_auto_place = !m_rmouse_auto_place;
3232 else if(updown == -1) {
3233 // Mouse has been moved and rmb is down and mouse pointer just
3234 // entered a new inventory field (checked in the entry-if, this
3235 // is the only action here that is generated by mouse movement)
3236 if(m_selected_item != NULL && s.isValid()){
3238 // TODO: middle mouse to move 10 items might be handy
3239 if (m_rmouse_auto_place) {
3240 // Only move an item if the destination slot is empty
3241 // or contains the same item type as what is going to be
3243 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3244 InventoryList *list_to = inv_s->getList(s.listname);
3245 assert(list_from && list_to);
3246 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3247 ItemStack stack_to = list_to->getItem(s.i);
3248 if (stack_to.empty() || stack_to.name == stack_from.name)
3254 // Possibly send inventory action to server
3257 // Send IACTION_MOVE
3259 assert(m_selected_item && m_selected_item->isValid());
3260 assert(s.isValid());
3262 assert(inv_selected && inv_s);
3263 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3264 InventoryList *list_to = inv_s->getList(s.listname);
3265 assert(list_from && list_to);
3266 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3267 ItemStack stack_to = list_to->getItem(s.i);
3269 // Check how many items can be moved
3270 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
3271 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
3272 // If source stack cannot be added to destination stack at all,
3274 if ((leftover.count == stack_from.count) &&
3275 (leftover.name == stack_from.name)) {
3276 m_selected_amount = stack_to.count;
3277 // In case the server doesn't directly swap them but instead
3278 // moves stack_to somewhere else, set this
3279 m_selected_content_guess = stack_to;
3280 m_selected_content_guess_inventory = s.inventoryloc;
3282 // Source stack goes fully into destination stack
3283 else if(leftover.empty()) {
3284 m_selected_amount -= move_amount;
3285 m_selected_content_guess = ItemStack(); // Clear
3287 // Source stack goes partly into destination stack
3289 move_amount -= leftover.count;
3290 m_selected_amount -= move_amount;
3291 m_selected_content_guess = ItemStack(); // Clear
3294 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
3295 IMoveAction *a = new IMoveAction();
3296 a->count = move_amount;
3297 a->from_inv = m_selected_item->inventoryloc;
3298 a->from_list = m_selected_item->listname;
3299 a->from_i = m_selected_item->i;
3300 a->to_inv = s.inventoryloc;
3301 a->to_list = s.listname;
3303 m_invmgr->inventoryAction(a);
3305 else if(drop_amount > 0) {
3306 m_selected_content_guess = ItemStack(); // Clear
3308 // Send IACTION_DROP
3310 assert(m_selected_item && m_selected_item->isValid());
3311 assert(inv_selected);
3312 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
3314 ItemStack stack_from = list_from->getItem(m_selected_item->i);
3316 // Check how many items can be dropped
3317 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
3318 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
3319 m_selected_amount -= drop_amount;
3321 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
3322 IDropAction *a = new IDropAction();
3323 a->count = drop_amount;
3324 a->from_inv = m_selected_item->inventoryloc;
3325 a->from_list = m_selected_item->listname;
3326 a->from_i = m_selected_item->i;
3327 m_invmgr->inventoryAction(a);
3329 else if(craft_amount > 0) {
3330 m_selected_content_guess = ItemStack(); // Clear
3332 // Send IACTION_CRAFT
3334 assert(s.isValid());
3337 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
3338 ICraftAction *a = new ICraftAction();
3339 a->count = craft_amount;
3340 a->craft_inv = s.inventoryloc;
3341 m_invmgr->inventoryAction(a);
3344 // If m_selected_amount has been decreased to zero, deselect
3345 if(m_selected_amount == 0) {
3346 delete m_selected_item;
3347 m_selected_item = NULL;
3348 m_selected_amount = 0;
3349 m_selected_dragging = false;
3350 m_selected_content_guess = ItemStack();
3352 m_old_pointer = m_pointer;
3354 if(event.EventType==EET_GUI_EVENT) {
3356 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
3358 // find the element that was clicked
3359 for(unsigned int i=0; i<m_fields.size(); i++) {
3360 FieldSpec &s = m_fields[i];
3361 if ((s.ftype == f_TabHeader) &&
3362 (s.fid == event.GUIEvent.Caller->getID())) {
3370 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
3372 if(!canTakeFocus(event.GUIEvent.Element)) {
3373 infostream<<"GUIFormSpecMenu: Not allowing focus change."
3375 // Returning true disables focus change
3379 if((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
3380 (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
3381 (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
3382 (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
3383 unsigned int btn_id = event.GUIEvent.Caller->getID();
3385 if (btn_id == 257) {
3387 acceptInput(quit_mode_accept);
3391 m_text_dst->gotText(narrow_to_wide("ExitButton"));
3393 // quitMenu deallocates menu
3397 // find the element that was clicked
3398 for(u32 i=0; i<m_fields.size(); i++) {
3399 FieldSpec &s = m_fields[i];
3400 // if its a button, set the send field so
3401 // lua knows which button was pressed
3402 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
3403 (s.fid == event.GUIEvent.Caller->getID())) {
3407 acceptInput(quit_mode_accept);
3410 m_text_dst->gotText(narrow_to_wide("ExitButton"));
3414 acceptInput(quit_mode_no);
3419 else if ((s.ftype == f_DropDown) &&
3420 (s.fid == event.GUIEvent.Caller->getID())) {
3421 // only send the changed dropdown
3422 for(u32 i=0; i<m_fields.size(); i++) {
3423 FieldSpec &s2 = m_fields[i];
3424 if (s2.ftype == f_DropDown) {
3429 acceptInput(quit_mode_no);
3431 // revert configuration to make sure dropdowns are sent on
3432 // regular button click
3433 for(u32 i=0; i<m_fields.size(); i++) {
3434 FieldSpec &s2 = m_fields[i];
3435 if (s2.ftype == f_DropDown) {
3441 else if ((s.ftype == f_ScrollBar) &&
3442 (s.fid == event.GUIEvent.Caller->getID()))
3444 s.fdefault = L"Changed";
3445 acceptInput(quit_mode_no);
3451 if(event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
3452 if(event.GUIEvent.Caller->getID() > 257) {
3455 acceptInput(quit_mode_accept);
3458 current_keys_pending.key_enter = true;
3461 // quitMenu deallocates menu
3466 if(event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
3467 int current_id = event.GUIEvent.Caller->getID();
3468 if(current_id > 257) {
3469 // find the element that was clicked
3470 for(u32 i=0; i<m_fields.size(); i++) {
3471 FieldSpec &s = m_fields[i];
3472 // if it's a table, set the send field
3473 // so lua knows which table was changed
3474 if ((s.ftype == f_Table) && (s.fid == current_id)) {
3485 return Parent ? Parent->OnEvent(event) : false;
3489 * get name of element by element id
3490 * @param id of element
3491 * @return name string or empty string
3493 std::wstring GUIFormSpecMenu::getNameByID(s32 id)
3495 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3496 iter != m_fields.end(); iter++) {
3497 if (iter->fid == id) {
3505 * get label of element by id
3506 * @param id of element
3507 * @return label string or empty string
3509 std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
3511 for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
3512 iter != m_fields.end(); iter++) {
3513 if (iter->fid == id) {
3514 return iter->flabel;