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.

138 lines
4.3KB

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