]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiConfirmRegistration.cpp
Move client-specific files to 'src/client' (#7902)
[dragonfireclient.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/client.h"
23 #include <IGUICheckBox.h>
24 #include <IGUIButton.h>
25 #include <IGUIStaticText.h>
26 #include <IGUIFont.h>
27 #include "intlGUIEditBox.h"
28 #include "porting.h"
29
30 #include "gettext.h"
31
32 // Continuing from guiPasswordChange.cpp
33 const int ID_confirmPassword = 262;
34 const int ID_confirm = 263;
35 const int ID_message = 264;
36 const int ID_cancel = 265;
37
38 GUIConfirmRegistration::GUIConfirmRegistration(gui::IGUIEnvironment *env,
39                 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr, Client *client,
40                 const std::string &playername, const std::string &password,
41                 const std::string &address, bool *aborted) :
42                 GUIModalMenu(env, parent, id, menumgr),
43                 m_client(client), m_playername(playername), m_password(password),
44                 m_address(address), m_aborted(aborted)
45 {
46 #ifdef __ANDROID__
47         m_touchscreen_visible = false;
48 #endif
49 }
50
51 GUIConfirmRegistration::~GUIConfirmRegistration()
52 {
53         removeChildren();
54 }
55
56 void GUIConfirmRegistration::removeChildren()
57 {
58         const core::list<gui::IGUIElement *> &children = getChildren();
59         core::list<gui::IGUIElement *> children_copy;
60         for (gui::IGUIElement *i : children)
61                 children_copy.push_back(i);
62         for (gui::IGUIElement *i : children_copy)
63                 i->remove();
64 }
65 void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
66 {
67         acceptInput();
68         removeChildren();
69
70         /*
71                 Calculate new sizes and positions
72         */
73         const float s = m_gui_scale;
74         DesiredRect = core::rect<s32>(
75                 screensize.X / 2 - 600 * s / 2,
76                 screensize.Y / 2 - 360 * s / 2,
77                 screensize.X / 2 + 600 * s / 2,
78                 screensize.Y / 2 + 360 * s / 2
79         );
80         recalculateAbsolutePosition(false);
81
82         v2s32 size = DesiredRect.getSize();
83         v2s32 topleft_client(0, 0);
84
85         const wchar_t *text;
86
87         /*
88                 Add stuff
89         */
90         s32 ypos = 30 * s;
91         {
92                 std::string address = m_address;
93                 if (address.empty())
94                         address = "localhost";
95                 core::rect<s32> rect2(0, 0, 540 * s, 180 * s);
96                 rect2 += topleft_client + v2s32(30 * s, ypos);
97                 static const std::string info_text_template = strgettext(
98                                 "You are about to join the server at %1$s with the "
99                                 "name \"%2$s\" for the first time. If you proceed, a "
100                                 "new account using your credentials will be created "
101                                 "on this server.\n"
102                                 "Please retype your password and click Register and "
103                                 "Join to confirm account creation or click Cancel to "
104                                 "abort.");
105                 char info_text_buf[1024];
106                 porting::mt_snprintf(info_text_buf, sizeof(info_text_buf),
107                                 info_text_template.c_str(), address.c_str(),
108                                 m_playername.c_str());
109
110                 wchar_t *info_text_buf_wide = utf8_to_wide_c(info_text_buf);
111                 gui::IGUIEditBox *e = new gui::intlGUIEditBox(info_text_buf_wide, true,
112                                 Environment, this, ID_message, rect2, false, true);
113                 delete[] info_text_buf_wide;
114                 e->drop();
115                 e->setMultiLine(true);
116                 e->setWordWrap(true);
117                 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
118         }
119
120         ypos += 210 * s;
121         {
122                 core::rect<s32> rect2(0, 0, 540 * s, 30 * s);
123                 rect2 += topleft_client + v2s32(30 * s, ypos);
124                 gui::IGUIEditBox *e = Environment->addEditBox(m_pass_confirm.c_str(),
125                                 rect2, true, this, ID_confirmPassword);
126                 e->setPasswordBox(true);
127         }
128
129         ypos += 60 * s;
130         {
131                 core::rect<s32> rect2(0, 0, 230 * s, 35 * s);
132                 rect2 = rect2 + v2s32(size.X / 2 - 220 * s, ypos);
133                 text = wgettext("Register and Join");
134                 Environment->addButton(rect2, this, ID_confirm, text);
135                 delete[] text;
136         }
137         {
138                 core::rect<s32> rect2(0, 0, 120 * s, 35 * s);
139                 rect2 = rect2 + v2s32(size.X / 2 + 70 * s, ypos);
140                 text = wgettext("Cancel");
141                 Environment->addButton(rect2, this, ID_cancel, text);
142                 delete[] text;
143         }
144         {
145                 core::rect<s32> rect2(0, 0, 200 * s, 20 * s);
146                 rect2 += topleft_client + v2s32(30 * s, ypos - 40 * s);
147                 text = wgettext("Passwords do not match!");
148                 IGUIElement *e = Environment->addStaticText(
149                                 text, rect2, false, true, this, ID_message);
150                 e->setVisible(false);
151                 delete[] text;
152         }
153 }
154
155 void GUIConfirmRegistration::drawMenu()
156 {
157         gui::IGUISkin *skin = Environment->getSkin();
158         if (!skin)
159                 return;
160         video::IVideoDriver *driver = Environment->getVideoDriver();
161
162         video::SColor bgcolor(140, 0, 0, 0);
163         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
164
165         gui::IGUIElement::draw();
166 #ifdef __ANDROID__
167         getAndroidUIInput();
168 #endif
169 }
170
171 void GUIConfirmRegistration::closeMenu(bool goNext)
172 {
173         if (goNext) {
174                 m_client->confirmRegistration();
175         } else {
176                 *m_aborted = true;
177                 infostream << "Connect aborted [Escape]" << std::endl;
178         }
179         quitMenu();
180 }
181
182 void GUIConfirmRegistration::acceptInput()
183 {
184         gui::IGUIElement *e;
185         e = getElementFromId(ID_confirmPassword);
186         if (e)
187                 m_pass_confirm = e->getText();
188 }
189
190 bool GUIConfirmRegistration::processInput()
191 {
192         std::wstring m_password_ws = narrow_to_wide(m_password);
193         if (m_password_ws != m_pass_confirm) {
194                 gui::IGUIElement *e = getElementFromId(ID_message);
195                 if (e)
196                         e->setVisible(true);
197                 return false;
198         }
199         return true;
200 }
201
202 bool GUIConfirmRegistration::OnEvent(const SEvent &event)
203 {
204         if (event.EventType == EET_KEY_INPUT_EVENT) {
205                 // clang-format off
206                 if ((event.KeyInput.Key == KEY_ESCAPE ||
207                                 event.KeyInput.Key == KEY_CANCEL) &&
208                                 event.KeyInput.PressedDown) {
209                         closeMenu(false);
210                         return true;
211                 }
212                 // clang-format on
213                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
214                         acceptInput();
215                         if (processInput())
216                                 closeMenu(true);
217                         return true;
218                 }
219         }
220
221         if (event.EventType != EET_GUI_EVENT)
222                 return Parent ? Parent->OnEvent(event) : false;
223
224         if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) {
225                 if (!canTakeFocus(event.GUIEvent.Element)) {
226                         dstream << "GUIConfirmRegistration: Not allowing focus "
227                                    "change."
228                                 << std::endl;
229                         // Returning true disables focus change
230                         return true;
231                 }
232         } else if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
233                 switch (event.GUIEvent.Caller->getID()) {
234                 case ID_confirm:
235                         acceptInput();
236                         if (processInput())
237                                 closeMenu(true);
238                         return true;
239                 case ID_cancel:
240                         closeMenu(false);
241                         return true;
242                 }
243         } else if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
244                 switch (event.GUIEvent.Caller->getID()) {
245                 case ID_confirmPassword:
246                         acceptInput();
247                         if (processInput())
248                                 closeMenu(true);
249                         return true;
250                 }
251         }
252
253         return false;
254 }
255
256 #ifdef __ANDROID__
257 bool GUIConfirmRegistration::getAndroidUIInput()
258 {
259         if (!hasAndroidUIInput() || m_jni_field_name != "password")
260                 return false;
261
262         std::string text = porting::getInputDialogValue();
263         gui::IGUIElement *e = getElementFromId(ID_confirmPassword);
264         if (e)
265                 e->setText(utf8_to_wide(text).c_str());
266
267         m_jni_field_name.clear();
268         return false;
269 }
270 #endif