Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
8.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. The base class for objects which can draw themselves, e.g. polygons, images, etc.
  23. @see DrawableComposite, DrawableImage, DrawablePath, DrawableText
  24. @tags{GUI}
  25. */
  26. class JUCE_API Drawable : public Component
  27. {
  28. protected:
  29. //==============================================================================
  30. /** The base class can't be instantiated directly.
  31. @see DrawableComposite, DrawableImage, DrawablePath, DrawableText
  32. */
  33. Drawable();
  34. public:
  35. /** Destructor. */
  36. ~Drawable() override;
  37. //==============================================================================
  38. /** Creates a deep copy of this Drawable object.
  39. Use this to create a new copy of this and any sub-objects in the tree.
  40. */
  41. virtual std::unique_ptr<Drawable> createCopy() const = 0;
  42. /** Creates a path that describes the outline of this drawable. */
  43. virtual Path getOutlineAsPath() const = 0;
  44. //==============================================================================
  45. /** Renders this Drawable object.
  46. Note that the preferred way to render a drawable in future is by using it
  47. as a component and adding it to a parent, so you might want to consider that
  48. before using this method.
  49. @see drawWithin
  50. */
  51. void draw (Graphics& g, float opacity,
  52. const AffineTransform& transform = AffineTransform()) const;
  53. /** Renders the Drawable at a given offset within the Graphics context.
  54. The coordinates passed-in are used to translate the object relative to its own
  55. origin before drawing it - this is basically a quick way of saying:
  56. @code
  57. draw (g, AffineTransform::translation (x, y)).
  58. @endcode
  59. Note that the preferred way to render a drawable in future is by using it
  60. as a component and adding it to a parent, so you might want to consider that
  61. before using this method.
  62. */
  63. void drawAt (Graphics& g, float x, float y, float opacity) const;
  64. /** Renders the Drawable within a rectangle, scaling it to fit neatly inside without
  65. changing its aspect-ratio.
  66. The object can placed arbitrarily within the rectangle based on a Justification type,
  67. and can either be made as big as possible, or just reduced to fit.
  68. Note that the preferred way to render a drawable in future is by using it
  69. as a component and adding it to a parent, so you might want to consider that
  70. before using this method.
  71. @param g the graphics context to render onto
  72. @param destArea the target rectangle to fit the drawable into
  73. @param placement defines the alignment and rescaling to use to fit
  74. this object within the target rectangle.
  75. @param opacity the opacity to use, in the range 0 to 1.0
  76. */
  77. void drawWithin (Graphics& g,
  78. Rectangle<float> destArea,
  79. RectanglePlacement placement,
  80. float opacity) const;
  81. //==============================================================================
  82. /** Resets any transformations on this drawable, and positions its origin within
  83. its parent component.
  84. */
  85. void setOriginWithOriginalSize (Point<float> originWithinParent);
  86. /** Sets a transform for this drawable that will position it within the specified
  87. area of its parent component.
  88. */
  89. void setTransformToFit (const Rectangle<float>& areaInParent, RectanglePlacement placement);
  90. /** Returns the DrawableComposite that contains this object, if there is one. */
  91. DrawableComposite* getParent() const;
  92. /** Sets a the clipping region of this drawable using another drawable.
  93. The drawable passed in will be deleted when no longer needed.
  94. */
  95. void setClipPath (std::unique_ptr<Drawable> drawableClipPath);
  96. //==============================================================================
  97. /** Tries to turn some kind of image file into a drawable.
  98. The data could be an image that the ImageFileFormat class understands, or it
  99. could be SVG.
  100. */
  101. static std::unique_ptr<Drawable> createFromImageData (const void* data, size_t numBytes);
  102. /** Tries to turn a stream containing some kind of image data into a drawable.
  103. The data could be an image that the ImageFileFormat class understands, or it
  104. could be SVG.
  105. */
  106. static std::unique_ptr<Drawable> createFromImageDataStream (InputStream& dataSource);
  107. /** Tries to turn a file containing some kind of image data into a drawable.
  108. The data could be an image that the ImageFileFormat class understands, or it
  109. could be SVG.
  110. */
  111. static std::unique_ptr<Drawable> createFromImageFile (const File& file);
  112. /** Attempts to parse an SVG (Scalable Vector Graphics) document, and to turn this
  113. into a Drawable tree.
  114. If something goes wrong while parsing, it may return nullptr.
  115. SVG is a pretty large and complex spec, and this doesn't aim to be a full
  116. implementation, but it can return the basic vector objects.
  117. */
  118. static std::unique_ptr<Drawable> createFromSVG (const XmlElement& svgDocument);
  119. /** Attempts to parse an SVG (Scalable Vector Graphics) document from a file,
  120. and to turn this into a Drawable tree.
  121. If something goes wrong while parsing, it may return nullptr.
  122. SVG is a pretty large and complex spec, and this doesn't aim to be a full
  123. implementation, but it can return the basic vector objects.
  124. Any references to references to external image files will be relative to
  125. the parent directory of the file passed.
  126. */
  127. static std::unique_ptr<Drawable> createFromSVGFile (const File& svgFile);
  128. /** Parses an SVG path string and returns it. */
  129. static Path parseSVGPath (const String& svgPath);
  130. //==============================================================================
  131. /** Returns the area that this drawable covers.
  132. The result is expressed in this drawable's own coordinate space, and does not take
  133. into account any transforms that may be applied to the component.
  134. */
  135. virtual Rectangle<float> getDrawableBounds() const = 0;
  136. /** Recursively replaces a colour that might be used for filling or stroking.
  137. return true if any instances of this colour were found.
  138. */
  139. virtual bool replaceColour (Colour originalColour, Colour replacementColour);
  140. protected:
  141. //==============================================================================
  142. friend class DrawableComposite;
  143. friend class DrawableShape;
  144. /** @internal */
  145. void transformContextToCorrectOrigin (Graphics&);
  146. /** @internal */
  147. void parentHierarchyChanged() override;
  148. /** @internal */
  149. void setBoundsToEnclose (Rectangle<float>);
  150. /** @internal */
  151. void applyDrawableClipPath (Graphics&);
  152. Point<int> originRelativeToComponent;
  153. std::unique_ptr<Drawable> drawableClipPath;
  154. void nonConstDraw (Graphics&, float opacity, const AffineTransform&);
  155. Drawable (const Drawable&);
  156. Drawable& operator= (const Drawable&);
  157. JUCE_LEAK_DETECTOR (Drawable)
  158. };
  159. } // namespace juce