GUIClasses.h 17 KB

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