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"
25 #include <IGUICheckBox.h>
26 #include <IGUIEditBox.h>
27 #include <IGUIButton.h>
28 #include <IGUIStaticText.h>
32 void drawInventoryItem(video::IVideoDriver *driver,
34 InventoryItem *item, core::rect<s32> rect,
35 const core::rect<s32> *clip)
40 video::ITexture *texture = NULL;
41 texture = item->getImage();
45 const video::SColor color(255,255,255,255);
46 const video::SColor colors[] = {color,color,color,color};
47 driver->draw2DImage(texture, rect,
48 core::rect<s32>(core::position2d<s32>(0,0),
49 core::dimension2di(texture->getOriginalSize())),
54 video::SColor bgcolor(255,50,50,128);
55 driver->draw2DRectangle(bgcolor, rect, clip);
60 std::string text = item->getText();
61 if(font && text != "")
63 v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
64 v2s32 sdim(dim.X,dim.Y);
66 core::rect<s32> rect2(
67 /*rect.UpperLeftCorner,
68 core::dimension2d<u32>(rect.getWidth(), 15)*/
69 rect.LowerRightCorner - sdim,
73 video::SColor bgcolor(128,0,0,0);
74 driver->draw2DRectangle(bgcolor, rect2, clip);
76 font->draw(text.c_str(), rect2,
77 video::SColor(255,255,255,255), false, false,
87 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
88 gui::IGUIElement* parent, s32 id,
89 IMenuManager *menumgr,
92 InventoryManager *invmgr
94 GUIModalMenu(env, parent, id, menumgr),
95 m_menu_size(menu_size),
99 m_selected_item = NULL;
102 GUIInventoryMenu::~GUIInventoryMenu()
107 delete m_selected_item;
110 void GUIInventoryMenu::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);
132 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
137 /*padding = v2s32(24,24);
138 spacing = v2s32(60,56);
139 imgsize = v2s32(48,48);*/
141 padding = v2s32(screensize.Y/40, screensize.Y/40);
142 spacing = v2s32(screensize.Y/12, screensize.Y/13);
143 imgsize = v2s32(screensize.Y/15, screensize.Y/15);
148 padding.X*2+spacing.X*(m_menu_size.X-1)+imgsize.X,
149 padding.Y*2+spacing.Y*(m_menu_size.Y-1)+imgsize.Y + helptext_h
152 core::rect<s32> rect(
153 screensize.X/2 - size.X/2,
154 screensize.Y/2 - size.Y/2,
155 screensize.X/2 + size.X/2,
156 screensize.Y/2 + size.Y/2
160 recalculateAbsolutePosition(false);
162 v2s32 basepos = getBasePos();
165 for(u16 i=0; i<m_init_draw_spec.size(); i++)
167 DrawSpec &s = m_init_draw_spec[i];
170 m_draw_spec.push_back(ListDrawSpec(s.name, s.subname,
171 basepos + v2s32(spacing.X*s.pos.X, spacing.Y*s.pos.Y),
178 m_draw_spec.push_back(ListDrawSpec("main",
179 basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
180 m_draw_spec.push_back(ListDrawSpec("craft",
181 basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
182 m_draw_spec.push_back(ListDrawSpec("craftresult",
183 basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
188 core::rect<s32> rect(0, 0, size.X-padding.X*2, helptext_h);
189 rect = rect + v2s32(size.X/2 - rect.getWidth()/2,
190 size.Y-rect.getHeight()-15);
191 const wchar_t *text =
192 L"Left click: Move all items, Right click: Move single item";
193 Environment->addStaticText(text, rect, false, true, this, 256);
197 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
199 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
201 for(u32 i=0; i<m_draw_spec.size(); i++)
203 const ListDrawSpec &s = m_draw_spec[i];
205 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
207 s32 x = (i%s.geom.X) * spacing.X;
208 s32 y = (i/s.geom.X) * spacing.Y;
210 core::rect<s32> rect = imgrect + s.pos + p0;
211 if(rect.isPointInside(p))
213 return ItemSpec(s.inventoryname, s.listname, i);
218 return ItemSpec("", "", -1);
221 void GUIInventoryMenu::drawList(const ListDrawSpec &s)
223 video::IVideoDriver* driver = Environment->getVideoDriver();
226 gui::IGUIFont *font = NULL;
227 gui::IGUISkin* skin = Environment->getSkin();
229 font = skin->getFont();
231 Inventory *inv = m_invmgr->getInventory(m_c, s.inventoryname);
233 InventoryList *ilist = inv->getList(s.listname);
235 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
237 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
239 s32 x = (i%s.geom.X) * spacing.X;
240 s32 y = (i/s.geom.X) * spacing.Y;
242 core::rect<s32> rect = imgrect + s.pos + p;
243 InventoryItem *item = NULL;
245 item = ilist->getItem(i);
247 if(m_selected_item != NULL && m_selected_item->listname == s.listname
248 && m_selected_item->i == i)
250 /*s32 border = imgsize.X/12;
251 driver->draw2DRectangle(video::SColor(255,192,192,192),
252 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*border,
253 rect.LowerRightCorner + v2s32(1,1)*border),
255 driver->draw2DRectangle(video::SColor(255,0,0,0),
256 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*((border+1)/2),
257 rect.LowerRightCorner + v2s32(1,1)*((border+1)/2)),
260 driver->draw2DRectangle(video::SColor(255,255,0,0),
261 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*border,
262 rect.LowerRightCorner + v2s32(1,1)*border),
263 &AbsoluteClippingRect);
266 video::SColor bgcolor(255,128,128,128);
267 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
271 drawInventoryItem(driver, font, item,
272 rect, &AbsoluteClippingRect);
278 void GUIInventoryMenu::drawMenu()
280 gui::IGUISkin* skin = Environment->getSkin();
283 video::IVideoDriver* driver = Environment->getVideoDriver();
285 video::SColor bgcolor(140,0,0,0);
286 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
292 for(u32 i=0; i<m_draw_spec.size(); i++)
294 ListDrawSpec &s = m_draw_spec[i];
301 gui::IGUIElement::draw();
304 bool GUIInventoryMenu::OnEvent(const SEvent& event)
306 if(event.EventType==EET_KEY_INPUT_EVENT)
308 KeyPress kp(event.KeyInput);
309 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
310 kp == getKeySetting("keymap_inventory")))
316 if(event.EventType==EET_MOUSE_INPUT_EVENT)
320 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
322 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
324 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
329 v2s32 p(event.MouseInput.X, event.MouseInput.Y);
330 //infostream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
331 ItemSpec s = getItemAtPos(p);
334 infostream<<"Mouse down on "<<s.inventoryname
335 <<"/"<<s.listname<<" "<<s.i<<std::endl;
338 Inventory *inv_from = m_invmgr->getInventory(m_c,
339 m_selected_item->inventoryname);
340 Inventory *inv_to = m_invmgr->getInventory(m_c,
344 InventoryList *list_from =
345 inv_from->getList(m_selected_item->listname);
346 InventoryList *list_to =
347 inv_to->getList(s.listname);
348 if(list_from == NULL)
349 infostream<<"from list doesn't exist"<<std::endl;
351 infostream<<"to list doesn't exist"<<std::endl;
352 // Indicates whether source slot completely empties
353 bool source_empties = false;
354 if(list_from && list_to
355 && list_from->getItem(m_selected_item->i) != NULL)
357 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
358 IMoveAction *a = new IMoveAction();
360 a->from_inv = m_selected_item->inventoryname;
361 a->from_list = m_selected_item->listname;
362 a->from_i = m_selected_item->i;
363 a->to_inv = s.inventoryname;
364 a->to_list = s.listname;
366 //ispec.actions->push_back(a);
367 m_invmgr->inventoryAction(a);
369 if(list_from->getItem(m_selected_item->i)->getCount()==1)
370 source_empties = true;
372 // Remove selection if target was left-clicked or source
374 if(amount == 0 || source_empties)
376 delete m_selected_item;
377 m_selected_item = NULL;
385 Inventory *inv = m_invmgr->getInventory(m_c,
388 InventoryList *list = inv->getList(s.listname);
389 if(list->getItem(s.i) != NULL)
391 m_selected_item = new ItemSpec(s);
399 delete m_selected_item;
400 m_selected_item = NULL;
405 if(event.EventType==EET_GUI_EVENT)
407 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
410 if(!canTakeFocus(event.GUIEvent.Element))
412 infostream<<"GUIInventoryMenu: Not allowing focus change."
414 // Returning true disables focus change
418 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
420 /*switch(event.GUIEvent.Caller->getID())
422 case 256: // continue
432 return Parent ? Parent->OnEvent(event) : false;
436 Here is an example traditional set-up sequence for a DrawSpec list:
438 std::string furnace_inv_id = "nodemetadata:0,1,2";
439 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
440 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
441 "list", furnace_inv_id, "fuel",
442 v2s32(2, 3), v2s32(1, 1)));
443 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
444 "list", furnace_inv_id, "src",
445 v2s32(2, 1), v2s32(1, 1)));
446 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
447 "list", furnace_inv_id, "dst",
448 v2s32(5, 1), v2s32(2, 2)));
449 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
450 "list", "current_player", "main",
451 v2s32(0, 5), v2s32(8, 4)));
452 setDrawSpec(draw_spec);
454 Here is the string for creating the same DrawSpec list (a single line,
455 spread to multiple lines here):
457 GUIInventoryMenu::makeDrawSpecArrayFromString(
459 "nodemetadata:0,1,2",
461 "list[current_name;fuel;2,3;1,1;]"
462 "list[current_name;src;2,1;1,1;]"
463 "list[current_name;dst;5,1;2,2;]"
464 "list[current_player;main;0,5;8,4;]");
466 Returns inventory menu size defined by invsize[].
468 v2s16 GUIInventoryMenu::makeDrawSpecArrayFromString(
469 core::array<GUIInventoryMenu::DrawSpec> &draw_spec,
470 const std::string &data,
471 const std::string ¤t_name)
475 while(f.atend() == false)
477 std::string type = trim(f.next("["));
478 //infostream<<"type="<<type<<std::endl;
481 std::string name = f.next(";");
482 if(name == "current_name")
484 std::string subname = f.next(";");
485 s32 pos_x = stoi(f.next(","));
486 s32 pos_y = stoi(f.next(";"));
487 s32 geom_x = stoi(f.next(","));
488 s32 geom_y = stoi(f.next(";"));
489 infostream<<"list name="<<name<<", subname="<<subname
490 <<", pos=("<<pos_x<<","<<pos_y<<")"
491 <<", geom=("<<geom_x<<","<<geom_y<<")"
493 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
495 v2s32(pos_x,pos_y),v2s32(geom_x,geom_y)));
498 else if(type == "invsize")
500 invsize.X = stoi(f.next(","));
501 invsize.Y = stoi(f.next(";"));
502 infostream<<"invsize ("<<invsize.X<<","<<invsize.Y<<")"<<std::endl;
508 std::string ts = f.next("]");
509 infostream<<"Unknown DrawSpec: type="<<type<<", data=\""<<ts<<"\""