GUIBase.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. #ifdef max
  9. #undef max
  10. #endif
  11. #ifdef min
  12. #undef min
  13. #endif
  14. /*
  15. * GUIBase.h, part of VCMI engine
  16. *
  17. * Authors: listed in file AUTHORS in main folder
  18. *
  19. * License: GNU General Public License v2.0 or later
  20. * Full text of license available in license.txt file, in main folder
  21. *
  22. */
  23. class CDefEssential;
  24. class AdventureMapButton;
  25. class CHighlightableButtonsGroup;
  26. class CDefHandler;
  27. struct HeroMoveDetails;
  28. class CDefEssential;
  29. class CGHeroInstance;
  30. class CAdvMapInt;
  31. class CCastleInterface;
  32. class CBattleInterface;
  33. class CStack;
  34. class SComponent;
  35. class CCreature;
  36. struct SDL_Surface;
  37. struct CPath;
  38. class CCreatureAnimation;
  39. class CSelectableComponent;
  40. class CCreatureSet;
  41. class CGObjectInstance;
  42. class CSlider;
  43. struct UpgradeInfo;
  44. template <typename T> struct CondSh;
  45. class CInGameConsole;
  46. class CGarrisonInt;
  47. class CInGameConsole;
  48. class Component;
  49. class CArmedInstance;
  50. class CGTownInstance;
  51. class StackState;
  52. class CPlayerInterface;
  53. struct Point
  54. {
  55. int x, y;
  56. //constructors
  57. Point(){};
  58. Point(int X, int Y)
  59. :x(X),y(Y)
  60. {};
  61. Point(const int3 &a)
  62. :x(a.x),y(a.y)
  63. {}
  64. Point operator+(const Point &b) const
  65. {
  66. return Point(x+b.x,y+b.y);
  67. }
  68. Point& operator+=(const Point &b)
  69. {
  70. x += b.x;
  71. y += b.y;
  72. return *this;
  73. }
  74. Point operator-(const Point &b) const
  75. {
  76. return Point(x+b.x,y+b.y);
  77. }
  78. Point& operator-=(const Point &b)
  79. {
  80. x -= b.x;
  81. y -= b.y;
  82. return *this;
  83. }
  84. bool operator<(const Point &b) const //product order
  85. {
  86. return x < b.x && y < b.y;
  87. }
  88. };
  89. struct Rect : public SDL_Rect
  90. {
  91. Rect()//default c-tor
  92. {
  93. x = y = w = h = -1;
  94. }
  95. Rect(int X, int Y, int W, int H) //c-tor
  96. {
  97. x = X;
  98. y = Y;
  99. w = W;
  100. h = H;
  101. }
  102. Rect(const SDL_Rect & r) //c-tor
  103. {
  104. x = r.x;
  105. y = r.y;
  106. w = r.w;
  107. h = r.h;
  108. }
  109. bool isIn(int qx, int qy) const //determines if given point lies inside rect
  110. {
  111. if (qx > x && qx<x+w && qy>y && qy<y+h)
  112. return true;
  113. return false;
  114. }
  115. bool isIn(const Point &q) const //determines if given point lies inside rect
  116. {
  117. return isIn(q.x,q.y);
  118. }
  119. Point topLeft() const //top left corner of this rect
  120. {
  121. return Point(x,y);
  122. }
  123. Point topRight() const //top right corner of this rect
  124. {
  125. return Point(x+w,y);
  126. }
  127. Point bottomLeft() const //bottom left corner of this rect
  128. {
  129. return Point(x,y+h);
  130. }
  131. Point bottomRight() const //bottom right corner of this rect
  132. {
  133. return Point(x+w,y+h);
  134. }
  135. Rect operator+(const Rect &p) const //moves this rect by p's rect position
  136. {
  137. return Rect(x+p.x,y+p.y,w,h);
  138. }
  139. Rect operator+(const Point &p) const //moves this rect by p's point position
  140. {
  141. return Rect(x+p.x,y+p.y,w,h);
  142. }
  143. Rect& operator=(const Rect &p) //assignment operator
  144. {
  145. x = p.x;
  146. y = p.y;
  147. w = p.w;
  148. h = p.h;
  149. return *this;
  150. }
  151. Rect& operator+=(const Rect &p) //works as operator+
  152. {
  153. x += p.x;
  154. y += p.y;
  155. return *this;
  156. }
  157. Rect& operator+=(const Point &p) //works as operator+
  158. {
  159. x += p.x;
  160. y += p.y;
  161. return *this;
  162. }
  163. Rect& operator-=(const Rect &p) //works as operator+
  164. {
  165. x -= p.x;
  166. y -= p.y;
  167. return *this;
  168. }
  169. Rect& operator-=(const Point &p) //works as operator+
  170. {
  171. x -= p.x;
  172. y -= p.y;
  173. return *this;
  174. }
  175. Rect operator&(const Rect &p) const //rect intersection
  176. {
  177. bool intersect = true;
  178. if(p.topLeft().y < y && p.bottomLeft().y < y) //rect p is above *this
  179. {
  180. intersect = false;
  181. }
  182. else if(p.topLeft().y > y+h && p.bottomLeft().y > y+h) //rect p is below *this
  183. {
  184. intersect = false;
  185. }
  186. else if(p.topLeft().x > x+w && p.topRight().x > x+w) //rect p is on the right hand side of this
  187. {
  188. intersect = false;
  189. }
  190. else if(p.topLeft().x < x && p.topRight().x < x) //rect p is on the left hand side of this
  191. {
  192. intersect = false;
  193. }
  194. if(intersect)
  195. {
  196. Rect ret;
  197. ret.x = std::max(this->x, p.x);
  198. ret.y = std::max(this->y, p.y);
  199. Point bR; //bottomRight point of returned rect
  200. bR.x = std::min(this->w+this->x, p.w+p.x);
  201. bR.y = std::min(this->h+this->y, p.h+p.y);
  202. ret.w = bR.x - ret.x;
  203. ret.h = bR.y - ret.y;
  204. return ret;
  205. }
  206. else
  207. {
  208. return Rect();
  209. }
  210. }
  211. };
  212. class IShowable
  213. {
  214. public:
  215. virtual void show(SDL_Surface * to)=0;
  216. virtual void showAll(SDL_Surface * to)
  217. {
  218. show(to);
  219. }
  220. virtual ~IShowable(){}; //d-tor
  221. };
  222. class IStatusBar
  223. {
  224. public:
  225. virtual ~IStatusBar(){}; //d-tor
  226. virtual void print(const std::string & text)=0; //prints text and refreshes statusbar
  227. virtual void clear()=0;//clears statusbar and refreshes
  228. virtual void show(SDL_Surface * to)=0; //shows statusbar (with current text)
  229. virtual std::string getCurrent()=0; //returns currently displayed text
  230. };
  231. class IActivable
  232. {
  233. public:
  234. virtual void activate()=0;
  235. virtual void deactivate()=0;
  236. virtual ~IActivable(){}; //d-tor
  237. };
  238. class IShowActivable : public IShowable, public IActivable
  239. {
  240. public:
  241. enum {WITH_GARRISON = 1};
  242. int type; //bin flags using etype
  243. IShowActivable();
  244. virtual ~IShowActivable(){}; //d-tor
  245. };
  246. class CIntObject : public IShowActivable //interface object
  247. {
  248. public:
  249. CIntObject *parent; //parent object
  250. std::vector<CIntObject *> children;
  251. Rect pos, //position of object on the screen
  252. posRelative; //position of object in the parent (not used if no parent)
  253. int ID; //object ID, rarely used by some classes for identification / internal info
  254. CIntObject();
  255. virtual ~CIntObject(){}; //d-tor
  256. //l-clicks handling
  257. bool pressedL; //for determining if object is L-pressed
  258. void activateLClick();
  259. void deactivateLClick();
  260. virtual void clickLeft(tribool down, bool previousState);
  261. //r-clicks handling
  262. bool pressedR; //for determining if object is R-pressed
  263. void activateRClick();
  264. void deactivateRClick();
  265. virtual void clickRight(tribool down, bool previousState);
  266. //hover handling
  267. bool hovered; //for determining if object is hovered
  268. void activateHover();
  269. void deactivateHover();
  270. virtual void hover (bool on);
  271. //keyboard handling
  272. bool captureAllKeys; //if true, only this object should get info about pressed keys
  273. void activateKeys();
  274. void deactivateKeys();
  275. virtual void keyPressed(const SDL_KeyboardEvent & key);
  276. //mouse movement handling
  277. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  278. void activateMouseMove();
  279. void deactivateMouseMove();
  280. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  281. //time handling
  282. int toNextTick;
  283. void activateTimer();
  284. void deactivateTimer();
  285. virtual void tick();
  286. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32};
  287. ui8 active;
  288. ui8 defActivation;
  289. void defActivate();
  290. void defDeactivate();
  291. void activate();
  292. void deactivate();
  293. void show(SDL_Surface * to);
  294. };
  295. //class for binding keys to left mouse button clicks
  296. //classes wanting use it should have it as one of their base classes
  297. class KeyShortcut : public virtual CIntObject
  298. {
  299. public:
  300. std::set<int> assignedKeys;
  301. KeyShortcut(){}; //c-tor
  302. KeyShortcut(int key){assignedKeys.insert(key);}; //c-tor
  303. KeyShortcut(std::set<int> Keys):assignedKeys(Keys){}; //c-tor
  304. virtual void keyPressed(const SDL_KeyboardEvent & key); //call-in
  305. };
  306. class CWindowWithGarrison : public CIntObject
  307. {
  308. public:
  309. CGarrisonInt *garr;
  310. CWindowWithGarrison();
  311. };
  312. class CSimpleWindow : public CIntObject
  313. {
  314. public:
  315. SDL_Surface * bitmap; //background
  316. CIntObject * owner; //who made this window
  317. virtual void show(SDL_Surface * to);
  318. CSimpleWindow():bitmap(NULL),owner(NULL){}; //c-tor
  319. virtual ~CSimpleWindow(); //d-tor
  320. void activate(){};
  321. void deactivate(){};
  322. };
  323. class CButtonBase : public KeyShortcut//basic buttton class
  324. {
  325. public:
  326. int bitmapOffset; //TODO: comment me
  327. int type; //advmapbutton=2 //TODO: comment me
  328. bool abs;//TODO: comment me
  329. bool active; //if true, this button is active and can be pressed
  330. bool notFreeButton; //TODO: comment me
  331. CIntObject * ourObj; // "owner"
  332. int state; //TODO: comment me
  333. std::vector< std::vector<SDL_Surface*> > imgs; //images for this button
  334. int curimg; //curently displayed image from imgs
  335. virtual void show(SDL_Surface * to);
  336. virtual void activate()=0;
  337. virtual void deactivate()=0;
  338. CButtonBase(); //c-tor
  339. virtual ~CButtonBase(); //d-tor
  340. };
  341. class CGuiHandler
  342. {
  343. public:
  344. timeHandler th;
  345. std::list<IShowActivable *> listInt; //list of interfaces - front=foreground; back = background (includes adventure map, window interfaces, all kind of active dialogs, and so on)
  346. //active GUI elements (listening for events
  347. std::list<CIntObject*> lclickable;
  348. std::list<CIntObject*> rclickable;
  349. std::list<CIntObject*> hoverable;
  350. std::list<CIntObject*> keyinterested;
  351. std::list<CIntObject*> motioninterested;
  352. std::list<CIntObject*> timeinterested;
  353. //objs to blit
  354. std::vector<IShowable*> objsToBlit;
  355. SDL_Event * current; //current event
  356. void totalRedraw(); //forces total redraw (using showAll)
  357. void simpleRedraw(); //update only top interface and draw background from buffer
  358. void popInt(IShowActivable *top); //removes given interface from the top and activates next
  359. void popIntTotally(IShowActivable *top); //deactivates, deletes, removes given interface from the top and activates next
  360. void pushInt(IShowActivable *newInt); //deactivate old top interface, activates this one and pushes to the top
  361. void popInts(int howMany); //pops one or more interfaces - deactivates top, deletes and removes given number of interfaces, activates new front
  362. IShowActivable *topInt(); //returns top interface
  363. void updateTime(); //handles timeInterested
  364. void handleEvents(); //takes events from queue and calls interested objects
  365. void handleEvent(SDL_Event *sEvent);
  366. void handleMouseMotion(SDL_Event *sEvent);
  367. };
  368. extern CGuiHandler GH; //global gui handler
  369. #endif //__GUIBASE_H__