GUIBase.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. class 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. Point(int X, int Y)
  60. :x(X),y(Y)
  61. {};
  62. Point(const int3 &a)
  63. :x(a.x),y(a.y)
  64. {}
  65. template<typename T>
  66. Point operator+(const T &b) const
  67. {
  68. return Point(x+b.x,y+b.y);
  69. }
  70. template<typename T>
  71. Point& operator+=(const T &b)
  72. {
  73. x += b.x;
  74. y += b.y;
  75. return *this;
  76. }
  77. template<typename T>
  78. Point operator-(const T &b) const
  79. {
  80. return Point(x - b.x, y - b.y);
  81. }
  82. template<typename T>
  83. Point& operator-=(const T &b)
  84. {
  85. x -= b.x;
  86. y -= b.y;
  87. return *this;
  88. }
  89. bool operator<(const Point &b) const //product order
  90. {
  91. return x < b.x && y < b.y;
  92. }
  93. template<typename T> Point& operator=(const T &t)
  94. {
  95. x = t.x;
  96. y = t.y;
  97. return *this;
  98. }
  99. template<typename T> bool operator==(const T &t)
  100. {
  101. return x == t.x && y == t.y;
  102. }
  103. };
  104. struct Rect : public SDL_Rect
  105. {
  106. Rect()//default c-tor
  107. {
  108. x = y = w = h = -1;
  109. }
  110. Rect(int X, int Y, int W, int H) //c-tor
  111. {
  112. x = X;
  113. y = Y;
  114. w = W;
  115. h = H;
  116. }
  117. Rect(const SDL_Rect & r) //c-tor
  118. {
  119. x = r.x;
  120. y = r.y;
  121. w = r.w;
  122. h = r.h;
  123. }
  124. bool isIn(int qx, int qy) const //determines if given point lies inside rect
  125. {
  126. if (qx > x && qx<x+w && qy>y && qy<y+h)
  127. return true;
  128. return false;
  129. }
  130. bool isIn(const Point &q) const //determines if given point lies inside rect
  131. {
  132. return isIn(q.x,q.y);
  133. }
  134. Point topLeft() const //top left corner of this rect
  135. {
  136. return Point(x,y);
  137. }
  138. Point topRight() const //top right corner of this rect
  139. {
  140. return Point(x+w,y);
  141. }
  142. Point bottomLeft() const //bottom left corner of this rect
  143. {
  144. return Point(x,y+h);
  145. }
  146. Point bottomRight() const //bottom right corner of this rect
  147. {
  148. return Point(x+w,y+h);
  149. }
  150. Rect operator+(const Rect &p) const //moves this rect by p's rect position
  151. {
  152. return Rect(x+p.x,y+p.y,w,h);
  153. }
  154. Rect operator+(const Point &p) const //moves this rect by p's point position
  155. {
  156. return Rect(x+p.x,y+p.y,w,h);
  157. }
  158. Rect& operator=(const Point &p) //assignment operator
  159. {
  160. x = p.x;
  161. y = p.y;
  162. return *this;
  163. }
  164. Rect& operator=(const Rect &p) //assignment operator
  165. {
  166. x = p.x;
  167. y = p.y;
  168. w = p.w;
  169. h = p.h;
  170. return *this;
  171. }
  172. Rect& operator+=(const Rect &p) //works as operator+
  173. {
  174. x += p.x;
  175. y += p.y;
  176. return *this;
  177. }
  178. Rect& operator+=(const Point &p) //works as operator+
  179. {
  180. x += p.x;
  181. y += p.y;
  182. return *this;
  183. }
  184. Rect& operator-=(const Rect &p) //works as operator+
  185. {
  186. x -= p.x;
  187. y -= p.y;
  188. return *this;
  189. }
  190. Rect& operator-=(const Point &p) //works as operator+
  191. {
  192. x -= p.x;
  193. y -= p.y;
  194. return *this;
  195. }
  196. template<typename T> Rect operator-(const T &t)
  197. {
  198. return Rect(x + t.x, y + t.y, w, h);
  199. }
  200. Rect operator&(const Rect &p) const //rect intersection
  201. {
  202. bool intersect = true;
  203. if(p.topLeft().y < y && p.bottomLeft().y < y) //rect p is above *this
  204. {
  205. intersect = false;
  206. }
  207. else if(p.topLeft().y > y+h && p.bottomLeft().y > y+h) //rect p is below *this
  208. {
  209. intersect = false;
  210. }
  211. else if(p.topLeft().x > x+w && p.topRight().x > x+w) //rect p is on the right hand side of this
  212. {
  213. intersect = false;
  214. }
  215. else if(p.topLeft().x < x && p.topRight().x < x) //rect p is on the left hand side of this
  216. {
  217. intersect = false;
  218. }
  219. if(intersect)
  220. {
  221. Rect ret;
  222. ret.x = std::max(this->x, p.x);
  223. ret.y = std::max(this->y, p.y);
  224. Point bR; //bottomRight point of returned rect
  225. bR.x = std::min(this->w+this->x, p.w+p.x);
  226. bR.y = std::min(this->h+this->y, p.h+p.y);
  227. ret.w = bR.x - ret.x;
  228. ret.h = bR.y - ret.y;
  229. return ret;
  230. }
  231. else
  232. {
  233. return Rect();
  234. }
  235. }
  236. };
  237. class IShowable
  238. {
  239. public:
  240. void redraw();
  241. virtual void show(SDL_Surface * to)=0;
  242. virtual void showAll(SDL_Surface * to)
  243. {
  244. show(to);
  245. }
  246. virtual ~IShowable(){}; //d-tor
  247. };
  248. class IStatusBar
  249. {
  250. public:
  251. virtual ~IStatusBar(){}; //d-tor
  252. virtual void print(const std::string & text)=0; //prints text and refreshes statusbar
  253. virtual void clear()=0;//clears statusbar and refreshes
  254. virtual void show(SDL_Surface * to)=0; //shows statusbar (with current text)
  255. virtual std::string getCurrent()=0; //returns currently displayed text
  256. };
  257. class IActivable
  258. {
  259. public:
  260. virtual void activate()=0;
  261. virtual void deactivate()=0;
  262. virtual ~IActivable(){}; //d-tor
  263. };
  264. class IShowActivable : public IShowable, public IActivable
  265. {
  266. public:
  267. enum {WITH_GARRISON = 1, BLOCK_ADV_HOTKEYS = 2};
  268. int type; //bin flags using etype
  269. IShowActivable();
  270. virtual ~IShowActivable(){}; //d-tor
  271. };
  272. class CIntObject : public IShowActivable //interface object
  273. {
  274. public:
  275. CIntObject *parent; //parent object
  276. std::vector<CIntObject *> children;
  277. Rect pos, //position of object on the screen
  278. posRelative; //position of object in the parent (not used if no parent)
  279. int ID; //object ID, rarely used by some classes for identification / internal info
  280. CIntObject();
  281. virtual ~CIntObject();; //d-tor
  282. //l-clicks handling
  283. bool pressedL; //for determining if object is L-pressed
  284. void activateLClick();
  285. void deactivateLClick();
  286. virtual void clickLeft(tribool down, bool previousState);
  287. //r-clicks handling
  288. bool pressedR; //for determining if object is R-pressed
  289. void activateRClick();
  290. void deactivateRClick();
  291. virtual void clickRight(tribool down, bool previousState);
  292. //hover handling
  293. bool hovered; //for determining if object is hovered
  294. void activateHover();
  295. void deactivateHover();
  296. virtual void hover (bool on);
  297. //keyboard handling
  298. bool captureAllKeys; //if true, only this object should get info about pressed keys
  299. void activateKeys();
  300. void deactivateKeys();
  301. virtual void keyPressed(const SDL_KeyboardEvent & key);
  302. //mouse movement handling
  303. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  304. void activateMouseMove();
  305. void deactivateMouseMove();
  306. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  307. //time handling
  308. int toNextTick;
  309. void activateTimer();
  310. void deactivateTimer();
  311. virtual void tick();
  312. //mouse wheel
  313. void activateWheel();
  314. void deactivateWheel();
  315. virtual void wheelScrolled(bool down, bool in);
  316. //double click
  317. void activateDClick();
  318. void deactivateDClick();
  319. virtual void onDoubleClick();
  320. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, ALL=0xffff};
  321. ui16 active;
  322. ui16 used;
  323. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  324. ui8 defActions; //which calls will be tried to be redirected to children
  325. ui8 recActions; //which calls we allow te receive from parent
  326. void disable(); //deactivates if needed, blocks all automatic activity, allows only disposal
  327. void enable(bool activation = true); //activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  328. void defActivate();
  329. void defDeactivate();
  330. void activate();
  331. void deactivate();
  332. void show(SDL_Surface * to);
  333. void showAll(SDL_Surface * to);
  334. void printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  335. void printToLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  336. void printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  337. void printAtMiddleWBLoc(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst, bool refrsh = false);
  338. void blitAtLoc(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  339. bool isItInLoc(const SDL_Rect &rect, int x, int y);
  340. bool isItInLoc(const SDL_Rect &rect, const Point &p);
  341. const Rect & center(const Rect &r); //sets pos so that r will be in the center of screen, returns new position
  342. const Rect & center(); //centers when pos.w and pos.h are set, returns new position
  343. void moveBy(const Point &p, bool propagate = true);
  344. void moveTo(const Point &p, bool propagate = true);
  345. };
  346. //class for binding keys to left mouse button clicks
  347. //classes wanting use it should have it as one of their base classes
  348. class KeyShortcut : public virtual CIntObject
  349. {
  350. public:
  351. std::set<int> assignedKeys;
  352. KeyShortcut(){}; //c-tor
  353. KeyShortcut(int key){assignedKeys.insert(key);}; //c-tor
  354. KeyShortcut(std::set<int> Keys):assignedKeys(Keys){}; //c-tor
  355. virtual void keyPressed(const SDL_KeyboardEvent & key); //call-in
  356. };
  357. class CWindowWithGarrison : public CIntObject
  358. {
  359. public:
  360. CGarrisonInt *garr;
  361. CWindowWithGarrison();
  362. };
  363. class CSimpleWindow : public CIntObject
  364. {
  365. public:
  366. SDL_Surface * bitmap; //background
  367. CIntObject * owner; //who made this window
  368. virtual void show(SDL_Surface * to);
  369. CSimpleWindow():bitmap(NULL),owner(NULL){}; //c-tor
  370. virtual ~CSimpleWindow(); //d-tor
  371. void activate(){};
  372. void deactivate(){};
  373. };
  374. class CPicture : public CIntObject
  375. {
  376. public:
  377. SDL_Surface *bg;
  378. bool freeSurf;
  379. CPicture(SDL_Surface *BG, int x, int y, bool Free = true);
  380. CPicture(const std::string &bmpname, int x, int y);
  381. ~CPicture();
  382. void showAll(SDL_Surface * to);
  383. };
  384. class CGuiHandler
  385. {
  386. public:
  387. timeHandler th;
  388. std::list<IShowActivable *> listInt; //list of interfaces - front=foreground; back = background (includes adventure map, window interfaces, all kind of active dialogs, and so on)
  389. //active GUI elements (listening for events
  390. std::list<CIntObject*> lclickable;
  391. std::list<CIntObject*> rclickable;
  392. std::list<CIntObject*> hoverable;
  393. std::list<CIntObject*> keyinterested;
  394. std::list<CIntObject*> motioninterested;
  395. std::list<CIntObject*> timeinterested;
  396. std::list<CIntObject*> wheelInterested;
  397. std::list<CIntObject*> doubleClickInterested;
  398. //objs to blit
  399. std::vector<IShowable*> objsToBlit;
  400. SDL_Event * current; //current event
  401. Point lastClick;
  402. unsigned lastClickTime;
  403. void totalRedraw(); //forces total redraw (using showAll)
  404. void simpleRedraw(); //update only top interface and draw background from buffer
  405. void popInt(IShowActivable *top); //removes given interface from the top and activates next
  406. void popIntTotally(IShowActivable *top); //deactivates, deletes, removes given interface from the top and activates next
  407. void pushInt(IShowActivable *newInt); //deactivate old top interface, activates this one and pushes to the top
  408. void popInts(int howMany); //pops one or more interfaces - deactivates top, deletes and removes given number of interfaces, activates new front
  409. IShowActivable *topInt(); //returns top interface
  410. void updateTime(); //handles timeInterested
  411. void handleEvents(); //takes events from queue and calls interested objects
  412. void handleEvent(SDL_Event *sEvent);
  413. void handleMouseMotion(SDL_Event *sEvent);
  414. void handleMoveInterested( const SDL_MouseMotionEvent & motion );
  415. void fakeMouseMove();
  416. ui8 defActionsDef; //default auto actions
  417. ui8 captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  418. std::list<CIntObject *> createdObj; //stack of objs being created
  419. };
  420. SDLKey arrowToNum(SDLKey key); //converts arrow key to according numpad key
  421. SDLKey numToDigit(SDLKey key);//converts numpad digit key to normal digit key
  422. bool isNumKey(SDLKey key, bool number = true); //checks if key is on numpad (numbers - check only for numpad digits)
  423. bool isArrowKey(SDLKey key);
  424. extern CGuiHandler GH; //global gui handler
  425. struct ObjectConstruction
  426. {
  427. CIntObject *myObj;
  428. ObjectConstruction(CIntObject *obj);
  429. ~ObjectConstruction();
  430. };
  431. struct SetCaptureState
  432. {
  433. bool previousCapture;
  434. ui8 prevActions;
  435. SetCaptureState(bool allow, ui8 actions);
  436. ~SetCaptureState();
  437. };
  438. #define OBJ_CONSTRUCTION ObjectConstruction obj__i(this)
  439. #define OBJ_CONSTRUCTION_CAPTURING_ALL defActions = 255; SetCaptureState obj__i1(true, 255); ObjectConstruction obj__i(this)
  440. #define BLOCK_CAPTURING SetCaptureState obj__i(false, 0)
  441. #endif //__GUIBASE_H__