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) :
72 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
79 m_selected_item(NULL),
81 m_selected_dragging(false),
82 m_tooltip_element(NULL),
86 current_keys_pending.key_down = false;
87 current_keys_pending.key_up = false;
88 current_keys_pending.key_enter = false;
89 current_keys_pending.key_escape = false;
93 GUIFormSpecMenu::~GUIFormSpecMenu()
97 delete m_selected_item;
102 void GUIFormSpecMenu::removeChildren()
104 const core::list<gui::IGUIElement*> &children = getChildren();
105 core::list<gui::IGUIElement*> children_copy;
106 for(core::list<gui::IGUIElement*>::ConstIterator
107 i = children.begin(); i != children.end(); i++)
109 children_copy.push_back(*i);
111 for(core::list<gui::IGUIElement*>::Iterator
112 i = children_copy.begin();
113 i != children_copy.end(); i++)
118 gui::IGUIElement *e = getElementFromId(256);
123 if(m_tooltip_element)
125 m_tooltip_element->remove();
126 m_tooltip_element->drop();
127 m_tooltip_element = NULL;
132 void GUIFormSpecMenu::setInitialFocus()
134 // Set initial focus according to following order of precedence:
135 // 1. first empty editbox
139 // 5. first focusable (not statictext, not tabheader)
140 // 6. first child element
142 core::list<gui::IGUIElement*> children = getChildren();
144 // in case "children" contains any NULL elements, remove them
145 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
146 it != children.end();) {
150 it = children.erase(it);
153 // 1. first empty editbox
154 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
155 it != children.end(); ++it) {
156 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
157 && (*it)->getText()[0] == 0) {
158 Environment->setFocus(*it);
164 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
165 it != children.end(); ++it) {
166 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
167 Environment->setFocus(*it);
173 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
174 it != children.end(); ++it) {
175 if ((*it)->getTypeName() == std::string("GUITable")) {
176 Environment->setFocus(*it);
182 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
183 it != children.end(); --it) {
184 if ((*it)->getType() == gui::EGUIET_BUTTON) {
185 Environment->setFocus(*it);
190 // 5. first focusable (not statictext, not tabheader)
191 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
192 it != children.end(); ++it) {
193 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
194 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
195 Environment->setFocus(*it);
200 // 6. first child element
201 if (children.empty())
202 Environment->setFocus(this);
204 Environment->setFocus(*(children.begin()));
207 GUITable* GUIFormSpecMenu::getTable(std::wstring tablename)
209 for (u32 i = 0; i < m_tables.size(); ++i) {
210 if (tablename == m_tables[i].first.fname)
211 return m_tables[i].second;
216 std::vector<std::string> split(const std::string &s, char delim) {
217 std::vector<std::string> tokens;
219 std::string current = "";
220 bool last_was_escape = false;
221 for(unsigned int i=0; i < s.size(); i++) {
222 if (last_was_escape) {
224 current += s.c_str()[i];
225 last_was_escape = false;
228 if (s.c_str()[i] == delim) {
229 tokens.push_back(current);
231 last_was_escape = false;
233 else if (s.c_str()[i] == '\\'){
234 last_was_escape = true;
237 current += s.c_str()[i];
238 last_was_escape = false;
243 tokens.push_back(current);
248 void GUIFormSpecMenu::parseSize(parserData* data,std::string element)
250 std::vector<std::string> parts = split(element,',');
252 if ((parts.size() == 2) || parts.size() == 3) {
255 if (parts[1].find(';') != std::string::npos)
256 parts[1] = parts[1].substr(0,parts[1].find(';'));
258 invsize.X = stof(parts[0]);
259 invsize.Y = stof(parts[1]);
262 if (parts.size() == 3) {
263 if (parts[2] == "true") {
264 lockSize(true,v2u32(800,600));
269 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
270 v2u32 delta = current_screensize - m_lockscreensize;
272 if (current_screensize.Y > m_lockscreensize.Y)
277 if (current_screensize.X > m_lockscreensize.X)
282 offset = v2s32(delta.X,delta.Y);
284 data->screensize = m_lockscreensize;
290 padding = v2s32(data->screensize.Y/40, data->screensize.Y/40);
291 spacing = v2s32(data->screensize.Y/12, data->screensize.Y/13);
292 imgsize = v2s32(data->screensize.Y/15, data->screensize.Y/15);
294 padding.X*2+spacing.X*(invsize.X-1.0)+imgsize.X,
295 padding.Y*2+spacing.Y*(invsize.Y-1.0)+imgsize.Y + (data->helptext_h-5)
297 data->rect = core::rect<s32>(
298 data->screensize.X/2 - data->size.X/2 + offset.X,
299 data->screensize.Y/2 - data->size.Y/2 + offset.Y,
300 data->screensize.X/2 + data->size.X/2 + offset.X,
301 data->screensize.Y/2 + data->size.Y/2 + offset.Y
304 DesiredRect = data->rect;
305 recalculateAbsolutePosition(false);
306 data->basepos = getBasePos();
310 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
313 void GUIFormSpecMenu::parseList(parserData* data,std::string element)
315 if (m_gamedef == 0) {
316 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
320 std::vector<std::string> parts = split(element,';');
322 if ((parts.size() == 4) || (parts.size() == 5)) {
323 std::string location = parts[0];
324 std::string listname = parts[1];
325 std::vector<std::string> v_pos = split(parts[2],',');
326 std::vector<std::string> v_geom = split(parts[3],',');
327 std::string startindex = "";
328 if (parts.size() == 5)
329 startindex = parts[4];
331 MY_CHECKPOS("list",2);
332 MY_CHECKGEOM("list",3);
334 InventoryLocation loc;
336 if(location == "context" || location == "current_name")
337 loc = m_current_inventory_location;
339 loc.deSerialize(location);
341 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
342 pos.X += stof(v_pos[0]) * (float)spacing.X;
343 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
346 geom.X = stoi(v_geom[0]);
347 geom.Y = stoi(v_geom[1]);
351 start_i = stoi(startindex);
353 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
354 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
358 if(data->bp_set != 2)
359 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
360 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
363 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
366 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
368 std::vector<std::string> parts = split(element,';');
370 if ((parts.size() == 3) || (parts.size() == 4)) {
371 std::vector<std::string> v_pos = split(parts[0],',');
372 std::string name = parts[1];
373 std::string label = parts[2];
374 std::string selected = "";
376 if (parts.size() == 4)
379 MY_CHECKPOS("checkbox",0);
382 pos.X += stof(v_pos[0]) * (float) spacing.X;
383 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
385 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
387 bool fselected = false;
389 if (selected == "true")
392 std::wstring wlabel = narrow_to_wide(label.c_str());
394 FieldSpec spec = FieldSpec(
395 narrow_to_wide(name.c_str()),
401 spec.ftype = f_CheckBox;
402 spec.flabel = wlabel; //Needed for displaying text on MSVC
403 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
404 spec.fid, spec.flabel.c_str());
406 if (spec.fname == data->focused_fieldname) {
407 Environment->setFocus(e);
410 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
411 m_fields.push_back(spec);
414 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
417 void GUIFormSpecMenu::parseImage(parserData* data,std::string element)
419 std::vector<std::string> parts = split(element,';');
421 if (parts.size() == 3) {
422 std::vector<std::string> v_pos = split(parts[0],',');
423 std::vector<std::string> v_geom = split(parts[1],',');
424 std::string name = unescape_string(parts[2]);
426 MY_CHECKPOS("image",0);
427 MY_CHECKGEOM("image",1);
429 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
430 pos.X += stof(v_pos[0]) * (float) spacing.X;
431 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
434 geom.X = stof(v_geom[0]) * (float)imgsize.X;
435 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
437 if(data->bp_set != 2)
438 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
439 m_images.push_back(ImageDrawSpec(name, pos, geom));
443 if (parts.size() == 2) {
444 std::vector<std::string> v_pos = split(parts[0],',');
445 std::string name = unescape_string(parts[1]);
447 MY_CHECKPOS("image",0);
449 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
450 pos.X += stof(v_pos[0]) * (float) spacing.X;
451 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
453 if(data->bp_set != 2)
454 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
455 m_images.push_back(ImageDrawSpec(name, pos));
458 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
461 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element)
463 std::vector<std::string> parts = split(element,';');
465 if (parts.size() == 3) {
466 std::vector<std::string> v_pos = split(parts[0],',');
467 std::vector<std::string> v_geom = split(parts[1],',');
468 std::string name = parts[2];
470 MY_CHECKPOS("itemimage",0);
471 MY_CHECKGEOM("itemimage",1);
473 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
474 pos.X += stof(v_pos[0]) * (float) spacing.X;
475 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
478 geom.X = stoi(v_geom[0]) * (float)imgsize.X;
479 geom.Y = stoi(v_geom[1]) * (float)imgsize.Y;
481 if(data->bp_set != 2)
482 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
483 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
486 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
489 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
492 std::vector<std::string> parts = split(element,';');
494 if (parts.size() == 4) {
495 std::vector<std::string> v_pos = split(parts[0],',');
496 std::vector<std::string> v_geom = split(parts[1],',');
497 std::string name = parts[2];
498 std::string label = parts[3];
500 MY_CHECKPOS("button",0);
501 MY_CHECKGEOM("button",1);
504 pos.X += stof(v_pos[0]) * (float)spacing.X;
505 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
508 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
509 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
511 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y-15, pos.X+geom.X, pos.Y+15);
513 if(data->bp_set != 2)
514 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
516 label = unescape_string(label);
518 std::wstring wlabel = narrow_to_wide(label.c_str());
520 FieldSpec spec = FieldSpec(
521 narrow_to_wide(name.c_str()),
526 spec.ftype = f_Button;
527 if(type == "button_exit")
530 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
531 spec.flabel.c_str());
533 if (spec.fname == data->focused_fieldname) {
534 Environment->setFocus(e);
537 m_fields.push_back(spec);
540 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
543 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element)
545 std::vector<std::string> parts = split(element,';');
547 if ((parts.size() == 3) || (parts.size() == 4)) {
548 std::vector<std::string> v_pos = split(parts[0],',');
549 std::vector<std::string> v_geom = split(parts[1],',');
550 std::string name = unescape_string(parts[2]);
552 MY_CHECKPOS("background",0);
553 MY_CHECKGEOM("background",1);
555 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
556 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
557 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
560 geom.X = stof(v_geom[0]) * (float)spacing.X;
561 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
563 if (parts.size() == 4) {
564 m_clipbackground = is_yes(parts[3]);
565 if (m_clipbackground) {
566 pos.X = stoi(v_pos[0]); //acts as offset
567 pos.Y = stoi(v_pos[1]); //acts as offset
571 if(data->bp_set != 2)
572 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
573 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
576 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
579 void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element)
581 std::vector<std::string> parts = split(element,';');
583 data->table_options.clear();
584 for (size_t i = 0; i < parts.size(); ++i) {
585 // Parse table option
586 std::string opt = unescape_string(parts[i]);
587 data->table_options.push_back(GUITable::splitOption(opt));
591 void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element)
593 std::vector<std::string> parts = split(element,';');
595 data->table_columns.clear();
596 for (size_t i = 0; i < parts.size(); ++i) {
597 std::vector<std::string> col_parts = split(parts[i],',');
598 GUITable::TableColumn column;
600 if (!col_parts.empty())
601 column.type = col_parts[0];
602 // Parse column options
603 for (size_t j = 1; j < col_parts.size(); ++j) {
604 std::string opt = unescape_string(col_parts[j]);
605 column.options.push_back(GUITable::splitOption(opt));
607 data->table_columns.push_back(column);
611 void GUIFormSpecMenu::parseTable(parserData* data,std::string element)
613 std::vector<std::string> parts = split(element,';');
615 if ((parts.size() == 4) || (parts.size() == 5)) {
616 std::vector<std::string> v_pos = split(parts[0],',');
617 std::vector<std::string> v_geom = split(parts[1],',');
618 std::string name = parts[2];
619 std::vector<std::string> items = split(parts[3],',');
620 std::string str_initial_selection = "";
621 std::string str_transparent = "false";
623 if (parts.size() >= 5)
624 str_initial_selection = parts[4];
626 MY_CHECKPOS("table",0);
627 MY_CHECKGEOM("table",1);
630 pos.X += stof(v_pos[0]) * (float)spacing.X;
631 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
634 geom.X = stof(v_geom[0]) * (float)spacing.X;
635 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
638 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
640 std::wstring fname_w = narrow_to_wide(name.c_str());
642 FieldSpec spec = FieldSpec(
649 spec.ftype = f_Table;
651 for (unsigned int i = 0; i < items.size(); ++i) {
652 items[i] = unescape_string(items[i]);
655 //now really show table
656 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
658 e->drop(); // IGUIElement maintains the remaining reference
660 if (spec.fname == data->focused_fieldname) {
661 Environment->setFocus(e);
664 e->setTable(data->table_options, data->table_columns, items);
666 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
667 e->setDynamicData(data->table_dyndata[fname_w]);
670 if ((str_initial_selection != "") &&
671 (str_initial_selection != "0"))
672 e->setSelected(stoi(str_initial_selection.c_str()));
674 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
675 m_fields.push_back(spec);
678 errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
681 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element)
683 std::vector<std::string> parts = split(element,';');
685 if ((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) {
686 std::vector<std::string> v_pos = split(parts[0],',');
687 std::vector<std::string> v_geom = split(parts[1],',');
688 std::string name = parts[2];
689 std::vector<std::string> items = split(parts[3],',');
690 std::string str_initial_selection = "";
691 std::string str_transparent = "false";
693 if (parts.size() >= 5)
694 str_initial_selection = parts[4];
696 if (parts.size() >= 6)
697 str_transparent = parts[5];
699 MY_CHECKPOS("textlist",0);
700 MY_CHECKGEOM("textlist",1);
703 pos.X += stof(v_pos[0]) * (float)spacing.X;
704 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
707 geom.X = stof(v_geom[0]) * (float)spacing.X;
708 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
711 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
713 std::wstring fname_w = narrow_to_wide(name.c_str());
715 FieldSpec spec = FieldSpec(
722 spec.ftype = f_Table;
724 for (unsigned int i = 0; i < items.size(); ++i) {
725 items[i] = unescape_string(items[i]);
728 //now really show list
729 GUITable *e = new GUITable(Environment, this, spec.fid, rect,
731 e->drop(); // IGUIElement maintains the remaining reference
733 if (spec.fname == data->focused_fieldname) {
734 Environment->setFocus(e);
737 e->setTextList(items, is_yes(str_transparent));
739 if (data->table_dyndata.find(fname_w) != data->table_dyndata.end()) {
740 e->setDynamicData(data->table_dyndata[fname_w]);
743 if ((str_initial_selection != "") &&
744 (str_initial_selection != "0"))
745 e->setSelected(stoi(str_initial_selection.c_str()));
747 m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
748 m_fields.push_back(spec);
751 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
755 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
757 std::vector<std::string> parts = split(element,';');
759 if (parts.size() == 5) {
760 std::vector<std::string> v_pos = split(parts[0],',');
761 std::string name = parts[2];
762 std::vector<std::string> items = split(parts[3],',');
763 std::string str_initial_selection = "";
764 str_initial_selection = parts[4];
766 MY_CHECKPOS("dropdown",0);
769 pos.X += stof(v_pos[0]) * (float)spacing.X;
770 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
772 s32 width = stof(parts[1]) * (float)spacing.Y;
774 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+width, pos.Y+30);
776 std::wstring fname_w = narrow_to_wide(name.c_str());
778 FieldSpec spec = FieldSpec(
785 spec.ftype = f_DropDown;
788 //now really show list
789 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
791 if (spec.fname == data->focused_fieldname) {
792 Environment->setFocus(e);
795 for (unsigned int i=0; i < items.size(); i++) {
796 e->addItem(narrow_to_wide(items[i]).c_str());
799 if (str_initial_selection != "")
800 e->setSelected(stoi(str_initial_selection.c_str())-1);
802 m_fields.push_back(spec);
805 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
806 << element << "'" << std::endl;
809 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
811 std::vector<std::string> parts = split(element,';');
813 if (parts.size() == 4) {
814 std::vector<std::string> v_pos = split(parts[0],',');
815 std::vector<std::string> v_geom = split(parts[1],',');
816 std::string name = parts[2];
817 std::string label = parts[3];
819 MY_CHECKPOS("pwdfield",0);
820 MY_CHECKGEOM("pwdfield",1);
823 pos.X += stof(v_pos[0]) * (float)spacing.X;
824 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
827 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
829 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
833 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
835 label = unescape_string(label);
837 std::wstring wlabel = narrow_to_wide(label.c_str());
839 FieldSpec spec = FieldSpec(
840 narrow_to_wide(name.c_str()),
847 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
849 if (spec.fname == data->focused_fieldname) {
850 Environment->setFocus(e);
853 if (label.length() >= 1)
855 rect.UpperLeftCorner.Y -= 15;
856 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
857 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
860 e->setPasswordBox(true,L'*');
863 evt.EventType = EET_KEY_INPUT_EVENT;
864 evt.KeyInput.Key = KEY_END;
865 evt.KeyInput.Char = 0;
866 evt.KeyInput.Control = 0;
867 evt.KeyInput.Shift = 0;
868 evt.KeyInput.PressedDown = true;
870 m_fields.push_back(spec);
873 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
876 void GUIFormSpecMenu::parseSimpleField(parserData* data,
877 std::vector<std::string> &parts)
879 std::string name = parts[0];
880 std::string label = parts[1];
881 std::string default_val = parts[2];
883 core::rect<s32> rect;
887 rect = core::rect<s32>(
888 data->screensize.X/2 - 580/2,
889 data->screensize.Y/2 - 300/2,
890 data->screensize.X/2 + 580/2,
891 data->screensize.Y/2 + 300/2
894 recalculateAbsolutePosition(false);
895 data->basepos = getBasePos();
898 else if(data->bp_set == 2)
899 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
901 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
902 pos.Y = ((m_fields.size()+2)*60);
903 v2s32 size = DesiredRect.getSize();
905 rect = core::rect<s32>(size.X/2-150, pos.Y, (size.X/2-150)+300, pos.Y+30);
909 default_val = m_form_src->resolveText(default_val);
911 default_val = unescape_string(default_val);
912 label = unescape_string(label);
914 std::wstring wlabel = narrow_to_wide(label.c_str());
916 FieldSpec spec = FieldSpec(
917 narrow_to_wide(name.c_str()),
919 narrow_to_wide(default_val.c_str()),
925 // spec field id to 0, this stops submit searching for a value that isn't there
926 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
931 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
933 if (spec.fname == data->focused_fieldname) {
934 Environment->setFocus(e);
938 evt.EventType = EET_KEY_INPUT_EVENT;
939 evt.KeyInput.Key = KEY_END;
940 evt.KeyInput.Char = 0;
941 evt.KeyInput.Control = 0;
942 evt.KeyInput.Shift = 0;
943 evt.KeyInput.PressedDown = true;
946 if (label.length() >= 1)
948 rect.UpperLeftCorner.Y -= 15;
949 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
950 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
954 m_fields.push_back(spec);
957 void GUIFormSpecMenu::parseTextArea(parserData* data,
958 std::vector<std::string>& parts,std::string type)
961 std::vector<std::string> v_pos = split(parts[0],',');
962 std::vector<std::string> v_geom = split(parts[1],',');
963 std::string name = parts[2];
964 std::string label = parts[3];
965 std::string default_val = parts[4];
968 MY_CHECKGEOM(type,1);
971 pos.X = stof(v_pos[0]) * (float) spacing.X;
972 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
976 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
978 if (type == "textarea")
980 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
985 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
990 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
992 if(data->bp_set != 2)
993 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
996 default_val = m_form_src->resolveText(default_val);
999 default_val = unescape_string(default_val);
1000 label = unescape_string(label);
1002 std::wstring wlabel = narrow_to_wide(label.c_str());
1004 FieldSpec spec = FieldSpec(
1005 narrow_to_wide(name.c_str()),
1007 narrow_to_wide(default_val.c_str()),
1013 // spec field id to 0, this stops submit searching for a value that isn't there
1014 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1019 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1021 if (spec.fname == data->focused_fieldname) {
1022 Environment->setFocus(e);
1025 if (type == "textarea")
1027 e->setMultiLine(true);
1028 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1031 evt.EventType = EET_KEY_INPUT_EVENT;
1032 evt.KeyInput.Key = KEY_END;
1033 evt.KeyInput.Char = 0;
1034 evt.KeyInput.Control = 0;
1035 evt.KeyInput.Shift = 0;
1036 evt.KeyInput.PressedDown = true;
1040 if (label.length() >= 1)
1042 rect.UpperLeftCorner.Y -= 15;
1043 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
1044 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1047 m_fields.push_back(spec);
1050 void GUIFormSpecMenu::parseField(parserData* data,std::string element,
1053 std::vector<std::string> parts = split(element,';');
1055 if (parts.size() == 3) {
1056 parseSimpleField(data,parts);
1060 if (parts.size() == 5) {
1061 parseTextArea(data,parts,type);
1064 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1067 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element)
1069 std::vector<std::string> parts = split(element,';');
1071 if (parts.size() == 2) {
1072 std::vector<std::string> v_pos = split(parts[0],',');
1073 std::string text = parts[1];
1075 MY_CHECKPOS("label",0);
1077 v2s32 pos = padding;
1078 pos.X += stof(v_pos[0]) * (float)spacing.X;
1079 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1081 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
1083 if(data->bp_set != 2)
1084 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1086 text = unescape_string(text);
1088 std::wstring wlabel = narrow_to_wide(text.c_str());
1090 FieldSpec spec = FieldSpec(
1096 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1097 m_fields.push_back(spec);
1100 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1103 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element)
1105 std::vector<std::string> parts = split(element,';');
1107 if (parts.size() == 2) {
1108 std::vector<std::string> v_pos = split(parts[0],',');
1109 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1111 MY_CHECKPOS("vertlabel",1);
1113 v2s32 pos = padding;
1114 pos.X += stof(v_pos[0]) * (float)spacing.X;
1115 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1117 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+15, pos.Y+300);
1119 if(data->bp_set != 2)
1120 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1122 std::wstring label = L"";
1124 for (unsigned int i=0; i < text.length(); i++) {
1129 FieldSpec spec = FieldSpec(
1135 gui::IGUIStaticText *t =
1136 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1137 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1138 m_fields.push_back(spec);
1141 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1144 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
1147 std::vector<std::string> parts = split(element,';');
1149 if ((parts.size() == 5) || (parts.size() == 7) || (parts.size() == 8)) {
1150 std::vector<std::string> v_pos = split(parts[0],',');
1151 std::vector<std::string> v_geom = split(parts[1],',');
1152 std::string image_name = parts[2];
1153 std::string name = parts[3];
1154 std::string label = parts[4];
1156 MY_CHECKPOS("imagebutton",0);
1157 MY_CHECKGEOM("imagebutton",1);
1159 v2s32 pos = padding;
1160 pos.X += stof(v_pos[0]) * (float)spacing.X;
1161 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1163 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1164 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1166 bool noclip = false;
1167 bool drawborder = true;
1169 if ((parts.size() >= 7)) {
1170 if (parts[5] == "true")
1173 if (parts[6] == "false")
1177 std::string pressed_image_name = "";
1179 if ((parts.size() == 8)) {
1180 pressed_image_name = parts[7];
1183 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1185 if(data->bp_set != 2)
1186 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1188 image_name = unescape_string(image_name);
1189 pressed_image_name = unescape_string(pressed_image_name);
1190 label = unescape_string(label);
1192 std::wstring wlabel = narrow_to_wide(label.c_str());
1194 FieldSpec spec = FieldSpec(
1195 narrow_to_wide(name.c_str()),
1197 narrow_to_wide(image_name.c_str()),
1200 spec.ftype = f_Button;
1201 if(type == "image_button_exit")
1202 spec.is_exit = true;
1204 video::ITexture *texture = 0;
1205 video::ITexture *pressed_texture = 0;
1206 texture = m_tsrc->getTexture(image_name);
1207 if (parts.size() == 8)
1208 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1210 pressed_texture = texture;
1212 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1214 if (spec.fname == data->focused_fieldname) {
1215 Environment->setFocus(e);
1218 e->setUseAlphaChannel(true);
1219 e->setImage(texture);
1220 e->setPressedImage(pressed_texture);
1221 e->setScaleImage(true);
1222 e->setNotClipped(noclip);
1223 e->setDrawBorder(drawborder);
1225 m_fields.push_back(spec);
1229 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1232 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element)
1234 std::vector<std::string> parts = split(element,';');
1236 if ((parts.size() == 4) || (parts.size() == 6)) {
1237 std::vector<std::string> v_pos = split(parts[0],',');
1238 std::string name = parts[1];
1239 std::vector<std::string> buttons = split(parts[2],',');
1240 std::string str_index = parts[3];
1241 bool show_background = true;
1242 bool show_border = true;
1243 int tab_index = stoi(str_index) -1;
1245 MY_CHECKPOS("tabheader",0);
1247 if (parts.size() == 6) {
1248 if (parts[4] == "true")
1249 show_background = false;
1250 if (parts[5] == "false")
1251 show_border = false;
1254 FieldSpec spec = FieldSpec(
1255 narrow_to_wide(name.c_str()),
1261 spec.ftype = f_TabHeader;
1263 v2s32 pos = padding;
1264 pos.X += stof(v_pos[0]) * (float)spacing.X;
1265 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1267 geom.X = data->screensize.Y;
1270 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1272 gui::IGUITabControl *e = Environment->addTabControl(rect,this,show_background,show_border,spec.fid);
1274 if (spec.fname == data->focused_fieldname) {
1275 Environment->setFocus(e);
1278 e->setNotClipped(true);
1280 for (unsigned int i=0; i< buttons.size(); i++) {
1281 wchar_t* wbutton = 0;
1283 std::wstring wlabel = narrow_to_wide(buttons[i]); //Needed for displaying text on windows
1284 wbutton = (wchar_t*) wlabel.c_str();
1286 e->addTab(wbutton,-1);
1289 if ((tab_index >= 0) &&
1290 (buttons.size() < INT_MAX) &&
1291 (tab_index < (int) buttons.size()))
1292 e->setActiveTab(tab_index);
1294 m_fields.push_back(spec);
1297 errorstream<< "Invalid TabHeader element(" << parts.size() << "): '" << element << "'" << std::endl;
1300 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
1303 if (m_gamedef == 0) {
1304 errorstream<<"WARNING: invalid use of item_image_button with m_gamedef==0"<<std::endl;
1308 std::vector<std::string> parts = split(element,';');
1310 if (parts.size() == 5) {
1311 std::vector<std::string> v_pos = split(parts[0],',');
1312 std::vector<std::string> v_geom = split(parts[1],',');
1313 std::string item_name = parts[2];
1314 std::string name = parts[3];
1315 std::string label = parts[4];
1317 MY_CHECKPOS("itemimagebutton",0);
1318 MY_CHECKGEOM("itemimagebutton",1);
1320 v2s32 pos = padding;
1321 pos.X += stof(v_pos[0]) * (float)spacing.X;
1322 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1324 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1325 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1327 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1329 if(data->bp_set != 2)
1330 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1332 IItemDefManager *idef = m_gamedef->idef();
1334 item.deSerialize(item_name, idef);
1335 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1336 std::string tooltip = item.getDefinition(idef).description;
1338 label = unescape_string(label);
1339 FieldSpec spec = FieldSpec(
1340 narrow_to_wide(name.c_str()),
1341 narrow_to_wide(label.c_str()),
1342 narrow_to_wide(item_name.c_str()),
1346 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1348 if (spec.fname == data->focused_fieldname) {
1349 Environment->setFocus(e);
1352 e->setUseAlphaChannel(true);
1353 e->setImage(texture);
1354 e->setPressedImage(texture);
1355 e->setScaleImage(true);
1356 spec.ftype = f_Button;
1357 rect+=data->basepos-padding;
1360 spec.tooltip=tooltip;
1361 m_fields.push_back(spec);
1364 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1367 void GUIFormSpecMenu::parseBox(parserData* data,std::string element)
1369 std::vector<std::string> parts = split(element,';');
1371 if (parts.size() == 3) {
1372 std::vector<std::string> v_pos = split(parts[0],',');
1373 std::vector<std::string> v_geom = split(parts[1],',');
1375 MY_CHECKPOS("box",0);
1376 MY_CHECKGEOM("box",1);
1378 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1379 pos.X += stof(v_pos[0]) * (float) spacing.X;
1380 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1383 geom.X = stof(v_geom[0]) * (float)spacing.X;
1384 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1386 video::SColor tmp_color;
1388 if (parseColor(parts[2], tmp_color, false)) {
1389 BoxDrawSpec spec(pos, geom, tmp_color);
1391 m_boxes.push_back(spec);
1394 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1398 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1401 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element)
1403 std::vector<std::string> parts = split(element,';');
1405 if ((parts.size() == 1) || (parts.size() == 2)) {
1406 parseColor(parts[0],m_bgcolor,false);
1408 if (parts.size() == 2) {
1409 std::string fullscreen = parts[1];
1410 m_bgfullscreen = is_yes(fullscreen);
1414 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1417 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element)
1419 std::vector<std::string> parts = split(element,';');
1421 if ((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) {
1422 parseColor(parts[0], m_slotbg_n, false);
1423 parseColor(parts[1], m_slotbg_h, false);
1425 if (parts.size() >= 3) {
1426 if (parseColor(parts[2], m_slotbordercolor, false)) {
1427 m_slotborder = true;
1430 if (parts.size() == 5) {
1431 video::SColor tmp_color;
1433 if (parseColor(parts[3], tmp_color, false))
1434 m_tooltip_element->setBackgroundColor(tmp_color);
1435 if (parseColor(parts[4], tmp_color, false))
1436 m_tooltip_element->setOverrideColor(tmp_color);
1440 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1443 void GUIFormSpecMenu::parseElement(parserData* data,std::string element)
1449 std::vector<std::string> parts = split(element,'[');
1451 // ugly workaround to keep compatibility
1452 if (parts.size() > 2) {
1453 if (trim(parts[0]) == "image") {
1454 for (unsigned int i=2;i< parts.size(); i++) {
1455 parts[1] += "[" + parts[i];
1461 if (parts.size() < 2) {
1465 std::string type = trim(parts[0]);
1466 std::string description = trim(parts[1]);
1468 if ((type == "size") || (type == "invsize")){
1469 parseSize(data,description);
1473 if (type == "list") {
1474 parseList(data,description);
1478 if (type == "checkbox") {
1479 parseCheckbox(data,description);
1483 if (type == "image") {
1484 parseImage(data,description);
1488 if (type == "item_image") {
1489 parseItemImage(data,description);
1493 if ((type == "button") || (type == "button_exit")) {
1494 parseButton(data,description,type);
1498 if (type == "background") {
1499 parseBackground(data,description);
1503 if (type == "tableoptions"){
1504 parseTableOptions(data,description);
1508 if (type == "tablecolumns"){
1509 parseTableColumns(data,description);
1513 if (type == "table"){
1514 parseTable(data,description);
1518 if (type == "textlist"){
1519 parseTextList(data,description);
1523 if (type == "dropdown"){
1524 parseDropDown(data,description);
1528 if (type == "pwdfield") {
1529 parsePwdField(data,description);
1533 if ((type == "field") || (type == "textarea")){
1534 parseField(data,description,type);
1538 if (type == "label") {
1539 parseLabel(data,description);
1543 if (type == "vertlabel") {
1544 parseVertLabel(data,description);
1548 if (type == "item_image_button") {
1549 parseItemImageButton(data,description);
1553 if ((type == "image_button") || (type == "image_button_exit")) {
1554 parseImageButton(data,description,type);
1558 if (type == "tabheader") {
1559 parseTabHeader(data,description);
1563 if (type == "box") {
1564 parseBox(data,description);
1568 if (type == "bgcolor") {
1569 parseBackgroundColor(data,description);
1573 if (type == "listcolors") {
1574 parseListColors(data,description);
1580 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1586 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1591 for (u32 i = 0; i < m_tables.size(); ++i) {
1592 std::wstring tablename = m_tables[i].first.fname;
1593 GUITable *table = m_tables[i].second;
1594 mydata.table_dyndata[tablename] = table->getDynamicData();
1598 gui::IGUIElement *focused_element = Environment->getFocus();
1599 if (focused_element && focused_element->getParent() == this) {
1600 s32 focused_id = focused_element->getID();
1601 if (focused_id > 257) {
1602 for (u32 i=0; i<m_fields.size(); i++) {
1603 if (m_fields[i].fid == focused_id) {
1604 mydata.focused_fieldname =
1615 mydata.size= v2s32(100,100);
1616 mydata.helptext_h = 15;
1617 mydata.screensize = screensize;
1619 // Base position of contents of form
1620 mydata.basepos = getBasePos();
1622 // State of basepos, 0 = not set, 1= set by formspec, 2 = set by size[] element
1623 // Used to adjust form size automatically if needed
1624 // A proceed button is added if there is no size[] element
1628 /* Convert m_init_draw_spec to m_inventorylists */
1630 m_inventorylists.clear();
1632 m_backgrounds.clear();
1633 m_itemimages.clear();
1635 m_checkboxes.clear();
1639 // Set default values (fits old formspec values)
1640 m_bgcolor = video::SColor(140,0,0,0);
1641 m_bgfullscreen = false;
1643 m_slotbg_n = video::SColor(255,128,128,128);
1644 m_slotbg_h = video::SColor(255,192,192,192);
1646 m_slotbordercolor = video::SColor(200,0,0,0);
1647 m_slotborder = false;
1649 m_clipbackground = false;
1652 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1653 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1654 m_tooltip_element->enableOverrideColor(true);
1655 m_tooltip_element->setBackgroundColor(video::SColor(255,110,130,60));
1656 m_tooltip_element->setDrawBackground(true);
1657 m_tooltip_element->setDrawBorder(true);
1658 m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
1659 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1660 m_tooltip_element->setWordWrap(false);
1661 //we're not parent so no autograb for this one!
1662 m_tooltip_element->grab();
1666 std::vector<std::string> elements = split(m_formspec_string,']');
1667 for (unsigned int i=0;i< elements.size();i++) {
1668 parseElement(&mydata,elements[i]);
1671 // If there's fields, add a Proceed button
1672 if (m_fields.size() && mydata.bp_set != 2)
1674 // if the size wasn't set by an invsize[] or size[] adjust it now to fit all the fields
1675 mydata.rect = core::rect<s32>(
1676 mydata.screensize.X/2 - 580/2,
1677 mydata.screensize.Y/2 - 300/2,
1678 mydata.screensize.X/2 + 580/2,
1679 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1681 DesiredRect = mydata.rect;
1682 recalculateAbsolutePosition(false);
1683 mydata.basepos = getBasePos();
1686 v2s32 pos = mydata.basepos;
1687 pos.Y = ((m_fields.size()+2)*60);
1689 v2s32 size = DesiredRect.getSize();
1690 mydata.rect = core::rect<s32>(size.X/2-70, pos.Y, (size.X/2-70)+140, pos.Y+30);
1691 wchar_t* text = wgettext("Proceed");
1692 Environment->addButton(mydata.rect, this, 257, text);
1698 //set initial focus if parser didn't set it
1699 focused_element = Environment->getFocus();
1700 if (!focused_element
1701 || !isMyChild(focused_element)
1702 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
1706 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
1708 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1710 for(u32 i=0; i<m_inventorylists.size(); i++)
1712 const ListDrawSpec &s = m_inventorylists[i];
1714 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1716 s32 item_i = i + s.start_item_i;
1717 s32 x = (i%s.geom.X) * spacing.X;
1718 s32 y = (i/s.geom.X) * spacing.Y;
1720 core::rect<s32> rect = imgrect + s.pos + p0;
1721 if(rect.isPointInside(p))
1723 return ItemSpec(s.inventoryloc, s.listname, item_i);
1728 return ItemSpec(InventoryLocation(), "", -1);
1731 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
1733 video::IVideoDriver* driver = Environment->getVideoDriver();
1736 gui::IGUIFont *font = NULL;
1737 gui::IGUISkin* skin = Environment->getSkin();
1739 font = skin->getFont();
1741 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1743 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1744 <<"The inventory location "
1745 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
1749 InventoryList *ilist = inv->getList(s.listname);
1751 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1752 <<"The inventory list \""<<s.listname<<"\" @ \""
1753 <<s.inventoryloc.dump()<<"\" doesn't exist"
1758 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1760 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1762 s32 item_i = i + s.start_item_i;
1763 if(item_i >= (s32) ilist->getSize())
1765 s32 x = (i%s.geom.X) * spacing.X;
1766 s32 y = (i/s.geom.X) * spacing.Y;
1768 core::rect<s32> rect = imgrect + s.pos + p;
1771 item = ilist->getItem(item_i);
1773 bool selected = m_selected_item
1774 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
1775 && m_selected_item->listname == s.listname
1776 && m_selected_item->i == item_i;
1777 bool hovering = rect.isPointInside(m_pointer);
1782 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
1784 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
1787 //Draw inv slot borders
1789 s32 x1 = rect.UpperLeftCorner.X;
1790 s32 y1 = rect.UpperLeftCorner.Y;
1791 s32 x2 = rect.LowerRightCorner.X;
1792 s32 y2 = rect.LowerRightCorner.Y;
1794 driver->draw2DRectangle(m_slotbordercolor,
1795 core::rect<s32>(v2s32(x1 - border, y1 - border),
1796 v2s32(x2 + border, y1)), NULL);
1797 driver->draw2DRectangle(m_slotbordercolor,
1798 core::rect<s32>(v2s32(x1 - border, y2),
1799 v2s32(x2 + border, y2 + border)), NULL);
1800 driver->draw2DRectangle(m_slotbordercolor,
1801 core::rect<s32>(v2s32(x1 - border, y1),
1802 v2s32(x1, y2)), NULL);
1803 driver->draw2DRectangle(m_slotbordercolor,
1804 core::rect<s32>(v2s32(x2, y1),
1805 v2s32(x2 + border, y2)), NULL);
1813 item.takeItem(m_selected_amount);
1817 drawItemStack(driver, font, item,
1818 rect, &AbsoluteClippingRect, m_gamedef);
1822 std::string tooltip_text = "";
1823 if(hovering && !m_selected_item)
1824 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
1825 if(tooltip_text != "")
1827 m_tooltip_element->setVisible(true);
1828 this->bringToFront(m_tooltip_element);
1829 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
1830 s32 tooltip_x = m_pointer.X + 15;
1831 s32 tooltip_y = m_pointer.Y + 15;
1832 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1833 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1834 m_tooltip_element->setRelativePosition(core::rect<s32>(
1835 core::position2d<s32>(tooltip_x, tooltip_y),
1836 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1842 void GUIFormSpecMenu::drawSelectedItem()
1844 if(!m_selected_item)
1847 video::IVideoDriver* driver = Environment->getVideoDriver();
1850 gui::IGUIFont *font = NULL;
1851 gui::IGUISkin* skin = Environment->getSkin();
1853 font = skin->getFont();
1855 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
1857 InventoryList *list = inv->getList(m_selected_item->listname);
1859 ItemStack stack = list->getItem(m_selected_item->i);
1860 stack.count = m_selected_amount;
1862 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1863 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
1864 drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
1867 void GUIFormSpecMenu::drawMenu()
1870 std::string newform = m_form_src->getForm();
1871 if(newform != m_formspec_string){
1872 m_formspec_string = newform;
1873 regenerateGui(m_screensize_old);
1877 m_pointer = m_device->getCursorControl()->getPosition();
1879 updateSelectedItem();
1881 gui::IGUISkin* skin = Environment->getSkin();
1884 video::IVideoDriver* driver = Environment->getVideoDriver();
1886 v2u32 screenSize = driver->getScreenSize();
1887 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
1889 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
1891 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
1893 m_tooltip_element->setVisible(false);
1898 for(u32 i=0; i<m_backgrounds.size(); i++)
1900 const ImageDrawSpec &spec = m_backgrounds[i];
1901 video::ITexture *texture = m_tsrc->getTexture(spec.name);
1904 // Image size on screen
1905 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1906 // Image rectangle on screen
1907 core::rect<s32> rect = imgrect + spec.pos;
1909 if (m_clipbackground) {
1910 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
1911 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
1912 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
1913 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
1914 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
1917 const video::SColor color(255,255,255,255);
1918 const video::SColor colors[] = {color,color,color,color};
1919 driver->draw2DImage(texture, rect,
1920 core::rect<s32>(core::position2d<s32>(0,0),
1921 core::dimension2di(texture->getOriginalSize())),
1922 NULL/*&AbsoluteClippingRect*/, colors, true);
1925 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
1926 errorstream << "\t" << spec.name << std::endl;
1933 for(u32 i=0; i<m_boxes.size(); i++)
1935 const BoxDrawSpec &spec = m_boxes[i];
1937 irr::video::SColor todraw = spec.color;
1939 todraw.setAlpha(140);
1941 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
1942 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
1944 driver->draw2DRectangle(todraw, rect, 0);
1949 for(u32 i=0; i<m_images.size(); i++)
1951 const ImageDrawSpec &spec = m_images[i];
1952 video::ITexture *texture = m_tsrc->getTexture(spec.name);
1955 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
1956 // Image size on screen
1957 core::rect<s32> imgrect;
1960 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
1963 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
1965 // Image rectangle on screen
1966 core::rect<s32> rect = imgrect + spec.pos;
1967 const video::SColor color(255,255,255,255);
1968 const video::SColor colors[] = {color,color,color,color};
1969 driver->draw2DImage(texture, rect,
1970 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
1971 NULL/*&AbsoluteClippingRect*/, colors, true);
1974 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
1975 errorstream << "\t" << spec.name << std::endl;
1982 for(u32 i=0; i<m_itemimages.size(); i++)
1987 const ImageDrawSpec &spec = m_itemimages[i];
1988 IItemDefManager *idef = m_gamedef->idef();
1990 item.deSerialize(spec.name, idef);
1991 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1992 // Image size on screen
1993 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1994 // Image rectangle on screen
1995 core::rect<s32> rect = imgrect + spec.pos;
1996 const video::SColor color(255,255,255,255);
1997 const video::SColor colors[] = {color,color,color,color};
1998 driver->draw2DImage(texture, rect,
1999 core::rect<s32>(core::position2d<s32>(0,0),
2000 core::dimension2di(texture->getOriginalSize())),
2001 NULL/*&AbsoluteClippingRect*/, colors, true);
2006 Phase 0: Item slot rectangles
2007 Phase 1: Item images; prepare tooltip
2010 for(int phase=start_phase; phase<=1; phase++)
2011 for(u32 i=0; i<m_inventorylists.size(); i++)
2013 drawList(m_inventorylists[i], phase);
2019 gui::IGUIElement::draw();
2022 Draw fields/buttons tooltips
2024 for(u32 i=0; i<m_fields.size(); i++)
2026 const FieldSpec &spec = m_fields[i];
2027 if (spec.tooltip != "")
2029 core::rect<s32> rect = spec.rect;
2030 if (rect.isPointInside(m_pointer))
2032 m_tooltip_element->setVisible(true);
2033 this->bringToFront(m_tooltip_element);
2034 m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
2035 s32 tooltip_x = m_pointer.X + 15;
2036 s32 tooltip_y = m_pointer.Y + 15;
2037 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
2038 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2039 m_tooltip_element->setRelativePosition(core::rect<s32>(
2040 core::position2d<s32>(tooltip_x, tooltip_y),
2041 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2047 Draw dragged item stack
2052 void GUIFormSpecMenu::updateSelectedItem()
2054 // If the selected stack has become empty for some reason, deselect it.
2055 // If the selected stack has become inaccessible, deselect it.
2056 // If the selected stack has become smaller, adjust m_selected_amount.
2057 ItemStack selected = verifySelectedItem();
2059 // WARNING: BLACK MAGIC
2060 // See if there is a stack suited for our current guess.
2061 // If such stack does not exist, clear the guess.
2062 if(m_selected_content_guess.name != "" &&
2063 selected.name == m_selected_content_guess.name &&
2064 selected.count == m_selected_content_guess.count){
2065 // Selected item fits the guess. Skip the black magic.
2067 else if(m_selected_content_guess.name != ""){
2069 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2070 const ListDrawSpec &s = m_inventorylists[i];
2071 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2074 InventoryList *list = inv->getList(s.listname);
2077 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2078 u32 item_i = i + s.start_item_i;
2079 if(item_i >= list->getSize())
2081 ItemStack stack = list->getItem(item_i);
2082 if(stack.name == m_selected_content_guess.name &&
2083 stack.count == m_selected_content_guess.count){
2085 infostream<<"Client: Changing selected content guess to "
2086 <<s.inventoryloc.dump()<<" "<<s.listname
2087 <<" "<<item_i<<std::endl;
2088 delete m_selected_item;
2089 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2090 m_selected_amount = stack.count;
2095 infostream<<"Client: Discarding selected content guess: "
2096 <<m_selected_content_guess.getItemString()<<std::endl;
2097 m_selected_content_guess.name = "";
2101 // If craftresult is nonempty and nothing else is selected, select it now.
2102 if(!m_selected_item)
2104 for(u32 i=0; i<m_inventorylists.size(); i++)
2106 const ListDrawSpec &s = m_inventorylists[i];
2107 if(s.listname == "craftpreview")
2109 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2110 InventoryList *list = inv->getList("craftresult");
2111 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2113 m_selected_item = new ItemSpec;
2114 m_selected_item->inventoryloc = s.inventoryloc;
2115 m_selected_item->listname = "craftresult";
2116 m_selected_item->i = 0;
2117 m_selected_amount = 0;
2118 m_selected_dragging = false;
2125 // If craftresult is selected, keep the whole stack selected
2126 if(m_selected_item && m_selected_item->listname == "craftresult")
2128 m_selected_amount = verifySelectedItem().count;
2132 ItemStack GUIFormSpecMenu::verifySelectedItem()
2134 // If the selected stack has become empty for some reason, deselect it.
2135 // If the selected stack has become inaccessible, deselect it.
2136 // If the selected stack has become smaller, adjust m_selected_amount.
2137 // Return the selected stack.
2141 if(m_selected_item->isValid())
2143 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2146 InventoryList *list = inv->getList(m_selected_item->listname);
2147 if(list && (u32) m_selected_item->i < list->getSize())
2149 ItemStack stack = list->getItem(m_selected_item->i);
2150 if(m_selected_amount > stack.count)
2151 m_selected_amount = stack.count;
2158 // selection was not valid
2159 delete m_selected_item;
2160 m_selected_item = NULL;
2161 m_selected_amount = 0;
2162 m_selected_dragging = false;
2167 void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
2171 std::map<std::string, std::string> fields;
2173 if (quitmode == quit_mode_accept) {
2174 fields["quit"] = "true";
2177 if (quitmode == quit_mode_cancel) {
2178 fields["quit"] = "true";
2179 m_text_dst->gotText(fields);
2183 if (current_keys_pending.key_down) {
2184 fields["key_down"] = "true";
2185 current_keys_pending.key_down = false;
2188 if (current_keys_pending.key_up) {
2189 fields["key_up"] = "true";
2190 current_keys_pending.key_up = false;
2193 if (current_keys_pending.key_enter) {
2194 fields["key_enter"] = "true";
2195 current_keys_pending.key_enter = false;
2198 if (current_keys_pending.key_escape) {
2199 fields["key_escape"] = "true";
2200 current_keys_pending.key_escape = false;
2203 for(u32 i=0; i<m_fields.size(); i++)
2205 const FieldSpec &s = m_fields[i];
2208 if(s.ftype == f_Button)
2210 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(s.flabel.c_str());
2212 else if(s.ftype == f_Table) {
2213 GUITable *table = getTable(s.fname);
2215 fields[wide_to_narrow(s.fname.c_str())]
2216 = table->checkEvent();
2219 else if(s.ftype == f_DropDown) {
2220 // no dynamic cast possible due to some distributions shipped
2221 // without rtti support in irrlicht
2222 IGUIElement * element = getElementFromId(s.fid);
2223 gui::IGUIComboBox *e = NULL;
2224 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2225 e = static_cast<gui::IGUIComboBox*>(element);
2227 s32 selected = e->getSelected();
2228 if (selected >= 0) {
2229 fields[wide_to_narrow(s.fname.c_str())] =
2230 wide_to_narrow(e->getItem(selected));
2233 else if (s.ftype == f_TabHeader) {
2234 // no dynamic cast possible due to some distributions shipped
2235 // without rtti support in irrlicht
2236 IGUIElement * element = getElementFromId(s.fid);
2237 gui::IGUITabControl *e = NULL;
2238 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2239 e = static_cast<gui::IGUITabControl*>(element);
2243 std::stringstream ss;
2244 ss << (e->getActiveTab() +1);
2245 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2248 else if (s.ftype == f_CheckBox) {
2249 // no dynamic cast possible due to some distributions shipped
2250 // without rtti support in irrlicht
2251 IGUIElement * element = getElementFromId(s.fid);
2252 gui::IGUICheckBox *e = NULL;
2253 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2254 e = static_cast<gui::IGUICheckBox*>(element);
2259 fields[wide_to_narrow(s.fname.c_str())] = "true";
2261 fields[wide_to_narrow(s.fname.c_str())] = "false";
2266 IGUIElement* e = getElementFromId(s.fid);
2269 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(e->getText());
2275 m_text_dst->gotText(fields);
2279 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2281 // Fix Esc/Return key being eaten by checkboxen and tables
2282 if(event.EventType==EET_KEY_INPUT_EVENT)
2284 KeyPress kp(event.KeyInput);
2285 if (kp == EscapeKey || kp == getKeySetting("keymap_inventory")
2286 || event.KeyInput.Key==KEY_RETURN)
2288 gui::IGUIElement *focused = Environment->getFocus();
2289 if (focused && isMyChild(focused) &&
2290 (focused->getType() == gui::EGUIET_LIST_BOX ||
2291 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2297 // Mouse wheel events: send to hovered element instead of focused
2298 if(event.EventType==EET_MOUSE_INPUT_EVENT
2299 && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
2301 s32 x = event.MouseInput.X;
2302 s32 y = event.MouseInput.Y;
2303 gui::IGUIElement *hovered =
2304 Environment->getRootGUIElement()->getElementFromPoint(
2305 core::position2d<s32>(x, y));
2306 if (hovered && isMyChild(hovered)) {
2307 hovered->OnEvent(event);
2314 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2316 if(event.EventType==EET_KEY_INPUT_EVENT)
2318 KeyPress kp(event.KeyInput);
2319 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
2320 kp == getKeySetting("keymap_inventory")))
2324 acceptInput(quit_mode_cancel);
2327 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
2331 if (event.KeyInput.PressedDown &&
2332 (event.KeyInput.Key==KEY_RETURN ||
2333 event.KeyInput.Key==KEY_UP ||
2334 event.KeyInput.Key==KEY_DOWN)
2338 switch (event.KeyInput.Key) {
2340 current_keys_pending.key_enter = true;
2343 current_keys_pending.key_up = true;
2346 current_keys_pending.key_down = true;
2350 //can't happen at all!
2351 assert("reached a source line that can't ever been reached" == 0);
2354 if (current_keys_pending.key_enter && m_allowclose) {
2355 acceptInput(quit_mode_accept);
2365 if(event.EventType==EET_MOUSE_INPUT_EVENT
2366 && event.MouseInput.Event != EMIE_MOUSE_MOVED)
2368 // Mouse event other than movement
2370 // Get selected item and hovered/clicked item (s)
2372 updateSelectedItem();
2373 ItemSpec s = getItemAtPos(m_pointer);
2375 Inventory *inv_selected = NULL;
2376 Inventory *inv_s = NULL;
2380 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
2381 assert(inv_selected);
2382 assert(inv_selected->getList(m_selected_item->listname) != NULL);
2389 inv_s = m_invmgr->getInventory(s.inventoryloc);
2392 errorstream<<"InventoryMenu: The selected inventory location "
2393 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2395 s.i = -1; // make it invalid again
2399 InventoryList *list = inv_s->getList(s.listname);
2401 verbosestream<<"InventoryMenu: The selected inventory list \""
2402 <<s.listname<<"\" does not exist"<<std::endl;
2403 s.i = -1; // make it invalid again
2407 if((u32)s.i >= list->getSize()){
2408 infostream<<"InventoryMenu: The selected inventory list \""
2409 <<s.listname<<"\" is too small (i="<<s.i<<", size="
2410 <<list->getSize()<<")"<<std::endl;
2411 s.i = -1; // make it invalid again
2415 s_count = list->getItem(s.i).count;
2418 bool identical = (m_selected_item != NULL) && s.isValid() &&
2419 (inv_selected == inv_s) &&
2420 (m_selected_item->listname == s.listname) &&
2421 (m_selected_item->i == s.i);
2423 // buttons: 0 = left, 1 = right, 2 = middle
2424 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
2427 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
2428 { button = 0; updown = 0; }
2429 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
2430 { button = 1; updown = 0; }
2431 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
2432 { button = 2; updown = 0; }
2433 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
2434 { button = 0; updown = 1; }
2435 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
2436 { button = 1; updown = 1; }
2437 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
2438 { button = 2; updown = 1; }
2440 // Set this number to a positive value to generate a move action
2441 // from m_selected_item to s.
2442 u32 move_amount = 0;
2444 // Set this number to a positive value to generate a drop action
2445 // from m_selected_item.
2446 u32 drop_amount = 0;
2448 // Set this number to a positive value to generate a craft action at s.
2449 u32 craft_amount = 0;
2453 // Some mouse button has been pressed
2455 //infostream<<"Mouse button "<<button<<" pressed at p=("
2456 // <<p.X<<","<<p.Y<<")"<<std::endl;
2458 m_selected_dragging = false;
2460 if(s.isValid() && s.listname == "craftpreview")
2462 // Craft preview has been clicked: craft
2463 craft_amount = (button == 2 ? 10 : 1);
2465 else if(m_selected_item == NULL)
2469 // Non-empty stack has been clicked: select it
2470 m_selected_item = new ItemSpec(s);
2472 if(button == 1) // right
2473 m_selected_amount = (s_count + 1) / 2;
2474 else if(button == 2) // middle
2475 m_selected_amount = MYMIN(s_count, 10);
2477 m_selected_amount = s_count;
2479 m_selected_dragging = true;
2482 else // m_selected_item != NULL
2484 assert(m_selected_amount >= 1);
2488 // Clicked a slot: move
2489 if(button == 1) // right
2491 else if(button == 2) // middle
2492 move_amount = MYMIN(m_selected_amount, 10);
2494 move_amount = m_selected_amount;
2498 if(move_amount >= m_selected_amount)
2499 m_selected_amount = 0;
2501 m_selected_amount -= move_amount;
2505 else if (!getAbsoluteClippingRect().isPointInside(m_pointer))
2507 // Clicked outside of the window: drop
2508 if(button == 1) // right
2510 else if(button == 2) // middle
2511 drop_amount = MYMIN(m_selected_amount, 10);
2513 drop_amount = m_selected_amount;
2517 else if(updown == 1)
2519 // Some mouse button has been released
2521 //infostream<<"Mouse button "<<button<<" released at p=("
2522 // <<p.X<<","<<p.Y<<")"<<std::endl;
2524 if(m_selected_item != NULL && m_selected_dragging && s.isValid())
2528 // Dragged to different slot: move all selected
2529 move_amount = m_selected_amount;
2532 else if(m_selected_item != NULL && m_selected_dragging &&
2533 !(getAbsoluteClippingRect().isPointInside(m_pointer)))
2535 // Dragged outside of window: drop all selected
2536 drop_amount = m_selected_amount;
2539 m_selected_dragging = false;
2542 // Possibly send inventory action to server
2545 // Send IACTION_MOVE
2547 assert(m_selected_item && m_selected_item->isValid());
2548 assert(s.isValid());
2550 assert(inv_selected && inv_s);
2551 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2552 InventoryList *list_to = inv_s->getList(s.listname);
2553 assert(list_from && list_to);
2554 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2555 ItemStack stack_to = list_to->getItem(s.i);
2557 // Check how many items can be moved
2558 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
2559 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
2560 // If source stack cannot be added to destination stack at all,
2562 if(leftover.count == stack_from.count && leftover.name == stack_from.name)
2564 m_selected_amount = stack_to.count;
2565 // In case the server doesn't directly swap them but instead
2566 // moves stack_to somewhere else, set this
2567 m_selected_content_guess = stack_to;
2568 m_selected_content_guess_inventory = s.inventoryloc;
2570 // Source stack goes fully into destination stack
2571 else if(leftover.empty())
2573 m_selected_amount -= move_amount;
2574 m_selected_content_guess = ItemStack(); // Clear
2576 // Source stack goes partly into destination stack
2579 move_amount -= leftover.count;
2580 m_selected_amount -= move_amount;
2581 m_selected_content_guess = ItemStack(); // Clear
2584 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
2585 IMoveAction *a = new IMoveAction();
2586 a->count = move_amount;
2587 a->from_inv = m_selected_item->inventoryloc;
2588 a->from_list = m_selected_item->listname;
2589 a->from_i = m_selected_item->i;
2590 a->to_inv = s.inventoryloc;
2591 a->to_list = s.listname;
2593 m_invmgr->inventoryAction(a);
2595 else if(drop_amount > 0)
2597 m_selected_content_guess = ItemStack(); // Clear
2599 // Send IACTION_DROP
2601 assert(m_selected_item && m_selected_item->isValid());
2602 assert(inv_selected);
2603 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2605 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2607 // Check how many items can be dropped
2608 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
2609 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
2610 m_selected_amount -= drop_amount;
2612 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
2613 IDropAction *a = new IDropAction();
2614 a->count = drop_amount;
2615 a->from_inv = m_selected_item->inventoryloc;
2616 a->from_list = m_selected_item->listname;
2617 a->from_i = m_selected_item->i;
2618 m_invmgr->inventoryAction(a);
2620 else if(craft_amount > 0)
2622 m_selected_content_guess = ItemStack(); // Clear
2624 // Send IACTION_CRAFT
2626 assert(s.isValid());
2629 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
2630 ICraftAction *a = new ICraftAction();
2631 a->count = craft_amount;
2632 a->craft_inv = s.inventoryloc;
2633 m_invmgr->inventoryAction(a);
2636 // If m_selected_amount has been decreased to zero, deselect
2637 if(m_selected_amount == 0)
2639 delete m_selected_item;
2640 m_selected_item = NULL;
2641 m_selected_amount = 0;
2642 m_selected_dragging = false;
2643 m_selected_content_guess = ItemStack();
2646 if(event.EventType==EET_GUI_EVENT)
2649 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
2652 // find the element that was clicked
2653 for(u32 i=0; i<m_fields.size(); i++)
2655 FieldSpec &s = m_fields[i];
2656 // if its a button, set the send field so
2657 // lua knows which button was pressed
2658 if ((s.ftype == f_TabHeader) && (s.fid == event.GUIEvent.Caller->getID()))
2667 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
2670 if(!canTakeFocus(event.GUIEvent.Element))
2672 infostream<<"GUIFormSpecMenu: Not allowing focus change."
2674 // Returning true disables focus change
2678 if((event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) ||
2679 (event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED))
2681 unsigned int btn_id = event.GUIEvent.Caller->getID();
2683 if (btn_id == 257) {
2685 acceptInput(quit_mode_accept);
2689 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2691 // quitMenu deallocates menu
2695 // find the element that was clicked
2696 for(u32 i=0; i<m_fields.size(); i++)
2698 FieldSpec &s = m_fields[i];
2699 // if its a button, set the send field so
2700 // lua knows which button was pressed
2701 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
2702 (s.fid == event.GUIEvent.Caller->getID()))
2707 acceptInput(quit_mode_accept);
2710 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2721 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
2723 if(event.GUIEvent.Caller->getID() > 257)
2727 acceptInput(quit_mode_accept);
2731 current_keys_pending.key_enter = true;
2734 // quitMenu deallocates menu
2739 if(event.GUIEvent.EventType==gui::EGET_TABLE_CHANGED)
2741 int current_id = event.GUIEvent.Caller->getID();
2742 if(current_id > 257)
2744 // find the element that was clicked
2745 for(u32 i=0; i<m_fields.size(); i++)
2747 FieldSpec &s = m_fields[i];
2748 // if it's a table, set the send field
2749 // so lua knows which table was changed
2750 if ((s.ftype == f_Table) && (s.fid == current_id))
2762 return Parent ? Parent->OnEvent(event) : false;
2765 bool GUIFormSpecMenu::parseColor(const std::string &value, video::SColor &color,
2768 const char *hexpattern = NULL;
2769 if (value[0] == '#') {
2770 if (value.size() == 9)
2771 hexpattern = "#RRGGBBAA";
2772 else if (value.size() == 7)
2773 hexpattern = "#RRGGBB";
2774 else if (value.size() == 5)
2775 hexpattern = "#RGBA";
2776 else if (value.size() == 4)
2777 hexpattern = "#RGB";
2781 assert(strlen(hexpattern) == value.size());
2782 video::SColor outcolor(255, 255, 255, 255);
2783 for (size_t pos = 0; pos < value.size(); ++pos) {
2784 // '#' in the pattern means skip that character
2785 if (hexpattern[pos] == '#')
2788 // Else assume hexpattern[pos] is one of 'R' 'G' 'B' 'A'
2789 // Read one or two digits, depending on hexpattern
2790 unsigned char c1, c2;
2791 if (hexpattern[pos+1] == hexpattern[pos]) {
2792 // Two digits, e.g. hexpattern == "#RRGGBB"
2793 if (!hex_digit_decode(value[pos], c1) ||
2794 !hex_digit_decode(value[pos+1], c2))
2799 // One digit, e.g. hexpattern == "#RGB"
2800 if (!hex_digit_decode(value[pos], c1))
2804 u32 colorpart = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
2806 // Update outcolor with newly read color part
2807 if (hexpattern[pos] == 'R')
2808 outcolor.setRed(colorpart);
2809 else if (hexpattern[pos] == 'G')
2810 outcolor.setGreen(colorpart);
2811 else if (hexpattern[pos] == 'B')
2812 outcolor.setBlue(colorpart);
2813 else if (hexpattern[pos] == 'A')
2814 outcolor.setAlpha(colorpart);
2821 // Optionally, named colors could be implemented here
2825 errorstream<<"Invalid color: \""<<value<<"\""<<std::endl;