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