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.

192 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. ImageButton::ImageButton (const String& text_)
  16. : Button (text_),
  17. scaleImageToFit (true),
  18. preserveProportions (true),
  19. alphaThreshold (0)
  20. {
  21. }
  22. ImageButton::~ImageButton()
  23. {
  24. }
  25. void ImageButton::setImages (const bool resizeButtonNowToFitThisImage,
  26. const bool rescaleImagesWhenButtonSizeChanges,
  27. const bool preserveImageProportions,
  28. const Image& normalImage_,
  29. const float imageOpacityWhenNormal,
  30. Colour overlayColourWhenNormal,
  31. const Image& overImage_,
  32. const float imageOpacityWhenOver,
  33. Colour overlayColourWhenOver,
  34. const Image& downImage_,
  35. const float imageOpacityWhenDown,
  36. Colour overlayColourWhenDown,
  37. const float hitTestAlphaThreshold)
  38. {
  39. normalImage = normalImage_;
  40. overImage = overImage_;
  41. downImage = downImage_;
  42. if (resizeButtonNowToFitThisImage && normalImage.isValid())
  43. {
  44. imageBounds.setSize (normalImage.getWidth(),
  45. normalImage.getHeight());
  46. setSize (imageBounds.getWidth(), imageBounds.getHeight());
  47. }
  48. scaleImageToFit = rescaleImagesWhenButtonSizeChanges;
  49. preserveProportions = preserveImageProportions;
  50. normalOpacity = imageOpacityWhenNormal;
  51. normalOverlay = overlayColourWhenNormal;
  52. overOpacity = imageOpacityWhenOver;
  53. overOverlay = overlayColourWhenOver;
  54. downOpacity = imageOpacityWhenDown;
  55. downOverlay = overlayColourWhenDown;
  56. alphaThreshold = (uint8) jlimit (0, 0xff, roundToInt (255.0f * hitTestAlphaThreshold));
  57. repaint();
  58. }
  59. Image ImageButton::getCurrentImage() const
  60. {
  61. if (isDown() || getToggleState())
  62. return getDownImage();
  63. if (isOver())
  64. return getOverImage();
  65. return getNormalImage();
  66. }
  67. Image ImageButton::getNormalImage() const
  68. {
  69. return normalImage;
  70. }
  71. Image ImageButton::getOverImage() const
  72. {
  73. return overImage.isValid() ? overImage
  74. : normalImage;
  75. }
  76. Image ImageButton::getDownImage() const
  77. {
  78. return downImage.isValid() ? downImage
  79. : getOverImage();
  80. }
  81. void ImageButton::paintButton (Graphics& g,
  82. bool shouldDrawButtonAsHighlighted,
  83. bool shouldDrawButtonAsDown)
  84. {
  85. if (! isEnabled())
  86. {
  87. shouldDrawButtonAsHighlighted = false;
  88. shouldDrawButtonAsDown = false;
  89. }
  90. Image im (getCurrentImage());
  91. if (im.isValid())
  92. {
  93. const int iw = im.getWidth();
  94. const int ih = im.getHeight();
  95. int w = getWidth();
  96. int h = getHeight();
  97. int x = (w - iw) / 2;
  98. int y = (h - ih) / 2;
  99. if (scaleImageToFit)
  100. {
  101. if (preserveProportions)
  102. {
  103. int newW, newH;
  104. const float imRatio = ih / (float) iw;
  105. const float destRatio = h / (float) w;
  106. if (imRatio > destRatio)
  107. {
  108. newW = roundToInt (h / imRatio);
  109. newH = h;
  110. }
  111. else
  112. {
  113. newW = w;
  114. newH = roundToInt (w * imRatio);
  115. }
  116. x = (w - newW) / 2;
  117. y = (h - newH) / 2;
  118. w = newW;
  119. h = newH;
  120. }
  121. else
  122. {
  123. x = 0;
  124. y = 0;
  125. }
  126. }
  127. if (! scaleImageToFit)
  128. {
  129. w = iw;
  130. h = ih;
  131. }
  132. imageBounds.setBounds (x, y, w, h);
  133. const bool useDownImage = shouldDrawButtonAsDown || getToggleState();
  134. getLookAndFeel().drawImageButton (g, &im, x, y, w, h,
  135. useDownImage ? downOverlay
  136. : (shouldDrawButtonAsHighlighted ? overOverlay
  137. : normalOverlay),
  138. useDownImage ? downOpacity
  139. : (shouldDrawButtonAsHighlighted ? overOpacity
  140. : normalOpacity),
  141. *this);
  142. }
  143. }
  144. bool ImageButton::hitTest (int x, int y)
  145. {
  146. if (! Component::hitTest (x, y)) // handle setInterceptsMouseClicks
  147. return false;
  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. }
  155. } // namespace juce