utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/minetest.git] / src / guiDeathScreen.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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                 Environment->addStaticText(wgettext("You died."), rect, false,
97                                 true, this, 256);
98         }
99         {
100                 core::rect<s32> rect(0, 0, 140, 30);
101                 rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+25);
102                 gui::IGUIElement *e = 
103                 Environment->addButton(rect, this, 257,
104                         wgettext("Respawn"));
105                 Environment->setFocus(e);
106         }
107         changeCtype("C");
108 }
109
110 void GUIDeathScreen::drawMenu()
111 {
112         gui::IGUISkin* skin = Environment->getSkin();
113         if (!skin)
114                 return;
115         video::IVideoDriver* driver = Environment->getVideoDriver();
116         
117         {
118                 video::SColor color(180,50,0,0);
119                 driver->draw2DRectangle(color,
120                                 core::rect<s32>(0,0,m_screensize.X,m_screensize.Y), NULL);
121         }
122         {
123                 video::SColor bgcolor(50,0,0,0);
124                 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
125         }
126
127         gui::IGUIElement::draw();
128 }
129
130 bool GUIDeathScreen::OnEvent(const SEvent& event)
131 {
132         if(event.EventType==EET_KEY_INPUT_EVENT)
133         {
134                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
135                 {
136                         respawn();
137                         quitMenu();
138                         return true;
139                 }
140                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
141                 {
142                         respawn();
143                         quitMenu();
144                         return true;
145                 }
146         }
147         if(event.EventType==EET_GUI_EVENT)
148         {
149                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
150                                 && isVisible())
151                 {
152                         if(!canTakeFocus(event.GUIEvent.Element))
153                         {
154                                 dstream<<"GUIDeathScreen: Not allowing focus change."
155                                                 <<std::endl;
156                                 // Returning true disables focus change
157                                 return true;
158                         }
159                 }
160                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
161                 {
162                         switch(event.GUIEvent.Caller->getID())
163                         {
164                         case 257:
165                                 respawn();
166                                 quitMenu();
167                                 return true;
168                         }
169                 }
170         }
171
172         return Parent ? Parent->OnEvent(event) : false;
173 }
174
175 void GUIDeathScreen::respawn()
176 {
177         m_respawner->respawn();
178 }
179