GUIClasses.h 16 KB

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