CLobbyScreen.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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, false), 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, false), 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(ExceptionMapMissing & e)
  117. {
  118. }
  119. catch(ExceptionNoHuman & e)
  120. {
  121. // You must position yourself prior to starting the game.
  122. CInfoWindow::showYesNoDialog(std::ref(CGI->generaltexth->allTexts[530]), CInfoWindow::TCompsInfo(), 0, std::bind(&CLobbyScreen::startScenario, this, true), PlayerColor(1));
  123. }
  124. catch(ExceptionNoTemplate & e)
  125. {
  126. CInfoWindow::showInfoDialog(std::ref(CGI->generaltexth->allTexts[751]), CInfoWindow::TCompsInfo(), PlayerColor(1));
  127. }
  128. catch(...)
  129. {
  130. }
  131. }
  132. void CLobbyScreen::toggleMode(bool host)
  133. {
  134. tabSel->toggleMode();
  135. buttonStart->block(!host);
  136. if(screenType == ESelectionScreen::campaignList)
  137. return;
  138. auto buttonColor = host ? Colors::WHITE : Colors::ORANGE;
  139. buttonSelect->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL, buttonColor);
  140. buttonOptions->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL, buttonColor);
  141. if(buttonRMG)
  142. {
  143. buttonRMG->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL, buttonColor);
  144. buttonRMG->block(!host);
  145. }
  146. buttonSelect->block(!host);
  147. buttonOptions->block(!host);
  148. if(CSH->mi)
  149. tabOpt->recreate();
  150. }
  151. void CLobbyScreen::toggleChat()
  152. {
  153. card->toggleChat();
  154. if(card->showChat)
  155. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL);
  156. else
  157. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL);
  158. }
  159. void CLobbyScreen::updateAfterStateChange()
  160. {
  161. if(CSH->mi && tabOpt)
  162. tabOpt->recreate();
  163. card->changeSelection();
  164. if(card->iconDifficulty)
  165. card->iconDifficulty->setSelected(CSH->si->difficulty);
  166. if(curTab == tabRand && CSH->si->mapGenOptions)
  167. tabRand->setMapGenOptions(CSH->si->mapGenOptions);
  168. }
  169. const StartInfo * CLobbyScreen::getStartInfo()
  170. {
  171. return CSH->si.get();
  172. }
  173. const CMapInfo * CLobbyScreen::getMapInfo()
  174. {
  175. return CSH->mi.get();
  176. }