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_BubbleComponent.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. void BubbleComponent::setPosition (Component* componentToPointTo)
  38. {
  39. jassert (componentToPointTo != nullptr);
  40. if (Component* p = getParentComponent())
  41. setPosition (p->getLocalArea (componentToPointTo, componentToPointTo->getLocalBounds()));
  42. else
  43. setPosition (componentToPointTo->getScreenBounds());
  44. }
  45. void BubbleComponent::setPosition (Point<int> pos)
  46. {
  47. setPosition (Rectangle<int> (pos.x, pos.y, 1, 1));
  48. }
  49. //==============================================================================
  50. void BubbleComponent::setPosition (const Rectangle<int>& rectangleToPointTo)
  51. {
  52. const int edgeSpace = 15;
  53. const int arrowLength = 10;
  54. {
  55. int contentW = 150, contentH = 30;
  56. getContentSize (contentW, contentH);
  57. content.setBounds (edgeSpace, edgeSpace, contentW, contentH);
  58. }
  59. const int totalW = content.getWidth() + edgeSpace * 2;
  60. const int totalH = content.getHeight() + edgeSpace * 2;
  61. const Rectangle<int> availableSpace (getParentComponent() != nullptr ? getParentComponent()->getLocalBounds()
  62. : getParentMonitorArea());
  63. int spaceAbove = ((allowablePlacements & above) != 0) ? jmax (0, rectangleToPointTo.getY() - availableSpace.getY()) : -1;
  64. int spaceBelow = ((allowablePlacements & below) != 0) ? jmax (0, availableSpace.getBottom() - rectangleToPointTo.getBottom()) : -1;
  65. int spaceLeft = ((allowablePlacements & left) != 0) ? jmax (0, rectangleToPointTo.getX() - availableSpace.getX()) : -1;
  66. int spaceRight = ((allowablePlacements & right) != 0) ? jmax (0, availableSpace.getRight() - rectangleToPointTo.getRight()) : -1;
  67. // look at whether the component is elongated, and if so, try to position next to its longer dimension.
  68. if (rectangleToPointTo.getWidth() > rectangleToPointTo.getHeight() * 2
  69. && (spaceAbove > totalH + 20 || spaceBelow > totalH + 20))
  70. {
  71. spaceLeft = spaceRight = 0;
  72. }
  73. else if (rectangleToPointTo.getWidth() < rectangleToPointTo.getHeight() / 2
  74. && (spaceLeft > totalW + 20 || spaceRight > totalW + 20))
  75. {
  76. spaceAbove = spaceBelow = 0;
  77. }
  78. int targetX, targetY;
  79. if (jmax (spaceAbove, spaceBelow) >= jmax (spaceLeft, spaceRight))
  80. {
  81. targetX = rectangleToPointTo.getCentre().x;
  82. arrowTip.x = totalW / 2;
  83. if (spaceAbove >= spaceBelow)
  84. {
  85. // above
  86. targetY = rectangleToPointTo.getY();
  87. arrowTip.y = content.getBottom() + arrowLength;
  88. }
  89. else
  90. {
  91. // below
  92. targetY = rectangleToPointTo.getBottom();
  93. arrowTip.y = content.getY() - arrowLength;
  94. }
  95. }
  96. else
  97. {
  98. targetY = rectangleToPointTo.getCentre().y;
  99. arrowTip.y = totalH / 2;
  100. if (spaceLeft > spaceRight)
  101. {
  102. // on the left
  103. targetX = rectangleToPointTo.getX();
  104. arrowTip.x = content.getRight() + arrowLength;
  105. }
  106. else
  107. {
  108. // on the right
  109. targetX = rectangleToPointTo.getRight();
  110. arrowTip.x = content.getX() - arrowLength;
  111. }
  112. }
  113. setBounds (targetX - arrowTip.x, targetY - arrowTip.y, totalW, totalH);
  114. }