GUIClasses.h 15 KB

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