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.

190 lines
5.9KB

  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. ImageButton::ImageButton (const String& text_)
  18. : Button (text_),
  19. scaleImageToFit (true),
  20. preserveProportions (true),
  21. alphaThreshold (0)
  22. {
  23. }
  24. ImageButton::~ImageButton()
  25. {
  26. }
  27. void ImageButton::setImages (const bool resizeButtonNowToFitThisImage,
  28. const bool rescaleImagesWhenButtonSizeChanges,
  29. const bool preserveImageProportions,
  30. const Image& normalImage_,
  31. const float imageOpacityWhenNormal,
  32. Colour overlayColourWhenNormal,
  33. const Image& overImage_,
  34. const float imageOpacityWhenOver,
  35. Colour overlayColourWhenOver,
  36. const Image& downImage_,
  37. const float imageOpacityWhenDown,
  38. Colour overlayColourWhenDown,
  39. const float hitTestAlphaThreshold)
  40. {
  41. normalImage = normalImage_;
  42. overImage = overImage_;
  43. downImage = downImage_;
  44. if (resizeButtonNowToFitThisImage && normalImage.isValid())
  45. {
  46. imageBounds.setSize (normalImage.getWidth(),
  47. normalImage.getHeight());
  48. setSize (imageBounds.getWidth(), imageBounds.getHeight());
  49. }
  50. scaleImageToFit = rescaleImagesWhenButtonSizeChanges;
  51. preserveProportions = preserveImageProportions;
  52. normalOpacity = imageOpacityWhenNormal;
  53. normalOverlay = overlayColourWhenNormal;
  54. overOpacity = imageOpacityWhenOver;
  55. overOverlay = overlayColourWhenOver;
  56. downOpacity = imageOpacityWhenDown;
  57. downOverlay = overlayColourWhenDown;
  58. alphaThreshold = (uint8) jlimit (0, 0xff, roundToInt (255.0f * hitTestAlphaThreshold));
  59. repaint();
  60. }
  61. Image ImageButton::getCurrentImage() const
  62. {
  63. if (isDown() || getToggleState())
  64. return getDownImage();
  65. if (isOver())
  66. return getOverImage();
  67. return getNormalImage();
  68. }
  69. Image ImageButton::getNormalImage() const
  70. {
  71. return normalImage;
  72. }
  73. Image ImageButton::getOverImage() const
  74. {
  75. return overImage.isValid() ? overImage
  76. : normalImage;
  77. }
  78. Image ImageButton::getDownImage() const
  79. {
  80. return downImage.isValid() ? downImage
  81. : getOverImage();
  82. }
  83. void ImageButton::paintButton (Graphics& g,
  84. bool isMouseOverButton,
  85. bool isButtonDown)
  86. {
  87. if (! isEnabled())
  88. {
  89. isMouseOverButton = false;
  90. isButtonDown = false;
  91. }
  92. Image im (getCurrentImage());
  93. if (im.isValid())
  94. {
  95. const int iw = im.getWidth();
  96. const int ih = im.getHeight();
  97. int w = getWidth();
  98. int h = getHeight();
  99. int x = (w - iw) / 2;
  100. int y = (h - ih) / 2;
  101. if (scaleImageToFit)
  102. {
  103. if (preserveProportions)
  104. {
  105. int newW, newH;
  106. const float imRatio = ih / (float) iw;
  107. const float destRatio = h / (float) w;
  108. if (imRatio > destRatio)
  109. {
  110. newW = roundToInt (h / imRatio);
  111. newH = h;
  112. }
  113. else
  114. {
  115. newW = w;
  116. newH = roundToInt (w * imRatio);
  117. }
  118. x = (w - newW) / 2;
  119. y = (h - newH) / 2;
  120. w = newW;
  121. h = newH;
  122. }
  123. else
  124. {
  125. x = 0;
  126. y = 0;
  127. }
  128. }
  129. if (! scaleImageToFit)
  130. {
  131. w = iw;
  132. h = ih;
  133. }
  134. imageBounds.setBounds (x, y, w, h);
  135. const bool useDownImage = isButtonDown || getToggleState();
  136. getLookAndFeel().drawImageButton (g, &im, x, y, w, h,
  137. useDownImage ? downOverlay
  138. : (isMouseOverButton ? overOverlay
  139. : normalOverlay),
  140. useDownImage ? downOpacity
  141. : (isMouseOverButton ? overOpacity
  142. : normalOpacity),
  143. *this);
  144. }
  145. }
  146. bool ImageButton::hitTest (int x, int y)
  147. {
  148. if (alphaThreshold == 0)
  149. return true;
  150. Image im (getCurrentImage());
  151. return im.isNull() || ((! imageBounds.isEmpty())
  152. && alphaThreshold < im.getPixelAt (((x - imageBounds.getX()) * im.getWidth()) / imageBounds.getWidth(),
  153. ((y - imageBounds.getY()) * im.getHeight()) / imageBounds.getHeight()).getAlpha());
  154. }