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.

145 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. BubbleComponent::BubbleComponent()
  18. : allowablePlacements (above | below | left | right)
  19. {
  20. setInterceptsMouseClicks (false, false);
  21. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.35f), 5, Point<int>()));
  22. setComponentEffect (&shadow);
  23. }
  24. BubbleComponent::~BubbleComponent() {}
  25. //==============================================================================
  26. void BubbleComponent::paint (Graphics& g)
  27. {
  28. getLookAndFeel().drawBubble (g, *this, arrowTip.toFloat(), content.toFloat());
  29. g.reduceClipRegion (content);
  30. g.setOrigin (content.getPosition());
  31. paintContent (g, content.getWidth(), content.getHeight());
  32. }
  33. void BubbleComponent::setAllowedPlacement (const int newPlacement)
  34. {
  35. allowablePlacements = newPlacement;
  36. }
  37. //==============================================================================
  38. void BubbleComponent::setPosition (Component* componentToPointTo, int distanceFromTarget, int arrowLength)
  39. {
  40. jassert (componentToPointTo != nullptr);
  41. Rectangle<int> target;
  42. if (Component* p = getParentComponent())
  43. target = p->getLocalArea (componentToPointTo, componentToPointTo->getLocalBounds());
  44. else
  45. target = componentToPointTo->getScreenBounds();
  46. setPosition (target, distanceFromTarget, arrowLength);
  47. }
  48. void BubbleComponent::setPosition (Point<int> arrowTipPos, int arrowLength)
  49. {
  50. setPosition (Rectangle<int> (arrowTipPos.x, arrowTipPos.y, 1, 1), arrowLength, arrowLength);
  51. }
  52. void BubbleComponent::setPosition (Rectangle<int> rectangleToPointTo,
  53. int distanceFromTarget, int arrowLength)
  54. {
  55. {
  56. int contentW = 150, contentH = 30;
  57. getContentSize (contentW, contentH);
  58. content.setBounds (distanceFromTarget, distanceFromTarget, contentW, contentH);
  59. }
  60. const int totalW = content.getWidth() + distanceFromTarget * 2;
  61. const int totalH = content.getHeight() + distanceFromTarget * 2;
  62. const Rectangle<int> availableSpace (getParentComponent() != nullptr ? getParentComponent()->getLocalBounds()
  63. : getParentMonitorArea());
  64. int spaceAbove = ((allowablePlacements & above) != 0) ? jmax (0, rectangleToPointTo.getY() - availableSpace.getY()) : -1;
  65. int spaceBelow = ((allowablePlacements & below) != 0) ? jmax (0, availableSpace.getBottom() - rectangleToPointTo.getBottom()) : -1;
  66. int spaceLeft = ((allowablePlacements & left) != 0) ? jmax (0, rectangleToPointTo.getX() - availableSpace.getX()) : -1;
  67. int spaceRight = ((allowablePlacements & right) != 0) ? jmax (0, availableSpace.getRight() - rectangleToPointTo.getRight()) : -1;
  68. // look at whether the component is elongated, and if so, try to position next to its longer dimension.
  69. if (rectangleToPointTo.getWidth() > rectangleToPointTo.getHeight() * 2
  70. && (spaceAbove > totalH + 20 || spaceBelow > totalH + 20))
  71. {
  72. spaceLeft = spaceRight = 0;
  73. }
  74. else if (rectangleToPointTo.getWidth() < rectangleToPointTo.getHeight() / 2
  75. && (spaceLeft > totalW + 20 || spaceRight > totalW + 20))
  76. {
  77. spaceAbove = spaceBelow = 0;
  78. }
  79. int targetX, targetY;
  80. if (jmax (spaceAbove, spaceBelow) >= jmax (spaceLeft, spaceRight))
  81. {
  82. targetX = rectangleToPointTo.getCentre().x;
  83. arrowTip.x = totalW / 2;
  84. if (spaceAbove >= spaceBelow)
  85. {
  86. // above
  87. targetY = rectangleToPointTo.getY();
  88. arrowTip.y = content.getBottom() + arrowLength;
  89. }
  90. else
  91. {
  92. // below
  93. targetY = rectangleToPointTo.getBottom();
  94. arrowTip.y = content.getY() - arrowLength;
  95. }
  96. }
  97. else
  98. {
  99. targetY = rectangleToPointTo.getCentre().y;
  100. arrowTip.y = totalH / 2;
  101. if (spaceLeft > spaceRight)
  102. {
  103. // on the left
  104. targetX = rectangleToPointTo.getX();
  105. arrowTip.x = content.getRight() + arrowLength;
  106. }
  107. else
  108. {
  109. // on the right
  110. targetX = rectangleToPointTo.getRight();
  111. arrowTip.x = content.getX() - arrowLength;
  112. }
  113. }
  114. setBounds (targetX - arrowTip.x, targetY - arrowTip.y, totalW, totalH);
  115. }