GlobalLobbyLoginWindow.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(), 265);
  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 currenly selected option
  58. }
  59. else
  60. {
  61. toggleMode->setSelected(1);
  62. onLoginModeChanged(1);
  63. }
  64. filledBackground->playerColored(PlayerColor(1));
  65. inputUsername->setCallback([this](const std::string & text)
  66. {
  67. this->buttonLogin->block(text.empty());
  68. });
  69. center();
  70. }
  71. void GlobalLobbyLoginWindow::onLoginModeChanged(int value)
  72. {
  73. if (value == 0)
  74. {
  75. inputUsername->enable();
  76. backgroundUsername->enable();
  77. labelUsernameTitle->enable();
  78. labelUsername->disable();
  79. }
  80. else
  81. {
  82. inputUsername->disable();
  83. backgroundUsername->disable();
  84. labelUsernameTitle->disable();
  85. labelUsername->enable();
  86. }
  87. redraw();
  88. }
  89. void GlobalLobbyLoginWindow::onClose()
  90. {
  91. close();
  92. // TODO: abort ongoing connection attempt, if any
  93. }
  94. void GlobalLobbyLoginWindow::onLogin()
  95. {
  96. labelStatus->setText(CGI->generaltexth->translate("vcmi.lobby.login.connecting"));
  97. if(!CSH->getGlobalLobby().isConnected())
  98. CSH->getGlobalLobby().connect();
  99. else
  100. onConnectionSuccess();
  101. buttonClose->block(true);
  102. }
  103. void GlobalLobbyLoginWindow::onConnectionSuccess()
  104. {
  105. std::string accountID = CSH->getGlobalLobby().getAccountID();
  106. if(toggleMode->getSelected() == 0)
  107. CSH->getGlobalLobby().sendClientRegister(inputUsername->getText());
  108. else
  109. CSH->getGlobalLobby().sendClientLogin();
  110. }
  111. void GlobalLobbyLoginWindow::onLoginSuccess()
  112. {
  113. close();
  114. CSH->getGlobalLobby().activateInterface();
  115. }
  116. void GlobalLobbyLoginWindow::onConnectionFailed(const std::string & reason)
  117. {
  118. MetaString formatter;
  119. formatter.appendTextID("vcmi.lobby.login.error");
  120. formatter.replaceRawString(reason);
  121. labelStatus->setText(formatter.toString());
  122. buttonClose->block(false);
  123. }