1 #ifndef DALI_INTERNAL_KEY_FRAMES_H
2 #define DALI_INTERNAL_KEY_FRAMES_H
5 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/animation/key-frames.h>
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/public-api/animation/alpha-function.h>
26 #include <dali/internal/event/animation/progress-value.h>
27 #include <dali/internal/event/animation/key-frame-channel.h>
35 using KeyFramesPtr = IntrusivePtr<KeyFrames>;
38 * KeyFrames class is responsible for creating and building a specialized KeyFrame class
39 * from the Property::Value type used in Add.
41 class KeyFrames : public BaseObject
44 static KeyFrames* New();
47 * Instantiate an empty KeyFrames object
52 ~KeyFrames() override;
56 * Don't allow copy constructor
58 KeyFrames(const KeyFrames& rhs);
61 * Don't allow copy operator
63 KeyFrames& operator=(const KeyFrames& rhs);
66 * Create a specialization from the given type, and store it to the mSpec
69 void CreateKeyFramesSpec(Property::Type type);
73 * Get the type of this key frame.
74 * An empty key frame will return Property::NONE, wheras an initialised
75 * key frame object will return the type of it's first element.
76 * @return the Property::Type of the key frame values
78 Property::Type GetType() const;
81 * Add a key frame. The first key frame to be added denotes the type
82 * of all subsequent key frames. If a value with a different type is
83 * added, it will throw a run time assert.
84 * @param[in] time The time (between 0 and 1)
85 * @param[in] value The value of the keyframe at the given time
86 * @param[in] alpha An alpha function to blend between this key frame and the
89 void Add(float time, Property::Value value, AlphaFunction alpha);
92 * Return the key frames without specialization. The GetSpecialization methods
93 * below will convert to the specialized objects.
95 KeyFrameSpec* GetKeyFramesBase() const;
98 * Return the value of the last key frame.
100 Dali::Property::Value GetLastKeyFrameValue() const;
103 Dali::Property::Type mType; // Type of the specialization
104 IntrusivePtr<KeyFrameSpec> mKeyFrames; // Pointer to the specialized key frame object
109 * This is the base class for the individual template specializations, allowing a ptr to be
110 * stored in the handle object above. It inherits from RefObject to allow smart pointers
111 * to be used for the specializations. Note that the derived template class below
112 * allows for a copy constructor so that the specialization object can be cloned before
113 * being passed to the scene-graph for animation.
115 class KeyFrameSpec : public RefObject
121 virtual std::size_t GetNumberOfKeyFrames() const = 0;
124 * Get the key frame value as a Property::Value.
125 * @param[in] index The index of the key frame to fetch
126 * @param[out] value The value of the given key frame
128 virtual void GetKeyFrameAsValue( std::size_t index, Property::Value& value ) = 0;
133 * A reference counted object may only be deleted by calling Unreference()
135 ~KeyFrameSpec() override {}
140 * The base template class for each key frame specialization. It stores a vector of
141 * ProgressValue pairs in mPVs, and uses the existing interface for KeyFrameChannel
142 * to point at this vector.
145 class KeyFrameBaseSpec : public KeyFrameSpec
148 using PV = ProgressValue<V>;
149 using PVContainer = std::vector<PV>;
151 PVContainer mPVs; // The ProgressValue pairs
152 KeyFrameChannel<V>* mKeyFrames; // The key frame interpolator
155 static KeyFrameBaseSpec<V>* New()
157 return new KeyFrameBaseSpec<V>();
160 static KeyFrameBaseSpec<V>* Clone(const KeyFrameBaseSpec<V>& keyFrames)
162 return new KeyFrameBaseSpec<V>(keyFrames);
168 KeyFrameBaseSpec<V>()
170 mKeyFrames = new KeyFrameChannel<V>(KeyFrameChannelBase::Translate, mPVs);
176 * Allow cloning of this object
178 KeyFrameBaseSpec<V>(const KeyFrameBaseSpec<V>& keyFrames)
179 : mPVs(keyFrames.mPVs)
181 mKeyFrames = new KeyFrameChannel<V>(KeyFrameChannelBase::Translate, mPVs);
184 KeyFrameBaseSpec<V>& operator=( const KeyFrameBaseSpec<V>& keyFrames )
186 if( this != &keyFrames )
189 mPVs = keyFrames.mPVs;
191 mKeyFrames = new KeyFrameChannel<V>(KeyFrameChannelBase::Translate, mPVs);
197 * Destructor. Ensure progress value pairs are cleared down
199 ~KeyFrameBaseSpec<V>() override
207 * Add a key frame to the progress value vector. Key frames should be added
208 * in time order (this method does not sort the vector by time)
209 * @param[in] t - progress
210 * @param[in] v - value
211 * @param[in] alpha - Alpha function for blending to the next keyframe
213 void AddKeyFrame(float t, V v, AlphaFunction alpha)
215 mPVs.push_back(PV(t, v));
219 * Get the number of key frames
220 * @return The size of the progress value vector
222 std::size_t GetNumberOfKeyFrames() const override
229 * @param[in] index The index of the key frame to fetch
230 * @param[out] time The progress of the given key frame
231 * @param[out] value The value of the given key frame
233 virtual void GetKeyFrame(unsigned int index, float& time, V& value) const
235 DALI_ASSERT_ALWAYS( index < mPVs.size() && "KeyFrame index is out of bounds" );
236 time = mPVs[index].mProgress;
237 value = mPVs[index].mValue;
241 * @copydoc KeyFrameSpec::GetKeyFrameAsValue()
243 void GetKeyFrameAsValue( std::size_t index, Property::Value& value ) override
245 value = mPVs[index].mValue;
249 * Return whether the progress is valid for the range of keyframes. (The first
250 * keyframe doesn't have to start at 0, and the last doesn't have to end at 1.0)
251 * @param[in] progress The progress to test
252 * @return True if the progress is valid for this object
254 bool IsActive(float progress) const
256 return mKeyFrames->IsActive(progress);
260 * Return an interpolated value for the given progress.
261 * @param[in] progress The progress to test
262 * @return The interpolated value
264 V GetValue(float progress, Dali::Animation::Interpolation interpolation) const
266 return mKeyFrames->GetValue(progress, interpolation);
270 using KeyFrameNumber = KeyFrameBaseSpec<float>;
271 using KeyFrameBoolean = KeyFrameBaseSpec<bool>;
272 using KeyFrameInteger = KeyFrameBaseSpec<int>;
273 using KeyFrameVector2 = KeyFrameBaseSpec<Vector2>;
274 using KeyFrameVector3 = KeyFrameBaseSpec<Vector3>;
275 using KeyFrameVector4 = KeyFrameBaseSpec<Vector4>;
276 using KeyFrameQuaternion = KeyFrameBaseSpec<Quaternion>;
278 using KeyFrameBooleanPtr = IntrusivePtr<KeyFrameBoolean>;
279 using KeyFrameNumberPtr = IntrusivePtr<KeyFrameNumber>;
280 using KeyFrameIntegerPtr = IntrusivePtr<KeyFrameInteger>;
281 using KeyFrameVector2Ptr = IntrusivePtr<KeyFrameVector2>;
282 using KeyFrameVector3Ptr = IntrusivePtr<KeyFrameVector3>;
283 using KeyFrameVector4Ptr = IntrusivePtr<KeyFrameVector4>;
284 using KeyFrameQuaternionPtr = IntrusivePtr<KeyFrameQuaternion>;
286 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameBoolean*& keyFrameSpec)
288 keyFrameSpec = static_cast<Internal::KeyFrameBoolean*>(keyFrames.GetKeyFramesBase());
291 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameBoolean*& keyFrameSpec)
293 keyFrameSpec = static_cast<const Internal::KeyFrameBoolean*>(keyFrames.GetKeyFramesBase());
296 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameNumber*& keyFrameSpec)
298 keyFrameSpec = static_cast<Internal::KeyFrameNumber*>(keyFrames.GetKeyFramesBase());
301 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameNumber*& keyFrameSpec)
303 keyFrameSpec = static_cast<const Internal::KeyFrameNumber*>(keyFrames.GetKeyFramesBase());
306 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameInteger*& keyFrameSpec)
308 keyFrameSpec = static_cast<Internal::KeyFrameInteger*>(keyFrames.GetKeyFramesBase());
311 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameInteger*& keyFrameSpec)
313 keyFrameSpec = static_cast<const Internal::KeyFrameInteger*>(keyFrames.GetKeyFramesBase());
316 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameVector2*& keyFrameSpec)
318 keyFrameSpec = static_cast<Internal::KeyFrameVector2*>(keyFrames.GetKeyFramesBase());
321 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameVector2*& keyFrameSpec)
323 keyFrameSpec = static_cast<const Internal::KeyFrameVector2*>(keyFrames.GetKeyFramesBase());
326 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameVector3*& keyFrameSpec)
328 keyFrameSpec = static_cast<Internal::KeyFrameVector3*>(keyFrames.GetKeyFramesBase());
331 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameVector3*& keyFrameSpec)
333 keyFrameSpec = static_cast<const Internal::KeyFrameVector3*>(keyFrames.GetKeyFramesBase());
336 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameVector4*& keyFrameSpec)
338 keyFrameSpec = static_cast<Internal::KeyFrameVector4*>(keyFrames.GetKeyFramesBase());
341 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameVector4*& keyFrameSpec)
343 keyFrameSpec = static_cast<const Internal::KeyFrameVector4*>(keyFrames.GetKeyFramesBase());
346 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameQuaternion*& keyFrameSpec)
348 keyFrameSpec = static_cast<Internal::KeyFrameQuaternion*>(keyFrames.GetKeyFramesBase());
351 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameQuaternion*& keyFrameSpec)
353 keyFrameSpec = static_cast<const Internal::KeyFrameQuaternion*>(keyFrames.GetKeyFramesBase());
359 // Get impl of handle
360 inline Internal::KeyFrames& GetImplementation(Dali::KeyFrames& keyFrames)
362 DALI_ASSERT_ALWAYS( keyFrames && "KeyFrames handle is empty" );
363 Dali::RefObject& object = keyFrames.GetBaseObject();
364 return static_cast<Internal::KeyFrames&>(object);
367 inline const Internal::KeyFrames& GetImplementation(const Dali::KeyFrames& keyFrames)
369 DALI_ASSERT_ALWAYS( keyFrames && "KeyFrames handle is empty" );
370 const Dali::RefObject& object = keyFrames.GetBaseObject();
371 return static_cast<const Internal::KeyFrames&>(object);
377 #endif // DALI_INTERNAL_KEY_FRAMES_H