AdventureMapClasses.h 12 KB

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