CPlayerInterface.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. #include <boost/function.hpp>
  8. class CDefEssential;
  9. class AdventureMapButton;
  10. class CDefHandler;
  11. struct HeroMoveDetails;
  12. class CDefEssential;
  13. class CGHeroInstance;
  14. class CAdvMapInt;
  15. class CCastleInterface;
  16. class CStack;
  17. class SComponent;
  18. class CCreature;
  19. struct SDL_Surface;
  20. struct CPath;
  21. class CCreatureAnimation;
  22. class CSelectableComponent;
  23. class CCreatureSet;
  24. class CGObjectInstance;
  25. class CSlider;
  26. class IShowable
  27. {
  28. public:
  29. virtual void show(SDL_Surface * to = NULL)=0;
  30. };
  31. class IStatusBar
  32. {
  33. public:
  34. virtual ~IStatusBar(){}; //d-tor
  35. virtual void print(std::string text)=0; //prints text and refreshes statusbar
  36. virtual void clear()=0;//clears statusbar and refreshes
  37. virtual void show()=0; //shows statusbar (with current text)
  38. virtual std::string getCurrent()=0;
  39. };
  40. class IActivable
  41. {
  42. public:
  43. virtual void activate()=0;
  44. virtual void deactivate()=0;
  45. virtual ~IActivable(){};
  46. };
  47. class CIntObject //interface object
  48. {
  49. public:
  50. SDL_Rect pos;
  51. int ID;
  52. };
  53. class CSimpleWindow : public virtual CIntObject, public IShowable
  54. {
  55. public:
  56. SDL_Surface * bitmap;
  57. CIntObject * owner;
  58. virtual void show(SDL_Surface * to = NULL);
  59. CSimpleWindow():bitmap(NULL),owner(NULL){};
  60. virtual ~CSimpleWindow();
  61. };
  62. class CButtonBase : public virtual CIntObject, public IShowable, public IActivable //basic buttton class
  63. {
  64. public:
  65. int bitmapOffset;
  66. int type; //advmapbutton=2
  67. bool abs;
  68. bool active;
  69. bool notFreeButton;
  70. CIntObject * ourObj; // "owner"
  71. int state;
  72. std::vector< std::vector<SDL_Surface*> > imgs;
  73. int curimg;
  74. virtual void show(SDL_Surface * to = NULL);
  75. virtual void activate()=0;
  76. virtual void deactivate()=0;
  77. CButtonBase();
  78. virtual ~CButtonBase();
  79. };
  80. class ClickableL : public virtual CIntObject //for left-clicks
  81. {
  82. public:
  83. bool pressedL;
  84. ClickableL();
  85. virtual ~ClickableL(){};
  86. virtual void clickLeft (boost::logic::tribool down)=0;
  87. virtual void activate()=0;
  88. virtual void deactivate()=0;
  89. };
  90. class ClickableR : public virtual CIntObject //for right-clicks
  91. {
  92. public:
  93. bool pressedR;
  94. ClickableR();
  95. virtual ~ClickableR(){};
  96. virtual void clickRight (boost::logic::tribool down)=0;
  97. virtual void activate()=0;
  98. virtual void deactivate()=0;
  99. };
  100. class Hoverable : public virtual CIntObject
  101. {
  102. public:
  103. Hoverable(){hovered=false;}
  104. virtual ~Hoverable(){};
  105. bool hovered;
  106. virtual void hover (bool on)=0;
  107. virtual void activate()=0;
  108. virtual void deactivate()=0;
  109. };
  110. class KeyInterested : public virtual CIntObject
  111. {
  112. public:
  113. virtual ~KeyInterested(){};
  114. virtual void keyPressed (SDL_KeyboardEvent & key)=0;
  115. virtual void activate()=0;
  116. virtual void deactivate()=0;
  117. };
  118. class MotionInterested: public virtual CIntObject
  119. {
  120. public:
  121. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  122. MotionInterested(){strongInterest=false;};
  123. virtual ~MotionInterested(){};
  124. virtual void mouseMoved (SDL_MouseMotionEvent & sEvent)=0;
  125. virtual void activate()=0;
  126. virtual void deactivate()=0;
  127. };
  128. class TimeInterested: public virtual CIntObject
  129. {
  130. public:
  131. virtual ~TimeInterested(){};
  132. int toNextTick;
  133. virtual void tick()=0;
  134. virtual void activate();
  135. virtual void deactivate();
  136. };
  137. template <typename T> class CSCButton: public CButtonBase, public ClickableL //prosty guzik, ktory tylko zmienia obrazek
  138. {
  139. public:
  140. int3 posr; //position in the bitmap
  141. int state;
  142. T* delg;
  143. void(T::*func)(boost::logic::tribool);
  144. CSCButton(CDefHandler * img, CIntObject * obj, void(T::*poin)(boost::logic::tribool), T* Delg=NULL);
  145. void clickLeft (boost::logic::tribool down);
  146. void activate();
  147. void deactivate();
  148. void show(SDL_Surface * to = NULL);
  149. };
  150. class CInfoWindow : public CSimpleWindow //text + comp. + ok button
  151. { //okno usuwa swoje komponenty w chwili zamkniecia
  152. public:
  153. CSCButton<CInfoWindow> okb;
  154. std::vector<SComponent*> components;
  155. virtual void okClicked(boost::logic::tribool down);
  156. virtual void close();
  157. CInfoWindow();
  158. virtual ~CInfoWindow();
  159. };
  160. class CSelWindow : public CInfoWindow //component selection window
  161. { //uwaga - to okno nie usuwa swoich komponentow przy usuwaniu, moga sie one jeszcze przydac skryptowi - tak wiec skrypt korzystajacyz tego okna musi je usunac
  162. public:
  163. void selectionChange(CSelectableComponent * to);
  164. void okClicked(boost::logic::tribool down);
  165. void close();
  166. CSelWindow(){};
  167. };
  168. class CRClickPopup : public IShowable, public ClickableR
  169. {
  170. public:
  171. virtual void activate();
  172. virtual void deactivate();
  173. virtual void close()=0;
  174. void clickRight (boost::logic::tribool down);
  175. virtual ~CRClickPopup(){};
  176. };
  177. class CInfoPopup : public CRClickPopup
  178. {
  179. public:
  180. bool free;
  181. SDL_Surface * bitmap;
  182. CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free=false);
  183. void close();
  184. void show(SDL_Surface * to = NULL);
  185. CInfoPopup(){free=false;bitmap=NULL;}
  186. ~CInfoPopup(){};
  187. };
  188. class SComponent : public ClickableR
  189. {
  190. public:
  191. enum Etype
  192. {
  193. primskill, secskill, resource, creature, artifact, experience
  194. } type;
  195. int subtype;
  196. int val;
  197. std::string description; //r-click
  198. std::string subtitle;
  199. SComponent(Etype Type, int Subtype, int Val);
  200. //SComponent(const & SComponent r);
  201. void clickRight (boost::logic::tribool down);
  202. virtual SDL_Surface * getImg();
  203. virtual void activate();
  204. virtual void deactivate();
  205. };
  206. class CSelectableComponent : public SComponent, public ClickableL
  207. {
  208. public:
  209. bool selected;
  210. bool customB;
  211. SDL_Surface * border, *myBitmap;
  212. CSelWindow * owner;
  213. void clickLeft(boost::logic::tribool down);
  214. CSelectableComponent(Etype Type, int Sub, int Val, CSelWindow * Owner=NULL, SDL_Surface * Border=NULL);
  215. ~CSelectableComponent();
  216. void activate();
  217. void deactivate();
  218. void select(bool on);
  219. SDL_Surface * getImg();
  220. };
  221. class CGarrisonInt;
  222. class CGarrisonSlot : public ClickableL, public ClickableR, public Hoverable
  223. {
  224. public:
  225. CGarrisonInt *owner;
  226. const CCreature * creature;
  227. int count;
  228. int upg; //0 - up garrison, 1 - down garrison
  229. bool active;
  230. virtual void hover (bool on);
  231. const CArmedInstance * getObj();
  232. void clickRight (boost::logic::tribool down);
  233. void clickLeft(boost::logic::tribool down);
  234. void activate();
  235. void deactivate();
  236. void show();
  237. CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg=0, const CCreature * Creature=NULL, int Count=0);
  238. ~CGarrisonSlot();
  239. };
  240. class CGarrisonInt :public CIntObject
  241. {
  242. public:
  243. int interx, intery;
  244. CGarrisonSlot *highlighted;
  245. SDL_Surface *sur;
  246. int offx, offy, p2;
  247. bool ignoreEvent, update, active, splitting, pb;
  248. const CCreatureSet *set1;
  249. const CCreatureSet *set2;
  250. std::vector<CGarrisonSlot*> *sup, *sdown;
  251. const CArmedInstance *oup, *odown;
  252. void activate();
  253. void deactivate();
  254. void show();
  255. void activeteSlots();
  256. void deactiveteSlots();
  257. void deleteSlots();
  258. void createSlots();
  259. void recreateSlots();
  260. void splitClick();
  261. void splitStacks(int am2);
  262. CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *pomsur, int OX, int OY, const CArmedInstance *s1, const CArmedInstance *s2=NULL);
  263. ~CGarrisonInt();
  264. };
  265. class CPlayerInterface : public CGameInterface
  266. {
  267. public:
  268. bool makingTurn;
  269. SDL_Event * current;
  270. IActivable *curint;
  271. CAdvMapInt * adventureInt;
  272. CCastleInterface * castleInt;
  273. FPSmanager * mainFPSmng;
  274. IStatusBar *statusbar;
  275. //TODO: town interace, battle interface, other interfaces
  276. CCallback * cb;
  277. std::vector<ClickableL*> lclickable;
  278. std::vector<ClickableR*> rclickable;
  279. std::vector<Hoverable*> hoverable;
  280. std::vector<KeyInterested*> keyinterested;
  281. std::vector<MotionInterested*> motioninterested;
  282. std::vector<TimeInterested*> timeinterested;
  283. std::vector<IShowable*> objsToBlit;
  284. SDL_Surface * hInfo, *tInfo;
  285. std::vector<std::pair<int, int> > slotsPos;
  286. CDefEssential *luck22, *luck30, *luck42, *luck82,
  287. *morale22, *morale30, *morale42, *morale82,
  288. *halls, *forts, *bigTownPic;
  289. std::map<int,SDL_Surface*> heroWins;
  290. std::map<int,SDL_Surface*> townWins;
  291. //overloaded funcs from Interface
  292. void yourTurn();
  293. void heroMoved(const HeroMoveDetails & details);
  294. void tileRevealed(int3 pos);
  295. void tileHidden(int3 pos);
  296. void heroKilled(const CGHeroInstance* hero);
  297. void heroCreated(const CGHeroInstance* hero);
  298. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val);
  299. void receivedResource(int type, int val);
  300. void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID);
  301. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town);
  302. void garrisonChanged(const CGObjectInstance * obj);
  303. void buildChanged(const CGTownInstance *town, int buildingID, int what); //what: 1 - built, 2 - demolished
  304. //battles
  305. void battleStart(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, boost::logic::tribool side); //called by engine when battle starts; side=0 - left, side=1 - right
  306. void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  307. void battleNewRound(int round); //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  308. void actionStarted(BattleAction action);//occurs BEFORE every action taken by any stack or by the hero
  309. void actionFinished(BattleAction action);//occurs AFTER every action taken by any stack or by the hero
  310. BattleAction activeStack(int stackID); //called when it's turn of that stack
  311. void battleEnd(CCreatureSet * army1, CCreatureSet * army2, CArmedInstance *hero1, CArmedInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner);
  312. void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  313. void battleStackAttacking(int ID, int dest);
  314. void battleStackIsAttacked(int ID);
  315. void battleStackKilled(int ID);
  316. //-------------//
  317. void showComp(SComponent comp);
  318. void openTownWindow(const CGTownInstance * town); //shows townscreen
  319. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  320. 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*
  321. void handleEvent(SDL_Event * sEvent);
  322. void handleKeyDown(SDL_Event *sEvent);
  323. void handleKeyUp(SDL_Event *sEvent);
  324. void handleMouseMotion(SDL_Event *sEvent);
  325. void init(ICallback * CB);
  326. int3 repairScreenPos(int3 pos);
  327. void showInfoDialog(std::string text, std::vector<SComponent*> & components);
  328. void removeObjToBlit(IShowable* obj);
  329. SDL_Surface * drawHeroInfoWin(const CGHeroInstance * curh);
  330. SDL_Surface * drawPrimarySkill(const CGHeroInstance *curh, SDL_Surface *ret, int from=0, int to=PRIMARY_SKILLS);
  331. SDL_Surface * drawTownInfoWin(const CGTownInstance * curh);
  332. CPlayerInterface(int Player, int serial);
  333. };
  334. class CStatusBar
  335. : public CIntObject, public IStatusBar
  336. {
  337. public:
  338. SDL_Surface * bg; //background
  339. int middlex, middley; //middle of statusbar
  340. std::string current; //text currently printed
  341. CStatusBar(int x, int y, std::string name="ADROLLVR.bmp", int maxw=-1); //c-tor
  342. ~CStatusBar(); //d-tor
  343. void print(std::string text); //prints text and refreshes statusbar
  344. void clear();//clears statusbar and refreshes
  345. void show(); //shows statusbar (with current text)
  346. std::string getCurrent();
  347. };
  348. class CList
  349. : public ClickableL, public ClickableR, public Hoverable, public KeyInterested, public virtual CIntObject, public MotionInterested
  350. {
  351. public:
  352. SDL_Surface * bg;
  353. CDefHandler *arrup, *arrdo;
  354. SDL_Surface *empty, *selection;
  355. SDL_Rect arrupp, arrdop; //positions of arrows
  356. int posw, posh; //position width/height
  357. int selected, //id of selected position, <0 if none
  358. from;
  359. const int SIZE;
  360. boost::logic::tribool pressed; //true=up; false=down; indeterminate=none
  361. CList(int Size = 5);
  362. void clickLeft(boost::logic::tribool down);
  363. void activate();
  364. void deactivate();
  365. virtual void mouseMoved (SDL_MouseMotionEvent & sEvent)=0;
  366. virtual void genList()=0;
  367. virtual void select(int which)=0;
  368. virtual void draw()=0;
  369. };
  370. class CHeroList
  371. : public CList
  372. {
  373. public:
  374. CDefHandler *mobile, *mana;
  375. std::vector<std::pair<const CGHeroInstance*, CPath *> > items;
  376. int posmobx, posporx, posmanx, posmoby, pospory, posmany;
  377. CHeroList(int Size = 5);
  378. void genList();
  379. void select(int which);
  380. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  381. void clickLeft(boost::logic::tribool down);
  382. void clickRight(boost::logic::tribool down);
  383. void hover (bool on);
  384. void keyPressed (SDL_KeyboardEvent & key);
  385. void updateHList();
  386. void updateMove(const CGHeroInstance* which); //draws move points bar
  387. void redrawAllOne(int which);
  388. void draw();
  389. void init();
  390. };
  391. class CTownList
  392. : public CList
  393. {
  394. public:
  395. boost::function<void()> fun;
  396. std::vector<const CGTownInstance*> items;
  397. int posporx,pospory;
  398. CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy);
  399. ~CTownList();
  400. void genList();
  401. void select(int which);
  402. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  403. void clickLeft(boost::logic::tribool down);
  404. void clickRight(boost::logic::tribool down);
  405. void hover (bool on);
  406. void keyPressed (SDL_KeyboardEvent & key);
  407. void draw();
  408. };
  409. class CCreaturePic //draws 100x130 picture with creature on background, use nextFrame=true to get animation
  410. {
  411. public:
  412. CCreature *c;
  413. CCreatureAnimation *anim;
  414. CCreaturePic(CCreature *cre);
  415. ~CCreaturePic();
  416. int blitPic(SDL_Surface *to, int x, int y, bool nextFrame);
  417. SDL_Surface * getPic(bool nextFrame);
  418. };
  419. class CRecrutationWindow : public IShowable, public ClickableL
  420. {
  421. public:
  422. struct creinfo
  423. {
  424. SDL_Rect pos;
  425. CCreaturePic *pic;
  426. int ID, amount; //creature ID and available amount
  427. std::vector<std::pair<int,int> > res; //res_id - cost_per_unit
  428. };
  429. std::vector<int> amounts; //how many creatures we can afford
  430. std::vector<creinfo> creatures;
  431. boost::function<void(int,int)> recruit; //void (int ID, int amount) <-- call to recruit creatures
  432. CSlider *slider;
  433. AdventureMapButton *max, *buy, *cancel;
  434. SDL_Surface *bitmap;
  435. int which; //which creature is active
  436. void close();
  437. void Max();
  438. void Buy();
  439. void Cancel();
  440. void sliderMoved(int to);
  441. void clickLeft(boost::logic::tribool down);
  442. void activate();
  443. void deactivate();
  444. void show(SDL_Surface * to = NULL);
  445. CRecrutationWindow(const std::vector<std::pair<int,int> > & Creatures, const boost::function<void(int,int)> & Recruit); //creatures - pairs<creature_ID,amount>
  446. ~CRecrutationWindow();
  447. };
  448. class CSplitWindow : public IShowable, public KeyInterested
  449. {
  450. public:
  451. CGarrisonInt *gar;
  452. CSlider *slider;
  453. CCreaturePic *anim;
  454. AdventureMapButton *ok, *cancel;
  455. SDL_Surface *bitmap;
  456. int a1, a2, c;
  457. bool which;
  458. CSplitWindow(int cid, int max, CGarrisonInt *Owner);
  459. ~CSplitWindow();
  460. void activate();
  461. void split();
  462. void close();
  463. void deactivate();
  464. void show(SDL_Surface * to = NULL);
  465. void keyPressed (SDL_KeyboardEvent & key);
  466. void sliderMoved(int to);
  467. };
  468. class CCreInfoWindow : public IShowable, public KeyInterested, public ClickableR
  469. {
  470. public:
  471. int type;//0 - rclick popup; 1 - normal window
  472. SDL_Surface *bitmap;
  473. bool anf;
  474. boost::function<void()> dsm;
  475. CCreaturePic *anim;
  476. CCreature *c;
  477. AdventureMapButton *dismiss, *upgrade, *ok;
  478. CCreInfoWindow(int Cid, int Type, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm);
  479. ~CCreInfoWindow();
  480. void activate();
  481. void close();
  482. void clickRight(boost::logic::tribool down);
  483. void dismissF();
  484. void keyPressed (SDL_KeyboardEvent & key);
  485. void deactivate();
  486. void show(SDL_Surface * to = NULL);
  487. };
  488. #endif //CPLAYERINTERFACE_H