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_StretchableObjectResizer.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
31
32void StretchableObjectResizer::addItem (const double size,
33 const double minSize, const double maxSize,
34 const int order)
35{
36 // the order must be >= 0 but less than the maximum integer value.
37 jassert (order >= 0 && order < std::numeric_limits<int>::max());
38 jassert (maxSize >= minSize);
39
40 Item item;
41 item.size = size;
42 item.minSize = minSize;
43 item.maxSize = maxSize;
44 item.order = order;
45 items.add (item);
46}
47
48double StretchableObjectResizer::getItemSize (const int index) const noexcept
49{
50 return isPositiveAndBelow (index, items.size()) ? items.getReference (index).size
51 : 0.0;
52}
53
55{
56 int order = 0;
57
58 for (;;)
59 {
60 double currentSize = 0;
61 double minSize = 0;
62 double maxSize = 0;
63
65
66 for (int i = 0; i < items.size(); ++i)
67 {
68 const Item& it = items.getReference (i);
69 currentSize += it.size;
70
71 if (it.order <= order)
72 {
73 minSize += it.minSize;
74 maxSize += it.maxSize;
75 }
76 else
77 {
78 minSize += it.size;
79 maxSize += it.size;
81 }
82 }
83
84 const double thisIterationTarget = jlimit (minSize, maxSize, targetSize);
85
86 if (thisIterationTarget >= currentSize)
87 {
88 const double availableExtraSpace = maxSize - currentSize;
89 const double targetAmountOfExtraSpace = thisIterationTarget - currentSize;
90 const double scale = availableExtraSpace > 0 ? targetAmountOfExtraSpace / availableExtraSpace : 1.0;
91
92 for (int i = 0; i < items.size(); ++i)
93 {
94 Item& it = items.getReference (i);
95
96 if (it.order <= order)
97 it.size = jlimit (it.minSize, it.maxSize, it.size + (it.maxSize - it.size) * scale);
98 }
99 }
100 else
101 {
102 const double amountOfSlack = currentSize - minSize;
103 const double targetAmountOfSlack = thisIterationTarget - minSize;
104 const double scale = targetAmountOfSlack / amountOfSlack;
105
106 for (int i = 0; i < items.size(); ++i)
107 {
108 Item& it = items.getReference (i);
109
110 if (it.order <= order)
111 it.size = jmax (it.minSize, it.minSize + (it.size - it.minSize) * scale);
112 }
113 }
114
116 order = nextHighestOrder;
117 else
118 break;
119 }
120}
121
122} // namespace juce
int size() const noexcept
Returns the current number of elements in the array.
Definition juce_Array.h:215
void add(const ElementType &newElement)
Appends a new element at the end of the array.
Definition juce_Array.h:418
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
Definition juce_Array.h:267
void addItem(double currentSize, double minSize, double maxSize, int order=0)
Adds an item to the list.
double getItemSize(int index) const noexcept
Returns the size of one of the items.
void resizeToFit(double targetSize)
Resizes all the items to fit this amount of space.
StretchableObjectResizer()
Creates an empty object resizer.
#define jassert(expression)
Platform-independent assertion macro.
T max(T... args)
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 jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
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
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Returns true if a value is at least zero, and also below a specified upper limit.