GUIBase.h 8.8 KB

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