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.

192 lines
7.3KB

  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. A base class implementing common functionality for Drawable classes which
  24. consist of some kind of filled and stroked outline.
  25. @see DrawablePath, DrawableRectangle
  26. */
  27. class JUCE_API DrawableShape : public Drawable
  28. {
  29. protected:
  30. //==============================================================================
  31. DrawableShape();
  32. DrawableShape (const DrawableShape&);
  33. public:
  34. /** Destructor. */
  35. ~DrawableShape();
  36. //==============================================================================
  37. /** A FillType wrapper that allows the gradient coordinates to be implemented using RelativePoint.
  38. */
  39. class RelativeFillType
  40. {
  41. public:
  42. RelativeFillType();
  43. RelativeFillType (const FillType& fill);
  44. RelativeFillType (const RelativeFillType&);
  45. RelativeFillType& operator= (const RelativeFillType&);
  46. bool operator== (const RelativeFillType&) const;
  47. bool operator!= (const RelativeFillType&) const;
  48. bool isDynamic() const;
  49. bool recalculateCoords (Expression::Scope* scope);
  50. void writeTo (ValueTree& v, ComponentBuilder::ImageProvider*, UndoManager*) const;
  51. bool readFrom (const ValueTree& v, ComponentBuilder::ImageProvider*);
  52. //==============================================================================
  53. FillType fill;
  54. RelativePoint gradientPoint1, gradientPoint2, gradientPoint3;
  55. };
  56. //==============================================================================
  57. /** Sets a fill type for the path.
  58. This colour is used to fill the path - if you don't want the path to be
  59. filled (e.g. if you're just drawing an outline), set this to a transparent
  60. colour.
  61. @see setPath, setStrokeFill
  62. */
  63. void setFill (const FillType& newFill);
  64. /** Sets a fill type for the path.
  65. This colour is used to fill the path - if you don't want the path to be
  66. filled (e.g. if you're just drawing an outline), set this to a transparent
  67. colour.
  68. @see setPath, setStrokeFill
  69. */
  70. void setFill (const RelativeFillType& newFill);
  71. /** Returns the current fill type.
  72. @see setFill
  73. */
  74. const RelativeFillType& getFill() const noexcept { return mainFill; }
  75. /** Sets the fill type with which the outline will be drawn.
  76. @see setFill
  77. */
  78. void setStrokeFill (const FillType& newStrokeFill);
  79. /** Sets the fill type with which the outline will be drawn.
  80. @see setFill
  81. */
  82. void setStrokeFill (const RelativeFillType& newStrokeFill);
  83. /** Returns the current stroke fill.
  84. @see setStrokeFill
  85. */
  86. const RelativeFillType& getStrokeFill() const noexcept { return strokeFill; }
  87. /** Changes the properties of the outline that will be drawn around the path.
  88. If the stroke has 0 thickness, no stroke will be drawn.
  89. @see setStrokeThickness, setStrokeColour
  90. */
  91. void setStrokeType (const PathStrokeType& newStrokeType);
  92. /** Changes the stroke thickness.
  93. This is a shortcut for calling setStrokeType.
  94. */
  95. void setStrokeThickness (float newThickness);
  96. /** Returns the current outline style. */
  97. const PathStrokeType& getStrokeType() const noexcept { return strokeType; }
  98. /** Provides a set of dash lengths to use for stroking the path. */
  99. void setDashLengths (const Array<float>& newDashLengths);
  100. /** Returns the set of dash lengths that the path is using. */
  101. const Array<float>& getDashLengths() const noexcept { return dashLengths; }
  102. //==============================================================================
  103. /** @internal */
  104. class FillAndStrokeState : public Drawable::ValueTreeWrapperBase
  105. {
  106. public:
  107. FillAndStrokeState (const ValueTree& state);
  108. ValueTree getFillState (const Identifier& fillOrStrokeType);
  109. RelativeFillType getFill (const Identifier& fillOrStrokeType, ComponentBuilder::ImageProvider*) const;
  110. void setFill (const Identifier& fillOrStrokeType, const RelativeFillType& newFill,
  111. ComponentBuilder::ImageProvider*, UndoManager*);
  112. PathStrokeType getStrokeType() const;
  113. void setStrokeType (const PathStrokeType& newStrokeType, UndoManager*);
  114. static const Identifier type, colour, colours, fill, stroke, path, jointStyle, capStyle, strokeWidth,
  115. gradientPoint1, gradientPoint2, gradientPoint3, radial, imageId, imageOpacity;
  116. };
  117. /** @internal */
  118. Rectangle<float> getDrawableBounds() const override;
  119. /** @internal */
  120. void paint (Graphics&) override;
  121. /** @internal */
  122. bool hitTest (int x, int y) override;
  123. /** @internal */
  124. bool replaceColour (Colour originalColour, Colour replacementColour) override;
  125. /** @internal */
  126. Path getOutlineAsPath() const override;
  127. protected:
  128. //==============================================================================
  129. /** Called when the cached path should be updated. */
  130. void pathChanged();
  131. /** Called when the cached stroke should be updated. */
  132. void strokeChanged();
  133. /** True if there's a stroke with a non-zero thickness and non-transparent colour. */
  134. bool isStrokeVisible() const noexcept;
  135. /** Updates the details from a FillAndStrokeState object, returning true if something changed. */
  136. void refreshFillTypes (const FillAndStrokeState& newState, ComponentBuilder::ImageProvider*);
  137. /** Writes the stroke and fill details to a FillAndStrokeState object. */
  138. void writeTo (FillAndStrokeState& state, ComponentBuilder::ImageProvider*, UndoManager*) const;
  139. //==============================================================================
  140. PathStrokeType strokeType;
  141. Array<float> dashLengths;
  142. Path path, strokePath;
  143. private:
  144. class RelativePositioner;
  145. RelativeFillType mainFill, strokeFill;
  146. ScopedPointer<RelativeCoordinatePositionerBase> mainFillPositioner, strokeFillPositioner;
  147. void setFillInternal (RelativeFillType& fill, const RelativeFillType& newFill,
  148. ScopedPointer<RelativeCoordinatePositionerBase>& positioner);
  149. DrawableShape& operator= (const DrawableShape&);
  150. };
  151. } // namespace juce