CIntObject.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "MouseButton.h"
  12. #include "../render/Graphics.h"
  13. #include "../../lib/Rect.h"
  14. struct SDL_Surface;
  15. class CGuiHandler;
  16. class CPicture;
  17. class InterfaceEventDispatcher;
  18. enum class EShortcut;
  19. using boost::logic::tribool;
  20. // Defines a activate/deactive method
  21. class IActivatable
  22. {
  23. public:
  24. virtual void activate()=0;
  25. virtual void deactivate()=0;
  26. virtual ~IActivatable() = default;
  27. };
  28. class IUpdateable
  29. {
  30. public:
  31. virtual void update()=0;
  32. virtual ~IUpdateable() = default;
  33. };
  34. // Defines a show method
  35. class IShowable
  36. {
  37. public:
  38. virtual void redraw()=0;
  39. virtual void show(SDL_Surface * to) = 0;
  40. virtual void showAll(SDL_Surface * to)
  41. {
  42. show(to);
  43. }
  44. virtual ~IShowable() = default;
  45. };
  46. class IShowActivatable : public IShowable, public IActivatable
  47. {
  48. public:
  49. virtual void onScreenResize() = 0;
  50. virtual ~IShowActivatable() = default;
  51. };
  52. class IEventsReceiver
  53. {
  54. public:
  55. virtual void keyPressed(EShortcut key) = 0;
  56. virtual void keyReleased(EShortcut key) = 0;
  57. virtual bool captureThisKey(EShortcut key) = 0;
  58. virtual void click(MouseButton btn, tribool down, bool previousState) = 0;
  59. virtual void clickLeft(tribool down, bool previousState) = 0;
  60. virtual void clickRight(tribool down, bool previousState) = 0;
  61. virtual void clickMiddle(tribool down, bool previousState) = 0;
  62. virtual void textInputed(const std::string & enteredText) = 0;
  63. virtual void textEdited(const std::string & enteredText) = 0;
  64. virtual void tick(uint32_t msPassed) = 0;
  65. virtual void wheelScrolled(bool down, bool in) = 0;
  66. virtual void mouseMoved(const Point & cursorPosition) = 0;
  67. virtual void hover(bool on) = 0;
  68. virtual void onDoubleClick() = 0;
  69. virtual ~IEventsReceiver() = default;
  70. };
  71. class AEventsReceiver : public IEventsReceiver
  72. {
  73. friend class InterfaceEventDispatcher;
  74. ui16 activeState;
  75. std::map<MouseButton, bool> currentMouseState;
  76. bool hoveredState; //for determining if object is hovered
  77. bool strongInterestState; //if true - report all mouse movements, if not - only when hovered
  78. protected:
  79. void setMoveEventStrongInterest(bool on);
  80. void activateEvents(ui16 what);
  81. void deactivateEvents(ui16 what);
  82. public:
  83. AEventsReceiver();
  84. // These are the arguments that can be used to determine what kind of input the CIntObject will receive
  85. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, TEXTINPUT=512, MCLICK=1024, ALL=0xffff};
  86. virtual bool isInside(const Point & position) = 0;
  87. bool isHovered() const;
  88. bool isActive() const;
  89. bool isActive(int flags) const;
  90. bool isMouseButtonPressed(MouseButton btn) const;
  91. };
  92. // Base UI element
  93. class CIntObject : public IShowActivatable, public AEventsReceiver //interface object
  94. {
  95. ui16 used;//change via addUsed() or delUsed
  96. //non-const versions of fields to allow changing them in CIntObject
  97. CIntObject *parent_m; //parent object
  98. public:
  99. //redraw parent flag - this int may be semi-transparent and require redraw of parent window
  100. enum {REDRAW_PARENT=8};
  101. int type; //bin flags using etype
  102. /*
  103. * Functions and fields that supposed to be private but are not for now.
  104. * Don't use them unless you really know what they are for
  105. */
  106. std::vector<CIntObject *> children;
  107. /*
  108. * Public interface
  109. */
  110. /// read-only parent access. May not be a "clean" solution but allows some compatibility
  111. CIntObject * const & parent;
  112. /// position of object on the screen. Please do not modify this anywhere but in constructor - use moveBy\moveTo instead
  113. /*const*/ Rect pos;
  114. CIntObject(int used=0, Point offset=Point());
  115. virtual ~CIntObject();
  116. void click(MouseButton btn, tribool down, bool previousState) final;
  117. void clickLeft(tribool down, bool previousState) override {}
  118. void clickRight(tribool down, bool previousState) override {}
  119. void clickMiddle(tribool down, bool previousState) override {}
  120. //hover handling
  121. void hover (bool on) override{}
  122. //keyboard handling
  123. bool captureAllKeys; //if true, only this object should get info about pressed keys
  124. void keyPressed(EShortcut key) override {}
  125. void keyReleased(EShortcut key) override {}
  126. bool captureThisKey(EShortcut key) override; //allows refining captureAllKeys against specific events (eg. don't capture ENTER)
  127. void textInputed(const std::string & enteredText) override{};
  128. void textEdited(const std::string & enteredText) override{};
  129. //mouse movement handling
  130. void mouseMoved (const Point & cursorPosition) override{}
  131. //time handling
  132. void tick(uint32_t msPassed) override {}
  133. //mouse wheel
  134. void wheelScrolled(bool down, bool in) override {}
  135. //double click
  136. void onDoubleClick() override {}
  137. void addUsedEvents(ui16 newActions);
  138. void removeUsedEvents(ui16 newActions);
  139. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  140. ui8 defActions; //which calls will be tried to be redirected to children
  141. ui8 recActions; //which calls we allow to receive from parent
  142. /// deactivates if needed, blocks all automatic activity, allows only disposal
  143. void disable();
  144. /// activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  145. void enable();
  146. /// deactivates or activates UI element based on flag
  147. void setEnabled(bool on);
  148. // activate or deactivate object. Inactive object won't receive any input events (keyboard\mouse)
  149. // usually used automatically by parent
  150. void activate() override;
  151. void deactivate() override;
  152. //called each frame to update screen
  153. void show(SDL_Surface * to) override;
  154. //called on complete redraw only
  155. void showAll(SDL_Surface * to) override;
  156. //request complete redraw of this object
  157. void redraw() override;
  158. /// called only for windows whenever screen size changes
  159. /// default behavior is to re-center, can be overriden
  160. void onScreenResize() override;
  161. bool isInside(const Point & position) override;
  162. 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
  163. const Rect & center(const Point &p, bool propagate = true); //moves object so that point p will be in its center
  164. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  165. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  166. void moveBy(const Point &p, bool propagate = true);
  167. void moveTo(const Point &p, bool propagate = true);//move this to new position, coordinates are absolute (0,0 is topleft screen corner)
  168. void addChild(CIntObject *child, bool adjustPosition = false);
  169. void removeChild(CIntObject *child, bool adjustPosition = false);
  170. //delChild - not needed, use normal "delete child" instead
  171. //delChildNull - not needed, use "vstd::clear_pointer(child)" instead
  172. /*
  173. * Functions that should be used only by specific GUI elements. Don't use them unless you really know why they are here
  174. */
  175. //functions for printing text. Use CLabel where possible instead
  176. void printAtLoc(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. friend class CGuiHandler;
  181. };
  182. /// Class for binding keys to left mouse button clicks
  183. /// Classes wanting use it should have it as one of their base classes
  184. class CKeyShortcut : public virtual CIntObject
  185. {
  186. public:
  187. EShortcut assignedKey;
  188. CKeyShortcut();
  189. CKeyShortcut(EShortcut key);
  190. void keyPressed(EShortcut key) override;
  191. void keyReleased(EShortcut key) override;
  192. };
  193. class WindowBase : public CIntObject
  194. {
  195. public:
  196. WindowBase(int used_ = 0, Point pos_ = Point());
  197. protected:
  198. void close();
  199. };
  200. class IStatusBar
  201. {
  202. public:
  203. virtual ~IStatusBar();
  204. /// set current text for the status bar
  205. virtual void write(const std::string & text) = 0;
  206. /// remove any current text from the status bar
  207. virtual void clear() = 0;
  208. /// remove text from status bar if current text matches tested text
  209. virtual void clearIfMatching(const std::string & testedText) = 0;
  210. /// enables mode for entering text instead of showing hover text
  211. virtual void setEnteringMode(bool on) = 0;
  212. /// overrides hover text from controls with text entered into in-game console (for chat/cheats)
  213. virtual void setEnteredText(const std::string & text) = 0;
  214. };
  215. class EmptyStatusBar : public IStatusBar
  216. {
  217. virtual void write(const std::string & text){};
  218. virtual void clear(){};
  219. virtual void clearIfMatching(const std::string & testedText){};
  220. virtual void setEnteringMode(bool on){};
  221. virtual void setEnteredText(const std::string & text){};
  222. };