-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShadow.h
More file actions
330 lines (296 loc) · 11.6 KB
/
Shadow.h
File metadata and controls
330 lines (296 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//==============================================================================
/* ██████╗ ██╗███╗ ███╗███████╗████████╗██╗ ██╗ ██████╗ ██╗ ██╗██╗ ██╗
* ██╔══██╗██║████╗ ████║██╔════╝╚══██╔══╝██║ ██║██╔═══██╗╚██╗██╔╝╚██╗ ██╔╝
* ██║ ██║██║██╔████╔██║█████╗ ██║ ███████║██║ ██║ ╚███╔╝ ╚████╔╝
* ██║ ██║██║██║╚██╔╝██║██╔══╝ ██║ ██╔══██║██║ ██║ ██╔██╗ ╚██╔╝
* ██████╔╝██║██║ ╚═╝ ██║███████╗ ██║ ██║ ██║╚██████╔╝██╔╝ ██╗ ██║
* ╚═════╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝
* Copyright (C) 2024 Dimethoxy Audio (https://dimethoxy.com)
*
* Part of the Dimethoxy Library, primarily intended for Dimethoxy plugins.
* External use is permitted but not recommended.
* No support or compatibility guarantees are provided.
*
* License:
* This code is licensed under the GPLv3 license. You are permitted to use and
* modify this code under the terms of this license.
* You must adhere GPLv3 license for any project using this code or parts of it.
* Your are not allowed to use this code in any closed-source project.
*
* Description:
* Shadow component for JUCE, supporting both inner and outer drop shadows
* for arbitrary paths. Designed for real-time GUI rendering.
*
* Authors:
* Lunix-420 (Primary Author)
*/
//==============================================================================
#pragma once
//==============================================================================
#include "utility/Scaleable.h"
#include "utility/Settings.h"
#include <JuceHeader.h>
#include <melatonin_blur/melatonin_blur.h>
//==============================================================================
namespace dmt {
namespace gui {
namespace widget {
//==============================================================================
/**
* @brief Component for rendering drop shadows on arbitrary paths.
*
* @details
* This class provides a reusable JUCE component for drawing both inner and
* outer drop shadows on any path. It is optimized for real-time GUI use,
* caching the shadow image and only repainting when necessary. The shadow
* can be toggled visible/invisible, and supports dynamic resizing and path
* changes. Designed for use in custom widgets and panels.
*/
class Shadow
: public juce::Component
, public dmt::Scaleable<Shadow>
{
//==============================================================================
// Alias for convenience
using Image = juce::Image;
using Graphics = juce::Graphics;
using String = juce::String;
using PixelFormat = juce::Image::PixelFormat;
public:
/**
* @brief Constructs a Shadow component.
*
* @param _visibility Whether the shadow is visible.
* @param _colour The colour of the shadow.
* @param _radius The blur radius for the shadow.
* @param _inner If true, draws an inner shadow; otherwise, draws an outer
* shadow.
*
* @details
* The constructor is constexpr for macOS compatibility. All parameters are
* stored as references or values for maximum efficiency and flexibility.
*/
explicit Shadow(const bool& _visibility,
const juce::Colour& _colour,
const float& _radius,
const bool _inner) noexcept
: visibility(_visibility)
, colour(&_colour)
, radius(_radius)
, inner(_inner)
{
}
//==============================================================================
/**
* @brief Paints the shadow image onto the component.
*
* @param _g The graphics context.
*
* @details
* If the shadow is not visible, nothing is drawn. If the cached image is
* valid, it is drawn directly for performance. Otherwise, the shadow is
* re-rendered to the image and then drawn.
*/
inline void paint(juce::Graphics& _g) override
{
TRACER("Shadow::paint");
if (!visibility)
return;
refreshCachedImageIfNeeded();
if (!needsRepaint) {
_g.drawImage(image,
0.0f,
0.0f,
static_cast<float>(getWidth()),
static_cast<float>(getHeight()),
0,
0,
image.getWidth(),
image.getHeight());
return;
}
juce::Graphics imageGraphics(image);
imageGraphics.addTransform(juce::AffineTransform::scale(scale, scale));
imageGraphics.fillAll(juce::Colours::transparentBlack);
imageGraphics.setColour(*colour); // dereference pointer
if (inner)
drawInnerForPath(imageGraphics, path);
else
drawOuterForPath(imageGraphics, path);
needsRepaint = false;
_g.drawImage(image,
0.0f,
0.0f,
static_cast<float>(getWidth()),
static_cast<float>(getHeight()),
0,
0,
image.getWidth(),
image.getHeight());
}
//==============================================================================
/**
* @brief Handles resizing of the shadow component.
*
* @details
* When the component is resized, the shadow image is recreated to match
* the new dimensions, and a repaint is triggered. If the new size is zero,
* nothing is done.
*/
inline void resized() override
{
TRACER("Shadow::resized");
refreshCachedImageIfNeeded(true);
}
//==============================================================================
/**
* @brief Changes the Colour of the shadow.
*
* @param _newColour The new colour to use for the shadow.
*
* @details
* Updates the internal colour and triggers a repaint to update the shadow
* image.
*/
inline void setColour(const juce::Colour& _newColour)
{
TRACER("Shadow::setColour");
colour = &_newColour;
needsRepaint = true;
repaint();
}
//==============================================================================
/**
* @brief Sets the path for which the shadow is rendered.
*
* @param _newPath The new path to use for the shadow.
*
* @details
* Updates the internal path and triggers a resize to update the shadow image.
*/
inline void setPath(juce::Path _newPath)
{
TRACER("Shadow::setPath");
path = _newPath;
resized();
}
//==============================================================================
/**
* @brief Directly draws the shadow for the given path.
*
* @param _g The graphics context.
* @param _target The path to shadow.
*
* @details
* This method is used if the shadow isn't used as a component, allowing
* for direct drawing of the shadow on a given graphics context.
* It doesn't do any caching or image management, so it's suitable for one-off
* rendering tasks.
*/
inline void directDraw(juce::Graphics& _g, juce::Path _target)
{
TRACER("Shadow::directDraw");
if (inner)
drawInnerForPath(_g, _target);
else
drawOuterForPath(_g, _target);
}
protected:
//==============================================================================
/**
* @brief Draws an inner drop shadow for the given path.
*
* @param _g The graphics context.
* @param _target The path to shadow.
*
* @details
* Uses JUCE's DropShadow to render an inner shadow by clipping to the
* target path and drawing the shadow on an expanded rectangle.
*/
inline void drawInnerForPath(juce::Graphics& _g, juce::Path _target)
{
TRACER("Shadow::drawInnerForPath");
updateShadowParameters();
innerShadowRenderer.render(_g, _target);
}
//==============================================================================
/**
* @brief Draws an outer drop shadow for the given path.
*
* @param _g The graphics context.
* @param _target The path to shadow.
*
* @details
* Uses JUCE's DropShadow to render an outer shadow by clipping to the
* expanded shadow path and drawing the shadow on the original path.
*/
inline void drawOuterForPath(juce::Graphics& _g, juce::Path _target)
{
TRACER("Shadow::drawOuterForPath");
juce::Graphics::ScopedSaveState saveState(_g);
juce::Path shadowPath(_target);
shadowPath.addRectangle(_target.getBounds().expanded(10.0f));
shadowPath.setUsingNonZeroWinding(false);
_g.reduceClipRegion(shadowPath);
updateShadowParameters();
outerShadowRenderer.render(_g, _target);
}
private:
inline void updateShadowParameters()
{
// Rendering uses a graphics transform for HiDPI, so keep shadow parameters
// in logical units to avoid applying scale twice.
const auto scaledRadius = static_cast<double>(radius * size);
const auto scaledOffset = juce::Point<float>(static_cast<float>(offset.x),
static_cast<float>(offset.y));
if (inner) {
innerShadowRenderer.setColor(*colour)
.setRadius(scaledRadius)
.setOffset(scaledOffset);
return;
}
outerShadowRenderer.setColor(*colour)
.setRadius(scaledRadius)
.setOffset(scaledOffset);
}
inline void refreshCachedImageIfNeeded(bool forceRepaint = false)
{
if (getWidth() == 0 || getHeight() == 0)
return;
const auto currentScale = static_cast<float>(scale);
const auto scaledWidth =
juce::jmax(1, juce::roundToInt(getWidth() * currentScale));
const auto scaledHeight =
juce::jmax(1, juce::roundToInt(getHeight() * currentScale));
const auto scaleChanged =
!juce::approximatelyEqual(lastRenderedScale, currentScale);
const auto imageSizeChanged =
image.getWidth() != scaledWidth || image.getHeight() != scaledHeight;
if (!forceRepaint && !scaleChanged && !imageSizeChanged)
return;
image = Image(PixelFormat::ARGB, scaledWidth, scaledHeight, true);
lastRenderedScale = currentScale;
needsRepaint = true;
}
//==============================================================================
// Members initialized in the initializer list
const bool& visibility;
const juce::Colour* colour; // pointer instead of reference
const float& radius;
const bool inner;
//==============================================================================
// Other members
juce::Point<int> offset = { 0, 0 };
juce::Path path;
bool needsRepaint = true;
float lastRenderedScale = 0.0f;
melatonin::DropShadow outerShadowRenderer;
melatonin::InnerShadow innerShadowRenderer;
Image image = Image(PixelFormat::ARGB, 1, 1, true);
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Shadow)
};
//==============================================================================
} // namespace widget
} // namespace gui
} // namespace dmt