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"
49 #define MY_CHECKPOS(a,b) \
50 if (v_pos.size() != 2) { \
51 errorstream<< "Invalid pos for element " << a << "specified: \"" \
52 << parts[b] << "\"" << std::endl; \
56 #define MY_CHECKGEOM(a,b) \
57 if (v_geom.size() != 2) { \
58 errorstream<< "Invalid pos for element " << a << "specified: \"" \
59 << parts[b] << "\"" << std::endl; \
68 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
69 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
70 InventoryManager *invmgr, IGameDef *gamedef,
71 ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
72 GUIFormSpecMenu** ext_ptr) :
73 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
78 m_selected_item(NULL),
80 m_selected_dragging(false),
81 m_tooltip_element(NULL),
88 current_keys_pending.key_down = false;
89 current_keys_pending.key_up = false;
90 current_keys_pending.key_enter = false;
91 current_keys_pending.key_escape = false;
95 GUIFormSpecMenu::~GUIFormSpecMenu()
99 delete m_selected_item;
101 if (m_form_src != NULL) {
104 if (m_text_dst != NULL) {
108 if (m_ext_ptr != NULL) {
109 assert(*m_ext_ptr == this);
114 void GUIFormSpecMenu::removeChildren()
116 const core::list<gui::IGUIElement*> &children = getChildren();
117 core::list<gui::IGUIElement*> children_copy;
118 for(core::list<gui::IGUIElement*>::ConstIterator
119 i = children.begin(); i != children.end(); i++)
121 children_copy.push_back(*i);
123 for(core::list<gui::IGUIElement*>::Iterator
124 i = children_copy.begin();
125 i != children_copy.end(); i++)
130 gui::IGUIElement *e = getElementFromId(256);
135 if(m_tooltip_element)
137 m_tooltip_element->remove();
138 m_tooltip_element->drop();
139 m_tooltip_element = NULL;
144 void GUIFormSpecMenu::setInitialFocus()
146 // Set initial focus according to following order of precedence:
147 // 1. first empty editbox
151 // 5. first focusable (not statictext, not tabheader)
152 // 6. first child element
154 core::list<gui::IGUIElement*> children = getChildren();
156 // in case "children" contains any NULL elements, remove them
157 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
158 it != children.end();) {
162 it = children.erase(it);
165 // 1. first empty editbox
166 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
167 it != children.end(); ++it) {
168 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
169 && (*it)->getText()[0] == 0) {
170 Environment->setFocus(*it);
176 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
177 it != children.end(); ++it) {
178 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
179 Environment->setFocus(*it);
185 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
186 it != children.end(); ++it) {
187 if ((*it)->getTypeName() == std::string("GUITable")) {
188 Environment->setFocus(*it);
194 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
195 it != children.end(); --it) {
196 if ((*it)->getType() == gui::EGUIET_BUTTON) {
197 Environment->setFocus(*it);
202 // 5. first focusable (not statictext, not tabheader)
203 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
204 it != children.end(); ++it) {
205 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
206 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
207 Environment->setFocus(*it);
212 // 6. first child element
213 if (children.empty())
214 Environment->setFocus(this);
216 Environment->setFocus(*(children.begin()));
219 GUITable* GUIFormSpecMenu::getTable(std::wstring tablename)
221 for (u32 i = 0; i < m_tables.size(); ++i) {
222 if (tablename == m_tables[i].first.fname)
223 return m_tables[i].second;
228 std::vector<std::string> split(const std::string &s, char delim) {
229 std::vector<std::string> tokens;
231 std::string current = "";
232 bool last_was_escape = false;
233 for(unsigned int i=0; i < s.size(); i++) {
234 if (last_was_escape) {
236 current += s.c_str()[i];
237 last_was_escape = false;
240 if (s.c_str()[i] == delim) {
241 tokens.push_back(current);
243 last_was_escape = false;
245 else if (s.c_str()[i] == '\\'){
246 last_was_escape = true;
249 current += s.c_str()[i];
250 last_was_escape = false;
255 tokens.push_back(current);
260 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
262 std::vector<std::string> parts = split(element,',');
264 if ((parts.size() == 2) || parts.size() == 3) {
267 if (parts[1].find(';') != std::string::npos)
268 parts[1] = parts[1].substr(0,parts[1].find(';'));
270 invsize.X = stof(parts[0]);
271 invsize.Y = stof(parts[1]);
274 if (parts.size() == 3) {
275 if (parts[2] == "true") {
276 lockSize(true,v2u32(800,600));
281 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
282 v2u32 delta = current_screensize - m_lockscreensize;
284 if (current_screensize.Y > m_lockscreensize.Y)
289 if (current_screensize.X > m_lockscreensize.X)
294 offset = v2s32(delta.X,delta.Y);
296 data->screensize = m_lockscreensize;
302 padding = v2s32(data->screensize.Y/40, data->screensize.Y/40);
303 spacing = v2s32(data->screensize.Y/12, data->screensize.Y/13);
304 imgsize = v2s32(data->screensize.Y/15, data->screensize.Y/15);
306 padding.X*2+spacing.X*(invsize.X-1.0)+imgsize.X,
307 padding.Y*2+spacing.Y*(invsize.Y-1.0)+imgsize.Y + (data->helptext_h-5)
309 data->rect = core::rect<s32>(
310 data->screensize.X/2 - data->size.X/2 + offset.X,
311 data->screensize.Y/2 - data->size.Y/2 + offset.Y,
312 data->screensize.X/2 + data->size.X/2 + offset.X,
313 data->screensize.Y/2 + data->size.Y/2 + offset.Y
316 DesiredRect = data->rect;
317 recalculateAbsolutePosition(false);
318 data->basepos = getBasePos();
322 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
325 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
327 if (m_gamedef == 0) {
328 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
332 std::vector<std::string> parts = split(element,';');
334 if ((parts.size() == 4) || (parts.size() == 5)) {
335 std::string location = parts[0];
336 std::string listname = parts[1];
337 std::vector<std::string> v_pos = split(parts[2],',');
338 std::vector<std::string> v_geom = split(parts[3],',');
339 std::string startindex = "";
340 if (parts.size() == 5)
341 startindex = parts[4];
343 MY_CHECKPOS("list",2);
344 MY_CHECKGEOM("list",3);
346 InventoryLocation loc;
348 if(location == "context" || location == "current_name")
349 loc = m_current_inventory_location;
351 loc.deSerialize(location);
353 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
354 pos.X += stof(v_pos[0]) * (float)spacing.X;
355 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
358 geom.X = stoi(v_geom[0]);
359 geom.Y = stoi(v_geom[1]);
363 start_i = stoi(startindex);
365 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
366 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
370 if(data->bp_set != 2)
371 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
372 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
375 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
378 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
380 std::vector<std::string> parts = split(element,';');
382 if ((parts.size() == 3) || (parts.size() == 4)) {
383 std::vector<std::string> v_pos = split(parts[0],',');
384 std::string name = parts[1];
385 std::string label = parts[2];
386 std::string selected = "";
388 if (parts.size() == 4)
391 MY_CHECKPOS("checkbox",0);
394 pos.X += stof(v_pos[0]) * (float) spacing.X;
395 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
397 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
399 bool fselected = false;
401 if (selected == "true")
404 std::wstring wlabel = narrow_to_wide(label.c_str());
406 FieldSpec spec = FieldSpec(
407 narrow_to_wide(name.c_str()),
413 spec.ftype = f_CheckBox;
414 spec.flabel = wlabel; //Needed for displaying text on MSVC
415 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
416 spec.fid, spec.flabel.c_str());
418 if (spec.fname == data->focused_fieldname) {
419 Environment->setFocus(e);
422 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
423 m_fields.push_back(spec);
426 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
429 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
431 std::vector<std::string> parts = split(element,';');
433 if (parts.size() == 3) {
434 std::vector<std::string> v_pos = split(parts[0],',');
435 std::vector<std::string> v_geom = split(parts[1],',');
436 std::string name = unescape_string(parts[2]);
438 MY_CHECKPOS("image",0);
439 MY_CHECKGEOM("image",1);
441 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
442 pos.X += stof(v_pos[0]) * (float) spacing.X;
443 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
446 geom.X = stof(v_geom[0]) * (float)imgsize.X;
447 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
449 if(data->bp_set != 2)
450 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
451 m_images.push_back(ImageDrawSpec(name, pos, geom));
455 if (parts.size() == 2) {
456 std::vector<std::string> v_pos = split(parts[0],',');
457 std::string name = unescape_string(parts[1]);
459 MY_CHECKPOS("image",0);
461 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
462 pos.X += stof(v_pos[0]) * (float) spacing.X;
463 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
465 if(data->bp_set != 2)
466 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
467 m_images.push_back(ImageDrawSpec(name, pos));
470 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
473 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
475 std::vector<std::string> parts = split(element,';');
477 if (parts.size() == 3) {
478 std::vector<std::string> v_pos = split(parts[0],',');
479 std::vector<std::string> v_geom = split(parts[1],',');
480 std::string name = parts[2];
482 MY_CHECKPOS("itemimage",0);
483 MY_CHECKGEOM("itemimage",1);
485 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
486 pos.X += stof(v_pos[0]) * (float) spacing.X;
487 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
490 geom.X = stoi(v_geom[0]) * (float)imgsize.X;
491 geom.Y = stoi(v_geom[1]) * (float)imgsize.Y;
493 if(data->bp_set != 2)
494 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
495 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
498 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
501 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
504 std::vector<std::string> parts = split(element,';');
506 if (parts.size() == 4) {
507 std::vector<std::string> v_pos = split(parts[0],',');
508 std::vector<std::string> v_geom = split(parts[1],',');
509 std::string name = parts[2];
510 std::string label = parts[3];
512 MY_CHECKPOS("button",0);
513 MY_CHECKGEOM("button",1);
516 pos.X += stof(v_pos[0]) * (float)spacing.X;
517 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
520 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
521 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
523 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y-15, pos.X+geom.X, pos.Y+15);
525 if(data->bp_set != 2)
526 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
528 label = unescape_string(label);
530 std::wstring wlabel = narrow_to_wide(label.c_str());
532 FieldSpec spec = FieldSpec(
533 narrow_to_wide(name.c_str()),
538 spec.ftype = f_Button;
539 if(type == "button_exit")
542 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
543 spec.flabel.c_str());
545 if (spec.fname == data->focused_fieldname) {
546 Environment->setFocus(e);
549 m_fields.push_back(spec);
552 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
555 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
557 std::vector<std::string> parts = split(element,';');
559 if ((parts.size() == 3) || (parts.size() == 4)) {
560 std::vector<std::string> v_pos = split(parts[0],',');
561 std::vector<std::string> v_geom = split(parts[1],',');
562 std::string name = unescape_string(parts[2]);
564 MY_CHECKPOS("background",0);
565 MY_CHECKGEOM("background",1);
567 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
568 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
569 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
572 geom.X = stof(v_geom[0]) * (float)spacing.X;
573 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
575 if (parts.size() == 4) {
576 m_clipbackground = is_yes(parts[3]);
577 if (m_clipbackground) {
578 pos.X = stoi(v_pos[0]); //acts as offset
579 pos.Y = stoi(v_pos[1]); //acts as offset
583 if(data->bp_set != 2)
584 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
585 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
588 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
591 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
593 std::vector<std::string> parts = split(element,';');
595 data->table_options.clear();
596 for (size_t i = 0; i < parts.size(); ++i) {
597 // Parse table option
598 std::string opt = unescape_string(parts[i]);
599 data->table_options.push_back(GUITable::splitOption(opt));
603 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
605 std::vector<std::string> parts = split(element,';');
607 data->table_columns.clear();
608 for (size_t i = 0; i < parts.size(); ++i) {
609 std::vector<std::string> col_parts = split(parts[i],',');
610 GUITable::TableColumn column;
612 if (!col_parts.empty())
613 column.type = col_parts[0];
614 // Parse column options
615 for (size_t j = 1; j < col_parts.size(); ++j) {
616 std::string opt = unescape_string(col_parts[j]);
617 column.options.push_back(GUITable::splitOption(opt));
619 data->table_columns.push_back(column);
623 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
625 std::vector<std::string> parts = split(element,';');
627 if ((parts.size() == 4) || (parts.size() == 5)) {
628 std::vector<std::string> v_pos = split(parts[0],',');
629 std::vector<std::string> v_geom = split(parts[1],',');
630 std::string name = parts[2];
631 std::vector<std::string> items = split(parts[3],',');
632 std::string str_initial_selection = "";
633 std::string str_transparent = "false";
635 if (parts.size() >= 5)
636 str_initial_selection = parts[4];
638 MY_CHECKPOS("table",0);
639 MY_CHECKGEOM("table",1);
642 pos.X += stof(v_pos[0]) * (float)spacing.X;
643 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
646 geom.X = stof(v_geom[0]) * (float)spacing.X;
647 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
650 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
652 std::wstring fname_w = narrow_to_wide(name.c_str());
654 FieldSpec spec = FieldSpec(
661 spec.ftype = f_Table;
663 for (unsigned int i = 0; i < items.size(); ++i) {
664 items[i] = unescape_string(items[i]);
667 //now really show table
668 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
670 e->drop(); // IGUIElement maintains the remaining reference
672 if (spec.fname == data->focused_fieldname) {
673 Environment->setFocus(e);
676 e->setTable(data->table_options, data->table_columns, items);
678 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
679 e->setDynamicData(data->table_dyndata[fname_w]);
682 if ((str_initial_selection != "") &&
683 (str_initial_selection != "0"))
684 e->setSelected(stoi(str_initial_selection.c_str()));
686 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
687 m_fields.push_back(spec);
690 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
693 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
695 std::vector<std::string> parts = split(element,';');
697 if ((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) {
698 std::vector<std::string> v_pos = split(parts[0],',');
699 std::vector<std::string> v_geom = split(parts[1],',');
700 std::string name = parts[2];
701 std::vector<std::string> items = split(parts[3],',');
702 std::string str_initial_selection = "";
703 std::string str_transparent = "false";
705 if (parts.size() >= 5)
706 str_initial_selection = parts[4];
708 if (parts.size() >= 6)
709 str_transparent = parts[5];
711 MY_CHECKPOS("textlist",0);
712 MY_CHECKGEOM("textlist",1);
715 pos.X += stof(v_pos[0]) * (float)spacing.X;
716 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
719 geom.X = stof(v_geom[0]) * (float)spacing.X;
720 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
723 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
725 std::wstring fname_w = narrow_to_wide(name.c_str());
727 FieldSpec spec = FieldSpec(
734 spec.ftype = f_Table;
736 for (unsigned int i = 0; i < items.size(); ++i) {
737 items[i] = unescape_string(items[i]);
740 //now really show list
741 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
743 e->drop(); // IGUIElement maintains the remaining reference
745 if (spec.fname == data->focused_fieldname) {
746 Environment->setFocus(e);
749 e->setTextList(items, is_yes(str_transparent));
751 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
752 e->setDynamicData(data->table_dyndata[fname_w]);
755 if ((str_initial_selection != "") &&
756 (str_initial_selection != "0"))
757 e->setSelected(stoi(str_initial_selection.c_str()));
759 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
760 m_fields.push_back(spec);
763 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
767 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
769 std::vector<std::string> parts = split(element,';');
771 if (parts.size() == 5) {
772 std::vector<std::string> v_pos = split(parts[0],',');
773 std::string name = parts[2];
774 std::vector<std::string> items = split(parts[3],',');
775 std::string str_initial_selection = "";
776 str_initial_selection = parts[4];
778 MY_CHECKPOS("dropdown",0);
781 pos.X += stof(v_pos[0]) * (float)spacing.X;
782 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
784 s32 width = stof(parts[1]) * (float)spacing.Y;
786 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+width, pos.Y+30);
788 std::wstring fname_w = narrow_to_wide(name.c_str());
790 FieldSpec spec = FieldSpec(
797 spec.ftype = f_DropDown;
800 //now really show list
801 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
803 if (spec.fname == data->focused_fieldname) {
804 Environment->setFocus(e);
807 for (unsigned int i=0; i < items.size(); i++) {
808 e->addItem(narrow_to_wide(items[i]).c_str());
811 if (str_initial_selection != "")
812 e->setSelected(stoi(str_initial_selection.c_str())-1);
814 m_fields.push_back(spec);
817 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
818 << element << "'" << std::endl;
821 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
823 std::vector<std::string> parts = split(element,';');
825 if (parts.size() == 4) {
826 std::vector<std::string> v_pos = split(parts[0],',');
827 std::vector<std::string> v_geom = split(parts[1],',');
828 std::string name = parts[2];
829 std::string label = parts[3];
831 MY_CHECKPOS("pwdfield",0);
832 MY_CHECKGEOM("pwdfield",1);
835 pos.X += stof(v_pos[0]) * (float)spacing.X;
836 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
839 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
841 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
845 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
847 label = unescape_string(label);
849 std::wstring wlabel = narrow_to_wide(label.c_str());
851 FieldSpec spec = FieldSpec(
852 narrow_to_wide(name.c_str()),
859 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
861 if (spec.fname == data->focused_fieldname) {
862 Environment->setFocus(e);
865 if (label.length() >= 1)
867 rect.UpperLeftCorner.Y -= 15;
868 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
869 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
872 e->setPasswordBox(true,L'*');
875 evt.EventType = EET_KEY_INPUT_EVENT;
876 evt.KeyInput.Key = KEY_END;
877 evt.KeyInput.Char = 0;
878 evt.KeyInput.Control = 0;
879 evt.KeyInput.Shift = 0;
880 evt.KeyInput.PressedDown = true;
882 m_fields.push_back(spec);
885 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
888 void GUIFormSpecMenu::parseSimpleField(parserData* data,
889 std::vector<std::string> &parts)
891 std::string name = parts[0];
892 std::string label = parts[1];
893 std::string default_val = parts[2];
895 core::rect<s32> rect;
899 rect = core::rect<s32>(
900 data->screensize.X/2 - 580/2,
901 data->screensize.Y/2 - 300/2,
902 data->screensize.X/2 + 580/2,
903 data->screensize.Y/2 + 300/2
906 recalculateAbsolutePosition(false);
907 data->basepos = getBasePos();
910 else if(data->bp_set == 2)
911 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
913 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
914 pos.Y = ((m_fields.size()+2)*60);
915 v2s32 size = DesiredRect.getSize();
917 rect = core::rect<s32>(size.X/2-150, pos.Y, (size.X/2-150)+300, pos.Y+30);
921 default_val = m_form_src->resolveText(default_val);
923 default_val = unescape_string(default_val);
924 label = unescape_string(label);
926 std::wstring wlabel = narrow_to_wide(label.c_str());
928 FieldSpec spec = FieldSpec(
929 narrow_to_wide(name.c_str()),
931 narrow_to_wide(default_val.c_str()),
937 // spec field id to 0, this stops submit searching for a value that isn't there
938 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
943 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
945 if (spec.fname == data->focused_fieldname) {
946 Environment->setFocus(e);
950 evt.EventType = EET_KEY_INPUT_EVENT;
951 evt.KeyInput.Key = KEY_END;
952 evt.KeyInput.Char = 0;
953 evt.KeyInput.Control = 0;
954 evt.KeyInput.Shift = 0;
955 evt.KeyInput.PressedDown = true;
958 if (label.length() >= 1)
960 rect.UpperLeftCorner.Y -= 15;
961 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
962 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
966 m_fields.push_back(spec);
969 void GUIFormSpecMenu::parseTextArea(parserData* data,
970 std::vector<std::string>& parts,std::string type)
973 std::vector<std::string> v_pos = split(parts[0],',');
974 std::vector<std::string> v_geom = split(parts[1],',');
975 std::string name = parts[2];
976 std::string label = parts[3];
977 std::string default_val = parts[4];
980 MY_CHECKGEOM(type,1);
983 pos.X = stof(v_pos[0]) * (float) spacing.X;
984 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
988 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
990 if (type == "textarea")
992 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
997 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
1002 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1004 if(data->bp_set != 2)
1005 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
1008 default_val = m_form_src->resolveText(default_val);
1011 default_val = unescape_string(default_val);
1012 label = unescape_string(label);
1014 std::wstring wlabel = narrow_to_wide(label.c_str());
1016 FieldSpec spec = FieldSpec(
1017 narrow_to_wide(name.c_str()),
1019 narrow_to_wide(default_val.c_str()),
1025 // spec field id to 0, this stops submit searching for a value that isn't there
1026 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1031 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1033 if (spec.fname == data->focused_fieldname) {
1034 Environment->setFocus(e);
1037 if (type == "textarea")
1039 e->setMultiLine(true);
1040 e->setWordWrap(true);
1041 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1044 evt.EventType = EET_KEY_INPUT_EVENT;
1045 evt.KeyInput.Key = KEY_END;
1046 evt.KeyInput.Char = 0;
1047 evt.KeyInput.Control = 0;
1048 evt.KeyInput.Shift = 0;
1049 evt.KeyInput.PressedDown = true;
1053 if (label.length() >= 1)
1055 rect.UpperLeftCorner.Y -= 15;
1056 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
1057 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1060 m_fields.push_back(spec);
1063 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1066 std::vector<std::string> parts = split(element,';');
1068 if (parts.size() == 3) {
1069 parseSimpleField(data,parts);
1073 if (parts.size() == 5) {
1074 parseTextArea(data,parts,type);
1077 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1080 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1082 std::vector<std::string> parts = split(element,';');
1084 if (parts.size() == 2) {
1085 std::vector<std::string> v_pos = split(parts[0],',');
1086 std::string text = parts[1];
1088 MY_CHECKPOS("label",0);
1090 v2s32 pos = padding;
1091 pos.X += stof(v_pos[0]) * (float)spacing.X;
1092 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1094 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
1096 if(data->bp_set != 2)
1097 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1099 text = unescape_string(text);
1101 std::wstring wlabel = narrow_to_wide(text.c_str());
1103 FieldSpec spec = FieldSpec(
1109 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1110 m_fields.push_back(spec);
1113 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1116 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1118 std::vector<std::string> parts = split(element,';');
1120 if (parts.size() == 2) {
1121 std::vector<std::string> v_pos = split(parts[0],',');
1122 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1124 MY_CHECKPOS("vertlabel",1);
1126 v2s32 pos = padding;
1127 pos.X += stof(v_pos[0]) * (float)spacing.X;
1128 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1130 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+15, pos.Y+300);
1132 if(data->bp_set != 2)
1133 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1135 std::wstring label = L"";
1137 for (unsigned int i=0; i < text.length(); i++) {
1142 FieldSpec spec = FieldSpec(
1148 gui::IGUIStaticText *t =
1149 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1150 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1151 m_fields.push_back(spec);
1154 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1157 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1160 std::vector<std::string> parts = split(element,';');
1162 if ((parts.size() == 5) || (parts.size() == 7) || (parts.size() == 8)) {
1163 std::vector<std::string> v_pos = split(parts[0],',');
1164 std::vector<std::string> v_geom = split(parts[1],',');
1165 std::string image_name = parts[2];
1166 std::string name = parts[3];
1167 std::string label = parts[4];
1169 MY_CHECKPOS("imagebutton",0);
1170 MY_CHECKGEOM("imagebutton",1);
1172 v2s32 pos = padding;
1173 pos.X += stof(v_pos[0]) * (float)spacing.X;
1174 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1176 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1177 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1179 bool noclip = false;
1180 bool drawborder = true;
1182 if ((parts.size() >= 7)) {
1183 if (parts[5] == "true")
1186 if (parts[6] == "false")
1190 std::string pressed_image_name = "";
1192 if ((parts.size() == 8)) {
1193 pressed_image_name = parts[7];
1196 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1198 if(data->bp_set != 2)
1199 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1201 image_name = unescape_string(image_name);
1202 pressed_image_name = unescape_string(pressed_image_name);
1203 label = unescape_string(label);
1205 std::wstring wlabel = narrow_to_wide(label.c_str());
1207 FieldSpec spec = FieldSpec(
1208 narrow_to_wide(name.c_str()),
1210 narrow_to_wide(image_name.c_str()),
1213 spec.ftype = f_Button;
1214 if(type == "image_button_exit")
1215 spec.is_exit = true;
1217 video::ITexture *texture = 0;
1218 video::ITexture *pressed_texture = 0;
1219 texture = m_tsrc->getTexture(image_name);
1220 if (parts.size() == 8)
1221 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1223 pressed_texture = texture;
1225 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1227 if (spec.fname == data->focused_fieldname) {
1228 Environment->setFocus(e);
1231 e->setUseAlphaChannel(true);
1232 e->setImage(texture);
1233 e->setPressedImage(pressed_texture);
1234 e->setScaleImage(true);
1235 e->setNotClipped(noclip);
1236 e->setDrawBorder(drawborder);
1238 m_fields.push_back(spec);
1242 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1245 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1247 std::vector<std::string> parts = split(element,';');
1249 if ((parts.size() == 4) || (parts.size() == 6)) {
1250 std::vector<std::string> v_pos = split(parts[0],',');
1251 std::string name = parts[1];
1252 std::vector<std::string> buttons = split(parts[2],',');
1253 std::string str_index = parts[3];
1254 bool show_background = true;
1255 bool show_border = true;
1256 int tab_index = stoi(str_index) -1;
1258 MY_CHECKPOS("tabheader",0);
1260 if (parts.size() == 6) {
1261 if (parts[4] == "true")
1262 show_background = false;
1263 if (parts[5] == "false")
1264 show_border = false;
1267 FieldSpec spec = FieldSpec(
1268 narrow_to_wide(name.c_str()),
1274 spec.ftype = f_TabHeader;
1276 v2s32 pos = padding;
1277 pos.X += stof(v_pos[0]) * (float)spacing.X;
1278 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1280 geom.X = data->screensize.Y;
1283 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1285 gui::IGUITabControl *e = Environment->addTabControl(rect,this,show_background,show_border,spec.fid);
1287 if (spec.fname == data->focused_fieldname) {
1288 Environment->setFocus(e);
1291 e->setNotClipped(true);
1293 for (unsigned int i=0; i< buttons.size(); i++) {
1294 wchar_t* wbutton = 0;
1296 std::wstring wlabel = narrow_to_wide(buttons[i]); //Needed for displaying text on windows
1297 wbutton = (wchar_t*) wlabel.c_str();
1299 e->addTab(wbutton,-1);
1302 if ((tab_index >= 0) &&
1303 (buttons.size() < INT_MAX) &&
1304 (tab_index < (int) buttons.size()))
1305 e->setActiveTab(tab_index);
1307 m_fields.push_back(spec);
1310 errorstream<< "Invalid TabHeader element(" << parts.size() << "): '" << element << "'" << std::endl;
1313 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1316 if (m_gamedef == 0) {
1317 errorstream<<"WARNING: invalid use of item_image_button with m_gamedef==0"<<std::endl;
1321 std::vector<std::string> parts = split(element,';');
1323 if (parts.size() == 5) {
1324 std::vector<std::string> v_pos = split(parts[0],',');
1325 std::vector<std::string> v_geom = split(parts[1],',');
1326 std::string item_name = parts[2];
1327 std::string name = parts[3];
1328 std::string label = parts[4];
1330 MY_CHECKPOS("itemimagebutton",0);
1331 MY_CHECKGEOM("itemimagebutton",1);
1333 v2s32 pos = padding;
1334 pos.X += stof(v_pos[0]) * (float)spacing.X;
1335 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1337 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1338 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1340 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1342 if(data->bp_set != 2)
1343 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1345 IItemDefManager *idef = m_gamedef->idef();
1347 item.deSerialize(item_name, idef);
1348 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1349 std::string tooltip = item.getDefinition(idef).description;
1351 label = unescape_string(label);
1352 FieldSpec spec = FieldSpec(
1353 narrow_to_wide(name.c_str()),
1354 narrow_to_wide(label.c_str()),
1355 narrow_to_wide(item_name.c_str()),
1359 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1361 if (spec.fname == data->focused_fieldname) {
1362 Environment->setFocus(e);
1365 e->setUseAlphaChannel(true);
1366 e->setImage(texture);
1367 e->setPressedImage(texture);
1368 e->setScaleImage(true);
1369 spec.ftype = f_Button;
1370 rect+=data->basepos-padding;
1373 spec.tooltip=tooltip;
1374 m_fields.push_back(spec);
1377 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1380 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1382 std::vector<std::string> parts = split(element,';');
1384 if (parts.size() == 3) {
1385 std::vector<std::string> v_pos = split(parts[0],',');
1386 std::vector<std::string> v_geom = split(parts[1],',');
1388 MY_CHECKPOS("box",0);
1389 MY_CHECKGEOM("box",1);
1391 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1392 pos.X += stof(v_pos[0]) * (float) spacing.X;
1393 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1396 geom.X = stof(v_geom[0]) * (float)spacing.X;
1397 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1399 video::SColor tmp_color;
1401 if (parseColor(parts[2], tmp_color, false)) {
1402 BoxDrawSpec spec(pos, geom, tmp_color);
1404 m_boxes.push_back(spec);
1407 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1411 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1414 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1416 std::vector<std::string> parts = split(element,';');
1418 if ((parts.size() == 1) || (parts.size() == 2)) {
1419 parseColor(parts[0],m_bgcolor,false);
1421 if (parts.size() == 2) {
1422 std::string fullscreen = parts[1];
1423 m_bgfullscreen = is_yes(fullscreen);
1427 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1430 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1432 std::vector<std::string> parts = split(element,';');
1434 if ((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) {
1435 parseColor(parts[0], m_slotbg_n, false);
1436 parseColor(parts[1], m_slotbg_h, false);
1438 if (parts.size() >= 3) {
1439 if (parseColor(parts[2], m_slotbordercolor, false)) {
1440 m_slotborder = true;
1443 if (parts.size() == 5) {
1444 video::SColor tmp_color;
1446 if (parseColor(parts[3], tmp_color, false))
1447 m_tooltip_element->setBackgroundColor(tmp_color);
1448 if (parseColor(parts[4], tmp_color, false))
1449 m_tooltip_element->setOverrideColor(tmp_color);
1453 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1456 void GUIFormSpecMenu::parseElement(parserData* data,std::string element)
1462 std::vector<std::string> parts = split(element,'[');
1464 // ugly workaround to keep compatibility
1465 if (parts.size() > 2) {
1466 if (trim(parts[0]) == "image") {
1467 for (unsigned int i=2;i< parts.size(); i++) {
1468 parts[1] += "[" + parts[i];
1474 if (parts.size() < 2) {
1478 std::string type = trim(parts[0]);
1479 std::string description = trim(parts[1]);
1481 if ((type == "size") || (type == "invsize")){
1482 parseSize(data,description);
1486 if (type == "list") {
1487 parseList(data,description);
1491 if (type == "checkbox") {
1492 parseCheckbox(data,description);
1496 if (type == "image") {
1497 parseImage(data,description);
1501 if (type == "item_image") {
1502 parseItemImage(data,description);
1506 if ((type == "button") || (type == "button_exit")) {
1507 parseButton(data,description,type);
1511 if (type == "background") {
1512 parseBackground(data,description);
1516 if (type == "tableoptions"){
1517 parseTableOptions(data,description);
1521 if (type == "tablecolumns"){
1522 parseTableColumns(data,description);
1526 if (type == "table"){
1527 parseTable(data,description);
1531 if (type == "textlist"){
1532 parseTextList(data,description);
1536 if (type == "dropdown"){
1537 parseDropDown(data,description);
1541 if (type == "pwdfield") {
1542 parsePwdField(data,description);
1546 if ((type == "field") || (type == "textarea")){
1547 parseField(data,description,type);
1551 if (type == "label") {
1552 parseLabel(data,description);
1556 if (type == "vertlabel") {
1557 parseVertLabel(data,description);
1561 if (type == "item_image_button") {
1562 parseItemImageButton(data,description);
1566 if ((type == "image_button") || (type == "image_button_exit")) {
1567 parseImageButton(data,description,type);
1571 if (type == "tabheader") {
1572 parseTabHeader(data,description);
1576 if (type == "box") {
1577 parseBox(data,description);
1581 if (type == "bgcolor") {
1582 parseBackgroundColor(data,description);
1586 if (type == "listcolors") {
1587 parseListColors(data,description);
1593 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1599 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1604 for (u32 i = 0; i < m_tables.size(); ++i) {
1605 std::wstring tablename = m_tables[i].first.fname;
1606 GUITable *table = m_tables[i].second;
1607 mydata.table_dyndata[tablename] = table->getDynamicData();
1611 gui::IGUIElement *focused_element = Environment->getFocus();
1612 if (focused_element && focused_element->getParent() == this) {
1613 s32 focused_id = focused_element->getID();
1614 if (focused_id > 257) {
1615 for (u32 i=0; i<m_fields.size(); i++) {
1616 if (m_fields[i].fid == focused_id) {
1617 mydata.focused_fieldname =
1628 mydata.size= v2s32(100,100);
1629 mydata.helptext_h = 15;
1630 mydata.screensize = screensize;
1632 // Base position of contents of form
1633 mydata.basepos = getBasePos();
1635 // State of basepos, 0 = not set, 1= set by formspec, 2 = set by size[] element
1636 // Used to adjust form size automatically if needed
1637 // A proceed button is added if there is no size[] element
1641 /* Convert m_init_draw_spec to m_inventorylists */
1643 m_inventorylists.clear();
1645 m_backgrounds.clear();
1646 m_itemimages.clear();
1648 m_checkboxes.clear();
1652 // Set default values (fits old formspec values)
1653 m_bgcolor = video::SColor(140,0,0,0);
1654 m_bgfullscreen = false;
1656 m_slotbg_n = video::SColor(255,128,128,128);
1657 m_slotbg_h = video::SColor(255,192,192,192);
1659 m_slotbordercolor = video::SColor(200,0,0,0);
1660 m_slotborder = false;
1662 m_clipbackground = false;
1665 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1666 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1667 m_tooltip_element->enableOverrideColor(true);
1668 m_tooltip_element->setBackgroundColor(video::SColor(255,110,130,60));
1669 m_tooltip_element->setDrawBackground(true);
1670 m_tooltip_element->setDrawBorder(true);
1671 m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
1672 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1673 m_tooltip_element->setWordWrap(false);
1674 //we're not parent so no autograb for this one!
1675 m_tooltip_element->grab();
1679 std::vector<std::string> elements = split(m_formspec_string,']');
1680 for (unsigned int i=0;i< elements.size();i++) {
1681 parseElement(&mydata,elements[i]);
1684 // If there's fields, add a Proceed button
1685 if (m_fields.size() && mydata.bp_set != 2)
1687 // if the size wasn't set by an invsize[] or size[] adjust it now to fit all the fields
1688 mydata.rect = core::rect<s32>(
1689 mydata.screensize.X/2 - 580/2,
1690 mydata.screensize.Y/2 - 300/2,
1691 mydata.screensize.X/2 + 580/2,
1692 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1694 DesiredRect = mydata.rect;
1695 recalculateAbsolutePosition(false);
1696 mydata.basepos = getBasePos();
1699 v2s32 pos = mydata.basepos;
1700 pos.Y = ((m_fields.size()+2)*60);
1702 v2s32 size = DesiredRect.getSize();
1703 mydata.rect = core::rect<s32>(size.X/2-70, pos.Y, (size.X/2-70)+140, pos.Y+30);
1704 wchar_t* text = wgettext("Proceed");
1705 Environment->addButton(mydata.rect, this, 257, text);
1711 //set initial focus if parser didn't set it
1712 focused_element = Environment->getFocus();
1713 if (!focused_element
1714 || !isMyChild(focused_element)
1715 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
1719 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
1721 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1723 for(u32 i=0; i<m_inventorylists.size(); i++)
1725 const ListDrawSpec &s = m_inventorylists[i];
1727 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1729 s32 item_i = i + s.start_item_i;
1730 s32 x = (i%s.geom.X) * spacing.X;
1731 s32 y = (i/s.geom.X) * spacing.Y;
1733 core::rect<s32> rect = imgrect + s.pos + p0;
1734 if(rect.isPointInside(p))
1736 return ItemSpec(s.inventoryloc, s.listname, item_i);
1741 return ItemSpec(InventoryLocation(), "", -1);
1744 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
1746 video::IVideoDriver* driver = Environment->getVideoDriver();
1749 gui::IGUIFont *font = NULL;
1750 gui::IGUISkin* skin = Environment->getSkin();
1752 font = skin->getFont();
1754 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1756 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1757 <<"The inventory location "
1758 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
1762 InventoryList *ilist = inv->getList(s.listname);
1764 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1765 <<"The inventory list \""<<s.listname<<"\" @ \""
1766 <<s.inventoryloc.dump()<<"\" doesn't exist"
1771 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1773 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1775 s32 item_i = i + s.start_item_i;
1776 if(item_i >= (s32) ilist->getSize())
1778 s32 x = (i%s.geom.X) * spacing.X;
1779 s32 y = (i/s.geom.X) * spacing.Y;
1781 core::rect<s32> rect = imgrect + s.pos + p;
1784 item = ilist->getItem(item_i);
1786 bool selected = m_selected_item
1787 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
1788 && m_selected_item->listname == s.listname
1789 && m_selected_item->i == item_i;
1790 bool hovering = rect.isPointInside(m_pointer);
1795 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
1797 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
1800 //Draw inv slot borders
1802 s32 x1 = rect.UpperLeftCorner.X;
1803 s32 y1 = rect.UpperLeftCorner.Y;
1804 s32 x2 = rect.LowerRightCorner.X;
1805 s32 y2 = rect.LowerRightCorner.Y;
1807 driver->draw2DRectangle(m_slotbordercolor,
1808 core::rect<s32>(v2s32(x1 - border, y1 - border),
1809 v2s32(x2 + border, y1)), NULL);
1810 driver->draw2DRectangle(m_slotbordercolor,
1811 core::rect<s32>(v2s32(x1 - border, y2),
1812 v2s32(x2 + border, y2 + border)), NULL);
1813 driver->draw2DRectangle(m_slotbordercolor,
1814 core::rect<s32>(v2s32(x1 - border, y1),
1815 v2s32(x1, y2)), NULL);
1816 driver->draw2DRectangle(m_slotbordercolor,
1817 core::rect<s32>(v2s32(x2, y1),
1818 v2s32(x2 + border, y2)), NULL);
1826 item.takeItem(m_selected_amount);
1830 drawItemStack(driver, font, item,
1831 rect, &AbsoluteClippingRect, m_gamedef);
1835 std::string tooltip_text = "";
1836 if(hovering && !m_selected_item)
1837 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
1838 if(tooltip_text != "")
1840 m_tooltip_element->setVisible(true);
1841 this->bringToFront(m_tooltip_element);
1842 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
1843 s32 tooltip_x = m_pointer.X + 15;
1844 s32 tooltip_y = m_pointer.Y + 15;
1845 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1846 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1847 m_tooltip_element->setRelativePosition(core::rect<s32>(
1848 core::position2d<s32>(tooltip_x, tooltip_y),
1849 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1855 void GUIFormSpecMenu::drawSelectedItem()
1857 if(!m_selected_item)
1860 video::IVideoDriver* driver = Environment->getVideoDriver();
1863 gui::IGUIFont *font = NULL;
1864 gui::IGUISkin* skin = Environment->getSkin();
1866 font = skin->getFont();
1868 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
1870 InventoryList *list = inv->getList(m_selected_item->listname);
1872 ItemStack stack = list->getItem(m_selected_item->i);
1873 stack.count = m_selected_amount;
1875 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1876 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
1877 drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
1880 void GUIFormSpecMenu::drawMenu()
1883 std::string newform = m_form_src->getForm();
1884 if(newform != m_formspec_string){
1885 m_formspec_string = newform;
1886 regenerateGui(m_screensize_old);
1890 m_pointer = m_device->getCursorControl()->getPosition();
1892 updateSelectedItem();
1894 gui::IGUISkin* skin = Environment->getSkin();
1897 video::IVideoDriver* driver = Environment->getVideoDriver();
1899 v2u32 screenSize = driver->getScreenSize();
1900 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
1902 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
1904 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
1906 m_tooltip_element->setVisible(false);
1911 for(u32 i=0; i<m_backgrounds.size(); i++)
1913 const ImageDrawSpec &spec = m_backgrounds[i];
1914 video::ITexture *texture = m_tsrc->getTexture(spec.name);
1917 // Image size on screen
1918 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1919 // Image rectangle on screen
1920 core::rect<s32> rect = imgrect + spec.pos;
1922 if (m_clipbackground) {
1923 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
1924 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
1925 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
1926 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
1927 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
1930 const video::SColor color(255,255,255,255);
1931 const video::SColor colors[] = {color,color,color,color};
1932 driver->draw2DImage(texture, rect,
1933 core::rect<s32>(core::position2d<s32>(0,0),
1934 core::dimension2di(texture->getOriginalSize())),
1935 NULL/*&AbsoluteClippingRect*/, colors, true);
1938 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
1939 errorstream << "\t" << spec.name << std::endl;
1946 for(u32 i=0; i<m_boxes.size(); i++)
1948 const BoxDrawSpec &spec = m_boxes[i];
1950 irr::video::SColor todraw = spec.color;
1952 todraw.setAlpha(140);
1954 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
1955 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
1957 driver->draw2DRectangle(todraw, rect, 0);
1962 for(u32 i=0; i<m_images.size(); i++)
1964 const ImageDrawSpec &spec = m_images[i];
1965 video::ITexture *texture = m_tsrc->getTexture(spec.name);
1968 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
1969 // Image size on screen
1970 core::rect<s32> imgrect;
1973 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
1976 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
1978 // Image rectangle on screen
1979 core::rect<s32> rect = imgrect + spec.pos;
1980 const video::SColor color(255,255,255,255);
1981 const video::SColor colors[] = {color,color,color,color};
1982 driver->draw2DImage(texture, rect,
1983 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
1984 NULL/*&AbsoluteClippingRect*/, colors, true);
1987 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
1988 errorstream << "\t" << spec.name << std::endl;
1995 for(u32 i=0; i<m_itemimages.size(); i++)
2000 const ImageDrawSpec &spec = m_itemimages[i];
2001 IItemDefManager *idef = m_gamedef->idef();
2003 item.deSerialize(spec.name, idef);
2004 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
2005 // Image size on screen
2006 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
2007 // Image rectangle on screen
2008 core::rect<s32> rect = imgrect + spec.pos;
2009 const video::SColor color(255,255,255,255);
2010 const video::SColor colors[] = {color,color,color,color};
2011 driver->draw2DImage(texture, rect,
2012 core::rect<s32>(core::position2d<s32>(0,0),
2013 core::dimension2di(texture->getOriginalSize())),
2014 NULL/*&AbsoluteClippingRect*/, colors, true);
2019 Phase 0: Item slot rectangles
2020 Phase 1: Item images; prepare tooltip
2023 for(int phase=start_phase; phase<=1; phase++)
2024 for(u32 i=0; i<m_inventorylists.size(); i++)
2026 drawList(m_inventorylists[i], phase);
2032 gui::IGUIElement::draw();
2035 Draw fields/buttons tooltips
2037 for(u32 i=0; i<m_fields.size(); i++)
2039 const FieldSpec &spec = m_fields[i];
2040 if (spec.tooltip != "")
2042 core::rect<s32> rect = spec.rect;
2043 if (rect.isPointInside(m_pointer))
2045 m_tooltip_element->setVisible(true);
2046 this->bringToFront(m_tooltip_element);
2047 m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
2048 s32 tooltip_x = m_pointer.X + 15;
2049 s32 tooltip_y = m_pointer.Y + 15;
2050 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
2051 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2052 m_tooltip_element->setRelativePosition(core::rect<s32>(
2053 core::position2d<s32>(tooltip_x, tooltip_y),
2054 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2060 Draw dragged item stack
2065 void GUIFormSpecMenu::updateSelectedItem()
2067 // If the selected stack has become empty for some reason, deselect it.
2068 // If the selected stack has become inaccessible, deselect it.
2069 // If the selected stack has become smaller, adjust m_selected_amount.
2070 ItemStack selected = verifySelectedItem();
2072 // WARNING: BLACK MAGIC
2073 // See if there is a stack suited for our current guess.
2074 // If such stack does not exist, clear the guess.
2075 if(m_selected_content_guess.name != "" &&
2076 selected.name == m_selected_content_guess.name &&
2077 selected.count == m_selected_content_guess.count){
2078 // Selected item fits the guess. Skip the black magic.
2080 else if(m_selected_content_guess.name != ""){
2082 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2083 const ListDrawSpec &s = m_inventorylists[i];
2084 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2087 InventoryList *list = inv->getList(s.listname);
2090 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2091 u32 item_i = i + s.start_item_i;
2092 if(item_i >= list->getSize())
2094 ItemStack stack = list->getItem(item_i);
2095 if(stack.name == m_selected_content_guess.name &&
2096 stack.count == m_selected_content_guess.count){
2098 infostream<<"Client: Changing selected content guess to "
2099 <<s.inventoryloc.dump()<<" "<<s.listname
2100 <<" "<<item_i<<std::endl;
2101 delete m_selected_item;
2102 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2103 m_selected_amount = stack.count;
2108 infostream<<"Client: Discarding selected content guess: "
2109 <<m_selected_content_guess.getItemString()<<std::endl;
2110 m_selected_content_guess.name = "";
2114 // If craftresult is nonempty and nothing else is selected, select it now.
2115 if(!m_selected_item)
2117 for(u32 i=0; i<m_inventorylists.size(); i++)
2119 const ListDrawSpec &s = m_inventorylists[i];
2120 if(s.listname == "craftpreview")
2122 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2123 InventoryList *list = inv->getList("craftresult");
2124 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2126 m_selected_item = new ItemSpec;
2127 m_selected_item->inventoryloc = s.inventoryloc;
2128 m_selected_item->listname = "craftresult";
2129 m_selected_item->i = 0;
2130 m_selected_amount = 0;
2131 m_selected_dragging = false;
2138 // If craftresult is selected, keep the whole stack selected
2139 if(m_selected_item && m_selected_item->listname == "craftresult")
2141 m_selected_amount = verifySelectedItem().count;
2145 ItemStack GUIFormSpecMenu::verifySelectedItem()
2147 // If the selected stack has become empty for some reason, deselect it.
2148 // If the selected stack has become inaccessible, deselect it.
2149 // If the selected stack has become smaller, adjust m_selected_amount.
2150 // Return the selected stack.
2154 if(m_selected_item->isValid())
2156 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2159 InventoryList *list = inv->getList(m_selected_item->listname);
2160 if(list && (u32) m_selected_item->i < list->getSize())
2162 ItemStack stack = list->getItem(m_selected_item->i);
2163 if(m_selected_amount > stack.count)
2164 m_selected_amount = stack.count;
2171 // selection was not valid
2172 delete m_selected_item;
2173 m_selected_item = NULL;
2174 m_selected_amount = 0;
2175 m_selected_dragging = false;
2180 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2184 std::map<std::string, std::string> fields;
2186 if (quitmode == quit_mode_accept) {
2187 fields["quit"] = "true";
2190 if (quitmode == quit_mode_cancel) {
2191 fields["quit"] = "true";
2192 m_text_dst->gotText(fields);
2196 if (current_keys_pending.key_down) {
2197 fields["key_down"] = "true";
2198 current_keys_pending.key_down = false;
2201 if (current_keys_pending.key_up) {
2202 fields["key_up"] = "true";
2203 current_keys_pending.key_up = false;
2206 if (current_keys_pending.key_enter) {
2207 fields["key_enter"] = "true";
2208 current_keys_pending.key_enter = false;
2211 if (current_keys_pending.key_escape) {
2212 fields["key_escape"] = "true";
2213 current_keys_pending.key_escape = false;
2216 for(u32 i=0; i<m_fields.size(); i++)
2218 const FieldSpec &s = m_fields[i];
2221 if(s.ftype == f_Button)
2223 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(s.flabel.c_str());
2225 else if(s.ftype == f_Table) {
2226 GUITable *table = getTable(s.fname);
2228 fields[wide_to_narrow(s.fname.c_str())]
2229 = table->checkEvent();
2232 else if(s.ftype == f_DropDown) {
2233 // no dynamic cast possible due to some distributions shipped
2234 // without rtti support in irrlicht
2235 IGUIElement * element = getElementFromId(s.fid);
2236 gui::IGUIComboBox *e = NULL;
2237 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2238 e = static_cast<gui::IGUIComboBox*>(element);
2240 s32 selected = e->getSelected();
2241 if (selected >= 0) {
2242 fields[wide_to_narrow(s.fname.c_str())] =
2243 wide_to_narrow(e->getItem(selected));
2246 else if (s.ftype == f_TabHeader) {
2247 // no dynamic cast possible due to some distributions shipped
2248 // without rtti support in irrlicht
2249 IGUIElement * element = getElementFromId(s.fid);
2250 gui::IGUITabControl *e = NULL;
2251 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2252 e = static_cast<gui::IGUITabControl*>(element);
2256 std::stringstream ss;
2257 ss << (e->getActiveTab() +1);
2258 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2261 else if (s.ftype == f_CheckBox) {
2262 // no dynamic cast possible due to some distributions shipped
2263 // without rtti support in irrlicht
2264 IGUIElement * element = getElementFromId(s.fid);
2265 gui::IGUICheckBox *e = NULL;
2266 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2267 e = static_cast<gui::IGUICheckBox*>(element);
2272 fields[wide_to_narrow(s.fname.c_str())] = "true";
2274 fields[wide_to_narrow(s.fname.c_str())] = "false";
2279 IGUIElement* e = getElementFromId(s.fid);
2282 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(e->getText());
2288 m_text_dst->gotText(fields);
2292 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2294 // Fix Esc/Return key being eaten by checkboxen and tables
2295 if(event.EventType==EET_KEY_INPUT_EVENT)
2297 KeyPress kp(event.KeyInput);
2298 if (kp == EscapeKey || kp == getKeySetting("keymap_inventory")
2299 || event.KeyInput.Key==KEY_RETURN)
2301 gui::IGUIElement *focused = Environment->getFocus();
2302 if (focused && isMyChild(focused) &&
2303 (focused->getType() == gui::EGUIET_LIST_BOX ||
2304 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2310 // Mouse wheel events: send to hovered element instead of focused
2311 if(event.EventType==EET_MOUSE_INPUT_EVENT
2312 && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
2314 s32 x = event.MouseInput.X;
2315 s32 y = event.MouseInput.Y;
2316 gui::IGUIElement *hovered =
2317 Environment->getRootGUIElement()->getElementFromPoint(
2318 core::position2d<s32>(x, y));
2319 if (hovered && isMyChild(hovered)) {
2320 hovered->OnEvent(event);
2327 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2329 if(event.EventType==EET_KEY_INPUT_EVENT)
2331 KeyPress kp(event.KeyInput);
2332 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
2333 kp == getKeySetting("keymap_inventory")))
2337 acceptInput(quit_mode_cancel);
2340 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
2344 if (event.KeyInput.PressedDown &&
2345 (event.KeyInput.Key==KEY_RETURN ||
2346 event.KeyInput.Key==KEY_UP ||
2347 event.KeyInput.Key==KEY_DOWN)
2351 switch (event.KeyInput.Key) {
2353 current_keys_pending.key_enter = true;
2356 current_keys_pending.key_up = true;
2359 current_keys_pending.key_down = true;
2363 //can't happen at all!
2364 assert("reached a source line that can't ever been reached" == 0);
2367 if (current_keys_pending.key_enter && m_allowclose) {
2368 acceptInput(quit_mode_accept);
2378 if(event.EventType==EET_MOUSE_INPUT_EVENT
2379 && event.MouseInput.Event != EMIE_MOUSE_MOVED)
2381 // Mouse event other than movement
2383 // Get selected item and hovered/clicked item (s)
2385 updateSelectedItem();
2386 ItemSpec s = getItemAtPos(m_pointer);
2388 Inventory *inv_selected = NULL;
2389 Inventory *inv_s = NULL;
2393 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
2394 assert(inv_selected);
2395 assert(inv_selected->getList(m_selected_item->listname) != NULL);
2402 inv_s = m_invmgr->getInventory(s.inventoryloc);
2405 errorstream<<"InventoryMenu: The selected inventory location "
2406 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2408 s.i = -1; // make it invalid again
2412 InventoryList *list = inv_s->getList(s.listname);
2414 verbosestream<<"InventoryMenu: The selected inventory list \""
2415 <<s.listname<<"\" does not exist"<<std::endl;
2416 s.i = -1; // make it invalid again
2420 if((u32)s.i >= list->getSize()){
2421 infostream<<"InventoryMenu: The selected inventory list \""
2422 <<s.listname<<"\" is too small (i="<<s.i<<", size="
2423 <<list->getSize()<<")"<<std::endl;
2424 s.i = -1; // make it invalid again
2428 s_count = list->getItem(s.i).count;
2431 bool identical = (m_selected_item != NULL) && s.isValid() &&
2432 (inv_selected == inv_s) &&
2433 (m_selected_item->listname == s.listname) &&
2434 (m_selected_item->i == s.i);
2436 // buttons: 0 = left, 1 = right, 2 = middle
2437 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
2440 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
2441 { button = 0; updown = 0; }
2442 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
2443 { button = 1; updown = 0; }
2444 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
2445 { button = 2; updown = 0; }
2446 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
2447 { button = 0; updown = 1; }
2448 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
2449 { button = 1; updown = 1; }
2450 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
2451 { button = 2; updown = 1; }
2453 // Set this number to a positive value to generate a move action
2454 // from m_selected_item to s.
2455 u32 move_amount = 0;
2457 // Set this number to a positive value to generate a drop action
2458 // from m_selected_item.
2459 u32 drop_amount = 0;
2461 // Set this number to a positive value to generate a craft action at s.
2462 u32 craft_amount = 0;
2466 // Some mouse button has been pressed
2468 //infostream<<"Mouse button "<<button<<" pressed at p=("
2469 // <<p.X<<","<<p.Y<<")"<<std::endl;
2471 m_selected_dragging = false;
2473 if(s.isValid() && s.listname == "craftpreview")
2475 // Craft preview has been clicked: craft
2476 craft_amount = (button == 2 ? 10 : 1);
2478 else if(m_selected_item == NULL)
2482 // Non-empty stack has been clicked: select it
2483 m_selected_item = new ItemSpec(s);
2485 if(button == 1) // right
2486 m_selected_amount = (s_count + 1) / 2;
2487 else if(button == 2) // middle
2488 m_selected_amount = MYMIN(s_count, 10);
2490 m_selected_amount = s_count;
2492 m_selected_dragging = true;
2495 else // m_selected_item != NULL
2497 assert(m_selected_amount >= 1);
2501 // Clicked a slot: move
2502 if(button == 1) // right
2504 else if(button == 2) // middle
2505 move_amount = MYMIN(m_selected_amount, 10);
2507 move_amount = m_selected_amount;
2511 if(move_amount >= m_selected_amount)
2512 m_selected_amount = 0;
2514 m_selected_amount -= move_amount;
2518 else if (!getAbsoluteClippingRect().isPointInside(m_pointer))
2520 // Clicked outside of the window: drop
2521 if(button == 1) // right
2523 else if(button == 2) // middle
2524 drop_amount = MYMIN(m_selected_amount, 10);
2526 drop_amount = m_selected_amount;
2530 else if(updown == 1)
2532 // Some mouse button has been released
2534 //infostream<<"Mouse button "<<button<<" released at p=("
2535 // <<p.X<<","<<p.Y<<")"<<std::endl;
2537 if(m_selected_item != NULL && m_selected_dragging && s.isValid())
2541 // Dragged to different slot: move all selected
2542 move_amount = m_selected_amount;
2545 else if(m_selected_item != NULL && m_selected_dragging &&
2546 !(getAbsoluteClippingRect().isPointInside(m_pointer)))
2548 // Dragged outside of window: drop all selected
2549 drop_amount = m_selected_amount;
2552 m_selected_dragging = false;
2555 // Possibly send inventory action to server
2558 // Send IACTION_MOVE
2560 assert(m_selected_item && m_selected_item->isValid());
2561 assert(s.isValid());
2563 assert(inv_selected && inv_s);
2564 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2565 InventoryList *list_to = inv_s->getList(s.listname);
2566 assert(list_from && list_to);
2567 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2568 ItemStack stack_to = list_to->getItem(s.i);
2570 // Check how many items can be moved
2571 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
2572 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
2573 // If source stack cannot be added to destination stack at all,
2575 if(leftover.count == stack_from.count && leftover.name == stack_from.name)
2577 m_selected_amount = stack_to.count;
2578 // In case the server doesn't directly swap them but instead
2579 // moves stack_to somewhere else, set this
2580 m_selected_content_guess = stack_to;
2581 m_selected_content_guess_inventory = s.inventoryloc;
2583 // Source stack goes fully into destination stack
2584 else if(leftover.empty())
2586 m_selected_amount -= move_amount;
2587 m_selected_content_guess = ItemStack(); // Clear
2589 // Source stack goes partly into destination stack
2592 move_amount -= leftover.count;
2593 m_selected_amount -= move_amount;
2594 m_selected_content_guess = ItemStack(); // Clear
2597 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
2598 IMoveAction *a = new IMoveAction();
2599 a->count = move_amount;
2600 a->from_inv = m_selected_item->inventoryloc;
2601 a->from_list = m_selected_item->listname;
2602 a->from_i = m_selected_item->i;
2603 a->to_inv = s.inventoryloc;
2604 a->to_list = s.listname;
2606 m_invmgr->inventoryAction(a);
2608 else if(drop_amount > 0)
2610 m_selected_content_guess = ItemStack(); // Clear
2612 // Send IACTION_DROP
2614 assert(m_selected_item && m_selected_item->isValid());
2615 assert(inv_selected);
2616 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2618 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2620 // Check how many items can be dropped
2621 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
2622 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
2623 m_selected_amount -= drop_amount;
2625 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
2626 IDropAction *a = new IDropAction();
2627 a->count = drop_amount;
2628 a->from_inv = m_selected_item->inventoryloc;
2629 a->from_list = m_selected_item->listname;
2630 a->from_i = m_selected_item->i;
2631 m_invmgr->inventoryAction(a);
2633 else if(craft_amount > 0)
2635 m_selected_content_guess = ItemStack(); // Clear
2637 // Send IACTION_CRAFT
2639 assert(s.isValid());
2642 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
2643 ICraftAction *a = new ICraftAction();
2644 a->count = craft_amount;
2645 a->craft_inv = s.inventoryloc;
2646 m_invmgr->inventoryAction(a);
2649 // If m_selected_amount has been decreased to zero, deselect
2650 if(m_selected_amount == 0)
2652 delete m_selected_item;
2653 m_selected_item = NULL;
2654 m_selected_amount = 0;
2655 m_selected_dragging = false;
2656 m_selected_content_guess = ItemStack();
2659 if(event.EventType==EET_GUI_EVENT)
2662 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
2665 // find the element that was clicked
2666 for(u32 i=0; i<m_fields.size(); i++)
2668 FieldSpec &s = m_fields[i];
2669 // if its a button, set the send field so
2670 // lua knows which button was pressed
2671 if ((s.ftype == f_TabHeader) && (s.fid == event.GUIEvent.Caller->getID()))
2680 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
2683 if(!canTakeFocus(event.GUIEvent.Element))
2685 infostream<<"GUIFormSpecMenu: Not allowing focus change."
2687 // Returning true disables focus change
2691 if((event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) ||
2692 (event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED))
2694 unsigned int btn_id = event.GUIEvent.Caller->getID();
2696 if (btn_id == 257) {
2698 acceptInput(quit_mode_accept);
2702 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2704 // quitMenu deallocates menu
2708 // find the element that was clicked
2709 for(u32 i=0; i<m_fields.size(); i++)
2711 FieldSpec &s = m_fields[i];
2712 // if its a button, set the send field so
2713 // lua knows which button was pressed
2714 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
2715 (s.fid == event.GUIEvent.Caller->getID()))
2720 acceptInput(quit_mode_accept);
2723 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2734 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
2736 if(event.GUIEvent.Caller->getID() > 257)
2740 acceptInput(quit_mode_accept);
2744 current_keys_pending.key_enter = true;
2747 // quitMenu deallocates menu
2752 if(event.GUIEvent.EventType==gui::EGET_TABLE_CHANGED)
2754 int current_id = event.GUIEvent.Caller->getID();
2755 if(current_id > 257)
2757 // find the element that was clicked
2758 for(u32 i=0; i<m_fields.size(); i++)
2760 FieldSpec &s = m_fields[i];
2761 // if it's a table, set the send field
2762 // so lua knows which table was changed
2763 if ((s.ftype == f_Table) && (s.fid == current_id))
2775 return Parent ? Parent->OnEvent(event) : false;
2778 bool GUIFormSpecMenu::parseColor(const std::string &value, video::SColor &color,
2781 const char *hexpattern = NULL;
2782 if (value[0] == '#') {
2783 if (value.size() == 9)
2784 hexpattern = "#RRGGBBAA";
2785 else if (value.size() == 7)
2786 hexpattern = "#RRGGBB";
2787 else if (value.size() == 5)
2788 hexpattern = "#RGBA";
2789 else if (value.size() == 4)
2790 hexpattern = "#RGB";
2794 assert(strlen(hexpattern) == value.size());
2795 video::SColor outcolor(255, 255, 255, 255);
2796 for (size_t pos = 0; pos < value.size(); ++pos) {
2797 // '#' in the pattern means skip that character
2798 if (hexpattern[pos] == '#')
2801 // Else assume hexpattern[pos] is one of 'R' 'G' 'B' 'A'
2802 // Read one or two digits, depending on hexpattern
2803 unsigned char c1, c2;
2804 if (hexpattern[pos+1] == hexpattern[pos]) {
2805 // Two digits, e.g. hexpattern == "#RRGGBB"
2806 if (!hex_digit_decode(value[pos], c1) ||
2807 !hex_digit_decode(value[pos+1], c2))
2812 // One digit, e.g. hexpattern == "#RGB"
2813 if (!hex_digit_decode(value[pos], c1))
2817 u32 colorpart = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
2819 // Update outcolor with newly read color part
2820 if (hexpattern[pos] == 'R')
2821 outcolor.setRed(colorpart);
2822 else if (hexpattern[pos] == 'G')
2823 outcolor.setGreen(colorpart);
2824 else if (hexpattern[pos] == 'B')
2825 outcolor.setBlue(colorpart);
2826 else if (hexpattern[pos] == 'A')
2827 outcolor.setAlpha(colorpart);
2834 // Optionally, named colors could be implemented here
2838 errorstream<<"Invalid color: \""<<value<<"\""<<std::endl;