GUIBase.h 14 KB

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