AdventureMapClasses.h 12 KB

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