GUIBase.h 16 KB

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