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