Registration confirmation dialog: Fix grammar
[oweals/minetest.git] / src / gui / guiConfirmRegistration.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 srifqi, Muhammad Rifqi Priyo Susanto
4                 <muhammadrifqipriyosusanto@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "guiConfirmRegistration.h"
22 #include "client.h"
23 #include <IGUICheckBox.h>
24 #include <IGUIEditBox.h>
25 #include <IGUIButton.h>
26 #include <IGUIStaticText.h>
27 #include <IGUIFont.h>
28
29 #include "gettext.h"
30
31 // Continuing from guiPasswordChange.cpp
32 const int ID_confirmPassword = 262;
33 const int ID_confirm = 263;
34 const int ID_message = 264;
35 const int ID_cancel = 265;
36
37 GUIConfirmRegistration::GUIConfirmRegistration(gui::IGUIEnvironment *env,
38                 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr, Client *client,
39                 const std::string &playername, const std::string &password,
40                 const std::string &address, bool *aborted) :
41                 GUIModalMenu(env, parent, id, menumgr),
42                 m_client(client), m_playername(playername), m_password(password),
43                 m_address(address), m_aborted(aborted)
44 {
45 }
46
47 GUIConfirmRegistration::~GUIConfirmRegistration()
48 {
49         removeChildren();
50 }
51
52 void GUIConfirmRegistration::removeChildren()
53 {
54         const core::list<gui::IGUIElement *> &children = getChildren();
55         core::list<gui::IGUIElement *> children_copy;
56         for (gui::IGUIElement *i : children)
57                 children_copy.push_back(i);
58         for (gui::IGUIElement *i : children_copy)
59                 i->remove();
60 }
61 void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
62 {
63         acceptInput();
64         removeChildren();
65
66         /*
67                 Calculate new sizes and positions
68         */
69         core::rect<s32> rect(screensize.X / 2 - 600 / 2, screensize.Y / 2 - 300 / 2,
70                         screensize.X / 2 + 600 / 2, screensize.Y / 2 + 300 / 2);
71
72         DesiredRect = rect;
73         recalculateAbsolutePosition(false);
74
75         v2s32 size = rect.getSize();
76         v2s32 topleft_client(0, 0);
77
78         const wchar_t *text;
79
80         /*
81                 Add stuff
82         */
83         s32 ypos = 30;
84         {
85                 std::string address = m_address;
86                 if (address.empty())
87                         address = "localhost";
88                 core::rect<s32> rect(0, 0, 540, 90);
89                 rect += topleft_client + v2s32(30, ypos);
90                 static const std::string info_text_template = strgettext(
91                                 "You are about to join the server at %1$s with the "
92                                 "name \"%2$s\" for the first time. If you proceed, a "
93                                 "new account using your credentials will be created "
94                                 "on this server.\n"
95                                 "Please retype your password and click Register and "
96                                 "Join to confirm account creation or click Cancel to "
97                                 "abort.");
98                 char info_text_buf[1024];
99                 snprintf(info_text_buf, sizeof(info_text_buf), info_text_template.c_str(),
100                                 address.c_str(), m_playername.c_str());
101                 Environment->addStaticText(narrow_to_wide_c(info_text_buf), rect, false,
102                                 true, this, -1);
103         }
104
105         ypos += 120;
106         {
107                 core::rect<s32> rect(0, 0, 540, 30);
108                 rect += topleft_client + v2s32(30, ypos);
109                 gui::IGUIEditBox *e = Environment->addEditBox(m_pass_confirm.c_str(),
110                                 rect, true, this, ID_confirmPassword);
111                 e->setPasswordBox(true);
112         }
113
114         ypos += 90;
115         {
116                 core::rect<s32> rect(0, 0, 230, 35);
117                 rect = rect + v2s32(size.X / 2 - 220, ypos);
118                 text = wgettext("Register and Join");
119                 Environment->addButton(rect, this, ID_confirm, text);
120                 delete[] text;
121         }
122         {
123                 core::rect<s32> rect(0, 0, 120, 35);
124                 rect = rect + v2s32(size.X / 2 + 70, ypos);
125                 text = wgettext("Cancel");
126                 Environment->addButton(rect, this, ID_cancel, text);
127                 delete[] text;
128         }
129         {
130                 core::rect<s32> rect(0, 0, 200, 20);
131                 rect += topleft_client + v2s32(30, ypos - 40);
132                 text = wgettext("Passwords do not match!");
133                 IGUIElement *e = Environment->addStaticText(
134                                 text, rect, false, true, this, ID_message);
135                 e->setVisible(false);
136                 delete[] text;
137         }
138 }
139
140 void GUIConfirmRegistration::drawMenu()
141 {
142         gui::IGUISkin *skin = Environment->getSkin();
143         if (!skin)
144                 return;
145         video::IVideoDriver *driver = Environment->getVideoDriver();
146
147         video::SColor bgcolor(140, 0, 0, 0);
148         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
149
150         gui::IGUIElement::draw();
151 }
152
153 void GUIConfirmRegistration::closeMenu(bool goNext)
154 {
155         quitMenu();
156         if (goNext) {
157                 m_client->confirmRegistration();
158         } else {
159                 *m_aborted = true;
160                 infostream << "Connect aborted [Escape]" << std::endl;
161         }
162 }
163
164 void GUIConfirmRegistration::acceptInput()
165 {
166         gui::IGUIElement *e;
167         e = getElementFromId(ID_confirmPassword);
168         if (e)
169                 m_pass_confirm = e->getText();
170 }
171
172 bool GUIConfirmRegistration::processInput()
173 {
174         std::wstring m_password_ws = narrow_to_wide(m_password);
175         if (m_password_ws != m_pass_confirm) {
176                 gui::IGUIElement *e = getElementFromId(ID_message);
177                 if (e)
178                         e->setVisible(true);
179                 return false;
180         }
181         return true;
182 }
183
184 bool GUIConfirmRegistration::OnEvent(const SEvent &event)
185 {
186         if (event.EventType == EET_KEY_INPUT_EVENT) {
187                 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
188                         closeMenu(false);
189                         return true;
190                 }
191                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
192                         acceptInput();
193                         if (processInput())
194                                 closeMenu(true);
195                         return true;
196                 }
197         }
198
199         if (event.EventType != EET_GUI_EVENT)
200                 return Parent ? Parent->OnEvent(event) : false;
201
202         if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) {
203                 if (!canTakeFocus(event.GUIEvent.Element)) {
204                         dstream << "GUIConfirmRegistration: Not allowing focus "
205                                    "change."
206                                 << std::endl;
207                         // Returning true disables focus change
208                         return true;
209                 }
210         } else if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
211                 switch (event.GUIEvent.Caller->getID()) {
212                 case ID_confirm:
213                         acceptInput();
214                         if (processInput())
215                                 closeMenu(true);
216                         return true;
217                 case ID_cancel:
218                         closeMenu(false);
219                         return true;
220                 }
221         } else if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
222                 switch (event.GUIEvent.Caller->getID()) {
223                 case ID_confirmPassword:
224                         acceptInput();
225                         if (processInput())
226                                 closeMenu(true);
227                         return true;
228                 }
229         }
230
231         return false;
232 }