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.

175 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. DrawableShape::DrawableShape()
  16. : strokeType (0.0f),
  17. mainFill (Colours::black),
  18. strokeFill (Colours::black)
  19. {
  20. }
  21. DrawableShape::DrawableShape (const DrawableShape& other)
  22. : Drawable (other),
  23. strokeType (other.strokeType),
  24. dashLengths (other.dashLengths),
  25. mainFill (other.mainFill),
  26. strokeFill (other.strokeFill)
  27. {
  28. }
  29. DrawableShape::~DrawableShape()
  30. {
  31. }
  32. //==============================================================================
  33. void DrawableShape::setFill (const FillType& newFill)
  34. {
  35. if (mainFill != newFill)
  36. {
  37. mainFill = newFill;
  38. repaint();
  39. }
  40. }
  41. void DrawableShape::setStrokeFill (const FillType& newFill)
  42. {
  43. if (strokeFill != newFill)
  44. {
  45. strokeFill = newFill;
  46. repaint();
  47. }
  48. }
  49. void DrawableShape::setStrokeType (const PathStrokeType& newStrokeType)
  50. {
  51. if (strokeType != newStrokeType)
  52. {
  53. strokeType = newStrokeType;
  54. strokeChanged();
  55. }
  56. }
  57. void DrawableShape::setDashLengths (const Array<float>& newDashLengths)
  58. {
  59. if (dashLengths != newDashLengths)
  60. {
  61. dashLengths = newDashLengths;
  62. strokeChanged();
  63. }
  64. }
  65. void DrawableShape::setStrokeThickness (const float newThickness)
  66. {
  67. setStrokeType (PathStrokeType (newThickness, strokeType.getJointStyle(), strokeType.getEndStyle()));
  68. }
  69. bool DrawableShape::isStrokeVisible() const noexcept
  70. {
  71. return strokeType.getStrokeThickness() > 0.0f && ! strokeFill.isInvisible();
  72. }
  73. //==============================================================================
  74. void DrawableShape::paint (Graphics& g)
  75. {
  76. transformContextToCorrectOrigin (g);
  77. applyDrawableClipPath (g);
  78. g.setFillType (mainFill);
  79. g.fillPath (path);
  80. if (isStrokeVisible())
  81. {
  82. g.setFillType (strokeFill);
  83. g.fillPath (strokePath);
  84. }
  85. }
  86. void DrawableShape::pathChanged()
  87. {
  88. strokeChanged();
  89. }
  90. void DrawableShape::strokeChanged()
  91. {
  92. strokePath.clear();
  93. const float extraAccuracy = 4.0f;
  94. if (dashLengths.isEmpty())
  95. strokeType.createStrokedPath (strokePath, path, AffineTransform(), extraAccuracy);
  96. else
  97. strokeType.createDashedStroke (strokePath, path, dashLengths.getRawDataPointer(),
  98. dashLengths.size(), AffineTransform(), extraAccuracy);
  99. setBoundsToEnclose (getDrawableBounds());
  100. repaint();
  101. }
  102. Rectangle<float> DrawableShape::getDrawableBounds() const
  103. {
  104. if (isStrokeVisible())
  105. return strokePath.getBounds();
  106. return path.getBounds();
  107. }
  108. bool DrawableShape::hitTest (int x, int y)
  109. {
  110. bool allowsClicksOnThisComponent, allowsClicksOnChildComponents;
  111. getInterceptsMouseClicks (allowsClicksOnThisComponent, allowsClicksOnChildComponents);
  112. if (! allowsClicksOnThisComponent)
  113. return false;
  114. auto globalX = (float) (x - originRelativeToComponent.x);
  115. auto globalY = (float) (y - originRelativeToComponent.y);
  116. return path.contains (globalX, globalY)
  117. || (isStrokeVisible() && strokePath.contains (globalX, globalY));
  118. }
  119. //==============================================================================
  120. static bool replaceColourInFill (FillType& fill, Colour original, Colour replacement)
  121. {
  122. if (fill.colour == original && fill.isColour())
  123. {
  124. fill = FillType (replacement);
  125. return true;
  126. }
  127. return false;
  128. }
  129. bool DrawableShape::replaceColour (Colour original, Colour replacement)
  130. {
  131. bool changed1 = replaceColourInFill (mainFill, original, replacement);
  132. bool changed2 = replaceColourInFill (strokeFill, original, replacement);
  133. return changed1 || changed2;
  134. }
  135. Path DrawableShape::getOutlineAsPath() const
  136. {
  137. auto outline = isStrokeVisible() ? strokePath : path;
  138. outline.applyTransform (getTransform());
  139. return outline;
  140. }
  141. } // namespace juce