CPlayerInterface.h 18 KB

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