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>
31 void drawInventoryItem(video::IVideoDriver *driver,
33 InventoryItem *item, core::rect<s32> rect,
34 const core::rect<s32> *clip)
39 video::ITexture *texture = NULL;
40 texture = item->getImage();
44 const video::SColor color(255,255,255,255);
45 const video::SColor colors[] = {color,color,color,color};
46 driver->draw2DImage(texture, rect,
47 core::rect<s32>(core::position2d<s32>(0,0),
48 core::dimension2di(texture->getOriginalSize())),
53 video::SColor bgcolor(255,50,50,128);
54 driver->draw2DRectangle(bgcolor, rect, clip);
59 std::string text = item->getText();
60 if(font && text != "")
62 v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
63 v2s32 sdim(dim.X,dim.Y);
65 core::rect<s32> rect2(
66 /*rect.UpperLeftCorner,
67 core::dimension2d<u32>(rect.getWidth(), 15)*/
68 rect.LowerRightCorner - sdim,
72 video::SColor bgcolor(128,0,0,0);
73 driver->draw2DRectangle(bgcolor, rect2, clip);
75 font->draw(text.c_str(), rect2,
76 video::SColor(255,255,255,255), false, false,
86 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
87 gui::IGUIElement* parent, s32 id,
88 IMenuManager *menumgr,
91 InventoryManager *invmgr
93 GUIModalMenu(env, parent, id, menumgr),
94 m_menu_size(menu_size),
98 m_selected_item = NULL;
101 GUIInventoryMenu::~GUIInventoryMenu()
106 delete m_selected_item;
109 void GUIInventoryMenu::removeChildren()
111 const core::list<gui::IGUIElement*> &children = getChildren();
112 core::list<gui::IGUIElement*> children_copy;
113 for(core::list<gui::IGUIElement*>::ConstIterator
114 i = children.begin(); i != children.end(); i++)
116 children_copy.push_back(*i);
118 for(core::list<gui::IGUIElement*>::Iterator
119 i = children_copy.begin();
120 i != children_copy.end(); i++)
125 gui::IGUIElement *e = getElementFromId(256);
131 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
136 /*padding = v2s32(24,24);
137 spacing = v2s32(60,56);
138 imgsize = v2s32(48,48);*/
140 padding = v2s32(screensize.Y/40, screensize.Y/40);
141 spacing = v2s32(screensize.Y/12, screensize.Y/13);
142 imgsize = v2s32(screensize.Y/15, screensize.Y/15);
147 padding.X*2+spacing.X*(m_menu_size.X-1)+imgsize.X,
148 padding.Y*2+spacing.Y*(m_menu_size.Y-1)+imgsize.Y + helptext_h
151 core::rect<s32> rect(
152 screensize.X/2 - size.X/2,
153 screensize.Y/2 - size.Y/2,
154 screensize.X/2 + size.X/2,
155 screensize.Y/2 + size.Y/2
159 recalculateAbsolutePosition(false);
161 v2s32 basepos = getBasePos();
164 for(u16 i=0; i<m_init_draw_spec.size(); i++)
166 DrawSpec &s = m_init_draw_spec[i];
169 m_draw_spec.push_back(ListDrawSpec(s.name, s.subname,
170 basepos + v2s32(spacing.X*s.pos.X, spacing.Y*s.pos.Y),
177 m_draw_spec.push_back(ListDrawSpec("main",
178 basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
179 m_draw_spec.push_back(ListDrawSpec("craft",
180 basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
181 m_draw_spec.push_back(ListDrawSpec("craftresult",
182 basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
187 core::rect<s32> rect(0, 0, size.X-padding.X*2, helptext_h);
188 rect = rect + v2s32(size.X/2 - rect.getWidth()/2,
189 size.Y-rect.getHeight()-15);
190 const wchar_t *text =
191 L"Left click: Move all items, Right click: Move single item";
192 Environment->addStaticText(text, rect, false, true, this, 256);
196 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
198 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
200 for(u32 i=0; i<m_draw_spec.size(); i++)
202 const ListDrawSpec &s = m_draw_spec[i];
204 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
206 s32 x = (i%s.geom.X) * spacing.X;
207 s32 y = (i/s.geom.X) * spacing.Y;
209 core::rect<s32> rect = imgrect + s.pos + p0;
210 if(rect.isPointInside(p))
212 return ItemSpec(s.inventoryname, s.listname, i);
217 return ItemSpec("", "", -1);
220 void GUIInventoryMenu::drawList(const ListDrawSpec &s)
222 video::IVideoDriver* driver = Environment->getVideoDriver();
225 gui::IGUIFont *font = NULL;
226 gui::IGUISkin* skin = Environment->getSkin();
228 font = skin->getFont();
230 Inventory *inv = m_invmgr->getInventory(m_c, s.inventoryname);
232 InventoryList *ilist = inv->getList(s.listname);
234 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
236 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
238 s32 x = (i%s.geom.X) * spacing.X;
239 s32 y = (i/s.geom.X) * spacing.Y;
241 core::rect<s32> rect = imgrect + s.pos + p;
242 InventoryItem *item = NULL;
244 item = ilist->getItem(i);
246 if(m_selected_item != NULL && m_selected_item->listname == s.listname
247 && m_selected_item->i == i)
249 /*s32 border = imgsize.X/12;
250 driver->draw2DRectangle(video::SColor(255,192,192,192),
251 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*border,
252 rect.LowerRightCorner + v2s32(1,1)*border),
254 driver->draw2DRectangle(video::SColor(255,0,0,0),
255 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*((border+1)/2),
256 rect.LowerRightCorner + v2s32(1,1)*((border+1)/2)),
259 driver->draw2DRectangle(video::SColor(255,255,0,0),
260 core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*border,
261 rect.LowerRightCorner + v2s32(1,1)*border),
262 &AbsoluteClippingRect);
265 video::SColor bgcolor(255,128,128,128);
266 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
270 drawInventoryItem(driver, font, item,
271 rect, &AbsoluteClippingRect);
277 void GUIInventoryMenu::drawMenu()
279 gui::IGUISkin* skin = Environment->getSkin();
282 video::IVideoDriver* driver = Environment->getVideoDriver();
284 video::SColor bgcolor(140,0,0,0);
285 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
291 for(u32 i=0; i<m_draw_spec.size(); i++)
293 ListDrawSpec &s = m_draw_spec[i];
300 gui::IGUIElement::draw();
303 bool GUIInventoryMenu::OnEvent(const SEvent& event)
305 if(event.EventType==EET_KEY_INPUT_EVENT)
307 KeyPress kp(event.KeyInput);
308 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
309 kp == getKeySetting("keymap_inventory")))
315 if(event.EventType==EET_MOUSE_INPUT_EVENT)
319 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
321 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
323 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
328 v2s32 p(event.MouseInput.X, event.MouseInput.Y);
329 //dstream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
330 ItemSpec s = getItemAtPos(p);
333 dstream<<"Mouse down on "<<s.inventoryname
334 <<"/"<<s.listname<<" "<<s.i<<std::endl;
337 Inventory *inv_from = m_invmgr->getInventory(m_c,
338 m_selected_item->inventoryname);
339 Inventory *inv_to = m_invmgr->getInventory(m_c,
343 InventoryList *list_from =
344 inv_from->getList(m_selected_item->listname);
345 InventoryList *list_to =
346 inv_to->getList(s.listname);
347 if(list_from == NULL)
348 dstream<<"from list doesn't exist"<<std::endl;
350 dstream<<"to list doesn't exist"<<std::endl;
351 // Indicates whether source slot completely empties
352 bool source_empties = false;
353 if(list_from && list_to
354 && list_from->getItem(m_selected_item->i) != NULL)
356 dstream<<"Handing IACTION_MOVE to manager"<<std::endl;
357 IMoveAction *a = new IMoveAction();
359 a->from_inv = m_selected_item->inventoryname;
360 a->from_list = m_selected_item->listname;
361 a->from_i = m_selected_item->i;
362 a->to_inv = s.inventoryname;
363 a->to_list = s.listname;
365 //ispec.actions->push_back(a);
366 m_invmgr->inventoryAction(a);
368 if(list_from->getItem(m_selected_item->i)->getCount()==1)
369 source_empties = true;
371 // Remove selection if target was left-clicked or source
373 if(amount == 0 || source_empties)
375 delete m_selected_item;
376 m_selected_item = NULL;
384 Inventory *inv = m_invmgr->getInventory(m_c,
387 InventoryList *list = inv->getList(s.listname);
388 if(list->getItem(s.i) != NULL)
390 m_selected_item = new ItemSpec(s);
398 delete m_selected_item;
399 m_selected_item = NULL;
404 if(event.EventType==EET_GUI_EVENT)
406 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
409 if(!canTakeFocus(event.GUIEvent.Element))
411 dstream<<"GUIInventoryMenu: Not allowing focus change."
413 // Returning true disables focus change
417 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
419 /*switch(event.GUIEvent.Caller->getID())
421 case 256: // continue
431 return Parent ? Parent->OnEvent(event) : false;
435 Here is an example traditional set-up sequence for a DrawSpec list:
437 std::string furnace_inv_id = "nodemetadata:0,1,2";
438 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
439 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
440 "list", furnace_inv_id, "fuel",
441 v2s32(2, 3), v2s32(1, 1)));
442 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
443 "list", furnace_inv_id, "src",
444 v2s32(2, 1), v2s32(1, 1)));
445 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
446 "list", furnace_inv_id, "dst",
447 v2s32(5, 1), v2s32(2, 2)));
448 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
449 "list", "current_player", "main",
450 v2s32(0, 5), v2s32(8, 4)));
451 setDrawSpec(draw_spec);
453 Here is the string for creating the same DrawSpec list (a single line,
454 spread to multiple lines here):
456 GUIInventoryMenu::makeDrawSpecArrayFromString(
458 "nodemetadata:0,1,2",
460 "list[current_name;fuel;2,3;1,1;]"
461 "list[current_name;src;2,1;1,1;]"
462 "list[current_name;dst;5,1;2,2;]"
463 "list[current_player;main;0,5;8,4;]");
465 Returns inventory menu size defined by invsize[].
467 v2s16 GUIInventoryMenu::makeDrawSpecArrayFromString(
468 core::array<GUIInventoryMenu::DrawSpec> &draw_spec,
469 const std::string &data,
470 const std::string ¤t_name)
474 while(f.atend() == false)
476 std::string type = trim(f.next("["));
477 //dstream<<"type="<<type<<std::endl;
480 std::string name = f.next(";");
481 if(name == "current_name")
483 std::string subname = f.next(";");
484 s32 pos_x = stoi(f.next(","));
485 s32 pos_y = stoi(f.next(";"));
486 s32 geom_x = stoi(f.next(","));
487 s32 geom_y = stoi(f.next(";"));
488 dstream<<"list name="<<name<<", subname="<<subname
489 <<", pos=("<<pos_x<<","<<pos_y<<")"
490 <<", geom=("<<geom_x<<","<<geom_y<<")"
492 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
494 v2s32(pos_x,pos_y),v2s32(geom_x,geom_y)));
497 else if(type == "invsize")
499 invsize.X = stoi(f.next(","));
500 invsize.Y = stoi(f.next(";"));
501 dstream<<"invsize ("<<invsize.X<<","<<invsize.Y<<")"<<std::endl;
507 std::string ts = f.next("]");
508 dstream<<"Unknown DrawSpec: type="<<type<<", data=\""<<ts<<"\""