GUIBase.h 13 KB

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