LMMS
Loading...
Searching...
No Matches
juce_ImageButton.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 (text_),
34{
35}
36
40
41void ImageButton::setImages (const bool resizeButtonNowToFitThisImage,
42 const bool rescaleImagesWhenButtonSizeChanges,
43 const bool preserveImageProportions,
44 const Image& normalImage_,
45 const float imageOpacityWhenNormal,
46 Colour overlayColourWhenNormal,
47 const Image& overImage_,
48 const float imageOpacityWhenOver,
49 Colour overlayColourWhenOver,
50 const Image& downImage_,
51 const float imageOpacityWhenDown,
52 Colour overlayColourWhenDown,
53 const float hitTestAlphaThreshold)
54{
55 normalImage = normalImage_;
56 overImage = overImage_;
57 downImage = downImage_;
58
59 if (resizeButtonNowToFitThisImage && normalImage.isValid())
60 {
61 imageBounds.setSize (normalImage.getWidth(),
62 normalImage.getHeight());
63
64 setSize (imageBounds.getWidth(), imageBounds.getHeight());
65 }
66
67 scaleImageToFit = rescaleImagesWhenButtonSizeChanges;
68 preserveProportions = preserveImageProportions;
69
70 normalOpacity = imageOpacityWhenNormal;
71 normalOverlay = overlayColourWhenNormal;
72 overOpacity = imageOpacityWhenOver;
73 overOverlay = overlayColourWhenOver;
74 downOpacity = imageOpacityWhenDown;
75 downOverlay = overlayColourWhenDown;
76
77 alphaThreshold = (uint8) jlimit (0, 0xff, roundToInt (255.0f * hitTestAlphaThreshold));
78
79 repaint();
80}
81
83{
84 if (isDown() || getToggleState())
85 return getDownImage();
86
87 if (isOver())
88 return getOverImage();
89
90 return getNormalImage();
91}
92
97
99{
100 return overImage.isValid() ? overImage
101 : normalImage;
102}
103
105{
106 return downImage.isValid() ? downImage
107 : getOverImage();
108}
109
111 bool shouldDrawButtonAsHighlighted,
112 bool shouldDrawButtonAsDown)
113{
114 if (! isEnabled())
115 {
116 shouldDrawButtonAsHighlighted = false;
117 shouldDrawButtonAsDown = false;
118 }
119
120 Image im (getCurrentImage());
121
122 if (im.isValid())
123 {
124 const int iw = im.getWidth();
125 const int ih = im.getHeight();
126 int w = getWidth();
127 int h = getHeight();
128 int x = (w - iw) / 2;
129 int y = (h - ih) / 2;
130
131 if (scaleImageToFit)
132 {
134 {
135 int newW, newH;
136 const float imRatio = (float) ih / (float) iw;
137 const float destRatio = (float) h / (float) w;
138
139 if (imRatio > destRatio)
140 {
141 newW = roundToInt ((float) h / imRatio);
142 newH = h;
143 }
144 else
145 {
146 newW = w;
147 newH = roundToInt ((float) w * imRatio);
148 }
149
150 x = (w - newW) / 2;
151 y = (h - newH) / 2;
152 w = newW;
153 h = newH;
154 }
155 else
156 {
157 x = 0;
158 y = 0;
159 }
160 }
161
162 if (! scaleImageToFit)
163 {
164 w = iw;
165 h = ih;
166 }
167
168 imageBounds.setBounds (x, y, w, h);
169
170 const bool useDownImage = shouldDrawButtonAsDown || getToggleState();
171
172 getLookAndFeel().drawImageButton (g, &im, x, y, w, h,
173 useDownImage ? downOverlay
174 : (shouldDrawButtonAsHighlighted ? overOverlay
175 : normalOverlay),
176 useDownImage ? downOpacity
177 : (shouldDrawButtonAsHighlighted ? overOpacity
178 : normalOpacity),
179 *this);
180 }
181}
182
183bool ImageButton::hitTest (int x, int y)
184{
185 if (! Component::hitTest (x, y)) // handle setInterceptsMouseClicks
186 return false;
187
188 if (alphaThreshold == 0)
189 return true;
190
191 Image im (getCurrentImage());
192
193 return im.isNull() || ((! imageBounds.isEmpty())
194 && alphaThreshold < im.getPixelAt (((x - imageBounds.getX()) * im.getWidth()) / imageBounds.getWidth(),
195 ((y - imageBounds.getY()) * im.getHeight()) / imageBounds.getHeight()).getAlpha());
196}
197
198} // namespace juce
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
Definition juce_Colour.h:38
uint8 getAlpha() const noexcept
Definition juce_Colour.h:211
int getHeight() const noexcept
Definition juce_Component.h:274
void repaint()
Definition juce_Component.cpp:1917
virtual bool hitTest(int x, int y)
Definition juce_Component.cpp:1400
void setSize(int newWidth, int newHeight)
Definition juce_Component.cpp:1262
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
Definition juce_GraphicsContext.h:45
Colour downOverlay
Definition juce_ImageButton.h:154
bool preserveProportions
Definition juce_ImageButton.h:149
Rectangle< int > imageBounds
Definition juce_ImageButton.h:151
Colour overOverlay
Definition juce_ImageButton.h:154
float downOpacity
Definition juce_ImageButton.h:153
Image getCurrentImage() const
Definition juce_ImageButton.cpp:82
Image getNormalImage() const
Definition juce_ImageButton.cpp:93
Image getDownImage() const
Definition juce_ImageButton.cpp:104
Image normalImage
Definition juce_ImageButton.h:152
float overOpacity
Definition juce_ImageButton.h:153
uint8 alphaThreshold
Definition juce_ImageButton.h:150
~ImageButton() override
Definition juce_ImageButton.cpp:37
void paintButton(Graphics &, bool, bool) override
Definition juce_ImageButton.cpp:110
void setImages(bool resizeButtonNowToFitThisImage, bool rescaleImagesWhenButtonSizeChanges, bool preserveImageProportions, const Image &normalImage, float imageOpacityWhenNormal, Colour overlayColourWhenNormal, const Image &overImage, float imageOpacityWhenOver, Colour overlayColourWhenOver, const Image &downImage, float imageOpacityWhenDown, Colour overlayColourWhenDown, float hitTestAlphaThreshold=0.0f)
Definition juce_ImageButton.cpp:41
bool hitTest(int x, int y) override
Definition juce_ImageButton.cpp:183
float normalOpacity
Definition juce_ImageButton.h:153
Image getOverImage() const
Definition juce_ImageButton.cpp:98
Image overImage
Definition juce_ImageButton.h:152
Image downImage
Definition juce_ImageButton.h:152
bool scaleImageToFit
Definition juce_ImageButton.h:149
ImageButton(const String &name=String())
Definition juce_ImageButton.cpp:29
Colour normalOverlay
Definition juce_ImageButton.h:154
Definition juce_Image.h:58
int getWidth() const noexcept
Definition juce_Image.cpp:271
Colour getPixelAt(int x, int y) const
Definition juce_Image.cpp:464
int getHeight() const noexcept
Definition juce_Image.cpp:272
bool isNull() const noexcept
Definition juce_Image.h:155
bool isValid() const noexcept
Definition juce_Image.h:147
Definition juce_String.h:53
UINT_D64 w
Definition inflate.c:942
int y
Definition inflate.c:1588
int g
Definition inflate.c:1573
unsigned x[BMAX+1]
Definition inflate.c:1586
Definition carla_juce.cpp:31
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Definition juce_MathsFunctions.h:262
unsigned char uint8
Definition juce_MathsFunctions.h:37
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
#define true
Definition ordinals.h:82
uch h[RAND_HEAD_LEN]
Definition crypt.c:459