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.

201 lines
5.6KB

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