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.3KB

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