2
0

CIntObject.h 8.0 KB

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