GUIBase.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #ifndef __GUIBASE_H__
  2. #define __GUIBASE_H__
  3. #include "../global.h"
  4. #include "SDL.h"
  5. #include <set>
  6. #include <list>
  7. #include "../timeHandler.h"
  8. #include "FontBase.h"
  9. #ifdef max
  10. #undef max
  11. #endif
  12. #ifdef min
  13. #undef min
  14. #endif
  15. /*
  16. * GUIBase.h, part of VCMI engine
  17. *
  18. * Authors: listed in file AUTHORS in main folder
  19. *
  20. * License: GNU General Public License v2.0 or later
  21. * Full text of license available in license.txt file, in main folder
  22. *
  23. */
  24. class CDefEssential;
  25. class AdventureMapButton;
  26. class CHighlightableButtonsGroup;
  27. class CDefHandler;
  28. struct HeroMoveDetails;
  29. class CDefEssential;
  30. class CGHeroInstance;
  31. class CAdvMapInt;
  32. class CCastleInterface;
  33. class CBattleInterface;
  34. class CStack;
  35. class SComponent;
  36. class CCreature;
  37. struct SDL_Surface;
  38. struct CPath;
  39. class CCreatureAnimation;
  40. class CSelectableComponent;
  41. class CCreatureSet;
  42. class CGObjectInstance;
  43. class CSlider;
  44. struct UpgradeInfo;
  45. template <typename T> struct CondSh;
  46. class CInGameConsole;
  47. class CGarrisonInt;
  48. class CInGameConsole;
  49. class Component;
  50. class CArmedInstance;
  51. class CGTownInstance;
  52. class StackState;
  53. class CPlayerInterface;
  54. struct Point
  55. {
  56. int x, y;
  57. //constructors
  58. Point()
  59. {
  60. x = y = 0;
  61. };
  62. Point(int X, int Y)
  63. :x(X),y(Y)
  64. {};
  65. Point(const int3 &a)
  66. :x(a.x),y(a.y)
  67. {}
  68. Point(const SDL_MouseMotionEvent &a)
  69. :x(a.x),y(a.y)
  70. {}
  71. template<typename T>
  72. Point operator+(const T &b) const
  73. {
  74. return Point(x+b.x,y+b.y);
  75. }
  76. template<typename T>
  77. Point& operator+=(const T &b)
  78. {
  79. x += b.x;
  80. y += b.y;
  81. return *this;
  82. }
  83. template<typename T>
  84. Point operator-(const T &b) const
  85. {
  86. return Point(x - b.x, y - b.y);
  87. }
  88. template<typename T>
  89. Point& operator-=(const T &b)
  90. {
  91. x -= b.x;
  92. y -= b.y;
  93. return *this;
  94. }
  95. bool operator<(const Point &b) const //product order
  96. {
  97. return x < b.x && y < b.y;
  98. }
  99. template<typename T> Point& operator=(const T &t)
  100. {
  101. x = t.x;
  102. y = t.y;
  103. return *this;
  104. }
  105. template<typename T> bool operator==(const T &t)
  106. {
  107. return x == t.x && y == t.y;
  108. }
  109. };
  110. struct Rect : public SDL_Rect
  111. {
  112. Rect()//default c-tor
  113. {
  114. x = y = w = h = -1;
  115. }
  116. Rect(int X, int Y, int W, int H) //c-tor
  117. {
  118. x = X;
  119. y = Y;
  120. w = W;
  121. h = H;
  122. }
  123. Rect(const SDL_Rect & r) //c-tor
  124. {
  125. x = r.x;
  126. y = r.y;
  127. w = r.w;
  128. h = r.h;
  129. }
  130. explicit Rect(const SDL_Surface * const &surf)
  131. {
  132. x = y = 0;
  133. w = surf->w;
  134. h = surf->h;
  135. }
  136. static Rect createCentered(int w, int h);
  137. static Rect around(const Rect &r, int width = 1); //creates rect around another
  138. bool isIn(int qx, int qy) const //determines if given point lies inside rect
  139. {
  140. if (qx > x && qx<x+w && qy>y && qy<y+h)
  141. return true;
  142. return false;
  143. }
  144. bool isIn(const Point &q) const //determines if given point lies inside rect
  145. {
  146. return isIn(q.x,q.y);
  147. }
  148. Point topLeft() const //top left corner of this rect
  149. {
  150. return Point(x,y);
  151. }
  152. Point topRight() const //top right corner of this rect
  153. {
  154. return Point(x+w,y);
  155. }
  156. Point bottomLeft() const //bottom left corner of this rect
  157. {
  158. return Point(x,y+h);
  159. }
  160. Point bottomRight() const //bottom right corner of this rect
  161. {
  162. return Point(x+w,y+h);
  163. }
  164. Rect operator+(const Rect &p) const //moves this rect by p's rect position
  165. {
  166. return Rect(x+p.x,y+p.y,w,h);
  167. }
  168. Rect operator+(const Point &p) const //moves this rect by p's point position
  169. {
  170. return Rect(x+p.x,y+p.y,w,h);
  171. }
  172. Rect& operator=(const Point &p) //assignment operator
  173. {
  174. x = p.x;
  175. y = p.y;
  176. return *this;
  177. }
  178. Rect& operator=(const Rect &p) //assignment operator
  179. {
  180. x = p.x;
  181. y = p.y;
  182. w = p.w;
  183. h = p.h;
  184. return *this;
  185. }
  186. Rect& operator+=(const Rect &p) //works as operator+
  187. {
  188. x += p.x;
  189. y += p.y;
  190. return *this;
  191. }
  192. Rect& operator+=(const Point &p) //works as operator+
  193. {
  194. x += p.x;
  195. y += p.y;
  196. return *this;
  197. }
  198. Rect& operator-=(const Rect &p) //works as operator+
  199. {
  200. x -= p.x;
  201. y -= p.y;
  202. return *this;
  203. }
  204. Rect& operator-=(const Point &p) //works as operator+
  205. {
  206. x -= p.x;
  207. y -= p.y;
  208. return *this;
  209. }
  210. template<typename T> Rect operator-(const T &t)
  211. {
  212. return Rect(x - t.x, y - t.y, w, h);
  213. }
  214. Rect operator&(const Rect &p) const //rect intersection
  215. {
  216. bool intersect = true;
  217. if(p.topLeft().y < y && p.bottomLeft().y < y) //rect p is above *this
  218. {
  219. intersect = false;
  220. }
  221. else if(p.topLeft().y > y+h && p.bottomLeft().y > y+h) //rect p is below *this
  222. {
  223. intersect = false;
  224. }
  225. else if(p.topLeft().x > x+w && p.topRight().x > x+w) //rect p is on the right hand side of this
  226. {
  227. intersect = false;
  228. }
  229. else if(p.topLeft().x < x && p.topRight().x < x) //rect p is on the left hand side of this
  230. {
  231. intersect = false;
  232. }
  233. if(intersect)
  234. {
  235. Rect ret;
  236. ret.x = std::max(this->x, p.x);
  237. ret.y = std::max(this->y, p.y);
  238. Point bR; //bottomRight point of returned rect
  239. bR.x = std::min(this->w+this->x, p.w+p.x);
  240. bR.y = std::min(this->h+this->y, p.h+p.y);
  241. ret.w = bR.x - ret.x;
  242. ret.h = bR.y - ret.y;
  243. return ret;
  244. }
  245. else
  246. {
  247. return Rect();
  248. }
  249. }
  250. };
  251. class IShowable
  252. {
  253. public:
  254. void redraw();
  255. virtual void show(SDL_Surface * to)=0;
  256. virtual void showAll(SDL_Surface * to)
  257. {
  258. show(to);
  259. }
  260. virtual ~IShowable(){}; //d-tor
  261. };
  262. class IStatusBar
  263. {
  264. public:
  265. virtual ~IStatusBar(){}; //d-tor
  266. virtual void print(const std::string & text)=0; //prints text and refreshes statusbar
  267. virtual void clear()=0;//clears statusbar and refreshes
  268. virtual void show(SDL_Surface * to)=0; //shows statusbar (with current text)
  269. virtual std::string getCurrent()=0; //returns currently displayed text
  270. };
  271. class IActivable
  272. {
  273. public:
  274. virtual void activate()=0;
  275. virtual void deactivate()=0;
  276. virtual ~IActivable(){}; //d-tor
  277. };
  278. class IShowActivable : public IShowable, public IActivable
  279. {
  280. public:
  281. enum {WITH_GARRISON = 1, BLOCK_ADV_HOTKEYS = 2};
  282. int type; //bin flags using etype
  283. IShowActivable();
  284. virtual ~IShowActivable(){}; //d-tor
  285. };
  286. class IUpdateable
  287. {
  288. public:
  289. virtual void update()=0;
  290. virtual ~IUpdateable(){}; //d-tor
  291. };
  292. class CIntObject : public IShowActivable //interface object
  293. {
  294. public:
  295. CIntObject *parent; //parent object
  296. std::vector<CIntObject *> children;
  297. Rect pos, //position of object on the screen
  298. posRelative; //position of object in the parent (not used if no parent)
  299. CIntObject();
  300. virtual ~CIntObject(); //d-tor
  301. //l-clicks handling
  302. bool pressedL; //for determining if object is L-pressed
  303. void activateLClick();
  304. void deactivateLClick();
  305. virtual void clickLeft(tribool down, bool previousState);
  306. //r-clicks handling
  307. bool pressedR; //for determining if object is R-pressed
  308. void activateRClick();
  309. void deactivateRClick();
  310. virtual void clickRight(tribool down, bool previousState);
  311. //hover handling
  312. bool hovered; //for determining if object is hovered
  313. void activateHover();
  314. void deactivateHover();
  315. virtual void hover (bool on);
  316. //keyboard handling
  317. bool captureAllKeys; //if true, only this object should get info about pressed keys
  318. void activateKeys();
  319. void deactivateKeys();
  320. virtual void keyPressed(const SDL_KeyboardEvent & key);
  321. //mouse movement handling
  322. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  323. void activateMouseMove();
  324. void deactivateMouseMove();
  325. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  326. //time handling
  327. int toNextTick;
  328. void activateTimer();
  329. void deactivateTimer();
  330. virtual void tick();
  331. //mouse wheel
  332. void activateWheel();
  333. void deactivateWheel();
  334. virtual void wheelScrolled(bool down, bool in);
  335. //double click
  336. void activateDClick();
  337. void deactivateDClick();
  338. virtual void onDoubleClick();
  339. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, ALL=0xffff};
  340. ui16 active;
  341. ui16 used;
  342. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  343. ui8 defActions; //which calls will be tried to be redirected to children
  344. ui8 recActions; //which calls we allow te receive from parent
  345. enum EAlignment {TOPLEFT, CENTER, BOTTOMRIGHT};
  346. void disable(); //deactivates if needed, blocks all automatic activity, allows only disposal
  347. void enable(bool activation = true); //activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  348. void defActivate();
  349. void defDeactivate();
  350. void activate();
  351. void deactivate();
  352. void activate(ui16 what);
  353. void deactivate(ui16 what);
  354. void show(SDL_Surface * to);
  355. void showAll(SDL_Surface * to);
  356. void printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  357. void printToLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  358. void printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  359. void printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  360. void printAtMiddleWBLoc(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst, bool refrsh = false);
  361. void blitAtLoc(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  362. void blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst);
  363. bool isItInLoc(const SDL_Rect &rect, int x, int y);
  364. bool isItInLoc(const SDL_Rect &rect, const Point &p);
  365. const Rect & center(const Rect &r, bool propagate = true); //sets pos so that r will be in the center of screen, returns new position
  366. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  367. void moveBy(const Point &p, bool propagate = true);
  368. void moveTo(const Point &p, bool propagate = true);
  369. void changeUsedEvents(ui16 what, bool enable, bool adjust = true);
  370. void addChild(CIntObject *child, bool adjustPosition = false);
  371. void removeChild(CIntObject *child, bool adjustPosition = false);
  372. void delChild(CIntObject *child); //removes from children list, deletes
  373. template <typename T> void delChildNUll(T *&child, bool deactivateIfNeeded = false) //removes from children list, deletes and sets pointer to NULL
  374. {
  375. if(!child)
  376. return;
  377. if(deactivateIfNeeded && child->active)
  378. child->deactivate();
  379. delChild(child);
  380. child = NULL;
  381. }
  382. };
  383. //class for binding keys to left mouse button clicks
  384. //classes wanting use it should have it as one of their base classes
  385. class KeyShortcut : public virtual CIntObject
  386. {
  387. public:
  388. std::set<int> assignedKeys;
  389. KeyShortcut(){}; //c-tor
  390. KeyShortcut(int key){assignedKeys.insert(key);}; //c-tor
  391. KeyShortcut(std::set<int> Keys):assignedKeys(Keys){}; //c-tor
  392. virtual void keyPressed(const SDL_KeyboardEvent & key); //call-in
  393. };
  394. class CGarrisonHolder : public CIntObject// to unify updating garrisons via PlayerInterface
  395. {
  396. public:
  397. CGarrisonHolder();
  398. virtual void updateGarrisons(){};
  399. };
  400. class CWindowWithGarrison : public CGarrisonHolder
  401. {
  402. public:
  403. CGarrisonInt *garr;
  404. virtual void updateGarrisons();
  405. };
  406. class CSimpleWindow : public CIntObject
  407. {
  408. public:
  409. SDL_Surface * bitmap; //background
  410. virtual void show(SDL_Surface * to);
  411. CSimpleWindow():bitmap(NULL){}; //c-tor
  412. virtual ~CSimpleWindow(); //d-tor
  413. };
  414. class CPicture : public CIntObject
  415. {
  416. public:
  417. SDL_Surface *bg;
  418. Rect *srcRect; //if NULL then whole surface will be used
  419. bool freeSurf; //whether surface will be freed upon CPicture destruction
  420. operator SDL_Surface*()
  421. {
  422. return bg;
  423. }
  424. CPicture(const Rect &r, const SDL_Color &color, bool screenFormat = false); //rect filled with given color
  425. CPicture(const Rect &r, ui32 color, bool screenFormat = false); //rect filled with given color
  426. CPicture(SDL_Surface *BG, int x, int y, bool Free = true); //wrap existing SDL_Surface
  427. CPicture(const std::string &bmpname, int x=0, int y=0);
  428. CPicture(SDL_Surface *BG, const Rect &SrcRext, int x = 0, int y = 0, bool free = false); //wrap subrect of given surface
  429. void init();
  430. void createSimpleRect(const Rect &r, bool screenFormat, ui32 color);
  431. ~CPicture();
  432. void showAll(SDL_Surface * to);
  433. void convertToScreenBPP();
  434. void colorizeAndConvert(int player);
  435. };
  436. class CGuiHandler
  437. {
  438. public:
  439. timeHandler th;
  440. std::list<IShowActivable *> listInt; //list of interfaces - front=foreground; back = background (includes adventure map, window interfaces, all kind of active dialogs, and so on)
  441. IStatusBar * statusbar;
  442. //active GUI elements (listening for events
  443. std::list<CIntObject*> lclickable;
  444. std::list<CIntObject*> rclickable;
  445. std::list<CIntObject*> hoverable;
  446. std::list<CIntObject*> keyinterested;
  447. std::list<CIntObject*> motioninterested;
  448. std::list<CIntObject*> timeinterested;
  449. std::list<CIntObject*> wheelInterested;
  450. std::list<CIntObject*> doubleClickInterested;
  451. //objs to blit
  452. std::vector<IShowable*> objsToBlit;
  453. SDL_Event * current; //current event - can be set to NULL to stop handling event
  454. IUpdateable *curInt;
  455. Point lastClick;
  456. unsigned lastClickTime;
  457. bool terminate;
  458. CGuiHandler();
  459. ~CGuiHandler();
  460. void run();
  461. void totalRedraw(); //forces total redraw (using showAll)
  462. void simpleRedraw(); //update only top interface and draw background from buffer
  463. void popInt(IShowActivable *top); //removes given interface from the top and activates next
  464. void popIntTotally(IShowActivable *top); //deactivates, deletes, removes given interface from the top and activates next
  465. void pushInt(IShowActivable *newInt); //deactivate old top interface, activates this one and pushes to the top
  466. void popInts(int howMany); //pops one or more interfaces - deactivates top, deletes and removes given number of interfaces, activates new front
  467. IShowActivable *topInt(); //returns top interface
  468. void updateTime(); //handles timeInterested
  469. void handleEvents(); //takes events from queue and calls interested objects
  470. void handleEvent(SDL_Event *sEvent);
  471. void handleMouseMotion(SDL_Event *sEvent);
  472. void handleMoveInterested( const SDL_MouseMotionEvent & motion );
  473. void fakeMouseMove();
  474. void breakEventHandling(); //current event won't be propagated anymore
  475. ui8 defActionsDef; //default auto actions
  476. ui8 captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  477. std::list<CIntObject *> createdObj; //stack of objs being created
  478. };
  479. extern CGuiHandler GH; //global gui handler
  480. SDLKey arrowToNum(SDLKey key); //converts arrow key to according numpad key
  481. SDLKey numToDigit(SDLKey key);//converts numpad digit key to normal digit key
  482. bool isNumKey(SDLKey key, bool number = true); //checks if key is on numpad (numbers - check only for numpad digits)
  483. bool isArrowKey(SDLKey key);
  484. CIntObject * moveChild(CIntObject *obj, CIntObject *from, CIntObject *to, bool adjustPos = false);
  485. template <typename T> void pushIntT()
  486. {
  487. GH.pushInt(new T());
  488. }
  489. struct ObjectConstruction
  490. {
  491. CIntObject *myObj;
  492. ObjectConstruction(CIntObject *obj);
  493. ~ObjectConstruction();
  494. };
  495. struct SetCaptureState
  496. {
  497. bool previousCapture;
  498. ui8 prevActions;
  499. SetCaptureState(bool allow, ui8 actions);
  500. ~SetCaptureState();
  501. };
  502. #define OBJ_CONSTRUCTION ObjectConstruction obj__i(this)
  503. #define OBJ_CONSTRUCTION_CAPTURING_ALL defActions = 255; SetCaptureState obj__i1(true, 255); ObjectConstruction obj__i(this)
  504. #define BLOCK_CAPTURING SetCaptureState obj__i(false, 0)
  505. #define BLOCK_CAPTURING_DONT_TOUCH_REC_ACTIONS SetCaptureState obj__i(false, GH.defActionsDef)
  506. #endif //__GUIBASE_H__