CIntObject.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma once
  2. #include <SDL_events.h>
  3. #include "Geometries.h"
  4. #include "../FontBase.h"
  5. struct SDL_Surface;
  6. /*
  7. * CIntObject.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. using boost::logic::tribool;
  16. // Defines a activate/deactive method
  17. class IActivatable
  18. {
  19. public:
  20. virtual void activate()=0;
  21. virtual void deactivate()=0;
  22. virtual ~IActivatable(){}; //d-tor
  23. };
  24. class IUpdateable
  25. {
  26. public:
  27. virtual void update()=0;
  28. virtual ~IUpdateable(){}; //d-tor
  29. };
  30. // Defines a show method
  31. class IShowable
  32. {
  33. public:
  34. virtual void redraw()=0;
  35. virtual void show(SDL_Surface * to) = 0;
  36. virtual void showAll(SDL_Surface * to)
  37. {
  38. show(to);
  39. }
  40. virtual ~IShowable(){}; //d-tor
  41. };
  42. class IShowActivatable : public IShowable, public IActivatable
  43. {
  44. public:
  45. //redraw parent flag - this int may be semi-transparent and require redraw of parent window
  46. enum {WITH_GARRISON = 1, BLOCK_ADV_HOTKEYS = 2, WITH_ARTIFACTS = 4, REDRAW_PARENT=8};
  47. int type; //bin flags using etype
  48. IShowActivatable();
  49. virtual ~IShowActivatable(){}; //d-tor
  50. };
  51. // Status bar interface
  52. class IStatusBar
  53. {
  54. public:
  55. virtual ~IStatusBar(){}; //d-tor
  56. virtual void print(const std::string & text)=0; //prints text and refreshes statusbar
  57. virtual void clear()=0;//clears statusbar and refreshes
  58. virtual void show(SDL_Surface * to)=0; //shows statusbar (with current text)
  59. virtual std::string getCurrent()=0; //returns currently displayed text
  60. };
  61. // Base UI element
  62. class CIntObject : public IShowActivatable //interface object
  63. {
  64. public:
  65. CIntObject *parent; //parent object
  66. std::vector<CIntObject *> children;
  67. Rect pos, //position of object on the screen
  68. posRelative; //position of object in the parent (not used if no parent)
  69. CIntObject();
  70. virtual ~CIntObject(); //d-tor
  71. //l-clicks handling
  72. bool pressedL; //for determining if object is L-pressed
  73. void activateLClick();
  74. void deactivateLClick();
  75. virtual void clickLeft(tribool down, bool previousState);
  76. //r-clicks handling
  77. bool pressedR; //for determining if object is R-pressed
  78. void activateRClick();
  79. void deactivateRClick();
  80. virtual void clickRight(tribool down, bool previousState);
  81. //hover handling
  82. bool hovered; //for determining if object is hovered
  83. void activateHover();
  84. void deactivateHover();
  85. virtual void hover (bool on);
  86. //keyboard handling
  87. bool captureAllKeys; //if true, only this object should get info about pressed keys
  88. void activateKeys();
  89. void deactivateKeys();
  90. virtual void keyPressed(const SDL_KeyboardEvent & key);
  91. virtual bool captureThisEvent(const SDL_KeyboardEvent & key); //allows refining captureAllKeys against specific events (eg. don't capture ENTER)
  92. //mouse movement handling
  93. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  94. void activateMouseMove();
  95. void deactivateMouseMove();
  96. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  97. //time handling
  98. int toNextTick;
  99. void activateTimer();
  100. void deactivateTimer();
  101. virtual void tick();
  102. //mouse wheel
  103. void activateWheel();
  104. void deactivateWheel();
  105. virtual void wheelScrolled(bool down, bool in);
  106. //double click
  107. void activateDClick();
  108. void deactivateDClick();
  109. virtual void onDoubleClick();
  110. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, ALL=0xffff};
  111. ui16 active;
  112. ui16 used;
  113. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  114. ui8 defActions; //which calls will be tried to be redirected to children
  115. ui8 recActions; //which calls we allow te receive from parent
  116. enum EAlignment {TOPLEFT, CENTER, BOTTOMRIGHT};
  117. void disable(); //deactivates if needed, blocks all automatic activity, allows only disposal
  118. void enable(bool activation = true); //activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  119. // activate or deactivate object. Inactive object won't receive any input events (keyboard\mouse)
  120. // usually used automatically by parent
  121. void activate();
  122. void deactivate();
  123. //activate or deactivate specific action (LCLICK, RCLICK...)
  124. void activate(ui16 what);
  125. void deactivate(ui16 what);
  126. //called each frame to update screen
  127. void show(SDL_Surface * to);
  128. //called on complete redraw only
  129. void showAll(SDL_Surface * to);
  130. //request complete redraw
  131. void redraw();
  132. void drawBorderLoc(SDL_Surface * sur, const Rect &r, const int3 &color);
  133. //functions for printing text. Use CLabel where possible instead
  134. void printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  135. void printToLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  136. void printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  137. void printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color color, SDL_Surface * dst);
  138. void printAtMiddleWBLoc(const std::string & text, int x, int y, EFonts font, int charsPerLine, SDL_Color color, SDL_Surface * dst);
  139. //image blitting. If possible use CPicture or CAnimImage instead
  140. void blitAtLoc(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  141. void blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst);
  142. bool isItInLoc(const SDL_Rect &rect, int x, int y);
  143. bool isItInLoc(const SDL_Rect &rect, const Point &p);
  144. 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
  145. const Rect & center(const Point &p, bool propagate = true); //moves object so that point p will be in its center
  146. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  147. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  148. void moveBy(const Point &p, bool propagate = true);
  149. void moveTo(const Point &p, bool propagate = true);//move this to new position, coordinates are absolute (0,0 is topleft screen corner)
  150. void changeUsedEvents(ui16 what, bool enable, bool adjust = true);
  151. //add child without parent to this. Use CGuiHandler::moveChild() if child already have parent
  152. void addChild(CIntObject *child, bool adjustPosition = false);
  153. //remove child from this without deleting
  154. void removeChild(CIntObject *child, bool adjustPosition = false);
  155. void delChild(CIntObject *child); //removes from children list, deletes
  156. template <typename T> void delChildNUll(T *&child, bool deactivateIfNeeded = false) //removes from children list, deletes and sets pointer to NULL
  157. {
  158. if(!child)
  159. return;
  160. if(deactivateIfNeeded && child->active)
  161. child->deactivate();
  162. delChild(child);
  163. child = NULL;
  164. }
  165. };
  166. /// Class for binding keys to left mouse button clicks
  167. /// Classes wanting use it should have it as one of their base classes
  168. class CKeyShortcut : public virtual CIntObject
  169. {
  170. public:
  171. std::set<int> assignedKeys;
  172. CKeyShortcut(){}; //c-tor
  173. CKeyShortcut(int key){assignedKeys.insert(key);}; //c-tor
  174. CKeyShortcut(std::set<int> Keys):assignedKeys(Keys){}; //c-tor
  175. virtual void keyPressed(const SDL_KeyboardEvent & key); //call-in
  176. };