CIntObject.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #pragma once
  2. #include <SDL_events.h>
  3. #include "Geometries.h"
  4. #include "../FontBase.h"
  5. struct SDL_Surface;
  6. /*
  7. * CIntObject.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. using boost::logic::tribool;
  16. // Defines a activate/deactive method
  17. class IActivatable
  18. {
  19. public:
  20. virtual void activate()=0;
  21. virtual void deactivate()=0;
  22. virtual ~IActivatable(){}; //d-tor
  23. };
  24. class IUpdateable
  25. {
  26. public:
  27. virtual void update()=0;
  28. virtual ~IUpdateable(){}; //d-tor
  29. };
  30. // Defines a show method
  31. class IShowable
  32. {
  33. public:
  34. virtual void redraw()=0;
  35. virtual void show(SDL_Surface * to) = 0;
  36. virtual void showAll(SDL_Surface * to)
  37. {
  38. show(to);
  39. }
  40. virtual ~IShowable(){}; //d-tor
  41. };
  42. class IShowActivatable : public IShowable, public IActivatable
  43. {
  44. public:
  45. //redraw parent flag - this int may be semi-transparent and require redraw of parent window
  46. enum {WITH_GARRISON = 1, BLOCK_ADV_HOTKEYS = 2, WITH_ARTIFACTS = 4, REDRAW_PARENT=8};
  47. int type; //bin flags using etype
  48. IShowActivatable();
  49. virtual ~IShowActivatable(){}; //d-tor
  50. };
  51. // Status bar interface
  52. class IStatusBar
  53. {
  54. public:
  55. virtual ~IStatusBar(){}; //d-tor
  56. virtual void print(const std::string & text)=0; //prints text and refreshes statusbar
  57. virtual void clear()=0;//clears statusbar and refreshes
  58. virtual void show(SDL_Surface * to)=0; //shows statusbar (with current text)
  59. virtual std::string getCurrent()=0; //returns currently displayed text
  60. };
  61. // Base UI element
  62. class CIntObject : public IShowActivatable //interface object
  63. {
  64. public:
  65. CIntObject *parent; //parent object
  66. std::vector<CIntObject *> children;
  67. Rect pos, //position of object on the screen
  68. posRelative; //position of object in the parent (not used if no parent)
  69. CIntObject();
  70. virtual ~CIntObject(); //d-tor
  71. //l-clicks handling
  72. bool pressedL; //for determining if object is L-pressed
  73. void activateLClick();
  74. void deactivateLClick();
  75. virtual void clickLeft(tribool down, bool previousState);
  76. //r-clicks handling
  77. bool pressedR; //for determining if object is R-pressed
  78. void activateRClick();
  79. void deactivateRClick();
  80. virtual void clickRight(tribool down, bool previousState);
  81. //hover handling
  82. bool hovered; //for determining if object is hovered
  83. void activateHover();
  84. void deactivateHover();
  85. virtual void hover (bool on);
  86. //keyboard handling
  87. bool captureAllKeys; //if true, only this object should get info about pressed keys
  88. void activateKeys();
  89. void deactivateKeys();
  90. virtual void keyPressed(const SDL_KeyboardEvent & key);
  91. //mouse movement handling
  92. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  93. void activateMouseMove();
  94. void deactivateMouseMove();
  95. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  96. //time handling
  97. int toNextTick;
  98. void activateTimer();
  99. void deactivateTimer();
  100. virtual void tick();
  101. //mouse wheel
  102. void activateWheel();
  103. void deactivateWheel();
  104. virtual void wheelScrolled(bool down, bool in);
  105. //double click
  106. void activateDClick();
  107. void deactivateDClick();
  108. virtual void onDoubleClick();
  109. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, ALL=0xffff};
  110. ui16 active;
  111. ui16 used;
  112. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  113. ui8 defActions; //which calls will be tried to be redirected to children
  114. ui8 recActions; //which calls we allow te receive from parent
  115. enum EAlignment {TOPLEFT, CENTER, BOTTOMRIGHT};
  116. void disable(); //deactivates if needed, blocks all automatic activity, allows only disposal
  117. void enable(bool activation = true); //activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  118. // activate or deactivate object. Inactive object won't receive any input events (keyboard\mouse)
  119. // usually used automatically by parent
  120. void activate();
  121. void deactivate();
  122. //activate or deactivate specific action (LCLICK, RCLICK...)
  123. void activate(ui16 what);
  124. void deactivate(ui16 what);
  125. //called each frame to update screen
  126. void show(SDL_Surface * to);
  127. //called on complete redraw only
  128. void showAll(SDL_Surface * to);
  129. //request complete redraw
  130. void redraw();
  131. void drawBorderLoc(SDL_Surface * sur, const Rect &r, const int3 &color);
  132. //functions for printing text. Use CLabel where possible instead
  133. void printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  134. void printToLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  135. void printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  136. void printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color color, SDL_Surface * dst);
  137. void printAtMiddleWBLoc(const std::string & text, int x, int y, EFonts font, int charsPerLine, SDL_Color color, SDL_Surface * dst);
  138. //image blitting. If possible use CPicture or CAnimImage instead
  139. void blitAtLoc(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  140. void blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst);
  141. bool isItInLoc(const SDL_Rect &rect, int x, int y);
  142. bool isItInLoc(const SDL_Rect &rect, const Point &p);
  143. const Rect & center(const Rect &r, bool propagate = true); //sets pos so that r will be in the center of screen, assigns sizes of r to pos, returns new position
  144. const Rect & center(const Point &p, bool propagate = true); //moves object so that point p will be in its center
  145. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  146. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  147. void moveBy(const Point &p, bool propagate = true);
  148. void moveTo(const Point &p, bool propagate = true);//move this to new position, coordinates are absolute (0,0 is topleft screen corner)
  149. void changeUsedEvents(ui16 what, bool enable, bool adjust = true);
  150. //add child without parent to this. Use CGuiHandler::moveChild() if child already have parent
  151. void addChild(CIntObject *child, bool adjustPosition = false);
  152. //remove child from this without deleting
  153. void removeChild(CIntObject *child, bool adjustPosition = false);
  154. void delChild(CIntObject *child); //removes from children list, deletes
  155. template <typename T> void delChildNUll(T *&child, bool deactivateIfNeeded = false) //removes from children list, deletes and sets pointer to NULL
  156. {
  157. if(!child)
  158. return;
  159. if(deactivateIfNeeded && child->active)
  160. child->deactivate();
  161. delChild(child);
  162. child = NULL;
  163. }
  164. };
  165. /// Class for binding keys to left mouse button clicks
  166. /// Classes wanting use it should have it as one of their base classes
  167. class CKeyShortcut : public virtual CIntObject
  168. {
  169. public:
  170. std::set<int> assignedKeys;
  171. CKeyShortcut(){}; //c-tor
  172. CKeyShortcut(int key){assignedKeys.insert(key);}; //c-tor
  173. CKeyShortcut(std::set<int> Keys):assignedKeys(Keys){}; //c-tor
  174. virtual void keyPressed(const SDL_KeyboardEvent & key); //call-in
  175. };