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