AdventureMapClasses.h 11 KB

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