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 9.9KB

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