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