5a885d80983e00c20b0de924d3a3aaa3ae07d871
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / key-frames-impl.cpp
1 /*
2  * Copyright (c) 2017 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 KeyFrames* KeyFrames::New()
27 {
28   return new KeyFrames();
29 }
30
31 KeyFrames::KeyFrames()
32   : mType(Property::NONE),
33     mKeyFrames(NULL)
34 {
35 }
36
37 KeyFrames::~KeyFrames()
38 {
39 }
40
41
42 void KeyFrames::CreateKeyFramesSpec(Property::Type type)
43 {
44   mType = type;
45   // Now we have a type, can create specific implementation
46   switch(type)
47   {
48     case Property::BOOLEAN:
49     {
50       mKeyFrames = Internal::KeyFrameBoolean::New();
51       break;
52     }
53     case Property::INTEGER:
54     {
55       mKeyFrames = Internal::KeyFrameInteger::New();
56       break;
57     }
58     case Property::FLOAT:
59     {
60       mKeyFrames = Internal::KeyFrameNumber::New();
61       break;
62     }
63     case Property::VECTOR2:
64     {
65       mKeyFrames = Internal::KeyFrameVector2::New();
66       break;
67     }
68     case Property::VECTOR3:
69     {
70       mKeyFrames = Internal::KeyFrameVector3::New();
71       break;
72     }
73     case Property::VECTOR4:
74     {
75       mKeyFrames = Internal::KeyFrameVector4::New();
76       break;
77     }
78     case Property::ROTATION:
79     {
80       mKeyFrames = Internal::KeyFrameQuaternion::New();
81       break;
82     }
83     default:
84     {
85       DALI_ABORT( "Type not animateable" );
86       break;
87     }
88   }
89 }
90
91 Property::Type KeyFrames::GetType() const
92 {
93   return mType;
94 }
95
96 void KeyFrames::Add(float time, Property::Value value, AlphaFunction alpha)
97 {
98   if(mType == Property::NONE)
99   {
100     CreateKeyFramesSpec(value.GetType());
101   }
102
103   // Once we have created a type, can only add values of the same type
104   DALI_ASSERT_ALWAYS( mType == value.GetType() && "Can only add values of the same type to a KeyFrame" );
105
106   DALI_ASSERT_DEBUG(mKeyFrames);
107
108   switch(mType)
109   {
110     case Property::BOOLEAN:
111     {
112       Internal::KeyFrameBoolean* kf = static_cast<Internal::KeyFrameBoolean*>(mKeyFrames.Get());
113       kf->AddKeyFrame(time, value.Get<bool>(), alpha);
114       break;
115     }
116     case Property::INTEGER:
117     {
118       Internal::KeyFrameInteger* kf = static_cast<Internal::KeyFrameInteger*>(mKeyFrames.Get());
119       kf->AddKeyFrame(time, value.Get<int>(), alpha);
120       break;
121     }
122     case Property::FLOAT:
123     {
124       Internal::KeyFrameNumber* kf = static_cast<Internal::KeyFrameNumber*>(mKeyFrames.Get());
125       kf->AddKeyFrame(time, value.Get<float>(), alpha);
126       break;
127     }
128     case Property::VECTOR2:
129     {
130       Internal::KeyFrameVector2* kf = static_cast<Internal::KeyFrameVector2*>(mKeyFrames.Get());
131       kf->AddKeyFrame(time, value.Get<Vector2>(), alpha);
132       break;
133     }
134     case Property::VECTOR3:
135     {
136       Internal::KeyFrameVector3* kf = static_cast<Internal::KeyFrameVector3*>(mKeyFrames.Get());
137       kf->AddKeyFrame(time, value.Get<Vector3>(), alpha);
138       break;
139     }
140     case Property::VECTOR4:
141     {
142       Internal::KeyFrameVector4* kf = static_cast<Internal::KeyFrameVector4*>(mKeyFrames.Get());
143       kf->AddKeyFrame(time, value.Get<Vector4>(), alpha);
144       break;
145     }
146     case Property::ROTATION:
147     {
148       Internal::KeyFrameQuaternion* kf = static_cast<Internal::KeyFrameQuaternion*>(mKeyFrames.Get());
149       kf->AddKeyFrame(time, value.Get<Quaternion>(), alpha);
150       break;
151     }
152     default:
153       DALI_ASSERT_DEBUG(!"Type not supported");
154       break;
155   }
156 }
157
158 KeyFrameSpec* KeyFrames::GetKeyFramesBase() const
159 {
160   return mKeyFrames.Get();
161 }
162
163 Property::Value KeyFrames::GetLastKeyFrameValue() const
164 {
165   Property::Value value;
166
167   std::size_t noOfKeyFrames = mKeyFrames->GetNumberOfKeyFrames();
168   if( noOfKeyFrames )
169   {
170     mKeyFrames->GetKeyFrameAsValue( noOfKeyFrames - 1, value );
171   }
172
173   return value;
174 }
175
176 } // Internal
177 } // Dali