juce_BubbleComponent.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. //==============================================================================
  22. /**
  23. A component for showing a message or other graphics inside a speech-bubble-shaped
  24. outline, pointing at a location on the screen.
  25. This is a base class that just draws and positions the bubble shape, but leaves
  26. the drawing of any content up to a subclass. See BubbleMessageComponent for a subclass
  27. that draws a text message.
  28. To use it, create your subclass, then either add it to a parent component or
  29. put it on the desktop with addToDesktop (0), use setPosition() to
  30. resize and position it, then make it visible.
  31. @see BubbleMessageComponent
  32. @tags{GUI}
  33. */
  34. class JUCE_API BubbleComponent : public Component
  35. {
  36. protected:
  37. //==============================================================================
  38. /** Creates a BubbleComponent.
  39. Your subclass will need to implement the getContentSize() and paintContent()
  40. methods to draw the bubble's contents.
  41. */
  42. BubbleComponent();
  43. public:
  44. /** Destructor. */
  45. ~BubbleComponent() override;
  46. //==============================================================================
  47. /** A list of permitted placements for the bubble, relative to the coordinates
  48. at which it should be pointing.
  49. @see setAllowedPlacement
  50. */
  51. enum BubblePlacement
  52. {
  53. above = 1,
  54. below = 2,
  55. left = 4,
  56. right = 8
  57. };
  58. /** Tells the bubble which positions it's allowed to put itself in, relative to the
  59. point at which it's pointing.
  60. By default when setPosition() is called, the bubble will place itself either
  61. above, below, left, or right of the target area. You can pass in a bitwise-'or' of
  62. the values in BubblePlacement to restrict this choice.
  63. E.g. if you only want your bubble to appear above or below the target area,
  64. use setAllowedPlacement (above | below);
  65. @see BubblePlacement
  66. */
  67. void setAllowedPlacement (int newPlacement);
  68. //==============================================================================
  69. /** Moves and resizes the bubble to point at a given component.
  70. This will resize the bubble to fit its content, then find a position for it
  71. so that it's next to, but doesn't overlap the given component.
  72. It'll put itself either above, below, or to the side of the component depending
  73. on where there's the most space, honouring any restrictions that were set
  74. with setAllowedPlacement().
  75. */
  76. void setPosition (Component* componentToPointTo,
  77. int distanceFromTarget = 15, int arrowLength = 10);
  78. /** Moves and resizes the bubble to point at a given point.
  79. This will resize the bubble to fit its content, then position it
  80. so that the tip of the bubble points to the given coordinate. The coordinates
  81. are relative to either the bubble component's parent component if it has one, or
  82. they are screen coordinates if not.
  83. It'll put itself either above, below, or to the side of this point, depending
  84. on where there's the most space, honouring any restrictions that were set
  85. with setAllowedPlacement().
  86. */
  87. void setPosition (Point<int> arrowTipPosition, int arrowLength = 10);
  88. /** Moves and resizes the bubble to point at a given rectangle.
  89. This will resize the bubble to fit its content, then find a position for it
  90. so that it's next to, but doesn't overlap the given rectangle. The rectangle's
  91. coordinates are relative to either the bubble component's parent component
  92. if it has one, or they are screen coordinates if not.
  93. It'll put itself either above, below, or to the side of the component depending
  94. on where there's the most space, honouring any restrictions that were set
  95. with setAllowedPlacement().
  96. distanceFromTarget is the amount of space to leave between the bubble and the
  97. target rectangle, and arrowLength is the length of the arrow that it will draw.
  98. */
  99. void setPosition (Rectangle<int> rectangleToPointTo,
  100. int distanceFromTarget = 15, int arrowLength = 10);
  101. //==============================================================================
  102. /** A set of colour IDs to use to change the colour of various aspects of the bubble component.
  103. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  104. methods.
  105. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  106. */
  107. enum ColourIds
  108. {
  109. backgroundColourId = 0x1000af0, /**< A background colour to fill the bubble with. */
  110. outlineColourId = 0x1000af1 /**< The colour to use for an outline around the bubble. */
  111. };
  112. //==============================================================================
  113. /** This abstract base class is implemented by LookAndFeel classes.
  114. */
  115. struct JUCE_API LookAndFeelMethods
  116. {
  117. virtual ~LookAndFeelMethods() = default;
  118. virtual void drawBubble (Graphics&, BubbleComponent&,
  119. const Point<float>& positionOfTip,
  120. const Rectangle<float>& body) = 0;
  121. };
  122. protected:
  123. //==============================================================================
  124. /** Subclasses should override this to return the size of the content they
  125. want to draw inside the bubble.
  126. */
  127. virtual void getContentSize (int& width, int& height) = 0;
  128. /** Subclasses should override this to draw their bubble's contents.
  129. The graphics object's clip region and the dimensions passed in here are
  130. set up to paint just the rectangle inside the bubble.
  131. */
  132. virtual void paintContent (Graphics& g, int width, int height) = 0;
  133. public:
  134. /** @internal */
  135. void paint (Graphics&) override;
  136. private:
  137. Rectangle<int> content;
  138. Point<int> arrowTip;
  139. int allowablePlacements;
  140. DropShadowEffect shadow;
  141. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BubbleComponent)
  142. };
  143. } // namespace juce