GUIBase.h 17 KB

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