CLobbyScreen.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "../../CCallback.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/CGeneralTextHandler.h"
  27. #include "../../lib/campaign/CampaignHandler.h"
  28. #include "../../lib/mapping/CMapInfo.h"
  29. #include "../../lib/networkPacks/PacksForLobby.h"
  30. #include "../../lib/rmg/CMapGenOptions.h"
  31. #include "../CGameInfo.h"
  32. CLobbyScreen::CLobbyScreen(ESelectionScreen screenType)
  33. : CSelectionBase(screenType), bonusSel(nullptr)
  34. {
  35. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  36. tabSel = std::make_shared<SelectionTab>(screenType);
  37. curTab = tabSel;
  38. auto initLobby = [&]()
  39. {
  40. tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, CSH, _1, nullptr);
  41. buttonSelect = std::make_shared<CButton>(Point(411, 80), AnimationPath::builtin("GSPBUTT.DEF"), CGI->generaltexth->zelp[45], 0, EShortcut::LOBBY_SELECT_SCENARIO);
  42. buttonSelect->addCallback([&]()
  43. {
  44. toggleTab(tabSel);
  45. CSH->setMapInfo(tabSel->getSelectedMapInfo());
  46. });
  47. 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);
  48. if(settings["general"]["enableUiEnhancements"].Bool())
  49. {
  50. buttonTurnOptions = std::make_shared<CButton>(Point(619, 105), AnimationPath::builtin("GSPBUT2.DEF"), CGI->generaltexth->zelp[46], std::bind(&CLobbyScreen::toggleTab, this, tabTurnOptions), EShortcut::NONE);
  51. buttonExtraOptions = std::make_shared<CButton>(Point(619, 510), AnimationPath::builtin("GSPBUT2.DEF"), CGI->generaltexth->zelp[46], std::bind(&CLobbyScreen::toggleTab, this, tabExtraOptions), EShortcut::NONE);
  52. }
  53. };
  54. 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);
  55. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL, Colors::WHITE);
  56. switch(screenType)
  57. {
  58. case ESelectionScreen::newGame:
  59. {
  60. tabOpt = std::make_shared<OptionsTab>();
  61. tabTurnOptions = std::make_shared<TurnOptionsTab>();
  62. tabExtraOptions = std::make_shared<ExtraOptionsTab>();
  63. tabRand = std::make_shared<RandomMapTab>();
  64. tabRand->mapInfoChanged += std::bind(&IServerAPI::setMapInfo, CSH, _1, _2);
  65. buttonRMG = std::make_shared<CButton>(Point(411, 105), AnimationPath::builtin("GSPBUTT.DEF"), CGI->generaltexth->zelp[47], 0, EShortcut::LOBBY_RANDOM_MAP);
  66. buttonRMG->addCallback([&]()
  67. {
  68. toggleTab(tabRand);
  69. tabRand->updateMapInfoByHost(); // TODO: This is only needed to force-update mapInfo in CSH when tab is opened
  70. });
  71. card->iconDifficulty->addCallback(std::bind(&IServerAPI::setDifficulty, CSH, _1));
  72. 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);
  73. initLobby();
  74. break;
  75. }
  76. case ESelectionScreen::loadGame:
  77. {
  78. tabOpt = std::make_shared<OptionsTab>();
  79. tabTurnOptions = std::make_shared<TurnOptionsTab>();
  80. tabExtraOptions = std::make_shared<ExtraOptionsTab>();
  81. 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);
  82. initLobby();
  83. break;
  84. }
  85. case ESelectionScreen::campaignList:
  86. tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, CSH, _1, nullptr);
  87. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRLOD.DEF"), CButton::tooltip(), std::bind(&CLobbyScreen::startCampaign, this), EShortcut::LOBBY_BEGIN_GAME);
  88. break;
  89. }
  90. buttonStart->block(true); // to be unblocked after map list is ready
  91. buttonBack = std::make_shared<CButton>(Point(581, 535), AnimationPath::builtin("SCNRBACK.DEF"), CGI->generaltexth->zelp[105], [&]()
  92. {
  93. CSH->sendClientDisconnecting();
  94. close();
  95. }, EShortcut::GLOBAL_CANCEL);
  96. }
  97. CLobbyScreen::~CLobbyScreen()
  98. {
  99. // TODO: For now we always destroy whole lobby when leaving bonus selection screen
  100. if(CSH->state == EClientState::LOBBY_CAMPAIGN)
  101. CSH->sendClientDisconnecting();
  102. }
  103. void CLobbyScreen::toggleTab(std::shared_ptr<CIntObject> tab)
  104. {
  105. if(tab == curTab)
  106. CSH->sendGuiAction(LobbyGuiAction::NO_TAB);
  107. else if(tab == tabOpt)
  108. CSH->sendGuiAction(LobbyGuiAction::OPEN_OPTIONS);
  109. else if(tab == tabSel)
  110. CSH->sendGuiAction(LobbyGuiAction::OPEN_SCENARIO_LIST);
  111. else if(tab == tabRand)
  112. CSH->sendGuiAction(LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS);
  113. else if(tab == tabTurnOptions)
  114. CSH->sendGuiAction(LobbyGuiAction::OPEN_TURN_OPTIONS);
  115. else if(tab == tabExtraOptions)
  116. CSH->sendGuiAction(LobbyGuiAction::OPEN_EXTRA_OPTIONS);
  117. CSelectionBase::toggleTab(tab);
  118. }
  119. void CLobbyScreen::startCampaign()
  120. {
  121. if(CSH->mi)
  122. {
  123. auto ourCampaign = CampaignHandler::getCampaign(CSH->mi->fileURI);
  124. CSH->setCampaignState(ourCampaign);
  125. }
  126. }
  127. void CLobbyScreen::startScenario(bool allowOnlyAI)
  128. {
  129. if (CSH->validateGameStart(allowOnlyAI))
  130. {
  131. CSH->sendStartGame(allowOnlyAI);
  132. buttonStart->block(true);
  133. }
  134. }
  135. void CLobbyScreen::toggleMode(bool host)
  136. {
  137. tabSel->toggleMode();
  138. buttonStart->block(!host);
  139. if(screenType == ESelectionScreen::campaignList)
  140. return;
  141. auto buttonColor = host ? Colors::WHITE : Colors::ORANGE;
  142. buttonSelect->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL, buttonColor);
  143. buttonOptions->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL, buttonColor);
  144. if (buttonTurnOptions)
  145. buttonTurnOptions->addTextOverlay(CGI->generaltexth->translate("vcmi.optionsTab.turnOptions.hover"), FONT_SMALL, buttonColor);
  146. if (buttonExtraOptions)
  147. buttonExtraOptions->addTextOverlay(CGI->generaltexth->translate("vcmi.optionsTab.extraOptions.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 (buttonExtraOptions)
  158. buttonExtraOptions->block(!host);
  159. if(CSH->mi)
  160. {
  161. tabOpt->recreate();
  162. tabTurnOptions->recreate();
  163. tabExtraOptions->recreate();
  164. }
  165. }
  166. void CLobbyScreen::toggleChat()
  167. {
  168. card->toggleChat();
  169. if(card->showChat)
  170. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL, Colors::WHITE);
  171. else
  172. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL, Colors::WHITE);
  173. }
  174. void CLobbyScreen::updateAfterStateChange()
  175. {
  176. if(CSH->mi)
  177. {
  178. if (tabOpt)
  179. tabOpt->recreate();
  180. if (tabTurnOptions)
  181. tabTurnOptions->recreate();
  182. if (tabExtraOptions)
  183. tabExtraOptions->recreate();
  184. }
  185. buttonStart->block(CSH->mi == nullptr || CSH->isGuest());
  186. card->changeSelection();
  187. if (card->iconDifficulty)
  188. {
  189. if (screenType == ESelectionScreen::loadGame)
  190. {
  191. // 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
  192. card->iconDifficulty->setSelectedOnly(CSH->si->difficulty);
  193. }
  194. else
  195. {
  196. card->iconDifficulty->setSelected(CSH->si->difficulty);
  197. }
  198. }
  199. if(curTab && curTab == tabRand && CSH->si->mapGenOptions)
  200. tabRand->setMapGenOptions(CSH->si->mapGenOptions);
  201. }
  202. const StartInfo * CLobbyScreen::getStartInfo()
  203. {
  204. return CSH->si.get();
  205. }
  206. const CMapInfo * CLobbyScreen::getMapInfo()
  207. {
  208. return CSH->mi.get();
  209. }