]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiInventoryMenu.cpp
minecraft-like crafting
[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
24 void drawInventoryItem(gui::IGUIEnvironment* env,
25                 InventoryItem *item, core::rect<s32> rect,
26                 const core::rect<s32> *clip)
27 {
28         gui::IGUISkin* skin = env->getSkin();
29         if (!skin)
30                 return;
31         video::IVideoDriver* driver = env->getVideoDriver();
32         
33         video::ITexture *texture = NULL;
34         
35         if(item != NULL)
36         {
37                 texture = item->getImage();
38         }
39
40         if(texture != NULL)
41         {
42                 const video::SColor color(255,255,255,255);
43                 const video::SColor colors[] = {color,color,color,color};
44                 driver->draw2DImage(texture, rect,
45                         core::rect<s32>(core::position2d<s32>(0,0),
46                         core::dimension2di(texture->getOriginalSize())),
47                         clip, colors, false);
48         }
49         else
50         {
51                 video::SColor bgcolor(128,128,128,128);
52                 driver->draw2DRectangle(bgcolor, rect, clip);
53         }
54
55         if(item != NULL)
56         {
57                 gui::IGUIFont *font = skin->getFont();
58                 std::string text = item->getText();
59                 if(font && text != "")
60                 {
61                         core::rect<s32> rect2(rect.UpperLeftCorner,
62                                         (core::dimension2d<u32>(rect.getWidth(), 15)));
63
64                         video::SColor bgcolor(128,0,0,0);
65                         driver->draw2DRectangle(bgcolor, rect2, clip);
66
67                         font->draw(text.c_str(), rect2,
68                                         video::SColor(255,255,255,255), false, false,
69                                         clip);
70                 }
71         }
72 }
73
74 /*
75         GUIInventoryMenu
76 */
77
78 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
79                 gui::IGUIElement* parent, s32 id,
80                 Inventory *inventory,
81                 Queue<InventoryAction*> *actions,
82                 int *active_menu_count):
83         GUIModalMenu(env, parent, id, active_menu_count)
84 {
85         m_inventory = inventory;
86         m_selected_item = NULL;
87         m_actions = actions;
88
89         /*m_selected_item = new ItemSpec;
90         m_selected_item->listname = "main";
91         m_selected_item->i = 3;*/
92 }
93
94 GUIInventoryMenu::~GUIInventoryMenu()
95 {
96         if(m_selected_item)
97                 delete m_selected_item;
98 }
99
100 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
101 {
102         padding = v2s32(24,24);
103         spacing = v2s32(60,56);
104         imgsize = v2s32(48,48);
105
106         v2s32 size(
107                 padding.X*2+spacing.X*(8-1)+imgsize.X,
108                 padding.Y*2+spacing.Y*(7-1)+imgsize.Y
109         );
110
111         core::rect<s32> rect(
112                         screensize.X/2 - size.X/2,
113                         screensize.Y/2 - size.Y/2,
114                         screensize.X/2 + size.X/2,
115                         screensize.Y/2 + size.Y/2
116         );
117         
118         DesiredRect = rect;
119         recalculateAbsolutePosition(false);
120
121         v2s32 basepos = getBasePos();
122         
123         m_draw_positions.clear();
124         m_draw_positions.push_back(ListDrawSpec("main",
125                         basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
126         m_draw_positions.push_back(ListDrawSpec("craft",
127                         basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
128         m_draw_positions.push_back(ListDrawSpec("craftresult",
129                         basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
130 }
131
132 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
133 {
134         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
135         
136         for(u32 i=0; i<m_draw_positions.size(); i++)
137         {
138                 const ListDrawSpec &s = m_draw_positions[i];
139
140                 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
141                 {
142                         s32 x = (i%s.geom.X) * spacing.X;
143                         s32 y = (i/s.geom.X) * spacing.Y;
144                         v2s32 p0(x,y);
145                         core::rect<s32> rect = imgrect + s.pos + p0;
146                         if(rect.isPointInside(p))
147                         {
148                                 return ItemSpec(s.listname, i);
149                         }
150                 }
151         }
152
153         return ItemSpec("", -1);
154 }
155
156 //void GUIInventoryMenu::drawList(const std::string &name, v2s32 pos, v2s32 geom)
157 void GUIInventoryMenu::drawList(const ListDrawSpec &s)
158 {
159         video::IVideoDriver* driver = Environment->getVideoDriver();
160
161         InventoryList *ilist = m_inventory->getList(s.listname);
162         
163         core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
164
165         for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
166         {
167                 s32 x = (i%s.geom.X) * spacing.X;
168                 s32 y = (i/s.geom.X) * spacing.Y;
169                 v2s32 p(x,y);
170                 core::rect<s32> rect = imgrect + s.pos + p;
171                 InventoryItem *item = NULL;
172                 if(ilist)
173                         item = ilist->getItem(i);
174
175                 if(m_selected_item != NULL && m_selected_item->listname == s.listname
176                                 && m_selected_item->i == i)
177                 {
178                         driver->draw2DRectangle(video::SColor(255,255,0,0),
179                                         core::rect<s32>(rect.UpperLeftCorner - v2s32(2,2),
180                                                         rect.LowerRightCorner + v2s32(2,2)),
181                                                         &AbsoluteClippingRect);
182                 }
183                 drawInventoryItem(Environment, item,
184                                 rect, &AbsoluteClippingRect);
185         }
186 }
187
188 void GUIInventoryMenu::drawMenu()
189 {
190         gui::IGUISkin* skin = Environment->getSkin();
191         if (!skin)
192                 return;
193         video::IVideoDriver* driver = Environment->getVideoDriver();
194         
195         video::SColor bgcolor(140,0,0,0);
196         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
197
198         /*
199                 Draw items
200         */
201         
202         for(u32 i=0; i<m_draw_positions.size(); i++)
203         {
204                 ListDrawSpec &s = m_draw_positions[i];
205                 drawList(s);
206         }
207
208         /*
209                 Call base class
210         */
211         gui::IGUIElement::draw();
212 }
213
214 bool GUIInventoryMenu::OnEvent(const SEvent& event)
215 {
216         if(event.EventType==EET_KEY_INPUT_EVENT)
217         {
218                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
219                 {
220                         quitMenu();
221                         return true;
222                 }
223                 if(event.KeyInput.Key==KEY_KEY_I && event.KeyInput.PressedDown)
224                 {
225                         quitMenu();
226                         return true;
227                 }
228         }
229         if(event.EventType==EET_MOUSE_INPUT_EVENT)
230         {
231                 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN
232                                 || event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
233                 {
234                         bool right = (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN);
235                         v2s32 p(event.MouseInput.X, event.MouseInput.Y);
236                         //dstream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
237                         ItemSpec s = getItemAtPos(p);
238                         if(s.isValid())
239                         {
240                                 //dstream<<"Mouse down on "<<s.listname<<" "<<s.i<<std::endl;
241                                 if(m_selected_item)
242                                 {
243                                         InventoryList *list_from =
244                                                         m_inventory->getList(m_selected_item->listname);
245                                         InventoryList *list_to =
246                                                         m_inventory->getList(s.listname);
247                                         if(list_from && list_to
248                                                         && list_from->getItem(m_selected_item->i) != NULL)
249                                         {
250                                                 dstream<<"Queueing IACTION_MOVE"<<std::endl;
251                                                 IMoveAction *a =
252                                                         new IMoveAction();
253                                                 a->count = right ? 1 : 0;
254                                                 a->from_name = m_selected_item->listname;
255                                                 a->from_i = m_selected_item->i;
256                                                 a->to_name = s.listname;
257                                                 a->to_i = s.i;
258                                                 m_actions->push_back(a);
259                                         }
260                                         bool source_empties = false;
261                                         if(list_from && list_from->getItem(m_selected_item->i)->getCount()==1)
262                                                 source_empties = true;
263                                         if(right == false || source_empties)
264                                         {
265                                                 delete m_selected_item;
266                                                 m_selected_item = NULL;
267                                         }
268                                 }
269                                 else
270                                 {
271                                         /*
272                                                 Select if non-NULL
273                                         */
274                                         InventoryList *list = m_inventory->getList(s.listname);
275                                         if(list->getItem(s.i) != NULL)
276                                         {
277                                                 m_selected_item = new ItemSpec(s);
278                                         }
279                                 }
280                         }
281                         else
282                         {
283                                 if(m_selected_item)
284                                 {
285                                         delete m_selected_item;
286                                         m_selected_item = NULL;
287                                 }
288                         }
289                 }
290         }
291         if(event.EventType==EET_GUI_EVENT)
292         {
293                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
294                                 && isVisible())
295                 {
296                         if(!canTakeFocus(event.GUIEvent.Element))
297                         {
298                                 dstream<<"GUIInventoryMenu: Not allowing focus change."
299                                                 <<std::endl;
300                                 // Returning true disables focus change
301                                 return true;
302                         }
303                 }
304                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
305                 {
306                         /*switch(event.GUIEvent.Caller->getID())
307                         {
308                         case 256: // continue
309                                 setVisible(false);
310                                 break;
311                         case 257: // exit
312                                 dev->closeDevice();
313                                 break;
314                         }*/
315                 }
316         }
317
318         return Parent ? Parent->OnEvent(event) : false;
319 }
320
321
322