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.

242 lines
6.9KB

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