GUIClasses.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. #pragma once
  2. #include "CAnimation.h"
  3. #include "FunctionList.h"
  4. #include "../lib/ResourceSet.h"
  5. #include "../lib/CConfigHandler.h"
  6. #include "../lib/GameConstants.h"
  7. #include "UIFramework/CIntObject.h"
  8. #include "UIFramework/CIntObjectClasses.h"
  9. #ifdef max
  10. #undef max
  11. #endif
  12. #ifdef min
  13. #undef min
  14. #endif
  15. /*
  16. * GUIClasses.h, part of VCMI engine
  17. *
  18. * Authors: listed in file AUTHORS in main folder
  19. *
  20. * License: GNU General Public License v2.0 or later
  21. * Full text of license available in license.txt file, in main folder
  22. *
  23. */
  24. struct ArtifactLocation;
  25. class CStackBasicDescriptor;
  26. class CBonusSystemNode;
  27. class CArtifact;
  28. class CDefEssential;
  29. class CAdventureMapButton;
  30. class CHighlightableButtonsGroup;
  31. class CDefHandler;
  32. struct HeroMoveDetails;
  33. class CDefEssential;
  34. class CGHeroInstance;
  35. class CAdvMapInt;
  36. class CCastleInterface;
  37. class CBattleInterface;
  38. class CStack;
  39. class CComponent;
  40. class CCreature;
  41. struct SDL_Surface;
  42. struct CPath;
  43. class CCreatureAnim;
  44. class CSelectableComponent;
  45. class CCreatureSet;
  46. class CGObjectInstance;
  47. class CGDwelling;
  48. class CSlider;
  49. struct UpgradeInfo;
  50. template <typename T> struct CondSh;
  51. class CInGameConsole;
  52. class CGarrisonInt;
  53. class CInGameConsole;
  54. struct Component;
  55. class CArmedInstance;
  56. class CGTownInstance;
  57. class StackState;
  58. class CPlayerInterface;
  59. class CHeroWindow;
  60. class CArtifact;
  61. class CArtifactsOfHero;
  62. class CCreatureArtifactSet;
  63. class CResDataBar;
  64. struct SPuzzleInfo;
  65. class CGGarrison;
  66. class CStackInstance;
  67. class IMarket;
  68. class CTextBox;
  69. class CArtifactInstance;
  70. class IBonusBearer;
  71. class CArtPlace;
  72. class CAnimImage;
  73. struct InfoAboutArmy;
  74. struct InfoAboutHero;
  75. struct InfoAboutTown;
  76. /// text + comp. + ok button
  77. class CInfoWindow : public CSimpleWindow
  78. { //window able to delete its components when closed
  79. bool delComps; //whether comps will be deleted
  80. public:
  81. typedef std::vector<std::pair<std::string,CFunctionList<void()> > > TButtonsInfo;
  82. typedef std::vector<CComponent*> TCompsInfo;
  83. int ID; //for identification
  84. CTextBox *text;
  85. std::vector<CAdventureMapButton *> buttons;
  86. std::vector<CComponent*> components;
  87. CSlider *slider;
  88. void setDelComps(bool DelComps);
  89. virtual void close();
  90. void show(SDL_Surface * to);
  91. void showAll(SDL_Surface * to);
  92. void sliderMoved(int to);
  93. CInfoWindow(std::string Text, int player, const TCompsInfo &comps = TCompsInfo(), const TButtonsInfo &Buttons = TButtonsInfo(), bool delComps = true); //c-tor
  94. CInfoWindow(); //c-tor
  95. ~CInfoWindow(); //d-tor
  96. static void showYesNoDialog( const std::string & text, const std::vector<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps = true, int player = 1); //use only before the game starts! (showYesNoDialog in LOCPLINT must be used then)
  97. static CInfoWindow *create(const std::string &text, int playerID = 1, const std::vector<CComponent*> *components = NULL, bool DelComps = false);
  98. /// create text from title and description: {title}\n\n description
  99. static std::string genText(std::string title, std::string description);
  100. };
  101. /// component selection window
  102. class CSelWindow : public CInfoWindow
  103. { //warning - this window deletes its components by closing!
  104. public:
  105. void selectionChange(unsigned to);
  106. void madeChoice(); //looks for selected component and calls callback
  107. CSelWindow(const std::string& text, int player, int charperline ,const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, int askID); //c-tor
  108. CSelWindow(){}; //c-tor
  109. //notification - this class inherits important destructor from CInfoWindow
  110. };
  111. /// popup displayed on R-click
  112. class CRClickPopup : public CIntObject
  113. {
  114. public:
  115. virtual void close();
  116. void clickRight(tribool down, bool previousState);
  117. CRClickPopup();
  118. virtual ~CRClickPopup(); //d-tor
  119. static CIntObject* createInfoWin(Point position, const CGObjectInstance * specific);
  120. static void createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps = CInfoWindow::TCompsInfo());
  121. static void createAndPush(const std::string &txt, CComponent * component);
  122. static void createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment = BOTTOMRIGHT);
  123. };
  124. /// popup displayed on R-click
  125. class CRClickPopupInt : public CRClickPopup
  126. {
  127. public:
  128. IShowActivatable *inner;
  129. bool delInner;
  130. void show(SDL_Surface * to);
  131. void showAll(SDL_Surface * to);
  132. CRClickPopupInt(IShowActivatable *our, bool deleteInt); //c-tor
  133. virtual ~CRClickPopupInt(); //d-tor
  134. };
  135. class CInfoPopup : public CRClickPopup
  136. {
  137. public:
  138. bool free; //TODO: comment me
  139. SDL_Surface * bitmap; //popup background
  140. void close();
  141. void show(SDL_Surface * to);
  142. CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free=false); //c-tor
  143. CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free=false); //c-tor
  144. CInfoPopup(SDL_Surface * Bitmap = NULL, bool Free = false); //default c-tor
  145. void init(int x, int y);
  146. ~CInfoPopup(); //d-tor
  147. };
  148. /// popup on adventure map for town\hero objects
  149. class CInfoBoxPopup : public CWindowObject
  150. {
  151. Point toScreen(Point pos);
  152. public:
  153. CInfoBoxPopup(Point position, const CGTownInstance * town);
  154. CInfoBoxPopup(Point position, const CGHeroInstance * hero);
  155. CInfoBoxPopup(Point position, const CGGarrison * garr);
  156. };
  157. /// common popup window component
  158. class CComponent : public virtual CIntObject
  159. {
  160. public:
  161. enum Etype
  162. {
  163. primskill, secskill, resource, creature, artifact, experience, spell, morale, luck, building, hero, flag, typeInvalid
  164. };
  165. //NOTE: not all types have exact these sizes or have less than 4 of them. In such cases closest one will be used
  166. enum ESize
  167. {
  168. tiny, // ~22-24px
  169. small, // ~30px
  170. medium,// ~42px
  171. large, // ~82px
  172. sizeInvalid
  173. };
  174. private:
  175. size_t getIndex();
  176. const std::vector<std::string> getFileName();
  177. void setSurface(std::string defName, int imgPos);
  178. std::string getSubtitleInternal();
  179. void init(Etype Type, int Subtype, int Val, ESize imageSize);
  180. public:
  181. CAnimImage *image; //our image
  182. Etype compType; //component type
  183. ESize size; //component size.
  184. int subtype; //type-dependant subtype. See getSomething methods for details
  185. int val; // value \ strength \ amount of component. See getSomething methods for details
  186. bool perDay; // add "per day" text to subtitle
  187. std::string getDescription();
  188. std::string getSubtitle();
  189. CComponent(Etype Type, int Subtype, int Val = 0, ESize imageSize=large);//c-tor
  190. CComponent(const Component &c); //c-tor
  191. void clickRight(tribool down, bool previousState); //call-in
  192. };
  193. /// component that can be selected or deselected
  194. class CSelectableComponent : public CComponent, public CKeyShortcut
  195. {
  196. void init();
  197. public:
  198. bool selected; //if true, this component is selected
  199. boost::function<void()> onSelect; //function called on selection change
  200. void showAll(SDL_Surface * to);
  201. void select(bool on);
  202. void clickLeft(tribool down, bool previousState); //call-in
  203. CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize=large, boost::function<void()> OnSelect = 0); //c-tor
  204. CSelectableComponent(const Component &c, boost::function<void()> OnSelect = 0); //c-tor
  205. };
  206. /// box with multiple components (up to 8?)
  207. /// will take ownership on components and delete them afterwards
  208. class CComponentBox : public CIntObject
  209. {
  210. std::vector<CComponent *> components;
  211. CSelectableComponent * selected;
  212. boost::function<void(int newID)> onSelect;
  213. void selectionChanged(CSelectableComponent * newSelection);
  214. //get position of "or" text between these comps
  215. //it will place "or" equidistant to both images
  216. Point getOrTextPos(CComponent *left, CComponent * right);
  217. //get distance between these copmonents
  218. int getDistance(CComponent *left, CComponent * right);
  219. void placeComponents(bool selectable);
  220. public:
  221. /// return index of selected item
  222. int selectedIndex();
  223. /// constructor for quite common 1-components popups
  224. /// if position width or height are 0 then it will be determined automatically
  225. CComponentBox(CComponent * components, Rect position);
  226. /// constructor for non-selectable components
  227. CComponentBox(std::vector<CComponent *> components, Rect position);
  228. /// constructor for selectable components
  229. /// will also create "or" labels between components
  230. /// onSelect - optional function that will be called every time on selection change
  231. CComponentBox(std::vector<CSelectableComponent *> components, Rect position, boost::function<void(int newID)> onSelect = 0);
  232. };
  233. ////////////////////////////////////////////////////////////////////////////////
  234. /// base class for hero/town/garrison tooltips
  235. class CArmyTooltip : public CIntObject
  236. {
  237. void init(const InfoAboutArmy &army);
  238. public:
  239. CArmyTooltip(Point pos, const InfoAboutArmy &army);
  240. CArmyTooltip(Point pos, const CArmedInstance * army);
  241. };
  242. /// Class for hero tooltip. Does not have any background!
  243. /// background for infoBox: ADSTATHR
  244. /// background for tooltip: HEROQVBK
  245. class CHeroTooltip : public CArmyTooltip
  246. {
  247. void init(const InfoAboutHero &hero);
  248. public:
  249. CHeroTooltip(Point pos, const InfoAboutHero &hero);
  250. CHeroTooltip(Point pos, const CGHeroInstance * hero);
  251. };
  252. /// Class for town tooltip. Does not have any background!
  253. /// background for infoBox: ADSTATCS
  254. /// background for tooltip: TOWNQVBK
  255. class CTownTooltip : public CArmyTooltip
  256. {
  257. void init(const InfoAboutTown &town);
  258. public:
  259. CTownTooltip(Point pos, const InfoAboutTown &town);
  260. CTownTooltip(Point pos, const CGTownInstance * town);
  261. };
  262. ///////////////////////////////////////////////////////////////////////////////
  263. class CGarrisonInt;
  264. /// A single garrison slot which holds one creature of a specific amount
  265. class CGarrisonSlot : public CIntObject
  266. {
  267. int ID; //for identification
  268. CGarrisonInt *owner;
  269. const CStackInstance *myStack; //NULL if slot is empty
  270. const CCreature *creature;
  271. int upg; //0 - up garrison, 1 - down garrison
  272. CAnimImage * creatureImage;
  273. CAnimImage * selectionImage; // image for selection, not always visible
  274. CLabel * stackCount;
  275. void setHighlight(bool on);
  276. public:
  277. virtual void hover (bool on); //call-in
  278. const CArmedInstance * getObj() const;
  279. bool our() const;
  280. void clickRight(tribool down, bool previousState);
  281. void clickLeft(tribool down, bool previousState);
  282. void update();
  283. CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg=0, const CStackInstance * Creature=NULL);
  284. friend class CGarrisonInt;
  285. };
  286. /// Class which manages slots of upper and lower garrison, splitting of units
  287. class CGarrisonInt :public CIntObject
  288. {
  289. CGarrisonSlot *highlighted; //chosen slot. Should be changed only via selectSlot
  290. bool inSplittingMode;
  291. public:
  292. void selectSlot(CGarrisonSlot * slot); //null = deselect
  293. const CGarrisonSlot * getSelection();
  294. void setSplittingMode(bool on);
  295. bool getSplittingMode();
  296. int interx; //space between slots
  297. Point garOffset; //offset between garrisons (not used if only one hero)
  298. std::vector<CAdventureMapButton *> splitButtons; //may be empty if no buttons
  299. int p2, //TODO: comment me
  300. shiftPos;//1st slot of the second row, set shiftPoint for effect
  301. bool pb,
  302. smallIcons, //true - 32x32 imgs, false - 58x64
  303. removableUnits,//player can remove units from up
  304. twoRows,//slots will be placed in 2 rows
  305. owned[2];//player owns up or down army [0] upper, [1] lower
  306. // const CCreatureSet *set1; //top set of creatures
  307. // const CCreatureSet *set2; //bottom set of creatures
  308. std::vector<CGarrisonSlot*> slotsUp, slotsDown; //slots of upper and lower garrison
  309. const CArmedInstance *armedObjs[2]; //[0] is upper, [1] is down
  310. //const CArmedInstance *oup, *odown; //upper and lower garrisons (heroes or towns)
  311. void setArmy(const CArmedInstance *army, bool bottomGarrison);
  312. void addSplitBtn(CAdventureMapButton * button);
  313. void createSet(std::vector<CGarrisonSlot*> &ret, const CCreatureSet * set, int posX, int distance, int posY, int Upg );
  314. void createSlots();
  315. void recreateSlots();
  316. void splitClick(); //handles click on split button
  317. void splitStacks(int amountLeft, int amountRight); //TODO: comment me
  318. //x, y - position;
  319. //inx - distance between slots;
  320. //pomsur, SurOffset - UNUSED
  321. //s1, s2 - top and bottom armies;
  322. //removableUnits - you can take units from top;
  323. //smallImgs - units images size 64x58 or 32x32;
  324. //twoRows - display slots in 2 row (1st row = 4 slots, 2nd = 3 slots)
  325. CGarrisonInt(int x, int y, int inx, const Point &garsOffset, SDL_Surface *pomsur, const Point &SurOffset, const CArmedInstance *s1, const CArmedInstance *s2=NULL, bool _removableUnits = true, bool smallImgs = false, bool _twoRows=false); //c-tor
  326. };
  327. /// draws picture with creature on background, use Animated=true to get animation
  328. class CCreaturePic : public CIntObject
  329. {
  330. private:
  331. CPicture *bg;
  332. CCreatureAnim *anim; //displayed animation
  333. public:
  334. CCreaturePic(int x, int y, const CCreature *cre, bool Big=true, bool Animated=true); //c-tor
  335. };
  336. /// Recruitment window where you can recruit creatures
  337. class CRecruitmentWindow : public CWindowObject
  338. {
  339. class CCreatureCard : public CIntObject
  340. {
  341. CRecruitmentWindow * parent;
  342. CCreaturePic *pic; //creature's animation
  343. bool selected;
  344. void clickLeft(tribool down, bool previousState);
  345. void clickRight(tribool down, bool previousState);
  346. void showAll(SDL_Surface *to);
  347. public:
  348. const CCreature * creature;
  349. si32 amount;
  350. void select(bool on);
  351. CCreatureCard(CRecruitmentWindow * window, const CCreature *crea, int totalAmount);
  352. };
  353. /// small class to display creature costs
  354. class CCostBox : public CIntObject
  355. {
  356. std::map<int, std::pair<CLabel *, CAnimImage * > > resources;
  357. public:
  358. //res - resources to show
  359. void set(TResources res);
  360. //res - visible resources
  361. CCostBox(Rect position, std::string title);
  362. void createItems(TResources res);
  363. };
  364. boost::function<void(int,int)> onRecruit; //void (int ID, int amount) <-- call to recruit creatures
  365. int level;
  366. const CArmedInstance *dst;
  367. CCreatureCard * selected;
  368. std::vector<CCreatureCard *> cards;
  369. CSlider *slider; //for selecting amount
  370. CAdventureMapButton *maxButton, *buyButton, *cancelButton;
  371. //labels for visible values
  372. CLabel * title;
  373. CLabel * availableValue;
  374. CLabel * toRecruitValue;
  375. CCostBox * costPerTroopValue;
  376. CCostBox * totalCostValue;
  377. void select(CCreatureCard * card);
  378. void buy();
  379. void sliderMoved(int to);
  380. void showAll(SDL_Surface *to);
  381. public:
  382. const CGDwelling * const dwelling;
  383. CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const boost::function<void(int,int)> & Recruit, int y_offset = 0); //creatures - pairs<creature_ID,amount> //c-tor
  384. void availableCreaturesChanged();
  385. };
  386. /// Split window where creatures can be splitted up into two single unit stacks
  387. class CSplitWindow : public CWindowObject
  388. {
  389. boost::function<void(int, int)> callback;
  390. int leftAmount;
  391. int rightAmount;
  392. int leftMin;
  393. int rightMin;
  394. CSlider *slider;
  395. CCreaturePic *animLeft, *animRight; //creature's animation
  396. CAdventureMapButton *ok, *cancel;
  397. CTextInput *leftInput, *rightInput;
  398. void setAmountText(std::string text, bool left);
  399. void setAmount(int value, bool left);
  400. void sliderMoved(int value);
  401. void apply();
  402. public:
  403. /**
  404. * creature - displayed creature
  405. * callback(leftAmount, rightAmount) - function to call on close
  406. * leftMin, rightMin - minimal amount of creatures in each stack
  407. * leftAmount, rightAmount - amount of creatures in each stack
  408. */
  409. CSplitWindow(const CCreature * creature, boost::function<void(int, int)> callback,
  410. int leftMin, int rightMin, int leftAmount, int rightAmount);
  411. };
  412. /// Raised up level windowe where you can select one out of two skills
  413. class CLevelWindow : public CWindowObject
  414. {
  415. CComponentBox * box; //skills to select
  416. boost::function<void(ui32)> cb;
  417. void selectionChanged(unsigned to);
  418. public:
  419. CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> callback); //c-tor
  420. ~CLevelWindow(); //d-tor
  421. };
  422. /// Resource bar like that at the bottom of the adventure map screen
  423. class CMinorResDataBar : public CIntObject
  424. {
  425. public:
  426. SDL_Surface *bg; //background bitmap
  427. void show(SDL_Surface * to);
  428. void showAll(SDL_Surface * to);
  429. CMinorResDataBar(); //c-tor
  430. ~CMinorResDataBar(); //d-tor
  431. };
  432. /// Town portal, castle gate window
  433. class CObjectListWindow : public CWindowObject
  434. {
  435. class CItem : public CIntObject
  436. {
  437. CObjectListWindow *parent;
  438. CLabel *text;
  439. CPicture *border;
  440. public:
  441. const size_t index;
  442. CItem(CObjectListWindow *parent, size_t id, std::string text);
  443. void select(bool on);
  444. void clickLeft(tribool down, bool previousState);
  445. };
  446. boost::function<void(int)> onSelect;//called when OK button is pressed, returns id of selected item.
  447. CLabel * title;
  448. CLabel * descr;
  449. CListBox *list;
  450. CPicture *titleImage;//title image (castle gate\town portal picture)
  451. CAdventureMapButton *ok, *exit;
  452. std::vector< std::pair<int, std::string> > items;//all items present in list
  453. void init(CPicture * titlePic, std::string _title, std::string _descr);
  454. public:
  455. size_t selected;//index of currently selected item
  456. /// Callback will be called when OK button is pressed, returns id of selected item. initState = initially selected item
  457. /// Image can be NULL
  458. ///item names will be taken from map objects
  459. CObjectListWindow(const std::vector<int> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  460. boost::function<void(int)> Callback);
  461. CObjectListWindow(const std::vector<std::string> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  462. boost::function<void(int)> Callback);
  463. CIntObject *genItem(size_t index);
  464. void elementSelected();//call callback and close this window
  465. void changeSelection(size_t which);
  466. void keyPressed (const SDL_KeyboardEvent & key);
  467. };
  468. class CArtifactHolder
  469. {
  470. public:
  471. CArtifactHolder();
  472. virtual void artifactRemoved(const ArtifactLocation &artLoc)=0;
  473. virtual void artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)=0;
  474. virtual void artifactDisassembled(const ArtifactLocation &artLoc)=0;
  475. virtual void artifactAssembled(const ArtifactLocation &artLoc)=0;
  476. };
  477. class CWindowWithArtifacts : public CArtifactHolder
  478. {
  479. public:
  480. std::vector<CArtifactsOfHero *> artSets;
  481. void artifactRemoved(const ArtifactLocation &artLoc);
  482. void artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc);
  483. void artifactDisassembled(const ArtifactLocation &artLoc);
  484. void artifactAssembled(const ArtifactLocation &artLoc);
  485. };
  486. class CTradeWindow : public CWindowObject, public CWindowWithArtifacts //base for markets and altar of sacrifice
  487. {
  488. public:
  489. enum EType
  490. {
  491. RESOURCE, PLAYER, ARTIFACT_TYPE, CREATURE, CREATURE_PLACEHOLDER, ARTIFACT_PLACEHOLDER, ARTIFACT_INSTANCE
  492. };
  493. class CTradeableItem : public CIntObject
  494. {
  495. CAnimImage * image;
  496. std::string getFilename();
  497. int getIndex();
  498. public:
  499. const CArtifactInstance *hlp; //holds ptr to artifact instance id type artifact
  500. EType type;
  501. int id;
  502. const int serial;
  503. const bool left;
  504. std::string subtitle; //empty if default
  505. void setType(EType newType);
  506. void setID(int newID);
  507. const CArtifactInstance *getArtInstance() const;
  508. void setArtInstance(const CArtifactInstance *art);
  509. CFunctionList<void()> callback;
  510. bool downSelection;
  511. void showAllAt(const Point &dstPos, const std::string &customSub, SDL_Surface * to);
  512. void clickRight(tribool down, bool previousState);
  513. void hover (bool on);
  514. void showAll(SDL_Surface * to);
  515. void clickLeft(tribool down, bool previousState);
  516. std::string getName(int number = -1) const;
  517. CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial);
  518. };
  519. const IMarket *market;
  520. const CGHeroInstance *hero;
  521. CArtifactsOfHero *arts;
  522. //all indexes: 1 = left, 0 = right
  523. std::vector<CTradeableItem*> items[2];
  524. CTradeableItem *hLeft, *hRight; //highlighted items (NULL if no highlight)
  525. EType itemsType[2];
  526. EMarketMode::EMarketMode mode;//0 - res<->res; 1 - res<->plauer; 2 - buy artifact; 3 - sell artifact
  527. CAdventureMapButton *ok, *max, *deal;
  528. CSlider *slider; //for choosing amount to be exchanged
  529. bool readyToTrade;
  530. CTradeWindow(std::string bgName, const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode); //c
  531. void showAll(SDL_Surface * to);
  532. void initSubs(bool Left);
  533. void initTypes();
  534. void initItems(bool Left);
  535. std::vector<int> *getItemsIds(bool Left); //NULL if default
  536. void getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const;
  537. void removeItems(const std::set<CTradeableItem *> &toRemove);
  538. void removeItem(CTradeableItem * t);
  539. void getEmptySlots(std::set<CTradeableItem *> &toRemove);
  540. void setMode(EMarketMode::EMarketMode Mode); //mode setter
  541. void artifactSelected(CArtPlace *slot); //used when selling artifacts -> called when user clicked on artifact slot
  542. virtual void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const = 0;
  543. virtual void selectionChanged(bool side) = 0; //true == left
  544. virtual Point selectionOffset(bool Left) const = 0;
  545. virtual std::string selectionSubtitle(bool Left) const = 0;
  546. virtual void garrisonChanged() = 0;
  547. virtual void artifactsChanged(bool left) = 0;
  548. };
  549. class CMarketplaceWindow : public CTradeWindow
  550. {
  551. bool printButtonFor(EMarketMode::EMarketMode M) const;
  552. std::string getBackgroundForMode(EMarketMode::EMarketMode mode);
  553. public:
  554. int r1, r2; //suggested amounts of traded resources
  555. bool madeTransaction; //if player made at least one transaction
  556. CTextBox *traderText;
  557. void setMax();
  558. void sliderMoved(int to);
  559. void makeDeal();
  560. void selectionChanged(bool side); //true == left
  561. CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero = NULL, EMarketMode::EMarketMode Mode = EMarketMode::RESOURCE_RESOURCE); //c-tor
  562. ~CMarketplaceWindow(); //d-tor
  563. Point selectionOffset(bool Left) const;
  564. std::string selectionSubtitle(bool Left) const;
  565. void garrisonChanged(); //removes creatures with count 0 from the list (apparently whole stack has been sold)
  566. void artifactsChanged(bool left);
  567. void resourceChanged(int type, int val);
  568. void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const;
  569. void updateTraderText();
  570. };
  571. class CAltarWindow : public CTradeWindow
  572. {
  573. CAnimImage * artIcon;
  574. public:
  575. CAltarWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode); //c-tor
  576. void getExpValues();
  577. ~CAltarWindow(); //d-tor
  578. std::vector<int> sacrificedUnits, //[slot_nr] -> how many creatures from that slot will be sacrificed
  579. expPerUnit;
  580. CAdventureMapButton *sacrificeAll, *sacrificeBackpack;
  581. CLabel *expToLevel, *expOnAltar;
  582. void selectionChanged(bool side); //true == left
  583. void SacrificeAll();
  584. void SacrificeBackpack();
  585. void putOnAltar(int backpackIndex);
  586. bool putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art);
  587. void makeDeal();
  588. void showAll(SDL_Surface * to);
  589. void blockTrade();
  590. void sliderMoved(int to);
  591. void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const;
  592. void mimicCres();
  593. Point selectionOffset(bool Left) const;
  594. std::string selectionSubtitle(bool Left) const;
  595. void garrisonChanged();
  596. void artifactsChanged(bool left);
  597. void calcTotalExp();
  598. void setExpToLevel();
  599. void updateRight(CTradeableItem *toUpdate);
  600. void artifactPicked();
  601. int firstFreeSlot();
  602. void moveFromSlotToAltar(int slotID, CTradeableItem* altarSlot, const CArtifactInstance *art);
  603. };
  604. class CSystemOptionsWindow : public CWindowObject
  605. {
  606. private:
  607. CLabel *title;
  608. CLabelGroup *leftGroup;
  609. CLabelGroup *rightGroup;
  610. CAdventureMapButton *load, *save, *restart, *mainMenu, *quitGame, *backToMap; //load and restart are not used yet
  611. CHighlightableButtonsGroup * heroMoveSpeed;
  612. CHighlightableButtonsGroup * mapScrollSpeed;
  613. CHighlightableButtonsGroup * musicVolume, * effectsVolume;
  614. //CHighlightableButton * showPath;
  615. CHighlightableButton * showReminder;
  616. //CHighlightableButton * quickCombat;
  617. //CHighlightableButton * videoSubs;
  618. CHighlightableButton * newCreatureWin;
  619. CHighlightableButton * fullscreen;
  620. CAdventureMapButton *gameResButton;
  621. CLabel *gameResLabel;
  622. SettingsListener onFullscreenChanged;
  623. void setMusicVolume( int newVolume );
  624. void setSoundVolume( int newVolume );
  625. void setHeroMoveSpeed( int newSpeed );
  626. void setMapScrollingSpeed( int newSpeed );
  627. //functions bound to buttons
  628. void bloadf(); //load game
  629. void bsavef(); //save game
  630. void bquitf(); //quit game
  631. void breturnf(); //return to game
  632. void brestartf(); //restart game
  633. void bmainmenuf(); //return to main menu
  634. //functions for checkboxes
  635. void toggleReminder(bool on);
  636. void toggleCreatureWin(bool on);
  637. void toggleFullscreen(bool on);
  638. void selectGameRes();
  639. void setGameRes(int index);
  640. void closeAndPushEvent(int eventType, int code = 0);
  641. public:
  642. CSystemOptionsWindow(); //c-tor
  643. };
  644. class CTavernWindow : public CWindowObject
  645. {
  646. public:
  647. class HeroPortrait : public CIntObject
  648. {
  649. public:
  650. std::string hoverName;
  651. const CGHeroInstance *h;
  652. char descr[100]; // "XXX is a level Y ZZZ with N artifacts"
  653. void clickLeft(tribool down, bool previousState);
  654. void clickRight(tribool down, bool previousState);
  655. void hover (bool on);
  656. HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H);
  657. private:
  658. int *_sel;
  659. const int _id;
  660. } *h1, *h2; //recruitable heroes
  661. CGStatusBar *bar; //tavern's internal status bar
  662. int selected;//0 (left) or 1 (right)
  663. int oldSelected;//0 (left) or 1 (right)
  664. CAdventureMapButton *thiefGuild, *cancel, *recruit;
  665. const CGObjectInstance *tavernObj;
  666. CTavernWindow(const CGObjectInstance *TavernObj); //c-tor
  667. ~CTavernWindow(); //d-tor
  668. void recruitb();
  669. void thievesguildb();
  670. void show(SDL_Surface * to);
  671. };
  672. class CInGameConsole : public CIntObject
  673. {
  674. private:
  675. std::list< std::pair< std::string, int > > texts; //<text to show, time of add>
  676. boost::mutex texts_mx; // protects texts
  677. std::vector< std::string > previouslyEntered; //previously entered texts, for up/down arrows to work
  678. int prevEntDisp; //displayed entry from previouslyEntered - if none it's -1
  679. int defaultTimeout; //timeout for new texts (in ms)
  680. int maxDisplayedTexts; //hiw many texts can be displayed simultaneously
  681. public:
  682. std::string enteredText;
  683. void show(SDL_Surface * to);
  684. void print(const std::string &txt);
  685. void keyPressed (const SDL_KeyboardEvent & key); //call-in
  686. void startEnteringText();
  687. void endEnteringText(bool printEnteredText);
  688. void refreshEnteredText();
  689. CInGameConsole(); //c-tor
  690. };
  691. /// Can interact on left and right mouse clicks
  692. class LRClickableAreaWTextComp: public LRClickableAreaWText
  693. {
  694. public:
  695. int baseType;
  696. int bonusValue, type;
  697. virtual void clickLeft(tribool down, bool previousState);
  698. virtual void clickRight(tribool down, bool previousState);
  699. LRClickableAreaWTextComp(const Rect &Pos = Rect(0,0,0,0), int BaseType = -1);
  700. CComponent * createComponent() const;
  701. };
  702. class MoraleLuckBox : public LRClickableAreaWTextComp
  703. {
  704. CAnimImage *image;
  705. public:
  706. bool morale; //true if morale, false if luck
  707. bool small;
  708. void set(const IBonusBearer *node);
  709. MoraleLuckBox(bool Morale, const Rect &r, bool Small=false);
  710. };
  711. /// Opens hero window by left-clicking on it
  712. class CHeroArea: public CIntObject
  713. {
  714. const CGHeroInstance * hero;
  715. public:
  716. CHeroArea(int x, int y, const CGHeroInstance * _hero);
  717. void clickLeft(tribool down, bool previousState);
  718. void clickRight(tribool down, bool previousState);
  719. void hover(bool on);
  720. };
  721. /// Opens town screen by left-clicking on it
  722. class LRClickableAreaOpenTown: public LRClickableAreaWTextComp
  723. {
  724. public:
  725. const CGTownInstance * town;
  726. void clickLeft(tribool down, bool previousState);
  727. void clickRight(tribool down, bool previousState);
  728. LRClickableAreaOpenTown();
  729. };
  730. /// Artifacts can be placed there. Gets shown at the hero window
  731. class CArtPlace: public LRClickableAreaWTextComp
  732. {
  733. CAnimImage *image;
  734. CAnimImage *selection;
  735. void createImage();
  736. public:
  737. // consider these members as const - change them only with appropriate methods e.g. lockSlot()
  738. bool locked;
  739. bool picked;
  740. bool marked;
  741. int slotID; //Arts::EPOS enum + backpack starting from Arts::BACKPACK_START
  742. void lockSlot(bool on);
  743. void pickSlot(bool on);
  744. void selectSlot(bool on);
  745. CArtifactsOfHero * ourOwner;
  746. const CArtifactInstance * ourArt; // should be changed only with setArtifact()
  747. CArtPlace(Point position, const CArtifactInstance * Art = NULL); //c-tor
  748. void clickLeft(tribool down, bool previousState);
  749. void clickRight(tribool down, bool previousState);
  750. void select ();
  751. void deselect ();
  752. void showAll(SDL_Surface * to);
  753. bool fitsHere (const CArtifactInstance * art) const; //returns true if given artifact can be placed here
  754. void setMeAsDest(bool backpackAsVoid = true);
  755. void setArtifact(const CArtifactInstance *art);
  756. };
  757. /// Contains artifacts of hero. Distincts which artifacts are worn or backpacked
  758. class CArtifactsOfHero : public CIntObject
  759. {
  760. const CGHeroInstance * curHero;
  761. std::vector<CArtPlace *> artWorn; // 0 - head; 1 - shoulders; 2 - neck; 3 - right hand; 4 - left hand; 5 - torso; 6 - right ring; 7 - left ring; 8 - feet; 9 - misc1; 10 - misc2; 11 - misc3; 12 - misc4; 13 - mach1; 14 - mach2; 15 - mach3; 16 - mach4; 17 - spellbook; 18 - misc5
  762. std::vector<CArtPlace *> backpack; //hero's visible backpack (only 5 elements!)
  763. int backpackPos; //number of first art visible in backpack (in hero's vector)
  764. public:
  765. struct SCommonPart
  766. {
  767. struct Artpos
  768. {
  769. int slotID;
  770. const CArtifactsOfHero *AOH;
  771. const CArtifactInstance *art;
  772. Artpos();
  773. void clear();
  774. void setTo(const CArtPlace *place, bool dontTakeBackpack);
  775. bool valid();
  776. bool operator==(const ArtifactLocation &al) const;
  777. } src, dst;
  778. std::set<CArtifactsOfHero *> participants; // Needed to mark slots.
  779. void reset();
  780. } * commonInfo; //when we have more than one CArtifactsOfHero in one window with exchange possibility, we use this (eg. in exchange window); to be provided externally
  781. bool updateState; // Whether the commonInfo should be updated on setHero or not.
  782. CAdventureMapButton * leftArtRoll, * rightArtRoll;
  783. bool allowedAssembling;
  784. std::multiset<const CArtifactInstance*> artifactsOnAltar; //artifacts id that are technically present in backpack but in GUI are moved to the altar - they'll be omitted in backpack slots
  785. boost::function<void(CArtPlace*)> highlightModeCallback; //if set, clicking on art place doesn't pick artifact but highlights the slot and calls this function
  786. void realizeCurrentTransaction(); //calls callback with parameters stored in commonInfo
  787. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst);
  788. void artifactRemoved(const ArtifactLocation &al);
  789. void artifactAssembled(const ArtifactLocation &al);
  790. void artifactDisassembled(const ArtifactLocation &al);
  791. CArtPlace *getArtPlace(int slot);
  792. void setHero(const CGHeroInstance * hero);
  793. const CGHeroInstance *getHero() const;
  794. void dispose(); //free resources not needed after closing windows and reset state
  795. void scrollBackpack(int dir); //dir==-1 => to left; dir==1 => to right
  796. void safeRedraw();
  797. void markPossibleSlots(const CArtifactInstance* art);
  798. void unmarkSlots(bool withRedraw = true); //unmarks slots in all visible AOHs
  799. void unmarkLocalSlots(bool withRedraw = true); //unmarks slots in that particular AOH
  800. void setSlotData (CArtPlace* artPlace, int slotID);
  801. void updateWornSlots (bool redrawParent = true);
  802. void updateSlot(int i);
  803. void eraseSlotData (CArtPlace* artPlace, int slotID);
  804. CArtifactsOfHero(const Point& position, bool createCommonPart = false);
  805. //Alternative constructor, used if custom artifacts positioning required (Kingdom interface)
  806. CArtifactsOfHero(std::vector<CArtPlace *> ArtWorn, std::vector<CArtPlace *> Backpack,
  807. CAdventureMapButton *leftScroll, CAdventureMapButton *rightScroll, bool createCommonPart = false);
  808. ~CArtifactsOfHero(); //d-tor
  809. void updateParentWindow();
  810. friend class CArtPlace;
  811. };
  812. class CGarrisonHolder
  813. {
  814. public:
  815. CGarrisonHolder();
  816. virtual void updateGarrisons()=0;
  817. };
  818. class CWindowWithGarrison : public virtual CGarrisonHolder
  819. {
  820. public:
  821. CGarrisonInt *garr;
  822. virtual void updateGarrisons();
  823. };
  824. /// Garrison window where you can take creatures out of the hero to place it on the garrison
  825. class CGarrisonWindow : public CWindowObject, public CWindowWithGarrison
  826. {
  827. public:
  828. CAdventureMapButton * quit;
  829. CGarrisonWindow(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits); //c-tor
  830. };
  831. class CExchangeWindow : public CWindowObject, public CWindowWithGarrison, public CWindowWithArtifacts
  832. {
  833. CGStatusBar * ourBar; //internal statusbar
  834. CAdventureMapButton * quit, * questlogButton[2];
  835. std::vector<LRClickableAreaWTextComp *> secSkillAreas[2], primSkillAreas;
  836. MoraleLuckBox *morale[2], *luck[2];
  837. LRClickableAreaWText *specialty[2];
  838. LRClickableAreaWText *experience[2];
  839. LRClickableAreaWText *spellPoints[2];
  840. CHeroArea *portrait[2];
  841. public:
  842. const CGHeroInstance* heroInst[2];
  843. CArtifactsOfHero * artifs[2];
  844. void questlog(int whichHero); //questlog button callback; whichHero: 0 - left, 1 - right
  845. void prepareBackground(); //prepares or redraws bg
  846. CExchangeWindow(si32 hero1, si32 hero2); //c-tor
  847. ~CExchangeWindow(); //d-tor
  848. };
  849. /// Here you can buy ships
  850. class CShipyardWindow : public CWindowObject
  851. {
  852. public:
  853. CGStatusBar *bar;
  854. CPicture *bgWater;
  855. CLabel *title;
  856. CLabel *costLabel;
  857. CAnimImage *woodPic, *goldPic;
  858. CLabel *woodCost, *goldCost;
  859. CAnimImage *bgShip;
  860. CAdventureMapButton *build, *quit;
  861. CGStatusBar * statusBar;
  862. CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const boost::function<void()> &onBuy);
  863. };
  864. /// Puzzle screen which gets uncovered when you visit obilisks
  865. class CPuzzleWindow : public CWindowObject
  866. {
  867. private:
  868. int3 grailPos;
  869. CAdventureMapButton * quitb;
  870. std::vector<CPicture * > piecesToRemove;
  871. ui8 currentAlpha;
  872. public:
  873. void showAll(SDL_Surface * to);
  874. void show(SDL_Surface * to);
  875. CPuzzleWindow(const int3 &grailPos, double discoveredRatio);
  876. };
  877. /// Creature transformer window
  878. class CTransformerWindow : public CWindowObject, public CGarrisonHolder
  879. {
  880. public:
  881. class CItem : public CIntObject
  882. {
  883. public:
  884. int id;//position of creature in hero army
  885. bool left;//position of the item
  886. int size; //size of creature stack
  887. CTransformerWindow * parent;
  888. CAnimImage *icon;
  889. void move();
  890. void clickLeft(tribool down, bool previousState);
  891. void update();
  892. CItem(CTransformerWindow * parent, int size, int id);
  893. };
  894. const CArmedInstance *army;//object with army for transforming (hero or town)
  895. const CGHeroInstance *hero;//only if we have hero in town
  896. const CGTownInstance *town;//market, town garrison is used if hero == NULL
  897. std::vector<CItem*> items;
  898. CAdventureMapButton *all, *convert, *cancel;
  899. CGStatusBar *bar;
  900. void makeDeal();
  901. void addAll();
  902. void updateGarrisons();
  903. CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town); //c-tor
  904. };
  905. class CUniversityWindow : public CWindowObject
  906. {
  907. class CItem : public CAnimImage
  908. {
  909. public:
  910. int ID;//id of selected skill
  911. CUniversityWindow * parent;
  912. void showAll(SDL_Surface * to);
  913. void clickLeft(tribool down, bool previousState);
  914. void clickRight(tribool down, bool previousState);
  915. void hover(bool on);
  916. int state();//0=can't learn, 1=learned, 2=can learn
  917. CItem(CUniversityWindow * _parent, int _ID, int X, int Y);
  918. };
  919. public:
  920. const CGHeroInstance *hero;
  921. const IMarket * market;
  922. CPicture * green, * yellow, * red;//colored bars near skills
  923. std::vector<CItem*> items;
  924. CAdventureMapButton *cancel;
  925. CGStatusBar *bar;
  926. CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market); //c-tor
  927. };
  928. /// Confirmation window for University
  929. class CUnivConfirmWindow : public CWindowObject
  930. {
  931. public:
  932. CUniversityWindow * parent;
  933. CGStatusBar *bar;
  934. CAdventureMapButton *confirm, *cancel;
  935. CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available); //c-tor
  936. void makeDeal(int skill);
  937. };
  938. /// Hill fort is the building where you can upgrade units
  939. class CHillFortWindow : public CWindowObject, public CWindowWithGarrison
  940. {
  941. public:
  942. int slotsCount;//=7;
  943. CGStatusBar * bar;
  944. CDefEssential *resources;
  945. CHeroArea *heroPic;//clickable hero image
  946. CAdventureMapButton *quit,//closes window
  947. *upgradeAll,//upgrade all creatures
  948. *upgrade[7];//upgrade single creature
  949. const CGObjectInstance * fort;
  950. const CGHeroInstance * hero;
  951. std::vector<int> currState;//current state of slot - to avoid calls to getState or updating buttons
  952. std::vector<TResources> costs;// costs [slot ID] [resource ID] = resource count for upgrade
  953. TResources totalSumm; // totalSum[resource ID] = value
  954. CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object); //c-tor
  955. void showAll (SDL_Surface *to);
  956. std::string getDefForSlot(int slot);//return def name for this slot
  957. std::string getTextForSlot(int slot);//return hover text for this slot
  958. void makeDeal(int slot);//-1 for upgrading all creatures
  959. int getState(int slot); //-1 = no creature 0=can't upgrade, 1=upgraded, 2=can upgrade
  960. void updateGarrisons();//update buttons after garrison changes
  961. };
  962. class CThievesGuildWindow : public CWindowObject
  963. {
  964. const CGObjectInstance * owner;
  965. CGStatusBar * statusBar;
  966. CAdventureMapButton * exitb;
  967. CMinorResDataBar * resdatabar;
  968. public:
  969. CThievesGuildWindow(const CGObjectInstance * _owner);
  970. };