CPlayerInterface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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, p2;
  245. bool ignoreEvent, update, active, splitting, pb;
  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. void splitClick();
  259. void splitStacks(int am2);
  260. CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *pomsur, int OX, int OY, const CGObjectInstance *s1, const CGObjectInstance *s2=NULL);
  261. ~CGarrisonInt();
  262. };
  263. class CPlayerInterface : public CGameInterface
  264. {
  265. public:
  266. bool makingTurn;
  267. SDL_Event * current;
  268. IActivable *curint;
  269. CAdvMapInt * adventureInt;
  270. CCastleInterface * castleInt;
  271. FPSmanager * mainFPSmng;
  272. IStatusBar *statusbar;
  273. //TODO: town interace, battle interface, other interfaces
  274. CCallback * cb;
  275. std::vector<ClickableL*> lclickable;
  276. std::vector<ClickableR*> rclickable;
  277. std::vector<Hoverable*> hoverable;
  278. std::vector<KeyInterested*> keyinterested;
  279. std::vector<MotionInterested*> motioninterested;
  280. std::vector<TimeInterested*> timeinterested;
  281. std::vector<IShowable*> objsToBlit;
  282. SDL_Surface * hInfo, *tInfo;
  283. std::vector<std::pair<int, int> > slotsPos;
  284. CDefEssential *luck22, *luck30, *luck42, *luck82,
  285. *morale22, *morale30, *morale42, *morale82,
  286. *halls, *forts, *bigTownPic;
  287. std::map<int,SDL_Surface*> heroWins;
  288. std::map<int,SDL_Surface*> townWins;
  289. //overloaded funcs from Interface
  290. void yourTurn();
  291. void heroMoved(const HeroMoveDetails & details);
  292. void tileRevealed(int3 pos);
  293. void tileHidden(int3 pos);
  294. void heroKilled(const CGHeroInstance* hero);
  295. void heroCreated(const CGHeroInstance* hero);
  296. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val);
  297. void receivedResource(int type, int val);
  298. void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID);
  299. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town);
  300. void garrisonChanged(const CGObjectInstance * obj);
  301. void buildChanged(const CGTownInstance *town, int buildingID, int what); //what: 1 - built, 2 - demolished
  302. //battles
  303. 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
  304. void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  305. void battleNewRound(int round); //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  306. void actionStarted(BattleAction action);//occurs BEFORE every action taken by any stack or by the hero
  307. void actionFinished(BattleAction action);//occurs AFTER every action taken by any stack or by the hero
  308. BattleAction activeStack(int stackID); //called when it's turn of that stack
  309. void battleEnd(CCreatureSet * army1, CCreatureSet * army2, CArmedInstance *hero1, CArmedInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner);
  310. void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  311. void battleStackAttacking(int ID, int dest);
  312. //-------------//
  313. void showComp(SComponent comp);
  314. void openTownWindow(const CGTownInstance * town); //shows townscreen
  315. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  316. 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*
  317. void handleEvent(SDL_Event * sEvent);
  318. void handleKeyDown(SDL_Event *sEvent);
  319. void handleKeyUp(SDL_Event *sEvent);
  320. void handleMouseMotion(SDL_Event *sEvent);
  321. void init(ICallback * CB);
  322. int3 repairScreenPos(int3 pos);
  323. void showInfoDialog(std::string text, std::vector<SComponent*> & components);
  324. void removeObjToBlit(IShowable* obj);
  325. SDL_Surface * drawHeroInfoWin(const CGHeroInstance * curh);
  326. SDL_Surface * drawPrimarySkill(const CGHeroInstance *curh, SDL_Surface *ret, int from=0, int to=PRIMARY_SKILLS);
  327. SDL_Surface * drawTownInfoWin(const CGTownInstance * curh);
  328. CPlayerInterface(int Player, int serial);
  329. };
  330. class CStatusBar
  331. : public CIntObject, public IStatusBar
  332. {
  333. public:
  334. SDL_Surface * bg; //background
  335. int middlex, middley; //middle of statusbar
  336. std::string current; //text currently printed
  337. CStatusBar(int x, int y, std::string name="ADROLLVR.bmp", int maxw=-1); //c-tor
  338. ~CStatusBar(); //d-tor
  339. void print(std::string text); //prints text and refreshes statusbar
  340. void clear();//clears statusbar and refreshes
  341. void show(); //shows statusbar (with current text)
  342. std::string getCurrent();
  343. };
  344. class CList
  345. : public ClickableL, public ClickableR, public Hoverable, public KeyInterested, public virtual CIntObject, public MotionInterested
  346. {
  347. public:
  348. SDL_Surface * bg;
  349. CDefHandler *arrup, *arrdo;
  350. SDL_Surface *empty, *selection;
  351. SDL_Rect arrupp, arrdop; //positions of arrows
  352. int posw, posh; //position width/height
  353. int selected, //id of selected position, <0 if none
  354. from;
  355. const int SIZE;
  356. boost::logic::tribool pressed; //true=up; false=down; indeterminate=none
  357. CList(int Size = 5);
  358. void clickLeft(boost::logic::tribool down);
  359. void activate();
  360. void deactivate();
  361. virtual void mouseMoved (SDL_MouseMotionEvent & sEvent)=0;
  362. virtual void genList()=0;
  363. virtual void select(int which)=0;
  364. virtual void draw()=0;
  365. };
  366. class CHeroList
  367. : public CList
  368. {
  369. public:
  370. CDefHandler *mobile, *mana;
  371. std::vector<std::pair<const CGHeroInstance*, CPath *> > items;
  372. int posmobx, posporx, posmanx, posmoby, pospory, posmany;
  373. CHeroList(int Size = 5);
  374. void genList();
  375. void select(int which);
  376. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  377. void clickLeft(boost::logic::tribool down);
  378. void clickRight(boost::logic::tribool down);
  379. void hover (bool on);
  380. void keyPressed (SDL_KeyboardEvent & key);
  381. void updateHList();
  382. void updateMove(const CGHeroInstance* which); //draws move points bar
  383. void redrawAllOne(int which);
  384. void draw();
  385. void init();
  386. };
  387. template<typename T>
  388. class CTownList
  389. : public CList
  390. {
  391. public:
  392. T* owner;
  393. void(T::*fun)();
  394. std::vector<const CGTownInstance*> items;
  395. int posporx,pospory;
  396. CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy);
  397. ~CTownList();
  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 draw();
  406. };
  407. class IRecruit
  408. {
  409. public:
  410. virtual void recruit(int ID, int amount)=0;
  411. };
  412. class CRecrutationWindow : public IShowable, public ClickableL
  413. {
  414. public:
  415. struct creinfo
  416. {
  417. SDL_Rect pos;
  418. int ID, amount; //creature ID and available amount
  419. CCreatureAnimation *anim;
  420. std::vector<std::pair<int,int> > res; //res_id - cost_per_unit
  421. };
  422. std::vector<creinfo> creatures;
  423. IRecruit *rec;
  424. CSlider<CRecrutationWindow> *slider;
  425. AdventureMapButton<CRecrutationWindow> *max, *buy, *cancel;
  426. SDL_Surface *bitmap;
  427. int which; //which creature is active
  428. void close();
  429. void Max();
  430. void Buy();
  431. void Cancel();
  432. void sliderMoved(int to);
  433. void clickLeft(boost::logic::tribool down);
  434. void activate();
  435. void deactivate();
  436. void show(SDL_Surface * to = NULL);
  437. CRecrutationWindow(std::vector<std::pair<int,int> > & Creatures, IRecruit *irec); //creatures - pairs<creature_ID,amount>
  438. ~CRecrutationWindow();
  439. };
  440. class CSplitWindow : public IShowable, public KeyInterested
  441. {
  442. public:
  443. CGarrisonInt *gar;
  444. CSlider<CSplitWindow> *slider;
  445. CCreatureAnimation *anim;
  446. AdventureMapButton<CSplitWindow> *ok, *cancel;
  447. SDL_Surface *bitmap;
  448. int a1, a2, c;
  449. bool which;
  450. CSplitWindow(int cid, int max, CGarrisonInt *Owner);
  451. ~CSplitWindow();
  452. void activate();
  453. void split();
  454. void close();
  455. void deactivate();
  456. void show(SDL_Surface * to = NULL);
  457. void keyPressed (SDL_KeyboardEvent & key);
  458. void sliderMoved(int to);
  459. };
  460. #endif //CPLAYERINTERFACE_H