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/devel-api/scripting/scripting.h>
22 #include <dali/internal/event/common/property-helper.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali/public-api/object/property-array.h>
25 #include <dali/public-api/object/type-registry.h>
31 bool EnumStringToInteger(const char* const value, const StringEnum* const enumTable, uint32_t tableCount, int& integerEnum)
38 if(value && enumTable && tableCount)
40 const char* pValue = value;
46 const StringEnum* table = enumTable;
48 for(uint32_t i = 0; i < tableCount; ++i)
50 if(Internal::CompareTokens(pValue, table->string, size))
63 // allow comma separated or'd value
64 if(*(pValue + size) == ',')
77 DALI_LOG_ERROR("Unknown enumeration string %s\n", value);
82 uint32_t FindEnumIndex(const char* value, const StringEnum* table, uint32_t tableCount)
86 for(uint32_t i = 0; i < tableCount; ++i, ++index)
88 uint32_t sizeIgnored = 0;
89 if(Internal::CompareTokens(value, table->string, sizeIgnored))
98 DALI_LOG_ERROR("Unknown enumeration string %s\n", value);
103 Actor NewActor(const Property::Map& map)
107 // First find type and create Actor
108 Property::Value* typeValue = map.Find("type");
111 TypeInfo type = TypeRegistry::Get().GetTypeInfo(typeValue->Get<std::string>());
114 handle = type.CreateInstance();
120 DALI_LOG_ERROR("Actor type not provided\n");
124 Actor actor(Actor::DownCast(handle));
128 // Now set the properties, or create children
129 for(Property::Map::SizeType i = 0, mapCount = map.Count(); i < mapCount; ++i)
131 const KeyValuePair pair(map.GetKeyValue(i));
132 if(pair.first.type == Property::Key::INDEX)
136 const std::string& key(pair.first.stringKey);
142 const Property::Value& value(pair.second);
147 Property::Array actorArray = value.Get<Property::Array>();
148 for(Property::Array::SizeType i = 0; i < actorArray.Size(); ++i)
150 actor.Add(NewActor(actorArray[i].Get<Property::Map>()));
155 Property::Index index(actor.GetPropertyIndex(key));
157 if(index != Property::INVALID_INDEX)
159 actor.SetProperty(index, value);
168 void CreatePropertyMap(Actor actor, Property::Map& map)
174 map["type"] = actor.GetTypeName();
176 // Default properties
177 Property::IndexContainer indices;
178 actor.GetPropertyIndices(indices);
179 const Property::IndexContainer::ConstIterator endIter = indices.End();
181 for(Property::IndexContainer::Iterator iter = indices.Begin(); iter != endIter; ++iter)
183 map[actor.GetPropertyName(*iter)] = actor.GetProperty(*iter);
187 std::size_t childCount(actor.GetChildCount());
190 Property::Array childArray;
191 for(uint32_t child = 0; child < childCount; ++child)
193 Property::Map childMap;
194 CreatePropertyMap(actor.GetChildAt(child), childMap);
195 childArray.PushBack(childMap);
197 map["actors"] = childArray;
202 void NewAnimation(const Property::Map& map, Dali::AnimationData& outputAnimationData)
204 // Note: Builder cannot currently pass generic Property::Maps "{" that are nested, so currently we can only have one AnimateTo per animation.
205 Dali::AnimationData::AnimationDataElement* element = new Dali::AnimationData::AnimationDataElement();
207 element->alphaFunction = AlphaFunction::LINEAR;
208 element->timePeriodDelay = 0.0f;
209 element->timePeriodDuration = 1.0f;
211 // Now set the properties, or create children
212 for(Property::Map::SizeType i = 0, animationMapCount = map.Count(); i < animationMapCount; ++i)
214 const KeyValuePair pair(map.GetKeyValue(i));
215 if(pair.first.type == Property::Key::INDEX)
217 continue; // We don't consider index keys.
219 const std::string& key(pair.first.stringKey);
221 const Property::Value& value(pair.second);
225 element->actor = value.Get<std::string>();
227 else if(key == "property")
229 element->property = value.Get<std::string>();
231 else if(key == "value")
233 element->value = value;
235 else if(key == "alphaFunction")
237 std::string alphaFunctionValue = value.Get<std::string>();
239 if(alphaFunctionValue == "LINEAR")
241 element->alphaFunction = AlphaFunction::LINEAR;
243 else if(alphaFunctionValue == "REVERSE")
245 element->alphaFunction = AlphaFunction::REVERSE;
247 else if(alphaFunctionValue == "EASE_IN_SQUARE")
249 element->alphaFunction = AlphaFunction::EASE_IN_SQUARE;
251 else if(alphaFunctionValue == "EASE_OUT_SQUARE")
253 element->alphaFunction = AlphaFunction::EASE_OUT_SQUARE;
255 else if(alphaFunctionValue == "EASE_IN")
257 element->alphaFunction = AlphaFunction::EASE_IN;
259 else if(alphaFunctionValue == "EASE_OUT")
261 element->alphaFunction = AlphaFunction::EASE_OUT;
263 else if(alphaFunctionValue == "EASE_IN_OUT")
265 element->alphaFunction = AlphaFunction::EASE_IN_OUT;
267 else if(alphaFunctionValue == "EASE_IN_SINE")
269 element->alphaFunction = AlphaFunction::EASE_IN_SINE;
271 else if(alphaFunctionValue == "EASE_OUT_SINE")
273 element->alphaFunction = AlphaFunction::EASE_OUT_SINE;
275 else if(alphaFunctionValue == "EASE_IN_OUT_SINE")
277 element->alphaFunction = AlphaFunction::EASE_IN_OUT_SINE;
279 else if(alphaFunctionValue == "BOUNCE")
281 element->alphaFunction = AlphaFunction::BOUNCE;
283 else if(alphaFunctionValue == "SIN")
285 element->alphaFunction = AlphaFunction::SIN;
287 else if(alphaFunctionValue == "EASE_OUT_BACK")
289 element->alphaFunction = AlphaFunction::EASE_OUT_BACK;
292 else if(key == "timePeriod")
294 Property::Map timeMap = value.Get<Property::Map>();
295 for(Property::Map::SizeType i = 0; i < timeMap.Count(); ++i)
297 const KeyValuePair timePair(timeMap.GetKeyValue(i));
298 if(timePair.first.type == Property::Key::INDEX)
302 const std::string& key(timePair.first.stringKey);
306 element->timePeriodDelay = timePair.second.Get<float>();
308 else if(key == "duration")
310 element->timePeriodDuration = timePair.second.Get<float>();
316 outputAnimationData.Add(element);
319 } // namespace Scripting