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.

157 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #pragma once
  18. //==============================================================================
  19. /**
  20. As the title suggests, this is a button containing an image.
  21. The colour and transparency of the image can be set to vary when the
  22. button state changes.
  23. @see Button, ShapeButton, TextButton
  24. */
  25. class JUCE_API ImageButton : public Button
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates an ImageButton.
  30. Use setImage() to specify the image to use. The colours and opacities that
  31. are specified here can be changed later using setImages().
  32. @param name the name to give the component
  33. */
  34. explicit ImageButton (const String& name = String());
  35. /** Destructor. */
  36. ~ImageButton();
  37. //==============================================================================
  38. /** Sets up the images to draw in various states.
  39. @param resizeButtonNowToFitThisImage if true, the button will be immediately
  40. resized to the same dimensions as the normal image
  41. @param rescaleImagesWhenButtonSizeChanges if true, the image will be rescaled to fit the
  42. button when the button's size changes
  43. @param preserveImageProportions if true then any rescaling of the image to fit
  44. the button will keep the image's x and y proportions
  45. correct - i.e. it won't distort its shape, although
  46. this might create gaps around the edges
  47. @param normalImage the image to use when the button is in its normal state.
  48. button no longer needs it.
  49. @param imageOpacityWhenNormal the opacity to use when drawing the normal image.
  50. @param overlayColourWhenNormal an overlay colour to use to fill the alpha channel of the
  51. normal image - if this colour is transparent, no overlay
  52. will be drawn. The overlay will be drawn over the top of the
  53. image, so you can basically add a solid or semi-transparent
  54. colour to the image to brighten or darken it
  55. @param overImage the image to use when the mouse is over the button. If
  56. you want to use the same image as was set in the normalImage
  57. parameter, this value can be a null image.
  58. @param imageOpacityWhenOver the opacity to use when drawing the image when the mouse
  59. is over the button
  60. @param overlayColourWhenOver an overlay colour to use to fill the alpha channel of the
  61. image when the mouse is over - if this colour is transparent,
  62. no overlay will be drawn
  63. @param downImage an image to use when the button is pressed down. If set
  64. to a null image, the 'over' image will be drawn instead (or the
  65. normal image if there isn't an 'over' image either).
  66. @param imageOpacityWhenDown the opacity to use when drawing the image when the button
  67. is pressed
  68. @param overlayColourWhenDown an overlay colour to use to fill the alpha channel of the
  69. image when the button is pressed down - if this colour is
  70. transparent, no overlay will be drawn
  71. @param hitTestAlphaThreshold if set to zero, the mouse is considered to be over the button
  72. whenever it's inside the button's bounding rectangle. If
  73. set to values higher than 0, the mouse will only be
  74. considered to be over the image when the value of the
  75. image's alpha channel at that position is greater than
  76. this level.
  77. */
  78. void setImages (bool resizeButtonNowToFitThisImage,
  79. bool rescaleImagesWhenButtonSizeChanges,
  80. bool preserveImageProportions,
  81. const Image& normalImage,
  82. float imageOpacityWhenNormal,
  83. Colour overlayColourWhenNormal,
  84. const Image& overImage,
  85. float imageOpacityWhenOver,
  86. Colour overlayColourWhenOver,
  87. const Image& downImage,
  88. float imageOpacityWhenDown,
  89. Colour overlayColourWhenDown,
  90. float hitTestAlphaThreshold = 0.0f);
  91. /** Returns the currently set 'normal' image. */
  92. Image getNormalImage() const;
  93. /** Returns the image that's drawn when the mouse is over the button.
  94. If a valid 'over' image has been set, this will return it; otherwise it'll
  95. just return the normal image.
  96. */
  97. Image getOverImage() const;
  98. /** Returns the image that's drawn when the button is held down.
  99. If a valid 'down' image has been set, this will return it; otherwise it'll
  100. return the 'over' image or normal image, depending on what's available.
  101. */
  102. Image getDownImage() const;
  103. //==============================================================================
  104. /** This abstract base class is implemented by LookAndFeel classes. */
  105. struct JUCE_API LookAndFeelMethods
  106. {
  107. virtual ~LookAndFeelMethods() {}
  108. virtual void drawImageButton (Graphics&, Image*,
  109. int imageX, int imageY, int imageW, int imageH,
  110. const Colour& overlayColour, float imageOpacity, ImageButton&) = 0;
  111. };
  112. protected:
  113. //==============================================================================
  114. /** @internal */
  115. bool hitTest (int x, int y) override;
  116. /** @internal */
  117. void paintButton (Graphics&, bool isMouseOver, bool isButtonDown) override;
  118. private:
  119. //==============================================================================
  120. bool scaleImageToFit, preserveProportions;
  121. uint8 alphaThreshold;
  122. Rectangle<int> imageBounds;
  123. Image normalImage, overImage, downImage;
  124. float normalOpacity, overOpacity, downOpacity;
  125. Colour normalOverlay, overOverlay, downOverlay;
  126. Image getCurrentImage() const;
  127. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ImageButton)
  128. };