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