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_ProgressBar.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ProgressBar::ProgressBar (double& progress_)
  18. : progress (progress_),
  19. displayPercentage (true),
  20. lastCallbackTime (0)
  21. {
  22. currentValue = jlimit (0.0, 1.0, progress);
  23. }
  24. ProgressBar::~ProgressBar()
  25. {
  26. }
  27. //==============================================================================
  28. void ProgressBar::setPercentageDisplay (const bool shouldDisplayPercentage)
  29. {
  30. displayPercentage = shouldDisplayPercentage;
  31. repaint();
  32. }
  33. void ProgressBar::setTextToDisplay (const String& text)
  34. {
  35. displayPercentage = false;
  36. displayedMessage = text;
  37. }
  38. void ProgressBar::lookAndFeelChanged()
  39. {
  40. setOpaque (findColour (backgroundColourId).isOpaque());
  41. }
  42. void ProgressBar::colourChanged()
  43. {
  44. lookAndFeelChanged();
  45. }
  46. void ProgressBar::paint (Graphics& g)
  47. {
  48. String text;
  49. if (displayPercentage)
  50. {
  51. if (currentValue >= 0 && currentValue <= 1.0)
  52. text << roundToInt (currentValue * 100.0) << '%';
  53. }
  54. else
  55. {
  56. text = displayedMessage;
  57. }
  58. getLookAndFeel().drawProgressBar (g, *this,
  59. getWidth(), getHeight(),
  60. currentValue, text);
  61. }
  62. void ProgressBar::visibilityChanged()
  63. {
  64. if (isVisible())
  65. startTimer (30);
  66. else
  67. stopTimer();
  68. }
  69. void ProgressBar::timerCallback()
  70. {
  71. double newProgress = progress;
  72. const uint32 now = Time::getMillisecondCounter();
  73. const int timeSinceLastCallback = (int) (now - lastCallbackTime);
  74. lastCallbackTime = now;
  75. if (currentValue != newProgress
  76. || newProgress < 0 || newProgress >= 1.0
  77. || currentMessage != displayedMessage)
  78. {
  79. if (currentValue < newProgress
  80. && newProgress >= 0 && newProgress < 1.0
  81. && currentValue >= 0 && currentValue < 1.0)
  82. {
  83. newProgress = jmin (currentValue + 0.0008 * timeSinceLastCallback,
  84. newProgress);
  85. }
  86. currentValue = newProgress;
  87. currentMessage = displayedMessage;
  88. repaint();
  89. }
  90. }