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.

117 lines
3.6KB

  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& t, Colour n, Colour o, Colour d)
  18. : Button (t),
  19. normalColour (n),
  20. overColour (o),
  21. downColour (d),
  22. maintainShapeProportions (false),
  23. outlineWidth (0.0f)
  24. {
  25. }
  26. ShapeButton::~ShapeButton()
  27. {
  28. }
  29. void ShapeButton::setColours (Colour newNormalColour,
  30. Colour newOverColour,
  31. Colour newDownColour)
  32. {
  33. normalColour = newNormalColour;
  34. overColour = newOverColour;
  35. downColour = newDownColour;
  36. }
  37. void ShapeButton::setOutline (Colour newOutlineColour,
  38. const float newOutlineWidth)
  39. {
  40. outlineColour = newOutlineColour;
  41. outlineWidth = newOutlineWidth;
  42. }
  43. void ShapeButton::setShape (const Path& newShape,
  44. const bool resizeNowToFitThisShape,
  45. const bool maintainShapeProportions_,
  46. const bool hasShadow)
  47. {
  48. shape = newShape;
  49. maintainShapeProportions = maintainShapeProportions_;
  50. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.5f), 3, Point<int>()));
  51. setComponentEffect (hasShadow ? &shadow : nullptr);
  52. if (resizeNowToFitThisShape)
  53. {
  54. Rectangle<float> newBounds (shape.getBounds());
  55. if (hasShadow)
  56. newBounds = newBounds.expanded (4.0f);
  57. shape.applyTransform (AffineTransform::translation (-newBounds.getX(),
  58. -newBounds.getY()));
  59. setSize (1 + (int) (newBounds.getWidth() + outlineWidth),
  60. 1 + (int) (newBounds.getHeight() + outlineWidth));
  61. }
  62. repaint();
  63. }
  64. void ShapeButton::paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)
  65. {
  66. if (! isEnabled())
  67. {
  68. isMouseOverButton = false;
  69. isButtonDown = false;
  70. }
  71. Rectangle<float> r (getLocalBounds().toFloat().reduced (outlineWidth * 0.5f));
  72. if (getComponentEffect() != nullptr)
  73. r = r.reduced (2.0f);
  74. if (isButtonDown)
  75. {
  76. const float sizeReductionWhenPressed = 0.04f;
  77. r = r.reduced (sizeReductionWhenPressed * r.getWidth(),
  78. sizeReductionWhenPressed * r.getHeight());
  79. }
  80. const AffineTransform trans (shape.getTransformToScaleToFit (r, maintainShapeProportions));
  81. g.setColour (isButtonDown ? downColour
  82. : isMouseOverButton ? overColour
  83. : normalColour);
  84. g.fillPath (shape, trans);
  85. if (outlineWidth > 0.0f)
  86. {
  87. g.setColour (outlineColour);
  88. g.strokePath (shape, PathStrokeType (outlineWidth), trans);
  89. }
  90. }