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 "serialization.h"
23 #include <IGUICheckBox.h>
24 #include <IGUIEditBox.h>
25 #include <IGUIButton.h>
26 #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,
43 GUIModalMenu(env, parent, id, menumgr),
48 GUIPasswordChange::~GUIPasswordChange()
53 void GUIPasswordChange::removeChildren()
55 const core::list<gui::IGUIElement *> &children = getChildren();
56 core::list<gui::IGUIElement *> children_copy;
57 for (core::list<gui::IGUIElement *>::ConstIterator i = children.begin();
58 i != children.end(); i++) {
59 children_copy.push_back(*i);
61 for (core::list<gui::IGUIElement *>::Iterator i = children_copy.begin();
62 i != children_copy.end(); i++) {
66 void GUIPasswordChange::regenerateGui(v2u32 screensize)
79 Calculate new sizes and positions
82 screensize.X/2 - 580/2,
83 screensize.Y/2 - 300/2,
84 screensize.X/2 + 580/2,
85 screensize.Y/2 + 300/2
89 recalculateAbsolutePosition(false);
91 v2s32 size = rect.getSize();
92 v2s32 topleft_client(40, 0);
101 core::rect<s32> rect(0, 0, 150, 20);
102 rect += topleft_client + v2s32(25, ypos + 6);
103 text = wgettext("Old Password");
104 Environment->addStaticText(text, rect, false, true, this, -1);
108 core::rect<s32> rect(0, 0, 230, 30);
109 rect += topleft_client + v2s32(160, ypos);
110 gui::IGUIEditBox *e = Environment->addEditBox(
111 m_oldpass.c_str(), rect, true, this, ID_oldPassword);
112 Environment->setFocus(e);
113 e->setPasswordBox(true);
117 core::rect<s32> rect(0, 0, 150, 20);
118 rect += topleft_client + v2s32(25, ypos + 6);
119 text = wgettext("New Password");
120 Environment->addStaticText(text, rect, false, true, this, -1);
124 core::rect<s32> rect(0, 0, 230, 30);
125 rect += topleft_client + v2s32(160, ypos);
126 gui::IGUIEditBox *e = Environment->addEditBox(
127 m_newpass.c_str(), rect, true, this, ID_newPassword1);
128 e->setPasswordBox(true);
132 core::rect<s32> rect(0, 0, 150, 20);
133 rect += topleft_client + v2s32(25, ypos + 6);
134 text = wgettext("Confirm Password");
135 Environment->addStaticText(text, rect, false, true, this, -1);
139 core::rect<s32> rect(0, 0, 230, 30);
140 rect += topleft_client + v2s32(160, ypos);
141 gui::IGUIEditBox *e = Environment->addEditBox(
142 m_newpass_confirm.c_str(), rect, true, this, ID_newPassword2);
143 e->setPasswordBox(true);
148 core::rect<s32> rect(0, 0, 100, 30);
149 rect = rect + v2s32(size.X / 4 + 56, ypos);
150 text = wgettext("Change");
151 Environment->addButton(rect, this, ID_change, text);
155 core::rect<s32> rect(0, 0, 100, 30);
156 rect = rect + v2s32(size.X / 4 + 185, ypos);
157 text = wgettext("Cancel");
158 Environment->addButton(rect, this, ID_cancel, text);
164 core::rect<s32> rect(0, 0, 300, 20);
165 rect += topleft_client + v2s32(35, ypos);
166 text = wgettext("Passwords do not match!");
168 Environment->addStaticText(
169 text, rect, false, true, this, ID_message);
170 e->setVisible(false);
175 void GUIPasswordChange::drawMenu()
177 gui::IGUISkin *skin = Environment->getSkin();
180 video::IVideoDriver *driver = Environment->getVideoDriver();
182 video::SColor bgcolor(140, 0, 0, 0);
183 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
185 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) {
217 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
221 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
228 if (event.EventType == EET_GUI_EVENT) {
229 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
231 if (!canTakeFocus(event.GUIEvent.Element)) {
232 dstream << "GUIPasswordChange: Not allowing focus change."
234 // Returning true disables focus change
238 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
239 switch (event.GUIEvent.Caller->getID()) {
250 if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
251 switch (event.GUIEvent.Caller->getID()) {
253 case ID_newPassword1:
254 case ID_newPassword2:
263 return Parent ? Parent->OnEvent(event) : false;