GUIClasses.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * GUIClasses.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 "CWindowObject.h"
  12. #include "../lib/ResourceSet.h"
  13. #include "../widgets/Images.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CGHeroInstance;
  16. class CGObjectInstance;
  17. class CGDwelling;
  18. class IMarket;
  19. VCMI_LIB_NAMESPACE_END
  20. class CButton;
  21. class LRClickableArea;
  22. class CreatureCostBox;
  23. class CCreaturePic;
  24. class CMinorResDataBar;
  25. class MoraleLuckBox;
  26. class CHeroArea;
  27. class CSlider;
  28. class CComponentBox;
  29. class CTextInput;
  30. class CListBox;
  31. class CLabelGroup;
  32. class CGStatusBar;
  33. class CTextBox;
  34. class CGarrisonInt;
  35. class CGarrisonSlot;
  36. class CHeroArea;
  37. class CAnimImage;
  38. class CFilledTexture;
  39. class IImage;
  40. enum class EUserEvent;
  41. /// Recruitment window where you can recruit creatures
  42. class CRecruitmentWindow : public CStatusbarWindow
  43. {
  44. class CCreatureCard : public CIntObject, public std::enable_shared_from_this<CCreatureCard>
  45. {
  46. CRecruitmentWindow * parent;
  47. std::shared_ptr<CCreaturePic> animation;
  48. bool selected;
  49. public:
  50. const CCreature * creature;
  51. si32 amount;
  52. void select(bool on);
  53. CCreatureCard(CRecruitmentWindow * window, const CCreature * crea, int totalAmount);
  54. void clickPressed(const Point & cursorPosition) override;
  55. void showPopupWindow(const Point & cursorPosition) override;
  56. void showAll(Canvas & to) override;
  57. };
  58. std::function<void(CreatureID,int)> onRecruit; //void (int ID, int amount) <-- call to recruit creatures
  59. std::function<void()> onClose;
  60. int level;
  61. const CArmedInstance * dst;
  62. std::shared_ptr<CCreatureCard> selected;
  63. std::vector<std::shared_ptr<CCreatureCard>> cards;
  64. std::shared_ptr<CSlider> slider;
  65. std::shared_ptr<CButton> maxButton;
  66. std::shared_ptr<CButton> buyButton;
  67. std::shared_ptr<CButton> cancelButton;
  68. std::shared_ptr<CLabel> title;
  69. std::shared_ptr<CLabel> availableValue;
  70. std::shared_ptr<CLabel> toRecruitValue;
  71. std::shared_ptr<CLabel> availableTitle;
  72. std::shared_ptr<CLabel> toRecruitTitle;
  73. std::shared_ptr<CreatureCostBox> costPerTroopValue;
  74. std::shared_ptr<CreatureCostBox> totalCostValue;
  75. void select(std::shared_ptr<CCreatureCard> card);
  76. void buy();
  77. void sliderMoved(int to);
  78. void showAll(Canvas & to) override;
  79. public:
  80. const CGDwelling * const dwelling;
  81. CRecruitmentWindow(const CGDwelling * Dwelling, int Level, const CArmedInstance * Dst, const std::function<void(CreatureID,int)> & Recruit, const std::function<void()> & onClose, int y_offset = 0);
  82. void availableCreaturesChanged();
  83. void close() override;
  84. };
  85. /// Split window where creatures can be split up into two single unit stacks
  86. class CSplitWindow : public CWindowObject
  87. {
  88. std::function<void(int, int)> callback;
  89. int leftAmount;
  90. int rightAmount;
  91. int leftMin;
  92. int rightMin;
  93. std::shared_ptr<CLabel> title;
  94. std::shared_ptr<CSlider> slider;
  95. std::shared_ptr<CCreaturePic> animLeft;
  96. std::shared_ptr<CCreaturePic> animRight;
  97. std::shared_ptr<CButton> ok;
  98. std::shared_ptr<CButton> cancel;
  99. std::shared_ptr<CTextInput> leftInput;
  100. std::shared_ptr<CTextInput> rightInput;
  101. void setAmountText(std::string text, bool left);
  102. void setAmount(int value, bool left);
  103. void sliderMoved(int value);
  104. void apply();
  105. public:
  106. /**
  107. * creature - displayed creature
  108. * callback(leftAmount, rightAmount) - function to call on close
  109. * leftMin, rightMin - minimal amount of creatures in each stack
  110. * leftAmount, rightAmount - amount of creatures in each stack
  111. */
  112. CSplitWindow(const CCreature * creature, std::function<void(int, int)> callback, int leftMin, int rightMin, int leftAmount, int rightAmount);
  113. };
  114. /// Raised up level window where you can select one out of two skills
  115. class CLevelWindow : public CWindowObject
  116. {
  117. std::shared_ptr<CHeroArea> portrait;
  118. std::shared_ptr<CButton> ok;
  119. std::shared_ptr<CLabel> mainTitle;
  120. std::shared_ptr<CLabel> levelTitle;
  121. std::shared_ptr<CAnimImage> skillIcon;
  122. std::shared_ptr<CLabel> skillValue;
  123. std::shared_ptr<CComponentBox> box; //skills to select
  124. std::function<void(ui32)> cb;
  125. void selectionChanged(unsigned to);
  126. public:
  127. CLevelWindow(const CGHeroInstance *hero, PrimarySkill pskill, std::vector<SecondarySkill> &skills, std::function<void(ui32)> callback);
  128. void close() override;
  129. };
  130. /// Town portal, castle gate window
  131. class CObjectListWindow : public CWindowObject
  132. {
  133. class CItem : public CIntObject
  134. {
  135. CObjectListWindow * parent;
  136. std::shared_ptr<CLabel> text;
  137. std::shared_ptr<CPicture> border;
  138. std::shared_ptr<CPicture> icon;
  139. public:
  140. const size_t index;
  141. CItem(CObjectListWindow * parent, size_t id, std::string text);
  142. void select(bool on);
  143. void clickPressed(const Point & cursorPosition) override;
  144. void clickDouble(const Point & cursorPosition) override;
  145. void showPopupWindow(const Point & cursorPosition) override;
  146. };
  147. std::function<void(int)> onSelect;//called when OK button is pressed, returns id of selected item.
  148. std::shared_ptr<CIntObject> titleWidget;
  149. std::shared_ptr<CLabel> title;
  150. std::shared_ptr<CLabel> descr;
  151. std::vector<std::shared_ptr<IImage>> images;
  152. std::shared_ptr<CListBox> list;
  153. std::shared_ptr<CButton> ok;
  154. std::shared_ptr<CButton> exit;
  155. std::vector< std::pair<int, std::string> > items;//all items present in list
  156. void init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr);
  157. void exitPressed();
  158. public:
  159. size_t selected;//index of currently selected item
  160. std::function<void()> onExit;//optional exit callback
  161. std::function<void(int)> onPopup;//optional popup callback
  162. std::function<void(int)> onClicked;//optional if clicked on item callback
  163. /// Callback will be called when OK button is pressed, returns id of selected item. initState = initially selected item
  164. /// Image can be nullptr
  165. ///item names will be taken from map objects
  166. CObjectListWindow(const std::vector<int> &_items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection = 0, std::vector<std::shared_ptr<IImage>> images = {});
  167. CObjectListWindow(const std::vector<std::string> &_items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection = 0, std::vector<std::shared_ptr<IImage>> images = {});
  168. std::shared_ptr<CIntObject> genItem(size_t index);
  169. void elementSelected();//call callback and close this window
  170. void changeSelection(size_t which);
  171. void keyPressed(EShortcut key) override;
  172. };
  173. class CTavernWindow : public CStatusbarWindow
  174. {
  175. std::function<void()> onWindowClosed;
  176. public:
  177. class HeroPortrait : public CIntObject
  178. {
  179. public:
  180. std::string hoverName;
  181. std::string description; // "XXX is a level Y ZZZ with N artifacts"
  182. const CGHeroInstance * h;
  183. std::function<void()> onChoose;
  184. void clickPressed(const Point & cursorPosition) override;
  185. void clickDouble(const Point & cursorPosition) override;
  186. void showPopupWindow(const Point & cursorPosition) override;
  187. void hover (bool on) override;
  188. HeroPortrait(int & sel, int id, int x, int y, const CGHeroInstance * H, std::function<void()> OnChoose = nullptr);
  189. private:
  190. int *_sel;
  191. const int _id;
  192. std::shared_ptr<CAnimImage> portrait;
  193. };
  194. class HeroSelector : public CWindowObject
  195. {
  196. public:
  197. std::shared_ptr<CFilledTexture> background;
  198. HeroSelector(std::map<HeroTypeID, CGHeroInstance*> InviteableHeroes, std::function<void(CGHeroInstance*)> OnChoose);
  199. private:
  200. std::map<HeroTypeID, CGHeroInstance*> inviteableHeroes;
  201. std::function<void(CGHeroInstance*)> onChoose;
  202. std::vector<std::shared_ptr<CAnimImage>> portraits;
  203. std::vector<std::shared_ptr<LRClickableArea>> portraitAreas;
  204. };
  205. //recruitable heroes
  206. std::shared_ptr<HeroPortrait> h1;
  207. std::shared_ptr<HeroPortrait> h2; //recruitable heroes
  208. int selected;//0 (left) or 1 (right)
  209. int oldSelected;//0 (left) or 1 (right)
  210. std::shared_ptr<CButton> thiefGuild;
  211. std::shared_ptr<CButton> cancel;
  212. std::shared_ptr<CButton> recruit;
  213. const CGObjectInstance * tavernObj;
  214. std::shared_ptr<CLabel> title;
  215. std::shared_ptr<CLabel> cost;
  216. std::shared_ptr<CLabel> heroesForHire;
  217. std::shared_ptr<CTextBox> heroDescription;
  218. std::shared_ptr<CTextBox> rumor;
  219. std::shared_ptr<CLabel> inviteHero;
  220. std::shared_ptr<CAnimImage> inviteHeroImage;
  221. std::shared_ptr<LRClickableArea> inviteHeroImageArea;
  222. std::map<HeroTypeID, CGHeroInstance*> inviteableHeroes;
  223. CGHeroInstance* heroToInvite;
  224. void addInvite();
  225. CTavernWindow(const CGObjectInstance * TavernObj, const std::function<void()> & onWindowClosed);
  226. ~CTavernWindow();
  227. void close() override;
  228. void recruitb();
  229. void thievesguildb();
  230. void show(Canvas & to) override;
  231. };
  232. /// Here you can buy ships
  233. class CShipyardWindow : public CStatusbarWindow
  234. {
  235. std::shared_ptr<CPicture> bgWater;
  236. std::shared_ptr<CAnimImage> bgShip;
  237. std::shared_ptr<CLabel> title;
  238. std::shared_ptr<CLabel> costLabel;
  239. std::shared_ptr<CAnimImage> woodPic;
  240. std::shared_ptr<CAnimImage> goldPic;
  241. std::shared_ptr<CLabel> woodCost;
  242. std::shared_ptr<CLabel> goldCost;
  243. std::shared_ptr<CButton> build;
  244. std::shared_ptr<CButton> quit;
  245. public:
  246. CShipyardWindow(const TResources & cost, int state, BoatId boatType, const std::function<void()> & onBuy);
  247. };
  248. /// Creature transformer window
  249. class CTransformerWindow : public CStatusbarWindow, public IGarrisonHolder
  250. {
  251. class CItem : public CIntObject
  252. {
  253. public:
  254. int id;//position of creature in hero army
  255. bool left;//position of the item
  256. int size; //size of creature stack
  257. CTransformerWindow * parent;
  258. std::shared_ptr<CAnimImage> icon;
  259. std::shared_ptr<CLabel> count;
  260. void move();
  261. void clickPressed(const Point & cursorPosition) override;
  262. void update();
  263. CItem(CTransformerWindow * parent, int size, int id);
  264. };
  265. const CArmedInstance * army;//object with army for transforming (hero or town)
  266. const CGHeroInstance * hero;//only if we have hero in town
  267. const IMarket * market;//market, town garrison is used if hero == nullptr
  268. std::shared_ptr<CLabel> titleLeft;
  269. std::shared_ptr<CLabel> titleRight;
  270. std::shared_ptr<CTextBox> helpLeft;
  271. std::shared_ptr<CTextBox> helpRight;
  272. std::vector<std::shared_ptr<CItem>> items;
  273. std::shared_ptr<CButton> all;
  274. std::shared_ptr<CButton> convert;
  275. std::shared_ptr<CButton> cancel;
  276. std::function<void()> onWindowClosed;
  277. public:
  278. void makeDeal();
  279. void addAll();
  280. void close() override;
  281. void updateGarrisons() override;
  282. bool holdsGarrison(const CArmedInstance * army) override;
  283. CTransformerWindow(const IMarket * _market, const CGHeroInstance * _hero, const std::function<void()> & onWindowClosed);
  284. };
  285. class CUniversityWindow : public CStatusbarWindow
  286. {
  287. class CItem : public CIntObject
  288. {
  289. std::shared_ptr<CAnimImage> icon;
  290. std::shared_ptr<CAnimImage> topBar;
  291. std::shared_ptr<CAnimImage> bottomBar;
  292. std::shared_ptr<CLabel> name;
  293. std::shared_ptr<CLabel> level;
  294. public:
  295. SecondarySkill ID;//id of selected skill
  296. CUniversityWindow * parent;
  297. void showAll(Canvas & to) override;
  298. void clickPressed(const Point & cursorPosition) override;
  299. void showPopupWindow(const Point & cursorPosition) override;
  300. void hover(bool on) override;
  301. int state();//0=can't learn, 1=learned, 2=can learn
  302. CItem(CUniversityWindow * _parent, int _ID, int X, int Y);
  303. };
  304. const CGHeroInstance * hero;
  305. const IMarket * market;
  306. std::shared_ptr<CAnimation> bars;
  307. std::vector<std::shared_ptr<CItem>> items;
  308. std::shared_ptr<CButton> cancel;
  309. std::shared_ptr<CIntObject> titlePic;
  310. std::shared_ptr<CLabel> title;
  311. std::shared_ptr<CTextBox> clerkSpeech;
  312. std::function<void()> onWindowClosed;
  313. public:
  314. CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market, const std::function<void()> & onWindowClosed);
  315. void makeDeal(SecondarySkill skill);
  316. void close();
  317. };
  318. /// Confirmation window for University
  319. class CUnivConfirmWindow : public CStatusbarWindow
  320. {
  321. std::shared_ptr<CTextBox> clerkSpeech;
  322. std::shared_ptr<CLabel> name;
  323. std::shared_ptr<CLabel> level;
  324. std::shared_ptr<CAnimImage> icon;
  325. CUniversityWindow * owner;
  326. std::shared_ptr<CButton> confirm;
  327. std::shared_ptr<CButton> cancel;
  328. std::shared_ptr<CAnimImage> costIcon;
  329. std::shared_ptr<CLabel> cost;
  330. void makeDeal(SecondarySkill skill);
  331. public:
  332. CUnivConfirmWindow(CUniversityWindow * PARENT, SecondarySkill SKILL, bool available);
  333. };
  334. /// Garrison window where you can take creatures out of the hero to place it on the garrison
  335. class CGarrisonWindow : public CWindowObject, public IGarrisonHolder
  336. {
  337. std::shared_ptr<CLabel> title;
  338. std::shared_ptr<CAnimImage> banner;
  339. std::shared_ptr<CAnimImage> portrait;
  340. std::shared_ptr<CGarrisonInt> garr;
  341. public:
  342. std::shared_ptr<CButton> quit;
  343. CGarrisonWindow(const CArmedInstance * up, const CGHeroInstance * down, bool removableUnits);
  344. void updateGarrisons() override;
  345. bool holdsGarrison(const CArmedInstance * army) override;
  346. };
  347. /// Hill fort is the building where you can upgrade units
  348. class CHillFortWindow : public CStatusbarWindow, public IGarrisonHolder
  349. {
  350. private:
  351. static const int slotsCount = 7;
  352. //todo: mithril support
  353. static const int resCount = 7;
  354. const CGObjectInstance * fort;
  355. const CGHeroInstance * hero;
  356. std::shared_ptr<CLabel> title;
  357. std::shared_ptr<CHeroArea> heroPic;
  358. std::array<std::shared_ptr<CAnimImage>, resCount> totalIcons;
  359. std::array<std::shared_ptr<CLabel>, resCount> totalLabels;
  360. std::array<std::shared_ptr<CButton>, slotsCount> upgrade;//upgrade single creature
  361. std::array<int, slotsCount + 1> currState;//current state of slot - to avoid calls to getState or updating buttons
  362. //there is a place for only 2 resources per slot
  363. std::array< std::array<std::shared_ptr<CAnimImage>, 2>, slotsCount> slotIcons;
  364. std::array< std::array<std::shared_ptr<CLabel>, 2>, slotsCount> slotLabels;
  365. std::shared_ptr<CButton> upgradeAll;
  366. std::shared_ptr<CButton> quit;
  367. std::shared_ptr<CGarrisonInt> garr;
  368. std::string getDefForSlot(SlotID slot);
  369. std::string getTextForSlot(SlotID slot);
  370. void makeDeal(SlotID slot);//-1 for upgrading all creatures
  371. int getState(SlotID slot); //-1 = no creature 0=can't upgrade, 1=upgraded, 2=can upgrade
  372. public:
  373. CHillFortWindow(const CGHeroInstance * visitor, const CGObjectInstance * object);
  374. void updateGarrisons() override;//update buttons after garrison changes
  375. bool holdsGarrison(const CArmedInstance * army) override;
  376. };
  377. class CThievesGuildWindow : public CStatusbarWindow
  378. {
  379. const CGObjectInstance * owner;
  380. std::shared_ptr<CButton> exitb;
  381. std::shared_ptr<CMinorResDataBar> resdatabar;
  382. std::vector<std::shared_ptr<CLabel>> rowHeaders;
  383. std::vector<std::shared_ptr<CAnimImage>> columnBackgrounds;
  384. std::vector<std::shared_ptr<CLabel>> columnHeaders;
  385. std::vector<std::shared_ptr<CAnimImage>> cells;
  386. std::vector<std::shared_ptr<CPicture>> banners;
  387. std::vector<std::shared_ptr<CAnimImage>> bestHeroes;
  388. std::vector<std::shared_ptr<CTextBox>> primSkillHeaders;
  389. std::vector<std::shared_ptr<CLabel>> primSkillValues;
  390. std::vector<std::shared_ptr<CAnimImage>> bestCreatures;
  391. std::vector<std::shared_ptr<CLabel>> personalities;
  392. public:
  393. CThievesGuildWindow(const CGObjectInstance * _owner);
  394. };