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.

185 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_DRAWABLESHAPE_JUCEHEADER__
  19. #define __JUCE_DRAWABLESHAPE_JUCEHEADER__
  20. #include "juce_Drawable.h"
  21. #include "../positioning/juce_RelativeCoordinatePositioner.h"
  22. //==============================================================================
  23. /**
  24. A base class implementing common functionality for Drawable classes which
  25. consist of some kind of filled and stroked outline.
  26. @see DrawablePath, DrawableRectangle
  27. */
  28. class JUCE_API DrawableShape : public Drawable
  29. {
  30. protected:
  31. //==============================================================================
  32. DrawableShape();
  33. DrawableShape (const DrawableShape&);
  34. public:
  35. /** Destructor. */
  36. ~DrawableShape();
  37. //==============================================================================
  38. /** A FillType wrapper that allows the gradient coordinates to be implemented using RelativePoint.
  39. */
  40. class RelativeFillType
  41. {
  42. public:
  43. RelativeFillType();
  44. RelativeFillType (const FillType& fill);
  45. RelativeFillType (const RelativeFillType&);
  46. RelativeFillType& operator= (const RelativeFillType&);
  47. bool operator== (const RelativeFillType&) const;
  48. bool operator!= (const RelativeFillType&) const;
  49. bool isDynamic() const;
  50. bool recalculateCoords (Expression::Scope* scope);
  51. void writeTo (ValueTree& v, ComponentBuilder::ImageProvider*, UndoManager*) const;
  52. bool readFrom (const ValueTree& v, ComponentBuilder::ImageProvider*);
  53. //==============================================================================
  54. FillType fill;
  55. RelativePoint gradientPoint1, gradientPoint2, gradientPoint3;
  56. };
  57. //==============================================================================
  58. /** Sets a fill type for the path.
  59. This colour is used to fill the path - if you don't want the path to be
  60. filled (e.g. if you're just drawing an outline), set this to a transparent
  61. colour.
  62. @see setPath, setStrokeFill
  63. */
  64. void setFill (const FillType& newFill);
  65. /** Sets a fill type for the path.
  66. This colour is used to fill the path - if you don't want the path to be
  67. filled (e.g. if you're just drawing an outline), set this to a transparent
  68. colour.
  69. @see setPath, setStrokeFill
  70. */
  71. void setFill (const RelativeFillType& newFill);
  72. /** Returns the current fill type.
  73. @see setFill
  74. */
  75. const RelativeFillType& getFill() const noexcept { return mainFill; }
  76. /** Sets the fill type with which the outline will be drawn.
  77. @see setFill
  78. */
  79. void setStrokeFill (const FillType& newStrokeFill);
  80. /** Sets the fill type with which the outline will be drawn.
  81. @see setFill
  82. */
  83. void setStrokeFill (const RelativeFillType& newStrokeFill);
  84. /** Returns the current stroke fill.
  85. @see setStrokeFill
  86. */
  87. const RelativeFillType& getStrokeFill() const noexcept { return strokeFill; }
  88. /** Changes the properties of the outline that will be drawn around the path.
  89. If the stroke has 0 thickness, no stroke will be drawn.
  90. @see setStrokeThickness, setStrokeColour
  91. */
  92. void setStrokeType (const PathStrokeType& newStrokeType);
  93. /** Changes the stroke thickness.
  94. This is a shortcut for calling setStrokeType.
  95. */
  96. void setStrokeThickness (float newThickness);
  97. /** Returns the current outline style. */
  98. const PathStrokeType& getStrokeType() const noexcept { return strokeType; }
  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;
  116. /** @internal */
  117. void paint (Graphics& g);
  118. /** @internal */
  119. bool hitTest (int x, int y);
  120. protected:
  121. //==============================================================================
  122. /** Called when the cached path should be updated. */
  123. void pathChanged();
  124. /** Called when the cached stroke should be updated. */
  125. void strokeChanged();
  126. /** True if there's a stroke with a non-zero thickness and non-transparent colour. */
  127. bool isStrokeVisible() const noexcept;
  128. /** Updates the details from a FillAndStrokeState object, returning true if something changed. */
  129. void refreshFillTypes (const FillAndStrokeState& newState, ComponentBuilder::ImageProvider*);
  130. /** Writes the stroke and fill details to a FillAndStrokeState object. */
  131. void writeTo (FillAndStrokeState& state, ComponentBuilder::ImageProvider*, UndoManager*) const;
  132. //==============================================================================
  133. PathStrokeType strokeType;
  134. Path path, strokePath;
  135. private:
  136. class RelativePositioner;
  137. RelativeFillType mainFill, strokeFill;
  138. ScopedPointer<RelativeCoordinatePositionerBase> mainFillPositioner, strokeFillPositioner;
  139. void setFillInternal (RelativeFillType& fill, const RelativeFillType& newFill,
  140. ScopedPointer<RelativeCoordinatePositionerBase>& positioner);
  141. DrawableShape& operator= (const DrawableShape&);
  142. };
  143. #endif // __JUCE_DRAWABLESHAPE_JUCEHEADER__