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"
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
80 screensize.X/2 - 580/2,
81 screensize.Y/2 - 300/2,
82 screensize.X/2 + 580/2,
83 screensize.Y/2 + 300/2
87 recalculateAbsolutePosition(false);
89 v2s32 size = rect.getSize();
90 v2s32 topleft_client(40, 0);
99 core::rect<s32> rect(0, 0, 150, 20);
100 rect += topleft_client + v2s32(25, ypos + 6);
101 text = wgettext("Old Password");
102 Environment->addStaticText(text, rect, false, true, this, -1);
106 core::rect<s32> rect(0, 0, 230, 30);
107 rect += topleft_client + v2s32(160, ypos);
108 gui::IGUIEditBox *e = Environment->addEditBox(
109 m_oldpass.c_str(), rect, true, this, ID_oldPassword);
110 Environment->setFocus(e);
111 e->setPasswordBox(true);
115 core::rect<s32> rect(0, 0, 150, 20);
116 rect += topleft_client + v2s32(25, ypos + 6);
117 text = wgettext("New Password");
118 Environment->addStaticText(text, rect, false, true, this, -1);
122 core::rect<s32> rect(0, 0, 230, 30);
123 rect += topleft_client + v2s32(160, ypos);
124 gui::IGUIEditBox *e = Environment->addEditBox(
125 m_newpass.c_str(), rect, true, this, ID_newPassword1);
126 e->setPasswordBox(true);
130 core::rect<s32> rect(0, 0, 150, 20);
131 rect += topleft_client + v2s32(25, ypos + 6);
132 text = wgettext("Confirm Password");
133 Environment->addStaticText(text, rect, false, true, this, -1);
137 core::rect<s32> rect(0, 0, 230, 30);
138 rect += topleft_client + v2s32(160, ypos);
139 gui::IGUIEditBox *e = Environment->addEditBox(
140 m_newpass_confirm.c_str(), rect, true, this, ID_newPassword2);
141 e->setPasswordBox(true);
146 core::rect<s32> rect(0, 0, 100, 30);
147 rect = rect + v2s32(size.X / 4 + 56, ypos);
148 text = wgettext("Change");
149 Environment->addButton(rect, this, ID_change, text);
153 core::rect<s32> rect(0, 0, 100, 30);
154 rect = rect + v2s32(size.X / 4 + 185, ypos);
155 text = wgettext("Cancel");
156 Environment->addButton(rect, this, ID_cancel, text);
162 core::rect<s32> rect(0, 0, 300, 20);
163 rect += topleft_client + v2s32(35, ypos);
164 text = wgettext("Passwords do not match!");
166 Environment->addStaticText(
167 text, rect, false, true, this, ID_message);
168 e->setVisible(false);
173 void GUIPasswordChange::drawMenu()
175 gui::IGUISkin *skin = Environment->getSkin();
178 video::IVideoDriver *driver = Environment->getVideoDriver();
180 video::SColor bgcolor(140, 0, 0, 0);
181 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
183 gui::IGUIElement::draw();
189 void GUIPasswordChange::acceptInput()
192 e = getElementFromId(ID_oldPassword);
194 m_oldpass = e->getText();
195 e = getElementFromId(ID_newPassword1);
197 m_newpass = e->getText();
198 e = getElementFromId(ID_newPassword2);
200 m_newpass_confirm = e->getText();
203 bool GUIPasswordChange::processInput()
205 if (m_newpass != m_newpass_confirm) {
206 gui::IGUIElement *e = getElementFromId(ID_message);
211 m_client->sendChangePassword(wide_to_utf8(m_oldpass), wide_to_utf8(m_newpass));
215 bool GUIPasswordChange::OnEvent(const SEvent &event)
217 if (event.EventType == EET_KEY_INPUT_EVENT) {
219 if ((event.KeyInput.Key == KEY_ESCAPE ||
220 event.KeyInput.Key == KEY_CANCEL) &&
221 event.KeyInput.PressedDown) {
226 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
233 if (event.EventType == EET_GUI_EVENT) {
234 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
236 if (!canTakeFocus(event.GUIEvent.Element)) {
237 dstream << "GUIPasswordChange: Not allowing focus change."
239 // Returning true disables focus change
243 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
244 switch (event.GUIEvent.Caller->getID()) {
255 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
256 switch (event.GUIEvent.Caller->getID()) {
258 case ID_newPassword1:
259 case ID_newPassword2:
268 return Parent ? Parent->OnEvent(event) : false;
271 std::string GUIPasswordChange::getNameByID(s32 id)
275 return "old_password";
276 case ID_newPassword1:
277 return "new_password_1";
278 case ID_newPassword2:
279 return "new_password_2";
285 bool GUIPasswordChange::getAndroidUIInput()
287 if (!hasAndroidUIInput())
290 gui::IGUIElement *e = nullptr;
291 if (m_jni_field_name == "old_password")
292 e = getElementFromId(ID_oldPassword);
293 else if (m_jni_field_name == "new_password_1")
294 e = getElementFromId(ID_newPassword1);
295 else if (m_jni_field_name == "new_password_2")
296 e = getElementFromId(ID_newPassword2);
299 std::string text = porting::getInputDialogValue();
300 e->setText(utf8_to_wide(text).c_str());
302 m_jni_field_name.clear();