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