LMMS
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
38
40
41void ShapeButton::setColours (Colour newNormalColour, Colour newOverColour, Colour newDownColour)
42{
43 normalColour = newNormalColour;
44 overColour = newOverColour;
45 downColour = newDownColour;
46}
47
48void ShapeButton::setOnColours (Colour newNormalColourOn, Colour newOverColourOn, Colour newDownColourOn)
49{
50 normalColourOn = newNormalColourOn;
51 overColourOn = newOverColourOn;
52 downColourOn = newDownColourOn;
53}
54
56{
57 useOnColours = shouldUse;
58}
59
60void ShapeButton::setOutline (Colour newOutlineColour, const float newOutlineWidth)
61{
62 outlineColour = newOutlineColour;
63 outlineWidth = newOutlineWidth;
64}
65
67{
68 border = newBorder;
69}
70
71void ShapeButton::setShape (const Path& newShape,
72 const bool resizeNowToFitThisShape,
73 const bool maintainShapeProportions_,
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
82 if (resizeNowToFitThisShape)
83 {
84 auto newBounds = shape.getBounds();
85
86 if (hasShadow)
87 newBounds = newBounds.expanded (4.0f);
88
89 shape.applyTransform (AffineTransform::translation (-newBounds.getX(),
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
99void ShapeButton::paintButton (Graphics& g, bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown)
100{
101 if (! isEnabled())
102 {
103 shouldDrawButtonAsHighlighted = false;
104 shouldDrawButtonAsDown = false;
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
114 if (shouldDrawButtonAsDown)
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
Definition juce_AffineTransform.cpp:81
Definition juce_BorderSize.h:42
bool getToggleState() const noexcept
Definition juce_Button.h:127
Button(const String &buttonName)
Definition juce_Button.cpp:76
Definition juce_Colour.h:38
void repaint()
Definition juce_Component.cpp:1917
void setSize(int newWidth, int newHeight)
Definition juce_Component.cpp:1262
ImageEffectFilter * getComponentEffect() const noexcept
Definition juce_Component.h:1147
bool isEnabled() const noexcept
Definition juce_Component.cpp:3104
Rectangle< int > getLocalBounds() const noexcept
Definition juce_Component.cpp:2283
void setComponentEffect(ImageEffectFilter *newEffect)
Definition juce_Component.cpp:2163
Definition juce_GraphicsContext.h:45
Definition juce_Path.h:65
Definition juce_PathStrokeType.h:42
Definition juce_Point.h:42
void shouldUseOnColours(bool shouldUse)
Definition juce_ShapeButton.cpp:55
BorderSize< int > border
Definition juce_ShapeButton.h:119
Path shape
Definition juce_ShapeButton.h:118
void setOutline(Colour outlineColour, float outlineStrokeWidth)
Definition juce_ShapeButton.cpp:60
Colour normalColourOn
Definition juce_ShapeButton.h:115
void setOnColours(Colour normalColourOn, Colour overColourOn, Colour downColourOn)
Definition juce_ShapeButton.cpp:48
Colour downColourOn
Definition juce_ShapeButton.h:115
Colour downColour
Definition juce_ShapeButton.h:114
Colour overColour
Definition juce_ShapeButton.h:114
void setColours(Colour normalColour, Colour overColour, Colour downColour)
Definition juce_ShapeButton.cpp:41
bool maintainShapeProportions
Definition juce_ShapeButton.h:120
DropShadowEffect shadow
Definition juce_ShapeButton.h:117
bool useOnColours
Definition juce_ShapeButton.h:116
Colour overColourOn
Definition juce_ShapeButton.h:115
void paintButton(Graphics &, bool, bool) override
Definition juce_ShapeButton.cpp:99
~ShapeButton() override
Definition juce_ShapeButton.cpp:39
Colour normalColour
Definition juce_ShapeButton.h:114
ShapeButton(const String &name, Colour normalColour, Colour overColour, Colour downColour)
Definition juce_ShapeButton.cpp:29
void setShape(const Path &newShape, bool resizeNowToFitThisShape, bool maintainShapeProportions, bool hasDropShadow)
Definition juce_ShapeButton.cpp:71
void setBorderSize(BorderSize< int > border)
Definition juce_ShapeButton.cpp:66
Colour outlineColour
Definition juce_ShapeButton.h:115
float outlineWidth
Definition juce_ShapeButton.h:121
Definition juce_String.h:53
struct huft * t
Definition inflate.c:943
unsigned d
Definition inflate.c:940
int g
Definition inflate.c:1573
const Colour black
Definition juce_Colours.h:50
Definition carla_juce.cpp:31
#define false
Definition ordinals.h:83
Definition juce_DropShadowEffect.h:36
int n
Definition crypt.c:458
int r
Definition crypt.c:458