GUIBase.h 15 KB

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