]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiConfirmRegistration.cpp
Android: Replace movement buttons with joystick (#7126)
[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.h"
23 #include <IGUICheckBox.h>
24 #include <IGUIButton.h>
25 #include <IGUIStaticText.h>
26 #include <IGUIFont.h>
27 #include "intlGUIEditBox.h"
28
29 #include "gettext.h"
30
31 // Continuing from guiPasswordChange.cpp
32 const int ID_confirmPassword = 262;
33 const int ID_confirm = 263;
34 const int ID_message = 264;
35 const int ID_cancel = 265;
36
37 GUIConfirmRegistration::GUIConfirmRegistration(gui::IGUIEnvironment *env,
38                 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr, Client *client,
39                 const std::string &playername, const std::string &password,
40                 const std::string &address, bool *aborted) :
41                 GUIModalMenu(env, parent, id, menumgr),
42                 m_client(client), m_playername(playername), m_password(password),
43                 m_address(address), m_aborted(aborted)
44 {
45 }
46
47 GUIConfirmRegistration::~GUIConfirmRegistration()
48 {
49         removeChildren();
50 }
51
52 void GUIConfirmRegistration::removeChildren()
53 {
54         const core::list<gui::IGUIElement *> &children = getChildren();
55         core::list<gui::IGUIElement *> children_copy;
56         for (gui::IGUIElement *i : children)
57                 children_copy.push_back(i);
58         for (gui::IGUIElement *i : children_copy)
59                 i->remove();
60 }
61 void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
62 {
63         acceptInput();
64         removeChildren();
65
66         /*
67                 Calculate new sizes and positions
68         */
69         core::rect<s32> rect(screensize.X / 2 - 600 / 2, screensize.Y / 2 - 360 / 2,
70                         screensize.X / 2 + 600 / 2, screensize.Y / 2 + 360 / 2);
71
72         DesiredRect = rect;
73         recalculateAbsolutePosition(false);
74
75         v2s32 size = rect.getSize();
76         v2s32 topleft_client(0, 0);
77
78         const wchar_t *text;
79
80         /*
81                 Add stuff
82         */
83         s32 ypos = 30;
84         {
85                 std::string address = m_address;
86                 if (address.empty())
87                         address = "localhost";
88                 core::rect<s32> rect2(0, 0, 540, 180);
89                 rect2 += topleft_client + v2s32(30, ypos);
90                 static const std::string info_text_template = strgettext(
91                                 "You are about to join the server at %1$s with the "
92                                 "name \"%2$s\" for the first time. If you proceed, a "
93                                 "new account using your credentials will be created "
94                                 "on this server.\n"
95                                 "Please retype your password and click Register and "
96                                 "Join to confirm account creation or click Cancel to "
97                                 "abort.");
98                 char info_text_buf[1024];
99                 snprintf(info_text_buf, sizeof(info_text_buf), info_text_template.c_str(),
100                                 address.c_str(), m_playername.c_str());
101
102                 gui::IGUIEditBox *e = new gui::intlGUIEditBox(
103                                 utf8_to_wide_c(info_text_buf), true, Environment, this,
104                                 ID_message, rect2, false, true);
105                 e->drop();
106                 e->setMultiLine(true);
107                 e->setWordWrap(true);
108                 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
109         }
110
111         ypos += 210;
112         {
113                 core::rect<s32> rect2(0, 0, 540, 30);
114                 rect2 += topleft_client + v2s32(30, ypos);
115                 gui::IGUIEditBox *e = Environment->addEditBox(m_pass_confirm.c_str(),
116                                 rect2, true, this, ID_confirmPassword);
117                 e->setPasswordBox(true);
118         }
119
120         ypos += 60;
121         {
122                 core::rect<s32> rect2(0, 0, 230, 35);
123                 rect2 = rect2 + v2s32(size.X / 2 - 220, ypos);
124                 text = wgettext("Register and Join");
125                 Environment->addButton(rect2, this, ID_confirm, text);
126                 delete[] text;
127         }
128         {
129                 core::rect<s32> rect2(0, 0, 120, 35);
130                 rect2 = rect2 + v2s32(size.X / 2 + 70, ypos);
131                 text = wgettext("Cancel");
132                 Environment->addButton(rect2, this, ID_cancel, text);
133                 delete[] text;
134         }
135         {
136                 core::rect<s32> rect2(0, 0, 200, 20);
137                 rect2 += topleft_client + v2s32(30, ypos - 40);
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 }
158
159 void GUIConfirmRegistration::closeMenu(bool goNext)
160 {
161         quitMenu();
162         if (goNext) {
163                 m_client->confirmRegistration();
164         } else {
165                 *m_aborted = true;
166                 infostream << "Connect aborted [Escape]" << std::endl;
167         }
168 }
169
170 void GUIConfirmRegistration::acceptInput()
171 {
172         gui::IGUIElement *e;
173         e = getElementFromId(ID_confirmPassword);
174         if (e)
175                 m_pass_confirm = e->getText();
176 }
177
178 bool GUIConfirmRegistration::processInput()
179 {
180         std::wstring m_password_ws = narrow_to_wide(m_password);
181         if (m_password_ws != m_pass_confirm) {
182                 gui::IGUIElement *e = getElementFromId(ID_message);
183                 if (e)
184                         e->setVisible(true);
185                 return false;
186         }
187         return true;
188 }
189
190 bool GUIConfirmRegistration::OnEvent(const SEvent &event)
191 {
192         if (event.EventType == EET_KEY_INPUT_EVENT) {
193                 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
194                         closeMenu(false);
195                         return true;
196                 }
197                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
198                         acceptInput();
199                         if (processInput())
200                                 closeMenu(true);
201                         return true;
202                 }
203         }
204
205         if (event.EventType != EET_GUI_EVENT)
206                 return Parent ? Parent->OnEvent(event) : false;
207
208         if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) {
209                 if (!canTakeFocus(event.GUIEvent.Element)) {
210                         dstream << "GUIConfirmRegistration: Not allowing focus "
211                                    "change."
212                                 << std::endl;
213                         // Returning true disables focus change
214                         return true;
215                 }
216         } else if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
217                 switch (event.GUIEvent.Caller->getID()) {
218                 case ID_confirm:
219                         acceptInput();
220                         if (processInput())
221                                 closeMenu(true);
222                         return true;
223                 case ID_cancel:
224                         closeMenu(false);
225                         return true;
226                 }
227         } else if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
228                 switch (event.GUIEvent.Caller->getID()) {
229                 case ID_confirmPassword:
230                         acceptInput();
231                         if (processInput())
232                                 closeMenu(true);
233                         return true;
234                 }
235         }
236
237         return false;
238 }