Merge pull request #32 from Oblomov/master
[oweals/minetest.git] / src / guiMessageMenu.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 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 "guiMessageMenu.h"
21 #include "debug.h"
22 #include "serialization.h"
23 #include <string>
24
25 #include "gettext.h"
26
27 GUIMessageMenu::GUIMessageMenu(gui::IGUIEnvironment* env,
28                 gui::IGUIElement* parent, s32 id,
29                 IMenuManager *menumgr,
30                 std::wstring message_text
31 ):
32         GUIModalMenu(env, parent, id, menumgr),
33         m_message_text(message_text),
34         m_status(false)
35 {
36 }
37
38 GUIMessageMenu::~GUIMessageMenu()
39 {
40         removeChildren();
41 }
42
43 void GUIMessageMenu::removeChildren()
44 {
45         {
46                 gui::IGUIElement *e = getElementFromId(256);
47                 if(e != NULL)
48                         e->remove();
49         }
50         {
51                 gui::IGUIElement *e = getElementFromId(257);
52                 if(e != NULL)
53                         e->remove();
54         }
55 }
56
57 void GUIMessageMenu::regenerateGui(v2u32 screensize)
58 {
59         /*
60                 Remove stuff
61         */
62         removeChildren();
63         
64         /*
65                 Calculate new sizes and positions
66         */
67         core::rect<s32> rect(
68                         screensize.X/2 - 580/2,
69                         screensize.Y/2 - 300/2,
70                         screensize.X/2 + 580/2,
71                         screensize.Y/2 + 300/2
72         );
73         
74         DesiredRect = rect;
75         recalculateAbsolutePosition(false);
76
77         v2s32 size = rect.getSize();
78
79         /*
80                 Add stuff
81         */
82         {
83                 core::rect<s32> rect(0, 0, 400, 50);
84                 rect = rect + v2s32(size.X/2-400/2, size.Y/2-50/2-25);
85                 Environment->addStaticText(m_message_text.c_str(), rect, false,
86                                 true, this, 256);
87         }
88         {
89                 core::rect<s32> rect(0, 0, 140, 30);
90                 rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+25);
91                 gui::IGUIElement *e = 
92                 Environment->addButton(rect, this, 257,
93                         chartowchar_t(gettext("Proceed")));
94                 Environment->setFocus(e);
95         }
96 }
97
98 void GUIMessageMenu::drawMenu()
99 {
100         gui::IGUISkin* skin = Environment->getSkin();
101         if (!skin)
102                 return;
103         video::IVideoDriver* driver = Environment->getVideoDriver();
104         
105         video::SColor bgcolor(140,0,0,0);
106         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
107
108         gui::IGUIElement::draw();
109 }
110
111 bool GUIMessageMenu::OnEvent(const SEvent& event)
112 {
113         if(event.EventType==EET_KEY_INPUT_EVENT)
114         {
115                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
116                 {
117                         m_status = true;
118                         quitMenu();
119                         return true;
120                 }
121                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
122                 {
123                         m_status = true;
124                         quitMenu();
125                         return true;
126                 }
127         }
128         if(event.EventType==EET_GUI_EVENT)
129         {
130                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
131                                 && isVisible())
132                 {
133                         if(!canTakeFocus(event.GUIEvent.Element))
134                         {
135                                 dstream<<"GUIMessageMenu: Not allowing focus change."
136                                                 <<std::endl;
137                                 // Returning true disables focus change
138                                 return true;
139                         }
140                 }
141                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
142                 {
143                         switch(event.GUIEvent.Caller->getID())
144                         {
145                         case 257:
146                                 m_status = true;
147                                 quitMenu();
148                                 return true;
149                         }
150                 }
151         }
152
153         return Parent ? Parent->OnEvent(event) : false;
154 }
155