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 void drawInventoryItem(video::IVideoDriver *driver,
28 InventoryItem *item, core::rect<s32> rect,
29 const core::rect<s32> *clip)
34 video::ITexture *texture = NULL;
35 texture = item->getImage();
39 const video::SColor color(255,255,255,255);
40 const video::SColor colors[] = {color,color,color,color};
41 driver->draw2DImage(texture, rect,
42 core::rect<s32>(core::position2d<s32>(0,0),
43 core::dimension2di(texture->getOriginalSize())),
48 video::SColor bgcolor(255,50,50,128);
49 driver->draw2DRectangle(bgcolor, rect, clip);
54 std::string text = item->getText();
55 if(font && text != "")
57 v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
58 v2s32 sdim(dim.X,dim.Y);
60 core::rect<s32> rect2(
61 /*rect.UpperLeftCorner,
62 core::dimension2d<u32>(rect.getWidth(), 15)*/
63 rect.LowerRightCorner - sdim,
67 video::SColor bgcolor(128,0,0,0);
68 driver->draw2DRectangle(bgcolor, rect2, clip);
70 font->draw(text.c_str(), rect2,
71 video::SColor(255,255,255,255), false, false,
81 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
82 gui::IGUIElement* parent, s32 id,
83 IMenuManager *menumgr,
86 InventoryManager *invmgr
88 GUIModalMenu(env, parent, id, menumgr),
89 m_menu_size(menu_size),
93 m_selected_item = NULL;
96 GUIInventoryMenu::~GUIInventoryMenu()
101 delete m_selected_item;
104 void GUIInventoryMenu::removeChildren()
106 const core::list<gui::IGUIElement*> &children = getChildren();
107 core::list<gui::IGUIElement*> children_copy;
108 for(core::list<gui::IGUIElement*>::ConstIterator
109 i = children.begin(); i != children.end(); i++)
111 children_copy.push_back(*i);
113 for(core::list<gui::IGUIElement*>::Iterator
114 i = children_copy.begin();
115 i != children_copy.end(); i++)
120 gui::IGUIElement *e = getElementFromId(256);
126 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
131 /*padding = v2s32(24,24);
132 spacing = v2s32(60,56);
133 imgsize = v2s32(48,48);*/
135 padding = v2s32(screensize.Y/40, screensize.Y/40);
136 spacing = v2s32(screensize.Y/12, screensize.Y/13);
137 imgsize = v2s32(screensize.Y/15, screensize.Y/15);
142 padding.X*2+spacing.X*(m_menu_size.X-1)+imgsize.X,
143 padding.Y*2+spacing.Y*(m_menu_size.Y-1)+imgsize.Y + helptext_h
146 core::rect<s32> rect(
147 screensize.X/2 - size.X/2,
148 screensize.Y/2 - size.Y/2,
149 screensize.X/2 + size.X/2,
150 screensize.Y/2 + size.Y/2
154 recalculateAbsolutePosition(false);
156 v2s32 basepos = getBasePos();
159 for(u16 i=0; i<m_init_draw_spec.size(); i++)
161 DrawSpec &s = m_init_draw_spec[i];
164 m_draw_spec.push_back(ListDrawSpec(s.name, s.subname,
165 basepos + v2s32(spacing.X*s.pos.X, spacing.Y*s.pos.Y),
172 m_draw_spec.push_back(ListDrawSpec("main",
173 basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
174 m_draw_spec.push_back(ListDrawSpec("craft",
175 basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
176 m_draw_spec.push_back(ListDrawSpec("craftresult",
177 basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
182 core::rect<s32> rect(0, 0, size.X-padding.X*2, helptext_h);
183 rect = rect + v2s32(size.X/2 - rect.getWidth()/2,
184 size.Y-rect.getHeight()-15);
185 const wchar_t *text =
186 L"Left click: Move all items, Right click: Move single item";
187 Environment->addStaticText(text, rect, false, true, this, 256);
191 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
193 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
195 for(u32 i=0; i<m_draw_spec.size(); i++)
197 const ListDrawSpec &s = m_draw_spec[i];
199 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
201 s32 x = (i%s.geom.X) * spacing.X;
202 s32 y = (i/s.geom.X) * spacing.Y;
204 core::rect<s32> rect = imgrect + s.pos + p0;
205 if(rect.isPointInside(p))
207 return ItemSpec(s.inventoryname, s.listname, i);
212 return ItemSpec("", "", -1);
215 void GUIInventoryMenu::drawList(const ListDrawSpec &s)
217 video::IVideoDriver* driver = Environment->getVideoDriver();
220 gui::IGUIFont *font = NULL;
221 gui::IGUISkin* skin = Environment->getSkin();
223 font = skin->getFont();
225 Inventory *inv = m_invmgr->getInventory(m_c, s.inventoryname);
227 InventoryList *ilist = inv->getList(s.listname);
229 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
231 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
233 s32 x = (i%s.geom.X) * spacing.X;
234 s32 y = (i/s.geom.X) * spacing.Y;
236 core::rect<s32> rect = imgrect + s.pos + p;
237 InventoryItem *item = NULL;
239 item = ilist->getItem(i);
241 if(m_selected_item != NULL && m_selected_item->listname == s.listname
242 && m_selected_item->i == i)
244 /*s32 border = imgsize.X/12;
245 driver->draw2DRectangle(video::SColor(255,192,192,192),
246 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*border,
247 rect.LowerRightCorner + v2s32(1,1)*border),
249 driver->draw2DRectangle(video::SColor(255,0,0,0),
250 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*((border+1)/2),
251 rect.LowerRightCorner + v2s32(1,1)*((border+1)/2)),
254 driver->draw2DRectangle(video::SColor(255,255,0,0),
255 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*border,
256 rect.LowerRightCorner + v2s32(1,1)*border),
257 &AbsoluteClippingRect);
260 video::SColor bgcolor(255,128,128,128);
261 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
265 drawInventoryItem(driver, font, item,
266 rect, &AbsoluteClippingRect);
272 void GUIInventoryMenu::drawMenu()
274 gui::IGUISkin* skin = Environment->getSkin();
277 video::IVideoDriver* driver = Environment->getVideoDriver();
279 video::SColor bgcolor(140,0,0,0);
280 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
286 for(u32 i=0; i<m_draw_spec.size(); i++)
288 ListDrawSpec &s = m_draw_spec[i];
295 gui::IGUIElement::draw();
298 bool GUIInventoryMenu::OnEvent(const SEvent& event)
300 if(event.EventType==EET_KEY_INPUT_EVENT)
302 KeyPress kp(event.KeyInput);
303 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
304 kp == getKeySetting("keymap_inventory")))
310 if(event.EventType==EET_MOUSE_INPUT_EVENT)
314 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
316 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
318 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
323 v2s32 p(event.MouseInput.X, event.MouseInput.Y);
324 //dstream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
325 ItemSpec s = getItemAtPos(p);
328 dstream<<"Mouse down on "<<s.inventoryname
329 <<"/"<<s.listname<<" "<<s.i<<std::endl;
332 Inventory *inv_from = m_invmgr->getInventory(m_c,
333 m_selected_item->inventoryname);
334 Inventory *inv_to = m_invmgr->getInventory(m_c,
338 InventoryList *list_from =
339 inv_from->getList(m_selected_item->listname);
340 InventoryList *list_to =
341 inv_to->getList(s.listname);
342 if(list_from == NULL)
343 dstream<<"from list doesn't exist"<<std::endl;
345 dstream<<"to list doesn't exist"<<std::endl;
346 // Indicates whether source slot completely empties
347 bool source_empties = false;
348 if(list_from && list_to
349 && list_from->getItem(m_selected_item->i) != NULL)
351 dstream<<"Handing IACTION_MOVE to manager"<<std::endl;
352 IMoveAction *a = new IMoveAction();
354 a->from_inv = m_selected_item->inventoryname;
355 a->from_list = m_selected_item->listname;
356 a->from_i = m_selected_item->i;
357 a->to_inv = s.inventoryname;
358 a->to_list = s.listname;
360 //ispec.actions->push_back(a);
361 m_invmgr->inventoryAction(a);
363 if(list_from->getItem(m_selected_item->i)->getCount()==1)
364 source_empties = true;
366 // Remove selection if target was left-clicked or source
368 if(amount == 0 || source_empties)
370 delete m_selected_item;
371 m_selected_item = NULL;
379 Inventory *inv = m_invmgr->getInventory(m_c,
382 InventoryList *list = inv->getList(s.listname);
383 if(list->getItem(s.i) != NULL)
385 m_selected_item = new ItemSpec(s);
393 delete m_selected_item;
394 m_selected_item = NULL;
399 if(event.EventType==EET_GUI_EVENT)
401 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
404 if(!canTakeFocus(event.GUIEvent.Element))
406 dstream<<"GUIInventoryMenu: Not allowing focus change."
408 // Returning true disables focus change
412 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
414 /*switch(event.GUIEvent.Caller->getID())
416 case 256: // continue
426 return Parent ? Parent->OnEvent(event) : false;
430 Here is an example traditional set-up sequence for a DrawSpec list:
432 std::string furnace_inv_id = "nodemetadata:0,1,2";
433 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
434 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
435 "list", furnace_inv_id, "fuel",
436 v2s32(2, 3), v2s32(1, 1)));
437 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
438 "list", furnace_inv_id, "src",
439 v2s32(2, 1), v2s32(1, 1)));
440 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
441 "list", furnace_inv_id, "dst",
442 v2s32(5, 1), v2s32(2, 2)));
443 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
444 "list", "current_player", "main",
445 v2s32(0, 5), v2s32(8, 4)));
446 setDrawSpec(draw_spec);
448 Here is the string for creating the same DrawSpec list (a single line,
449 spread to multiple lines here):
451 GUIInventoryMenu::makeDrawSpecArrayFromString(
453 "nodemetadata:0,1,2",
455 "list[current_name;fuel;2,3;1,1;]"
456 "list[current_name;src;2,1;1,1;]"
457 "list[current_name;dst;5,1;2,2;]"
458 "list[current_player;main;0,5;8,4;]");
460 Returns inventory menu size defined by invsize[].
462 v2s16 GUIInventoryMenu::makeDrawSpecArrayFromString(
463 core::array<GUIInventoryMenu::DrawSpec> &draw_spec,
464 const std::string &data,
465 const std::string ¤t_name)
469 while(f.atend() == false)
471 std::string type = trim(f.next("["));
472 //dstream<<"type="<<type<<std::endl;
475 std::string name = f.next(";");
476 if(name == "current_name")
478 std::string subname = f.next(";");
479 s32 pos_x = stoi(f.next(","));
480 s32 pos_y = stoi(f.next(";"));
481 s32 geom_x = stoi(f.next(","));
482 s32 geom_y = stoi(f.next(";"));
483 dstream<<"list name="<<name<<", subname="<<subname
484 <<", pos=("<<pos_x<<","<<pos_y<<")"
485 <<", geom=("<<geom_x<<","<<geom_y<<")"
487 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
489 v2s32(pos_x,pos_y),v2s32(geom_x,geom_y)));
492 else if(type == "invsize")
494 invsize.X = stoi(f.next(","));
495 invsize.Y = stoi(f.next(";"));
496 dstream<<"invsize ("<<invsize.X<<","<<invsize.Y<<")"<<std::endl;
502 std::string ts = f.next("]");
503 dstream<<"Unknown DrawSpec: type="<<type<<", data=\""<<ts<<"\""