GlobalLobbyLoginWindow.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "../GameInstance.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/texts/CGeneralTextHandler.h"
  25. #include "../../lib/texts/MetaString.h"
  26. #include "../../lib/VCMI_Lib.h"
  27. GlobalLobbyLoginWindow::GlobalLobbyLoginWindow()
  28. : CWindowObject(BORDERED)
  29. {
  30. OBJECT_CONSTRUCTION;
  31. pos.w = 284;
  32. pos.h = 220;
  33. MetaString loginAs;
  34. loginAs.appendTextID("vcmi.lobby.login.as");
  35. loginAs.replaceRawString(GAME->server().getGlobalLobby().getAccountDisplayName());
  36. filledBackground = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
  37. labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, VLC->generaltexth->translate("vcmi.lobby.login.title"));
  38. labelUsernameTitle = std::make_shared<CLabel>( 10, 65, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, VLC->generaltexth->translate("vcmi.lobby.login.username"));
  39. labelUsername = std::make_shared<CLabel>( 10, 65, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, loginAs.toString(), 265);
  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, ETextAlignment::CENTERLEFT, true);
  42. buttonLogin = std::make_shared<CButton>(Point(10, 180), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onLogin(); }, EShortcut::GLOBAL_ACCEPT);
  43. buttonClose = std::make_shared<CButton>(Point(210, 180), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); }, EShortcut::GLOBAL_CANCEL);
  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(VLC->generaltexth->translate("vcmi.lobby.login.create"), EFonts::FONT_SMALL, Colors::YELLOW);
  48. buttonLogin->setTextOverlay(VLC->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 (GAME->server().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 currently selected option
  59. }
  60. else
  61. {
  62. toggleMode->setSelected(1);
  63. onLoginModeChanged(1);
  64. }
  65. filledBackground->setPlayerColor(PlayerColor(1));
  66. inputUsername->setCallback([this](const std::string & text)
  67. {
  68. this->buttonLogin->block(text.empty());
  69. });
  70. center();
  71. }
  72. void GlobalLobbyLoginWindow::onLoginModeChanged(int value)
  73. {
  74. if (value == 0)
  75. {
  76. inputUsername->enable();
  77. backgroundUsername->enable();
  78. labelUsernameTitle->enable();
  79. labelUsername->disable();
  80. }
  81. else
  82. {
  83. inputUsername->disable();
  84. backgroundUsername->disable();
  85. labelUsernameTitle->disable();
  86. labelUsername->enable();
  87. }
  88. redraw();
  89. }
  90. void GlobalLobbyLoginWindow::onClose()
  91. {
  92. close();
  93. // TODO: abort ongoing connection attempt, if any
  94. }
  95. void GlobalLobbyLoginWindow::onLogin()
  96. {
  97. labelStatus->setText(VLC->generaltexth->translate("vcmi.lobby.login.connecting"));
  98. if(!GAME->server().getGlobalLobby().isConnected())
  99. GAME->server().getGlobalLobby().connect();
  100. else
  101. onConnectionSuccess();
  102. buttonClose->block(true);
  103. buttonLogin->block(true);
  104. }
  105. void GlobalLobbyLoginWindow::onConnectionSuccess()
  106. {
  107. std::string accountID = GAME->server().getGlobalLobby().getAccountID();
  108. if(toggleMode->getSelected() == 0)
  109. GAME->server().getGlobalLobby().sendClientRegister(inputUsername->getText());
  110. else
  111. GAME->server().getGlobalLobby().sendClientLogin();
  112. }
  113. void GlobalLobbyLoginWindow::onLoginSuccess()
  114. {
  115. close();
  116. GAME->server().getGlobalLobby().activateInterface();
  117. }
  118. void GlobalLobbyLoginWindow::onConnectionFailed(const std::string & reason)
  119. {
  120. MetaString formatter;
  121. formatter.appendTextID("vcmi.lobby.login.error");
  122. formatter.replaceRawString(reason);
  123. labelStatus->setText(formatter.toString());
  124. buttonClose->block(false);
  125. buttonLogin->block(false);
  126. }