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_DrawableShape.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
29DrawableShape::DrawableShape()
30 : strokeType (0.0f),
31 mainFill (Colours::black),
32 strokeFill (Colours::black)
33{
34}
35
36DrawableShape::DrawableShape (const DrawableShape& other)
37 : Drawable (other),
38 strokeType (other.strokeType),
39 dashLengths (other.dashLengths),
40 mainFill (other.mainFill),
41 strokeFill (other.strokeFill)
42{
43}
44
45DrawableShape::~DrawableShape()
46{
47}
48
49//==============================================================================
50void DrawableShape::setFill (const FillType& newFill)
51{
52 if (mainFill != newFill)
53 {
54 mainFill = newFill;
55 repaint();
56 }
57}
58
59void DrawableShape::setStrokeFill (const FillType& newFill)
60{
61 if (strokeFill != newFill)
62 {
63 strokeFill = newFill;
64 repaint();
65 }
66}
67
68void DrawableShape::setStrokeType (const PathStrokeType& newStrokeType)
69{
70 if (strokeType != newStrokeType)
71 {
72 strokeType = newStrokeType;
73 strokeChanged();
74 }
75}
76
77void DrawableShape::setDashLengths (const Array<float>& newDashLengths)
78{
79 if (dashLengths != newDashLengths)
80 {
81 dashLengths = newDashLengths;
82 strokeChanged();
83 }
84}
85
86void DrawableShape::setStrokeThickness (const float newThickness)
87{
88 setStrokeType (PathStrokeType (newThickness, strokeType.getJointStyle(), strokeType.getEndStyle()));
89}
90
91bool DrawableShape::isStrokeVisible() const noexcept
92{
93 return strokeType.getStrokeThickness() > 0.0f && ! strokeFill.isInvisible();
94}
95
96//==============================================================================
97void DrawableShape::paint (Graphics& g)
98{
99 transformContextToCorrectOrigin (g);
100 applyDrawableClipPath (g);
101
102 g.setFillType (mainFill);
103 g.fillPath (path);
104
105 if (isStrokeVisible())
106 {
107 g.setFillType (strokeFill);
108 g.fillPath (strokePath);
109 }
110}
111
112void DrawableShape::pathChanged()
113{
114 strokeChanged();
115}
116
117void DrawableShape::strokeChanged()
118{
119 strokePath.clear();
120 const float extraAccuracy = 4.0f;
121
122 if (dashLengths.isEmpty())
123 strokeType.createStrokedPath (strokePath, path, AffineTransform(), extraAccuracy);
124 else
125 strokeType.createDashedStroke (strokePath, path, dashLengths.getRawDataPointer(),
126 dashLengths.size(), AffineTransform(), extraAccuracy);
127
128 setBoundsToEnclose (getDrawableBounds());
129 repaint();
130}
131
132Rectangle<float> DrawableShape::getDrawableBounds() const
133{
134 if (isStrokeVisible())
135 return strokePath.getBounds();
136
137 return path.getBounds();
138}
139
140bool DrawableShape::hitTest (int x, int y)
141{
142 bool allowsClicksOnThisComponent, allowsClicksOnChildComponents;
143 getInterceptsMouseClicks (allowsClicksOnThisComponent, allowsClicksOnChildComponents);
144
145 if (! allowsClicksOnThisComponent)
146 return false;
147
148 auto globalX = (float) (x - originRelativeToComponent.x);
149 auto globalY = (float) (y - originRelativeToComponent.y);
150
151 return path.contains (globalX, globalY)
152 || (isStrokeVisible() && strokePath.contains (globalX, globalY));
153}
154
155//==============================================================================
156static bool replaceColourInFill (FillType& fill, Colour original, Colour replacement)
157{
158 if (fill.colour == original && fill.isColour())
159 {
160 fill = FillType (replacement);
161 return true;
162 }
163
164 return false;
165}
166
167bool DrawableShape::replaceColour (Colour original, Colour replacement)
168{
169 bool changed1 = replaceColourInFill (mainFill, original, replacement);
170 bool changed2 = replaceColourInFill (strokeFill, original, replacement);
171 return changed1 || changed2;
172}
173
174Path DrawableShape::getOutlineAsPath() const
175{
176 auto outline = isStrokeVisible() ? strokePath : path;
177 outline.applyTransform (getTransform());
178 return outline;
179}
180
181} // namespace juce
Represents a 2D affine-transformation matrix.
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
Represents a colour or fill pattern to use for rendering paths.
A graphics context, used for drawing a component or image.
void setFillType(const FillType &newFill)
Changes the current fill settings.
void fillPath(const Path &path) const
Fills a path 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
void applyTransform(const AffineTransform &transform) noexcept
Applies a 2D transform to all the vertices in the path.
Manages a rectangle and allows geometric operations to be performed on it.
typedef float
JUCE Namespace.