CPlayerInterface.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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, 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 yourTurn();
  310. void heroMoved(const HeroMoveDetails & details);
  311. void tileRevealed(int3 pos);
  312. void tileHidden(int3 pos);
  313. void heroKilled(const CGHeroInstance* hero);
  314. void heroCreated(const CGHeroInstance* hero);
  315. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val);
  316. void receivedResource(int type, int val);
  317. void showInfoDialog(std::string &text, std::vector<Component*> &components);
  318. void showSelDialog(std::string &text, std::vector<Component*> &components, ui32 askID);
  319. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town);
  320. void garrisonChanged(const CGObjectInstance * obj);
  321. void buildChanged(const CGTownInstance *town, int buildingID, int what); //what: 1 - built, 2 - demolished
  322. void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  323. void heroInGarrisonChange(const CGTownInstance *town);
  324. //for battles
  325. 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
  326. void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  327. void battleNewRound(int round); //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  328. //void actionStarted(BattleAction action);//occurs BEFORE every action taken by any stack or by the hero
  329. //void actionFinished(BattleAction action);//occurs AFTER every action taken by any stack or by the hero
  330. BattleAction activeStack(int stackID); //called when it's turn of that stack
  331. void battleEnd(BattleResult *br);
  332. void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  333. void battleAttack(BattleAttack *ba);
  334. void battleStackKilled(int ID, int dmg, int killed, int IDby, bool byShooting);
  335. void battleStackIsShooting(int ID, int dest); //called when stack with id ID is shooting to hex dest
  336. //-------------//
  337. void showComp(SComponent comp);
  338. void openTownWindow(const CGTownInstance * town); //shows townscreen
  339. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  340. 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*
  341. void handleEvent(SDL_Event * sEvent);
  342. void handleKeyDown(SDL_Event *sEvent);
  343. void handleKeyUp(SDL_Event *sEvent);
  344. void handleMouseMotion(SDL_Event *sEvent);
  345. void init(ICallback * CB);
  346. int3 repairScreenPos(int3 pos);
  347. void removeObjToBlit(IShowable* obj);
  348. void showInfoDialog(std::string &text, std::vector<SComponent*> & components);
  349. void showYesNoDialog(std::string &text, 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
  350. CPlayerInterface(int Player, int serial);//c-tor
  351. ~CPlayerInterface();//d-tor
  352. };
  353. class CStatusBar
  354. : public CIntObject, public IStatusBar
  355. {
  356. public:
  357. SDL_Surface * bg; //background
  358. int middlex, middley; //middle of statusbar
  359. std::string current; //text currently printed
  360. CStatusBar(int x, int y, std::string name="ADROLLVR.bmp", int maxw=-1); //c-tor
  361. ~CStatusBar(); //d-tor
  362. void print(std::string text); //prints text and refreshes statusbar
  363. void clear();//clears statusbar and refreshes
  364. void show(); //shows statusbar (with current text)
  365. std::string getCurrent();
  366. };
  367. class CList
  368. : public ClickableL, public ClickableR, public Hoverable, public KeyInterested, public virtual CIntObject, public MotionInterested
  369. {
  370. public:
  371. SDL_Surface * bg;
  372. CDefHandler *arrup, *arrdo;
  373. SDL_Surface *empty, *selection;
  374. SDL_Rect arrupp, arrdop; //positions of arrows
  375. int posw, posh; //position width/height
  376. int selected, //id of selected position, <0 if none
  377. from;
  378. const int SIZE;
  379. boost::logic::tribool pressed; //true=up; false=down; indeterminate=none
  380. CList(int Size = 5);
  381. void clickLeft(boost::logic::tribool down);
  382. void activate();
  383. void deactivate();
  384. virtual void mouseMoved (SDL_MouseMotionEvent & sEvent)=0;
  385. virtual void genList()=0;
  386. virtual void select(int which)=0;
  387. virtual void draw()=0;
  388. };
  389. class CHeroList
  390. : public CList
  391. {
  392. public:
  393. CDefHandler *mobile, *mana;
  394. std::vector<std::pair<const CGHeroInstance*, CPath *> > items;
  395. int posmobx, posporx, posmanx, posmoby, pospory, posmany;
  396. CHeroList(int Size = 5);
  397. void genList();
  398. void select(int which);
  399. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  400. void clickLeft(boost::logic::tribool down);
  401. void clickRight(boost::logic::tribool down);
  402. void hover (bool on);
  403. void keyPressed (SDL_KeyboardEvent & key);
  404. void updateHList();
  405. void updateMove(const CGHeroInstance* which); //draws move points bar
  406. void redrawAllOne(int which);
  407. void draw();
  408. void init();
  409. };
  410. class CTownList
  411. : public CList
  412. {
  413. public:
  414. boost::function<void()> fun;
  415. std::vector<const CGTownInstance*> items;
  416. int posporx,pospory;
  417. CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy);
  418. ~CTownList();
  419. void genList();
  420. void select(int which);
  421. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  422. void clickLeft(boost::logic::tribool down);
  423. void clickRight(boost::logic::tribool down);
  424. void hover (bool on);
  425. void keyPressed (SDL_KeyboardEvent & key);
  426. void draw();
  427. };
  428. class CCreaturePic //draws picture with creature on background, use nextFrame=true to get animation
  429. {
  430. public:
  431. bool big; //big => 100x130; !big => 100x120
  432. CCreature *c;
  433. CCreatureAnimation *anim;
  434. CCreaturePic(CCreature *cre, bool Big=true);
  435. ~CCreaturePic();
  436. int blitPic(SDL_Surface *to, int x, int y, bool nextFrame);
  437. SDL_Surface * getPic(bool nextFrame);
  438. };
  439. class CRecrutationWindow : public IShowable, public ClickableL
  440. {
  441. public:
  442. struct creinfo
  443. {
  444. SDL_Rect pos;
  445. CCreaturePic *pic;
  446. int ID, amount; //creature ID and available amount
  447. std::vector<std::pair<int,int> > res; //res_id - cost_per_unit
  448. };
  449. std::vector<int> amounts; //how many creatures we can afford
  450. std::vector<creinfo> creatures;
  451. boost::function<void(int,int)> recruit; //void (int ID, int amount) <-- call to recruit creatures
  452. CSlider *slider;
  453. AdventureMapButton *max, *buy, *cancel;
  454. SDL_Surface *bitmap;
  455. int which; //which creature is active
  456. void close();
  457. void Max();
  458. void Buy();
  459. void Cancel();
  460. void sliderMoved(int to);
  461. void clickLeft(boost::logic::tribool down);
  462. void activate();
  463. void deactivate();
  464. void show(SDL_Surface * to = NULL);
  465. CRecrutationWindow(const std::vector<std::pair<int,int> > & Creatures, const boost::function<void(int,int)> & Recruit); //creatures - pairs<creature_ID,amount>
  466. ~CRecrutationWindow();
  467. };
  468. class CSplitWindow : public IShowable, public KeyInterested
  469. {
  470. public:
  471. CGarrisonInt *gar;
  472. CSlider *slider;
  473. CCreaturePic *anim;
  474. AdventureMapButton *ok, *cancel;
  475. SDL_Surface *bitmap;
  476. int a1, a2, c;
  477. bool which;
  478. CSplitWindow(int cid, int max, CGarrisonInt *Owner);
  479. ~CSplitWindow();
  480. void activate();
  481. void split();
  482. void close();
  483. void deactivate();
  484. void show(SDL_Surface * to = NULL);
  485. void keyPressed (SDL_KeyboardEvent & key);
  486. void sliderMoved(int to);
  487. };
  488. class CCreInfoWindow : public IShowable, public KeyInterested, public ClickableR
  489. {
  490. public:
  491. bool active;
  492. int type;//0 - rclick popup; 1 - normal window
  493. SDL_Surface *bitmap;
  494. char anf;
  495. std::string count; //creature count in text format
  496. boost::function<void()> dsm;
  497. CCreaturePic *anim;
  498. CCreature *c;
  499. CInfoWindow *dependant; //it may be dialog asking whther upgrade/dismiss stack (if opened)
  500. std::vector<SComponent*> upgResCost; //cost of upgrade (if not possible then empty)
  501. AdventureMapButton *dismiss, *upgrade, *ok;
  502. CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui);
  503. ~CCreInfoWindow();
  504. void activate();
  505. void close();
  506. void clickRight(boost::logic::tribool down);
  507. void dismissF();
  508. void keyPressed (SDL_KeyboardEvent & key);
  509. void deactivate();
  510. void show(SDL_Surface * to = NULL);
  511. void onUpgradeYes();
  512. void onUpgradeNo();
  513. };
  514. class CLevelWindow : public IShowable, public CIntObject
  515. {
  516. public:
  517. int heroType;
  518. SDL_Surface *bitmap;
  519. std::vector<CSelectableComponent *> comps; //skills to select
  520. AdventureMapButton *ok;
  521. boost::function<void(ui32)> cb;
  522. void close();
  523. CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  524. ~CLevelWindow();
  525. void activate();
  526. void deactivate();
  527. void selectionChanged(unsigned to);
  528. void show(SDL_Surface * to = NULL);
  529. };
  530. class CMinorResDataBar : public IShowable, public CIntObject
  531. {
  532. public:
  533. SDL_Surface *bg;
  534. void show(SDL_Surface * to=NULL);
  535. CMinorResDataBar();
  536. ~CMinorResDataBar();
  537. };
  538. extern CPlayerInterface * LOCPLINT;
  539. #endif //CPLAYERINTERFACE_H