VisualComponent.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_GUI_VISUALCOMPONENT
  24. #define DFPSR_GUI_VISUALCOMPONENT
  25. #include "../persistent/includePersistent.h"
  26. #include "BackendWindow.h" // TODO: Separate event types from the window module
  27. #include "FlexRegion.h"
  28. #include "InputEvent.h"
  29. #include "VisualTheme.h"
  30. #include "../api/imageAPI.h"
  31. #include "../api/drawAPI.h"
  32. namespace dsr {
  33. class VisualComponent : public Persistent {
  34. PERSISTENT_DECLARATION(VisualComponent)
  35. protected:
  36. // Parent component
  37. VisualComponent *parent = nullptr;
  38. // Child components
  39. List<std::shared_ptr<VisualComponent>> children;
  40. // Remember the component used for a drag event
  41. // Ensures that mouse down events are followed by mouse up events on the same component
  42. int holdCount = 0;
  43. std::shared_ptr<VisualComponent> dragComponent;
  44. // Remember the focused component for keyboard input
  45. std::shared_ptr<VisualComponent> focusComponent;
  46. // Saved properties
  47. FlexRegion region;
  48. PersistentString name;
  49. PersistentInteger index;
  50. PersistentBoolean visible = PersistentBoolean(true);
  51. void declareAttributes(StructureDefinition &target) const override {
  52. target.declareAttribute(U"Name");
  53. target.declareAttribute(U"Index");
  54. target.declareAttribute(U"Visible");
  55. target.declareAttribute(U"Left");
  56. target.declareAttribute(U"Top");
  57. target.declareAttribute(U"Right");
  58. target.declareAttribute(U"Bottom");
  59. }
  60. public:
  61. Persistent* findAttribute(const ReadableString &name) override {
  62. if (string_caseInsensitiveMatch(name, U"Name")) {
  63. return &(this->name);
  64. } else if (string_caseInsensitiveMatch(name, U"Index")) {
  65. return &(this->index);
  66. } else if (string_caseInsensitiveMatch(name, U"Visible")) {
  67. return &(this->visible);
  68. } else if (string_caseInsensitiveMatch(name, U"Left")) {
  69. return &(this->region.sides[0]);
  70. } else if (string_caseInsensitiveMatch(name, U"Top")) {
  71. return &(this->region.sides[1]);
  72. } else if (string_caseInsensitiveMatch(name, U"Right")) {
  73. return &(this->region.sides[2]);
  74. } else if (string_caseInsensitiveMatch(name, U"Bottom")) {
  75. return &(this->region.sides[3]);
  76. } else {
  77. return nullptr;
  78. }
  79. }
  80. protected:
  81. // Generated automatically from region in applyLayout
  82. IRect location;
  83. void setLocation(const IRect &newLocation);
  84. // Applied reqursively while selecting the correct theme
  85. VisualTheme theme;
  86. public:
  87. void applyTheme(VisualTheme theme);
  88. VisualTheme getTheme() const;
  89. public:
  90. // Constructor
  91. VisualComponent();
  92. // Destructor
  93. virtual ~VisualComponent();
  94. public:
  95. virtual bool isContainer() const;
  96. IRect getLocation() const;
  97. IVector2D getSize() const;
  98. void setRegion(const FlexRegion &newRegion);
  99. FlexRegion getRegion() const;
  100. void setVisible(bool visible);
  101. bool getVisible() const;
  102. void setName(const String& newName);
  103. String getName() const;
  104. void setIndex(int index);
  105. int getIndex() const;
  106. public:
  107. // Callbacks
  108. DECLARE_CALLBACK(pressedEvent, emptyCallback);
  109. DECLARE_CALLBACK(mouseDownEvent, mouseCallback);
  110. DECLARE_CALLBACK(mouseUpEvent, mouseCallback);
  111. DECLARE_CALLBACK(mouseMoveEvent, mouseCallback);
  112. DECLARE_CALLBACK(mouseScrollEvent, mouseCallback);
  113. DECLARE_CALLBACK(keyDownEvent, keyboardCallback);
  114. DECLARE_CALLBACK(keyUpEvent, keyboardCallback);
  115. DECLARE_CALLBACK(keyTypeEvent, keyboardCallback);
  116. DECLARE_CALLBACK(selectEvent, indexCallback);
  117. private:
  118. std::shared_ptr<VisualComponent> getDirectChild(const IVector2D& pixelPosition, bool includeInvisible);
  119. public:
  120. // Draw the component
  121. // The component is responsible for drawing the component at this->location + offset.
  122. // The caller is responsible for drawing the background for any pixels in the component that might not be fully opaque.
  123. // If drawing out of bound, the pixels that are outside should be skipped without any warning nor crash.
  124. // To clip the drawing of a component when calling this, give a sub-image and adjust for the new coordinate system using offset.
  125. // If not implemented, a rectangle will mark the region where the component will be drawn as a reference.
  126. // targetImage is the image being drawn to.
  127. // offset is the upper left corner of the parent container relative to the image.
  128. // Clipping will affect the offset by being relative to the new sub-image.
  129. void draw(ImageRgbaU8& targetImage, const IVector2D& offset);
  130. // A basic request to have the component itself drawn to targetImage at relativeLocation.
  131. // The method is responsible for clipping without a warning when bound is outside of targetImage.
  132. // Clipping will be common if the component is drawn using multiple dirty rectangles to save time.
  133. virtual void drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation);
  134. // Draw the component while skipping pixels outside of clipRegion
  135. // Multiple calls with non-overlapping clip regions should be equivalent to one call with the union of all clip regions.
  136. // This means that the draw methods should handle border clipping so that no extra borderlines or rounded edges appear from nowhere.
  137. // Example:
  138. // drawClipped(i, o, IRect(0, 0, 20, 20)) // Full region
  139. // <=>
  140. // drawClipped(i, o, IRect(0, 0, 10, 20)) // Left half
  141. // drawClipped(i, o, IRect(10, 0, 10, 20)) // Right half
  142. // Drawing with the whole target image as a clip region should be equivalent to a corresponding call to draw with the same targetImage and offset.
  143. // draw(i, o) <=> drawClipped(i, o, IRect(0, 0, i.width(), i.height()))
  144. void drawClipped(ImageRgbaU8 targetImage, const IVector2D& offset, const IRect& clipRegion);
  145. // TODO: Distinguish from the generic version
  146. // Add a child component
  147. // Preconditions:
  148. // The parent's component type is a container.
  149. // The child does not already have a parent.
  150. void addChildComponent(std::shared_ptr<VisualComponent> child);
  151. // Called with any persistent type when constructing child components from text
  152. bool addChild(std::shared_ptr<Persistent> child) override;
  153. // Called when saving to text
  154. int getChildCount() const override;
  155. std::shared_ptr<Persistent> getChild(int index) const override;
  156. // TODO: Reuse in Persistent
  157. // Returns true iff child is a member of the component
  158. // Searches recursively
  159. bool hasChild(VisualComponent *child) const;
  160. bool hasChild(std::shared_ptr<VisualComponent> child) const;
  161. // Find the first child component with the requested name using a case sensitive match.
  162. // If mustExist is true, failure will raise an exception directly.
  163. // Returns: A shared pointer to the child or null if not found.
  164. std::shared_ptr<VisualComponent> findChildByName(ReadableString name, bool mustExist) const;
  165. std::shared_ptr<VisualComponent> findChildByNameAndIndex(ReadableString name, int index, bool mustExist) const;
  166. // Detach the component from any parent
  167. void detachFromParent();
  168. // Adapt the location based on the region
  169. // parentWidth must be the current width of the parent container
  170. // parentHeight must be the current height of the parent container
  171. // Override to apply a custom behaviour, which may be useful for fixed size components.
  172. virtual void applyLayout(IVector2D parentSize);
  173. // Called after the component has been created, moved or resized.
  174. virtual void updateLocationEvent(const IRect& oldLocation, const IRect& newLocation);
  175. // Returns true iff the pixel with its upper left corner at pixelPosition is inside the component.
  176. // A rectangular bound check with location is used by default.
  177. // The caller is responsible for checking if the component is visible when needed.
  178. virtual bool pointIsInside(const IVector2D& pixelPosition);
  179. // Get a reference to the topmost child
  180. // Invisible components are ignored by default, but includeInvisible can be enabled to change that.
  181. // Returns an empty reference if the pixel position didn't hit anything inside.
  182. // Since the root component might not be heap allocated, it cannot return itself by reference.
  183. // Use pointIsInside if your root component doesn't cover the whole window.
  184. std::shared_ptr<VisualComponent> getTopChild(const IVector2D& pixelPosition, bool includeInvisible = false);
  185. // Send a mouse down event to the component
  186. // pixelPosition is relative to the parent container.
  187. // The component is reponsible for bound checking, which can be used to either block the signal or pass to components below.
  188. void sendMouseEvent(const MouseEvent& event);
  189. void sendKeyboardEvent(const KeyboardEvent& event);
  190. // Defines what the component does when it has received an event that didn't hit any sub components on the way.
  191. // pixelPosition is relative to the parent container.
  192. // This is not a callback event.
  193. virtual void receiveMouseEvent(const MouseEvent& event);
  194. virtual void receiveKeyboardEvent(const KeyboardEvent& event);
  195. // Notifies when the theme has been changed, so that temporary data depending on the theme can be replaced
  196. virtual void changedTheme(VisualTheme newTheme);
  197. // Override to be notified about individual attribute changes
  198. virtual void changedAttribute(const ReadableString &name) {};
  199. // Override to be notified about location changes
  200. virtual void changedLocation(const IRect &oldLocation, const IRect &newLocation) {};
  201. // Custom call handler to manipulate components across a generic API
  202. virtual String call(const ReadableString &methodName, const ReadableString &arguments);
  203. };
  204. }
  205. #endif