CLobbyScreen.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "../widgets/Buttons.h"
  19. #include "../windows/InfoWindows.h"
  20. #include "../../CCallback.h"
  21. #include "../CGameInfo.h"
  22. #include "../../lib/NetPacksLobby.h"
  23. #include "../../lib/CGeneralTextHandler.h"
  24. #include "../../lib/mapping/CMapInfo.h"
  25. #include "../../lib/mapping/CCampaignHandler.h"
  26. #include "../../lib/rmg/CMapGenOptions.h"
  27. CLobbyScreen::CLobbyScreen(ESelectionScreen screenType)
  28. : CSelectionBase(screenType), bonusSel(nullptr)
  29. {
  30. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  31. tabSel = std::make_shared<SelectionTab>(screenType);
  32. curTab = tabSel;
  33. auto initLobby = [&]()
  34. {
  35. tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, CSH, _1, nullptr);
  36. buttonSelect = std::make_shared<CButton>(Point(411, 80), "GSPBUTT.DEF", CGI->generaltexth->zelp[45], 0, SDLK_s);
  37. buttonSelect->addCallback([&]()
  38. {
  39. toggleTab(tabSel);
  40. CSH->setMapInfo(tabSel->getSelectedMapInfo());
  41. });
  42. buttonOptions = std::make_shared<CButton>(Point(411, 510), "GSPBUTT.DEF", CGI->generaltexth->zelp[46], std::bind(&CLobbyScreen::toggleTab, this, tabOpt), SDLK_a);
  43. };
  44. buttonChat = std::make_shared<CButton>(Point(619, 83), "GSPBUT2.DEF", CGI->generaltexth->zelp[48], std::bind(&CLobbyScreen::toggleChat, this), SDLK_h);
  45. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL);
  46. switch(screenType)
  47. {
  48. case ESelectionScreen::newGame:
  49. {
  50. tabOpt = std::make_shared<OptionsTab>();
  51. tabRand = std::make_shared<RandomMapTab>();
  52. tabRand->mapInfoChanged += std::bind(&IServerAPI::setMapInfo, CSH, _1, _2);
  53. buttonRMG = std::make_shared<CButton>(Point(411, 105), "GSPBUTT.DEF", CGI->generaltexth->zelp[47], 0, SDLK_r);
  54. buttonRMG->addCallback([&]()
  55. {
  56. toggleTab(tabRand);
  57. tabRand->updateMapInfoByHost(); // TODO: This is only needed to force-update mapInfo in CSH when tab is opened
  58. });
  59. card->iconDifficulty->addCallback(std::bind(&IServerAPI::setDifficulty, CSH, _1));
  60. buttonStart = std::make_shared<CButton>(Point(411, 535), "SCNRBEG.DEF", CGI->generaltexth->zelp[103], std::bind(&CLobbyScreen::startScenario, this, true), SDLK_b);
  61. initLobby();
  62. break;
  63. }
  64. case ESelectionScreen::loadGame:
  65. {
  66. tabOpt = std::make_shared<OptionsTab>();
  67. buttonStart = std::make_shared<CButton>(Point(411, 535), "SCNRLOD.DEF", CGI->generaltexth->zelp[103], std::bind(&CLobbyScreen::startScenario, this, true), SDLK_l);
  68. initLobby();
  69. break;
  70. }
  71. case ESelectionScreen::campaignList:
  72. tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, CSH, _1, nullptr);
  73. buttonStart = std::make_shared<CButton>(Point(411, 535), "SCNRLOD.DEF", CButton::tooltip(), std::bind(&CLobbyScreen::startCampaign, this), SDLK_b);
  74. break;
  75. }
  76. buttonStart->assignedKeys.insert(SDLK_RETURN);
  77. buttonBack = std::make_shared<CButton>(Point(581, 535), "SCNRBACK.DEF", CGI->generaltexth->zelp[105], [&]()
  78. {
  79. CSH->sendClientDisconnecting();
  80. close();
  81. }, SDLK_ESCAPE);
  82. }
  83. CLobbyScreen::~CLobbyScreen()
  84. {
  85. // TODO: For now we always destroy whole lobby when leaving bonus selection screen
  86. if(CSH->state == EClientState::LOBBY_CAMPAIGN)
  87. CSH->sendClientDisconnecting();
  88. }
  89. void CLobbyScreen::toggleTab(std::shared_ptr<CIntObject> tab)
  90. {
  91. if(tab == curTab)
  92. CSH->sendGuiAction(LobbyGuiAction::NO_TAB);
  93. else if(tab == tabOpt)
  94. CSH->sendGuiAction(LobbyGuiAction::OPEN_OPTIONS);
  95. else if(tab == tabSel)
  96. CSH->sendGuiAction(LobbyGuiAction::OPEN_SCENARIO_LIST);
  97. else if(tab == tabRand)
  98. CSH->sendGuiAction(LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS);
  99. CSelectionBase::toggleTab(tab);
  100. }
  101. void CLobbyScreen::startCampaign()
  102. {
  103. if(CSH->mi)
  104. {
  105. auto ourCampaign = std::make_shared<CCampaignState>(CCampaignHandler::getCampaign(CSH->mi->fileURI));
  106. CSH->setCampaignState(ourCampaign);
  107. }
  108. }
  109. void CLobbyScreen::startScenario(bool allowOnlyAI)
  110. {
  111. try
  112. {
  113. CSH->sendStartGame(allowOnlyAI);
  114. buttonStart->block(true);
  115. }
  116. catch(CModHandler::Incompatibility & e)
  117. {
  118. logGlobal->warn("Incompatibility exception during start scenario: %s", e.what());
  119. auto errorMsg = VLC->generaltexth->translate("vcmi.server.errors.modsIncompatibility") + '\n';
  120. errorMsg += e.what();
  121. CInfoWindow::showInfoDialog(errorMsg, CInfoWindow::TCompsInfo(), PlayerColor(1));
  122. }
  123. catch(std::exception & e)
  124. {
  125. logGlobal->error("Exception during startScenario: %s", e.what());
  126. if(std::string(e.what()) == "ExceptionNoHuman")
  127. CInfoWindow::showInfoDialog(CGI->generaltexth->allTexts[530], CInfoWindow::TCompsInfo(), PlayerColor(1));
  128. if(std::string(e.what()) == "ExceptionNoTemplate")
  129. CInfoWindow::showInfoDialog(CGI->generaltexth->allTexts[751], CInfoWindow::TCompsInfo(), PlayerColor(1));
  130. }
  131. catch(...)
  132. {
  133. logGlobal->error("Unknown exception");
  134. }
  135. }
  136. void CLobbyScreen::toggleMode(bool host)
  137. {
  138. tabSel->toggleMode();
  139. buttonStart->block(!host);
  140. if(screenType == ESelectionScreen::campaignList)
  141. return;
  142. auto buttonColor = host ? Colors::WHITE : Colors::ORANGE;
  143. buttonSelect->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL, buttonColor);
  144. buttonOptions->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL, buttonColor);
  145. if(buttonRMG)
  146. {
  147. buttonRMG->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL, buttonColor);
  148. buttonRMG->block(!host);
  149. }
  150. buttonSelect->block(!host);
  151. buttonOptions->block(!host);
  152. if(CSH->mi)
  153. tabOpt->recreate();
  154. }
  155. void CLobbyScreen::toggleChat()
  156. {
  157. card->toggleChat();
  158. if(card->showChat)
  159. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL);
  160. else
  161. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL);
  162. }
  163. void CLobbyScreen::updateAfterStateChange()
  164. {
  165. if(CSH->mi && tabOpt)
  166. tabOpt->recreate();
  167. card->changeSelection();
  168. if (card->iconDifficulty)
  169. {
  170. if (screenType == ESelectionScreen::loadGame)
  171. {
  172. // 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
  173. card->iconDifficulty->setSelectedOnly(CSH->si->difficulty);
  174. }
  175. else
  176. {
  177. card->iconDifficulty->setSelected(CSH->si->difficulty);
  178. }
  179. }
  180. if(curTab && curTab == tabRand && CSH->si->mapGenOptions)
  181. tabRand->setMapGenOptions(CSH->si->mapGenOptions);
  182. }
  183. const StartInfo * CLobbyScreen::getStartInfo()
  184. {
  185. return CSH->si.get();
  186. }
  187. const CMapInfo * CLobbyScreen::getMapInfo()
  188. {
  189. return CSH->mi.get();
  190. }