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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. ProgressBar::ProgressBar (double& progress_)
  21. : progress (progress_),
  22. displayPercentage (true),
  23. lastCallbackTime (0)
  24. {
  25. currentValue = jlimit (0.0, 1.0, progress);
  26. }
  27. ProgressBar::~ProgressBar()
  28. {
  29. }
  30. //==============================================================================
  31. void ProgressBar::setPercentageDisplay (const bool shouldDisplayPercentage)
  32. {
  33. displayPercentage = shouldDisplayPercentage;
  34. repaint();
  35. }
  36. void ProgressBar::setTextToDisplay (const String& text)
  37. {
  38. displayPercentage = false;
  39. displayedMessage = text;
  40. }
  41. void ProgressBar::lookAndFeelChanged()
  42. {
  43. setOpaque (getLookAndFeel().isProgressBarOpaque (*this));
  44. }
  45. void ProgressBar::colourChanged()
  46. {
  47. lookAndFeelChanged();
  48. }
  49. void ProgressBar::paint (Graphics& g)
  50. {
  51. String text;
  52. if (displayPercentage)
  53. {
  54. if (currentValue >= 0 && currentValue <= 1.0)
  55. text << roundToInt (currentValue * 100.0) << '%';
  56. }
  57. else
  58. {
  59. text = displayedMessage;
  60. }
  61. getLookAndFeel().drawProgressBar (g, *this,
  62. getWidth(), getHeight(),
  63. currentValue, text);
  64. }
  65. void ProgressBar::visibilityChanged()
  66. {
  67. if (isVisible())
  68. startTimer (30);
  69. else
  70. stopTimer();
  71. }
  72. void ProgressBar::timerCallback()
  73. {
  74. double newProgress = progress;
  75. const uint32 now = Time::getMillisecondCounter();
  76. const int timeSinceLastCallback = (int) (now - lastCallbackTime);
  77. lastCallbackTime = now;
  78. if (currentValue != newProgress
  79. || newProgress < 0 || newProgress >= 1.0
  80. || currentMessage != displayedMessage)
  81. {
  82. if (currentValue < newProgress
  83. && newProgress >= 0 && newProgress < 1.0
  84. && currentValue >= 0 && currentValue < 1.0)
  85. {
  86. newProgress = jmin (currentValue + 0.0008 * timeSinceLastCallback,
  87. newProgress);
  88. }
  89. currentValue = newProgress;
  90. currentMessage = displayedMessage;
  91. repaint();
  92. }
  93. }
  94. } // namespace juce