LMMS
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
30 : Button (name), style (buttonStyle)
31{
32}
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
66
67 currentImage = nullptr;
68
70}
71
72//==============================================================================
74{
75 if (style != newStyle)
76 {
77 style = newStyle;
79 }
80}
81
82void DrawableButton::setEdgeIndent (const int numPixelsIndent)
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
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 {
126 transformFlags |= RectanglePlacement::stretchToFit;
127 }
128 else
129 {
130 transformFlags |= RectanglePlacement::centred;
131
133 transformFlags |= RectanglePlacement::doNotResize;
134 }
135
136 currentImage->setTransformToFit (getImageBounds(), transformFlags);
137 }
138 }
139}
140
142{
143 repaint();
144
145 Drawable* imageToDraw = nullptr;
146 float opacity = 1.0f;
147
148 if (isEnabled())
149 {
150 imageToDraw = getCurrentImage();
151 }
152 else
153 {
154 imageToDraw = getToggleState() ? disabledImageOn.get()
155 : disabledImage.get();
156
157 if (imageToDraw == nullptr)
158 {
159 opacity = 0.4f;
160 imageToDraw = getNormalImage();
161 }
162 }
163
164 if (imageToDraw != currentImage)
165 {
167 currentImage = imageToDraw;
168
169 if (currentImage != nullptr)
170 {
171 currentImage->setInterceptsMouseClicks (false, false);
173 resized();
174 }
175 }
176
177 if (currentImage != nullptr)
178 currentImage->setAlpha (opacity);
179}
180
186
191
193 const bool shouldDrawButtonAsHighlighted,
194 const bool shouldDrawButtonAsDown)
195{
196 auto& lf = getLookAndFeel();
197
199 lf.drawButtonBackground (g, *this,
202 shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown);
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
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
Type jmax(const Type a, const Type b)
Definition MathsFunctions.h:48
#define noexcept
Definition DistrhoDefines.h:72
float normal(const fft_t *freqs, off_t x)
Definition OscilGen.cpp:46
bool getToggleState() const noexcept
Definition juce_Button.h:127
Button(const String &buttonName)
Definition juce_Button.cpp:76
bool isDown() const noexcept
Definition juce_Button.cpp:317
bool isOver() const noexcept
Definition juce_Button.cpp:318
void enablementChanged() override
Definition juce_Button.cpp:273
int proportionOfWidth(float proportion) const noexcept
Definition juce_Component.cpp:1111
int proportionOfHeight(float proportion) const noexcept
Definition juce_Component.cpp:1112
int getHeight() const noexcept
Definition juce_Component.h:274
void addAndMakeVisible(Component *child, int zOrder=-1)
Definition juce_Component.cpp:1554
void repaint()
Definition juce_Component.cpp:1917
virtual void resized()
Definition juce_Component.cpp:2318
void removeChildComponent(Component *childToRemove)
Definition juce_Component.cpp:1569
Colour findColour(int colourID, bool inheritFromParent=false) const
Definition juce_Component.cpp:2219
int getWidth() const noexcept
Definition juce_Component.h:271
bool isEnabled() const noexcept
Definition juce_Component.cpp:3104
LookAndFeel & getLookAndFeel() const noexcept
Definition juce_Component.cpp:2173
Rectangle< int > getLocalBounds() const noexcept
Definition juce_Component.cpp:2283
Drawable * currentImage
Definition juce_DrawableButton.h:191
DrawableButton(const String &buttonName, ButtonStyle buttonStyle)
Definition juce_DrawableButton.cpp:29
bool shouldDrawButtonBackground() const
Definition juce_DrawableButton.h:185
ButtonStyle style
Definition juce_DrawableButton.h:188
void resized() override
Definition juce_DrawableButton.cpp:114
ButtonStyle
Definition juce_DrawableButton.h:45
@ ImageOnButtonBackgroundOriginalSize
Definition juce_DrawableButton.h:54
@ ImageAboveTextLabel
Definition juce_DrawableButton.h:49
@ ImageStretched
Definition juce_DrawableButton.h:55
@ ImageRaw
Definition juce_DrawableButton.h:47
std::unique_ptr< Drawable > downImageOn
Definition juce_DrawableButton.h:190
Drawable * getNormalImage() const noexcept
Definition juce_DrawableButton.cpp:216
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)
Definition juce_DrawableButton.cpp:47
void buttonStateChanged() override
Definition juce_DrawableButton.cpp:141
std::unique_ptr< Drawable > disabledImageOn
Definition juce_DrawableButton.h:190
Drawable * getCurrentImage() const noexcept
Definition juce_DrawableButton.cpp:208
std::unique_ptr< Drawable > downImage
Definition juce_DrawableButton.h:189
~DrawableButton() override
Definition juce_DrawableButton.cpp:34
virtual Rectangle< float > getImageBounds() const
Definition juce_DrawableButton.cpp:89
Drawable * getOverImage() const noexcept
Definition juce_DrawableButton.cpp:222
std::unique_ptr< Drawable > normalImage
Definition juce_DrawableButton.h:189
void enablementChanged() override
Definition juce_DrawableButton.cpp:181
Drawable * getDownImage() const noexcept
Definition juce_DrawableButton.cpp:233
std::unique_ptr< Drawable > overImage
Definition juce_DrawableButton.h:189
void setButtonStyle(ButtonStyle newStyle)
Definition juce_DrawableButton.cpp:73
int edgeIndent
Definition juce_DrawableButton.h:192
std::unique_ptr< Drawable > disabledImage
Definition juce_DrawableButton.h:189
std::unique_ptr< Drawable > overImageOn
Definition juce_DrawableButton.h:190
void paintButton(Graphics &, bool, bool) override
Definition juce_DrawableButton.cpp:192
std::unique_ptr< Drawable > normalImageOn
Definition juce_DrawableButton.h:190
void setEdgeIndent(int numPixelsIndent)
Definition juce_DrawableButton.cpp:82
void colourChanged() override
Definition juce_DrawableButton.cpp:187
Definition juce_Drawable.h:38
Definition juce_GraphicsContext.h:45
Definition juce_Rectangle.h:67
@ doNotResize
Definition juce_RectanglePlacement.h:115
@ centred
Definition juce_RectanglePlacement.h:119
@ stretchToFit
Definition juce_RectanglePlacement.h:90
Definition juce_String.h:53
@ buttonColourId
Definition juce_TextButton.h:73
@ buttonOnColourId
Definition juce_TextButton.h:76
unsigned d
Definition inflate.c:940
int g
Definition inflate.c:1573
static const char * name
Definition pugl.h:1582
#define jassert(expression)
Definition carla_juce.cpp:31
constexpr Type jmin(Type a, Type b)
Definition juce_MathsFunctions.h:106
static std::unique_ptr< Drawable > copyDrawableIfNotNull(const Drawable *const d)
Definition juce_DrawableButton.cpp:39
int r
Definition crypt.c:458
#define const
Definition zconf.h:137