LMMS
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
44 setTransform (other.getTransform());
45
46 if (auto* clipPath = other.drawableClipPath.get())
47 setClipPath (clipPath->createCopy());
48}
49
53
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
76 (float) -(originRelativeToComponent.y))
77 .followedBy (getTransform())
78 .followedBy (transform));
79
81
82 if (! g.isClipEmpty())
83 {
84 if (opacity < 1.0f)
85 {
86 g.beginTransparencyLayer (opacity);
87 paintEntireComponent (g, true);
88 g.endTransparencyLayer();
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
114void Drawable::setClipPath (std::unique_ptr<Drawable> clipPath)
115{
116 if (drawableClipPath != clipPath)
117 {
118 drawableClipPath = std::move (clipPath);
119 repaint();
120 }
121}
122
127
132
134{
135 Point<int> parentOrigin;
136
137 if (auto* parent = getParent())
138 parentOrigin = parent->originRelativeToComponent;
139
140 auto newBounds = area.getSmallestIntegerContainer() + parentOrigin;
141 originRelativeToComponent = parentOrigin - newBounds.getPosition();
142 setBounds (newBounds);
143}
144
145//==============================================================================
146bool Drawable::replaceColour (Colour original, Colour replacement)
147{
148 bool changed = false;
149
150 for (auto* c : getChildren())
151 if (auto* d = dynamic_cast<Drawable*> (c))
152 changed = d->replaceColour (original, replacement) || changed;
153
154 return changed;
155}
156
157//==============================================================================
159{
160 setTransform (AffineTransform::translation (originWithinParent.x, originWithinParent.y));
161}
162
164{
165 if (! area.isEmpty())
167}
168
169//==============================================================================
170std::unique_ptr<Drawable> Drawable::createFromImageData (const void* data, const size_t numBytes)
171{
172 auto image = ImageFileFormat::loadFrom (data, numBytes);
173
174 if (image.isValid())
175 return std::make_unique<DrawableImage> (image);
176
177 if (auto svg = parseXMLIfTagMatches (String::createStringFromData (data, (int) numBytes), "svg"))
178 return Drawable::createFromSVG (*svg);
179
180 return {};
181}
182
183std::unique_ptr<Drawable> Drawable::createFromImageDataStream (InputStream& dataSource)
184{
186 mo << dataSource;
187
188 return createFromImageData (mo.getData(), mo.getDataSize());
189}
190
191std::unique_ptr<Drawable> Drawable::createFromImageFile (const File& file)
192{
193 FileInputStream fin (file);
194
195 if (fin.openedOk())
196 return createFromImageDataStream (fin);
197
198 return {};
199}
200
201} // namespace juce
Definition juce_AffineTransform.h:43
static AffineTransform translation(float deltaX, float deltaY) noexcept
Definition juce_AffineTransform.cpp:81
Definition juce_Colour.h:38
void paintEntireComponent(Graphics &context, bool ignoreAlphaLevel)
Definition juce_Component.cpp:2071
AffineTransform getTransform() const
Definition juce_Component.cpp:1378
void setTransform(const AffineTransform &transform)
Definition juce_Component.cpp:1341
void setComponentID(const String &newID)
Definition juce_Component.cpp:570
void setAccessible(bool shouldBeAccessible)
Definition juce_Component.cpp:3273
void setInterceptsMouseClicks(bool allowClicksOnThisComponent, bool allowClicksOnChildComponents) noexcept
Definition juce_Component.cpp:1420
Component * getParentComponent() const noexcept
Definition juce_Component.h:804
void setPaintingIsUnclipped(bool shouldPaintWithoutClipping) noexcept
Definition juce_Component.cpp:2124
void repaint()
Definition juce_Component.cpp:1917
Component() noexcept
Definition juce_Component.cpp:517
void setBounds(int x, int y, int width, int height)
Definition juce_Component.cpp:1147
String getComponentID() const noexcept
Definition juce_Component.h:90
const Array< Component * > & getChildren() const noexcept
Definition juce_Component.h:685
String getName() const noexcept
Definition juce_Component.h:76
void draw(Graphics &g, float opacity, const AffineTransform &transform=AffineTransform()) const
Definition juce_Drawable.cpp:66
std::unique_ptr< Drawable > drawableClipPath
Definition juce_Drawable.h:204
void setBoundsToEnclose(Rectangle< float >)
Definition juce_Drawable.cpp:133
void applyDrawableClipPath(Graphics &)
Definition juce_Drawable.cpp:54
static std::unique_ptr< Drawable > createFromImageData(const void *data, size_t numBytes)
Definition juce_Drawable.cpp:170
void drawAt(Graphics &g, float x, float y, float opacity) const
Definition juce_Drawable.cpp:97
static std::unique_ptr< Drawable > createFromSVG(const XmlElement &svgDocument)
Definition juce_SVGParser.cpp:1757
DrawableComposite * getParent() const
Definition juce_Drawable.cpp:109
void parentHierarchyChanged() override
Definition juce_Drawable.cpp:128
Point< int > originRelativeToComponent
Definition juce_Drawable.h:203
Drawable()
Definition juce_Drawable.cpp:29
static std::unique_ptr< Drawable > createFromImageFile(const File &file)
Definition juce_Drawable.cpp:191
void drawWithin(Graphics &g, Rectangle< float > destArea, RectanglePlacement placement, float opacity) const
Definition juce_Drawable.cpp:102
void setOriginWithOriginalSize(Point< float > originWithinParent)
Definition juce_Drawable.cpp:158
friend class DrawableComposite
Definition juce_Drawable.h:191
static std::unique_ptr< Drawable > createFromImageDataStream(InputStream &dataSource)
Definition juce_Drawable.cpp:183
void setClipPath(std::unique_ptr< Drawable > drawableClipPath)
Definition juce_Drawable.cpp:114
void setTransformToFit(const Rectangle< float > &areaInParent, RectanglePlacement placement)
Definition juce_Drawable.cpp:163
void transformContextToCorrectOrigin(Graphics &)
Definition juce_Drawable.cpp:123
void nonConstDraw(Graphics &, float opacity, const AffineTransform &)
Definition juce_Drawable.cpp:71
virtual bool replaceColour(Colour originalColour, Colour replacementColour)
Definition juce_Drawable.cpp:146
~Drawable() override
Definition juce_Drawable.cpp:50
virtual Rectangle< float > getDrawableBounds() const =0
Definition juce_File.h:45
Definition juce_FileInputStream.h:35
bool openedOk() const noexcept
Definition juce_FileInputStream.h:67
Definition juce_GraphicsContext.h:660
Definition juce_GraphicsContext.h:45
static Image loadFrom(InputStream &input)
Definition juce_ImageFileFormat.cpp:79
Definition juce_InputStream.h:37
Definition juce_MemoryOutputStream.h:36
Definition juce_Point.h:42
ValueType y
Definition juce_Point.h:247
ValueType x
Definition juce_Point.h:246
Definition juce_Rectangle.h:67
Rectangle< int > getSmallestIntegerContainer() const noexcept
Definition juce_Rectangle.h:840
bool isEmpty() const noexcept
Definition juce_Rectangle.h:121
Definition juce_RectanglePlacement.h:40
AffineTransform getTransformToFit(const Rectangle< float > &source, const Rectangle< float > &destination) const noexcept
Definition juce_RectanglePlacement.cpp:82
static String createStringFromData(const void *data, int size)
Definition juce_String.cpp:1968
int y
Definition inflate.c:1588
unsigned d
Definition inflate.c:940
int g
Definition inflate.c:1573
unsigned x[BMAX+1]
Definition inflate.c:1586
static uintptr_t parent
Definition pugl.h:1644
JSAMPIMAGE data
Definition jpeglib.h:945
Definition carla_juce.cpp:31
std::unique_ptr< XmlElement > parseXMLIfTagMatches(const String &textToParse, StringRef requiredTag)
Definition juce_XmlDocument.cpp:51
@ image
Definition juce_AccessibilityRole.h:42
return c
Definition crypt.c:175
struct zdirent * file
Definition win32.c:1500
mo
Definition zipinfo.c:2287
ss
Definition zipinfo.c:2292