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 std::wstring wlabel = narrow_to_wide(buttons[i]); //Needed for displaying text on windows
1257 wbutton = (wchar_t*) wlabel.c_str();
1259 e->addTab(wbutton,-1);
1262 if ((tab_index >= 0) &&
1263 (buttons.size() < INT_MAX) &&
1264 (tab_index < (int) buttons.size()))
1265 e->setActiveTab(tab_index);
1267 m_fields.push_back(spec);
1270 errorstream<< "Invalid TabHeader element(" << parts.size() << "): '" << element << "'" << std::endl;
1273 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element) {
1275 if (m_gamedef == 0) {
1276 errorstream<<"WARNING: invalid use of item_image_button with m_gamedef==0"<<std::endl;
1280 std::vector<std::string> parts = split(element,';');
1282 if (parts.size() == 5) {
1283 std::vector<std::string> v_pos = split(parts[0],',');
1284 std::vector<std::string> v_geom = split(parts[1],',');
1285 std::string item_name = parts[2];
1286 std::string name = parts[3];
1287 std::string label = parts[4];
1289 MY_CHECKPOS("itemimagebutton",0);
1290 MY_CHECKGEOM("itemimagebutton",1);
1292 v2s32 pos = padding;
1293 pos.X += stof(v_pos[0]) * (float)spacing.X;
1294 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1296 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1297 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1299 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1301 if(data->bp_set != 2)
1302 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1304 IItemDefManager *idef = m_gamedef->idef();
1306 item.deSerialize(item_name, idef);
1307 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1308 std::string tooltip = item.getDefinition(idef).description;
1310 label = unescape_string(label);
1311 FieldSpec spec = FieldSpec(
1312 narrow_to_wide(name.c_str()),
1313 narrow_to_wide(label.c_str()),
1314 narrow_to_wide(item_name.c_str()),
1318 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1320 if (spec.fname == data->focused_fieldname) {
1321 Environment->setFocus(e);
1324 e->setUseAlphaChannel(true);
1325 e->setImage(texture);
1326 e->setPressedImage(texture);
1327 e->setScaleImage(true);
1328 spec.ftype = f_Button;
1329 rect+=data->basepos-padding;
1332 spec.tooltip=tooltip;
1333 m_fields.push_back(spec);
1336 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1339 void GUIFormSpecMenu::parseBox(parserData* data,std::string element) {
1340 std::vector<std::string> parts = split(element,';');
1342 if (parts.size() == 3) {
1343 std::vector<std::string> v_pos = split(parts[0],',');
1344 std::vector<std::string> v_geom = split(parts[1],',');
1345 std::string color_str = parts[2];
1347 MY_CHECKPOS("box",0);
1348 MY_CHECKGEOM("box",1);
1350 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1351 pos.X += stof(v_pos[0]) * (float) spacing.X;
1352 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1355 geom.X = stof(v_geom[0]) * (float)spacing.X;
1356 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1358 irr::video::SColor color;
1360 if (parseColor(color_str, color)) {
1361 BoxDrawSpec spec(pos,geom,color);
1363 m_boxes.push_back(spec);
1366 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1370 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1373 void GUIFormSpecMenu::parseElement(parserData* data,std::string element) {
1379 std::vector<std::string> parts = split(element,'[');
1381 // ugly workaround to keep compatibility
1382 if (parts.size() > 2) {
1383 if (trim(parts[0]) == "image") {
1384 for (unsigned int i=2;i< parts.size(); i++) {
1385 parts[1] += "[" + parts[i];
1391 if (parts.size() < 2) {
1395 std::string type = trim(parts[0]);
1396 std::string description = trim(parts[1]);
1398 if ((type == "size") || (type == "invsize")){
1399 parseSize(data,description);
1403 if (type == "list") {
1404 parseList(data,description);
1408 if (type == "checkbox") {
1409 parseCheckbox(data,description);
1413 if (type == "image") {
1414 parseImage(data,description);
1418 if (type == "item_image") {
1419 parseItemImage(data,description);
1423 if ((type == "button") || (type == "button_exit")) {
1424 parseButton(data,description,type);
1428 if (type == "background") {
1429 parseBackground(data,description);
1433 if (type == "textlist"){
1434 parseTextList(data,description);
1438 if (type == "dropdown"){
1439 parseDropDown(data,description);
1443 if (type == "pwdfield") {
1444 parsePwdField(data,description);
1448 if ((type == "field") || (type == "textarea")){
1449 parseField(data,description,type);
1453 if (type == "label") {
1454 parseLabel(data,description);
1458 if (type == "vertlabel") {
1459 parseVertLabel(data,description);
1463 if (type == "item_image_button") {
1464 parseItemImageButton(data,description);
1468 if ((type == "image_button") || (type == "image_button_exit")) {
1469 parseImageButton(data,description,type);
1473 if (type == "tabheader") {
1474 parseTabHeader(data,description);
1478 if (type == "box") {
1479 parseBox(data,description);
1485 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1491 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1495 //preserve listboxes
1496 for (unsigned int i = 0; i < m_listboxes.size(); i++) {
1497 std::wstring listboxname = m_listboxes[i].first.fname;
1498 gui::IGUIListBox *listbox = m_listboxes[i].second;
1500 int selection = listbox->getSelected();
1501 if (selection != -1) {
1502 mydata.listbox_selections[listboxname] = selection;
1505 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(listbox);
1507 mydata.listbox_scroll[listboxname] = scrollbar->getPos();
1512 gui::IGUIElement *focused_element = Environment->getFocus();
1513 if (focused_element && focused_element->getParent() == this) {
1514 s32 focused_id = focused_element->getID();
1515 if (focused_id > 257) {
1516 for (u32 i=0; i<m_fields.size(); i++) {
1517 if (m_fields[i].fid == focused_id) {
1518 mydata.focused_fieldname =
1529 mydata.size= v2s32(100,100);
1530 mydata.helptext_h = 15;
1531 mydata.screensize = screensize;
1533 // Base position of contents of form
1534 mydata.basepos = getBasePos();
1536 // State of basepos, 0 = not set, 1= set by formspec, 2 = set by size[] element
1537 // Used to adjust form size automatically if needed
1538 // A proceed button is added if there is no size[] element
1542 /* Convert m_init_draw_spec to m_inventorylists */
1544 m_inventorylists.clear();
1546 m_backgrounds.clear();
1547 m_itemimages.clear();
1548 m_listboxes.clear();
1549 m_checkboxes.clear();
1554 std::vector<std::string> elements = split(m_formspec_string,']');
1556 for (unsigned int i=0;i< elements.size();i++) {
1557 parseElement(&mydata,elements[i]);
1560 // If there's inventory, put the usage string at the bottom
1561 if (m_inventorylists.size())
1564 core::rect<s32> rect(0, 0, mydata.size.X-padding.X*2, mydata.helptext_h);
1565 rect = rect + v2s32((mydata.size.X/2 - mydata.rect.getWidth()/2) +5,
1566 mydata.size.Y-5-mydata.helptext_h);
1567 const wchar_t *text = wgettext("Left click: Move all items, Right click: Move single item");
1568 Environment->addStaticText(text, rect, false, true, this, 256);
1572 // If there's fields, add a Proceed button
1573 if (m_fields.size() && mydata.bp_set != 2)
1575 // if the size wasn't set by an invsize[] or size[] adjust it now to fit all the fields
1576 mydata.rect = core::rect<s32>(
1577 mydata.screensize.X/2 - 580/2,
1578 mydata.screensize.Y/2 - 300/2,
1579 mydata.screensize.X/2 + 580/2,
1580 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1582 DesiredRect = mydata.rect;
1583 recalculateAbsolutePosition(false);
1584 mydata.basepos = getBasePos();
1588 v2s32 pos = mydata.basepos;
1589 pos.Y = ((m_fields.size()+2)*60);
1591 v2s32 size = DesiredRect.getSize();
1592 mydata.rect = core::rect<s32>(size.X/2-70, pos.Y, (size.X/2-70)+140, pos.Y+30);
1593 wchar_t* text = wgettext("Proceed");
1594 Environment->addButton(mydata.rect, this, 257, text);
1601 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1602 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1603 m_tooltip_element->enableOverrideColor(true);
1604 m_tooltip_element->setBackgroundColor(video::SColor(255,110,130,60));
1605 m_tooltip_element->setDrawBackground(true);
1606 m_tooltip_element->setDrawBorder(true);
1607 m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
1608 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1609 m_tooltip_element->setWordWrap(false);
1612 //set initial focus if parser didn't set it
1613 focused_element = Environment->getFocus();
1614 if (!focused_element
1615 || !isMyChild(focused_element)
1616 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
1620 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
1622 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1624 for(u32 i=0; i<m_inventorylists.size(); i++)
1626 const ListDrawSpec &s = m_inventorylists[i];
1628 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1630 s32 item_i = i + s.start_item_i;
1631 s32 x = (i%s.geom.X) * spacing.X;
1632 s32 y = (i/s.geom.X) * spacing.Y;
1634 core::rect<s32> rect = imgrect + s.pos + p0;
1635 if(rect.isPointInside(p))
1637 return ItemSpec(s.inventoryloc, s.listname, item_i);
1642 return ItemSpec(InventoryLocation(), "", -1);
1645 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
1647 video::IVideoDriver* driver = Environment->getVideoDriver();
1650 gui::IGUIFont *font = NULL;
1651 gui::IGUISkin* skin = Environment->getSkin();
1653 font = skin->getFont();
1655 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1657 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1658 <<"The inventory location "
1659 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
1663 InventoryList *ilist = inv->getList(s.listname);
1665 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1666 <<"The inventory list \""<<s.listname<<"\" @ \""
1667 <<s.inventoryloc.dump()<<"\" doesn't exist"
1672 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1674 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1676 s32 item_i = i + s.start_item_i;
1677 if(item_i >= (s32) ilist->getSize())
1679 s32 x = (i%s.geom.X) * spacing.X;
1680 s32 y = (i/s.geom.X) * spacing.Y;
1682 core::rect<s32> rect = imgrect + s.pos + p;
1685 item = ilist->getItem(item_i);
1687 bool selected = m_selected_item
1688 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
1689 && m_selected_item->listname == s.listname
1690 && m_selected_item->i == item_i;
1691 bool hovering = rect.isPointInside(m_pointer);
1695 if(hovering && m_selected_item)
1697 video::SColor bgcolor(255,192,192,192);
1698 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
1702 video::SColor bgcolor(255,128,128,128);
1703 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
1712 item.takeItem(m_selected_amount);
1716 drawItemStack(driver, font, item,
1717 rect, &AbsoluteClippingRect, m_gamedef);
1721 std::string tooltip_text = "";
1722 if(hovering && !m_selected_item)
1723 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
1724 if(tooltip_text != "")
1726 m_tooltip_element->setVisible(true);
1727 this->bringToFront(m_tooltip_element);
1728 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
1729 s32 tooltip_x = m_pointer.X + 15;
1730 s32 tooltip_y = m_pointer.Y + 15;
1731 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1732 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1733 m_tooltip_element->setRelativePosition(core::rect<s32>(
1734 core::position2d<s32>(tooltip_x, tooltip_y),
1735 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1741 void GUIFormSpecMenu::drawSelectedItem()
1743 if(!m_selected_item)
1746 video::IVideoDriver* driver = Environment->getVideoDriver();
1749 gui::IGUIFont *font = NULL;
1750 gui::IGUISkin* skin = Environment->getSkin();
1752 font = skin->getFont();
1754 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
1756 InventoryList *list = inv->getList(m_selected_item->listname);
1758 ItemStack stack = list->getItem(m_selected_item->i);
1759 stack.count = m_selected_amount;
1761 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1762 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
1763 drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
1766 void GUIFormSpecMenu::drawMenu()
1769 std::string newform = m_form_src->getForm();
1770 if(newform != m_formspec_string){
1771 m_formspec_string = newform;
1772 regenerateGui(m_screensize_old);
1776 m_pointer = m_device->getCursorControl()->getPosition();
1778 updateSelectedItem();
1780 gui::IGUISkin* skin = Environment->getSkin();
1783 video::IVideoDriver* driver = Environment->getVideoDriver();
1785 video::SColor bgcolor(140,0,0,0);
1786 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
1788 m_tooltip_element->setVisible(false);
1793 for(u32 i=0; i<m_backgrounds.size(); i++)
1795 const ImageDrawSpec &spec = m_backgrounds[i];
1796 video::ITexture *texture = 0;
1799 texture = m_gamedef->tsrc()->getTexture(spec.name);
1802 texture = driver->getTexture(spec.name.c_str());
1803 m_Textures.push_back(texture);
1807 // Image size on screen
1808 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1809 // Image rectangle on screen
1810 core::rect<s32> rect = imgrect + spec.pos;
1811 const video::SColor color(255,255,255,255);
1812 const video::SColor colors[] = {color,color,color,color};
1813 driver->draw2DImage(texture, rect,
1814 core::rect<s32>(core::position2d<s32>(0,0),
1815 core::dimension2di(texture->getOriginalSize())),
1816 NULL/*&AbsoluteClippingRect*/, colors, true);
1819 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
1820 errorstream << "\t" << spec.name << std::endl;
1827 for(u32 i=0; i<m_boxes.size(); i++)
1829 const BoxDrawSpec &spec = m_boxes[i];
1831 irr::video::SColor todraw = spec.color;
1833 todraw.setAlpha(140);
1835 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
1836 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
1838 driver->draw2DRectangle(todraw, rect, 0);
1843 for(u32 i=0; i<m_images.size(); i++)
1845 const ImageDrawSpec &spec = m_images[i];
1846 video::ITexture *texture = 0;
1849 texture = m_gamedef->tsrc()->getTexture(spec.name);
1852 texture = driver->getTexture(spec.name.c_str());
1853 m_Textures.push_back(texture);
1856 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
1857 // Image size on screen
1858 core::rect<s32> imgrect;
1861 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
1864 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
1866 // Image rectangle on screen
1867 core::rect<s32> rect = imgrect + spec.pos;
1868 const video::SColor color(255,255,255,255);
1869 const video::SColor colors[] = {color,color,color,color};
1870 driver->draw2DImage(texture, rect,
1871 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
1872 NULL/*&AbsoluteClippingRect*/, colors, true);
1875 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
1876 errorstream << "\t" << spec.name << std::endl;
1883 for(u32 i=0; i<m_itemimages.size(); i++)
1888 const ImageDrawSpec &spec = m_itemimages[i];
1889 IItemDefManager *idef = m_gamedef->idef();
1891 item.deSerialize(spec.name, idef);
1892 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1893 // Image size on screen
1894 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1895 // Image rectangle on screen
1896 core::rect<s32> rect = imgrect + spec.pos;
1897 const video::SColor color(255,255,255,255);
1898 const video::SColor colors[] = {color,color,color,color};
1899 driver->draw2DImage(texture, rect,
1900 core::rect<s32>(core::position2d<s32>(0,0),
1901 core::dimension2di(texture->getOriginalSize())),
1902 NULL/*&AbsoluteClippingRect*/, colors, true);
1907 Phase 0: Item slot rectangles
1908 Phase 1: Item images; prepare tooltip
1909 If backgrounds used, do not draw Item slot rectangles
1912 if (m_backgrounds.size() > 0) start_phase=1;
1913 for(int phase=start_phase; phase<=1; phase++)
1914 for(u32 i=0; i<m_inventorylists.size(); i++)
1916 drawList(m_inventorylists[i], phase);
1922 gui::IGUIElement::draw();
1925 Draw fields/buttons tooltips
1927 for(u32 i=0; i<m_fields.size(); i++)
1929 const FieldSpec &spec = m_fields[i];
1930 if (spec.tooltip != "")
1932 core::rect<s32> rect = spec.rect;
1933 if (rect.isPointInside(m_pointer))
1935 m_tooltip_element->setVisible(true);
1936 this->bringToFront(m_tooltip_element);
1937 m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
1938 s32 tooltip_x = m_pointer.X + 15;
1939 s32 tooltip_y = m_pointer.Y + 15;
1940 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1941 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1942 m_tooltip_element->setRelativePosition(core::rect<s32>(
1943 core::position2d<s32>(tooltip_x, tooltip_y),
1944 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1950 Draw dragged item stack
1955 void GUIFormSpecMenu::updateSelectedItem()
1957 // If the selected stack has become empty for some reason, deselect it.
1958 // If the selected stack has become inaccessible, deselect it.
1959 // If the selected stack has become smaller, adjust m_selected_amount.
1960 ItemStack selected = verifySelectedItem();
1962 // WARNING: BLACK MAGIC
1963 // See if there is a stack suited for our current guess.
1964 // If such stack does not exist, clear the guess.
1965 if(m_selected_content_guess.name != "" &&
1966 selected.name == m_selected_content_guess.name &&
1967 selected.count == m_selected_content_guess.count){
1968 // Selected item fits the guess. Skip the black magic.
1970 else if(m_selected_content_guess.name != ""){
1972 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
1973 const ListDrawSpec &s = m_inventorylists[i];
1974 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1977 InventoryList *list = inv->getList(s.listname);
1980 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
1981 u32 item_i = i + s.start_item_i;
1982 if(item_i >= list->getSize())
1984 ItemStack stack = list->getItem(item_i);
1985 if(stack.name == m_selected_content_guess.name &&
1986 stack.count == m_selected_content_guess.count){
1988 infostream<<"Client: Changing selected content guess to "
1989 <<s.inventoryloc.dump()<<" "<<s.listname
1990 <<" "<<item_i<<std::endl;
1991 delete m_selected_item;
1992 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
1993 m_selected_amount = stack.count;
1998 infostream<<"Client: Discarding selected content guess: "
1999 <<m_selected_content_guess.getItemString()<<std::endl;
2000 m_selected_content_guess.name = "";
2004 // If craftresult is nonempty and nothing else is selected, select it now.
2005 if(!m_selected_item)
2007 for(u32 i=0; i<m_inventorylists.size(); i++)
2009 const ListDrawSpec &s = m_inventorylists[i];
2010 if(s.listname == "craftpreview")
2012 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2013 InventoryList *list = inv->getList("craftresult");
2014 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2016 m_selected_item = new ItemSpec;
2017 m_selected_item->inventoryloc = s.inventoryloc;
2018 m_selected_item->listname = "craftresult";
2019 m_selected_item->i = 0;
2020 m_selected_amount = 0;
2021 m_selected_dragging = false;
2028 // If craftresult is selected, keep the whole stack selected
2029 if(m_selected_item && m_selected_item->listname == "craftresult")
2031 m_selected_amount = verifySelectedItem().count;
2035 ItemStack GUIFormSpecMenu::verifySelectedItem()
2037 // If the selected stack has become empty for some reason, deselect it.
2038 // If the selected stack has become inaccessible, deselect it.
2039 // If the selected stack has become smaller, adjust m_selected_amount.
2040 // Return the selected stack.
2044 if(m_selected_item->isValid())
2046 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2049 InventoryList *list = inv->getList(m_selected_item->listname);
2050 if(list && (u32) m_selected_item->i < list->getSize())
2052 ItemStack stack = list->getItem(m_selected_item->i);
2053 if(m_selected_amount > stack.count)
2054 m_selected_amount = stack.count;
2061 // selection was not valid
2062 delete m_selected_item;
2063 m_selected_item = NULL;
2064 m_selected_amount = 0;
2065 m_selected_dragging = false;
2070 void GUIFormSpecMenu::acceptInput()
2074 std::map<std::string, std::string> fields;
2076 if (current_keys_pending.key_down) {
2077 fields["key_down"] = "true";
2078 current_keys_pending.key_down = false;
2081 if (current_keys_pending.key_up) {
2082 fields["key_up"] = "true";
2083 current_keys_pending.key_up = false;
2086 if (current_keys_pending.key_enter) {
2087 fields["key_enter"] = "true";
2088 current_keys_pending.key_enter = false;
2091 if (current_keys_pending.key_escape) {
2092 fields["key_escape"] = "true";
2093 current_keys_pending.key_escape = false;
2096 for(u32 i=0; i<m_fields.size(); i++)
2098 const FieldSpec &s = m_fields[i];
2101 if(s.ftype == f_Button)
2103 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(s.flabel.c_str());
2105 else if(s.ftype == f_ListBox) {
2106 std::stringstream ss;
2108 if (m_listbox_doubleclick) {
2114 ss << (getListboxIndex(wide_to_narrow(s.fname.c_str()))+1);
2115 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2117 else if(s.ftype == f_DropDown) {
2118 // no dynamic cast possible due to some distributions shipped
2119 // without rtti support in irrlicht
2120 IGUIElement * element = getElementFromId(s.fid);
2121 gui::IGUIComboBox *e = NULL;
2122 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2123 e = static_cast<gui::IGUIComboBox*>(element);
2125 fields[wide_to_narrow(s.fname.c_str())] =
2126 wide_to_narrow(e->getItem(e->getSelected()));
2128 else if (s.ftype == f_TabHeader) {
2129 // no dynamic cast possible due to some distributions shipped
2130 // without rtti support in irrlicht
2131 IGUIElement * element = getElementFromId(s.fid);
2132 gui::IGUITabControl *e = NULL;
2133 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2134 e = static_cast<gui::IGUITabControl*>(element);
2138 std::stringstream ss;
2139 ss << (e->getActiveTab() +1);
2140 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2143 else if (s.ftype == f_CheckBox) {
2144 // no dynamic cast possible due to some distributions shipped
2145 // without rtti support in irrlicht
2146 IGUIElement * element = getElementFromId(s.fid);
2147 gui::IGUICheckBox *e = NULL;
2148 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2149 e = static_cast<gui::IGUICheckBox*>(element);
2154 fields[wide_to_narrow(s.fname.c_str())] = "true";
2156 fields[wide_to_narrow(s.fname.c_str())] = "false";
2161 IGUIElement* e = getElementFromId(s.fid);
2164 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(e->getText());
2170 m_text_dst->gotText(fields);
2174 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2176 // Fix Esc/Return key being eaten by checkboxen and listboxen
2177 if(event.EventType==EET_KEY_INPUT_EVENT)
2179 KeyPress kp(event.KeyInput);
2180 if (kp == EscapeKey || kp == getKeySetting("keymap_inventory")
2181 || event.KeyInput.Key==KEY_RETURN)
2183 gui::IGUIElement *focused = Environment->getFocus();
2184 if (focused && isMyChild(focused) &&
2185 (focused->getType() == gui::EGUIET_LIST_BOX ||
2186 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2192 // Mouse wheel events: send to hovered element instead of focused
2193 if(event.EventType==EET_MOUSE_INPUT_EVENT
2194 && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
2196 s32 x = event.MouseInput.X;
2197 s32 y = event.MouseInput.Y;
2198 gui::IGUIElement *hovered =
2199 Environment->getRootGUIElement()->getElementFromPoint(
2200 core::position2d<s32>(x, y));
2201 if (hovered && isMyChild(hovered)) {
2202 hovered->OnEvent(event);
2209 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2211 if(event.EventType==EET_KEY_INPUT_EVENT)
2213 KeyPress kp(event.KeyInput);
2214 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
2215 kp == getKeySetting("keymap_inventory")))
2220 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
2223 if (event.KeyInput.PressedDown &&
2224 (event.KeyInput.Key==KEY_RETURN ||
2225 event.KeyInput.Key==KEY_UP ||
2226 event.KeyInput.Key==KEY_DOWN)
2230 switch (event.KeyInput.Key) {
2237 current_keys_pending.key_enter = true;
2240 current_keys_pending.key_up = true;
2243 current_keys_pending.key_down = true;
2247 //can't happen at all!
2248 assert("reached a source line that can't ever been reached" == 0);
2256 if(event.EventType==EET_MOUSE_INPUT_EVENT
2257 && event.MouseInput.Event != EMIE_MOUSE_MOVED)
2259 // Mouse event other than movement
2261 // Get selected item and hovered/clicked item (s)
2263 updateSelectedItem();
2264 ItemSpec s = getItemAtPos(m_pointer);
2266 Inventory *inv_selected = NULL;
2267 Inventory *inv_s = NULL;
2271 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
2272 assert(inv_selected);
2273 assert(inv_selected->getList(m_selected_item->listname) != NULL);
2280 inv_s = m_invmgr->getInventory(s.inventoryloc);
2283 errorstream<<"InventoryMenu: The selected inventory location "
2284 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2286 s.i = -1; // make it invalid again
2290 InventoryList *list = inv_s->getList(s.listname);
2292 verbosestream<<"InventoryMenu: The selected inventory list \""
2293 <<s.listname<<"\" does not exist"<<std::endl;
2294 s.i = -1; // make it invalid again
2298 if((u32)s.i >= list->getSize()){
2299 infostream<<"InventoryMenu: The selected inventory list \""
2300 <<s.listname<<"\" is too small (i="<<s.i<<", size="
2301 <<list->getSize()<<")"<<std::endl;
2302 s.i = -1; // make it invalid again
2306 s_count = list->getItem(s.i).count;
2309 bool identical = (m_selected_item != NULL) && s.isValid() &&
2310 (inv_selected == inv_s) &&
2311 (m_selected_item->listname == s.listname) &&
2312 (m_selected_item->i == s.i);
2314 // buttons: 0 = left, 1 = right, 2 = middle
2315 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
2318 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
2319 { button = 0; updown = 0; }
2320 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
2321 { button = 1; updown = 0; }
2322 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
2323 { button = 2; updown = 0; }
2324 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
2325 { button = 0; updown = 1; }
2326 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
2327 { button = 1; updown = 1; }
2328 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
2329 { button = 2; updown = 1; }
2331 // Set this number to a positive value to generate a move action
2332 // from m_selected_item to s.
2333 u32 move_amount = 0;
2335 // Set this number to a positive value to generate a drop action
2336 // from m_selected_item.
2337 u32 drop_amount = 0;
2339 // Set this number to a positive value to generate a craft action at s.
2340 u32 craft_amount = 0;
2344 // Some mouse button has been pressed
2346 //infostream<<"Mouse button "<<button<<" pressed at p=("
2347 // <<p.X<<","<<p.Y<<")"<<std::endl;
2349 m_selected_dragging = false;
2351 if(s.isValid() && s.listname == "craftpreview")
2353 // Craft preview has been clicked: craft
2354 craft_amount = (button == 2 ? 10 : 1);
2356 else if(m_selected_item == NULL)
2360 // Non-empty stack has been clicked: select it
2361 m_selected_item = new ItemSpec(s);
2363 if(button == 1) // right
2364 m_selected_amount = (s_count + 1) / 2;
2365 else if(button == 2) // middle
2366 m_selected_amount = MYMIN(s_count, 10);
2368 m_selected_amount = s_count;
2370 m_selected_dragging = true;
2373 else // m_selected_item != NULL
2375 assert(m_selected_amount >= 1);
2379 // Clicked a slot: move
2380 if(button == 1) // right
2382 else if(button == 2) // middle
2383 move_amount = MYMIN(m_selected_amount, 10);
2385 move_amount = m_selected_amount;
2389 if(move_amount >= m_selected_amount)
2390 m_selected_amount = 0;
2392 m_selected_amount -= move_amount;
2396 else if(getAbsoluteClippingRect().isPointInside(m_pointer))
2398 // Clicked somewhere else: deselect
2399 m_selected_amount = 0;
2403 // Clicked outside of the window: drop
2404 if(button == 1) // right
2406 else if(button == 2) // middle
2407 drop_amount = MYMIN(m_selected_amount, 10);
2409 drop_amount = m_selected_amount;
2413 else if(updown == 1)
2415 // Some mouse button has been released
2417 //infostream<<"Mouse button "<<button<<" released at p=("
2418 // <<p.X<<","<<p.Y<<")"<<std::endl;
2420 if(m_selected_item != NULL && m_selected_dragging && s.isValid())
2424 // Dragged to different slot: move all selected
2425 move_amount = m_selected_amount;
2428 else if(m_selected_item != NULL && m_selected_dragging &&
2429 !(getAbsoluteClippingRect().isPointInside(m_pointer)))
2431 // Dragged outside of window: drop all selected
2432 drop_amount = m_selected_amount;
2435 m_selected_dragging = false;
2438 // Possibly send inventory action to server
2441 // Send IACTION_MOVE
2443 assert(m_selected_item && m_selected_item->isValid());
2444 assert(s.isValid());
2446 assert(inv_selected && inv_s);
2447 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2448 InventoryList *list_to = inv_s->getList(s.listname);
2449 assert(list_from && list_to);
2450 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2451 ItemStack stack_to = list_to->getItem(s.i);
2453 // Check how many items can be moved
2454 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
2455 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
2456 // If source stack cannot be added to destination stack at all,
2458 if(leftover.count == stack_from.count && leftover.name == stack_from.name)
2460 m_selected_amount = stack_to.count;
2461 // In case the server doesn't directly swap them but instead
2462 // moves stack_to somewhere else, set this
2463 m_selected_content_guess = stack_to;
2464 m_selected_content_guess_inventory = s.inventoryloc;
2466 // Source stack goes fully into destination stack
2467 else if(leftover.empty())
2469 m_selected_amount -= move_amount;
2470 m_selected_content_guess = ItemStack(); // Clear
2472 // Source stack goes partly into destination stack
2475 move_amount -= leftover.count;
2476 m_selected_amount -= move_amount;
2477 m_selected_content_guess = ItemStack(); // Clear
2480 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
2481 IMoveAction *a = new IMoveAction();
2482 a->count = move_amount;
2483 a->from_inv = m_selected_item->inventoryloc;
2484 a->from_list = m_selected_item->listname;
2485 a->from_i = m_selected_item->i;
2486 a->to_inv = s.inventoryloc;
2487 a->to_list = s.listname;
2489 m_invmgr->inventoryAction(a);
2491 else if(drop_amount > 0)
2493 m_selected_content_guess = ItemStack(); // Clear
2495 // Send IACTION_DROP
2497 assert(m_selected_item && m_selected_item->isValid());
2498 assert(inv_selected);
2499 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2501 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2503 // Check how many items can be dropped
2504 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
2505 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
2506 m_selected_amount -= drop_amount;
2508 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
2509 IDropAction *a = new IDropAction();
2510 a->count = drop_amount;
2511 a->from_inv = m_selected_item->inventoryloc;
2512 a->from_list = m_selected_item->listname;
2513 a->from_i = m_selected_item->i;
2514 m_invmgr->inventoryAction(a);
2516 else if(craft_amount > 0)
2518 m_selected_content_guess = ItemStack(); // Clear
2520 // Send IACTION_CRAFT
2522 assert(s.isValid());
2525 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
2526 ICraftAction *a = new ICraftAction();
2527 a->count = craft_amount;
2528 a->craft_inv = s.inventoryloc;
2529 m_invmgr->inventoryAction(a);
2532 // If m_selected_amount has been decreased to zero, deselect
2533 if(m_selected_amount == 0)
2535 delete m_selected_item;
2536 m_selected_item = NULL;
2537 m_selected_amount = 0;
2538 m_selected_dragging = false;
2539 m_selected_content_guess = ItemStack();
2542 if(event.EventType==EET_GUI_EVENT)
2545 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
2548 // find the element that was clicked
2549 for(u32 i=0; i<m_fields.size(); i++)
2551 FieldSpec &s = m_fields[i];
2552 // if its a button, set the send field so
2553 // lua knows which button was pressed
2554 if ((s.ftype == f_TabHeader) && (s.fid == event.GUIEvent.Caller->getID()))
2563 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
2566 if(!canTakeFocus(event.GUIEvent.Element))
2568 infostream<<"GUIFormSpecMenu: Not allowing focus change."
2570 // Returning true disables focus change
2574 if((event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) ||
2575 (event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED))
2577 unsigned int btn_id = event.GUIEvent.Caller->getID();
2579 if (btn_id == 257) {
2584 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2585 // quitMenu deallocates menu
2589 // find the element that was clicked
2590 for(u32 i=0; i<m_fields.size(); i++)
2592 FieldSpec &s = m_fields[i];
2593 // if its a button, set the send field so
2594 // lua knows which button was pressed
2595 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
2596 (s.fid == event.GUIEvent.Caller->getID()))
2604 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2613 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
2615 if(event.GUIEvent.Caller->getID() > 257)
2623 current_keys_pending.key_enter = true;
2626 // quitMenu deallocates menu
2631 if((event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN) ||
2632 (event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED))
2634 int current_id = event.GUIEvent.Caller->getID();
2635 if(current_id > 257)
2637 // find the element that was clicked
2638 for(u32 i=0; i<m_fields.size(); i++)
2640 FieldSpec &s = m_fields[i];
2641 // if its a listbox, set the send field so
2642 // lua knows which listbox was changed
2643 // checkListboxClick() is black magic
2644 // for properly handling double clicks
2645 if ((s.ftype == f_ListBox) && (s.fid == current_id)
2646 && checkListboxClick(s.fname,
2647 event.GUIEvent.EventType))
2659 return Parent ? Parent->OnEvent(event) : false;
2662 bool GUIFormSpecMenu::parseColor(std::string color, irr::video::SColor& outcolor) {
2663 outcolor = irr::video::SColor(0,0,0,0);
2665 if (!string_allowed(color, "0123456789abcdefABCDEF"))
2669 std::istringstream iss(color);
2670 iss >> std::hex >> color_value;
2672 outcolor = irr::video::SColor(color_value);
2674 outcolor.setAlpha(255);