]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiConfirmRegistration.cpp
Use "Aux1" key name consistently everywhere
[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 #ifdef __ANDROID__
77         const float s = m_gui_scale * porting::getDisplayDensity() / 2;
78 #else
79         const float s = m_gui_scale;
80 #endif
81         DesiredRect = core::rect<s32>(
82                 screensize.X / 2 - 600 * s / 2,
83                 screensize.Y / 2 - 360 * s / 2,
84                 screensize.X / 2 + 600 * s / 2,
85                 screensize.Y / 2 + 360 * s / 2
86         );
87         recalculateAbsolutePosition(false);
88
89         v2s32 size = DesiredRect.getSize();
90         v2s32 topleft_client(0, 0);
91
92         const wchar_t *text;
93
94         /*
95                 Add stuff
96         */
97         s32 ypos = 30 * s;
98         {
99                 core::rect<s32> rect2(0, 0, 540 * s, 180 * s);
100                 rect2 += topleft_client + v2s32(30 * s, ypos);
101                 static const std::string info_text_template = strgettext(
102                                 "You are about to join this server with the name \"%s\" for the "
103                                 "first time.\n"
104                                 "If you proceed, a new account using your credentials will be "
105                                 "created on this server.\n"
106                                 "Please retype your password and click 'Register and Join' to "
107                                 "confirm account creation, or click 'Cancel' to abort.");
108                 char info_text_buf[1024];
109                 porting::mt_snprintf(info_text_buf, sizeof(info_text_buf),
110                                 info_text_template.c_str(), m_playername.c_str());
111
112                 wchar_t *info_text_buf_wide = utf8_to_wide_c(info_text_buf);
113                 gui::IGUIEditBox *e = new gui::intlGUIEditBox(info_text_buf_wide, true,
114                                 Environment, this, ID_intotext, rect2, false, true);
115                 delete[] info_text_buf_wide;
116                 e->drop();
117                 e->setMultiLine(true);
118                 e->setWordWrap(true);
119                 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
120         }
121
122         ypos += 200 * s;
123         {
124                 core::rect<s32> rect2(0, 0, 540 * s, 30 * s);
125                 rect2 += topleft_client + v2s32(30 * s, ypos);
126                 gui::IGUIEditBox *e = Environment->addEditBox(m_pass_confirm.c_str(),
127                                 rect2, true, this, ID_confirmPassword);
128                 e->setPasswordBox(true);
129                 Environment->setFocus(e);
130         }
131
132         ypos += 50 * s;
133         {
134                 core::rect<s32> rect2(0, 0, 230 * s, 35 * s);
135                 rect2 = rect2 + v2s32(size.X / 2 - 220 * s, ypos);
136                 text = wgettext("Register and Join");
137                 GUIButton::addButton(Environment, rect2, m_tsrc, this, ID_confirm, text);
138                 delete[] text;
139         }
140         {
141                 core::rect<s32> rect2(0, 0, 120 * s, 35 * s);
142                 rect2 = rect2 + v2s32(size.X / 2 + 70 * s, ypos);
143                 text = wgettext("Cancel");
144                 GUIButton::addButton(Environment, rect2, m_tsrc, this, ID_cancel, text);
145                 delete[] text;
146         }
147         {
148                 core::rect<s32> rect2(0, 0, 500 * s, 40 * s);
149                 rect2 += topleft_client + v2s32(30 * s, ypos + 40 * s);
150                 text = wgettext("Passwords do not match!");
151                 IGUIElement *e = Environment->addStaticText(
152                                 text, rect2, false, true, this, ID_message);
153                 e->setVisible(false);
154                 delete[] text;
155         }
156 }
157
158 void GUIConfirmRegistration::drawMenu()
159 {
160         gui::IGUISkin *skin = Environment->getSkin();
161         if (!skin)
162                 return;
163         video::IVideoDriver *driver = Environment->getVideoDriver();
164
165         video::SColor bgcolor(140, 0, 0, 0);
166         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
167
168         gui::IGUIElement::draw();
169 #ifdef __ANDROID__
170         getAndroidUIInput();
171 #endif
172 }
173
174 void GUIConfirmRegistration::closeMenu(bool goNext)
175 {
176         if (goNext) {
177                 m_client->confirmRegistration();
178         } else {
179                 *m_aborted = true;
180                 infostream << "Connect aborted [Escape]" << std::endl;
181         }
182         quitMenu();
183 }
184
185 void GUIConfirmRegistration::acceptInput()
186 {
187         gui::IGUIElement *e;
188         e = getElementFromId(ID_confirmPassword);
189         if (e)
190                 m_pass_confirm = e->getText();
191 }
192
193 bool GUIConfirmRegistration::processInput()
194 {
195         if (utf8_to_wide(m_password) != m_pass_confirm) {
196                 gui::IGUIElement *e = getElementFromId(ID_message);
197                 if (e)
198                         e->setVisible(true);
199                 return false;
200         }
201         return true;
202 }
203
204 bool GUIConfirmRegistration::OnEvent(const SEvent &event)
205 {
206         if (event.EventType == EET_KEY_INPUT_EVENT) {
207                 // clang-format off
208                 if ((event.KeyInput.Key == KEY_ESCAPE ||
209                                 event.KeyInput.Key == KEY_CANCEL) &&
210                                 event.KeyInput.PressedDown) {
211                         closeMenu(false);
212                         return true;
213                 }
214                 // clang-format on
215                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
216                         acceptInput();
217                         if (processInput())
218                                 closeMenu(true);
219                         return true;
220                 }
221         }
222
223         if (event.EventType != EET_GUI_EVENT)
224                 return Parent ? Parent->OnEvent(event) : false;
225
226         if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) {
227                 if (!canTakeFocus(event.GUIEvent.Element)) {
228                         infostream << "GUIConfirmRegistration: Not allowing focus change."
229                                 << std::endl;
230                         // Returning true disables focus change
231                         return true;
232                 }
233         } else if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
234                 switch (event.GUIEvent.Caller->getID()) {
235                 case ID_confirm:
236                         acceptInput();
237                         if (processInput())
238                                 closeMenu(true);
239                         return true;
240                 case ID_cancel:
241                         closeMenu(false);
242                         return true;
243                 }
244         } else if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
245                 switch (event.GUIEvent.Caller->getID()) {
246                 case ID_confirmPassword:
247                         acceptInput();
248                         if (processInput())
249                                 closeMenu(true);
250                         return true;
251                 }
252         }
253
254         return false;
255 }
256
257 #ifdef __ANDROID__
258 bool GUIConfirmRegistration::getAndroidUIInput()
259 {
260         if (!hasAndroidUIInput() || m_jni_field_name != "password")
261                 return false;
262
263         // still waiting
264         if (porting::getInputDialogState() == -1)
265                 return true;
266
267         m_jni_field_name.clear();
268
269         gui::IGUIElement *e = getElementFromId(ID_confirmPassword);
270
271         if (!e || e->getType() != irr::gui::EGUIET_EDIT_BOX)
272                 return false;
273
274         std::string text = porting::getInputDialogValue();
275         e->setText(utf8_to_wide(text).c_str());
276         return false;
277 }
278 #endif