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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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 "guiInventoryMenu.h"
22 #include "constants.h"
26 #include <IGUICheckBox.h>
27 #include <IGUIEditBox.h>
28 #include <IGUIButton.h>
29 #include <IGUIStaticText.h>
33 void drawItemStack(video::IVideoDriver *driver,
35 const ItemStack &item,
36 const core::rect<s32> &rect,
37 const core::rect<s32> *clip,
43 const ItemDefinition &def = item.getDefinition(gamedef->idef());
44 video::ITexture *texture = def.inventory_texture;
46 // Draw the inventory texture
49 const video::SColor color(255,255,255,255);
50 const video::SColor colors[] = {color,color,color,color};
51 driver->draw2DImage(texture, rect,
52 core::rect<s32>(core::position2d<s32>(0,0),
53 core::dimension2di(texture->getOriginalSize())),
57 if(def.type == ITEM_TOOL && item.wear != 0)
60 float barheight = rect.getHeight()/16;
61 float barpad_x = rect.getWidth()/16;
62 float barpad_y = rect.getHeight()/16;
63 core::rect<s32> progressrect(
64 rect.UpperLeftCorner.X + barpad_x,
65 rect.LowerRightCorner.Y - barpad_y - barheight,
66 rect.LowerRightCorner.X - barpad_x,
67 rect.LowerRightCorner.Y - barpad_y);
69 // Shrink progressrect by amount of tool damage
70 float wear = item.wear / 65535.0;
72 wear * progressrect.UpperLeftCorner.X +
73 (1-wear) * progressrect.LowerRightCorner.X;
75 // Compute progressbar color
79 video::SColor color(255,255,255,255);
80 int wear_i = MYMIN(floor(wear * 600), 511);
81 wear_i = MYMIN(wear_i + 10, 511);
83 color.set(255, wear_i, 255, 0);
85 color.set(255, 255, 511-wear_i, 0);
87 core::rect<s32> progressrect2 = progressrect;
88 progressrect2.LowerRightCorner.X = progressmid;
89 driver->draw2DRectangle(color, progressrect2, clip);
91 color = video::SColor(255,0,0,0);
92 progressrect2 = progressrect;
93 progressrect2.UpperLeftCorner.X = progressmid;
94 driver->draw2DRectangle(color, progressrect2, clip);
97 if(font != NULL && item.count >= 2)
99 // Get the item count as a string
100 std::string text = itos(item.count);
101 v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
102 v2s32 sdim(dim.X,dim.Y);
104 core::rect<s32> rect2(
105 /*rect.UpperLeftCorner,
106 core::dimension2d<u32>(rect.getWidth(), 15)*/
107 rect.LowerRightCorner - sdim,
111 video::SColor bgcolor(128,0,0,0);
112 driver->draw2DRectangle(bgcolor, rect2, clip);
114 video::SColor color(255,255,255,255);
115 font->draw(text.c_str(), rect2, color, false, false, clip);
123 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
124 gui::IGUIElement* parent, s32 id,
125 IMenuManager *menumgr,
127 InventoryManager *invmgr,
130 GUIModalMenu(env, parent, id, menumgr),
131 m_menu_size(menu_size),
135 m_selected_item = NULL;
138 GUIInventoryMenu::~GUIInventoryMenu()
143 delete m_selected_item;
146 void GUIInventoryMenu::removeChildren()
148 const core::list<gui::IGUIElement*> &children = getChildren();
149 core::list<gui::IGUIElement*> children_copy;
150 for(core::list<gui::IGUIElement*>::ConstIterator
151 i = children.begin(); i != children.end(); i++)
153 children_copy.push_back(*i);
155 for(core::list<gui::IGUIElement*>::Iterator
156 i = children_copy.begin();
157 i != children_copy.end(); i++)
162 gui::IGUIElement *e = getElementFromId(256);
168 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
173 /*padding = v2s32(24,24);
174 spacing = v2s32(60,56);
175 imgsize = v2s32(48,48);*/
177 padding = v2s32(screensize.Y/40, screensize.Y/40);
178 spacing = v2s32(screensize.Y/12, screensize.Y/13);
179 imgsize = v2s32(screensize.Y/15, screensize.Y/15);
184 padding.X*2+spacing.X*(m_menu_size.X-1)+imgsize.X,
185 padding.Y*2+spacing.Y*(m_menu_size.Y-1)+imgsize.Y + helptext_h
188 core::rect<s32> rect(
189 screensize.X/2 - size.X/2,
190 screensize.Y/2 - size.Y/2,
191 screensize.X/2 + size.X/2,
192 screensize.Y/2 + size.Y/2
196 recalculateAbsolutePosition(false);
198 v2s32 basepos = getBasePos();
201 for(u16 i=0; i<m_init_draw_spec.size(); i++)
203 DrawSpec &s = m_init_draw_spec[i];
206 m_draw_spec.push_back(ListDrawSpec(s.name, s.subname,
207 basepos + v2s32(spacing.X*s.pos.X, spacing.Y*s.pos.Y),
214 m_draw_spec.push_back(ListDrawSpec("main",
215 basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
216 m_draw_spec.push_back(ListDrawSpec("craft",
217 basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
218 m_draw_spec.push_back(ListDrawSpec("craftresult",
219 basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
224 core::rect<s32> rect(0, 0, size.X-padding.X*2, helptext_h);
225 rect = rect + v2s32(size.X/2 - rect.getWidth()/2,
226 size.Y-rect.getHeight()-15);
227 const wchar_t *text =
228 L"Left click: Move all items, Right click: Move single item";
229 Environment->addStaticText(text, rect, false, true, this, 256);
233 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
235 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
237 for(u32 i=0; i<m_draw_spec.size(); i++)
239 const ListDrawSpec &s = m_draw_spec[i];
241 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
243 s32 x = (i%s.geom.X) * spacing.X;
244 s32 y = (i/s.geom.X) * spacing.Y;
246 core::rect<s32> rect = imgrect + s.pos + p0;
247 if(rect.isPointInside(p))
249 return ItemSpec(s.inventoryloc, s.listname, i);
254 return ItemSpec(InventoryLocation(), "", -1);
257 void GUIInventoryMenu::drawList(const ListDrawSpec &s)
259 video::IVideoDriver* driver = Environment->getVideoDriver();
262 gui::IGUIFont *font = NULL;
263 gui::IGUISkin* skin = Environment->getSkin();
265 font = skin->getFont();
267 Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
269 InventoryList *ilist = inv->getList(s.listname);
271 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
273 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
275 s32 x = (i%s.geom.X) * spacing.X;
276 s32 y = (i/s.geom.X) * spacing.Y;
278 core::rect<s32> rect = imgrect + s.pos + p;
281 item = ilist->getItem(i);
283 if(m_selected_item != NULL && m_selected_item->listname == s.listname
284 && m_selected_item->i == i)
286 /*s32 border = imgsize.X/12;
287 driver->draw2DRectangle(video::SColor(255,192,192,192),
288 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*border,
289 rect.LowerRightCorner + v2s32(1,1)*border),
291 driver->draw2DRectangle(video::SColor(255,0,0,0),
292 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*((border+1)/2),
293 rect.LowerRightCorner + v2s32(1,1)*((border+1)/2)),
296 driver->draw2DRectangle(video::SColor(255,255,0,0),
297 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*border,
298 rect.LowerRightCorner + v2s32(1,1)*border),
299 &AbsoluteClippingRect);
302 if(rect.isPointInside(m_pointer) && m_selected_item)
304 video::SColor bgcolor(255,192,192,192);
305 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
309 video::SColor bgcolor(255,128,128,128);
310 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
315 drawItemStack(driver, font, item,
316 rect, &AbsoluteClippingRect, m_gamedef);
322 void GUIInventoryMenu::drawMenu()
324 gui::IGUISkin* skin = Environment->getSkin();
327 video::IVideoDriver* driver = Environment->getVideoDriver();
329 video::SColor bgcolor(140,0,0,0);
330 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
336 for(u32 i=0; i<m_draw_spec.size(); i++)
338 drawList(m_draw_spec[i]);
344 gui::IGUIElement::draw();
347 bool GUIInventoryMenu::OnEvent(const SEvent& event)
349 if(event.EventType==EET_KEY_INPUT_EVENT)
351 KeyPress kp(event.KeyInput);
352 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
353 kp == getKeySetting("keymap_inventory")))
359 if(event.EventType==EET_MOUSE_INPUT_EVENT)
363 v2s32 p(event.MouseInput.X, event.MouseInput.Y);
364 ItemSpec s = getItemAtPos(p);
366 if(event.MouseInput.Event==EMIE_MOUSE_MOVED)
367 m_pointer = v2s32(event.MouseInput.X, event.MouseInput.Y);
368 else if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
370 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
372 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
374 else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP &&
376 (m_selected_item->listname != s.listname
377 || m_selected_item->i != s.i))
383 //infostream<<"Mouse action at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
386 infostream<<"Mouse action on "<<s.inventoryloc.dump()
387 <<"/"<<s.listname<<" "<<s.i<<std::endl;
390 Inventory *inv_from = m_invmgr->getInventory(
391 m_selected_item->inventoryloc);
392 Inventory *inv_to = m_invmgr->getInventory(
396 InventoryList *list_from =
397 inv_from->getList(m_selected_item->listname);
398 InventoryList *list_to =
399 inv_to->getList(s.listname);
400 if(list_from == NULL)
401 infostream<<"from list doesn't exist"<<std::endl;
403 infostream<<"to list doesn't exist"<<std::endl;
404 // Indicates whether source slot completely empties
405 bool source_empties = false;
406 if(list_from && list_to
407 && !list_from->getItem(m_selected_item->i).empty())
409 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
410 IMoveAction *a = new IMoveAction();
412 a->from_inv = m_selected_item->inventoryloc;
413 a->from_list = m_selected_item->listname;
414 a->from_i = m_selected_item->i;
415 a->to_inv = s.inventoryloc;
416 a->to_list = s.listname;
418 //ispec.actions->push_back(a);
419 m_invmgr->inventoryAction(a);
421 if(list_from->getItem(m_selected_item->i).count<=amount)
422 source_empties = true;
424 // Remove selection if target was left-clicked or source
426 if(amount == 0 || source_empties)
428 delete m_selected_item;
429 m_selected_item = NULL;
437 Inventory *inv = m_invmgr->getInventory(
440 InventoryList *list = inv->getList(s.listname);
441 if(!list->getItem(s.i).empty())
443 m_selected_item = new ItemSpec(s);
451 delete m_selected_item;
452 m_selected_item = NULL;
457 if(event.EventType==EET_GUI_EVENT)
459 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
462 if(!canTakeFocus(event.GUIEvent.Element))
464 infostream<<"GUIInventoryMenu: Not allowing focus change."
466 // Returning true disables focus change
470 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
472 /*switch(event.GUIEvent.Caller->getID())
474 case 256: // continue
484 return Parent ? Parent->OnEvent(event) : false;
488 Here is an example traditional set-up sequence for a DrawSpec list:
490 std::string furnace_inv_id = "nodemetadata:0,1,2";
491 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
492 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
493 "list", furnace_inv_id, "fuel",
494 v2s32(2, 3), v2s32(1, 1)));
495 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
496 "list", furnace_inv_id, "src",
497 v2s32(2, 1), v2s32(1, 1)));
498 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
499 "list", furnace_inv_id, "dst",
500 v2s32(5, 1), v2s32(2, 2)));
501 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
502 "list", "current_player", "main",
503 v2s32(0, 5), v2s32(8, 4)));
504 setDrawSpec(draw_spec);
506 Here is the string for creating the same DrawSpec list (a single line,
507 spread to multiple lines here):
509 GUIInventoryMenu::makeDrawSpecArrayFromString(
511 "nodemetadata:0,1,2",
513 "list[current_name;fuel;2,3;1,1;]"
514 "list[current_name;src;2,1;1,1;]"
515 "list[current_name;dst;5,1;2,2;]"
516 "list[current_player;main;0,5;8,4;]");
518 Returns inventory menu size defined by invsize[].
520 v2s16 GUIInventoryMenu::makeDrawSpecArrayFromString(
521 core::array<GUIInventoryMenu::DrawSpec> &draw_spec,
522 const std::string &data,
523 const InventoryLocation ¤t_location)
527 while(f.atend() == false)
529 std::string type = trim(f.next("["));
530 //infostream<<"type="<<type<<std::endl;
533 std::string name = f.next(";");
534 InventoryLocation loc;
535 if(name == "current_name")
536 loc = current_location;
538 loc.deSerialize(name);
539 std::string subname = f.next(";");
540 s32 pos_x = stoi(f.next(","));
541 s32 pos_y = stoi(f.next(";"));
542 s32 geom_x = stoi(f.next(","));
543 s32 geom_y = stoi(f.next(";"));
544 infostream<<"list name="<<name<<", subname="<<subname
545 <<", pos=("<<pos_x<<","<<pos_y<<")"
546 <<", geom=("<<geom_x<<","<<geom_y<<")"
548 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
550 v2s32(pos_x,pos_y),v2s32(geom_x,geom_y)));
553 else if(type == "invsize")
555 invsize.X = stoi(f.next(","));
556 invsize.Y = stoi(f.next(";"));
557 infostream<<"invsize ("<<invsize.X<<","<<invsize.Y<<")"<<std::endl;
563 std::string ts = f.next("]");
564 infostream<<"Unknown DrawSpec: type="<<type<<", data=\""<<ts<<"\""