CIntObject.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. typedef int32_t SDL_Keycode;
  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 {BLOCK_ADV_HOTKEYS = 2, REDRAW_PARENT=8};
  50. int type; //bin flags using etype
  51. IShowActivatable();
  52. virtual ~IShowActivatable(){};
  53. };
  54. // Base UI element
  55. class CIntObject : public IShowActivatable //interface object
  56. {
  57. ui16 used;//change via addUsed() or delUsed
  58. std::map<MouseButton, bool> currentMouseState;
  59. //non-const versions of fields to allow changing them in CIntObject
  60. CIntObject *parent_m; //parent object
  61. ui16 active_m;
  62. protected:
  63. //activate or deactivate specific action (LCLICK, RCLICK...)
  64. void activate(ui16 what);
  65. void deactivate(ui16 what);
  66. public:
  67. /*
  68. * Functions and fields that supposed to be private but are not for now.
  69. * Don't use them unless you really know what they are for
  70. */
  71. std::vector<CIntObject *> children;
  72. /*
  73. * Public interface
  74. */
  75. /// read-only parent access. May not be a "clean" solution but allows some compatibility
  76. CIntObject * const & parent;
  77. /// position of object on the screen. Please do not modify this anywhere but in constructor - use moveBy\moveTo instead
  78. /*const*/ Rect pos;
  79. CIntObject(int used=0, Point offset=Point());
  80. virtual ~CIntObject();
  81. void updateMouseState(MouseButton btn, bool state) { currentMouseState[btn] = state; }
  82. bool mouseState(MouseButton btn) const { return currentMouseState.count(btn) ? currentMouseState.at(btn) : false; }
  83. virtual void click(MouseButton btn, tribool down, bool previousState);
  84. virtual void clickLeft(tribool down, bool previousState) {}
  85. virtual void clickRight(tribool down, bool previousState) {}
  86. virtual void clickMiddle(tribool down, bool previousState) {}
  87. //hover handling
  88. /*const*/ bool hovered; //for determining if object is hovered
  89. virtual void hover (bool on){}
  90. //keyboard handling
  91. bool captureAllKeys; //if true, only this object should get info about pressed keys
  92. virtual void keyPressed(const SDL_Keycode & key){}
  93. virtual void keyReleased(const SDL_Keycode & key){}
  94. virtual bool captureThisKey(const SDL_Keycode & key); //allows refining captureAllKeys against specific events (eg. don't capture ENTER)
  95. virtual void textInputed(const std::string & enteredText){};
  96. virtual void textEdited(const std::string & enteredText){};
  97. //mouse movement handling
  98. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  99. virtual void mouseMoved (const Point & cursorPosition){}
  100. //time handling
  101. virtual void tick(uint32_t msPassed){}
  102. //mouse wheel
  103. virtual void wheelScrolled(bool down, bool in){}
  104. //double click
  105. virtual void onDoubleClick(){}
  106. // These are the arguments that can be used to determine what kind of input the CIntObject will receive
  107. 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};
  108. const ui16 & active;
  109. void addUsedEvents(ui16 newActions);
  110. void removeUsedEvents(ui16 newActions);
  111. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  112. ui8 defActions; //which calls will be tried to be redirected to children
  113. ui8 recActions; //which calls we allow to receive from parent
  114. void disable(); //deactivates if needed, blocks all automatic activity, allows only disposal
  115. void enable(); //activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  116. // activate or deactivate object. Inactive object won't receive any input events (keyboard\mouse)
  117. // usually used automatically by parent
  118. void activate() override;
  119. void deactivate() override;
  120. //called each frame to update screen
  121. void show(SDL_Surface * to) override;
  122. //called on complete redraw only
  123. void showAll(SDL_Surface * to) override;
  124. //request complete redraw of this object
  125. void redraw() override;
  126. 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
  127. const Rect & center(const Point &p, bool propagate = true); //moves object so that point p will be in its center
  128. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  129. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  130. void moveBy(const Point &p, bool propagate = true);
  131. void moveTo(const Point &p, bool propagate = true);//move this to new position, coordinates are absolute (0,0 is topleft screen corner)
  132. void addChild(CIntObject *child, bool adjustPosition = false);
  133. void removeChild(CIntObject *child, bool adjustPosition = false);
  134. //delChild - not needed, use normal "delete child" instead
  135. //delChildNull - not needed, use "vstd::clear_pointer(child)" instead
  136. /*
  137. * Functions that should be used only by specific GUI elements. Don't use them unless you really know why they are here
  138. */
  139. //functions for printing text. Use CLabel where possible instead
  140. void printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  141. void printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  142. void printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color color, SDL_Surface * dst);
  143. void printAtMiddleWBLoc(const std::string & text, int x, int y, EFonts font, int charsPerLine, SDL_Color color, SDL_Surface * dst);
  144. //image blitting. If possible use CPicture or CAnimImage instead
  145. void blitAtLoc(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  146. void blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst);
  147. friend class CGuiHandler;
  148. };
  149. /// Class for binding keys to left mouse button clicks
  150. /// Classes wanting use it should have it as one of their base classes
  151. class CKeyShortcut : public virtual CIntObject
  152. {
  153. public:
  154. std::set<int> assignedKeys;
  155. CKeyShortcut();
  156. CKeyShortcut(int key);
  157. CKeyShortcut(std::set<int> Keys);
  158. void keyPressed(const SDL_Keycode & key) override;
  159. void keyReleased(const SDL_Keycode & key) override;
  160. };
  161. class WindowBase : public CIntObject
  162. {
  163. public:
  164. WindowBase(int used_ = 0, Point pos_ = Point());
  165. protected:
  166. void close();
  167. };
  168. class IStatusBar
  169. {
  170. public:
  171. virtual ~IStatusBar();
  172. /// set current text for the status bar
  173. virtual void write(const std::string & text) = 0;
  174. /// remove any current text from the status bar
  175. virtual void clear() = 0;
  176. /// remove text from status bar if current text matches tested text
  177. virtual void clearIfMatching(const std::string & testedText) = 0;
  178. /// enables mode for entering text instead of showing hover text
  179. virtual void setEnteringMode(bool on) = 0;
  180. /// overrides hover text from controls with text entered into in-game console (for chat/cheats)
  181. virtual void setEnteredText(const std::string & text) = 0;
  182. };