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_Drawable.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{
31 setInterceptsMouseClicks (false, false);
33 setAccessible (false);
34}
35
37 : Component (other.getName())
38{
39 setInterceptsMouseClicks (false, false);
41 setAccessible (false);
42
43 setComponentID (other.getComponentID());
44 setTransform (other.getTransform());
45
46 if (auto* clipPath = other.drawableClipPath.get())
47 setClipPath (clipPath->createCopy());
48}
49
53
54void Drawable::applyDrawableClipPath (Graphics& g)
55{
56 if (drawableClipPath != nullptr)
57 {
58 auto clipPath = drawableClipPath->getOutlineAsPath();
59
60 if (! clipPath.isEmpty())
61 g.getInternalContext().clipToPath (clipPath, {});
62 }
63}
64
65//==============================================================================
66void Drawable::draw (Graphics& g, float opacity, const AffineTransform& transform) const
67{
68 const_cast<Drawable*> (this)->nonConstDraw (g, opacity, transform);
69}
70
71void Drawable::nonConstDraw (Graphics& g, float opacity, const AffineTransform& transform)
72{
74
75 g.addTransform (AffineTransform::translation ((float) -(originRelativeToComponent.x),
76 (float) -(originRelativeToComponent.y))
78 .followedBy (transform));
79
80 applyDrawableClipPath (g);
81
82 if (! g.isClipEmpty())
83 {
84 if (opacity < 1.0f)
85 {
86 g.beginTransparencyLayer (opacity);
87 paintEntireComponent (g, true);
89 }
90 else
91 {
92 paintEntireComponent (g, true);
93 }
94 }
95}
96
97void Drawable::drawAt (Graphics& g, float x, float y, float opacity) const
98{
99 draw (g, opacity, AffineTransform::translation (x, y));
100}
101
103 RectanglePlacement placement, float opacity) const
104{
105 draw (g, opacity, placement.getTransformToFit (getDrawableBounds(), destArea));
106}
107
108//==============================================================================
110{
111 return dynamic_cast<DrawableComposite*> (getParentComponent());
112}
113
115{
116 if (drawableClipPath != clipPath)
117 {
118 drawableClipPath = std::move (clipPath);
119 repaint();
120 }
121}
122
123void Drawable::transformContextToCorrectOrigin (Graphics& g)
124{
125 g.setOrigin (originRelativeToComponent);
126}
127
129{
130 setBoundsToEnclose (getDrawableBounds());
131}
132
133void Drawable::setBoundsToEnclose (Rectangle<float> area)
134{
136
137 if (auto* parent = getParent())
138 parentOrigin = parent->originRelativeToComponent;
139
142 originRelativeToComponent = -smallestIntegerContainer.getPosition();
144}
145
146//==============================================================================
148{
149 bool changed = false;
150
151 for (auto* c : getChildren())
152 if (auto* d = dynamic_cast<Drawable*> (c))
153 changed = d->replaceColour (original, replacement) || changed;
154
155 return changed;
156}
157
159{
160 drawableTransform = transform;
161 updateTransform();
162}
163
164void Drawable::updateTransform()
165{
166 if (drawableTransform.isIdentity())
167 return;
168
169 const auto transformationOrigin = originRelativeToComponent + getPosition();
171 .followedBy (drawableTransform)
173}
174
175//==============================================================================
180
182{
183 if (! area.isEmpty())
185}
186
187//==============================================================================
188std::unique_ptr<Drawable> Drawable::createFromImageData (const void* data, const size_t numBytes)
189{
190 auto image = ImageFileFormat::loadFrom (data, numBytes);
191
192 if (image.isValid())
193 return std::make_unique<DrawableImage> (image);
194
195 if (auto svg = parseXMLIfTagMatches (String::createStringFromData (data, (int) numBytes), "svg"))
197
198 return {};
199}
200
208
210{
211 FileInputStream fin (file);
212
213 if (fin.openedOk())
215
216 return {};
217}
218
219} // namespace juce
Represents a 2D affine-transformation matrix.
AffineTransform followedBy(const AffineTransform &other) const noexcept
Returns the result of concatenating another transformation after this one.
bool isIdentity() const noexcept
Returns true if this transform has no effect on points.
static AffineTransform translation(float deltaX, float deltaY) noexcept
Returns a new transform which is a translation.
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
The base class for all JUCE user-interface objects.
void paintEntireComponent(Graphics &context, bool ignoreAlphaLevel)
Draws this component and all its subcomponents onto the specified graphics context.
AffineTransform getTransform() const
Returns the transform that is currently being applied to this component.
void setTransform(const AffineTransform &transform)
Sets a transform matrix to be applied to this component.
void setComponentID(const String &newID)
Sets the component's ID string.
void setAccessible(bool shouldBeAccessible)
Sets whether this component and its children are visible to accessibility clients.
void setInterceptsMouseClicks(bool allowClicksOnThisComponent, bool allowClicksOnChildComponents) noexcept
Changes the default return value for the hitTest() method.
Component * getParentComponent() const noexcept
Returns the component which this component is inside.
Point< int > getPosition() const noexcept
Returns the component's top-left position as a Point.
void setPaintingIsUnclipped(bool shouldPaintWithoutClipping) noexcept
This allows you to indicate that this component doesn't require its graphics context to be clipped wh...
void repaint()
Marks the whole component as needing to be redrawn.
void setBounds(int x, int y, int width, int height)
Changes the component's position and size.
const Array< Component * > & getChildren() const noexcept
Provides access to the underlying array of child components.
A drawable object which acts as a container for a set of other Drawables.
The base class for objects which can draw themselves, e.g.
void draw(Graphics &g, float opacity, const AffineTransform &transform=AffineTransform()) const
Renders this Drawable object.
static std::unique_ptr< Drawable > createFromImageData(const void *data, size_t numBytes)
Tries to turn some kind of image file into a drawable.
void drawAt(Graphics &g, float x, float y, float opacity) const
Renders the Drawable at a given offset within the Graphics context.
static std::unique_ptr< Drawable > createFromSVG(const XmlElement &svgDocument)
Attempts to parse an SVG (Scalable Vector Graphics) document, and to turn this into a Drawable tree.
DrawableComposite * getParent() const
Returns the DrawableComposite that contains this object, if there is one.
void parentHierarchyChanged() override
Called to indicate that the component's parents have changed.
Drawable()
The base class can't be instantiated directly.
static std::unique_ptr< Drawable > createFromImageFile(const File &file)
Tries to turn a file containing some kind of image data into a drawable.
void drawWithin(Graphics &g, Rectangle< float > destArea, RectanglePlacement placement, float opacity) const
Renders the Drawable within a rectangle, scaling it to fit neatly inside without changing its aspect-...
void setOriginWithOriginalSize(Point< float > originWithinParent)
Resets any transformations on this drawable, and positions its origin within its parent component.
static std::unique_ptr< Drawable > createFromImageDataStream(InputStream &dataSource)
Tries to turn a stream containing some kind of image data into a drawable.
void setClipPath(std::unique_ptr< Drawable > drawableClipPath)
Sets a the clipping region of this drawable using another drawable.
void setTransformToFit(const Rectangle< float > &areaInParent, RectanglePlacement placement)
Sets a transform for this drawable that will position it within the specified area of its parent comp...
void setDrawableTransform(const AffineTransform &transform)
Sets a transformation that applies to the same coordinate system in which the rest of the draw calls ...
virtual bool replaceColour(Colour originalColour, Colour replacementColour)
Recursively replaces a colour that might be used for filling or stroking.
~Drawable() override
Destructor.
virtual Rectangle< float > getDrawableBounds() const =0
Returns the area that this drawable covers.
An input stream that reads from a local file.
Represents a local file or directory.
Definition juce_File.h:45
Uses RAII to save and restore the state of a graphics context.
A graphics context, used for drawing a component or image.
bool isClipEmpty() const
Returns true if no drawing can be done because the clip region is zero.
void endTransparencyLayer()
Completes a drawing operation to a temporary semi-transparent buffer.
void addTransform(const AffineTransform &transform)
Adds a transformation which will be performed on all the graphics operations that the context subsequ...
void beginTransparencyLayer(float layerOpacity)
Begins rendering to an off-screen bitmap which will later be flattened onto the current context with ...
void setOrigin(Point< int > newOrigin)
Moves the position of the context's origin.
static Image loadFrom(InputStream &input)
Tries to load an image from a stream.
The base class for streams that read data.
Writes data to an internal memory buffer, which grows as required.
A pair of (x, y) coordinates.
Definition juce_Point.h:42
ValueType y
The point's Y coordinate.
Definition juce_Point.h:252
ValueType x
The point's X coordinate.
Definition juce_Point.h:251
Defines the method used to position some kind of rectangular object within a rectangular viewport.
AffineTransform getTransformToFit(const Rectangle< float > &source, const Rectangle< float > &destination) const noexcept
Returns the transform that should be applied to these source coordinates to fit them into the destina...
Manages a rectangle and allows geometric operations to be performed on it.
Rectangle< int > getSmallestIntegerContainer() const noexcept
Returns the smallest integer-aligned rectangle that completely contains this one.
bool isEmpty() const noexcept
Returns true if the rectangle's width or height are zero or less.
static String createStringFromData(const void *data, int size)
Creates a string from data in an unknown format.
JUCE Namespace.
std::unique_ptr< XmlElement > parseXMLIfTagMatches(const String &textToParse, StringRef requiredTag)
Does an inexpensive check to see whether the top-level element has the given tag name,...
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