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.h 8.1KB

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