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,
74 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
80 m_selected_item(NULL),
82 m_selected_dragging(false),
83 m_listbox_click_fname(),
84 m_listbox_click_index(-1),
85 m_listbox_click_time(0),
86 m_listbox_doubleclick(false),
87 m_tooltip_element(NULL),
91 current_keys_pending.key_down = false;
92 current_keys_pending.key_up = false;
93 current_keys_pending.key_enter = false;
94 current_keys_pending.key_escape = false;
98 GUIFormSpecMenu::~GUIFormSpecMenu()
102 delete m_selected_item;
107 void GUIFormSpecMenu::removeChildren()
109 const core::list<gui::IGUIElement*> &children = getChildren();
110 core::list<gui::IGUIElement*> children_copy;
111 for(core::list<gui::IGUIElement*>::ConstIterator
112 i = children.begin(); i != children.end(); i++)
114 children_copy.push_back(*i);
116 for(core::list<gui::IGUIElement*>::Iterator
117 i = children_copy.begin();
118 i != children_copy.end(); i++)
123 gui::IGUIElement *e = getElementFromId(256);
127 if(m_tooltip_element)
129 m_tooltip_element->remove();
130 m_tooltip_element = NULL;
134 void GUIFormSpecMenu::setInitialFocus()
136 // Set initial focus according to following order of precedence:
137 // 1. first empty editbox
141 // 5. first focusable (not statictext, not tabheader)
142 // 6. first child element
144 core::list<gui::IGUIElement*> children = getChildren();
146 // in case "children" contains any NULL elements, remove them
147 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
148 it != children.end();) {
152 it = children.erase(it);
155 // 1. first empty editbox
156 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
157 it != children.end(); ++it) {
158 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
159 && (*it)->getText()[0] == 0) {
160 Environment->setFocus(*it);
166 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
167 it != children.end(); ++it) {
168 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
169 Environment->setFocus(*it);
175 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
176 it != children.end(); ++it) {
177 if ((*it)->getType() == gui::EGUIET_LIST_BOX) {
178 Environment->setFocus(*it);
184 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
185 it != children.end(); --it) {
186 if ((*it)->getType() == gui::EGUIET_BUTTON) {
187 Environment->setFocus(*it);
192 // 5. first focusable (not statictext, not tabheader)
193 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
194 it != children.end(); ++it) {
195 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
196 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
197 Environment->setFocus(*it);
202 // 6. first child element
203 if (children.empty())
204 Environment->setFocus(this);
206 Environment->setFocus(*(children.begin()));
209 int GUIFormSpecMenu::getListboxIndex(std::string listboxname) {
211 std::wstring wlistboxname = narrow_to_wide(listboxname.c_str());
213 for(unsigned int i=0; i < m_listboxes.size(); i++) {
215 std::wstring name(m_listboxes[i].first.fname.c_str());
216 if ( name == wlistboxname) {
217 return m_listboxes[i].second->getSelected();
223 bool GUIFormSpecMenu::checkListboxClick(std::wstring wlistboxname,
226 // WARNING: BLACK IRRLICHT MAGIC
227 // Used to fix Irrlicht's subpar reporting of single clicks and double
228 // clicks in listboxes (gui::EGET_LISTBOX_CHANGED,
229 // gui::EGET_LISTBOX_SELECTED_AGAIN):
230 // 1. IGUIListBox::setSelected() is counted as a click.
231 // Including the initial setSelected() done by parseTextList().
232 // 2. Clicking on a the selected item and then dragging for less
233 // than 500ms is counted as a doubleclick, no matter when the
234 // item was previously selected (e.g. more than 500ms ago)
236 // So when Irrlicht reports a doubleclick, we need to check
237 // for ourselves if really was a doubleclick. Or just a fake.
239 for(unsigned int i=0; i < m_listboxes.size(); i++) {
240 std::wstring name(m_listboxes[i].first.fname.c_str());
241 int selected = m_listboxes[i].second->getSelected();
242 if (name == wlistboxname && selected >= 0) {
243 u32 now = getTimeMs();
245 (eventtype == gui::EGET_LISTBOX_SELECTED_AGAIN)
246 && (name == m_listbox_click_fname)
247 && (selected == m_listbox_click_index)
248 && (m_listbox_click_time >= now - 500);
249 m_listbox_click_fname = name;
250 m_listbox_click_index = selected;
251 m_listbox_click_time = now;
252 m_listbox_doubleclick = doubleclick;
259 gui::IGUIScrollBar* GUIFormSpecMenu::getListboxScrollbar(
260 gui::IGUIListBox *listbox)
262 // WARNING: BLACK IRRLICHT MAGIC
263 // Ordinarily, due to how formspecs work (recreating the entire GUI
264 // when something changes), when you select an item in a textlist
265 // with more items than fit in the visible area, the newly selected
266 // item is scrolled to the bottom of the visible area. This is
267 // annoying and breaks GUI designs that use double clicks.
269 // This function helps fixing this problem by giving direct access
270 // to a listbox's scrollbar. This works because CGUIListBox doesn't
271 // cache the scrollbar position anywhere.
273 // If this stops working in a future irrlicht version, consider
274 // maintaining a local copy of irr::gui::CGUIListBox, possibly also
275 // fixing the other reasons why black irrlicht magic is needed.
277 core::list<gui::IGUIElement*> children = listbox->getChildren();
278 for(core::list<gui::IGUIElement*>::Iterator it = children.begin();
279 it != children.end(); ++it) {
280 gui::IGUIElement* child = *it;
281 if (child && child->getType() == gui::EGUIET_SCROLL_BAR) {
282 return static_cast<gui::IGUIScrollBar*>(child);
286 verbosestream<<"getListboxScrollbar: WARNING: "
287 <<"listbox has no scrollbar"<<std::endl;
291 std::vector<std::string> split(const std::string &s, char delim) {
292 std::vector<std::string> tokens;
294 std::string current = "";
295 bool last_was_escape = false;
296 for(unsigned int i=0; i < s.size(); i++) {
297 if (last_was_escape) {
299 current += s.c_str()[i];
300 last_was_escape = false;
303 if (s.c_str()[i] == delim) {
304 tokens.push_back(current);
306 last_was_escape = false;
308 else if (s.c_str()[i] == '\\'){
309 last_was_escape = true;
312 current += s.c_str()[i];
313 last_was_escape = false;
318 tokens.push_back(current);
323 void GUIFormSpecMenu::parseSize(parserData* data,std::string element) {
324 std::vector<std::string> parts = split(element,',');
326 if (parts.size() == 2) {
329 if (parts[1].find(';') != std::string::npos)
330 parts[1] = parts[1].substr(0,parts[1].find(';'));
332 invsize.X = stof(parts[0]);
333 invsize.Y = stof(parts[1]);
336 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
337 v2u32 delta = current_screensize - m_lockscreensize;
339 if (current_screensize.Y > m_lockscreensize.Y)
344 if (current_screensize.X > m_lockscreensize.X)
349 offset = v2s32(delta.X,delta.Y);
351 data->screensize = m_lockscreensize;
357 padding = v2s32(data->screensize.Y/40, data->screensize.Y/40);
358 spacing = v2s32(data->screensize.Y/12, data->screensize.Y/13);
359 imgsize = v2s32(data->screensize.Y/15, data->screensize.Y/15);
361 padding.X*2+spacing.X*(invsize.X-1.0)+imgsize.X,
362 padding.Y*2+spacing.Y*(invsize.Y-1.0)+imgsize.Y + (data->helptext_h-5)
364 data->rect = core::rect<s32>(
365 data->screensize.X/2 - data->size.X/2 + offset.X,
366 data->screensize.Y/2 - data->size.Y/2 + offset.Y,
367 data->screensize.X/2 + data->size.X/2 + offset.X,
368 data->screensize.Y/2 + data->size.Y/2 + offset.Y
371 DesiredRect = data->rect;
372 recalculateAbsolutePosition(false);
373 data->basepos = getBasePos();
377 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
380 void GUIFormSpecMenu::parseList(parserData* data,std::string element) {
382 if (m_gamedef == 0) {
383 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
387 std::vector<std::string> parts = split(element,';');
389 if ((parts.size() == 4) || (parts.size() == 5)) {
390 std::string location = parts[0];
391 std::string listname = parts[1];
392 std::vector<std::string> v_pos = split(parts[2],',');
393 std::vector<std::string> v_geom = split(parts[3],',');
394 std::string startindex = "";
395 if (parts.size() == 5)
396 startindex = parts[4];
398 MY_CHECKPOS("list",2);
399 MY_CHECKGEOM("list",3);
401 InventoryLocation loc;
403 if(location == "context" || location == "current_name")
404 loc = m_current_inventory_location;
406 loc.deSerialize(location);
408 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
409 pos.X += stof(v_pos[0]) * (float)spacing.X;
410 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
413 geom.X = stoi(v_geom[0]);
414 geom.Y = stoi(v_geom[1]);
418 start_i = stoi(startindex);
419 if(data->bp_set != 2)
420 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
421 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
424 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
427 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element) {
428 std::vector<std::string> parts = split(element,';');
430 if ((parts.size() == 3) || (parts.size() == 4)) {
431 std::vector<std::string> v_pos = split(parts[0],',');
432 std::string name = parts[1];
433 std::string label = parts[2];
434 std::string selected = "";
436 if (parts.size() == 4)
439 MY_CHECKPOS("checkbox",0);
442 pos.X += stof(v_pos[0]) * (float) spacing.X;
443 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
445 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
447 bool fselected = false;
449 if (selected == "true")
452 std::wstring wlabel = narrow_to_wide(label.c_str());
454 FieldSpec spec = FieldSpec(
455 narrow_to_wide(name.c_str()),
461 spec.ftype = f_CheckBox;
462 spec.flabel = wlabel; //Needed for displaying text on MSVC
463 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
464 spec.fid, spec.flabel.c_str());
466 if (spec.fname == data->focused_fieldname) {
467 Environment->setFocus(e);
470 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
471 m_fields.push_back(spec);
474 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
477 void GUIFormSpecMenu::parseImage(parserData* data,std::string element) {
478 std::vector<std::string> parts = split(element,';');
480 if (parts.size() == 3) {
481 std::vector<std::string> v_pos = split(parts[0],',');
482 std::vector<std::string> v_geom = split(parts[1],',');
483 std::string name = parts[2];
485 MY_CHECKPOS("image",0);
486 MY_CHECKGEOM("image",1);
488 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
489 pos.X += stof(v_pos[0]) * (float) spacing.X;
490 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
493 geom.X = stof(v_geom[0]) * (float)imgsize.X;
494 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
496 if(data->bp_set != 2)
497 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
498 m_images.push_back(ImageDrawSpec(name, pos, geom));
502 if (parts.size() == 2) {
503 std::vector<std::string> v_pos = split(parts[0],',');
504 std::string name = parts[1];
506 MY_CHECKPOS("image",0);
508 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
509 pos.X += stof(v_pos[0]) * (float) spacing.X;
510 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
512 if(data->bp_set != 2)
513 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
514 m_images.push_back(ImageDrawSpec(name, pos));
517 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
520 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element) {
521 std::vector<std::string> parts = split(element,';');
523 if (parts.size() == 3) {
524 std::vector<std::string> v_pos = split(parts[0],',');
525 std::vector<std::string> v_geom = split(parts[1],',');
526 std::string name = parts[2];
528 MY_CHECKPOS("itemimage",0);
529 MY_CHECKGEOM("itemimage",1);
531 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
532 pos.X += stof(v_pos[0]) * (float) spacing.X;
533 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
536 geom.X = stoi(v_geom[0]) * (float)imgsize.X;
537 geom.Y = stoi(v_geom[1]) * (float)imgsize.Y;
539 if(data->bp_set != 2)
540 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
541 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
544 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
547 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,std::string type) {
548 std::vector<std::string> parts = split(element,';');
550 if (parts.size() == 4) {
551 std::vector<std::string> v_pos = split(parts[0],',');
552 std::vector<std::string> v_geom = split(parts[1],',');
553 std::string name = parts[2];
554 std::string label = parts[3];
556 MY_CHECKPOS("button",0);
557 MY_CHECKGEOM("button",1);
560 pos.X += stof(v_pos[0]) * (float)spacing.X;
561 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
564 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
565 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
567 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y-15, pos.X+geom.X, pos.Y+15);
569 if(data->bp_set != 2)
570 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
572 label = unescape_string(label);
574 std::wstring wlabel = narrow_to_wide(label.c_str());
576 FieldSpec spec = FieldSpec(
577 narrow_to_wide(name.c_str()),
582 spec.ftype = f_Button;
583 if(type == "button_exit")
586 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
587 spec.flabel.c_str());
589 if (spec.fname == data->focused_fieldname) {
590 Environment->setFocus(e);
593 m_fields.push_back(spec);
596 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
599 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element) {
600 std::vector<std::string> parts = split(element,';');
602 if (parts.size() == 3) {
603 std::vector<std::string> v_pos = split(parts[0],',');
604 std::vector<std::string> v_geom = split(parts[1],',');
605 std::string name = parts[2];
607 MY_CHECKPOS("background",0);
608 MY_CHECKGEOM("background",1);
610 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
611 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
612 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
615 geom.X = stof(v_geom[0]) * (float)spacing.X;
616 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
618 if(data->bp_set != 2)
619 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
620 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
623 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
626 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element) {
627 std::vector<std::string> parts = split(element,';');
629 if ((parts.size() == 5) || (parts.size() == 6)) {
630 std::vector<std::string> v_pos = split(parts[0],',');
631 std::vector<std::string> v_geom = split(parts[1],',');
632 std::string name = parts[2];
633 std::vector<std::string> items = split(parts[3],',');
634 std::string str_initial_selection = "";
635 std::string str_transparent = "false";
637 if (parts.size() >= 5)
638 str_initial_selection = parts[4];
640 if (parts.size() >= 6)
641 str_transparent = parts[5];
643 MY_CHECKPOS("textlist",0);
644 MY_CHECKGEOM("textlist",1);
647 pos.X += stof(v_pos[0]) * (float)spacing.X;
648 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
651 geom.X = stof(v_geom[0]) * (float)spacing.X;
652 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
655 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
657 std::wstring fname_w = narrow_to_wide(name.c_str());
659 FieldSpec spec = FieldSpec(
666 spec.ftype = f_ListBox;
668 //now really show list
669 gui::IGUIListBox *e = Environment->addListBox(rect, this,spec.fid);
671 if (spec.fname == data->focused_fieldname) {
672 Environment->setFocus(e);
675 if (str_transparent == "false")
676 e->setDrawBackground(true);
678 for (unsigned int i=0; i < items.size(); i++) {
679 if (items[i].c_str()[0] == '#') {
680 if (items[i].c_str()[1] == '#') {
681 e->addItem(narrow_to_wide(unescape_string(items[i])).c_str() +1);
684 std::string color = items[i].substr(1,6);
686 narrow_to_wide(unescape_string(items[i]).c_str() + 7);
688 e->addItem(toadd.c_str());
690 irr::video::SColor toset;
692 if (parseColor(color, toset))
693 e->setItemOverrideColor(i,toset);
697 e->addItem(narrow_to_wide(unescape_string(items[i])).c_str());
701 if (data->listbox_selections.find(fname_w) != data->listbox_selections.end()) {
702 e->setSelected(data->listbox_selections[fname_w]);
705 if (data->listbox_scroll.find(fname_w) != data->listbox_scroll.end()) {
706 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(e);
708 scrollbar->setPos(data->listbox_scroll[fname_w]);
712 if (str_initial_selection != "")
713 e->setSelected(stoi(str_initial_selection.c_str())-1);
715 m_listboxes.push_back(std::pair<FieldSpec,gui::IGUIListBox*>(spec,e));
716 m_fields.push_back(spec);
719 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
723 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element) {
724 std::vector<std::string> parts = split(element,';');
726 if (parts.size() == 5) {
727 std::vector<std::string> v_pos = split(parts[0],',');
728 std::string name = parts[2];
729 std::vector<std::string> items = split(parts[3],',');
730 std::string str_initial_selection = "";
731 str_initial_selection = parts[4];
733 MY_CHECKPOS("dropdown",0);
736 pos.X += stof(v_pos[0]) * (float)spacing.X;
737 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
739 s32 width = stof(parts[1]) * (float)spacing.Y;
741 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+width, pos.Y+30);
743 std::wstring fname_w = narrow_to_wide(name.c_str());
745 FieldSpec spec = FieldSpec(
752 spec.ftype = f_DropDown;
755 //now really show list
756 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
758 if (spec.fname == data->focused_fieldname) {
759 Environment->setFocus(e);
762 for (unsigned int i=0; i < items.size(); i++) {
763 e->addItem(narrow_to_wide(items[i]).c_str());
766 if (str_initial_selection != "")
767 e->setSelected(stoi(str_initial_selection.c_str())-1);
769 //if (data->listbox_selections.find(fname_w) != data->listbox_selections.end()) {
770 // e->setSelected(data->listbox_selections[fname_w]);
773 //m_listboxes.push_back(std::pair<FieldSpec,gui::IGUIListBox*>(spec,e));
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::string text = 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 text = unescape_string(text);
1086 std::string label = "";
1088 for (unsigned int i=0; i < text.length(); i++) {
1089 label += text.c_str()[i];
1093 FieldSpec spec = FieldSpec(
1095 narrow_to_wide(label.c_str()),
1099 gui::IGUIStaticText *t =
1100 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1101 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1102 m_fields.push_back(spec);
1105 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1108 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,std::string type) {
1109 std::vector<std::string> parts = split(element,';');
1111 if ((parts.size() == 5) || (parts.size() == 7) || (parts.size() == 8)) {
1112 std::vector<std::string> v_pos = split(parts[0],',');
1113 std::vector<std::string> v_geom = split(parts[1],',');
1114 std::string image_name = parts[2];
1115 std::string name = parts[3];
1116 std::string label = parts[4];
1118 MY_CHECKPOS("imagebutton",0);
1119 MY_CHECKGEOM("imagebutton",1);
1121 v2s32 pos = padding;
1122 pos.X += stof(v_pos[0]) * (float)spacing.X;
1123 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1125 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1126 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1128 bool noclip = false;
1129 bool drawborder = true;
1131 if ((parts.size() >= 7)) {
1132 if (parts[5] == "true")
1135 if (parts[6] == "false")
1139 std::string pressed_image_name = "";
1141 if ((parts.size() == 8)) {
1142 pressed_image_name = parts[7];
1145 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1147 if(data->bp_set != 2)
1148 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1150 label = unescape_string(label);
1152 std::wstring wlabel = narrow_to_wide(label.c_str());
1154 FieldSpec spec = FieldSpec(
1155 narrow_to_wide(name.c_str()),
1157 narrow_to_wide(image_name.c_str()),
1160 spec.ftype = f_Button;
1161 if(type == "image_button_exit")
1162 spec.is_exit = true;
1164 video::ITexture *texture = 0;
1165 video::ITexture *pressed_texture = 0;
1166 //if there's no gamedef specified try to get direct
1167 //TODO check for possible texture leak
1168 if (m_gamedef != 0) {
1169 texture = m_gamedef->tsrc()->getTexture(image_name);
1170 if ((parts.size() == 8)) {
1171 pressed_texture = m_gamedef->tsrc()->getTexture(pressed_image_name);
1174 if (fs::PathExists(image_name)) {
1175 texture = Environment->getVideoDriver()->getTexture(image_name.c_str());
1176 m_Textures.push_back(texture);
1178 if (fs::PathExists(pressed_image_name)) {
1179 pressed_texture = Environment->getVideoDriver()->getTexture(pressed_image_name.c_str());
1180 m_Textures.push_back(pressed_texture);
1183 if (parts.size() < 8)
1184 pressed_texture = texture;
1186 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1188 if (spec.fname == data->focused_fieldname) {
1189 Environment->setFocus(e);
1192 e->setUseAlphaChannel(true);
1193 e->setImage(texture);
1194 e->setPressedImage(pressed_texture);
1195 e->setScaleImage(true);
1196 e->setNotClipped(noclip);
1197 e->setDrawBorder(drawborder);
1199 m_fields.push_back(spec);
1203 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1206 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element) {
1207 std::vector<std::string> parts = split(element,';');
1209 if ((parts.size() == 4) || (parts.size() == 6)) {
1210 std::vector<std::string> v_pos = split(parts[0],',');
1211 std::string name = parts[1];
1212 std::vector<std::string> buttons = split(parts[2],',');
1213 std::string str_index = parts[3];
1214 bool show_background = true;
1215 bool show_border = true;
1216 int tab_index = stoi(str_index) -1;
1218 MY_CHECKPOS("tabheader",0);
1220 if (parts.size() == 6) {
1221 if (parts[4] == "true")
1222 show_background = false;
1223 if (parts[5] == "false")
1224 show_border = false;
1227 FieldSpec spec = FieldSpec(
1228 narrow_to_wide(name.c_str()),
1234 spec.ftype = f_TabHeader;
1236 v2s32 pos = padding;
1237 pos.X += stof(v_pos[0]) * (float)spacing.X;
1238 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1240 geom.X = data->screensize.Y;
1243 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1245 gui::IGUITabControl *e = Environment->addTabControl(rect,this,show_background,show_border,spec.fid);
1247 if (spec.fname == data->focused_fieldname) {
1248 Environment->setFocus(e);
1251 e->setNotClipped(true);
1253 for (unsigned int i=0; i< buttons.size(); i++) {
1254 wchar_t* wbutton = 0;
1256 wbutton = (wchar_t*) narrow_to_wide(buttons[i].c_str()).c_str();
1258 e->addTab(wbutton,-1);
1261 if ((tab_index >= 0) &&
1262 (buttons.size() < INT_MAX) &&
1263 (tab_index < (int) buttons.size()))
1264 e->setActiveTab(tab_index);
1266 m_fields.push_back(spec);
1269 errorstream<< "Invalid TabHeader element(" << parts.size() << "): '" << element << "'" << std::endl;
1272 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element) {
1274 if (m_gamedef == 0) {
1275 errorstream<<"WARNING: invalid use of item_image_button with m_gamedef==0"<<std::endl;
1279 std::vector<std::string> parts = split(element,';');
1281 if (parts.size() == 5) {
1282 std::vector<std::string> v_pos = split(parts[0],',');
1283 std::vector<std::string> v_geom = split(parts[1],',');
1284 std::string item_name = parts[2];
1285 std::string name = parts[3];
1286 std::string label = parts[4];
1288 MY_CHECKPOS("itemimagebutton",0);
1289 MY_CHECKGEOM("itemimagebutton",1);
1291 v2s32 pos = padding;
1292 pos.X += stof(v_pos[0]) * (float)spacing.X;
1293 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1295 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1296 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1298 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1300 if(data->bp_set != 2)
1301 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1303 IItemDefManager *idef = m_gamedef->idef();
1305 item.deSerialize(item_name, idef);
1306 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1307 std::string tooltip = item.getDefinition(idef).description;
1309 label = unescape_string(label);
1310 FieldSpec spec = FieldSpec(
1311 narrow_to_wide(name.c_str()),
1312 narrow_to_wide(label.c_str()),
1313 narrow_to_wide(item_name.c_str()),
1317 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1319 if (spec.fname == data->focused_fieldname) {
1320 Environment->setFocus(e);
1323 e->setUseAlphaChannel(true);
1324 e->setImage(texture);
1325 e->setPressedImage(texture);
1326 e->setScaleImage(true);
1327 spec.ftype = f_Button;
1328 rect+=data->basepos-padding;
1331 spec.tooltip=tooltip;
1332 m_fields.push_back(spec);
1335 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1338 void GUIFormSpecMenu::parseBox(parserData* data,std::string element) {
1339 std::vector<std::string> parts = split(element,';');
1341 if (parts.size() == 3) {
1342 std::vector<std::string> v_pos = split(parts[0],',');
1343 std::vector<std::string> v_geom = split(parts[1],',');
1344 std::string color_str = parts[2];
1346 MY_CHECKPOS("box",0);
1347 MY_CHECKGEOM("box",1);
1349 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1350 pos.X += stof(v_pos[0]) * (float) spacing.X;
1351 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1354 geom.X = stof(v_geom[0]) * (float)spacing.X;
1355 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1357 irr::video::SColor color;
1359 if (parseColor(color_str, color)) {
1360 BoxDrawSpec spec(pos,geom,color);
1362 m_boxes.push_back(spec);
1365 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1369 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1372 void GUIFormSpecMenu::parseElement(parserData* data,std::string element) {
1378 std::vector<std::string> parts = split(element,'[');
1380 // ugly workaround to keep compatibility
1381 if (parts.size() > 2) {
1382 if (trim(parts[0]) == "image") {
1383 for (unsigned int i=2;i< parts.size(); i++) {
1384 parts[1] += "[" + parts[i];
1390 if (parts.size() < 2) {
1394 std::string type = trim(parts[0]);
1395 std::string description = trim(parts[1]);
1397 if ((type == "size") || (type == "invsize")){
1398 parseSize(data,description);
1402 if (type == "list") {
1403 parseList(data,description);
1407 if (type == "checkbox") {
1408 parseCheckbox(data,description);
1412 if (type == "image") {
1413 parseImage(data,description);
1417 if (type == "item_image") {
1418 parseItemImage(data,description);
1422 if ((type == "button") || (type == "button_exit")) {
1423 parseButton(data,description,type);
1427 if (type == "background") {
1428 parseBackground(data,description);
1432 if (type == "textlist"){
1433 parseTextList(data,description);
1437 if (type == "dropdown"){
1438 parseDropDown(data,description);
1442 if (type == "pwdfield") {
1443 parsePwdField(data,description);
1447 if ((type == "field") || (type == "textarea")){
1448 parseField(data,description,type);
1452 if (type == "label") {
1453 parseLabel(data,description);
1457 if (type == "vertlabel") {
1458 parseVertLabel(data,description);
1462 if (type == "item_image_button") {
1463 parseItemImageButton(data,description);
1467 if ((type == "image_button") || (type == "image_button_exit")) {
1468 parseImageButton(data,description,type);
1472 if (type == "tabheader") {
1473 parseTabHeader(data,description);
1477 if (type == "box") {
1478 parseBox(data,description);
1484 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1490 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1494 //preserve listboxes
1495 for (unsigned int i = 0; i < m_listboxes.size(); i++) {
1496 std::wstring listboxname = m_listboxes[i].first.fname;
1497 gui::IGUIListBox *listbox = m_listboxes[i].second;
1499 int selection = listbox->getSelected();
1500 if (selection != -1) {
1501 mydata.listbox_selections[listboxname] = selection;
1504 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(listbox);
1506 mydata.listbox_scroll[listboxname] = scrollbar->getPos();
1511 gui::IGUIElement *focused_element = Environment->getFocus();
1512 if (focused_element && focused_element->getParent() == this) {
1513 s32 focused_id = focused_element->getID();
1514 if (focused_id > 257) {
1515 for (u32 i=0; i<m_fields.size(); i++) {
1516 if (m_fields[i].fid == focused_id) {
1517 mydata.focused_fieldname =
1528 mydata.size= v2s32(100,100);
1529 mydata.helptext_h = 15;
1530 mydata.screensize = screensize;
1532 // Base position of contents of form
1533 mydata.basepos = getBasePos();
1535 // State of basepos, 0 = not set, 1= set by formspec, 2 = set by size[] element
1536 // Used to adjust form size automatically if needed
1537 // A proceed button is added if there is no size[] element
1541 /* Convert m_init_draw_spec to m_inventorylists */
1543 m_inventorylists.clear();
1545 m_backgrounds.clear();
1546 m_itemimages.clear();
1547 m_listboxes.clear();
1548 m_checkboxes.clear();
1553 std::vector<std::string> elements = split(m_formspec_string,']');
1555 for (unsigned int i=0;i< elements.size();i++) {
1556 parseElement(&mydata,elements[i]);
1559 // If there's inventory, put the usage string at the bottom
1560 if (m_inventorylists.size())
1563 core::rect<s32> rect(0, 0, mydata.size.X-padding.X*2, mydata.helptext_h);
1564 rect = rect + v2s32((mydata.size.X/2 - mydata.rect.getWidth()/2) +5,
1565 mydata.size.Y-5-mydata.helptext_h);
1566 const wchar_t *text = wgettext("Left click: Move all items, Right click: Move single item");
1567 Environment->addStaticText(text, rect, false, true, this, 256);
1571 // If there's fields, add a Proceed button
1572 if (m_fields.size() && mydata.bp_set != 2)
1574 // if the size wasn't set by an invsize[] or size[] adjust it now to fit all the fields
1575 mydata.rect = core::rect<s32>(
1576 mydata.screensize.X/2 - 580/2,
1577 mydata.screensize.Y/2 - 300/2,
1578 mydata.screensize.X/2 + 580/2,
1579 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1581 DesiredRect = mydata.rect;
1582 recalculateAbsolutePosition(false);
1583 mydata.basepos = getBasePos();
1587 v2s32 pos = mydata.basepos;
1588 pos.Y = ((m_fields.size()+2)*60);
1590 v2s32 size = DesiredRect.getSize();
1591 mydata.rect = core::rect<s32>(size.X/2-70, pos.Y, (size.X/2-70)+140, pos.Y+30);
1592 wchar_t* text = wgettext("Proceed");
1593 Environment->addButton(mydata.rect, this, 257, text);
1600 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1601 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1602 m_tooltip_element->enableOverrideColor(true);
1603 m_tooltip_element->setBackgroundColor(video::SColor(255,110,130,60));
1604 m_tooltip_element->setDrawBackground(true);
1605 m_tooltip_element->setDrawBorder(true);
1606 m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
1607 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1608 m_tooltip_element->setWordWrap(false);
1611 //set initial focus if parser didn't set it
1612 focused_element = Environment->getFocus();
1613 if (!focused_element
1614 || !isMyChild(focused_element)
1615 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
1619 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
1621 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1623 for(u32 i=0; i<m_inventorylists.size(); i++)
1625 const ListDrawSpec &s = m_inventorylists[i];
1627 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1629 s32 item_i = i + s.start_item_i;
1630 s32 x = (i%s.geom.X) * spacing.X;
1631 s32 y = (i/s.geom.X) * spacing.Y;
1633 core::rect<s32> rect = imgrect + s.pos + p0;
1634 if(rect.isPointInside(p))
1636 return ItemSpec(s.inventoryloc, s.listname, item_i);
1641 return ItemSpec(InventoryLocation(), "", -1);
1644 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
1646 video::IVideoDriver* driver = Environment->getVideoDriver();
1649 gui::IGUIFont *font = NULL;
1650 gui::IGUISkin* skin = Environment->getSkin();
1652 font = skin->getFont();
1654 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1656 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1657 <<"The inventory location "
1658 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
1662 InventoryList *ilist = inv->getList(s.listname);
1664 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1665 <<"The inventory list \""<<s.listname<<"\" @ \""
1666 <<s.inventoryloc.dump()<<"\" doesn't exist"
1671 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1673 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1675 s32 item_i = i + s.start_item_i;
1676 if(item_i >= (s32) ilist->getSize())
1678 s32 x = (i%s.geom.X) * spacing.X;
1679 s32 y = (i/s.geom.X) * spacing.Y;
1681 core::rect<s32> rect = imgrect + s.pos + p;
1684 item = ilist->getItem(item_i);
1686 bool selected = m_selected_item
1687 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
1688 && m_selected_item->listname == s.listname
1689 && m_selected_item->i == item_i;
1690 bool hovering = rect.isPointInside(m_pointer);
1694 if(hovering && m_selected_item)
1696 video::SColor bgcolor(255,192,192,192);
1697 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
1701 video::SColor bgcolor(255,128,128,128);
1702 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
1711 item.takeItem(m_selected_amount);
1715 drawItemStack(driver, font, item,
1716 rect, &AbsoluteClippingRect, m_gamedef);
1720 std::string tooltip_text = "";
1721 if(hovering && !m_selected_item)
1722 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
1723 if(tooltip_text != "")
1725 m_tooltip_element->setVisible(true);
1726 this->bringToFront(m_tooltip_element);
1727 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
1728 s32 tooltip_x = m_pointer.X + 15;
1729 s32 tooltip_y = m_pointer.Y + 15;
1730 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1731 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1732 m_tooltip_element->setRelativePosition(core::rect<s32>(
1733 core::position2d<s32>(tooltip_x, tooltip_y),
1734 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1740 void GUIFormSpecMenu::drawSelectedItem()
1742 if(!m_selected_item)
1745 video::IVideoDriver* driver = Environment->getVideoDriver();
1748 gui::IGUIFont *font = NULL;
1749 gui::IGUISkin* skin = Environment->getSkin();
1751 font = skin->getFont();
1753 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
1755 InventoryList *list = inv->getList(m_selected_item->listname);
1757 ItemStack stack = list->getItem(m_selected_item->i);
1758 stack.count = m_selected_amount;
1760 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1761 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
1762 drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
1765 void GUIFormSpecMenu::drawMenu()
1768 std::string newform = m_form_src->getForm();
1769 if(newform != m_formspec_string){
1770 m_formspec_string = newform;
1771 regenerateGui(m_screensize_old);
1775 m_pointer = m_device->getCursorControl()->getPosition();
1777 updateSelectedItem();
1779 gui::IGUISkin* skin = Environment->getSkin();
1782 video::IVideoDriver* driver = Environment->getVideoDriver();
1784 video::SColor bgcolor(140,0,0,0);
1785 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
1787 m_tooltip_element->setVisible(false);
1792 for(u32 i=0; i<m_backgrounds.size(); i++)
1794 const ImageDrawSpec &spec = m_backgrounds[i];
1795 video::ITexture *texture = 0;
1798 texture = m_gamedef->tsrc()->getTexture(spec.name);
1801 texture = driver->getTexture(spec.name.c_str());
1802 m_Textures.push_back(texture);
1806 // Image size on screen
1807 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1808 // Image rectangle on screen
1809 core::rect<s32> rect = imgrect + spec.pos;
1810 const video::SColor color(255,255,255,255);
1811 const video::SColor colors[] = {color,color,color,color};
1812 driver->draw2DImage(texture, rect,
1813 core::rect<s32>(core::position2d<s32>(0,0),
1814 core::dimension2di(texture->getOriginalSize())),
1815 NULL/*&AbsoluteClippingRect*/, colors, true);
1818 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
1819 errorstream << "\t" << spec.name << std::endl;
1826 for(u32 i=0; i<m_boxes.size(); i++)
1828 const BoxDrawSpec &spec = m_boxes[i];
1830 irr::video::SColor todraw = spec.color;
1832 todraw.setAlpha(140);
1834 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
1835 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
1837 driver->draw2DRectangle(todraw, rect, 0);
1842 for(u32 i=0; i<m_images.size(); i++)
1844 const ImageDrawSpec &spec = m_images[i];
1845 video::ITexture *texture = 0;
1848 texture = m_gamedef->tsrc()->getTexture(spec.name);
1851 texture = driver->getTexture(spec.name.c_str());
1852 m_Textures.push_back(texture);
1855 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
1856 // Image size on screen
1857 core::rect<s32> imgrect;
1860 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
1863 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
1865 // Image rectangle on screen
1866 core::rect<s32> rect = imgrect + spec.pos;
1867 const video::SColor color(255,255,255,255);
1868 const video::SColor colors[] = {color,color,color,color};
1869 driver->draw2DImage(texture, rect,
1870 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
1871 NULL/*&AbsoluteClippingRect*/, colors, true);
1874 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
1875 errorstream << "\t" << spec.name << std::endl;
1882 for(u32 i=0; i<m_itemimages.size(); i++)
1887 const ImageDrawSpec &spec = m_itemimages[i];
1888 IItemDefManager *idef = m_gamedef->idef();
1890 item.deSerialize(spec.name, idef);
1891 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1892 // Image size on screen
1893 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1894 // Image rectangle on screen
1895 core::rect<s32> rect = imgrect + spec.pos;
1896 const video::SColor color(255,255,255,255);
1897 const video::SColor colors[] = {color,color,color,color};
1898 driver->draw2DImage(texture, rect,
1899 core::rect<s32>(core::position2d<s32>(0,0),
1900 core::dimension2di(texture->getOriginalSize())),
1901 NULL/*&AbsoluteClippingRect*/, colors, true);
1906 Phase 0: Item slot rectangles
1907 Phase 1: Item images; prepare tooltip
1908 If backgrounds used, do not draw Item slot rectangles
1911 if (m_backgrounds.size() > 0) start_phase=1;
1912 for(int phase=start_phase; phase<=1; phase++)
1913 for(u32 i=0; i<m_inventorylists.size(); i++)
1915 drawList(m_inventorylists[i], phase);
1921 gui::IGUIElement::draw();
1924 Draw fields/buttons tooltips
1926 for(u32 i=0; i<m_fields.size(); i++)
1928 const FieldSpec &spec = m_fields[i];
1929 if (spec.tooltip != "")
1931 core::rect<s32> rect = spec.rect;
1932 if (rect.isPointInside(m_pointer))
1934 m_tooltip_element->setVisible(true);
1935 this->bringToFront(m_tooltip_element);
1936 m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
1937 s32 tooltip_x = m_pointer.X + 15;
1938 s32 tooltip_y = m_pointer.Y + 15;
1939 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1940 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1941 m_tooltip_element->setRelativePosition(core::rect<s32>(
1942 core::position2d<s32>(tooltip_x, tooltip_y),
1943 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1949 Draw dragged item stack
1954 void GUIFormSpecMenu::updateSelectedItem()
1956 // If the selected stack has become empty for some reason, deselect it.
1957 // If the selected stack has become inaccessible, deselect it.
1958 // If the selected stack has become smaller, adjust m_selected_amount.
1959 ItemStack selected = verifySelectedItem();
1961 // WARNING: BLACK MAGIC
1962 // See if there is a stack suited for our current guess.
1963 // If such stack does not exist, clear the guess.
1964 if(m_selected_content_guess.name != "" &&
1965 selected.name == m_selected_content_guess.name &&
1966 selected.count == m_selected_content_guess.count){
1967 // Selected item fits the guess. Skip the black magic.
1969 else if(m_selected_content_guess.name != ""){
1971 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
1972 const ListDrawSpec &s = m_inventorylists[i];
1973 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1976 InventoryList *list = inv->getList(s.listname);
1979 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
1980 u32 item_i = i + s.start_item_i;
1981 if(item_i >= list->getSize())
1983 ItemStack stack = list->getItem(item_i);
1984 if(stack.name == m_selected_content_guess.name &&
1985 stack.count == m_selected_content_guess.count){
1987 infostream<<"Client: Changing selected content guess to "
1988 <<s.inventoryloc.dump()<<" "<<s.listname
1989 <<" "<<item_i<<std::endl;
1990 delete m_selected_item;
1991 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
1992 m_selected_amount = stack.count;
1997 infostream<<"Client: Discarding selected content guess: "
1998 <<m_selected_content_guess.getItemString()<<std::endl;
1999 m_selected_content_guess.name = "";
2003 // If craftresult is nonempty and nothing else is selected, select it now.
2004 if(!m_selected_item)
2006 for(u32 i=0; i<m_inventorylists.size(); i++)
2008 const ListDrawSpec &s = m_inventorylists[i];
2009 if(s.listname == "craftpreview")
2011 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2012 InventoryList *list = inv->getList("craftresult");
2013 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2015 m_selected_item = new ItemSpec;
2016 m_selected_item->inventoryloc = s.inventoryloc;
2017 m_selected_item->listname = "craftresult";
2018 m_selected_item->i = 0;
2019 m_selected_amount = 0;
2020 m_selected_dragging = false;
2027 // If craftresult is selected, keep the whole stack selected
2028 if(m_selected_item && m_selected_item->listname == "craftresult")
2030 m_selected_amount = verifySelectedItem().count;
2034 ItemStack GUIFormSpecMenu::verifySelectedItem()
2036 // If the selected stack has become empty for some reason, deselect it.
2037 // If the selected stack has become inaccessible, deselect it.
2038 // If the selected stack has become smaller, adjust m_selected_amount.
2039 // Return the selected stack.
2043 if(m_selected_item->isValid())
2045 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2048 InventoryList *list = inv->getList(m_selected_item->listname);
2049 if(list && (u32) m_selected_item->i < list->getSize())
2051 ItemStack stack = list->getItem(m_selected_item->i);
2052 if(m_selected_amount > stack.count)
2053 m_selected_amount = stack.count;
2060 // selection was not valid
2061 delete m_selected_item;
2062 m_selected_item = NULL;
2063 m_selected_amount = 0;
2064 m_selected_dragging = false;
2069 void GUIFormSpecMenu::acceptInput()
2073 std::map<std::string, std::string> fields;
2075 if (current_keys_pending.key_down) {
2076 fields["key_down"] = "true";
2077 current_keys_pending.key_down = false;
2080 if (current_keys_pending.key_up) {
2081 fields["key_up"] = "true";
2082 current_keys_pending.key_up = false;
2085 if (current_keys_pending.key_enter) {
2086 fields["key_enter"] = "true";
2087 current_keys_pending.key_enter = false;
2090 if (current_keys_pending.key_escape) {
2091 fields["key_escape"] = "true";
2092 current_keys_pending.key_escape = false;
2095 for(u32 i=0; i<m_fields.size(); i++)
2097 const FieldSpec &s = m_fields[i];
2100 if(s.ftype == f_Button)
2102 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(s.flabel.c_str());
2104 else if(s.ftype == f_ListBox) {
2105 std::stringstream ss;
2107 if (m_listbox_doubleclick) {
2113 ss << (getListboxIndex(wide_to_narrow(s.fname.c_str()))+1);
2114 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2116 else if(s.ftype == f_DropDown) {
2117 // no dynamic cast possible due to some distributions shipped
2118 // without rtti support in irrlicht
2119 IGUIElement * element = getElementFromId(s.fid);
2120 gui::IGUIComboBox *e = NULL;
2121 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2122 e = static_cast<gui::IGUIComboBox*>(element);
2124 fields[wide_to_narrow(s.fname.c_str())] =
2125 wide_to_narrow(e->getItem(e->getSelected()));
2127 else if (s.ftype == f_TabHeader) {
2128 // no dynamic cast possible due to some distributions shipped
2129 // without rtti support in irrlicht
2130 IGUIElement * element = getElementFromId(s.fid);
2131 gui::IGUITabControl *e = NULL;
2132 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2133 e = static_cast<gui::IGUITabControl*>(element);
2137 std::stringstream ss;
2138 ss << (e->getActiveTab() +1);
2139 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2142 else if (s.ftype == f_CheckBox) {
2143 // no dynamic cast possible due to some distributions shipped
2144 // without rtti support in irrlicht
2145 IGUIElement * element = getElementFromId(s.fid);
2146 gui::IGUICheckBox *e = NULL;
2147 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2148 e = static_cast<gui::IGUICheckBox*>(element);
2153 fields[wide_to_narrow(s.fname.c_str())] = "true";
2155 fields[wide_to_narrow(s.fname.c_str())] = "false";
2160 IGUIElement* e = getElementFromId(s.fid);
2163 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(e->getText());
2169 m_text_dst->gotText(fields);
2173 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2175 // Fix Esc/Return key being eaten by checkboxen and listboxen
2176 if(event.EventType==EET_KEY_INPUT_EVENT)
2178 KeyPress kp(event.KeyInput);
2179 if (kp == EscapeKey || kp == getKeySetting("keymap_inventory")
2180 || event.KeyInput.Key==KEY_RETURN)
2182 gui::IGUIElement *focused = Environment->getFocus();
2183 if (focused && isMyChild(focused) &&
2184 (focused->getType() == gui::EGUIET_LIST_BOX ||
2185 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2191 // Mouse wheel events: send to hovered element instead of focused
2192 if(event.EventType==EET_MOUSE_INPUT_EVENT
2193 && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
2195 s32 x = event.MouseInput.X;
2196 s32 y = event.MouseInput.Y;
2197 gui::IGUIElement *hovered =
2198 Environment->getRootGUIElement()->getElementFromPoint(
2199 core::position2d<s32>(x, y));
2200 if (hovered && isMyChild(hovered)) {
2201 hovered->OnEvent(event);
2208 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2210 if(event.EventType==EET_KEY_INPUT_EVENT)
2212 KeyPress kp(event.KeyInput);
2213 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
2214 kp == getKeySetting("keymap_inventory")))
2219 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
2222 if (event.KeyInput.PressedDown &&
2223 (event.KeyInput.Key==KEY_RETURN ||
2224 event.KeyInput.Key==KEY_UP ||
2225 event.KeyInput.Key==KEY_DOWN)
2229 switch (event.KeyInput.Key) {
2236 current_keys_pending.key_enter = true;
2239 current_keys_pending.key_up = true;
2242 current_keys_pending.key_down = true;
2246 //can't happen at all!
2247 assert("reached a source line that can't ever been reached" == 0);
2255 if(event.EventType==EET_MOUSE_INPUT_EVENT
2256 && event.MouseInput.Event != EMIE_MOUSE_MOVED)
2258 // Mouse event other than movement
2260 // Get selected item and hovered/clicked item (s)
2262 updateSelectedItem();
2263 ItemSpec s = getItemAtPos(m_pointer);
2265 Inventory *inv_selected = NULL;
2266 Inventory *inv_s = NULL;
2270 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
2271 assert(inv_selected);
2272 assert(inv_selected->getList(m_selected_item->listname) != NULL);
2279 inv_s = m_invmgr->getInventory(s.inventoryloc);
2282 errorstream<<"InventoryMenu: The selected inventory location "
2283 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2285 s.i = -1; // make it invalid again
2289 InventoryList *list = inv_s->getList(s.listname);
2291 verbosestream<<"InventoryMenu: The selected inventory list \""
2292 <<s.listname<<"\" does not exist"<<std::endl;
2293 s.i = -1; // make it invalid again
2297 if((u32)s.i >= list->getSize()){
2298 infostream<<"InventoryMenu: The selected inventory list \""
2299 <<s.listname<<"\" is too small (i="<<s.i<<", size="
2300 <<list->getSize()<<")"<<std::endl;
2301 s.i = -1; // make it invalid again
2305 s_count = list->getItem(s.i).count;
2308 bool identical = (m_selected_item != NULL) && s.isValid() &&
2309 (inv_selected == inv_s) &&
2310 (m_selected_item->listname == s.listname) &&
2311 (m_selected_item->i == s.i);
2313 // buttons: 0 = left, 1 = right, 2 = middle
2314 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
2317 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
2318 { button = 0; updown = 0; }
2319 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
2320 { button = 1; updown = 0; }
2321 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
2322 { button = 2; updown = 0; }
2323 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
2324 { button = 0; updown = 1; }
2325 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
2326 { button = 1; updown = 1; }
2327 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
2328 { button = 2; updown = 1; }
2330 // Set this number to a positive value to generate a move action
2331 // from m_selected_item to s.
2332 u32 move_amount = 0;
2334 // Set this number to a positive value to generate a drop action
2335 // from m_selected_item.
2336 u32 drop_amount = 0;
2338 // Set this number to a positive value to generate a craft action at s.
2339 u32 craft_amount = 0;
2343 // Some mouse button has been pressed
2345 //infostream<<"Mouse button "<<button<<" pressed at p=("
2346 // <<p.X<<","<<p.Y<<")"<<std::endl;
2348 m_selected_dragging = false;
2350 if(s.isValid() && s.listname == "craftpreview")
2352 // Craft preview has been clicked: craft
2353 craft_amount = (button == 2 ? 10 : 1);
2355 else if(m_selected_item == NULL)
2359 // Non-empty stack has been clicked: select it
2360 m_selected_item = new ItemSpec(s);
2362 if(button == 1) // right
2363 m_selected_amount = (s_count + 1) / 2;
2364 else if(button == 2) // middle
2365 m_selected_amount = MYMIN(s_count, 10);
2367 m_selected_amount = s_count;
2369 m_selected_dragging = true;
2372 else // m_selected_item != NULL
2374 assert(m_selected_amount >= 1);
2378 // Clicked a slot: move
2379 if(button == 1) // right
2381 else if(button == 2) // middle
2382 move_amount = MYMIN(m_selected_amount, 10);
2384 move_amount = m_selected_amount;
2388 if(move_amount >= m_selected_amount)
2389 m_selected_amount = 0;
2391 m_selected_amount -= move_amount;
2395 else if(getAbsoluteClippingRect().isPointInside(m_pointer))
2397 // Clicked somewhere else: deselect
2398 m_selected_amount = 0;
2402 // Clicked outside of the window: drop
2403 if(button == 1) // right
2405 else if(button == 2) // middle
2406 drop_amount = MYMIN(m_selected_amount, 10);
2408 drop_amount = m_selected_amount;
2412 else if(updown == 1)
2414 // Some mouse button has been released
2416 //infostream<<"Mouse button "<<button<<" released at p=("
2417 // <<p.X<<","<<p.Y<<")"<<std::endl;
2419 if(m_selected_item != NULL && m_selected_dragging && s.isValid())
2423 // Dragged to different slot: move all selected
2424 move_amount = m_selected_amount;
2427 else if(m_selected_item != NULL && m_selected_dragging &&
2428 !(getAbsoluteClippingRect().isPointInside(m_pointer)))
2430 // Dragged outside of window: drop all selected
2431 drop_amount = m_selected_amount;
2434 m_selected_dragging = false;
2437 // Possibly send inventory action to server
2440 // Send IACTION_MOVE
2442 assert(m_selected_item && m_selected_item->isValid());
2443 assert(s.isValid());
2445 assert(inv_selected && inv_s);
2446 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2447 InventoryList *list_to = inv_s->getList(s.listname);
2448 assert(list_from && list_to);
2449 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2450 ItemStack stack_to = list_to->getItem(s.i);
2452 // Check how many items can be moved
2453 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
2454 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
2455 // If source stack cannot be added to destination stack at all,
2457 if(leftover.count == stack_from.count && leftover.name == stack_from.name)
2459 m_selected_amount = stack_to.count;
2460 // In case the server doesn't directly swap them but instead
2461 // moves stack_to somewhere else, set this
2462 m_selected_content_guess = stack_to;
2463 m_selected_content_guess_inventory = s.inventoryloc;
2465 // Source stack goes fully into destination stack
2466 else if(leftover.empty())
2468 m_selected_amount -= move_amount;
2469 m_selected_content_guess = ItemStack(); // Clear
2471 // Source stack goes partly into destination stack
2474 move_amount -= leftover.count;
2475 m_selected_amount -= move_amount;
2476 m_selected_content_guess = ItemStack(); // Clear
2479 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
2480 IMoveAction *a = new IMoveAction();
2481 a->count = move_amount;
2482 a->from_inv = m_selected_item->inventoryloc;
2483 a->from_list = m_selected_item->listname;
2484 a->from_i = m_selected_item->i;
2485 a->to_inv = s.inventoryloc;
2486 a->to_list = s.listname;
2488 m_invmgr->inventoryAction(a);
2490 else if(drop_amount > 0)
2492 m_selected_content_guess = ItemStack(); // Clear
2494 // Send IACTION_DROP
2496 assert(m_selected_item && m_selected_item->isValid());
2497 assert(inv_selected);
2498 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2500 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2502 // Check how many items can be dropped
2503 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
2504 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
2505 m_selected_amount -= drop_amount;
2507 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
2508 IDropAction *a = new IDropAction();
2509 a->count = drop_amount;
2510 a->from_inv = m_selected_item->inventoryloc;
2511 a->from_list = m_selected_item->listname;
2512 a->from_i = m_selected_item->i;
2513 m_invmgr->inventoryAction(a);
2515 else if(craft_amount > 0)
2517 m_selected_content_guess = ItemStack(); // Clear
2519 // Send IACTION_CRAFT
2521 assert(s.isValid());
2524 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
2525 ICraftAction *a = new ICraftAction();
2526 a->count = craft_amount;
2527 a->craft_inv = s.inventoryloc;
2528 m_invmgr->inventoryAction(a);
2531 // If m_selected_amount has been decreased to zero, deselect
2532 if(m_selected_amount == 0)
2534 delete m_selected_item;
2535 m_selected_item = NULL;
2536 m_selected_amount = 0;
2537 m_selected_dragging = false;
2538 m_selected_content_guess = ItemStack();
2541 if(event.EventType==EET_GUI_EVENT)
2544 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
2547 // find the element that was clicked
2548 for(u32 i=0; i<m_fields.size(); i++)
2550 FieldSpec &s = m_fields[i];
2551 // if its a button, set the send field so
2552 // lua knows which button was pressed
2553 if ((s.ftype == f_TabHeader) && (s.fid == event.GUIEvent.Caller->getID()))
2562 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
2565 if(!canTakeFocus(event.GUIEvent.Element))
2567 infostream<<"GUIFormSpecMenu: Not allowing focus change."
2569 // Returning true disables focus change
2573 if((event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) ||
2574 (event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED))
2576 unsigned int btn_id = event.GUIEvent.Caller->getID();
2578 if (btn_id == 257) {
2583 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2584 // quitMenu deallocates menu
2588 // find the element that was clicked
2589 for(u32 i=0; i<m_fields.size(); i++)
2591 FieldSpec &s = m_fields[i];
2592 // if its a button, set the send field so
2593 // lua knows which button was pressed
2594 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
2595 (s.fid == event.GUIEvent.Caller->getID()))
2603 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2612 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
2614 if(event.GUIEvent.Caller->getID() > 257)
2622 current_keys_pending.key_enter = true;
2625 // quitMenu deallocates menu
2630 if((event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN) ||
2631 (event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED))
2633 int current_id = event.GUIEvent.Caller->getID();
2634 if(current_id > 257)
2636 // find the element that was clicked
2637 for(u32 i=0; i<m_fields.size(); i++)
2639 FieldSpec &s = m_fields[i];
2640 // if its a listbox, set the send field so
2641 // lua knows which listbox was changed
2642 // checkListboxClick() is black magic
2643 // for properly handling double clicks
2644 if ((s.ftype == f_ListBox) && (s.fid == current_id)
2645 && checkListboxClick(s.fname,
2646 event.GUIEvent.EventType))
2658 return Parent ? Parent->OnEvent(event) : false;
2661 bool GUIFormSpecMenu::parseColor(std::string color, irr::video::SColor& outcolor) {
2662 outcolor = irr::video::SColor(0,0,0,0);
2664 if (!string_allowed(color, "0123456789abcdefABCDEF"))
2668 std::istringstream iss(color);
2669 iss >> std::hex >> color_value;
2671 outcolor = irr::video::SColor(color_value);
2673 outcolor.setAlpha(255);