GlobalLobbyServerSetup.cpp 6.6 KB

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