AdventureMapClasses.h 12 KB

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