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