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,
41 video::ITexture *texture = NULL;
42 texture = item->getImage(tsrc);
46 const video::SColor color(255,255,255,255);
47 const video::SColor colors[] = {color,color,color,color};
48 driver->draw2DImage(texture, rect,
49 core::rect<s32>(core::position2d<s32>(0,0),
50 core::dimension2di(texture->getOriginalSize())),
55 video::SColor bgcolor(255,50,50,128);
56 driver->draw2DRectangle(bgcolor, rect, clip);
61 std::string text = item->getText();
62 if(font && text != "")
64 v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
65 v2s32 sdim(dim.X,dim.Y);
67 core::rect<s32> rect2(
68 /*rect.UpperLeftCorner,
69 core::dimension2d<u32>(rect.getWidth(), 15)*/
70 rect.LowerRightCorner - sdim,
74 video::SColor bgcolor(128,0,0,0);
75 driver->draw2DRectangle(bgcolor, rect2, clip);
77 font->draw(text.c_str(), rect2,
78 video::SColor(255,255,255,255), false, false,
88 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
89 gui::IGUIElement* parent, s32 id,
90 IMenuManager *menumgr,
93 InventoryManager *invmgr,
96 GUIModalMenu(env, parent, id, menumgr),
97 m_menu_size(menu_size),
102 m_selected_item = NULL;
105 GUIInventoryMenu::~GUIInventoryMenu()
110 delete m_selected_item;
113 void GUIInventoryMenu::removeChildren()
115 const core::list<gui::IGUIElement*> &children = getChildren();
116 core::list<gui::IGUIElement*> children_copy;
117 for(core::list<gui::IGUIElement*>::ConstIterator
118 i = children.begin(); i != children.end(); i++)
120 children_copy.push_back(*i);
122 for(core::list<gui::IGUIElement*>::Iterator
123 i = children_copy.begin();
124 i != children_copy.end(); i++)
129 gui::IGUIElement *e = getElementFromId(256);
135 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
140 /*padding = v2s32(24,24);
141 spacing = v2s32(60,56);
142 imgsize = v2s32(48,48);*/
144 padding = v2s32(screensize.Y/40, screensize.Y/40);
145 spacing = v2s32(screensize.Y/12, screensize.Y/13);
146 imgsize = v2s32(screensize.Y/15, screensize.Y/15);
151 padding.X*2+spacing.X*(m_menu_size.X-1)+imgsize.X,
152 padding.Y*2+spacing.Y*(m_menu_size.Y-1)+imgsize.Y + helptext_h
155 core::rect<s32> rect(
156 screensize.X/2 - size.X/2,
157 screensize.Y/2 - size.Y/2,
158 screensize.X/2 + size.X/2,
159 screensize.Y/2 + size.Y/2
163 recalculateAbsolutePosition(false);
165 v2s32 basepos = getBasePos();
168 for(u16 i=0; i<m_init_draw_spec.size(); i++)
170 DrawSpec &s = m_init_draw_spec[i];
173 m_draw_spec.push_back(ListDrawSpec(s.name, s.subname,
174 basepos + v2s32(spacing.X*s.pos.X, spacing.Y*s.pos.Y),
181 m_draw_spec.push_back(ListDrawSpec("main",
182 basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
183 m_draw_spec.push_back(ListDrawSpec("craft",
184 basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
185 m_draw_spec.push_back(ListDrawSpec("craftresult",
186 basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
191 core::rect<s32> rect(0, 0, size.X-padding.X*2, helptext_h);
192 rect = rect + v2s32(size.X/2 - rect.getWidth()/2,
193 size.Y-rect.getHeight()-15);
194 const wchar_t *text =
195 L"Left click: Move all items, Right click: Move single item";
196 Environment->addStaticText(text, rect, false, true, this, 256);
200 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
202 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
204 for(u32 i=0; i<m_draw_spec.size(); i++)
206 const ListDrawSpec &s = m_draw_spec[i];
208 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
210 s32 x = (i%s.geom.X) * spacing.X;
211 s32 y = (i/s.geom.X) * spacing.Y;
213 core::rect<s32> rect = imgrect + s.pos + p0;
214 if(rect.isPointInside(p))
216 return ItemSpec(s.inventoryname, s.listname, i);
221 return ItemSpec("", "", -1);
224 void GUIInventoryMenu::drawList(const ListDrawSpec &s, ITextureSource *tsrc)
226 video::IVideoDriver* driver = Environment->getVideoDriver();
229 gui::IGUIFont *font = NULL;
230 gui::IGUISkin* skin = Environment->getSkin();
232 font = skin->getFont();
234 Inventory *inv = m_invmgr->getInventory(m_c, s.inventoryname);
236 InventoryList *ilist = inv->getList(s.listname);
238 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
240 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
242 s32 x = (i%s.geom.X) * spacing.X;
243 s32 y = (i/s.geom.X) * spacing.Y;
245 core::rect<s32> rect = imgrect + s.pos + p;
246 InventoryItem *item = NULL;
248 item = ilist->getItem(i);
250 if(m_selected_item != NULL && m_selected_item->listname == s.listname
251 && m_selected_item->i == i)
253 /*s32 border = imgsize.X/12;
254 driver->draw2DRectangle(video::SColor(255,192,192,192),
255 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*border,
256 rect.LowerRightCorner + v2s32(1,1)*border),
258 driver->draw2DRectangle(video::SColor(255,0,0,0),
259 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*((border+1)/2),
260 rect.LowerRightCorner + v2s32(1,1)*((border+1)/2)),
263 driver->draw2DRectangle(video::SColor(255,255,0,0),
264 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*border,
265 rect.LowerRightCorner + v2s32(1,1)*border),
266 &AbsoluteClippingRect);
269 video::SColor bgcolor(255,128,128,128);
270 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
274 drawInventoryItem(driver, font, item,
275 rect, &AbsoluteClippingRect, tsrc);
281 void GUIInventoryMenu::drawMenu()
283 gui::IGUISkin* skin = Environment->getSkin();
286 video::IVideoDriver* driver = Environment->getVideoDriver();
288 video::SColor bgcolor(140,0,0,0);
289 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
295 for(u32 i=0; i<m_draw_spec.size(); i++)
297 ListDrawSpec &s = m_draw_spec[i];
304 gui::IGUIElement::draw();
307 bool GUIInventoryMenu::OnEvent(const SEvent& event)
309 if(event.EventType==EET_KEY_INPUT_EVENT)
311 KeyPress kp(event.KeyInput);
312 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
313 kp == getKeySetting("keymap_inventory")))
319 if(event.EventType==EET_MOUSE_INPUT_EVENT)
323 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
325 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
327 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
332 v2s32 p(event.MouseInput.X, event.MouseInput.Y);
333 //infostream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
334 ItemSpec s = getItemAtPos(p);
337 infostream<<"Mouse down on "<<s.inventoryname
338 <<"/"<<s.listname<<" "<<s.i<<std::endl;
341 Inventory *inv_from = m_invmgr->getInventory(m_c,
342 m_selected_item->inventoryname);
343 Inventory *inv_to = m_invmgr->getInventory(m_c,
347 InventoryList *list_from =
348 inv_from->getList(m_selected_item->listname);
349 InventoryList *list_to =
350 inv_to->getList(s.listname);
351 if(list_from == NULL)
352 infostream<<"from list doesn't exist"<<std::endl;
354 infostream<<"to list doesn't exist"<<std::endl;
355 // Indicates whether source slot completely empties
356 bool source_empties = false;
357 if(list_from && list_to
358 && list_from->getItem(m_selected_item->i) != NULL)
360 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
361 IMoveAction *a = new IMoveAction();
363 a->from_inv = m_selected_item->inventoryname;
364 a->from_list = m_selected_item->listname;
365 a->from_i = m_selected_item->i;
366 a->to_inv = s.inventoryname;
367 a->to_list = s.listname;
369 //ispec.actions->push_back(a);
370 m_invmgr->inventoryAction(a);
372 if(list_from->getItem(m_selected_item->i)->getCount()==1)
373 source_empties = true;
375 // Remove selection if target was left-clicked or source
377 if(amount == 0 || source_empties)
379 delete m_selected_item;
380 m_selected_item = NULL;
388 Inventory *inv = m_invmgr->getInventory(m_c,
391 InventoryList *list = inv->getList(s.listname);
392 if(list->getItem(s.i) != NULL)
394 m_selected_item = new ItemSpec(s);
402 delete m_selected_item;
403 m_selected_item = NULL;
408 if(event.EventType==EET_GUI_EVENT)
410 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
413 if(!canTakeFocus(event.GUIEvent.Element))
415 infostream<<"GUIInventoryMenu: Not allowing focus change."
417 // Returning true disables focus change
421 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
423 /*switch(event.GUIEvent.Caller->getID())
425 case 256: // continue
435 return Parent ? Parent->OnEvent(event) : false;
439 Here is an example traditional set-up sequence for a DrawSpec list:
441 std::string furnace_inv_id = "nodemetadata:0,1,2";
442 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
443 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
444 "list", furnace_inv_id, "fuel",
445 v2s32(2, 3), v2s32(1, 1)));
446 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
447 "list", furnace_inv_id, "src",
448 v2s32(2, 1), v2s32(1, 1)));
449 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
450 "list", furnace_inv_id, "dst",
451 v2s32(5, 1), v2s32(2, 2)));
452 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
453 "list", "current_player", "main",
454 v2s32(0, 5), v2s32(8, 4)));
455 setDrawSpec(draw_spec);
457 Here is the string for creating the same DrawSpec list (a single line,
458 spread to multiple lines here):
460 GUIInventoryMenu::makeDrawSpecArrayFromString(
462 "nodemetadata:0,1,2",
464 "list[current_name;fuel;2,3;1,1;]"
465 "list[current_name;src;2,1;1,1;]"
466 "list[current_name;dst;5,1;2,2;]"
467 "list[current_player;main;0,5;8,4;]");
469 Returns inventory menu size defined by invsize[].
471 v2s16 GUIInventoryMenu::makeDrawSpecArrayFromString(
472 core::array<GUIInventoryMenu::DrawSpec> &draw_spec,
473 const std::string &data,
474 const std::string ¤t_name)
478 while(f.atend() == false)
480 std::string type = trim(f.next("["));
481 //infostream<<"type="<<type<<std::endl;
484 std::string name = f.next(";");
485 if(name == "current_name")
487 std::string subname = f.next(";");
488 s32 pos_x = stoi(f.next(","));
489 s32 pos_y = stoi(f.next(";"));
490 s32 geom_x = stoi(f.next(","));
491 s32 geom_y = stoi(f.next(";"));
492 infostream<<"list name="<<name<<", subname="<<subname
493 <<", pos=("<<pos_x<<","<<pos_y<<")"
494 <<", geom=("<<geom_x<<","<<geom_y<<")"
496 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
498 v2s32(pos_x,pos_y),v2s32(geom_x,geom_y)));
501 else if(type == "invsize")
503 invsize.X = stoi(f.next(","));
504 invsize.Y = stoi(f.next(";"));
505 infostream<<"invsize ("<<invsize.X<<","<<invsize.Y<<")"<<std::endl;
511 std::string ts = f.next("]");
512 infostream<<"Unknown DrawSpec: type="<<type<<", data=\""<<ts<<"\""