Show git hash in version string at top left corner of window
[oweals/minetest.git] / src / guiPauseMenu.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #include "guiPauseMenu.h"
21 #include "debug.h"
22 #include "serialization.h"
23 #include "porting.h"
24 #include "config.h"
25 #include "version.h"
26 #include "main.h"
27 #include <IGUICheckBox.h>
28 #include <IGUIEditBox.h>
29 #include <IGUIButton.h>
30 #include <IGUIStaticText.h>
31 #include <IGUIFont.h>
32 #include "gettext.h"
33 #include "util/string.h"
34
35 GUIPauseMenu::GUIPauseMenu(gui::IGUIEnvironment* env,
36                 gui::IGUIElement* parent, s32 id,
37                 IGameCallback *gamecallback,
38                 IMenuManager *menumgr,
39                 bool simple_singleplayer_mode):
40         GUIModalMenu(env, parent, id, menumgr),
41         m_gamecallback(gamecallback),
42         m_simple_singleplayer_mode(simple_singleplayer_mode)
43 {
44 }
45
46 GUIPauseMenu::~GUIPauseMenu()
47 {
48         removeChildren();
49 }
50
51 void GUIPauseMenu::removeChildren()
52 {
53         {
54                 gui::IGUIElement *e = getElementFromId(256);
55                 if(e != NULL)
56                         e->remove();
57         }
58         {
59                 gui::IGUIElement *e = getElementFromId(257);
60                 if(e != NULL)
61                         e->remove();
62         }
63         {
64                 gui::IGUIElement *e = getElementFromId(258);
65                 if(e != NULL)
66                         e->remove();
67         }
68         {
69                 gui::IGUIElement *e = getElementFromId(259);
70                 if(e != NULL)
71                         e->remove();
72         }
73         {
74                 gui::IGUIElement *e = getElementFromId(260);
75                 if(e != NULL)
76                         e->remove();
77         }
78         {
79                 gui::IGUIElement *e = getElementFromId(261);
80                 if(e != NULL)
81                         e->remove();
82         }
83         {
84                 gui::IGUIElement *e = getElementFromId(262);
85                 if(e != NULL)
86                         e->remove();
87         }
88 }
89
90 void GUIPauseMenu::regenerateGui(v2u32 screensize)
91 {
92         /*
93                 Remove stuff
94         */
95         removeChildren();
96         
97         /*
98                 Calculate new sizes and positions
99         */
100         core::rect<s32> rect(
101                         screensize.X/2 - 580/2,
102                         screensize.Y/2 - 300/2,
103                         screensize.X/2 + 580/2,
104                         screensize.Y/2 + 300/2
105         );
106         
107         DesiredRect = rect;
108         recalculateAbsolutePosition(false);
109
110         v2s32 size = rect.getSize();
111
112         /*
113                 Add stuff
114         */
115         const s32 btn_height = 30;
116         const s32 btn_gap = 20;
117         const s32 btn_num = m_simple_singleplayer_mode ? 4 : 5;
118         s32 btn_y = size.Y/2-((btn_num*btn_height+(btn_num-1)*btn_gap))/2;
119         changeCtype("");
120         {
121                 core::rect<s32> rect(0, 0, 140, btn_height);
122                 rect = rect + v2s32(size.X/2-140/2, btn_y);
123                 wchar_t* text = wgettext("Continue");
124                 Environment->addButton(rect, this, 256,
125                         text);
126                 delete[] text;
127         }
128         btn_y += btn_height + btn_gap;
129         if(!m_simple_singleplayer_mode)
130         {
131                 {
132                         core::rect<s32> rect(0, 0, 140, btn_height);
133                         rect = rect + v2s32(size.X/2-140/2, btn_y);
134                         wchar_t* text = wgettext("Change Password");
135                         Environment->addButton(rect, this, 261,
136                                 text);
137                         delete[] text;
138                 }
139                 btn_y += btn_height + btn_gap;
140         }
141         {
142                 core::rect<s32> rect(0, 0, 140, btn_height);
143                 rect = rect + v2s32(size.X/2-140/2, btn_y);
144                 wchar_t* text = wgettext("Sound Volume");
145                 Environment->addButton(rect, this, 262,
146                         text);
147                 delete[] text;
148         }
149         btn_y += btn_height + btn_gap;
150         {
151                 core::rect<s32> rect(0, 0, 140, btn_height);
152                 rect = rect + v2s32(size.X/2-140/2, btn_y);
153                 wchar_t* text = wgettext("Exit to Menu");
154                 Environment->addButton(rect, this, 260,
155                         text);
156                 delete[] text;
157         }
158         btn_y += btn_height + btn_gap;
159         {
160                 core::rect<s32> rect(0, 0, 140, btn_height);
161                 rect = rect + v2s32(size.X/2-140/2, btn_y);
162                 wchar_t* text = wgettext("Exit to OS");
163                 Environment->addButton(rect, this, 257,
164                         text);
165                 delete[] text;
166         }
167
168         {
169                 core::rect<s32> rect(0, 0, 180, 240);
170                 rect = rect + v2s32(size.X/2 + 90, size.Y/2-rect.getHeight()/2);
171                 wchar_t* text = wgettext("Default Controls:\n"
172                                 "- WASD: move\n"
173                                 "- Space: jump/climb\n"
174                                 "- Shift: sneak/go down\n"
175                                 "- Q: drop item\n"
176                                 "- I: inventory\n"
177                                 "- Mouse: turn/look\n"
178                                 "- Mouse left: dig/punch\n"
179                                 "- Mouse right: place/use\n"
180                                 "- Mouse wheel: select item\n"
181                                 "- T: chat\n"
182                                 );
183                 Environment->addStaticText(text, rect, false, true, this, 258);
184                 delete[] text;
185
186         }
187         {
188                 core::rect<s32> rect(0, 0, 180, 220);
189                 rect = rect + v2s32(size.X/2 - 90 - rect.getWidth(), size.Y/2-rect.getHeight()/2);
190         
191                 v2u32 max_texture_size;
192                 {
193                         video::IVideoDriver* driver = Environment->getVideoDriver();
194                         max_texture_size = driver->getMaxTextureSize();
195                 }
196
197                 std::ostringstream os;
198                 os<<"Minetest\n";
199                 os<<minetest_build_info<<"\n";
200                 os<<"path_user = "<<wrap_rows(porting::path_user, 20)<<"\n";
201         
202                 Environment->addStaticText(narrow_to_wide(os.str()).c_str(), rect, false, true, this, 259);
203         }
204         changeCtype("C");
205 }
206
207 void GUIPauseMenu::drawMenu()
208 {
209         gui::IGUISkin* skin = Environment->getSkin();
210         if (!skin)
211                 return;
212         video::IVideoDriver* driver = Environment->getVideoDriver();
213         
214         video::SColor bgcolor(140,0,0,0);
215         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
216
217         gui::IGUIElement::draw();
218 }
219
220 bool GUIPauseMenu::OnEvent(const SEvent& event)
221 {
222
223         if(event.EventType==EET_KEY_INPUT_EVENT)
224         {
225                 if(event.KeyInput.PressedDown)
226                 {
227                         if(event.KeyInput.Key==KEY_ESCAPE)
228                         {
229                                 quitMenu();
230                                 return true;
231                         }
232                         else if(event.KeyInput.Key==KEY_RETURN)
233                         {
234                                 quitMenu();
235                                 return true;
236                         }
237                 }
238         }
239         if(event.EventType==EET_GUI_EVENT)
240         {
241                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
242                                 && isVisible())
243                 {
244                         if(!canTakeFocus(event.GUIEvent.Element))
245                         {
246                                 dstream<<"GUIPauseMenu: Not allowing focus change."
247                                                 <<std::endl;
248                                 // Returning true disables focus change
249                                 return true;
250                         }
251                 }
252                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
253                 {
254                         switch(event.GUIEvent.Caller->getID())
255                         {
256                         case 256: // continue
257                                 quitMenu();
258                                 // ALWAYS return immediately after quitMenu()
259                                 return true;
260                         case 261:
261                                 quitMenu();
262                                 m_gamecallback->changePassword();
263                                 return true;
264                         case 262:
265                                 quitMenu();
266                                 m_gamecallback->changeVolume();
267                                 return true;
268                         case 260: // disconnect
269                                 m_gamecallback->disconnect();
270                                 quitMenu();
271                                 return true;
272                         case 257: // exit
273                                 m_gamecallback->exitToOS();
274                                 quitMenu();
275                                 return true;
276                         }
277                 }
278         }
279         
280         return Parent ? Parent->OnEvent(event) : false;
281 }
282