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.

147 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. BubbleComponent::BubbleComponent()
  20. : allowablePlacements (above | below | left | right)
  21. {
  22. setInterceptsMouseClicks (false, false);
  23. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.35f), 5, Point<int>()));
  24. setComponentEffect (&shadow);
  25. }
  26. BubbleComponent::~BubbleComponent() {}
  27. //==============================================================================
  28. void BubbleComponent::paint (Graphics& g)
  29. {
  30. getLookAndFeel().drawBubble (g, *this, arrowTip.toFloat(), content.toFloat());
  31. g.reduceClipRegion (content);
  32. g.setOrigin (content.getPosition());
  33. paintContent (g, content.getWidth(), content.getHeight());
  34. }
  35. void BubbleComponent::setAllowedPlacement (const int newPlacement)
  36. {
  37. allowablePlacements = newPlacement;
  38. }
  39. //==============================================================================
  40. void BubbleComponent::setPosition (Component* componentToPointTo, int distanceFromTarget, int arrowLength)
  41. {
  42. jassert (componentToPointTo != nullptr);
  43. Rectangle<int> target;
  44. if (Component* p = getParentComponent())
  45. target = p->getLocalArea (componentToPointTo, componentToPointTo->getLocalBounds());
  46. else
  47. target = componentToPointTo->getScreenBounds();
  48. setPosition (target, distanceFromTarget, arrowLength);
  49. }
  50. void BubbleComponent::setPosition (Point<int> arrowTipPos, int arrowLength)
  51. {
  52. setPosition (Rectangle<int> (arrowTipPos.x, arrowTipPos.y, 1, 1), arrowLength, arrowLength);
  53. }
  54. void BubbleComponent::setPosition (Rectangle<int> rectangleToPointTo,
  55. int distanceFromTarget, int arrowLength)
  56. {
  57. {
  58. int contentW = 150, contentH = 30;
  59. getContentSize (contentW, contentH);
  60. content.setBounds (distanceFromTarget, distanceFromTarget, contentW, contentH);
  61. }
  62. const int totalW = content.getWidth() + distanceFromTarget * 2;
  63. const int totalH = content.getHeight() + distanceFromTarget * 2;
  64. const Rectangle<int> availableSpace (getParentComponent() != nullptr ? getParentComponent()->getLocalBounds()
  65. : getParentMonitorArea());
  66. int spaceAbove = ((allowablePlacements & above) != 0) ? jmax (0, rectangleToPointTo.getY() - availableSpace.getY()) : -1;
  67. int spaceBelow = ((allowablePlacements & below) != 0) ? jmax (0, availableSpace.getBottom() - rectangleToPointTo.getBottom()) : -1;
  68. int spaceLeft = ((allowablePlacements & left) != 0) ? jmax (0, rectangleToPointTo.getX() - availableSpace.getX()) : -1;
  69. int spaceRight = ((allowablePlacements & right) != 0) ? jmax (0, availableSpace.getRight() - rectangleToPointTo.getRight()) : -1;
  70. // look at whether the component is elongated, and if so, try to position next to its longer dimension.
  71. if (rectangleToPointTo.getWidth() > rectangleToPointTo.getHeight() * 2
  72. && (spaceAbove > totalH + 20 || spaceBelow > totalH + 20))
  73. {
  74. spaceLeft = spaceRight = 0;
  75. }
  76. else if (rectangleToPointTo.getWidth() < rectangleToPointTo.getHeight() / 2
  77. && (spaceLeft > totalW + 20 || spaceRight > totalW + 20))
  78. {
  79. spaceAbove = spaceBelow = 0;
  80. }
  81. int targetX, targetY;
  82. if (jmax (spaceAbove, spaceBelow) >= jmax (spaceLeft, spaceRight))
  83. {
  84. targetX = rectangleToPointTo.getCentre().x;
  85. arrowTip.x = totalW / 2;
  86. if (spaceAbove >= spaceBelow)
  87. {
  88. // above
  89. targetY = rectangleToPointTo.getY();
  90. arrowTip.y = content.getBottom() + arrowLength;
  91. }
  92. else
  93. {
  94. // below
  95. targetY = rectangleToPointTo.getBottom();
  96. arrowTip.y = content.getY() - arrowLength;
  97. }
  98. }
  99. else
  100. {
  101. targetY = rectangleToPointTo.getCentre().y;
  102. arrowTip.y = totalH / 2;
  103. if (spaceLeft > spaceRight)
  104. {
  105. // on the left
  106. targetX = rectangleToPointTo.getX();
  107. arrowTip.x = content.getRight() + arrowLength;
  108. }
  109. else
  110. {
  111. // on the right
  112. targetX = rectangleToPointTo.getRight();
  113. arrowTip.x = content.getX() - arrowLength;
  114. }
  115. }
  116. setBounds (targetX - arrowTip.x, targetY - arrowTip.y, totalW, totalH);
  117. }