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.

223 lines
6.6KB

  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. DrawableButton::DrawableButton (const String& name, const DrawableButton::ButtonStyle buttonStyle)
  18. : Button (name),
  19. style (buttonStyle),
  20. currentImage (nullptr),
  21. edgeIndent (3)
  22. {
  23. }
  24. DrawableButton::~DrawableButton()
  25. {
  26. }
  27. //==============================================================================
  28. static Drawable* copyDrawableIfNotNull (const Drawable* const d)
  29. {
  30. return d != nullptr ? d->createCopy() : nullptr;
  31. }
  32. void DrawableButton::setImages (const Drawable* normal,
  33. const Drawable* over,
  34. const Drawable* down,
  35. const Drawable* disabled,
  36. const Drawable* normalOn,
  37. const Drawable* overOn,
  38. const Drawable* downOn,
  39. const Drawable* disabledOn)
  40. {
  41. jassert (normal != nullptr); // you really need to give it at least a normal image..
  42. normalImage = copyDrawableIfNotNull (normal);
  43. overImage = copyDrawableIfNotNull (over);
  44. downImage = copyDrawableIfNotNull (down);
  45. disabledImage = copyDrawableIfNotNull (disabled);
  46. normalImageOn = copyDrawableIfNotNull (normalOn);
  47. overImageOn = copyDrawableIfNotNull (overOn);
  48. downImageOn = copyDrawableIfNotNull (downOn);
  49. disabledImageOn = copyDrawableIfNotNull (disabledOn);
  50. currentImage = nullptr;
  51. buttonStateChanged();
  52. }
  53. //==============================================================================
  54. void DrawableButton::setButtonStyle (const DrawableButton::ButtonStyle newStyle)
  55. {
  56. if (style != newStyle)
  57. {
  58. style = newStyle;
  59. buttonStateChanged();
  60. }
  61. }
  62. void DrawableButton::setEdgeIndent (const int numPixelsIndent)
  63. {
  64. edgeIndent = numPixelsIndent;
  65. repaint();
  66. resized();
  67. }
  68. Rectangle<float> DrawableButton::getImageBounds() const
  69. {
  70. Rectangle<int> r (getLocalBounds());
  71. if (style != ImageStretched)
  72. {
  73. int indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
  74. int indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
  75. if (style == ImageOnButtonBackground)
  76. {
  77. indentX = jmax (getWidth() / 4, indentX);
  78. indentY = jmax (getHeight() / 4, indentY);
  79. }
  80. else if (style == ImageAboveTextLabel)
  81. {
  82. r = r.withTrimmedBottom (jmin (16, proportionOfHeight (0.25f)));
  83. }
  84. r = r.reduced (indentX, indentY);
  85. }
  86. return r.toFloat();
  87. }
  88. void DrawableButton::resized()
  89. {
  90. Button::resized();
  91. if (currentImage != nullptr)
  92. {
  93. if (style == ImageRaw)
  94. currentImage->setOriginWithOriginalSize (Point<float>());
  95. else
  96. currentImage->setTransformToFit (getImageBounds(),
  97. style == ImageStretched ? RectanglePlacement::stretchToFit
  98. : RectanglePlacement::centred);
  99. }
  100. }
  101. void DrawableButton::buttonStateChanged()
  102. {
  103. repaint();
  104. Drawable* imageToDraw = nullptr;
  105. float opacity = 1.0f;
  106. if (isEnabled())
  107. {
  108. imageToDraw = getCurrentImage();
  109. }
  110. else
  111. {
  112. imageToDraw = getToggleState() ? disabledImageOn
  113. : disabledImage;
  114. if (imageToDraw == nullptr)
  115. {
  116. opacity = 0.4f;
  117. imageToDraw = getNormalImage();
  118. }
  119. }
  120. if (imageToDraw != currentImage)
  121. {
  122. removeChildComponent (currentImage);
  123. currentImage = imageToDraw;
  124. if (currentImage != nullptr)
  125. {
  126. currentImage->setInterceptsMouseClicks (false, false);
  127. addAndMakeVisible (currentImage);
  128. resized();
  129. }
  130. }
  131. if (currentImage != nullptr)
  132. currentImage->setAlpha (opacity);
  133. }
  134. void DrawableButton::enablementChanged()
  135. {
  136. Button::enablementChanged();
  137. buttonStateChanged();
  138. }
  139. void DrawableButton::colourChanged()
  140. {
  141. repaint();
  142. }
  143. void DrawableButton::paintButton (Graphics& g,
  144. const bool isMouseOverButton,
  145. const bool isButtonDown)
  146. {
  147. LookAndFeel& lf = getLookAndFeel();
  148. if (style == ImageOnButtonBackground)
  149. lf.drawButtonBackground (g, *this,
  150. findColour (getToggleState() ? TextButton::buttonOnColourId
  151. : TextButton::buttonColourId),
  152. isMouseOverButton, isButtonDown);
  153. else
  154. lf.drawDrawableButton (g, *this, isMouseOverButton, isButtonDown);
  155. }
  156. //==============================================================================
  157. Drawable* DrawableButton::getCurrentImage() const noexcept
  158. {
  159. if (isDown()) return getDownImage();
  160. if (isOver()) return getOverImage();
  161. return getNormalImage();
  162. }
  163. Drawable* DrawableButton::getNormalImage() const noexcept
  164. {
  165. return (getToggleState() && normalImageOn != nullptr) ? normalImageOn
  166. : normalImage;
  167. }
  168. Drawable* DrawableButton::getOverImage() const noexcept
  169. {
  170. if (getToggleState())
  171. {
  172. if (overImageOn != nullptr) return overImageOn;
  173. if (normalImageOn != nullptr) return normalImageOn;
  174. }
  175. return overImage != nullptr ? overImage : normalImage;
  176. }
  177. Drawable* DrawableButton::getDownImage() const noexcept
  178. {
  179. if (Drawable* const d = getToggleState() ? downImageOn : downImage)
  180. return d;
  181. return getOverImage();
  182. }