AdventureMapClasses.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #pragma once
  2. #include "gui/CIntObject.h"
  3. #include "gui/CIntObjectClasses.h"
  4. class CArmedInstance;
  5. class CShowableAnim;
  6. class CGGarrison;
  7. class CGObjectInstance;
  8. class CGHeroInstance;
  9. class CGTownInstance;
  10. struct Component;
  11. struct InfoAboutArmy;
  12. struct InfoAboutHero;
  13. struct InfoAboutTown;
  14. /*
  15. * CAdventureMapClasses.h, part of VCMI engine
  16. *
  17. * Authors: listed in file AUTHORS in main folder
  18. *
  19. * License: GNU General Public License v2.0 or later
  20. * Full text of license available in license.txt file, in main folder
  21. *
  22. */
  23. /// Base UI Element for hero\town lists
  24. class CList : public CIntObject
  25. {
  26. protected:
  27. class CListItem : public CIntObject
  28. {
  29. CList * parent;
  30. CIntObject * selection;
  31. public:
  32. CListItem(CList * parent);
  33. ~CListItem();
  34. void clickRight(tribool down, bool previousState);
  35. void clickLeft(tribool down, bool previousState);
  36. void hover(bool on);
  37. void onSelect(bool on);
  38. /// create object with selection rectangle
  39. virtual CIntObject * genSelection()=0;
  40. /// reaction on item selection (e.g. enable selection border)
  41. /// NOTE: item may be deleted in selected state
  42. virtual void select(bool on)=0;
  43. /// open item (town or hero screen)
  44. virtual void open()=0;
  45. /// show right-click tooltip
  46. virtual void showTooltip()=0;
  47. /// get hover text for status bar
  48. virtual std::string getHoverText()=0;
  49. };
  50. CListBox * list;
  51. const size_t size;
  52. /**
  53. * @brief CList - protected constructor
  54. * @param Size - maximal amount of visible at once items
  55. * @param position - cordinates
  56. * @param btnUp - path to image to use as top button
  57. * @param btnDown - path to image to use as bottom button
  58. * @param listAmount - amount of items in the list
  59. * @param helpUp - index in zelp.txt for button help tooltip
  60. * @param helpDown - index in zelp.txt for button help tooltip
  61. * @param create - function for creating items in listbox
  62. * @param destroy - function for deleting items in listbox
  63. */
  64. CList(int size, Point position, std::string btnUp, std::string btnDown, size_t listAmount, int helpUp, int helpDown,
  65. CListBox::CreateFunc create, CListBox::DestroyFunc destroy = CListBox::DestroyFunc());
  66. //for selection\deselection
  67. CListItem *selected;
  68. void select(CListItem * which);
  69. friend class CListItem;
  70. /// should be called when list is invalidated
  71. void update();
  72. public:
  73. CAdventureMapButton * scrollUp;
  74. CAdventureMapButton * scrollDown;
  75. /// functions that will be called when selection changes
  76. CFunctionList<void()> onSelect;
  77. /// return index of currently selected element
  78. int getSelectedIndex();
  79. /// set of methods to switch selection
  80. void selectIndex(int which);
  81. void selectNext();
  82. void selectPrev();
  83. };
  84. /// List of heroes which is shown at the right of the adventure map screen
  85. class CHeroList : public CList
  86. {
  87. /// Empty hero item used as placeholder for unused entries in list
  88. class CEmptyHeroItem : public CIntObject
  89. {
  90. public:
  91. CEmptyHeroItem();
  92. };
  93. class CHeroItem : public CListItem
  94. {
  95. CAnimImage * movement;
  96. CAnimImage * mana;
  97. CAnimImage * portrait;
  98. public:
  99. const CGHeroInstance * const hero;
  100. CHeroItem(CHeroList *parent, const CGHeroInstance * hero);
  101. CIntObject * genSelection();
  102. void update();
  103. void select(bool on);
  104. void open();
  105. void showTooltip();
  106. std::string getHoverText();
  107. };
  108. CIntObject * createHeroItem(size_t index);
  109. public:
  110. /**
  111. * @brief CHeroList
  112. * @param Size, position, btnUp, btnDown @see CList::CList
  113. */
  114. CHeroList(int size, Point position, std::string btnUp, std::string btnDown);
  115. /// Select specific hero and scroll if needed
  116. void select(const CGHeroInstance * hero = nullptr);
  117. /// Update hero. Will add or remove it from the list if needed
  118. void update(const CGHeroInstance * hero = nullptr);
  119. };
  120. /// List of towns which is shown at the right of the adventure map screen or in the town screen
  121. class CTownList : public CList
  122. {
  123. class CTownItem : public CListItem
  124. {
  125. CAnimImage * picture;
  126. public:
  127. const CGTownInstance * const town;
  128. CTownItem(CTownList *parent, const CGTownInstance * town);
  129. CIntObject * genSelection();
  130. void update();
  131. void select(bool on);
  132. void open();
  133. void showTooltip();
  134. std::string getHoverText();
  135. };
  136. CIntObject * createTownItem(size_t index);
  137. public:
  138. /**
  139. * @brief CTownList
  140. * @param Size, position, btnUp, btnDown @see CList::CList
  141. */
  142. CTownList(int size, Point position, std::string btnUp, std::string btnDown);
  143. /// Select specific town and scroll if needed
  144. void select(const CGTownInstance * town = nullptr);
  145. /// Update town. Will add or remove it from the list if needed
  146. void update(const CGTownInstance * town = nullptr);
  147. };
  148. class CMinimap;
  149. class CMinimapInstance : public CIntObject
  150. {
  151. CMinimap *parent;
  152. SDL_Surface * minimap;
  153. int level;
  154. //get color of selected tile on minimap
  155. const SDL_Color & getTileColor(const int3 & pos);
  156. void blitTileWithColor(const SDL_Color & color, const int3 & pos, SDL_Surface *to, int x, int y);
  157. //draw minimap already scaled.
  158. //result is not antialiased. Will result in "missing" pixels on huge maps (>144)
  159. void drawScaled(int level);
  160. public:
  161. CMinimapInstance(CMinimap * parent, int level);
  162. ~CMinimapInstance();
  163. void showAll(SDL_Surface *to);
  164. void tileToPixels (const int3 &tile, int &x, int &y,int toX = 0, int toY = 0);
  165. void refreshTile(const int3 &pos);
  166. };
  167. /// Minimap which is displayed at the right upper corner of adventure map
  168. class CMinimap : public CIntObject
  169. {
  170. protected:
  171. CPicture *aiShield; //the graphic displayed during AI turn
  172. CMinimapInstance * minimap;
  173. int level;
  174. //to initialize colors
  175. std::map<int, std::pair<SDL_Color, SDL_Color> > loadColors(std::string from);
  176. void clickLeft(tribool down, bool previousState);
  177. void clickRight(tribool down, bool previousState);
  178. void hover (bool on);
  179. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  180. void moveAdvMapSelection();
  181. public:
  182. // terrainID -> (normal color, blocked color)
  183. const std::map<int, std::pair<SDL_Color, SDL_Color> > colors;
  184. CMinimap(const Rect & position);
  185. //should be called to invalidate whole map - different player or level
  186. int3 translateMousePosition();
  187. void update();
  188. void setLevel(int level);
  189. void setAIRadar(bool on);
  190. void showAll(SDL_Surface * to);
  191. void hideTile(const int3 &pos); //puts FoW
  192. void showTile(const int3 &pos); //removes FoW
  193. };
  194. /// Info box which shows next week/day information, hold the current date
  195. class CInfoBar : public CIntObject
  196. {
  197. //all visible information located in one object - for ease of replacing
  198. class CVisibleInfo : public CIntObject
  199. {
  200. //list of objects that must be redrawed on each frame on a top of animation
  201. std::list<CIntObject *> forceRefresh;
  202. //the only part of gui we need to know about for updating - AI progress
  203. CAnimImage *aiProgress;
  204. std::string getNewDayName();
  205. void playNewDaySound();
  206. public:
  207. CVisibleInfo(Point position);
  208. void show(SDL_Surface *to);
  209. //functions that must be called only once
  210. void loadHero(const CGHeroInstance * hero);
  211. void loadTown(const CGTownInstance * town);
  212. void loadDay();
  213. void loadEnemyTurn(PlayerColor player);
  214. void loadGameStatus();
  215. void loadComponent(const Component &comp, std::string message);
  216. //can be called multiple times
  217. void updateEnemyTurn(double progress);
  218. };
  219. enum EState
  220. {
  221. EMPTY, HERO, TOWN, DATE, GAME, AITURN, COMPONENT
  222. };
  223. CVisibleInfo * visibleInfo;
  224. EState state;
  225. //currently displayed object. May be null if state is not hero or town
  226. const CGObjectInstance * currentObject;
  227. //removes all information about current state, deactivates timer (if any)
  228. void reset(EState newState);
  229. void tick();
  230. void clickLeft(tribool down, bool previousState);
  231. void clickRight(tribool down, bool previousState);
  232. void hover(bool on);
  233. public:
  234. CInfoBar(const Rect & pos);
  235. /// show new day/week animation
  236. void showDate();
  237. /// show component for 3 seconds. Used to display picked up resources
  238. void showComponent(const Component & comp, std::string message);
  239. /// print enemy turn progress
  240. void startEnemyTurn(PlayerColor color);
  241. /// updates enemy turn.
  242. /// NOTE: currently DISABLED. Check comments in CInfoBar::CVisibleInfo::loadEnemyTurn()
  243. void updateEnemyTurn(double progress);
  244. /// reset to default view - selected object
  245. void showSelection();
  246. /// show hero\town information
  247. void showHeroSelection(const CGHeroInstance * hero);
  248. void showTownSelection(const CGTownInstance * town);
  249. /// for 3 seconds shows amount of town halls and players status
  250. void showGameStatus();
  251. };