CIntObject.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "../render/Graphics.h"
  12. #include "../../lib/Rect.h"
  13. #include "EventsReceiver.h"
  14. struct SDL_Surface;
  15. class CGuiHandler;
  16. class CPicture;
  17. class IUpdateable
  18. {
  19. public:
  20. virtual void update()=0;
  21. virtual ~IUpdateable() = default;
  22. };
  23. class IShowActivatable
  24. {
  25. public:
  26. virtual void activate()=0;
  27. virtual void deactivate()=0;
  28. virtual void redraw()=0;
  29. virtual void show(SDL_Surface * to) = 0;
  30. virtual void showAll(SDL_Surface * to) = 0;
  31. virtual void onScreenResize() = 0;
  32. virtual ~IShowActivatable() = default;
  33. };
  34. // Base UI element
  35. class CIntObject : public IShowActivatable, public AEventsReceiver //interface object
  36. {
  37. ui16 used;
  38. //non-const versions of fields to allow changing them in CIntObject
  39. CIntObject *parent_m; //parent object
  40. public:
  41. //redraw parent flag - this int may be semi-transparent and require redraw of parent window
  42. enum {REDRAW_PARENT=8};
  43. int type; //bin flags using etype
  44. std::vector<CIntObject *> children;
  45. /// read-only parent access. May not be a "clean" solution but allows some compatibility
  46. CIntObject * const & parent;
  47. /// position of object on the screen. Please do not modify this anywhere but in constructor - use moveBy\moveTo instead
  48. /*const*/ Rect pos;
  49. CIntObject(int used=0, Point offset=Point());
  50. virtual ~CIntObject();
  51. //hover handling
  52. void hover (bool on) override{}
  53. //keyboard handling
  54. bool captureAllKeys; //if true, only this object should get info about pressed keys
  55. bool captureThisKey(EShortcut key) override; //allows refining captureAllKeys against specific events (eg. don't capture ENTER)
  56. void addUsedEvents(ui16 newActions);
  57. void removeUsedEvents(ui16 newActions);
  58. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  59. ui8 defActions; //which calls will be tried to be redirected to children
  60. ui8 recActions; //which calls we allow to receive from parent
  61. /// deactivates if needed, blocks all automatic activity, allows only disposal
  62. void disable();
  63. /// activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  64. void enable();
  65. /// deactivates or activates UI element based on flag
  66. void setEnabled(bool on);
  67. // activate or deactivate object. Inactive object won't receive any input events (keyboard\mouse)
  68. // usually used automatically by parent
  69. void activate() override;
  70. void deactivate() override;
  71. //called each frame to update screen
  72. void show(SDL_Surface * to) override;
  73. //called on complete redraw only
  74. void showAll(SDL_Surface * to) override;
  75. //request complete redraw of this object
  76. void redraw() override;
  77. /// called only for windows whenever screen size changes
  78. /// default behavior is to re-center, can be overriden
  79. void onScreenResize() override;
  80. bool isInside(const Point & position) override;
  81. 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
  82. const Rect & center(const Point &p, bool propagate = true); //moves object so that point p will be in its center
  83. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  84. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  85. void moveBy(const Point &p, bool propagate = true);
  86. void moveTo(const Point &p, bool propagate = true);//move this to new position, coordinates are absolute (0,0 is topleft screen corner)
  87. void addChild(CIntObject *child, bool adjustPosition = false);
  88. void removeChild(CIntObject *child, bool adjustPosition = false);
  89. /// functions for printing text.
  90. /// Deprecated. Use CLabel where possible instead
  91. void printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, const SDL_Color & color, SDL_Surface * dst);
  92. void printAtMiddleWBLoc(const std::string & text, const Point &p, EFonts font, int charsPerLine, const SDL_Color & color, SDL_Surface * dst);
  93. };
  94. /// Class for binding keys to left mouse button clicks
  95. /// Classes wanting use it should have it as one of their base classes
  96. class CKeyShortcut : public virtual CIntObject
  97. {
  98. bool shortcutPressed;
  99. public:
  100. EShortcut assignedKey;
  101. CKeyShortcut();
  102. CKeyShortcut(EShortcut key);
  103. void keyPressed(EShortcut key) override;
  104. void keyReleased(EShortcut key) override;
  105. };
  106. class WindowBase : public CIntObject
  107. {
  108. public:
  109. WindowBase(int used_ = 0, Point pos_ = Point());
  110. protected:
  111. void close();
  112. };
  113. class IStatusBar
  114. {
  115. public:
  116. virtual ~IStatusBar() = default;
  117. /// set current text for the status bar
  118. virtual void write(const std::string & text) = 0;
  119. /// remove any current text from the status bar
  120. virtual void clear() = 0;
  121. /// remove text from status bar if current text matches tested text
  122. virtual void clearIfMatching(const std::string & testedText) = 0;
  123. /// enables mode for entering text instead of showing hover text
  124. virtual void setEnteringMode(bool on) = 0;
  125. /// overrides hover text from controls with text entered into in-game console (for chat/cheats)
  126. virtual void setEnteredText(const std::string & text) = 0;
  127. };
  128. class EmptyStatusBar : public IStatusBar
  129. {
  130. virtual void write(const std::string & text){};
  131. virtual void clear(){};
  132. virtual void clearIfMatching(const std::string & testedText){};
  133. virtual void setEnteringMode(bool on){};
  134. virtual void setEnteredText(const std::string & text){};
  135. };