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.

161 lines
8.2KB

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