GUIClasses.h 17 KB

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