CPlayerInterface.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. template <typename T> class CSCButton: public CButtonBase, public ClickableL //prosty guzik, ktory tylko zmienia obrazek
  154. {
  155. public:
  156. int3 posr; //position in the bitmap
  157. int state;
  158. T* delg;
  159. void(T::*func)(boost::logic::tribool);
  160. CSCButton(CDefHandler * img, CIntObject * obj, void(T::*poin)(boost::logic::tribool), T* Delg=NULL);
  161. void clickLeft (boost::logic::tribool down);
  162. void activate();
  163. void deactivate();
  164. void show(SDL_Surface * to = NULL);
  165. };
  166. class CInfoWindow : public CSimpleWindow //text + comp. + ok button
  167. { //window able to delete its components when closed
  168. public:
  169. bool delComps; //whether comps will be deleted
  170. std::vector<AdventureMapButton *> buttons;
  171. std::vector<SComponent*> components;
  172. virtual void close();
  173. virtual void show(SDL_Surface * to = NULL);
  174. void activate();
  175. void deactivate();
  176. CInfoWindow(std::string text, int player, int charperline, const std::vector<SComponent*> &comps, std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons);
  177. CInfoWindow();
  178. ~CInfoWindow();
  179. };
  180. class CSelWindow : public CInfoWindow //component selection window
  181. { //uwaga - to okno usuwa swoje komponenty przy zamykaniu
  182. public:
  183. void selectionChange(unsigned to);
  184. void close();
  185. CSelWindow(std::string text, int player, int charperline, std::vector<CSelectableComponent*> &comps, std::vector<std::pair<std::string,boost::function<void()> > > &Buttons);
  186. CSelWindow(){};
  187. };
  188. class CRClickPopup : public IShowable, public ClickableR
  189. {
  190. public:
  191. virtual void activate();
  192. virtual void deactivate();
  193. virtual void close()=0;
  194. void clickRight (boost::logic::tribool down);
  195. virtual ~CRClickPopup(){};
  196. };
  197. class CInfoPopup : public CRClickPopup
  198. {
  199. public:
  200. bool free;
  201. SDL_Surface * bitmap;
  202. CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free=false);
  203. void close();
  204. void show(SDL_Surface * to = NULL);
  205. CInfoPopup(){free=false;bitmap=NULL;}
  206. ~CInfoPopup(){};
  207. };
  208. class SComponent : public ClickableR
  209. {
  210. public:
  211. enum Etype
  212. {
  213. primskill, secskill, resource, creature, artifact, experience, secskill44
  214. } type;
  215. int subtype;
  216. int val;
  217. std::string description; //r-click
  218. std::string subtitle;
  219. void init(Etype Type, int Subtype, int Val);
  220. SComponent(Etype Type, int Subtype, int Val);
  221. SComponent(const Component &c);
  222. void clickRight (boost::logic::tribool down);
  223. virtual SDL_Surface * getImg();
  224. virtual void show(SDL_Surface * to = NULL);
  225. virtual void activate();
  226. virtual void deactivate();
  227. };
  228. class CSelectableComponent : public SComponent, public ClickableL
  229. {
  230. public:
  231. bool selected;
  232. bool customB;
  233. SDL_Surface * border, *myBitmap;
  234. boost::function<void()> onSelect;
  235. void clickLeft(boost::logic::tribool down);
  236. void init(SDL_Surface * Border);
  237. CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect = 0, SDL_Surface * Border=NULL);
  238. CSelectableComponent(const Component &c, boost::function<void()> OnSelect = 0, SDL_Surface * Border=NULL);
  239. ~CSelectableComponent();
  240. virtual void show(SDL_Surface * to = NULL);
  241. void activate();
  242. void deactivate();
  243. void select(bool on);
  244. SDL_Surface * getImg();
  245. };
  246. class CGarrisonInt;
  247. class CGarrisonSlot : public ClickableL, public ClickableR, public Hoverable
  248. {
  249. public:
  250. CGarrisonInt *owner;
  251. const CCreature * creature;
  252. int count;
  253. int upg; //0 - up garrison, 1 - down garrison
  254. bool active;
  255. virtual void hover (bool on);
  256. const CArmedInstance * getObj();
  257. void clickRight (boost::logic::tribool down);
  258. void clickLeft(boost::logic::tribool down);
  259. void activate();
  260. void deactivate();
  261. void show();
  262. CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg=0, const CCreature * Creature=NULL, int Count=0);
  263. ~CGarrisonSlot();
  264. };
  265. class CGarrisonInt :public CIntObject
  266. {
  267. public:
  268. int interx, intery;
  269. CGarrisonSlot *highlighted;
  270. SDL_Surface *sur;
  271. int offx, offy, p2;
  272. bool ignoreEvent, update, active, splitting, pb;
  273. const CCreatureSet *set1;
  274. const CCreatureSet *set2;
  275. std::vector<CGarrisonSlot*> *sup, *sdown;
  276. const CArmedInstance *oup, *odown;
  277. void activate();
  278. void deactivate();
  279. void show();
  280. void activeteSlots();
  281. void deactiveteSlots();
  282. void deleteSlots();
  283. void createSlots();
  284. void recreateSlots();
  285. void splitClick();
  286. void splitStacks(int am2);
  287. CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *pomsur, int OX, int OY, const CArmedInstance *s1, const CArmedInstance *s2=NULL);
  288. ~CGarrisonInt();
  289. };
  290. class CPlayerInterface : public CGameInterface
  291. {
  292. public:
  293. //minor interfaces
  294. CondSh<bool> *showingDialog;
  295. boost::mutex *pim;
  296. bool makingTurn;
  297. SDL_Event * current;
  298. CMainInterface *curint;
  299. CAdvMapInt * adventureInt;
  300. CCastleInterface * castleInt;
  301. FPSmanager * mainFPSmng;
  302. IStatusBar *statusbar;
  303. //to commucate with engine
  304. CCallback * cb;
  305. //GUI elements
  306. std::vector<ClickableL*> lclickable;
  307. std::vector<ClickableR*> rclickable;
  308. std::vector<Hoverable*> hoverable;
  309. std::vector<KeyInterested*> keyinterested;
  310. std::vector<MotionInterested*> motioninterested;
  311. std::vector<TimeInterested*> timeinterested;
  312. std::vector<IShowable*> objsToBlit;
  313. //overloaded funcs from CGameInterface
  314. void buildChanged(const CGTownInstance *town, int buildingID, int what); //what: 1 - built, 2 - demolished
  315. void garrisonChanged(const CGObjectInstance * obj);
  316. void heroArtifactSetChanged(const CGHeroInstance*hero);
  317. void heroCreated(const CGHeroInstance* hero);
  318. void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  319. void heroInGarrisonChange(const CGTownInstance *town);
  320. void heroKilled(const CGHeroInstance* hero);
  321. void heroMoved(const HeroMoveDetails & details);
  322. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val);
  323. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town);
  324. void receivedResource(int type, int val);
  325. void showInfoDialog(std::string &text, const std::vector<Component*> &components);
  326. void showSelDialog(std::string &text, const std::vector<Component*> &components, ui32 askID);
  327. void showYesNoDialog(std::string &text, const std::vector<Component*> &components, ui32 askID);
  328. void tileHidden(int3 pos);
  329. void tileRevealed(int3 pos);
  330. void yourTurn();
  331. //for battles
  332. //void actionFinished(BattleAction action);//occurs AFTER every action taken by any stack or by the hero
  333. //void actionStarted(BattleAction action);//occurs BEFORE every action taken by any stack or by the hero
  334. BattleAction activeStack(int stackID); //called when it's turn of that stack
  335. void battleAttack(BattleAttack *ba);
  336. void battleEnd(BattleResult *br);
  337. void battleNewRound(int round); //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  338. void battleStackIsShooting(int ID, int dest); //called when stack with id ID is shooting to hex dest
  339. void battleStackKilled(int ID, int dmg, int killed, int IDby, bool byShooting);
  340. void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  341. 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
  342. void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  343. //-------------//
  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. void genList();
  405. void select(int which);
  406. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  407. void clickLeft(boost::logic::tribool down);
  408. void clickRight(boost::logic::tribool down);
  409. void hover (bool on);
  410. void keyPressed (SDL_KeyboardEvent & key);
  411. void updateHList();
  412. void updateMove(const CGHeroInstance* which); //draws move points bar
  413. void redrawAllOne(int which);
  414. void draw();
  415. void init();
  416. };
  417. class CTownList
  418. : public CList
  419. {
  420. public:
  421. boost::function<void()> fun;
  422. std::vector<const CGTownInstance*> items;
  423. int posporx,pospory;
  424. CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy);
  425. ~CTownList();
  426. void genList();
  427. void select(int which);
  428. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  429. void clickLeft(boost::logic::tribool down);
  430. void clickRight(boost::logic::tribool down);
  431. void hover (bool on);
  432. void keyPressed (SDL_KeyboardEvent & key);
  433. void draw();
  434. };
  435. class CCreaturePic //draws picture with creature on background, use nextFrame=true to get animation
  436. {
  437. public:
  438. bool big; //big => 100x130; !big => 100x120
  439. CCreature *c;
  440. CCreatureAnimation *anim;
  441. CCreaturePic(CCreature *cre, bool Big=true);
  442. ~CCreaturePic();
  443. int blitPic(SDL_Surface *to, int x, int y, bool nextFrame);
  444. SDL_Surface * getPic(bool nextFrame);
  445. };
  446. class CRecrutationWindow : public IShowable, public ClickableL
  447. {
  448. public:
  449. struct creinfo
  450. {
  451. SDL_Rect pos;
  452. CCreaturePic *pic;
  453. int ID, amount; //creature ID and available amount
  454. std::vector<std::pair<int,int> > res; //res_id - cost_per_unit
  455. };
  456. std::vector<int> amounts; //how many creatures we can afford
  457. std::vector<creinfo> creatures;
  458. boost::function<void(int,int)> recruit; //void (int ID, int amount) <-- call to recruit creatures
  459. CSlider *slider;
  460. AdventureMapButton *max, *buy, *cancel;
  461. SDL_Surface *bitmap;
  462. int which; //which creature is active
  463. void close();
  464. void Max();
  465. void Buy();
  466. void Cancel();
  467. void sliderMoved(int to);
  468. void clickLeft(boost::logic::tribool down);
  469. void activate();
  470. void deactivate();
  471. void show(SDL_Surface * to = NULL);
  472. CRecrutationWindow(const std::vector<std::pair<int,int> > & Creatures, const boost::function<void(int,int)> & Recruit); //creatures - pairs<creature_ID,amount>
  473. ~CRecrutationWindow();
  474. };
  475. class CSplitWindow : public IShowable, public KeyInterested
  476. {
  477. public:
  478. CGarrisonInt *gar;
  479. CSlider *slider;
  480. CCreaturePic *anim;
  481. AdventureMapButton *ok, *cancel;
  482. SDL_Surface *bitmap;
  483. int a1, a2, c;
  484. bool which;
  485. CSplitWindow(int cid, int max, CGarrisonInt *Owner);
  486. ~CSplitWindow();
  487. void activate();
  488. void split();
  489. void close();
  490. void deactivate();
  491. void show(SDL_Surface * to = NULL);
  492. void keyPressed (SDL_KeyboardEvent & key);
  493. void sliderMoved(int to);
  494. };
  495. class CCreInfoWindow : public IShowable, public KeyInterested, public ClickableR
  496. {
  497. public:
  498. bool active;
  499. int type;//0 - rclick popup; 1 - normal window
  500. SDL_Surface *bitmap;
  501. char anf;
  502. std::string count; //creature count in text format
  503. boost::function<void()> dsm;
  504. CCreaturePic *anim;
  505. CCreature *c;
  506. CInfoWindow *dependant; //it may be dialog asking whther upgrade/dismiss stack (if opened)
  507. std::vector<SComponent*> upgResCost; //cost of upgrade (if not possible then empty)
  508. AdventureMapButton *dismiss, *upgrade, *ok;
  509. CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui);
  510. ~CCreInfoWindow();
  511. void activate();
  512. void close();
  513. void clickRight(boost::logic::tribool down);
  514. void dismissF();
  515. void keyPressed (SDL_KeyboardEvent & key);
  516. void deactivate();
  517. void show(SDL_Surface * to = NULL);
  518. void onUpgradeYes();
  519. void onUpgradeNo();
  520. };
  521. class CLevelWindow : public IShowable, public CIntObject
  522. {
  523. public:
  524. int heroType;
  525. SDL_Surface *bitmap;
  526. std::vector<CSelectableComponent *> comps; //skills to select
  527. AdventureMapButton *ok;
  528. boost::function<void(ui32)> cb;
  529. void close();
  530. CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  531. ~CLevelWindow();
  532. void activate();
  533. void deactivate();
  534. void selectionChanged(unsigned to);
  535. void show(SDL_Surface * to = NULL);
  536. };
  537. class CMinorResDataBar : public IShowable, public CIntObject
  538. {
  539. public:
  540. SDL_Surface *bg;
  541. void show(SDL_Surface * to=NULL);
  542. CMinorResDataBar();
  543. ~CMinorResDataBar();
  544. };
  545. extern CPlayerInterface * LOCPLINT;
  546. #endif //CPLAYERINTERFACE_H