The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

196 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. ImageButton::ImageButton (const String& text_)
  21. : Button (text_),
  22. scaleImageToFit (true),
  23. preserveProportions (true),
  24. alphaThreshold (0),
  25. imageX (0),
  26. imageY (0),
  27. imageW (0),
  28. imageH (0)
  29. {
  30. }
  31. ImageButton::~ImageButton()
  32. {
  33. }
  34. void ImageButton::setImages (const bool resizeButtonNowToFitThisImage,
  35. const bool rescaleImagesWhenButtonSizeChanges,
  36. const bool preserveImageProportions,
  37. const Image& normalImage_,
  38. const float imageOpacityWhenNormal,
  39. const Colour& overlayColourWhenNormal,
  40. const Image& overImage_,
  41. const float imageOpacityWhenOver,
  42. const Colour& overlayColourWhenOver,
  43. const Image& downImage_,
  44. const float imageOpacityWhenDown,
  45. const Colour& overlayColourWhenDown,
  46. const float hitTestAlphaThreshold)
  47. {
  48. normalImage = normalImage_;
  49. overImage = overImage_;
  50. downImage = downImage_;
  51. if (resizeButtonNowToFitThisImage && normalImage.isValid())
  52. {
  53. imageW = normalImage.getWidth();
  54. imageH = normalImage.getHeight();
  55. setSize (imageW, imageH);
  56. }
  57. scaleImageToFit = rescaleImagesWhenButtonSizeChanges;
  58. preserveProportions = preserveImageProportions;
  59. normalOpacity = imageOpacityWhenNormal;
  60. normalOverlay = overlayColourWhenNormal;
  61. overOpacity = imageOpacityWhenOver;
  62. overOverlay = overlayColourWhenOver;
  63. downOpacity = imageOpacityWhenDown;
  64. downOverlay = overlayColourWhenDown;
  65. alphaThreshold = (unsigned char) jlimit (0, 0xff, roundToInt (255.0f * hitTestAlphaThreshold));
  66. repaint();
  67. }
  68. Image ImageButton::getCurrentImage() const
  69. {
  70. if (isDown() || getToggleState())
  71. return getDownImage();
  72. if (isOver())
  73. return getOverImage();
  74. return getNormalImage();
  75. }
  76. Image ImageButton::getNormalImage() const
  77. {
  78. return normalImage;
  79. }
  80. Image ImageButton::getOverImage() const
  81. {
  82. return overImage.isValid() ? overImage
  83. : normalImage;
  84. }
  85. Image ImageButton::getDownImage() const
  86. {
  87. return downImage.isValid() ? downImage
  88. : getOverImage();
  89. }
  90. void ImageButton::paintButton (Graphics& g,
  91. bool isMouseOverButton,
  92. bool isButtonDown)
  93. {
  94. if (! isEnabled())
  95. {
  96. isMouseOverButton = false;
  97. isButtonDown = false;
  98. }
  99. Image im (getCurrentImage());
  100. if (im.isValid())
  101. {
  102. const int iw = im.getWidth();
  103. const int ih = im.getHeight();
  104. imageW = getWidth();
  105. imageH = getHeight();
  106. imageX = (imageW - iw) >> 1;
  107. imageY = (imageH - ih) >> 1;
  108. if (scaleImageToFit)
  109. {
  110. if (preserveProportions)
  111. {
  112. int newW, newH;
  113. const float imRatio = ih / (float)iw;
  114. const float destRatio = imageH / (float)imageW;
  115. if (imRatio > destRatio)
  116. {
  117. newW = roundToInt (imageH / imRatio);
  118. newH = imageH;
  119. }
  120. else
  121. {
  122. newW = imageW;
  123. newH = roundToInt (imageW * imRatio);
  124. }
  125. imageX = (imageW - newW) / 2;
  126. imageY = (imageH - newH) / 2;
  127. imageW = newW;
  128. imageH = newH;
  129. }
  130. else
  131. {
  132. imageX = 0;
  133. imageY = 0;
  134. }
  135. }
  136. if (! scaleImageToFit)
  137. {
  138. imageW = iw;
  139. imageH = ih;
  140. }
  141. getLookAndFeel().drawImageButton (g, &im, imageX, imageY, imageW, imageH,
  142. isButtonDown ? downOverlay
  143. : (isMouseOverButton ? overOverlay
  144. : normalOverlay),
  145. isButtonDown ? downOpacity
  146. : (isMouseOverButton ? overOpacity
  147. : normalOpacity),
  148. *this);
  149. }
  150. }
  151. bool ImageButton::hitTest (int x, int y)
  152. {
  153. if (alphaThreshold == 0)
  154. return true;
  155. Image im (getCurrentImage());
  156. return im.isNull() || (imageW > 0 && imageH > 0
  157. && alphaThreshold < im.getPixelAt (((x - imageX) * im.getWidth()) / imageW,
  158. ((y - imageY) * im.getHeight()) / imageH).getAlpha());
  159. }
  160. END_JUCE_NAMESPACE