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_ImageButton.cpp 6.0KB

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