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.

juce_DrawableComposite.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. DrawableComposite::DrawableComposite()
  21. : bounds ({ 0.0f, 0.0f, 100.0f, 100.0f })
  22. {
  23. setContentArea ({ 0.0f, 0.0f, 100.0f, 100.0f });
  24. }
  25. DrawableComposite::DrawableComposite (const DrawableComposite& other)
  26. : Drawable (other),
  27. bounds (other.bounds),
  28. contentArea (other.contentArea)
  29. {
  30. for (auto* c : other.getChildren())
  31. if (auto* d = dynamic_cast<const Drawable*> (c))
  32. addAndMakeVisible (d->createCopy().release());
  33. }
  34. DrawableComposite::~DrawableComposite()
  35. {
  36. deleteAllChildren();
  37. }
  38. std::unique_ptr<Drawable> DrawableComposite::createCopy() const
  39. {
  40. return std::make_unique<DrawableComposite> (*this);
  41. }
  42. //==============================================================================
  43. Rectangle<float> DrawableComposite::getDrawableBounds() const
  44. {
  45. Rectangle<float> r;
  46. for (auto* c : getChildren())
  47. if (auto* d = dynamic_cast<const Drawable*> (c))
  48. r = r.getUnion (d->isTransformed() ? d->getDrawableBounds().transformedBy (d->getTransform())
  49. : d->getDrawableBounds());
  50. return r;
  51. }
  52. void DrawableComposite::setContentArea (Rectangle<float> newArea)
  53. {
  54. contentArea = newArea;
  55. }
  56. void DrawableComposite::setBoundingBox (Rectangle<float> newBounds)
  57. {
  58. setBoundingBox (Parallelogram<float> (newBounds));
  59. }
  60. void DrawableComposite::setBoundingBox (Parallelogram<float> newBounds)
  61. {
  62. if (bounds != newBounds)
  63. {
  64. bounds = newBounds;
  65. auto t = AffineTransform::fromTargetPoints (contentArea.getTopLeft(), bounds.topLeft,
  66. contentArea.getTopRight(), bounds.topRight,
  67. contentArea.getBottomLeft(), bounds.bottomLeft);
  68. if (t.isSingularity())
  69. t = {};
  70. setTransform (t);
  71. }
  72. }
  73. void DrawableComposite::resetBoundingBoxToContentArea()
  74. {
  75. setBoundingBox (contentArea);
  76. }
  77. void DrawableComposite::resetContentAreaAndBoundingBoxToFitChildren()
  78. {
  79. setContentArea (getDrawableBounds());
  80. resetBoundingBoxToContentArea();
  81. }
  82. void DrawableComposite::parentHierarchyChanged()
  83. {
  84. if (auto* parent = getParent())
  85. originRelativeToComponent = parent->originRelativeToComponent - getPosition();
  86. }
  87. void DrawableComposite::childBoundsChanged (Component*)
  88. {
  89. updateBoundsToFitChildren();
  90. }
  91. void DrawableComposite::childrenChanged()
  92. {
  93. updateBoundsToFitChildren();
  94. }
  95. void DrawableComposite::updateBoundsToFitChildren()
  96. {
  97. if (! updateBoundsReentrant)
  98. {
  99. const ScopedValueSetter<bool> setter (updateBoundsReentrant, true, false);
  100. Rectangle<int> childArea;
  101. for (auto* c : getChildren())
  102. childArea = childArea.getUnion (c->getBoundsInParent());
  103. auto delta = childArea.getPosition();
  104. childArea += getPosition();
  105. if (childArea != getBounds())
  106. {
  107. if (! delta.isOrigin())
  108. {
  109. originRelativeToComponent -= delta;
  110. for (auto* c : getChildren())
  111. c->setBounds (c->getBounds() - delta);
  112. }
  113. setBounds (childArea);
  114. }
  115. }
  116. }
  117. //==============================================================================
  118. Path DrawableComposite::getOutlineAsPath() const
  119. {
  120. Path p;
  121. for (auto* c : getChildren())
  122. if (auto* d = dynamic_cast<Drawable*> (c))
  123. p.addPath (d->getOutlineAsPath());
  124. p.applyTransform (getTransform());
  125. return p;
  126. }
  127. } // namespace juce