AdventureMapClasses.h 11 KB

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