CLobbyScreen.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. CSelectionBase::toggleTab(tab);
  108. }
  109. void CLobbyScreen::startCampaign()
  110. {
  111. if(CSH->mi)
  112. {
  113. auto ourCampaign = CampaignHandler::getCampaign(CSH->mi->fileURI);
  114. CSH->setCampaignState(ourCampaign);
  115. }
  116. }
  117. void CLobbyScreen::startScenario(bool allowOnlyAI)
  118. {
  119. if (CSH->validateGameStart(allowOnlyAI))
  120. {
  121. CSH->sendStartGame(allowOnlyAI);
  122. buttonStart->block(true);
  123. }
  124. }
  125. void CLobbyScreen::toggleMode(bool host)
  126. {
  127. tabSel->toggleMode();
  128. buttonStart->block(!host);
  129. if(screenType == ESelectionScreen::campaignList)
  130. return;
  131. auto buttonColor = host ? Colors::WHITE : Colors::ORANGE;
  132. buttonSelect->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL, buttonColor);
  133. buttonOptions->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL, buttonColor);
  134. if (buttonTurnOptions)
  135. buttonTurnOptions->addTextOverlay(CGI->generaltexth->translate("vcmi.optionsTab.turnOptions.hover"), FONT_SMALL, buttonColor);
  136. if(buttonRMG)
  137. {
  138. buttonRMG->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL, buttonColor);
  139. buttonRMG->block(!host);
  140. }
  141. buttonSelect->block(!host);
  142. buttonOptions->block(!host);
  143. if (buttonTurnOptions)
  144. buttonTurnOptions->block(!host);
  145. if(CSH->mi)
  146. {
  147. tabOpt->recreate();
  148. tabTurnOptions->recreate();
  149. }
  150. }
  151. void CLobbyScreen::toggleChat()
  152. {
  153. card->toggleChat();
  154. if(card->showChat)
  155. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL, Colors::WHITE);
  156. else
  157. buttonChat->addTextOverlay(CGI->generaltexth->allTexts[532], FONT_SMALL, Colors::WHITE);
  158. }
  159. void CLobbyScreen::updateAfterStateChange()
  160. {
  161. if(CSH->mi)
  162. {
  163. if (tabOpt)
  164. tabOpt->recreate();
  165. if (tabTurnOptions)
  166. tabTurnOptions->recreate();
  167. }
  168. buttonStart->block(CSH->mi == nullptr || CSH->isGuest());
  169. card->changeSelection();
  170. if (card->iconDifficulty)
  171. {
  172. if (screenType == ESelectionScreen::loadGame)
  173. {
  174. // 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
  175. card->iconDifficulty->setSelectedOnly(CSH->si->difficulty);
  176. }
  177. else
  178. {
  179. card->iconDifficulty->setSelected(CSH->si->difficulty);
  180. }
  181. }
  182. if(curTab && curTab == tabRand && CSH->si->mapGenOptions)
  183. tabRand->setMapGenOptions(CSH->si->mapGenOptions);
  184. }
  185. const StartInfo * CLobbyScreen::getStartInfo()
  186. {
  187. return CSH->si.get();
  188. }
  189. const CMapInfo * CLobbyScreen::getMapInfo()
  190. {
  191. return CSH->mi.get();
  192. }