CLobbyScreen.cpp 8.7 KB

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