1 #ifndef __DALI_INTERNAL_KEY_FRAMES_H__
2 #define __DALI_INTERNAL_KEY_FRAMES_H__
5 * Copyright (c) 2014 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-functions.h>
26 #include <dali/internal/event/animation/progress-value.h>
27 #include <dali/internal/event/animation/key-frame-channel.h>
35 typedef IntrusivePtr<KeyFrames> KeyFramesPtr;
39 * KeyFrames class is responsible for creating and building a specialized KeyFrame class
40 * from the Property::Value type used in Add.
42 class KeyFrames : public BaseObject
45 static KeyFrames* New();
48 * Instantiate an empty KeyFrames object
57 * Don't allow copy constructor
59 KeyFrames(const KeyFrames& rhs);
62 * Don't allow copy operator
64 KeyFrames& operator=(const KeyFrames& rhs);
67 * Create a specialization from the given type, and store it to the mSpec
70 void CreateKeyFramesSpec(Property::Type type);
74 * Get the type of this key frame.
75 * An empty key frame will return Property::NONE, wheras an initialised
76 * key frame object will return the type of it's first element.
77 * @return the Property::Type of the key frame values
79 Property::Type GetType() const;
82 * Add a key frame. The first key frame to be added denotes the type
83 * of all subsequent key frames. If a value with a different type is
84 * added, it will throw a run time assert.
85 * @param[in] time The time (between 0 and 1)
86 * @param[in] value The value of the keyframe at the given time
87 * @param[in] alpha An alpha function to blend between this key frame and the
90 void Add(float time, Property::Value value, AlphaFunction alpha);
93 * Return the key frames without specialization. The GetSpecialization methods
94 * below will convert to the specialized objects.
96 KeyFrameSpec* GetKeyFramesBase() const;
99 Dali::Property::Type mType; // Type of the specialization
100 IntrusivePtr<KeyFrameSpec> mKeyFrames; // Pointer to the specialized key frame object
105 * This is the base class for the individual template specializations, allowing a ptr to be
106 * stored in the handle object above. It inherits from RefObject to allow smart pointers
107 * to be used for the specializations. Note that the derived template class below
108 * allows for a copy constructor so that the specialization object can be cloned before
109 * being passed to the scene-graph for animation.
111 class KeyFrameSpec : public RefObject
117 virtual unsigned int GetNumberOfKeyFrames() const = 0;
122 * A reference counted object may only be deleted by calling Unreference()
124 virtual ~KeyFrameSpec() {}
129 * The base template class for each key frame specialization. It stores a vector of
130 * ProgressValue pairs in mPVs, and uses the existing interface for KeyFrameChannel
131 * to point at this vector.
132 * TODO: Incorporate KeyFrameChannel into this base template
135 class KeyFrameBaseSpec : public KeyFrameSpec
138 typedef ProgressValue<V> PV;
139 typedef std::vector<PV> PVContainer;
141 PVContainer mPVs; // The ProgressValue pairs
142 KeyFrameChannel<V>* mKeyFrames; // The key frame interpolator
145 static KeyFrameBaseSpec<V>* New()
147 return new KeyFrameBaseSpec<V>();
150 static KeyFrameBaseSpec<V>* Clone(const KeyFrameBaseSpec<V>& keyFrames)
152 return new KeyFrameBaseSpec<V>(keyFrames);
158 KeyFrameBaseSpec<V>()
160 mKeyFrames = new KeyFrameChannel<V>(KeyFrameChannelBase::Translate, mPVs);
166 * Allow cloning of this object
168 KeyFrameBaseSpec<V>(const KeyFrameBaseSpec<V>& keyFrames)
169 : mPVs(keyFrames.mPVs)
171 mKeyFrames = new KeyFrameChannel<V>(KeyFrameChannelBase::Translate, mPVs);
175 * Destructor. Ensure progress value pairs are cleared down
177 virtual ~KeyFrameBaseSpec<V>()
185 * Add a key frame to the progress value vector. Key frames should be added
186 * in time order (this method does not sort the vector by time)
187 * @param[in] t - progress
188 * @param[in] v - value
189 * @param[in] alpha - Alpha function for blending to the next keyframe
191 void AddKeyFrame(float t, V v, AlphaFunction alpha)
193 // TODO:: Support alpha
194 mPVs.push_back(PV(t, v));
198 * Get the number of key frames
199 * @return The size of the progress value vector
201 virtual unsigned int GetNumberOfKeyFrames() const
208 * @param[in] index The index of the key frame to fetch
209 * @param[out] time The progress of the given key frame
210 * @param[out] value The value of the given key frame
212 virtual void GetKeyFrame(unsigned int index, float& time, V& value) const
214 DALI_ASSERT_ALWAYS( index < mPVs.size() && "KeyFrame index is out of bounds" );
215 time = mPVs[index].mProgress;
216 value = mPVs[index].mValue;
220 * Return whether the progress is valid for the range of keyframes. (The first
221 * keyframe doesn't have to start at 0, and the last doesn't have to end at 1.0)
222 * @param[in] progress The progress to test
223 * @return True if the progress is valid for this object
225 bool IsActive(float progress) const
227 return mKeyFrames->IsActive(progress);
231 * Return an interpolated value for the given progress.
232 * @param[in] progress The progress to test
233 * @return The interpolated value
235 V GetValue(float progress) const
237 return mKeyFrames->GetValue(progress);
241 typedef KeyFrameBaseSpec<float> KeyFrameNumber;
242 typedef KeyFrameBaseSpec<bool> KeyFrameBoolean;
243 typedef KeyFrameBaseSpec<Vector2> KeyFrameVector2;
244 typedef KeyFrameBaseSpec<Vector3> KeyFrameVector3;
245 typedef KeyFrameBaseSpec<Vector4> KeyFrameVector4;
246 typedef KeyFrameBaseSpec<Quaternion> KeyFrameQuaternion;
248 typedef IntrusivePtr<KeyFrameBoolean> KeyFrameBooleanPtr;
249 typedef IntrusivePtr<KeyFrameNumber> KeyFrameNumberPtr;
250 typedef IntrusivePtr<KeyFrameVector2> KeyFrameVector2Ptr;
251 typedef IntrusivePtr<KeyFrameVector3> KeyFrameVector3Ptr;
252 typedef IntrusivePtr<KeyFrameVector4> KeyFrameVector4Ptr;
253 typedef IntrusivePtr<KeyFrameQuaternion> KeyFrameQuaternionPtr;
256 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameBoolean*& keyFrameSpec)
258 keyFrameSpec = static_cast<Internal::KeyFrameBoolean*>(keyFrames.GetKeyFramesBase());
261 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameBoolean*& keyFrameSpec)
263 keyFrameSpec = static_cast<const Internal::KeyFrameBoolean*>(keyFrames.GetKeyFramesBase());
267 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameNumber*& keyFrameSpec)
269 keyFrameSpec = static_cast<Internal::KeyFrameNumber*>(keyFrames.GetKeyFramesBase());
272 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameNumber*& keyFrameSpec)
274 keyFrameSpec = static_cast<const Internal::KeyFrameNumber*>(keyFrames.GetKeyFramesBase());
277 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameVector2*& keyFrameSpec)
279 keyFrameSpec = static_cast<Internal::KeyFrameVector2*>(keyFrames.GetKeyFramesBase());
282 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameVector2*& keyFrameSpec)
284 keyFrameSpec = static_cast<const Internal::KeyFrameVector2*>(keyFrames.GetKeyFramesBase());
287 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameVector3*& keyFrameSpec)
289 keyFrameSpec = static_cast<Internal::KeyFrameVector3*>(keyFrames.GetKeyFramesBase());
292 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameVector3*& keyFrameSpec)
294 keyFrameSpec = static_cast<const Internal::KeyFrameVector3*>(keyFrames.GetKeyFramesBase());
297 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameVector4*& keyFrameSpec)
299 keyFrameSpec = static_cast<Internal::KeyFrameVector4*>(keyFrames.GetKeyFramesBase());
302 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameVector4*& keyFrameSpec)
304 keyFrameSpec = static_cast<const Internal::KeyFrameVector4*>(keyFrames.GetKeyFramesBase());
307 inline void GetSpecialization(Internal::KeyFrames& keyFrames, Internal::KeyFrameQuaternion*& keyFrameSpec)
309 keyFrameSpec = static_cast<Internal::KeyFrameQuaternion*>(keyFrames.GetKeyFramesBase());
312 inline void GetSpecialization(const Internal::KeyFrames& keyFrames, const Internal::KeyFrameQuaternion*& keyFrameSpec)
314 keyFrameSpec = static_cast<const Internal::KeyFrameQuaternion*>(keyFrames.GetKeyFramesBase());
320 // Get impl of handle
321 inline Internal::KeyFrames& GetImplementation(Dali::KeyFrames& keyFrames)
323 DALI_ASSERT_ALWAYS( keyFrames && "KeyFrames handle is empty" );
324 Dali::RefObject& object = keyFrames.GetBaseObject();
325 return static_cast<Internal::KeyFrames&>(object);
328 inline const Internal::KeyFrames& GetImplementation(const Dali::KeyFrames& keyFrames)
330 DALI_ASSERT_ALWAYS( keyFrames && "KeyFrames handle is empty" );
331 const Dali::RefObject& object = keyFrames.GetBaseObject();
332 return static_cast<const Internal::KeyFrames&>(object);
338 #endif //__DALI_INTERNAL_KEY_FRAMES_H__