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 <IGUICheckBox.h>
22 #include <IGUIEditBox.h>
23 #include <IGUIButton.h>
24 #include <IGUIStaticText.h>
30 const int ID_oldPassword = 256;
31 const int ID_newPassword1 = 257;
32 const int ID_newPassword2 = 258;
33 const int ID_change = 259;
34 const int ID_message = 260;
35 const int ID_cancel = 261;
37 GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
38 gui::IGUIElement* parent, s32 id,
39 IMenuManager *menumgr,
42 GUIModalMenu(env, parent, id, menumgr),
47 GUIPasswordChange::~GUIPasswordChange()
52 void GUIPasswordChange::removeChildren()
54 const core::list<gui::IGUIElement *> &children = getChildren();
55 core::list<gui::IGUIElement *> children_copy;
56 for (gui::IGUIElement *i : children) {
57 children_copy.push_back(i);
60 for (gui::IGUIElement *i : children_copy) {
64 void GUIPasswordChange::regenerateGui(v2u32 screensize)
77 Calculate new sizes and positions
79 const float s = m_gui_scale;
80 DesiredRect = core::rect<s32>(
81 screensize.X / 2 - 580 * s / 2,
82 screensize.Y / 2 - 300 * s / 2,
83 screensize.X / 2 + 580 * s / 2,
84 screensize.Y / 2 + 300 * s / 2
86 recalculateAbsolutePosition(false);
88 v2s32 size = DesiredRect.getSize();
89 v2s32 topleft_client(40 * s, 0);
98 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
99 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
100 text = wgettext("Old Password");
101 Environment->addStaticText(text, rect, false, true, this, -1);
105 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
106 rect += topleft_client + v2s32(160 * s, ypos);
107 gui::IGUIEditBox *e = Environment->addEditBox(
108 m_oldpass.c_str(), rect, true, this, ID_oldPassword);
109 Environment->setFocus(e);
110 e->setPasswordBox(true);
114 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
115 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
116 text = wgettext("New Password");
117 Environment->addStaticText(text, rect, false, true, this, -1);
121 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
122 rect += topleft_client + v2s32(160 * s, ypos);
123 gui::IGUIEditBox *e = Environment->addEditBox(
124 m_newpass.c_str(), rect, true, this, ID_newPassword1);
125 e->setPasswordBox(true);
129 core::rect<s32> rect(0, 0, 150 * s, 20 * s);
130 rect += topleft_client + v2s32(25 * s, ypos + 6 * s);
131 text = wgettext("Confirm Password");
132 Environment->addStaticText(text, rect, false, true, this, -1);
136 core::rect<s32> rect(0, 0, 230 * s, 30 * s);
137 rect += topleft_client + v2s32(160 * s, ypos);
138 gui::IGUIEditBox *e = Environment->addEditBox(
139 m_newpass_confirm.c_str(), rect, true, this, ID_newPassword2);
140 e->setPasswordBox(true);
145 core::rect<s32> rect(0, 0, 100 * s, 30 * s);
146 rect = rect + v2s32(size.X / 4 + 56 * s, ypos);
147 text = wgettext("Change");
148 Environment->addButton(rect, this, ID_change, text);
152 core::rect<s32> rect(0, 0, 100 * s, 30 * s);
153 rect = rect + v2s32(size.X / 4 + 185 * s, ypos);
154 text = wgettext("Cancel");
155 Environment->addButton(rect, this, ID_cancel, text);
161 core::rect<s32> rect(0, 0, 300 * s, 20 * s);
162 rect += topleft_client + v2s32(35 * s, ypos);
163 text = wgettext("Passwords do not match!");
165 Environment->addStaticText(
166 text, rect, false, true, this, ID_message);
167 e->setVisible(false);
172 void GUIPasswordChange::drawMenu()
174 gui::IGUISkin *skin = Environment->getSkin();
177 video::IVideoDriver *driver = Environment->getVideoDriver();
179 video::SColor bgcolor(140, 0, 0, 0);
180 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
182 gui::IGUIElement::draw();
188 void GUIPasswordChange::acceptInput()
191 e = getElementFromId(ID_oldPassword);
193 m_oldpass = e->getText();
194 e = getElementFromId(ID_newPassword1);
196 m_newpass = e->getText();
197 e = getElementFromId(ID_newPassword2);
199 m_newpass_confirm = e->getText();
202 bool GUIPasswordChange::processInput()
204 if (m_newpass != m_newpass_confirm) {
205 gui::IGUIElement *e = getElementFromId(ID_message);
210 m_client->sendChangePassword(wide_to_utf8(m_oldpass), wide_to_utf8(m_newpass));
214 bool GUIPasswordChange::OnEvent(const SEvent &event)
216 if (event.EventType == EET_KEY_INPUT_EVENT) {
218 if ((event.KeyInput.Key == KEY_ESCAPE ||
219 event.KeyInput.Key == KEY_CANCEL) &&
220 event.KeyInput.PressedDown) {
225 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
232 if (event.EventType == EET_GUI_EVENT) {
233 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
235 if (!canTakeFocus(event.GUIEvent.Element)) {
236 dstream << "GUIPasswordChange: Not allowing focus change."
238 // Returning true disables focus change
242 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
243 switch (event.GUIEvent.Caller->getID()) {
254 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
255 switch (event.GUIEvent.Caller->getID()) {
257 case ID_newPassword1:
258 case ID_newPassword2:
267 return Parent ? Parent->OnEvent(event) : false;
270 std::string GUIPasswordChange::getNameByID(s32 id)
274 return "old_password";
275 case ID_newPassword1:
276 return "new_password_1";
277 case ID_newPassword2:
278 return "new_password_2";
284 bool GUIPasswordChange::getAndroidUIInput()
286 if (!hasAndroidUIInput())
289 gui::IGUIElement *e = nullptr;
290 if (m_jni_field_name == "old_password")
291 e = getElementFromId(ID_oldPassword);
292 else if (m_jni_field_name == "new_password_1")
293 e = getElementFromId(ID_newPassword1);
294 else if (m_jni_field_name == "new_password_2")
295 e = getElementFromId(ID_newPassword2);
298 std::string text = porting::getInputDialogValue();
299 e->setText(utf8_to_wide(text).c_str());
301 m_jni_field_name.clear();