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