CIntObject.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. class CGuiHandler;
  15. class CPicture;
  16. class Canvas;
  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(Canvas & to) = 0;
  30. virtual void showAll(Canvas & to) = 0;
  31. virtual bool isPopupWindow() const = 0;
  32. virtual void onScreenResize() = 0;
  33. virtual ~IShowActivatable() = default;
  34. };
  35. // Base UI element
  36. class CIntObject : public IShowActivatable, public AEventsReceiver //interface object
  37. {
  38. ui16 used;
  39. //non-const versions of fields to allow changing them in CIntObject
  40. CIntObject *parent_m; //parent object
  41. bool inputEnabled;
  42. bool redrawParent;
  43. public:
  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. /// allows capturing key input so it will be delivered only to this element
  52. bool captureThisKey(EShortcut key) override;
  53. void addUsedEvents(ui16 newActions);
  54. void removeUsedEvents(ui16 newActions);
  55. enum {ACTIVATE=1, DEACTIVATE=2, UPDATE=4, SHOWALL=8, DISPOSE=16, SHARE_POS=32};
  56. ui8 defActions; //which calls will be tried to be redirected to children
  57. ui8 recActions; //which calls we allow to receive from parent
  58. /// deactivates if needed, blocks all automatic activity, allows only disposal
  59. void disable();
  60. /// activates if needed, all activity enabled (Warning: may not be symetric with disable if recActions was limited!)
  61. void enable();
  62. /// deactivates or activates UI element based on flag
  63. void setEnabled(bool on);
  64. /// Block (or allow) all user input, e.g. mouse/keyboard/touch without hiding element
  65. void setInputEnabled(bool on);
  66. /// Mark this input as one that requires parent redraw on update,
  67. /// for example if current control might have semi-transparent elements and requires redrawing of background
  68. void setRedrawParent(bool on);
  69. // activate or deactivate object. Inactive object won't receive any input events (keyboard\mouse)
  70. // usually used automatically by parent
  71. void activate() override;
  72. void deactivate() override;
  73. //called each frame to update screen
  74. void show(Canvas & to) override;
  75. //called on complete redraw only
  76. void showAll(Canvas & to) override;
  77. //request complete redraw of this object
  78. void redraw() override;
  79. /// returns true if this element is a popup window
  80. /// called only for windows
  81. bool isPopupWindow() const override;
  82. /// called only for windows whenever screen size changes
  83. /// default behavior is to re-center, can be overriden
  84. void onScreenResize() override;
  85. /// returns true if UI elements wants to handle event of specific type (LCLICK, SHOW_POPUP ...)
  86. /// by default, usedEvents inside UI elements are always handled
  87. bool receiveEvent(const Point & position, int eventType) const override;
  88. 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
  89. const Rect & center(const Point &p, bool propagate = true); //moves object so that point p will be in its center
  90. const Rect & center(bool propagate = true); //centers when pos.w and pos.h are set, returns new position
  91. void fitToScreen(int borderWidth, bool propagate = true); //moves window to fit into screen
  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. bool shortcutPressed;
  102. public:
  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. protected:
  114. void close();
  115. };
  116. class IStatusBar
  117. {
  118. public:
  119. virtual ~IStatusBar() = default;
  120. /// set current text for the status bar
  121. virtual void write(const std::string & text) = 0;
  122. /// remove any current text from the status bar
  123. virtual void clear() = 0;
  124. /// remove text from status bar if current text matches tested text
  125. virtual void clearIfMatching(const std::string & testedText) = 0;
  126. /// enables mode for entering text instead of showing hover text
  127. virtual void setEnteringMode(bool on) = 0;
  128. /// overrides hover text from controls with text entered into in-game console (for chat/cheats)
  129. virtual void setEnteredText(const std::string & text) = 0;
  130. };
  131. class EmptyStatusBar : public IStatusBar
  132. {
  133. virtual void write(const std::string & text){};
  134. virtual void clear(){};
  135. virtual void clearIfMatching(const std::string & testedText){};
  136. virtual void setEnteringMode(bool on){};
  137. virtual void setEnteredText(const std::string & text){};
  138. };