The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

186 lines
7.3KB

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