GlobalLobbyLoginWindow.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "../CServerHandler.h"
  14. #include "../GameEngine.h"
  15. #include "../gui/Shortcut.h"
  16. #include "../widgets/Buttons.h"
  17. #include "../widgets/CTextInput.h"
  18. #include "../widgets/Images.h"
  19. #include "../widgets/GraphicalPrimitiveCanvas.h"
  20. #include "../widgets/MiscWidgets.h"
  21. #include "../widgets/TextControls.h"
  22. #include "../../lib/CConfigHandler.h"
  23. #include "../../lib/texts/CGeneralTextHandler.h"
  24. #include "../../lib/texts/MetaString.h"
  25. #include "../../lib/VCMI_Lib.h"
  26. GlobalLobbyLoginWindow::GlobalLobbyLoginWindow()
  27. : CWindowObject(BORDERED)
  28. {
  29. OBJECT_CONSTRUCTION;
  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>(Rect(0, 0, pos.w, pos.h));
  36. labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, VLC->generaltexth->translate("vcmi.lobby.login.title"));
  37. labelUsernameTitle = std::make_shared<CLabel>( 10, 65, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, VLC->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(VLC->generaltexth->translate("vcmi.lobby.login.create"), EFonts::FONT_SMALL, Colors::YELLOW);
  47. buttonLogin->setTextOverlay(VLC->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. {
  61. toggleMode->setSelected(1);
  62. onLoginModeChanged(1);
  63. }
  64. filledBackground->setPlayerColor(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(VLC->generaltexth->translate("vcmi.lobby.login.connecting"));
  97. if(!CSH->getGlobalLobby().isConnected())
  98. CSH->getGlobalLobby().connect();
  99. else
  100. onConnectionSuccess();
  101. buttonClose->block(true);
  102. buttonLogin->block(true);
  103. }
  104. void GlobalLobbyLoginWindow::onConnectionSuccess()
  105. {
  106. std::string accountID = CSH->getGlobalLobby().getAccountID();
  107. if(toggleMode->getSelected() == 0)
  108. CSH->getGlobalLobby().sendClientRegister(inputUsername->getText());
  109. else
  110. CSH->getGlobalLobby().sendClientLogin();
  111. }
  112. void GlobalLobbyLoginWindow::onLoginSuccess()
  113. {
  114. close();
  115. CSH->getGlobalLobby().activateInterface();
  116. }
  117. void GlobalLobbyLoginWindow::onConnectionFailed(const std::string & reason)
  118. {
  119. MetaString formatter;
  120. formatter.appendTextID("vcmi.lobby.login.error");
  121. formatter.replaceRawString(reason);
  122. labelStatus->setText(formatter.toString());
  123. buttonClose->block(false);
  124. buttonLogin->block(false);
  125. }