GlobalLobbyServerSetup.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * GlobalLobbyServerSetup.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 "GlobalLobbyServerSetup.h"
  12. #include "GlobalLobbyClient.h"
  13. #include "../CServerHandler.h"
  14. #include "../GameEngine.h"
  15. #include "../GameInstance.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../mainmenu/CMainMenu.h"
  18. #include "../widgets/Buttons.h"
  19. #include "../widgets/Images.h"
  20. #include "../widgets/TextControls.h"
  21. #include "../../lib/CConfigHandler.h"
  22. #include "../../lib/texts/CGeneralTextHandler.h"
  23. #include "../../lib/texts/MetaString.h"
  24. #include "../../lib/GameLibrary.h"
  25. GlobalLobbyServerSetup::GlobalLobbyServerSetup()
  26. : CWindowObject(BORDERED)
  27. {
  28. OBJECT_CONSTRUCTION;
  29. pos.w = 284;
  30. pos.h = 340;
  31. filledBackground = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
  32. labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->translate("vcmi.lobby.room.create.hover"));
  33. labelPlayerLimit = std::make_shared<CLabel>( pos.w / 2, 48, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->translate("vcmi.lobby.room.players.limit"));
  34. labelRoomType = std::make_shared<CLabel>( pos.w / 2, 108, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->translate("vcmi.lobby.room.type"));
  35. labelGameMode = std::make_shared<CLabel>( pos.w / 2, 158, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->translate("vcmi.lobby.room.mode"));
  36. togglePlayerLimit = std::make_shared<CToggleGroup>(nullptr);
  37. togglePlayerLimit->addToggle(2, std::make_shared<CToggleButton>(Point(10 + 39*0, 60), AnimationPath::builtin("RanNum2"), CButton::tooltip(), 0));
  38. togglePlayerLimit->addToggle(3, std::make_shared<CToggleButton>(Point(10 + 39*1, 60), AnimationPath::builtin("RanNum3"), CButton::tooltip(), 0));
  39. togglePlayerLimit->addToggle(4, std::make_shared<CToggleButton>(Point(10 + 39*2, 60), AnimationPath::builtin("RanNum4"), CButton::tooltip(), 0));
  40. togglePlayerLimit->addToggle(5, std::make_shared<CToggleButton>(Point(10 + 39*3, 60), AnimationPath::builtin("RanNum5"), CButton::tooltip(), 0));
  41. togglePlayerLimit->addToggle(6, std::make_shared<CToggleButton>(Point(10 + 39*4, 60), AnimationPath::builtin("RanNum6"), CButton::tooltip(), 0));
  42. togglePlayerLimit->addToggle(7, std::make_shared<CToggleButton>(Point(10 + 39*5, 60), AnimationPath::builtin("RanNum7"), CButton::tooltip(), 0));
  43. togglePlayerLimit->addToggle(8, std::make_shared<CToggleButton>(Point(10 + 39*6, 60), AnimationPath::builtin("RanNum8"), CButton::tooltip(), 0));
  44. togglePlayerLimit->setSelected(settings["lobby"]["roomPlayerLimit"].Integer());
  45. togglePlayerLimit->addCallback([this](int index){onPlayerLimitChanged(index);});
  46. auto buttonPublic = std::make_shared<CToggleButton>(Point(10, 120), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), 0);
  47. auto buttonPrivate = std::make_shared<CToggleButton>(Point(146, 120), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), 0);
  48. buttonPublic->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.room.state.public"), EFonts::FONT_SMALL, Colors::YELLOW);
  49. buttonPrivate->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.room.state.private"), EFonts::FONT_SMALL, Colors::YELLOW);
  50. toggleRoomType = std::make_shared<CToggleGroup>(nullptr);
  51. toggleRoomType->addToggle(0, buttonPublic);
  52. toggleRoomType->addToggle(1, buttonPrivate);
  53. toggleRoomType->setSelected(settings["lobby"]["roomType"].Integer());
  54. toggleRoomType->addCallback([this](int index){onRoomTypeChanged(index);});
  55. auto buttonNewGame = std::make_shared<CToggleButton>(Point(10, 170), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), 0);
  56. auto buttonLoadGame = std::make_shared<CToggleButton>(Point(146, 170), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), 0);
  57. buttonNewGame->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.room.new"), EFonts::FONT_SMALL, Colors::YELLOW);
  58. buttonLoadGame->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.room.load"), EFonts::FONT_SMALL, Colors::YELLOW);
  59. toggleGameMode = std::make_shared<CToggleGroup>(nullptr);
  60. toggleGameMode->addToggle(0, buttonNewGame);
  61. toggleGameMode->addToggle(1, buttonLoadGame);
  62. toggleGameMode->setSelected(settings["lobby"]["roomMode"].Integer());
  63. toggleGameMode->addCallback([this](int index){onGameModeChanged(index);});
  64. labelDescription = std::make_shared<CTextBox>("", Rect(10, 195, pos.w - 20, 80), 1, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  65. buttonCreate = std::make_shared<CButton>(Point(10, 300), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onCreate(); }, EShortcut::GLOBAL_ACCEPT);
  66. buttonClose = std::make_shared<CButton>(Point(210, 300), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); }, EShortcut::GLOBAL_CANCEL);
  67. filledBackground->setPlayerColor(PlayerColor(1));
  68. updateDescription();
  69. center();
  70. }
  71. void GlobalLobbyServerSetup::updateDescription()
  72. {
  73. MetaString description;
  74. description.appendRawString("%s %s %s");
  75. if(toggleRoomType->getSelected() == 0)
  76. description.replaceTextID("vcmi.lobby.room.description.public");
  77. else
  78. description.replaceTextID("vcmi.lobby.room.description.private");
  79. if(toggleGameMode->getSelected() == 0)
  80. description.replaceTextID("vcmi.lobby.room.description.new");
  81. else
  82. description.replaceTextID("vcmi.lobby.room.description.load");
  83. description.replaceTextID("vcmi.lobby.room.description.limit");
  84. description.replaceNumber(togglePlayerLimit->getSelected());
  85. labelDescription->setText(description.toString());
  86. }
  87. void GlobalLobbyServerSetup::onPlayerLimitChanged(int value)
  88. {
  89. Settings config = settings.write["lobby"]["roomPlayerLimit"];
  90. config->Integer() = value;
  91. updateDescription();
  92. }
  93. void GlobalLobbyServerSetup::onRoomTypeChanged(int value)
  94. {
  95. Settings config = settings.write["lobby"]["roomType"];
  96. config->Integer() = value;
  97. updateDescription();
  98. }
  99. void GlobalLobbyServerSetup::onGameModeChanged(int value)
  100. {
  101. Settings config = settings.write["lobby"]["roomMode"];
  102. config->Integer() = value;
  103. updateDescription();
  104. }
  105. void GlobalLobbyServerSetup::onCreate()
  106. {
  107. if(toggleGameMode->getSelected() == 0)
  108. GAME->server().resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOBBY_HOST, { GAME->server().getGlobalLobby().getAccountDisplayName() });
  109. else
  110. GAME->server().resetStateForLobby(EStartMode::LOAD_GAME, ESelectionScreen::loadGame, EServerMode::LOBBY_HOST, { GAME->server().getGlobalLobby().getAccountDisplayName() });
  111. GAME->server().loadMode = ELoadMode::MULTI;
  112. GAME->server().startLocalServerAndConnect(true);
  113. buttonCreate->block(true);
  114. buttonClose->block(true);
  115. }
  116. void GlobalLobbyServerSetup::onClose()
  117. {
  118. close();
  119. }