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);
128 if(m_tooltip_element)
130 m_tooltip_element->remove();
131 m_tooltip_element->drop();
132 m_tooltip_element = NULL;
137 void GUIFormSpecMenu::setInitialFocus()
139 // Set initial focus according to following order of precedence:
140 // 1. first empty editbox
144 // 5. first focusable (not statictext, not tabheader)
145 // 6. first child element
147 core::list<gui::IGUIElement*> children = getChildren();
149 // in case "children" contains any NULL elements, remove them
150 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
151 it != children.end();) {
155 it = children.erase(it);
158 // 1. first empty editbox
159 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
160 it != children.end(); ++it) {
161 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
162 && (*it)->getText()[0] == 0) {
163 Environment->setFocus(*it);
169 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
170 it != children.end(); ++it) {
171 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
172 Environment->setFocus(*it);
178 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
179 it != children.end(); ++it) {
180 if ((*it)->getType() == gui::EGUIET_LIST_BOX) {
181 Environment->setFocus(*it);
187 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
188 it != children.end(); --it) {
189 if ((*it)->getType() == gui::EGUIET_BUTTON) {
190 Environment->setFocus(*it);
195 // 5. first focusable (not statictext, not tabheader)
196 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
197 it != children.end(); ++it) {
198 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
199 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
200 Environment->setFocus(*it);
205 // 6. first child element
206 if (children.empty())
207 Environment->setFocus(this);
209 Environment->setFocus(*(children.begin()));
212 int GUIFormSpecMenu::getListboxIndex(std::string listboxname) {
214 std::wstring wlistboxname = narrow_to_wide(listboxname.c_str());
216 for(unsigned int i=0; i < m_listboxes.size(); i++) {
218 std::wstring name(m_listboxes[i].first.fname.c_str());
219 if ( name == wlistboxname) {
220 return m_listboxes[i].second->getSelected();
226 bool GUIFormSpecMenu::checkListboxClick(std::wstring wlistboxname,
229 // WARNING: BLACK IRRLICHT MAGIC
230 // Used to fix Irrlicht's subpar reporting of single clicks and double
231 // clicks in listboxes (gui::EGET_LISTBOX_CHANGED,
232 // gui::EGET_LISTBOX_SELECTED_AGAIN):
233 // 1. IGUIListBox::setSelected() is counted as a click.
234 // Including the initial setSelected() done by parseTextList().
235 // 2. Clicking on a the selected item and then dragging for less
236 // than 500ms is counted as a doubleclick, no matter when the
237 // item was previously selected (e.g. more than 500ms ago)
239 // So when Irrlicht reports a doubleclick, we need to check
240 // for ourselves if really was a doubleclick. Or just a fake.
242 for(unsigned int i=0; i < m_listboxes.size(); i++) {
243 std::wstring name(m_listboxes[i].first.fname.c_str());
244 int selected = m_listboxes[i].second->getSelected();
245 if (name == wlistboxname && selected >= 0) {
246 u32 now = getTimeMs();
248 (eventtype == gui::EGET_LISTBOX_SELECTED_AGAIN)
249 && (name == m_listbox_click_fname)
250 && (selected == m_listbox_click_index)
251 && (m_listbox_click_time >= now - 500);
252 m_listbox_click_fname = name;
253 m_listbox_click_index = selected;
254 m_listbox_click_time = now;
255 m_listbox_doubleclick = doubleclick;
262 gui::IGUIScrollBar* GUIFormSpecMenu::getListboxScrollbar(
263 gui::IGUIListBox *listbox)
265 // WARNING: BLACK IRRLICHT MAGIC
266 // Ordinarily, due to how formspecs work (recreating the entire GUI
267 // when something changes), when you select an item in a textlist
268 // with more items than fit in the visible area, the newly selected
269 // item is scrolled to the bottom of the visible area. This is
270 // annoying and breaks GUI designs that use double clicks.
272 // This function helps fixing this problem by giving direct access
273 // to a listbox's scrollbar. This works because CGUIListBox doesn't
274 // cache the scrollbar position anywhere.
276 // If this stops working in a future irrlicht version, consider
277 // maintaining a local copy of irr::gui::CGUIListBox, possibly also
278 // fixing the other reasons why black irrlicht magic is needed.
280 core::list<gui::IGUIElement*> children = listbox->getChildren();
281 for(core::list<gui::IGUIElement*>::Iterator it = children.begin();
282 it != children.end(); ++it) {
283 gui::IGUIElement* child = *it;
284 if (child && child->getType() == gui::EGUIET_SCROLL_BAR) {
285 return static_cast<gui::IGUIScrollBar*>(child);
289 verbosestream<<"getListboxScrollbar: WARNING: "
290 <<"listbox has no scrollbar"<<std::endl;
294 std::vector<std::string> split(const std::string &s, char delim) {
295 std::vector<std::string> tokens;
297 std::string current = "";
298 bool last_was_escape = false;
299 for(unsigned int i=0; i < s.size(); i++) {
300 if (last_was_escape) {
302 current += s.c_str()[i];
303 last_was_escape = false;
306 if (s.c_str()[i] == delim) {
307 tokens.push_back(current);
309 last_was_escape = false;
311 else if (s.c_str()[i] == '\\'){
312 last_was_escape = true;
315 current += s.c_str()[i];
316 last_was_escape = false;
321 tokens.push_back(current);
326 void GUIFormSpecMenu::parseSize(parserData* data,std::string element) {
327 std::vector<std::string> parts = split(element,',');
329 if (parts.size() == 2) {
332 if (parts[1].find(';') != std::string::npos)
333 parts[1] = parts[1].substr(0,parts[1].find(';'));
335 invsize.X = stof(parts[0]);
336 invsize.Y = stof(parts[1]);
339 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
340 v2u32 delta = current_screensize - m_lockscreensize;
342 if (current_screensize.Y > m_lockscreensize.Y)
347 if (current_screensize.X > m_lockscreensize.X)
352 offset = v2s32(delta.X,delta.Y);
354 data->screensize = m_lockscreensize;
360 padding = v2s32(data->screensize.Y/40, data->screensize.Y/40);
361 spacing = v2s32(data->screensize.Y/12, data->screensize.Y/13);
362 imgsize = v2s32(data->screensize.Y/15, data->screensize.Y/15);
364 padding.X*2+spacing.X*(invsize.X-1.0)+imgsize.X,
365 padding.Y*2+spacing.Y*(invsize.Y-1.0)+imgsize.Y + (data->helptext_h-5)
367 data->rect = core::rect<s32>(
368 data->screensize.X/2 - data->size.X/2 + offset.X,
369 data->screensize.Y/2 - data->size.Y/2 + offset.Y,
370 data->screensize.X/2 + data->size.X/2 + offset.X,
371 data->screensize.Y/2 + data->size.Y/2 + offset.Y
374 DesiredRect = data->rect;
375 recalculateAbsolutePosition(false);
376 data->basepos = getBasePos();
380 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
383 void GUIFormSpecMenu::parseList(parserData* data,std::string element) {
385 if (m_gamedef == 0) {
386 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
390 std::vector<std::string> parts = split(element,';');
392 if ((parts.size() == 4) || (parts.size() == 5)) {
393 std::string location = parts[0];
394 std::string listname = parts[1];
395 std::vector<std::string> v_pos = split(parts[2],',');
396 std::vector<std::string> v_geom = split(parts[3],',');
397 std::string startindex = "";
398 if (parts.size() == 5)
399 startindex = parts[4];
401 MY_CHECKPOS("list",2);
402 MY_CHECKGEOM("list",3);
404 InventoryLocation loc;
406 if(location == "context" || location == "current_name")
407 loc = m_current_inventory_location;
409 loc.deSerialize(location);
411 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
412 pos.X += stof(v_pos[0]) * (float)spacing.X;
413 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
416 geom.X = stoi(v_geom[0]);
417 geom.Y = stoi(v_geom[1]);
421 start_i = stoi(startindex);
422 if(data->bp_set != 2)
423 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
424 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
427 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
430 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element) {
431 std::vector<std::string> parts = split(element,';');
433 if ((parts.size() == 3) || (parts.size() == 4)) {
434 std::vector<std::string> v_pos = split(parts[0],',');
435 std::string name = parts[1];
436 std::string label = parts[2];
437 std::string selected = "";
439 if (parts.size() == 4)
442 MY_CHECKPOS("checkbox",0);
445 pos.X += stof(v_pos[0]) * (float) spacing.X;
446 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
448 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
450 bool fselected = false;
452 if (selected == "true")
455 std::wstring wlabel = narrow_to_wide(label.c_str());
457 FieldSpec spec = FieldSpec(
458 narrow_to_wide(name.c_str()),
464 spec.ftype = f_CheckBox;
465 spec.flabel = wlabel; //Needed for displaying text on MSVC
466 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
467 spec.fid, spec.flabel.c_str());
469 if (spec.fname == data->focused_fieldname) {
470 Environment->setFocus(e);
473 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
474 m_fields.push_back(spec);
477 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
480 void GUIFormSpecMenu::parseImage(parserData* data,std::string element) {
481 std::vector<std::string> parts = split(element,';');
483 if (parts.size() == 3) {
484 std::vector<std::string> v_pos = split(parts[0],',');
485 std::vector<std::string> v_geom = split(parts[1],',');
486 std::string name = parts[2];
488 MY_CHECKPOS("image",0);
489 MY_CHECKGEOM("image",1);
491 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
492 pos.X += stof(v_pos[0]) * (float) spacing.X;
493 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
496 geom.X = stof(v_geom[0]) * (float)imgsize.X;
497 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
499 if(data->bp_set != 2)
500 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
501 m_images.push_back(ImageDrawSpec(name, pos, geom));
505 if (parts.size() == 2) {
506 std::vector<std::string> v_pos = split(parts[0],',');
507 std::string name = parts[1];
509 MY_CHECKPOS("image",0);
511 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
512 pos.X += stof(v_pos[0]) * (float) spacing.X;
513 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
515 if(data->bp_set != 2)
516 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
517 m_images.push_back(ImageDrawSpec(name, pos));
520 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
523 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element) {
524 std::vector<std::string> parts = split(element,';');
526 if (parts.size() == 3) {
527 std::vector<std::string> v_pos = split(parts[0],',');
528 std::vector<std::string> v_geom = split(parts[1],',');
529 std::string name = parts[2];
531 MY_CHECKPOS("itemimage",0);
532 MY_CHECKGEOM("itemimage",1);
534 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
535 pos.X += stof(v_pos[0]) * (float) spacing.X;
536 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
539 geom.X = stoi(v_geom[0]) * (float)imgsize.X;
540 geom.Y = stoi(v_geom[1]) * (float)imgsize.Y;
542 if(data->bp_set != 2)
543 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
544 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
547 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
550 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,std::string type) {
551 std::vector<std::string> parts = split(element,';');
553 if (parts.size() == 4) {
554 std::vector<std::string> v_pos = split(parts[0],',');
555 std::vector<std::string> v_geom = split(parts[1],',');
556 std::string name = parts[2];
557 std::string label = parts[3];
559 MY_CHECKPOS("button",0);
560 MY_CHECKGEOM("button",1);
563 pos.X += stof(v_pos[0]) * (float)spacing.X;
564 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
567 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
568 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
570 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y-15, pos.X+geom.X, pos.Y+15);
572 if(data->bp_set != 2)
573 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
575 label = unescape_string(label);
577 std::wstring wlabel = narrow_to_wide(label.c_str());
579 FieldSpec spec = FieldSpec(
580 narrow_to_wide(name.c_str()),
585 spec.ftype = f_Button;
586 if(type == "button_exit")
589 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
590 spec.flabel.c_str());
592 if (spec.fname == data->focused_fieldname) {
593 Environment->setFocus(e);
596 m_fields.push_back(spec);
599 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
602 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element) {
603 std::vector<std::string> parts = split(element,';');
605 if (parts.size() == 3) {
606 std::vector<std::string> v_pos = split(parts[0],',');
607 std::vector<std::string> v_geom = split(parts[1],',');
608 std::string name = parts[2];
610 MY_CHECKPOS("background",0);
611 MY_CHECKGEOM("background",1);
613 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
614 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
615 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
618 geom.X = stof(v_geom[0]) * (float)spacing.X;
619 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
621 if(data->bp_set != 2)
622 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
623 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
626 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
629 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element) {
630 std::vector<std::string> parts = split(element,';');
632 if ((parts.size() == 5) || (parts.size() == 6)) {
633 std::vector<std::string> v_pos = split(parts[0],',');
634 std::vector<std::string> v_geom = split(parts[1],',');
635 std::string name = parts[2];
636 std::vector<std::string> items = split(parts[3],',');
637 std::string str_initial_selection = "";
638 std::string str_transparent = "false";
640 if (parts.size() >= 5)
641 str_initial_selection = parts[4];
643 if (parts.size() >= 6)
644 str_transparent = parts[5];
646 MY_CHECKPOS("textlist",0);
647 MY_CHECKGEOM("textlist",1);
650 pos.X += stof(v_pos[0]) * (float)spacing.X;
651 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
654 geom.X = stof(v_geom[0]) * (float)spacing.X;
655 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
658 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
660 std::wstring fname_w = narrow_to_wide(name.c_str());
662 FieldSpec spec = FieldSpec(
669 spec.ftype = f_ListBox;
671 //now really show list
672 gui::IGUIListBox *e = Environment->addListBox(rect, this,spec.fid);
674 if (spec.fname == data->focused_fieldname) {
675 Environment->setFocus(e);
678 if (str_transparent == "false")
679 e->setDrawBackground(true);
681 for (unsigned int i=0; i < items.size(); i++) {
682 if (items[i].c_str()[0] == '#') {
683 if (items[i].c_str()[1] == '#') {
684 e->addItem(narrow_to_wide(unescape_string(items[i])).c_str() +1);
687 std::string color = items[i].substr(1,6);
689 narrow_to_wide(unescape_string(items[i]).c_str() + 7);
691 e->addItem(toadd.c_str());
693 irr::video::SColor toset;
695 if (parseColor(color, toset))
696 e->setItemOverrideColor(i,toset);
700 e->addItem(narrow_to_wide(unescape_string(items[i])).c_str());
704 if (data->listbox_selections.find(fname_w) != data->listbox_selections.end()) {
705 e->setSelected(data->listbox_selections[fname_w]);
708 if (data->listbox_scroll.find(fname_w) != data->listbox_scroll.end()) {
709 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(e);
711 scrollbar->setPos(data->listbox_scroll[fname_w]);
715 if (str_initial_selection != "")
716 e->setSelected(stoi(str_initial_selection.c_str())-1);
718 m_listboxes.push_back(std::pair<FieldSpec,gui::IGUIListBox*>(spec,e));
719 m_fields.push_back(spec);
722 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
726 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element) {
727 std::vector<std::string> parts = split(element,';');
729 if (parts.size() == 5) {
730 std::vector<std::string> v_pos = split(parts[0],',');
731 std::string name = parts[2];
732 std::vector<std::string> items = split(parts[3],',');
733 std::string str_initial_selection = "";
734 str_initial_selection = parts[4];
736 MY_CHECKPOS("dropdown",0);
739 pos.X += stof(v_pos[0]) * (float)spacing.X;
740 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
742 s32 width = stof(parts[1]) * (float)spacing.Y;
744 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+width, pos.Y+30);
746 std::wstring fname_w = narrow_to_wide(name.c_str());
748 FieldSpec spec = FieldSpec(
755 spec.ftype = f_DropDown;
758 //now really show list
759 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
761 if (spec.fname == data->focused_fieldname) {
762 Environment->setFocus(e);
765 for (unsigned int i=0; i < items.size(); i++) {
766 e->addItem(narrow_to_wide(items[i]).c_str());
769 if (str_initial_selection != "")
770 e->setSelected(stoi(str_initial_selection.c_str())-1);
772 //if (data->listbox_selections.find(fname_w) != data->listbox_selections.end()) {
773 // e->setSelected(data->listbox_selections[fname_w]);
776 //m_listboxes.push_back(std::pair<FieldSpec,gui::IGUIListBox*>(spec,e));
777 m_fields.push_back(spec);
780 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
781 << element << "'" << std::endl;
784 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element) {
785 std::vector<std::string> parts = split(element,';');
787 if (parts.size() == 4) {
788 std::vector<std::string> v_pos = split(parts[0],',');
789 std::vector<std::string> v_geom = split(parts[1],',');
790 std::string name = parts[2];
791 std::string label = parts[3];
793 MY_CHECKPOS("pwdfield",0);
794 MY_CHECKGEOM("pwdfield",1);
797 pos.X += stof(v_pos[0]) * (float)spacing.X;
798 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
801 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
803 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
807 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
809 label = unescape_string(label);
811 std::wstring wlabel = narrow_to_wide(label.c_str());
813 FieldSpec spec = FieldSpec(
814 narrow_to_wide(name.c_str()),
821 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
823 if (spec.fname == data->focused_fieldname) {
824 Environment->setFocus(e);
827 if (label.length() > 1)
829 rect.UpperLeftCorner.Y -= 15;
830 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
831 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
834 e->setPasswordBox(true,L'*');
837 evt.EventType = EET_KEY_INPUT_EVENT;
838 evt.KeyInput.Key = KEY_END;
839 evt.KeyInput.Char = 0;
840 evt.KeyInput.Control = 0;
841 evt.KeyInput.Shift = 0;
842 evt.KeyInput.PressedDown = true;
844 m_fields.push_back(spec);
847 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
850 void GUIFormSpecMenu::parseSimpleField(parserData* data,std::vector<std::string> &parts) {
851 std::string name = parts[0];
852 std::string label = parts[1];
853 std::string default_val = parts[2];
855 core::rect<s32> rect;
859 rect = core::rect<s32>(
860 data->screensize.X/2 - 580/2,
861 data->screensize.Y/2 - 300/2,
862 data->screensize.X/2 + 580/2,
863 data->screensize.Y/2 + 300/2
866 recalculateAbsolutePosition(false);
867 data->basepos = getBasePos();
870 else if(data->bp_set == 2)
871 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
873 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
874 pos.Y = ((m_fields.size()+2)*60);
875 v2s32 size = DesiredRect.getSize();
877 rect = core::rect<s32>(size.X/2-150, pos.Y, (size.X/2-150)+300, pos.Y+30);
881 default_val = m_form_src->resolveText(default_val);
883 default_val = unescape_string(default_val);
884 label = unescape_string(label);
886 std::wstring wlabel = narrow_to_wide(label.c_str());
888 FieldSpec spec = FieldSpec(
889 narrow_to_wide(name.c_str()),
891 narrow_to_wide(default_val.c_str()),
897 // spec field id to 0, this stops submit searching for a value that isn't there
898 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
903 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
905 if (spec.fname == data->focused_fieldname) {
906 Environment->setFocus(e);
910 evt.EventType = EET_KEY_INPUT_EVENT;
911 evt.KeyInput.Key = KEY_END;
912 evt.KeyInput.Char = 0;
913 evt.KeyInput.Control = 0;
914 evt.KeyInput.Shift = 0;
915 evt.KeyInput.PressedDown = true;
918 if (label.length() > 1)
920 rect.UpperLeftCorner.Y -= 15;
921 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
922 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
926 m_fields.push_back(spec);
929 void GUIFormSpecMenu::parseTextArea(parserData* data,std::vector<std::string>& parts,std::string type) {
931 std::vector<std::string> v_pos = split(parts[0],',');
932 std::vector<std::string> v_geom = split(parts[1],',');
933 std::string name = parts[2];
934 std::string label = parts[3];
935 std::string default_val = parts[4];
938 MY_CHECKGEOM(type,1);
941 pos.X = stof(v_pos[0]) * (float) spacing.X;
942 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
946 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
948 if (type == "textarea")
950 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
955 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
960 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
962 if(data->bp_set != 2)
963 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
966 default_val = m_form_src->resolveText(default_val);
969 default_val = unescape_string(default_val);
970 label = unescape_string(label);
972 std::wstring wlabel = narrow_to_wide(label.c_str());
974 FieldSpec spec = FieldSpec(
975 narrow_to_wide(name.c_str()),
977 narrow_to_wide(default_val.c_str()),
983 // spec field id to 0, this stops submit searching for a value that isn't there
984 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
989 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
991 if (spec.fname == data->focused_fieldname) {
992 Environment->setFocus(e);
995 if (type == "textarea")
997 e->setMultiLine(true);
998 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1001 evt.EventType = EET_KEY_INPUT_EVENT;
1002 evt.KeyInput.Key = KEY_END;
1003 evt.KeyInput.Char = 0;
1004 evt.KeyInput.Control = 0;
1005 evt.KeyInput.Shift = 0;
1006 evt.KeyInput.PressedDown = true;
1010 if (label.length() > 1)
1012 rect.UpperLeftCorner.Y -= 15;
1013 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
1014 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1017 m_fields.push_back(spec);
1020 void GUIFormSpecMenu::parseField(parserData* data,std::string element,std::string type) {
1021 std::vector<std::string> parts = split(element,';');
1023 if (parts.size() == 3) {
1024 parseSimpleField(data,parts);
1028 if (parts.size() == 5) {
1029 parseTextArea(data,parts,type);
1032 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1035 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element) {
1036 std::vector<std::string> parts = split(element,';');
1038 if (parts.size() == 2) {
1039 std::vector<std::string> v_pos = split(parts[0],',');
1040 std::string text = parts[1];
1042 MY_CHECKPOS("label",0);
1044 v2s32 pos = padding;
1045 pos.X += stof(v_pos[0]) * (float)spacing.X;
1046 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1048 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
1050 if(data->bp_set != 2)
1051 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1053 text = unescape_string(text);
1055 std::wstring wlabel = narrow_to_wide(text.c_str());
1057 FieldSpec spec = FieldSpec(
1063 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1064 m_fields.push_back(spec);
1067 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1070 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element) {
1071 std::vector<std::string> parts = split(element,';');
1073 if (parts.size() == 2) {
1074 std::vector<std::string> v_pos = split(parts[0],',');
1075 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1077 MY_CHECKPOS("vertlabel",1);
1079 v2s32 pos = padding;
1080 pos.X += stof(v_pos[0]) * (float)spacing.X;
1081 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1083 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+15, pos.Y+300);
1085 if(data->bp_set != 2)
1086 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1088 std::wstring label = L"";
1090 for (unsigned int i=0; i < text.length(); i++) {
1095 FieldSpec spec = FieldSpec(
1101 gui::IGUIStaticText *t =
1102 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1103 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1104 m_fields.push_back(spec);
1107 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1110 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,std::string type) {
1111 std::vector<std::string> parts = split(element,';');
1113 if ((parts.size() == 5) || (parts.size() == 7) || (parts.size() == 8)) {
1114 std::vector<std::string> v_pos = split(parts[0],',');
1115 std::vector<std::string> v_geom = split(parts[1],',');
1116 std::string image_name = parts[2];
1117 std::string name = parts[3];
1118 std::string label = parts[4];
1120 MY_CHECKPOS("imagebutton",0);
1121 MY_CHECKGEOM("imagebutton",1);
1123 v2s32 pos = padding;
1124 pos.X += stof(v_pos[0]) * (float)spacing.X;
1125 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1127 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1128 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1130 bool noclip = false;
1131 bool drawborder = true;
1133 if ((parts.size() >= 7)) {
1134 if (parts[5] == "true")
1137 if (parts[6] == "false")
1141 std::string pressed_image_name = "";
1143 if ((parts.size() == 8)) {
1144 pressed_image_name = parts[7];
1147 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1149 if(data->bp_set != 2)
1150 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1152 label = unescape_string(label);
1154 std::wstring wlabel = narrow_to_wide(label.c_str());
1156 FieldSpec spec = FieldSpec(
1157 narrow_to_wide(name.c_str()),
1159 narrow_to_wide(image_name.c_str()),
1162 spec.ftype = f_Button;
1163 if(type == "image_button_exit")
1164 spec.is_exit = true;
1166 video::ITexture *texture = 0;
1167 video::ITexture *pressed_texture = 0;
1168 //if there's no gamedef specified try to get direct
1169 //TODO check for possible texture leak
1170 if (m_gamedef != 0) {
1171 texture = m_gamedef->tsrc()->getTexture(image_name);
1172 if ((parts.size() == 8)) {
1173 pressed_texture = m_gamedef->tsrc()->getTexture(pressed_image_name);
1176 if (fs::PathExists(image_name)) {
1177 texture = Environment->getVideoDriver()->getTexture(image_name.c_str());
1178 m_Textures.push_back(texture);
1180 if (fs::PathExists(pressed_image_name)) {
1181 pressed_texture = Environment->getVideoDriver()->getTexture(pressed_image_name.c_str());
1182 m_Textures.push_back(pressed_texture);
1185 if (parts.size() < 8)
1186 pressed_texture = texture;
1188 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1190 if (spec.fname == data->focused_fieldname) {
1191 Environment->setFocus(e);
1194 e->setUseAlphaChannel(true);
1195 e->setImage(texture);
1196 e->setPressedImage(pressed_texture);
1197 e->setScaleImage(true);
1198 e->setNotClipped(noclip);
1199 e->setDrawBorder(drawborder);
1201 m_fields.push_back(spec);
1205 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1208 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element) {
1209 std::vector<std::string> parts = split(element,';');
1211 if ((parts.size() == 4) || (parts.size() == 6)) {
1212 std::vector<std::string> v_pos = split(parts[0],',');
1213 std::string name = parts[1];
1214 std::vector<std::string> buttons = split(parts[2],',');
1215 std::string str_index = parts[3];
1216 bool show_background = true;
1217 bool show_border = true;
1218 int tab_index = stoi(str_index) -1;
1220 MY_CHECKPOS("tabheader",0);
1222 if (parts.size() == 6) {
1223 if (parts[4] == "true")
1224 show_background = false;
1225 if (parts[5] == "false")
1226 show_border = false;
1229 FieldSpec spec = FieldSpec(
1230 narrow_to_wide(name.c_str()),
1236 spec.ftype = f_TabHeader;
1238 v2s32 pos = padding;
1239 pos.X += stof(v_pos[0]) * (float)spacing.X;
1240 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1242 geom.X = data->screensize.Y;
1245 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1247 gui::IGUITabControl *e = Environment->addTabControl(rect,this,show_background,show_border,spec.fid);
1249 if (spec.fname == data->focused_fieldname) {
1250 Environment->setFocus(e);
1253 e->setNotClipped(true);
1255 for (unsigned int i=0; i< buttons.size(); i++) {
1256 wchar_t* wbutton = 0;
1258 std::wstring wlabel = narrow_to_wide(buttons[i]); //Needed for displaying text on windows
1259 wbutton = (wchar_t*) wlabel.c_str();
1261 e->addTab(wbutton,-1);
1264 if ((tab_index >= 0) &&
1265 (buttons.size() < INT_MAX) &&
1266 (tab_index < (int) buttons.size()))
1267 e->setActiveTab(tab_index);
1269 m_fields.push_back(spec);
1272 errorstream<< "Invalid TabHeader element(" << parts.size() << "): '" << element << "'" << std::endl;
1275 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element) {
1277 if (m_gamedef == 0) {
1278 errorstream<<"WARNING: invalid use of item_image_button with m_gamedef==0"<<std::endl;
1282 std::vector<std::string> parts = split(element,';');
1284 if (parts.size() == 5) {
1285 std::vector<std::string> v_pos = split(parts[0],',');
1286 std::vector<std::string> v_geom = split(parts[1],',');
1287 std::string item_name = parts[2];
1288 std::string name = parts[3];
1289 std::string label = parts[4];
1291 MY_CHECKPOS("itemimagebutton",0);
1292 MY_CHECKGEOM("itemimagebutton",1);
1294 v2s32 pos = padding;
1295 pos.X += stof(v_pos[0]) * (float)spacing.X;
1296 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1298 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1299 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1301 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1303 if(data->bp_set != 2)
1304 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1306 IItemDefManager *idef = m_gamedef->idef();
1308 item.deSerialize(item_name, idef);
1309 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1310 std::string tooltip = item.getDefinition(idef).description;
1312 label = unescape_string(label);
1313 FieldSpec spec = FieldSpec(
1314 narrow_to_wide(name.c_str()),
1315 narrow_to_wide(label.c_str()),
1316 narrow_to_wide(item_name.c_str()),
1320 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1322 if (spec.fname == data->focused_fieldname) {
1323 Environment->setFocus(e);
1326 e->setUseAlphaChannel(true);
1327 e->setImage(texture);
1328 e->setPressedImage(texture);
1329 e->setScaleImage(true);
1330 spec.ftype = f_Button;
1331 rect+=data->basepos-padding;
1334 spec.tooltip=tooltip;
1335 m_fields.push_back(spec);
1338 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1341 void GUIFormSpecMenu::parseBox(parserData* data,std::string element) {
1342 std::vector<std::string> parts = split(element,';');
1344 if (parts.size() == 3) {
1345 std::vector<std::string> v_pos = split(parts[0],',');
1346 std::vector<std::string> v_geom = split(parts[1],',');
1347 std::string color_str = parts[2];
1349 MY_CHECKPOS("box",0);
1350 MY_CHECKGEOM("box",1);
1352 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1353 pos.X += stof(v_pos[0]) * (float) spacing.X;
1354 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1357 geom.X = stof(v_geom[0]) * (float)spacing.X;
1358 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1360 irr::video::SColor color;
1362 if (parseColor(color_str, color)) {
1363 BoxDrawSpec spec(pos,geom,color);
1365 m_boxes.push_back(spec);
1368 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1372 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1375 void GUIFormSpecMenu::parseElement(parserData* data,std::string element) {
1381 std::vector<std::string> parts = split(element,'[');
1383 // ugly workaround to keep compatibility
1384 if (parts.size() > 2) {
1385 if (trim(parts[0]) == "image") {
1386 for (unsigned int i=2;i< parts.size(); i++) {
1387 parts[1] += "[" + parts[i];
1393 if (parts.size() < 2) {
1397 std::string type = trim(parts[0]);
1398 std::string description = trim(parts[1]);
1400 if ((type == "size") || (type == "invsize")){
1401 parseSize(data,description);
1405 if (type == "list") {
1406 parseList(data,description);
1410 if (type == "checkbox") {
1411 parseCheckbox(data,description);
1415 if (type == "image") {
1416 parseImage(data,description);
1420 if (type == "item_image") {
1421 parseItemImage(data,description);
1425 if ((type == "button") || (type == "button_exit")) {
1426 parseButton(data,description,type);
1430 if (type == "background") {
1431 parseBackground(data,description);
1435 if (type == "textlist"){
1436 parseTextList(data,description);
1440 if (type == "dropdown"){
1441 parseDropDown(data,description);
1445 if (type == "pwdfield") {
1446 parsePwdField(data,description);
1450 if ((type == "field") || (type == "textarea")){
1451 parseField(data,description,type);
1455 if (type == "label") {
1456 parseLabel(data,description);
1460 if (type == "vertlabel") {
1461 parseVertLabel(data,description);
1465 if (type == "item_image_button") {
1466 parseItemImageButton(data,description);
1470 if ((type == "image_button") || (type == "image_button_exit")) {
1471 parseImageButton(data,description,type);
1475 if (type == "tabheader") {
1476 parseTabHeader(data,description);
1480 if (type == "box") {
1481 parseBox(data,description);
1487 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1493 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1497 //preserve listboxes
1498 for (unsigned int i = 0; i < m_listboxes.size(); i++) {
1499 std::wstring listboxname = m_listboxes[i].first.fname;
1500 gui::IGUIListBox *listbox = m_listboxes[i].second;
1502 int selection = listbox->getSelected();
1503 if (selection != -1) {
1504 mydata.listbox_selections[listboxname] = selection;
1507 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(listbox);
1509 mydata.listbox_scroll[listboxname] = scrollbar->getPos();
1514 gui::IGUIElement *focused_element = Environment->getFocus();
1515 if (focused_element && focused_element->getParent() == this) {
1516 s32 focused_id = focused_element->getID();
1517 if (focused_id > 257) {
1518 for (u32 i=0; i<m_fields.size(); i++) {
1519 if (m_fields[i].fid == focused_id) {
1520 mydata.focused_fieldname =
1531 mydata.size= v2s32(100,100);
1532 mydata.helptext_h = 15;
1533 mydata.screensize = screensize;
1535 // Base position of contents of form
1536 mydata.basepos = getBasePos();
1538 // State of basepos, 0 = not set, 1= set by formspec, 2 = set by size[] element
1539 // Used to adjust form size automatically if needed
1540 // A proceed button is added if there is no size[] element
1544 /* Convert m_init_draw_spec to m_inventorylists */
1546 m_inventorylists.clear();
1548 m_backgrounds.clear();
1549 m_itemimages.clear();
1550 m_listboxes.clear();
1551 m_checkboxes.clear();
1556 std::vector<std::string> elements = split(m_formspec_string,']');
1558 for (unsigned int i=0;i< elements.size();i++) {
1559 parseElement(&mydata,elements[i]);
1562 // If there's inventory, put the usage string at the bottom
1563 if (m_inventorylists.size())
1566 core::rect<s32> rect(0, 0, mydata.size.X-padding.X*2, mydata.helptext_h);
1567 rect = rect + v2s32((mydata.size.X/2 - mydata.rect.getWidth()/2) +5,
1568 mydata.size.Y-5-mydata.helptext_h);
1569 const wchar_t *text = wgettext("Left click: Move all items, Right click: Move single item");
1570 Environment->addStaticText(text, rect, false, true, this, 256);
1574 // If there's fields, add a Proceed button
1575 if (m_fields.size() && mydata.bp_set != 2)
1577 // if the size wasn't set by an invsize[] or size[] adjust it now to fit all the fields
1578 mydata.rect = core::rect<s32>(
1579 mydata.screensize.X/2 - 580/2,
1580 mydata.screensize.Y/2 - 300/2,
1581 mydata.screensize.X/2 + 580/2,
1582 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1584 DesiredRect = mydata.rect;
1585 recalculateAbsolutePosition(false);
1586 mydata.basepos = getBasePos();
1590 v2s32 pos = mydata.basepos;
1591 pos.Y = ((m_fields.size()+2)*60);
1593 v2s32 size = DesiredRect.getSize();
1594 mydata.rect = core::rect<s32>(size.X/2-70, pos.Y, (size.X/2-70)+140, pos.Y+30);
1595 wchar_t* text = wgettext("Proceed");
1596 Environment->addButton(mydata.rect, this, 257, text);
1603 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1604 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1605 m_tooltip_element->enableOverrideColor(true);
1606 m_tooltip_element->setBackgroundColor(video::SColor(255,110,130,60));
1607 m_tooltip_element->setDrawBackground(true);
1608 m_tooltip_element->setDrawBorder(true);
1609 m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
1610 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1611 m_tooltip_element->setWordWrap(false);
1612 //we're not parent so no autograb for this one!
1613 m_tooltip_element->grab();
1616 //set initial focus if parser didn't set it
1617 focused_element = Environment->getFocus();
1618 if (!focused_element
1619 || !isMyChild(focused_element)
1620 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
1624 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
1626 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1628 for(u32 i=0; i<m_inventorylists.size(); i++)
1630 const ListDrawSpec &s = m_inventorylists[i];
1632 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1634 s32 item_i = i + s.start_item_i;
1635 s32 x = (i%s.geom.X) * spacing.X;
1636 s32 y = (i/s.geom.X) * spacing.Y;
1638 core::rect<s32> rect = imgrect + s.pos + p0;
1639 if(rect.isPointInside(p))
1641 return ItemSpec(s.inventoryloc, s.listname, item_i);
1646 return ItemSpec(InventoryLocation(), "", -1);
1649 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
1651 video::IVideoDriver* driver = Environment->getVideoDriver();
1654 gui::IGUIFont *font = NULL;
1655 gui::IGUISkin* skin = Environment->getSkin();
1657 font = skin->getFont();
1659 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1661 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1662 <<"The inventory location "
1663 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
1667 InventoryList *ilist = inv->getList(s.listname);
1669 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1670 <<"The inventory list \""<<s.listname<<"\" @ \""
1671 <<s.inventoryloc.dump()<<"\" doesn't exist"
1676 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1678 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1680 s32 item_i = i + s.start_item_i;
1681 if(item_i >= (s32) ilist->getSize())
1683 s32 x = (i%s.geom.X) * spacing.X;
1684 s32 y = (i/s.geom.X) * spacing.Y;
1686 core::rect<s32> rect = imgrect + s.pos + p;
1689 item = ilist->getItem(item_i);
1691 bool selected = m_selected_item
1692 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
1693 && m_selected_item->listname == s.listname
1694 && m_selected_item->i == item_i;
1695 bool hovering = rect.isPointInside(m_pointer);
1699 if(hovering && m_selected_item)
1701 video::SColor bgcolor(255,192,192,192);
1702 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
1706 video::SColor bgcolor(255,128,128,128);
1707 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
1716 item.takeItem(m_selected_amount);
1720 drawItemStack(driver, font, item,
1721 rect, &AbsoluteClippingRect, m_gamedef);
1725 std::string tooltip_text = "";
1726 if(hovering && !m_selected_item)
1727 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
1728 if(tooltip_text != "")
1730 m_tooltip_element->setVisible(true);
1731 this->bringToFront(m_tooltip_element);
1732 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
1733 s32 tooltip_x = m_pointer.X + 15;
1734 s32 tooltip_y = m_pointer.Y + 15;
1735 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1736 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1737 m_tooltip_element->setRelativePosition(core::rect<s32>(
1738 core::position2d<s32>(tooltip_x, tooltip_y),
1739 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1745 void GUIFormSpecMenu::drawSelectedItem()
1747 if(!m_selected_item)
1750 video::IVideoDriver* driver = Environment->getVideoDriver();
1753 gui::IGUIFont *font = NULL;
1754 gui::IGUISkin* skin = Environment->getSkin();
1756 font = skin->getFont();
1758 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
1760 InventoryList *list = inv->getList(m_selected_item->listname);
1762 ItemStack stack = list->getItem(m_selected_item->i);
1763 stack.count = m_selected_amount;
1765 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1766 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
1767 drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
1770 void GUIFormSpecMenu::drawMenu()
1773 std::string newform = m_form_src->getForm();
1774 if(newform != m_formspec_string){
1775 m_formspec_string = newform;
1776 regenerateGui(m_screensize_old);
1780 m_pointer = m_device->getCursorControl()->getPosition();
1782 updateSelectedItem();
1784 gui::IGUISkin* skin = Environment->getSkin();
1787 video::IVideoDriver* driver = Environment->getVideoDriver();
1789 video::SColor bgcolor(140,0,0,0);
1790 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
1792 m_tooltip_element->setVisible(false);
1797 for(u32 i=0; i<m_backgrounds.size(); i++)
1799 const ImageDrawSpec &spec = m_backgrounds[i];
1800 video::ITexture *texture = 0;
1803 texture = m_gamedef->tsrc()->getTexture(spec.name);
1806 texture = driver->getTexture(spec.name.c_str());
1807 m_Textures.push_back(texture);
1811 // Image size on screen
1812 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1813 // Image rectangle on screen
1814 core::rect<s32> rect = imgrect + spec.pos;
1815 const video::SColor color(255,255,255,255);
1816 const video::SColor colors[] = {color,color,color,color};
1817 driver->draw2DImage(texture, rect,
1818 core::rect<s32>(core::position2d<s32>(0,0),
1819 core::dimension2di(texture->getOriginalSize())),
1820 NULL/*&AbsoluteClippingRect*/, colors, true);
1823 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
1824 errorstream << "\t" << spec.name << std::endl;
1831 for(u32 i=0; i<m_boxes.size(); i++)
1833 const BoxDrawSpec &spec = m_boxes[i];
1835 irr::video::SColor todraw = spec.color;
1837 todraw.setAlpha(140);
1839 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
1840 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
1842 driver->draw2DRectangle(todraw, rect, 0);
1847 for(u32 i=0; i<m_images.size(); i++)
1849 const ImageDrawSpec &spec = m_images[i];
1850 video::ITexture *texture = 0;
1853 texture = m_gamedef->tsrc()->getTexture(spec.name);
1856 texture = driver->getTexture(spec.name.c_str());
1857 m_Textures.push_back(texture);
1860 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
1861 // Image size on screen
1862 core::rect<s32> imgrect;
1865 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
1868 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
1870 // Image rectangle on screen
1871 core::rect<s32> rect = imgrect + spec.pos;
1872 const video::SColor color(255,255,255,255);
1873 const video::SColor colors[] = {color,color,color,color};
1874 driver->draw2DImage(texture, rect,
1875 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
1876 NULL/*&AbsoluteClippingRect*/, colors, true);
1879 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
1880 errorstream << "\t" << spec.name << std::endl;
1887 for(u32 i=0; i<m_itemimages.size(); i++)
1892 const ImageDrawSpec &spec = m_itemimages[i];
1893 IItemDefManager *idef = m_gamedef->idef();
1895 item.deSerialize(spec.name, idef);
1896 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1897 // Image size on screen
1898 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1899 // Image rectangle on screen
1900 core::rect<s32> rect = imgrect + spec.pos;
1901 const video::SColor color(255,255,255,255);
1902 const video::SColor colors[] = {color,color,color,color};
1903 driver->draw2DImage(texture, rect,
1904 core::rect<s32>(core::position2d<s32>(0,0),
1905 core::dimension2di(texture->getOriginalSize())),
1906 NULL/*&AbsoluteClippingRect*/, colors, true);
1911 Phase 0: Item slot rectangles
1912 Phase 1: Item images; prepare tooltip
1913 If backgrounds used, do not draw Item slot rectangles
1916 if (m_backgrounds.size() > 0) start_phase=1;
1917 for(int phase=start_phase; phase<=1; phase++)
1918 for(u32 i=0; i<m_inventorylists.size(); i++)
1920 drawList(m_inventorylists[i], phase);
1926 gui::IGUIElement::draw();
1929 Draw fields/buttons tooltips
1931 for(u32 i=0; i<m_fields.size(); i++)
1933 const FieldSpec &spec = m_fields[i];
1934 if (spec.tooltip != "")
1936 core::rect<s32> rect = spec.rect;
1937 if (rect.isPointInside(m_pointer))
1939 m_tooltip_element->setVisible(true);
1940 this->bringToFront(m_tooltip_element);
1941 m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
1942 s32 tooltip_x = m_pointer.X + 15;
1943 s32 tooltip_y = m_pointer.Y + 15;
1944 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1945 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1946 m_tooltip_element->setRelativePosition(core::rect<s32>(
1947 core::position2d<s32>(tooltip_x, tooltip_y),
1948 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1954 Draw dragged item stack
1959 void GUIFormSpecMenu::updateSelectedItem()
1961 // If the selected stack has become empty for some reason, deselect it.
1962 // If the selected stack has become inaccessible, deselect it.
1963 // If the selected stack has become smaller, adjust m_selected_amount.
1964 ItemStack selected = verifySelectedItem();
1966 // WARNING: BLACK MAGIC
1967 // See if there is a stack suited for our current guess.
1968 // If such stack does not exist, clear the guess.
1969 if(m_selected_content_guess.name != "" &&
1970 selected.name == m_selected_content_guess.name &&
1971 selected.count == m_selected_content_guess.count){
1972 // Selected item fits the guess. Skip the black magic.
1974 else if(m_selected_content_guess.name != ""){
1976 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
1977 const ListDrawSpec &s = m_inventorylists[i];
1978 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1981 InventoryList *list = inv->getList(s.listname);
1984 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
1985 u32 item_i = i + s.start_item_i;
1986 if(item_i >= list->getSize())
1988 ItemStack stack = list->getItem(item_i);
1989 if(stack.name == m_selected_content_guess.name &&
1990 stack.count == m_selected_content_guess.count){
1992 infostream<<"Client: Changing selected content guess to "
1993 <<s.inventoryloc.dump()<<" "<<s.listname
1994 <<" "<<item_i<<std::endl;
1995 delete m_selected_item;
1996 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
1997 m_selected_amount = stack.count;
2002 infostream<<"Client: Discarding selected content guess: "
2003 <<m_selected_content_guess.getItemString()<<std::endl;
2004 m_selected_content_guess.name = "";
2008 // If craftresult is nonempty and nothing else is selected, select it now.
2009 if(!m_selected_item)
2011 for(u32 i=0; i<m_inventorylists.size(); i++)
2013 const ListDrawSpec &s = m_inventorylists[i];
2014 if(s.listname == "craftpreview")
2016 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2017 InventoryList *list = inv->getList("craftresult");
2018 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2020 m_selected_item = new ItemSpec;
2021 m_selected_item->inventoryloc = s.inventoryloc;
2022 m_selected_item->listname = "craftresult";
2023 m_selected_item->i = 0;
2024 m_selected_amount = 0;
2025 m_selected_dragging = false;
2032 // If craftresult is selected, keep the whole stack selected
2033 if(m_selected_item && m_selected_item->listname == "craftresult")
2035 m_selected_amount = verifySelectedItem().count;
2039 ItemStack GUIFormSpecMenu::verifySelectedItem()
2041 // If the selected stack has become empty for some reason, deselect it.
2042 // If the selected stack has become inaccessible, deselect it.
2043 // If the selected stack has become smaller, adjust m_selected_amount.
2044 // Return the selected stack.
2048 if(m_selected_item->isValid())
2050 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2053 InventoryList *list = inv->getList(m_selected_item->listname);
2054 if(list && (u32) m_selected_item->i < list->getSize())
2056 ItemStack stack = list->getItem(m_selected_item->i);
2057 if(m_selected_amount > stack.count)
2058 m_selected_amount = stack.count;
2065 // selection was not valid
2066 delete m_selected_item;
2067 m_selected_item = NULL;
2068 m_selected_amount = 0;
2069 m_selected_dragging = false;
2074 void GUIFormSpecMenu::acceptInput()
2078 std::map<std::string, std::string> fields;
2080 if (current_keys_pending.key_down) {
2081 fields["key_down"] = "true";
2082 current_keys_pending.key_down = false;
2085 if (current_keys_pending.key_up) {
2086 fields["key_up"] = "true";
2087 current_keys_pending.key_up = false;
2090 if (current_keys_pending.key_enter) {
2091 fields["key_enter"] = "true";
2092 current_keys_pending.key_enter = false;
2095 if (current_keys_pending.key_escape) {
2096 fields["key_escape"] = "true";
2097 current_keys_pending.key_escape = false;
2100 for(u32 i=0; i<m_fields.size(); i++)
2102 const FieldSpec &s = m_fields[i];
2105 if(s.ftype == f_Button)
2107 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(s.flabel.c_str());
2109 else if(s.ftype == f_ListBox) {
2110 std::stringstream ss;
2112 if (m_listbox_doubleclick) {
2118 ss << (getListboxIndex(wide_to_narrow(s.fname.c_str()))+1);
2119 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2121 else if(s.ftype == f_DropDown) {
2122 // no dynamic cast possible due to some distributions shipped
2123 // without rtti support in irrlicht
2124 IGUIElement * element = getElementFromId(s.fid);
2125 gui::IGUIComboBox *e = NULL;
2126 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2127 e = static_cast<gui::IGUIComboBox*>(element);
2129 fields[wide_to_narrow(s.fname.c_str())] =
2130 wide_to_narrow(e->getItem(e->getSelected()));
2132 else if (s.ftype == f_TabHeader) {
2133 // no dynamic cast possible due to some distributions shipped
2134 // without rtti support in irrlicht
2135 IGUIElement * element = getElementFromId(s.fid);
2136 gui::IGUITabControl *e = NULL;
2137 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2138 e = static_cast<gui::IGUITabControl*>(element);
2142 std::stringstream ss;
2143 ss << (e->getActiveTab() +1);
2144 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2147 else if (s.ftype == f_CheckBox) {
2148 // no dynamic cast possible due to some distributions shipped
2149 // without rtti support in irrlicht
2150 IGUIElement * element = getElementFromId(s.fid);
2151 gui::IGUICheckBox *e = NULL;
2152 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2153 e = static_cast<gui::IGUICheckBox*>(element);
2158 fields[wide_to_narrow(s.fname.c_str())] = "true";
2160 fields[wide_to_narrow(s.fname.c_str())] = "false";
2165 IGUIElement* e = getElementFromId(s.fid);
2168 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(e->getText());
2174 m_text_dst->gotText(fields);
2178 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2180 // Fix Esc/Return key being eaten by checkboxen and listboxen
2181 if(event.EventType==EET_KEY_INPUT_EVENT)
2183 KeyPress kp(event.KeyInput);
2184 if (kp == EscapeKey || kp == getKeySetting("keymap_inventory")
2185 || event.KeyInput.Key==KEY_RETURN)
2187 gui::IGUIElement *focused = Environment->getFocus();
2188 if (focused && isMyChild(focused) &&
2189 (focused->getType() == gui::EGUIET_LIST_BOX ||
2190 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2196 // Mouse wheel events: send to hovered element instead of focused
2197 if(event.EventType==EET_MOUSE_INPUT_EVENT
2198 && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
2200 s32 x = event.MouseInput.X;
2201 s32 y = event.MouseInput.Y;
2202 gui::IGUIElement *hovered =
2203 Environment->getRootGUIElement()->getElementFromPoint(
2204 core::position2d<s32>(x, y));
2205 if (hovered && isMyChild(hovered)) {
2206 hovered->OnEvent(event);
2213 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2215 if(event.EventType==EET_KEY_INPUT_EVENT)
2217 KeyPress kp(event.KeyInput);
2218 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
2219 kp == getKeySetting("keymap_inventory")))
2224 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
2227 if (event.KeyInput.PressedDown &&
2228 (event.KeyInput.Key==KEY_RETURN ||
2229 event.KeyInput.Key==KEY_UP ||
2230 event.KeyInput.Key==KEY_DOWN)
2234 switch (event.KeyInput.Key) {
2241 current_keys_pending.key_enter = true;
2244 current_keys_pending.key_up = true;
2247 current_keys_pending.key_down = true;
2251 //can't happen at all!
2252 assert("reached a source line that can't ever been reached" == 0);
2260 if(event.EventType==EET_MOUSE_INPUT_EVENT
2261 && event.MouseInput.Event != EMIE_MOUSE_MOVED)
2263 // Mouse event other than movement
2265 // Get selected item and hovered/clicked item (s)
2267 updateSelectedItem();
2268 ItemSpec s = getItemAtPos(m_pointer);
2270 Inventory *inv_selected = NULL;
2271 Inventory *inv_s = NULL;
2275 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
2276 assert(inv_selected);
2277 assert(inv_selected->getList(m_selected_item->listname) != NULL);
2284 inv_s = m_invmgr->getInventory(s.inventoryloc);
2287 errorstream<<"InventoryMenu: The selected inventory location "
2288 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2290 s.i = -1; // make it invalid again
2294 InventoryList *list = inv_s->getList(s.listname);
2296 verbosestream<<"InventoryMenu: The selected inventory list \""
2297 <<s.listname<<"\" does not exist"<<std::endl;
2298 s.i = -1; // make it invalid again
2302 if((u32)s.i >= list->getSize()){
2303 infostream<<"InventoryMenu: The selected inventory list \""
2304 <<s.listname<<"\" is too small (i="<<s.i<<", size="
2305 <<list->getSize()<<")"<<std::endl;
2306 s.i = -1; // make it invalid again
2310 s_count = list->getItem(s.i).count;
2313 bool identical = (m_selected_item != NULL) && s.isValid() &&
2314 (inv_selected == inv_s) &&
2315 (m_selected_item->listname == s.listname) &&
2316 (m_selected_item->i == s.i);
2318 // buttons: 0 = left, 1 = right, 2 = middle
2319 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
2322 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
2323 { button = 0; updown = 0; }
2324 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
2325 { button = 1; updown = 0; }
2326 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
2327 { button = 2; updown = 0; }
2328 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
2329 { button = 0; updown = 1; }
2330 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
2331 { button = 1; updown = 1; }
2332 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
2333 { button = 2; updown = 1; }
2335 // Set this number to a positive value to generate a move action
2336 // from m_selected_item to s.
2337 u32 move_amount = 0;
2339 // Set this number to a positive value to generate a drop action
2340 // from m_selected_item.
2341 u32 drop_amount = 0;
2343 // Set this number to a positive value to generate a craft action at s.
2344 u32 craft_amount = 0;
2348 // Some mouse button has been pressed
2350 //infostream<<"Mouse button "<<button<<" pressed at p=("
2351 // <<p.X<<","<<p.Y<<")"<<std::endl;
2353 m_selected_dragging = false;
2355 if(s.isValid() && s.listname == "craftpreview")
2357 // Craft preview has been clicked: craft
2358 craft_amount = (button == 2 ? 10 : 1);
2360 else if(m_selected_item == NULL)
2364 // Non-empty stack has been clicked: select it
2365 m_selected_item = new ItemSpec(s);
2367 if(button == 1) // right
2368 m_selected_amount = (s_count + 1) / 2;
2369 else if(button == 2) // middle
2370 m_selected_amount = MYMIN(s_count, 10);
2372 m_selected_amount = s_count;
2374 m_selected_dragging = true;
2377 else // m_selected_item != NULL
2379 assert(m_selected_amount >= 1);
2383 // Clicked a slot: move
2384 if(button == 1) // right
2386 else if(button == 2) // middle
2387 move_amount = MYMIN(m_selected_amount, 10);
2389 move_amount = m_selected_amount;
2393 if(move_amount >= m_selected_amount)
2394 m_selected_amount = 0;
2396 m_selected_amount -= move_amount;
2400 else if(getAbsoluteClippingRect().isPointInside(m_pointer))
2402 // Clicked somewhere else: deselect
2403 m_selected_amount = 0;
2407 // Clicked outside of the window: drop
2408 if(button == 1) // right
2410 else if(button == 2) // middle
2411 drop_amount = MYMIN(m_selected_amount, 10);
2413 drop_amount = m_selected_amount;
2417 else if(updown == 1)
2419 // Some mouse button has been released
2421 //infostream<<"Mouse button "<<button<<" released at p=("
2422 // <<p.X<<","<<p.Y<<")"<<std::endl;
2424 if(m_selected_item != NULL && m_selected_dragging && s.isValid())
2428 // Dragged to different slot: move all selected
2429 move_amount = m_selected_amount;
2432 else if(m_selected_item != NULL && m_selected_dragging &&
2433 !(getAbsoluteClippingRect().isPointInside(m_pointer)))
2435 // Dragged outside of window: drop all selected
2436 drop_amount = m_selected_amount;
2439 m_selected_dragging = false;
2442 // Possibly send inventory action to server
2445 // Send IACTION_MOVE
2447 assert(m_selected_item && m_selected_item->isValid());
2448 assert(s.isValid());
2450 assert(inv_selected && inv_s);
2451 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2452 InventoryList *list_to = inv_s->getList(s.listname);
2453 assert(list_from && list_to);
2454 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2455 ItemStack stack_to = list_to->getItem(s.i);
2457 // Check how many items can be moved
2458 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
2459 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
2460 // If source stack cannot be added to destination stack at all,
2462 if(leftover.count == stack_from.count && leftover.name == stack_from.name)
2464 m_selected_amount = stack_to.count;
2465 // In case the server doesn't directly swap them but instead
2466 // moves stack_to somewhere else, set this
2467 m_selected_content_guess = stack_to;
2468 m_selected_content_guess_inventory = s.inventoryloc;
2470 // Source stack goes fully into destination stack
2471 else if(leftover.empty())
2473 m_selected_amount -= move_amount;
2474 m_selected_content_guess = ItemStack(); // Clear
2476 // Source stack goes partly into destination stack
2479 move_amount -= leftover.count;
2480 m_selected_amount -= move_amount;
2481 m_selected_content_guess = ItemStack(); // Clear
2484 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
2485 IMoveAction *a = new IMoveAction();
2486 a->count = move_amount;
2487 a->from_inv = m_selected_item->inventoryloc;
2488 a->from_list = m_selected_item->listname;
2489 a->from_i = m_selected_item->i;
2490 a->to_inv = s.inventoryloc;
2491 a->to_list = s.listname;
2493 m_invmgr->inventoryAction(a);
2495 else if(drop_amount > 0)
2497 m_selected_content_guess = ItemStack(); // Clear
2499 // Send IACTION_DROP
2501 assert(m_selected_item && m_selected_item->isValid());
2502 assert(inv_selected);
2503 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2505 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2507 // Check how many items can be dropped
2508 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
2509 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
2510 m_selected_amount -= drop_amount;
2512 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
2513 IDropAction *a = new IDropAction();
2514 a->count = drop_amount;
2515 a->from_inv = m_selected_item->inventoryloc;
2516 a->from_list = m_selected_item->listname;
2517 a->from_i = m_selected_item->i;
2518 m_invmgr->inventoryAction(a);
2520 else if(craft_amount > 0)
2522 m_selected_content_guess = ItemStack(); // Clear
2524 // Send IACTION_CRAFT
2526 assert(s.isValid());
2529 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
2530 ICraftAction *a = new ICraftAction();
2531 a->count = craft_amount;
2532 a->craft_inv = s.inventoryloc;
2533 m_invmgr->inventoryAction(a);
2536 // If m_selected_amount has been decreased to zero, deselect
2537 if(m_selected_amount == 0)
2539 delete m_selected_item;
2540 m_selected_item = NULL;
2541 m_selected_amount = 0;
2542 m_selected_dragging = false;
2543 m_selected_content_guess = ItemStack();
2546 if(event.EventType==EET_GUI_EVENT)
2549 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
2552 // find the element that was clicked
2553 for(u32 i=0; i<m_fields.size(); i++)
2555 FieldSpec &s = m_fields[i];
2556 // if its a button, set the send field so
2557 // lua knows which button was pressed
2558 if ((s.ftype == f_TabHeader) && (s.fid == event.GUIEvent.Caller->getID()))
2567 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
2570 if(!canTakeFocus(event.GUIEvent.Element))
2572 infostream<<"GUIFormSpecMenu: Not allowing focus change."
2574 // Returning true disables focus change
2578 if((event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) ||
2579 (event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED))
2581 unsigned int btn_id = event.GUIEvent.Caller->getID();
2583 if (btn_id == 257) {
2588 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2589 // quitMenu deallocates menu
2593 // find the element that was clicked
2594 for(u32 i=0; i<m_fields.size(); i++)
2596 FieldSpec &s = m_fields[i];
2597 // if its a button, set the send field so
2598 // lua knows which button was pressed
2599 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
2600 (s.fid == event.GUIEvent.Caller->getID()))
2608 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2617 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
2619 if(event.GUIEvent.Caller->getID() > 257)
2627 current_keys_pending.key_enter = true;
2630 // quitMenu deallocates menu
2635 if((event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN) ||
2636 (event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED))
2638 int current_id = event.GUIEvent.Caller->getID();
2639 if(current_id > 257)
2641 // find the element that was clicked
2642 for(u32 i=0; i<m_fields.size(); i++)
2644 FieldSpec &s = m_fields[i];
2645 // if its a listbox, set the send field so
2646 // lua knows which listbox was changed
2647 // checkListboxClick() is black magic
2648 // for properly handling double clicks
2649 if ((s.ftype == f_ListBox) && (s.fid == current_id)
2650 && checkListboxClick(s.fname,
2651 event.GUIEvent.EventType))
2663 return Parent ? Parent->OnEvent(event) : false;
2666 bool GUIFormSpecMenu::parseColor(std::string color, irr::video::SColor& outcolor) {
2667 outcolor = irr::video::SColor(0,0,0,0);
2669 if (!string_allowed(color, "0123456789abcdefABCDEF"))
2673 std::istringstream iss(color);
2674 iss >> std::hex >> color_value;
2676 outcolor = irr::video::SColor(color_value);
2678 outcolor.setAlpha(255);