Add Lua on_mapgen_init callback, and minetest.set_mapgen_params API
[oweals/minetest.git] / src / guiDeathScreen.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 "guiDeathScreen.h"
21 #include "debug.h"
22 #include "serialization.h"
23 #include <string>
24 #include <IGUICheckBox.h>
25 #include <IGUIEditBox.h>
26 #include <IGUIButton.h>
27 #include <IGUIStaticText.h>
28 #include <IGUIFont.h>
29 #include "gettext.h"
30 #include "client.h"
31
32 GUIDeathScreen::GUIDeathScreen(gui::IGUIEnvironment* env,
33                 gui::IGUIElement* parent, s32 id,
34                 IMenuManager *menumgr, IRespawnInitiator *respawner
35 ):
36         GUIModalMenu(env, parent, id, menumgr),
37         m_respawner(respawner),
38         m_screensize(1,1)
39 {
40 }
41
42 GUIDeathScreen::~GUIDeathScreen()
43 {
44         removeChildren();
45         delete m_respawner;
46 }
47
48 void GUIDeathScreen::removeChildren()
49 {
50         const core::list<gui::IGUIElement*> &children = getChildren();
51         core::list<gui::IGUIElement*> children_copy;
52         for(core::list<gui::IGUIElement*>::ConstIterator
53                         i = children.begin(); i != children.end(); i++)
54         {
55                 children_copy.push_back(*i);
56         }
57         for(core::list<gui::IGUIElement*>::Iterator
58                         i = children_copy.begin();
59                         i != children_copy.end(); i++)
60         {
61                 (*i)->remove();
62         }
63 }
64
65 void GUIDeathScreen::regenerateGui(v2u32 screensize)
66 {
67         m_screensize = screensize;
68
69         /*
70                 Remove stuff
71         */
72         removeChildren();
73         
74         /*
75                 Calculate new sizes and positions
76         */
77         core::rect<s32> rect(
78                         screensize.X/2 - 500/2,
79                         screensize.Y/2 - 200/2,
80                         screensize.X/2 + 500/2,
81                         screensize.Y/2 + 200/2
82         );
83         
84         DesiredRect = rect;
85         recalculateAbsolutePosition(false);
86
87         v2s32 size = rect.getSize();
88
89         /*
90                 Add stuff
91         */
92         changeCtype("");
93         {
94                 core::rect<s32> rect(0, 0, 400, 50);
95                 rect = rect + v2s32(size.X/2-400/2, size.Y/2-50/2-25);
96                 wchar_t* text = wgettext("You died.");
97                 Environment->addStaticText(text, rect, false,
98                                 true, this, 256);
99                 delete[] text;
100         }
101         {
102                 core::rect<s32> rect(0, 0, 140, 30);
103                 rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+25);
104                 wchar_t* text = wgettext("Respawn");
105                 gui::IGUIElement *e = 
106                 Environment->addButton(rect, this, 257,
107                         text);
108                 delete[] text;
109                 Environment->setFocus(e);
110         }
111         changeCtype("C");
112 }
113
114 void GUIDeathScreen::drawMenu()
115 {
116         gui::IGUISkin* skin = Environment->getSkin();
117         if (!skin)
118                 return;
119         video::IVideoDriver* driver = Environment->getVideoDriver();
120         
121         {
122                 video::SColor color(180,50,0,0);
123                 driver->draw2DRectangle(color,
124                                 core::rect<s32>(0,0,m_screensize.X,m_screensize.Y), NULL);
125         }
126         {
127                 video::SColor bgcolor(50,0,0,0);
128                 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
129         }
130
131         gui::IGUIElement::draw();
132 }
133
134 bool GUIDeathScreen::OnEvent(const SEvent& event)
135 {
136         if(event.EventType==EET_KEY_INPUT_EVENT)
137         {
138                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
139                 {
140                         respawn();
141                         quitMenu();
142                         return true;
143                 }
144                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
145                 {
146                         respawn();
147                         quitMenu();
148                         return true;
149                 }
150         }
151         if(event.EventType==EET_GUI_EVENT)
152         {
153                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
154                                 && isVisible())
155                 {
156                         if(!canTakeFocus(event.GUIEvent.Element))
157                         {
158                                 dstream<<"GUIDeathScreen: Not allowing focus change."
159                                                 <<std::endl;
160                                 // Returning true disables focus change
161                                 return true;
162                         }
163                 }
164                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
165                 {
166                         switch(event.GUIEvent.Caller->getID())
167                         {
168                         case 257:
169                                 respawn();
170                                 quitMenu();
171                                 return true;
172                         }
173                 }
174         }
175
176         return Parent ? Parent->OnEvent(event) : false;
177 }
178
179 void GUIDeathScreen::respawn()
180 {
181         m_respawner->respawn();
182 }
183