]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiDeathScreen.cpp
Fix building under MSVC
[dragonfireclient.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         
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 }
112
113 void GUIDeathScreen::drawMenu()
114 {
115         gui::IGUISkin* skin = Environment->getSkin();
116         if (!skin)
117                 return;
118         video::IVideoDriver* driver = Environment->getVideoDriver();
119         
120         {
121                 video::SColor color(180,50,0,0);
122                 driver->draw2DRectangle(color,
123                                 core::rect<s32>(0,0,m_screensize.X,m_screensize.Y), NULL);
124         }
125         {
126                 video::SColor bgcolor(50,0,0,0);
127                 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
128         }
129
130         gui::IGUIElement::draw();
131 }
132
133 bool GUIDeathScreen::OnEvent(const SEvent& event)
134 {
135         if(event.EventType==EET_KEY_INPUT_EVENT)
136         {
137                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
138                 {
139                         respawn();
140                         quitMenu();
141                         return true;
142                 }
143                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
144                 {
145                         respawn();
146                         quitMenu();
147                         return true;
148                 }
149         }
150         if(event.EventType==EET_GUI_EVENT)
151         {
152                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
153                                 && isVisible())
154                 {
155                         if(!canTakeFocus(event.GUIEvent.Element))
156                         {
157                                 dstream<<"GUIDeathScreen: Not allowing focus change."
158                                                 <<std::endl;
159                                 // Returning true disables focus change
160                                 return true;
161                         }
162                 }
163                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
164                 {
165                         switch(event.GUIEvent.Caller->getID())
166                         {
167                         case 257:
168                                 respawn();
169                                 quitMenu();
170                                 return true;
171                         }
172                 }
173         }
174
175         return Parent ? Parent->OnEvent(event) : false;
176 }
177
178 void GUIDeathScreen::respawn()
179 {
180         m_respawner->respawn();
181 }
182