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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. ShapeButton::ShapeButton (const String& t, Colour n, Colour o, Colour d)
  16. : Button (t),
  17. normalColour (n), overColour (o), downColour (d),
  18. normalColourOn (n), overColourOn (o), downColourOn (d),
  19. useOnColours(false),
  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::setOnColours (Colour newNormalColourOn, Colour newOverColourOn, Colour newDownColourOn)
  32. {
  33. normalColourOn = newNormalColourOn;
  34. overColourOn = newOverColourOn;
  35. downColourOn = newDownColourOn;
  36. }
  37. void ShapeButton::shouldUseOnColours (bool shouldUse)
  38. {
  39. useOnColours = shouldUse;
  40. }
  41. void ShapeButton::setOutline (Colour newOutlineColour, const float newOutlineWidth)
  42. {
  43. outlineColour = newOutlineColour;
  44. outlineWidth = newOutlineWidth;
  45. }
  46. void ShapeButton::setBorderSize (BorderSize<int> newBorder)
  47. {
  48. border = newBorder;
  49. }
  50. void ShapeButton::setShape (const Path& newShape,
  51. const bool resizeNowToFitThisShape,
  52. const bool maintainShapeProportions_,
  53. const bool hasShadow)
  54. {
  55. shape = newShape;
  56. maintainShapeProportions = maintainShapeProportions_;
  57. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.5f), 3, Point<int>()));
  58. setComponentEffect (hasShadow ? &shadow : nullptr);
  59. if (resizeNowToFitThisShape)
  60. {
  61. auto newBounds = shape.getBounds();
  62. if (hasShadow)
  63. newBounds = newBounds.expanded (4.0f);
  64. shape.applyTransform (AffineTransform::translation (-newBounds.getX(),
  65. -newBounds.getY()));
  66. setSize (1 + (int) (newBounds.getWidth() + outlineWidth) + border.getLeftAndRight(),
  67. 1 + (int) (newBounds.getHeight() + outlineWidth) + border.getTopAndBottom());
  68. }
  69. repaint();
  70. }
  71. void ShapeButton::paintButton (Graphics& g, bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown)
  72. {
  73. if (! isEnabled())
  74. {
  75. shouldDrawButtonAsHighlighted = false;
  76. shouldDrawButtonAsDown = false;
  77. }
  78. auto r = border.subtractedFrom (getLocalBounds())
  79. .toFloat()
  80. .reduced (outlineWidth * 0.5f);
  81. if (getComponentEffect() != nullptr)
  82. r = r.reduced (2.0f);
  83. if (shouldDrawButtonAsDown)
  84. {
  85. const float sizeReductionWhenPressed = 0.04f;
  86. r = r.reduced (sizeReductionWhenPressed * r.getWidth(),
  87. sizeReductionWhenPressed * r.getHeight());
  88. }
  89. auto trans = shape.getTransformToScaleToFit (r, maintainShapeProportions);
  90. if (shouldDrawButtonAsDown) g.setColour (getToggleState() && useOnColours ? downColourOn : downColour);
  91. else if (shouldDrawButtonAsHighlighted) g.setColour (getToggleState() && useOnColours ? overColourOn : overColour);
  92. else g.setColour (getToggleState() && useOnColours ? normalColourOn : normalColour);
  93. g.fillPath (shape, trans);
  94. if (outlineWidth > 0.0f)
  95. {
  96. g.setColour (outlineColour);
  97. g.strokePath (shape, PathStrokeType (outlineWidth), trans);
  98. }
  99. }
  100. } // namespace juce