GUIBase.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 Rect &p) const //rect intersection
  156. {
  157. bool intersect = true;
  158. if(p.topLeft().y < y && p.bottomLeft().y < y) //rect p is above *this
  159. {
  160. intersect = false;
  161. }
  162. else if(p.topLeft().y > y+h && p.bottomLeft().y > y+h) //rect p is below *this
  163. {
  164. intersect = false;
  165. }
  166. else if(p.topLeft().x > x+w && p.topRight().x > x+w) //rect p is on the right hand side of this
  167. {
  168. intersect = false;
  169. }
  170. else if(p.topLeft().x < x && p.topRight().x < x) //rect p is on the left hand side of this
  171. {
  172. intersect = false;
  173. }
  174. if(intersect)
  175. {
  176. Rect ret;
  177. ret.x = std::max(this->x, p.x);
  178. ret.y = std::max(this->y, p.y);
  179. Point bR; //bottomRight point of returned rect
  180. bR.x = std::min(this->w+this->x, p.w+p.x);
  181. bR.y = std::min(this->h+this->y, p.h+p.y);
  182. ret.w = bR.x - ret.x;
  183. ret.h = bR.y - ret.y;
  184. return ret;
  185. }
  186. else
  187. {
  188. return Rect();
  189. }
  190. }
  191. };
  192. class IShowable
  193. {
  194. public:
  195. virtual void show(SDL_Surface * to)=0;
  196. virtual void showAll(SDL_Surface * to)
  197. {
  198. show(to);
  199. }
  200. virtual ~IShowable(){}; //d-tor
  201. };
  202. class IStatusBar
  203. {
  204. public:
  205. virtual ~IStatusBar(){}; //d-tor
  206. virtual void print(const std::string & text)=0; //prints text and refreshes statusbar
  207. virtual void clear()=0;//clears statusbar and refreshes
  208. virtual void show(SDL_Surface * to)=0; //shows statusbar (with current text)
  209. virtual std::string getCurrent()=0; //returns currently displayed text
  210. };
  211. class IActivable
  212. {
  213. public:
  214. virtual void activate()=0;
  215. virtual void deactivate()=0;
  216. virtual ~IActivable(){}; //d-tor
  217. };
  218. class IShowActivable : public IShowable, public IActivable
  219. {
  220. public:
  221. enum {WITH_GARRISON = 1};
  222. int type; //bin flags using etype
  223. IShowActivable();
  224. virtual ~IShowActivable(){}; //d-tor
  225. };
  226. class CWindowWithGarrison : public IShowActivable
  227. {
  228. public:
  229. CGarrisonInt *garr;
  230. CWindowWithGarrison();
  231. };
  232. class CMainInterface : public IShowActivable
  233. {
  234. public:
  235. IShowActivable *subInt;
  236. };
  237. class CIntObject //interface object
  238. {
  239. public:
  240. Rect pos; //position of object on the screen
  241. int ID; //object unique ID, rarely (if at all) used
  242. //virtual bool isIn(int x, int y)
  243. //{
  244. // return pos.isIn(x,y);
  245. //}
  246. virtual ~CIntObject(){}; //d-tor
  247. };
  248. class CSimpleWindow : public IShowActivable, public virtual CIntObject
  249. {
  250. public:
  251. SDL_Surface * bitmap; //background
  252. CIntObject * owner; //who made this window
  253. virtual void show(SDL_Surface * to);
  254. CSimpleWindow():bitmap(NULL),owner(NULL){}; //c-tor
  255. virtual ~CSimpleWindow(); //d-tor
  256. void activate(){};
  257. void deactivate(){};
  258. };
  259. class CButtonBase : public virtual CIntObject, public IShowable, public IActivable //basic buttton class
  260. {
  261. public:
  262. int bitmapOffset; //TODO: comment me
  263. int type; //advmapbutton=2 //TODO: comment me
  264. bool abs;//TODO: comment me
  265. bool active; //if true, this button is active and can be pressed
  266. bool notFreeButton; //TODO: comment me
  267. CIntObject * ourObj; // "owner"
  268. int state; //TODO: comment me
  269. std::vector< std::vector<SDL_Surface*> > imgs; //images for this button
  270. int curimg; //curently displayed image from imgs
  271. virtual void show(SDL_Surface * to);
  272. virtual void activate()=0;
  273. virtual void deactivate()=0;
  274. CButtonBase(); //c-tor
  275. virtual ~CButtonBase(); //d-tor
  276. };
  277. class ClickableL : public virtual CIntObject //for left-clicks
  278. {
  279. public:
  280. bool pressedL; //for determining if object is L-pressed
  281. ClickableL(); //c-tor
  282. virtual ~ClickableL();//{};//d-tor
  283. virtual void clickLeft (boost::logic::tribool down)=0;
  284. virtual void activate();
  285. virtual void deactivate();
  286. };
  287. class ClickableR : public virtual CIntObject //for right-clicks
  288. {
  289. public:
  290. bool pressedR; //for determining if object is R-pressed
  291. ClickableR(); //c-tor
  292. virtual ~ClickableR();//{};//d-tor
  293. virtual void clickRight (boost::logic::tribool down)=0;
  294. virtual void activate()=0;
  295. virtual void deactivate()=0;
  296. };
  297. class Hoverable : public virtual CIntObject
  298. {
  299. public:
  300. Hoverable() : hovered(false){} //c-tor
  301. virtual ~Hoverable();//{}; //d-tor
  302. bool hovered; //for determining if object is hovered
  303. virtual void hover (bool on)=0;
  304. virtual void activate()=0;
  305. virtual void deactivate()=0;
  306. };
  307. class KeyInterested : public virtual CIntObject
  308. {
  309. public:
  310. bool captureAllKeys; //if true, only this object should get info about pressed keys
  311. KeyInterested(): captureAllKeys(false){}
  312. virtual ~KeyInterested();//{};//d-tor
  313. virtual void keyPressed(const SDL_KeyboardEvent & key)=0;
  314. virtual void activate()=0;
  315. virtual void deactivate()=0;
  316. };
  317. //class for binding keys to left mouse button clicks
  318. //classes wanting use it should have it as one of their base classes
  319. class KeyShortcut : public KeyInterested, public ClickableL
  320. {
  321. public:
  322. std::set<int> assignedKeys;
  323. KeyShortcut(){}; //c-tor
  324. KeyShortcut(int key){assignedKeys.insert(key);}; //c-tor
  325. KeyShortcut(std::set<int> Keys):assignedKeys(Keys){}; //c-tor
  326. virtual void keyPressed(const SDL_KeyboardEvent & key); //call-in
  327. };
  328. class MotionInterested: public virtual CIntObject
  329. {
  330. public:
  331. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  332. MotionInterested(){strongInterest=false;};
  333. virtual ~MotionInterested(){};//d-tor
  334. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent)=0;
  335. virtual void activate()=0;
  336. virtual void deactivate()=0;
  337. };
  338. class TimeInterested: public virtual CIntObject
  339. {
  340. public:
  341. virtual ~TimeInterested(){}; //d-tor
  342. int toNextTick;
  343. virtual void tick()=0;
  344. virtual void activate();
  345. virtual void deactivate();
  346. };
  347. #endif //__GUIBASE_H__