GlobalLobbyLoginWindow.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "../CGameInfo.h"
  15. #include "../CServerHandler.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/WindowHandler.h"
  18. #include "../widgets/Buttons.h"
  19. #include "../widgets/Images.h"
  20. #include "../widgets/GraphicalPrimitiveCanvas.h"
  21. #include "../widgets/MiscWidgets.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../../lib/CConfigHandler.h"
  24. #include "../../lib/CGeneralTextHandler.h"
  25. #include "../../lib/MetaString.h"
  26. GlobalLobbyLoginWindow::GlobalLobbyLoginWindow()
  27. : CWindowObject(BORDERED)
  28. {
  29. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  30. pos.w = 284;
  31. pos.h = 220;
  32. filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  33. labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.login.title"));
  34. labelUsername = std::make_shared<CLabel>( 10, 65, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("vcmi.lobby.login.username"));
  35. backgroundUsername = std::make_shared<TransparentFilledRectangle>(Rect(10, 90, 264, 20), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64));
  36. inputUsername = std::make_shared<CTextInput>(Rect(15, 93, 260, 16), FONT_SMALL, nullptr, ETextAlignment::TOPLEFT, true);
  37. buttonLogin = std::make_shared<CButton>(Point(10, 180), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onLogin(); });
  38. buttonClose = std::make_shared<CButton>(Point(210, 180), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); });
  39. labelStatus = std::make_shared<CTextBox>( "", Rect(15, 115, 255, 60), 1, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  40. auto buttonRegister = std::make_shared<CToggleButton>(Point(10, 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), 0);
  41. auto buttonLogin = std::make_shared<CToggleButton>(Point(146, 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), 0);
  42. buttonRegister->setTextOverlay(CGI->generaltexth->translate("vcmi.lobby.login.create"), EFonts::FONT_SMALL, Colors::YELLOW);
  43. buttonLogin->setTextOverlay(CGI->generaltexth->translate("vcmi.lobby.login.login"), EFonts::FONT_SMALL, Colors::YELLOW);
  44. toggleMode = std::make_shared<CToggleGroup>(nullptr);
  45. toggleMode->addToggle(0, buttonRegister);
  46. toggleMode->addToggle(1, buttonLogin);
  47. toggleMode->setSelected(settings["lobby"]["roomType"].Integer());
  48. toggleMode->addCallback([this](int index){onLoginModeChanged(index);});
  49. if (settings["lobby"]["accountID"].String().empty())
  50. {
  51. buttonLogin->block(true);
  52. toggleMode->setSelected(0);
  53. }
  54. else
  55. toggleMode->setSelected(1);
  56. filledBackground->playerColored(PlayerColor(1));
  57. inputUsername->cb += [this](const std::string & text)
  58. {
  59. this->buttonLogin->block(text.empty());
  60. };
  61. center();
  62. }
  63. void GlobalLobbyLoginWindow::onLoginModeChanged(int value)
  64. {
  65. if (value == 0)
  66. {
  67. inputUsername->setText("");
  68. }
  69. else
  70. {
  71. inputUsername->setText(settings["lobby"]["displayName"].String());
  72. }
  73. }
  74. void GlobalLobbyLoginWindow::onClose()
  75. {
  76. close();
  77. // TODO: abort ongoing connection attempt, if any
  78. }
  79. void GlobalLobbyLoginWindow::onLogin()
  80. {
  81. labelStatus->setText(CGI->generaltexth->translate("vcmi.lobby.login.connecting"));
  82. if(!CSH->getGlobalLobby().isConnected())
  83. CSH->getGlobalLobby().connect();
  84. else
  85. onConnectionSuccess();
  86. buttonClose->block(true);
  87. }
  88. void GlobalLobbyLoginWindow::onConnectionSuccess()
  89. {
  90. std::string accountID = settings["lobby"]["accountID"].String();
  91. if(toggleMode->getSelected() == 0)
  92. CSH->getGlobalLobby().sendClientRegister(inputUsername->getText());
  93. else
  94. CSH->getGlobalLobby().sendClientLogin();
  95. }
  96. void GlobalLobbyLoginWindow::onLoginSuccess()
  97. {
  98. close();
  99. CSH->getGlobalLobby().activateInterface();
  100. }
  101. void GlobalLobbyLoginWindow::onConnectionFailed(const std::string & reason)
  102. {
  103. MetaString formatter;
  104. formatter.appendTextID("vcmi.lobby.login.error");
  105. formatter.replaceRawString(reason);
  106. labelStatus->setText(formatter.toString());
  107. buttonClose->block(false);
  108. }