just savin'
[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
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                 if(font)
59                 {
60                         core::rect<s32> rect2(rect.UpperLeftCorner,
61                                         (core::dimension2d<u32>(rect.getWidth(), 15)));
62
63                         video::SColor bgcolor(128,0,0,0);
64                         driver->draw2DRectangle(bgcolor, rect2, clip);
65
66                         font->draw(item->getText().c_str(), rect2,
67                                         video::SColor(255,255,255,255), false, false,
68                                         clip);
69                 }
70         }
71 }
72
73 /*
74         GUIInventorySlot
75 */
76
77 GUIInventorySlot::GUIInventorySlot(gui::IGUIEnvironment* env,
78                 gui::IGUIElement* parent, s32 id, core::rect<s32> rect):
79         IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rect)
80 {
81         m_item = NULL;
82 }
83
84 void GUIInventorySlot::draw()
85 {
86         if(!IsVisible)
87                 return;
88         
89         drawInventoryItem(Environment, m_item, AbsoluteRect,
90                         &AbsoluteClippingRect);
91
92         gui::IGUIElement::draw();
93 }
94
95 bool GUIInventorySlot::OnEvent(const SEvent& event)
96 {
97         /*if (!IsEnabled)
98                 return IGUIElement::OnEvent(event);*/
99
100         switch(event.EventType)
101         {
102         case EET_MOUSE_INPUT_EVENT:
103                 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
104                 {
105                         dstream<<"Slot pressed"<<std::endl;
106                         //return true;
107                 }
108                 else
109                 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
110                 {
111                         dstream<<"Slot released"<<std::endl;
112                         //return true;
113                 }
114                 break;
115         default:
116                 break;
117         }
118
119         return Parent ? Parent->OnEvent(event) : false;
120 }
121
122 /*
123         GUIInventoryMenu
124 */
125
126 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
127                 gui::IGUIElement* parent, s32 id,
128                 Inventory *inventory):
129         IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
130                         core::rect<s32>(0,0,100,100))
131 {
132         m_inventory = inventory;
133         m_screensize_old = v2u32(0,0);
134
135         resizeGui();
136
137         setVisible(false);
138 }
139
140 GUIInventoryMenu::~GUIInventoryMenu()
141 {
142 }
143
144 void GUIInventoryMenu::resizeGui()
145 {
146         video::IVideoDriver* driver = Environment->getVideoDriver();
147         v2u32 screensize = driver->getScreenSize();
148         if(screensize == m_screensize_old)
149                 return;
150         m_screensize_old = screensize;
151
152         core::rect<s32> rect(
153                         screensize.X/2 - 560/2,
154                         screensize.Y/2 - 480/2,
155                         screensize.X/2 + 560/2,
156                         screensize.Y/2 + 480/2
157         );
158         
159         DesiredRect = rect;
160         recalculateAbsolutePosition(false);
161 }
162
163 void GUIInventoryMenu::update()
164 {
165 }
166
167 void GUIInventoryMenu::draw()
168 {
169         if(!IsVisible)
170                 return;
171                 
172         gui::IGUISkin* skin = Environment->getSkin();
173         if (!skin)
174                 return;
175         video::IVideoDriver* driver = Environment->getVideoDriver();
176         
177         video::SColor bgcolor(140,0,0,0);
178         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
179
180         /*
181                 Draw items
182         */
183         
184         {
185                 InventoryList *ilist = m_inventory->getList("main");
186                 if(ilist != NULL)
187                 {
188                         core::rect<s32> imgsize(0,0,48,48);
189                         v2s32 basepos(30, 210);
190                         basepos += AbsoluteRect.UpperLeftCorner;
191                         for(s32 i=0; i<PLAYER_INVENTORY_SIZE; i++)
192                         {
193                                 s32 x = (i%8) * 64;
194                                 s32 y = (i/8) * 64;
195                                 v2s32 p(x,y);
196                                 core::rect<s32> rect = imgsize + basepos + p;
197                                 drawInventoryItem(Environment, ilist->getItem(i),
198                                                 rect, &AbsoluteClippingRect);
199                         }
200                 }
201         }
202
203         {
204                 InventoryList *ilist = m_inventory->getList("craft");
205                 if(ilist != NULL)
206                 {
207                         core::rect<s32> imgsize(0,0,48,48);
208                         v2s32 basepos(30, 30);
209                         basepos += AbsoluteRect.UpperLeftCorner;
210                         for(s32 i=0; i<9; i++)
211                         {
212                                 s32 x = (i%3) * 64;
213                                 s32 y = (i/3) * 64;
214                                 v2s32 p(x,y);
215                                 core::rect<s32> rect = imgsize + basepos + p;
216                                 drawInventoryItem(Environment, ilist->getItem(i),
217                                                 rect, &AbsoluteClippingRect);
218                         }
219                 }
220         }
221
222         /*
223                 Call base class
224         */
225         gui::IGUIElement::draw();
226 }
227
228 bool GUIInventoryMenu::OnEvent(const SEvent& event)
229 {
230         if(event.EventType==EET_KEY_INPUT_EVENT)
231         {
232                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
233                 {
234                         setVisible(false);
235                         return true;
236                 }
237                 if(event.KeyInput.Key==KEY_KEY_I && event.KeyInput.PressedDown)
238                 {
239                         setVisible(false);
240                         return true;
241                 }
242         }
243         if(event.EventType==EET_MOUSE_INPUT_EVENT)
244         {
245                 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
246                 {
247                 }
248         }
249         if(event.EventType==EET_GUI_EVENT)
250         {
251                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
252                                 && isVisible())
253                 {
254                         if(!canTakeFocus(event.GUIEvent.Element))
255                         {
256                                 dstream<<"GUIInventoryMenu: Not allowing focus change."
257                                                 <<std::endl;
258                                 // Returning true disables focus change
259                                 return true;
260                         }
261                 }
262                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
263                 {
264                         /*switch(event.GUIEvent.Caller->getID())
265                         {
266                         case 256: // continue
267                                 setVisible(false);
268                                 break;
269                         case 257: // exit
270                                 dev->closeDevice();
271                                 break;
272                         }*/
273                 }
274         }
275
276         return Parent ? Parent->OnEvent(event) : false;
277 }
278
279