GUIBase.h 16 KB

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