CIntObject.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "EventsReceiver.h"
  12. #include "../../lib/Rect.h"
  13. #include "../../lib/Color.h"
  14. #include "../../lib/GameConstants.h"
  15. class GameEngine;
  16. class CPicture;
  17. class Canvas;
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. class CArmedInstance;
  20. VCMI_LIB_NAMESPACE_END
  21. class IShowActivatable
  22. {
  23. public:
  24. virtual void activate()=0;
  25. virtual void deactivate()=0;
  26. virtual void redraw()=0;
  27. virtual void show(Canvas & to) = 0;
  28. virtual void showAll(Canvas & to) = 0;
  29. virtual bool isPopupWindow() const = 0;
  30. virtual void onScreenResize() = 0;
  31. virtual ~IShowActivatable() = default;
  32. };
  33. // Base UI element
  34. class CIntObject : public IShowActivatable, public AEventsReceiver //interface object
  35. {
  36. ui16 used;
  37. //non-const versions of fields to allow changing them in CIntObject
  38. CIntObject *parent_m; //parent object
  39. bool inputEnabled;
  40. bool redrawParent;
  41. public:
  42. std::vector<CIntObject *> children;
  43. /// read-only parent access. May not be a "clean" solution but allows some compatibility
  44. CIntObject * const & parent;
  45. /// position of object on the screen. Please do not modify this anywhere but in constructor - use moveBy\moveTo instead
  46. /*const*/ Rect pos;
  47. CIntObject(int used=0, Point offset=Point());
  48. virtual ~CIntObject();
  49. bool captureThisKey(EShortcut key) override;
  50. void addUsedEvents(ui16 newActions);
  51. void removeUsedEvents(ui16 newActions);
  52. enum {NO_ACTIONS = 0, ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, SHARE_POS=16, ALL_ACTIONS=31};
  53. ui8 recActions; //which calls we allow to receive from parent
  54. /// deactivates if needed, blocks all automatic activity, allows only disposal
  55. void disable();
  56. /// activates if needed, all activity enabled (Warning: may not be symmetric with disable if recActions was limited!)
  57. void enable();
  58. /// deactivates or activates UI element based on flag
  59. void setEnabled(bool on);
  60. /// Block (or allow) all user input, e.g. mouse/keyboard/touch without hiding element
  61. void setInputEnabled(bool on);
  62. /// Mark this input as one that requires parent redraw on update,
  63. /// for example if current control might have semi-transparent elements and requires redrawing of background
  64. void setRedrawParent(bool on);
  65. // activate or deactivate object. Inactive object won't receive any input events (keyboard\mouse)
  66. // usually used automatically by parent
  67. void activate() override;
  68. void deactivate() override;
  69. //called each frame to update screen
  70. void show(Canvas & to) override;
  71. //called on complete redraw only
  72. void showAll(Canvas & to) override;
  73. //request complete redraw of this object
  74. void redraw() override;
  75. // Move child object to foreground
  76. void moveChildForeground(const CIntObject * childToMove);
  77. /// returns true if this element is a popup window
  78. /// called only for windows
  79. bool isPopupWindow() const override;
  80. /// called only for windows whenever screen size changes
  81. /// default behavior is to re-center, can be overridden
  82. void onScreenResize() override;
  83. /// returns true if UI elements wants to handle event of specific type (LCLICK, SHOW_POPUP ...)
  84. /// by default, usedEvents inside UI elements are always handled
  85. bool receiveEvent(const Point & position, int eventType) const override;
  86. const Rect & getPosition() const override;
  87. 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
  88. const Rect & center(const Point &p, bool propagate = true); //moves object so that point p will be in its center
  89. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  90. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  91. void fitToRect(Rect rect, int borderWidth, bool propagate = true); //moves window to fit into rect
  92. void moveBy(const Point &p, bool propagate = true);
  93. void moveTo(const Point &p, bool propagate = true);//move this to new position, coordinates are absolute (0,0 is topleft screen corner)
  94. void addChild(CIntObject *child, bool adjustPosition = false);
  95. void removeChild(CIntObject *child, bool adjustPosition = false);
  96. };
  97. /// Class for binding keys to left mouse button clicks
  98. /// Classes wanting use it should have it as one of their base classes
  99. class CKeyShortcut : public virtual CIntObject
  100. {
  101. public:
  102. bool shortcutPressed;
  103. EShortcut assignedKey;
  104. CKeyShortcut();
  105. CKeyShortcut(EShortcut key);
  106. void keyPressed(EShortcut key) override;
  107. void keyReleased(EShortcut key) override;
  108. };
  109. class WindowBase : public CIntObject
  110. {
  111. public:
  112. WindowBase(int used_ = 0, Point pos_ = Point());
  113. virtual void close();
  114. };
  115. class IGarrisonHolder
  116. {
  117. public:
  118. bool holdsGarrisons(std::vector<const CArmedInstance *> armies)
  119. {
  120. for (auto const * army : armies)
  121. if (holdsGarrison(army))
  122. return true;
  123. return false;
  124. }
  125. virtual bool holdsGarrison(const CArmedInstance * army) = 0;
  126. virtual void updateGarrisons() = 0;
  127. };
  128. class IMarketHolder
  129. {
  130. public:
  131. virtual void updateResources() {};
  132. virtual void updateExperience() {};
  133. virtual void updateSecondarySkills() {};
  134. virtual void updateArtifacts() {};
  135. };
  136. class ITownHolder
  137. {
  138. public:
  139. virtual void buildChanged() = 0;
  140. };
  141. class IStatusBar
  142. {
  143. public:
  144. virtual ~IStatusBar() = default;
  145. /// set current text for the status bar
  146. virtual void write(const std::string & text) = 0;
  147. /// remove any current text from the status bar
  148. virtual void clear() = 0;
  149. /// remove text from status bar if current text matches tested text
  150. virtual void clearIfMatching(const std::string & testedText) = 0;
  151. /// enables mode for entering text instead of showing hover text
  152. virtual void setEnteringMode(bool on) = 0;
  153. /// overrides hover text from controls with text entered into in-game console (for chat/cheats)
  154. virtual void setEnteredText(const std::string & text) = 0;
  155. };
  156. class EmptyStatusBar : public IStatusBar
  157. {
  158. virtual void write(const std::string & text){};
  159. virtual void clear(){};
  160. virtual void clearIfMatching(const std::string & testedText){};
  161. virtual void setEnteringMode(bool on){};
  162. virtual void setEnteredText(const std::string & text){};
  163. };
  164. class ObjectConstruction : boost::noncopyable
  165. {
  166. public:
  167. ObjectConstruction(CIntObject *obj);
  168. ~ObjectConstruction();
  169. };
  170. /// If used, all UI widgets created inside this scope will be added to children of 'this'
  171. #define OBJECT_CONSTRUCTION ObjectConstruction obj__i(this)
  172. /// If used, all UI widgets created inside this scope will be added to children of provided object
  173. #define OBJECT_CONSTRUCTION_TARGETED(obj) ObjectConstruction obj__i(obj)