CLobbyScreen.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/CModHandler.h"
  25. #include "../../lib/NetPacksLobby.h"
  26. #include "../../lib/CGeneralTextHandler.h"
  27. #include "../../lib/campaign/CampaignHandler.h"
  28. #include "../../lib/mapping/CMapInfo.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), "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), "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), "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), "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), "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), "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), "SCNRLOD.DEF", CButton::tooltip(), std::bind(&CLobbyScreen::startCampaign, this), EShortcut::LOBBY_BEGIN_GAME);
  77. break;
  78. }
  79. buttonBack = std::make_shared<CButton>(Point(581, 535), "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. try
  114. {
  115. CSH->sendStartGame(allowOnlyAI);
  116. buttonStart->block(true);
  117. }
  118. catch(CModHandler::Incompatibility & e)
  119. {
  120. logGlobal->warn("Incompatibility exception during start scenario: %s", e.what());
  121. auto errorMsg = CGI->generaltexth->translate("vcmi.server.errors.modsIncompatibility") + '\n';
  122. errorMsg += e.what();
  123. CInfoWindow::showInfoDialog(errorMsg, CInfoWindow::TCompsInfo(), PlayerColor(1));
  124. }
  125. catch(std::exception & e)
  126. {
  127. logGlobal->error("Exception during startScenario: %s", e.what());
  128. if(std::string(e.what()) == "ExceptionNoHuman")
  129. CInfoWindow::showInfoDialog(CGI->generaltexth->allTexts[530], CInfoWindow::TCompsInfo(), PlayerColor(1));
  130. if(std::string(e.what()) == "ExceptionNoTemplate")
  131. CInfoWindow::showInfoDialog(CGI->generaltexth->allTexts[751], CInfoWindow::TCompsInfo(), PlayerColor(1));
  132. }
  133. catch(...)
  134. {
  135. logGlobal->error("Unknown exception");
  136. }
  137. }
  138. void CLobbyScreen::toggleMode(bool host)
  139. {
  140. tabSel->toggleMode();
  141. buttonStart->block(!host);
  142. if(screenType == ESelectionScreen::campaignList)
  143. return;
  144. auto buttonColor = host ? Colors::WHITE : Colors::ORANGE;
  145. buttonSelect->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL, buttonColor);
  146. buttonOptions->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL, buttonColor);
  147. if(buttonRMG)
  148. {
  149. buttonRMG->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL, buttonColor);
  150. buttonRMG->block(!host);
  151. }
  152. buttonSelect->block(!host);
  153. buttonOptions->block(!host);
  154. if(CSH->mi)
  155. tabOpt->recreate();
  156. }
  157. void CLobbyScreen::toggleChat()
  158. {
  159. card->toggleChat();
  160. if(card->showChat)
  161. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL, Colors::WHITE);
  162. else
  163. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL, Colors::WHITE);
  164. }
  165. void CLobbyScreen::updateAfterStateChange()
  166. {
  167. if(CSH->mi && tabOpt)
  168. tabOpt->recreate();
  169. card->changeSelection();
  170. if (card->iconDifficulty)
  171. {
  172. if (screenType == ESelectionScreen::loadGame)
  173. {
  174. // 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
  175. card->iconDifficulty->setSelectedOnly(CSH->si->difficulty);
  176. }
  177. else
  178. {
  179. card->iconDifficulty->setSelected(CSH->si->difficulty);
  180. }
  181. }
  182. if(curTab && curTab == tabRand && CSH->si->mapGenOptions)
  183. tabRand->setMapGenOptions(CSH->si->mapGenOptions);
  184. }
  185. const StartInfo * CLobbyScreen::getStartInfo()
  186. {
  187. return CSH->si.get();
  188. }
  189. const CMapInfo * CLobbyScreen::getMapInfo()
  190. {
  191. return CSH->mi.get();
  192. }