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
44 #include "util/string.h"
45 #include "util/numeric.h"
50 #define MY_CHECKPOS(a,b) \
51 if (v_pos.size() != 2) { \
52 errorstream<< "Invalid pos for element " << a << "specified: \"" \
53 << parts[b] << "\"" << std::endl; \
57 #define MY_CHECKGEOM(a,b) \
58 if (v_geom.size() != 2) { \
59 errorstream<< "Invalid pos for element " << a << "specified: \"" \
60 << parts[b] << "\"" << std::endl; \
69 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
70 gui::IGUIElement* parent, s32 id,
71 IMenuManager *menumgr,
72 InventoryManager *invmgr,
74 ISimpleTextureSource *tsrc
76 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
83 m_selected_item(NULL),
85 m_selected_dragging(false),
86 m_listbox_click_fname(),
87 m_listbox_click_index(-1),
88 m_listbox_click_time(0),
89 m_listbox_doubleclick(false),
90 m_tooltip_element(NULL),
94 current_keys_pending.key_down = false;
95 current_keys_pending.key_up = false;
96 current_keys_pending.key_enter = false;
97 current_keys_pending.key_escape = false;
101 GUIFormSpecMenu::~GUIFormSpecMenu()
105 delete m_selected_item;
110 void GUIFormSpecMenu::removeChildren()
112 const core::list<gui::IGUIElement*> &children = getChildren();
113 core::list<gui::IGUIElement*> children_copy;
114 for(core::list<gui::IGUIElement*>::ConstIterator
115 i = children.begin(); i != children.end(); i++)
117 children_copy.push_back(*i);
119 for(core::list<gui::IGUIElement*>::Iterator
120 i = children_copy.begin();
121 i != children_copy.end(); i++)
126 gui::IGUIElement *e = getElementFromId(256);
131 if(m_tooltip_element)
133 m_tooltip_element->remove();
134 m_tooltip_element->drop();
135 m_tooltip_element = NULL;
140 void GUIFormSpecMenu::setInitialFocus()
142 // Set initial focus according to following order of precedence:
143 // 1. first empty editbox
147 // 5. first focusable (not statictext, not tabheader)
148 // 6. first child element
150 core::list<gui::IGUIElement*> children = getChildren();
152 // in case "children" contains any NULL elements, remove them
153 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
154 it != children.end();) {
158 it = children.erase(it);
161 // 1. first empty editbox
162 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
163 it != children.end(); ++it) {
164 if ((*it)->getType() == gui::EGUIET_EDIT_BOX
165 && (*it)->getText()[0] == 0) {
166 Environment->setFocus(*it);
172 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
173 it != children.end(); ++it) {
174 if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
175 Environment->setFocus(*it);
181 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
182 it != children.end(); ++it) {
183 if ((*it)->getType() == gui::EGUIET_LIST_BOX) {
184 Environment->setFocus(*it);
190 for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
191 it != children.end(); --it) {
192 if ((*it)->getType() == gui::EGUIET_BUTTON) {
193 Environment->setFocus(*it);
198 // 5. first focusable (not statictext, not tabheader)
199 for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
200 it != children.end(); ++it) {
201 if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
202 (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
203 Environment->setFocus(*it);
208 // 6. first child element
209 if (children.empty())
210 Environment->setFocus(this);
212 Environment->setFocus(*(children.begin()));
215 int GUIFormSpecMenu::getListboxIndex(std::string listboxname) {
217 std::wstring wlistboxname = narrow_to_wide(listboxname.c_str());
219 for(unsigned int i=0; i < m_listboxes.size(); i++) {
221 std::wstring name(m_listboxes[i].first.fname.c_str());
222 if ( name == wlistboxname) {
223 return m_listboxes[i].second->getSelected();
229 bool GUIFormSpecMenu::checkListboxClick(std::wstring wlistboxname,
232 // WARNING: BLACK IRRLICHT MAGIC
233 // Used to fix Irrlicht's subpar reporting of single clicks and double
234 // clicks in listboxes (gui::EGET_LISTBOX_CHANGED,
235 // gui::EGET_LISTBOX_SELECTED_AGAIN):
236 // 1. IGUIListBox::setSelected() is counted as a click.
237 // Including the initial setSelected() done by parseTextList().
238 // 2. Clicking on a the selected item and then dragging for less
239 // than 500ms is counted as a doubleclick, no matter when the
240 // item was previously selected (e.g. more than 500ms ago)
242 // So when Irrlicht reports a doubleclick, we need to check
243 // for ourselves if really was a doubleclick. Or just a fake.
245 for(unsigned int i=0; i < m_listboxes.size(); i++) {
246 std::wstring name(m_listboxes[i].first.fname.c_str());
247 int selected = m_listboxes[i].second->getSelected();
248 if (name == wlistboxname && selected >= 0) {
249 u32 now = getTimeMs();
251 (eventtype == gui::EGET_LISTBOX_SELECTED_AGAIN)
252 && (name == m_listbox_click_fname)
253 && (selected == m_listbox_click_index)
254 && (m_listbox_click_time >= now - 500);
255 m_listbox_click_fname = name;
256 m_listbox_click_index = selected;
257 m_listbox_click_time = now;
258 m_listbox_doubleclick = doubleclick;
265 gui::IGUIScrollBar* GUIFormSpecMenu::getListboxScrollbar(
266 gui::IGUIListBox *listbox)
268 // WARNING: BLACK IRRLICHT MAGIC
269 // Ordinarily, due to how formspecs work (recreating the entire GUI
270 // when something changes), when you select an item in a textlist
271 // with more items than fit in the visible area, the newly selected
272 // item is scrolled to the bottom of the visible area. This is
273 // annoying and breaks GUI designs that use double clicks.
275 // This function helps fixing this problem by giving direct access
276 // to a listbox's scrollbar. This works because CGUIListBox doesn't
277 // cache the scrollbar position anywhere.
279 // If this stops working in a future irrlicht version, consider
280 // maintaining a local copy of irr::gui::CGUIListBox, possibly also
281 // fixing the other reasons why black irrlicht magic is needed.
283 core::list<gui::IGUIElement*> children = listbox->getChildren();
284 for(core::list<gui::IGUIElement*>::Iterator it = children.begin();
285 it != children.end(); ++it) {
286 gui::IGUIElement* child = *it;
287 if (child && child->getType() == gui::EGUIET_SCROLL_BAR) {
288 return static_cast<gui::IGUIScrollBar*>(child);
292 verbosestream<<"getListboxScrollbar: WARNING: "
293 <<"listbox has no scrollbar"<<std::endl;
297 std::vector<std::string> split(const std::string &s, char delim) {
298 std::vector<std::string> tokens;
300 std::string current = "";
301 bool last_was_escape = false;
302 for(unsigned int i=0; i < s.size(); i++) {
303 if (last_was_escape) {
305 current += s.c_str()[i];
306 last_was_escape = false;
309 if (s.c_str()[i] == delim) {
310 tokens.push_back(current);
312 last_was_escape = false;
314 else if (s.c_str()[i] == '\\'){
315 last_was_escape = true;
318 current += s.c_str()[i];
319 last_was_escape = false;
324 tokens.push_back(current);
329 void GUIFormSpecMenu::parseSize(parserData* data,std::string element) {
330 std::vector<std::string> parts = split(element,',');
332 if (parts.size() == 2) {
335 if (parts[1].find(';') != std::string::npos)
336 parts[1] = parts[1].substr(0,parts[1].find(';'));
338 invsize.X = stof(parts[0]);
339 invsize.Y = stof(parts[1]);
342 v2u32 current_screensize = m_device->getVideoDriver()->getScreenSize();
343 v2u32 delta = current_screensize - m_lockscreensize;
345 if (current_screensize.Y > m_lockscreensize.Y)
350 if (current_screensize.X > m_lockscreensize.X)
355 offset = v2s32(delta.X,delta.Y);
357 data->screensize = m_lockscreensize;
363 padding = v2s32(data->screensize.Y/40, data->screensize.Y/40);
364 spacing = v2s32(data->screensize.Y/12, data->screensize.Y/13);
365 imgsize = v2s32(data->screensize.Y/15, data->screensize.Y/15);
367 padding.X*2+spacing.X*(invsize.X-1.0)+imgsize.X,
368 padding.Y*2+spacing.Y*(invsize.Y-1.0)+imgsize.Y + (data->helptext_h-5)
370 data->rect = core::rect<s32>(
371 data->screensize.X/2 - data->size.X/2 + offset.X,
372 data->screensize.Y/2 - data->size.Y/2 + offset.Y,
373 data->screensize.X/2 + data->size.X/2 + offset.X,
374 data->screensize.Y/2 + data->size.Y/2 + offset.Y
377 DesiredRect = data->rect;
378 recalculateAbsolutePosition(false);
379 data->basepos = getBasePos();
383 errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
386 void GUIFormSpecMenu::parseList(parserData* data,std::string element) {
388 if (m_gamedef == 0) {
389 errorstream<<"WARNING: invalid use of 'list' with m_gamedef==0"<<std::endl;
393 std::vector<std::string> parts = split(element,';');
395 if ((parts.size() == 4) || (parts.size() == 5)) {
396 std::string location = parts[0];
397 std::string listname = parts[1];
398 std::vector<std::string> v_pos = split(parts[2],',');
399 std::vector<std::string> v_geom = split(parts[3],',');
400 std::string startindex = "";
401 if (parts.size() == 5)
402 startindex = parts[4];
404 MY_CHECKPOS("list",2);
405 MY_CHECKGEOM("list",3);
407 InventoryLocation loc;
409 if(location == "context" || location == "current_name")
410 loc = m_current_inventory_location;
412 loc.deSerialize(location);
414 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
415 pos.X += stof(v_pos[0]) * (float)spacing.X;
416 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
419 geom.X = stoi(v_geom[0]);
420 geom.Y = stoi(v_geom[1]);
424 start_i = stoi(startindex);
426 if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
427 errorstream<< "Invalid list element: '" << element << "'" << std::endl;
431 if(data->bp_set != 2)
432 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
433 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
436 errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
439 void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element) {
440 std::vector<std::string> parts = split(element,';');
442 if ((parts.size() == 3) || (parts.size() == 4)) {
443 std::vector<std::string> v_pos = split(parts[0],',');
444 std::string name = parts[1];
445 std::string label = parts[2];
446 std::string selected = "";
448 if (parts.size() == 4)
451 MY_CHECKPOS("checkbox",0);
454 pos.X += stof(v_pos[0]) * (float) spacing.X;
455 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
457 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
459 bool fselected = false;
461 if (selected == "true")
464 std::wstring wlabel = narrow_to_wide(label.c_str());
466 FieldSpec spec = FieldSpec(
467 narrow_to_wide(name.c_str()),
473 spec.ftype = f_CheckBox;
474 spec.flabel = wlabel; //Needed for displaying text on MSVC
475 gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
476 spec.fid, spec.flabel.c_str());
478 if (spec.fname == data->focused_fieldname) {
479 Environment->setFocus(e);
482 m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
483 m_fields.push_back(spec);
486 errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
489 void GUIFormSpecMenu::parseImage(parserData* data,std::string element) {
490 std::vector<std::string> parts = split(element,';');
492 if (parts.size() == 3) {
493 std::vector<std::string> v_pos = split(parts[0],',');
494 std::vector<std::string> v_geom = split(parts[1],',');
495 std::string name = unescape_string(parts[2]);
497 MY_CHECKPOS("image",0);
498 MY_CHECKGEOM("image",1);
500 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
501 pos.X += stof(v_pos[0]) * (float) spacing.X;
502 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
505 geom.X = stof(v_geom[0]) * (float)imgsize.X;
506 geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
508 if(data->bp_set != 2)
509 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
510 m_images.push_back(ImageDrawSpec(name, pos, geom));
514 if (parts.size() == 2) {
515 std::vector<std::string> v_pos = split(parts[0],',');
516 std::string name = unescape_string(parts[1]);
518 MY_CHECKPOS("image",0);
520 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
521 pos.X += stof(v_pos[0]) * (float) spacing.X;
522 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
524 if(data->bp_set != 2)
525 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
526 m_images.push_back(ImageDrawSpec(name, pos));
529 errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
532 void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element) {
533 std::vector<std::string> parts = split(element,';');
535 if (parts.size() == 3) {
536 std::vector<std::string> v_pos = split(parts[0],',');
537 std::vector<std::string> v_geom = split(parts[1],',');
538 std::string name = parts[2];
540 MY_CHECKPOS("itemimage",0);
541 MY_CHECKGEOM("itemimage",1);
543 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
544 pos.X += stof(v_pos[0]) * (float) spacing.X;
545 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
548 geom.X = stoi(v_geom[0]) * (float)imgsize.X;
549 geom.Y = stoi(v_geom[1]) * (float)imgsize.Y;
551 if(data->bp_set != 2)
552 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
553 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
556 errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
559 void GUIFormSpecMenu::parseButton(parserData* data,std::string element,std::string type) {
560 std::vector<std::string> parts = split(element,';');
562 if (parts.size() == 4) {
563 std::vector<std::string> v_pos = split(parts[0],',');
564 std::vector<std::string> v_geom = split(parts[1],',');
565 std::string name = parts[2];
566 std::string label = parts[3];
568 MY_CHECKPOS("button",0);
569 MY_CHECKGEOM("button",1);
572 pos.X += stof(v_pos[0]) * (float)spacing.X;
573 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
576 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
577 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
579 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y-15, pos.X+geom.X, pos.Y+15);
581 if(data->bp_set != 2)
582 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
584 label = unescape_string(label);
586 std::wstring wlabel = narrow_to_wide(label.c_str());
588 FieldSpec spec = FieldSpec(
589 narrow_to_wide(name.c_str()),
594 spec.ftype = f_Button;
595 if(type == "button_exit")
598 gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
599 spec.flabel.c_str());
601 if (spec.fname == data->focused_fieldname) {
602 Environment->setFocus(e);
605 m_fields.push_back(spec);
608 errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
611 void GUIFormSpecMenu::parseBackground(parserData* data,std::string element) {
612 std::vector<std::string> parts = split(element,';');
614 if ((parts.size() == 3) || (parts.size() == 4)) {
615 std::vector<std::string> v_pos = split(parts[0],',');
616 std::vector<std::string> v_geom = split(parts[1],',');
617 std::string name = unescape_string(parts[2]);
619 MY_CHECKPOS("background",0);
620 MY_CHECKGEOM("background",1);
622 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
623 pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
624 pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
627 geom.X = stof(v_geom[0]) * (float)spacing.X;
628 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
630 if (parts.size() == 4) {
631 m_clipbackground = is_yes(parts[3]);
632 if (m_clipbackground) {
633 pos.X = stoi(v_pos[0]); //acts as offset
634 pos.Y = stoi(v_pos[1]); //acts as offset
638 if(data->bp_set != 2)
639 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
640 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
643 errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
646 void GUIFormSpecMenu::parseTextList(parserData* data,std::string element) {
647 std::vector<std::string> parts = split(element,';');
649 if ((parts.size() == 5) || (parts.size() == 6)) {
650 std::vector<std::string> v_pos = split(parts[0],',');
651 std::vector<std::string> v_geom = split(parts[1],',');
652 std::string name = parts[2];
653 std::vector<std::string> items = split(parts[3],',');
654 std::string str_initial_selection = "";
655 std::string str_transparent = "false";
657 if (parts.size() >= 5)
658 str_initial_selection = parts[4];
660 if (parts.size() >= 6)
661 str_transparent = parts[5];
663 MY_CHECKPOS("textlist",0);
664 MY_CHECKGEOM("textlist",1);
667 pos.X += stof(v_pos[0]) * (float)spacing.X;
668 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
671 geom.X = stof(v_geom[0]) * (float)spacing.X;
672 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
675 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
677 std::wstring fname_w = narrow_to_wide(name.c_str());
679 FieldSpec spec = FieldSpec(
686 spec.ftype = f_ListBox;
688 //now really show list
689 gui::IGUIListBox *e = Environment->addListBox(rect, this,spec.fid);
691 if (spec.fname == data->focused_fieldname) {
692 Environment->setFocus(e);
695 if (str_transparent == "false")
696 e->setDrawBackground(true);
698 for (unsigned int i=0; i < items.size(); i++) {
699 if (items[i].c_str()[0] == '#') {
700 if (items[i].c_str()[1] == '#') {
701 e->addItem(narrow_to_wide(unescape_string(items[i])).c_str() +1);
704 std::string color = items[i].substr(0,7);
706 narrow_to_wide(unescape_string(items[i]).c_str() + 7);
708 e->addItem(toadd.c_str());
710 video::SColor tmp_color;
712 if (parseColor(color, tmp_color, false))
713 e->setItemOverrideColor(i,tmp_color);
717 e->addItem(narrow_to_wide(unescape_string(items[i])).c_str());
721 if (data->listbox_selections.find(fname_w) != data->listbox_selections.end()) {
722 e->setSelected(data->listbox_selections[fname_w]);
725 if (data->listbox_scroll.find(fname_w) != data->listbox_scroll.end()) {
726 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(e);
728 scrollbar->setPos(data->listbox_scroll[fname_w]);
732 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(e);
734 scrollbar->setPos(0);
738 if ((str_initial_selection != "") &&
739 (str_initial_selection != "0"))
740 e->setSelected(stoi(str_initial_selection.c_str())-1);
742 m_listboxes.push_back(std::pair<FieldSpec,gui::IGUIListBox*>(spec,e));
743 m_fields.push_back(spec);
746 errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
750 void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element) {
751 std::vector<std::string> parts = split(element,';');
753 if (parts.size() == 5) {
754 std::vector<std::string> v_pos = split(parts[0],',');
755 std::string name = parts[2];
756 std::vector<std::string> items = split(parts[3],',');
757 std::string str_initial_selection = "";
758 str_initial_selection = parts[4];
760 MY_CHECKPOS("dropdown",0);
763 pos.X += stof(v_pos[0]) * (float)spacing.X;
764 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
766 s32 width = stof(parts[1]) * (float)spacing.Y;
768 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+width, pos.Y+30);
770 std::wstring fname_w = narrow_to_wide(name.c_str());
772 FieldSpec spec = FieldSpec(
779 spec.ftype = f_DropDown;
782 //now really show list
783 gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
785 if (spec.fname == data->focused_fieldname) {
786 Environment->setFocus(e);
789 for (unsigned int i=0; i < items.size(); i++) {
790 e->addItem(narrow_to_wide(items[i]).c_str());
793 if (str_initial_selection != "")
794 e->setSelected(stoi(str_initial_selection.c_str())-1);
796 m_fields.push_back(spec);
799 errorstream << "Invalid dropdown element(" << parts.size() << "): '"
800 << element << "'" << std::endl;
803 void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element) {
804 std::vector<std::string> parts = split(element,';');
806 if (parts.size() == 4) {
807 std::vector<std::string> v_pos = split(parts[0],',');
808 std::vector<std::string> v_geom = split(parts[1],',');
809 std::string name = parts[2];
810 std::string label = parts[3];
812 MY_CHECKPOS("pwdfield",0);
813 MY_CHECKGEOM("pwdfield",1);
816 pos.X += stof(v_pos[0]) * (float)spacing.X;
817 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
820 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
822 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
826 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
828 label = unescape_string(label);
830 std::wstring wlabel = narrow_to_wide(label.c_str());
832 FieldSpec spec = FieldSpec(
833 narrow_to_wide(name.c_str()),
840 gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
842 if (spec.fname == data->focused_fieldname) {
843 Environment->setFocus(e);
846 if (label.length() >= 1)
848 rect.UpperLeftCorner.Y -= 15;
849 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
850 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
853 e->setPasswordBox(true,L'*');
856 evt.EventType = EET_KEY_INPUT_EVENT;
857 evt.KeyInput.Key = KEY_END;
858 evt.KeyInput.Char = 0;
859 evt.KeyInput.Control = 0;
860 evt.KeyInput.Shift = 0;
861 evt.KeyInput.PressedDown = true;
863 m_fields.push_back(spec);
866 errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
869 void GUIFormSpecMenu::parseSimpleField(parserData* data,std::vector<std::string> &parts) {
870 std::string name = parts[0];
871 std::string label = parts[1];
872 std::string default_val = parts[2];
874 core::rect<s32> rect;
878 rect = core::rect<s32>(
879 data->screensize.X/2 - 580/2,
880 data->screensize.Y/2 - 300/2,
881 data->screensize.X/2 + 580/2,
882 data->screensize.Y/2 + 300/2
885 recalculateAbsolutePosition(false);
886 data->basepos = getBasePos();
889 else if(data->bp_set == 2)
890 errorstream<<"WARNING: invalid use of unpositioned \"field\" in inventory"<<std::endl;
892 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
893 pos.Y = ((m_fields.size()+2)*60);
894 v2s32 size = DesiredRect.getSize();
896 rect = core::rect<s32>(size.X/2-150, pos.Y, (size.X/2-150)+300, pos.Y+30);
900 default_val = m_form_src->resolveText(default_val);
902 default_val = unescape_string(default_val);
903 label = unescape_string(label);
905 std::wstring wlabel = narrow_to_wide(label.c_str());
907 FieldSpec spec = FieldSpec(
908 narrow_to_wide(name.c_str()),
910 narrow_to_wide(default_val.c_str()),
916 // spec field id to 0, this stops submit searching for a value that isn't there
917 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
922 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
924 if (spec.fname == data->focused_fieldname) {
925 Environment->setFocus(e);
929 evt.EventType = EET_KEY_INPUT_EVENT;
930 evt.KeyInput.Key = KEY_END;
931 evt.KeyInput.Char = 0;
932 evt.KeyInput.Control = 0;
933 evt.KeyInput.Shift = 0;
934 evt.KeyInput.PressedDown = true;
937 if (label.length() >= 1)
939 rect.UpperLeftCorner.Y -= 15;
940 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
941 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
945 m_fields.push_back(spec);
948 void GUIFormSpecMenu::parseTextArea(parserData* data,std::vector<std::string>& parts,std::string type) {
950 std::vector<std::string> v_pos = split(parts[0],',');
951 std::vector<std::string> v_geom = split(parts[1],',');
952 std::string name = parts[2];
953 std::string label = parts[3];
954 std::string default_val = parts[4];
957 MY_CHECKGEOM(type,1);
960 pos.X = stof(v_pos[0]) * (float) spacing.X;
961 pos.Y = stof(v_pos[1]) * (float) spacing.Y;
965 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
967 if (type == "textarea")
969 geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
974 pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
979 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
981 if(data->bp_set != 2)
982 errorstream<<"WARNING: invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
985 default_val = m_form_src->resolveText(default_val);
988 default_val = unescape_string(default_val);
989 label = unescape_string(label);
991 std::wstring wlabel = narrow_to_wide(label.c_str());
993 FieldSpec spec = FieldSpec(
994 narrow_to_wide(name.c_str()),
996 narrow_to_wide(default_val.c_str()),
1002 // spec field id to 0, this stops submit searching for a value that isn't there
1003 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1008 gui::IGUIEditBox *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1010 if (spec.fname == data->focused_fieldname) {
1011 Environment->setFocus(e);
1014 if (type == "textarea")
1016 e->setMultiLine(true);
1017 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1020 evt.EventType = EET_KEY_INPUT_EVENT;
1021 evt.KeyInput.Key = KEY_END;
1022 evt.KeyInput.Char = 0;
1023 evt.KeyInput.Control = 0;
1024 evt.KeyInput.Shift = 0;
1025 evt.KeyInput.PressedDown = true;
1029 if (label.length() >= 1)
1031 rect.UpperLeftCorner.Y -= 15;
1032 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 15;
1033 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1036 m_fields.push_back(spec);
1039 void GUIFormSpecMenu::parseField(parserData* data,std::string element,std::string type) {
1040 std::vector<std::string> parts = split(element,';');
1042 if (parts.size() == 3) {
1043 parseSimpleField(data,parts);
1047 if (parts.size() == 5) {
1048 parseTextArea(data,parts,type);
1051 errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
1054 void GUIFormSpecMenu::parseLabel(parserData* data,std::string element) {
1055 std::vector<std::string> parts = split(element,';');
1057 if (parts.size() == 2) {
1058 std::vector<std::string> v_pos = split(parts[0],',');
1059 std::string text = parts[1];
1061 MY_CHECKPOS("label",0);
1063 v2s32 pos = padding;
1064 pos.X += stof(v_pos[0]) * (float)spacing.X;
1065 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1067 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
1069 if(data->bp_set != 2)
1070 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1072 text = unescape_string(text);
1074 std::wstring wlabel = narrow_to_wide(text.c_str());
1076 FieldSpec spec = FieldSpec(
1082 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1083 m_fields.push_back(spec);
1086 errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
1089 void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element) {
1090 std::vector<std::string> parts = split(element,';');
1092 if (parts.size() == 2) {
1093 std::vector<std::string> v_pos = split(parts[0],',');
1094 std::wstring text = narrow_to_wide(unescape_string(parts[1]));
1096 MY_CHECKPOS("vertlabel",1);
1098 v2s32 pos = padding;
1099 pos.X += stof(v_pos[0]) * (float)spacing.X;
1100 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1102 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+15, pos.Y+300);
1104 if(data->bp_set != 2)
1105 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
1107 std::wstring label = L"";
1109 for (unsigned int i=0; i < text.length(); i++) {
1114 FieldSpec spec = FieldSpec(
1120 gui::IGUIStaticText *t =
1121 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
1122 t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1123 m_fields.push_back(spec);
1126 errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
1129 void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,std::string type) {
1130 std::vector<std::string> parts = split(element,';');
1132 if ((parts.size() == 5) || (parts.size() == 7) || (parts.size() == 8)) {
1133 std::vector<std::string> v_pos = split(parts[0],',');
1134 std::vector<std::string> v_geom = split(parts[1],',');
1135 std::string image_name = parts[2];
1136 std::string name = parts[3];
1137 std::string label = parts[4];
1139 MY_CHECKPOS("imagebutton",0);
1140 MY_CHECKGEOM("imagebutton",1);
1142 v2s32 pos = padding;
1143 pos.X += stof(v_pos[0]) * (float)spacing.X;
1144 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1146 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1147 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1149 bool noclip = false;
1150 bool drawborder = true;
1152 if ((parts.size() >= 7)) {
1153 if (parts[5] == "true")
1156 if (parts[6] == "false")
1160 std::string pressed_image_name = "";
1162 if ((parts.size() == 8)) {
1163 pressed_image_name = parts[7];
1166 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1168 if(data->bp_set != 2)
1169 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
1171 image_name = unescape_string(image_name);
1172 pressed_image_name = unescape_string(pressed_image_name);
1173 label = unescape_string(label);
1175 std::wstring wlabel = narrow_to_wide(label.c_str());
1177 FieldSpec spec = FieldSpec(
1178 narrow_to_wide(name.c_str()),
1180 narrow_to_wide(image_name.c_str()),
1183 spec.ftype = f_Button;
1184 if(type == "image_button_exit")
1185 spec.is_exit = true;
1187 video::ITexture *texture = 0;
1188 video::ITexture *pressed_texture = 0;
1189 texture = m_tsrc->getTexture(image_name);
1190 if (parts.size() == 8)
1191 pressed_texture = m_tsrc->getTexture(pressed_image_name);
1193 pressed_texture = texture;
1195 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1197 if (spec.fname == data->focused_fieldname) {
1198 Environment->setFocus(e);
1201 e->setUseAlphaChannel(true);
1202 e->setImage(texture);
1203 e->setPressedImage(pressed_texture);
1204 e->setScaleImage(true);
1205 e->setNotClipped(noclip);
1206 e->setDrawBorder(drawborder);
1208 m_fields.push_back(spec);
1212 errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1215 void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element) {
1216 std::vector<std::string> parts = split(element,';');
1218 if ((parts.size() == 4) || (parts.size() == 6)) {
1219 std::vector<std::string> v_pos = split(parts[0],',');
1220 std::string name = parts[1];
1221 std::vector<std::string> buttons = split(parts[2],',');
1222 std::string str_index = parts[3];
1223 bool show_background = true;
1224 bool show_border = true;
1225 int tab_index = stoi(str_index) -1;
1227 MY_CHECKPOS("tabheader",0);
1229 if (parts.size() == 6) {
1230 if (parts[4] == "true")
1231 show_background = false;
1232 if (parts[5] == "false")
1233 show_border = false;
1236 FieldSpec spec = FieldSpec(
1237 narrow_to_wide(name.c_str()),
1243 spec.ftype = f_TabHeader;
1245 v2s32 pos = padding;
1246 pos.X += stof(v_pos[0]) * (float)spacing.X;
1247 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1249 geom.X = data->screensize.Y;
1252 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1254 gui::IGUITabControl *e = Environment->addTabControl(rect,this,show_background,show_border,spec.fid);
1256 if (spec.fname == data->focused_fieldname) {
1257 Environment->setFocus(e);
1260 e->setNotClipped(true);
1262 for (unsigned int i=0; i< buttons.size(); i++) {
1263 wchar_t* wbutton = 0;
1265 std::wstring wlabel = narrow_to_wide(buttons[i]); //Needed for displaying text on windows
1266 wbutton = (wchar_t*) wlabel.c_str();
1268 e->addTab(wbutton,-1);
1271 if ((tab_index >= 0) &&
1272 (buttons.size() < INT_MAX) &&
1273 (tab_index < (int) buttons.size()))
1274 e->setActiveTab(tab_index);
1276 m_fields.push_back(spec);
1279 errorstream<< "Invalid TabHeader element(" << parts.size() << "): '" << element << "'" << std::endl;
1282 void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element) {
1284 if (m_gamedef == 0) {
1285 errorstream<<"WARNING: invalid use of item_image_button with m_gamedef==0"<<std::endl;
1289 std::vector<std::string> parts = split(element,';');
1291 if (parts.size() == 5) {
1292 std::vector<std::string> v_pos = split(parts[0],',');
1293 std::vector<std::string> v_geom = split(parts[1],',');
1294 std::string item_name = parts[2];
1295 std::string name = parts[3];
1296 std::string label = parts[4];
1298 MY_CHECKPOS("itemimagebutton",0);
1299 MY_CHECKGEOM("itemimagebutton",1);
1301 v2s32 pos = padding;
1302 pos.X += stof(v_pos[0]) * (float)spacing.X;
1303 pos.Y += stof(v_pos[1]) * (float)spacing.Y;
1305 geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
1306 geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
1308 core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
1310 if(data->bp_set != 2)
1311 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
1313 IItemDefManager *idef = m_gamedef->idef();
1315 item.deSerialize(item_name, idef);
1316 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1317 std::string tooltip = item.getDefinition(idef).description;
1319 label = unescape_string(label);
1320 FieldSpec spec = FieldSpec(
1321 narrow_to_wide(name.c_str()),
1322 narrow_to_wide(label.c_str()),
1323 narrow_to_wide(item_name.c_str()),
1327 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
1329 if (spec.fname == data->focused_fieldname) {
1330 Environment->setFocus(e);
1333 e->setUseAlphaChannel(true);
1334 e->setImage(texture);
1335 e->setPressedImage(texture);
1336 e->setScaleImage(true);
1337 spec.ftype = f_Button;
1338 rect+=data->basepos-padding;
1341 spec.tooltip=tooltip;
1342 m_fields.push_back(spec);
1345 errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
1348 void GUIFormSpecMenu::parseBox(parserData* data,std::string element) {
1349 std::vector<std::string> parts = split(element,';');
1351 if (parts.size() == 3) {
1352 std::vector<std::string> v_pos = split(parts[0],',');
1353 std::vector<std::string> v_geom = split(parts[1],',');
1355 MY_CHECKPOS("box",0);
1356 MY_CHECKGEOM("box",1);
1358 v2s32 pos = padding + AbsoluteRect.UpperLeftCorner;
1359 pos.X += stof(v_pos[0]) * (float) spacing.X;
1360 pos.Y += stof(v_pos[1]) * (float) spacing.Y;
1363 geom.X = stof(v_geom[0]) * (float)spacing.X;
1364 geom.Y = stof(v_geom[1]) * (float)spacing.Y;
1366 video::SColor tmp_color;
1368 if (parseColor(parts[2], tmp_color, false)) {
1369 BoxDrawSpec spec(pos, geom, tmp_color);
1371 m_boxes.push_back(spec);
1374 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
1378 errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
1381 void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element) {
1382 std::vector<std::string> parts = split(element,';');
1384 if ((parts.size() == 1) || (parts.size() == 2)) {
1385 parseColor(parts[0],m_bgcolor,false);
1387 if (parts.size() == 2) {
1388 std::string fullscreen = parts[1];
1389 m_bgfullscreen = is_yes(fullscreen);
1393 errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
1396 void GUIFormSpecMenu::parseListColors(parserData* data,std::string element) {
1397 std::vector<std::string> parts = split(element,';');
1399 if ((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) {
1400 parseColor(parts[0], m_slotbg_n, false);
1401 parseColor(parts[1], m_slotbg_h, false);
1403 if (parts.size() >= 3) {
1404 if (parseColor(parts[2], m_slotbordercolor, false)) {
1405 m_slotborder = true;
1408 if (parts.size() == 5) {
1409 video::SColor tmp_color;
1411 if (parseColor(parts[3], tmp_color, false))
1412 m_tooltip_element->setBackgroundColor(tmp_color);
1413 if (parseColor(parts[4], tmp_color, false))
1414 m_tooltip_element->setOverrideColor(tmp_color);
1418 errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
1421 void GUIFormSpecMenu::parseElement(parserData* data,std::string element) {
1427 std::vector<std::string> parts = split(element,'[');
1429 // ugly workaround to keep compatibility
1430 if (parts.size() > 2) {
1431 if (trim(parts[0]) == "image") {
1432 for (unsigned int i=2;i< parts.size(); i++) {
1433 parts[1] += "[" + parts[i];
1439 if (parts.size() < 2) {
1443 std::string type = trim(parts[0]);
1444 std::string description = trim(parts[1]);
1446 if ((type == "size") || (type == "invsize")){
1447 parseSize(data,description);
1451 if (type == "list") {
1452 parseList(data,description);
1456 if (type == "checkbox") {
1457 parseCheckbox(data,description);
1461 if (type == "image") {
1462 parseImage(data,description);
1466 if (type == "item_image") {
1467 parseItemImage(data,description);
1471 if ((type == "button") || (type == "button_exit")) {
1472 parseButton(data,description,type);
1476 if (type == "background") {
1477 parseBackground(data,description);
1481 if (type == "textlist"){
1482 parseTextList(data,description);
1486 if (type == "dropdown"){
1487 parseDropDown(data,description);
1491 if (type == "pwdfield") {
1492 parsePwdField(data,description);
1496 if ((type == "field") || (type == "textarea")){
1497 parseField(data,description,type);
1501 if (type == "label") {
1502 parseLabel(data,description);
1506 if (type == "vertlabel") {
1507 parseVertLabel(data,description);
1511 if (type == "item_image_button") {
1512 parseItemImageButton(data,description);
1516 if ((type == "image_button") || (type == "image_button_exit")) {
1517 parseImageButton(data,description,type);
1521 if (type == "tabheader") {
1522 parseTabHeader(data,description);
1526 if (type == "box") {
1527 parseBox(data,description);
1531 if (type == "bgcolor") {
1532 parseBackgroundColor(data,description);
1536 if (type == "listcolors") {
1537 parseListColors(data,description);
1543 << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
1549 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
1553 //preserve listboxes
1554 for (unsigned int i = 0; i < m_listboxes.size(); i++) {
1555 std::wstring listboxname = m_listboxes[i].first.fname;
1556 gui::IGUIListBox *listbox = m_listboxes[i].second;
1558 int selection = listbox->getSelected();
1559 if (selection != -1) {
1560 mydata.listbox_selections[listboxname] = selection;
1563 gui::IGUIScrollBar *scrollbar = getListboxScrollbar(listbox);
1565 mydata.listbox_scroll[listboxname] = scrollbar->getPos();
1570 gui::IGUIElement *focused_element = Environment->getFocus();
1571 if (focused_element && focused_element->getParent() == this) {
1572 s32 focused_id = focused_element->getID();
1573 if (focused_id > 257) {
1574 for (u32 i=0; i<m_fields.size(); i++) {
1575 if (m_fields[i].fid == focused_id) {
1576 mydata.focused_fieldname =
1587 mydata.size= v2s32(100,100);
1588 mydata.helptext_h = 15;
1589 mydata.screensize = screensize;
1591 // Base position of contents of form
1592 mydata.basepos = getBasePos();
1594 // State of basepos, 0 = not set, 1= set by formspec, 2 = set by size[] element
1595 // Used to adjust form size automatically if needed
1596 // A proceed button is added if there is no size[] element
1600 /* Convert m_init_draw_spec to m_inventorylists */
1602 m_inventorylists.clear();
1604 m_backgrounds.clear();
1605 m_itemimages.clear();
1606 m_listboxes.clear();
1607 m_checkboxes.clear();
1611 // Set default values (fits old formspec values)
1612 m_bgcolor = video::SColor(140,0,0,0);
1613 m_bgfullscreen = false;
1615 m_slotbg_n = video::SColor(255,128,128,128);
1616 m_slotbg_h = video::SColor(255,192,192,192);
1618 m_slotbordercolor = video::SColor(200,0,0,0);
1619 m_slotborder = false;
1621 m_clipbackground = false;
1624 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
1625 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
1626 m_tooltip_element->enableOverrideColor(true);
1627 m_tooltip_element->setBackgroundColor(video::SColor(255,110,130,60));
1628 m_tooltip_element->setDrawBackground(true);
1629 m_tooltip_element->setDrawBorder(true);
1630 m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
1631 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
1632 m_tooltip_element->setWordWrap(false);
1633 //we're not parent so no autograb for this one!
1634 m_tooltip_element->grab();
1638 std::vector<std::string> elements = split(m_formspec_string,']');
1639 for (unsigned int i=0;i< elements.size();i++) {
1640 parseElement(&mydata,elements[i]);
1643 // If there's fields, add a Proceed button
1644 if (m_fields.size() && mydata.bp_set != 2)
1646 // if the size wasn't set by an invsize[] or size[] adjust it now to fit all the fields
1647 mydata.rect = core::rect<s32>(
1648 mydata.screensize.X/2 - 580/2,
1649 mydata.screensize.Y/2 - 300/2,
1650 mydata.screensize.X/2 + 580/2,
1651 mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
1653 DesiredRect = mydata.rect;
1654 recalculateAbsolutePosition(false);
1655 mydata.basepos = getBasePos();
1658 v2s32 pos = mydata.basepos;
1659 pos.Y = ((m_fields.size()+2)*60);
1661 v2s32 size = DesiredRect.getSize();
1662 mydata.rect = core::rect<s32>(size.X/2-70, pos.Y, (size.X/2-70)+140, pos.Y+30);
1663 wchar_t* text = wgettext("Proceed");
1664 Environment->addButton(mydata.rect, this, 257, text);
1670 //set initial focus if parser didn't set it
1671 focused_element = Environment->getFocus();
1672 if (!focused_element
1673 || !isMyChild(focused_element)
1674 || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
1678 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
1680 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1682 for(u32 i=0; i<m_inventorylists.size(); i++)
1684 const ListDrawSpec &s = m_inventorylists[i];
1686 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1688 s32 item_i = i + s.start_item_i;
1689 s32 x = (i%s.geom.X) * spacing.X;
1690 s32 y = (i/s.geom.X) * spacing.Y;
1692 core::rect<s32> rect = imgrect + s.pos + p0;
1693 if(rect.isPointInside(p))
1695 return ItemSpec(s.inventoryloc, s.listname, item_i);
1700 return ItemSpec(InventoryLocation(), "", -1);
1703 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
1705 video::IVideoDriver* driver = Environment->getVideoDriver();
1708 gui::IGUIFont *font = NULL;
1709 gui::IGUISkin* skin = Environment->getSkin();
1711 font = skin->getFont();
1713 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
1715 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1716 <<"The inventory location "
1717 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
1721 InventoryList *ilist = inv->getList(s.listname);
1723 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
1724 <<"The inventory list \""<<s.listname<<"\" @ \""
1725 <<s.inventoryloc.dump()<<"\" doesn't exist"
1730 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1732 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
1734 s32 item_i = i + s.start_item_i;
1735 if(item_i >= (s32) ilist->getSize())
1737 s32 x = (i%s.geom.X) * spacing.X;
1738 s32 y = (i/s.geom.X) * spacing.Y;
1740 core::rect<s32> rect = imgrect + s.pos + p;
1743 item = ilist->getItem(item_i);
1745 bool selected = m_selected_item
1746 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
1747 && m_selected_item->listname == s.listname
1748 && m_selected_item->i == item_i;
1749 bool hovering = rect.isPointInside(m_pointer);
1754 driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
1756 driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
1759 //Draw inv slot borders
1761 s32 x1 = rect.UpperLeftCorner.X;
1762 s32 y1 = rect.UpperLeftCorner.Y;
1763 s32 x2 = rect.LowerRightCorner.X;
1764 s32 y2 = rect.LowerRightCorner.Y;
1766 driver->draw2DRectangle(m_slotbordercolor,
1767 core::rect<s32>(v2s32(x1 - border, y1 - border),
1768 v2s32(x2 + border, y1)), NULL);
1769 driver->draw2DRectangle(m_slotbordercolor,
1770 core::rect<s32>(v2s32(x1 - border, y2),
1771 v2s32(x2 + border, y2 + border)), NULL);
1772 driver->draw2DRectangle(m_slotbordercolor,
1773 core::rect<s32>(v2s32(x1 - border, y1),
1774 v2s32(x1, y2)), NULL);
1775 driver->draw2DRectangle(m_slotbordercolor,
1776 core::rect<s32>(v2s32(x2, y1),
1777 v2s32(x2 + border, y2)), NULL);
1785 item.takeItem(m_selected_amount);
1789 drawItemStack(driver, font, item,
1790 rect, &AbsoluteClippingRect, m_gamedef);
1794 std::string tooltip_text = "";
1795 if(hovering && !m_selected_item)
1796 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
1797 if(tooltip_text != "")
1799 m_tooltip_element->setVisible(true);
1800 this->bringToFront(m_tooltip_element);
1801 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
1802 s32 tooltip_x = m_pointer.X + 15;
1803 s32 tooltip_y = m_pointer.Y + 15;
1804 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
1805 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
1806 m_tooltip_element->setRelativePosition(core::rect<s32>(
1807 core::position2d<s32>(tooltip_x, tooltip_y),
1808 core::dimension2d<s32>(tooltip_width, tooltip_height)));
1814 void GUIFormSpecMenu::drawSelectedItem()
1816 if(!m_selected_item)
1819 video::IVideoDriver* driver = Environment->getVideoDriver();
1822 gui::IGUIFont *font = NULL;
1823 gui::IGUISkin* skin = Environment->getSkin();
1825 font = skin->getFont();
1827 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
1829 InventoryList *list = inv->getList(m_selected_item->listname);
1831 ItemStack stack = list->getItem(m_selected_item->i);
1832 stack.count = m_selected_amount;
1834 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
1835 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
1836 drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
1839 void GUIFormSpecMenu::drawMenu()
1842 std::string newform = m_form_src->getForm();
1843 if(newform != m_formspec_string){
1844 m_formspec_string = newform;
1845 regenerateGui(m_screensize_old);
1849 m_pointer = m_device->getCursorControl()->getPosition();
1851 updateSelectedItem();
1853 gui::IGUISkin* skin = Environment->getSkin();
1856 video::IVideoDriver* driver = Environment->getVideoDriver();
1858 v2u32 screenSize = driver->getScreenSize();
1859 core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
1861 driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
1863 driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
1865 m_tooltip_element->setVisible(false);
1870 for(u32 i=0; i<m_backgrounds.size(); i++)
1872 const ImageDrawSpec &spec = m_backgrounds[i];
1873 video::ITexture *texture = m_tsrc->getTexture(spec.name);
1876 // Image size on screen
1877 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1878 // Image rectangle on screen
1879 core::rect<s32> rect = imgrect + spec.pos;
1881 if (m_clipbackground) {
1882 core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
1883 rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
1884 AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
1885 AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
1886 AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
1889 const video::SColor color(255,255,255,255);
1890 const video::SColor colors[] = {color,color,color,color};
1891 driver->draw2DImage(texture, rect,
1892 core::rect<s32>(core::position2d<s32>(0,0),
1893 core::dimension2di(texture->getOriginalSize())),
1894 NULL/*&AbsoluteClippingRect*/, colors, true);
1897 errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
1898 errorstream << "\t" << spec.name << std::endl;
1905 for(u32 i=0; i<m_boxes.size(); i++)
1907 const BoxDrawSpec &spec = m_boxes[i];
1909 irr::video::SColor todraw = spec.color;
1911 todraw.setAlpha(140);
1913 core::rect<s32> rect(spec.pos.X,spec.pos.Y,
1914 spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
1916 driver->draw2DRectangle(todraw, rect, 0);
1921 for(u32 i=0; i<m_images.size(); i++)
1923 const ImageDrawSpec &spec = m_images[i];
1924 video::ITexture *texture = m_tsrc->getTexture(spec.name);
1927 const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
1928 // Image size on screen
1929 core::rect<s32> imgrect;
1932 imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
1935 imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
1937 // Image rectangle on screen
1938 core::rect<s32> rect = imgrect + spec.pos;
1939 const video::SColor color(255,255,255,255);
1940 const video::SColor colors[] = {color,color,color,color};
1941 driver->draw2DImage(texture, rect,
1942 core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
1943 NULL/*&AbsoluteClippingRect*/, colors, true);
1946 errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
1947 errorstream << "\t" << spec.name << std::endl;
1954 for(u32 i=0; i<m_itemimages.size(); i++)
1959 const ImageDrawSpec &spec = m_itemimages[i];
1960 IItemDefManager *idef = m_gamedef->idef();
1962 item.deSerialize(spec.name, idef);
1963 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
1964 // Image size on screen
1965 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
1966 // Image rectangle on screen
1967 core::rect<s32> rect = imgrect + spec.pos;
1968 const video::SColor color(255,255,255,255);
1969 const video::SColor colors[] = {color,color,color,color};
1970 driver->draw2DImage(texture, rect,
1971 core::rect<s32>(core::position2d<s32>(0,0),
1972 core::dimension2di(texture->getOriginalSize())),
1973 NULL/*&AbsoluteClippingRect*/, colors, true);
1978 Phase 0: Item slot rectangles
1979 Phase 1: Item images; prepare tooltip
1982 for(int phase=start_phase; phase<=1; phase++)
1983 for(u32 i=0; i<m_inventorylists.size(); i++)
1985 drawList(m_inventorylists[i], phase);
1991 gui::IGUIElement::draw();
1994 Draw fields/buttons tooltips
1996 for(u32 i=0; i<m_fields.size(); i++)
1998 const FieldSpec &spec = m_fields[i];
1999 if (spec.tooltip != "")
2001 core::rect<s32> rect = spec.rect;
2002 if (rect.isPointInside(m_pointer))
2004 m_tooltip_element->setVisible(true);
2005 this->bringToFront(m_tooltip_element);
2006 m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
2007 s32 tooltip_x = m_pointer.X + 15;
2008 s32 tooltip_y = m_pointer.Y + 15;
2009 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
2010 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2011 m_tooltip_element->setRelativePosition(core::rect<s32>(
2012 core::position2d<s32>(tooltip_x, tooltip_y),
2013 core::dimension2d<s32>(tooltip_width, tooltip_height)));
2019 Draw dragged item stack
2024 void GUIFormSpecMenu::updateSelectedItem()
2026 // If the selected stack has become empty for some reason, deselect it.
2027 // If the selected stack has become inaccessible, deselect it.
2028 // If the selected stack has become smaller, adjust m_selected_amount.
2029 ItemStack selected = verifySelectedItem();
2031 // WARNING: BLACK MAGIC
2032 // See if there is a stack suited for our current guess.
2033 // If such stack does not exist, clear the guess.
2034 if(m_selected_content_guess.name != "" &&
2035 selected.name == m_selected_content_guess.name &&
2036 selected.count == m_selected_content_guess.count){
2037 // Selected item fits the guess. Skip the black magic.
2039 else if(m_selected_content_guess.name != ""){
2041 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
2042 const ListDrawSpec &s = m_inventorylists[i];
2043 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2046 InventoryList *list = inv->getList(s.listname);
2049 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
2050 u32 item_i = i + s.start_item_i;
2051 if(item_i >= list->getSize())
2053 ItemStack stack = list->getItem(item_i);
2054 if(stack.name == m_selected_content_guess.name &&
2055 stack.count == m_selected_content_guess.count){
2057 infostream<<"Client: Changing selected content guess to "
2058 <<s.inventoryloc.dump()<<" "<<s.listname
2059 <<" "<<item_i<<std::endl;
2060 delete m_selected_item;
2061 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
2062 m_selected_amount = stack.count;
2067 infostream<<"Client: Discarding selected content guess: "
2068 <<m_selected_content_guess.getItemString()<<std::endl;
2069 m_selected_content_guess.name = "";
2073 // If craftresult is nonempty and nothing else is selected, select it now.
2074 if(!m_selected_item)
2076 for(u32 i=0; i<m_inventorylists.size(); i++)
2078 const ListDrawSpec &s = m_inventorylists[i];
2079 if(s.listname == "craftpreview")
2081 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
2082 InventoryList *list = inv->getList("craftresult");
2083 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
2085 m_selected_item = new ItemSpec;
2086 m_selected_item->inventoryloc = s.inventoryloc;
2087 m_selected_item->listname = "craftresult";
2088 m_selected_item->i = 0;
2089 m_selected_amount = 0;
2090 m_selected_dragging = false;
2097 // If craftresult is selected, keep the whole stack selected
2098 if(m_selected_item && m_selected_item->listname == "craftresult")
2100 m_selected_amount = verifySelectedItem().count;
2104 ItemStack GUIFormSpecMenu::verifySelectedItem()
2106 // If the selected stack has become empty for some reason, deselect it.
2107 // If the selected stack has become inaccessible, deselect it.
2108 // If the selected stack has become smaller, adjust m_selected_amount.
2109 // Return the selected stack.
2113 if(m_selected_item->isValid())
2115 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
2118 InventoryList *list = inv->getList(m_selected_item->listname);
2119 if(list && (u32) m_selected_item->i < list->getSize())
2121 ItemStack stack = list->getItem(m_selected_item->i);
2122 if(m_selected_amount > stack.count)
2123 m_selected_amount = stack.count;
2130 // selection was not valid
2131 delete m_selected_item;
2132 m_selected_item = NULL;
2133 m_selected_amount = 0;
2134 m_selected_dragging = false;
2139 void GUIFormSpecMenu::acceptInput(bool quit=false)
2143 std::map<std::string, std::string> fields;
2146 fields["quit"] = "true";
2149 if (current_keys_pending.key_down) {
2150 fields["key_down"] = "true";
2151 current_keys_pending.key_down = false;
2154 if (current_keys_pending.key_up) {
2155 fields["key_up"] = "true";
2156 current_keys_pending.key_up = false;
2159 if (current_keys_pending.key_enter) {
2160 fields["key_enter"] = "true";
2161 current_keys_pending.key_enter = false;
2164 if (current_keys_pending.key_escape) {
2165 fields["key_escape"] = "true";
2166 current_keys_pending.key_escape = false;
2169 for(u32 i=0; i<m_fields.size(); i++)
2171 const FieldSpec &s = m_fields[i];
2174 if(s.ftype == f_Button)
2176 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(s.flabel.c_str());
2178 else if(s.ftype == f_ListBox) {
2179 std::stringstream ss;
2181 if (m_listbox_doubleclick) {
2187 ss << (getListboxIndex(wide_to_narrow(s.fname.c_str()))+1);
2188 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2190 else if(s.ftype == f_DropDown) {
2191 // no dynamic cast possible due to some distributions shipped
2192 // without rtti support in irrlicht
2193 IGUIElement * element = getElementFromId(s.fid);
2194 gui::IGUIComboBox *e = NULL;
2195 if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
2196 e = static_cast<gui::IGUIComboBox*>(element);
2198 s32 selected = e->getSelected();
2199 if (selected >= 0) {
2200 fields[wide_to_narrow(s.fname.c_str())] =
2201 wide_to_narrow(e->getItem(selected));
2204 else if (s.ftype == f_TabHeader) {
2205 // no dynamic cast possible due to some distributions shipped
2206 // without rtti support in irrlicht
2207 IGUIElement * element = getElementFromId(s.fid);
2208 gui::IGUITabControl *e = NULL;
2209 if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
2210 e = static_cast<gui::IGUITabControl*>(element);
2214 std::stringstream ss;
2215 ss << (e->getActiveTab() +1);
2216 fields[wide_to_narrow(s.fname.c_str())] = ss.str();
2219 else if (s.ftype == f_CheckBox) {
2220 // no dynamic cast possible due to some distributions shipped
2221 // without rtti support in irrlicht
2222 IGUIElement * element = getElementFromId(s.fid);
2223 gui::IGUICheckBox *e = NULL;
2224 if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
2225 e = static_cast<gui::IGUICheckBox*>(element);
2230 fields[wide_to_narrow(s.fname.c_str())] = "true";
2232 fields[wide_to_narrow(s.fname.c_str())] = "false";
2237 IGUIElement* e = getElementFromId(s.fid);
2240 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(e->getText());
2246 m_text_dst->gotText(fields);
2250 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
2252 // Fix Esc/Return key being eaten by checkboxen and listboxen
2253 if(event.EventType==EET_KEY_INPUT_EVENT)
2255 KeyPress kp(event.KeyInput);
2256 if (kp == EscapeKey || kp == getKeySetting("keymap_inventory")
2257 || event.KeyInput.Key==KEY_RETURN)
2259 gui::IGUIElement *focused = Environment->getFocus();
2260 if (focused && isMyChild(focused) &&
2261 (focused->getType() == gui::EGUIET_LIST_BOX ||
2262 focused->getType() == gui::EGUIET_CHECK_BOX)) {
2268 // Mouse wheel events: send to hovered element instead of focused
2269 if(event.EventType==EET_MOUSE_INPUT_EVENT
2270 && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
2272 s32 x = event.MouseInput.X;
2273 s32 y = event.MouseInput.Y;
2274 gui::IGUIElement *hovered =
2275 Environment->getRootGUIElement()->getElementFromPoint(
2276 core::position2d<s32>(x, y));
2277 if (hovered && isMyChild(hovered)) {
2278 hovered->OnEvent(event);
2285 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
2287 if(event.EventType==EET_KEY_INPUT_EVENT)
2289 KeyPress kp(event.KeyInput);
2290 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
2291 kp == getKeySetting("keymap_inventory")))
2297 m_text_dst->gotText(narrow_to_wide("MenuQuit"));
2301 if (event.KeyInput.PressedDown &&
2302 (event.KeyInput.Key==KEY_RETURN ||
2303 event.KeyInput.Key==KEY_UP ||
2304 event.KeyInput.Key==KEY_DOWN)
2308 switch (event.KeyInput.Key) {
2310 current_keys_pending.key_enter = true;
2313 current_keys_pending.key_up = true;
2316 current_keys_pending.key_down = true;
2320 //can't happen at all!
2321 assert("reached a source line that can't ever been reached" == 0);
2324 if (current_keys_pending.key_enter && m_allowclose) {
2335 if(event.EventType==EET_MOUSE_INPUT_EVENT
2336 && event.MouseInput.Event != EMIE_MOUSE_MOVED)
2338 // Mouse event other than movement
2340 // Get selected item and hovered/clicked item (s)
2342 updateSelectedItem();
2343 ItemSpec s = getItemAtPos(m_pointer);
2345 Inventory *inv_selected = NULL;
2346 Inventory *inv_s = NULL;
2350 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
2351 assert(inv_selected);
2352 assert(inv_selected->getList(m_selected_item->listname) != NULL);
2359 inv_s = m_invmgr->getInventory(s.inventoryloc);
2362 errorstream<<"InventoryMenu: The selected inventory location "
2363 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
2365 s.i = -1; // make it invalid again
2369 InventoryList *list = inv_s->getList(s.listname);
2371 verbosestream<<"InventoryMenu: The selected inventory list \""
2372 <<s.listname<<"\" does not exist"<<std::endl;
2373 s.i = -1; // make it invalid again
2377 if((u32)s.i >= list->getSize()){
2378 infostream<<"InventoryMenu: The selected inventory list \""
2379 <<s.listname<<"\" is too small (i="<<s.i<<", size="
2380 <<list->getSize()<<")"<<std::endl;
2381 s.i = -1; // make it invalid again
2385 s_count = list->getItem(s.i).count;
2388 bool identical = (m_selected_item != NULL) && s.isValid() &&
2389 (inv_selected == inv_s) &&
2390 (m_selected_item->listname == s.listname) &&
2391 (m_selected_item->i == s.i);
2393 // buttons: 0 = left, 1 = right, 2 = middle
2394 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
2397 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
2398 { button = 0; updown = 0; }
2399 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
2400 { button = 1; updown = 0; }
2401 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
2402 { button = 2; updown = 0; }
2403 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
2404 { button = 0; updown = 1; }
2405 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
2406 { button = 1; updown = 1; }
2407 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
2408 { button = 2; updown = 1; }
2410 // Set this number to a positive value to generate a move action
2411 // from m_selected_item to s.
2412 u32 move_amount = 0;
2414 // Set this number to a positive value to generate a drop action
2415 // from m_selected_item.
2416 u32 drop_amount = 0;
2418 // Set this number to a positive value to generate a craft action at s.
2419 u32 craft_amount = 0;
2423 // Some mouse button has been pressed
2425 //infostream<<"Mouse button "<<button<<" pressed at p=("
2426 // <<p.X<<","<<p.Y<<")"<<std::endl;
2428 m_selected_dragging = false;
2430 if(s.isValid() && s.listname == "craftpreview")
2432 // Craft preview has been clicked: craft
2433 craft_amount = (button == 2 ? 10 : 1);
2435 else if(m_selected_item == NULL)
2439 // Non-empty stack has been clicked: select it
2440 m_selected_item = new ItemSpec(s);
2442 if(button == 1) // right
2443 m_selected_amount = (s_count + 1) / 2;
2444 else if(button == 2) // middle
2445 m_selected_amount = MYMIN(s_count, 10);
2447 m_selected_amount = s_count;
2449 m_selected_dragging = true;
2452 else // m_selected_item != NULL
2454 assert(m_selected_amount >= 1);
2458 // Clicked a slot: move
2459 if(button == 1) // right
2461 else if(button == 2) // middle
2462 move_amount = MYMIN(m_selected_amount, 10);
2464 move_amount = m_selected_amount;
2468 if(move_amount >= m_selected_amount)
2469 m_selected_amount = 0;
2471 m_selected_amount -= move_amount;
2475 else if (!getAbsoluteClippingRect().isPointInside(m_pointer))
2477 // Clicked outside of the window: drop
2478 if(button == 1) // right
2480 else if(button == 2) // middle
2481 drop_amount = MYMIN(m_selected_amount, 10);
2483 drop_amount = m_selected_amount;
2487 else if(updown == 1)
2489 // Some mouse button has been released
2491 //infostream<<"Mouse button "<<button<<" released at p=("
2492 // <<p.X<<","<<p.Y<<")"<<std::endl;
2494 if(m_selected_item != NULL && m_selected_dragging && s.isValid())
2498 // Dragged to different slot: move all selected
2499 move_amount = m_selected_amount;
2502 else if(m_selected_item != NULL && m_selected_dragging &&
2503 !(getAbsoluteClippingRect().isPointInside(m_pointer)))
2505 // Dragged outside of window: drop all selected
2506 drop_amount = m_selected_amount;
2509 m_selected_dragging = false;
2512 // Possibly send inventory action to server
2515 // Send IACTION_MOVE
2517 assert(m_selected_item && m_selected_item->isValid());
2518 assert(s.isValid());
2520 assert(inv_selected && inv_s);
2521 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2522 InventoryList *list_to = inv_s->getList(s.listname);
2523 assert(list_from && list_to);
2524 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2525 ItemStack stack_to = list_to->getItem(s.i);
2527 // Check how many items can be moved
2528 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
2529 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
2530 // If source stack cannot be added to destination stack at all,
2532 if(leftover.count == stack_from.count && leftover.name == stack_from.name)
2534 m_selected_amount = stack_to.count;
2535 // In case the server doesn't directly swap them but instead
2536 // moves stack_to somewhere else, set this
2537 m_selected_content_guess = stack_to;
2538 m_selected_content_guess_inventory = s.inventoryloc;
2540 // Source stack goes fully into destination stack
2541 else if(leftover.empty())
2543 m_selected_amount -= move_amount;
2544 m_selected_content_guess = ItemStack(); // Clear
2546 // Source stack goes partly into destination stack
2549 move_amount -= leftover.count;
2550 m_selected_amount -= move_amount;
2551 m_selected_content_guess = ItemStack(); // Clear
2554 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
2555 IMoveAction *a = new IMoveAction();
2556 a->count = move_amount;
2557 a->from_inv = m_selected_item->inventoryloc;
2558 a->from_list = m_selected_item->listname;
2559 a->from_i = m_selected_item->i;
2560 a->to_inv = s.inventoryloc;
2561 a->to_list = s.listname;
2563 m_invmgr->inventoryAction(a);
2565 else if(drop_amount > 0)
2567 m_selected_content_guess = ItemStack(); // Clear
2569 // Send IACTION_DROP
2571 assert(m_selected_item && m_selected_item->isValid());
2572 assert(inv_selected);
2573 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
2575 ItemStack stack_from = list_from->getItem(m_selected_item->i);
2577 // Check how many items can be dropped
2578 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
2579 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
2580 m_selected_amount -= drop_amount;
2582 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
2583 IDropAction *a = new IDropAction();
2584 a->count = drop_amount;
2585 a->from_inv = m_selected_item->inventoryloc;
2586 a->from_list = m_selected_item->listname;
2587 a->from_i = m_selected_item->i;
2588 m_invmgr->inventoryAction(a);
2590 else if(craft_amount > 0)
2592 m_selected_content_guess = ItemStack(); // Clear
2594 // Send IACTION_CRAFT
2596 assert(s.isValid());
2599 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
2600 ICraftAction *a = new ICraftAction();
2601 a->count = craft_amount;
2602 a->craft_inv = s.inventoryloc;
2603 m_invmgr->inventoryAction(a);
2606 // If m_selected_amount has been decreased to zero, deselect
2607 if(m_selected_amount == 0)
2609 delete m_selected_item;
2610 m_selected_item = NULL;
2611 m_selected_amount = 0;
2612 m_selected_dragging = false;
2613 m_selected_content_guess = ItemStack();
2616 if(event.EventType==EET_GUI_EVENT)
2619 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED
2622 // find the element that was clicked
2623 for(u32 i=0; i<m_fields.size(); i++)
2625 FieldSpec &s = m_fields[i];
2626 // if its a button, set the send field so
2627 // lua knows which button was pressed
2628 if ((s.ftype == f_TabHeader) && (s.fid == event.GUIEvent.Caller->getID()))
2637 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
2640 if(!canTakeFocus(event.GUIEvent.Element))
2642 infostream<<"GUIFormSpecMenu: Not allowing focus change."
2644 // Returning true disables focus change
2648 if((event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) ||
2649 (event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED))
2651 unsigned int btn_id = event.GUIEvent.Caller->getID();
2653 if (btn_id == 257) {
2659 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2661 // quitMenu deallocates menu
2665 // find the element that was clicked
2666 for(u32 i=0; i<m_fields.size(); i++)
2668 FieldSpec &s = m_fields[i];
2669 // if its a button, set the send field so
2670 // lua knows which button was pressed
2671 if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
2672 (s.fid == event.GUIEvent.Caller->getID()))
2681 m_text_dst->gotText(narrow_to_wide("ExitButton"));
2691 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
2693 if(event.GUIEvent.Caller->getID() > 257)
2701 current_keys_pending.key_enter = true;
2704 // quitMenu deallocates menu
2709 if((event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN) ||
2710 (event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED))
2712 int current_id = event.GUIEvent.Caller->getID();
2713 if(current_id > 257)
2715 // find the element that was clicked
2716 for(u32 i=0; i<m_fields.size(); i++)
2718 FieldSpec &s = m_fields[i];
2719 // if its a listbox, set the send field so
2720 // lua knows which listbox was changed
2721 // checkListboxClick() is black magic
2722 // for properly handling double clicks
2723 if ((s.ftype == f_ListBox) && (s.fid == current_id)
2724 && checkListboxClick(s.fname,
2725 event.GUIEvent.EventType))
2737 return Parent ? Parent->OnEvent(event) : false;
2740 bool GUIFormSpecMenu::parseColor(std::string &value, video::SColor &color, bool quiet)
2742 const char *hexpattern = NULL;
2743 if (value[0] == '#') {
2744 if (value.size() == 9)
2745 hexpattern = "#RRGGBBAA";
2746 else if (value.size() == 7)
2747 hexpattern = "#RRGGBB";
2748 else if (value.size() == 5)
2749 hexpattern = "#RGBA";
2750 else if (value.size() == 4)
2751 hexpattern = "#RGB";
2755 assert(strlen(hexpattern) == value.size());
2756 video::SColor outcolor(255, 255, 255, 255);
2757 for (size_t pos = 0; pos < value.size(); ++pos) {
2758 // '#' in the pattern means skip that character
2759 if (hexpattern[pos] == '#')
2762 // Else assume hexpattern[pos] is one of 'R' 'G' 'B' 'A'
2763 // Read one or two digits, depending on hexpattern
2764 unsigned char c1, c2;
2765 if (hexpattern[pos+1] == hexpattern[pos]) {
2766 // Two digits, e.g. hexpattern == "#RRGGBB"
2767 if (!hex_digit_decode(value[pos], c1) ||
2768 !hex_digit_decode(value[pos+1], c2))
2773 // One digit, e.g. hexpattern == "#RGB"
2774 if (!hex_digit_decode(value[pos], c1))
2778 u32 colorpart = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
2780 // Update outcolor with newly read color part
2781 if (hexpattern[pos] == 'R')
2782 outcolor.setRed(colorpart);
2783 else if (hexpattern[pos] == 'G')
2784 outcolor.setGreen(colorpart);
2785 else if (hexpattern[pos] == 'B')
2786 outcolor.setBlue(colorpart);
2787 else if (hexpattern[pos] == 'A')
2788 outcolor.setAlpha(colorpart);
2795 // Optionally, named colors could be implemented here
2799 errorstream<<"Invalid color: \""<<value<<"\""<<std::endl;