CIntObject.h 8.1 KB

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