2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/visuals/transition-data-impl.h>
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>
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");
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 )
68 TransitionData::TransitionData()
72 TransitionData::~TransitionData()
76 TransitionDataPtr TransitionData::New( const Property::Array& value )
78 TransitionDataPtr transitionData( new TransitionData() );
79 transitionData->Initialize(value);
80 return transitionData;
83 TransitionDataPtr TransitionData::New( const Property::Map& value )
85 TransitionDataPtr transitionData( new TransitionData() );
86 transitionData->Initialize(value);
87 return transitionData;
91 void TransitionData::Initialize( const Property::Map& map )
93 TransitionData::Animator* animator = ConvertMap( map );
97 void TransitionData::Initialize( const Property::Array& array )
99 for( unsigned int arrayIdx = 0, transitionArrayCount = array.Count(); arrayIdx < transitionArrayCount; ++arrayIdx )
101 const Property::Value& element = array.GetElementAt( arrayIdx );
102 // Expect each child to be an object representing an animator:
104 Property::Map* map = element.GetMap();
107 TransitionData::Animator* animator = ConvertMap( *map );
113 TransitionData::Animator* TransitionData::ConvertMap( const Property::Map& map)
115 TransitionData::Animator* animator = new TransitionData::Animator();
116 animator->alphaFunction = AlphaFunction::LINEAR;
117 animator->timePeriodDelay = 0.0f;
118 animator->timePeriodDuration = 1.0f;
120 for( unsigned int mapIdx = 0; mapIdx < map.Count(); ++mapIdx )
122 const KeyValuePair pair( map.GetKeyValue( mapIdx ) );
123 if( pair.first.type == Property::Key::INDEX )
125 continue; // We don't consider index keys.
128 const std::string& key( pair.first.stringKey );
129 const Property::Value& value( pair.second );
131 if( key == TOKEN_TARGET )
133 animator->objectName = value.Get< std::string >();
135 else if( key == TOKEN_PROPERTY )
137 if( value.GetType() == Property::STRING )
139 animator->propertyKey = Property::Key( value.Get<std::string>() );
143 animator->propertyKey = Property::Key( value.Get<int>() );
146 else if( key == TOKEN_INITIAL_VALUE )
148 animator->initialValue = value;
150 else if( key == TOKEN_TARGET_VALUE )
152 animator->targetValue = value;
154 else if( key == TOKEN_ANIMATOR )
156 animator->animate = true;
157 Property::Map animatorMap = value.Get< Property::Map >();
158 for( size_t animatorMapIdx = 0; animatorMapIdx < animatorMap.Count(); ++animatorMapIdx )
160 const KeyValuePair pair( animatorMap.GetKeyValue( animatorMapIdx ) );
162 if( pair.first.type == Property::Key::INDEX )
164 continue; // We don't consider index keys.
167 const std::string& key( pair.first.stringKey );
168 const Property::Value& value( pair.second );
170 if( key == TOKEN_ALPHA_FUNCTION )
172 std::string alphaFunctionValue = value.Get< std::string >();
174 if( alphaFunctionValue == "LINEAR" )
176 animator->alphaFunction = AlphaFunction::LINEAR;
178 else if( ! alphaFunctionValue.compare(0, 5, "EASE_" ) )
180 if( alphaFunctionValue == "EASE_IN" )
182 animator->alphaFunction = AlphaFunction::EASE_IN;
184 else if( alphaFunctionValue == "EASE_OUT" )
186 animator->alphaFunction = AlphaFunction::EASE_OUT;
188 else if( ! alphaFunctionValue.compare( 5, 3, "IN_" ) )
190 if( ! alphaFunctionValue.compare(8, -1, "SQUARE" ))
192 animator->alphaFunction = AlphaFunction::EASE_IN_SQUARE;
194 else if( ! alphaFunctionValue.compare(8, -1, "OUT" ))
196 animator->alphaFunction = AlphaFunction::EASE_IN_OUT;
198 else if( ! alphaFunctionValue.compare(8, -1, "OUT_SINE" ))
200 animator->alphaFunction = AlphaFunction::EASE_IN_OUT_SINE;
202 else if( ! alphaFunctionValue.compare(8, -1, "SINE" ))
204 animator->alphaFunction = AlphaFunction::EASE_IN_SINE;
207 else if( ! alphaFunctionValue.compare( 5, 4, "OUT_" ) )
209 if( ! alphaFunctionValue.compare(9, -1, "SQUARE" ) )
211 animator->alphaFunction = AlphaFunction::EASE_OUT_SQUARE;
213 else if( ! alphaFunctionValue.compare(9, -1, "SINE" ) )
215 animator->alphaFunction = AlphaFunction::EASE_OUT_SINE;
217 else if( ! alphaFunctionValue.compare(9, -1, "BACK" ) )
219 animator->alphaFunction = AlphaFunction::EASE_OUT_BACK;
223 else if( alphaFunctionValue == "REVERSE" )
225 animator->alphaFunction = AlphaFunction::REVERSE;
227 else if( alphaFunctionValue == "BOUNCE" )
229 animator->alphaFunction = AlphaFunction::BOUNCE;
231 else if( alphaFunctionValue == "SIN" )
233 animator->alphaFunction = AlphaFunction::SIN;
236 else if( key == TOKEN_TIME_PERIOD )
238 Property::Map timeMap = value.Get< Property::Map >();
239 for( size_t timeMapIdx = 0; timeMapIdx < timeMap.Count(); ++timeMapIdx )
241 const KeyValuePair pair( timeMap.GetKeyValue( timeMapIdx ) );
242 if( pair.first.type == Property::Key::INDEX )
246 const std::string& key( pair.first.stringKey );
248 if( key == TOKEN_DELAY )
250 animator->timePeriodDelay = pair.second.Get< float >();
252 else if( key == TOKEN_DURATION )
254 animator->timePeriodDuration = pair.second.Get< float >();
264 void TransitionData::Add( Animator* animator )
266 mAnimators.PushBack( animator );
269 TransitionData::Iterator TransitionData::Begin() const
271 return mAnimators.Begin();
274 TransitionData::Iterator TransitionData::End() const
276 return mAnimators.End();
279 size_t TransitionData::Count() const
281 return mAnimators.Count();
284 Property::Map TransitionData::GetAnimatorAt( size_t index )
286 DALI_ASSERT_ALWAYS( index < Count() && "index exceeds bounds" );
288 Animator* animator = mAnimators[index];
290 map[TOKEN_TARGET] = animator->objectName;
291 if( animator->propertyKey.type == Property::Key::INDEX )
293 map[TOKEN_PROPERTY] = animator->propertyKey.indexKey;
297 map[TOKEN_PROPERTY] = animator->propertyKey.stringKey;
299 if( animator->initialValue.GetType() != Property::NONE )
301 map[TOKEN_INITIAL_VALUE] = animator->initialValue;
303 if( animator->targetValue.GetType() != Property::NONE )
305 map[TOKEN_TARGET_VALUE] = animator->targetValue;
307 if( animator->animate )
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 ));
321 } // namespace Internal
322 } // namespace Toolkit