GUIBase.h 17 KB

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