CPlayerInterface.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. namespace boost
  27. {
  28. class mutex;
  29. };
  30. class IShowable
  31. {
  32. public:
  33. virtual void show(SDL_Surface * to = NULL)=0;
  34. };
  35. class IStatusBar
  36. {
  37. public:
  38. virtual ~IStatusBar(){}; //d-tor
  39. virtual void print(std::string text)=0; //prints text and refreshes statusbar
  40. virtual void clear()=0;//clears statusbar and refreshes
  41. virtual void show()=0; //shows statusbar (with current text)
  42. virtual std::string getCurrent()=0;
  43. };
  44. class IActivable
  45. {
  46. public:
  47. virtual void activate()=0;
  48. virtual void deactivate()=0;
  49. virtual ~IActivable(){};
  50. };
  51. class CIntObject //interface object
  52. {
  53. public:
  54. SDL_Rect pos;
  55. int ID;
  56. };
  57. class CSimpleWindow : public virtual CIntObject, public IShowable
  58. {
  59. public:
  60. SDL_Surface * bitmap;
  61. CIntObject * owner;
  62. virtual void show(SDL_Surface * to = NULL);
  63. CSimpleWindow():bitmap(NULL),owner(NULL){};
  64. virtual ~CSimpleWindow();
  65. };
  66. class CButtonBase : public virtual CIntObject, public IShowable, public IActivable //basic buttton class
  67. {
  68. public:
  69. int bitmapOffset;
  70. int type; //advmapbutton=2
  71. bool abs;
  72. bool active;
  73. bool notFreeButton;
  74. CIntObject * ourObj; // "owner"
  75. int state;
  76. std::vector< std::vector<SDL_Surface*> > imgs;
  77. int curimg;
  78. virtual void show(SDL_Surface * to = NULL);
  79. virtual void activate()=0;
  80. virtual void deactivate()=0;
  81. CButtonBase();
  82. virtual ~CButtonBase();
  83. };
  84. class ClickableL : public virtual CIntObject //for left-clicks
  85. {
  86. public:
  87. bool pressedL;
  88. ClickableL();
  89. virtual ~ClickableL(){};
  90. virtual void clickLeft (boost::logic::tribool down)=0;
  91. virtual void activate()=0;
  92. virtual void deactivate()=0;
  93. };
  94. class ClickableR : public virtual CIntObject //for right-clicks
  95. {
  96. public:
  97. bool pressedR;
  98. ClickableR();
  99. virtual ~ClickableR(){};
  100. virtual void clickRight (boost::logic::tribool down)=0;
  101. virtual void activate()=0;
  102. virtual void deactivate()=0;
  103. };
  104. class Hoverable : public virtual CIntObject
  105. {
  106. public:
  107. Hoverable(){hovered=false;}
  108. virtual ~Hoverable(){};
  109. bool hovered;
  110. virtual void hover (bool on)=0;
  111. virtual void activate()=0;
  112. virtual void deactivate()=0;
  113. };
  114. class KeyInterested : public virtual CIntObject
  115. {
  116. public:
  117. virtual ~KeyInterested(){};
  118. virtual void keyPressed (SDL_KeyboardEvent & key)=0;
  119. virtual void activate()=0;
  120. virtual void deactivate()=0;
  121. };
  122. class MotionInterested: public virtual CIntObject
  123. {
  124. public:
  125. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  126. MotionInterested(){strongInterest=false;};
  127. virtual ~MotionInterested(){};
  128. virtual void mouseMoved (SDL_MouseMotionEvent & sEvent)=0;
  129. virtual void activate()=0;
  130. virtual void deactivate()=0;
  131. };
  132. class TimeInterested: public virtual CIntObject
  133. {
  134. public:
  135. virtual ~TimeInterested(){};
  136. int toNextTick;
  137. virtual void tick()=0;
  138. virtual void activate();
  139. virtual void deactivate();
  140. };
  141. template <typename T> class CSCButton: public CButtonBase, public ClickableL //prosty guzik, ktory tylko zmienia obrazek
  142. {
  143. public:
  144. int3 posr; //position in the bitmap
  145. int state;
  146. T* delg;
  147. void(T::*func)(boost::logic::tribool);
  148. CSCButton(CDefHandler * img, CIntObject * obj, void(T::*poin)(boost::logic::tribool), T* Delg=NULL);
  149. void clickLeft (boost::logic::tribool down);
  150. void activate();
  151. void deactivate();
  152. void show(SDL_Surface * to = NULL);
  153. };
  154. class CInfoWindow : public CSimpleWindow //text + comp. + ok button
  155. { //window deletes its components when closed
  156. public:
  157. CSCButton<CInfoWindow> okb;
  158. std::vector<SComponent*> components;
  159. virtual void okClicked(boost::logic::tribool down);
  160. virtual void close();
  161. CInfoWindow();
  162. virtual ~CInfoWindow();
  163. };
  164. class CSelWindow : public CInfoWindow //component selection window
  165. { //uwaga - to okno nie usuwa swoich komponentow przy usuwaniu, moga sie one jeszcze przydac skryptowi - tak wiec skrypt korzystajacyz tego okna musi je usunac
  166. public:
  167. void selectionChange(CSelectableComponent * to);
  168. void okClicked(boost::logic::tribool down);
  169. void close();
  170. CSelWindow(){};
  171. };
  172. class CRClickPopup : public IShowable, public ClickableR
  173. {
  174. public:
  175. virtual void activate();
  176. virtual void deactivate();
  177. virtual void close()=0;
  178. void clickRight (boost::logic::tribool down);
  179. virtual ~CRClickPopup(){};
  180. };
  181. class CInfoPopup : public CRClickPopup
  182. {
  183. public:
  184. bool free;
  185. SDL_Surface * bitmap;
  186. CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free=false);
  187. void close();
  188. void show(SDL_Surface * to = NULL);
  189. CInfoPopup(){free=false;bitmap=NULL;}
  190. ~CInfoPopup(){};
  191. };
  192. class SComponent : public ClickableR
  193. {
  194. public:
  195. enum Etype
  196. {
  197. primskill, secskill, resource, creature, artifact, experience
  198. } type;
  199. int subtype;
  200. int val;
  201. std::string description; //r-click
  202. std::string subtitle;
  203. void init(Etype Type, int Subtype, int Val);
  204. SComponent(Etype Type, int Subtype, int Val);
  205. SComponent(const Component &c);
  206. void clickRight (boost::logic::tribool down);
  207. virtual SDL_Surface * getImg();
  208. virtual void activate();
  209. virtual void deactivate();
  210. };
  211. class CSelectableComponent : public SComponent, public ClickableL
  212. {
  213. public:
  214. bool selected;
  215. bool customB;
  216. SDL_Surface * border, *myBitmap;
  217. CSelWindow * owner;
  218. void clickLeft(boost::logic::tribool down);
  219. CSelectableComponent(Etype Type, int Sub, int Val, CSelWindow * Owner=NULL, SDL_Surface * Border=NULL);
  220. ~CSelectableComponent();
  221. void activate();
  222. void deactivate();
  223. void select(bool on);
  224. SDL_Surface * getImg();
  225. };
  226. class CGarrisonInt;
  227. class CGarrisonSlot : public ClickableL, public ClickableR, public Hoverable
  228. {
  229. public:
  230. CGarrisonInt *owner;
  231. const CCreature * creature;
  232. int count;
  233. int upg; //0 - up garrison, 1 - down garrison
  234. bool active;
  235. virtual void hover (bool on);
  236. const CArmedInstance * getObj();
  237. void clickRight (boost::logic::tribool down);
  238. void clickLeft(boost::logic::tribool down);
  239. void activate();
  240. void deactivate();
  241. void show();
  242. CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg=0, const CCreature * Creature=NULL, int Count=0);
  243. ~CGarrisonSlot();
  244. };
  245. class CGarrisonInt :public CIntObject
  246. {
  247. public:
  248. int interx, intery;
  249. CGarrisonSlot *highlighted;
  250. SDL_Surface *sur;
  251. int offx, offy, p2;
  252. bool ignoreEvent, update, active, splitting, pb;
  253. const CCreatureSet *set1;
  254. const CCreatureSet *set2;
  255. std::vector<CGarrisonSlot*> *sup, *sdown;
  256. const CArmedInstance *oup, *odown;
  257. void activate();
  258. void deactivate();
  259. void show();
  260. void activeteSlots();
  261. void deactiveteSlots();
  262. void deleteSlots();
  263. void createSlots();
  264. void recreateSlots();
  265. void splitClick();
  266. void splitStacks(int am2);
  267. CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *pomsur, int OX, int OY, const CArmedInstance *s1, const CArmedInstance *s2=NULL);
  268. ~CGarrisonInt();
  269. };
  270. class CPlayerInterface : public CGameInterface
  271. {
  272. public:
  273. //minor interfaces
  274. boost::mutex *pim;
  275. bool makingTurn;
  276. SDL_Event * current;
  277. IActivable *curint;
  278. CAdvMapInt * adventureInt;
  279. CCastleInterface * castleInt;
  280. FPSmanager * mainFPSmng;
  281. IStatusBar *statusbar;
  282. //to commucate with engine
  283. CCallback * cb;
  284. //GUI elements
  285. std::vector<ClickableL*> lclickable;
  286. std::vector<ClickableR*> rclickable;
  287. std::vector<Hoverable*> hoverable;
  288. std::vector<KeyInterested*> keyinterested;
  289. std::vector<MotionInterested*> motioninterested;
  290. std::vector<TimeInterested*> timeinterested;
  291. std::vector<IShowable*> objsToBlit;
  292. //overloaded funcs from CGameInterface
  293. void yourTurn();
  294. void heroMoved(const HeroMoveDetails & details);
  295. void tileRevealed(int3 pos);
  296. void tileHidden(int3 pos);
  297. void heroKilled(const CGHeroInstance* hero);
  298. void heroCreated(const CGHeroInstance* hero);
  299. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val);
  300. void receivedResource(int type, int val);
  301. void showInfoDialog(std::string text, std::vector<Component*> &components);
  302. void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID);
  303. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town);
  304. void garrisonChanged(const CGObjectInstance * obj);
  305. void buildChanged(const CGTownInstance *town, int buildingID, int what); //what: 1 - built, 2 - demolished
  306. //for battles
  307. 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
  308. void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  309. void battleNewRound(int round); //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  310. void actionStarted(BattleAction action);//occurs BEFORE every action taken by any stack or by the hero
  311. void actionFinished(BattleAction action);//occurs AFTER every action taken by any stack or by the hero
  312. BattleAction activeStack(int stackID); //called when it's turn of that stack
  313. void battleEnd(CCreatureSet * army1, CCreatureSet * army2, CArmedInstance *hero1, CArmedInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner);
  314. void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  315. void battleStackAttacking(int ID, int dest);
  316. void battleStackIsAttacked(int ID, int dmg, int killed, int IDby);
  317. void battleStackKilled(int ID, int dmg, int killed, int IDby);
  318. //-------------//
  319. void showComp(SComponent comp);
  320. void openTownWindow(const CGTownInstance * town); //shows townscreen
  321. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  322. 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*
  323. void handleEvent(SDL_Event * sEvent);
  324. void handleKeyDown(SDL_Event *sEvent);
  325. void handleKeyUp(SDL_Event *sEvent);
  326. void handleMouseMotion(SDL_Event *sEvent);
  327. void init(ICallback * CB);
  328. int3 repairScreenPos(int3 pos);
  329. void removeObjToBlit(IShowable* obj);
  330. void showInfoDialog(std::string text, std::vector<SComponent*> & components);
  331. CPlayerInterface(int Player, int serial);//c-tor
  332. ~CPlayerInterface();//d-tor
  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. extern CPlayerInterface * LOCPLINT;
  489. #endif //CPLAYERINTERFACE_H