CMainMenu.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 CampaignState;
  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(Canvas & 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. std::string getPlayerName();
  74. void onNameChange(std::string newText);
  75. };
  76. /// Hot seat player window
  77. class CMultiPlayers : public WindowBase
  78. {
  79. bool host;
  80. ELoadMode loadMode;
  81. ESelectionScreen screenType;
  82. std::shared_ptr<CPicture> background;
  83. std::shared_ptr<CTextBox> textTitle;
  84. std::array<std::shared_ptr<CTextInput>, 8> inputNames;
  85. std::shared_ptr<CButton> buttonOk;
  86. std::shared_ptr<CButton> buttonCancel;
  87. std::shared_ptr<CGStatusBar> statusBar;
  88. void onChange(std::string newText);
  89. void enterSelectionScreen();
  90. public:
  91. CMultiPlayers(const std::string & firstPlayer, ESelectionScreen ScreenType, bool Host, ELoadMode LoadMode);
  92. };
  93. /// Manages the configuration of pregame GUI elements like campaign screen, main menu, loading screen,...
  94. class CMainMenuConfig
  95. {
  96. public:
  97. static CMainMenuConfig & get();
  98. const JsonNode & getConfig() const;
  99. const JsonNode & getCampaigns() const;
  100. private:
  101. CMainMenuConfig();
  102. const JsonNode campaignSets;
  103. const JsonNode config;
  104. };
  105. /// Handles background screen, loads graphics for victory/loss condition and random town or hero selection
  106. class CMainMenu : public CIntObject, public IUpdateable, public std::enable_shared_from_this<CMainMenu>
  107. {
  108. std::shared_ptr<CFilledTexture> backgroundAroundMenu;
  109. CMainMenu(); //Use CMainMenu::create
  110. public:
  111. std::shared_ptr<CMenuScreen> menu;
  112. ~CMainMenu();
  113. void activate() override;
  114. void onScreenResize() override;
  115. void update() override;
  116. static void openLobby(ESelectionScreen screenType, bool host, const std::vector<std::string> * names, ELoadMode loadMode);
  117. static void openCampaignLobby(const std::string & campaignFileName);
  118. static void openCampaignLobby(std::shared_ptr<CampaignState> campaign);
  119. void openCampaignScreen(std::string name);
  120. static std::shared_ptr<CMainMenu> create();
  121. static std::shared_ptr<CPicture> createPicture(const JsonNode & config);
  122. };
  123. /// Simple window to enter the server's address.
  124. class CSimpleJoinScreen : public WindowBase
  125. {
  126. std::shared_ptr<CPicture> background;
  127. std::shared_ptr<CTextBox> textTitle;
  128. std::shared_ptr<CButton> buttonOk;
  129. std::shared_ptr<CButton> buttonCancel;
  130. std::shared_ptr<CGStatusBar> statusBar;
  131. std::shared_ptr<CTextInput> inputAddress;
  132. std::shared_ptr<CTextInput> inputPort;
  133. void connectToServer();
  134. void leaveScreen();
  135. void onChange(const std::string & newText);
  136. void startConnectThread(const std::string & addr = {}, ui16 port = 0);
  137. void connectThread(const std::string & addr, ui16 port);
  138. public:
  139. CSimpleJoinScreen(bool host = true);
  140. };
  141. class CLoadingScreen : public CWindowObject
  142. {
  143. boost::thread loadingThread;
  144. std::string getBackground();
  145. public:
  146. CLoadingScreen(std::function<void()> loader);
  147. ~CLoadingScreen();
  148. void showAll(Canvas & to) override;
  149. };
  150. extern std::shared_ptr<CMainMenu> CMM;