CPlayerInterface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. { //okno usuwa swoje komponenty w chwili zamkniecia
  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. SComponent(Etype Type, int Subtype, int Val);
  200. //SComponent(const & SComponent r);
  201. void clickRight (boost::logic::tribool down);
  202. virtual SDL_Surface * getImg();
  203. virtual void activate();
  204. virtual void deactivate();
  205. };
  206. class CSelectableComponent : public SComponent, public ClickableL
  207. {
  208. public:
  209. bool selected;
  210. bool customB;
  211. SDL_Surface * border, *myBitmap;
  212. CSelWindow * owner;
  213. void clickLeft(boost::logic::tribool down);
  214. CSelectableComponent(Etype Type, int Sub, int Val, CSelWindow * Owner=NULL, SDL_Surface * Border=NULL);
  215. ~CSelectableComponent();
  216. void activate();
  217. void deactivate();
  218. void select(bool on);
  219. SDL_Surface * getImg();
  220. };
  221. class CGarrisonInt;
  222. class CGarrisonSlot : public ClickableL, public ClickableR, public Hoverable
  223. {
  224. public:
  225. CGarrisonInt *owner;
  226. const CCreature * creature;
  227. int count;
  228. int upg; //0 - up garrison, 1 - down garrison
  229. bool active;
  230. virtual void hover (bool on);
  231. const CArmedInstance * getObj();
  232. void clickRight (boost::logic::tribool down);
  233. void clickLeft(boost::logic::tribool down);
  234. void activate();
  235. void deactivate();
  236. void show();
  237. CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg=0, const CCreature * Creature=NULL, int Count=0);
  238. ~CGarrisonSlot();
  239. };
  240. class CGarrisonInt :public CIntObject
  241. {
  242. public:
  243. int interx, intery;
  244. CGarrisonSlot *highlighted;
  245. SDL_Surface *sur;
  246. int offx, offy, p2;
  247. bool ignoreEvent, update, active, splitting, pb;
  248. const CCreatureSet *set1;
  249. const CCreatureSet *set2;
  250. std::vector<CGarrisonSlot*> *sup, *sdown;
  251. const CArmedInstance *oup, *odown;
  252. void activate();
  253. void deactivate();
  254. void show();
  255. void activeteSlots();
  256. void deactiveteSlots();
  257. void deleteSlots();
  258. void createSlots();
  259. void recreateSlots();
  260. void splitClick();
  261. void splitStacks(int am2);
  262. CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *pomsur, int OX, int OY, const CArmedInstance *s1, const CArmedInstance *s2=NULL);
  263. ~CGarrisonInt();
  264. };
  265. class CPlayerInterface : public CGameInterface
  266. {
  267. public:
  268. //minor interfaces
  269. bool makingTurn;
  270. SDL_Event * current;
  271. IActivable *curint;
  272. CAdvMapInt * adventureInt;
  273. CCastleInterface * castleInt;
  274. FPSmanager * mainFPSmng;
  275. IStatusBar *statusbar;
  276. //to commucate with engine
  277. CCallback * cb;
  278. //GUI elements
  279. std::vector<ClickableL*> lclickable;
  280. std::vector<ClickableR*> rclickable;
  281. std::vector<Hoverable*> hoverable;
  282. std::vector<KeyInterested*> keyinterested;
  283. std::vector<MotionInterested*> motioninterested;
  284. std::vector<TimeInterested*> timeinterested;
  285. std::vector<IShowable*> objsToBlit;
  286. //overloaded funcs from CGameInterface
  287. void yourTurn();
  288. void heroMoved(const HeroMoveDetails & details);
  289. void tileRevealed(int3 pos);
  290. void tileHidden(int3 pos);
  291. void heroKilled(const CGHeroInstance* hero);
  292. void heroCreated(const CGHeroInstance* hero);
  293. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val);
  294. void receivedResource(int type, int val);
  295. void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID);
  296. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town);
  297. void garrisonChanged(const CGObjectInstance * obj);
  298. void buildChanged(const CGTownInstance *town, int buildingID, int what); //what: 1 - built, 2 - demolished
  299. //for battles
  300. 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
  301. void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  302. void battleNewRound(int round); //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  303. void actionStarted(BattleAction action);//occurs BEFORE every action taken by any stack or by the hero
  304. void actionFinished(BattleAction action);//occurs AFTER every action taken by any stack or by the hero
  305. BattleAction activeStack(int stackID); //called when it's turn of that stack
  306. void battleEnd(CCreatureSet * army1, CCreatureSet * army2, CArmedInstance *hero1, CArmedInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner);
  307. void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  308. void battleStackAttacking(int ID, int dest);
  309. void battleStackIsAttacked(int ID, int dmg, int killed, int IDby);
  310. void battleStackKilled(int ID, int dmg, int killed, int IDby);
  311. //-------------//
  312. void showComp(SComponent comp);
  313. void openTownWindow(const CGTownInstance * town); //shows townscreen
  314. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  315. 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*
  316. void handleEvent(SDL_Event * sEvent);
  317. void handleKeyDown(SDL_Event *sEvent);
  318. void handleKeyUp(SDL_Event *sEvent);
  319. void handleMouseMotion(SDL_Event *sEvent);
  320. void init(ICallback * CB);
  321. int3 repairScreenPos(int3 pos);
  322. void showInfoDialog(std::string text, std::vector<SComponent*> & components);
  323. void removeObjToBlit(IShowable* obj);
  324. CPlayerInterface(int Player, int serial);//c-tor
  325. };
  326. class CStatusBar
  327. : public CIntObject, public IStatusBar
  328. {
  329. public:
  330. SDL_Surface * bg; //background
  331. int middlex, middley; //middle of statusbar
  332. std::string current; //text currently printed
  333. CStatusBar(int x, int y, std::string name="ADROLLVR.bmp", int maxw=-1); //c-tor
  334. ~CStatusBar(); //d-tor
  335. void print(std::string text); //prints text and refreshes statusbar
  336. void clear();//clears statusbar and refreshes
  337. void show(); //shows statusbar (with current text)
  338. std::string getCurrent();
  339. };
  340. class CList
  341. : public ClickableL, public ClickableR, public Hoverable, public KeyInterested, public virtual CIntObject, public MotionInterested
  342. {
  343. public:
  344. SDL_Surface * bg;
  345. CDefHandler *arrup, *arrdo;
  346. SDL_Surface *empty, *selection;
  347. SDL_Rect arrupp, arrdop; //positions of arrows
  348. int posw, posh; //position width/height
  349. int selected, //id of selected position, <0 if none
  350. from;
  351. const int SIZE;
  352. boost::logic::tribool pressed; //true=up; false=down; indeterminate=none
  353. CList(int Size = 5);
  354. void clickLeft(boost::logic::tribool down);
  355. void activate();
  356. void deactivate();
  357. virtual void mouseMoved (SDL_MouseMotionEvent & sEvent)=0;
  358. virtual void genList()=0;
  359. virtual void select(int which)=0;
  360. virtual void draw()=0;
  361. };
  362. class CHeroList
  363. : public CList
  364. {
  365. public:
  366. CDefHandler *mobile, *mana;
  367. std::vector<std::pair<const CGHeroInstance*, CPath *> > items;
  368. int posmobx, posporx, posmanx, posmoby, pospory, posmany;
  369. CHeroList(int Size = 5);
  370. void genList();
  371. void select(int which);
  372. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  373. void clickLeft(boost::logic::tribool down);
  374. void clickRight(boost::logic::tribool down);
  375. void hover (bool on);
  376. void keyPressed (SDL_KeyboardEvent & key);
  377. void updateHList();
  378. void updateMove(const CGHeroInstance* which); //draws move points bar
  379. void redrawAllOne(int which);
  380. void draw();
  381. void init();
  382. };
  383. class CTownList
  384. : public CList
  385. {
  386. public:
  387. boost::function<void()> fun;
  388. std::vector<const CGTownInstance*> items;
  389. int posporx,pospory;
  390. CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy);
  391. ~CTownList();
  392. void genList();
  393. void select(int which);
  394. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  395. void clickLeft(boost::logic::tribool down);
  396. void clickRight(boost::logic::tribool down);
  397. void hover (bool on);
  398. void keyPressed (SDL_KeyboardEvent & key);
  399. void draw();
  400. };
  401. class CCreaturePic //draws 100x130 picture with creature on background, use nextFrame=true to get animation
  402. {
  403. public:
  404. CCreature *c;
  405. CCreatureAnimation *anim;
  406. CCreaturePic(CCreature *cre);
  407. ~CCreaturePic();
  408. int blitPic(SDL_Surface *to, int x, int y, bool nextFrame);
  409. SDL_Surface * getPic(bool nextFrame);
  410. };
  411. class CRecrutationWindow : public IShowable, public ClickableL
  412. {
  413. public:
  414. struct creinfo
  415. {
  416. SDL_Rect pos;
  417. CCreaturePic *pic;
  418. int ID, amount; //creature ID and available amount
  419. std::vector<std::pair<int,int> > res; //res_id - cost_per_unit
  420. };
  421. std::vector<int> amounts; //how many creatures we can afford
  422. std::vector<creinfo> creatures;
  423. boost::function<void(int,int)> recruit; //void (int ID, int amount) <-- call to recruit creatures
  424. CSlider *slider;
  425. AdventureMapButton *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(const std::vector<std::pair<int,int> > & Creatures, const boost::function<void(int,int)> & Recruit); //creatures - pairs<creature_ID,amount>
  438. ~CRecrutationWindow();
  439. };
  440. class CSplitWindow : public IShowable, public KeyInterested
  441. {
  442. public:
  443. CGarrisonInt *gar;
  444. CSlider *slider;
  445. CCreaturePic *anim;
  446. AdventureMapButton *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. class CCreInfoWindow : public IShowable, public KeyInterested, public ClickableR
  461. {
  462. public:
  463. int type;//0 - rclick popup; 1 - normal window
  464. SDL_Surface *bitmap;
  465. bool anf;
  466. boost::function<void()> dsm;
  467. CCreaturePic *anim;
  468. CCreature *c;
  469. AdventureMapButton *dismiss, *upgrade, *ok;
  470. CCreInfoWindow(int Cid, int Type, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm);
  471. ~CCreInfoWindow();
  472. void activate();
  473. void close();
  474. void clickRight(boost::logic::tribool down);
  475. void dismissF();
  476. void keyPressed (SDL_KeyboardEvent & key);
  477. void deactivate();
  478. void show(SDL_Surface * to = NULL);
  479. };
  480. extern CPlayerInterface * LOCPLINT;
  481. #endif //CPLAYERINTERFACE_H