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 if( value.GetType() == Property::ARRAY )
175 Vector4 controlPoints;
176 Property::Array *array = value.GetArray();
177 if( array && array->Count() >= 4 )
179 for( size_t vecIdx = 0; vecIdx < 4; ++vecIdx )
181 Property::Value& v = array->GetElementAt(vecIdx);
182 if( v.GetType() == Property::FLOAT )
184 controlPoints[vecIdx] = v.Get<float>();
200 Vector2 controlPoint1( controlPoints.x, controlPoints.y );
201 Vector2 controlPoint2( controlPoints.z, controlPoints.w );
202 animator->alphaFunction = AlphaFunction( controlPoint1, controlPoint2 );
206 animator->animate = false;
209 else if( value.GetType() == Property::VECTOR4 )
211 Vector4 controlPoints = value.Get<Vector4>();
212 Vector2 controlPoint1( controlPoints.x, controlPoints.y );
213 Vector2 controlPoint2( controlPoints.z, controlPoints.w );
214 animator->alphaFunction = AlphaFunction( controlPoint1, controlPoint2 );
216 else if( value.GetType() == Property::STRING )
218 std::string alphaFunctionValue = value.Get< std::string >();
220 if( alphaFunctionValue == "LINEAR" )
222 animator->alphaFunction = AlphaFunction(AlphaFunction::LINEAR);
224 else if( ! alphaFunctionValue.compare(0, 5, "EASE_" ) )
226 if( alphaFunctionValue == "EASE_IN" )
228 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN);
230 else if( alphaFunctionValue == "EASE_OUT" )
232 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT);
234 else if( ! alphaFunctionValue.compare( 5, 3, "IN_" ) )
236 if( ! alphaFunctionValue.compare(8, -1, "SQUARE" ))
238 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_SQUARE);
240 else if( ! alphaFunctionValue.compare(8, -1, "OUT" ))
242 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_OUT);
244 else if( ! alphaFunctionValue.compare(8, -1, "OUT_SINE" ))
246 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_OUT_SINE);
248 else if( ! alphaFunctionValue.compare(8, -1, "SINE" ))
250 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_SINE);
253 else if( ! alphaFunctionValue.compare( 5, 4, "OUT_" ) )
255 if( ! alphaFunctionValue.compare(9, -1, "SQUARE" ) )
257 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT_SQUARE);
259 else if( ! alphaFunctionValue.compare(9, -1, "SINE" ) )
261 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT_SINE);
263 else if( ! alphaFunctionValue.compare(9, -1, "BACK" ) )
265 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT_BACK);
269 else if( alphaFunctionValue == "REVERSE" )
271 animator->alphaFunction = AlphaFunction(AlphaFunction::REVERSE);
273 else if( alphaFunctionValue == "BOUNCE" )
275 animator->alphaFunction = AlphaFunction(AlphaFunction::BOUNCE);
277 else if( alphaFunctionValue == "SIN" )
279 animator->alphaFunction = AlphaFunction(AlphaFunction::SIN);
284 animator->animate = false;
287 else if( key == TOKEN_TIME_PERIOD )
289 Property::Map timeMap = value.Get< Property::Map >();
290 for( size_t timeMapIdx = 0; timeMapIdx < timeMap.Count(); ++timeMapIdx )
292 const KeyValuePair pair( timeMap.GetKeyValue( timeMapIdx ) );
293 if( pair.first.type == Property::Key::INDEX )
297 const std::string& key( pair.first.stringKey );
299 if( key == TOKEN_DELAY )
301 animator->timePeriodDelay = pair.second.Get< float >();
303 else if( key == TOKEN_DURATION )
305 animator->timePeriodDuration = pair.second.Get< float >();
315 void TransitionData::Add( Animator* animator )
317 mAnimators.PushBack( animator );
320 TransitionData::Iterator TransitionData::Begin() const
322 return mAnimators.Begin();
325 TransitionData::Iterator TransitionData::End() const
327 return mAnimators.End();
330 size_t TransitionData::Count() const
332 return mAnimators.Count();
335 Property::Map TransitionData::GetAnimatorAt( size_t index )
337 DALI_ASSERT_ALWAYS( index < Count() && "index exceeds bounds" );
339 Animator* animator = mAnimators[index];
341 map[TOKEN_TARGET] = animator->objectName;
342 if( animator->propertyKey.type == Property::Key::INDEX )
344 map[TOKEN_PROPERTY] = animator->propertyKey.indexKey;
348 map[TOKEN_PROPERTY] = animator->propertyKey.stringKey;
350 if( animator->initialValue.GetType() != Property::NONE )
352 map[TOKEN_INITIAL_VALUE] = animator->initialValue;
354 if( animator->targetValue.GetType() != Property::NONE )
356 map[TOKEN_TARGET_VALUE] = animator->targetValue;
358 if( animator->animate )
360 Property::Map animateMap;
362 if( animator->alphaFunction.GetMode() == AlphaFunction::BUILTIN_FUNCTION )
364 animateMap.Add(TOKEN_ALPHA_FUNCTION, GetEnumerationName( animator->alphaFunction.GetBuiltinFunction(),
365 ALPHA_FUNCTION_BUILTIN_TABLE,
366 ALPHA_FUNCTION_BUILTIN_TABLE_COUNT ));
368 else if( animator->alphaFunction.GetMode() == AlphaFunction::BEZIER )
370 Vector4 controlPoints = animator->alphaFunction.GetBezierControlPoints();
371 animateMap.Add( TOKEN_ALPHA_FUNCTION, controlPoints );
373 animateMap.Add(TOKEN_TIME_PERIOD, Property::Map()
374 .Add( TOKEN_DELAY, animator->timePeriodDelay )
375 .Add( TOKEN_DURATION, animator->timePeriodDuration ));
377 map[TOKEN_ANIMATOR] = animateMap;
383 } // namespace Internal
384 } // namespace Toolkit