2 * Copyright (c) 2015 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/images/resource-image-impl.h>
28 #include <dali/internal/event/images/frame-buffer-image-impl.h>
29 #include <dali/internal/event/images/buffer-image-impl.h>
30 #include <dali/internal/event/effects/shader-effect-impl.h>
41 // Tables used here for converting strings to the enumerations and vice versa
47 const AnchorValue ANCHOR_CONSTANT_TABLE[] =
49 { "TOP_LEFT", ParentOrigin::TOP_LEFT },
50 { "TOP_CENTER", ParentOrigin::TOP_CENTER },
51 { "TOP_RIGHT", ParentOrigin::TOP_RIGHT },
52 { "CENTER_LEFT", ParentOrigin::CENTER_LEFT },
53 { "CENTER", ParentOrigin::CENTER },
54 { "CENTER_RIGHT", ParentOrigin::CENTER_RIGHT },
55 { "BOTTOM_LEFT", ParentOrigin::BOTTOM_LEFT },
56 { "BOTTOM_CENTER", ParentOrigin::BOTTOM_CENTER },
57 { "BOTTOM_RIGHT", ParentOrigin::BOTTOM_RIGHT },
59 const unsigned int ANCHOR_CONSTANT_TABLE_COUNT = sizeof( ANCHOR_CONSTANT_TABLE ) / sizeof( ANCHOR_CONSTANT_TABLE[0] );
61 const StringEnum COLOR_MODE_TABLE[] =
63 { "USE_OWN_COLOR", USE_OWN_COLOR },
64 { "USE_PARENT_COLOR", USE_PARENT_COLOR },
65 { "USE_OWN_MULTIPLY_PARENT_COLOR", USE_OWN_MULTIPLY_PARENT_COLOR },
66 { "USE_OWN_MULTIPLY_PARENT_ALPHA", USE_OWN_MULTIPLY_PARENT_ALPHA },
68 const unsigned int COLOR_MODE_TABLE_COUNT = sizeof( COLOR_MODE_TABLE ) / sizeof( COLOR_MODE_TABLE[0] );
70 const StringEnum POSITION_INHERITANCE_MODE_TABLE[] =
72 { "INHERIT_PARENT_POSITION", INHERIT_PARENT_POSITION },
73 { "USE_PARENT_POSITION", USE_PARENT_POSITION },
74 { "USE_PARENT_POSITION_PLUS_LOCAL_POSITION", USE_PARENT_POSITION_PLUS_LOCAL_POSITION },
75 { "DONT_INHERIT_POSITION", DONT_INHERIT_POSITION },
77 const unsigned int POSITION_INHERITANCE_MODE_TABLE_COUNT = sizeof( POSITION_INHERITANCE_MODE_TABLE ) / sizeof( POSITION_INHERITANCE_MODE_TABLE[0] );
79 const StringEnum DRAW_MODE_TABLE[] =
81 { "NORMAL", DrawMode::NORMAL },
82 { "OVERLAY_2D", DrawMode::OVERLAY_2D },
83 { "STENCIL", DrawMode::STENCIL },
85 const unsigned int DRAW_MODE_TABLE_COUNT = sizeof( DRAW_MODE_TABLE ) / sizeof( DRAW_MODE_TABLE[0] );
87 const StringEnum IMAGE_LOAD_POLICY_TABLE[] =
89 { "IMMEDIATE", ResourceImage::IMMEDIATE },
90 { "ON_DEMAND", ResourceImage::ON_DEMAND },
92 const unsigned int IMAGE_LOAD_POLICY_TABLE_COUNT = sizeof( IMAGE_LOAD_POLICY_TABLE ) / sizeof( IMAGE_LOAD_POLICY_TABLE[0] );
94 const StringEnum IMAGE_RELEASE_POLICY_TABLE[] =
96 { "UNUSED", Image::UNUSED },
97 { "NEVER", Image::NEVER },
99 const unsigned int IMAGE_RELEASE_POLICY_TABLE_COUNT = sizeof( IMAGE_RELEASE_POLICY_TABLE ) / sizeof( IMAGE_RELEASE_POLICY_TABLE[0] );
101 const StringEnum PIXEL_FORMAT_TABLE[] =
105 { "LA88", Pixel::LA88 },
106 { "RGB565", Pixel::RGB565 },
107 { "BGR565", Pixel::BGR565 },
108 { "RGBA4444", Pixel::RGBA4444 },
109 { "BGRA4444", Pixel::BGRA4444 },
110 { "RGBA5551", Pixel::RGBA5551 },
111 { "BGRA5551", Pixel::BGRA5551 },
112 { "RGB888", Pixel::RGB888 },
113 { "RGB8888", Pixel::RGB8888 },
114 { "BGR8888", Pixel::BGR8888 },
115 { "RGBA8888", Pixel::RGBA8888 },
116 { "BGRA8888", Pixel::BGRA8888 },
117 { "COMPRESSED_R11_EAC", Pixel::COMPRESSED_R11_EAC },
118 { "COMPRESSED_SIGNED_R11_EAC", Pixel::COMPRESSED_SIGNED_R11_EAC },
119 { "COMPRESSED_SIGNED_RG11_EAC", Pixel::COMPRESSED_SIGNED_RG11_EAC },
120 { "COMPRESSED_RG11_EAC", Pixel::COMPRESSED_RG11_EAC },
121 { "COMPRESSED_RGB8_ETC2", Pixel::COMPRESSED_RGB8_ETC2 },
122 { "COMPRESSED_SRGB8_ETC2", Pixel::COMPRESSED_SRGB8_ETC2 },
123 { "COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2", Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 },
124 { "COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2", Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 },
125 { "COMPRESSED_RGBA8_ETC2_EAC", Pixel::COMPRESSED_RGBA8_ETC2_EAC },
126 { "COMPRESSED_SRGB8_ALPHA8_ETC2_EAC", Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC },
127 { "COMPRESSED_RGB8_ETC1", Pixel::COMPRESSED_RGB8_ETC1 },
128 { "COMPRESSED_RGB_PVRTC_4BPPV1", Pixel::COMPRESSED_RGB_PVRTC_4BPPV1 },
130 const unsigned int PIXEL_FORMAT_TABLE_COUNT = sizeof( PIXEL_FORMAT_TABLE ) / sizeof( PIXEL_FORMAT_TABLE[0] );
132 const StringEnum IMAGE_FITTING_MODE_TABLE[] =
134 { "SHRINK_TO_FIT", FittingMode::SHRINK_TO_FIT },
135 { "SCALE_TO_FILL", FittingMode::SCALE_TO_FILL },
136 { "FIT_WIDTH", FittingMode::FIT_WIDTH },
137 { "FIT_HEIGHT", FittingMode::FIT_HEIGHT },
139 const unsigned int IMAGE_FITTING_MODE_TABLE_COUNT = sizeof( IMAGE_FITTING_MODE_TABLE ) / sizeof( IMAGE_FITTING_MODE_TABLE[0] );
141 const StringEnum IMAGE_SAMPLING_MODE_TABLE[] =
143 { "BOX", SamplingMode::BOX },
144 { "NEAREST", SamplingMode::NEAREST },
145 { "LINEAR", SamplingMode::LINEAR },
146 { "BOX_THEN_NEAREST", SamplingMode::BOX_THEN_NEAREST },
147 { "BOX_THEN_LINEAR", SamplingMode::BOX_THEN_LINEAR },
148 { "NO_FILTER", SamplingMode::NO_FILTER },
149 { "DONT_CARE", SamplingMode::DONT_CARE },
151 const unsigned int IMAGE_SAMPLING_MODE_TABLE_COUNT = sizeof( IMAGE_SAMPLING_MODE_TABLE ) / sizeof( IMAGE_SAMPLING_MODE_TABLE[0] );
153 const char* ImageTypeName[] = { "ResourceImage", "FrameBufferImage", "BufferImage" };
154 enum ImageType { RESOURCE_IMAGE, FRAME_BUFFER_IMAGE, BUFFER_IMAGE };
155 const unsigned int imageTypeCount = sizeof( ImageTypeName ) / sizeof( const char* );
157 bool CompareEnums( const char * a, const char * b )
159 while( ( *a != '\0' ) && ( *b != '\0' ) )
164 if( ( ( ca == '-' ) || ( ca == '_') ) &&
165 ( ( cb == '-' ) || ( cb == '_') ) )
172 if( ( 'A' <= ca ) && ( ca <= 'Z') )
174 ca = ca + ( 'a' - 'A' );
177 if( ( 'A' <= cb ) && ( cb <= 'Z') )
179 cb = cb + ( 'a' - 'A' );
191 if( ( *a == '\0' ) && ( *b == '\0' ) )
199 } // unnamed namespace
201 unsigned int FindEnumIndex( const char* value, const StringEnum* table, unsigned int tableCount )
203 unsigned int index = 0;
205 for ( unsigned int i = 0; i < tableCount; ++i, ++index )
207 if( CompareEnums( value, table->string ) )
216 DALI_LOG_ERROR( "Unknown enumeration string %s\n", value );
221 ColorMode GetColorMode( const std::string& value )
223 // return default on error
224 ColorMode mode( USE_OWN_MULTIPLY_PARENT_ALPHA );
225 GetEnumeration< ColorMode >( value.c_str(), COLOR_MODE_TABLE, COLOR_MODE_TABLE_COUNT, mode );
230 std::string GetColorMode( ColorMode value )
232 const char* name = GetEnumerationName< ColorMode >( value, COLOR_MODE_TABLE, COLOR_MODE_TABLE_COUNT );
235 return std::string( name );
237 return std::string();
240 PositionInheritanceMode GetPositionInheritanceMode( const std::string& value )
242 // return default on error
243 PositionInheritanceMode mode( INHERIT_PARENT_POSITION );
244 GetEnumeration< PositionInheritanceMode >( value.c_str(), POSITION_INHERITANCE_MODE_TABLE, POSITION_INHERITANCE_MODE_TABLE_COUNT, mode );
249 std::string GetPositionInheritanceMode( PositionInheritanceMode value )
251 const char* name = GetEnumerationName< PositionInheritanceMode >( value, POSITION_INHERITANCE_MODE_TABLE, POSITION_INHERITANCE_MODE_TABLE_COUNT );
254 return std::string( name );
256 return std::string();
260 DrawMode::Type GetDrawMode( const std::string& value )
262 // return default on error
263 DrawMode::Type mode( DrawMode::NORMAL );
264 GetEnumeration< DrawMode::Type >( value.c_str(), DRAW_MODE_TABLE, DRAW_MODE_TABLE_COUNT, mode );
269 std::string GetDrawMode( DrawMode::Type value )
271 const char* name = GetEnumerationName< DrawMode::Type >( value, DRAW_MODE_TABLE, DRAW_MODE_TABLE_COUNT );
274 return std::string( name );
276 return std::string();
280 Vector3 GetAnchorConstant( const std::string& value )
282 for( unsigned int i = 0; i < ANCHOR_CONSTANT_TABLE_COUNT; ++i )
284 if( CompareEnums( value.c_str(), ANCHOR_CONSTANT_TABLE[ i ].name ) )
286 return ANCHOR_CONSTANT_TABLE[ i ].value;
293 Image NewImage( const Property::Value& property )
297 std::string filename;
298 ResourceImage::LoadPolicy loadPolicy = Dali::Internal::IMAGE_LOAD_POLICY_DEFAULT;
299 Image::ReleasePolicy releasePolicy = Dali::Internal::IMAGE_RELEASE_POLICY_DEFAULT;
300 Internal::ImageAttributes attributes = Internal::ImageAttributes::New();
302 const Property::Map* map = property.GetMap();
303 ImageType imageType = RESOURCE_IMAGE; // default to resource image
306 // first check the type as it determines, which other parameters are needed
307 const Property::Value* value = map->Find( "type" );
312 for( unsigned int i = 0; i < imageTypeCount; ++i )
314 if( 0 == type.compare( ImageTypeName[ i ] ) )
316 imageType = static_cast<ImageType>( i );
322 // filename is only needed for resource images
323 if( RESOURCE_IMAGE == imageType )
325 const Property::Value* value = map->Find( "filename" );
328 value->Get( filename );
330 // if empty file, no need to go further
331 if( filename.size() == 0 )
333 DALI_LOG_ERROR( "No filename\n" );
338 value = map->Find( "load-policy" );
342 value->Get( policy );
343 // keep default value on error
344 GetEnumeration< ResourceImage::LoadPolicy >( policy.c_str(), IMAGE_LOAD_POLICY_TABLE, IMAGE_LOAD_POLICY_TABLE_COUNT, loadPolicy );
347 value = map->Find( "release-policy" );
351 value->Get( policy );
352 // keep default value on error
353 GetEnumeration< Image::ReleasePolicy >( policy.c_str(), IMAGE_RELEASE_POLICY_TABLE, IMAGE_RELEASE_POLICY_TABLE_COUNT, releasePolicy );
356 // Width and height can be set individually. Dali derives the unspecified
357 // dimension from the aspect ratio of the raw image.
358 int width = 0, height = 0;
360 value = map->Find( "width" );
363 // handle floats and integer the same for json script
364 if( value->GetType() == Property::FLOAT )
366 width = static_cast<unsigned int>( value->Get<float>() );
373 value = map->Find( "height" );
376 if( value->GetType() == Property::FLOAT )
378 height = static_cast<int>( value->Get<float>() );
382 value->Get( height );
385 attributes.SetSize( width, height );
387 Pixel::Format pixelFormat = Pixel::RGBA8888;
388 value = map->Find( "pixel-format" );
392 value->Get( format );
393 GetEnumeration< Pixel::Format >( format.c_str(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT, pixelFormat );
396 value = map->Find( "fitting-mode" );
400 value->Get( fitting );
401 FittingMode::Type mode;
402 if( GetEnumeration< FittingMode::Type >( fitting.c_str(), IMAGE_FITTING_MODE_TABLE, IMAGE_FITTING_MODE_TABLE_COUNT, mode ) )
404 attributes.SetScalingMode( mode );
408 value = map->Find( "sampling-mode" );
411 std::string sampling;
412 value->Get( sampling );
413 SamplingMode::Type mode;
414 if( GetEnumeration< SamplingMode::Type >( sampling.c_str(), IMAGE_SAMPLING_MODE_TABLE, IMAGE_SAMPLING_MODE_TABLE_COUNT, mode ) )
416 attributes.SetFilterMode( mode );
420 value = map->Find( "orientation" );
423 bool b = value->Get<bool>();
424 attributes.SetOrientationCorrection( b );
429 case RESOURCE_IMAGE :
431 ret = ResourceImage::New( filename, loadPolicy, releasePolicy,
432 ImageDimensions( attributes.GetSize().x, attributes.GetSize().y ),
433 attributes.GetScalingMode(), attributes.GetFilterMode(), attributes.GetOrientationCorrection() );
438 ret = BufferImage::New( attributes.GetWidth(),
439 attributes.GetHeight(),
444 case FRAME_BUFFER_IMAGE :
446 ret = FrameBufferImage::New( attributes.GetWidth(),
447 attributes.GetHeight(),
457 } // Image NewImage( Property::Value map )
460 ShaderEffect NewShaderEffect( const Property::Value& property )
462 Internal::ShaderEffectPtr ret;
464 const Property::Map* map = property.GetMap();
467 ret = Internal::ShaderEffect::New( Dali::ShaderEffect::HINT_NONE ); // hint can be reset if in map
469 const Property::Value* value = map->Find( "program" );
472 Property::Index index = ret->GetPropertyIndex( "program" );
473 ret->SetProperty( index, *value );
476 for( unsigned int i = 0; i < map->Count(); ++i )
478 const std::string& key = map->GetKey( i );
479 if( key != "program" )
481 Property::Index index = ret->GetPropertyIndex( key );
483 const Property::Value& value = map->GetValue( i );
484 if( Property::INVALID_INDEX != index )
486 ret->SetProperty( index, value );
490 // if its not a property then register it as a uniform (making a custom property)
491 if( value.GetType() == Property::INTEGER )
493 // valid uniforms are floats, vec3's etc so we recast if the user accidentally
494 // set as integer. Note the map could have come from json script.
495 Property::Value asFloat( static_cast<float>( value.Get<int>() ) );
496 ret->SetUniform( key, asFloat, Dali::ShaderEffect::COORDINATE_TYPE_DEFAULT );
500 ret->SetUniform( key, value, Dali::ShaderEffect::COORDINATE_TYPE_DEFAULT );
507 return Dali::ShaderEffect(ret.Get());
511 Actor NewActor( const Property::Map& map )
515 // First find type and create Actor
516 Property::Value* typeValue = map.Find( "type" );
519 TypeInfo type = TypeRegistry::Get().GetTypeInfo( typeValue->Get< std::string >() );
522 handle = type.CreateInstance();
528 DALI_LOG_ERROR( "Actor type not provided\n" );
532 Actor actor( Actor::DownCast( handle ) );
536 // Now set the properties, or create children
537 for ( unsigned int i = 0, mapCount = map.Count(); i < mapCount; ++i )
539 const StringValuePair& pair( map.GetPair( i ) );
540 const std::string& key( pair.first );
546 const Property::Value& value( pair.second );
548 if ( key == "actors" )
551 Property::Array actorArray = value.Get< Property::Array >();
552 for ( Property::Array::SizeType i = 0; i < actorArray.Size(); ++i)
554 actor.Add( NewActor( actorArray[i].Get< Property::Map >() ) );
557 else if( key == "parent-origin" )
559 // Parent Origin can be a string constant as well as a Vector3
561 const Property::Type type( value.GetType() );
562 if ( type == Property::VECTOR3 )
564 actor.SetParentOrigin( value.Get< Vector3 >() );
566 else if( type == Property::STRING )
568 actor.SetParentOrigin( GetAnchorConstant( value.Get< std::string >() ) );
571 else if( key == "anchor-point" )
573 // Anchor Point can be a string constant as well as a Vector3
575 const Property::Type type( value.GetType() );
576 if ( type == Property::VECTOR3 )
578 actor.SetAnchorPoint( value.Get< Vector3 >() );
580 else if( type == Property::STRING )
582 actor.SetAnchorPoint( GetAnchorConstant( value.Get< std::string >() ) );
587 Property::Index index( actor.GetPropertyIndex( key ) );
589 if ( index != Property::INVALID_INDEX )
591 actor.SetProperty( index, value );
600 void CreatePropertyMap( Actor actor, Property::Map& map )
606 map[ "type" ] = actor.GetTypeName();
608 // Default properties
609 Property::IndexContainer indices;
610 actor.GetPropertyIndices( indices );
611 const Property::IndexContainer::ConstIterator endIter = indices.End();
613 for ( Property::IndexContainer::Iterator iter = indices.Begin(); iter != endIter; ++iter )
615 map[ actor.GetPropertyName( *iter ) ] = actor.GetProperty( *iter );
619 unsigned int childCount( actor.GetChildCount() );
622 Property::Array childArray;
623 for ( unsigned int child = 0; child < childCount; ++child )
625 Property::Map childMap;
626 CreatePropertyMap( actor.GetChildAt( child ), childMap );
627 childArray.PushBack( childMap );
629 map[ "actors" ] = childArray;
634 void CreatePropertyMap( Image image, Property::Map& map )
640 std::string imageType( "ResourceImage" );
642 // Get Type - cannot use TypeRegistry as Image is not an Object and thus, not registered
643 BufferImage bufferImage = BufferImage::DownCast( image );
646 imageType = "BufferImage";
647 map[ "pixel-format" ] = GetEnumerationName< Pixel::Format >( bufferImage.GetPixelFormat(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT );
649 else if ( FrameBufferImage::DownCast( image ) )
651 imageType = "FrameBufferImage";
654 map[ "type" ] = imageType;
655 map[ "release-policy" ] = GetEnumerationName< Image::ReleasePolicy >( image.GetReleasePolicy(), IMAGE_RELEASE_POLICY_TABLE, IMAGE_RELEASE_POLICY_TABLE_COUNT );
657 ResourceImage resourceImage = ResourceImage::DownCast( image );
660 map[ "filename" ] = resourceImage.GetUrl();
661 map[ "load-policy" ] = GetEnumerationName< ResourceImage::LoadPolicy >( resourceImage.GetLoadPolicy(), IMAGE_LOAD_POLICY_TABLE, IMAGE_LOAD_POLICY_TABLE_COUNT );
664 int width( image.GetWidth() );
665 int height( image.GetHeight() );
667 if ( width && height )
669 map[ "width" ] = width;
670 map[ "height" ] = height;
675 } // namespace scripting