2 * Copyright (c) 2020 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 )
71 /// Parses a Property::Array and sets up the animator appropriately
72 void ParseArray(TransitionData::Animator* animator, const Property::Array* array)
75 Vector4 controlPoints;
76 if( array && array->Count() >= 4 )
78 for( size_t vecIdx = 0; vecIdx < 4; ++vecIdx )
80 const Property::Value& v = array->GetElementAt(vecIdx);
81 if( v.GetType() == Property::FLOAT )
83 controlPoints[vecIdx] = v.Get<float>();
99 Vector2 controlPoint1( controlPoints.x, controlPoints.y );
100 Vector2 controlPoint2( controlPoints.z, controlPoints.w );
101 animator->alphaFunction = AlphaFunction( controlPoint1, controlPoint2 );
105 animator->animate = false;
109 /// Parses a string value and sets up the animator appropriately
110 void ParseString(TransitionData::Animator* animator, std::string alphaFunctionValue)
112 if( alphaFunctionValue == "LINEAR" )
114 animator->alphaFunction = AlphaFunction(AlphaFunction::LINEAR);
116 else if( ! alphaFunctionValue.compare(0, 5, "EASE_" ) )
118 if( alphaFunctionValue == "EASE_IN" )
120 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN);
122 else if( alphaFunctionValue == "EASE_OUT" )
124 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT);
126 else if( ! alphaFunctionValue.compare( 5, 3, "IN_" ) )
128 if( ! alphaFunctionValue.compare(8, -1, "SQUARE" ))
130 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_SQUARE);
132 else if( ! alphaFunctionValue.compare(8, -1, "OUT" ))
134 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_OUT);
136 else if( ! alphaFunctionValue.compare(8, -1, "OUT_SINE" ))
138 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_OUT_SINE);
140 else if( ! alphaFunctionValue.compare(8, -1, "SINE" ))
142 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_SINE);
145 else if( ! alphaFunctionValue.compare( 5, 4, "OUT_" ) )
147 if( ! alphaFunctionValue.compare(9, -1, "SQUARE" ) )
149 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT_SQUARE);
151 else if( ! alphaFunctionValue.compare(9, -1, "SINE" ) )
153 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT_SINE);
155 else if( ! alphaFunctionValue.compare(9, -1, "BACK" ) )
157 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT_BACK);
161 else if( alphaFunctionValue == "REVERSE" )
163 animator->alphaFunction = AlphaFunction(AlphaFunction::REVERSE);
165 else if( alphaFunctionValue == "BOUNCE" )
167 animator->alphaFunction = AlphaFunction(AlphaFunction::BOUNCE);
169 else if( alphaFunctionValue == "SIN" )
171 animator->alphaFunction = AlphaFunction(AlphaFunction::SIN);
174 } // unnamed namespace
176 TransitionData::TransitionData()
180 TransitionData::~TransitionData()
184 TransitionDataPtr TransitionData::New( const Property::Array& value )
186 TransitionDataPtr transitionData( new TransitionData() );
187 transitionData->Initialize(value);
188 return transitionData;
191 TransitionDataPtr TransitionData::New( const Property::Map& value )
193 TransitionDataPtr transitionData( new TransitionData() );
194 transitionData->Initialize(value);
195 return transitionData;
199 void TransitionData::Initialize( const Property::Map& map )
201 TransitionData::Animator* animator = ConvertMap( map );
205 void TransitionData::Initialize( const Property::Array& array )
207 for( unsigned int arrayIdx = 0, transitionArrayCount = array.Count(); arrayIdx < transitionArrayCount; ++arrayIdx )
209 const Property::Value& element = array.GetElementAt( arrayIdx );
210 // Expect each child to be an object representing an animator:
212 const Property::Map* map = element.GetMap();
215 TransitionData::Animator* animator = ConvertMap( *map );
221 TransitionData::Animator* TransitionData::ConvertMap( const Property::Map& map)
223 TransitionData::Animator* animator = new TransitionData::Animator();
224 animator->alphaFunction = AlphaFunction::LINEAR;
225 animator->timePeriodDelay = 0.0f;
226 animator->timePeriodDuration = 1.0f;
228 for( unsigned int mapIdx = 0; mapIdx < map.Count(); ++mapIdx )
230 const KeyValuePair pair( map.GetKeyValue( mapIdx ) );
231 if( pair.first.type == Property::Key::INDEX )
233 continue; // We don't consider index keys.
236 const std::string& key( pair.first.stringKey );
237 const Property::Value& value( pair.second );
239 if( key == TOKEN_TARGET )
241 animator->objectName = value.Get< std::string >();
243 else if( key == TOKEN_PROPERTY )
245 if( value.GetType() == Property::STRING )
247 animator->propertyKey = Property::Key( value.Get<std::string>() );
251 animator->propertyKey = Property::Key( value.Get<int>() );
254 else if( key == TOKEN_INITIAL_VALUE )
256 animator->initialValue = value;
258 else if( key == TOKEN_TARGET_VALUE )
260 animator->targetValue = value;
262 else if( key == TOKEN_ANIMATOR )
264 animator->animate = true;
265 Property::Map animatorMap = value.Get< Property::Map >();
266 for( size_t animatorMapIdx = 0; animatorMapIdx < animatorMap.Count(); ++animatorMapIdx )
268 const KeyValuePair pair( animatorMap.GetKeyValue( animatorMapIdx ) );
270 if( pair.first.type == Property::Key::INDEX )
272 continue; // We don't consider index keys.
275 const std::string& key( pair.first.stringKey );
276 const Property::Value& value( pair.second );
278 if( key == TOKEN_ALPHA_FUNCTION )
280 if( value.GetType() == Property::ARRAY )
282 ParseArray(animator, value.GetArray());
284 else if( value.GetType() == Property::VECTOR4 )
286 Vector4 controlPoints = value.Get<Vector4>();
287 Vector2 controlPoint1( controlPoints.x, controlPoints.y );
288 Vector2 controlPoint2( controlPoints.z, controlPoints.w );
289 animator->alphaFunction = AlphaFunction( controlPoint1, controlPoint2 );
291 else if( value.GetType() == Property::STRING )
293 ParseString(animator, value.Get< std::string >());
297 animator->animate = false;
300 else if( key == TOKEN_TIME_PERIOD )
302 Property::Map timeMap = value.Get< Property::Map >();
303 for( size_t timeMapIdx = 0; timeMapIdx < timeMap.Count(); ++timeMapIdx )
305 const KeyValuePair pair( timeMap.GetKeyValue( timeMapIdx ) );
306 if( pair.first.type == Property::Key::INDEX )
310 const std::string& key( pair.first.stringKey );
312 if( key == TOKEN_DELAY )
314 animator->timePeriodDelay = pair.second.Get< float >();
316 else if( key == TOKEN_DURATION )
318 animator->timePeriodDuration = pair.second.Get< float >();
328 void TransitionData::Add( Animator* animator )
330 mAnimators.PushBack( animator );
333 TransitionData::Iterator TransitionData::Begin() const
335 return mAnimators.Begin();
338 TransitionData::Iterator TransitionData::End() const
340 return mAnimators.End();
343 size_t TransitionData::Count() const
345 return mAnimators.Count();
348 Property::Map TransitionData::GetAnimatorAt( size_t index )
350 DALI_ASSERT_ALWAYS( index < Count() && "index exceeds bounds" );
352 Animator* animator = mAnimators[index];
354 map[TOKEN_TARGET] = animator->objectName;
355 if( animator->propertyKey.type == Property::Key::INDEX )
357 map[TOKEN_PROPERTY] = animator->propertyKey.indexKey;
361 map[TOKEN_PROPERTY] = animator->propertyKey.stringKey;
363 if( animator->initialValue.GetType() != Property::NONE )
365 map[TOKEN_INITIAL_VALUE] = animator->initialValue;
367 if( animator->targetValue.GetType() != Property::NONE )
369 map[TOKEN_TARGET_VALUE] = animator->targetValue;
371 if( animator->animate )
373 Property::Map animateMap;
375 if( animator->alphaFunction.GetMode() == AlphaFunction::BUILTIN_FUNCTION )
377 animateMap.Add(TOKEN_ALPHA_FUNCTION, GetEnumerationName( animator->alphaFunction.GetBuiltinFunction(),
378 ALPHA_FUNCTION_BUILTIN_TABLE,
379 ALPHA_FUNCTION_BUILTIN_TABLE_COUNT ));
381 else if( animator->alphaFunction.GetMode() == AlphaFunction::BEZIER )
383 Vector4 controlPoints = animator->alphaFunction.GetBezierControlPoints();
384 animateMap.Add( TOKEN_ALPHA_FUNCTION, controlPoints );
386 animateMap.Add(TOKEN_TIME_PERIOD, Property::Map()
387 .Add( TOKEN_DELAY, animator->timePeriodDelay )
388 .Add( TOKEN_DURATION, animator->timePeriodDuration ));
390 map[TOKEN_ANIMATOR] = animateMap;
396 } // namespace Internal
397 } // namespace Toolkit