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 driver->draw2DRectangle(video::SColor(255,255,0,0),
245 core::rect<s32>(rect.UpperLeftCorner - v2s32(2,2),
246 rect.LowerRightCorner + v2s32(2,2)),
247 &AbsoluteClippingRect);
252 drawInventoryItem(driver, font, item,
253 rect, &AbsoluteClippingRect);
257 video::SColor bgcolor(255,128,128,128);
258 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
263 void GUIInventoryMenu::drawMenu()
265 gui::IGUISkin* skin = Environment->getSkin();
268 video::IVideoDriver* driver = Environment->getVideoDriver();
270 video::SColor bgcolor(140,0,0,0);
271 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
277 for(u32 i=0; i<m_draw_spec.size(); i++)
279 ListDrawSpec &s = m_draw_spec[i];
286 gui::IGUIElement::draw();
289 bool GUIInventoryMenu::OnEvent(const SEvent& event)
291 if(event.EventType==EET_KEY_INPUT_EVENT)
293 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
298 if(event.KeyInput.Key==getKeySetting("keymap_inventory") && event.KeyInput.PressedDown)
304 if(event.EventType==EET_MOUSE_INPUT_EVENT)
306 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN
307 || event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
309 bool right = (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN);
310 v2s32 p(event.MouseInput.X, event.MouseInput.Y);
311 //dstream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
312 ItemSpec s = getItemAtPos(p);
315 dstream<<"Mouse down on "<<s.inventoryname
316 <<"/"<<s.listname<<" "<<s.i<<std::endl;
319 Inventory *inv_from = m_invmgr->getInventory(m_c,
320 m_selected_item->inventoryname);
321 Inventory *inv_to = m_invmgr->getInventory(m_c,
325 InventoryList *list_from =
326 inv_from->getList(m_selected_item->listname);
327 InventoryList *list_to =
328 inv_to->getList(s.listname);
329 if(list_from == NULL)
330 dstream<<"from list doesn't exist"<<std::endl;
332 dstream<<"to list doesn't exist"<<std::endl;
333 // Indicates whether source slot completely empties
334 bool source_empties = false;
335 if(list_from && list_to
336 && list_from->getItem(m_selected_item->i) != NULL)
338 dstream<<"Handing IACTION_MOVE to manager"<<std::endl;
339 IMoveAction *a = new IMoveAction();
340 a->count = right ? 1 : 0;
341 a->from_inv = m_selected_item->inventoryname;
342 a->from_list = m_selected_item->listname;
343 a->from_i = m_selected_item->i;
344 a->to_inv = s.inventoryname;
345 a->to_list = s.listname;
347 //ispec.actions->push_back(a);
348 m_invmgr->inventoryAction(a);
350 if(list_from->getItem(m_selected_item->i)->getCount()==1)
351 source_empties = true;
353 // Remove selection if target was left-clicked or source
355 if(right == false || source_empties)
357 delete m_selected_item;
358 m_selected_item = NULL;
366 Inventory *inv = m_invmgr->getInventory(m_c,
369 InventoryList *list = inv->getList(s.listname);
370 if(list->getItem(s.i) != NULL)
372 m_selected_item = new ItemSpec(s);
380 delete m_selected_item;
381 m_selected_item = NULL;
386 if(event.EventType==EET_GUI_EVENT)
388 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
391 if(!canTakeFocus(event.GUIEvent.Element))
393 dstream<<"GUIInventoryMenu: Not allowing focus change."
395 // Returning true disables focus change
399 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
401 /*switch(event.GUIEvent.Caller->getID())
403 case 256: // continue
413 return Parent ? Parent->OnEvent(event) : false;
417 Here is an example traditional set-up sequence for a DrawSpec list:
419 std::string furnace_inv_id = "nodemetadata:0,1,2";
420 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
421 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
422 "list", furnace_inv_id, "fuel",
423 v2s32(2, 3), v2s32(1, 1)));
424 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
425 "list", furnace_inv_id, "src",
426 v2s32(2, 1), v2s32(1, 1)));
427 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
428 "list", furnace_inv_id, "dst",
429 v2s32(5, 1), v2s32(2, 2)));
430 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
431 "list", "current_player", "main",
432 v2s32(0, 5), v2s32(8, 4)));
433 setDrawSpec(draw_spec);
435 Here is the string for creating the same DrawSpec list (a single line,
436 spread to multiple lines here):
438 GUIInventoryMenu::makeDrawSpecArrayFromString(
440 "nodemetadata:0,1,2",
442 "list[current_name;fuel;2,3;1,1;]"
443 "list[current_name;src;2,1;1,1;]"
444 "list[current_name;dst;5,1;2,2;]"
445 "list[current_player;main;0,5;8,4;]");
447 Returns inventory menu size defined by invsize[].
449 v2s16 GUIInventoryMenu::makeDrawSpecArrayFromString(
450 core::array<GUIInventoryMenu::DrawSpec> &draw_spec,
451 const std::string &data,
452 const std::string ¤t_name)
456 while(f.atend() == false)
458 std::string type = trim(f.next("["));
459 //dstream<<"type="<<type<<std::endl;
462 std::string name = f.next(";");
463 if(name == "current_name")
465 std::string subname = f.next(";");
466 s32 pos_x = stoi(f.next(","));
467 s32 pos_y = stoi(f.next(";"));
468 s32 geom_x = stoi(f.next(","));
469 s32 geom_y = stoi(f.next(";"));
470 dstream<<"list name="<<name<<", subname="<<subname
471 <<", pos=("<<pos_x<<","<<pos_y<<")"
472 <<", geom=("<<geom_x<<","<<geom_y<<")"
474 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
476 v2s32(pos_x,pos_y),v2s32(geom_x,geom_y)));
479 else if(type == "invsize")
481 invsize.X = stoi(f.next(","));
482 invsize.Y = stoi(f.next(";"));
483 dstream<<"invsize ("<<invsize.X<<","<<invsize.Y<<")"<<std::endl;
489 std::string ts = f.next("]");
490 dstream<<"Unknown DrawSpec: type="<<type<<", data=\""<<ts<<"\""