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 void drawInventoryItem(video::IVideoDriver *driver,
27 InventoryItem *item, core::rect<s32> rect,
28 const core::rect<s32> *clip)
33 video::ITexture *texture = NULL;
34 texture = item->getImage();
38 const video::SColor color(255,255,255,255);
39 const video::SColor colors[] = {color,color,color,color};
40 driver->draw2DImage(texture, rect,
41 core::rect<s32>(core::position2d<s32>(0,0),
42 core::dimension2di(texture->getOriginalSize())),
47 video::SColor bgcolor(255,50,50,128);
48 driver->draw2DRectangle(bgcolor, rect, clip);
53 std::string text = item->getText();
54 if(font && text != "")
56 v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
57 v2s32 sdim(dim.X,dim.Y);
59 core::rect<s32> rect2(
60 /*rect.UpperLeftCorner,
61 core::dimension2d<u32>(rect.getWidth(), 15)*/
62 rect.LowerRightCorner - sdim,
66 video::SColor bgcolor(128,0,0,0);
67 driver->draw2DRectangle(bgcolor, rect2, clip);
69 font->draw(text.c_str(), rect2,
70 video::SColor(255,255,255,255), false, false,
80 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
81 gui::IGUIElement* parent, s32 id,
82 IMenuManager *menumgr,
85 InventoryManager *invmgr
87 GUIModalMenu(env, parent, id, menumgr),
88 m_menu_size(menu_size),
92 m_selected_item = NULL;
95 GUIInventoryMenu::~GUIInventoryMenu()
100 delete m_selected_item;
103 void GUIInventoryMenu::removeChildren()
105 const core::list<gui::IGUIElement*> &children = getChildren();
106 core::list<gui::IGUIElement*> children_copy;
107 for(core::list<gui::IGUIElement*>::ConstIterator
108 i = children.begin(); i != children.end(); i++)
110 children_copy.push_back(*i);
112 for(core::list<gui::IGUIElement*>::Iterator
113 i = children_copy.begin();
114 i != children_copy.end(); i++)
119 gui::IGUIElement *e = getElementFromId(256);
125 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
130 /*padding = v2s32(24,24);
131 spacing = v2s32(60,56);
132 imgsize = v2s32(48,48);*/
134 padding = v2s32(screensize.Y/40, screensize.Y/40);
135 spacing = v2s32(screensize.Y/12, screensize.Y/13);
136 imgsize = v2s32(screensize.Y/15, screensize.Y/15);
141 padding.X*2+spacing.X*(m_menu_size.X-1)+imgsize.X,
142 padding.Y*2+spacing.Y*(m_menu_size.Y-1)+imgsize.Y + helptext_h
145 core::rect<s32> rect(
146 screensize.X/2 - size.X/2,
147 screensize.Y/2 - size.Y/2,
148 screensize.X/2 + size.X/2,
149 screensize.Y/2 + size.Y/2
153 recalculateAbsolutePosition(false);
155 v2s32 basepos = getBasePos();
158 for(u16 i=0; i<m_init_draw_spec.size(); i++)
160 DrawSpec &s = m_init_draw_spec[i];
163 m_draw_spec.push_back(ListDrawSpec(s.name, s.subname,
164 basepos + v2s32(spacing.X*s.pos.X, spacing.Y*s.pos.Y),
171 m_draw_spec.push_back(ListDrawSpec("main",
172 basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
173 m_draw_spec.push_back(ListDrawSpec("craft",
174 basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
175 m_draw_spec.push_back(ListDrawSpec("craftresult",
176 basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
181 core::rect<s32> rect(0, 0, size.X-padding.X*2, helptext_h);
182 rect = rect + v2s32(size.X/2 - rect.getWidth()/2,
183 size.Y-rect.getHeight()-15);
184 const wchar_t *text =
185 L"Left click: Move all items, Right click: Move single item";
186 Environment->addStaticText(text, rect, false, true, this, 256);
190 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
192 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
194 for(u32 i=0; i<m_draw_spec.size(); i++)
196 const ListDrawSpec &s = m_draw_spec[i];
198 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
200 s32 x = (i%s.geom.X) * spacing.X;
201 s32 y = (i/s.geom.X) * spacing.Y;
203 core::rect<s32> rect = imgrect + s.pos + p0;
204 if(rect.isPointInside(p))
206 return ItemSpec(s.inventoryname, s.listname, i);
211 return ItemSpec("", "", -1);
214 void GUIInventoryMenu::drawList(const ListDrawSpec &s)
216 video::IVideoDriver* driver = Environment->getVideoDriver();
219 gui::IGUIFont *font = NULL;
220 gui::IGUISkin* skin = Environment->getSkin();
222 font = skin->getFont();
224 Inventory *inv = m_invmgr->getInventory(m_c, s.inventoryname);
226 InventoryList *ilist = inv->getList(s.listname);
228 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
230 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
232 s32 x = (i%s.geom.X) * spacing.X;
233 s32 y = (i/s.geom.X) * spacing.Y;
235 core::rect<s32> rect = imgrect + s.pos + p;
236 InventoryItem *item = NULL;
238 item = ilist->getItem(i);
240 if(m_selected_item != NULL && m_selected_item->listname == s.listname
241 && m_selected_item->i == i)
243 driver->draw2DRectangle(video::SColor(255,255,0,0),
244 core::rect<s32>(rect.UpperLeftCorner - v2s32(2,2),
245 rect.LowerRightCorner + v2s32(2,2)),
246 &AbsoluteClippingRect);
251 drawInventoryItem(driver, font, item,
252 rect, &AbsoluteClippingRect);
256 video::SColor bgcolor(255,128,128,128);
257 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
262 void GUIInventoryMenu::drawMenu()
264 gui::IGUISkin* skin = Environment->getSkin();
267 video::IVideoDriver* driver = Environment->getVideoDriver();
269 video::SColor bgcolor(140,0,0,0);
270 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
276 for(u32 i=0; i<m_draw_spec.size(); i++)
278 ListDrawSpec &s = m_draw_spec[i];
285 gui::IGUIElement::draw();
288 bool GUIInventoryMenu::OnEvent(const SEvent& event)
290 if(event.EventType==EET_KEY_INPUT_EVENT)
292 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
297 if(event.KeyInput.Key==getKeySetting("keymap_inventory") && event.KeyInput.PressedDown)
303 if(event.EventType==EET_MOUSE_INPUT_EVENT)
305 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN
306 || event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
308 bool right = (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN);
309 v2s32 p(event.MouseInput.X, event.MouseInput.Y);
310 //dstream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
311 ItemSpec s = getItemAtPos(p);
314 dstream<<"Mouse down on "<<s.inventoryname
315 <<"/"<<s.listname<<" "<<s.i<<std::endl;
318 Inventory *inv_from = m_invmgr->getInventory(m_c,
319 m_selected_item->inventoryname);
320 Inventory *inv_to = m_invmgr->getInventory(m_c,
324 InventoryList *list_from =
325 inv_from->getList(m_selected_item->listname);
326 InventoryList *list_to =
327 inv_to->getList(s.listname);
328 if(list_from == NULL)
329 dstream<<"from list doesn't exist"<<std::endl;
331 dstream<<"to list doesn't exist"<<std::endl;
332 // Indicates whether source slot completely empties
333 bool source_empties = false;
334 if(list_from && list_to
335 && list_from->getItem(m_selected_item->i) != NULL)
337 dstream<<"Handing IACTION_MOVE to manager"<<std::endl;
338 IMoveAction *a = new IMoveAction();
339 a->count = right ? 1 : 0;
340 a->from_inv = m_selected_item->inventoryname;
341 a->from_list = m_selected_item->listname;
342 a->from_i = m_selected_item->i;
343 a->to_inv = s.inventoryname;
344 a->to_list = s.listname;
346 //ispec.actions->push_back(a);
347 m_invmgr->inventoryAction(a);
349 if(list_from->getItem(m_selected_item->i)->getCount()==1)
350 source_empties = true;
352 // Remove selection if target was left-clicked or source
354 if(right == false || source_empties)
356 delete m_selected_item;
357 m_selected_item = NULL;
365 Inventory *inv = m_invmgr->getInventory(m_c,
368 InventoryList *list = inv->getList(s.listname);
369 if(list->getItem(s.i) != NULL)
371 m_selected_item = new ItemSpec(s);
379 delete m_selected_item;
380 m_selected_item = NULL;
385 if(event.EventType==EET_GUI_EVENT)
387 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
390 if(!canTakeFocus(event.GUIEvent.Element))
392 dstream<<"GUIInventoryMenu: Not allowing focus change."
394 // Returning true disables focus change
398 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
400 /*switch(event.GUIEvent.Caller->getID())
402 case 256: // continue
412 return Parent ? Parent->OnEvent(event) : false;