Re-add jungles, apple trees
[oweals/minetest.git] / src / guiConfirmMenu.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 "guiConfirmMenu.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
30 #include "gettext.h"
31
32 enum
33 {
34         GUI_ID_YES = 101,
35         GUI_ID_NO,
36 };
37
38 GUIConfirmMenu::GUIConfirmMenu(gui::IGUIEnvironment* env,
39                 gui::IGUIElement* parent, s32 id,
40                 IMenuManager *menumgr,
41                 ConfirmDest *dest,
42                 std::wstring message_text
43 ):
44         GUIModalMenu(env, parent, id, menumgr),
45         m_dest(dest),
46         m_message_text(message_text)
47 {
48 }
49
50 GUIConfirmMenu::~GUIConfirmMenu()
51 {
52         removeChildren();
53         if(m_dest)
54                 delete m_dest;
55 }
56
57 void GUIConfirmMenu::removeChildren()
58 {
59         const core::list<gui::IGUIElement*> &children = getChildren();
60         core::list<gui::IGUIElement*> children_copy;
61         for(core::list<gui::IGUIElement*>::ConstIterator
62                         i = children.begin(); i != children.end(); i++)
63         {
64                 children_copy.push_back(*i);
65         }
66         for(core::list<gui::IGUIElement*>::Iterator
67                         i = children_copy.begin();
68                         i != children_copy.end(); i++)
69         {
70                 (*i)->remove();
71         }
72 }
73
74 void GUIConfirmMenu::regenerateGui(v2u32 screensize)
75 {
76         /*
77                 Remove stuff
78         */
79         removeChildren();
80         
81         /*
82                 Calculate new sizes and positions
83         */
84         core::rect<s32> rect(
85                         screensize.X/2 - 580/2,
86                         screensize.Y/2 - 300/2,
87                         screensize.X/2 + 580/2,
88                         screensize.Y/2 + 300/2
89         );
90         
91         DesiredRect = rect;
92         recalculateAbsolutePosition(false);
93
94         v2s32 size = rect.getSize();
95
96         gui::IGUISkin *skin = Environment->getSkin();
97         gui::IGUIFont *font = skin->getFont();
98         s32 msg_h = font->getDimension(m_message_text.c_str()).Height;
99         s32 msg_w = font->getDimension(m_message_text.c_str()).Width;
100         if(msg_h > 200)
101                 msg_h = 200;
102         if(msg_w > 540)
103                 msg_w = 540;
104
105         /*
106                 Add stuff
107         */
108         {
109                 core::rect<s32> rect(0, 0, msg_w, msg_h);
110                 rect += v2s32(size.X/2-msg_w/2, size.Y/2-30/2 - msg_h/2);
111                 Environment->addStaticText(m_message_text.c_str(),
112                         rect, false, true, this, -1);
113         }
114         changeCtype("");
115         int bw = 100;
116         {
117                 core::rect<s32> rect(0, 0, bw, 30);
118                 rect = rect + v2s32(size.X/2-bw/2-(bw/2+5), size.Y/2-30/2+5 + msg_h/2);
119                 Environment->addButton(rect, this, GUI_ID_YES,
120                         wgettext("Yes"));
121         }
122         {
123                 core::rect<s32> rect(0, 0, bw, 30);
124                 rect = rect + v2s32(size.X/2-bw/2+(bw/2+5), size.Y/2-30/2+5 + msg_h/2);
125                 Environment->addButton(rect, this, GUI_ID_NO,
126                         wgettext("No"));
127         }
128         changeCtype("C");
129 }
130
131 void GUIConfirmMenu::drawMenu()
132 {
133         gui::IGUISkin* skin = Environment->getSkin();
134         if (!skin)
135                 return;
136         video::IVideoDriver* driver = Environment->getVideoDriver();
137         
138         video::SColor bgcolor(140,0,0,0);
139         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
140
141         gui::IGUIElement::draw();
142 }
143
144 void GUIConfirmMenu::acceptInput(bool answer)
145 {
146         if(m_dest)
147                 m_dest->answer(answer);
148 }
149
150 bool GUIConfirmMenu::OnEvent(const SEvent& event)
151 {
152         if(event.EventType==EET_KEY_INPUT_EVENT)
153         {
154                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
155                 {
156                         acceptInput(false);
157                         quitMenu();
158                         return true;
159                 }
160                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
161                 {
162                         acceptInput(true);
163                         quitMenu();
164                         return true;
165                 }
166         }
167         if(event.EventType==EET_GUI_EVENT)
168         {
169                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
170                                 && isVisible())
171                 {
172                         if(!canTakeFocus(event.GUIEvent.Element))
173                         {
174                                 dstream<<"GUIConfirmMenu: Not allowing focus change."
175                                                 <<std::endl;
176                                 // Returning true disables focus change
177                                 return true;
178                         }
179                 }
180                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
181                 {
182                         switch(event.GUIEvent.Caller->getID())
183                         {
184                         case GUI_ID_YES:
185                                 acceptInput(true);
186                                 quitMenu();
187                                 // quitMenu deallocates menu
188                                 return true;
189                         case GUI_ID_NO:
190                                 acceptInput(false);
191                                 quitMenu();
192                                 // quitMenu deallocates menu
193                                 return true;
194                         }
195                 }
196         }
197
198         return Parent ? Parent->OnEvent(event) : false;
199 }
200