JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ShapeButton.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30 : Button (t),
31 normalColour (n), overColour (o), downColour (d),
32 normalColourOn (n), overColourOn (o), downColourOn (d),
33 useOnColours (false),
34 maintainShapeProportions (false),
35 outlineWidth (0.0f)
36{
37}
38
40
47
54
56{
57 useOnColours = shouldUse;
58}
59
61{
62 outlineColour = newOutlineColour;
63 outlineWidth = newOutlineWidth;
64}
65
70
72 const bool resizeNowToFitThisShape,
74 const bool hasShadow)
75{
76 shape = newShape;
77 maintainShapeProportions = maintainShapeProportions_;
78
79 shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.5f), 3, Point<int>()));
80 setComponentEffect (hasShadow ? &shadow : nullptr);
81
83 {
84 auto newBounds = shape.getBounds();
85
86 if (hasShadow)
88
90 -newBounds.getY()));
91
92 setSize (1 + (int) (newBounds.getWidth() + outlineWidth) + border.getLeftAndRight(),
93 1 + (int) (newBounds.getHeight() + outlineWidth) + border.getTopAndBottom());
94 }
95
96 repaint();
97}
98
100{
101 if (! isEnabled())
102 {
105 }
106
107 auto r = border.subtractedFrom (getLocalBounds())
108 .toFloat()
109 .reduced (outlineWidth * 0.5f);
110
111 if (getComponentEffect() != nullptr)
112 r = r.reduced (2.0f);
113
115 {
116 const float sizeReductionWhenPressed = 0.04f;
117
118 r = r.reduced (sizeReductionWhenPressed * r.getWidth(),
119 sizeReductionWhenPressed * r.getHeight());
120 }
121
122 auto trans = shape.getTransformToScaleToFit (r, maintainShapeProportions);
123
124 if (shouldDrawButtonAsDown) g.setColour (getToggleState() && useOnColours ? downColourOn : downColour);
125 else if (shouldDrawButtonAsHighlighted) g.setColour (getToggleState() && useOnColours ? overColourOn : overColour);
126 else g.setColour (getToggleState() && useOnColours ? normalColourOn : normalColour);
127
128 g.fillPath (shape, trans);
129
130 if (outlineWidth > 0.0f)
131 {
132 g.setColour (outlineColour);
133 g.strokePath (shape, PathStrokeType (outlineWidth), trans);
134 }
135}
136
137} // namespace juce
static AffineTransform translation(float deltaX, float deltaY) noexcept
Returns a new transform which is a translation.
Specifies a set of gaps to be left around the sides of a rectangle.
ValueType getTopAndBottom() const noexcept
Returns the sum of the top and bottom gaps.
Rectangle< ValueType > subtractedFrom(const Rectangle< ValueType > &original) const noexcept
Returns a rectangle with these borders removed from it.
ValueType getLeftAndRight() const noexcept
Returns the sum of the left and right gaps.
A base class for buttons.
Definition juce_Button.h:43
bool getToggleState() const noexcept
Returns true if the button is 'on'.
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
void repaint()
Marks the whole component as needing to be redrawn.
void setSize(int newWidth, int newHeight)
Changes the size of the component.
ImageEffectFilter * getComponentEffect() const noexcept
Returns the current component effect.
bool isEnabled() const noexcept
Returns true if the component (and all its parents) are enabled.
Rectangle< int > getLocalBounds() const noexcept
Returns the component's bounds, relative to its own origin.
void setComponentEffect(ImageEffectFilter *newEffect)
Adds an effect filter to alter the component's appearance.
void setShadowProperties(const DropShadow &newShadow)
Sets up parameters affecting the shadow's appearance.
A graphics context, used for drawing a component or image.
void fillPath(const Path &path) const
Fills a path using the currently selected colour or brush.
void setColour(Colour newColour)
Changes the current drawing colour.
void strokePath(const Path &path, const PathStrokeType &strokeType, const AffineTransform &transform={}) const
Draws a path's outline using the currently selected colour or brush.
Describes a type of stroke used to render a solid outline along a path.
A path is a sequence of lines and curves that may either form a closed shape or be open-ended.
Definition juce_Path.h:65
AffineTransform getTransformToScaleToFit(float x, float y, float width, float height, bool preserveProportions, Justification justificationType=Justification::centred) const
Returns a transform that can be used to rescale the path to fit into a given space.
void applyTransform(const AffineTransform &transform) noexcept
Applies a 2D transform to all the vertices in the path.
Rectangle< float > getBounds() const noexcept
Returns the smallest rectangle that contains all points within the path.
A pair of (x, y) coordinates.
Definition juce_Point.h:42
Rectangle< float > toFloat() const noexcept
Casts this rectangle to a Rectangle<float>.
Rectangle reduced(ValueType deltaX, ValueType deltaY) const noexcept
Returns a rectangle that is smaller than this one by a given amount.
Rectangle expanded(ValueType deltaX, ValueType deltaY) const noexcept
Returns a rectangle that is larger than this one by a given amount.
void shouldUseOnColours(bool shouldUse)
Set whether the button should use the 'on' set of colours when its toggle state is 'on'.
void setOutline(Colour outlineColour, float outlineStrokeWidth)
Sets up an outline to draw around the shape.
void setOnColours(Colour normalColourOn, Colour overColourOn, Colour downColourOn)
Sets the colours to use for drawing the shape when the button's toggle state is 'on'.
void setColours(Colour normalColour, Colour overColour, Colour downColour)
Set the colours to use for drawing the shape.
void paintButton(Graphics &, bool, bool) override
Subclasses should override this to actually paint the button's contents.
~ShapeButton() override
Destructor.
ShapeButton(const String &name, Colour normalColour, Colour overColour, Colour downColour)
Creates a ShapeButton.
void setShape(const Path &newShape, bool resizeNowToFitThisShape, bool maintainShapeProportions, bool hasDropShadow)
Sets the shape to use.
void setBorderSize(BorderSize< int > border)
This lets you specify a border to be left around the edge of the button when drawing the shape.
The JUCE String class!
Definition juce_String.h:53
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
Defines a drop-shadow effect.