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