The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

124 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. ShapeButton::ShapeButton (const String& text_,
  21. const Colour& normalColour_,
  22. const Colour& overColour_,
  23. const Colour& downColour_)
  24. : Button (text_),
  25. normalColour (normalColour_),
  26. overColour (overColour_),
  27. downColour (downColour_),
  28. maintainShapeProportions (false),
  29. outlineWidth (0.0f)
  30. {
  31. }
  32. ShapeButton::~ShapeButton()
  33. {
  34. }
  35. void ShapeButton::setColours (const Colour& newNormalColour,
  36. const Colour& newOverColour,
  37. const Colour& newDownColour)
  38. {
  39. normalColour = newNormalColour;
  40. overColour = newOverColour;
  41. downColour = newDownColour;
  42. }
  43. void ShapeButton::setOutline (const Colour& newOutlineColour,
  44. const float newOutlineWidth)
  45. {
  46. outlineColour = newOutlineColour;
  47. outlineWidth = newOutlineWidth;
  48. }
  49. void ShapeButton::setShape (const Path& newShape,
  50. const bool resizeNowToFitThisShape,
  51. const bool maintainShapeProportions_,
  52. const bool hasShadow)
  53. {
  54. shape = newShape;
  55. maintainShapeProportions = maintainShapeProportions_;
  56. shadow.setShadowProperties (3.0f, 0.5f, 0, 0);
  57. setComponentEffect ((hasShadow) ? &shadow : 0);
  58. if (resizeNowToFitThisShape)
  59. {
  60. Rectangle<float> newBounds (shape.getBounds());
  61. if (hasShadow)
  62. newBounds.expand (4.0f, 4.0f);
  63. shape.applyTransform (AffineTransform::translation (-newBounds.getX(), -newBounds.getY()));
  64. setSize (1 + (int) (newBounds.getWidth() + outlineWidth),
  65. 1 + (int) (newBounds.getHeight() + outlineWidth));
  66. }
  67. }
  68. void ShapeButton::paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)
  69. {
  70. if (! isEnabled())
  71. {
  72. isMouseOverButton = false;
  73. isButtonDown = false;
  74. }
  75. g.setColour ((isButtonDown) ? downColour
  76. : (isMouseOverButton) ? overColour
  77. : normalColour);
  78. int w = getWidth();
  79. int h = getHeight();
  80. if (getComponentEffect() != nullptr)
  81. {
  82. w -= 4;
  83. h -= 4;
  84. }
  85. const float offset = (outlineWidth * 0.5f) + (isButtonDown ? 1.5f : 0.0f);
  86. const AffineTransform trans (shape.getTransformToScaleToFit (offset, offset,
  87. w - offset - outlineWidth,
  88. h - offset - outlineWidth,
  89. maintainShapeProportions));
  90. g.fillPath (shape, trans);
  91. if (outlineWidth > 0.0f)
  92. {
  93. g.setColour (outlineColour);
  94. g.strokePath (shape, PathStrokeType (outlineWidth), trans);
  95. }
  96. }
  97. END_JUCE_NAMESPACE