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 "intlGUIEditBox.h"
33 // Continuing from guiPasswordChange.cpp
34 const int ID_confirmPassword = 262;
35 const int ID_confirm = 263;
36 const int ID_intotext = 264;
37 const int ID_cancel = 265;
38 const int ID_message = 266;
40 GUIConfirmRegistration::GUIConfirmRegistration(gui::IGUIEnvironment *env,
41 gui::IGUIElement *parent, s32 id, IMenuManager *menumgr, Client *client,
42 const std::string &playername, const std::string &password,
43 bool *aborted, ISimpleTextureSource *tsrc) :
44 GUIModalMenu(env, parent, id, menumgr),
45 m_client(client), m_playername(playername), m_password(password),
46 m_aborted(aborted), m_tsrc(tsrc)
49 m_touchscreen_visible = false;
53 GUIConfirmRegistration::~GUIConfirmRegistration()
58 void GUIConfirmRegistration::removeChildren()
60 const core::list<gui::IGUIElement *> &children = getChildren();
61 core::list<gui::IGUIElement *> children_copy;
62 for (gui::IGUIElement *i : children)
63 children_copy.push_back(i);
64 for (gui::IGUIElement *i : children_copy)
68 void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
74 Calculate new sizes and positions
77 const float s = m_gui_scale * porting::getDisplayDensity() / 2;
79 const float s = m_gui_scale;
81 DesiredRect = core::rect<s32>(
82 screensize.X / 2 - 600 * s / 2,
83 screensize.Y / 2 - 360 * s / 2,
84 screensize.X / 2 + 600 * s / 2,
85 screensize.Y / 2 + 360 * s / 2
87 recalculateAbsolutePosition(false);
89 v2s32 size = DesiredRect.getSize();
90 v2s32 topleft_client(0, 0);
99 core::rect<s32> rect2(0, 0, 540 * s, 180 * s);
100 rect2 += topleft_client + v2s32(30 * s, ypos);
101 static const std::string info_text_template = strgettext(
102 "You are about to join this server with the name \"%s\" for the "
104 "If you proceed, a new account using your credentials will be "
105 "created on this server.\n"
106 "Please retype your password and click 'Register and Join' to "
107 "confirm account creation, or click 'Cancel' to abort.");
108 char info_text_buf[1024];
109 porting::mt_snprintf(info_text_buf, sizeof(info_text_buf),
110 info_text_template.c_str(), m_playername.c_str());
112 wchar_t *info_text_buf_wide = utf8_to_wide_c(info_text_buf);
113 gui::IGUIEditBox *e = new gui::intlGUIEditBox(info_text_buf_wide, true,
114 Environment, this, ID_intotext, rect2, false, true);
115 delete[] info_text_buf_wide;
117 e->setMultiLine(true);
118 e->setWordWrap(true);
119 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
124 core::rect<s32> rect2(0, 0, 540 * s, 30 * s);
125 rect2 += topleft_client + v2s32(30 * s, ypos);
126 gui::IGUIEditBox *e = Environment->addEditBox(m_pass_confirm.c_str(),
127 rect2, true, this, ID_confirmPassword);
128 e->setPasswordBox(true);
129 Environment->setFocus(e);
134 core::rect<s32> rect2(0, 0, 230 * s, 35 * s);
135 rect2 = rect2 + v2s32(size.X / 2 - 220 * s, ypos);
136 text = wgettext("Register and Join");
137 GUIButton::addButton(Environment, rect2, m_tsrc, this, ID_confirm, text);
141 core::rect<s32> rect2(0, 0, 120 * s, 35 * s);
142 rect2 = rect2 + v2s32(size.X / 2 + 70 * s, ypos);
143 text = wgettext("Cancel");
144 GUIButton::addButton(Environment, rect2, m_tsrc, this, ID_cancel, text);
148 core::rect<s32> rect2(0, 0, 500 * s, 40 * s);
149 rect2 += topleft_client + v2s32(30 * s, ypos + 40 * s);
150 text = wgettext("Passwords do not match!");
151 IGUIElement *e = Environment->addStaticText(
152 text, rect2, false, true, this, ID_message);
153 e->setVisible(false);
158 void GUIConfirmRegistration::drawMenu()
160 gui::IGUISkin *skin = Environment->getSkin();
163 video::IVideoDriver *driver = Environment->getVideoDriver();
165 video::SColor bgcolor(140, 0, 0, 0);
166 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
168 gui::IGUIElement::draw();
174 void GUIConfirmRegistration::closeMenu(bool goNext)
177 m_client->confirmRegistration();
180 infostream << "Connect aborted [Escape]" << std::endl;
185 void GUIConfirmRegistration::acceptInput()
188 e = getElementFromId(ID_confirmPassword);
190 m_pass_confirm = e->getText();
193 bool GUIConfirmRegistration::processInput()
195 if (utf8_to_wide(m_password) != m_pass_confirm) {
196 gui::IGUIElement *e = getElementFromId(ID_message);
204 bool GUIConfirmRegistration::OnEvent(const SEvent &event)
206 if (event.EventType == EET_KEY_INPUT_EVENT) {
208 if ((event.KeyInput.Key == KEY_ESCAPE ||
209 event.KeyInput.Key == KEY_CANCEL) &&
210 event.KeyInput.PressedDown) {
215 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
223 if (event.EventType != EET_GUI_EVENT)
224 return Parent ? Parent->OnEvent(event) : false;
226 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) {
227 if (!canTakeFocus(event.GUIEvent.Element)) {
228 infostream << "GUIConfirmRegistration: Not allowing focus change."
230 // Returning true disables focus change
233 } else if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
234 switch (event.GUIEvent.Caller->getID()) {
244 } else if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
245 switch (event.GUIEvent.Caller->getID()) {
246 case ID_confirmPassword:
258 bool GUIConfirmRegistration::getAndroidUIInput()
260 if (!hasAndroidUIInput() || m_jni_field_name != "password")
264 if (porting::getInputDialogState() == -1)
267 m_jni_field_name.clear();
269 gui::IGUIElement *e = getElementFromId(ID_confirmPassword);
271 if (!e || e->getType() != irr::gui::EGUIET_EDIT_BOX)
274 std::string text = porting::getInputDialogValue();
275 e->setText(utf8_to_wide(text).c_str());