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. 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, bool 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(BattleResult *br);
  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, bool byShooting);
  317. void battleStackKilled(int ID, int dmg, int killed, int IDby, bool byShooting);
  318. void battleStackIsShooting(int ID, int dest); //called when stack with id ID is shooting to hex dest
  319. //-------------//
  320. void showComp(SComponent comp);
  321. void openTownWindow(const CGTownInstance * town); //shows townscreen
  322. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  323. 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*
  324. void handleEvent(SDL_Event * sEvent);
  325. void handleKeyDown(SDL_Event *sEvent);
  326. void handleKeyUp(SDL_Event *sEvent);
  327. void handleMouseMotion(SDL_Event *sEvent);
  328. void init(ICallback * CB);
  329. int3 repairScreenPos(int3 pos);
  330. void removeObjToBlit(IShowable* obj);
  331. void showInfoDialog(std::string text, std::vector<SComponent*> & components);
  332. CPlayerInterface(int Player, int serial);//c-tor
  333. ~CPlayerInterface();//d-tor
  334. };
  335. class CStatusBar
  336. : public CIntObject, public IStatusBar
  337. {
  338. public:
  339. SDL_Surface * bg; //background
  340. int middlex, middley; //middle of statusbar
  341. std::string current; //text currently printed
  342. CStatusBar(int x, int y, std::string name="ADROLLVR.bmp", int maxw=-1); //c-tor
  343. ~CStatusBar(); //d-tor
  344. void print(std::string text); //prints text and refreshes statusbar
  345. void clear();//clears statusbar and refreshes
  346. void show(); //shows statusbar (with current text)
  347. std::string getCurrent();
  348. };
  349. class CList
  350. : public ClickableL, public ClickableR, public Hoverable, public KeyInterested, public virtual CIntObject, public MotionInterested
  351. {
  352. public:
  353. SDL_Surface * bg;
  354. CDefHandler *arrup, *arrdo;
  355. SDL_Surface *empty, *selection;
  356. SDL_Rect arrupp, arrdop; //positions of arrows
  357. int posw, posh; //position width/height
  358. int selected, //id of selected position, <0 if none
  359. from;
  360. const int SIZE;
  361. boost::logic::tribool pressed; //true=up; false=down; indeterminate=none
  362. CList(int Size = 5);
  363. void clickLeft(boost::logic::tribool down);
  364. void activate();
  365. void deactivate();
  366. virtual void mouseMoved (SDL_MouseMotionEvent & sEvent)=0;
  367. virtual void genList()=0;
  368. virtual void select(int which)=0;
  369. virtual void draw()=0;
  370. };
  371. class CHeroList
  372. : public CList
  373. {
  374. public:
  375. CDefHandler *mobile, *mana;
  376. std::vector<std::pair<const CGHeroInstance*, CPath *> > items;
  377. int posmobx, posporx, posmanx, posmoby, pospory, posmany;
  378. CHeroList(int Size = 5);
  379. void genList();
  380. void select(int which);
  381. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  382. void clickLeft(boost::logic::tribool down);
  383. void clickRight(boost::logic::tribool down);
  384. void hover (bool on);
  385. void keyPressed (SDL_KeyboardEvent & key);
  386. void updateHList();
  387. void updateMove(const CGHeroInstance* which); //draws move points bar
  388. void redrawAllOne(int which);
  389. void draw();
  390. void init();
  391. };
  392. class CTownList
  393. : public CList
  394. {
  395. public:
  396. boost::function<void()> fun;
  397. std::vector<const CGTownInstance*> items;
  398. int posporx,pospory;
  399. CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy);
  400. ~CTownList();
  401. void genList();
  402. void select(int which);
  403. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  404. void clickLeft(boost::logic::tribool down);
  405. void clickRight(boost::logic::tribool down);
  406. void hover (bool on);
  407. void keyPressed (SDL_KeyboardEvent & key);
  408. void draw();
  409. };
  410. class CCreaturePic //draws 100x130 picture with creature on background, use nextFrame=true to get animation
  411. {
  412. public:
  413. CCreature *c;
  414. CCreatureAnimation *anim;
  415. CCreaturePic(CCreature *cre);
  416. ~CCreaturePic();
  417. int blitPic(SDL_Surface *to, int x, int y, bool nextFrame);
  418. SDL_Surface * getPic(bool nextFrame);
  419. };
  420. class CRecrutationWindow : public IShowable, public ClickableL
  421. {
  422. public:
  423. struct creinfo
  424. {
  425. SDL_Rect pos;
  426. CCreaturePic *pic;
  427. int ID, amount; //creature ID and available amount
  428. std::vector<std::pair<int,int> > res; //res_id - cost_per_unit
  429. };
  430. std::vector<int> amounts; //how many creatures we can afford
  431. std::vector<creinfo> creatures;
  432. boost::function<void(int,int)> recruit; //void (int ID, int amount) <-- call to recruit creatures
  433. CSlider *slider;
  434. AdventureMapButton *max, *buy, *cancel;
  435. SDL_Surface *bitmap;
  436. int which; //which creature is active
  437. void close();
  438. void Max();
  439. void Buy();
  440. void Cancel();
  441. void sliderMoved(int to);
  442. void clickLeft(boost::logic::tribool down);
  443. void activate();
  444. void deactivate();
  445. void show(SDL_Surface * to = NULL);
  446. CRecrutationWindow(const std::vector<std::pair<int,int> > & Creatures, const boost::function<void(int,int)> & Recruit); //creatures - pairs<creature_ID,amount>
  447. ~CRecrutationWindow();
  448. };
  449. class CSplitWindow : public IShowable, public KeyInterested
  450. {
  451. public:
  452. CGarrisonInt *gar;
  453. CSlider *slider;
  454. CCreaturePic *anim;
  455. AdventureMapButton *ok, *cancel;
  456. SDL_Surface *bitmap;
  457. int a1, a2, c;
  458. bool which;
  459. CSplitWindow(int cid, int max, CGarrisonInt *Owner);
  460. ~CSplitWindow();
  461. void activate();
  462. void split();
  463. void close();
  464. void deactivate();
  465. void show(SDL_Surface * to = NULL);
  466. void keyPressed (SDL_KeyboardEvent & key);
  467. void sliderMoved(int to);
  468. };
  469. class CCreInfoWindow : public IShowable, public KeyInterested, public ClickableR
  470. {
  471. public:
  472. int type;//0 - rclick popup; 1 - normal window
  473. SDL_Surface *bitmap;
  474. bool anf;
  475. boost::function<void()> dsm;
  476. CCreaturePic *anim;
  477. CCreature *c;
  478. AdventureMapButton *dismiss, *upgrade, *ok;
  479. CCreInfoWindow(int Cid, int Type, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm);
  480. ~CCreInfoWindow();
  481. void activate();
  482. void close();
  483. void clickRight(boost::logic::tribool down);
  484. void dismissF();
  485. void keyPressed (SDL_KeyboardEvent & key);
  486. void deactivate();
  487. void show(SDL_Surface * to = NULL);
  488. };
  489. extern CPlayerInterface * LOCPLINT;
  490. #endif //CPLAYERINTERFACE_H