submodule: add rive-cpp to rive-tizen as submodule
[platform/core/uifw/rive-tizen.git] / submodule / src / animation / keyed_property.cpp
1 #include "animation/keyed_property.hpp"
2 #include "animation/keyframe.hpp"
3
4 using namespace rive;
5
6 KeyedProperty::~KeyedProperty()
7 {
8         for (auto keyframe : m_KeyFrames)
9         {
10                 delete keyframe;
11         }
12 }
13
14 void KeyedProperty::addKeyFrame(KeyFrame* keyframe)
15 {
16         m_KeyFrames.push_back(keyframe);
17 }
18
19 void KeyedProperty::apply(Core* object, float seconds, float mix)
20 {
21         assert(!m_KeyFrames.empty());
22
23         int idx = 0;
24         int mid = 0;
25         float closestSeconds = 0.0f;
26         int start = 0;
27         auto numKeyFrames = m_KeyFrames.size();
28         int end = (int)numKeyFrames - 1;
29         while (start <= end)
30         {
31                 mid = (start + end) >> 1;
32                 closestSeconds = m_KeyFrames[mid]->seconds();
33                 if (closestSeconds < seconds)
34                 {
35                         start = mid + 1;
36                 }
37                 else if (closestSeconds > seconds)
38                 {
39                         end = mid - 1;
40                 }
41                 else
42                 {
43                         idx = start = mid;
44                         break;
45                 }
46                 idx = start;
47         }
48         int pk = propertyKey();
49         if (idx == 0)
50         {
51                 m_KeyFrames[0]->apply(object, pk, mix);
52         }
53         else
54         {
55                 if (idx < numKeyFrames)
56                 {
57                         KeyFrame* fromFrame = m_KeyFrames[idx - 1];
58                         KeyFrame* toFrame = m_KeyFrames[idx];
59                         if (seconds == toFrame->seconds())
60                         {
61                                 toFrame->apply(object, pk, mix);
62                         }
63                         else
64                         {
65                                 if (fromFrame->interpolationType() == 0)
66                                 {
67                                         fromFrame->apply(object, pk, mix);
68                                 }
69                                 else
70                                 {
71                                         fromFrame->applyInterpolation(
72                                             object, pk, seconds, toFrame, mix);
73                                 }
74                         }
75                 }
76                 else
77                 {
78                         m_KeyFrames[idx - 1]->apply(object, pk, mix);
79                 }
80         }
81 }
82
83 StatusCode KeyedProperty::onAddedDirty(CoreContext* context)
84 {
85         StatusCode code;
86         for (auto keyframe : m_KeyFrames)
87         {
88                 if ((code = keyframe->onAddedDirty(context)) != StatusCode::Ok)
89                 {
90                         return code;
91                 }
92         }
93         return StatusCode::Ok;
94 }
95
96 StatusCode KeyedProperty::onAddedClean(CoreContext* context)
97 {
98         StatusCode code;
99         for (auto keyframe : m_KeyFrames)
100         {
101                 if ((code = keyframe->onAddedClean(context)) != StatusCode::Ok)
102                 {
103                         return code;
104                 }
105         }
106         return StatusCode::Ok;
107 }