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

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