Move Event Handlers to View class
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / transition-data-impl.cpp
1 /*
2  * Copyright (c) 2016 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-toolkit/internal/visuals/transition-data-impl.h>
20
21 // EXTERNAL HEADERS
22 #include <dali/dali.h>
23 #include <dali/devel-api/scripting/enum-helper.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/public-api/controls/control.h>
26 #include <dali-toolkit/public-api/controls/control-impl.h>
27 #include <sstream>
28
29 using namespace Dali;
30
31 namespace
32 {
33 const char* TOKEN_TARGET("target");
34 const char* TOKEN_PROPERTY("property");
35 const char* TOKEN_INITIAL_VALUE("initialValue");
36 const char* TOKEN_TARGET_VALUE("targetValue");
37 const char* TOKEN_ANIMATOR("animator");
38 const char* TOKEN_TIME_PERIOD("timePeriod");
39 const char* TOKEN_DURATION("duration");
40 const char* TOKEN_DELAY("delay");
41 const char* TOKEN_ALPHA_FUNCTION("alphaFunction");
42
43
44 DALI_ENUM_TO_STRING_TABLE_BEGIN( ALPHA_FUNCTION_BUILTIN )
45 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, LINEAR)
46 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, REVERSE)
47 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, EASE_IN)
48 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, EASE_OUT)
49 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, EASE_IN_OUT)
50 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, EASE_IN_SQUARE)
51 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, EASE_OUT_SQUARE)
52 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, EASE_IN_SINE)
53 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, EASE_OUT_SINE)
54 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, EASE_IN_OUT_SINE)
55 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, EASE_OUT_BACK)
56 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, BOUNCE)
57 DALI_ENUM_TO_STRING_WITH_SCOPE(AlphaFunction, SIN)
58 DALI_ENUM_TO_STRING_TABLE_END( ALPHA_FUNCTION_BUILTIN )
59 }
60
61 namespace Dali
62 {
63 namespace Toolkit
64 {
65 namespace Internal
66 {
67
68 TransitionData::TransitionData()
69 {
70 }
71
72 TransitionData::~TransitionData()
73 {
74 }
75
76 TransitionDataPtr TransitionData::New( const Property::Array& value )
77 {
78   TransitionDataPtr transitionData( new TransitionData() );
79   transitionData->Initialize(value);
80   return transitionData;
81 }
82
83 TransitionDataPtr TransitionData::New( const Property::Map& value )
84 {
85   TransitionDataPtr transitionData( new TransitionData() );
86   transitionData->Initialize(value);
87   return transitionData;
88 }
89
90
91 void TransitionData::Initialize( const Property::Map& map )
92 {
93   TransitionData::Animator* animator = ConvertMap( map );
94   Add( animator );
95 }
96
97 void TransitionData::Initialize( const Property::Array& array )
98 {
99   for( unsigned int arrayIdx = 0, transitionArrayCount = array.Count(); arrayIdx < transitionArrayCount; ++arrayIdx )
100   {
101     const Property::Value& element = array.GetElementAt( arrayIdx );
102     // Expect each child to be an object representing an animator:
103
104     Property::Map* map = element.GetMap();
105     if( map != NULL )
106     {
107       TransitionData::Animator* animator = ConvertMap( *map );
108       Add( animator );
109     }
110   }
111 }
112
113 TransitionData::Animator* TransitionData::ConvertMap( const Property::Map& map)
114 {
115   TransitionData::Animator* animator = new TransitionData::Animator();
116   animator->alphaFunction = AlphaFunction::LINEAR;
117   animator->timePeriodDelay = 0.0f;
118   animator->timePeriodDuration = 1.0f;
119
120   for( unsigned int mapIdx = 0; mapIdx < map.Count(); ++mapIdx )
121   {
122     const KeyValuePair pair( map.GetKeyValue( mapIdx ) );
123     if( pair.first.type == Property::Key::INDEX )
124     {
125       continue; // We don't consider index keys.
126     }
127
128     const std::string& key( pair.first.stringKey );
129     const Property::Value& value( pair.second );
130
131     if( key == TOKEN_TARGET )
132     {
133       animator->objectName = value.Get< std::string >();
134     }
135     else if( key == TOKEN_PROPERTY )
136     {
137       if( value.GetType() == Property::STRING )
138       {
139         animator->propertyKey = Property::Key( value.Get<std::string>() );
140       }
141       else
142       {
143         animator->propertyKey = Property::Key( value.Get<int>() );
144       }
145     }
146     else if( key == TOKEN_INITIAL_VALUE )
147     {
148       animator->initialValue = value;
149     }
150     else if( key == TOKEN_TARGET_VALUE )
151     {
152       animator->targetValue = value;
153     }
154     else if( key == TOKEN_ANIMATOR )
155     {
156       animator->animate = true;
157       Property::Map animatorMap = value.Get< Property::Map >();
158       for( size_t animatorMapIdx = 0; animatorMapIdx < animatorMap.Count(); ++animatorMapIdx )
159       {
160         const KeyValuePair pair( animatorMap.GetKeyValue( animatorMapIdx ) );
161
162         if( pair.first.type == Property::Key::INDEX )
163         {
164           continue; // We don't consider index keys.
165         }
166
167         const std::string& key( pair.first.stringKey );
168         const Property::Value& value( pair.second );
169
170         if( key == TOKEN_ALPHA_FUNCTION )
171         {
172           std::string alphaFunctionValue = value.Get< std::string >();
173
174           if( alphaFunctionValue == "LINEAR" )
175           {
176             animator->alphaFunction = AlphaFunction::LINEAR;
177           }
178           else if( ! alphaFunctionValue.compare(0, 5, "EASE_" ) )
179           {
180             if( alphaFunctionValue == "EASE_IN" )
181             {
182               animator->alphaFunction = AlphaFunction::EASE_IN;
183             }
184             else if( alphaFunctionValue == "EASE_OUT" )
185             {
186               animator->alphaFunction = AlphaFunction::EASE_OUT;
187             }
188             else if( ! alphaFunctionValue.compare( 5, 3, "IN_" ) )
189             {
190               if( ! alphaFunctionValue.compare(8, -1, "SQUARE" ))
191               {
192                 animator->alphaFunction = AlphaFunction::EASE_IN_SQUARE;
193               }
194               else if( ! alphaFunctionValue.compare(8, -1, "OUT" ))
195               {
196                 animator->alphaFunction = AlphaFunction::EASE_IN_OUT;
197               }
198               else if( ! alphaFunctionValue.compare(8, -1, "OUT_SINE" ))
199               {
200                 animator->alphaFunction = AlphaFunction::EASE_IN_OUT_SINE;
201               }
202               else if( ! alphaFunctionValue.compare(8, -1, "SINE" ))
203               {
204                 animator->alphaFunction = AlphaFunction::EASE_IN_SINE;
205               }
206             }
207             else if( ! alphaFunctionValue.compare( 5, 4, "OUT_" ) )
208             {
209               if( ! alphaFunctionValue.compare(9, -1, "SQUARE" ) )
210               {
211                 animator->alphaFunction = AlphaFunction::EASE_OUT_SQUARE;
212               }
213               else if( ! alphaFunctionValue.compare(9, -1, "SINE" ) )
214               {
215                 animator->alphaFunction = AlphaFunction::EASE_OUT_SINE;
216               }
217               else if( ! alphaFunctionValue.compare(9, -1, "BACK" ) )
218               {
219                 animator->alphaFunction = AlphaFunction::EASE_OUT_BACK;
220               }
221             }
222           }
223           else if( alphaFunctionValue == "REVERSE" )
224           {
225             animator->alphaFunction = AlphaFunction::REVERSE;
226           }
227           else if( alphaFunctionValue == "BOUNCE" )
228           {
229             animator->alphaFunction = AlphaFunction::BOUNCE;
230           }
231           else if( alphaFunctionValue == "SIN" )
232           {
233             animator->alphaFunction = AlphaFunction::SIN;
234           }
235         }
236         else if( key == TOKEN_TIME_PERIOD )
237         {
238           Property::Map timeMap = value.Get< Property::Map >();
239           for( size_t timeMapIdx = 0; timeMapIdx < timeMap.Count(); ++timeMapIdx )
240           {
241             const KeyValuePair pair( timeMap.GetKeyValue( timeMapIdx ) );
242             if( pair.first.type == Property::Key::INDEX )
243             {
244               continue;
245             }
246             const std::string& key( pair.first.stringKey );
247
248             if( key == TOKEN_DELAY )
249             {
250               animator->timePeriodDelay = pair.second.Get< float >();
251             }
252             else if( key == TOKEN_DURATION )
253             {
254               animator->timePeriodDuration = pair.second.Get< float >();
255             }
256           }
257         }
258       }
259     }
260   }
261   return animator;
262 }
263
264 void TransitionData::Add( Animator* animator )
265 {
266   mAnimators.PushBack( animator );
267 }
268
269 TransitionData::Iterator TransitionData::Begin() const
270 {
271   return mAnimators.Begin();
272 }
273
274 TransitionData::Iterator TransitionData::End() const
275 {
276   return mAnimators.End();
277 }
278
279 size_t TransitionData::Count() const
280 {
281   return mAnimators.Count();
282 }
283
284 Property::Map TransitionData::GetAnimatorAt( size_t index )
285 {
286   DALI_ASSERT_ALWAYS( index < Count() && "index exceeds bounds" );
287
288   Animator* animator = mAnimators[index];
289   Property::Map map;
290   map[TOKEN_TARGET] = animator->objectName;
291   if( animator->propertyKey.type == Property::Key::INDEX )
292   {
293     map[TOKEN_PROPERTY] = animator->propertyKey.indexKey;
294   }
295   else
296   {
297     map[TOKEN_PROPERTY] = animator->propertyKey.stringKey;
298   }
299   if( animator->initialValue.GetType() != Property::NONE )
300   {
301     map[TOKEN_INITIAL_VALUE] = animator->initialValue;
302   }
303   if( animator->targetValue.GetType() != Property::NONE )
304   {
305     map[TOKEN_TARGET_VALUE] = animator->targetValue;
306   }
307   if( animator->animate )
308   {
309     map[TOKEN_ANIMATOR] = Property::Map()
310       .Add(TOKEN_ALPHA_FUNCTION, GetEnumerationName( animator->alphaFunction,
311                                                      ALPHA_FUNCTION_BUILTIN_TABLE,
312                                                      ALPHA_FUNCTION_BUILTIN_TABLE_COUNT ))
313       .Add(TOKEN_TIME_PERIOD, Property::Map()
314            .Add( TOKEN_DELAY, animator->timePeriodDelay )
315            .Add( TOKEN_DURATION, animator->timePeriodDuration ));
316   }
317
318   return map;
319 }
320
321 } // namespace Internal
322 } // namespace Toolkit
323 } // namespace Dali