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_DrawableShape.h 7.3KB

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