AdventureMapClasses.h 11 KB

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