]> git.lizzy.rs Git - minetest.git/blob - src/guiPauseMenu.cpp
Fix crash when no world is selected and configure button is pressed.
[minetest.git] / src / guiPauseMenu.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 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
84 void GUIPauseMenu::regenerateGui(v2u32 screensize)
85 {
86         /*
87                 Remove stuff
88         */
89         removeChildren();
90         
91         /*
92                 Calculate new sizes and positions
93         */
94         core::rect<s32> rect(
95                         screensize.X/2 - 580/2,
96                         screensize.Y/2 - 300/2,
97                         screensize.X/2 + 580/2,
98                         screensize.Y/2 + 300/2
99         );
100         
101         DesiredRect = rect;
102         recalculateAbsolutePosition(false);
103
104         v2s32 size = rect.getSize();
105
106         /*
107                 Add stuff
108         */
109         const s32 btn_height = 30;
110         const s32 btn_gap = 20;
111         const s32 btn_num = m_simple_singleplayer_mode ? 3 : 4;
112         s32 btn_y = size.Y/2-((btn_num*btn_height+(btn_num-1)*btn_gap))/2;
113         changeCtype("");
114         {
115                 core::rect<s32> rect(0, 0, 140, btn_height);
116                 rect = rect + v2s32(size.X/2-140/2, btn_y);
117                 Environment->addButton(rect, this, 256,
118                         wgettext("Continue"));
119         }
120         btn_y += btn_height + btn_gap;
121         if(!m_simple_singleplayer_mode)
122         {
123                 {
124                         core::rect<s32> rect(0, 0, 140, btn_height);
125                         rect = rect + v2s32(size.X/2-140/2, btn_y);
126                         Environment->addButton(rect, this, 261,
127                                 wgettext("Change Password"));
128                 }
129                 btn_y += btn_height + btn_gap;
130         }
131         {
132                 core::rect<s32> rect(0, 0, 140, btn_height);
133                 rect = rect + v2s32(size.X/2-140/2, btn_y);
134                 Environment->addButton(rect, this, 260,
135                         wgettext("Exit to Menu"));
136         }
137         btn_y += btn_height + btn_gap;
138         {
139                 core::rect<s32> rect(0, 0, 140, btn_height);
140                 rect = rect + v2s32(size.X/2-140/2, btn_y);
141                 Environment->addButton(rect, this, 257,
142                         wgettext("Exit to OS"));
143         }
144
145         {
146                 core::rect<s32> rect(0, 0, 180, 240);
147                 rect = rect + v2s32(size.X/2 + 90, size.Y/2-rect.getHeight()/2);
148                 Environment->addStaticText(chartowchar_t(gettext(
149                 "Default Controls:\n"
150                 "- WASD: Walk\n"
151                 "- Mouse left: dig/hit\n"
152                 "- Mouse right: place/use\n"
153                 "- Mouse wheel: select item\n"
154                 "- 0...9: select item\n"
155                 "- Shift: sneak\n"
156                 "- R: Toggle viewing all loaded chunks\n"
157                 "- I: Inventory menu\n"
158                 "- ESC: This menu\n"
159                 "- T: Chat\n"
160                 )), rect, false, true, this, 258);
161         }
162         {
163                 core::rect<s32> rect(0, 0, 180, 220);
164                 rect = rect + v2s32(size.X/2 - 90 - rect.getWidth(), size.Y/2-rect.getHeight()/2);
165         
166                 v2u32 max_texture_size;
167                 {
168                         video::IVideoDriver* driver = Environment->getVideoDriver();
169                         max_texture_size = driver->getMaxTextureSize();
170                 }
171
172                 std::ostringstream os;
173                 os<<"Minetest\n";
174                 os<<BUILD_INFO<<"\n";
175                 os<<"path_user = "<<wrap_rows(porting::path_user, 20)<<"\n";
176         
177                 Environment->addStaticText(narrow_to_wide(os.str()).c_str(), rect, false, true, this, 259);
178         }
179         changeCtype("C");
180 }
181
182 void GUIPauseMenu::drawMenu()
183 {
184         gui::IGUISkin* skin = Environment->getSkin();
185         if (!skin)
186                 return;
187         video::IVideoDriver* driver = Environment->getVideoDriver();
188         
189         video::SColor bgcolor(140,0,0,0);
190         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
191
192         gui::IGUIElement::draw();
193 }
194
195 bool GUIPauseMenu::OnEvent(const SEvent& event)
196 {
197
198         if(event.EventType==EET_KEY_INPUT_EVENT)
199         {
200                 if(event.KeyInput.PressedDown)
201                 {
202                         if(event.KeyInput.Key==KEY_ESCAPE)
203                         {
204                                 quitMenu();
205                                 return true;
206                         }
207                         else if(event.KeyInput.Key==KEY_RETURN)
208                         {
209                                 quitMenu();
210                                 return true;
211                         }
212                 }
213         }
214         if(event.EventType==EET_GUI_EVENT)
215         {
216                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
217                                 && isVisible())
218                 {
219                         if(!canTakeFocus(event.GUIEvent.Element))
220                         {
221                                 dstream<<"GUIPauseMenu: Not allowing focus change."
222                                                 <<std::endl;
223                                 // Returning true disables focus change
224                                 return true;
225                         }
226                 }
227                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
228                 {
229                         switch(event.GUIEvent.Caller->getID())
230                         {
231                         case 256: // continue
232                                 quitMenu();
233                                 // ALWAYS return immediately after quitMenu()
234                                 return true;
235                         case 261:
236                                 quitMenu();
237                                 m_gamecallback->changePassword();
238                                 return true;
239                         case 260: // disconnect
240                                 m_gamecallback->disconnect();
241                                 quitMenu();
242                                 return true;
243                         case 257: // exit
244                                 m_gamecallback->exitToOS();
245                                 quitMenu();
246                                 return true;
247                         }
248                 }
249         }
250         
251         return Parent ? Parent->OnEvent(event) : false;
252 }
253