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/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 // Tables used here for converting strings to the enumerations and vice versa
42 const StringEnum IMAGE_LOAD_POLICY_TABLE[] =
44 { "IMMEDIATE", ResourceImage::IMMEDIATE },
45 { "ON_DEMAND", ResourceImage::ON_DEMAND },
47 const unsigned int IMAGE_LOAD_POLICY_TABLE_COUNT = sizeof( IMAGE_LOAD_POLICY_TABLE ) / sizeof( IMAGE_LOAD_POLICY_TABLE[0] );
49 const StringEnum IMAGE_RELEASE_POLICY_TABLE[] =
51 { "UNUSED", Image::UNUSED },
52 { "NEVER", Image::NEVER },
54 const unsigned int IMAGE_RELEASE_POLICY_TABLE_COUNT = sizeof( IMAGE_RELEASE_POLICY_TABLE ) / sizeof( IMAGE_RELEASE_POLICY_TABLE[0] );
56 const StringEnum PIXEL_FORMAT_TABLE[] =
60 { "LA88", Pixel::LA88 },
61 { "RGB565", Pixel::RGB565 },
62 { "BGR565", Pixel::BGR565 },
63 { "RGBA4444", Pixel::RGBA4444 },
64 { "BGRA4444", Pixel::BGRA4444 },
65 { "RGBA5551", Pixel::RGBA5551 },
66 { "BGRA5551", Pixel::BGRA5551 },
67 { "RGB888", Pixel::RGB888 },
68 { "RGB8888", Pixel::RGB8888 },
69 { "BGR8888", Pixel::BGR8888 },
70 { "RGBA8888", Pixel::RGBA8888 },
71 { "BGRA8888", Pixel::BGRA8888 },
72 { "COMPRESSED_R11_EAC", Pixel::COMPRESSED_R11_EAC },
73 { "COMPRESSED_SIGNED_R11_EAC", Pixel::COMPRESSED_SIGNED_R11_EAC },
74 { "COMPRESSED_SIGNED_RG11_EAC", Pixel::COMPRESSED_SIGNED_RG11_EAC },
75 { "COMPRESSED_RG11_EAC", Pixel::COMPRESSED_RG11_EAC },
76 { "COMPRESSED_RGB8_ETC2", Pixel::COMPRESSED_RGB8_ETC2 },
77 { "COMPRESSED_SRGB8_ETC2", Pixel::COMPRESSED_SRGB8_ETC2 },
78 { "COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2", Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 },
79 { "COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2", Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 },
80 { "COMPRESSED_RGBA8_ETC2_EAC", Pixel::COMPRESSED_RGBA8_ETC2_EAC },
81 { "COMPRESSED_SRGB8_ALPHA8_ETC2_EAC", Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC },
82 { "COMPRESSED_RGB8_ETC1", Pixel::COMPRESSED_RGB8_ETC1 },
83 { "COMPRESSED_RGB_PVRTC_4BPPV1", Pixel::COMPRESSED_RGB_PVRTC_4BPPV1 },
85 const unsigned int PIXEL_FORMAT_TABLE_COUNT = sizeof( PIXEL_FORMAT_TABLE ) / sizeof( PIXEL_FORMAT_TABLE[0] );
87 const StringEnum IMAGE_FITTING_MODE_TABLE[] =
89 { "SHRINK_TO_FIT", FittingMode::SHRINK_TO_FIT },
90 { "SCALE_TO_FILL", FittingMode::SCALE_TO_FILL },
91 { "FIT_WIDTH", FittingMode::FIT_WIDTH },
92 { "FIT_HEIGHT", FittingMode::FIT_HEIGHT },
94 const unsigned int IMAGE_FITTING_MODE_TABLE_COUNT = sizeof( IMAGE_FITTING_MODE_TABLE ) / sizeof( IMAGE_FITTING_MODE_TABLE[0] );
96 const StringEnum IMAGE_SAMPLING_MODE_TABLE[] =
98 { "BOX", SamplingMode::BOX },
99 { "NEAREST", SamplingMode::NEAREST },
100 { "LINEAR", SamplingMode::LINEAR },
101 { "BOX_THEN_NEAREST", SamplingMode::BOX_THEN_NEAREST },
102 { "BOX_THEN_LINEAR", SamplingMode::BOX_THEN_LINEAR },
103 { "NO_FILTER", SamplingMode::NO_FILTER },
104 { "DONT_CARE", SamplingMode::DONT_CARE },
106 const unsigned int IMAGE_SAMPLING_MODE_TABLE_COUNT = sizeof( IMAGE_SAMPLING_MODE_TABLE ) / sizeof( IMAGE_SAMPLING_MODE_TABLE[0] );
108 const char* ImageTypeName[] = { "ResourceImage", "FrameBufferImage", "BufferImage" };
109 enum ImageType { RESOURCE_IMAGE, FRAME_BUFFER_IMAGE, BUFFER_IMAGE };
110 const unsigned int imageTypeCount = sizeof( ImageTypeName ) / sizeof( const char* );
112 } // unnamed namespace
114 bool EnumStringToInteger( const char * const value, const StringEnum* const enumTable, unsigned int tableCount, int& integerEnum )
121 if( value && enumTable && tableCount )
123 const char* pValue = value;
129 const StringEnum* table = enumTable;
131 for ( unsigned int i = 0; i < tableCount; ++i )
133 if( Internal::CompareTokens( pValue, table->string, size ) )
146 // allow comma separated or'd value
147 if( *(pValue+size) == ',' )
161 DALI_LOG_ERROR( "Unknown enumeration string %s\n", value );
166 unsigned int FindEnumIndex( const char* value, const StringEnum* table, unsigned int tableCount )
168 unsigned int index = 0;
170 for ( unsigned int i = 0; i < tableCount; ++i, ++index )
172 size_t sizeIgnored = 0;
173 if( Internal::CompareTokens( value, table->string, sizeIgnored ) )
182 DALI_LOG_ERROR( "Unknown enumeration string %s\n", value );
188 Image NewImage( const Property::Value& property )
192 std::string filename;
193 Internal::ImageAttributes attributes = Internal::ImageAttributes::New();
195 const Property::Map* map = property.GetMap();
196 ImageType imageType = RESOURCE_IMAGE; // default to resource image
199 // first check the type as it determines, which other parameters are needed
200 const Property::Value* value = map->Find( "type" );
205 for( unsigned int i = 0; i < imageTypeCount; ++i )
207 if( 0 == type.compare( ImageTypeName[ i ] ) )
209 imageType = static_cast<ImageType>( i );
215 // filename is only needed for resource images
216 if( RESOURCE_IMAGE == imageType )
218 const Property::Value* value = map->Find( "filename" );
221 value->Get( filename );
223 // if empty file, no need to go further
224 if( filename.size() == 0 )
226 DALI_LOG_ERROR( "No filename\n" );
231 // Width and height can be set individually. Dali derives the unspecified
232 // dimension from the aspect ratio of the raw image.
233 int width = 0, height = 0;
235 value = map->Find( "width" );
238 // handle floats and integer the same for json script
239 if( value->GetType() == Property::FLOAT )
241 width = static_cast<unsigned int>( value->Get<float>() );
248 value = map->Find( "height" );
251 if( value->GetType() == Property::FLOAT )
253 height = static_cast<int>( value->Get<float>() );
257 value->Get( height );
260 attributes.SetSize( width, height );
262 Pixel::Format pixelFormat = Pixel::RGBA8888;
263 value = map->Find( "pixelFormat" );
267 value->Get( format );
268 GetEnumeration< Pixel::Format >( format.c_str(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT, pixelFormat );
271 value = map->Find( "fittingMode" );
275 value->Get( fitting );
276 FittingMode::Type mode;
277 if( GetEnumeration< FittingMode::Type >( fitting.c_str(), IMAGE_FITTING_MODE_TABLE, IMAGE_FITTING_MODE_TABLE_COUNT, mode ) )
279 attributes.SetScalingMode( mode );
283 value = map->Find( "samplingMode" );
286 std::string sampling;
287 value->Get( sampling );
288 SamplingMode::Type mode;
289 if( GetEnumeration< SamplingMode::Type >( sampling.c_str(), IMAGE_SAMPLING_MODE_TABLE, IMAGE_SAMPLING_MODE_TABLE_COUNT, mode ) )
291 attributes.SetFilterMode( mode );
295 value = map->Find( "orientation" );
298 bool b = value->Get<bool>();
299 attributes.SetOrientationCorrection( b );
304 case RESOURCE_IMAGE :
306 ret = ResourceImage::New( filename, ImageDimensions( attributes.GetSize().x, attributes.GetSize().y ),
307 attributes.GetScalingMode(), attributes.GetFilterMode(), attributes.GetOrientationCorrection() );
312 ret = BufferImage::New( attributes.GetWidth(),
313 attributes.GetHeight(),
317 case FRAME_BUFFER_IMAGE :
319 ret = FrameBufferImage::New( attributes.GetWidth(),
320 attributes.GetHeight(),
329 } // Image NewImage( Property::Value map )
332 Actor NewActor( const Property::Map& map )
336 // First find type and create Actor
337 Property::Value* typeValue = map.Find( "type" );
340 TypeInfo type = TypeRegistry::Get().GetTypeInfo( typeValue->Get< std::string >() );
343 handle = type.CreateInstance();
349 DALI_LOG_ERROR( "Actor type not provided\n" );
353 Actor actor( Actor::DownCast( handle ) );
357 // Now set the properties, or create children
358 for ( unsigned int i = 0, mapCount = map.Count(); i < mapCount; ++i )
360 const StringValuePair& pair( map.GetPair( i ) );
361 const std::string& key( pair.first );
367 const Property::Value& value( pair.second );
369 if ( key == "actors" )
372 Property::Array actorArray = value.Get< Property::Array >();
373 for ( Property::Array::SizeType i = 0; i < actorArray.Size(); ++i)
375 actor.Add( NewActor( actorArray[i].Get< Property::Map >() ) );
380 Property::Index index( actor.GetPropertyIndex( key ) );
382 if ( index != Property::INVALID_INDEX )
384 actor.SetProperty( index, value );
393 void CreatePropertyMap( Actor actor, Property::Map& map )
399 map[ "type" ] = actor.GetTypeName();
401 // Default properties
402 Property::IndexContainer indices;
403 actor.GetPropertyIndices( indices );
404 const Property::IndexContainer::ConstIterator endIter = indices.End();
406 for ( Property::IndexContainer::Iterator iter = indices.Begin(); iter != endIter; ++iter )
408 map[ actor.GetPropertyName( *iter ) ] = actor.GetProperty( *iter );
412 unsigned int childCount( actor.GetChildCount() );
415 Property::Array childArray;
416 for ( unsigned int child = 0; child < childCount; ++child )
418 Property::Map childMap;
419 CreatePropertyMap( actor.GetChildAt( child ), childMap );
420 childArray.PushBack( childMap );
422 map[ "actors" ] = childArray;
427 void CreatePropertyMap( Image image, Property::Map& map )
433 std::string imageType( "ResourceImage" );
435 // Get Type - cannot use TypeRegistry as Image is not an Object and thus, not registered
436 BufferImage bufferImage = BufferImage::DownCast( image );
439 imageType = "BufferImage";
440 map[ "pixelFormat" ] = GetEnumerationName< Pixel::Format >( bufferImage.GetPixelFormat(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT );
442 else if ( FrameBufferImage::DownCast( image ) )
444 imageType = "FrameBufferImage";
447 map[ "type" ] = imageType;
448 map[ "releasePolicy" ] = GetEnumerationName< Image::ReleasePolicy >( image.GetReleasePolicy(), IMAGE_RELEASE_POLICY_TABLE, IMAGE_RELEASE_POLICY_TABLE_COUNT );
450 ResourceImage resourceImage = ResourceImage::DownCast( image );
453 map[ "filename" ] = resourceImage.GetUrl();
454 map[ "loadPolicy" ] = GetEnumerationName< ResourceImage::LoadPolicy >( resourceImage.GetLoadPolicy(), IMAGE_LOAD_POLICY_TABLE, IMAGE_LOAD_POLICY_TABLE_COUNT );
457 int width( image.GetWidth() );
458 int height( image.GetHeight() );
460 if ( width && height )
462 map[ "width" ] = width;
463 map[ "height" ] = height;
468 void NewAnimation( const Property::Map& map, Dali::AnimationData& outputAnimationData )
470 // Note: Builder cannot currently pass generic Property::Maps "{" that are nested, so currently we can only have one AnimateTo per animation.
471 Dali::AnimationData::AnimationDataElement* element = new Dali::AnimationData::AnimationDataElement();
472 element->alphaFunction = AlphaFunction::LINEAR;
473 element->timePeriodDelay = 0.0f;
474 element->timePeriodDuration = 1.0f;
476 // Now set the properties, or create children
477 for( unsigned int i = 0, animationMapCount = map.Count(); i < animationMapCount; ++i )
479 const StringValuePair& pair( map.GetPair( i ) );
480 const std::string& key( pair.first );
481 const Property::Value& value( pair.second );
485 element->actor = value.Get< std::string >();
487 else if( key == "property" )
489 element->property = value.Get< std::string >();
491 else if( key == "value" )
493 element->value = value;
495 else if( key == "alphaFunction" )
497 std::string alphaFunctionValue = value.Get< std::string >();
499 if( alphaFunctionValue == "LINEAR" )
501 element->alphaFunction = AlphaFunction::LINEAR;
503 else if( alphaFunctionValue == "REVERSE" )
505 element->alphaFunction = AlphaFunction::REVERSE;
507 else if( alphaFunctionValue == "EASE_IN_SQUARE" )
509 element->alphaFunction = AlphaFunction::EASE_IN_SQUARE;
511 else if( alphaFunctionValue == "EASE_OUT_SQUARE" )
513 element->alphaFunction = AlphaFunction::EASE_OUT_SQUARE;
515 else if( alphaFunctionValue == "EASE_IN" )
517 element->alphaFunction = AlphaFunction::EASE_IN;
519 else if( alphaFunctionValue == "EASE_OUT" )
521 element->alphaFunction = AlphaFunction::EASE_OUT;
523 else if( alphaFunctionValue == "EASE_IN_OUT" )
525 element->alphaFunction = AlphaFunction::EASE_IN_OUT;
527 else if( alphaFunctionValue == "EASE_IN_SINE" )
529 element->alphaFunction = AlphaFunction::EASE_IN_SINE;
531 else if( alphaFunctionValue == "EASE_OUT_SINE" )
533 element->alphaFunction = AlphaFunction::EASE_OUT_SINE;
535 else if( alphaFunctionValue == "EASE_IN_OUT_SINE" )
537 element->alphaFunction = AlphaFunction::EASE_IN_OUT_SINE;
539 else if( alphaFunctionValue == "BOUNCE" )
541 element->alphaFunction = AlphaFunction::BOUNCE;
543 else if( alphaFunctionValue == "SIN" )
545 element->alphaFunction = AlphaFunction::SIN;
547 else if( alphaFunctionValue == "EASE_OUT_BACK" )
549 element->alphaFunction = AlphaFunction::EASE_OUT_BACK;
552 else if( key == "timePeriod" )
554 Property::Map timeMap = value.Get< Property::Map >();
555 for( unsigned int i = 0; i < timeMap.Count(); ++i )
557 const StringValuePair& pair( timeMap.GetPair( i ) );
558 if( pair.first == "delay" )
560 element->timePeriodDelay = pair.second.Get< float >();
562 else if( pair.first == "duration" )
564 element->timePeriodDuration = pair.second.Get< float >();
570 outputAnimationData.Add( element );
573 } // namespace scripting