]> git.lizzy.rs Git - minetest.git/blob - src/guiMessageMenu.cpp
Don't use msvc libs for mingw build
[minetest.git] / src / guiMessageMenu.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 "guiMessageMenu.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 GUIMessageMenu::GUIMessageMenu(gui::IGUIEnvironment* env,
33                 gui::IGUIElement* parent, s32 id,
34                 IMenuManager *menumgr,
35                 std::wstring message_text
36 ):
37         GUIModalMenu(env, parent, id, menumgr),
38         m_message_text(message_text),
39         m_status(false)
40 {
41 }
42
43 GUIMessageMenu::~GUIMessageMenu()
44 {
45         removeChildren();
46 }
47
48 void GUIMessageMenu::removeChildren()
49 {
50         {
51                 gui::IGUIElement *e = getElementFromId(256);
52                 if(e != NULL)
53                         e->remove();
54         }
55         {
56                 gui::IGUIElement *e = getElementFromId(257);
57                 if(e != NULL)
58                         e->remove();
59         }
60 }
61
62 void GUIMessageMenu::regenerateGui(v2u32 screensize)
63 {
64         /*
65                 Remove stuff
66         */
67         removeChildren();
68         
69         /*
70                 Calculate new sizes and positions
71         */
72         core::rect<s32> rect(
73                         screensize.X/2 - 580/2,
74                         screensize.Y/2 - 300/2,
75                         screensize.X/2 + 580/2,
76                         screensize.Y/2 + 300/2
77         );
78         
79         DesiredRect = rect;
80         recalculateAbsolutePosition(false);
81
82         v2s32 size = rect.getSize();
83
84         gui::IGUISkin *skin = Environment->getSkin();
85         gui::IGUIFont *font = skin->getFont();
86         s32 msg_h = font->getDimension(m_message_text.c_str()).Height;
87         s32 msg_w = font->getDimension(m_message_text.c_str()).Width;
88         if(msg_h > 200)
89                 msg_h = 200;
90         if(msg_w > 540)
91                 msg_w = 540;
92
93         /*
94                 Add stuff
95         */
96         {
97                 core::rect<s32> rect(0, 0, msg_w, msg_h);
98                 rect += v2s32(size.X/2-msg_w/2, size.Y/2-30/2 - msg_h/2);
99                 Environment->addStaticText(m_message_text.c_str(),
100                         rect, false, true, this, -1);
101         }
102
103         int bw = 140;
104         {
105                 core::rect<s32> rect(0, 0, bw, 30);
106                 rect = rect + v2s32(size.X/2-bw/2, size.Y/2-30/2+5 + msg_h/2);
107                 wchar_t* text = wgettext("Proceed");
108                 gui::IGUIElement *e = 
109                 Environment->addButton(rect, this, 257,
110                         text);
111                 Environment->setFocus(e);
112                 delete[] text;
113         }
114 }
115
116 void GUIMessageMenu::drawMenu()
117 {
118         gui::IGUISkin* skin = Environment->getSkin();
119         if (!skin)
120                 return;
121         video::IVideoDriver* driver = Environment->getVideoDriver();
122         
123         video::SColor bgcolor(140,0,0,0);
124         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
125
126         gui::IGUIElement::draw();
127 }
128
129 bool GUIMessageMenu::OnEvent(const SEvent& event)
130 {
131         if(event.EventType==EET_KEY_INPUT_EVENT)
132         {
133                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
134                 {
135                         m_status = true;
136                         quitMenu();
137                         return true;
138                 }
139                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
140                 {
141                         m_status = true;
142                         quitMenu();
143                         return true;
144                 }
145         }
146         if(event.EventType==EET_GUI_EVENT)
147         {
148                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
149                                 && isVisible())
150                 {
151                         if(!canTakeFocus(event.GUIEvent.Element))
152                         {
153                                 dstream<<"GUIMessageMenu: Not allowing focus change."
154                                                 <<std::endl;
155                                 // Returning true disables focus change
156                                 return true;
157                         }
158                 }
159                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
160                 {
161                         switch(event.GUIEvent.Caller->getID())
162                         {
163                         case 257:
164                                 m_status = true;
165                                 quitMenu();
166                                 return true;
167                         }
168                 }
169         }
170
171         return Parent ? Parent->OnEvent(event) : false;
172 }
173