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