CIntObject.h 7.8 KB

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