Revert "License conversion from Flora to Apache 2.0"
[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) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali/public-api/common/vector-wrapper.h>
22 #include <dali/internal/event/animation/progress-value.h>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28
29 class KeyFrameChannelBase
30 {
31 public:
32   enum KeyFrameChannelId
33   {
34     Translate, Rotate, Scale,
35   };
36
37   KeyFrameChannelBase(KeyFrameChannelId channel_id)
38   : mChannelId(channel_id)
39   {
40   }
41
42   virtual ~KeyFrameChannelBase()
43   {
44   }
45
46   KeyFrameChannelId GetId() const
47   {
48     return mChannelId;
49   }
50
51   virtual bool IsActive(float progress) = 0;
52
53 protected:
54   KeyFrameChannelId       mChannelId;
55 };
56
57
58 template <typename V>
59 class KeyFrameChannel : public KeyFrameChannelBase
60 {
61 public:
62   typedef std::vector<ProgressValue<V> > ProgressValues;
63
64   KeyFrameChannel(KeyFrameChannelId channel_id, ProgressValues& values )
65   : KeyFrameChannelBase(channel_id),
66     mValues(values)
67   {
68   }
69
70
71   virtual ~KeyFrameChannel()
72   {
73   }
74
75   bool IsActive (float progress);
76
77   V GetValue(float progress) const;
78
79   bool FindInterval(typename ProgressValues::iterator& start,
80                     typename ProgressValues::iterator& end,
81                     float progress) const;
82
83   ProgressValues& mValues;
84 };
85
86 template <class V>
87 bool KeyFrameChannel<V>::IsActive (float progress)
88 {
89   bool active = false;
90   if(!mValues.empty())
91   {
92     ProgressValue<V>& first = mValues.front();
93
94     if( progress >= first.GetProgress() )
95     {
96       active = true;
97     }
98   }
99   return active;
100 }
101
102 /**
103  * Use a linear search to find the interval containing progress
104  * TODO: Use binary search instead
105  */
106 template <class V>
107 bool KeyFrameChannel<V>::FindInterval(
108   typename ProgressValues::iterator& start,
109   typename ProgressValues::iterator& end,
110   float progress) const
111 {
112   bool found = false;
113   typename std::vector<ProgressValue<V> >::iterator iter = mValues.begin();
114   typename std::vector<ProgressValue<V> >::iterator prevIter = iter;
115
116   while(iter != mValues.end() && iter->GetProgress() <= progress)
117   {
118     prevIter = iter;
119     ++iter;
120   }
121
122   if(iter == mValues.end())
123   {
124     --prevIter;
125     --iter;
126   }
127
128   if(prevIter->GetProgress() <= progress
129      &&
130      iter->GetProgress() > progress)
131   {
132     found = true;
133     start = prevIter;
134     end   = iter;
135   }
136
137   return found;
138 }
139
140 template <class V>
141 V KeyFrameChannel<V>::GetValue (float progress) const
142 {
143   ProgressValue<V>&  firstPV =  mValues.front();
144
145   typename std::vector<ProgressValue<V> >::iterator start;
146   typename std::vector<ProgressValue<V> >::iterator end;
147
148   V interpolatedV = firstPV.GetValue();
149   if(progress >= mValues.back().GetProgress() )
150   {
151     interpolatedV = mValues.back().GetValue(); // This should probably be last value...
152   }
153   else if(FindInterval(start, end, progress))
154   {
155     float frameProgress = (progress - start->GetProgress()) / (end->GetProgress() - start->GetProgress());
156
157     interpolatedV = Interpolate(*start, *end, frameProgress);
158   }
159
160   return interpolatedV;
161 }
162
163 typedef KeyFrameChannel<float>      KeyFrameChannelNumber;
164 typedef KeyFrameChannel<Vector2>    KeyFrameChannelVector2;
165 typedef KeyFrameChannel<Vector3>    KeyFrameChannelVector3;
166 typedef KeyFrameChannel<Vector4>    KeyFrameChannelVector4;
167 typedef KeyFrameChannel<Quaternion> KeyFrameChannelQuaternion;
168 typedef KeyFrameChannel<AngleAxis>  KeyFrameChannelAngleAxis;
169
170
171 } // Internal
172 } // namespace Dali
173
174 #endif // __DALI_INTERNAL_KEY_FRAME_CHANNEL_H__