Update inventory texture too
[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 #include "strfnd.h"
25 #include <IGUICheckBox.h>
26 #include <IGUIEditBox.h>
27 #include <IGUIButton.h>
28 #include <IGUIStaticText.h>
29 #include <IGUIFont.h>
30 #include "log.h"
31
32 void drawInventoryItem(video::IVideoDriver *driver,
33                 gui::IGUIFont *font,
34                 InventoryItem *item, core::rect<s32> rect,
35                 const core::rect<s32> *clip,
36                 ITextureSource *tsrc)
37 {
38         if(item == NULL)
39                 return;
40         
41         video::ITexture *texture = NULL;
42         texture = item->getImage(tsrc);
43
44         if(texture != NULL)
45         {
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())),
51                         clip, colors, true);
52         }
53         else
54         {
55                 video::SColor bgcolor(255,50,50,128);
56                 driver->draw2DRectangle(bgcolor, rect, clip);
57         }
58
59         if(font != NULL)
60         {
61                 std::string text = item->getText();
62                 if(font && text != "")
63                 {
64                         v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
65                         v2s32 sdim(dim.X,dim.Y);
66
67                         core::rect<s32> rect2(
68                                 /*rect.UpperLeftCorner,
69                                 core::dimension2d<u32>(rect.getWidth(), 15)*/
70                                 rect.LowerRightCorner - sdim,
71                                 sdim
72                         );
73
74                         video::SColor bgcolor(128,0,0,0);
75                         driver->draw2DRectangle(bgcolor, rect2, clip);
76                         
77                         font->draw(text.c_str(), rect2,
78                                         video::SColor(255,255,255,255), false, false,
79                                         clip);
80                 }
81         }
82 }
83
84 /*
85         GUIInventoryMenu
86 */
87
88 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
89                 gui::IGUIElement* parent, s32 id,
90                 IMenuManager *menumgr,
91                 v2s16 menu_size,
92                 InventoryContext *c,
93                 InventoryManager *invmgr,
94                 ITextureSource *tsrc
95                 ):
96         GUIModalMenu(env, parent, id, menumgr),
97         m_menu_size(menu_size),
98         m_c(c),
99         m_invmgr(invmgr),
100         m_tsrc(tsrc)
101 {
102         m_selected_item = NULL;
103 }
104
105 GUIInventoryMenu::~GUIInventoryMenu()
106 {
107         removeChildren();
108
109         if(m_selected_item)
110                 delete m_selected_item;
111 }
112
113 void GUIInventoryMenu::removeChildren()
114 {
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++)
119         {
120                 children_copy.push_back(*i);
121         }
122         for(core::list<gui::IGUIElement*>::Iterator
123                         i = children_copy.begin();
124                         i != children_copy.end(); i++)
125         {
126                 (*i)->remove();
127         }
128         /*{
129                 gui::IGUIElement *e = getElementFromId(256);
130                 if(e != NULL)
131                         e->remove();
132         }*/
133 }
134
135 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
136 {
137         // Remove children
138         removeChildren();
139         
140         /*padding = v2s32(24,24);
141         spacing = v2s32(60,56);
142         imgsize = v2s32(48,48);*/
143
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);
147
148         s32 helptext_h = 15;
149
150         v2s32 size(
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
153         );
154
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
160         );
161         
162         DesiredRect = rect;
163         recalculateAbsolutePosition(false);
164
165         v2s32 basepos = getBasePos();
166         
167         m_draw_spec.clear();
168         for(u16 i=0; i<m_init_draw_spec.size(); i++)
169         {
170                 DrawSpec &s = m_init_draw_spec[i];
171                 if(s.type == "list")
172                 {
173                         m_draw_spec.push_back(ListDrawSpec(s.name, s.subname,
174                                         basepos + v2s32(spacing.X*s.pos.X, spacing.Y*s.pos.Y),
175                                         s.geom));
176                 }
177         }
178
179         /*
180         m_draw_spec.clear();
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)));
187         */
188         
189         // Add children
190         {
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);
197         }
198 }
199
200 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
201 {
202         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
203         
204         for(u32 i=0; i<m_draw_spec.size(); i++)
205         {
206                 const ListDrawSpec &s = m_draw_spec[i];
207
208                 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
209                 {
210                         s32 x = (i%s.geom.X) * spacing.X;
211                         s32 y = (i/s.geom.X) * spacing.Y;
212                         v2s32 p0(x,y);
213                         core::rect<s32> rect = imgrect + s.pos + p0;
214                         if(rect.isPointInside(p))
215                         {
216                                 return ItemSpec(s.inventoryname, s.listname, i);
217                         }
218                 }
219         }
220
221         return ItemSpec("", "", -1);
222 }
223
224 void GUIInventoryMenu::drawList(const ListDrawSpec &s, ITextureSource *tsrc)
225 {
226         video::IVideoDriver* driver = Environment->getVideoDriver();
227
228         // Get font
229         gui::IGUIFont *font = NULL;
230         gui::IGUISkin* skin = Environment->getSkin();
231         if (skin)
232                 font = skin->getFont();
233         
234         Inventory *inv = m_invmgr->getInventory(m_c, s.inventoryname);
235         assert(inv);
236         InventoryList *ilist = inv->getList(s.listname);
237         
238         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
239         
240         for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
241         {
242                 s32 x = (i%s.geom.X) * spacing.X;
243                 s32 y = (i/s.geom.X) * spacing.Y;
244                 v2s32 p(x,y);
245                 core::rect<s32> rect = imgrect + s.pos + p;
246                 InventoryItem *item = NULL;
247                 if(ilist)
248                         item = ilist->getItem(i);
249
250                 if(m_selected_item != NULL && m_selected_item->listname == s.listname
251                                 && m_selected_item->i == i)
252                 {
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),
257                                         NULL);
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)),
261                                         NULL);*/
262                         s32 border = 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);
267                 }
268
269                 video::SColor bgcolor(255,128,128,128);
270                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
271
272                 if(item)
273                 {
274                         drawInventoryItem(driver, font, item,
275                                         rect, &AbsoluteClippingRect, tsrc);
276                 }
277
278         }
279 }
280
281 void GUIInventoryMenu::drawMenu()
282 {
283         gui::IGUISkin* skin = Environment->getSkin();
284         if (!skin)
285                 return;
286         video::IVideoDriver* driver = Environment->getVideoDriver();
287         
288         video::SColor bgcolor(140,0,0,0);
289         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
290
291         /*
292                 Draw items
293         */
294         
295         for(u32 i=0; i<m_draw_spec.size(); i++)
296         {
297                 ListDrawSpec &s = m_draw_spec[i];
298                 drawList(s, m_tsrc);
299         }
300
301         /*
302                 Call base class
303         */
304         gui::IGUIElement::draw();
305 }
306
307 bool GUIInventoryMenu::OnEvent(const SEvent& event)
308 {
309         if(event.EventType==EET_KEY_INPUT_EVENT)
310         {
311                 KeyPress kp(event.KeyInput);
312                 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
313                         kp == getKeySetting("keymap_inventory")))
314                 {
315                         quitMenu();
316                         return true;
317                 }
318         }
319         if(event.EventType==EET_MOUSE_INPUT_EVENT)
320         {
321                 char amount = -1;
322                 
323                 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
324                         amount = 0;
325                 else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
326                         amount = 1;
327                 else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
328                         amount = 10;
329                 
330                 if(amount >= 0)
331                 {
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);
335                         if(s.isValid())
336                         {
337                                 infostream<<"Mouse down on "<<s.inventoryname
338                                                 <<"/"<<s.listname<<" "<<s.i<<std::endl;
339                                 if(m_selected_item)
340                                 {
341                                         Inventory *inv_from = m_invmgr->getInventory(m_c,
342                                                         m_selected_item->inventoryname);
343                                         Inventory *inv_to = m_invmgr->getInventory(m_c,
344                                                         s.inventoryname);
345                                         assert(inv_from);
346                                         assert(inv_to);
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;
353                                         if(list_to == NULL)
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)
359                                         {
360                                                 infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
361                                                 IMoveAction *a = new IMoveAction();
362                                                 a->count = amount;
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;
368                                                 a->to_i = s.i;
369                                                 //ispec.actions->push_back(a);
370                                                 m_invmgr->inventoryAction(a);
371                                                 
372                                                 if(list_from->getItem(m_selected_item->i)->getCount()==1)
373                                                         source_empties = true;
374                                         }
375                                         // Remove selection if target was left-clicked or source
376                                         // slot was emptied
377                                         if(amount == 0 || source_empties)
378                                         {
379                                                 delete m_selected_item;
380                                                 m_selected_item = NULL;
381                                         }
382                                 }
383                                 else
384                                 {
385                                         /*
386                                                 Select if non-NULL
387                                         */
388                                         Inventory *inv = m_invmgr->getInventory(m_c,
389                                                         s.inventoryname);
390                                         assert(inv);
391                                         InventoryList *list = inv->getList(s.listname);
392                                         if(list->getItem(s.i) != NULL)
393                                         {
394                                                 m_selected_item = new ItemSpec(s);
395                                         }
396                                 }
397                         }
398                         else
399                         {
400                                 if(m_selected_item)
401                                 {
402                                         delete m_selected_item;
403                                         m_selected_item = NULL;
404                                 }
405                         }
406                 }
407         }
408         if(event.EventType==EET_GUI_EVENT)
409         {
410                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
411                                 && isVisible())
412                 {
413                         if(!canTakeFocus(event.GUIEvent.Element))
414                         {
415                                 infostream<<"GUIInventoryMenu: Not allowing focus change."
416                                                 <<std::endl;
417                                 // Returning true disables focus change
418                                 return true;
419                         }
420                 }
421                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
422                 {
423                         /*switch(event.GUIEvent.Caller->getID())
424                         {
425                         case 256: // continue
426                                 setVisible(false);
427                                 break;
428                         case 257: // exit
429                                 dev->closeDevice();
430                                 break;
431                         }*/
432                 }
433         }
434
435         return Parent ? Parent->OnEvent(event) : false;
436 }
437
438 /*
439         Here is an example traditional set-up sequence for a DrawSpec list:
440
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);
456
457         Here is the string for creating the same DrawSpec list (a single line,
458         spread to multiple lines here):
459         
460         GUIInventoryMenu::makeDrawSpecArrayFromString(
461                         draw_spec,
462                         "nodemetadata:0,1,2",
463                         "invsize[8,9;]"
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;]");
468         
469         Returns inventory menu size defined by invsize[].
470 */
471 v2s16 GUIInventoryMenu::makeDrawSpecArrayFromString(
472                 core::array<GUIInventoryMenu::DrawSpec> &draw_spec,
473                 const std::string &data,
474                 const std::string &current_name)
475 {
476         v2s16 invsize(8,9);
477         Strfnd f(data);
478         while(f.atend() == false)
479         {
480                 std::string type = trim(f.next("["));
481                 //infostream<<"type="<<type<<std::endl;
482                 if(type == "list")
483                 {
484                         std::string name = f.next(";");
485                         if(name == "current_name")
486                                 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<<")"
495                                         <<std::endl;
496                         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
497                                         type, name, subname,
498                                         v2s32(pos_x,pos_y),v2s32(geom_x,geom_y)));
499                         f.next("]");
500                 }
501                 else if(type == "invsize")
502                 {
503                         invsize.X = stoi(f.next(","));
504                         invsize.Y = stoi(f.next(";"));
505                         infostream<<"invsize ("<<invsize.X<<","<<invsize.Y<<")"<<std::endl;
506                         f.next("]");
507                 }
508                 else
509                 {
510                         // Ignore others
511                         std::string ts = f.next("]");
512                         infostream<<"Unknown DrawSpec: type="<<type<<", data=\""<<ts<<"\""
513                                         <<std::endl;
514                 }
515         }
516
517         return invsize;
518 }
519