GUIClasses.h 16 KB

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