GlobalLobbyLoginWindow.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * GlobalLobbyLoginWindow.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "GlobalLobbyLoginWindow.h"
  12. #include "GlobalLobbyClient.h"
  13. #include "GlobalLobbyWindow.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/WindowHandler.h"
  16. #include "../widgets/TextControls.h"
  17. #include "../widgets/Images.h"
  18. #include "../widgets/Buttons.h"
  19. #include "../widgets/MiscWidgets.h"
  20. #include "../CGameInfo.h"
  21. #include "../CServerHandler.h"
  22. #include "../../lib/CGeneralTextHandler.h"
  23. #include "../../lib/MetaString.h"
  24. #include "../../lib/CConfigHandler.h"
  25. GlobalLobbyLoginWindow::GlobalLobbyLoginWindow()
  26. : CWindowObject(BORDERED)
  27. {
  28. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  29. pos.w = 200;
  30. pos.h = 200;
  31. background = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  32. labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.login.title"));
  33. labelUsername = std::make_shared<CLabel>( 10, 45, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("vcmi.lobby.login.username"));
  34. backgroundUsername = std::make_shared<TransparentFilledRectangle>(Rect(10, 70, 180, 20), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64));
  35. inputUsername = std::make_shared<CTextInput>(Rect(15, 73, 176, 16), FONT_SMALL, nullptr, ETextAlignment::TOPLEFT, true);
  36. buttonLogin = std::make_shared<CButton>(Point(10, 160), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onLogin(); });
  37. buttonClose = std::make_shared<CButton>(Point(126, 160), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); });
  38. labelStatus = std::make_shared<CTextBox>( "", Rect(15, 95, 175, 60), 1, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  39. background->playerColored(PlayerColor(1));
  40. inputUsername->setText(settings["lobby"]["displayName"].String());
  41. inputUsername->cb += [this](const std::string & text)
  42. {
  43. buttonLogin->block(text.empty());
  44. };
  45. center();
  46. }
  47. void GlobalLobbyLoginWindow::onClose()
  48. {
  49. close();
  50. // TODO: abort ongoing connection attempt, if any
  51. }
  52. void GlobalLobbyLoginWindow::onLogin()
  53. {
  54. Settings config = settings.write["lobby"]["displayName"];
  55. config->String() = inputUsername->getText();
  56. labelStatus->setText(CGI->generaltexth->translate("vcmi.lobby.login.connecting"));
  57. if (!CSH->getGlobalLobby().isConnected())
  58. CSH->getGlobalLobby().connect();
  59. buttonClose->block(true);
  60. }
  61. void GlobalLobbyLoginWindow::onConnectionSuccess()
  62. {
  63. close();
  64. GH.windows().pushWindow(CSH->getGlobalLobby().createLobbyWindow());
  65. }
  66. void GlobalLobbyLoginWindow::onConnectionFailed(const std::string & reason)
  67. {
  68. MetaString formatter;
  69. formatter.appendTextID("vcmi.lobby.login.error");
  70. formatter.replaceRawString(reason);
  71. labelStatus->setText(formatter.toString());
  72. buttonClose->block(false);
  73. }