GUIBase.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. #ifdef max
  9. #undef max
  10. #endif
  11. #ifdef min
  12. #undef min
  13. #endif
  14. /*
  15. * GUIBase.h, part of VCMI engine
  16. *
  17. * Authors: listed in file AUTHORS in main folder
  18. *
  19. * License: GNU General Public License v2.0 or later
  20. * Full text of license available in license.txt file, in main folder
  21. *
  22. */
  23. class CDefEssential;
  24. class AdventureMapButton;
  25. class CHighlightableButtonsGroup;
  26. class CDefHandler;
  27. struct HeroMoveDetails;
  28. class CDefEssential;
  29. class CGHeroInstance;
  30. class CAdvMapInt;
  31. class CCastleInterface;
  32. class CBattleInterface;
  33. class CStack;
  34. class SComponent;
  35. class CCreature;
  36. struct SDL_Surface;
  37. struct CPath;
  38. class CCreatureAnimation;
  39. class CSelectableComponent;
  40. class CCreatureSet;
  41. class CGObjectInstance;
  42. class CSlider;
  43. struct UpgradeInfo;
  44. template <typename T> struct CondSh;
  45. class CInGameConsole;
  46. class CGarrisonInt;
  47. class CInGameConsole;
  48. class Component;
  49. class CArmedInstance;
  50. class CGTownInstance;
  51. class StackState;
  52. class CPlayerInterface;
  53. enum EFonts;
  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. Point operator+(const Point &b) const
  66. {
  67. return Point(x+b.x,y+b.y);
  68. }
  69. Point& operator+=(const Point &b)
  70. {
  71. x += b.x;
  72. y += b.y;
  73. return *this;
  74. }
  75. Point operator-(const Point &b) const
  76. {
  77. return Point(x+b.x,y+b.y);
  78. }
  79. Point& operator-=(const Point &b)
  80. {
  81. x -= b.x;
  82. y -= b.y;
  83. return *this;
  84. }
  85. bool operator<(const Point &b) const //product order
  86. {
  87. return x < b.x && y < b.y;
  88. }
  89. };
  90. struct Rect : public SDL_Rect
  91. {
  92. Rect()//default c-tor
  93. {
  94. x = y = w = h = -1;
  95. }
  96. Rect(int X, int Y, int W, int H) //c-tor
  97. {
  98. x = X;
  99. y = Y;
  100. w = W;
  101. h = H;
  102. }
  103. Rect(const SDL_Rect & r) //c-tor
  104. {
  105. x = r.x;
  106. y = r.y;
  107. w = r.w;
  108. h = r.h;
  109. }
  110. bool isIn(int qx, int qy) const //determines if given point lies inside rect
  111. {
  112. if (qx > x && qx<x+w && qy>y && qy<y+h)
  113. return true;
  114. return false;
  115. }
  116. bool isIn(const Point &q) const //determines if given point lies inside rect
  117. {
  118. return isIn(q.x,q.y);
  119. }
  120. Point topLeft() const //top left corner of this rect
  121. {
  122. return Point(x,y);
  123. }
  124. Point topRight() const //top right corner of this rect
  125. {
  126. return Point(x+w,y);
  127. }
  128. Point bottomLeft() const //bottom left corner of this rect
  129. {
  130. return Point(x,y+h);
  131. }
  132. Point bottomRight() const //bottom right corner of this rect
  133. {
  134. return Point(x+w,y+h);
  135. }
  136. Rect operator+(const Rect &p) const //moves this rect by p's rect position
  137. {
  138. return Rect(x+p.x,y+p.y,w,h);
  139. }
  140. Rect operator+(const Point &p) const //moves this rect by p's point position
  141. {
  142. return Rect(x+p.x,y+p.y,w,h);
  143. }
  144. Rect& operator=(const Rect &p) //assignment operator
  145. {
  146. x = p.x;
  147. y = p.y;
  148. w = p.w;
  149. h = p.h;
  150. return *this;
  151. }
  152. Rect& operator+=(const Rect &p) //works as operator+
  153. {
  154. x += p.x;
  155. y += p.y;
  156. return *this;
  157. }
  158. Rect& operator+=(const Point &p) //works as operator+
  159. {
  160. x += p.x;
  161. y += p.y;
  162. return *this;
  163. }
  164. Rect& operator-=(const Rect &p) //works as operator+
  165. {
  166. x -= p.x;
  167. y -= p.y;
  168. return *this;
  169. }
  170. Rect& operator-=(const Point &p) //works as operator+
  171. {
  172. x -= p.x;
  173. y -= p.y;
  174. return *this;
  175. }
  176. Rect operator&(const Rect &p) const //rect intersection
  177. {
  178. bool intersect = true;
  179. if(p.topLeft().y < y && p.bottomLeft().y < y) //rect p is above *this
  180. {
  181. intersect = false;
  182. }
  183. else if(p.topLeft().y > y+h && p.bottomLeft().y > y+h) //rect p is below *this
  184. {
  185. intersect = false;
  186. }
  187. else if(p.topLeft().x > x+w && p.topRight().x > x+w) //rect p is on the right hand side of this
  188. {
  189. intersect = false;
  190. }
  191. else if(p.topLeft().x < x && p.topRight().x < x) //rect p is on the left hand side of this
  192. {
  193. intersect = false;
  194. }
  195. if(intersect)
  196. {
  197. Rect ret;
  198. ret.x = std::max(this->x, p.x);
  199. ret.y = std::max(this->y, p.y);
  200. Point bR; //bottomRight point of returned rect
  201. bR.x = std::min(this->w+this->x, p.w+p.x);
  202. bR.y = std::min(this->h+this->y, p.h+p.y);
  203. ret.w = bR.x - ret.x;
  204. ret.h = bR.y - ret.y;
  205. return ret;
  206. }
  207. else
  208. {
  209. return Rect();
  210. }
  211. }
  212. };
  213. class IShowable
  214. {
  215. public:
  216. void redraw();
  217. virtual void show(SDL_Surface * to)=0;
  218. virtual void showAll(SDL_Surface * to)
  219. {
  220. show(to);
  221. }
  222. virtual ~IShowable(){}; //d-tor
  223. };
  224. class IStatusBar
  225. {
  226. public:
  227. virtual ~IStatusBar(){}; //d-tor
  228. virtual void print(const std::string & text)=0; //prints text and refreshes statusbar
  229. virtual void clear()=0;//clears statusbar and refreshes
  230. virtual void show(SDL_Surface * to)=0; //shows statusbar (with current text)
  231. virtual std::string getCurrent()=0; //returns currently displayed text
  232. };
  233. class IActivable
  234. {
  235. public:
  236. virtual void activate()=0;
  237. virtual void deactivate()=0;
  238. virtual ~IActivable(){}; //d-tor
  239. };
  240. class IShowActivable : public IShowable, public IActivable
  241. {
  242. public:
  243. enum {WITH_GARRISON = 1};
  244. int type; //bin flags using etype
  245. IShowActivable();
  246. virtual ~IShowActivable(){}; //d-tor
  247. };
  248. class CIntObject : public IShowActivable //interface object
  249. {
  250. public:
  251. CIntObject *parent; //parent object
  252. std::vector<CIntObject *> children;
  253. Rect pos, //position of object on the screen
  254. posRelative; //position of object in the parent (not used if no parent)
  255. int ID; //object ID, rarely used by some classes for identification / internal info
  256. CIntObject();
  257. virtual ~CIntObject();; //d-tor
  258. //l-clicks handling
  259. bool pressedL; //for determining if object is L-pressed
  260. void activateLClick();
  261. void deactivateLClick();
  262. virtual void clickLeft(tribool down, bool previousState);
  263. //r-clicks handling
  264. bool pressedR; //for determining if object is R-pressed
  265. void activateRClick();
  266. void deactivateRClick();
  267. virtual void clickRight(tribool down, bool previousState);
  268. //hover handling
  269. bool hovered; //for determining if object is hovered
  270. void activateHover();
  271. void deactivateHover();
  272. virtual void hover (bool on);
  273. //keyboard handling
  274. bool captureAllKeys; //if true, only this object should get info about pressed keys
  275. void activateKeys();
  276. void deactivateKeys();
  277. virtual void keyPressed(const SDL_KeyboardEvent & key);
  278. //mouse movement handling
  279. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  280. void activateMouseMove();
  281. void deactivateMouseMove();
  282. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  283. //time handling
  284. int toNextTick;
  285. void activateTimer();
  286. void deactivateTimer();
  287. virtual void tick();
  288. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64};
  289. ui8 active;
  290. ui8 used;
  291. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  292. ui8 defActions; //which calls will be tried to be redirected to children
  293. ui8 recActions; //which calls we allow te receive from parent
  294. void defActivate();
  295. void defDeactivate();
  296. void activate();
  297. void deactivate();
  298. void show(SDL_Surface * to);
  299. void showAll(SDL_Surface * to);
  300. void printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  301. void printToLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  302. void printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  303. void printAtMiddleWBLoc(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst, bool refrsh = false);
  304. void blitAtLoc(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  305. };
  306. //class for binding keys to left mouse button clicks
  307. //classes wanting use it should have it as one of their base classes
  308. class KeyShortcut : public virtual CIntObject
  309. {
  310. public:
  311. std::set<int> assignedKeys;
  312. KeyShortcut(){}; //c-tor
  313. KeyShortcut(int key){assignedKeys.insert(key);}; //c-tor
  314. KeyShortcut(std::set<int> Keys):assignedKeys(Keys){}; //c-tor
  315. virtual void keyPressed(const SDL_KeyboardEvent & key); //call-in
  316. };
  317. class CWindowWithGarrison : public CIntObject
  318. {
  319. public:
  320. CGarrisonInt *garr;
  321. CWindowWithGarrison();
  322. };
  323. class CSimpleWindow : public CIntObject
  324. {
  325. public:
  326. SDL_Surface * bitmap; //background
  327. CIntObject * owner; //who made this window
  328. virtual void show(SDL_Surface * to);
  329. CSimpleWindow():bitmap(NULL),owner(NULL){}; //c-tor
  330. virtual ~CSimpleWindow(); //d-tor
  331. void activate(){};
  332. void deactivate(){};
  333. };
  334. class CPicture : public CIntObject
  335. {
  336. public:
  337. SDL_Surface *bg;
  338. bool freeSurf;
  339. CPicture(SDL_Surface *BG, int x, int y, bool Free);
  340. ~CPicture();
  341. void showAll(SDL_Surface * to);
  342. };
  343. class CGuiHandler
  344. {
  345. public:
  346. timeHandler th;
  347. std::list<IShowActivable *> listInt; //list of interfaces - front=foreground; back = background (includes adventure map, window interfaces, all kind of active dialogs, and so on)
  348. //active GUI elements (listening for events
  349. std::list<CIntObject*> lclickable;
  350. std::list<CIntObject*> rclickable;
  351. std::list<CIntObject*> hoverable;
  352. std::list<CIntObject*> keyinterested;
  353. std::list<CIntObject*> motioninterested;
  354. std::list<CIntObject*> timeinterested;
  355. //objs to blit
  356. std::vector<IShowable*> objsToBlit;
  357. SDL_Event * current; //current event
  358. void totalRedraw(); //forces total redraw (using showAll)
  359. void simpleRedraw(); //update only top interface and draw background from buffer
  360. void popInt(IShowActivable *top); //removes given interface from the top and activates next
  361. void popIntTotally(IShowActivable *top); //deactivates, deletes, removes given interface from the top and activates next
  362. void pushInt(IShowActivable *newInt); //deactivate old top interface, activates this one and pushes to the top
  363. void popInts(int howMany); //pops one or more interfaces - deactivates top, deletes and removes given number of interfaces, activates new front
  364. IShowActivable *topInt(); //returns top interface
  365. void updateTime(); //handles timeInterested
  366. void handleEvents(); //takes events from queue and calls interested objects
  367. void handleEvent(SDL_Event *sEvent);
  368. void handleMouseMotion(SDL_Event *sEvent);
  369. ui8 defActionsDef; //default auto actions
  370. ui8 captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  371. std::list<CIntObject *> createdObj; //stack of objs being created
  372. };
  373. extern CGuiHandler GH; //global gui handler
  374. struct ObjectConstruction
  375. {
  376. CIntObject *myObj;
  377. ObjectConstruction(CIntObject *obj);
  378. ~ObjectConstruction();
  379. };
  380. struct BlockCapture
  381. {
  382. bool previous;
  383. BlockCapture();
  384. ~BlockCapture();
  385. };
  386. #define OBJ_CONSTRUCTION ObjectConstruction obj__i(this)
  387. #define BLOCK_CAPTURING BlockCapture obj__i
  388. #endif //__GUIBASE_H__