GUIClasses.h 16 KB

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