2 * Copyright (c) 2018 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/public-api/actors/actor.h>
23 #include <dali/public-api/images/resource-image.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/object/property-array.h>
26 #include <dali/internal/common/image-attributes.h>
27 #include <dali/internal/event/common/property-helper.h>
28 #include <dali/internal/event/images/resource-image-impl.h>
29 #include <dali/internal/event/images/frame-buffer-image-impl.h>
30 #include <dali/internal/event/images/buffer-image-impl.h>
41 const StringEnum PIXEL_FORMAT_TABLE[] =
45 { "LA88", Pixel::LA88 },
46 { "RGB565", Pixel::RGB565 },
47 { "BGR565", Pixel::BGR565 },
48 { "RGBA4444", Pixel::RGBA4444 },
49 { "BGRA4444", Pixel::BGRA4444 },
50 { "RGBA5551", Pixel::RGBA5551 },
51 { "BGRA5551", Pixel::BGRA5551 },
52 { "RGB888", Pixel::RGB888 },
53 { "RGB8888", Pixel::RGB8888 },
54 { "BGR8888", Pixel::BGR8888 },
55 { "RGBA8888", Pixel::RGBA8888 },
56 { "BGRA8888", Pixel::BGRA8888 },
57 { "COMPRESSED_R11_EAC", Pixel::COMPRESSED_R11_EAC },
58 { "COMPRESSED_SIGNED_R11_EAC", Pixel::COMPRESSED_SIGNED_R11_EAC },
59 { "COMPRESSED_SIGNED_RG11_EAC", Pixel::COMPRESSED_SIGNED_RG11_EAC },
60 { "COMPRESSED_RG11_EAC", Pixel::COMPRESSED_RG11_EAC },
61 { "COMPRESSED_RGB8_ETC2", Pixel::COMPRESSED_RGB8_ETC2 },
62 { "COMPRESSED_SRGB8_ETC2", Pixel::COMPRESSED_SRGB8_ETC2 },
63 { "COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2", Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 },
64 { "COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2", Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 },
65 { "COMPRESSED_RGBA8_ETC2_EAC", Pixel::COMPRESSED_RGBA8_ETC2_EAC },
66 { "COMPRESSED_SRGB8_ALPHA8_ETC2_EAC", Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC },
67 { "COMPRESSED_RGB8_ETC1", Pixel::COMPRESSED_RGB8_ETC1 },
68 { "COMPRESSED_RGB_PVRTC_4BPPV1", Pixel::COMPRESSED_RGB_PVRTC_4BPPV1 },
70 const uint32_t PIXEL_FORMAT_TABLE_COUNT = static_cast<uint32_t>( sizeof( PIXEL_FORMAT_TABLE ) / sizeof( PIXEL_FORMAT_TABLE[0] ) );
72 const StringEnum IMAGE_FITTING_MODE_TABLE[] =
74 { "SHRINK_TO_FIT", FittingMode::SHRINK_TO_FIT },
75 { "SCALE_TO_FILL", FittingMode::SCALE_TO_FILL },
76 { "FIT_WIDTH", FittingMode::FIT_WIDTH },
77 { "FIT_HEIGHT", FittingMode::FIT_HEIGHT },
79 const uint32_t IMAGE_FITTING_MODE_TABLE_COUNT = static_cast<uint32_t>( sizeof( IMAGE_FITTING_MODE_TABLE ) / sizeof( IMAGE_FITTING_MODE_TABLE[0] ) );
81 const StringEnum IMAGE_SAMPLING_MODE_TABLE[] =
83 { "BOX", SamplingMode::BOX },
84 { "NEAREST", SamplingMode::NEAREST },
85 { "LINEAR", SamplingMode::LINEAR },
86 { "BOX_THEN_NEAREST", SamplingMode::BOX_THEN_NEAREST },
87 { "BOX_THEN_LINEAR", SamplingMode::BOX_THEN_LINEAR },
88 { "NO_FILTER", SamplingMode::NO_FILTER },
89 { "DONT_CARE", SamplingMode::DONT_CARE },
91 const uint32_t IMAGE_SAMPLING_MODE_TABLE_COUNT = static_cast<uint32_t>( sizeof( IMAGE_SAMPLING_MODE_TABLE ) / sizeof( IMAGE_SAMPLING_MODE_TABLE[0] ) );
93 const char* ImageTypeName[] = { "ResourceImage", "FrameBufferImage", "BufferImage" };
94 enum ImageType { RESOURCE_IMAGE, FRAME_BUFFER_IMAGE, BUFFER_IMAGE };
95 const uint32_t imageTypeCount = static_cast<uint32_t>( sizeof( ImageTypeName ) / sizeof( const char* ) );
97 } // unnamed namespace
99 bool EnumStringToInteger( const char * const value, const StringEnum* const enumTable, uint32_t tableCount, int& integerEnum )
106 if( value && enumTable && tableCount )
108 const char* pValue = value;
114 const StringEnum* table = enumTable;
116 for ( uint32_t i = 0; i < tableCount; ++i )
118 if( Internal::CompareTokens( pValue, table->string, size ) )
131 // allow comma separated or'd value
132 if( *(pValue+size) == ',' )
146 DALI_LOG_ERROR( "Unknown enumeration string %s\n", value );
151 uint32_t FindEnumIndex( const char* value, const StringEnum* table, uint32_t tableCount )
155 for ( uint32_t i = 0; i < tableCount; ++i, ++index )
157 uint32_t sizeIgnored = 0;
158 if( Internal::CompareTokens( value, table->string, sizeIgnored ) )
167 DALI_LOG_ERROR( "Unknown enumeration string %s\n", value );
173 Image NewImage( const Property::Value& property )
177 std::string filename;
178 Internal::ImageAttributes attributes = Internal::ImageAttributes::New();
180 const Property::Map* map = property.GetMap();
181 ImageType imageType = RESOURCE_IMAGE; // default to resource image
184 // first check the type as it determines, which other parameters are needed
185 const Property::Value* value = map->Find( "type" );
190 for( uint32_t i = 0; i < imageTypeCount; ++i )
192 if( 0 == type.compare( ImageTypeName[ i ] ) )
194 imageType = static_cast<ImageType>( i );
200 // filename is only needed for resource images
201 if( RESOURCE_IMAGE == imageType )
203 const Property::Value* value = map->Find( "filename" );
206 value->Get( filename );
208 // if empty file, no need to go further
209 if( filename.size() == 0 )
211 DALI_LOG_ERROR( "No filename\n" );
216 // Width and height can be set individually. Dali derives the unspecified
217 // dimension from the aspect ratio of the raw image.
218 int32_t width = 0, height = 0;
220 value = map->Find( "width" );
223 // handle floats and integer the same for json script
224 if( value->GetType() == Property::FLOAT )
226 width = static_cast<uint32_t>( value->Get<float>() );
233 value = map->Find( "height" );
236 if( value->GetType() == Property::FLOAT )
238 height = static_cast<int32_t>( value->Get<float>() );
242 value->Get( height );
245 attributes.SetSize( width, height );
247 Pixel::Format pixelFormat = Pixel::RGBA8888;
248 value = map->Find( "pixelFormat" );
252 value->Get( format );
253 GetEnumeration< Pixel::Format >( format.c_str(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT, pixelFormat );
256 value = map->Find( "fittingMode" );
260 value->Get( fitting );
261 FittingMode::Type mode;
262 if( GetEnumeration< FittingMode::Type >( fitting.c_str(), IMAGE_FITTING_MODE_TABLE, IMAGE_FITTING_MODE_TABLE_COUNT, mode ) )
264 attributes.SetScalingMode( mode );
268 value = map->Find( "samplingMode" );
271 std::string sampling;
272 value->Get( sampling );
273 SamplingMode::Type mode;
274 if( GetEnumeration< SamplingMode::Type >( sampling.c_str(), IMAGE_SAMPLING_MODE_TABLE, IMAGE_SAMPLING_MODE_TABLE_COUNT, mode ) )
276 attributes.SetFilterMode( mode );
280 value = map->Find( "orientation" );
283 bool b = value->Get<bool>();
284 attributes.SetOrientationCorrection( b );
289 case RESOURCE_IMAGE :
291 ret = ResourceImage::New( filename, ImageDimensions( static_cast<uint32_t>( attributes.GetSize().x ), static_cast<uint32_t>( attributes.GetSize().y ) ),
292 attributes.GetScalingMode(), attributes.GetFilterMode(), attributes.GetOrientationCorrection() );
297 ret = BufferImage::New( attributes.GetWidth(),
298 attributes.GetHeight(),
302 case FRAME_BUFFER_IMAGE :
304 ret = FrameBufferImage::New( attributes.GetWidth(),
305 attributes.GetHeight(),
314 } // Image NewImage( Property::Value map )
317 Actor NewActor( const Property::Map& map )
321 // First find type and create Actor
322 Property::Value* typeValue = map.Find( "type" );
325 TypeInfo type = TypeRegistry::Get().GetTypeInfo( typeValue->Get< std::string >() );
328 handle = type.CreateInstance();
334 DALI_LOG_ERROR( "Actor type not provided\n" );
338 Actor actor( Actor::DownCast( handle ) );
342 // Now set the properties, or create children
343 for ( Property::Map::SizeType i = 0, mapCount = map.Count(); i < mapCount; ++i )
345 const KeyValuePair pair( map.GetKeyValue( i ) );
346 if( pair.first.type == Property::Key::INDEX )
350 const std::string& key( pair.first.stringKey );
356 const Property::Value& value( pair.second );
358 if ( key == "actors" )
361 Property::Array actorArray = value.Get< Property::Array >();
362 for ( Property::Array::SizeType i = 0; i < actorArray.Size(); ++i)
364 actor.Add( NewActor( actorArray[i].Get< Property::Map >() ) );
369 Property::Index index( actor.GetPropertyIndex( key ) );
371 if ( index != Property::INVALID_INDEX )
373 actor.SetProperty( index, value );
382 void CreatePropertyMap( Actor actor, Property::Map& map )
388 map[ "type" ] = actor.GetTypeName();
390 // Default properties
391 Property::IndexContainer indices;
392 actor.GetPropertyIndices( indices );
393 const Property::IndexContainer::ConstIterator endIter = indices.End();
395 for ( Property::IndexContainer::Iterator iter = indices.Begin(); iter != endIter; ++iter )
397 map[ actor.GetPropertyName( *iter ) ] = actor.GetProperty( *iter );
401 std::size_t childCount( actor.GetChildCount() );
404 Property::Array childArray;
405 for ( uint32_t child = 0; child < childCount; ++child )
407 Property::Map childMap;
408 CreatePropertyMap( actor.GetChildAt( child ), childMap );
409 childArray.PushBack( childMap );
411 map[ "actors" ] = childArray;
416 void CreatePropertyMap( Image image, Property::Map& map )
422 std::string imageType( "ResourceImage" );
424 // Get Type - cannot use TypeRegistry as Image is not an Object and thus, not registered
425 BufferImage bufferImage = BufferImage::DownCast( image );
428 imageType = "BufferImage";
429 map[ "pixelFormat" ] = GetEnumerationName< Pixel::Format >( bufferImage.GetPixelFormat(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT );
431 else if ( FrameBufferImage::DownCast( image ) )
433 imageType = "FrameBufferImage";
436 map[ "type" ] = imageType;
438 ResourceImage resourceImage = ResourceImage::DownCast( image );
441 map[ "filename" ] = resourceImage.GetUrl();
444 int32_t width( image.GetWidth() );
445 int32_t height( image.GetHeight() );
447 if ( width && height )
449 map[ "width" ] = width;
450 map[ "height" ] = height;
455 void NewAnimation( const Property::Map& map, Dali::AnimationData& outputAnimationData )
457 // Note: Builder cannot currently pass generic Property::Maps "{" that are nested, so currently we can only have one AnimateTo per animation.
458 Dali::AnimationData::AnimationDataElement* element = new Dali::AnimationData::AnimationDataElement();
459 element->alphaFunction = AlphaFunction::LINEAR;
460 element->timePeriodDelay = 0.0f;
461 element->timePeriodDuration = 1.0f;
463 // Now set the properties, or create children
464 for( Property::Map::SizeType i = 0, animationMapCount = map.Count(); i < animationMapCount; ++i )
466 const KeyValuePair pair( map.GetKeyValue( i ) );
467 if( pair.first.type == Property::Key::INDEX )
469 continue; // We don't consider index keys.
471 const std::string& key( pair.first.stringKey );
473 const Property::Value& value( pair.second );
477 element->actor = value.Get< std::string >();
479 else if( key == "property" )
481 element->property = value.Get< std::string >();
483 else if( key == "value" )
485 element->value = value;
487 else if( key == "alphaFunction" )
489 std::string alphaFunctionValue = value.Get< std::string >();
491 if( alphaFunctionValue == "LINEAR" )
493 element->alphaFunction = AlphaFunction::LINEAR;
495 else if( alphaFunctionValue == "REVERSE" )
497 element->alphaFunction = AlphaFunction::REVERSE;
499 else if( alphaFunctionValue == "EASE_IN_SQUARE" )
501 element->alphaFunction = AlphaFunction::EASE_IN_SQUARE;
503 else if( alphaFunctionValue == "EASE_OUT_SQUARE" )
505 element->alphaFunction = AlphaFunction::EASE_OUT_SQUARE;
507 else if( alphaFunctionValue == "EASE_IN" )
509 element->alphaFunction = AlphaFunction::EASE_IN;
511 else if( alphaFunctionValue == "EASE_OUT" )
513 element->alphaFunction = AlphaFunction::EASE_OUT;
515 else if( alphaFunctionValue == "EASE_IN_OUT" )
517 element->alphaFunction = AlphaFunction::EASE_IN_OUT;
519 else if( alphaFunctionValue == "EASE_IN_SINE" )
521 element->alphaFunction = AlphaFunction::EASE_IN_SINE;
523 else if( alphaFunctionValue == "EASE_OUT_SINE" )
525 element->alphaFunction = AlphaFunction::EASE_OUT_SINE;
527 else if( alphaFunctionValue == "EASE_IN_OUT_SINE" )
529 element->alphaFunction = AlphaFunction::EASE_IN_OUT_SINE;
531 else if( alphaFunctionValue == "BOUNCE" )
533 element->alphaFunction = AlphaFunction::BOUNCE;
535 else if( alphaFunctionValue == "SIN" )
537 element->alphaFunction = AlphaFunction::SIN;
539 else if( alphaFunctionValue == "EASE_OUT_BACK" )
541 element->alphaFunction = AlphaFunction::EASE_OUT_BACK;
544 else if( key == "timePeriod" )
546 Property::Map timeMap = value.Get< Property::Map >();
547 for( Property::Map::SizeType i = 0; i < timeMap.Count(); ++i )
549 const KeyValuePair timePair( timeMap.GetKeyValue( i ) );
550 if( timePair.first.type == Property::Key::INDEX )
554 const std::string& key( timePair.first.stringKey );
558 element->timePeriodDelay = timePair.second.Get< float >();
560 else if( key == "duration" )
562 element->timePeriodDuration = timePair.second.Get< float >();
568 outputAnimationData.Add( element );
571 } // namespace scripting