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