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.

220 lines
6.4KB

  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. const auto smallestIntegerContainer = area.getSmallestIntegerContainer();
  112. auto newBounds = smallestIntegerContainer + parentOrigin;
  113. originRelativeToComponent = -smallestIntegerContainer.getPosition();
  114. setBounds (newBounds);
  115. }
  116. //==============================================================================
  117. bool Drawable::replaceColour (Colour original, Colour replacement)
  118. {
  119. bool changed = false;
  120. for (auto* c : getChildren())
  121. if (auto* d = dynamic_cast<Drawable*> (c))
  122. changed = d->replaceColour (original, replacement) || changed;
  123. return changed;
  124. }
  125. void Drawable::setDrawableTransform (const AffineTransform& transform)
  126. {
  127. drawableTransform = transform;
  128. updateTransform();
  129. }
  130. void Drawable::updateTransform()
  131. {
  132. if (drawableTransform.isIdentity())
  133. return;
  134. const auto transformationOrigin = originRelativeToComponent + getPosition();
  135. setTransform (AffineTransform::translation (transformationOrigin * (-1))
  136. .followedBy (drawableTransform)
  137. .followedBy (AffineTransform::translation (transformationOrigin)));
  138. }
  139. //==============================================================================
  140. void Drawable::setOriginWithOriginalSize (Point<float> originWithinParent)
  141. {
  142. setTransform (AffineTransform::translation (originWithinParent.x, originWithinParent.y));
  143. }
  144. void Drawable::setTransformToFit (const Rectangle<float>& area, RectanglePlacement placement)
  145. {
  146. if (! area.isEmpty())
  147. setTransform (placement.getTransformToFit (getDrawableBounds(), area));
  148. }
  149. //==============================================================================
  150. std::unique_ptr<Drawable> Drawable::createFromImageData (const void* data, const size_t numBytes)
  151. {
  152. auto image = ImageFileFormat::loadFrom (data, numBytes);
  153. if (image.isValid())
  154. return std::make_unique<DrawableImage> (image);
  155. if (auto svg = parseXMLIfTagMatches (String::createStringFromData (data, (int) numBytes), "svg"))
  156. return Drawable::createFromSVG (*svg);
  157. return {};
  158. }
  159. std::unique_ptr<Drawable> Drawable::createFromImageDataStream (InputStream& dataSource)
  160. {
  161. MemoryOutputStream mo;
  162. mo << dataSource;
  163. return createFromImageData (mo.getData(), mo.getDataSize());
  164. }
  165. std::unique_ptr<Drawable> Drawable::createFromImageFile (const File& file)
  166. {
  167. FileInputStream fin (file);
  168. if (fin.openedOk())
  169. return createFromImageDataStream (fin);
  170. return {};
  171. }
  172. } // namespace juce