guiConfirmRegistration.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. Minetest
  3. Copyright (C) 2018 srifqi, Muhammad Rifqi Priyo Susanto
  4. <muhammadrifqipriyosusanto@gmail.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "guiConfirmRegistration.h"
  18. #include "client/client.h"
  19. #include "guiButton.h"
  20. #include <IGUICheckBox.h>
  21. #include <IGUIButton.h>
  22. #include <IGUIStaticText.h>
  23. #include <IGUIFont.h>
  24. #include "guiEditBoxWithScrollbar.h"
  25. #include "porting.h"
  26. #ifdef HAVE_TOUCHSCREENGUI
  27. #include "client/renderingengine.h"
  28. #endif
  29. #include "gettext.h"
  30. // Continuing from guiPasswordChange.cpp
  31. const int ID_confirmPassword = 262;
  32. const int ID_confirm = 263;
  33. const int ID_intotext = 264;
  34. const int ID_cancel = 265;
  35. const int ID_message = 266;
  36. GUIConfirmRegistration::GUIConfirmRegistration(gui::IGUIEnvironment *env,
  37. gui::IGUIElement *parent, s32 id, IMenuManager *menumgr, Client *client,
  38. const std::string &playername, const std::string &password,
  39. bool *aborted, ISimpleTextureSource *tsrc) :
  40. GUIModalMenu(env, parent, id, menumgr),
  41. m_client(client), m_playername(playername), m_password(password),
  42. m_aborted(aborted), m_tsrc(tsrc)
  43. {
  44. #ifdef HAVE_TOUCHSCREENGUI
  45. m_touchscreen_visible = false;
  46. #endif
  47. }
  48. void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
  49. {
  50. acceptInput();
  51. removeAllChildren();
  52. /*
  53. Calculate new sizes and positions
  54. */
  55. #ifdef HAVE_TOUCHSCREENGUI
  56. const float s = m_gui_scale * RenderingEngine::getDisplayDensity() / 2;
  57. #else
  58. const float s = m_gui_scale;
  59. #endif
  60. DesiredRect = core::rect<s32>(
  61. screensize.X / 2 - 600 * s / 2,
  62. screensize.Y / 2 - 360 * s / 2,
  63. screensize.X / 2 + 600 * s / 2,
  64. screensize.Y / 2 + 360 * s / 2
  65. );
  66. recalculateAbsolutePosition(false);
  67. v2s32 size = DesiredRect.getSize();
  68. v2s32 topleft_client(0, 0);
  69. const wchar_t *text;
  70. /*
  71. Add stuff
  72. */
  73. s32 ypos = 30 * s;
  74. {
  75. core::rect<s32> rect2(0, 0, 540 * s, 180 * s);
  76. rect2 += topleft_client + v2s32(30 * s, ypos);
  77. static const std::string info_text_template = strgettext(
  78. "You are about to join this server with the name \"%s\" for the "
  79. "first time.\n"
  80. "If you proceed, a new account using your credentials will be "
  81. "created on this server.\n"
  82. "Please retype your password and click 'Register and Join' to "
  83. "confirm account creation, or click 'Cancel' to abort.");
  84. char info_text_buf[1024];
  85. porting::mt_snprintf(info_text_buf, sizeof(info_text_buf),
  86. info_text_template.c_str(), m_playername.c_str());
  87. std::wstring info_text_w = utf8_to_wide(info_text_buf);
  88. gui::IGUIEditBox *e = new GUIEditBoxWithScrollBar(info_text_w.c_str(),
  89. true, Environment, this, ID_intotext, rect2, false, true);
  90. e->drop();
  91. e->setMultiLine(true);
  92. e->setWordWrap(true);
  93. e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
  94. }
  95. ypos += 200 * s;
  96. {
  97. core::rect<s32> rect2(0, 0, 540 * s, 30 * s);
  98. rect2 += topleft_client + v2s32(30 * s, ypos);
  99. gui::IGUIEditBox *e = Environment->addEditBox(m_pass_confirm.c_str(),
  100. rect2, true, this, ID_confirmPassword);
  101. e->setPasswordBox(true);
  102. Environment->setFocus(e);
  103. }
  104. ypos += 50 * s;
  105. {
  106. core::rect<s32> rect2(0, 0, 230 * s, 35 * s);
  107. rect2 = rect2 + v2s32(size.X / 2 - 220 * s, ypos);
  108. text = wgettext("Register and Join");
  109. GUIButton::addButton(Environment, rect2, m_tsrc, this, ID_confirm, text);
  110. delete[] text;
  111. }
  112. {
  113. core::rect<s32> rect2(0, 0, 120 * s, 35 * s);
  114. rect2 = rect2 + v2s32(size.X / 2 + 70 * s, ypos);
  115. text = wgettext("Cancel");
  116. GUIButton::addButton(Environment, rect2, m_tsrc, this, ID_cancel, text);
  117. delete[] text;
  118. }
  119. {
  120. core::rect<s32> rect2(0, 0, 500 * s, 40 * s);
  121. rect2 += topleft_client + v2s32(30 * s, ypos + 40 * s);
  122. text = wgettext("Passwords do not match!");
  123. IGUIElement *e = Environment->addStaticText(
  124. text, rect2, false, true, this, ID_message);
  125. e->setVisible(false);
  126. delete[] text;
  127. }
  128. }
  129. void GUIConfirmRegistration::drawMenu()
  130. {
  131. gui::IGUISkin *skin = Environment->getSkin();
  132. if (!skin)
  133. return;
  134. video::IVideoDriver *driver = Environment->getVideoDriver();
  135. video::SColor bgcolor(140, 0, 0, 0);
  136. driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
  137. gui::IGUIElement::draw();
  138. #ifdef __ANDROID__
  139. getAndroidUIInput();
  140. #endif
  141. }
  142. void GUIConfirmRegistration::closeMenu(bool goNext)
  143. {
  144. if (goNext) {
  145. m_client->confirmRegistration();
  146. } else {
  147. *m_aborted = true;
  148. infostream << "Connect aborted [Escape]" << std::endl;
  149. }
  150. quitMenu();
  151. }
  152. void GUIConfirmRegistration::acceptInput()
  153. {
  154. gui::IGUIElement *e;
  155. e = getElementFromId(ID_confirmPassword);
  156. if (e)
  157. m_pass_confirm = e->getText();
  158. }
  159. bool GUIConfirmRegistration::processInput()
  160. {
  161. if (utf8_to_wide(m_password) != m_pass_confirm) {
  162. gui::IGUIElement *e = getElementFromId(ID_message);
  163. if (e)
  164. e->setVisible(true);
  165. return false;
  166. }
  167. return true;
  168. }
  169. bool GUIConfirmRegistration::OnEvent(const SEvent &event)
  170. {
  171. if (event.EventType == EET_KEY_INPUT_EVENT) {
  172. // clang-format off
  173. if ((event.KeyInput.Key == KEY_ESCAPE ||
  174. event.KeyInput.Key == KEY_CANCEL) &&
  175. event.KeyInput.PressedDown) {
  176. closeMenu(false);
  177. return true;
  178. }
  179. // clang-format on
  180. if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
  181. acceptInput();
  182. if (processInput())
  183. closeMenu(true);
  184. return true;
  185. }
  186. }
  187. if (event.EventType != EET_GUI_EVENT)
  188. return Parent ? Parent->OnEvent(event) : false;
  189. if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) {
  190. if (!canTakeFocus(event.GUIEvent.Element)) {
  191. infostream << "GUIConfirmRegistration: Not allowing focus change."
  192. << std::endl;
  193. // Returning true disables focus change
  194. return true;
  195. }
  196. } else if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
  197. switch (event.GUIEvent.Caller->getID()) {
  198. case ID_confirm:
  199. acceptInput();
  200. if (processInput())
  201. closeMenu(true);
  202. return true;
  203. case ID_cancel:
  204. closeMenu(false);
  205. return true;
  206. }
  207. } else if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
  208. switch (event.GUIEvent.Caller->getID()) {
  209. case ID_confirmPassword:
  210. acceptInput();
  211. if (processInput())
  212. closeMenu(true);
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. #ifdef __ANDROID__
  219. bool GUIConfirmRegistration::getAndroidUIInput()
  220. {
  221. if (!hasAndroidUIInput() || m_jni_field_name != "password")
  222. return false;
  223. // still waiting
  224. if (porting::getInputDialogState() == -1)
  225. return true;
  226. m_jni_field_name.clear();
  227. gui::IGUIElement *e = getElementFromId(ID_confirmPassword);
  228. if (!e || e->getType() != irr::gui::EGUIET_EDIT_BOX)
  229. return false;
  230. std::string text = porting::getInputDialogValue();
  231. e->setText(utf8_to_wide(text).c_str());
  232. return false;
  233. }
  234. #endif