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.

juce_ShapeButton.cpp 3.7KB

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