CMainMenu.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/JsonNode.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CCampaignState;
  15. VCMI_LIB_NAMESPACE_END
  16. class CTextInput;
  17. class CGStatusBar;
  18. class CTextBox;
  19. class CTabbedInt;
  20. class CAnimation;
  21. class CButton;
  22. class CFilledTexture;
  23. // TODO: Find new location for these enums
  24. enum ESelectionScreen : ui8 {
  25. unknown = 0, newGame, loadGame, saveGame, scenarioInfo, campaignList
  26. };
  27. enum ELoadMode : ui8
  28. {
  29. NONE = 0, SINGLE, MULTI, CAMPAIGN
  30. };
  31. /// The main menu screens listed in the EState enum
  32. class CMenuScreen : public CWindowObject
  33. {
  34. const JsonNode & config;
  35. std::shared_ptr<CTabbedInt> tabs;
  36. std::shared_ptr<CPicture> background;
  37. std::vector<std::shared_ptr<CPicture>> images;
  38. std::shared_ptr<CIntObject> createTab(size_t index);
  39. public:
  40. std::vector<std::string> menuNameToEntry;
  41. CMenuScreen(const JsonNode & configNode);
  42. void show(SDL_Surface * to) override;
  43. void activate() override;
  44. void deactivate() override;
  45. void switchToTab(size_t index);
  46. void switchToTab(std::string name);
  47. size_t getActiveTab() const;
  48. };
  49. class CMenuEntry : public CIntObject
  50. {
  51. std::vector<std::shared_ptr<CPicture>> images;
  52. std::vector<std::shared_ptr<CButton>> buttons;
  53. std::shared_ptr<CButton> createButton(CMenuScreen * parent, const JsonNode & button);
  54. public:
  55. CMenuEntry(CMenuScreen * parent, const JsonNode & config);
  56. };
  57. /// Multiplayer mode
  58. class CMultiMode : public WindowBase
  59. {
  60. public:
  61. ESelectionScreen screenType;
  62. std::shared_ptr<CPicture> background;
  63. std::shared_ptr<CPicture> picture;
  64. std::shared_ptr<CTextInput> playerName;
  65. std::shared_ptr<CButton> buttonHotseat;
  66. std::shared_ptr<CButton> buttonHost;
  67. std::shared_ptr<CButton> buttonJoin;
  68. std::shared_ptr<CButton> buttonCancel;
  69. std::shared_ptr<CGStatusBar> statusBar;
  70. CMultiMode(ESelectionScreen ScreenType);
  71. void hostTCP();
  72. void joinTCP();
  73. void onNameChange(std::string newText);
  74. };
  75. /// Hot seat player window
  76. class CMultiPlayers : public WindowBase
  77. {
  78. bool host;
  79. ELoadMode loadMode;
  80. ESelectionScreen screenType;
  81. std::shared_ptr<CPicture> background;
  82. std::shared_ptr<CTextBox> textTitle;
  83. std::array<std::shared_ptr<CTextInput>, 8> inputNames;
  84. std::shared_ptr<CButton> buttonOk;
  85. std::shared_ptr<CButton> buttonCancel;
  86. std::shared_ptr<CGStatusBar> statusBar;
  87. void onChange(std::string newText);
  88. void enterSelectionScreen();
  89. public:
  90. CMultiPlayers(const std::string & firstPlayer, ESelectionScreen ScreenType, bool Host, ELoadMode LoadMode);
  91. };
  92. /// Manages the configuration of pregame GUI elements like campaign screen, main menu, loading screen,...
  93. class CMainMenuConfig
  94. {
  95. public:
  96. static CMainMenuConfig & get();
  97. const JsonNode & getConfig() const;
  98. const JsonNode & getCampaigns() const;
  99. private:
  100. CMainMenuConfig();
  101. const JsonNode campaignSets;
  102. const JsonNode config;
  103. };
  104. /// Handles background screen, loads graphics for victory/loss condition and random town or hero selection
  105. class CMainMenu : public CIntObject, public IUpdateable, public std::enable_shared_from_this<CMainMenu>
  106. {
  107. std::shared_ptr<CFilledTexture> backgroundAroundMenu;
  108. CMainMenu(); //Use CMainMenu::create
  109. public:
  110. std::shared_ptr<CMenuScreen> menu;
  111. ~CMainMenu();
  112. void activate() override;
  113. void onScreenResize() override;
  114. void update() override;
  115. static void openLobby(ESelectionScreen screenType, bool host, const std::vector<std::string> * names, ELoadMode loadMode);
  116. static void openCampaignLobby(const std::string & campaignFileName);
  117. static void openCampaignLobby(std::shared_ptr<CCampaignState> campaign);
  118. void openCampaignScreen(std::string name);
  119. static std::shared_ptr<CMainMenu> create();
  120. static std::shared_ptr<CPicture> createPicture(const JsonNode & config);
  121. };
  122. /// Simple window to enter the server's address.
  123. class CSimpleJoinScreen : public WindowBase
  124. {
  125. std::shared_ptr<CPicture> background;
  126. std::shared_ptr<CTextBox> textTitle;
  127. std::shared_ptr<CButton> buttonOk;
  128. std::shared_ptr<CButton> buttonCancel;
  129. std::shared_ptr<CGStatusBar> statusBar;
  130. std::shared_ptr<CTextInput> inputAddress;
  131. std::shared_ptr<CTextInput> inputPort;
  132. void connectToServer();
  133. void leaveScreen();
  134. void onChange(const std::string & newText);
  135. void startConnectThread(const std::string & addr = {}, ui16 port = 0);
  136. void connectThread(const std::string & addr, ui16 port);
  137. public:
  138. CSimpleJoinScreen(bool host = true);
  139. };
  140. class CLoadingScreen : public CWindowObject
  141. {
  142. boost::thread loadingThread;
  143. std::string getBackground();
  144. public:
  145. CLoadingScreen(std::function<void()> loader);
  146. ~CLoadingScreen();
  147. void showAll(SDL_Surface * to) override;
  148. };
  149. extern std::shared_ptr<CMainMenu> CMM;