2 * Copyright (c) 2021 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-toolkit/public-api/controls/control-impl.h>
23 #include <dali-toolkit/public-api/controls/control.h>
24 #include <dali/dali.h>
25 #include <dali/devel-api/scripting/enum-helper.h>
26 #include <dali/integration-api/debug.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");
42 const char* TOKEN_ANIMATION_TYPE("animationType");
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)
69 /// Parses a Property::Array and sets up the animator appropriately
70 void ParseArray(TransitionData::Animator* animator, const Property::Array* array)
73 Vector4 controlPoints;
74 if(array && array->Count() >= 4)
76 for(size_t vecIdx = 0; vecIdx < 4; ++vecIdx)
78 const Property::Value& v = array->GetElementAt(vecIdx);
79 if(v.GetType() == Property::FLOAT)
81 controlPoints[vecIdx] = v.Get<float>();
97 Vector2 controlPoint1(controlPoints.x, controlPoints.y);
98 Vector2 controlPoint2(controlPoints.z, controlPoints.w);
99 animator->alphaFunction = AlphaFunction(controlPoint1, controlPoint2);
103 animator->animate = false;
107 /// Parses a string value and sets up the animator appropriately
108 void ParseString(TransitionData::Animator* animator, std::string alphaFunctionValue)
110 if(alphaFunctionValue == "LINEAR")
112 animator->alphaFunction = AlphaFunction(AlphaFunction::LINEAR);
114 else if(!alphaFunctionValue.compare(0, 5, "EASE_"))
116 if(alphaFunctionValue == "EASE_IN")
118 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN);
120 else if(alphaFunctionValue == "EASE_OUT")
122 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT);
124 else if(!alphaFunctionValue.compare(5, 3, "IN_"))
126 if(!alphaFunctionValue.compare(8, -1, "SQUARE"))
128 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_SQUARE);
130 else if(!alphaFunctionValue.compare(8, -1, "OUT"))
132 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_OUT);
134 else if(!alphaFunctionValue.compare(8, -1, "OUT_SINE"))
136 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_OUT_SINE);
138 else if(!alphaFunctionValue.compare(8, -1, "SINE"))
140 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_IN_SINE);
143 else if(!alphaFunctionValue.compare(5, 4, "OUT_"))
145 if(!alphaFunctionValue.compare(9, -1, "SQUARE"))
147 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT_SQUARE);
149 else if(!alphaFunctionValue.compare(9, -1, "SINE"))
151 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT_SINE);
153 else if(!alphaFunctionValue.compare(9, -1, "BACK"))
155 animator->alphaFunction = AlphaFunction(AlphaFunction::EASE_OUT_BACK);
159 else if(alphaFunctionValue == "REVERSE")
161 animator->alphaFunction = AlphaFunction(AlphaFunction::REVERSE);
163 else if(alphaFunctionValue == "BOUNCE")
165 animator->alphaFunction = AlphaFunction(AlphaFunction::BOUNCE);
167 else if(alphaFunctionValue == "SIN")
169 animator->alphaFunction = AlphaFunction(AlphaFunction::SIN);
172 } // unnamed namespace
174 TransitionData::TransitionData()
178 TransitionData::~TransitionData()
182 TransitionDataPtr TransitionData::New(const Property::Array& value)
184 TransitionDataPtr transitionData(new TransitionData());
185 transitionData->Initialize(value);
186 return transitionData;
189 TransitionDataPtr TransitionData::New(const Property::Map& value)
191 TransitionDataPtr transitionData(new TransitionData());
192 transitionData->Initialize(value);
193 return transitionData;
196 void TransitionData::Initialize(const Property::Map& map)
198 TransitionData::Animator* animator = ConvertMap(map);
202 void TransitionData::Initialize(const Property::Array& array)
204 for(unsigned int arrayIdx = 0, transitionArrayCount = array.Count(); arrayIdx < transitionArrayCount; ++arrayIdx)
206 const Property::Value& element = array.GetElementAt(arrayIdx);
207 // Expect each child to be an object representing an animator:
209 const Property::Map* map = element.GetMap();
212 TransitionData::Animator* animator = ConvertMap(*map);
218 TransitionData::Animator* TransitionData::ConvertMap(const Property::Map& map)
220 TransitionData::Animator* animator = new TransitionData::Animator();
221 animator->alphaFunction = AlphaFunction::LINEAR;
222 animator->timePeriodDelay = 0.0f;
223 animator->timePeriodDuration = 1.0f;
225 for(unsigned int mapIdx = 0; mapIdx < map.Count(); ++mapIdx)
227 const KeyValuePair pair(map.GetKeyValue(mapIdx));
228 if(pair.first.type == Property::Key::INDEX)
230 continue; // We don't consider index keys.
233 const std::string& key(pair.first.stringKey);
234 const Property::Value& value(pair.second);
236 if(key == TOKEN_TARGET)
238 animator->objectName = value.Get<std::string>();
240 else if(key == TOKEN_PROPERTY)
242 if(value.GetType() == Property::STRING)
244 animator->propertyKey = Property::Key(value.Get<std::string>());
248 animator->propertyKey = Property::Key(value.Get<int>());
251 else if(key == TOKEN_INITIAL_VALUE)
253 animator->initialValue = value;
255 else if(key == TOKEN_TARGET_VALUE)
257 animator->targetValue = value;
259 else if(key == TOKEN_ANIMATOR)
261 animator->animate = true;
262 Property::Map animatorMap = value.Get<Property::Map>();
263 for(size_t animatorMapIdx = 0; animatorMapIdx < animatorMap.Count(); ++animatorMapIdx)
265 const KeyValuePair pair(animatorMap.GetKeyValue(animatorMapIdx));
267 if(pair.first.type == Property::Key::INDEX)
269 continue; // We don't consider index keys.
272 const std::string& key(pair.first.stringKey);
273 const Property::Value& value(pair.second);
275 if(key == TOKEN_ALPHA_FUNCTION)
277 if(value.GetType() == Property::ARRAY)
279 ParseArray(animator, value.GetArray());
281 else if(value.GetType() == Property::VECTOR4)
283 Vector4 controlPoints = value.Get<Vector4>();
284 Vector2 controlPoint1(controlPoints.x, controlPoints.y);
285 Vector2 controlPoint2(controlPoints.z, controlPoints.w);
286 animator->alphaFunction = AlphaFunction(controlPoint1, controlPoint2);
288 else if(value.GetType() == Property::STRING)
290 ParseString(animator, value.Get<std::string>());
294 animator->animate = false;
297 else if(key == TOKEN_TIME_PERIOD)
299 Property::Map timeMap = value.Get<Property::Map>();
300 for(size_t timeMapIdx = 0; timeMapIdx < timeMap.Count(); ++timeMapIdx)
302 const KeyValuePair pair(timeMap.GetKeyValue(timeMapIdx));
303 if(pair.first.type == Property::Key::INDEX)
307 const std::string& key(pair.first.stringKey);
309 if(key == TOKEN_DELAY)
311 animator->timePeriodDelay = pair.second.Get<float>();
313 else if(key == TOKEN_DURATION)
315 animator->timePeriodDuration = pair.second.Get<float>();
319 else if(key == TOKEN_ANIMATION_TYPE)
321 if((value.GetType() == Property::STRING))
323 if(value.Get<std::string>() == "TO")
325 animator->animationType = AnimationType::TO;
327 else if(value.Get<std::string>() == "BETWEEN")
329 animator->animationType = AnimationType::BETWEEN;
331 else if(value.Get<std::string>() == "BY")
333 animator->animationType = AnimationType::BY;
343 void TransitionData::Add(Animator* animator)
345 mAnimators.PushBack(animator);
348 TransitionData::Iterator TransitionData::Begin() const
350 return mAnimators.Begin();
353 TransitionData::Iterator TransitionData::End() const
355 return mAnimators.End();
358 size_t TransitionData::Count() const
360 return mAnimators.Count();
363 Property::Map TransitionData::GetAnimatorAt(size_t index)
365 DALI_ASSERT_ALWAYS(index < Count() && "index exceeds bounds");
367 Animator* animator = mAnimators[index];
369 map[TOKEN_TARGET] = animator->objectName;
370 if(animator->propertyKey.type == Property::Key::INDEX)
372 map[TOKEN_PROPERTY] = animator->propertyKey.indexKey;
376 map[TOKEN_PROPERTY] = animator->propertyKey.stringKey;
378 if(animator->initialValue.GetType() != Property::NONE)
380 map[TOKEN_INITIAL_VALUE] = animator->initialValue;
382 if(animator->targetValue.GetType() != Property::NONE)
384 map[TOKEN_TARGET_VALUE] = animator->targetValue;
386 if(animator->animate)
388 Property::Map animateMap;
390 if(animator->alphaFunction.GetMode() == AlphaFunction::BUILTIN_FUNCTION)
392 animateMap.Add(TOKEN_ALPHA_FUNCTION, GetEnumerationName(animator->alphaFunction.GetBuiltinFunction(), ALPHA_FUNCTION_BUILTIN_TABLE, ALPHA_FUNCTION_BUILTIN_TABLE_COUNT));
394 else if(animator->alphaFunction.GetMode() == AlphaFunction::BEZIER)
396 Vector4 controlPoints = animator->alphaFunction.GetBezierControlPoints();
397 animateMap.Add(TOKEN_ALPHA_FUNCTION, controlPoints);
399 animateMap.Add(TOKEN_TIME_PERIOD, Property::Map().Add(TOKEN_DELAY, animator->timePeriodDelay).Add(TOKEN_DURATION, animator->timePeriodDuration));
401 map[TOKEN_ANIMATOR] = animateMap;
407 } // namespace Internal
408 } // namespace Toolkit