GUIBase.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. };
  271. /// Defines a show method
  272. class IShowable
  273. {
  274. public:
  275. void redraw();
  276. virtual void show(SDL_Surface * to)=0;
  277. virtual void showAll(SDL_Surface * to)
  278. {
  279. show(to);
  280. }
  281. virtual ~IShowable(){}; //d-tor
  282. };
  283. /// Status bar interface
  284. class IStatusBar
  285. {
  286. public:
  287. virtual ~IStatusBar(){}; //d-tor
  288. virtual void print(const std::string & text)=0; //prints text and refreshes statusbar
  289. virtual void clear()=0;//clears statusbar and refreshes
  290. virtual void show(SDL_Surface * to)=0; //shows statusbar (with current text)
  291. virtual std::string getCurrent()=0; //returns currently displayed text
  292. };
  293. /// Defines a activate/deactive method
  294. class IActivable
  295. {
  296. public:
  297. virtual void activate()=0;
  298. virtual void deactivate()=0;
  299. virtual ~IActivable(){}; //d-tor
  300. };
  301. class IShowActivable : public IShowable, public IActivable
  302. {
  303. public:
  304. enum {WITH_GARRISON = 1, BLOCK_ADV_HOTKEYS = 2, WITH_ARTIFACTS = 4};
  305. int type; //bin flags using etype
  306. IShowActivable();
  307. virtual ~IShowActivable(){}; //d-tor
  308. };
  309. class IUpdateable
  310. {
  311. public:
  312. virtual void update()=0;
  313. virtual ~IUpdateable(){}; //d-tor
  314. };
  315. /// Base UI element
  316. class CIntObject : public IShowActivable //interface object
  317. {
  318. public:
  319. CIntObject *parent; //parent object
  320. std::vector<CIntObject *> children;
  321. Rect pos, //position of object on the screen
  322. posRelative; //position of object in the parent (not used if no parent)
  323. CIntObject();
  324. virtual ~CIntObject(); //d-tor
  325. //l-clicks handling
  326. bool pressedL; //for determining if object is L-pressed
  327. void activateLClick();
  328. void deactivateLClick();
  329. virtual void clickLeft(tribool down, bool previousState);
  330. //r-clicks handling
  331. bool pressedR; //for determining if object is R-pressed
  332. void activateRClick();
  333. void deactivateRClick();
  334. virtual void clickRight(tribool down, bool previousState);
  335. //hover handling
  336. bool hovered; //for determining if object is hovered
  337. void activateHover();
  338. void deactivateHover();
  339. virtual void hover (bool on);
  340. //keyboard handling
  341. bool captureAllKeys; //if true, only this object should get info about pressed keys
  342. void activateKeys();
  343. void deactivateKeys();
  344. virtual void keyPressed(const SDL_KeyboardEvent & key);
  345. //mouse movement handling
  346. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  347. void activateMouseMove();
  348. void deactivateMouseMove();
  349. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  350. //time handling
  351. int toNextTick;
  352. void activateTimer();
  353. void deactivateTimer();
  354. virtual void tick();
  355. //mouse wheel
  356. void activateWheel();
  357. void deactivateWheel();
  358. virtual void wheelScrolled(bool down, bool in);
  359. //double click
  360. void activateDClick();
  361. void deactivateDClick();
  362. virtual void onDoubleClick();
  363. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, ALL=0xffff};
  364. ui16 active;
  365. ui16 used;
  366. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  367. ui8 defActions; //which calls will be tried to be redirected to children
  368. ui8 recActions; //which calls we allow te receive from parent
  369. enum EAlignment {TOPLEFT, CENTER, BOTTOMRIGHT};
  370. void disable(); //deactivates if needed, blocks all automatic activity, allows only disposal
  371. void enable(bool activation = true); //activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  372. void defActivate();
  373. void defDeactivate();
  374. void activate();
  375. void deactivate();
  376. void activate(ui16 what);
  377. void deactivate(ui16 what);
  378. void show(SDL_Surface * to);
  379. void showAll(SDL_Surface * to);
  380. void printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst);
  381. void printToLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst);
  382. void printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst);
  383. void printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color kolor, SDL_Surface * dst);
  384. void printAtMiddleWBLoc(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst);
  385. void blitAtLoc(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  386. void blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst);
  387. bool isItInLoc(const SDL_Rect &rect, int x, int y);
  388. bool isItInLoc(const SDL_Rect &rect, const Point &p);
  389. 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
  390. const Rect & center(const Point &p, bool propagate = true); //moves object so that point p will be in its center
  391. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  392. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  393. void moveBy(const Point &p, bool propagate = true);
  394. void moveTo(const Point &p, bool propagate = true);
  395. void changeUsedEvents(ui16 what, bool enable, bool adjust = true);
  396. void addChild(CIntObject *child, bool adjustPosition = false);
  397. void removeChild(CIntObject *child, bool adjustPosition = false);
  398. void delChild(CIntObject *child); //removes from children list, deletes
  399. template <typename T> void delChildNUll(T *&child, bool deactivateIfNeeded = false) //removes from children list, deletes and sets pointer to NULL
  400. {
  401. if(!child)
  402. return;
  403. if(deactivateIfNeeded && child->active)
  404. child->deactivate();
  405. delChild(child);
  406. child = NULL;
  407. }
  408. };
  409. /// Class for binding keys to left mouse button clicks
  410. /// Classes wanting use it should have it as one of their base classes
  411. class KeyShortcut : public virtual CIntObject
  412. {
  413. public:
  414. std::set<int> assignedKeys;
  415. KeyShortcut(){}; //c-tor
  416. KeyShortcut(int key){assignedKeys.insert(key);}; //c-tor
  417. KeyShortcut(std::set<int> Keys):assignedKeys(Keys){}; //c-tor
  418. virtual void keyPressed(const SDL_KeyboardEvent & key); //call-in
  419. };
  420. /// to unify updating garrisons via PlayerInterface
  421. class CGarrisonHolder : public virtual CIntObject
  422. {
  423. public:
  424. CGarrisonHolder();
  425. virtual void updateGarrisons(){};
  426. };
  427. class CWindowWithGarrison : public CGarrisonHolder
  428. {
  429. public:
  430. CGarrisonInt *garr;
  431. virtual void updateGarrisons();
  432. };
  433. /// Window GUI class
  434. class CSimpleWindow : public CIntObject
  435. {
  436. public:
  437. SDL_Surface * bitmap; //background
  438. virtual void show(SDL_Surface * to);
  439. CSimpleWindow():bitmap(NULL){}; //c-tor
  440. virtual ~CSimpleWindow(); //d-tor
  441. };
  442. /// Image class
  443. class CPicture : public CIntObject
  444. {
  445. public:
  446. SDL_Surface *bg;
  447. Rect *srcRect; //if NULL then whole surface will be used
  448. bool freeSurf; //whether surface will be freed upon CPicture destruction
  449. bool needRefresh;//Surface needs to be displayed each frame
  450. operator SDL_Surface*()
  451. {
  452. return bg;
  453. }
  454. CPicture(const Rect &r, const SDL_Color &color, bool screenFormat = false); //rect filled with given color
  455. CPicture(const Rect &r, ui32 color, bool screenFormat = false); //rect filled with given color
  456. CPicture(SDL_Surface *BG, int x=0, int y=0, bool Free = true); //wrap existing SDL_Surface
  457. CPicture(const std::string &bmpname, int x=0, int y=0);
  458. CPicture(SDL_Surface *BG, const Rect &SrcRext, int x = 0, int y = 0, bool free = false); //wrap subrect of given surface
  459. void init();
  460. void createSimpleRect(const Rect &r, bool screenFormat, ui32 color);
  461. ~CPicture();
  462. void show(SDL_Surface * to);
  463. void showAll(SDL_Surface * to);
  464. void convertToScreenBPP();
  465. void colorizeAndConvert(int player);
  466. void colorize(int player);
  467. };
  468. /// Handles GUI logic and drawing
  469. class CGuiHandler
  470. {
  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 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. class Colors
  538. {
  539. public:
  540. static SDL_Color MetallicGold;
  541. static SDL_Color Yellow;
  542. static SDL_Color createColor(int r, int g, int b);
  543. };
  544. #define OBJ_CONSTRUCTION ObjectConstruction obj__i(this)
  545. #define OBJ_CONSTRUCTION_CAPTURING_ALL defActions = 255; SetCaptureState obj__i1(true, 255); ObjectConstruction obj__i(this)
  546. #define BLOCK_CAPTURING SetCaptureState obj__i(false, 0)
  547. #define BLOCK_CAPTURING_DONT_TOUCH_REC_ACTIONS SetCaptureState obj__i(false, GH.defActionsDef)
  548. #endif //__GUIBASE_H__