CPlayerInterface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. { //window deletes its components when closed
  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. void init(Etype Type, int Subtype, int Val);
  200. SComponent(Etype Type, int Subtype, int Val);
  201. SComponent(const Component &c);
  202. void clickRight (boost::logic::tribool down);
  203. virtual SDL_Surface * getImg();
  204. virtual void activate();
  205. virtual void deactivate();
  206. };
  207. class CSelectableComponent : public SComponent, public ClickableL
  208. {
  209. public:
  210. bool selected;
  211. bool customB;
  212. SDL_Surface * border, *myBitmap;
  213. CSelWindow * owner;
  214. void clickLeft(boost::logic::tribool down);
  215. CSelectableComponent(Etype Type, int Sub, int Val, CSelWindow * Owner=NULL, SDL_Surface * Border=NULL);
  216. ~CSelectableComponent();
  217. void activate();
  218. void deactivate();
  219. void select(bool on);
  220. SDL_Surface * getImg();
  221. };
  222. class CGarrisonInt;
  223. class CGarrisonSlot : public ClickableL, public ClickableR, public Hoverable
  224. {
  225. public:
  226. CGarrisonInt *owner;
  227. const CCreature * creature;
  228. int count;
  229. int upg; //0 - up garrison, 1 - down garrison
  230. bool active;
  231. virtual void hover (bool on);
  232. const CArmedInstance * getObj();
  233. void clickRight (boost::logic::tribool down);
  234. void clickLeft(boost::logic::tribool down);
  235. void activate();
  236. void deactivate();
  237. void show();
  238. CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg=0, const CCreature * Creature=NULL, int Count=0);
  239. ~CGarrisonSlot();
  240. };
  241. class CGarrisonInt :public CIntObject
  242. {
  243. public:
  244. int interx, intery;
  245. CGarrisonSlot *highlighted;
  246. SDL_Surface *sur;
  247. int offx, offy, p2;
  248. bool ignoreEvent, update, active, splitting, pb;
  249. const CCreatureSet *set1;
  250. const CCreatureSet *set2;
  251. std::vector<CGarrisonSlot*> *sup, *sdown;
  252. const CArmedInstance *oup, *odown;
  253. void activate();
  254. void deactivate();
  255. void show();
  256. void activeteSlots();
  257. void deactiveteSlots();
  258. void deleteSlots();
  259. void createSlots();
  260. void recreateSlots();
  261. void splitClick();
  262. void splitStacks(int am2);
  263. CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *pomsur, int OX, int OY, const CArmedInstance *s1, const CArmedInstance *s2=NULL);
  264. ~CGarrisonInt();
  265. };
  266. class CPlayerInterface : public CGameInterface
  267. {
  268. public:
  269. //minor interfaces
  270. bool makingTurn;
  271. SDL_Event * current;
  272. IActivable *curint;
  273. CAdvMapInt * adventureInt;
  274. CCastleInterface * castleInt;
  275. FPSmanager * mainFPSmng;
  276. IStatusBar *statusbar;
  277. //to commucate with engine
  278. CCallback * cb;
  279. //GUI elements
  280. std::vector<ClickableL*> lclickable;
  281. std::vector<ClickableR*> rclickable;
  282. std::vector<Hoverable*> hoverable;
  283. std::vector<KeyInterested*> keyinterested;
  284. std::vector<MotionInterested*> motioninterested;
  285. std::vector<TimeInterested*> timeinterested;
  286. std::vector<IShowable*> objsToBlit;
  287. //overloaded funcs from CGameInterface
  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 showInfoDialog(std::string text, std::vector<Component*> &components);
  297. void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID);
  298. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town);
  299. void garrisonChanged(const CGObjectInstance * obj);
  300. void buildChanged(const CGTownInstance *town, int buildingID, int what); //what: 1 - built, 2 - demolished
  301. //for battles
  302. 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
  303. void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  304. void battleNewRound(int round); //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  305. void actionStarted(BattleAction action);//occurs BEFORE every action taken by any stack or by the hero
  306. void actionFinished(BattleAction action);//occurs AFTER every action taken by any stack or by the hero
  307. BattleAction activeStack(int stackID); //called when it's turn of that stack
  308. void battleEnd(CCreatureSet * army1, CCreatureSet * army2, CArmedInstance *hero1, CArmedInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner);
  309. void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  310. void battleStackAttacking(int ID, int dest);
  311. void battleStackIsAttacked(int ID, int dmg, int killed, int IDby);
  312. void battleStackKilled(int ID, int dmg, int killed, int IDby);
  313. //-------------//
  314. void showComp(SComponent comp);
  315. void openTownWindow(const CGTownInstance * town); //shows townscreen
  316. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  317. 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*
  318. void handleEvent(SDL_Event * sEvent);
  319. void handleKeyDown(SDL_Event *sEvent);
  320. void handleKeyUp(SDL_Event *sEvent);
  321. void handleMouseMotion(SDL_Event *sEvent);
  322. void init(ICallback * CB);
  323. int3 repairScreenPos(int3 pos);
  324. void removeObjToBlit(IShowable* obj);
  325. void showInfoDialog(std::string text, std::vector<SComponent*> & components);
  326. CPlayerInterface(int Player, int serial);//c-tor
  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. class CTownList
  386. : public CList
  387. {
  388. public:
  389. boost::function<void()> fun;
  390. std::vector<const CGTownInstance*> items;
  391. int posporx,pospory;
  392. CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy);
  393. ~CTownList();
  394. void genList();
  395. void select(int which);
  396. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  397. void clickLeft(boost::logic::tribool down);
  398. void clickRight(boost::logic::tribool down);
  399. void hover (bool on);
  400. void keyPressed (SDL_KeyboardEvent & key);
  401. void draw();
  402. };
  403. class CCreaturePic //draws 100x130 picture with creature on background, use nextFrame=true to get animation
  404. {
  405. public:
  406. CCreature *c;
  407. CCreatureAnimation *anim;
  408. CCreaturePic(CCreature *cre);
  409. ~CCreaturePic();
  410. int blitPic(SDL_Surface *to, int x, int y, bool nextFrame);
  411. SDL_Surface * getPic(bool nextFrame);
  412. };
  413. class CRecrutationWindow : public IShowable, public ClickableL
  414. {
  415. public:
  416. struct creinfo
  417. {
  418. SDL_Rect pos;
  419. CCreaturePic *pic;
  420. int ID, amount; //creature ID and available amount
  421. std::vector<std::pair<int,int> > res; //res_id - cost_per_unit
  422. };
  423. std::vector<int> amounts; //how many creatures we can afford
  424. std::vector<creinfo> creatures;
  425. boost::function<void(int,int)> recruit; //void (int ID, int amount) <-- call to recruit creatures
  426. CSlider *slider;
  427. AdventureMapButton *max, *buy, *cancel;
  428. SDL_Surface *bitmap;
  429. int which; //which creature is active
  430. void close();
  431. void Max();
  432. void Buy();
  433. void Cancel();
  434. void sliderMoved(int to);
  435. void clickLeft(boost::logic::tribool down);
  436. void activate();
  437. void deactivate();
  438. void show(SDL_Surface * to = NULL);
  439. CRecrutationWindow(const std::vector<std::pair<int,int> > & Creatures, const boost::function<void(int,int)> & Recruit); //creatures - pairs<creature_ID,amount>
  440. ~CRecrutationWindow();
  441. };
  442. class CSplitWindow : public IShowable, public KeyInterested
  443. {
  444. public:
  445. CGarrisonInt *gar;
  446. CSlider *slider;
  447. CCreaturePic *anim;
  448. AdventureMapButton *ok, *cancel;
  449. SDL_Surface *bitmap;
  450. int a1, a2, c;
  451. bool which;
  452. CSplitWindow(int cid, int max, CGarrisonInt *Owner);
  453. ~CSplitWindow();
  454. void activate();
  455. void split();
  456. void close();
  457. void deactivate();
  458. void show(SDL_Surface * to = NULL);
  459. void keyPressed (SDL_KeyboardEvent & key);
  460. void sliderMoved(int to);
  461. };
  462. class CCreInfoWindow : public IShowable, public KeyInterested, public ClickableR
  463. {
  464. public:
  465. int type;//0 - rclick popup; 1 - normal window
  466. SDL_Surface *bitmap;
  467. bool anf;
  468. boost::function<void()> dsm;
  469. CCreaturePic *anim;
  470. CCreature *c;
  471. AdventureMapButton *dismiss, *upgrade, *ok;
  472. CCreInfoWindow(int Cid, int Type, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm);
  473. ~CCreInfoWindow();
  474. void activate();
  475. void close();
  476. void clickRight(boost::logic::tribool down);
  477. void dismissF();
  478. void keyPressed (SDL_KeyboardEvent & key);
  479. void deactivate();
  480. void show(SDL_Surface * to = NULL);
  481. };
  482. extern CPlayerInterface * LOCPLINT;
  483. #endif //CPLAYERINTERFACE_H