refactor dali-animation.
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / key-frame-channel.h
1 #ifndef DALI_INTERNAL_KEY_FRAME_CHANNEL_H
2 #define DALI_INTERNAL_KEY_FRAME_CHANNEL_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 <algorithm>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/event/animation/progress-value.h>
26 #include <dali/public-api/animation/animation.h>
27 #include <dali/public-api/common/vector-wrapper.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 template<typename V>
34 struct KeyFrameChannel
35 {
36   using ProgressValues = std::vector<ProgressValue<V>>;
37
38   bool IsActive(float progress) const
39   {
40     if(!mValues.empty() && (progress >= mValues[0].GetProgress()))
41     {
42       return true;
43     }
44     return false;
45   }
46
47   V GetValue(float progress, Dali::Animation::Interpolation interpolation) const
48   {
49     V interpolatedV{};
50
51     if(progress >= mValues.back().GetProgress())
52     {
53       interpolatedV = mValues.back().GetValue();
54     }
55     else
56     {
57       auto end   = std::find_if(mValues.begin(), mValues.end(), [=](const auto& element) { return element.GetProgress() > progress; });
58       auto start = end - 1;
59
60       const bool validInterval = (end != mValues.end()) && (start->GetProgress() <= progress);
61
62       if(validInterval)
63       {
64         float frameProgress = (progress - start->GetProgress()) / (end->GetProgress() - start->GetProgress());
65         if(interpolation == Dali::Animation::LINEAR)
66         {
67           Interpolate(interpolatedV, start->GetValue(), end->GetValue(), frameProgress);
68         }
69         else
70         {
71           //Calculate prev and next values
72           V prev;
73           if(start != mValues.begin())
74           {
75             prev = (start - 1)->GetValue();
76           }
77           else
78           {
79             //Project next value through start point
80             prev = start->GetValue() + (start->GetValue() - (start + 1)->GetValue());
81           }
82
83           V next;
84           if(end != mValues.end() - 1)
85           {
86             next = (end + 1)->GetValue();
87           }
88           else
89           {
90             //Project prev value through end point
91             next = end->GetValue() + (end->GetValue() - (end - 1)->GetValue());
92           }
93
94           CubicInterpolate(interpolatedV, prev, start->GetValue(), end->GetValue(), next, frameProgress);
95         }
96       }
97     }
98     return interpolatedV;
99   }
100
101   ProgressValues mValues;
102 };
103
104 } // Internal
105 } // namespace Dali
106
107 #endif // DALI_INTERNAL_KEY_FRAME_CHANNEL_H