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_DrawableButton.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
33
37
38//==============================================================================
39static std::unique_ptr<Drawable> copyDrawableIfNotNull (const Drawable* const d)
40{
41 if (d != nullptr)
42 return d->createCopy();
43
44 return {};
45}
46
48 const Drawable* over,
49 const Drawable* down,
50 const Drawable* disabled,
51 const Drawable* normalOn,
52 const Drawable* overOn,
53 const Drawable* downOn,
54 const Drawable* disabledOn)
55{
56 jassert (normal != nullptr); // you really need to give it at least a normal image..
57
58 normalImage = copyDrawableIfNotNull (normal);
59 overImage = copyDrawableIfNotNull (over);
60 downImage = copyDrawableIfNotNull (down);
61 disabledImage = copyDrawableIfNotNull (disabled);
62 normalImageOn = copyDrawableIfNotNull (normalOn);
63 overImageOn = copyDrawableIfNotNull (overOn);
64 downImageOn = copyDrawableIfNotNull (downOn);
65 disabledImageOn = copyDrawableIfNotNull (disabledOn);
66
67 currentImage = nullptr;
68
70}
71
72//==============================================================================
74{
75 if (style != newStyle)
76 {
77 style = newStyle;
79 }
80}
81
83{
84 edgeIndent = numPixelsIndent;
85 repaint();
86 resized();
87}
88
90{
91 auto r = getLocalBounds();
92
93 if (style != ImageStretched)
94 {
95 auto indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
96 auto indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
97
98 if (shouldDrawButtonBackground())
99 {
100 indentX = jmax (getWidth() / 4, indentX);
101 indentY = jmax (getHeight() / 4, indentY);
102 }
103 else if (style == ImageAboveTextLabel)
104 {
105 r = r.withTrimmedBottom (jmin (16, proportionOfHeight (0.25f)));
106 }
107
108 r = r.reduced (indentX, indentY);
109 }
110
111 return r.toFloat();
112}
113
115{
117
118 if (currentImage != nullptr)
119 {
120 if (style != ImageRaw)
121 {
122 int transformFlags = 0;
123
124 if (style == ImageStretched)
125 {
127 }
128 else
129 {
131
132 if (style == ImageOnButtonBackgroundOriginalSize)
134 }
135
137 }
138 }
139}
140
142{
143 repaint();
144
145 Drawable* imageToDraw = nullptr;
146 float opacity = 1.0f;
147
148 if (isEnabled())
149 {
151 }
152 else
153 {
154 imageToDraw = getToggleState() ? disabledImageOn.get()
155 : disabledImage.get();
156
157 if (imageToDraw == nullptr)
158 {
159 opacity = 0.4f;
161 }
162 }
163
164 if (imageToDraw != currentImage)
165 {
166 removeChildComponent (currentImage);
167 currentImage = imageToDraw;
168
169 if (currentImage != nullptr)
170 {
171 currentImage->setInterceptsMouseClicks (false, false);
172 addAndMakeVisible (currentImage);
173 resized();
174 }
175 }
176
177 if (currentImage != nullptr)
178 currentImage->setAlpha (opacity);
179}
180
186
191
194 const bool shouldDrawButtonAsDown)
195{
196 auto& lf = getLookAndFeel();
197
198 if (shouldDrawButtonBackground())
199 lf.drawButtonBackground (g, *this,
203 else
204 lf.drawDrawableButton (g, *this, shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown);
205}
206
207//==============================================================================
209{
210 if (isDown()) return getDownImage();
211 if (isOver()) return getOverImage();
212
213 return getNormalImage();
214}
215
217{
218 return (getToggleState() && normalImageOn != nullptr) ? normalImageOn.get()
219 : normalImage.get();
220}
221
223{
224 if (getToggleState())
225 {
226 if (overImageOn != nullptr) return overImageOn.get();
227 if (normalImageOn != nullptr) return normalImageOn.get();
228 }
229
230 return overImage != nullptr ? overImage.get() : normalImage.get();
231}
232
234{
235 if (auto* d = getToggleState() ? downImageOn.get() : downImage.get())
236 return d;
237
238 return getOverImage();
239}
240
241} // namespace juce
A base class for buttons.
Definition juce_Button.h:43
bool getToggleState() const noexcept
Returns true if the button is 'on'.
bool isDown() const noexcept
Returns true if the button is currently being held down.
bool isOver() const noexcept
Returns true if the mouse is currently over the button.
void enablementChanged() override
Callback to indicate that this component has been enabled or disabled.
int proportionOfWidth(float proportion) const noexcept
Returns a proportion of the component's width.
void setInterceptsMouseClicks(bool allowClicksOnThisComponent, bool allowClicksOnChildComponents) noexcept
Changes the default return value for the hitTest() method.
int proportionOfHeight(float proportion) const noexcept
Returns a proportion of the component's height.
int getHeight() const noexcept
Returns the component's height in pixels.
void setAlpha(float newAlpha)
Changes the transparency of this component.
void addAndMakeVisible(Component *child, int zOrder=-1)
Adds a child component to this one, and also makes the child visible if it isn't already.
void repaint()
Marks the whole component as needing to be redrawn.
virtual void resized()
Called when this component's size has been changed.
void removeChildComponent(Component *childToRemove)
Removes one of this component's child-components.
Colour findColour(int colourID, bool inheritFromParent=false) const
Looks for a colour that has been registered with the given colour ID number.
int getWidth() const noexcept
Returns the component's width in pixels.
bool isEnabled() const noexcept
Returns true if the component (and all its parents) are enabled.
LookAndFeel & getLookAndFeel() const noexcept
Finds the appropriate look-and-feel to use for this component.
Rectangle< int > getLocalBounds() const noexcept
Returns the component's bounds, relative to its own origin.
DrawableButton(const String &buttonName, ButtonStyle buttonStyle)
Creates a DrawableButton.
void resized() override
Called when this component's size has been changed.
@ ImageAboveTextLabel
Draws the button as a text label across the bottom with the image resized and scaled to fit above it.
@ ImageStretched
Same as ImageOnButtonBackground, but keeps the original image size.
@ ImageRaw
The button will just display the images in their normal size and position.
Drawable * getNormalImage() const noexcept
Returns the image that the button will use for its normal state.
void setImages(const Drawable *normalImage, const Drawable *overImage=nullptr, const Drawable *downImage=nullptr, const Drawable *disabledImage=nullptr, const Drawable *normalImageOn=nullptr, const Drawable *overImageOn=nullptr, const Drawable *downImageOn=nullptr, const Drawable *disabledImageOn=nullptr)
Sets up the images to draw for the various button states.
void buttonStateChanged() override
Called when the button's up/down/over state changes.
Drawable * getCurrentImage() const noexcept
Returns the image that the button is currently displaying.
~DrawableButton() override
Destructor.
virtual Rectangle< float > getImageBounds() const
Can be overridden to specify a custom position for the image within the button.
Drawable * getOverImage() const noexcept
Returns the image that the button will use when the mouse is over it.
void enablementChanged() override
Callback to indicate that this component has been enabled or disabled.
Drawable * getDownImage() const noexcept
Returns the image that the button will use when the mouse is held down on it.
void setButtonStyle(ButtonStyle newStyle)
Changes the button's style.
void paintButton(Graphics &, bool, bool) override
Subclasses should override this to actually paint the button's contents.
void setEdgeIndent(int numPixelsIndent)
Gives the button an optional amount of space around the edge of the drawable.
void colourChanged() override
This method is called when a colour is changed by the setColour() method, or when the look-and-feel i...
The base class for objects which can draw themselves, e.g.
virtual std::unique_ptr< Drawable > createCopy() const =0
Creates a deep copy of this Drawable object.
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...
A graphics context, used for drawing a component or image.
@ doNotResize
Indicates that the source rectangle's size should be left unchanged.
@ centred
A shorthand value that is equivalent to (xMid | yMid).
@ stretchToFit
If this flag is set, then the source rectangle will be resized to completely fill the destination rec...
Manages a rectangle and allows geometric operations to be performed on it.
The JUCE String class!
Definition juce_String.h:53
@ buttonColourId
The colour used to fill the button shape (when the button is toggled 'off').
@ buttonOnColourId
The colour used to fill the button shape (when the button is toggled 'on').
#define jassert(expression)
Platform-independent assertion macro.
JUCE Namespace.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
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