Line data Source code
1 : /*
2 : Part of Minetest
3 : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 : Copyright (C) 2013 Ciaran Gultnieks <ciaran@ciarang.com>
5 :
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.
9 :
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.
17 : */
18 :
19 : #include "guiPasswordChange.h"
20 : #include "debug.h"
21 : #include "serialization.h"
22 : #include <string>
23 : #include <IGUICheckBox.h>
24 : #include <IGUIEditBox.h>
25 : #include <IGUIButton.h>
26 : #include <IGUIStaticText.h>
27 : #include <IGUIFont.h>
28 :
29 : #include "gettext.h"
30 :
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 :
37 0 : GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
38 : gui::IGUIElement* parent, s32 id,
39 : IMenuManager *menumgr,
40 : Client* client
41 : ):
42 : GUIModalMenu(env, parent, id, menumgr),
43 0 : m_client(client)
44 : {
45 0 : }
46 :
47 0 : GUIPasswordChange::~GUIPasswordChange()
48 : {
49 0 : removeChildren();
50 0 : }
51 :
52 0 : void GUIPasswordChange::removeChildren()
53 : {
54 : {
55 0 : gui::IGUIElement *e = getElementFromId(ID_oldPassword);
56 0 : if(e != NULL)
57 0 : e->remove();
58 : }
59 : {
60 0 : gui::IGUIElement *e = getElementFromId(ID_newPassword1);
61 0 : if(e != NULL)
62 0 : e->remove();
63 : }
64 : {
65 0 : gui::IGUIElement *e = getElementFromId(ID_newPassword2);
66 0 : if(e != NULL)
67 0 : e->remove();
68 : }
69 : {
70 0 : gui::IGUIElement *e = getElementFromId(ID_change);
71 0 : if(e != NULL)
72 0 : e->remove();
73 : }
74 0 : }
75 :
76 0 : void GUIPasswordChange::regenerateGui(v2u32 screensize)
77 : {
78 : /*
79 : Remove stuff
80 : */
81 0 : removeChildren();
82 :
83 : /*
84 : Calculate new sizes and positions
85 : */
86 : core::rect<s32> rect(
87 0 : screensize.X/2 - 580/2,
88 0 : screensize.Y/2 - 300/2,
89 0 : screensize.X/2 + 580/2,
90 0 : screensize.Y/2 + 300/2
91 0 : );
92 :
93 0 : DesiredRect = rect;
94 0 : recalculateAbsolutePosition(false);
95 :
96 0 : v2s32 size = rect.getSize();
97 0 : v2s32 topleft_client(40, 0);
98 :
99 : const wchar_t *text;
100 :
101 : /*
102 : Add stuff
103 : */
104 0 : s32 ypos = 50;
105 : {
106 0 : core::rect<s32> rect(0, 0, 150, 20);
107 0 : rect += topleft_client + v2s32(25, ypos+6);
108 0 : text = wgettext("Old Password");
109 0 : Environment->addStaticText(text, rect, false, true, this, -1);
110 0 : delete[] text;
111 : }
112 : {
113 0 : core::rect<s32> rect(0, 0, 230, 30);
114 0 : rect += topleft_client + v2s32(160, ypos);
115 : gui::IGUIEditBox *e =
116 0 : Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
117 0 : Environment->setFocus(e);
118 0 : e->setPasswordBox(true);
119 : }
120 0 : ypos += 50;
121 : {
122 0 : core::rect<s32> rect(0, 0, 150, 20);
123 0 : rect += topleft_client + v2s32(25, ypos+6);
124 0 : text = wgettext("New Password");
125 0 : Environment->addStaticText(text, rect, false, true, this, -1);
126 0 : delete[] text;
127 : }
128 : {
129 0 : core::rect<s32> rect(0, 0, 230, 30);
130 0 : rect += topleft_client + v2s32(160, ypos);
131 : gui::IGUIEditBox *e =
132 0 : Environment->addEditBox(L"", rect, true, this, ID_newPassword1);
133 0 : e->setPasswordBox(true);
134 : }
135 0 : ypos += 50;
136 : {
137 0 : core::rect<s32> rect(0, 0, 150, 20);
138 0 : rect += topleft_client + v2s32(25, ypos+6);
139 0 : text = wgettext("Confirm Password");
140 0 : Environment->addStaticText(text, rect, false, true, this, -1);
141 0 : delete[] text;
142 : }
143 : {
144 0 : core::rect<s32> rect(0, 0, 230, 30);
145 0 : rect += topleft_client + v2s32(160, ypos);
146 : gui::IGUIEditBox *e =
147 0 : Environment->addEditBox(L"", rect, true, this, ID_newPassword2);
148 0 : e->setPasswordBox(true);
149 : }
150 :
151 0 : ypos += 50;
152 : {
153 0 : core::rect<s32> rect(0, 0, 140, 30);
154 0 : rect = rect + v2s32(size.X/2-140/2, ypos);
155 0 : text = wgettext("Change");
156 0 : Environment->addButton(rect, this, ID_change, text);
157 0 : delete[] text;
158 : }
159 :
160 0 : ypos += 50;
161 : {
162 0 : core::rect<s32> rect(0, 0, 300, 20);
163 0 : rect += topleft_client + v2s32(35, ypos);
164 0 : text = wgettext("Passwords do not match!");
165 : IGUIElement *e =
166 0 : Environment->addStaticText(
167 : text,
168 0 : rect, false, true, this, ID_message);
169 0 : e->setVisible(false);
170 0 : delete[] text;
171 : }
172 0 : }
173 :
174 0 : void GUIPasswordChange::drawMenu()
175 : {
176 0 : gui::IGUISkin* skin = Environment->getSkin();
177 0 : if (!skin)
178 0 : return;
179 0 : video::IVideoDriver* driver = Environment->getVideoDriver();
180 :
181 0 : video::SColor bgcolor(140,0,0,0);
182 0 : driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
183 :
184 0 : gui::IGUIElement::draw();
185 : }
186 :
187 0 : bool GUIPasswordChange::acceptInput()
188 : {
189 0 : std::wstring oldpass;
190 0 : std::wstring newpass;
191 : gui::IGUIElement *e;
192 0 : e = getElementFromId(ID_oldPassword);
193 0 : if(e != NULL)
194 0 : oldpass = e->getText();
195 0 : e = getElementFromId(ID_newPassword1);
196 0 : if(e != NULL)
197 0 : newpass = e->getText();
198 0 : e = getElementFromId(ID_newPassword2);
199 0 : if(e != NULL && newpass != e->getText())
200 : {
201 0 : e = getElementFromId(ID_message);
202 0 : if(e != NULL)
203 0 : e->setVisible(true);
204 0 : return false;
205 : }
206 0 : m_client->sendChangePassword(wide_to_narrow(oldpass),
207 0 : wide_to_narrow(newpass));
208 0 : return true;
209 : }
210 :
211 0 : bool GUIPasswordChange::OnEvent(const SEvent& event)
212 : {
213 0 : if(event.EventType==EET_KEY_INPUT_EVENT)
214 : {
215 0 : if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
216 : {
217 0 : quitMenu();
218 0 : return true;
219 : }
220 0 : if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
221 : {
222 0 : if(acceptInput())
223 0 : quitMenu();
224 0 : return true;
225 : }
226 : }
227 0 : if(event.EventType==EET_GUI_EVENT)
228 : {
229 0 : if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
230 0 : && isVisible())
231 : {
232 0 : if(!canTakeFocus(event.GUIEvent.Element))
233 : {
234 0 : dstream<<"GUIPasswordChange: Not allowing focus change."
235 0 : <<std::endl;
236 : // Returning true disables focus change
237 0 : return true;
238 : }
239 : }
240 0 : if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
241 : {
242 0 : switch(event.GUIEvent.Caller->getID())
243 : {
244 : case ID_change:
245 0 : if(acceptInput())
246 0 : quitMenu();
247 0 : return true;
248 : }
249 : }
250 0 : if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
251 : {
252 0 : switch(event.GUIEvent.Caller->getID())
253 : {
254 : case ID_oldPassword:
255 : case ID_newPassword1:
256 : case ID_newPassword2:
257 0 : if(acceptInput())
258 0 : quitMenu();
259 0 : return true;
260 : }
261 : }
262 : }
263 :
264 0 : return Parent ? Parent->OnEvent(event) : false;
265 3 : }
266 :
|