CLobbyScreen.cpp 6.7 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 "../gui/Shortcut.h"
  19. #include "../widgets/Buttons.h"
  20. #include "../windows/InfoWindows.h"
  21. #include "../../CCallback.h"
  22. #include "../CGameInfo.h"
  23. #include "../../lib/CModHandler.h"
  24. #include "../../lib/NetPacksLobby.h"
  25. #include "../../lib/CGeneralTextHandler.h"
  26. #include "../../lib/mapping/CMapInfo.h"
  27. #include "../../lib/mapping/CCampaignHandler.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), "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), "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), "GSPBUT2.DEF", CGI->generaltexth->zelp[48], std::bind(&CLobbyScreen::toggleChat, this), EShortcut::LOBBY_HIDE_CHAT);
  47. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL);
  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), "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), "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), "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), "SCNRLOD.DEF", CButton::tooltip(), std::bind(&CLobbyScreen::startCampaign, this), EShortcut::LOBBY_BEGIN_GAME);
  76. break;
  77. }
  78. buttonBack = std::make_shared<CButton>(Point(581, 535), "SCNRBACK.DEF", CGI->generaltexth->zelp[105], [&]()
  79. {
  80. CSH->sendClientDisconnecting();
  81. close();
  82. }, EShortcut::GLOBAL_CANCEL);
  83. }
  84. CLobbyScreen::~CLobbyScreen()
  85. {
  86. // TODO: For now we always destroy whole lobby when leaving bonus selection screen
  87. if(CSH->state == EClientState::LOBBY_CAMPAIGN)
  88. CSH->sendClientDisconnecting();
  89. }
  90. void CLobbyScreen::toggleTab(std::shared_ptr<CIntObject> tab)
  91. {
  92. if(tab == curTab)
  93. CSH->sendGuiAction(LobbyGuiAction::NO_TAB);
  94. else if(tab == tabOpt)
  95. CSH->sendGuiAction(LobbyGuiAction::OPEN_OPTIONS);
  96. else if(tab == tabSel)
  97. CSH->sendGuiAction(LobbyGuiAction::OPEN_SCENARIO_LIST);
  98. else if(tab == tabRand)
  99. CSH->sendGuiAction(LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS);
  100. CSelectionBase::toggleTab(tab);
  101. }
  102. void CLobbyScreen::startCampaign()
  103. {
  104. if(CSH->mi)
  105. {
  106. auto ourCampaign = CampaignHandler::getCampaign(CSH->mi->fileURI);
  107. CSH->setCampaignState(ourCampaign);
  108. }
  109. }
  110. void CLobbyScreen::startScenario(bool allowOnlyAI)
  111. {
  112. try
  113. {
  114. CSH->sendStartGame(allowOnlyAI);
  115. buttonStart->block(true);
  116. }
  117. catch(CModHandler::Incompatibility & e)
  118. {
  119. logGlobal->warn("Incompatibility exception during start scenario: %s", e.what());
  120. auto errorMsg = CGI->generaltexth->translate("vcmi.server.errors.modsIncompatibility") + '\n';
  121. errorMsg += e.what();
  122. CInfoWindow::showInfoDialog(errorMsg, CInfoWindow::TCompsInfo(), PlayerColor(1));
  123. }
  124. catch(std::exception & e)
  125. {
  126. logGlobal->error("Exception during startScenario: %s", e.what());
  127. if(std::string(e.what()) == "ExceptionNoHuman")
  128. CInfoWindow::showInfoDialog(CGI->generaltexth->allTexts[530], CInfoWindow::TCompsInfo(), PlayerColor(1));
  129. if(std::string(e.what()) == "ExceptionNoTemplate")
  130. CInfoWindow::showInfoDialog(CGI->generaltexth->allTexts[751], CInfoWindow::TCompsInfo(), PlayerColor(1));
  131. }
  132. catch(...)
  133. {
  134. logGlobal->error("Unknown exception");
  135. }
  136. }
  137. void CLobbyScreen::toggleMode(bool host)
  138. {
  139. tabSel->toggleMode();
  140. buttonStart->block(!host);
  141. if(screenType == ESelectionScreen::campaignList)
  142. return;
  143. auto buttonColor = host ? Colors::WHITE : Colors::ORANGE;
  144. buttonSelect->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL, buttonColor);
  145. buttonOptions->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL, buttonColor);
  146. if(buttonRMG)
  147. {
  148. buttonRMG->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL, buttonColor);
  149. buttonRMG->block(!host);
  150. }
  151. buttonSelect->block(!host);
  152. buttonOptions->block(!host);
  153. if(CSH->mi)
  154. tabOpt->recreate();
  155. }
  156. void CLobbyScreen::toggleChat()
  157. {
  158. card->toggleChat();
  159. if(card->showChat)
  160. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL);
  161. else
  162. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL);
  163. }
  164. void CLobbyScreen::updateAfterStateChange()
  165. {
  166. if(CSH->mi && tabOpt)
  167. tabOpt->recreate();
  168. card->changeSelection();
  169. if (card->iconDifficulty)
  170. {
  171. if (screenType == ESelectionScreen::loadGame)
  172. {
  173. // 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
  174. card->iconDifficulty->setSelectedOnly(CSH->si->difficulty);
  175. }
  176. else
  177. {
  178. card->iconDifficulty->setSelected(CSH->si->difficulty);
  179. }
  180. }
  181. if(curTab && curTab == tabRand && CSH->si->mapGenOptions)
  182. tabRand->setMapGenOptions(CSH->si->mapGenOptions);
  183. }
  184. const StartInfo * CLobbyScreen::getStartInfo()
  185. {
  186. return CSH->si.get();
  187. }
  188. const CMapInfo * CLobbyScreen::getMapInfo()
  189. {
  190. return CSH->mi.get();
  191. }