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 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 unsigned int PIXEL_FORMAT_TABLE_COUNT = 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 unsigned int IMAGE_FITTING_MODE_TABLE_COUNT = 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 unsigned int IMAGE_SAMPLING_MODE_TABLE_COUNT = 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 unsigned int imageTypeCount = sizeof( ImageTypeName ) / sizeof( const char* );
97 } // unnamed namespace
99 bool EnumStringToInteger( const char * const value, const StringEnum* const enumTable, unsigned int tableCount, int& integerEnum )
106 if( value && enumTable && tableCount )
108 const char* pValue = value;
114 const StringEnum* table = enumTable;
116 for ( unsigned int 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 unsigned int FindEnumIndex( const char* value, const StringEnum* table, unsigned int tableCount )
153 unsigned int index = 0;
155 for ( unsigned int i = 0; i < tableCount; ++i, ++index )
157 size_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( unsigned int 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 int 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<unsigned int>( value->Get<float>() );
233 value = map->Find( "height" );
236 if( value->GetType() == Property::FLOAT )
238 height = static_cast<int>( 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( attributes.GetSize().x, 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 ( unsigned int i = 0, mapCount = map.Count(); i < mapCount; ++i )
345 const StringValuePair& pair( map.GetPair( i ) );
346 const std::string& key( pair.first );
352 const Property::Value& value( pair.second );
354 if ( key == "actors" )
357 Property::Array actorArray = value.Get< Property::Array >();
358 for ( Property::Array::SizeType i = 0; i < actorArray.Size(); ++i)
360 actor.Add( NewActor( actorArray[i].Get< Property::Map >() ) );
365 Property::Index index( actor.GetPropertyIndex( key ) );
367 if ( index != Property::INVALID_INDEX )
369 actor.SetProperty( index, value );
378 void CreatePropertyMap( Actor actor, Property::Map& map )
384 map[ "type" ] = actor.GetTypeName();
386 // Default properties
387 Property::IndexContainer indices;
388 actor.GetPropertyIndices( indices );
389 const Property::IndexContainer::ConstIterator endIter = indices.End();
391 for ( Property::IndexContainer::Iterator iter = indices.Begin(); iter != endIter; ++iter )
393 map[ actor.GetPropertyName( *iter ) ] = actor.GetProperty( *iter );
397 unsigned int childCount( actor.GetChildCount() );
400 Property::Array childArray;
401 for ( unsigned int child = 0; child < childCount; ++child )
403 Property::Map childMap;
404 CreatePropertyMap( actor.GetChildAt( child ), childMap );
405 childArray.PushBack( childMap );
407 map[ "actors" ] = childArray;
412 void CreatePropertyMap( Image image, Property::Map& map )
418 std::string imageType( "ResourceImage" );
420 // Get Type - cannot use TypeRegistry as Image is not an Object and thus, not registered
421 BufferImage bufferImage = BufferImage::DownCast( image );
424 imageType = "BufferImage";
425 map[ "pixelFormat" ] = GetEnumerationName< Pixel::Format >( bufferImage.GetPixelFormat(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT );
427 else if ( FrameBufferImage::DownCast( image ) )
429 imageType = "FrameBufferImage";
432 map[ "type" ] = imageType;
434 ResourceImage resourceImage = ResourceImage::DownCast( image );
437 map[ "filename" ] = resourceImage.GetUrl();
440 int width( image.GetWidth() );
441 int height( image.GetHeight() );
443 if ( width && height )
445 map[ "width" ] = width;
446 map[ "height" ] = height;
451 void NewAnimation( const Property::Map& map, Dali::AnimationData& outputAnimationData )
453 // Note: Builder cannot currently pass generic Property::Maps "{" that are nested, so currently we can only have one AnimateTo per animation.
454 Dali::AnimationData::AnimationDataElement* element = new Dali::AnimationData::AnimationDataElement();
455 element->alphaFunction = AlphaFunction::LINEAR;
456 element->timePeriodDelay = 0.0f;
457 element->timePeriodDuration = 1.0f;
459 // Now set the properties, or create children
460 for( unsigned int i = 0, animationMapCount = map.Count(); i < animationMapCount; ++i )
462 const StringValuePair& pair( map.GetPair( i ) );
463 const std::string& key( pair.first );
464 const Property::Value& value( pair.second );
468 element->actor = value.Get< std::string >();
470 else if( key == "property" )
472 element->property = value.Get< std::string >();
474 else if( key == "value" )
476 element->value = value;
478 else if( key == "alphaFunction" )
480 std::string alphaFunctionValue = value.Get< std::string >();
482 if( alphaFunctionValue == "LINEAR" )
484 element->alphaFunction = AlphaFunction::LINEAR;
486 else if( alphaFunctionValue == "REVERSE" )
488 element->alphaFunction = AlphaFunction::REVERSE;
490 else if( alphaFunctionValue == "EASE_IN_SQUARE" )
492 element->alphaFunction = AlphaFunction::EASE_IN_SQUARE;
494 else if( alphaFunctionValue == "EASE_OUT_SQUARE" )
496 element->alphaFunction = AlphaFunction::EASE_OUT_SQUARE;
498 else if( alphaFunctionValue == "EASE_IN" )
500 element->alphaFunction = AlphaFunction::EASE_IN;
502 else if( alphaFunctionValue == "EASE_OUT" )
504 element->alphaFunction = AlphaFunction::EASE_OUT;
506 else if( alphaFunctionValue == "EASE_IN_OUT" )
508 element->alphaFunction = AlphaFunction::EASE_IN_OUT;
510 else if( alphaFunctionValue == "EASE_IN_SINE" )
512 element->alphaFunction = AlphaFunction::EASE_IN_SINE;
514 else if( alphaFunctionValue == "EASE_OUT_SINE" )
516 element->alphaFunction = AlphaFunction::EASE_OUT_SINE;
518 else if( alphaFunctionValue == "EASE_IN_OUT_SINE" )
520 element->alphaFunction = AlphaFunction::EASE_IN_OUT_SINE;
522 else if( alphaFunctionValue == "BOUNCE" )
524 element->alphaFunction = AlphaFunction::BOUNCE;
526 else if( alphaFunctionValue == "SIN" )
528 element->alphaFunction = AlphaFunction::SIN;
530 else if( alphaFunctionValue == "EASE_OUT_BACK" )
532 element->alphaFunction = AlphaFunction::EASE_OUT_BACK;
535 else if( key == "timePeriod" )
537 Property::Map timeMap = value.Get< Property::Map >();
538 for( unsigned int i = 0; i < timeMap.Count(); ++i )
540 const StringValuePair& pair( timeMap.GetPair( i ) );
541 if( pair.first == "delay" )
543 element->timePeriodDelay = pair.second.Get< float >();
545 else if( pair.first == "duration" )
547 element->timePeriodDuration = pair.second.Get< float >();
553 outputAnimationData.Add( element );
556 } // namespace scripting