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_DrawableButton.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. A button that displays a Drawable.
  23. Up to three Drawable objects can be given to this button, to represent the
  24. 'normal', 'over' and 'down' states.
  25. @see Button
  26. @tags{GUI}
  27. */
  28. class JUCE_API DrawableButton : public Button
  29. {
  30. public:
  31. //==============================================================================
  32. enum ButtonStyle
  33. {
  34. ImageFitted, /**< The button will just display the images, but will resize and centre them to fit inside it. */
  35. ImageRaw, /**< The button will just display the images in their normal size and position.
  36. This leaves it up to the caller to make sure the images are the correct size and position for the button. */
  37. ImageAboveTextLabel, /**< Draws the button as a text label across the bottom with the image resized and scaled to fit above it. */
  38. ImageOnButtonBackground, /**< Draws the button as a standard rounded-rectangle button with the image on top. The image will be resized
  39. to match the button's proportions.
  40. Note that if you use this style, the colour IDs that control the button colour are
  41. TextButton::buttonColourId and TextButton::buttonOnColourId. */
  42. ImageOnButtonBackgroundOriginalSize, /** Same as ImageOnButtonBackground, but keeps the original image size. */
  43. ImageStretched /**< Fills the button with a stretched version of the image. */
  44. };
  45. //==============================================================================
  46. /** Creates a DrawableButton.
  47. After creating one of these, use setImages() to specify the drawables to use.
  48. @param buttonName the name to give the component
  49. @param buttonStyle the layout to use
  50. @see ButtonStyle, setButtonStyle, setImages
  51. */
  52. DrawableButton (const String& buttonName,
  53. ButtonStyle buttonStyle);
  54. /** Destructor. */
  55. ~DrawableButton() override;
  56. //==============================================================================
  57. /** Sets up the images to draw for the various button states.
  58. The button will keep its own internal copies of these drawables.
  59. @param normalImage the thing to draw for the button's 'normal' state. An internal copy
  60. will be made of the object passed-in if it is non-null.
  61. @param overImage the thing to draw for the button's 'over' state - if this is
  62. null, the button's normal image will be used when the mouse is
  63. over it. An internal copy will be made of the object passed-in
  64. if it is non-null.
  65. @param downImage the thing to draw for the button's 'down' state - if this is
  66. null, the 'over' image will be used instead (or the normal image
  67. as a last resort). An internal copy will be made of the object
  68. passed-in if it is non-null.
  69. @param disabledImage an image to draw when the button is disabled. If this is null,
  70. the normal image will be drawn with a reduced opacity instead.
  71. An internal copy will be made of the object passed-in if it is
  72. non-null.
  73. @param normalImageOn same as the normalImage, but this is used when the button's toggle
  74. state is 'on'. If this is nullptr, the normal image is used instead
  75. @param overImageOn same as the overImage, but this is used when the button's toggle
  76. state is 'on'. If this is nullptr, the normalImageOn is drawn instead
  77. @param downImageOn same as the downImage, but this is used when the button's toggle
  78. state is 'on'. If this is nullptr, the overImageOn is drawn instead
  79. @param disabledImageOn same as the disabledImage, but this is used when the button's toggle
  80. state is 'on'. If this is nullptr, the normal image will be drawn instead
  81. with a reduced opacity
  82. */
  83. void setImages (const Drawable* normalImage,
  84. const Drawable* overImage = nullptr,
  85. const Drawable* downImage = nullptr,
  86. const Drawable* disabledImage = nullptr,
  87. const Drawable* normalImageOn = nullptr,
  88. const Drawable* overImageOn = nullptr,
  89. const Drawable* downImageOn = nullptr,
  90. const Drawable* disabledImageOn = nullptr);
  91. //==============================================================================
  92. /** Changes the button's style.
  93. @see ButtonStyle
  94. */
  95. void setButtonStyle (ButtonStyle newStyle);
  96. /** Returns the current style. */
  97. ButtonStyle getStyle() const noexcept { return style; }
  98. //==============================================================================
  99. /** Gives the button an optional amount of space around the edge of the drawable.
  100. By default there's a gap of about 3 pixels.
  101. */
  102. void setEdgeIndent (int numPixelsIndent);
  103. /** Returns the current edge indent size. */
  104. int getEdgeIndent() const noexcept { return edgeIndent; }
  105. //==============================================================================
  106. /** Returns the image that the button is currently displaying. */
  107. Drawable* getCurrentImage() const noexcept;
  108. /** Returns the image that the button will use for its normal state. */
  109. Drawable* getNormalImage() const noexcept;
  110. /** Returns the image that the button will use when the mouse is over it. */
  111. Drawable* getOverImage() const noexcept;
  112. /** Returns the image that the button will use when the mouse is held down on it. */
  113. Drawable* getDownImage() const noexcept;
  114. /** Can be overridden to specify a custom position for the image within the button. */
  115. virtual Rectangle<float> getImageBounds() const;
  116. //==============================================================================
  117. /** A set of colour IDs to use to change the colour of various aspects of the link.
  118. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  119. methods.
  120. Note that when the ImageOnButtonBackground style is used, the colour IDs that control
  121. the button colour are TextButton::buttonColourId and TextButton::buttonOnColourId.
  122. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  123. */
  124. enum ColourIds
  125. {
  126. textColourId = 0x1004010, /**< The colour to use for the button's text label. */
  127. textColourOnId = 0x1004013, /**< The colour to use for the button's text when the button's toggle state is "on". */
  128. backgroundColourId = 0x1004011, /**< The colour used to fill the button's background (when
  129. the button is toggled 'off'). Note that if you use the
  130. ImageOnButtonBackground style, you should use TextButton::buttonColourId
  131. to change the button's colour. */
  132. backgroundOnColourId = 0x1004012, /**< The colour used to fill the button's background (when
  133. the button is toggled 'on'). Note that if you use the
  134. ImageOnButtonBackground style, you should use TextButton::buttonOnColourId
  135. to change the button's colour. */
  136. };
  137. //==============================================================================
  138. /** @internal */
  139. void paintButton (Graphics&, bool, bool) override;
  140. /** @internal */
  141. void buttonStateChanged() override;
  142. /** @internal */
  143. void resized() override;
  144. /** @internal */
  145. void enablementChanged() override;
  146. /** @internal */
  147. void colourChanged() override;
  148. private:
  149. //==============================================================================
  150. bool shouldDrawButtonBackground() const { return style == ImageOnButtonBackground || style == ImageOnButtonBackgroundOriginalSize; }
  151. //==============================================================================
  152. ButtonStyle style;
  153. std::unique_ptr<Drawable> normalImage, overImage, downImage, disabledImage,
  154. normalImageOn, overImageOn, downImageOn, disabledImageOn;
  155. Drawable* currentImage = nullptr;
  156. int edgeIndent = 3;
  157. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DrawableButton)
  158. };
  159. } // namespace juce