GlobalLobbyLoginWindow.cpp 4.9 KB

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