[Tizen] (Animation) Ensure AnimateBetween updates the cached event-side properties
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / key-frames-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/animation/key-frames-impl.h>
20
21 namespace Dali
22 {
23 namespace Internal
24 {
25
26 namespace
27 {
28
29 /// Helper to retrieve the value of the final key frame
30 template< typename PropertyType, typename KeyFrameType >
31 inline void GetLastKeyFrameValueInternal( const IntrusivePtr< KeyFrameSpec >& keyFrames, Property::Value& value )
32 {
33   KeyFrameType* kf = static_cast< KeyFrameType* >( keyFrames.Get() );
34   float time = 0;
35   PropertyType actualValue;
36   kf->GetKeyFrame( kf->GetNumberOfKeyFrames() - 1, time, actualValue );
37   value = actualValue;
38 }
39
40 } // unnamed namespace
41
42 KeyFrames* KeyFrames::New()
43 {
44   return new KeyFrames();
45 }
46
47 KeyFrames::KeyFrames()
48   : mType(Property::NONE),
49     mKeyFrames(NULL)
50 {
51 }
52
53 KeyFrames::~KeyFrames()
54 {
55 }
56
57
58 void KeyFrames::CreateKeyFramesSpec(Property::Type type)
59 {
60   mType = type;
61   // Now we have a type, can create specific implementation
62   switch(type)
63   {
64     case Property::BOOLEAN:
65     {
66       mKeyFrames = Internal::KeyFrameBoolean::New();
67       break;
68     }
69     case Property::INTEGER:
70     {
71       mKeyFrames = Internal::KeyFrameInteger::New();
72       break;
73     }
74     case Property::FLOAT:
75     {
76       mKeyFrames = Internal::KeyFrameNumber::New();
77       break;
78     }
79     case Property::VECTOR2:
80     {
81       mKeyFrames = Internal::KeyFrameVector2::New();
82       break;
83     }
84     case Property::VECTOR3:
85     {
86       mKeyFrames = Internal::KeyFrameVector3::New();
87       break;
88     }
89     case Property::VECTOR4:
90     {
91       mKeyFrames = Internal::KeyFrameVector4::New();
92       break;
93     }
94     case Property::ROTATION:
95     {
96       mKeyFrames = Internal::KeyFrameQuaternion::New();
97       break;
98     }
99     default:
100     {
101       DALI_ABORT( "Type not animateable" );
102       break;
103     }
104   }
105 }
106
107 Property::Type KeyFrames::GetType() const
108 {
109   return mType;
110 }
111
112 void KeyFrames::Add(float time, Property::Value value, AlphaFunction alpha)
113 {
114   if(mType == Property::NONE)
115   {
116     CreateKeyFramesSpec(value.GetType());
117   }
118
119   // Once we have created a type, can only add values of the same type
120   DALI_ASSERT_ALWAYS( mType == value.GetType() && "Can only add values of the same type to a KeyFrame" );
121
122   DALI_ASSERT_DEBUG(mKeyFrames);
123
124   switch(mType)
125   {
126     case Property::BOOLEAN:
127     {
128       Internal::KeyFrameBoolean* kf = static_cast<Internal::KeyFrameBoolean*>(mKeyFrames.Get());
129       kf->AddKeyFrame(time, value.Get<bool>(), alpha);
130       break;
131     }
132     case Property::INTEGER:
133     {
134       Internal::KeyFrameInteger* kf = static_cast<Internal::KeyFrameInteger*>(mKeyFrames.Get());
135       kf->AddKeyFrame(time, value.Get<int>(), alpha);
136       break;
137     }
138     case Property::FLOAT:
139     {
140       Internal::KeyFrameNumber* kf = static_cast<Internal::KeyFrameNumber*>(mKeyFrames.Get());
141       kf->AddKeyFrame(time, value.Get<float>(), alpha);
142       break;
143     }
144     case Property::VECTOR2:
145     {
146       Internal::KeyFrameVector2* kf = static_cast<Internal::KeyFrameVector2*>(mKeyFrames.Get());
147       kf->AddKeyFrame(time, value.Get<Vector2>(), alpha);
148       break;
149     }
150     case Property::VECTOR3:
151     {
152       Internal::KeyFrameVector3* kf = static_cast<Internal::KeyFrameVector3*>(mKeyFrames.Get());
153       kf->AddKeyFrame(time, value.Get<Vector3>(), alpha);
154       break;
155     }
156     case Property::VECTOR4:
157     {
158       Internal::KeyFrameVector4* kf = static_cast<Internal::KeyFrameVector4*>(mKeyFrames.Get());
159       kf->AddKeyFrame(time, value.Get<Vector4>(), alpha);
160       break;
161     }
162     case Property::ROTATION:
163     {
164       Internal::KeyFrameQuaternion* kf = static_cast<Internal::KeyFrameQuaternion*>(mKeyFrames.Get());
165       kf->AddKeyFrame(time, value.Get<Quaternion>(), alpha);
166       break;
167     }
168     default:
169       DALI_ASSERT_DEBUG(!"Type not supported");
170       break;
171   }
172 }
173
174 KeyFrameSpec* KeyFrames::GetKeyFramesBase() const
175 {
176   return mKeyFrames.Get();
177 }
178
179 Property::Value KeyFrames::GetLastKeyFrameValue() const
180 {
181   Property::Value value;
182
183   switch(mType)
184   {
185     case Property::BOOLEAN:
186     {
187       GetLastKeyFrameValueInternal< bool, KeyFrameBoolean >( mKeyFrames, value );
188       break;
189     }
190     case Property::INTEGER:
191     {
192       GetLastKeyFrameValueInternal< int, KeyFrameInteger >( mKeyFrames, value );
193       break;
194     }
195     case Property::FLOAT:
196     {
197       GetLastKeyFrameValueInternal< float, KeyFrameNumber >( mKeyFrames, value );
198       break;
199     }
200     case Property::VECTOR2:
201     {
202       GetLastKeyFrameValueInternal< Vector2, KeyFrameVector2 >( mKeyFrames, value );
203       break;
204     }
205     case Property::VECTOR3:
206     {
207       GetLastKeyFrameValueInternal< Vector3, KeyFrameVector3 >( mKeyFrames, value );
208       break;
209     }
210     case Property::VECTOR4:
211     {
212       GetLastKeyFrameValueInternal< Vector4, KeyFrameVector4 >( mKeyFrames, value );
213       break;
214     }
215     case Property::ROTATION:
216     {
217       GetLastKeyFrameValueInternal< Quaternion, KeyFrameQuaternion >( mKeyFrames, value );
218       break;
219     }
220     default:
221       DALI_ASSERT_DEBUG(!"Type not supported");
222       break;
223   }
224
225   return value;
226 }
227
228 } // Internal
229 } // Dali