CPlayerInterface.h 14 KB

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