3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 Ciaran Gultnieks <ciaran@ciarang.com>
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "guiPasswordChange.h"
20 #include "client/client.h"
21 #include "guiButton.h"
22 #include <IGUICheckBox.h>
23 #include <IGUIEditBox.h>
24 #include <IGUIButton.h>
25 #include <IGUIStaticText.h>
31 const int ID_oldPassword = 256;
32 const int ID_newPassword1 = 257;
33 const int ID_newPassword2 = 258;
34 const int ID_change = 259;
35 const int ID_message = 260;
36 const int ID_cancel = 261;
38 GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
39 gui::IGUIElement* parent, s32 id,
40 IMenuManager *menumgr,
42 ISimpleTextureSource *tsrc
44 GUIModalMenu(env, parent, id, menumgr),
50 GUIPasswordChange::~GUIPasswordChange()
55 void GUIPasswordChange::removeChildren()
57 const core::list<gui::IGUIElement *> &children = getChildren();
58 core::list<gui::IGUIElement *> children_copy;
59 for (gui::IGUIElement *i : children) {
60 children_copy.push_back(i);
63 for (gui::IGUIElement *i : children_copy) {
67 void GUIPasswordChange::regenerateGui(v2u32 screensize)
80 Calculate new sizes and positions
83 const float s = m_gui_scale * porting::getDisplayDensity() / 2;
85 const float s = m_gui_scale;
87 DesiredRect = core::rect<s32>(
88 screensize.X / 2 - 580 * s / 2,
89 screensize.Y / 2 - 300 * s / 2,
90 screensize.X / 2 + 580 * s / 2,
91 screensize.Y / 2 + 300 * s / 2
93 recalculateAbsolutePosition(false);
95 v2s32 size = DesiredRect.getSize();
96 v2s32 topleft_client(40 * s, 0);
105 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
106 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
107 text = wgettext("Old Password");
108 Environment->addStaticText(text, rect, false, true, this, -1);
112 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
113 rect += topleft_client + v2s32(160 * s, ypos);
114 gui::IGUIEditBox *e = Environment->addEditBox(
115 m_oldpass.c_str(), rect, true, this, ID_oldPassword);
116 Environment->setFocus(e);
117 e->setPasswordBox(true);
121 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
122 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
123 text = wgettext("New Password");
124 Environment->addStaticText(text, rect, false, true, this, -1);
128 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
129 rect += topleft_client + v2s32(160 * s, ypos);
130 gui::IGUIEditBox *e = Environment->addEditBox(
131 m_newpass.c_str(), rect, true, this, ID_newPassword1);
132 e->setPasswordBox(true);
136 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
137 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
138 text = wgettext("Confirm Password");
139 Environment->addStaticText(text, rect, false, true, this, -1);
143 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
144 rect += topleft_client + v2s32(160 * s, ypos);
145 gui::IGUIEditBox *e = Environment->addEditBox(
146 m_newpass_confirm.c_str(), rect, true, this, ID_newPassword2);
147 e->setPasswordBox(true);
152 core::rect<s32> rect(0, 0, 100 * s, 30 * s);
153 rect = rect + v2s32(size.X / 4 + 56 * s, ypos);
154 text = wgettext("Change");
155 GUIButton::addButton(Environment, rect, m_tsrc, this, ID_change, text);
159 core::rect<s32> rect(0, 0, 100 * s, 30 * s);
160 rect = rect + v2s32(size.X / 4 + 185 * s, ypos);
161 text = wgettext("Cancel");
162 GUIButton::addButton(Environment, rect, m_tsrc, this, ID_cancel, text);
168 core::rect<s32> rect(0, 0, 300 * s, 20 * s);
169 rect += topleft_client + v2s32(35 * s, ypos);
170 text = wgettext("Passwords do not match!");
172 Environment->addStaticText(
173 text, rect, false, true, this, ID_message);
174 e->setVisible(false);
179 void GUIPasswordChange::drawMenu()
181 gui::IGUISkin *skin = Environment->getSkin();
184 video::IVideoDriver *driver = Environment->getVideoDriver();
186 video::SColor bgcolor(140, 0, 0, 0);
187 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
189 gui::IGUIElement::draw();
195 void GUIPasswordChange::acceptInput()
198 e = getElementFromId(ID_oldPassword);
200 m_oldpass = e->getText();
201 e = getElementFromId(ID_newPassword1);
203 m_newpass = e->getText();
204 e = getElementFromId(ID_newPassword2);
206 m_newpass_confirm = e->getText();
209 bool GUIPasswordChange::processInput()
211 if (m_newpass != m_newpass_confirm) {
212 gui::IGUIElement *e = getElementFromId(ID_message);
217 m_client->sendChangePassword(wide_to_utf8(m_oldpass), wide_to_utf8(m_newpass));
221 bool GUIPasswordChange::OnEvent(const SEvent &event)
223 if (event.EventType == EET_KEY_INPUT_EVENT) {
225 if ((event.KeyInput.Key == KEY_ESCAPE ||
226 event.KeyInput.Key == KEY_CANCEL) &&
227 event.KeyInput.PressedDown) {
232 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
239 if (event.EventType == EET_GUI_EVENT) {
240 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
242 if (!canTakeFocus(event.GUIEvent.Element)) {
243 infostream << "GUIPasswordChange: Not allowing focus change."
245 // Returning true disables focus change
249 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
250 switch (event.GUIEvent.Caller->getID()) {
261 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
262 switch (event.GUIEvent.Caller->getID()) {
264 case ID_newPassword1:
265 case ID_newPassword2:
274 return Parent ? Parent->OnEvent(event) : false;
277 std::string GUIPasswordChange::getNameByID(s32 id)
281 return "old_password";
282 case ID_newPassword1:
283 return "new_password_1";
284 case ID_newPassword2:
285 return "new_password_2";
291 bool GUIPasswordChange::getAndroidUIInput()
293 if (!hasAndroidUIInput())
297 if (porting::getInputDialogState() == -1)
300 gui::IGUIElement *e = nullptr;
301 if (m_jni_field_name == "old_password")
302 e = getElementFromId(ID_oldPassword);
303 else if (m_jni_field_name == "new_password_1")
304 e = getElementFromId(ID_newPassword1);
305 else if (m_jni_field_name == "new_password_2")
306 e = getElementFromId(ID_newPassword2);
307 m_jni_field_name.clear();
309 if (!e || e->getType() != irr::gui::EGUIET_EDIT_BOX)
312 std::string text = porting::getInputDialogValue();
313 e->setText(utf8_to_wide(text).c_str());