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.

202 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. Drawable::Drawable()
  21. {
  22. setInterceptsMouseClicks (false, false);
  23. setPaintingIsUnclipped (true);
  24. setAccessible (false);
  25. }
  26. Drawable::Drawable (const Drawable& other)
  27. : Component (other.getName())
  28. {
  29. setInterceptsMouseClicks (false, false);
  30. setPaintingIsUnclipped (true);
  31. setAccessible (false);
  32. setComponentID (other.getComponentID());
  33. setTransform (other.getTransform());
  34. if (auto* clipPath = other.drawableClipPath.get())
  35. setClipPath (clipPath->createCopy());
  36. }
  37. Drawable::~Drawable()
  38. {
  39. }
  40. void Drawable::applyDrawableClipPath (Graphics& g)
  41. {
  42. if (drawableClipPath != nullptr)
  43. {
  44. auto clipPath = drawableClipPath->getOutlineAsPath();
  45. if (! clipPath.isEmpty())
  46. g.getInternalContext().clipToPath (clipPath, {});
  47. }
  48. }
  49. //==============================================================================
  50. void Drawable::draw (Graphics& g, float opacity, const AffineTransform& transform) const
  51. {
  52. const_cast<Drawable*> (this)->nonConstDraw (g, opacity, transform);
  53. }
  54. void Drawable::nonConstDraw (Graphics& g, float opacity, const AffineTransform& transform)
  55. {
  56. Graphics::ScopedSaveState ss (g);
  57. g.addTransform (AffineTransform::translation ((float) -(originRelativeToComponent.x),
  58. (float) -(originRelativeToComponent.y))
  59. .followedBy (getTransform())
  60. .followedBy (transform));
  61. applyDrawableClipPath (g);
  62. if (! g.isClipEmpty())
  63. {
  64. if (opacity < 1.0f)
  65. {
  66. g.beginTransparencyLayer (opacity);
  67. paintEntireComponent (g, true);
  68. g.endTransparencyLayer();
  69. }
  70. else
  71. {
  72. paintEntireComponent (g, true);
  73. }
  74. }
  75. }
  76. void Drawable::drawAt (Graphics& g, float x, float y, float opacity) const
  77. {
  78. draw (g, opacity, AffineTransform::translation (x, y));
  79. }
  80. void Drawable::drawWithin (Graphics& g, Rectangle<float> destArea,
  81. RectanglePlacement placement, float opacity) const
  82. {
  83. draw (g, opacity, placement.getTransformToFit (getDrawableBounds(), destArea));
  84. }
  85. //==============================================================================
  86. DrawableComposite* Drawable::getParent() const
  87. {
  88. return dynamic_cast<DrawableComposite*> (getParentComponent());
  89. }
  90. void Drawable::setClipPath (std::unique_ptr<Drawable> clipPath)
  91. {
  92. if (drawableClipPath != clipPath)
  93. {
  94. drawableClipPath = std::move (clipPath);
  95. repaint();
  96. }
  97. }
  98. void Drawable::transformContextToCorrectOrigin (Graphics& g)
  99. {
  100. g.setOrigin (originRelativeToComponent);
  101. }
  102. void Drawable::parentHierarchyChanged()
  103. {
  104. setBoundsToEnclose (getDrawableBounds());
  105. }
  106. void Drawable::setBoundsToEnclose (Rectangle<float> area)
  107. {
  108. Point<int> parentOrigin;
  109. if (auto* parent = getParent())
  110. parentOrigin = parent->originRelativeToComponent;
  111. auto newBounds = area.getSmallestIntegerContainer() + parentOrigin;
  112. originRelativeToComponent = parentOrigin - newBounds.getPosition();
  113. setBounds (newBounds);
  114. }
  115. //==============================================================================
  116. bool Drawable::replaceColour (Colour original, Colour replacement)
  117. {
  118. bool changed = false;
  119. for (auto* c : getChildren())
  120. if (auto* d = dynamic_cast<Drawable*> (c))
  121. changed = d->replaceColour (original, replacement) || changed;
  122. return changed;
  123. }
  124. //==============================================================================
  125. void Drawable::setOriginWithOriginalSize (Point<float> originWithinParent)
  126. {
  127. setTransform (AffineTransform::translation (originWithinParent.x, originWithinParent.y));
  128. }
  129. void Drawable::setTransformToFit (const Rectangle<float>& area, RectanglePlacement placement)
  130. {
  131. if (! area.isEmpty())
  132. setTransform (placement.getTransformToFit (getDrawableBounds(), area));
  133. }
  134. //==============================================================================
  135. std::unique_ptr<Drawable> Drawable::createFromImageData (const void* data, const size_t numBytes)
  136. {
  137. auto image = ImageFileFormat::loadFrom (data, numBytes);
  138. if (image.isValid())
  139. return std::make_unique<DrawableImage> (image);
  140. if (auto svg = parseXMLIfTagMatches (String::createStringFromData (data, (int) numBytes), "svg"))
  141. return Drawable::createFromSVG (*svg);
  142. return {};
  143. }
  144. std::unique_ptr<Drawable> Drawable::createFromImageDataStream (InputStream& dataSource)
  145. {
  146. MemoryOutputStream mo;
  147. mo << dataSource;
  148. return createFromImageData (mo.getData(), mo.getDataSize());
  149. }
  150. std::unique_ptr<Drawable> Drawable::createFromImageFile (const File& file)
  151. {
  152. FileInputStream fin (file);
  153. if (fin.openedOk())
  154. return createFromImageDataStream (fin);
  155. return {};
  156. }
  157. } // namespace juce