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"
24 void drawInventoryItem(video::IVideoDriver *driver,
26 InventoryItem *item, core::rect<s32> rect,
27 const core::rect<s32> *clip)
32 video::ITexture *texture = NULL;
33 texture = item->getImage();
37 const video::SColor color(255,255,255,255);
38 const video::SColor colors[] = {color,color,color,color};
39 driver->draw2DImage(texture, rect,
40 core::rect<s32>(core::position2d<s32>(0,0),
41 core::dimension2di(texture->getOriginalSize())),
46 video::SColor bgcolor(255,50,50,128);
47 driver->draw2DRectangle(bgcolor, rect, clip);
52 std::string text = item->getText();
53 if(font && text != "")
55 v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
56 v2s32 sdim(dim.X,dim.Y);
58 core::rect<s32> rect2(
59 /*rect.UpperLeftCorner,
60 core::dimension2d<u32>(rect.getWidth(), 15)*/
61 rect.LowerRightCorner - sdim,
65 video::SColor bgcolor(128,0,0,0);
66 driver->draw2DRectangle(bgcolor, rect2, clip);
68 font->draw(text.c_str(), rect2,
69 video::SColor(255,255,255,255), false, false,
79 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
80 gui::IGUIElement* parent, s32 id,
81 IMenuManager *menumgr,
84 InventoryManager *invmgr
86 GUIModalMenu(env, parent, id, menumgr),
87 m_menu_size(menu_size),
91 m_selected_item = NULL;
94 GUIInventoryMenu::~GUIInventoryMenu()
99 delete m_selected_item;
102 void GUIInventoryMenu::removeChildren()
104 const core::list<gui::IGUIElement*> &children = getChildren();
105 core::list<gui::IGUIElement*> children_copy;
106 for(core::list<gui::IGUIElement*>::ConstIterator
107 i = children.begin(); i != children.end(); i++)
109 children_copy.push_back(*i);
111 for(core::list<gui::IGUIElement*>::Iterator
112 i = children_copy.begin();
113 i != children_copy.end(); i++)
118 gui::IGUIElement *e = getElementFromId(256);
124 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
129 /*padding = v2s32(24,24);
130 spacing = v2s32(60,56);
131 imgsize = v2s32(48,48);*/
133 padding = v2s32(screensize.Y/40, screensize.Y/40);
134 spacing = v2s32(screensize.Y/12, screensize.Y/13);
135 imgsize = v2s32(screensize.Y/15, screensize.Y/15);
140 padding.X*2+spacing.X*(m_menu_size.X-1)+imgsize.X,
141 padding.Y*2+spacing.Y*(m_menu_size.Y-1)+imgsize.Y + helptext_h
144 core::rect<s32> rect(
145 screensize.X/2 - size.X/2,
146 screensize.Y/2 - size.Y/2,
147 screensize.X/2 + size.X/2,
148 screensize.Y/2 + size.Y/2
152 recalculateAbsolutePosition(false);
154 v2s32 basepos = getBasePos();
157 for(u16 i=0; i<m_init_draw_spec.size(); i++)
159 DrawSpec &s = m_init_draw_spec[i];
162 m_draw_spec.push_back(ListDrawSpec(s.name, s.subname,
163 basepos + v2s32(spacing.X*s.pos.X, spacing.Y*s.pos.Y),
170 m_draw_spec.push_back(ListDrawSpec("main",
171 basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
172 m_draw_spec.push_back(ListDrawSpec("craft",
173 basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
174 m_draw_spec.push_back(ListDrawSpec("craftresult",
175 basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
180 core::rect<s32> rect(0, 0, size.X-padding.X*2, helptext_h);
181 rect = rect + v2s32(size.X/2 - rect.getWidth()/2,
182 size.Y-rect.getHeight()-15);
183 const wchar_t *text =
184 L"Left click: Move all items, Right click: Move single item";
185 Environment->addStaticText(text, rect, false, true, this, 256);
189 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
191 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
193 for(u32 i=0; i<m_draw_spec.size(); i++)
195 const ListDrawSpec &s = m_draw_spec[i];
197 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
199 s32 x = (i%s.geom.X) * spacing.X;
200 s32 y = (i/s.geom.X) * spacing.Y;
202 core::rect<s32> rect = imgrect + s.pos + p0;
203 if(rect.isPointInside(p))
205 return ItemSpec(s.inventoryname, s.listname, i);
210 return ItemSpec("", "", -1);
213 void GUIInventoryMenu::drawList(const ListDrawSpec &s)
215 video::IVideoDriver* driver = Environment->getVideoDriver();
218 gui::IGUIFont *font = NULL;
219 gui::IGUISkin* skin = Environment->getSkin();
221 font = skin->getFont();
223 Inventory *inv = m_invmgr->getInventory(m_c, s.inventoryname);
225 InventoryList *ilist = inv->getList(s.listname);
227 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
229 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
231 s32 x = (i%s.geom.X) * spacing.X;
232 s32 y = (i/s.geom.X) * spacing.Y;
234 core::rect<s32> rect = imgrect + s.pos + p;
235 InventoryItem *item = NULL;
237 item = ilist->getItem(i);
239 if(m_selected_item != NULL && m_selected_item->listname == s.listname
240 && m_selected_item->i == i)
242 driver->draw2DRectangle(video::SColor(255,255,0,0),
243 core::rect<s32>(rect.UpperLeftCorner - v2s32(2,2),
244 rect.LowerRightCorner + v2s32(2,2)),
245 &AbsoluteClippingRect);
250 drawInventoryItem(driver, font, item,
251 rect, &AbsoluteClippingRect);
255 video::SColor bgcolor(255,128,128,128);
256 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
261 void GUIInventoryMenu::drawMenu()
263 gui::IGUISkin* skin = Environment->getSkin();
266 video::IVideoDriver* driver = Environment->getVideoDriver();
268 video::SColor bgcolor(140,0,0,0);
269 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
275 for(u32 i=0; i<m_draw_spec.size(); i++)
277 ListDrawSpec &s = m_draw_spec[i];
284 gui::IGUIElement::draw();
287 bool GUIInventoryMenu::OnEvent(const SEvent& event)
289 if(event.EventType==EET_KEY_INPUT_EVENT)
291 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
296 if(event.KeyInput.Key==KEY_KEY_I && event.KeyInput.PressedDown)
302 if(event.EventType==EET_MOUSE_INPUT_EVENT)
304 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN
305 || event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
307 bool right = (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN);
308 v2s32 p(event.MouseInput.X, event.MouseInput.Y);
309 //dstream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
310 ItemSpec s = getItemAtPos(p);
313 dstream<<"Mouse down on "<<s.inventoryname
314 <<"/"<<s.listname<<" "<<s.i<<std::endl;
317 Inventory *inv_from = m_invmgr->getInventory(m_c,
318 m_selected_item->inventoryname);
319 Inventory *inv_to = m_invmgr->getInventory(m_c,
323 InventoryList *list_from =
324 inv_from->getList(m_selected_item->listname);
325 InventoryList *list_to =
326 inv_to->getList(s.listname);
327 if(list_from == NULL)
328 dstream<<"from list doesn't exist"<<std::endl;
330 dstream<<"to list doesn't exist"<<std::endl;
331 // Indicates whether source slot completely empties
332 bool source_empties = false;
333 if(list_from && list_to
334 && list_from->getItem(m_selected_item->i) != NULL)
336 dstream<<"Handing IACTION_MOVE to manager"<<std::endl;
337 IMoveAction *a = new IMoveAction();
338 a->count = right ? 1 : 0;
339 a->from_inv = m_selected_item->inventoryname;
340 a->from_list = m_selected_item->listname;
341 a->from_i = m_selected_item->i;
342 a->to_inv = s.inventoryname;
343 a->to_list = s.listname;
345 //ispec.actions->push_back(a);
346 m_invmgr->inventoryAction(a);
348 if(list_from->getItem(m_selected_item->i)->getCount()==1)
349 source_empties = true;
351 // Remove selection if target was left-clicked or source
353 if(right == false || source_empties)
355 delete m_selected_item;
356 m_selected_item = NULL;
364 Inventory *inv = m_invmgr->getInventory(m_c,
367 InventoryList *list = inv->getList(s.listname);
368 if(list->getItem(s.i) != NULL)
370 m_selected_item = new ItemSpec(s);
378 delete m_selected_item;
379 m_selected_item = NULL;
384 if(event.EventType==EET_GUI_EVENT)
386 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
389 if(!canTakeFocus(event.GUIEvent.Element))
391 dstream<<"GUIInventoryMenu: Not allowing focus change."
393 // Returning true disables focus change
397 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
399 /*switch(event.GUIEvent.Caller->getID())
401 case 256: // continue
411 return Parent ? Parent->OnEvent(event) : false;