CLobbyScreen.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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], [&](){CSH->sendClientDisconnecting(); GH.popIntTotally(this);}, SDLK_ESCAPE);
  78. }
  79. CLobbyScreen::~CLobbyScreen()
  80. {
  81. // TODO: For now we always destroy whole lobby when leaving bonus selection screen
  82. if(CSH->state == EClientState::LOBBY_CAMPAIGN)
  83. CSH->sendClientDisconnecting();
  84. }
  85. void CLobbyScreen::toggleTab(std::shared_ptr<CIntObject> tab)
  86. {
  87. if(tab == curTab)
  88. CSH->sendGuiAction(LobbyGuiAction::NO_TAB);
  89. else if(tab == tabOpt)
  90. CSH->sendGuiAction(LobbyGuiAction::OPEN_OPTIONS);
  91. else if(tab == tabSel)
  92. CSH->sendGuiAction(LobbyGuiAction::OPEN_SCENARIO_LIST);
  93. else if(tab == tabRand)
  94. CSH->sendGuiAction(LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS);
  95. CSelectionBase::toggleTab(tab);
  96. }
  97. void CLobbyScreen::startCampaign()
  98. {
  99. if(CSH->mi)
  100. {
  101. auto ourCampaign = std::make_shared<CCampaignState>(CCampaignHandler::getCampaign(CSH->mi->fileURI));
  102. CSH->setCampaignState(ourCampaign);
  103. }
  104. }
  105. void CLobbyScreen::startScenario(bool allowOnlyAI)
  106. {
  107. try
  108. {
  109. CSH->sendStartGame(allowOnlyAI);
  110. buttonStart->block(true);
  111. }
  112. catch(ExceptionMapMissing & e)
  113. {
  114. }
  115. catch(ExceptionNoHuman & e)
  116. {
  117. // You must position yourself prior to starting the game.
  118. CInfoWindow::showYesNoDialog(std::ref(CGI->generaltexth->allTexts[530]), CInfoWindow::TCompsInfo(), 0, std::bind(&CLobbyScreen::startScenario, this, true), PlayerColor(1));
  119. }
  120. catch(ExceptionNoTemplate & e)
  121. {
  122. GH.pushInt(CInfoWindow::create(CGI->generaltexth->allTexts[751]));
  123. }
  124. catch(...)
  125. {
  126. }
  127. }
  128. void CLobbyScreen::toggleMode(bool host)
  129. {
  130. tabSel->toggleMode();
  131. buttonStart->block(!host);
  132. if(screenType == ESelectionScreen::campaignList)
  133. return;
  134. auto buttonColor = host ? Colors::WHITE : Colors::ORANGE;
  135. buttonSelect->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL, buttonColor);
  136. buttonOptions->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL, buttonColor);
  137. if(buttonRMG)
  138. {
  139. buttonRMG->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL, buttonColor);
  140. buttonRMG->block(!host);
  141. }
  142. buttonSelect->block(!host);
  143. buttonOptions->block(!host);
  144. if(CSH->mi)
  145. tabOpt->recreate();
  146. }
  147. void CLobbyScreen::toggleChat()
  148. {
  149. card->toggleChat();
  150. if(card->showChat)
  151. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL);
  152. else
  153. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL);
  154. }
  155. void CLobbyScreen::updateAfterStateChange()
  156. {
  157. if(CSH->mi && tabOpt)
  158. tabOpt->recreate();
  159. card->changeSelection();
  160. if(card->iconDifficulty)
  161. card->iconDifficulty->setSelected(CSH->si->difficulty);
  162. if(curTab == tabRand && CSH->si->mapGenOptions)
  163. tabRand->setMapGenOptions(CSH->si->mapGenOptions);
  164. }
  165. const StartInfo * CLobbyScreen::getStartInfo()
  166. {
  167. return CSH->si.get();
  168. }
  169. const CMapInfo * CLobbyScreen::getMapInfo()
  170. {
  171. return CSH->mi.get();
  172. }