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_BubbleMessageComponent.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. BubbleMessageComponent::BubbleMessageComponent (int fadeOutLengthMs)
  18. : fadeOutLength (fadeOutLengthMs), mouseClickCounter (0),
  19. expiryTime (0), deleteAfterUse (false)
  20. {
  21. }
  22. BubbleMessageComponent::~BubbleMessageComponent()
  23. {
  24. }
  25. void BubbleMessageComponent::showAt (const Rectangle<int>& pos,
  26. const AttributedString& text,
  27. const int numMillisecondsBeforeRemoving,
  28. const bool removeWhenMouseClicked,
  29. const bool deleteSelfAfterUse)
  30. {
  31. createLayout (text);
  32. setPosition (pos);
  33. init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
  34. }
  35. void BubbleMessageComponent::showAt (Component* const component,
  36. const AttributedString& text,
  37. const int numMillisecondsBeforeRemoving,
  38. const bool removeWhenMouseClicked,
  39. const bool deleteSelfAfterUse)
  40. {
  41. createLayout (text);
  42. setPosition (component);
  43. init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
  44. }
  45. void BubbleMessageComponent::createLayout (const AttributedString& text)
  46. {
  47. textLayout.createLayoutWithBalancedLineLengths (text, 256);
  48. }
  49. void BubbleMessageComponent::init (const int numMillisecondsBeforeRemoving,
  50. const bool removeWhenMouseClicked,
  51. const bool deleteSelfAfterUse)
  52. {
  53. setAlpha (1.0f);
  54. setVisible (true);
  55. deleteAfterUse = deleteSelfAfterUse;
  56. expiryTime = numMillisecondsBeforeRemoving > 0
  57. ? (Time::getMillisecondCounter() + (uint32) numMillisecondsBeforeRemoving) : 0;
  58. mouseClickCounter = Desktop::getInstance().getMouseButtonClickCounter();
  59. if (! (removeWhenMouseClicked && isShowing()))
  60. mouseClickCounter += 0xfffff;
  61. startTimer (77);
  62. repaint();
  63. }
  64. const float bubblePaddingX = 20.0f;
  65. const float bubblePaddingY = 14.0f;
  66. void BubbleMessageComponent::getContentSize (int& w, int& h)
  67. {
  68. w = (int) (bubblePaddingX + textLayout.getWidth());
  69. h = (int) (bubblePaddingY + textLayout.getHeight());
  70. }
  71. void BubbleMessageComponent::paintContent (Graphics& g, int w, int h)
  72. {
  73. g.setColour (findColour (TooltipWindow::textColourId));
  74. textLayout.draw (g, Rectangle<float> (bubblePaddingX / 2.0f, bubblePaddingY / 2.0f,
  75. w - bubblePaddingX, h - bubblePaddingY));
  76. }
  77. void BubbleMessageComponent::timerCallback()
  78. {
  79. if (Desktop::getInstance().getMouseButtonClickCounter() > mouseClickCounter)
  80. hide (false);
  81. else if (expiryTime != 0 && Time::getMillisecondCounter() > expiryTime)
  82. hide (true);
  83. }
  84. void BubbleMessageComponent::hide (const bool fadeOut)
  85. {
  86. stopTimer();
  87. if (fadeOut)
  88. Desktop::getInstance().getAnimator().fadeOut (this, fadeOutLength);
  89. else
  90. setVisible (false);
  91. if (deleteAfterUse)
  92. delete this;
  93. }