juce_Drawable.h 8.5 KB

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