Better apple tree generation
[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         changeCtype("");
89         {
90                 core::rect<s32> rect(0, 0, 140, 30);
91                 rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+25);
92                 gui::IGUIElement *e = 
93                 Environment->addButton(rect, this, 257,
94                         wgettext("Proceed"));
95                 Environment->setFocus(e);
96         }
97         changeCtype("C");
98 }
99
100 void GUIMessageMenu::drawMenu()
101 {
102         gui::IGUISkin* skin = Environment->getSkin();
103         if (!skin)
104                 return;
105         video::IVideoDriver* driver = Environment->getVideoDriver();
106         
107         video::SColor bgcolor(140,0,0,0);
108         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
109
110         gui::IGUIElement::draw();
111 }
112
113 bool GUIMessageMenu::OnEvent(const SEvent& event)
114 {
115         if(event.EventType==EET_KEY_INPUT_EVENT)
116         {
117                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
118                 {
119                         m_status = true;
120                         quitMenu();
121                         return true;
122                 }
123                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
124                 {
125                         m_status = true;
126                         quitMenu();
127                         return true;
128                 }
129         }
130         if(event.EventType==EET_GUI_EVENT)
131         {
132                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
133                                 && isVisible())
134                 {
135                         if(!canTakeFocus(event.GUIEvent.Element))
136                         {
137                                 dstream<<"GUIMessageMenu: Not allowing focus change."
138                                                 <<std::endl;
139                                 // Returning true disables focus change
140                                 return true;
141                         }
142                 }
143                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
144                 {
145                         switch(event.GUIEvent.Caller->getID())
146                         {
147                         case 257:
148                                 m_status = true;
149                                 quitMenu();
150                                 return true;
151                         }
152                 }
153         }
154
155         return Parent ? Parent->OnEvent(event) : false;
156 }
157