The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

222 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. DrawableButton::DrawableButton (const String& name, const DrawableButton::ButtonStyle buttonStyle)
  20. : Button (name), style (buttonStyle)
  21. {
  22. }
  23. DrawableButton::~DrawableButton()
  24. {
  25. }
  26. //==============================================================================
  27. static Drawable* copyDrawableIfNotNull (const Drawable* const d)
  28. {
  29. return d != nullptr ? d->createCopy() : nullptr;
  30. }
  31. void DrawableButton::setImages (const Drawable* normal,
  32. const Drawable* over,
  33. const Drawable* down,
  34. const Drawable* disabled,
  35. const Drawable* normalOn,
  36. const Drawable* overOn,
  37. const Drawable* downOn,
  38. const Drawable* disabledOn)
  39. {
  40. jassert (normal != nullptr); // you really need to give it at least a normal image..
  41. normalImage = copyDrawableIfNotNull (normal);
  42. overImage = copyDrawableIfNotNull (over);
  43. downImage = copyDrawableIfNotNull (down);
  44. disabledImage = copyDrawableIfNotNull (disabled);
  45. normalImageOn = copyDrawableIfNotNull (normalOn);
  46. overImageOn = copyDrawableIfNotNull (overOn);
  47. downImageOn = copyDrawableIfNotNull (downOn);
  48. disabledImageOn = copyDrawableIfNotNull (disabledOn);
  49. currentImage = nullptr;
  50. buttonStateChanged();
  51. }
  52. //==============================================================================
  53. void DrawableButton::setButtonStyle (const DrawableButton::ButtonStyle newStyle)
  54. {
  55. if (style != newStyle)
  56. {
  57. style = newStyle;
  58. buttonStateChanged();
  59. }
  60. }
  61. void DrawableButton::setEdgeIndent (const int numPixelsIndent)
  62. {
  63. edgeIndent = numPixelsIndent;
  64. repaint();
  65. resized();
  66. }
  67. Rectangle<float> DrawableButton::getImageBounds() const
  68. {
  69. auto r = getLocalBounds();
  70. if (style != ImageStretched)
  71. {
  72. auto indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
  73. auto indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
  74. if (style == ImageOnButtonBackground)
  75. {
  76. indentX = jmax (getWidth() / 4, indentX);
  77. indentY = jmax (getHeight() / 4, indentY);
  78. }
  79. else if (style == ImageAboveTextLabel)
  80. {
  81. r = r.withTrimmedBottom (jmin (16, proportionOfHeight (0.25f)));
  82. }
  83. r = r.reduced (indentX, indentY);
  84. }
  85. return r.toFloat();
  86. }
  87. void DrawableButton::resized()
  88. {
  89. Button::resized();
  90. if (currentImage != nullptr)
  91. {
  92. if (style == ImageRaw)
  93. currentImage->setOriginWithOriginalSize (Point<float>());
  94. else
  95. currentImage->setTransformToFit (getImageBounds(),
  96. style == ImageStretched ? RectanglePlacement::stretchToFit
  97. : RectanglePlacement::centred);
  98. }
  99. }
  100. void DrawableButton::buttonStateChanged()
  101. {
  102. repaint();
  103. Drawable* imageToDraw = nullptr;
  104. float opacity = 1.0f;
  105. if (isEnabled())
  106. {
  107. imageToDraw = getCurrentImage();
  108. }
  109. else
  110. {
  111. imageToDraw = getToggleState() ? disabledImageOn
  112. : disabledImage;
  113. if (imageToDraw == nullptr)
  114. {
  115. opacity = 0.4f;
  116. imageToDraw = getNormalImage();
  117. }
  118. }
  119. if (imageToDraw != currentImage)
  120. {
  121. removeChildComponent (currentImage);
  122. currentImage = imageToDraw;
  123. if (currentImage != nullptr)
  124. {
  125. currentImage->setInterceptsMouseClicks (false, false);
  126. addAndMakeVisible (currentImage);
  127. resized();
  128. }
  129. }
  130. if (currentImage != nullptr)
  131. currentImage->setAlpha (opacity);
  132. }
  133. void DrawableButton::enablementChanged()
  134. {
  135. Button::enablementChanged();
  136. buttonStateChanged();
  137. }
  138. void DrawableButton::colourChanged()
  139. {
  140. repaint();
  141. }
  142. void DrawableButton::paintButton (Graphics& g,
  143. const bool isMouseOverButton,
  144. const bool isButtonDown)
  145. {
  146. auto& lf = getLookAndFeel();
  147. if (style == ImageOnButtonBackground)
  148. lf.drawButtonBackground (g, *this,
  149. findColour (getToggleState() ? TextButton::buttonOnColourId
  150. : TextButton::buttonColourId),
  151. isMouseOverButton, isButtonDown);
  152. else
  153. lf.drawDrawableButton (g, *this, isMouseOverButton, isButtonDown);
  154. }
  155. //==============================================================================
  156. Drawable* DrawableButton::getCurrentImage() const noexcept
  157. {
  158. if (isDown()) return getDownImage();
  159. if (isOver()) return getOverImage();
  160. return getNormalImage();
  161. }
  162. Drawable* DrawableButton::getNormalImage() const noexcept
  163. {
  164. return (getToggleState() && normalImageOn != nullptr) ? normalImageOn
  165. : normalImage;
  166. }
  167. Drawable* DrawableButton::getOverImage() const noexcept
  168. {
  169. if (getToggleState())
  170. {
  171. if (overImageOn != nullptr) return overImageOn;
  172. if (normalImageOn != nullptr) return normalImageOn;
  173. }
  174. return overImage != nullptr ? overImage : normalImage;
  175. }
  176. Drawable* DrawableButton::getDownImage() const noexcept
  177. {
  178. if (auto* d = getToggleState() ? downImageOn.get() : downImage.get())
  179. return d;
  180. return getOverImage();
  181. }