CLobbyScreen.cpp 9.5 KB

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