CPlayerInterface.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #ifndef CPLAYERINTERFACE_H
  2. #define CPLAYERINTERFACE_H
  3. #include "global.h"
  4. #include "CGameInterface.h"
  5. #include "SDL_framerate.h"
  6. #include <map>
  7. class CDefEssential;
  8. class AdventureMapButton;
  9. class CDefHandler;
  10. struct HeroMoveDetails;
  11. class CDefEssential;
  12. class CGHeroInstance;
  13. class CAdvMapInt;
  14. class CCastleInterface;
  15. class CStack;
  16. class SComponent;
  17. class CCreature;
  18. struct SDL_Surface;
  19. struct CPath;
  20. class CCreatureAnimation;
  21. class CSelectableComponent;
  22. class CCreatureSet;
  23. class CGObjectInstance;
  24. class CSlider;
  25. struct UpgradeInfo;
  26. template <typename T> struct CondSh;
  27. namespace boost
  28. {
  29. class mutex;
  30. };
  31. class IShowable
  32. {
  33. public:
  34. virtual void show(SDL_Surface * to = NULL)=0;
  35. virtual ~IShowable(){};
  36. };
  37. class IStatusBar
  38. {
  39. public:
  40. virtual ~IStatusBar(){}; //d-tor
  41. virtual void print(std::string text)=0; //prints text and refreshes statusbar
  42. virtual void clear()=0;//clears statusbar and refreshes
  43. virtual void show()=0; //shows statusbar (with current text)
  44. virtual std::string getCurrent()=0;
  45. };
  46. class IActivable
  47. {
  48. public:
  49. virtual void activate()=0;
  50. virtual void deactivate()=0;
  51. virtual ~IActivable(){};
  52. };
  53. class IShowActivable : public IShowable, public IActivable
  54. {
  55. public:
  56. virtual ~IShowActivable(){};
  57. };
  58. class CMainInterface : public IShowActivable
  59. {
  60. public:
  61. IShowActivable *subInt;
  62. };
  63. class CIntObject //interface object
  64. {
  65. public:
  66. SDL_Rect pos;
  67. int ID;
  68. };
  69. class CSimpleWindow : public virtual CIntObject, public IShowable
  70. {
  71. public:
  72. SDL_Surface * bitmap;
  73. CIntObject * owner;
  74. virtual void show(SDL_Surface * to = NULL);
  75. CSimpleWindow():bitmap(NULL),owner(NULL){};
  76. virtual ~CSimpleWindow();
  77. };
  78. class CButtonBase : public virtual CIntObject, public IShowable, public IActivable //basic buttton class
  79. {
  80. public:
  81. int bitmapOffset;
  82. int type; //advmapbutton=2
  83. bool abs;
  84. bool active;
  85. bool notFreeButton;
  86. CIntObject * ourObj; // "owner"
  87. int state;
  88. std::vector< std::vector<SDL_Surface*> > imgs;
  89. int curimg;
  90. virtual void show(SDL_Surface * to = NULL);
  91. virtual void activate()=0;
  92. virtual void deactivate()=0;
  93. CButtonBase();
  94. virtual ~CButtonBase();
  95. };
  96. class ClickableL : public virtual CIntObject //for left-clicks
  97. {
  98. public:
  99. bool pressedL;
  100. ClickableL();
  101. virtual ~ClickableL(){};
  102. virtual void clickLeft (boost::logic::tribool down)=0;
  103. virtual void activate()=0;
  104. virtual void deactivate()=0;
  105. };
  106. class ClickableR : public virtual CIntObject //for right-clicks
  107. {
  108. public:
  109. bool pressedR;
  110. ClickableR();
  111. virtual ~ClickableR(){};
  112. virtual void clickRight (boost::logic::tribool down)=0;
  113. virtual void activate()=0;
  114. virtual void deactivate()=0;
  115. };
  116. class Hoverable : public virtual CIntObject
  117. {
  118. public:
  119. Hoverable(){hovered=false;}
  120. virtual ~Hoverable(){};
  121. bool hovered;
  122. virtual void hover (bool on)=0;
  123. virtual void activate()=0;
  124. virtual void deactivate()=0;
  125. };
  126. class KeyInterested : public virtual CIntObject
  127. {
  128. public:
  129. virtual ~KeyInterested(){};
  130. virtual void keyPressed (SDL_KeyboardEvent & key)=0;
  131. virtual void activate()=0;
  132. virtual void deactivate()=0;
  133. };
  134. class MotionInterested: public virtual CIntObject
  135. {
  136. public:
  137. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  138. MotionInterested(){strongInterest=false;};
  139. virtual ~MotionInterested(){};
  140. virtual void mouseMoved (SDL_MouseMotionEvent & sEvent)=0;
  141. virtual void activate()=0;
  142. virtual void deactivate()=0;
  143. };
  144. class TimeInterested: public virtual CIntObject
  145. {
  146. public:
  147. virtual ~TimeInterested(){};
  148. int toNextTick;
  149. virtual void tick()=0;
  150. virtual void activate();
  151. virtual void deactivate();
  152. };
  153. class CInfoWindow : public CSimpleWindow //text + comp. + ok button
  154. { //window able to delete its components when closed
  155. public:
  156. bool delComps; //whether comps will be deleted
  157. std::vector<AdventureMapButton *> buttons;
  158. std::vector<SComponent*> components;
  159. virtual void close();
  160. virtual void show(SDL_Surface * to = NULL);
  161. void activate();
  162. void deactivate();
  163. CInfoWindow(std::string text, int player, int charperline, const std::vector<SComponent*> &comps, std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons);
  164. CInfoWindow();
  165. ~CInfoWindow();
  166. };
  167. class CSelWindow : public CInfoWindow //component selection window
  168. { //uwaga - to okno usuwa swoje komponenty przy zamykaniu
  169. public:
  170. void selectionChange(unsigned to);
  171. void close();
  172. CSelWindow(std::string text, int player, int charperline, std::vector<CSelectableComponent*> &comps, std::vector<std::pair<std::string,boost::function<void()> > > &Buttons);
  173. CSelWindow(){};
  174. };
  175. class CRClickPopup : public IShowable, public ClickableR
  176. {
  177. public:
  178. virtual void activate();
  179. virtual void deactivate();
  180. virtual void close()=0;
  181. void clickRight (boost::logic::tribool down);
  182. virtual ~CRClickPopup(){};
  183. };
  184. class CInfoPopup : public CRClickPopup
  185. {
  186. public:
  187. bool free;
  188. SDL_Surface * bitmap;
  189. CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free=false);
  190. void close();
  191. void show(SDL_Surface * to = NULL);
  192. CInfoPopup(){free=false;bitmap=NULL;}
  193. ~CInfoPopup(){};
  194. };
  195. class SComponent : public ClickableR
  196. {
  197. public:
  198. enum Etype
  199. {
  200. primskill, secskill, resource, creature, artifact, experience, secskill44, spell
  201. } type;
  202. int subtype;
  203. int val;
  204. std::string description; //r-click
  205. std::string subtitle;
  206. void init(Etype Type, int Subtype, int Val);
  207. SComponent(Etype Type, int Subtype, int Val);
  208. SComponent(const Component &c);
  209. SComponent(){};
  210. virtual ~SComponent(){};
  211. void clickRight (boost::logic::tribool down);
  212. virtual SDL_Surface * getImg();
  213. virtual void show(SDL_Surface * to = NULL);
  214. virtual void activate();
  215. virtual void deactivate();
  216. };
  217. class CCustomImgComponent : public SComponent
  218. {
  219. public:
  220. bool free; //should surface be freed on delete
  221. SDL_Surface *bmp;
  222. SDL_Surface * getImg();
  223. CCustomImgComponent(Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur);
  224. ~CCustomImgComponent();
  225. };
  226. class CSelectableComponent : public SComponent, public ClickableL
  227. {
  228. public:
  229. bool selected;
  230. bool customB;
  231. SDL_Surface * border, *myBitmap;
  232. boost::function<void()> onSelect;
  233. void clickLeft(boost::logic::tribool down);
  234. void init(SDL_Surface * Border);
  235. CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect = 0, SDL_Surface * Border=NULL);
  236. CSelectableComponent(const Component &c, boost::function<void()> OnSelect = 0, SDL_Surface * Border=NULL);
  237. ~CSelectableComponent();
  238. virtual void show(SDL_Surface * to = NULL);
  239. void activate();
  240. void deactivate();
  241. void select(bool on);
  242. SDL_Surface * getImg();
  243. };
  244. class CGarrisonInt;
  245. class CGarrisonSlot : public ClickableL, public ClickableR, public Hoverable
  246. {
  247. public:
  248. CGarrisonInt *owner;
  249. const CCreature * creature;
  250. int count;
  251. int upg; //0 - up garrison, 1 - down garrison
  252. bool active;
  253. virtual void hover (bool on);
  254. const CArmedInstance * getObj();
  255. void clickRight (boost::logic::tribool down);
  256. void clickLeft(boost::logic::tribool down);
  257. void activate();
  258. void deactivate();
  259. void show();
  260. CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg=0, const CCreature * Creature=NULL, int Count=0);
  261. ~CGarrisonSlot();
  262. };
  263. class CGarrisonInt :public CIntObject
  264. {
  265. public:
  266. int interx, intery;
  267. CGarrisonSlot *highlighted;
  268. SDL_Surface *sur;
  269. int offx, offy, p2;
  270. bool ignoreEvent, update, active, splitting, pb;
  271. const CCreatureSet *set1;
  272. const CCreatureSet *set2;
  273. std::vector<CGarrisonSlot*> *sup, *sdown;
  274. const CArmedInstance *oup, *odown;
  275. void activate();
  276. void deactivate();
  277. void show();
  278. void activeteSlots();
  279. void deactiveteSlots();
  280. void deleteSlots();
  281. void createSlots();
  282. void recreateSlots();
  283. void splitClick();
  284. void splitStacks(int am2);
  285. CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *pomsur, int OX, int OY, const CArmedInstance *s1, const CArmedInstance *s2=NULL);
  286. ~CGarrisonInt();
  287. };
  288. class CPlayerInterface : public CGameInterface
  289. {
  290. public:
  291. //minor interfaces
  292. CondSh<bool> *showingDialog;
  293. boost::mutex *pim;
  294. bool makingTurn;
  295. SDL_Event * current;
  296. CMainInterface *curint;
  297. CAdvMapInt * adventureInt;
  298. CCastleInterface * castleInt;
  299. FPSmanager * mainFPSmng;
  300. IStatusBar *statusbar;
  301. //to commucate with engine
  302. CCallback * cb;
  303. //GUI elements
  304. std::vector<ClickableL*> lclickable;
  305. std::vector<ClickableR*> rclickable;
  306. std::vector<Hoverable*> hoverable;
  307. std::vector<KeyInterested*> keyinterested;
  308. std::vector<MotionInterested*> motioninterested;
  309. std::vector<TimeInterested*> timeinterested;
  310. std::vector<IShowable*> objsToBlit;
  311. //overloaded funcs from CGameInterface
  312. void buildChanged(const CGTownInstance *town, int buildingID, int what); //what: 1 - built, 2 - demolished
  313. void garrisonChanged(const CGObjectInstance * obj);
  314. void heroArtifactSetChanged(const CGHeroInstance*hero);
  315. void heroCreated(const CGHeroInstance* hero);
  316. void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  317. void heroInGarrisonChange(const CGTownInstance *town);
  318. void heroKilled(const CGHeroInstance* hero);
  319. void heroMoved(const HeroMoveDetails & details);
  320. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val);
  321. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town);
  322. void receivedResource(int type, int val);
  323. void showInfoDialog(std::string &text, const std::vector<Component*> &components);
  324. void showSelDialog(std::string &text, const std::vector<Component*> &components, ui32 askID);
  325. void showYesNoDialog(std::string &text, const std::vector<Component*> &components, ui32 askID);
  326. void tileHidden(int3 pos);
  327. void tileRevealed(int3 pos);
  328. void yourTurn();
  329. void availableCreaturesChanged(const CGTownInstance *town);
  330. //for battles
  331. //void actionFinished(BattleAction action);//occurs AFTER every action taken by any stack or by the hero
  332. //void actionStarted(BattleAction action);//occurs BEFORE every action taken by any stack or by the hero
  333. BattleAction activeStack(int stackID); //called when it's turn of that stack
  334. void battleAttack(BattleAttack *ba);
  335. void battleEnd(BattleResult *br);
  336. void battleNewRound(int round); //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  337. void battleStackIsShooting(int ID, int dest); //called when stack with id ID is shooting to hex dest
  338. void battleStackKilled(int ID, int dmg, int killed, int IDby, bool byShooting);
  339. void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  340. void battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side); //called by engine when battle starts; side=0 - left, side=1 - right
  341. void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  342. //-------------//
  343. void updateWater();
  344. void showComp(SComponent comp);
  345. void openTownWindow(const CGTownInstance * town); //shows townscreen
  346. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  347. SDL_Surface * infoWin(const CGObjectInstance * specific); //specific=0 => draws info about selected town/hero //TODO - gdy sie dorobi sensowna hierarchie klas ins. to wywalic tego brzydkiego void*
  348. void handleEvent(SDL_Event * sEvent);
  349. void handleKeyDown(SDL_Event *sEvent);
  350. void handleKeyUp(SDL_Event *sEvent);
  351. void handleMouseMotion(SDL_Event *sEvent);
  352. void init(ICallback * CB);
  353. int3 repairScreenPos(int3 pos);
  354. void removeObjToBlit(IShowable* obj);
  355. void showInfoDialog(std::string &text, const std::vector<SComponent*> & components);
  356. void showYesNoDialog(std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool deactivateCur, bool DelComps); //deactivateCur - whether current main interface should be deactivated; delComps - if components will be deleted on window close
  357. CPlayerInterface(int Player, int serial);//c-tor
  358. ~CPlayerInterface();//d-tor
  359. };
  360. class CStatusBar
  361. : public CIntObject, public IStatusBar
  362. {
  363. public:
  364. SDL_Surface * bg; //background
  365. int middlex, middley; //middle of statusbar
  366. std::string current; //text currently printed
  367. CStatusBar(int x, int y, std::string name="ADROLLVR.bmp", int maxw=-1); //c-tor
  368. ~CStatusBar(); //d-tor
  369. void print(std::string text); //prints text and refreshes statusbar
  370. void clear();//clears statusbar and refreshes
  371. void show(); //shows statusbar (with current text)
  372. std::string getCurrent();
  373. };
  374. class CList
  375. : public ClickableL, public ClickableR, public Hoverable, public KeyInterested, public virtual CIntObject, public MotionInterested
  376. {
  377. public:
  378. SDL_Surface * bg;
  379. CDefHandler *arrup, *arrdo;
  380. SDL_Surface *empty, *selection;
  381. SDL_Rect arrupp, arrdop; //positions of arrows
  382. int posw, posh; //position width/height
  383. int selected, //id of selected position, <0 if none
  384. from;
  385. const int SIZE;
  386. boost::logic::tribool pressed; //true=up; false=down; indeterminate=none
  387. CList(int Size = 5);
  388. void clickLeft(boost::logic::tribool down);
  389. void activate();
  390. void deactivate();
  391. virtual void mouseMoved (SDL_MouseMotionEvent & sEvent)=0;
  392. virtual void genList()=0;
  393. virtual void select(int which)=0;
  394. virtual void draw()=0;
  395. };
  396. class CHeroList
  397. : public CList
  398. {
  399. public:
  400. CDefHandler *mobile, *mana;
  401. std::vector<std::pair<const CGHeroInstance*, CPath *> > items;
  402. int posmobx, posporx, posmanx, posmoby, pospory, posmany;
  403. CHeroList(int Size = 5);
  404. int getPosOfHero(const CArmedInstance* h);
  405. void genList();
  406. void select(int which);
  407. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  408. void clickLeft(boost::logic::tribool down);
  409. void clickRight(boost::logic::tribool down);
  410. void hover (bool on);
  411. void keyPressed (SDL_KeyboardEvent & key);
  412. void updateHList();
  413. void updateMove(const CGHeroInstance* which); //draws move points bar
  414. void redrawAllOne(int which);
  415. void draw();
  416. void init();
  417. };
  418. class CTownList
  419. : public CList
  420. {
  421. public:
  422. boost::function<void()> fun;
  423. std::vector<const CGTownInstance*> items;
  424. int posporx,pospory;
  425. CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy);
  426. ~CTownList();
  427. void genList();
  428. void select(int which);
  429. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  430. void clickLeft(boost::logic::tribool down);
  431. void clickRight(boost::logic::tribool down);
  432. void hover (bool on);
  433. void keyPressed (SDL_KeyboardEvent & key);
  434. void draw();
  435. };
  436. class CCreaturePic //draws picture with creature on background, use nextFrame=true to get animation
  437. {
  438. public:
  439. bool big; //big => 100x130; !big => 100x120
  440. CCreature *c;
  441. CCreatureAnimation *anim;
  442. CCreaturePic(CCreature *cre, bool Big=true);
  443. ~CCreaturePic();
  444. int blitPic(SDL_Surface *to, int x, int y, bool nextFrame);
  445. SDL_Surface * getPic(bool nextFrame);
  446. };
  447. class CRecrutationWindow : public IShowable, public ClickableL
  448. {
  449. public:
  450. struct creinfo
  451. {
  452. SDL_Rect pos;
  453. CCreaturePic *pic;
  454. int ID, amount; //creature ID and available amount
  455. std::vector<std::pair<int,int> > res; //res_id - cost_per_unit
  456. };
  457. std::vector<int> amounts; //how many creatures we can afford
  458. std::vector<creinfo> creatures;
  459. boost::function<void(int,int)> recruit; //void (int ID, int amount) <-- call to recruit creatures
  460. CSlider *slider;
  461. AdventureMapButton *max, *buy, *cancel;
  462. SDL_Surface *bitmap;
  463. int which; //which creature is active
  464. void close();
  465. void Max();
  466. void Buy();
  467. void Cancel();
  468. void sliderMoved(int to);
  469. void clickLeft(boost::logic::tribool down);
  470. void activate();
  471. void deactivate();
  472. void show(SDL_Surface * to = NULL);
  473. CRecrutationWindow(const std::vector<std::pair<int,int> > & Creatures, const boost::function<void(int,int)> & Recruit); //creatures - pairs<creature_ID,amount>
  474. ~CRecrutationWindow();
  475. };
  476. class CSplitWindow : public IShowable, public KeyInterested
  477. {
  478. public:
  479. CGarrisonInt *gar;
  480. CSlider *slider;
  481. CCreaturePic *anim;
  482. AdventureMapButton *ok, *cancel;
  483. SDL_Surface *bitmap;
  484. int a1, a2, c;
  485. bool which;
  486. CSplitWindow(int cid, int max, CGarrisonInt *Owner);
  487. ~CSplitWindow();
  488. void activate();
  489. void split();
  490. void close();
  491. void deactivate();
  492. void show(SDL_Surface * to = NULL);
  493. void keyPressed (SDL_KeyboardEvent & key);
  494. void sliderMoved(int to);
  495. };
  496. class CCreInfoWindow : public IShowable, public KeyInterested, public ClickableR
  497. {
  498. public:
  499. bool active;
  500. int type;//0 - rclick popup; 1 - normal window
  501. SDL_Surface *bitmap;
  502. char anf;
  503. std::string count; //creature count in text format
  504. boost::function<void()> dsm;
  505. CCreaturePic *anim;
  506. CCreature *c;
  507. CInfoWindow *dependant; //it may be dialog asking whther upgrade/dismiss stack (if opened)
  508. std::vector<SComponent*> upgResCost; //cost of upgrade (if not possible then empty)
  509. AdventureMapButton *dismiss, *upgrade, *ok;
  510. CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui);
  511. ~CCreInfoWindow();
  512. void activate();
  513. void close();
  514. void clickRight(boost::logic::tribool down);
  515. void dismissF();
  516. void keyPressed (SDL_KeyboardEvent & key);
  517. void deactivate();
  518. void show(SDL_Surface * to = NULL);
  519. void onUpgradeYes();
  520. void onUpgradeNo();
  521. };
  522. class CLevelWindow : public IShowable, public CIntObject
  523. {
  524. public:
  525. int heroType;
  526. SDL_Surface *bitmap;
  527. std::vector<CSelectableComponent *> comps; //skills to select
  528. AdventureMapButton *ok;
  529. boost::function<void(ui32)> cb;
  530. void close();
  531. CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  532. ~CLevelWindow();
  533. void activate();
  534. void deactivate();
  535. void selectionChanged(unsigned to);
  536. void show(SDL_Surface * to = NULL);
  537. };
  538. class CMinorResDataBar : public IShowable, public CIntObject
  539. {
  540. public:
  541. SDL_Surface *bg;
  542. void show(SDL_Surface * to=NULL);
  543. CMinorResDataBar();
  544. ~CMinorResDataBar();
  545. };
  546. extern CPlayerInterface * LOCPLINT;
  547. #endif //CPLAYERINTERFACE_H