CIntObject.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. class IUpdateable
  21. {
  22. public:
  23. virtual void update()=0;
  24. virtual ~IUpdateable() = default;
  25. };
  26. class IShowActivatable
  27. {
  28. public:
  29. virtual void activate()=0;
  30. virtual void deactivate()=0;
  31. virtual void redraw()=0;
  32. virtual void show(SDL_Surface * to) = 0;
  33. virtual void showAll(SDL_Surface * to) = 0;
  34. virtual void onScreenResize() = 0;
  35. virtual ~IShowActivatable() = default;
  36. };
  37. class AEventsReceiver
  38. {
  39. friend class InterfaceEventDispatcher;
  40. ui16 activeState;
  41. std::map<MouseButton, bool> currentMouseState;
  42. bool hoveredState; //for determining if object is hovered
  43. bool strongInterestState; //if true - report all mouse movements, if not - only when hovered
  44. void click(MouseButton btn, tribool down, bool previousState);
  45. protected:
  46. void setMoveEventStrongInterest(bool on);
  47. void activateEvents(ui16 what);
  48. void deactivateEvents(ui16 what);
  49. virtual void clickLeft(tribool down, bool previousState) {}
  50. virtual void clickRight(tribool down, bool previousState) {}
  51. virtual void clickMiddle(tribool down, bool previousState) {}
  52. virtual void textInputed(const std::string & enteredText) {}
  53. virtual void textEdited(const std::string & enteredText) {}
  54. virtual void tick(uint32_t msPassed) {}
  55. virtual void wheelScrolled(bool down, bool in) {}
  56. virtual void mouseMoved(const Point & cursorPosition) {}
  57. virtual void hover(bool on) {}
  58. virtual void onDoubleClick() {}
  59. virtual void keyPressed(EShortcut key) {}
  60. virtual void keyReleased(EShortcut key) {}
  61. virtual bool captureThisKey(EShortcut key) = 0;
  62. virtual bool isInside(const Point & position) = 0;
  63. public:
  64. AEventsReceiver();
  65. virtual ~AEventsReceiver() = default;
  66. // These are the arguments that can be used to determine what kind of input the CIntObject will receive
  67. 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};
  68. bool isHovered() const;
  69. bool isActive() const;
  70. bool isActive(int flags) const;
  71. bool isMouseButtonPressed(MouseButton btn) const;
  72. };
  73. // Base UI element
  74. class CIntObject : public IShowActivatable, public AEventsReceiver //interface object
  75. {
  76. ui16 used;
  77. //non-const versions of fields to allow changing them in CIntObject
  78. CIntObject *parent_m; //parent object
  79. public:
  80. //redraw parent flag - this int may be semi-transparent and require redraw of parent window
  81. enum {REDRAW_PARENT=8};
  82. int type; //bin flags using etype
  83. std::vector<CIntObject *> children;
  84. /// read-only parent access. May not be a "clean" solution but allows some compatibility
  85. CIntObject * const & parent;
  86. /// position of object on the screen. Please do not modify this anywhere but in constructor - use moveBy\moveTo instead
  87. /*const*/ Rect pos;
  88. CIntObject(int used=0, Point offset=Point());
  89. virtual ~CIntObject();
  90. //hover handling
  91. void hover (bool on) override{}
  92. //keyboard handling
  93. bool captureAllKeys; //if true, only this object should get info about pressed keys
  94. bool captureThisKey(EShortcut key) override; //allows refining captureAllKeys against specific events (eg. don't capture ENTER)
  95. void addUsedEvents(ui16 newActions);
  96. void removeUsedEvents(ui16 newActions);
  97. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  98. ui8 defActions; //which calls will be tried to be redirected to children
  99. ui8 recActions; //which calls we allow to receive from parent
  100. /// deactivates if needed, blocks all automatic activity, allows only disposal
  101. void disable();
  102. /// activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  103. void enable();
  104. /// deactivates or activates UI element based on flag
  105. void setEnabled(bool on);
  106. // activate or deactivate object. Inactive object won't receive any input events (keyboard\mouse)
  107. // usually used automatically by parent
  108. void activate() override;
  109. void deactivate() override;
  110. //called each frame to update screen
  111. void show(SDL_Surface * to) override;
  112. //called on complete redraw only
  113. void showAll(SDL_Surface * to) override;
  114. //request complete redraw of this object
  115. void redraw() override;
  116. /// called only for windows whenever screen size changes
  117. /// default behavior is to re-center, can be overriden
  118. void onScreenResize() override;
  119. bool isInside(const Point & position) override;
  120. 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
  121. const Rect & center(const Point &p, bool propagate = true); //moves object so that point p will be in its center
  122. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  123. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  124. void moveBy(const Point &p, bool propagate = true);
  125. void moveTo(const Point &p, bool propagate = true);//move this to new position, coordinates are absolute (0,0 is topleft screen corner)
  126. void addChild(CIntObject *child, bool adjustPosition = false);
  127. void removeChild(CIntObject *child, bool adjustPosition = false);
  128. /// functions for printing text.
  129. /// Deprecated. Use CLabel where possible instead
  130. void printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color color, SDL_Surface * dst);
  131. void printAtMiddleWBLoc(const std::string & text, const Point &p, EFonts font, int charsPerLine, SDL_Color color, SDL_Surface * dst);
  132. };
  133. /// Class for binding keys to left mouse button clicks
  134. /// Classes wanting use it should have it as one of their base classes
  135. class CKeyShortcut : public virtual CIntObject
  136. {
  137. bool shortcutPressed;
  138. public:
  139. EShortcut assignedKey;
  140. CKeyShortcut();
  141. CKeyShortcut(EShortcut key);
  142. void keyPressed(EShortcut key) override;
  143. void keyReleased(EShortcut key) override;
  144. };
  145. class WindowBase : public CIntObject
  146. {
  147. public:
  148. WindowBase(int used_ = 0, Point pos_ = Point());
  149. protected:
  150. void close();
  151. };
  152. class IStatusBar
  153. {
  154. public:
  155. virtual ~IStatusBar();
  156. /// set current text for the status bar
  157. virtual void write(const std::string & text) = 0;
  158. /// remove any current text from the status bar
  159. virtual void clear() = 0;
  160. /// remove text from status bar if current text matches tested text
  161. virtual void clearIfMatching(const std::string & testedText) = 0;
  162. /// enables mode for entering text instead of showing hover text
  163. virtual void setEnteringMode(bool on) = 0;
  164. /// overrides hover text from controls with text entered into in-game console (for chat/cheats)
  165. virtual void setEnteredText(const std::string & text) = 0;
  166. };
  167. class EmptyStatusBar : public IStatusBar
  168. {
  169. virtual void write(const std::string & text){};
  170. virtual void clear(){};
  171. virtual void clearIfMatching(const std::string & testedText){};
  172. virtual void setEnteringMode(bool on){};
  173. virtual void setEnteredText(const std::string & text){};
  174. };