CLobbyScreen.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * CLobbyScreen.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 "CLobbyScreen.h"
  12. #include "CBonusSelection.h"
  13. #include "SelectionTab.h"
  14. #include "RandomMapTab.h"
  15. #include "OptionsTab.h"
  16. #include "../CServerHandler.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/Shortcut.h"
  19. #include "../widgets/Buttons.h"
  20. #include "../windows/InfoWindows.h"
  21. #include "../render/Colors.h"
  22. #include "../../CCallback.h"
  23. #include "../CGameInfo.h"
  24. #include "../../lib/NetPacksLobby.h"
  25. #include "../../lib/CGeneralTextHandler.h"
  26. #include "../../lib/campaign/CampaignHandler.h"
  27. #include "../../lib/mapping/CMapInfo.h"
  28. #include "../../lib/modding/ModIncompatibility.h"
  29. #include "../../lib/rmg/CMapGenOptions.h"
  30. CLobbyScreen::CLobbyScreen(ESelectionScreen screenType)
  31. : CSelectionBase(screenType), bonusSel(nullptr)
  32. {
  33. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  34. tabSel = std::make_shared<SelectionTab>(screenType);
  35. curTab = tabSel;
  36. auto initLobby = [&]()
  37. {
  38. tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, CSH, _1, nullptr);
  39. buttonSelect = std::make_shared<CButton>(Point(411, 80), AnimationPath::builtin("GSPBUTT.DEF"), CGI->generaltexth->zelp[45], 0, EShortcut::LOBBY_SELECT_SCENARIO);
  40. buttonSelect->addCallback([&]()
  41. {
  42. toggleTab(tabSel);
  43. CSH->setMapInfo(tabSel->getSelectedMapInfo());
  44. });
  45. buttonOptions = std::make_shared<CButton>(Point(411, 510), AnimationPath::builtin("GSPBUTT.DEF"), CGI->generaltexth->zelp[46], std::bind(&CLobbyScreen::toggleTab, this, tabOpt), EShortcut::LOBBY_ADDITIONAL_OPTIONS);
  46. };
  47. buttonChat = std::make_shared<CButton>(Point(619, 80), AnimationPath::builtin("GSPBUT2.DEF"), CGI->generaltexth->zelp[48], std::bind(&CLobbyScreen::toggleChat, this), EShortcut::LOBBY_HIDE_CHAT);
  48. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL, Colors::WHITE);
  49. switch(screenType)
  50. {
  51. case ESelectionScreen::newGame:
  52. {
  53. tabOpt = std::make_shared<OptionsTab>();
  54. tabRand = std::make_shared<RandomMapTab>();
  55. tabRand->mapInfoChanged += std::bind(&IServerAPI::setMapInfo, CSH, _1, _2);
  56. buttonRMG = std::make_shared<CButton>(Point(411, 105), AnimationPath::builtin("GSPBUTT.DEF"), CGI->generaltexth->zelp[47], 0, EShortcut::LOBBY_RANDOM_MAP);
  57. buttonRMG->addCallback([&]()
  58. {
  59. toggleTab(tabRand);
  60. tabRand->updateMapInfoByHost(); // TODO: This is only needed to force-update mapInfo in CSH when tab is opened
  61. });
  62. card->iconDifficulty->addCallback(std::bind(&IServerAPI::setDifficulty, CSH, _1));
  63. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRBEG.DEF"), CGI->generaltexth->zelp[103], std::bind(&CLobbyScreen::startScenario, this, true), EShortcut::LOBBY_BEGIN_GAME);
  64. initLobby();
  65. break;
  66. }
  67. case ESelectionScreen::loadGame:
  68. {
  69. tabOpt = std::make_shared<OptionsTab>();
  70. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRLOD.DEF"), CGI->generaltexth->zelp[103], std::bind(&CLobbyScreen::startScenario, this, true), EShortcut::LOBBY_LOAD_GAME);
  71. initLobby();
  72. break;
  73. }
  74. case ESelectionScreen::campaignList:
  75. tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, CSH, _1, nullptr);
  76. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRLOD.DEF"), CButton::tooltip(), std::bind(&CLobbyScreen::startCampaign, this), EShortcut::LOBBY_BEGIN_GAME);
  77. break;
  78. }
  79. buttonStart->block(true); // to be unblocked after map list is ready
  80. buttonBack = std::make_shared<CButton>(Point(581, 535), AnimationPath::builtin("SCNRBACK.DEF"), CGI->generaltexth->zelp[105], [&]()
  81. {
  82. CSH->sendClientDisconnecting();
  83. close();
  84. }, EShortcut::GLOBAL_CANCEL);
  85. }
  86. CLobbyScreen::~CLobbyScreen()
  87. {
  88. // TODO: For now we always destroy whole lobby when leaving bonus selection screen
  89. if(CSH->state == EClientState::LOBBY_CAMPAIGN)
  90. CSH->sendClientDisconnecting();
  91. }
  92. void CLobbyScreen::toggleTab(std::shared_ptr<CIntObject> tab)
  93. {
  94. if(tab == curTab)
  95. CSH->sendGuiAction(LobbyGuiAction::NO_TAB);
  96. else if(tab == tabOpt)
  97. CSH->sendGuiAction(LobbyGuiAction::OPEN_OPTIONS);
  98. else if(tab == tabSel)
  99. CSH->sendGuiAction(LobbyGuiAction::OPEN_SCENARIO_LIST);
  100. else if(tab == tabRand)
  101. CSH->sendGuiAction(LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS);
  102. CSelectionBase::toggleTab(tab);
  103. }
  104. void CLobbyScreen::startCampaign()
  105. {
  106. if(CSH->mi)
  107. {
  108. auto ourCampaign = CampaignHandler::getCampaign(CSH->mi->fileURI);
  109. CSH->setCampaignState(ourCampaign);
  110. }
  111. }
  112. void CLobbyScreen::startScenario(bool allowOnlyAI)
  113. {
  114. if (CSH->validateGameStart(allowOnlyAI))
  115. {
  116. CSH->sendStartGame(allowOnlyAI);
  117. buttonStart->block(true);
  118. }
  119. }
  120. void CLobbyScreen::toggleMode(bool host)
  121. {
  122. tabSel->toggleMode();
  123. buttonStart->block(!host);
  124. if(screenType == ESelectionScreen::campaignList)
  125. return;
  126. auto buttonColor = host ? Colors::WHITE : Colors::ORANGE;
  127. buttonSelect->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL, buttonColor);
  128. buttonOptions->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL, buttonColor);
  129. if(buttonRMG)
  130. {
  131. buttonRMG->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL, buttonColor);
  132. buttonRMG->block(!host);
  133. }
  134. buttonSelect->block(!host);
  135. buttonOptions->block(!host);
  136. if(CSH->mi)
  137. tabOpt->recreate();
  138. }
  139. void CLobbyScreen::toggleChat()
  140. {
  141. card->toggleChat();
  142. if(card->showChat)
  143. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL, Colors::WHITE);
  144. else
  145. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL, Colors::WHITE);
  146. }
  147. void CLobbyScreen::updateAfterStateChange()
  148. {
  149. if(CSH->mi && tabOpt)
  150. tabOpt->recreate();
  151. buttonStart->block(CSH->mi == nullptr || CSH->isGuest());
  152. card->changeSelection();
  153. if (card->iconDifficulty)
  154. {
  155. if (screenType == ESelectionScreen::loadGame)
  156. {
  157. // When loading the game, only one button in the difficulty toggle group should be enabled, so here disable all other buttons first, then make selection
  158. card->iconDifficulty->setSelectedOnly(CSH->si->difficulty);
  159. }
  160. else
  161. {
  162. card->iconDifficulty->setSelected(CSH->si->difficulty);
  163. }
  164. }
  165. if(curTab && curTab == tabRand && CSH->si->mapGenOptions)
  166. tabRand->setMapGenOptions(CSH->si->mapGenOptions);
  167. }
  168. const StartInfo * CLobbyScreen::getStartInfo()
  169. {
  170. return CSH->si.get();
  171. }
  172. const CMapInfo * CLobbyScreen::getMapInfo()
  173. {
  174. return CSH->mi.get();
  175. }