3 Copyright (C) 2018 srifqi, Muhammad Rifqi Priyo Susanto
4 <muhammadrifqipriyosusanto@gmail.com>
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.
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.
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.
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>
28 #include "guiEditBoxWithScrollbar.h"
31 #ifdef HAVE_TOUCHSCREENGUI
32 #include "client/renderingengine.h"
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;
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)
52 #ifdef HAVE_TOUCHSCREENGUI
53 m_touchscreen_visible = false;
57 void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
63 Calculate new sizes and positions
65 #ifdef HAVE_TOUCHSCREENGUI
66 const float s = m_gui_scale * RenderingEngine::getDisplayDensity() / 2;
68 const float s = m_gui_scale;
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
76 recalculateAbsolutePosition(false);
78 v2s32 size = DesiredRect.getSize();
79 v2s32 topleft_client(0, 0);
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 "
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());
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);
105 e->setMultiLine(true);
106 e->setWordWrap(true);
107 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
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);
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);
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);
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);
146 void GUIConfirmRegistration::drawMenu()
148 gui::IGUISkin *skin = Environment->getSkin();
151 video::IVideoDriver *driver = Environment->getVideoDriver();
153 video::SColor bgcolor(140, 0, 0, 0);
154 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
156 gui::IGUIElement::draw();
162 void GUIConfirmRegistration::closeMenu(bool goNext)
165 m_client->confirmRegistration();
168 infostream << "Connect aborted [Escape]" << std::endl;
173 void GUIConfirmRegistration::acceptInput()
176 e = getElementFromId(ID_confirmPassword);
178 m_pass_confirm = e->getText();
181 bool GUIConfirmRegistration::processInput()
183 if (utf8_to_wide(m_password) != m_pass_confirm) {
184 gui::IGUIElement *e = getElementFromId(ID_message);
192 bool GUIConfirmRegistration::OnEvent(const SEvent &event)
194 if (event.EventType == EET_KEY_INPUT_EVENT) {
196 if ((event.KeyInput.Key == KEY_ESCAPE ||
197 event.KeyInput.Key == KEY_CANCEL) &&
198 event.KeyInput.PressedDown) {
203 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
211 if (event.EventType != EET_GUI_EVENT)
212 return Parent ? Parent->OnEvent(event) : false;
214 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) {
215 if (!canTakeFocus(event.GUIEvent.Element)) {
216 infostream << "GUIConfirmRegistration: Not allowing focus change."
218 // Returning true disables focus change
221 } else if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
222 switch (event.GUIEvent.Caller->getID()) {
232 } else if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
233 switch (event.GUIEvent.Caller->getID()) {
234 case ID_confirmPassword:
246 bool GUIConfirmRegistration::getAndroidUIInput()
248 if (!hasAndroidUIInput() || m_jni_field_name != "password")
252 if (porting::getInputDialogState() == -1)
255 m_jni_field_name.clear();
257 gui::IGUIElement *e = getElementFromId(ID_confirmPassword);
259 if (!e || e->getType() != irr::gui::EGUIET_EDIT_BOX)
262 std::string text = porting::getInputDialogValue();
263 e->setText(utf8_to_wide(text).c_str());