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