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.

151 lines
5.3KB

  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. BubbleComponent::BubbleComponent()
  21. : allowablePlacements (above | below | left | right)
  22. {
  23. setInterceptsMouseClicks (false, false);
  24. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.35f), 5, Point<int>()));
  25. setComponentEffect (&shadow);
  26. }
  27. BubbleComponent::~BubbleComponent() {}
  28. //==============================================================================
  29. void BubbleComponent::paint (Graphics& g)
  30. {
  31. getLookAndFeel().drawBubble (g, *this, arrowTip.toFloat(), content.toFloat());
  32. g.reduceClipRegion (content);
  33. g.setOrigin (content.getPosition());
  34. paintContent (g, content.getWidth(), content.getHeight());
  35. }
  36. void BubbleComponent::setAllowedPlacement (const int newPlacement)
  37. {
  38. allowablePlacements = newPlacement;
  39. }
  40. //==============================================================================
  41. void BubbleComponent::setPosition (Component* componentToPointTo, int distanceFromTarget, int arrowLength)
  42. {
  43. jassert (componentToPointTo != nullptr);
  44. Rectangle<int> target;
  45. if (Component* p = getParentComponent())
  46. target = p->getLocalArea (componentToPointTo, componentToPointTo->getLocalBounds());
  47. else
  48. target = componentToPointTo->getScreenBounds().transformedBy (getTransform().inverted());
  49. setPosition (target, distanceFromTarget, arrowLength);
  50. }
  51. void BubbleComponent::setPosition (Point<int> arrowTipPos, int arrowLength)
  52. {
  53. setPosition (Rectangle<int> (arrowTipPos.x, arrowTipPos.y, 1, 1), arrowLength, arrowLength);
  54. }
  55. void BubbleComponent::setPosition (Rectangle<int> rectangleToPointTo,
  56. int distanceFromTarget, int arrowLength)
  57. {
  58. {
  59. int contentW = 150, contentH = 30;
  60. getContentSize (contentW, contentH);
  61. content.setBounds (distanceFromTarget, distanceFromTarget, contentW, contentH);
  62. }
  63. const int totalW = content.getWidth() + distanceFromTarget * 2;
  64. const int totalH = content.getHeight() + distanceFromTarget * 2;
  65. auto availableSpace = (getParentComponent() != nullptr ? getParentComponent()->getLocalBounds()
  66. : getParentMonitorArea().transformedBy (getTransform().inverted()));
  67. int spaceAbove = ((allowablePlacements & above) != 0) ? jmax (0, rectangleToPointTo.getY() - availableSpace.getY()) : -1;
  68. int spaceBelow = ((allowablePlacements & below) != 0) ? jmax (0, availableSpace.getBottom() - rectangleToPointTo.getBottom()) : -1;
  69. int spaceLeft = ((allowablePlacements & left) != 0) ? jmax (0, rectangleToPointTo.getX() - availableSpace.getX()) : -1;
  70. int spaceRight = ((allowablePlacements & right) != 0) ? jmax (0, availableSpace.getRight() - rectangleToPointTo.getRight()) : -1;
  71. // look at whether the component is elongated, and if so, try to position next to its longer dimension.
  72. if (rectangleToPointTo.getWidth() > rectangleToPointTo.getHeight() * 2
  73. && (spaceAbove > totalH + 20 || spaceBelow > totalH + 20))
  74. {
  75. spaceLeft = spaceRight = 0;
  76. }
  77. else if (rectangleToPointTo.getWidth() < rectangleToPointTo.getHeight() / 2
  78. && (spaceLeft > totalW + 20 || spaceRight > totalW + 20))
  79. {
  80. spaceAbove = spaceBelow = 0;
  81. }
  82. int targetX, targetY;
  83. if (jmax (spaceAbove, spaceBelow) >= jmax (spaceLeft, spaceRight))
  84. {
  85. targetX = rectangleToPointTo.getCentre().x;
  86. arrowTip.x = totalW / 2;
  87. if (spaceAbove >= spaceBelow)
  88. {
  89. // above
  90. targetY = rectangleToPointTo.getY();
  91. arrowTip.y = content.getBottom() + arrowLength;
  92. }
  93. else
  94. {
  95. // below
  96. targetY = rectangleToPointTo.getBottom();
  97. arrowTip.y = content.getY() - arrowLength;
  98. }
  99. }
  100. else
  101. {
  102. targetY = rectangleToPointTo.getCentre().y;
  103. arrowTip.y = totalH / 2;
  104. if (spaceLeft > spaceRight)
  105. {
  106. // on the left
  107. targetX = rectangleToPointTo.getX();
  108. arrowTip.x = content.getRight() + arrowLength;
  109. }
  110. else
  111. {
  112. // on the right
  113. targetX = rectangleToPointTo.getRight();
  114. arrowTip.x = content.getX() - arrowLength;
  115. }
  116. }
  117. setBounds (targetX - arrowTip.x, targetY - arrowTip.y, totalW, totalH);
  118. }
  119. } // namespace juce