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