2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../../core/juce_StandardHeader.h"
30 #include "juce_StretchableObjectResizer.h"
33 //==============================================================================
34 StretchableObjectResizer::StretchableObjectResizer()
38 StretchableObjectResizer::~StretchableObjectResizer()
42 void StretchableObjectResizer::addItem (const double size
,
43 const double minSize
, const double maxSize
,
46 // the order must be >= 0 but less than the maximum integer value.
47 jassert (order
>= 0 && order
< std::numeric_limits
<int>::max());
49 Item
* const item
= new Item();
51 item
->minSize
= minSize
;
52 item
->maxSize
= maxSize
;
57 double StretchableObjectResizer::getItemSize (const int index
) const noexcept
59 const Item
* const it
= items
[index
];
60 return it
!= nullptr ? it
->size
: 0;
63 void StretchableObjectResizer::resizeToFit (const double targetSize
)
69 double currentSize
= 0;
73 int nextHighestOrder
= std::numeric_limits
<int>::max();
75 for (int i
= 0; i
< items
.size(); ++i
)
77 const Item
* const it
= items
.getUnchecked(i
);
78 currentSize
+= it
->size
;
80 if (it
->order
<= order
)
82 minSize
+= it
->minSize
;
83 maxSize
+= it
->maxSize
;
89 nextHighestOrder
= jmin (nextHighestOrder
, it
->order
);
93 const double thisIterationTarget
= jlimit (minSize
, maxSize
, targetSize
);
95 if (thisIterationTarget
>= currentSize
)
97 const double availableExtraSpace
= maxSize
- currentSize
;
98 const double targetAmountOfExtraSpace
= thisIterationTarget
- currentSize
;
99 const double scale
= targetAmountOfExtraSpace
/ availableExtraSpace
;
101 for (int i
= 0; i
< items
.size(); ++i
)
103 Item
* const it
= items
.getUnchecked(i
);
105 if (it
->order
<= order
)
106 it
->size
= jmin (it
->maxSize
, it
->size
+ (it
->maxSize
- it
->size
) * scale
);
111 const double amountOfSlack
= currentSize
- minSize
;
112 const double targetAmountOfSlack
= thisIterationTarget
- minSize
;
113 const double scale
= targetAmountOfSlack
/ amountOfSlack
;
115 for (int i
= 0; i
< items
.size(); ++i
)
117 Item
* const it
= items
.getUnchecked(i
);
119 if (it
->order
<= order
)
120 it
->size
= jmax (it
->minSize
, it
->minSize
+ (it
->size
- it
->minSize
) * scale
);
124 if (nextHighestOrder
< std::numeric_limits
<int>::max())
125 order
= nextHighestOrder
;