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.

208 lines
5.9KB

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