GUIBase.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 IUpdateable
  274. {
  275. public:
  276. virtual void update()=0;
  277. virtual ~IUpdateable(){}; //d-tor
  278. };
  279. class CIntObject : public IShowActivable //interface object
  280. {
  281. public:
  282. CIntObject *parent; //parent object
  283. std::vector<CIntObject *> children;
  284. Rect pos, //position of object on the screen
  285. posRelative; //position of object in the parent (not used if no parent)
  286. int ID; //object ID, rarely used by some classes for identification / internal info
  287. CIntObject();
  288. virtual ~CIntObject(); //d-tor
  289. //l-clicks handling
  290. bool pressedL; //for determining if object is L-pressed
  291. void activateLClick();
  292. void deactivateLClick();
  293. virtual void clickLeft(tribool down, bool previousState);
  294. //r-clicks handling
  295. bool pressedR; //for determining if object is R-pressed
  296. void activateRClick();
  297. void deactivateRClick();
  298. virtual void clickRight(tribool down, bool previousState);
  299. //hover handling
  300. bool hovered; //for determining if object is hovered
  301. void activateHover();
  302. void deactivateHover();
  303. virtual void hover (bool on);
  304. //keyboard handling
  305. bool captureAllKeys; //if true, only this object should get info about pressed keys
  306. void activateKeys();
  307. void deactivateKeys();
  308. virtual void keyPressed(const SDL_KeyboardEvent & key);
  309. //mouse movement handling
  310. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  311. void activateMouseMove();
  312. void deactivateMouseMove();
  313. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  314. //time handling
  315. int toNextTick;
  316. void activateTimer();
  317. void deactivateTimer();
  318. virtual void tick();
  319. //mouse wheel
  320. void activateWheel();
  321. void deactivateWheel();
  322. virtual void wheelScrolled(bool down, bool in);
  323. //double click
  324. void activateDClick();
  325. void deactivateDClick();
  326. virtual void onDoubleClick();
  327. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, ALL=0xffff};
  328. ui16 active;
  329. ui16 used;
  330. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  331. ui8 defActions; //which calls will be tried to be redirected to children
  332. ui8 recActions; //which calls we allow te receive from parent
  333. void disable(); //deactivates if needed, blocks all automatic activity, allows only disposal
  334. void enable(bool activation = true); //activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  335. void defActivate();
  336. void defDeactivate();
  337. void activate();
  338. void deactivate();
  339. void show(SDL_Surface * to);
  340. void showAll(SDL_Surface * to);
  341. void printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  342. void printToLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  343. void printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh = false);
  344. void printAtMiddleWBLoc(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst, bool refrsh = false);
  345. void blitAtLoc(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  346. bool isItInLoc(const SDL_Rect &rect, int x, int y);
  347. bool isItInLoc(const SDL_Rect &rect, const Point &p);
  348. const Rect & center(const Rect &r); //sets pos so that r will be in the center of screen, returns new position
  349. const Rect & center(); //centers when pos.w and pos.h are set, returns new position
  350. void moveBy(const Point &p, bool propagate = true);
  351. void moveTo(const Point &p, bool propagate = true);
  352. };
  353. //class for binding keys to left mouse button clicks
  354. //classes wanting use it should have it as one of their base classes
  355. class KeyShortcut : public virtual CIntObject
  356. {
  357. public:
  358. std::set<int> assignedKeys;
  359. KeyShortcut(){}; //c-tor
  360. KeyShortcut(int key){assignedKeys.insert(key);}; //c-tor
  361. KeyShortcut(std::set<int> Keys):assignedKeys(Keys){}; //c-tor
  362. virtual void keyPressed(const SDL_KeyboardEvent & key); //call-in
  363. };
  364. class CWindowWithGarrison : public CIntObject
  365. {
  366. public:
  367. CGarrisonInt *garr;
  368. CWindowWithGarrison();
  369. };
  370. class CSimpleWindow : public CIntObject
  371. {
  372. public:
  373. SDL_Surface * bitmap; //background
  374. CIntObject * owner; //who made this window
  375. virtual void show(SDL_Surface * to);
  376. CSimpleWindow():bitmap(NULL),owner(NULL){}; //c-tor
  377. virtual ~CSimpleWindow(); //d-tor
  378. void activate(){};
  379. void deactivate(){};
  380. };
  381. class CPicture : public CIntObject
  382. {
  383. public:
  384. SDL_Surface *bg;
  385. bool freeSurf;
  386. operator SDL_Surface*()
  387. {
  388. return bg;
  389. }
  390. CPicture(SDL_Surface *BG, int x, int y, bool Free = true);
  391. CPicture(const std::string &bmpname, int x, int y);
  392. ~CPicture();
  393. void showAll(SDL_Surface * to);
  394. };
  395. class CGuiHandler
  396. {
  397. public:
  398. timeHandler th;
  399. std::list<IShowActivable *> listInt; //list of interfaces - front=foreground; back = background (includes adventure map, window interfaces, all kind of active dialogs, and so on)
  400. //active GUI elements (listening for events
  401. std::list<CIntObject*> lclickable;
  402. std::list<CIntObject*> rclickable;
  403. std::list<CIntObject*> hoverable;
  404. std::list<CIntObject*> keyinterested;
  405. std::list<CIntObject*> motioninterested;
  406. std::list<CIntObject*> timeinterested;
  407. std::list<CIntObject*> wheelInterested;
  408. std::list<CIntObject*> doubleClickInterested;
  409. //objs to blit
  410. std::vector<IShowable*> objsToBlit;
  411. SDL_Event * current; //current event
  412. IUpdateable *curInt;
  413. Point lastClick;
  414. unsigned lastClickTime;
  415. bool terminate;
  416. CGuiHandler();
  417. ~CGuiHandler();
  418. void run();
  419. void totalRedraw(); //forces total redraw (using showAll)
  420. void simpleRedraw(); //update only top interface and draw background from buffer
  421. void popInt(IShowActivable *top); //removes given interface from the top and activates next
  422. void popIntTotally(IShowActivable *top); //deactivates, deletes, removes given interface from the top and activates next
  423. void pushInt(IShowActivable *newInt); //deactivate old top interface, activates this one and pushes to the top
  424. void popInts(int howMany); //pops one or more interfaces - deactivates top, deletes and removes given number of interfaces, activates new front
  425. IShowActivable *topInt(); //returns top interface
  426. void updateTime(); //handles timeInterested
  427. void handleEvents(); //takes events from queue and calls interested objects
  428. void handleEvent(SDL_Event *sEvent);
  429. void handleMouseMotion(SDL_Event *sEvent);
  430. void handleMoveInterested( const SDL_MouseMotionEvent & motion );
  431. void fakeMouseMove();
  432. ui8 defActionsDef; //default auto actions
  433. ui8 captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  434. std::list<CIntObject *> createdObj; //stack of objs being created
  435. };
  436. SDLKey arrowToNum(SDLKey key); //converts arrow key to according numpad key
  437. SDLKey numToDigit(SDLKey key);//converts numpad digit key to normal digit key
  438. bool isNumKey(SDLKey key, bool number = true); //checks if key is on numpad (numbers - check only for numpad digits)
  439. bool isArrowKey(SDLKey key);
  440. extern CGuiHandler GH; //global gui handler
  441. struct ObjectConstruction
  442. {
  443. CIntObject *myObj;
  444. ObjectConstruction(CIntObject *obj);
  445. ~ObjectConstruction();
  446. };
  447. struct SetCaptureState
  448. {
  449. bool previousCapture;
  450. ui8 prevActions;
  451. SetCaptureState(bool allow, ui8 actions);
  452. ~SetCaptureState();
  453. };
  454. #define OBJ_CONSTRUCTION ObjectConstruction obj__i(this)
  455. #define OBJ_CONSTRUCTION_CAPTURING_ALL defActions = 255; SetCaptureState obj__i1(true, 255); ObjectConstruction obj__i(this)
  456. #define BLOCK_CAPTURING SetCaptureState obj__i(false, 0)
  457. #endif //__GUIBASE_H__