GUIBase.h 15 KB

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