AdventureMapClasses.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * AdventureMapClasses.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 "ObjectLists.h"
  12. #include "../../lib/FunctionList.h"
  13. #include "Terrain.h"
  14. class CArmedInstance;
  15. class CAnimation;
  16. class CAnimImage;
  17. class CShowableAnim;
  18. class CFilledTexture;
  19. class CGGarrison;
  20. class CGObjectInstance;
  21. class CGHeroInstance;
  22. class CGTownInstance;
  23. class CButton;
  24. struct Component;
  25. class CComponent;
  26. struct InfoAboutArmy;
  27. struct InfoAboutHero;
  28. struct InfoAboutTown;
  29. class CHeroTooltip;
  30. class CTownTooltip;
  31. class CTextBox;
  32. /// Base UI Element for hero\town lists
  33. class CList : public CIntObject
  34. {
  35. protected:
  36. class CListItem : public CIntObject, public std::enable_shared_from_this<CListItem>
  37. {
  38. CList * parent;
  39. std::shared_ptr<CIntObject> selection;
  40. public:
  41. CListItem(CList * parent);
  42. ~CListItem();
  43. void clickRight(tribool down, bool previousState) override;
  44. void clickLeft(tribool down, bool previousState) override;
  45. void hover(bool on) override;
  46. void onSelect(bool on);
  47. /// create object with selection rectangle
  48. virtual std::shared_ptr<CIntObject> genSelection()=0;
  49. /// reaction on item selection (e.g. enable selection border)
  50. /// NOTE: item may be deleted in selected state
  51. virtual void select(bool on)=0;
  52. /// open item (town or hero screen)
  53. virtual void open()=0;
  54. /// show right-click tooltip
  55. virtual void showTooltip()=0;
  56. /// get hover text for status bar
  57. virtual std::string getHoverText()=0;
  58. };
  59. std::shared_ptr<CListBox> listBox;
  60. const size_t size;
  61. /**
  62. * @brief CList - protected constructor
  63. * @param size - maximal amount of visible at once items
  64. * @param position - cordinates
  65. * @param btnUp - path to image to use as top button
  66. * @param btnDown - path to image to use as bottom button
  67. * @param listAmount - amount of items in the list
  68. * @param helpUp - index in zelp.txt for button help tooltip
  69. * @param helpDown - index in zelp.txt for button help tooltip
  70. * @param create - function for creating items in listbox
  71. * @param destroy - function for deleting items in listbox
  72. */
  73. CList(int size, Point position, std::string btnUp, std::string btnDown, size_t listAmount, int helpUp, int helpDown, CListBox::CreateFunc create);
  74. //for selection\deselection
  75. std::shared_ptr<CListItem> selected;
  76. void select(std::shared_ptr<CListItem> which);
  77. friend class CListItem;
  78. std::shared_ptr<CButton> scrollUp;
  79. std::shared_ptr<CButton> scrollDown;
  80. /// should be called when list is invalidated
  81. void update();
  82. public:
  83. /// functions that will be called when selection changes
  84. CFunctionList<void()> onSelect;
  85. /// return index of currently selected element
  86. int getSelectedIndex();
  87. /// set of methods to switch selection
  88. void selectIndex(int which);
  89. void selectNext();
  90. void selectPrev();
  91. };
  92. /// List of heroes which is shown at the right of the adventure map screen
  93. class CHeroList : public CList
  94. {
  95. /// Empty hero item used as placeholder for unused entries in list
  96. class CEmptyHeroItem : public CIntObject
  97. {
  98. std::shared_ptr<CAnimImage> movement;
  99. std::shared_ptr<CAnimImage> mana;
  100. std::shared_ptr<CPicture> portrait;
  101. public:
  102. CEmptyHeroItem();
  103. };
  104. class CHeroItem : public CListItem
  105. {
  106. std::shared_ptr<CAnimImage> movement;
  107. std::shared_ptr<CAnimImage> mana;
  108. std::shared_ptr<CAnimImage> portrait;
  109. public:
  110. const CGHeroInstance * const hero;
  111. CHeroItem(CHeroList * parent, const CGHeroInstance * hero);
  112. std::shared_ptr<CIntObject> genSelection() override;
  113. void update();
  114. void select(bool on) override;
  115. void open() override;
  116. void showTooltip() override;
  117. std::string getHoverText() override;
  118. };
  119. std::shared_ptr<CIntObject> createHeroItem(size_t index);
  120. public:
  121. /**
  122. * @brief CHeroList
  123. * @param size, position, btnUp, btnDown @see CList::CList
  124. */
  125. CHeroList(int size, Point position, std::string btnUp, std::string btnDown);
  126. /// Select specific hero and scroll if needed
  127. void select(const CGHeroInstance * hero = nullptr);
  128. /// Update hero. Will add or remove it from the list if needed
  129. void update(const CGHeroInstance * hero = nullptr);
  130. };
  131. /// List of towns which is shown at the right of the adventure map screen or in the town screen
  132. class CTownList : public CList
  133. {
  134. class CTownItem : public CListItem
  135. {
  136. std::shared_ptr<CAnimImage> picture;
  137. public:
  138. const CGTownInstance * const town;
  139. CTownItem(CTownList *parent, const CGTownInstance * town);
  140. std::shared_ptr<CIntObject> genSelection() override;
  141. void update();
  142. void select(bool on) override;
  143. void open() override;
  144. void showTooltip() override;
  145. std::string getHoverText() override;
  146. };
  147. std::shared_ptr<CIntObject> createTownItem(size_t index);
  148. public:
  149. /**
  150. * @brief CTownList
  151. * @param size, position, btnUp, btnDown @see CList::CList
  152. */
  153. CTownList(int size, Point position, std::string btnUp, std::string btnDown);
  154. /// Select specific town and scroll if needed
  155. void select(const CGTownInstance * town = nullptr);
  156. /// Update town. Will add or remove it from the list if needed
  157. void update(const CGTownInstance * town = nullptr);
  158. };
  159. class CMinimap;
  160. class CMinimapInstance : public CIntObject
  161. {
  162. CMinimap * parent;
  163. SDL_Surface * minimap;
  164. int level;
  165. //get color of selected tile on minimap
  166. const SDL_Color & getTileColor(const int3 & pos);
  167. void blitTileWithColor(const SDL_Color & color, const int3 & pos, SDL_Surface * to, int x, int y);
  168. //draw minimap already scaled.
  169. //result is not antialiased. Will result in "missing" pixels on huge maps (>144)
  170. void drawScaled(int level);
  171. public:
  172. CMinimapInstance(CMinimap * parent, int level);
  173. ~CMinimapInstance();
  174. void showAll(SDL_Surface * to) override;
  175. void tileToPixels (const int3 & tile, int & x, int & y, int toX = 0, int toY = 0);
  176. void refreshTile(const int3 & pos);
  177. };
  178. /// Minimap which is displayed at the right upper corner of adventure map
  179. class CMinimap : public CIntObject
  180. {
  181. protected:
  182. std::shared_ptr<CPicture> aiShield; //the graphic displayed during AI turn
  183. std::shared_ptr<CMinimapInstance> minimap;
  184. int level;
  185. //to initialize colors
  186. std::map<TTerrainId, std::pair<SDL_Color, SDL_Color> > loadColors();
  187. void clickLeft(tribool down, bool previousState) override;
  188. void clickRight(tribool down, bool previousState) override;
  189. void hover (bool on) override;
  190. void mouseMoved (const SDL_MouseMotionEvent & sEvent) override;
  191. void moveAdvMapSelection();
  192. public:
  193. // terrainID -> (normal color, blocked color)
  194. const std::map<TTerrainId, std::pair<SDL_Color, SDL_Color> > colors;
  195. CMinimap(const Rect & position);
  196. //should be called to invalidate whole map - different player or level
  197. int3 translateMousePosition();
  198. void update();
  199. void setLevel(int level);
  200. void setAIRadar(bool on);
  201. void showAll(SDL_Surface * to) override;
  202. void hideTile(const int3 &pos); //puts FoW
  203. void showTile(const int3 &pos); //removes FoW
  204. };
  205. /// Info box which shows next week/day information, hold the current date
  206. class CInfoBar : public CIntObject
  207. {
  208. //all visible information located in one object - for ease of replacing
  209. class CVisibleInfo : public CIntObject
  210. {
  211. public:
  212. void show(SDL_Surface * to) override;
  213. protected:
  214. std::shared_ptr<CPicture> background;
  215. std::list<std::shared_ptr<CIntObject>> forceRefresh;
  216. CVisibleInfo();
  217. };
  218. class EmptyVisibleInfo : public CVisibleInfo
  219. {
  220. public:
  221. EmptyVisibleInfo();
  222. };
  223. class VisibleHeroInfo : public CVisibleInfo
  224. {
  225. std::shared_ptr<CHeroTooltip> heroTooltip;
  226. public:
  227. VisibleHeroInfo(const CGHeroInstance * hero);
  228. };
  229. class VisibleTownInfo : public CVisibleInfo
  230. {
  231. std::shared_ptr<CTownTooltip> townTooltip;
  232. public:
  233. VisibleTownInfo(const CGTownInstance * town);
  234. };
  235. class VisibleDateInfo : public CVisibleInfo
  236. {
  237. std::shared_ptr<CShowableAnim> animation;
  238. std::shared_ptr<CLabel> label;
  239. std::string getNewDayName();
  240. public:
  241. VisibleDateInfo();
  242. };
  243. class VisibleEnemyTurnInfo : public CVisibleInfo
  244. {
  245. std::shared_ptr<CAnimImage> banner;
  246. std::shared_ptr<CShowableAnim> glass;
  247. std::shared_ptr<CShowableAnim> sand;
  248. public:
  249. VisibleEnemyTurnInfo(PlayerColor player);
  250. };
  251. class VisibleGameStatusInfo : public CVisibleInfo
  252. {
  253. std::shared_ptr<CLabel> allyLabel;
  254. std::shared_ptr<CLabel> enemyLabel;
  255. std::vector<std::shared_ptr<CAnimImage>> flags;
  256. std::vector<std::shared_ptr<CAnimImage>> hallIcons;
  257. std::vector<std::shared_ptr<CLabel>> hallLabels;
  258. public:
  259. VisibleGameStatusInfo();
  260. };
  261. class VisibleComponentInfo : public CVisibleInfo
  262. {
  263. std::shared_ptr<CComponent> comp;
  264. std::shared_ptr<CTextBox> text;
  265. public:
  266. VisibleComponentInfo(const Component & compToDisplay, std::string message);
  267. };
  268. enum EState
  269. {
  270. EMPTY, HERO, TOWN, DATE, GAME, AITURN, COMPONENT
  271. };
  272. std::shared_ptr<CVisibleInfo> visibleInfo;
  273. EState state;
  274. //removes all information about current state, deactivates timer (if any)
  275. void reset();
  276. void tick() override;
  277. void clickLeft(tribool down, bool previousState) override;
  278. void clickRight(tribool down, bool previousState) override;
  279. void hover(bool on) override;
  280. void playNewDaySound();
  281. public:
  282. CInfoBar(const Rect & pos);
  283. /// show new day/week animation
  284. void showDate();
  285. /// show component for 3 seconds. Used to display picked up resources
  286. void showComponent(const Component & comp, std::string message);
  287. /// print enemy turn progress
  288. void startEnemyTurn(PlayerColor color);
  289. /// reset to default view - selected object
  290. void showSelection();
  291. /// show hero\town information
  292. void showHeroSelection(const CGHeroInstance * hero);
  293. void showTownSelection(const CGTownInstance * town);
  294. /// for 3 seconds shows amount of town halls and players status
  295. void showGameStatus();
  296. };
  297. /// simple panel that contains other displayable elements; used to separate groups of controls
  298. class CAdvMapPanel : public CIntObject
  299. {
  300. std::vector<std::shared_ptr<CButton>> colorableButtons;
  301. std::vector<std::shared_ptr<CIntObject>> otherObjects;
  302. /// the surface passed to this obj will be freed in dtor
  303. SDL_Surface * background;
  304. public:
  305. CAdvMapPanel(SDL_Surface * bg, Point position);
  306. virtual ~CAdvMapPanel();
  307. void addChildToPanel(std::shared_ptr<CIntObject> obj, ui8 actions = 0);
  308. void addChildColorableButton(std::shared_ptr<CButton> button);
  309. /// recolors all buttons to given player color
  310. void setPlayerColor(const PlayerColor & clr);
  311. void showAll(SDL_Surface * to) override;
  312. };
  313. /// specialized version of CAdvMapPanel that handles recolorable def-based pictures for world view info panel
  314. class CAdvMapWorldViewPanel : public CAdvMapPanel
  315. {
  316. /// data that allows reconstruction of panel info icons
  317. std::vector<std::pair<int, Point>> iconsData;
  318. /// ptrs to child-pictures constructed from iconsData
  319. std::vector<std::shared_ptr<CAnimImage>> currentIcons;
  320. /// surface drawn below world view panel on higher resolutions (won't be needed when world view panel is configured for extraResolutions mod)
  321. std::shared_ptr<CFilledTexture> backgroundFiller;
  322. std::shared_ptr<CAnimation> icons;
  323. public:
  324. CAdvMapWorldViewPanel(std::shared_ptr<CAnimation> _icons, SDL_Surface * bg, Point position, int spaceBottom, const PlayerColor &color);
  325. virtual ~CAdvMapWorldViewPanel();
  326. void addChildIcon(std::pair<int, Point> data, int indexOffset);
  327. /// recreates all pictures from given def to recolor them according to current player color
  328. void recolorIcons(const PlayerColor & color, int indexOffset);
  329. };
  330. class CInGameConsole : public CIntObject
  331. {
  332. private:
  333. std::list< std::pair< std::string, Uint32 > > texts; //list<text to show, time of add>
  334. boost::mutex texts_mx; // protects texts
  335. std::vector< std::string > previouslyEntered; //previously entered texts, for up/down arrows to work
  336. int prevEntDisp; //displayed entry from previouslyEntered - if none it's -1
  337. int defaultTimeout; //timeout for new texts (in ms)
  338. int maxDisplayedTexts; //hiw many texts can be displayed simultaneously
  339. public:
  340. std::string enteredText;
  341. void show(SDL_Surface * to) override;
  342. void print(const std::string &txt);
  343. void keyPressed (const SDL_KeyboardEvent & key) override; //call-in
  344. void textInputed(const SDL_TextInputEvent & event) override;
  345. void textEdited(const SDL_TextEditingEvent & event) override;
  346. void startEnteringText();
  347. void endEnteringText(bool printEnteredText);
  348. void refreshEnteredText();
  349. CInGameConsole();
  350. };