24 template<
typename KeyType >
30 std::vector< Key< KeyType > > Keys;
31 std::vector< UPtr< Interpolator > > Interpolators;
38 bool AddKey ( TimeType time,
const KeyType& value );
39 bool AddKey ( TimeType time,
const KeyType& value, InterpolatorType type );
40 bool UpdateKey ( TimeType time,
const KeyType& value );
49 typename std::vector< Key< KeyType > >::iterator FindKey ( TimeType time );
50 typename std::vector< Key< KeyType > >::iterator FindPlace ( TimeType time );
65 extern const TimeType epsilon;
69 template<
typename KeyType,
template<
typename KeyType >
class InterpolatorType >
77 template<
typename KeyType >
81 firstKey.Time = TimeType( 0.0 );
82 firstKey.Value = KeyType();
84 Keys.push_back( std::move( firstKey ) );
89 template<
typename KeyType >
92 auto iter = FindKey( time );
93 if( iter != Keys.end() )
101 template<
typename KeyType >
104 auto iter = FindPlace( time );
106 if( iter == Keys.end() )
109 AddDefaultInterpolator( Keys.size() - 1 );
115 if( abs( iter->Time - time ) < epsilon )
123 AddDefaultInterpolator( std::distance( Keys.begin(), iter ) );
133 template<
typename KeyType >
141 template<
typename KeyType >
144 auto iter = FindKey( time );
145 if( iter == Keys.end() )
150 auto keyIdx = std::distance( Keys.begin(), iter );
151 UpdateInterpolator( keyIdx );
152 UpdateInterpolator( keyIdx - 1 );
161 template<
typename KeyType >
164 if( Keys.size() <= 1 )
167 auto iter = FindKey( time );
168 if( iter == Keys.end() )
171 iter = Keys.erase( iter );
174 auto keyIdx = std::distance( Keys.begin(), iter );
175 auto interIter = Interpolators.erase( Interpolators.begin() + keyIdx );
177 Size interpolatorIdx = std::distance( Interpolators.begin(), interIter );
178 UpdateInterpolator( interpolatorIdx );
179 UpdateInterpolator( interpolatorIdx - 1 );
186 template<
typename KeyType >
189 if( Keys.size() == 1 )
190 return Keys[ 0 ].Value;
194 auto iter = Keys.begin();
195 for( ; iter != Keys.end(); ++iter )
197 if( iter->Time > time )
201 Size keyIdx = std::distance( Keys.begin(), iter );
203 if( keyIdx >= Keys.size() )
204 return Keys.back().Value;
206 return Keys[ 0 ].Value;
208 return Interpolators[ keyIdx - 1 ]->Interpolate( time, Keys[ keyIdx - 1 ], Keys[ keyIdx ] );
213 template<
typename KeyType>
216 if( idx < Interpolators.size() )
218 Interpolators[ idx ] = std::move( interpolator );
219 UpdateInterpolator( idx );
234 template<
typename KeyType >
240 auto iterRange = std::equal_range( Keys.begin(), Keys.end(), fakeKey );
242 assert( std::distance( iterRange.first, iterRange.second ) <= 1 );
243 if( std::distance( iterRange.first, iterRange.second ) == 1 )
244 return iterRange.first;
251 template<
typename KeyType >
257 auto iter = std::lower_bound( Keys.begin(), Keys.end(), fakeKey );
263 template<
typename KeyType >
269 if( keyIndex == Keys.size() - 1 )
271 leftKeyIdx = keyIndex - 1;
272 rightKeyIdx = keyIndex;
276 leftKeyIdx = keyIndex;
277 rightKeyIdx = keyIndex + 1;
282 auto& leftKey = Keys[ leftKeyIdx ];
283 auto& rightKey = Keys[ rightKeyIdx ];
286 auto& leftInterpolator = keyIndex > 0 && keyIndex < Interpolators.size() ? Interpolators[ keyIndex - 1 ] : MakeUPtr< DummyInterpolator< KeyType > >();
287 auto& rightInterpolator = keyIndex < Interpolators.size() ? Interpolators[ keyIndex ] : MakeUPtr< DummyInterpolator< KeyType > >();
289 auto interpolator = DefaultInterpolators::Create< KeyType >( leftKey, rightKey, CastConst( leftInterpolator ), CastConst( rightInterpolator ) );
292 if( keyIndex == Keys.size() - 1 )
293 Interpolators.push_back( std::move( interpolator ) );
295 Interpolators.insert( Interpolators.begin() + keyIndex, std::move( interpolator ) );
299 UpdateInterpolator( keyIndex - 1 );
300 UpdateInterpolator( keyIndex + 1 );
305 template<
typename KeyType >
309 if( index >= Interpolators.size() )
313 assert( Keys.size() == Interpolators.size() + 1 );
315 auto& leftInterpolator = index >= 1 ? CastConst( Interpolators[ index - 1 ] ) : CastConst( MakeUPtr<
DummyInterpolator< KeyType > >() );
316 auto& rightInterpolator = index + 1 < Interpolators.size() ? CastConst( Interpolators[ index + 1 ] ) : CastConst( MakeUPtr<
DummyInterpolator< KeyType > >() );
318 auto& leftKey = Keys[ index ];
319 auto& rightKey = Keys[ index + 1 ];
321 auto& interpolator = Interpolators[ index ];
323 interpolator->Update( leftKey, rightKey, leftInterpolator, rightInterpolator );
Base class for interpolators.
Definition: IInterpolator.h:38
bool AddKey(TimeType time, const KeyType &value)
Adds key. If key exists updates this key.
Definition: KeySet.h:102
void UpdateInterpolator(Size index)
Updates interpolator under index. If interpolator doesn't exists, nothing bad happens.
Definition: KeySet.h:306
bool UpdateKey(TimeType time, const KeyType &value)
Updates key.
Definition: KeySet.h:142
KeyType Evaluate(TimeType time)
Definition: KeySet.h:187
Animation key.
Definition: IInterpolator.h:21
Set of animation keys and interpolators.
Definition: KeySet.h:25
KeySet()
Constructor insert default KeyType in time 0.0;.
Definition: KeySet.h:78
void AddDefaultInterpolator(Size keyIndex)
Adds default interpolator for given type. Adds interpolator defined by function DefaultInterpolators:...
Definition: KeySet.h:264
bool RemoveKey(TimeType time)
Removes key. Last key can't be removed.
Definition: KeySet.h:162
Temporary interpolator for internal use.
Definition: IInterpolator.h:67