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.

118 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. ShapeButton::ShapeButton (const String& text_,
  18. const Colour& normalColour_,
  19. const Colour& overColour_,
  20. const Colour& downColour_)
  21. : Button (text_),
  22. normalColour (normalColour_),
  23. overColour (overColour_),
  24. downColour (downColour_),
  25. maintainShapeProportions (false),
  26. outlineWidth (0.0f)
  27. {
  28. }
  29. ShapeButton::~ShapeButton()
  30. {
  31. }
  32. void ShapeButton::setColours (const Colour& newNormalColour,
  33. const Colour& newOverColour,
  34. const Colour& newDownColour)
  35. {
  36. normalColour = newNormalColour;
  37. overColour = newOverColour;
  38. downColour = newDownColour;
  39. }
  40. void ShapeButton::setOutline (const Colour& newOutlineColour,
  41. const float newOutlineWidth)
  42. {
  43. outlineColour = newOutlineColour;
  44. outlineWidth = newOutlineWidth;
  45. }
  46. void ShapeButton::setShape (const Path& newShape,
  47. const bool resizeNowToFitThisShape,
  48. const bool maintainShapeProportions_,
  49. const bool hasShadow)
  50. {
  51. shape = newShape;
  52. maintainShapeProportions = maintainShapeProportions_;
  53. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.5f), 3, Point<int>()));
  54. setComponentEffect (hasShadow ? &shadow : nullptr);
  55. if (resizeNowToFitThisShape)
  56. {
  57. Rectangle<float> newBounds (shape.getBounds());
  58. if (hasShadow)
  59. newBounds.expand (4.0f, 4.0f);
  60. shape.applyTransform (AffineTransform::translation (-newBounds.getX(), -newBounds.getY()));
  61. setSize (1 + (int) (newBounds.getWidth() + outlineWidth),
  62. 1 + (int) (newBounds.getHeight() + outlineWidth));
  63. }
  64. }
  65. void ShapeButton::paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)
  66. {
  67. if (! isEnabled())
  68. {
  69. isMouseOverButton = false;
  70. isButtonDown = false;
  71. }
  72. g.setColour (isButtonDown ? downColour
  73. : isMouseOverButton ? overColour
  74. : normalColour);
  75. int w = getWidth();
  76. int h = getHeight();
  77. if (getComponentEffect() != nullptr)
  78. {
  79. w -= 4;
  80. h -= 4;
  81. }
  82. const float offset = (outlineWidth * 0.5f) + (isButtonDown ? 1.5f : 0.0f);
  83. const AffineTransform trans (shape.getTransformToScaleToFit (offset, offset,
  84. w - offset - outlineWidth,
  85. h - offset - outlineWidth,
  86. maintainShapeProportions));
  87. g.fillPath (shape, trans);
  88. if (outlineWidth > 0.0f)
  89. {
  90. g.setColour (outlineColour);
  91. g.strokePath (shape, PathStrokeType (outlineWidth), trans);
  92. }
  93. }