CLobbyScreen.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "TurnOptionsTab.h"
  14. #include "OptionsTab.h"
  15. #include "RandomMapTab.h"
  16. #include "SelectionTab.h"
  17. #include "../CServerHandler.h"
  18. #include "../gui/CGuiHandler.h"
  19. #include "../gui/Shortcut.h"
  20. #include "../widgets/Buttons.h"
  21. #include "../windows/InfoWindows.h"
  22. #include "../render/Colors.h"
  23. #include "../../CCallback.h"
  24. #include "../../lib/CConfigHandler.h"
  25. #include "../../lib/CGeneralTextHandler.h"
  26. #include "../../lib/campaign/CampaignHandler.h"
  27. #include "../../lib/mapping/CMapInfo.h"
  28. #include "../../lib/networkPacks/PacksForLobby.h"
  29. #include "../../lib/rmg/CMapGenOptions.h"
  30. #include "../CGameInfo.h"
  31. CLobbyScreen::CLobbyScreen(ESelectionScreen screenType)
  32. : CSelectionBase(screenType), bonusSel(nullptr)
  33. {
  34. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  35. tabSel = std::make_shared<SelectionTab>(screenType);
  36. curTab = tabSel;
  37. auto initLobby = [&]()
  38. {
  39. tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, CSH, _1, nullptr);
  40. buttonSelect = std::make_shared<CButton>(Point(411, 80), AnimationPath::builtin("GSPBUTT.DEF"), CGI->generaltexth->zelp[45], 0, EShortcut::LOBBY_SELECT_SCENARIO);
  41. buttonSelect->addCallback([&]()
  42. {
  43. toggleTab(tabSel);
  44. CSH->setMapInfo(tabSel->getSelectedMapInfo());
  45. });
  46. buttonOptions = std::make_shared<CButton>(Point(411, 510), AnimationPath::builtin("GSPBUTT.DEF"), CGI->generaltexth->zelp[46], std::bind(&CLobbyScreen::toggleTab, this, tabOpt), EShortcut::LOBBY_ADDITIONAL_OPTIONS);
  47. if(settings["general"]["enableUiEnhancements"].Bool())
  48. buttonTurnOptions = std::make_shared<CButton>(Point(619, 510), AnimationPath::builtin("GSPBUT2.DEF"), CGI->generaltexth->zelp[46], std::bind(&CLobbyScreen::toggleTab, this, tabTurnOptions), EShortcut::NONE);
  49. };
  50. buttonChat = std::make_shared<CButton>(Point(619, 80), AnimationPath::builtin("GSPBUT2.DEF"), CGI->generaltexth->zelp[48], std::bind(&CLobbyScreen::toggleChat, this), EShortcut::LOBBY_HIDE_CHAT);
  51. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL, Colors::WHITE);
  52. switch(screenType)
  53. {
  54. case ESelectionScreen::newGame:
  55. {
  56. tabOpt = std::make_shared<OptionsTab>();
  57. tabTurnOptions = std::make_shared<TurnOptionsTab>();
  58. tabRand = std::make_shared<RandomMapTab>();
  59. tabRand->mapInfoChanged += std::bind(&IServerAPI::setMapInfo, CSH, _1, _2);
  60. buttonRMG = std::make_shared<CButton>(Point(411, 105), AnimationPath::builtin("GSPBUTT.DEF"), CGI->generaltexth->zelp[47], 0, EShortcut::LOBBY_RANDOM_MAP);
  61. buttonRMG->addCallback([&]()
  62. {
  63. toggleTab(tabRand);
  64. tabRand->updateMapInfoByHost(); // TODO: This is only needed to force-update mapInfo in CSH when tab is opened
  65. });
  66. card->iconDifficulty->addCallback(std::bind(&IServerAPI::setDifficulty, CSH, _1));
  67. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRBEG.DEF"), CGI->generaltexth->zelp[103], std::bind(&CLobbyScreen::startScenario, this, false), EShortcut::LOBBY_BEGIN_GAME);
  68. initLobby();
  69. break;
  70. }
  71. case ESelectionScreen::loadGame:
  72. {
  73. tabOpt = std::make_shared<OptionsTab>();
  74. tabTurnOptions = std::make_shared<TurnOptionsTab>();
  75. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRLOD.DEF"), CGI->generaltexth->zelp[103], std::bind(&CLobbyScreen::startScenario, this, false), EShortcut::LOBBY_LOAD_GAME);
  76. initLobby();
  77. break;
  78. }
  79. case ESelectionScreen::campaignList:
  80. tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, CSH, _1, nullptr);
  81. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRLOD.DEF"), CButton::tooltip(), std::bind(&CLobbyScreen::startCampaign, this), EShortcut::LOBBY_BEGIN_GAME);
  82. break;
  83. }
  84. buttonStart->block(true); // to be unblocked after map list is ready
  85. buttonBack = std::make_shared<CButton>(Point(581, 535), AnimationPath::builtin("SCNRBACK.DEF"), CGI->generaltexth->zelp[105], [&]()
  86. {
  87. CSH->sendClientDisconnecting();
  88. close();
  89. }, EShortcut::GLOBAL_CANCEL);
  90. }
  91. CLobbyScreen::~CLobbyScreen()
  92. {
  93. // TODO: For now we always destroy whole lobby when leaving bonus selection screen
  94. if(CSH->state == EClientState::LOBBY_CAMPAIGN)
  95. CSH->sendClientDisconnecting();
  96. }
  97. void CLobbyScreen::toggleTab(std::shared_ptr<CIntObject> tab)
  98. {
  99. if(tab == curTab)
  100. CSH->sendGuiAction(LobbyGuiAction::NO_TAB);
  101. else if(tab == tabOpt)
  102. CSH->sendGuiAction(LobbyGuiAction::OPEN_OPTIONS);
  103. else if(tab == tabSel)
  104. CSH->sendGuiAction(LobbyGuiAction::OPEN_SCENARIO_LIST);
  105. else if(tab == tabRand)
  106. CSH->sendGuiAction(LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS);
  107. else if(tab == tabTurnOptions)
  108. CSH->sendGuiAction(LobbyGuiAction::OPEN_TURN_OPTIONS);
  109. CSelectionBase::toggleTab(tab);
  110. }
  111. void CLobbyScreen::startCampaign()
  112. {
  113. if(!CSH->mi)
  114. return;
  115. try {
  116. auto ourCampaign = CampaignHandler::getCampaign(CSH->mi->fileURI);
  117. CSH->setCampaignState(ourCampaign);
  118. }
  119. catch (const std::runtime_error &e)
  120. {
  121. // handle possible exception on map loading. For example campaign that contains map in unsupported format
  122. // for example, wog campaigns or hota campaigns without hota map support mod
  123. MetaString message;
  124. message.appendTextID("vcmi.client.errors.invalidMap");
  125. message.replaceRawString(e.what());
  126. CInfoWindow::showInfoDialog(message.toString(), {});
  127. }
  128. }
  129. void CLobbyScreen::startScenario(bool allowOnlyAI)
  130. {
  131. if (CSH->validateGameStart(allowOnlyAI))
  132. {
  133. CSH->sendStartGame(allowOnlyAI);
  134. buttonStart->block(true);
  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 (buttonTurnOptions)
  147. buttonTurnOptions->addTextOverlay(CGI->generaltexth->translate("vcmi.optionsTab.turnOptions.hover"), FONT_SMALL, buttonColor);
  148. if(buttonRMG)
  149. {
  150. buttonRMG->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL, buttonColor);
  151. buttonRMG->block(!host);
  152. }
  153. buttonSelect->block(!host);
  154. buttonOptions->block(!host);
  155. if (buttonTurnOptions)
  156. buttonTurnOptions->block(!host);
  157. if(CSH->mi)
  158. {
  159. tabOpt->recreate();
  160. tabTurnOptions->recreate();
  161. }
  162. }
  163. void CLobbyScreen::toggleChat()
  164. {
  165. card->toggleChat();
  166. if(card->showChat)
  167. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL, Colors::WHITE);
  168. else
  169. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL, Colors::WHITE);
  170. }
  171. void CLobbyScreen::updateAfterStateChange()
  172. {
  173. if(CSH->mi)
  174. {
  175. if (tabOpt)
  176. tabOpt->recreate();
  177. if (tabTurnOptions)
  178. tabTurnOptions->recreate();
  179. }
  180. buttonStart->block(CSH->mi == nullptr || CSH->isGuest());
  181. card->changeSelection();
  182. if (card->iconDifficulty)
  183. {
  184. if (screenType == ESelectionScreen::loadGame)
  185. {
  186. // 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
  187. card->iconDifficulty->setSelectedOnly(CSH->si->difficulty);
  188. }
  189. else
  190. {
  191. card->iconDifficulty->setSelected(CSH->si->difficulty);
  192. }
  193. }
  194. if(curTab && curTab == tabRand && CSH->si->mapGenOptions)
  195. tabRand->setMapGenOptions(CSH->si->mapGenOptions);
  196. }
  197. const StartInfo * CLobbyScreen::getStartInfo()
  198. {
  199. return CSH->si.get();
  200. }
  201. const CMapInfo * CLobbyScreen::getMapInfo()
  202. {
  203. return CSH->mi.get();
  204. }