CLobbyScreen.cpp 6.3 KB

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