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