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.3KB

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