merged an old head into main branch
[oweals/minetest.git] / src / guiInventoryMenu.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20
21 #include "guiInventoryMenu.h"
22 #include "constants.h"
23 #include "keycode.h"
24
25 void drawInventoryItem(video::IVideoDriver *driver,
26                 gui::IGUIFont *font,
27                 InventoryItem *item, core::rect<s32> rect,
28                 const core::rect<s32> *clip)
29 {
30         if(item == NULL)
31                 return;
32         
33         video::ITexture *texture = NULL;
34         texture = item->getImage();
35
36         if(texture != NULL)
37         {
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())),
43                         clip, colors, false);
44         }
45         else
46         {
47                 video::SColor bgcolor(255,50,50,128);
48                 driver->draw2DRectangle(bgcolor, rect, clip);
49         }
50
51         if(font != NULL)
52         {
53                 std::string text = item->getText();
54                 if(font && text != "")
55                 {
56                         v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
57                         v2s32 sdim(dim.X,dim.Y);
58
59                         core::rect<s32> rect2(
60                                 /*rect.UpperLeftCorner,
61                                 core::dimension2d<u32>(rect.getWidth(), 15)*/
62                                 rect.LowerRightCorner - sdim,
63                                 sdim
64                         );
65
66                         video::SColor bgcolor(128,0,0,0);
67                         driver->draw2DRectangle(bgcolor, rect2, clip);
68                         
69                         font->draw(text.c_str(), rect2,
70                                         video::SColor(255,255,255,255), false, false,
71                                         clip);
72                 }
73         }
74 }
75
76 /*
77         GUIInventoryMenu
78 */
79
80 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
81                 gui::IGUIElement* parent, s32 id,
82                 IMenuManager *menumgr,
83                 v2s16 menu_size,
84                 InventoryContext *c,
85                 InventoryManager *invmgr
86                 ):
87         GUIModalMenu(env, parent, id, menumgr),
88         m_menu_size(menu_size),
89         m_c(c),
90         m_invmgr(invmgr)
91 {
92         m_selected_item = NULL;
93 }
94
95 GUIInventoryMenu::~GUIInventoryMenu()
96 {
97         removeChildren();
98
99         if(m_selected_item)
100                 delete m_selected_item;
101 }
102
103 void GUIInventoryMenu::removeChildren()
104 {
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++)
109         {
110                 children_copy.push_back(*i);
111         }
112         for(core::list<gui::IGUIElement*>::Iterator
113                         i = children_copy.begin();
114                         i != children_copy.end(); i++)
115         {
116                 (*i)->remove();
117         }
118         /*{
119                 gui::IGUIElement *e = getElementFromId(256);
120                 if(e != NULL)
121                         e->remove();
122         }*/
123 }
124
125 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
126 {
127         // Remove children
128         removeChildren();
129         
130         /*padding = v2s32(24,24);
131         spacing = v2s32(60,56);
132         imgsize = v2s32(48,48);*/
133
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);
137
138         s32 helptext_h = 15;
139
140         v2s32 size(
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
143         );
144
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
150         );
151         
152         DesiredRect = rect;
153         recalculateAbsolutePosition(false);
154
155         v2s32 basepos = getBasePos();
156         
157         m_draw_spec.clear();
158         for(u16 i=0; i<m_init_draw_spec.size(); i++)
159         {
160                 DrawSpec &s = m_init_draw_spec[i];
161                 if(s.type == "list")
162                 {
163                         m_draw_spec.push_back(ListDrawSpec(s.name, s.subname,
164                                         basepos + v2s32(spacing.X*s.pos.X, spacing.Y*s.pos.Y),
165                                         s.geom));
166                 }
167         }
168
169         /*
170         m_draw_spec.clear();
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)));
177         */
178         
179         // Add children
180         {
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);
187         }
188 }
189
190 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
191 {
192         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
193         
194         for(u32 i=0; i<m_draw_spec.size(); i++)
195         {
196                 const ListDrawSpec &s = m_draw_spec[i];
197
198                 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
199                 {
200                         s32 x = (i%s.geom.X) * spacing.X;
201                         s32 y = (i/s.geom.X) * spacing.Y;
202                         v2s32 p0(x,y);
203                         core::rect<s32> rect = imgrect + s.pos + p0;
204                         if(rect.isPointInside(p))
205                         {
206                                 return ItemSpec(s.inventoryname, s.listname, i);
207                         }
208                 }
209         }
210
211         return ItemSpec("", "", -1);
212 }
213
214 void GUIInventoryMenu::drawList(const ListDrawSpec &s)
215 {
216         video::IVideoDriver* driver = Environment->getVideoDriver();
217
218         // Get font
219         gui::IGUIFont *font = NULL;
220         gui::IGUISkin* skin = Environment->getSkin();
221         if (skin)
222                 font = skin->getFont();
223         
224         Inventory *inv = m_invmgr->getInventory(m_c, s.inventoryname);
225         assert(inv);
226         InventoryList *ilist = inv->getList(s.listname);
227         
228         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
229         
230         for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
231         {
232                 s32 x = (i%s.geom.X) * spacing.X;
233                 s32 y = (i/s.geom.X) * spacing.Y;
234                 v2s32 p(x,y);
235                 core::rect<s32> rect = imgrect + s.pos + p;
236                 InventoryItem *item = NULL;
237                 if(ilist)
238                         item = ilist->getItem(i);
239
240                 if(m_selected_item != NULL && m_selected_item->listname == s.listname
241                                 && m_selected_item->i == i)
242                 {
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);
247                 }
248
249                 if(item)
250                 {
251                         drawInventoryItem(driver, font, item,
252                                         rect, &AbsoluteClippingRect);
253                 }
254                 else
255                 {
256                         video::SColor bgcolor(255,128,128,128);
257                         driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
258                 }
259         }
260 }
261
262 void GUIInventoryMenu::drawMenu()
263 {
264         gui::IGUISkin* skin = Environment->getSkin();
265         if (!skin)
266                 return;
267         video::IVideoDriver* driver = Environment->getVideoDriver();
268         
269         video::SColor bgcolor(140,0,0,0);
270         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
271
272         /*
273                 Draw items
274         */
275         
276         for(u32 i=0; i<m_draw_spec.size(); i++)
277         {
278                 ListDrawSpec &s = m_draw_spec[i];
279                 drawList(s);
280         }
281
282         /*
283                 Call base class
284         */
285         gui::IGUIElement::draw();
286 }
287
288 bool GUIInventoryMenu::OnEvent(const SEvent& event)
289 {
290         if(event.EventType==EET_KEY_INPUT_EVENT)
291         {
292                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
293                 {
294                         quitMenu();
295                         return true;
296                 }
297                 if(event.KeyInput.Key==getKeySetting("keymap_inventory") && event.KeyInput.PressedDown)
298                 {
299                         quitMenu();
300                         return true;
301                 }
302         }
303         if(event.EventType==EET_MOUSE_INPUT_EVENT)
304         {
305                 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN
306                                 || event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
307                 {
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);
312                         if(s.isValid())
313                         {
314                                 dstream<<"Mouse down on "<<s.inventoryname
315                                                 <<"/"<<s.listname<<" "<<s.i<<std::endl;
316                                 if(m_selected_item)
317                                 {
318                                         Inventory *inv_from = m_invmgr->getInventory(m_c,
319                                                         m_selected_item->inventoryname);
320                                         Inventory *inv_to = m_invmgr->getInventory(m_c,
321                                                         s.inventoryname);
322                                         assert(inv_from);
323                                         assert(inv_to);
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;
330                                         if(list_to == NULL)
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)
336                                         {
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;
345                                                 a->to_i = s.i;
346                                                 //ispec.actions->push_back(a);
347                                                 m_invmgr->inventoryAction(a);
348                                                 
349                                                 if(list_from->getItem(m_selected_item->i)->getCount()==1)
350                                                         source_empties = true;
351                                         }
352                                         // Remove selection if target was left-clicked or source
353                                         // slot was emptied
354                                         if(right == false || source_empties)
355                                         {
356                                                 delete m_selected_item;
357                                                 m_selected_item = NULL;
358                                         }
359                                 }
360                                 else
361                                 {
362                                         /*
363                                                 Select if non-NULL
364                                         */
365                                         Inventory *inv = m_invmgr->getInventory(m_c,
366                                                         s.inventoryname);
367                                         assert(inv);
368                                         InventoryList *list = inv->getList(s.listname);
369                                         if(list->getItem(s.i) != NULL)
370                                         {
371                                                 m_selected_item = new ItemSpec(s);
372                                         }
373                                 }
374                         }
375                         else
376                         {
377                                 if(m_selected_item)
378                                 {
379                                         delete m_selected_item;
380                                         m_selected_item = NULL;
381                                 }
382                         }
383                 }
384         }
385         if(event.EventType==EET_GUI_EVENT)
386         {
387                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
388                                 && isVisible())
389                 {
390                         if(!canTakeFocus(event.GUIEvent.Element))
391                         {
392                                 dstream<<"GUIInventoryMenu: Not allowing focus change."
393                                                 <<std::endl;
394                                 // Returning true disables focus change
395                                 return true;
396                         }
397                 }
398                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
399                 {
400                         /*switch(event.GUIEvent.Caller->getID())
401                         {
402                         case 256: // continue
403                                 setVisible(false);
404                                 break;
405                         case 257: // exit
406                                 dev->closeDevice();
407                                 break;
408                         }*/
409                 }
410         }
411
412         return Parent ? Parent->OnEvent(event) : false;
413 }
414
415
416