juce_BubbleComponent.cpp 5.3 KB

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