GlobalLobbyLoginWindow.cpp 4.9 KB

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