AdventureMapClasses.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #pragma once
  2. #include "UIFramework/CIntObject.h"
  3. #include "UIFramework/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 refreshTile(const int3 &pos);
  165. };
  166. /// Minimap which is displayed at the right upper corner of adventure map
  167. class CMinimap : public CIntObject
  168. {
  169. CPicture *aiShield; //the graphic displayed during AI turn
  170. CMinimapInstance * minimap;
  171. int level;
  172. //to initialize colors
  173. std::map<int, std::pair<SDL_Color, SDL_Color> > loadColors(std::string from);
  174. void clickLeft(tribool down, bool previousState);
  175. void clickRight(tribool down, bool previousState);
  176. void hover (bool on);
  177. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  178. protected:
  179. void moveAdvMapSelection();
  180. public:
  181. // terrainID -> (normal color, blocked color)
  182. const std::map<int, std::pair<SDL_Color, SDL_Color> > colors;
  183. CMinimap(const Rect & position);
  184. //should be called to invalidate whole map - different player or level
  185. void update();
  186. void setLevel(int level);
  187. void setAIRadar(bool on);
  188. void showAll(SDL_Surface * to);
  189. void hideTile(const int3 &pos); //puts FoW
  190. void showTile(const int3 &pos); //removes FoW
  191. };
  192. /// Info box which shows next week/day information, hold the current date
  193. class CInfoBar : public CIntObject
  194. {
  195. //all visible information located in one object - for ease of replacing
  196. class CVisibleInfo : public CIntObject
  197. {
  198. //list of objects that must be redrawed on each frame on a top of animation
  199. std::list<CIntObject *> forceRefresh;
  200. //the only part of gui we need to know about for updating - AI progress
  201. CAnimImage *aiProgress;
  202. std::string getNewDayName();
  203. void playNewDaySound();
  204. public:
  205. CVisibleInfo(Point position);
  206. void show(SDL_Surface *to);
  207. //functions that must be called only once
  208. void loadHero(const CGHeroInstance * hero);
  209. void loadTown(const CGTownInstance * town);
  210. void loadDay();
  211. void loadEnemyTurn(int player);
  212. void loadGameStatus();
  213. void loadComponent(const Component comp, std::string message);
  214. //can be called multiple times
  215. void updateEnemyTurn(double progress);
  216. };
  217. enum EState
  218. {
  219. EMPTY, HERO, TOWN, DATE, GAME, AITURN, COMPONENT
  220. };
  221. CVisibleInfo * visibleInfo;
  222. EState state;
  223. //currently displayed object. May be null if state is not hero or town
  224. const CGObjectInstance * currentObject;
  225. //removes all information about current state, deactivates timer (if any)
  226. void reset(EState newState);
  227. void tick();
  228. void clickLeft(tribool down, bool previousState);
  229. void clickRight(tribool down, bool previousState);
  230. void hover(bool on);
  231. public:
  232. CInfoBar(const Rect & pos);
  233. /// show new day/week animation
  234. void showDate();
  235. /// show component for 3 seconds. Used to display picked up resources
  236. void showComponent(const Component comp, std::string message);
  237. /// print enemy turn progress
  238. void startEnemyTurn(ui8 color);
  239. /// updates enemy turn.
  240. /// NOTE: currently DISABLED. Check comments in CInfoBar::CVisibleInfo::loadEnemyTurn()
  241. void updateEnemyTurn(double progress);
  242. /// reset to default view - selected object
  243. void showSelection();
  244. /// show hero\town information
  245. void showHeroSelection(const CGHeroInstance * hero);
  246. void showTownSelection(const CGTownInstance * town);
  247. /// for 3 seconds shows amount of town halls and players status
  248. void showGameStatus();
  249. };