CIntObject.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #pragma once
  2. #include <SDL_events.h>
  3. #include "../../lib/int3.h"
  4. #include "../Gfx/Basic.h"
  5. #include "../Graphics.h"
  6. struct SDL_Surface;
  7. class CPicture;
  8. class CGuiHandler;
  9. /*
  10. * CIntObject.h, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. using boost::logic::tribool;
  19. using Gfx::Point;
  20. using Gfx::Rect;
  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(){}; //d-tor
  28. };
  29. class IUpdateable
  30. {
  31. public:
  32. virtual void update()=0;
  33. virtual ~IUpdateable(){}; //d-tor
  34. };
  35. // Defines a show method
  36. class IShowable
  37. {
  38. public:
  39. virtual void redraw()=0;
  40. virtual void show() = 0;
  41. virtual void showAll()
  42. {
  43. show();
  44. }
  45. virtual ~IShowable(){}; //d-tor
  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(){}; //d-tor
  55. };
  56. //typedef ui16 ActivityFlag;
  57. // Base UI element
  58. class CIntObject : public IShowActivatable //interface object
  59. {
  60. ui16 used;//change via addUsed() or delUsed
  61. //time handling
  62. int toNextTick;
  63. int timerDelay;
  64. void onTimer(int timePassed);
  65. //non-const versions of fields to allow changing them in CIntObject
  66. CIntObject *parent_m; //parent object
  67. ui16 active_m;
  68. protected:
  69. //activate or deactivate specific action (LCLICK, RCLICK...)
  70. void activate(ui16 what);
  71. void deactivate(ui16 what);
  72. public:
  73. /*
  74. * Functions and fields that supposed to be private but are not for now.
  75. * Don't use them unless you really know what they are for
  76. */
  77. std::vector<CIntObject *> children;
  78. //FIXME: workaround
  79. void deactivateKeyboard()
  80. {
  81. deactivate(KEYBOARD);
  82. }
  83. /*
  84. * Public interface
  85. */
  86. /// read-only parent access. May not be a "clean" solution but allows some compatibility
  87. CIntObject * const & parent;
  88. const ui16 & active;
  89. /// position of object on the screen. Please do not modify this anywhere but in constructor - use moveBy\moveTo instead
  90. Rect pos;
  91. CIntObject(int used_ = 0, Point offset = Point(0, 0));
  92. virtual ~CIntObject(); //d-tor
  93. //l-clicks handling
  94. /*const*/ bool pressedL; //for determining if object is L-pressed
  95. virtual void clickLeft(tribool down, bool previousState){}
  96. //r-clicks handling
  97. /*const*/ bool pressedR; //for determining if object is R-pressed
  98. virtual void clickRight(tribool down, bool previousState){}
  99. //hover handling
  100. /*const*/ bool hovered; //for determining if object is hovered
  101. virtual void hover (bool on){}
  102. //keyboard handling
  103. bool captureAllKeys; //if true, only this object should get info about pressed keys
  104. virtual void keyPressed(const SDL_KeyboardEvent & key){}
  105. virtual bool captureThisEvent(const SDL_KeyboardEvent & key); //allows refining captureAllKeys against specific events (eg. don't capture ENTER)
  106. //mouse movement handling
  107. bool strongInterest; //if true - report all mouse movements, if not - only when hovered
  108. virtual void mouseMoved (const SDL_MouseMotionEvent & sEvent){}
  109. //time handling
  110. void setTimer(int msToTrigger);//set timer delay and activate timer if needed.
  111. virtual void tick(){}
  112. //mouse wheel
  113. virtual void wheelScrolled(bool down, bool in){}
  114. //double click
  115. virtual void onDoubleClick(){}
  116. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, ALL=0xffff};
  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();
  127. void deactivate();
  128. //called each frame to update screen
  129. void show();
  130. //called on complete redraw only
  131. void showAll();
  132. //request complete redraw of this object
  133. void redraw();
  134. enum EAlignment {TOPLEFT, CENTER, BOTTOMRIGHT};
  135. bool isItInLoc(const Rect &rect, int x, int y);
  136. bool isItInLoc(const Rect &rect, Point p);
  137. 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
  138. const Rect & center(Point p, bool propagate = true); //moves object so that point p will be in its center
  139. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  140. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  141. void moveBy(Point p, bool propagate = true);
  142. void moveTo(Point p, bool propagate = true);//move this to new position, coordinates are absolute (0,0 is topleft screen corner)
  143. void addChild(CIntObject *child, bool adjustPosition = false);
  144. void removeChild(CIntObject *child, bool adjustPosition = false);
  145. //delChild - not needed, use normal "delete child" instead
  146. //delChildNull - not needed, use "vstd::clear_pointer(child)" instead
  147. /*
  148. * Functions that should be used only by specific GUI elements. Don't use them unless you really know why they are here
  149. */
  150. //wrappers for CSDL_Ext methods. This versions use coordinates relative to pos
  151. void drawBorderLoc(SDL_Surface * sur, const Rect &r, const int3 &color);
  152. //functions for printing text. Use CLabel where possible instead
  153. void printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  154. void printToLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  155. void printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color color, SDL_Surface * dst);
  156. void printAtMiddleLoc(const std::string & text, Point p, EFonts font, SDL_Color color, SDL_Surface * dst);
  157. void printAtMiddleWBLoc(const std::string & text, int x, int y, EFonts font, int charsPerLine, SDL_Color color, SDL_Surface * dst);
  158. //image blitting. If possible use CPicture or CAnimImage instead
  159. void blitAtLoc(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  160. void blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst);
  161. friend class CGuiHandler;
  162. };
  163. /// Class for binding keys to left mouse button clicks
  164. /// Classes wanting use it should have it as one of their base classes
  165. class CKeyShortcut : public virtual CIntObject
  166. {
  167. public:
  168. std::set<int> assignedKeys;
  169. CKeyShortcut(){}; //c-tor
  170. CKeyShortcut(int key){assignedKeys.insert(key);}; //c-tor
  171. CKeyShortcut(std::set<int> Keys):assignedKeys(Keys){}; //c-tor
  172. virtual void keyPressed(const SDL_KeyboardEvent & key); //call-in
  173. };