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 GUIConfirmRegistration::~GUIConfirmRegistration()
62 void GUIConfirmRegistration::removeChildren()
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)
72 void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
78 Calculate new sizes and positions
80 #ifdef HAVE_TOUCHSCREENGUI
81 const float s = m_gui_scale * RenderingEngine::getDisplayDensity() / 2;
83 const float s = m_gui_scale;
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
91 recalculateAbsolutePosition(false);
93 v2s32 size = DesiredRect.getSize();
94 v2s32 topleft_client(0, 0);
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 "
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());
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);
120 e->setMultiLine(true);
121 e->setWordWrap(true);
122 e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
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);
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);
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);
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);
161 void GUIConfirmRegistration::drawMenu()
163 gui::IGUISkin *skin = Environment->getSkin();
166 video::IVideoDriver *driver = Environment->getVideoDriver();
168 video::SColor bgcolor(140, 0, 0, 0);
169 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
171 gui::IGUIElement::draw();
177 void GUIConfirmRegistration::closeMenu(bool goNext)
180 m_client->confirmRegistration();
183 infostream << "Connect aborted [Escape]" << std::endl;
188 void GUIConfirmRegistration::acceptInput()
191 e = getElementFromId(ID_confirmPassword);
193 m_pass_confirm = e->getText();
196 bool GUIConfirmRegistration::processInput()
198 if (utf8_to_wide(m_password) != m_pass_confirm) {
199 gui::IGUIElement *e = getElementFromId(ID_message);
207 bool GUIConfirmRegistration::OnEvent(const SEvent &event)
209 if (event.EventType == EET_KEY_INPUT_EVENT) {
211 if ((event.KeyInput.Key == KEY_ESCAPE ||
212 event.KeyInput.Key == KEY_CANCEL) &&
213 event.KeyInput.PressedDown) {
218 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
226 if (event.EventType != EET_GUI_EVENT)
227 return Parent ? Parent->OnEvent(event) : false;
229 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) {
230 if (!canTakeFocus(event.GUIEvent.Element)) {
231 infostream << "GUIConfirmRegistration: Not allowing focus change."
233 // Returning true disables focus change
236 } else if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
237 switch (event.GUIEvent.Caller->getID()) {
247 } else if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
248 switch (event.GUIEvent.Caller->getID()) {
249 case ID_confirmPassword:
261 bool GUIConfirmRegistration::getAndroidUIInput()
263 if (!hasAndroidUIInput() || m_jni_field_name != "password")
267 if (porting::getInputDialogState() == -1)
270 m_jni_field_name.clear();
272 gui::IGUIElement *e = getElementFromId(ID_confirmPassword);
274 if (!e || e->getType() != irr::gui::EGUIET_EDIT_BOX)
277 std::string text = porting::getInputDialogValue();
278 e->setText(utf8_to_wide(text).c_str());