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>
28 #ifdef HAVE_TOUCHSCREENGUI
29 #include "client/renderingengine.h"
35 const int ID_oldPassword = 256;
36 const int ID_newPassword1 = 257;
37 const int ID_newPassword2 = 258;
38 const int ID_change = 259;
39 const int ID_message = 260;
40 const int ID_cancel = 261;
42 GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
43 gui::IGUIElement* parent, s32 id,
44 IMenuManager *menumgr,
46 ISimpleTextureSource *tsrc
48 GUIModalMenu(env, parent, id, menumgr),
54 void GUIPasswordChange::regenerateGui(v2u32 screensize)
67 Calculate new sizes and positions
69 #ifdef HAVE_TOUCHSCREENGUI
70 const float s = m_gui_scale * RenderingEngine::getDisplayDensity() / 2;
72 const float s = m_gui_scale;
74 DesiredRect = core::rect<s32>(
75 screensize.X / 2 - 580 * s / 2,
76 screensize.Y / 2 - 300 * s / 2,
77 screensize.X / 2 + 580 * s / 2,
78 screensize.Y / 2 + 300 * s / 2
80 recalculateAbsolutePosition(false);
82 v2s32 size = DesiredRect.getSize();
83 v2s32 topleft_client(40 * s, 0);
92 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
93 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
94 text = wgettext("Old Password");
95 Environment->addStaticText(text, rect, false, true, this, -1);
99 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
100 rect += topleft_client + v2s32(160 * s, ypos);
101 gui::IGUIEditBox *e = Environment->addEditBox(
102 m_oldpass.c_str(), rect, true, this, ID_oldPassword);
103 Environment->setFocus(e);
104 e->setPasswordBox(true);
108 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
109 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
110 text = wgettext("New Password");
111 Environment->addStaticText(text, rect, false, true, this, -1);
115 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
116 rect += topleft_client + v2s32(160 * s, ypos);
117 gui::IGUIEditBox *e = Environment->addEditBox(
118 m_newpass.c_str(), rect, true, this, ID_newPassword1);
119 e->setPasswordBox(true);
123 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
124 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
125 text = wgettext("Confirm Password");
126 Environment->addStaticText(text, rect, false, true, this, -1);
130 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
131 rect += topleft_client + v2s32(160 * s, ypos);
132 gui::IGUIEditBox *e = Environment->addEditBox(
133 m_newpass_confirm.c_str(), rect, true, this, ID_newPassword2);
134 e->setPasswordBox(true);
139 core::rect<s32> rect(0, 0, 100 * s, 30 * s);
140 rect = rect + v2s32(size.X / 4 + 56 * s, ypos);
141 text = wgettext("Change");
142 GUIButton::addButton(Environment, rect, m_tsrc, this, ID_change, text);
146 core::rect<s32> rect(0, 0, 100 * s, 30 * s);
147 rect = rect + v2s32(size.X / 4 + 185 * s, ypos);
148 text = wgettext("Cancel");
149 GUIButton::addButton(Environment, rect, m_tsrc, this, ID_cancel, text);
155 core::rect<s32> rect(0, 0, 300 * s, 20 * s);
156 rect += topleft_client + v2s32(35 * s, ypos);
157 text = wgettext("Passwords do not match!");
159 Environment->addStaticText(
160 text, rect, false, true, this, ID_message);
161 e->setVisible(false);
166 void GUIPasswordChange::drawMenu()
168 gui::IGUISkin *skin = Environment->getSkin();
171 video::IVideoDriver *driver = Environment->getVideoDriver();
173 video::SColor bgcolor(140, 0, 0, 0);
174 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
176 gui::IGUIElement::draw();
182 void GUIPasswordChange::acceptInput()
185 e = getElementFromId(ID_oldPassword);
187 m_oldpass = e->getText();
188 e = getElementFromId(ID_newPassword1);
190 m_newpass = e->getText();
191 e = getElementFromId(ID_newPassword2);
193 m_newpass_confirm = e->getText();
196 bool GUIPasswordChange::processInput()
198 if (m_newpass != m_newpass_confirm) {
199 gui::IGUIElement *e = getElementFromId(ID_message);
204 m_client->sendChangePassword(wide_to_utf8(m_oldpass), wide_to_utf8(m_newpass));
208 bool GUIPasswordChange::OnEvent(const SEvent &event)
210 if (event.EventType == EET_KEY_INPUT_EVENT) {
212 if ((event.KeyInput.Key == KEY_ESCAPE ||
213 event.KeyInput.Key == KEY_CANCEL) &&
214 event.KeyInput.PressedDown) {
219 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
226 if (event.EventType == EET_GUI_EVENT) {
227 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
229 if (!canTakeFocus(event.GUIEvent.Element)) {
230 infostream << "GUIPasswordChange: Not allowing focus change."
232 // Returning true disables focus change
236 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
237 switch (event.GUIEvent.Caller->getID()) {
248 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
249 switch (event.GUIEvent.Caller->getID()) {
251 case ID_newPassword1:
252 case ID_newPassword2:
261 return Parent ? Parent->OnEvent(event) : false;
264 std::string GUIPasswordChange::getNameByID(s32 id)
268 return "old_password";
269 case ID_newPassword1:
270 return "new_password_1";
271 case ID_newPassword2:
272 return "new_password_2";
278 bool GUIPasswordChange::getAndroidUIInput()
280 if (!hasAndroidUIInput())
284 if (porting::getInputDialogState() == -1)
287 gui::IGUIElement *e = nullptr;
288 if (m_jni_field_name == "old_password")
289 e = getElementFromId(ID_oldPassword);
290 else if (m_jni_field_name == "new_password_1")
291 e = getElementFromId(ID_newPassword1);
292 else if (m_jni_field_name == "new_password_2")
293 e = getElementFromId(ID_newPassword2);
294 m_jni_field_name.clear();
296 if (!e || e->getType() != irr::gui::EGUIET_EDIT_BOX)
299 std::string text = porting::getInputDialogValue();
300 e->setText(utf8_to_wide(text).c_str());