3 Copyright (C) 2010 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.
21 #include "guiFormSpecMenu.h"
22 #include "constants.h"
26 #include <IGUICheckBox.h>
27 #include <IGUIEditBox.h>
28 #include <IGUIButton.h>
29 #include <IGUIStaticText.h>
32 #include "tile.h" // ITextureSource
33 #include "util/string.h"
34 #include "util/numeric.h"
38 void drawItemStack(video::IVideoDriver *driver,
40 const ItemStack &item,
41 const core::rect<s32> &rect,
42 const core::rect<s32> *clip,
48 const ItemDefinition &def = item.getDefinition(gamedef->idef());
49 video::ITexture *texture = gamedef->idef()->getInventoryTexture(def.name, gamedef);
51 // Draw the inventory texture
54 const video::SColor color(255,255,255,255);
55 const video::SColor colors[] = {color,color,color,color};
56 driver->draw2DImage(texture, rect,
57 core::rect<s32>(core::position2d<s32>(0,0),
58 core::dimension2di(texture->getOriginalSize())),
62 if(def.type == ITEM_TOOL && item.wear != 0)
65 float barheight = rect.getHeight()/16;
66 float barpad_x = rect.getWidth()/16;
67 float barpad_y = rect.getHeight()/16;
68 core::rect<s32> progressrect(
69 rect.UpperLeftCorner.X + barpad_x,
70 rect.LowerRightCorner.Y - barpad_y - barheight,
71 rect.LowerRightCorner.X - barpad_x,
72 rect.LowerRightCorner.Y - barpad_y);
74 // Shrink progressrect by amount of tool damage
75 float wear = item.wear / 65535.0;
77 wear * progressrect.UpperLeftCorner.X +
78 (1-wear) * progressrect.LowerRightCorner.X;
80 // Compute progressbar color
84 video::SColor color(255,255,255,255);
85 int wear_i = MYMIN(floor(wear * 600), 511);
86 wear_i = MYMIN(wear_i + 10, 511);
88 color.set(255, wear_i, 255, 0);
90 color.set(255, 255, 511-wear_i, 0);
92 core::rect<s32> progressrect2 = progressrect;
93 progressrect2.LowerRightCorner.X = progressmid;
94 driver->draw2DRectangle(color, progressrect2, clip);
96 color = video::SColor(255,0,0,0);
97 progressrect2 = progressrect;
98 progressrect2.UpperLeftCorner.X = progressmid;
99 driver->draw2DRectangle(color, progressrect2, clip);
102 if(font != NULL && item.count >= 2)
104 // Get the item count as a string
105 std::string text = itos(item.count);
106 v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
107 v2s32 sdim(dim.X,dim.Y);
109 core::rect<s32> rect2(
110 /*rect.UpperLeftCorner,
111 core::dimension2d<u32>(rect.getWidth(), 15)*/
112 rect.LowerRightCorner - sdim,
116 video::SColor bgcolor(128,0,0,0);
117 driver->draw2DRectangle(bgcolor, rect2, clip);
119 video::SColor color(255,255,255,255);
120 font->draw(text.c_str(), rect2, color, false, false, clip);
128 GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
129 gui::IGUIElement* parent, s32 id,
130 IMenuManager *menumgr,
131 InventoryManager *invmgr,
134 GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
140 m_selected_item(NULL),
141 m_selected_amount(0),
142 m_selected_dragging(false),
143 m_tooltip_element(NULL)
147 GUIFormSpecMenu::~GUIFormSpecMenu()
151 delete m_selected_item;
156 void GUIFormSpecMenu::removeChildren()
158 const core::list<gui::IGUIElement*> &children = getChildren();
159 core::list<gui::IGUIElement*> children_copy;
160 for(core::list<gui::IGUIElement*>::ConstIterator
161 i = children.begin(); i != children.end(); i++)
163 children_copy.push_back(*i);
165 for(core::list<gui::IGUIElement*>::Iterator
166 i = children_copy.begin();
167 i != children_copy.end(); i++)
172 gui::IGUIElement *e = getElementFromId(256);
176 if(m_tooltip_element)
178 m_tooltip_element->remove();
179 m_tooltip_element = NULL;
183 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
190 core::rect<s32> rect;
192 // Base position of contents of form
193 v2s32 basepos = getBasePos();
194 // State of basepos, 0 = not set, 1= set by formspec, 2 = set by size[] element
195 // Used to adjust form size automatically if needed
196 // A proceed button is added if there is no size[] element
199 /* Convert m_init_draw_spec to m_inventorylists */
201 m_inventorylists.clear();
203 m_backgrounds.clear();
204 m_itemimages.clear();
207 Strfnd f(m_formspec_string);
208 while(f.atend() == false)
210 std::string type = trim(f.next("["));
211 if(type == "invsize" || type == "size")
214 invsize.X = stof(f.next(","));
217 invsize.Y = stof(f.next("]"));
220 invsize.Y = stof(f.next(";"));
223 infostream<<"Form size ("<<invsize.X<<","<<invsize.Y<<")"<<std::endl;
225 padding = v2s32(screensize.Y/40, screensize.Y/40);
226 spacing = v2s32(screensize.Y/12, screensize.Y/13);
227 imgsize = v2s32(screensize.Y/15, screensize.Y/15);
229 padding.X*2+spacing.X*(invsize.X-1.0)+imgsize.X,
230 padding.Y*2+spacing.Y*(invsize.Y-1.0)+imgsize.Y + (helptext_h-5)
232 rect = core::rect<s32>(
233 screensize.X/2 - size.X/2,
234 screensize.Y/2 - size.Y/2,
235 screensize.X/2 + size.X/2,
236 screensize.Y/2 + size.Y/2
239 recalculateAbsolutePosition(false);
240 basepos = getBasePos();
243 else if(type == "list")
245 std::string name = f.next(";");
246 InventoryLocation loc;
247 if(name == "context" || name == "current_name")
248 loc = m_current_inventory_location;
250 loc.deSerialize(name);
251 std::string listname = f.next(";");
253 pos.X += stof(f.next(",")) * (float)spacing.X;
254 pos.Y += stof(f.next(";")) * (float)spacing.Y;
256 geom.X = stoi(f.next(","));
257 geom.Y = stoi(f.next(";"));
258 infostream<<"list inv="<<name<<", listname="<<listname
259 <<", pos=("<<pos.X<<","<<pos.Y<<")"
260 <<", geom=("<<geom.X<<","<<geom.Y<<")"
262 std::string start_i_s = f.next("]");
265 start_i = stoi(start_i_s);
267 errorstream<<"WARNING: invalid use of list without a size[] element"<<std::endl;
268 m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
270 else if(type == "image")
273 pos.X += stof(f.next(",")) * (float)spacing.X;
274 pos.Y += stof(f.next(";")) * (float)spacing.Y;
276 geom.X = stof(f.next(",")) * (float)imgsize.X;
277 geom.Y = stof(f.next(";")) * (float)imgsize.Y;
278 std::string name = f.next("]");
279 infostream<<"image name="<<name
280 <<", pos=("<<pos.X<<","<<pos.Y<<")"
281 <<", geom=("<<geom.X<<","<<geom.Y<<")"
284 errorstream<<"WARNING: invalid use of image without a size[] element"<<std::endl;
285 m_images.push_back(ImageDrawSpec(name, pos, geom));
287 else if(type == "item_image")
290 pos.X += stof(f.next(",")) * (float)spacing.X;
291 pos.Y += stof(f.next(";")) * (float)spacing.Y;
293 geom.X = stof(f.next(",")) * (float)imgsize.X;
294 geom.Y = stof(f.next(";")) * (float)imgsize.Y;
295 std::string name = f.next("]");
296 errorstream<<"item name="<<name
297 <<", pos=("<<pos.X<<","<<pos.Y<<")"
298 <<", geom=("<<geom.X<<","<<geom.Y<<")"
301 errorstream<<"WARNING: invalid use of item_image without a size[] element"<<std::endl;
302 m_itemimages.push_back(ImageDrawSpec(name, pos, geom));
304 else if(type == "background")
307 pos.X += stof(f.next(",")) * (float)spacing.X - ((float)spacing.X-(float)imgsize.X)/2;
308 pos.Y += stof(f.next(";")) * (float)spacing.Y - ((float)spacing.Y-(float)imgsize.Y)/2;
310 geom.X = stof(f.next(",")) * (float)spacing.X;
311 geom.Y = stof(f.next(";")) * (float)spacing.Y;
312 std::string name = f.next("]");
313 infostream<<"image name="<<name
314 <<", pos=("<<pos.X<<","<<pos.Y<<")"
315 <<", geom=("<<geom.X<<","<<geom.Y<<")"
318 errorstream<<"WARNING: invalid use of background without a size[] element"<<std::endl;
319 m_backgrounds.push_back(ImageDrawSpec(name, pos, geom));
321 else if(type == "field")
323 std::string fname = f.next(";");
324 std::string flabel = f.next(";");
326 if(fname.find(",") == std::string::npos && flabel.find(",") == std::string::npos)
330 rect = core::rect<s32>(
331 screensize.X/2 - 580/2,
332 screensize.Y/2 - 300/2,
333 screensize.X/2 + 580/2,
334 screensize.Y/2 + 300/2
337 recalculateAbsolutePosition(false);
338 basepos = getBasePos();
342 errorstream<<"WARNING: invalid use of unpositioned field in inventory"<<std::endl;
345 pos.Y = ((m_fields.size()+2)*60);
346 v2s32 size = DesiredRect.getSize();
347 rect = core::rect<s32>(size.X/2-150, pos.Y, (size.X/2-150)+300, pos.Y+30);
352 pos.X = stof(fname.substr(0,fname.find(","))) * (float)spacing.X;
353 pos.Y = stof(fname.substr(fname.find(",")+1)) * (float)spacing.Y;
355 geom.X = (stof(flabel.substr(0,flabel.find(","))) * (float)spacing.X)-(spacing.X-imgsize.X);
356 pos.Y += (stof(flabel.substr(flabel.find(",")+1)) * (float)imgsize.Y)/2;
358 rect = core::rect<s32>(pos.X, pos.Y-15, pos.X+geom.X, pos.Y+15);
361 flabel = f.next(";");
363 errorstream<<"WARNING: invalid use of positioned field without a size[] element"<<std::endl;
367 std::string odefault = f.next("]");
368 std::string fdefault;
370 // fdefault may contain a variable reference, which
371 // needs to be resolved from the node metadata
373 fdefault = m_form_src->resolveText(odefault);
377 FieldSpec spec = FieldSpec(
378 narrow_to_wide(fname.c_str()),
379 narrow_to_wide(flabel.c_str()),
380 narrow_to_wide(fdefault.c_str()),
384 // three cases: field and no label, label and no field, label and field
388 gui::IGUIElement *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
389 Environment->setFocus(e);
392 evt.EventType = EET_KEY_INPUT_EVENT;
393 evt.KeyInput.Key = KEY_END;
394 evt.KeyInput.PressedDown = true;
397 else if (fname == "")
399 // set spec field id to 0, this stops submit searching for a value that isn't there
400 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
405 gui::IGUIElement *e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
406 Environment->setFocus(e);
407 rect.UpperLeftCorner.Y -= 15;
408 rect.LowerRightCorner.Y -= 15;
409 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
412 evt.EventType = EET_KEY_INPUT_EVENT;
413 evt.KeyInput.Key = KEY_END;
414 evt.KeyInput.PressedDown = true;
418 m_fields.push_back(spec);
420 else if(type == "label")
423 pos.X += stof(f.next(",")) * (float)spacing.X;
424 pos.Y += stof(f.next(";")) * (float)spacing.Y;
426 rect = core::rect<s32>(pos.X, pos.Y+((imgsize.Y/2)-15), pos.X+300, pos.Y+((imgsize.Y/2)+15));
428 std::string flabel = f.next("]");
430 errorstream<<"WARNING: invalid use of label without a size[] element"<<std::endl;
432 FieldSpec spec = FieldSpec(
434 narrow_to_wide(flabel.c_str()),
438 Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
439 m_fields.push_back(spec);
441 else if(type == "button" || type == "button_exit")
444 pos.X += stof(f.next(",")) * (float)spacing.X;
445 pos.Y += stof(f.next(";")) * (float)spacing.Y;
447 geom.X = (stof(f.next(",")) * (float)spacing.X)-(spacing.X-imgsize.X);
448 pos.Y += (stof(f.next(";")) * (float)imgsize.Y)/2;
450 rect = core::rect<s32>(pos.X, pos.Y-15, pos.X+geom.X, pos.Y+15);
452 std::string fname = f.next(";");
453 std::string flabel = f.next("]");
455 errorstream<<"WARNING: invalid use of button without a size[] element"<<std::endl;
457 FieldSpec spec = FieldSpec(
458 narrow_to_wide(fname.c_str()),
459 narrow_to_wide(flabel.c_str()),
463 spec.is_button = true;
464 if(type == "button_exit")
466 Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
467 m_fields.push_back(spec);
469 else if(type == "image_button" || type == "image_button_exit")
472 pos.X += stof(f.next(",")) * (float)spacing.X;
473 pos.Y += stof(f.next(";")) * (float)spacing.Y;
475 geom.X = (stof(f.next(",")) * (float)spacing.X)-(spacing.X-imgsize.X);
476 geom.Y = (stof(f.next(";")) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
478 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
480 std::string fimage = f.next(";");
481 std::string fname = f.next(";");
482 std::string flabel = f.next("]");
484 errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
486 FieldSpec spec = FieldSpec(
487 narrow_to_wide(fname.c_str()),
488 narrow_to_wide(flabel.c_str()),
489 narrow_to_wide(fimage.c_str()),
492 spec.is_button = true;
493 if(type == "image_button_exit")
496 video::ITexture *texture = m_gamedef->tsrc()->getTextureRaw(fimage);
497 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
498 e->setUseAlphaChannel(true);
499 e->setImage(texture);
500 e->setPressedImage(texture);
501 e->setScaleImage(true);
503 m_fields.push_back(spec);
505 else if(type == "item_image_button")
508 pos.X += stof(f.next(",")) * (float)spacing.X;
509 pos.Y += stof(f.next(";")) * (float)spacing.Y;
511 geom.X = (stof(f.next(",")) * (float)spacing.X)-(spacing.X-imgsize.X);
512 geom.Y = (stof(f.next(";")) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
513 rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
514 std::string fimage = f.next(";");
515 std::string fname = f.next(";");
516 std::string flabel = f.next("]");
518 errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
519 IItemDefManager *idef = m_gamedef->idef();
521 item.deSerialize(fimage, idef);
522 video::ITexture *texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
523 std::string tooltip = item.getDefinition(idef).description;
524 FieldSpec spec = FieldSpec(
525 narrow_to_wide(fname.c_str()),
526 narrow_to_wide(flabel.c_str()),
527 narrow_to_wide(fimage.c_str()),
530 gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
531 e->setUseAlphaChannel(true);
532 e->setImage(texture);
533 e->setPressedImage(texture);
534 e->setScaleImage(true);
535 spec.is_button = true;
536 rect+=basepos-padding;
539 spec.tooltip=tooltip;
540 m_fields.push_back(spec);
545 std::string ts = f.next("]");
546 infostream<<"Unknown DrawSpec: type="<<type<<", data=\""<<ts<<"\""
551 // If there's inventory, put the usage string at the bottom
552 if (m_inventorylists.size())
555 core::rect<s32> rect(0, 0, size.X-padding.X*2, helptext_h);
556 rect = rect + v2s32(size.X/2 - rect.getWidth()/2,
557 size.Y-rect.getHeight()-5);
558 const wchar_t *text = wgettext("Left click: Move all items, Right click: Move single item");
559 Environment->addStaticText(text, rect, false, true, this, 256);
562 // If there's fields, add a Proceed button
563 if (m_fields.size() && bp_set != 2)
565 // if the size wasn't set by an invsize[] or size[] adjust it now to fit all the fields
566 rect = core::rect<s32>(
567 screensize.X/2 - 580/2,
568 screensize.Y/2 - 300/2,
569 screensize.X/2 + 580/2,
570 screensize.Y/2 + 240/2+(m_fields.size()*60)
573 recalculateAbsolutePosition(false);
574 basepos = getBasePos();
579 pos.Y = ((m_fields.size()+2)*60);
581 v2s32 size = DesiredRect.getSize();
582 rect = core::rect<s32>(size.X/2-70, pos.Y, (size.X/2-70)+140, pos.Y+30);
583 Environment->addButton(rect, this, 257, wgettext("Proceed"));
589 // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
590 m_tooltip_element = Environment->addStaticText(L"",core::rect<s32>(0,0,110,18));
591 m_tooltip_element->enableOverrideColor(true);
592 m_tooltip_element->setBackgroundColor(video::SColor(255,110,130,60));
593 m_tooltip_element->setDrawBackground(true);
594 m_tooltip_element->setDrawBorder(true);
595 m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
596 m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
597 m_tooltip_element->setWordWrap(false);
601 GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
603 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
605 for(u32 i=0; i<m_inventorylists.size(); i++)
607 const ListDrawSpec &s = m_inventorylists[i];
609 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
611 s32 item_i = i + s.start_item_i;
612 s32 x = (i%s.geom.X) * spacing.X;
613 s32 y = (i/s.geom.X) * spacing.Y;
615 core::rect<s32> rect = imgrect + s.pos + p0;
616 if(rect.isPointInside(p))
618 return ItemSpec(s.inventoryloc, s.listname, item_i);
623 return ItemSpec(InventoryLocation(), "", -1);
626 void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase)
628 video::IVideoDriver* driver = Environment->getVideoDriver();
631 gui::IGUIFont *font = NULL;
632 gui::IGUISkin* skin = Environment->getSkin();
634 font = skin->getFont();
636 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
638 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
639 <<"The inventory location "
640 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
644 InventoryList *ilist = inv->getList(s.listname);
646 infostream<<"GUIFormSpecMenu::drawList(): WARNING: "
647 <<"The inventory list \""<<s.listname<<"\" @ \""
648 <<s.inventoryloc.dump()<<"\" doesn't exist"
653 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
655 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
657 u32 item_i = i + s.start_item_i;
658 if(item_i >= ilist->getSize())
660 s32 x = (i%s.geom.X) * spacing.X;
661 s32 y = (i/s.geom.X) * spacing.Y;
663 core::rect<s32> rect = imgrect + s.pos + p;
666 item = ilist->getItem(item_i);
668 bool selected = m_selected_item
669 && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
670 && m_selected_item->listname == s.listname
671 && m_selected_item->i == i;
672 bool hovering = rect.isPointInside(m_pointer);
676 if(hovering && m_selected_item)
678 video::SColor bgcolor(255,192,192,192);
679 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
683 video::SColor bgcolor(255,128,128,128);
684 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
693 item.takeItem(m_selected_amount);
697 drawItemStack(driver, font, item,
698 rect, &AbsoluteClippingRect, m_gamedef);
702 std::string tooltip_text = "";
703 if(hovering && !m_selected_item)
704 tooltip_text = item.getDefinition(m_gamedef->idef()).description;
705 if(tooltip_text != "")
707 m_tooltip_element->setVisible(true);
708 this->bringToFront(m_tooltip_element);
709 m_tooltip_element->setText(narrow_to_wide(tooltip_text).c_str());
710 s32 tooltip_x = m_pointer.X + 15;
711 s32 tooltip_y = m_pointer.Y + 15;
712 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
713 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
714 m_tooltip_element->setRelativePosition(core::rect<s32>(
715 core::position2d<s32>(tooltip_x, tooltip_y),
716 core::dimension2d<s32>(tooltip_width, tooltip_height)));
722 void GUIFormSpecMenu::drawSelectedItem()
727 video::IVideoDriver* driver = Environment->getVideoDriver();
730 gui::IGUIFont *font = NULL;
731 gui::IGUISkin* skin = Environment->getSkin();
733 font = skin->getFont();
735 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
737 InventoryList *list = inv->getList(m_selected_item->listname);
739 ItemStack stack = list->getItem(m_selected_item->i);
740 stack.count = m_selected_amount;
742 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
743 core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
744 drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
747 void GUIFormSpecMenu::drawMenu()
750 std::string newform = m_form_src->getForm();
751 if(newform != m_formspec_string){
752 m_formspec_string = newform;
753 regenerateGui(m_screensize_old);
757 m_pointer = m_device->getCursorControl()->getPosition();
759 updateSelectedItem();
761 gui::IGUISkin* skin = Environment->getSkin();
764 video::IVideoDriver* driver = Environment->getVideoDriver();
766 video::SColor bgcolor(140,0,0,0);
767 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
769 m_tooltip_element->setVisible(false);
774 for(u32 i=0; i<m_backgrounds.size(); i++)
776 const ImageDrawSpec &spec = m_backgrounds[i];
777 video::ITexture *texture =
778 m_gamedef->tsrc()->getTextureRaw(spec.name);
779 // Image size on screen
780 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
781 // Image rectangle on screen
782 core::rect<s32> rect = imgrect + spec.pos;
783 const video::SColor color(255,255,255,255);
784 const video::SColor colors[] = {color,color,color,color};
785 driver->draw2DImage(texture, rect,
786 core::rect<s32>(core::position2d<s32>(0,0),
787 core::dimension2di(texture->getOriginalSize())),
788 NULL/*&AbsoluteClippingRect*/, colors, true);
794 for(u32 i=0; i<m_images.size(); i++)
796 const ImageDrawSpec &spec = m_images[i];
797 video::ITexture *texture =
798 m_gamedef->tsrc()->getTextureRaw(spec.name);
799 // Image size on screen
800 core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
801 // Image rectangle on screen
802 core::rect<s32> rect = imgrect + spec.pos;
803 const video::SColor color(255,255,255,255);
804 const video::SColor colors[] = {color,color,color,color};
805 driver->draw2DImage(texture, rect,
806 core::rect<s32>(core::position2d<s32>(0,0),
807 core::dimension2di(texture->getOriginalSize())),
808 NULL/*&AbsoluteClippingRect*/, colors, true);
813 Phase 0: Item slot rectangles
814 Phase 1: Item images; prepare tooltip
815 If backgrounds used, do not draw Item slot rectangles
818 if (m_backgrounds.size() > 0) start_phase=1;
819 for(int phase=start_phase; phase<=1; phase++)
820 for(u32 i=0; i<m_inventorylists.size(); i++)
822 drawList(m_inventorylists[i], phase);
828 gui::IGUIElement::draw();
831 Draw fields/buttons tooltips
833 for(u32 i=0; i<m_fields.size(); i++)
835 const FieldSpec &spec = m_fields[i];
836 if (spec.tooltip != "")
838 core::rect<s32> rect = spec.rect;
839 if (rect.isPointInside(m_pointer))
841 m_tooltip_element->setVisible(true);
842 this->bringToFront(m_tooltip_element);
843 m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
844 s32 tooltip_x = m_pointer.X + 15;
845 s32 tooltip_y = m_pointer.Y + 15;
846 s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
847 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
848 m_tooltip_element->setRelativePosition(core::rect<s32>(
849 core::position2d<s32>(tooltip_x, tooltip_y),
850 core::dimension2d<s32>(tooltip_width, tooltip_height)));
856 Draw dragged item stack
861 void GUIFormSpecMenu::updateSelectedItem()
863 // WARNING: BLACK MAGIC
864 // See if there is a stack suited for our current guess.
865 // If such stack does not exist, clear the guess.
866 if(m_selected_content_guess.name != "")
869 for(u32 i=0; i<m_inventorylists.size() && !found; i++){
870 const ListDrawSpec &s = m_inventorylists[i];
871 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
874 InventoryList *list = inv->getList(s.listname);
877 for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
878 u32 item_i = i + s.start_item_i;
879 if(item_i >= list->getSize())
881 ItemStack stack = list->getItem(item_i);
882 if(stack.name == m_selected_content_guess.name &&
883 stack.count == m_selected_content_guess.count){
886 // If guessed stack is already selected, all is fine
887 if(m_selected_item->inventoryloc == s.inventoryloc &&
888 m_selected_item->listname == s.listname &&
889 m_selected_item->i == (s32)item_i &&
890 m_selected_amount == stack.count){
893 delete m_selected_item;
894 m_selected_item = NULL;
896 infostream<<"Client: Changing selected content guess to "
897 <<s.inventoryloc.dump()<<" "<<s.listname
898 <<" "<<item_i<<std::endl;
899 m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
900 m_selected_amount = stack.count;
906 infostream<<"Client: Discarding selected content guess: "
907 <<m_selected_content_guess.getItemString()<<std::endl;
908 m_selected_content_guess.name = "";
911 // If the selected stack has become empty for some reason, deselect it.
912 // If the selected stack has become smaller, adjust m_selected_amount.
915 bool selection_valid = false;
916 if(m_selected_item->isValid())
918 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
921 InventoryList *list = inv->getList(m_selected_item->listname);
922 if(list && (u32) m_selected_item->i < list->getSize())
924 ItemStack stack = list->getItem(m_selected_item->i);
925 if(m_selected_amount > stack.count)
926 m_selected_amount = stack.count;
928 selection_valid = true;
934 delete m_selected_item;
935 m_selected_item = NULL;
936 m_selected_amount = 0;
937 m_selected_dragging = false;
941 // If craftresult is nonempty and nothing else is selected, select it now.
944 for(u32 i=0; i<m_inventorylists.size(); i++)
946 const ListDrawSpec &s = m_inventorylists[i];
947 if(s.listname == "craftpreview")
949 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
950 InventoryList *list = inv->getList("craftresult");
951 if(list && list->getSize() >= 1 && !list->getItem(0).empty())
953 m_selected_item = new ItemSpec;
954 m_selected_item->inventoryloc = s.inventoryloc;
955 m_selected_item->listname = "craftresult";
956 m_selected_item->i = 0;
957 m_selected_amount = 0;
958 m_selected_dragging = false;
965 // If craftresult is selected, keep the whole stack selected
966 if(m_selected_item && m_selected_item->listname == "craftresult")
968 Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
970 InventoryList *list = inv->getList(m_selected_item->listname);
972 m_selected_amount = list->getItem(m_selected_item->i).count;
976 void GUIFormSpecMenu::acceptInput()
980 std::map<std::string, std::string> fields;
982 for(u32 i=0; i<m_fields.size(); i++)
984 const FieldSpec &s = m_fields[i];
989 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(s.flabel.c_str());
993 e = getElementFromId(s.fid);
996 fields[wide_to_narrow(s.fname.c_str())] = wide_to_narrow(e->getText());
1001 m_text_dst->gotText(fields);
1005 bool GUIFormSpecMenu::OnEvent(const SEvent& event)
1007 if(event.EventType==EET_KEY_INPUT_EVENT)
1009 KeyPress kp(event.KeyInput);
1010 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
1011 kp == getKeySetting("keymap_inventory")))
1016 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
1023 if(event.EventType==EET_MOUSE_INPUT_EVENT
1024 && event.MouseInput.Event != EMIE_MOUSE_MOVED)
1026 // Mouse event other than movement
1028 // Get selected item and hovered/clicked item (s)
1030 updateSelectedItem();
1031 ItemSpec s = getItemAtPos(m_pointer);
1033 Inventory *inv_selected = NULL;
1034 Inventory *inv_s = NULL;
1038 inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
1039 assert(inv_selected);
1040 assert(inv_selected->getList(m_selected_item->listname) != NULL);
1047 inv_s = m_invmgr->getInventory(s.inventoryloc);
1050 errorstream<<"InventoryMenu: The selected inventory location "
1051 <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
1053 s.i = -1; // make it invalid again
1057 InventoryList *list = inv_s->getList(s.listname);
1059 verbosestream<<"InventoryMenu: The selected inventory list \""
1060 <<s.listname<<"\" does not exist"<<std::endl;
1061 s.i = -1; // make it invalid again
1065 if((u32)s.i >= list->getSize()){
1066 infostream<<"InventoryMenu: The selected inventory list \""
1067 <<s.listname<<"\" is too small (i="<<s.i<<", size="
1068 <<list->getSize()<<")"<<std::endl;
1069 s.i = -1; // make it invalid again
1073 s_count = list->getItem(s.i).count;
1076 bool identical = (m_selected_item != NULL) && s.isValid() &&
1077 (inv_selected == inv_s) &&
1078 (m_selected_item->listname == s.listname) &&
1079 (m_selected_item->i == s.i);
1081 // buttons: 0 = left, 1 = right, 2 = middle
1082 // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
1085 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
1086 { button = 0; updown = 0; }
1087 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
1088 { button = 1; updown = 0; }
1089 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
1090 { button = 2; updown = 0; }
1091 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
1092 { button = 0; updown = 1; }
1093 else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
1094 { button = 1; updown = 1; }
1095 else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
1096 { button = 2; updown = 1; }
1098 // Set this number to a positive value to generate a move action
1099 // from m_selected_item to s.
1100 u32 move_amount = 0;
1102 // Set this number to a positive value to generate a drop action
1103 // from m_selected_item.
1104 u32 drop_amount = 0;
1106 // Set this number to a positive value to generate a craft action at s.
1107 u32 craft_amount = 0;
1111 // Some mouse button has been pressed
1113 //infostream<<"Mouse button "<<button<<" pressed at p=("
1114 // <<p.X<<","<<p.Y<<")"<<std::endl;
1116 m_selected_dragging = false;
1118 if(s.isValid() && s.listname == "craftpreview")
1120 // Craft preview has been clicked: craft
1121 craft_amount = (button == 2 ? 10 : 1);
1123 else if(m_selected_item == NULL)
1127 // Non-empty stack has been clicked: select it
1128 m_selected_item = new ItemSpec(s);
1130 if(button == 1) // right
1131 m_selected_amount = (s_count + 1) / 2;
1132 else if(button == 2) // middle
1133 m_selected_amount = MYMIN(s_count, 10);
1135 m_selected_amount = s_count;
1137 m_selected_dragging = true;
1140 else // m_selected_item != NULL
1142 assert(m_selected_amount >= 1);
1146 // Clicked a slot: move
1147 if(button == 1) // right
1149 else if(button == 2) // middle
1150 move_amount = MYMIN(m_selected_amount, 10);
1152 move_amount = m_selected_amount;
1156 if(move_amount >= m_selected_amount)
1157 m_selected_amount = 0;
1159 m_selected_amount -= move_amount;
1163 else if(getAbsoluteClippingRect().isPointInside(m_pointer))
1165 // Clicked somewhere else: deselect
1166 m_selected_amount = 0;
1170 // Clicked outside of the window: drop
1171 if(button == 1) // right
1173 else if(button == 2) // middle
1174 drop_amount = MYMIN(m_selected_amount, 10);
1176 drop_amount = m_selected_amount;
1180 else if(updown == 1)
1182 // Some mouse button has been released
1184 //infostream<<"Mouse button "<<button<<" released at p=("
1185 // <<p.X<<","<<p.Y<<")"<<std::endl;
1187 if(m_selected_item != NULL && m_selected_dragging && s.isValid())
1191 // Dragged to different slot: move all selected
1192 move_amount = m_selected_amount;
1195 else if(m_selected_item != NULL && m_selected_dragging &&
1196 !(getAbsoluteClippingRect().isPointInside(m_pointer)))
1198 // Dragged outside of window: drop all selected
1199 drop_amount = m_selected_amount;
1202 m_selected_dragging = false;
1205 // Possibly send inventory action to server
1208 // Send IACTION_MOVE
1210 assert(m_selected_item && m_selected_item->isValid());
1211 assert(s.isValid());
1213 assert(inv_selected && inv_s);
1214 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
1215 InventoryList *list_to = inv_s->getList(s.listname);
1216 assert(list_from && list_to);
1217 ItemStack stack_from = list_from->getItem(m_selected_item->i);
1218 ItemStack stack_to = list_to->getItem(s.i);
1220 // Check how many items can be moved
1221 move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
1222 ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
1223 // If source stack cannot be added to destination stack at all,
1225 if(leftover.count == stack_from.count && leftover.name == stack_from.name)
1227 m_selected_amount = stack_to.count;
1228 // In case the server doesn't directly swap them but instead
1229 // moves stack_to somewhere else, set this
1230 m_selected_content_guess = stack_to;
1231 m_selected_content_guess_inventory = s.inventoryloc;
1233 // Source stack goes fully into destination stack
1234 else if(leftover.empty())
1236 m_selected_amount -= move_amount;
1237 m_selected_content_guess = ItemStack(); // Clear
1239 // Source stack goes partly into destination stack
1242 move_amount -= leftover.count;
1243 m_selected_amount -= move_amount;
1244 m_selected_content_guess = ItemStack(); // Clear
1247 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
1248 IMoveAction *a = new IMoveAction();
1249 a->count = move_amount;
1250 a->from_inv = m_selected_item->inventoryloc;
1251 a->from_list = m_selected_item->listname;
1252 a->from_i = m_selected_item->i;
1253 a->to_inv = s.inventoryloc;
1254 a->to_list = s.listname;
1256 m_invmgr->inventoryAction(a);
1258 else if(drop_amount > 0)
1260 m_selected_content_guess = ItemStack(); // Clear
1262 // Send IACTION_DROP
1264 assert(m_selected_item && m_selected_item->isValid());
1265 assert(inv_selected);
1266 InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
1268 ItemStack stack_from = list_from->getItem(m_selected_item->i);
1270 // Check how many items can be dropped
1271 drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
1272 assert(drop_amount > 0 && drop_amount <= m_selected_amount);
1273 m_selected_amount -= drop_amount;
1275 infostream<<"Handing IACTION_DROP to manager"<<std::endl;
1276 IDropAction *a = new IDropAction();
1277 a->count = drop_amount;
1278 a->from_inv = m_selected_item->inventoryloc;
1279 a->from_list = m_selected_item->listname;
1280 a->from_i = m_selected_item->i;
1281 m_invmgr->inventoryAction(a);
1283 else if(craft_amount > 0)
1285 m_selected_content_guess = ItemStack(); // Clear
1287 // Send IACTION_CRAFT
1289 assert(s.isValid());
1292 infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
1293 ICraftAction *a = new ICraftAction();
1294 a->count = craft_amount;
1295 a->craft_inv = s.inventoryloc;
1296 m_invmgr->inventoryAction(a);
1299 // If m_selected_amount has been decreased to zero, deselect
1300 if(m_selected_amount == 0)
1302 delete m_selected_item;
1303 m_selected_item = NULL;
1304 m_selected_amount = 0;
1305 m_selected_dragging = false;
1306 m_selected_content_guess = ItemStack();
1309 if(event.EventType==EET_GUI_EVENT)
1311 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
1314 if(!canTakeFocus(event.GUIEvent.Element))
1316 infostream<<"GUIFormSpecMenu: Not allowing focus change."
1318 // Returning true disables focus change
1322 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
1324 switch(event.GUIEvent.Caller->getID())
1329 // quitMenu deallocates menu
1332 // find the element that was clicked
1333 for(u32 i=0; i<m_fields.size(); i++)
1335 FieldSpec &s = m_fields[i];
1336 // if its a button, set the send field so
1337 // lua knows which button was pressed
1338 if (s.is_button && s.fid == event.GUIEvent.Caller->getID())
1347 // Restore focus to the full form
1348 Environment->setFocus(this);
1354 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
1356 if(event.GUIEvent.Caller->getID() > 257)
1360 // quitMenu deallocates menu
1366 return Parent ? Parent->OnEvent(event) : false;