Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / key-frames-impl.h
1 #ifndef DALI_INTERNAL_KEY_FRAMES_H
2 #define DALI_INTERNAL_KEY_FRAMES_H
3
4 /*
5  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <memory>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/vector-wrapper.h>
26 #include <dali/public-api/animation/key-frames.h>
27 #include <dali/public-api/object/base-object.h>
28 #include <dali/public-api/animation/alpha-function.h>
29 #include <dali/internal/event/animation/key-frame-channel.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 class KeyFrameSpec;
36 class KeyFrames;
37
38 /**
39  * KeyFrames class is responsible for creating and building a specialized KeyFrame class
40  * from the Property::Value type used in Add.
41  */
42 class KeyFrames : public BaseObject
43 {
44 public:
45   static KeyFrames* New();
46
47 private:
48
49   /**
50    * Create a specialization from the given type, and store it to the mSpec
51    * member variable
52    */
53   void CreateKeyFramesSpec(Property::Type type);
54
55 public:
56   /**
57    * Get the type of this key frame.
58    * An empty key frame will return Property::NONE, wheras an initialised
59    * key frame object will return the type of it's first element.
60    * @return the Property::Type of the key frame values
61    */
62   Property::Type GetType() const;
63
64   /**
65    * Add a key frame. The first key frame to be added denotes the type
66    * of all subsequent key frames. If a value with a different type is
67    * added, it will throw a run time assert.
68    * @param[in] time The time (between 0 and 1)
69    * @param[in] value The value of the keyframe at the given time
70    * @param[in] alpha An alpha function to blend between this key frame and the
71    * next key frame.
72    */
73   void Add(float time, const Property::Value& value, AlphaFunction alpha);
74
75   /**
76    * Return the key frames without specialization. The GetSpecialization methods
77    * below will convert to the specialized objects.
78    */
79   KeyFrameSpec* GetKeyFramesBase() const;
80
81   /**
82    * Return the value of the last key frame.
83    */
84   Dali::Property::Value GetLastKeyFrameValue() const;
85
86 private:
87   Dali::Property::Type          mType{Property::NONE}; // Type of the specialization
88   std::unique_ptr<KeyFrameSpec> mKeyFrames;            // Pointer to the specialized key frame object
89 };
90
91 /**
92  * This is the base class for the individual template specializations, allowing a ptr to be
93  * stored in the handle object above.
94  */
95 class KeyFrameSpec
96 {
97 public:
98   virtual ~KeyFrameSpec() = default;
99
100   virtual std::size_t GetNumberOfKeyFrames() const = 0;
101
102   /**
103    * Get the key frame value as a Property::Value.
104    * @param[in] index The index of the key frame to fetch
105    * @param[out] value The value of the given key frame
106    */
107   virtual void GetKeyFrameAsValue( std::size_t index, Property::Value& value ) = 0;
108 };
109
110 /**
111  * The base template class for each key frame specialization.
112  */
113 template<typename V>
114 class KeyFrameBaseSpec : public KeyFrameSpec
115 {
116   KeyFrameChannel<V> mChannel; // The key frame channel
117
118 public:
119   /**
120    * Add a key frame to the channel. Key frames should be added
121    * in time order (this method does not sort the vector by time)
122    * @param[in] t - progress
123    * @param[in] v - value
124    * @param[in] alpha - Alpha function for blending to the next keyframe
125    */
126   void AddKeyFrame(float t, V v, AlphaFunction alpha)
127   {
128     mChannel.mValues.push_back({t, v});
129   }
130
131   /**
132    * Get the number of key frames
133    * @return Channel size
134    */
135   std::size_t GetNumberOfKeyFrames() const override
136   {
137     return mChannel.mValues.size();
138   }
139
140   /**
141    * Get a key frame.
142    * @param[in] index The index of the key frame to fetch
143    * @param[out] time The progress of the given key frame
144    * @param[out] value The value of the given key frame
145    */
146   void GetKeyFrame(unsigned int index, float& time, V& value) const
147   {
148     DALI_ASSERT_ALWAYS(index < mChannel.mValues.size() && "KeyFrame index is out of bounds");
149     const auto& element = mChannel.mValues[index];
150     time                = element.mProgress;
151     value               = element.mValue;
152   }
153
154   /**
155    * @copydoc KeyFrameSpec::GetKeyFrameAsValue()
156    */
157   void GetKeyFrameAsValue( std::size_t index, Property::Value& value ) override
158   {
159     value = mChannel.mValues[index].mValue;
160   }
161
162   /**
163    * Return whether the progress is valid for the range of keyframes. (The first
164    * keyframe doesn't have to start at 0, and the last doesn't have to end at 1.0)
165    * @param[in] progress The progress to test
166    * @return True if the progress is valid for this object
167    */
168   bool IsActive(float progress) const
169   {
170     return mChannel.IsActive(progress);
171   }
172
173   /**
174    * Return an interpolated value for the given progress.
175    * @param[in] progress The progress to test
176    * @return The interpolated value
177    */
178   V GetValue(float progress, Dali::Animation::Interpolation interpolation) const
179   {
180     return mChannel.GetValue(progress, interpolation);
181   }
182 };
183
184 using KeyFrameNumber     = KeyFrameBaseSpec<float>;
185 using KeyFrameBoolean    = KeyFrameBaseSpec<bool>;
186 using KeyFrameInteger    = KeyFrameBaseSpec<int>;
187 using KeyFrameVector2    = KeyFrameBaseSpec<Vector2>;
188 using KeyFrameVector3    = KeyFrameBaseSpec<Vector3>;
189 using KeyFrameVector4    = KeyFrameBaseSpec<Vector4>;
190 using KeyFrameQuaternion = KeyFrameBaseSpec<Quaternion>;
191
192 template<typename DeriveClass>
193 auto GetSpecialization(const Internal::KeyFrames& keyFrames)
194 {
195   return static_cast<DeriveClass>(keyFrames.GetKeyFramesBase());
196 }
197
198 } // Internal
199
200
201 // Get impl of handle
202 inline Internal::KeyFrames& GetImplementation(Dali::KeyFrames& keyFrames)
203 {
204   DALI_ASSERT_ALWAYS( keyFrames && "KeyFrames handle is empty" );
205   Dali::RefObject& object = keyFrames.GetBaseObject();
206   return static_cast<Internal::KeyFrames&>(object);
207 }
208
209 inline const Internal::KeyFrames& GetImplementation(const Dali::KeyFrames& keyFrames)
210 {
211   DALI_ASSERT_ALWAYS( keyFrames && "KeyFrames handle is empty" );
212   const Dali::RefObject& object = keyFrames.GetBaseObject();
213   return static_cast<const Internal::KeyFrames&>(object);
214 }
215
216
217 } // Dali
218
219 #endif // DALI_INTERNAL_KEY_FRAMES_H