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()
56 gui::IGUIElement *e = getElementFromId(ID_oldPassword);
61 gui::IGUIElement *e = getElementFromId(ID_newPassword1);
66 gui::IGUIElement *e = getElementFromId(ID_newPassword2);
71 gui::IGUIElement *e = getElementFromId(ID_change);
76 gui::IGUIElement *e = getElementFromId(ID_cancel);
82 void GUIPasswordChange::regenerateGui(v2u32 screensize)
90 Calculate new sizes and positions
93 screensize.X/2 - 580/2,
94 screensize.Y/2 - 300/2,
95 screensize.X/2 + 580/2,
96 screensize.Y/2 + 300/2
100 recalculateAbsolutePosition(false);
102 v2s32 size = rect.getSize();
103 v2s32 topleft_client(40, 0);
112 core::rect<s32> rect(0, 0, 150, 20);
113 rect += topleft_client + v2s32(25, ypos + 6);
114 text = wgettext("Old Password");
115 Environment->addStaticText(text, rect, false, true, this, -1);
119 core::rect<s32> rect(0, 0, 230, 30);
120 rect += topleft_client + v2s32(160, ypos);
121 gui::IGUIEditBox *e = Environment->addEditBox(
122 L"", rect, true, this, ID_oldPassword);
123 Environment->setFocus(e);
124 e->setPasswordBox(true);
128 core::rect<s32> rect(0, 0, 150, 20);
129 rect += topleft_client + v2s32(25, ypos + 6);
130 text = wgettext("New Password");
131 Environment->addStaticText(text, rect, false, true, this, -1);
135 core::rect<s32> rect(0, 0, 230, 30);
136 rect += topleft_client + v2s32(160, ypos);
137 gui::IGUIEditBox *e = Environment->addEditBox(
138 L"", rect, true, this, ID_newPassword1);
139 e->setPasswordBox(true);
143 core::rect<s32> rect(0, 0, 150, 20);
144 rect += topleft_client + v2s32(25, ypos + 6);
145 text = wgettext("Confirm Password");
146 Environment->addStaticText(text, rect, false, true, this, -1);
150 core::rect<s32> rect(0, 0, 230, 30);
151 rect += topleft_client + v2s32(160, ypos);
152 gui::IGUIEditBox *e = Environment->addEditBox(
153 L"", rect, true, this, ID_newPassword2);
154 e->setPasswordBox(true);
159 core::rect<s32> rect(0, 0, 100, 30);
160 rect = rect + v2s32(size.X / 4 + 56, ypos);
161 text = wgettext("Change");
162 Environment->addButton(rect, this, ID_change, text);
166 core::rect<s32> rect(0, 0, 100, 30);
167 rect = rect + v2s32(size.X / 4 + 185, ypos);
168 text = wgettext("Cancel");
169 Environment->addButton(rect, this, ID_cancel, text);
175 core::rect<s32> rect(0, 0, 300, 20);
176 rect += topleft_client + v2s32(35, ypos);
177 text = wgettext("Passwords do not match!");
179 Environment->addStaticText(
180 text, rect, false, true, this, ID_message);
181 e->setVisible(false);
186 void GUIPasswordChange::drawMenu()
188 gui::IGUISkin *skin = Environment->getSkin();
191 video::IVideoDriver *driver = Environment->getVideoDriver();
193 video::SColor bgcolor(140, 0, 0, 0);
194 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
196 gui::IGUIElement::draw();
199 bool GUIPasswordChange::acceptInput()
201 std::wstring oldpass;
202 std::wstring newpass;
204 e = getElementFromId(ID_oldPassword);
206 oldpass = e->getText();
207 e = getElementFromId(ID_newPassword1);
209 newpass = e->getText();
210 e = getElementFromId(ID_newPassword2);
211 if (e != NULL && newpass != e->getText()) {
212 e = getElementFromId(ID_message);
217 m_client->sendChangePassword(wide_to_utf8(oldpass), wide_to_utf8(newpass));
221 bool GUIPasswordChange::OnEvent(const SEvent &event)
223 if (event.EventType == EET_KEY_INPUT_EVENT) {
224 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
228 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
234 if (event.EventType == EET_GUI_EVENT) {
235 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST &&
237 if (!canTakeFocus(event.GUIEvent.Element)) {
238 dstream << "GUIPasswordChange: Not allowing focus change."
240 // Returning true disables focus change
244 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
245 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:
267 return Parent ? Parent->OnEvent(event) : false;