GUIBase.h 13 KB

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