CPreGame.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. #pragma once
  2. #include <SDL.h>
  3. #include "../lib/StartInfo.h"
  4. #include "GUIClasses.h"
  5. #include "FunctionList.h"
  6. #include "../lib/CMapInfo.h"
  7. /*
  8. * CPreGame.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CMusicHandler;
  17. class CMapHeader;
  18. class CCampaignHeader;
  19. class CTextInput;
  20. class CCampaign;
  21. class CGStatusBar;
  22. class CTextBox;
  23. class CCampaignState;
  24. class CConnection;
  25. class JsonNode;
  26. struct CPackForSelectionScreen;
  27. struct PlayerInfo;
  28. struct FileInfo;
  29. namespace boost{ class thread; class recursive_mutex;}
  30. enum ESortBy{_playerAm, _size, _format, _name, _viccon, _loscon, _numOfMaps}; //_numOfMaps is for campaigns
  31. /// Class which handles map sorting by different criteria
  32. class mapSorter
  33. {
  34. public:
  35. ESortBy sortBy;
  36. bool operator()(const CMapInfo *aaa, const CMapInfo *bbb);
  37. mapSorter(ESortBy es):sortBy(es){};
  38. };
  39. /// The main menu screens listed in the EState enum
  40. class CMenuScreen : public CIntObject
  41. {
  42. const JsonNode& config;
  43. CTabbedInt *tabs;
  44. CPicture * background;
  45. std::vector<CPicture*> images;
  46. CIntObject *createTab(size_t index);
  47. public:
  48. std::vector<std::string> menuNameToEntry;
  49. enum EState { //where are we?
  50. mainMenu, newGame, loadGame, campaignMain, saveGame, scenarioInfo, campaignList
  51. };
  52. enum EMultiMode {
  53. SINGLE_PLAYER = 0, MULTI_HOT_SEAT, MULTI_NETWORK_HOST, MULTI_NETWORK_GUEST
  54. };
  55. CMenuScreen(const JsonNode& configNode);
  56. void showAll(SDL_Surface * to);
  57. void show(SDL_Surface * to);
  58. void activate();
  59. void deactivate();
  60. void switchToTab(size_t index);
  61. };
  62. class CMenuEntry : public CIntObject
  63. {
  64. std::vector<CPicture*> images;
  65. std::vector<CAdventureMapButton*> buttons;
  66. CAdventureMapButton* createButton(CMenuScreen* parent, const JsonNode& button);
  67. public:
  68. CMenuEntry(CMenuScreen* parent, const JsonNode &config);
  69. };
  70. class CreditsScreen : public CIntObject
  71. {
  72. CTextBox* credits;
  73. public:
  74. CreditsScreen();
  75. void show(SDL_Surface * to);
  76. void showAll(SDL_Surface * to);
  77. void clickLeft(tribool down, bool previousState);
  78. void clickRight(tribool down, bool previousState);
  79. };
  80. /// Implementation of the chat box
  81. class CChatBox : public CIntObject
  82. {
  83. public:
  84. CTextBox *chatHistory;
  85. CTextInput *inputBox;
  86. CChatBox(const Rect &rect);
  87. void keyPressed(const SDL_KeyboardEvent & key);
  88. void addNewMessage(const std::string &text);
  89. };
  90. class InfoCard : public CIntObject
  91. {
  92. public:
  93. CPicture *bg;
  94. CMenuScreen::EState type;
  95. bool network;
  96. bool chatOn; //if chat is shown, then description is hidden
  97. CTextBox *mapDescription;
  98. CChatBox *chat;
  99. CPicture *playerListBg;
  100. CHighlightableButtonsGroup *difficulty;
  101. CDefHandler *sizes, *sFlags;;
  102. void changeSelection(const CMapInfo *to);
  103. void showAll(SDL_Surface * to);
  104. void clickRight(tribool down, bool previousState);
  105. void showTeamsPopup();
  106. void toggleChat();
  107. void setChat(bool activateChat);
  108. InfoCard(bool Network = false);
  109. ~InfoCard();
  110. };
  111. /// The selection tab which is shown at the map selection screen
  112. class SelectionTab : public CIntObject
  113. {
  114. private:
  115. CDefHandler *format; //map size
  116. void parseMaps(std::vector<FileInfo> &files, int start = 0, int threads = 1);
  117. void parseGames(std::vector<FileInfo> &files, bool multi);
  118. void parseCampaigns( std::vector<FileInfo> & files );
  119. void getFiles(std::vector<FileInfo> &out, const std::string &dirname, const std::string &ext);
  120. CMenuScreen::EState tabType;
  121. public:
  122. int positions; //how many entries (games/maps) can be shown
  123. CPicture *bg; //general bg image
  124. CSlider *slider;
  125. std::vector<CMapInfo> allItems;
  126. std::vector<CMapInfo*> curItems;
  127. size_t selectionPos;
  128. boost::function<void(CMapInfo *)> onSelect;
  129. ESortBy sortingBy;
  130. bool ascending;
  131. CTextInput *txt;
  132. void filter(int size, bool selectFirst = false); //0 - all
  133. void select(int position); //position: <0 - positions> position on the screen
  134. void selectAbs(int position); //position: absolute position in curItems vector
  135. int getPosition(int x, int y); //convert mouse coords to entry position; -1 means none
  136. void sliderMove(int slidPos);
  137. void sortBy(int criteria);
  138. void sort();
  139. void printMaps(SDL_Surface *to);
  140. int getLine();
  141. void selectFName(const std::string &fname);
  142. void showAll(SDL_Surface * to);
  143. void clickLeft(tribool down, bool previousState);
  144. void keyPressed(const SDL_KeyboardEvent & key);
  145. void onDoubleClick();
  146. SelectionTab(CMenuScreen::EState Type, const boost::function<void(CMapInfo *)> &OnSelect, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER);
  147. ~SelectionTab();
  148. };
  149. /// The options tab which is shown at the map selection phase.
  150. class OptionsTab : public CIntObject
  151. {
  152. CPicture *bg;
  153. public:
  154. enum SelType {TOWN, HERO, BONUS};
  155. struct SelectedBox : public CIntObject //img with current town/hero/bonus
  156. {
  157. SelType which;
  158. ui8 player; //serial nr
  159. SDL_Surface *getImg() const;
  160. const std::string *getText() const;
  161. SelectedBox(SelType Which, ui8 Player);
  162. void showAll(SDL_Surface * to);
  163. void clickRight(tribool down, bool previousState);
  164. };
  165. struct PlayerOptionsEntry : public CIntObject
  166. {
  167. PlayerInfo &pi;
  168. PlayerSettings &s;
  169. CPicture *bg;
  170. CAdventureMapButton *btns[6]; //left and right for town, hero, bonus
  171. CAdventureMapButton *flag;
  172. SelectedBox *town;
  173. SelectedBox *hero;
  174. SelectedBox *bonus;
  175. enum {HUMAN_OR_CPU, HUMAN, CPU} whoCanPlay;
  176. PlayerOptionsEntry(OptionsTab *owner, PlayerSettings &S);
  177. void selectButtons(); //hides unavailable buttons
  178. void showAll(SDL_Surface * to);
  179. };
  180. CSlider *turnDuration;
  181. std::set<int> usedHeroes;
  182. struct PlayerToRestore
  183. {
  184. int color, id;
  185. void reset() { color = id = -1; }
  186. PlayerToRestore(){ reset(); }
  187. } playerToRestore;
  188. std::map<int, PlayerOptionsEntry *> entries; //indexed by color
  189. void nextCastle(int player, int dir); //dir == -1 or +1
  190. void nextHero(int player, int dir); //dir == -1 or +1
  191. void nextBonus(int player, int dir); //dir == -1 or +1
  192. void setTurnLength(int npos);
  193. void flagPressed(int player);
  194. void recreate();
  195. OptionsTab();
  196. ~OptionsTab();
  197. void showAll(SDL_Surface * to);
  198. int nextAllowedHero( int min, int max, int incl, int dir );
  199. bool canUseThisHero( int ID );
  200. };
  201. /// Interface for selecting a map.
  202. class ISelectionScreenInfo
  203. {
  204. public:
  205. CMenuScreen::EMultiMode multiPlayer;
  206. CMenuScreen::EState screenType; //new/save/load#Game
  207. const CMapInfo *current;
  208. StartInfo sInfo;
  209. std::map<ui32, std::string> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  210. ISelectionScreenInfo(const std::map<ui32, std::string> *Names = NULL);
  211. virtual ~ISelectionScreenInfo();
  212. virtual void update(){};
  213. virtual void propagateOptions() {};
  214. virtual void postRequest(ui8 what, ui8 dir) {};
  215. virtual void postChatMessage(const std::string &txt){};
  216. void setPlayer(PlayerSettings &pset, unsigned player);
  217. void updateStartInfo( std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader );
  218. int getIdOfFirstUnallocatedPlayer(); //returns 0 if none
  219. bool isGuest() const;
  220. bool isHost() const;
  221. };
  222. /// The actual map selection screen which consists of the options and selection tab
  223. class CSelectionScreen : public CIntObject, public ISelectionScreenInfo
  224. {
  225. bool bordered;
  226. public:
  227. CPicture *bg; //general bg image
  228. InfoCard *card;
  229. OptionsTab *opt;
  230. CAdventureMapButton *start, *back;
  231. SelectionTab *sel;
  232. CIntObject *curTab;
  233. boost::thread *serverHandlingThread;
  234. boost::recursive_mutex *mx;
  235. std::list<CPackForSelectionScreen *> upcomingPacks; //protected by mx
  236. CConnection *serv; //connection to server, used in MP mode
  237. bool ongoingClosing;
  238. ui8 myNameID; //used when networking - otherwise all player are "mine"
  239. CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER, const std::map<ui32, std::string> *Names = NULL);
  240. ~CSelectionScreen();
  241. void toggleTab(CIntObject *tab);
  242. void changeSelection(const CMapInfo *to);
  243. void startCampaign();
  244. void startGame();
  245. void difficultyChange(int to);
  246. void handleConnection();
  247. void processPacks();
  248. void setSInfo(const StartInfo &si);
  249. void update() OVERRIDE;
  250. void propagateOptions() OVERRIDE;
  251. void postRequest(ui8 what, ui8 dir) OVERRIDE;
  252. void postChatMessage(const std::string &txt) OVERRIDE;
  253. void propagateNames();
  254. void showAll(SDL_Surface *to);
  255. };
  256. /// Save game screen
  257. class CSavingScreen : public CSelectionScreen
  258. {
  259. public:
  260. const CMapInfo *ourGame;
  261. CSavingScreen(bool hotseat = false);
  262. ~CSavingScreen();
  263. };
  264. /// Scenario information screen shown during the game (thus not really a "pre-game" but fits here anyway)
  265. class CScenarioInfo : public CIntObject, public ISelectionScreenInfo
  266. {
  267. public:
  268. CAdventureMapButton *back;
  269. InfoCard *card;
  270. OptionsTab *opt;
  271. CScenarioInfo(const CMapHeader *mapHeader, const StartInfo *startInfo);
  272. ~CScenarioInfo();
  273. };
  274. /// Multiplayer mode
  275. class CMultiMode : public CIntObject
  276. {
  277. public:
  278. CPicture *bg;
  279. CTextInput *txt;
  280. CAdventureMapButton *btns[7]; //0 - hotseat, 6 - cancel
  281. CGStatusBar *bar;
  282. CMultiMode();
  283. void openHotseat();
  284. void hostTCP();
  285. void joinTCP();
  286. };
  287. /// Hot seat player window
  288. class CHotSeatPlayers : public CIntObject
  289. {
  290. CPicture *bg;
  291. CTextBox *title;
  292. CTextInput* txt[8];
  293. CAdventureMapButton *ok, *cancel;
  294. CGStatusBar *bar;
  295. void onChange(std::string newText);
  296. void enterSelectionScreen();
  297. public:
  298. CHotSeatPlayers(const std::string &firstPlayer);
  299. };
  300. /// Campaign screen where you can choose one out of three starting bonuses
  301. class CBonusSelection : public CIntObject
  302. {
  303. SDL_Surface * background;
  304. CAdventureMapButton * startB, * backB;
  305. //campaign & map descriptions:
  306. CTextBox * cmpgDesc, * mapDesc;
  307. struct SCampPositions
  308. {
  309. std::string campPrefix;
  310. int colorSuffixLength;
  311. struct SRegionDesc
  312. {
  313. std::string infix;
  314. int xpos, ypos;
  315. };
  316. std::vector<SRegionDesc> regions;
  317. };
  318. std::vector<SCampPositions> campDescriptions;
  319. class CRegion : public CIntObject
  320. {
  321. CBonusSelection * owner;
  322. SDL_Surface* graphics[3]; //[0] - not selected, [1] - selected, [2] - striped
  323. bool accessible; //false if region should be striped
  324. bool selectable; //true if region should be selectable
  325. int myNumber; //number of region
  326. public:
  327. std::string rclickText;
  328. CRegion(CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber);
  329. ~CRegion();
  330. void clickLeft(tribool down, bool previousState);
  331. void clickRight(tribool down, bool previousState);
  332. void show(SDL_Surface * to);
  333. };
  334. std::vector<CRegion *> regions;
  335. CRegion * highlightedRegion;
  336. void loadPositionsOfGraphics();
  337. CCampaignState * ourCampaign;
  338. CMapHeader *ourHeader;
  339. CDefHandler *sizes; //icons of map sizes
  340. SDL_Surface* diffPics[5]; //pictures of difficulties, user-selectable (or not if campaign locks this)
  341. CAdventureMapButton * diffLb, * diffRb; //buttons for changing difficulty
  342. void changeDiff(bool increase); //if false, then decrease
  343. //bonus selection
  344. void updateBonusSelection();
  345. CHighlightableButtonsGroup * bonuses;
  346. public:
  347. void bonusSelectionChanges(int choosenBonus);
  348. StartInfo sInfo;
  349. CDefHandler *sFlags;
  350. void selectMap(int whichOne);
  351. void selectBonus(int id);
  352. CBonusSelection(CCampaignState * _ourCampaign);
  353. ~CBonusSelection();
  354. void showAll(SDL_Surface * to);
  355. void show(SDL_Surface * to);
  356. void goBack();
  357. void startMap();
  358. };
  359. /// Campaign selection screen
  360. class CCampaignScreen : public CIntObject
  361. {
  362. public:
  363. enum CampaignStatus {DEFAULT = 0, ENABLED, DISABLED, COMPLETED}; // the status of the campaign
  364. private:
  365. /// A button which plays a video when you move the mouse cursor over it
  366. class CCampaignButton : public CIntObject
  367. {
  368. private:
  369. CPicture *image;
  370. CPicture *checkMark;
  371. CLabel *hoverLabel;
  372. CampaignStatus status;
  373. std::string campFile; // the filename/resourcename of the campaign
  374. std::string video; // the resource name of the video
  375. std::string hoverText;
  376. void clickLeft(tribool down, bool previousState);
  377. void hover(bool on);
  378. public:
  379. CCampaignButton(const JsonNode &config );
  380. void show(SDL_Surface * to);
  381. };
  382. CAdventureMapButton *back;
  383. std::vector<CCampaignButton*> campButtons;
  384. std::vector<CPicture*> images;
  385. CAdventureMapButton* createExitButton(const JsonNode& button);
  386. public:
  387. enum CampaignSet {ROE, AB, SOD, WOG};
  388. CCampaignScreen(const JsonNode &config);
  389. void showAll(SDL_Surface *to);
  390. };
  391. /// Handles background screen, loads graphics for victory/loss condition and random town or hero selection
  392. class CGPreGame : public CIntObject, public IUpdateable
  393. {
  394. const JsonNode * const pregameConfig;
  395. void loadGraphics();
  396. void disposeGraphics();
  397. CGPreGame(); //Use createIfNotPresent
  398. public:
  399. CMenuScreen* menu;
  400. SDL_Surface *nHero, *rHero, *nTown, *rTown; // none/random hero/town imgs
  401. CDefHandler *bonuses;
  402. CDefHandler *victory, *loss;
  403. ~CGPreGame();
  404. void update();
  405. void openSel(CMenuScreen::EState type, CMenuScreen::EMultiMode multi = CMenuScreen::SINGLE_PLAYER);
  406. void openCampaignScreen(std::string name);
  407. static CGPreGame * create();
  408. void removeFromGui();
  409. };
  410. extern ISelectionScreenInfo *SEL;
  411. extern CGPreGame *CGP;