Change way commands are displayed in chat window
[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, false);
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                         driver->draw2DRectangle(video::SColor(255,255,0,0),
245                                         core::rect<s32>(rect.UpperLeftCorner - v2s32(2,2),
246                                                         rect.LowerRightCorner + v2s32(2,2)),
247                                         &AbsoluteClippingRect);
248                 }
249
250                 if(item)
251                 {
252                         drawInventoryItem(driver, font, item,
253                                         rect, &AbsoluteClippingRect);
254                 }
255                 else
256                 {
257                         video::SColor bgcolor(255,128,128,128);
258                         driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
259                 }
260         }
261 }
262
263 void GUIInventoryMenu::drawMenu()
264 {
265         gui::IGUISkin* skin = Environment->getSkin();
266         if (!skin)
267                 return;
268         video::IVideoDriver* driver = Environment->getVideoDriver();
269         
270         video::SColor bgcolor(140,0,0,0);
271         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
272
273         /*
274                 Draw items
275         */
276         
277         for(u32 i=0; i<m_draw_spec.size(); i++)
278         {
279                 ListDrawSpec &s = m_draw_spec[i];
280                 drawList(s);
281         }
282
283         /*
284                 Call base class
285         */
286         gui::IGUIElement::draw();
287 }
288
289 bool GUIInventoryMenu::OnEvent(const SEvent& event)
290 {
291         if(event.EventType==EET_KEY_INPUT_EVENT)
292         {
293                 KeyPress kp(event.KeyInput);
294                 if (event.KeyInput.PressedDown && (kp == EscapeKey ||
295                         kp == getKeySetting("keymap_inventory")))
296                 {
297                         quitMenu();
298                         return true;
299                 }
300         }
301         if(event.EventType==EET_MOUSE_INPUT_EVENT)
302         {
303                 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN
304                                 || event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
305                 {
306                         bool right = (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN);
307                         v2s32 p(event.MouseInput.X, event.MouseInput.Y);
308                         //dstream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
309                         ItemSpec s = getItemAtPos(p);
310                         if(s.isValid())
311                         {
312                                 dstream<<"Mouse down on "<<s.inventoryname
313                                                 <<"/"<<s.listname<<" "<<s.i<<std::endl;
314                                 if(m_selected_item)
315                                 {
316                                         Inventory *inv_from = m_invmgr->getInventory(m_c,
317                                                         m_selected_item->inventoryname);
318                                         Inventory *inv_to = m_invmgr->getInventory(m_c,
319                                                         s.inventoryname);
320                                         assert(inv_from);
321                                         assert(inv_to);
322                                         InventoryList *list_from =
323                                                         inv_from->getList(m_selected_item->listname);
324                                         InventoryList *list_to =
325                                                         inv_to->getList(s.listname);
326                                         if(list_from == NULL)
327                                                 dstream<<"from list doesn't exist"<<std::endl;
328                                         if(list_to == NULL)
329                                                 dstream<<"to list doesn't exist"<<std::endl;
330                                         // Indicates whether source slot completely empties
331                                         bool source_empties = false;
332                                         if(list_from && list_to
333                                                         && list_from->getItem(m_selected_item->i) != NULL)
334                                         {
335                                                 dstream<<"Handing IACTION_MOVE to manager"<<std::endl;
336                                                 IMoveAction *a = new IMoveAction();
337                                                 a->count = right ? 1 : 0;
338                                                 a->from_inv = m_selected_item->inventoryname;
339                                                 a->from_list = m_selected_item->listname;
340                                                 a->from_i = m_selected_item->i;
341                                                 a->to_inv = s.inventoryname;
342                                                 a->to_list = s.listname;
343                                                 a->to_i = s.i;
344                                                 //ispec.actions->push_back(a);
345                                                 m_invmgr->inventoryAction(a);
346                                                 
347                                                 if(list_from->getItem(m_selected_item->i)->getCount()==1)
348                                                         source_empties = true;
349                                         }
350                                         // Remove selection if target was left-clicked or source
351                                         // slot was emptied
352                                         if(right == false || source_empties)
353                                         {
354                                                 delete m_selected_item;
355                                                 m_selected_item = NULL;
356                                         }
357                                 }
358                                 else
359                                 {
360                                         /*
361                                                 Select if non-NULL
362                                         */
363                                         Inventory *inv = m_invmgr->getInventory(m_c,
364                                                         s.inventoryname);
365                                         assert(inv);
366                                         InventoryList *list = inv->getList(s.listname);
367                                         if(list->getItem(s.i) != NULL)
368                                         {
369                                                 m_selected_item = new ItemSpec(s);
370                                         }
371                                 }
372                         }
373                         else
374                         {
375                                 if(m_selected_item)
376                                 {
377                                         delete m_selected_item;
378                                         m_selected_item = NULL;
379                                 }
380                         }
381                 }
382         }
383         if(event.EventType==EET_GUI_EVENT)
384         {
385                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
386                                 && isVisible())
387                 {
388                         if(!canTakeFocus(event.GUIEvent.Element))
389                         {
390                                 dstream<<"GUIInventoryMenu: Not allowing focus change."
391                                                 <<std::endl;
392                                 // Returning true disables focus change
393                                 return true;
394                         }
395                 }
396                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
397                 {
398                         /*switch(event.GUIEvent.Caller->getID())
399                         {
400                         case 256: // continue
401                                 setVisible(false);
402                                 break;
403                         case 257: // exit
404                                 dev->closeDevice();
405                                 break;
406                         }*/
407                 }
408         }
409
410         return Parent ? Parent->OnEvent(event) : false;
411 }
412
413 /*
414         Here is an example traditional set-up sequence for a DrawSpec list:
415
416         std::string furnace_inv_id = "nodemetadata:0,1,2";
417         core::array<GUIInventoryMenu::DrawSpec> draw_spec;
418         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
419                         "list", furnace_inv_id, "fuel",
420                         v2s32(2, 3), v2s32(1, 1)));
421         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
422                         "list", furnace_inv_id, "src",
423                         v2s32(2, 1), v2s32(1, 1)));
424         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
425                         "list", furnace_inv_id, "dst",
426                         v2s32(5, 1), v2s32(2, 2)));
427         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
428                         "list", "current_player", "main",
429                         v2s32(0, 5), v2s32(8, 4)));
430         setDrawSpec(draw_spec);
431
432         Here is the string for creating the same DrawSpec list (a single line,
433         spread to multiple lines here):
434         
435         GUIInventoryMenu::makeDrawSpecArrayFromString(
436                         draw_spec,
437                         "nodemetadata:0,1,2",
438                         "invsize[8,9;]"
439                         "list[current_name;fuel;2,3;1,1;]"
440                         "list[current_name;src;2,1;1,1;]"
441                         "list[current_name;dst;5,1;2,2;]"
442                         "list[current_player;main;0,5;8,4;]");
443         
444         Returns inventory menu size defined by invsize[].
445 */
446 v2s16 GUIInventoryMenu::makeDrawSpecArrayFromString(
447                 core::array<GUIInventoryMenu::DrawSpec> &draw_spec,
448                 const std::string &data,
449                 const std::string &current_name)
450 {
451         v2s16 invsize(8,9);
452         Strfnd f(data);
453         while(f.atend() == false)
454         {
455                 std::string type = trim(f.next("["));
456                 //dstream<<"type="<<type<<std::endl;
457                 if(type == "list")
458                 {
459                         std::string name = f.next(";");
460                         if(name == "current_name")
461                                 name = current_name;
462                         std::string subname = f.next(";");
463                         s32 pos_x = stoi(f.next(","));
464                         s32 pos_y = stoi(f.next(";"));
465                         s32 geom_x = stoi(f.next(","));
466                         s32 geom_y = stoi(f.next(";"));
467                         dstream<<"list name="<<name<<", subname="<<subname
468                                         <<", pos=("<<pos_x<<","<<pos_y<<")"
469                                         <<", geom=("<<geom_x<<","<<geom_y<<")"
470                                         <<std::endl;
471                         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
472                                         type, name, subname,
473                                         v2s32(pos_x,pos_y),v2s32(geom_x,geom_y)));
474                         f.next("]");
475                 }
476                 else if(type == "invsize")
477                 {
478                         invsize.X = stoi(f.next(","));
479                         invsize.Y = stoi(f.next(";"));
480                         dstream<<"invsize ("<<invsize.X<<","<<invsize.Y<<")"<<std::endl;
481                         f.next("]");
482                 }
483                 else
484                 {
485                         // Ignore others
486                         std::string ts = f.next("]");
487                         dstream<<"Unknown DrawSpec: type="<<type<<", data=\""<<ts<<"\""
488                                         <<std::endl;
489                 }
490         }
491
492         return invsize;
493 }
494