CMainMenu.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * CMainMenu.h, 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. #pragma once
  11. #include "../windows/CWindowObject.h"
  12. #include "../../lib/json/JsonNode.h"
  13. #include "../../lib/LoadProgress.h"
  14. #include "../../lib/network/NetworkInterface.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CampaignState;
  17. VCMI_LIB_NAMESPACE_END
  18. class CTextInput;
  19. class CGStatusBar;
  20. class CTextBox;
  21. class CTabbedInt;
  22. class CAnimImage;
  23. class CButton;
  24. class CFilledTexture;
  25. class CLabel;
  26. class VideoWidget;
  27. // TODO: Find new location for these enums
  28. enum class ESelectionScreen : ui8 {
  29. unknown = 0, newGame, loadGame, saveGame, scenarioInfo, campaignList
  30. };
  31. enum class ELoadMode : ui8
  32. {
  33. NONE = 0, SINGLE, MULTI, CAMPAIGN, TUTORIAL
  34. };
  35. /// The main menu screens listed in the EState enum
  36. class CMenuScreen : public CWindowObject
  37. {
  38. const JsonNode & config;
  39. std::shared_ptr<CTabbedInt> tabs;
  40. std::shared_ptr<CPicture> background;
  41. std::shared_ptr<VideoWidget> videoPlayer;
  42. std::vector<std::shared_ptr<CPicture>> images;
  43. std::shared_ptr<CIntObject> createTab(size_t index);
  44. public:
  45. std::vector<std::string> menuNameToEntry;
  46. CMenuScreen(const JsonNode & configNode);
  47. void activate() override;
  48. void show(Canvas & to) override;
  49. void switchToTab(size_t index);
  50. void switchToTab(std::string name);
  51. size_t getActiveTab() const;
  52. };
  53. class CMenuEntry : public CIntObject
  54. {
  55. std::vector<std::shared_ptr<CPicture>> images;
  56. std::vector<std::shared_ptr<CButton>> buttons;
  57. std::shared_ptr<CButton> createButton(CMenuScreen * parent, const JsonNode & button);
  58. public:
  59. CMenuEntry(CMenuScreen * parent, const JsonNode & config);
  60. };
  61. /// Multiplayer mode
  62. class CMultiMode : public WindowBase
  63. {
  64. public:
  65. ESelectionScreen screenType;
  66. std::shared_ptr<CPicture> background;
  67. std::shared_ptr<CPicture> picture;
  68. std::shared_ptr<CTextBox> textTitle;
  69. std::shared_ptr<CTextBox> textTitleIp;
  70. std::shared_ptr<CTextInput> playerName;
  71. std::shared_ptr<CButton> buttonHotseat;
  72. std::shared_ptr<CButton> buttonLobby;
  73. std::shared_ptr<CButton> buttonHost;
  74. std::shared_ptr<CButton> buttonJoin;
  75. std::shared_ptr<CButton> buttonCancel;
  76. std::shared_ptr<CGStatusBar> statusBar;
  77. CMultiMode(ESelectionScreen ScreenType);
  78. void openLobby();
  79. void hostTCP(EShortcut shortcut);
  80. void joinTCP(EShortcut shortcut);
  81. /// Get all configured player names. The first name would always be present and initialized to its default value.
  82. static std::vector<std::string> getPlayersNames();
  83. void onNameChange(std::string newText);
  84. };
  85. /// Multiplayer join
  86. class JoinScreen : public WindowBase, public IServerDiscoveryObserver
  87. {
  88. public:
  89. ESelectionScreen screenType;
  90. std::vector<std::string> playerNames;
  91. std::shared_ptr<CPicture> background;
  92. std::shared_ptr<CTextBox> textTitle;
  93. std::shared_ptr<CTextInput> playerName;
  94. std::shared_ptr<CButton> buttonSearch;
  95. std::shared_ptr<CButton> buttonCancel;
  96. std::shared_ptr<CGStatusBar> statusBar;
  97. std::vector<std::shared_ptr<CLabel>> labelsJoin;
  98. std::vector<std::shared_ptr<CButton>> buttonsJoin;
  99. std::shared_ptr<IServerDiscovery> serverDiscovery;
  100. JoinScreen(ESelectionScreen ScreenType, std::vector<std::string> playerNames);
  101. ~JoinScreen();
  102. void onServerDiscovered(const DiscoveredServer & server) override;
  103. };
  104. /// Hot seat player window
  105. class CMultiPlayers : public WindowBase
  106. {
  107. bool host;
  108. ELoadMode loadMode;
  109. ESelectionScreen screenType;
  110. std::shared_ptr<CPicture> background;
  111. std::shared_ptr<CTextBox> textTitle;
  112. std::array<std::shared_ptr<CTextInput>, 8> inputNames;
  113. std::shared_ptr<CButton> buttonOk;
  114. std::shared_ptr<CButton> buttonCancel;
  115. std::shared_ptr<CGStatusBar> statusBar;
  116. void onChange(std::string newText);
  117. void enterSelectionScreen();
  118. public:
  119. CMultiPlayers(const std::vector<std::string> & playerNames, ESelectionScreen ScreenType, bool Host, ELoadMode LoadMode, EShortcut shortcut);
  120. };
  121. /// Manages the configuration of pregame GUI elements like campaign screen, main menu, loading screen,...
  122. class CMainMenuConfig
  123. {
  124. public:
  125. static const CMainMenuConfig & get();
  126. const JsonNode & getConfig() const;
  127. const JsonNode & getCampaigns() const;
  128. private:
  129. CMainMenuConfig();
  130. const JsonNode campaignSets;
  131. const JsonNode config;
  132. };
  133. /// Handles background screen, loads graphics for victory/loss condition and random town or hero selection
  134. class CMainMenu final : public CIntObject, public std::enable_shared_from_this<CMainMenu>
  135. {
  136. std::shared_ptr<CFilledTexture> backgroundAroundMenu;
  137. std::vector<VideoPath> videoPlayList;
  138. public:
  139. CMainMenu();
  140. std::shared_ptr<CMenuScreen> menu;
  141. ~CMainMenu();
  142. void activate() override;
  143. void onScreenResize() override;
  144. void makeActiveInterface();
  145. static void openLobby(ESelectionScreen screenType, bool host, const std::vector<std::string> & names, ELoadMode loadMode, bool battleMode, std::string server = {}, ui16 port = 0);
  146. static void openCampaignLobby(const std::string & campaignFileName, std::string campaignSet = "");
  147. static void openCampaignLobby(std::shared_ptr<CampaignState> campaign);
  148. static void startTutorial();
  149. static void openHighScoreScreen();
  150. void openCampaignScreen(std::string name);
  151. static std::shared_ptr<CPicture> createPicture(const JsonNode & config);
  152. void playIntroVideos();
  153. void playMusic();
  154. };
  155. /// Simple window to enter the server's address.
  156. class CSimpleJoinScreen : public WindowBase
  157. {
  158. std::shared_ptr<CPicture> background;
  159. std::shared_ptr<CTextBox> textTitle;
  160. std::shared_ptr<CButton> buttonOk;
  161. std::shared_ptr<CButton> buttonCancel;
  162. std::shared_ptr<CGStatusBar> statusBar;
  163. std::shared_ptr<CTextInput> inputAddress;
  164. std::shared_ptr<CTextInput> inputPort;
  165. void connectToServer();
  166. void leaveScreen();
  167. void onChange(const std::string & newText);
  168. void startConnection(const std::string & addr = {}, ui16 port = 0);
  169. public:
  170. CSimpleJoinScreen(bool host = true, std::string server = {}, ui16 port = 0);
  171. };
  172. class CLoadingScreen : virtual public CWindowObject, virtual public Load::Progress
  173. {
  174. std::shared_ptr<CPicture> backimg;
  175. std::vector<std::shared_ptr<CPicture>> images;
  176. std::shared_ptr<CPicture> loadFrame;
  177. std::vector<std::shared_ptr<CAnimImage>> progressBlocks;
  178. ImagePath getBackground();
  179. public:
  180. CLoadingScreen();
  181. CLoadingScreen(ImagePath background);
  182. ~CLoadingScreen();
  183. void tick(uint32_t msPassed) override;
  184. };