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.

juce_Drawable.h 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_DRAWABLE_H_INCLUDED
  18. #define JUCE_DRAWABLE_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. The base class for objects which can draw themselves, e.g. polygons, images, etc.
  22. @see DrawableComposite, DrawableImage, DrawablePath, DrawableText
  23. */
  24. class JUCE_API Drawable : public Component
  25. {
  26. protected:
  27. //==============================================================================
  28. /** The base class can't be instantiated directly.
  29. @see DrawableComposite, DrawableImage, DrawablePath, DrawableText
  30. */
  31. Drawable();
  32. public:
  33. /** Destructor. */
  34. virtual ~Drawable();
  35. //==============================================================================
  36. /** Creates a deep copy of this Drawable object.
  37. Use this to create a new copy of this and any sub-objects in the tree.
  38. */
  39. virtual Drawable* createCopy() const = 0;
  40. //==============================================================================
  41. /** Renders this Drawable object.
  42. Note that the preferred way to render a drawable in future is by using it
  43. as a component and adding it to a parent, so you might want to consider that
  44. before using this method.
  45. @see drawWithin
  46. */
  47. void draw (Graphics& g, float opacity,
  48. const AffineTransform& transform = AffineTransform::identity) const;
  49. /** Renders the Drawable at a given offset within the Graphics context.
  50. The coordinates passed-in are used to translate the object relative to its own
  51. origin before drawing it - this is basically a quick way of saying:
  52. @code
  53. draw (g, AffineTransform::translation (x, y)).
  54. @endcode
  55. Note that the preferred way to render a drawable in future is by using it
  56. as a component and adding it to a parent, so you might want to consider that
  57. before using this method.
  58. */
  59. void drawAt (Graphics& g, float x, float y, float opacity) const;
  60. /** Renders the Drawable within a rectangle, scaling it to fit neatly inside without
  61. changing its aspect-ratio.
  62. The object can placed arbitrarily within the rectangle based on a Justification type,
  63. and can either be made as big as possible, or just reduced to fit.
  64. Note that the preferred way to render a drawable in future is by using it
  65. as a component and adding it to a parent, so you might want to consider that
  66. before using this method.
  67. @param g the graphics context to render onto
  68. @param destArea the target rectangle to fit the drawable into
  69. @param placement defines the alignment and rescaling to use to fit
  70. this object within the target rectangle.
  71. @param opacity the opacity to use, in the range 0 to 1.0
  72. */
  73. void drawWithin (Graphics& g,
  74. const Rectangle<float>& destArea,
  75. RectanglePlacement placement,
  76. float opacity) const;
  77. //==============================================================================
  78. /** Resets any transformations on this drawable, and positions its origin within
  79. its parent component.
  80. */
  81. void setOriginWithOriginalSize (Point<float> originWithinParent);
  82. /** Sets a transform for this drawable that will position it within the specified
  83. area of its parent component.
  84. */
  85. void setTransformToFit (const Rectangle<float>& areaInParent, RectanglePlacement placement);
  86. /** Returns the DrawableComposite that contains this object, if there is one. */
  87. DrawableComposite* getParent() const;
  88. //==============================================================================
  89. /** Tries to turn some kind of image file into a drawable.
  90. The data could be an image that the ImageFileFormat class understands, or it
  91. could be SVG.
  92. */
  93. static Drawable* createFromImageData (const void* data, size_t numBytes);
  94. /** Tries to turn a stream containing some kind of image data into a drawable.
  95. The data could be an image that the ImageFileFormat class understands, or it
  96. could be SVG.
  97. */
  98. static Drawable* createFromImageDataStream (InputStream& dataSource);
  99. /** Tries to turn a file containing some kind of image data into a drawable.
  100. The data could be an image that the ImageFileFormat class understands, or it
  101. could be SVG.
  102. */
  103. static Drawable* createFromImageFile (const File& file);
  104. /** Attempts to parse an SVG (Scalable Vector Graphics) document, and to turn this
  105. into a Drawable tree.
  106. The object returned must be deleted by the caller. If something goes wrong
  107. while parsing, it may return 0.
  108. SVG is a pretty large and complex spec, and this doesn't aim to be a full
  109. implementation, but it can return the basic vector objects.
  110. */
  111. static Drawable* createFromSVG (const XmlElement& svgDocument);
  112. /** Parses an SVG path string and returns it. */
  113. static Path parseSVGPath (const String& svgPath);
  114. //==============================================================================
  115. /** Tries to create a Drawable from a previously-saved ValueTree.
  116. The ValueTree must have been created by the createValueTree() method.
  117. If there are any images used within the drawable, you'll need to provide a valid
  118. ImageProvider object that can be used to retrieve these images from whatever type
  119. of identifier is used to represent them.
  120. Internally, this uses a ComponentBuilder, and registerDrawableTypeHandlers().
  121. */
  122. static Drawable* createFromValueTree (const ValueTree& tree, ComponentBuilder::ImageProvider* imageProvider);
  123. /** Creates a ValueTree to represent this Drawable.
  124. The ValueTree that is returned can be turned back into a Drawable with createFromValueTree().
  125. If there are any images used in this drawable, you'll need to provide a valid ImageProvider
  126. object that can be used to create storable representations of them.
  127. */
  128. virtual ValueTree createValueTree (ComponentBuilder::ImageProvider* imageProvider) const = 0;
  129. /** Returns the area that this drawble covers.
  130. The result is expressed in this drawable's own coordinate space, and does not take
  131. into account any transforms that may be applied to the component.
  132. */
  133. virtual Rectangle<float> getDrawableBounds() const = 0;
  134. //==============================================================================
  135. /** Internal class used to manage ValueTrees that represent Drawables. */
  136. class ValueTreeWrapperBase
  137. {
  138. public:
  139. ValueTreeWrapperBase (const ValueTree& state);
  140. ValueTree& getState() noexcept { return state; }
  141. String getID() const;
  142. void setID (const String& newID);
  143. ValueTree state;
  144. };
  145. //==============================================================================
  146. /** Registers a set of ComponentBuilder::TypeHandler objects that can be used to
  147. load all the different Drawable types from a saved state.
  148. @see ComponentBuilder::registerTypeHandler()
  149. */
  150. static void registerDrawableTypeHandlers (ComponentBuilder& componentBuilder);
  151. protected:
  152. //==============================================================================
  153. friend class DrawableComposite;
  154. friend class DrawableShape;
  155. /** @internal */
  156. void transformContextToCorrectOrigin (Graphics&);
  157. /** @internal */
  158. void parentHierarchyChanged() override;
  159. /** @internal */
  160. void setBoundsToEnclose (const Rectangle<float>&);
  161. Point<int> originRelativeToComponent;
  162. #ifndef DOXYGEN
  163. /** Internal utility class used by Drawables. */
  164. template <class DrawableType>
  165. class Positioner : public RelativeCoordinatePositionerBase
  166. {
  167. public:
  168. Positioner (DrawableType& c)
  169. : RelativeCoordinatePositionerBase (c),
  170. owner (c)
  171. {}
  172. bool registerCoordinates() { return owner.registerCoordinates (*this); }
  173. void applyToComponentBounds()
  174. {
  175. ComponentScope scope (getComponent());
  176. owner.recalculateCoordinates (&scope);
  177. }
  178. void applyNewBounds (const Rectangle<int>&)
  179. {
  180. jassertfalse; // drawables can't be resized directly!
  181. }
  182. private:
  183. DrawableType& owner;
  184. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Positioner)
  185. };
  186. Drawable (const Drawable&);
  187. #endif
  188. private:
  189. void nonConstDraw (Graphics&, float opacity, const AffineTransform&);
  190. Drawable& operator= (const Drawable&);
  191. JUCE_LEAK_DETECTOR (Drawable)
  192. };
  193. #endif // JUCE_DRAWABLE_H_INCLUDED