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"
27 #include "constants.h"
31 #include <IGUICheckBox.h>
32 #include <IGUIEditBox.h>
33 #include <IGUIButton.h>
34 #include <IGUIStaticText.h>
36 #include <IGUIListBox.h>
37 #include <IGUITabControl.h>
38 #include <IGUIScrollBar.h>
39 #include <IGUIComboBox.h>
41 #include "tile.h" // ITextureSource
42 #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,
70 IMenuManager *menumgr,
71 InventoryManager *invmgr,
73 ISimpleTextureSource *tsrc
75 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
82 m_selected_item(NULL),
84 m_selected_dragging(false),
85 m_listbox_click_fname(),
86 m_listbox_click_index(-1),
87 m_listbox_click_time(0),
88 m_listbox_doubleclick(false),
89 m_tooltip_element(NULL),
93 current_keys_pending.key_down = false;
94 current_keys_pending.key_up = false;
95 current_keys_pending.key_enter = false;
96 current_keys_pending.key_escape = false;
100 GUIFormSpecMenu::~GUIFormSpecMenu()
104 delete m_selected_item;
109 void GUIFormSpecMenu::removeChildren()
111 const core::list<gui::IGUIElement*> &children = getChildren();
112 core::list<gui::IGUIElement*> children_copy;
113 for(core::list<gui::IGUIElement*>::ConstIterator
114 i = children.begin(); i != children.end(); i++)
116 children_copy.push_back(*i);
118 for(core::list<gui::IGUIElement*>::Iterator
119 i = children_copy.begin();
120 i != children_copy.end(); i++)
125 gui::IGUIElement *e = getElementFromId(256);
130 if(m_tooltip_element)
132 m_tooltip_element->remove();
133 m_tooltip_element->drop();
134 m_tooltip_element = NULL;
139 void GUIFormSpecMenu::setInitialFocus()
141 // Set initial focus according to following order of precedence:
142 // 1. first empty editbox
146 // 5. first focusable (not statictext, not tabheader)
147 // 6. first child element
149 core::list<gui::IGUIElement*> children = getChildren();
151 // in case "children" contains any NULL elements, remove them
152 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
153 it != children.end();) {
157 it = children.erase(it);
160 // 1. first empty editbox
161 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
162 it != children.end(); ++it) {
163 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
164 && (*it)->getText()[0] == 0) {
165 Environment->setFocus(*it);
171 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
172 it != children.end(); ++it) {
173 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
174 Environment->setFocus(*it);
180 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
181 it != children.end(); ++it) {
182 if ((*it)->getType() == gui::EGUIET_LIST_BOX) {
183 Environment->setFocus(*it);
189 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
190 it != children.end(); --it) {
191 if ((*it)->getType() == gui::EGUIET_BUTTON) {
192 Environment->setFocus(*it);
197 // 5. first focusable (not statictext, not tabheader)
198 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
199 it != children.end(); ++it) {
200 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
201 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
202 Environment->setFocus(*it);
207 // 6. first child element
208 if (children.empty())
209 Environment->setFocus(this);
211 Environment->setFocus(*(children.begin()));
214 int GUIFormSpecMenu::getListboxIndex(std::string listboxname) {
216 std::wstring wlistboxname = narrow_to_wide(listboxname.c_str());
218 for(unsigned int i=0; i < m_listboxes.size(); i++) {
220 std::wstring name(m_listboxes[i].first.fname.c_str());
221 if ( name == wlistboxname) {
222 return m_listboxes[i].second->getSelected();
228 bool GUIFormSpecMenu::checkListboxClick(std::wstring wlistboxname,
231 // WARNING: BLACK IRRLICHT MAGIC
232 // Used to fix Irrlicht's subpar reporting of single clicks and double
233 // clicks in listboxes (gui::EGET_LISTBOX_CHANGED,
234 // gui::EGET_LISTBOX_SELECTED_AGAIN):
235 // 1. IGUIListBox::setSelected() is counted as a click.
236 // Including the initial setSelected() done by parseTextList().
237 // 2. Clicking on a the selected item and then dragging for less
238 // than 500ms is counted as a doubleclick, no matter when the
239 // item was previously selected (e.g. more than 500ms ago)
241 // So when Irrlicht reports a doubleclick, we need to check
242 // for ourselves if really was a doubleclick. Or just a fake.
244 for(unsigned int i=0; i < m_listboxes.size(); i++) {
245 std::wstring name(m_listboxes[i].first.fname.c_str());
246 int selected = m_listboxes[i].second->getSelected();
247 if (name == wlistboxname && selected >= 0) {
248 u32 now = getTimeMs();
250 (eventtype == gui::EGET_LISTBOX_SELECTED_AGAIN)
251 && (name == m_listbox_click_fname)
252 && (selected == m_listbox_click_index)
253 && (m_listbox_click_time >= now - 500);
254 m_listbox_click_fname = name;
255 m_listbox_click_index = selected;
256 m_listbox_click_time = now;
257 m_listbox_doubleclick = doubleclick;
264 gui::IGUIScrollBar* GUIFormSpecMenu::getListboxScrollbar(
265 gui::IGUIListBox *listbox)
267 // WARNING: BLACK IRRLICHT MAGIC
268 // Ordinarily, due to how formspecs work (recreating the entire GUI
269 // when something changes), when you select an item in a textlist
270 // with more items than fit in the visible area, the newly selected
271 // item is scrolled to the bottom of the visible area. This is
272 // annoying and breaks GUI designs that use double clicks.
274 // This function helps fixing this problem by giving direct access
275 // to a listbox's scrollbar. This works because CGUIListBox doesn't
276 // cache the scrollbar position anywhere.
278 // If this stops working in a future irrlicht version, consider
279 // maintaining a local copy of irr::gui::CGUIListBox, possibly also
280 // fixing the other reasons why black irrlicht magic is needed.
282 core::list<gui::IGUIElement*> children = listbox->getChildren();
283 for(core::list<gui::IGUIElement*>::Iterator it = children.begin();
284 it != children.end(); ++it) {
285 gui::IGUIElement* child = *it;
286 if (child && child->getType() == gui::EGUIET_SCROLL_BAR) {
287 return static_cast<gui::IGUIScrollBar*>(child);
291 verbosestream<<"getListboxScrollbar: WARNING: "
292 <<"listbox has no scrollbar"<<std::endl;
296 std::vector<std::string> split(const std::string &s, char delim) {
297 std::vector<std::string> tokens;
299 std::string current = "";
300 bool last_was_escape = false;
301 for(unsigned int i=0; i < s.size(); i++) {
302 if (last_was_escape) {
304 current += s.c_str()[i];
305 last_was_escape = false;
308 if (s.c_str()[i] == delim) {
309 tokens.push_back(current);
311 last_was_escape = false;
313 else if (s.c_str()[i] == '\\'){
314 last_was_escape = true;
317 current += s.c_str()[i];
318 last_was_escape = false;
323 tokens.push_back(current);
328 void GUIFormSpecMenu::parseSize(parserData* data,std::string element) {
329 std::vector<std::string> parts = split(element,',');
331 if (parts.size() == 2) {
334 if (parts[1].find(';') != std::string::npos)
335 parts[1] = parts[1].substr(0,parts[1].find(';'));
337 invsize.X = stof(parts[0]);
338 invsize.Y = stof(parts[1]);
341 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
342 v2u32 delta = current_screensize - m_lockscreensize;
344 if (current_screensize.Y > m_lockscreensize.Y)
349 if (current_screensize.X > m_lockscreensize.X)
354 offset = v2s32(delta.X,delta.Y);
356 data->screensize = m_lockscreensize;
362 padding = v2s32(data->screensize.Y/40, data->screensize.Y/40);
363 spacing = v2s32(data->screensize.Y/12, data->screensize.Y/13);
364 imgsize = v2s32(data->screensize.Y/15, data->screensize.Y/15);
366 padding.X*2+spacing.X*(invsize.X-1.0)+imgsize.X,
367 padding.Y*2+spacing.Y*(invsize.Y-1.0)+imgsize.Y + (data->helptext_h-5)
369 data->rect = core::rect<s32>(
370 data->screensize.X/2 - data->size.X/2 + offset.X,
371 data->screensize.Y/2 - data->size.Y/2 + offset.Y,
372 data->screensize.X/2 + data->size.X/2 + offset.X,
373 data->screensize.Y/2 + data->size.Y/2 + offset.Y
376 DesiredRect = data->rect;
377 recalculateAbsolutePosition(false);
378 data->basepos = getBasePos();
382 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
385 void GUIFormSpecMenu::parseList(parserData* data,std::string element) {
387 if (m_gamedef == 0) {
388 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
392 std::vector<std::string> parts = split(element,';');
394 if ((parts.size() == 4) || (parts.size() == 5)) {
395 std::string location = parts[0];
396 std::string listname = parts[1];
397 std::vector<std::string> v_pos = split(parts[2],',');
398 std::vector<std::string> v_geom = split(parts[3],',');
399 std::string startindex = "";
400 if (parts.size() == 5)
401 startindex = parts[4];
403 MY_CHECKPOS("list",2);
404 MY_CHECKGEOM("list",3);
406 InventoryLocation loc;
408 if(location == "context" || location == "current_name")
409 loc = m_current_inventory_location;
411 loc.deSerialize(location);
413 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
414 pos.X += stof(v_pos[0]) * (float)spacing.X;
415 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
418 geom.X = stoi(v_geom[0]);
419 geom.Y = stoi(v_geom[1]);
423 start_i = stoi(startindex);
424 if(data->bp_set != 2)
425 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
426 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
429 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
432 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element) {
433 std::vector<std::string> parts = split(element,';');
435 if ((parts.size() == 3) || (parts.size() == 4)) {
436 std::vector<std::string> v_pos = split(parts[0],',');
437 std::string name = parts[1];
438 std::string label = parts[2];
439 std::string selected = "";
441 if (parts.size() == 4)
444 MY_CHECKPOS("checkbox",0);
447 pos.X += stof(v_pos[0]) * (float) spacing.X;
448 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
450 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
452 bool fselected = false;
454 if (selected == "true")
457 std::wstring wlabel = narrow_to_wide(label.c_str());
459 FieldSpec spec = FieldSpec(
460 narrow_to_wide(name.c_str()),
466 spec.ftype = f_CheckBox;
467 spec.flabel = wlabel; //Needed for displaying text on MSVC
468 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
469 spec.fid, spec.flabel.c_str());
471 if (spec.fname == data->focused_fieldname) {
472 Environment->setFocus(e);
475 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
476 m_fields.push_back(spec);
479 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
482 void GUIFormSpecMenu::parseImage(parserData* data,std::string element) {
483 std::vector<std::string> parts = split(element,';');
485 if (parts.size() == 3) {
486 std::vector<std::string> v_pos = split(parts[0],',');
487 std::vector<std::string> v_geom = split(parts[1],',');
488 std::string name = unescape_string(parts[2]);
490 MY_CHECKPOS("image",0);
491 MY_CHECKGEOM("image",1);
493 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
494 pos.X += stof(v_pos[0]) * (float) spacing.X;
495 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
498 geom.X = stof(v_geom[0]) * (float)imgsize.X;
499 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
501 if(data->bp_set != 2)
502 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
503 m_images.push_back(ImageDrawSpec(name, pos, geom));
507 if (parts.size() == 2) {
508 std::vector<std::string> v_pos = split(parts[0],',');
509 std::string name = unescape_string(parts[1]);
511 MY_CHECKPOS("image",0);
513 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
514 pos.X += stof(v_pos[0]) * (float) spacing.X;
515 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
517 if(data->bp_set != 2)
518 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
519 m_images.push_back(ImageDrawSpec(name, pos));
522 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
525 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element) {
526 std::vector<std::string> parts = split(element,';');
528 if (parts.size() == 3) {
529 std::vector<std::string> v_pos = split(parts[0],',');
530 std::vector<std::string> v_geom = split(parts[1],',');
531 std::string name = parts[2];
533 MY_CHECKPOS("itemimage",0);
534 MY_CHECKGEOM("itemimage",1);
536 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
537 pos.X += stof(v_pos[0]) * (float) spacing.X;
538 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
541 geom.X = stoi(v_geom[0]) * (float)imgsize.X;
542 geom.Y = stoi(v_geom[1]) * (float)imgsize.Y;
544 if(data->bp_set != 2)
545 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
546 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
549 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
552 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,std::string type) {
553 std::vector<std::string> parts = split(element,';');
555 if (parts.size() == 4) {
556 std::vector<std::string> v_pos = split(parts[0],',');
557 std::vector<std::string> v_geom = split(parts[1],',');
558 std::string name = parts[2];
559 std::string label = parts[3];
561 MY_CHECKPOS("button",0);
562 MY_CHECKGEOM("button",1);
565 pos.X += stof(v_pos[0]) * (float)spacing.X;
566 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
569 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
570 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
572 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y-15, pos.X+geom.X, pos.Y+15);
574 if(data->bp_set != 2)
575 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
577 label = unescape_string(label);
579 std::wstring wlabel = narrow_to_wide(label.c_str());
581 FieldSpec spec = FieldSpec(
582 narrow_to_wide(name.c_str()),
587 spec.ftype = f_Button;
588 if(type == "button_exit")
591 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
592 spec.flabel.c_str());
594 if (spec.fname == data->focused_fieldname) {
595 Environment->setFocus(e);
598 m_fields.push_back(spec);
601 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
604 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element) {
605 std::vector<std::string> parts = split(element,';');
607 if (parts.size() == 3) {
608 std::vector<std::string> v_pos = split(parts[0],',');
609 std::vector<std::string> v_geom = split(parts[1],',');
610 std::string name = unescape_string(parts[2]);
612 MY_CHECKPOS("background",0);
613 MY_CHECKGEOM("background",1);
615 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
616 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
617 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
620 geom.X = stof(v_geom[0]) * (float)spacing.X;
621 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
623 if(data->bp_set != 2)
624 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
625 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
628 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
631 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element) {
632 std::vector<std::string> parts = split(element,';');
634 if ((parts.size() == 5) || (parts.size() == 6)) {
635 std::vector<std::string> v_pos = split(parts[0],',');
636 std::vector<std::string> v_geom = split(parts[1],',');
637 std::string name = parts[2];
638 std::vector<std::string> items = split(parts[3],',');
639 std::string str_initial_selection = "";
640 std::string str_transparent = "false";
642 if (parts.size() >= 5)
643 str_initial_selection = parts[4];
645 if (parts.size() >= 6)
646 str_transparent = parts[5];
648 MY_CHECKPOS("textlist",0);
649 MY_CHECKGEOM("textlist",1);
652 pos.X += stof(v_pos[0]) * (float)spacing.X;
653 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
656 geom.X = stof(v_geom[0]) * (float)spacing.X;
657 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
660 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
662 std::wstring fname_w = narrow_to_wide(name.c_str());
664 FieldSpec spec = FieldSpec(
671 spec.ftype = f_ListBox;
673 //now really show list
674 gui::IGUIListBox *e = Environment->addListBox(rect, this,spec.fid);
676 if (spec.fname == data->focused_fieldname) {
677 Environment->setFocus(e);
680 if (str_transparent == "false")
681 e->setDrawBackground(true);
683 for (unsigned int i=0; i < items.size(); i++) {
684 if (items[i].c_str()[0] == '#') {
685 if (items[i].c_str()[1] == '#') {
686 e->addItem(narrow_to_wide(unescape_string(items[i])).c_str() +1);
689 std::string color = items[i].substr(1,6);
691 narrow_to_wide(unescape_string(items[i]).c_str() + 7);
693 e->addItem(toadd.c_str());
695 irr::video::SColor toset;
697 if (parseColor(color, toset))
698 e->setItemOverrideColor(i,toset);
702 e->addItem(narrow_to_wide(unescape_string(items[i])).c_str());
706 if (data->listbox_selections.find(fname_w) != data->listbox_selections.end()) {
707 e->setSelected(data->listbox_selections[fname_w]);
710 if (data->listbox_scroll.find(fname_w) != data->listbox_scroll.end()) {
711 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(e);
713 scrollbar->setPos(data->listbox_scroll[fname_w]);
717 if (str_initial_selection != "")
718 e->setSelected(stoi(str_initial_selection.c_str())-1);
720 m_listboxes.push_back(std::pair<FieldSpec,gui::IGUIListBox*>(spec,e));
721 m_fields.push_back(spec);
724 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
728 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element) {
729 std::vector<std::string> parts = split(element,';');
731 if (parts.size() == 5) {
732 std::vector<std::string> v_pos = split(parts[0],',');
733 std::string name = parts[2];
734 std::vector<std::string> items = split(parts[3],',');
735 std::string str_initial_selection = "";
736 str_initial_selection = parts[4];
738 MY_CHECKPOS("dropdown",0);
741 pos.X += stof(v_pos[0]) * (float)spacing.X;
742 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
744 s32 width = stof(parts[1]) * (float)spacing.Y;
746 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+width, pos.Y+30);
748 std::wstring fname_w = narrow_to_wide(name.c_str());
750 FieldSpec spec = FieldSpec(
757 spec.ftype = f_DropDown;
760 //now really show list
761 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
763 if (spec.fname == data->focused_fieldname) {
764 Environment->setFocus(e);
767 for (unsigned int i=0; i < items.size(); i++) {
768 e->addItem(narrow_to_wide(items[i]).c_str());
771 if (str_initial_selection != "")
772 e->setSelected(stoi(str_initial_selection.c_str())-1);
774 m_fields.push_back(spec);
777 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
778 << element << "'" << std::endl;
781 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element) {
782 std::vector<std::string> parts = split(element,';');
784 if (parts.size() == 4) {
785 std::vector<std::string> v_pos = split(parts[0],',');
786 std::vector<std::string> v_geom = split(parts[1],',');
787 std::string name = parts[2];
788 std::string label = parts[3];
790 MY_CHECKPOS("pwdfield",0);
791 MY_CHECKGEOM("pwdfield",1);
794 pos.X += stof(v_pos[0]) * (float)spacing.X;
795 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
798 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
800 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
804 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
806 label = unescape_string(label);
808 std::wstring wlabel = narrow_to_wide(label.c_str());
810 FieldSpec spec = FieldSpec(
811 narrow_to_wide(name.c_str()),
818 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
820 if (spec.fname == data->focused_fieldname) {
821 Environment->setFocus(e);
824 if (label.length() > 1)
826 rect.UpperLeftCorner.Y -= 15;
827 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
828 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
831 e->setPasswordBox(true,L'*');
834 evt.EventType = EET_KEY_INPUT_EVENT;
835 evt.KeyInput.Key = KEY_END;
836 evt.KeyInput.Char = 0;
837 evt.KeyInput.Control = 0;
838 evt.KeyInput.Shift = 0;
839 evt.KeyInput.PressedDown = true;
841 m_fields.push_back(spec);
844 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
847 void GUIFormSpecMenu::parseSimpleField(parserData* data,std::vector<std::string> &parts) {
848 std::string name = parts[0];
849 std::string label = parts[1];
850 std::string default_val = parts[2];
852 core::rect<s32> rect;
856 rect = core::rect<s32>(
857 data->screensize.X/2 - 580/2,
858 data->screensize.Y/2 - 300/2,
859 data->screensize.X/2 + 580/2,
860 data->screensize.Y/2 + 300/2
863 recalculateAbsolutePosition(false);
864 data->basepos = getBasePos();
867 else if(data->bp_set == 2)
868 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
870 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
871 pos.Y = ((m_fields.size()+2)*60);
872 v2s32 size = DesiredRect.getSize();
874 rect = core::rect<s32>(size.X/2-150, pos.Y, (size.X/2-150)+300, pos.Y+30);
878 default_val = m_form_src->resolveText(default_val);
880 default_val = unescape_string(default_val);
881 label = unescape_string(label);
883 std::wstring wlabel = narrow_to_wide(label.c_str());
885 FieldSpec spec = FieldSpec(
886 narrow_to_wide(name.c_str()),
888 narrow_to_wide(default_val.c_str()),
894 // spec field id to 0, this stops submit searching for a value that isn't there
895 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
900 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
902 if (spec.fname == data->focused_fieldname) {
903 Environment->setFocus(e);
907 evt.EventType = EET_KEY_INPUT_EVENT;
908 evt.KeyInput.Key = KEY_END;
909 evt.KeyInput.Char = 0;
910 evt.KeyInput.Control = 0;
911 evt.KeyInput.Shift = 0;
912 evt.KeyInput.PressedDown = true;
915 if (label.length() > 1)
917 rect.UpperLeftCorner.Y -= 15;
918 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
919 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
923 m_fields.push_back(spec);
926 void GUIFormSpecMenu::parseTextArea(parserData* data,std::vector<std::string>& parts,std::string type) {
928 std::vector<std::string> v_pos = split(parts[0],',');
929 std::vector<std::string> v_geom = split(parts[1],',');
930 std::string name = parts[2];
931 std::string label = parts[3];
932 std::string default_val = parts[4];
935 MY_CHECKGEOM(type,1);
938 pos.X = stof(v_pos[0]) * (float) spacing.X;
939 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
943 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
945 if (type == "textarea")
947 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
952 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
957 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
959 if(data->bp_set != 2)
960 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
963 default_val = m_form_src->resolveText(default_val);
966 default_val = unescape_string(default_val);
967 label = unescape_string(label);
969 std::wstring wlabel = narrow_to_wide(label.c_str());
971 FieldSpec spec = FieldSpec(
972 narrow_to_wide(name.c_str()),
974 narrow_to_wide(default_val.c_str()),
980 // spec field id to 0, this stops submit searching for a value that isn't there
981 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
986 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
988 if (spec.fname == data->focused_fieldname) {
989 Environment->setFocus(e);
992 if (type == "textarea")
994 e->setMultiLine(true);
995 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
998 evt.EventType = EET_KEY_INPUT_EVENT;
999 evt.KeyInput.Key = KEY_END;
1000 evt.KeyInput.Char = 0;
1001 evt.KeyInput.Control = 0;
1002 evt.KeyInput.Shift = 0;
1003 evt.KeyInput.PressedDown = true;
1007 if (label.length() > 1)
1009 rect.UpperLeftCorner.Y -= 15;
1010 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
1011 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1014 m_fields.push_back(spec);
1017 void GUIFormSpecMenu::parseField(parserData* data,std::string element,std::string type) {
1018 std::vector<std::string> parts = split(element,';');
1020 if (parts.size() == 3) {
1021 parseSimpleField(data,parts);
1025 if (parts.size() == 5) {
1026 parseTextArea(data,parts,type);
1029 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1032 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element) {
1033 std::vector<std::string> parts = split(element,';');
1035 if (parts.size() == 2) {
1036 std::vector<std::string> v_pos = split(parts[0],',');
1037 std::string text = parts[1];
1039 MY_CHECKPOS("label",0);
1041 v2s32 pos = padding;
1042 pos.X += stof(v_pos[0]) * (float)spacing.X;
1043 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1045 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
1047 if(data->bp_set != 2)
1048 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1050 text = unescape_string(text);
1052 std::wstring wlabel = narrow_to_wide(text.c_str());
1054 FieldSpec spec = FieldSpec(
1060 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1061 m_fields.push_back(spec);
1064 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1067 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element) {
1068 std::vector<std::string> parts = split(element,';');
1070 if (parts.size() == 2) {
1071 std::vector<std::string> v_pos = split(parts[0],',');
1072 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1074 MY_CHECKPOS("vertlabel",1);
1076 v2s32 pos = padding;
1077 pos.X += stof(v_pos[0]) * (float)spacing.X;
1078 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1080 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+15, pos.Y+300);
1082 if(data->bp_set != 2)
1083 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1085 std::wstring label = L"";
1087 for (unsigned int i=0; i < text.length(); i++) {
1092 FieldSpec spec = FieldSpec(
1098 gui::IGUIStaticText *t =
1099 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1100 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1101 m_fields.push_back(spec);
1104 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1107 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,std::string type) {
1108 std::vector<std::string> parts = split(element,';');
1110 if ((parts.size() == 5) || (parts.size() == 7) || (parts.size() == 8)) {
1111 std::vector<std::string> v_pos = split(parts[0],',');
1112 std::vector<std::string> v_geom = split(parts[1],',');
1113 std::string image_name = parts[2];
1114 std::string name = parts[3];
1115 std::string label = parts[4];
1117 MY_CHECKPOS("imagebutton",0);
1118 MY_CHECKGEOM("imagebutton",1);
1120 v2s32 pos = padding;
1121 pos.X += stof(v_pos[0]) * (float)spacing.X;
1122 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1124 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1125 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1127 bool noclip = false;
1128 bool drawborder = true;
1130 if ((parts.size() >= 7)) {
1131 if (parts[5] == "true")
1134 if (parts[6] == "false")
1138 std::string pressed_image_name = "";
1140 if ((parts.size() == 8)) {
1141 pressed_image_name = parts[7];
1144 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1146 if(data->bp_set != 2)
1147 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1149 image_name = unescape_string(image_name);
1150 pressed_image_name = unescape_string(pressed_image_name);
1151 label = unescape_string(label);
1153 std::wstring wlabel = narrow_to_wide(label.c_str());
1155 FieldSpec spec = FieldSpec(
1156 narrow_to_wide(name.c_str()),
1158 narrow_to_wide(image_name.c_str()),
1161 spec.ftype = f_Button;
1162 if(type == "image_button_exit")
1163 spec.is_exit = true;
1165 video::ITexture *texture = 0;
1166 video::ITexture *pressed_texture = 0;
1167 texture = m_tsrc->getTexture(image_name);
1168 if (parts.size() == 8)
1169 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1171 pressed_texture = texture;
1173 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1175 if (spec.fname == data->focused_fieldname) {
1176 Environment->setFocus(e);
1179 e->setUseAlphaChannel(true);
1180 e->setImage(texture);
1181 e->setPressedImage(pressed_texture);
1182 e->setScaleImage(true);
1183 e->setNotClipped(noclip);
1184 e->setDrawBorder(drawborder);
1186 m_fields.push_back(spec);
1190 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1193 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element) {
1194 std::vector<std::string> parts = split(element,';');
1196 if ((parts.size() == 4) || (parts.size() == 6)) {
1197 std::vector<std::string> v_pos = split(parts[0],',');
1198 std::string name = parts[1];
1199 std::vector<std::string> buttons = split(parts[2],',');
1200 std::string str_index = parts[3];
1201 bool show_background = true;
1202 bool show_border = true;
1203 int tab_index = stoi(str_index) -1;
1205 MY_CHECKPOS("tabheader",0);
1207 if (parts.size() == 6) {
1208 if (parts[4] == "true")
1209 show_background = false;
1210 if (parts[5] == "false")
1211 show_border = false;
1214 FieldSpec spec = FieldSpec(
1215 narrow_to_wide(name.c_str()),
1221 spec.ftype = f_TabHeader;
1223 v2s32 pos = padding;
1224 pos.X += stof(v_pos[0]) * (float)spacing.X;
1225 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1227 geom.X = data->screensize.Y;
1230 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1232 gui::IGUITabControl *e = Environment->addTabControl(rect,this,show_background,show_border,spec.fid);
1234 if (spec.fname == data->focused_fieldname) {
1235 Environment->setFocus(e);
1238 e->setNotClipped(true);
1240 for (unsigned int i=0; i< buttons.size(); i++) {
1241 wchar_t* wbutton = 0;
1243 std::wstring wlabel = narrow_to_wide(buttons[i]); //Needed for displaying text on windows
1244 wbutton = (wchar_t*) wlabel.c_str();
1246 e->addTab(wbutton,-1);
1249 if ((tab_index >= 0) &&
1250 (buttons.size() < INT_MAX) &&
1251 (tab_index < (int) buttons.size()))
1252 e->setActiveTab(tab_index);
1254 m_fields.push_back(spec);
1257 errorstream<< "Invalid TabHeader element(" << parts.size() << "): '" << element << "'" << std::endl;
1260 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element) {
1262 if (m_gamedef == 0) {
1263 errorstream<<"WARNING: invalid use of item_image_button with m_gamedef==0"<<std::endl;
1267 std::vector<std::string> parts = split(element,';');
1269 if (parts.size() == 5) {
1270 std::vector<std::string> v_pos = split(parts[0],',');
1271 std::vector<std::string> v_geom = split(parts[1],',');
1272 std::string item_name = parts[2];
1273 std::string name = parts[3];
1274 std::string label = parts[4];
1276 MY_CHECKPOS("itemimagebutton",0);
1277 MY_CHECKGEOM("itemimagebutton",1);
1279 v2s32 pos = padding;
1280 pos.X += stof(v_pos[0]) * (float)spacing.X;
1281 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1283 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1284 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1286 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1288 if(data->bp_set != 2)
1289 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1291 IItemDefManager *idef = m_gamedef->idef();
1293 item.deSerialize(item_name, idef);
1294 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1295 std::string tooltip = item.getDefinition(idef).description;
1297 label = unescape_string(label);
1298 FieldSpec spec = FieldSpec(
1299 narrow_to_wide(name.c_str()),
1300 narrow_to_wide(label.c_str()),
1301 narrow_to_wide(item_name.c_str()),
1305 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1307 if (spec.fname == data->focused_fieldname) {
1308 Environment->setFocus(e);
1311 e->setUseAlphaChannel(true);
1312 e->setImage(texture);
1313 e->setPressedImage(texture);
1314 e->setScaleImage(true);
1315 spec.ftype = f_Button;
1316 rect+=data->basepos-padding;
1319 spec.tooltip=tooltip;
1320 m_fields.push_back(spec);
1323 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1326 void GUIFormSpecMenu::parseBox(parserData* data,std::string element) {
1327 std::vector<std::string> parts = split(element,';');
1329 if (parts.size() == 3) {
1330 std::vector<std::string> v_pos = split(parts[0],',');
1331 std::vector<std::string> v_geom = split(parts[1],',');
1332 std::string color_str = parts[2];
1334 MY_CHECKPOS("box",0);
1335 MY_CHECKGEOM("box",1);
1337 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1338 pos.X += stof(v_pos[0]) * (float) spacing.X;
1339 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1342 geom.X = stof(v_geom[0]) * (float)spacing.X;
1343 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1345 irr::video::SColor color;
1347 if (parseColor(color_str, color)) {
1348 BoxDrawSpec spec(pos,geom,color);
1350 m_boxes.push_back(spec);
1353 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1357 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1360 void GUIFormSpecMenu::parseElement(parserData* data,std::string element) {
1366 std::vector<std::string> parts = split(element,'[');
1368 // ugly workaround to keep compatibility
1369 if (parts.size() > 2) {
1370 if (trim(parts[0]) == "image") {
1371 for (unsigned int i=2;i< parts.size(); i++) {
1372 parts[1] += "[" + parts[i];
1378 if (parts.size() < 2) {
1382 std::string type = trim(parts[0]);
1383 std::string description = trim(parts[1]);
1385 if ((type == "size") || (type == "invsize")){
1386 parseSize(data,description);
1390 if (type == "list") {
1391 parseList(data,description);
1395 if (type == "checkbox") {
1396 parseCheckbox(data,description);
1400 if (type == "image") {
1401 parseImage(data,description);
1405 if (type == "item_image") {
1406 parseItemImage(data,description);
1410 if ((type == "button") || (type == "button_exit")) {
1411 parseButton(data,description,type);
1415 if (type == "background") {
1416 parseBackground(data,description);
1420 if (type == "textlist"){
1421 parseTextList(data,description);
1425 if (type == "dropdown"){
1426 parseDropDown(data,description);
1430 if (type == "pwdfield") {
1431 parsePwdField(data,description);
1435 if ((type == "field") || (type == "textarea")){
1436 parseField(data,description,type);
1440 if (type == "label") {
1441 parseLabel(data,description);
1445 if (type == "vertlabel") {
1446 parseVertLabel(data,description);
1450 if (type == "item_image_button") {
1451 parseItemImageButton(data,description);
1455 if ((type == "image_button") || (type == "image_button_exit")) {
1456 parseImageButton(data,description,type);
1460 if (type == "tabheader") {
1461 parseTabHeader(data,description);
1465 if (type == "box") {
1466 parseBox(data,description);
1472 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1478 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1482 //preserve listboxes
1483 for (unsigned int i = 0; i < m_listboxes.size(); i++) {
1484 std::wstring listboxname = m_listboxes[i].first.fname;
1485 gui::IGUIListBox *listbox = m_listboxes[i].second;
1487 int selection = listbox->getSelected();
1488 if (selection != -1) {
1489 mydata.listbox_selections[listboxname] = selection;
1492 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(listbox);
1494 mydata.listbox_scroll[listboxname] = scrollbar->getPos();
1499 gui::IGUIElement *focused_element = Environment->getFocus();
1500 if (focused_element && focused_element->getParent() == this) {
1501 s32 focused_id = focused_element->getID();
1502 if (focused_id > 257) {
1503 for (u32 i=0; i<m_fields.size(); i++) {
1504 if (m_fields[i].fid == focused_id) {
1505 mydata.focused_fieldname =
1516 mydata.size= v2s32(100,100);
1517 mydata.helptext_h = 15;
1518 mydata.screensize = screensize;
1520 // Base position of contents of form
1521 mydata.basepos = getBasePos();
1523 // State of basepos, 0 = not set, 1= set by formspec, 2 = set by size[] element
1524 // Used to adjust form size automatically if needed
1525 // A proceed button is added if there is no size[] element
1529 /* Convert m_init_draw_spec to m_inventorylists */
1531 m_inventorylists.clear();
1533 m_backgrounds.clear();
1534 m_itemimages.clear();
1535 m_listboxes.clear();
1536 m_checkboxes.clear();
1541 std::vector<std::string> elements = split(m_formspec_string,']');
1543 for (unsigned int i=0;i< elements.size();i++) {
1544 parseElement(&mydata,elements[i]);
1547 // If there's inventory, put the usage string at the bottom
1548 if (m_inventorylists.size())
1551 core::rect<s32> rect(0, 0, mydata.size.X-padding.X*2, mydata.helptext_h);
1552 rect = rect + v2s32((mydata.size.X/2 - mydata.rect.getWidth()/2) +5,
1553 mydata.size.Y-5-mydata.helptext_h);
1554 const wchar_t *text = wgettext("Left click: Move all items, Right click: Move single item");
1555 Environment->addStaticText(text, rect, false, true, this, 256);
1559 // If there's fields, add a Proceed button
1560 if (m_fields.size() && mydata.bp_set != 2)
1562 // if the size wasn't set by an invsize[] or size[] adjust it now to fit all the fields
1563 mydata.rect = core::rect<s32>(
1564 mydata.screensize.X/2 - 580/2,
1565 mydata.screensize.Y/2 - 300/2,
1566 mydata.screensize.X/2 + 580/2,
1567 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1569 DesiredRect = mydata.rect;
1570 recalculateAbsolutePosition(false);
1571 mydata.basepos = getBasePos();
1575 v2s32 pos = mydata.basepos;
1576 pos.Y = ((m_fields.size()+2)*60);
1578 v2s32 size = DesiredRect.getSize();
1579 mydata.rect = core::rect<s32>(size.X/2-70, pos.Y, (size.X/2-70)+140, pos.Y+30);
1580 wchar_t* text = wgettext("Proceed");
1581 Environment->addButton(mydata.rect, this, 257, text);
1588 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1589 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1590 m_tooltip_element->enableOverrideColor(true);
1591 m_tooltip_element->setBackgroundColor(video::SColor(255,110,130,60));
1592 m_tooltip_element->setDrawBackground(true);
1593 m_tooltip_element->setDrawBorder(true);
1594 m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
1595 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1596 m_tooltip_element->setWordWrap(false);
1597 //we're not parent so no autograb for this one!
1598 m_tooltip_element->grab();
1601 //set initial focus if parser didn't set it
1602 focused_element = Environment->getFocus();
1603 if (!focused_element
1604 || !isMyChild(focused_element)
1605 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
1609 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
1611 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1613 for(u32 i=0; i<m_inventorylists.size(); i++)
1615 const ListDrawSpec &s = m_inventorylists[i];
1617 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1619 s32 item_i = i + s.start_item_i;
1620 s32 x = (i%s.geom.X) * spacing.X;
1621 s32 y = (i/s.geom.X) * spacing.Y;
1623 core::rect<s32> rect = imgrect + s.pos + p0;
1624 if(rect.isPointInside(p))
1626 return ItemSpec(s.inventoryloc, s.listname, item_i);
1631 return ItemSpec(InventoryLocation(), "", -1);
1634 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
1636 video::IVideoDriver* driver = Environment->getVideoDriver();
1639 gui::IGUIFont *font = NULL;
1640 gui::IGUISkin* skin = Environment->getSkin();
1642 font = skin->getFont();
1644 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1646 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1647 <<"The inventory location "
1648 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
1652 InventoryList *ilist = inv->getList(s.listname);
1654 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1655 <<"The inventory list \""<<s.listname<<"\" @ \""
1656 <<s.inventoryloc.dump()<<"\" doesn't exist"
1661 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1663 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1665 s32 item_i = i + s.start_item_i;
1666 if(item_i >= (s32) ilist->getSize())
1668 s32 x = (i%s.geom.X) * spacing.X;
1669 s32 y = (i/s.geom.X) * spacing.Y;
1671 core::rect<s32> rect = imgrect + s.pos + p;
1674 item = ilist->getItem(item_i);
1676 bool selected = m_selected_item
1677 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
1678 && m_selected_item->listname == s.listname
1679 && m_selected_item->i == item_i;
1680 bool hovering = rect.isPointInside(m_pointer);
1684 if(hovering && m_selected_item)
1686 video::SColor bgcolor(255,192,192,192);
1687 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
1691 video::SColor bgcolor(255,128,128,128);
1692 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
1701 item.takeItem(m_selected_amount);
1705 drawItemStack(driver, font, item,
1706 rect, &AbsoluteClippingRect, m_gamedef);
1710 std::string tooltip_text = "";
1711 if(hovering && !m_selected_item)
1712 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
1713 if(tooltip_text != "")
1715 m_tooltip_element->setVisible(true);
1716 this->bringToFront(m_tooltip_element);
1717 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
1718 s32 tooltip_x = m_pointer.X + 15;
1719 s32 tooltip_y = m_pointer.Y + 15;
1720 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1721 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1722 m_tooltip_element->setRelativePosition(core::rect<s32>(
1723 core::position2d<s32>(tooltip_x, tooltip_y),
1724 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1730 void GUIFormSpecMenu::drawSelectedItem()
1732 if(!m_selected_item)
1735 video::IVideoDriver* driver = Environment->getVideoDriver();
1738 gui::IGUIFont *font = NULL;
1739 gui::IGUISkin* skin = Environment->getSkin();
1741 font = skin->getFont();
1743 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
1745 InventoryList *list = inv->getList(m_selected_item->listname);
1747 ItemStack stack = list->getItem(m_selected_item->i);
1748 stack.count = m_selected_amount;
1750 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1751 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
1752 drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
1755 void GUIFormSpecMenu::drawMenu()
1758 std::string newform = m_form_src->getForm();
1759 if(newform != m_formspec_string){
1760 m_formspec_string = newform;
1761 regenerateGui(m_screensize_old);
1765 m_pointer = m_device->getCursorControl()->getPosition();
1767 updateSelectedItem();
1769 gui::IGUISkin* skin = Environment->getSkin();
1772 video::IVideoDriver* driver = Environment->getVideoDriver();
1774 video::SColor bgcolor(140,0,0,0);
1775 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
1777 m_tooltip_element->setVisible(false);
1782 for(u32 i=0; i<m_backgrounds.size(); i++)
1784 const ImageDrawSpec &spec = m_backgrounds[i];
1785 video::ITexture *texture = m_tsrc->getTexture(spec.name);
1788 // Image size on screen
1789 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1790 // Image rectangle on screen
1791 core::rect<s32> rect = imgrect + spec.pos;
1792 const video::SColor color(255,255,255,255);
1793 const video::SColor colors[] = {color,color,color,color};
1794 driver->draw2DImage(texture, rect,
1795 core::rect<s32>(core::position2d<s32>(0,0),
1796 core::dimension2di(texture->getOriginalSize())),
1797 NULL/*&AbsoluteClippingRect*/, colors, true);
1800 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
1801 errorstream << "\t" << spec.name << std::endl;
1808 for(u32 i=0; i<m_boxes.size(); i++)
1810 const BoxDrawSpec &spec = m_boxes[i];
1812 irr::video::SColor todraw = spec.color;
1814 todraw.setAlpha(140);
1816 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
1817 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
1819 driver->draw2DRectangle(todraw, rect, 0);
1824 for(u32 i=0; i<m_images.size(); i++)
1826 const ImageDrawSpec &spec = m_images[i];
1827 video::ITexture *texture = m_tsrc->getTexture(spec.name);
1830 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
1831 // Image size on screen
1832 core::rect<s32> imgrect;
1835 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
1838 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
1840 // Image rectangle on screen
1841 core::rect<s32> rect = imgrect + spec.pos;
1842 const video::SColor color(255,255,255,255);
1843 const video::SColor colors[] = {color,color,color,color};
1844 driver->draw2DImage(texture, rect,
1845 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
1846 NULL/*&AbsoluteClippingRect*/, colors, true);
1849 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
1850 errorstream << "\t" << spec.name << std::endl;
1857 for(u32 i=0; i<m_itemimages.size(); i++)
1862 const ImageDrawSpec &spec = m_itemimages[i];
1863 IItemDefManager *idef = m_gamedef->idef();
1865 item.deSerialize(spec.name, idef);
1866 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1867 // Image size on screen
1868 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1869 // Image rectangle on screen
1870 core::rect<s32> rect = imgrect + spec.pos;
1871 const video::SColor color(255,255,255,255);
1872 const video::SColor colors[] = {color,color,color,color};
1873 driver->draw2DImage(texture, rect,
1874 core::rect<s32>(core::position2d<s32>(0,0),
1875 core::dimension2di(texture->getOriginalSize())),
1876 NULL/*&AbsoluteClippingRect*/, colors, true);
1881 Phase 0: Item slot rectangles
1882 Phase 1: Item images; prepare tooltip
1883 If backgrounds used, do not draw Item slot rectangles
1886 if (m_backgrounds.size() > 0) start_phase=1;
1887 for(int phase=start_phase; phase<=1; phase++)
1888 for(u32 i=0; i<m_inventorylists.size(); i++)
1890 drawList(m_inventorylists[i], phase);
1896 gui::IGUIElement::draw();
1899 Draw fields/buttons tooltips
1901 for(u32 i=0; i<m_fields.size(); i++)
1903 const FieldSpec &spec = m_fields[i];
1904 if (spec.tooltip != "")
1906 core::rect<s32> rect = spec.rect;
1907 if (rect.isPointInside(m_pointer))
1909 m_tooltip_element->setVisible(true);
1910 this->bringToFront(m_tooltip_element);
1911 m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
1912 s32 tooltip_x = m_pointer.X + 15;
1913 s32 tooltip_y = m_pointer.Y + 15;
1914 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1915 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1916 m_tooltip_element->setRelativePosition(core::rect<s32>(
1917 core::position2d<s32>(tooltip_x, tooltip_y),
1918 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1924 Draw dragged item stack
1929 void GUIFormSpecMenu::updateSelectedItem()
1931 // If the selected stack has become empty for some reason, deselect it.
1932 // If the selected stack has become inaccessible, deselect it.
1933 // If the selected stack has become smaller, adjust m_selected_amount.
1934 ItemStack selected = verifySelectedItem();
1936 // WARNING: BLACK MAGIC
1937 // See if there is a stack suited for our current guess.
1938 // If such stack does not exist, clear the guess.
1939 if(m_selected_content_guess.name != "" &&
1940 selected.name == m_selected_content_guess.name &&
1941 selected.count == m_selected_content_guess.count){
1942 // Selected item fits the guess. Skip the black magic.
1944 else if(m_selected_content_guess.name != ""){
1946 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
1947 const ListDrawSpec &s = m_inventorylists[i];
1948 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1951 InventoryList *list = inv->getList(s.listname);
1954 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
1955 u32 item_i = i + s.start_item_i;
1956 if(item_i >= list->getSize())
1958 ItemStack stack = list->getItem(item_i);
1959 if(stack.name == m_selected_content_guess.name &&
1960 stack.count == m_selected_content_guess.count){
1962 infostream<<"Client: Changing selected content guess to "
1963 <<s.inventoryloc.dump()<<" "<<s.listname
1964 <<" "<<item_i<<std::endl;
1965 delete m_selected_item;
1966 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
1967 m_selected_amount = stack.count;
1972 infostream<<"Client: Discarding selected content guess: "
1973 <<m_selected_content_guess.getItemString()<<std::endl;
1974 m_selected_content_guess.name = "";
1978 // If craftresult is nonempty and nothing else is selected, select it now.
1979 if(!m_selected_item)
1981 for(u32 i=0; i<m_inventorylists.size(); i++)
1983 const ListDrawSpec &s = m_inventorylists[i];
1984 if(s.listname == "craftpreview")
1986 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1987 InventoryList *list = inv->getList("craftresult");
1988 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
1990 m_selected_item = new ItemSpec;
1991 m_selected_item->inventoryloc = s.inventoryloc;
1992 m_selected_item->listname = "craftresult";
1993 m_selected_item->i = 0;
1994 m_selected_amount = 0;
1995 m_selected_dragging = false;
2002 // If craftresult is selected, keep the whole stack selected
2003 if(m_selected_item && m_selected_item->listname == "craftresult")
2005 m_selected_amount = verifySelectedItem().count;
2009 ItemStack GUIFormSpecMenu::verifySelectedItem()
2011 // If the selected stack has become empty for some reason, deselect it.
2012 // If the selected stack has become inaccessible, deselect it.
2013 // If the selected stack has become smaller, adjust m_selected_amount.
2014 // Return the selected stack.
2018 if(m_selected_item->isValid())
2020 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2023 InventoryList *list = inv->getList(m_selected_item->listname);
2024 if(list && (u32) m_selected_item->i < list->getSize())
2026 ItemStack stack = list->getItem(m_selected_item->i);
2027 if(m_selected_amount > stack.count)
2028 m_selected_amount = stack.count;
2035 // selection was not valid
2036 delete m_selected_item;
2037 m_selected_item = NULL;
2038 m_selected_amount = 0;
2039 m_selected_dragging = false;
2044 void GUIFormSpecMenu::acceptInput(bool quit=false)
2048 std::map<std::string, std::string> fields;
2051 fields["quit"] = "true";
2054 if (current_keys_pending.key_down) {
2055 fields["key_down"] = "true";
2056 current_keys_pending.key_down = false;
2059 if (current_keys_pending.key_up) {
2060 fields["key_up"] = "true";
2061 current_keys_pending.key_up = false;
2064 if (current_keys_pending.key_enter) {
2065 fields["key_enter"] = "true";
2066 current_keys_pending.key_enter = false;
2069 if (current_keys_pending.key_escape) {
2070 fields["key_escape"] = "true";
2071 current_keys_pending.key_escape = false;
2074 for(u32 i=0; i<m_fields.size(); i++)
2076 const FieldSpec &s = m_fields[i];
2079 if(s.ftype == f_Button)
2081 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(s.flabel.c_str());
2083 else if(s.ftype == f_ListBox) {
2084 std::stringstream ss;
2086 if (m_listbox_doubleclick) {
2092 ss << (getListboxIndex(wide_to_narrow(s.fname.c_str()))+1);
2093 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2095 else if(s.ftype == f_DropDown) {
2096 // no dynamic cast possible due to some distributions shipped
2097 // without rtti support in irrlicht
2098 IGUIElement * element = getElementFromId(s.fid);
2099 gui::IGUIComboBox *e = NULL;
2100 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2101 e = static_cast<gui::IGUIComboBox*>(element);
2103 fields[wide_to_narrow(s.fname.c_str())] =
2104 wide_to_narrow(e->getItem(e->getSelected()));
2106 else if (s.ftype == f_TabHeader) {
2107 // no dynamic cast possible due to some distributions shipped
2108 // without rtti support in irrlicht
2109 IGUIElement * element = getElementFromId(s.fid);
2110 gui::IGUITabControl *e = NULL;
2111 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2112 e = static_cast<gui::IGUITabControl*>(element);
2116 std::stringstream ss;
2117 ss << (e->getActiveTab() +1);
2118 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2121 else if (s.ftype == f_CheckBox) {
2122 // no dynamic cast possible due to some distributions shipped
2123 // without rtti support in irrlicht
2124 IGUIElement * element = getElementFromId(s.fid);
2125 gui::IGUICheckBox *e = NULL;
2126 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2127 e = static_cast<gui::IGUICheckBox*>(element);
2132 fields[wide_to_narrow(s.fname.c_str())] = "true";
2134 fields[wide_to_narrow(s.fname.c_str())] = "false";
2139 IGUIElement* e = getElementFromId(s.fid);
2142 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(e->getText());
2148 m_text_dst->gotText(fields);
2152 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2154 // Fix Esc/Return key being eaten by checkboxen and listboxen
2155 if(event.EventType==EET_KEY_INPUT_EVENT)
2157 KeyPress kp(event.KeyInput);
2158 if (kp == EscapeKey || kp == getKeySetting("keymap_inventory")
2159 || event.KeyInput.Key==KEY_RETURN)
2161 gui::IGUIElement *focused = Environment->getFocus();
2162 if (focused && isMyChild(focused) &&
2163 (focused->getType() == gui::EGUIET_LIST_BOX ||
2164 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2170 // Mouse wheel events: send to hovered element instead of focused
2171 if(event.EventType==EET_MOUSE_INPUT_EVENT
2172 && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
2174 s32 x = event.MouseInput.X;
2175 s32 y = event.MouseInput.Y;
2176 gui::IGUIElement *hovered =
2177 Environment->getRootGUIElement()->getElementFromPoint(
2178 core::position2d<s32>(x, y));
2179 if (hovered && isMyChild(hovered)) {
2180 hovered->OnEvent(event);
2187 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2189 if(event.EventType==EET_KEY_INPUT_EVENT)
2191 KeyPress kp(event.KeyInput);
2192 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
2193 kp == getKeySetting("keymap_inventory")))
2199 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
2203 if (event.KeyInput.PressedDown &&
2204 (event.KeyInput.Key==KEY_RETURN ||
2205 event.KeyInput.Key==KEY_UP ||
2206 event.KeyInput.Key==KEY_DOWN)
2210 switch (event.KeyInput.Key) {
2217 current_keys_pending.key_enter = true;
2220 current_keys_pending.key_up = true;
2223 current_keys_pending.key_down = true;
2227 //can't happen at all!
2228 assert("reached a source line that can't ever been reached" == 0);
2236 if(event.EventType==EET_MOUSE_INPUT_EVENT
2237 && event.MouseInput.Event != EMIE_MOUSE_MOVED)
2239 // Mouse event other than movement
2241 // Get selected item and hovered/clicked item (s)
2243 updateSelectedItem();
2244 ItemSpec s = getItemAtPos(m_pointer);
2246 Inventory *inv_selected = NULL;
2247 Inventory *inv_s = NULL;
2251 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
2252 assert(inv_selected);
2253 assert(inv_selected->getList(m_selected_item->listname) != NULL);
2260 inv_s = m_invmgr->getInventory(s.inventoryloc);
2263 errorstream<<"InventoryMenu: The selected inventory location "
2264 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2266 s.i = -1; // make it invalid again
2270 InventoryList *list = inv_s->getList(s.listname);
2272 verbosestream<<"InventoryMenu: The selected inventory list \""
2273 <<s.listname<<"\" does not exist"<<std::endl;
2274 s.i = -1; // make it invalid again
2278 if((u32)s.i >= list->getSize()){
2279 infostream<<"InventoryMenu: The selected inventory list \""
2280 <<s.listname<<"\" is too small (i="<<s.i<<", size="
2281 <<list->getSize()<<")"<<std::endl;
2282 s.i = -1; // make it invalid again
2286 s_count = list->getItem(s.i).count;
2289 bool identical = (m_selected_item != NULL) && s.isValid() &&
2290 (inv_selected == inv_s) &&
2291 (m_selected_item->listname == s.listname) &&
2292 (m_selected_item->i == s.i);
2294 // buttons: 0 = left, 1 = right, 2 = middle
2295 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
2298 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
2299 { button = 0; updown = 0; }
2300 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
2301 { button = 1; updown = 0; }
2302 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
2303 { button = 2; updown = 0; }
2304 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
2305 { button = 0; updown = 1; }
2306 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
2307 { button = 1; updown = 1; }
2308 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
2309 { button = 2; updown = 1; }
2311 // Set this number to a positive value to generate a move action
2312 // from m_selected_item to s.
2313 u32 move_amount = 0;
2315 // Set this number to a positive value to generate a drop action
2316 // from m_selected_item.
2317 u32 drop_amount = 0;
2319 // Set this number to a positive value to generate a craft action at s.
2320 u32 craft_amount = 0;
2324 // Some mouse button has been pressed
2326 //infostream<<"Mouse button "<<button<<" pressed at p=("
2327 // <<p.X<<","<<p.Y<<")"<<std::endl;
2329 m_selected_dragging = false;
2331 if(s.isValid() && s.listname == "craftpreview")
2333 // Craft preview has been clicked: craft
2334 craft_amount = (button == 2 ? 10 : 1);
2336 else if(m_selected_item == NULL)
2340 // Non-empty stack has been clicked: select it
2341 m_selected_item = new ItemSpec(s);
2343 if(button == 1) // right
2344 m_selected_amount = (s_count + 1) / 2;
2345 else if(button == 2) // middle
2346 m_selected_amount = MYMIN(s_count, 10);
2348 m_selected_amount = s_count;
2350 m_selected_dragging = true;
2353 else // m_selected_item != NULL
2355 assert(m_selected_amount >= 1);
2359 // Clicked a slot: move
2360 if(button == 1) // right
2362 else if(button == 2) // middle
2363 move_amount = MYMIN(m_selected_amount, 10);
2365 move_amount = m_selected_amount;
2369 if(move_amount >= m_selected_amount)
2370 m_selected_amount = 0;
2372 m_selected_amount -= move_amount;
2376 else if(getAbsoluteClippingRect().isPointInside(m_pointer))
2378 // Clicked somewhere else: deselect
2379 m_selected_amount = 0;
2383 // Clicked outside of the window: drop
2384 if(button == 1) // right
2386 else if(button == 2) // middle
2387 drop_amount = MYMIN(m_selected_amount, 10);
2389 drop_amount = m_selected_amount;
2393 else if(updown == 1)
2395 // Some mouse button has been released
2397 //infostream<<"Mouse button "<<button<<" released at p=("
2398 // <<p.X<<","<<p.Y<<")"<<std::endl;
2400 if(m_selected_item != NULL && m_selected_dragging && s.isValid())
2404 // Dragged to different slot: move all selected
2405 move_amount = m_selected_amount;
2408 else if(m_selected_item != NULL && m_selected_dragging &&
2409 !(getAbsoluteClippingRect().isPointInside(m_pointer)))
2411 // Dragged outside of window: drop all selected
2412 drop_amount = m_selected_amount;
2415 m_selected_dragging = false;
2418 // Possibly send inventory action to server
2421 // Send IACTION_MOVE
2423 assert(m_selected_item && m_selected_item->isValid());
2424 assert(s.isValid());
2426 assert(inv_selected && inv_s);
2427 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2428 InventoryList *list_to = inv_s->getList(s.listname);
2429 assert(list_from && list_to);
2430 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2431 ItemStack stack_to = list_to->getItem(s.i);
2433 // Check how many items can be moved
2434 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
2435 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
2436 // If source stack cannot be added to destination stack at all,
2438 if(leftover.count == stack_from.count && leftover.name == stack_from.name)
2440 m_selected_amount = stack_to.count;
2441 // In case the server doesn't directly swap them but instead
2442 // moves stack_to somewhere else, set this
2443 m_selected_content_guess = stack_to;
2444 m_selected_content_guess_inventory = s.inventoryloc;
2446 // Source stack goes fully into destination stack
2447 else if(leftover.empty())
2449 m_selected_amount -= move_amount;
2450 m_selected_content_guess = ItemStack(); // Clear
2452 // Source stack goes partly into destination stack
2455 move_amount -= leftover.count;
2456 m_selected_amount -= move_amount;
2457 m_selected_content_guess = ItemStack(); // Clear
2460 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
2461 IMoveAction *a = new IMoveAction();
2462 a->count = move_amount;
2463 a->from_inv = m_selected_item->inventoryloc;
2464 a->from_list = m_selected_item->listname;
2465 a->from_i = m_selected_item->i;
2466 a->to_inv = s.inventoryloc;
2467 a->to_list = s.listname;
2469 m_invmgr->inventoryAction(a);
2471 else if(drop_amount > 0)
2473 m_selected_content_guess = ItemStack(); // Clear
2475 // Send IACTION_DROP
2477 assert(m_selected_item && m_selected_item->isValid());
2478 assert(inv_selected);
2479 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2481 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2483 // Check how many items can be dropped
2484 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
2485 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
2486 m_selected_amount -= drop_amount;
2488 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
2489 IDropAction *a = new IDropAction();
2490 a->count = drop_amount;
2491 a->from_inv = m_selected_item->inventoryloc;
2492 a->from_list = m_selected_item->listname;
2493 a->from_i = m_selected_item->i;
2494 m_invmgr->inventoryAction(a);
2496 else if(craft_amount > 0)
2498 m_selected_content_guess = ItemStack(); // Clear
2500 // Send IACTION_CRAFT
2502 assert(s.isValid());
2505 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
2506 ICraftAction *a = new ICraftAction();
2507 a->count = craft_amount;
2508 a->craft_inv = s.inventoryloc;
2509 m_invmgr->inventoryAction(a);
2512 // If m_selected_amount has been decreased to zero, deselect
2513 if(m_selected_amount == 0)
2515 delete m_selected_item;
2516 m_selected_item = NULL;
2517 m_selected_amount = 0;
2518 m_selected_dragging = false;
2519 m_selected_content_guess = ItemStack();
2522 if(event.EventType==EET_GUI_EVENT)
2525 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
2528 // find the element that was clicked
2529 for(u32 i=0; i<m_fields.size(); i++)
2531 FieldSpec &s = m_fields[i];
2532 // if its a button, set the send field so
2533 // lua knows which button was pressed
2534 if ((s.ftype == f_TabHeader) && (s.fid == event.GUIEvent.Caller->getID()))
2543 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
2546 if(!canTakeFocus(event.GUIEvent.Element))
2548 infostream<<"GUIFormSpecMenu: Not allowing focus change."
2550 // Returning true disables focus change
2554 if((event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) ||
2555 (event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED))
2557 unsigned int btn_id = event.GUIEvent.Caller->getID();
2559 if (btn_id == 257) {
2565 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2567 // quitMenu deallocates menu
2571 // find the element that was clicked
2572 for(u32 i=0; i<m_fields.size(); i++)
2574 FieldSpec &s = m_fields[i];
2575 // if its a button, set the send field so
2576 // lua knows which button was pressed
2577 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
2578 (s.fid == event.GUIEvent.Caller->getID()))
2587 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2597 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
2599 if(event.GUIEvent.Caller->getID() > 257)
2607 current_keys_pending.key_enter = true;
2610 // quitMenu deallocates menu
2615 if((event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN) ||
2616 (event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED))
2618 int current_id = event.GUIEvent.Caller->getID();
2619 if(current_id > 257)
2621 // find the element that was clicked
2622 for(u32 i=0; i<m_fields.size(); i++)
2624 FieldSpec &s = m_fields[i];
2625 // if its a listbox, set the send field so
2626 // lua knows which listbox was changed
2627 // checkListboxClick() is black magic
2628 // for properly handling double clicks
2629 if ((s.ftype == f_ListBox) && (s.fid == current_id)
2630 && checkListboxClick(s.fname,
2631 event.GUIEvent.EventType))
2643 return Parent ? Parent->OnEvent(event) : false;
2646 bool GUIFormSpecMenu::parseColor(std::string color, irr::video::SColor& outcolor) {
2647 outcolor = irr::video::SColor(0,0,0,0);
2649 if (!string_allowed(color, "0123456789abcdefABCDEF"))
2653 std::istringstream iss(color);
2654 iss >> std::hex >> color_value;
2656 outcolor = irr::video::SColor(color_value);
2658 outcolor.setAlpha(255);