2 * Copyright (c) 2014 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/public-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/images/image-attributes.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/internal/event/images/resource-image-impl.h>
27 #include <dali/internal/event/images/frame-buffer-image-impl.h>
28 #include <dali/internal/event/images/buffer-image-impl.h>
29 #include <dali/internal/event/effects/shader-effect-impl.h>
40 // Tables used here for converting strings to the enumerations and vice versa
42 const StringEnum< ColorMode > COLOR_MODE_TABLE[] =
44 { "USE_OWN_COLOR", USE_OWN_COLOR },
45 { "USE_PARENT_COLOR", USE_PARENT_COLOR },
46 { "USE_OWN_MULTIPLY_PARENT_COLOR", USE_OWN_MULTIPLY_PARENT_COLOR },
47 { "USE_OWN_MULTIPLY_PARENT_ALPHA", USE_OWN_MULTIPLY_PARENT_ALPHA },
49 const unsigned int COLOR_MODE_TABLE_COUNT = sizeof( COLOR_MODE_TABLE ) / sizeof( COLOR_MODE_TABLE[0] );
51 const StringEnum< PositionInheritanceMode > POSITION_INHERITANCE_MODE_TABLE[] =
53 { "INHERIT_PARENT_POSITION", INHERIT_PARENT_POSITION },
54 { "USE_PARENT_POSITION", USE_PARENT_POSITION },
55 { "USE_PARENT_POSITION_PLUS_LOCAL_POSITION", USE_PARENT_POSITION_PLUS_LOCAL_POSITION },
56 { "DONT_INHERIT_POSITION", DONT_INHERIT_POSITION },
58 const unsigned int POSITION_INHERITANCE_MODE_TABLE_COUNT = sizeof( POSITION_INHERITANCE_MODE_TABLE ) / sizeof( POSITION_INHERITANCE_MODE_TABLE[0] );
60 const StringEnum< DrawMode::Type > DRAW_MODE_TABLE[] =
62 { "NORMAL", DrawMode::NORMAL },
63 { "OVERLAY", DrawMode::OVERLAY },
64 { "STENCIL", DrawMode::STENCIL },
66 const unsigned int DRAW_MODE_TABLE_COUNT = sizeof( DRAW_MODE_TABLE ) / sizeof( DRAW_MODE_TABLE[0] );
68 const StringEnum< Vector3 > ANCHOR_CONSTANT_TABLE[] =
70 { "TOP_LEFT", ParentOrigin::TOP_LEFT },
71 { "TOP_CENTER", ParentOrigin::TOP_CENTER },
72 { "TOP_RIGHT", ParentOrigin::TOP_RIGHT },
73 { "CENTER_LEFT", ParentOrigin::CENTER_LEFT },
74 { "CENTER", ParentOrigin::CENTER },
75 { "CENTER_RIGHT", ParentOrigin::CENTER_RIGHT },
76 { "BOTTOM_LEFT", ParentOrigin::BOTTOM_LEFT },
77 { "BOTTOM_CENTER", ParentOrigin::BOTTOM_CENTER },
78 { "BOTTOM_RIGHT", ParentOrigin::BOTTOM_RIGHT },
80 const unsigned int ANCHOR_CONSTANT_TABLE_COUNT = sizeof( ANCHOR_CONSTANT_TABLE ) / sizeof( ANCHOR_CONSTANT_TABLE[0] );
82 const StringEnum< ResourceImage::LoadPolicy > IMAGE_LOAD_POLICY_TABLE[] =
84 { "IMMEDIATE", ResourceImage::IMMEDIATE },
85 { "ON_DEMAND", ResourceImage::ON_DEMAND },
87 const unsigned int IMAGE_LOAD_POLICY_TABLE_COUNT = sizeof( IMAGE_LOAD_POLICY_TABLE ) / sizeof( IMAGE_LOAD_POLICY_TABLE[0] );
89 const StringEnum< Image::ReleasePolicy > IMAGE_RELEASE_POLICY_TABLE[] =
91 { "UNUSED", Image::UNUSED },
92 { "NEVER", Image::NEVER },
94 const unsigned int IMAGE_RELEASE_POLICY_TABLE_COUNT = sizeof( IMAGE_RELEASE_POLICY_TABLE ) / sizeof( IMAGE_RELEASE_POLICY_TABLE[0] );
96 const StringEnum< Pixel::Format > PIXEL_FORMAT_TABLE[] =
100 { "LA88", Pixel::LA88 },
101 { "RGB565", Pixel::RGB565 },
102 { "BGR565", Pixel::BGR565 },
103 { "RGBA4444", Pixel::RGBA4444 },
104 { "BGRA4444", Pixel::BGRA4444 },
105 { "RGBA5551", Pixel::RGBA5551 },
106 { "BGRA5551", Pixel::BGRA5551 },
107 { "RGB888", Pixel::RGB888 },
108 { "RGB8888", Pixel::RGB8888 },
109 { "BGR8888", Pixel::BGR8888 },
110 { "RGBA8888", Pixel::RGBA8888 },
111 { "BGRA8888", Pixel::BGRA8888 },
112 { "COMPRESSED_R11_EAC", Pixel::COMPRESSED_R11_EAC },
113 { "COMPRESSED_SIGNED_R11_EAC", Pixel::COMPRESSED_SIGNED_R11_EAC },
114 { "COMPRESSED_RG11_EAC", Pixel::COMPRESSED_RG11_EAC },
115 { "COMPRESSED_SIGNED_RG11_EAC", Pixel::COMPRESSED_SIGNED_RG11_EAC },
116 { "COMPRESSED_RGB8_ETC2", Pixel::COMPRESSED_RGB8_ETC2 },
117 { "COMPRESSED_SRGB8_ETC2", Pixel::COMPRESSED_SRGB8_ETC2 },
118 { "COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2", Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 },
119 { "COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2", Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 },
120 { "COMPRESSED_RGBA8_ETC2_EAC", Pixel::COMPRESSED_RGBA8_ETC2_EAC },
121 { "COMPRESSED_SRGB8_ALPHA8_ETC2_EAC", Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC },
122 { "COMPRESSED_RGB8_ETC1", Pixel::COMPRESSED_RGB8_ETC1 },
123 { "COMPRESSED_RGB_PVRTC_4BPPV1", Pixel::COMPRESSED_RGB_PVRTC_4BPPV1 },
125 const unsigned int PIXEL_FORMAT_TABLE_COUNT = sizeof( PIXEL_FORMAT_TABLE ) / sizeof( PIXEL_FORMAT_TABLE[0] );
127 const StringEnum< ImageAttributes::ScalingMode > IMAGE_SCALING_MODE_TABLE[] =
129 { "SHRINK_TO_FIT", ImageAttributes::ShrinkToFit },
130 { "SCALE_TO_FILL", ImageAttributes::ScaleToFill },
131 { "FIT_WIDTH", ImageAttributes::FitWidth },
132 { "FIT_HEIGHT", ImageAttributes::FitHeight },
134 const unsigned int IMAGE_SCALING_MODE_TABLE_COUNT = sizeof( IMAGE_SCALING_MODE_TABLE ) / sizeof( IMAGE_SCALING_MODE_TABLE[0] );
136 } // unnamed namespace
138 bool CompareEnums( const char * a, const char * b )
140 while( ( *a != '\0' ) && ( *b != '\0' ) )
145 if( ( ( ca == '-' ) || ( ca == '_') ) &&
146 ( ( cb == '-' ) || ( cb == '_') ) )
153 if( ( 'A' <= ca ) && ( ca <= 'Z') )
155 ca = ca + ( 'a' - 'A' );
158 if( ( 'A' <= cb ) && ( cb <= 'Z') )
160 cb = cb + ( 'a' - 'A' );
172 if( ( *a == '\0' ) && ( *b == '\0' ) )
181 ColorMode GetColorMode( const std::string& value )
183 return GetEnumeration< ColorMode >( value.c_str(), COLOR_MODE_TABLE, COLOR_MODE_TABLE_COUNT );
187 std::string GetColorMode( ColorMode value )
189 return GetEnumerationName< ColorMode >( value, COLOR_MODE_TABLE, COLOR_MODE_TABLE_COUNT );
192 PositionInheritanceMode GetPositionInheritanceMode( const std::string& value )
194 return GetEnumeration< PositionInheritanceMode >( value.c_str(), POSITION_INHERITANCE_MODE_TABLE, POSITION_INHERITANCE_MODE_TABLE_COUNT );
198 std::string GetPositionInheritanceMode( PositionInheritanceMode value )
200 return GetEnumerationName< PositionInheritanceMode >( value, POSITION_INHERITANCE_MODE_TABLE, POSITION_INHERITANCE_MODE_TABLE_COUNT );
204 DrawMode::Type GetDrawMode( const std::string& value )
206 return GetEnumeration< DrawMode::Type >( value.c_str(), DRAW_MODE_TABLE, DRAW_MODE_TABLE_COUNT );
210 std::string GetDrawMode( DrawMode::Type value )
212 return GetEnumerationName< DrawMode::Type >( value, DRAW_MODE_TABLE, DRAW_MODE_TABLE_COUNT );
216 Vector3 GetAnchorConstant( const std::string& value )
218 return GetEnumeration< Vector3 >( value.c_str(), ANCHOR_CONSTANT_TABLE, ANCHOR_CONSTANT_TABLE_COUNT );
222 Image NewImage( const Property::Value& map )
226 std::string filename;
227 ResourceImage::LoadPolicy loadPolicy = Dali::Internal::IMAGE_LOAD_POLICY_DEFAULT;
228 Image::ReleasePolicy releasePolicy = Dali::Internal::IMAGE_RELEASE_POLICY_DEFAULT;
229 ImageAttributes attributes = ImageAttributes::New();
231 if( Property::MAP == map.GetType() )
233 std::string field = "filename";
234 if( map.HasKey(field) )
236 DALI_ASSERT_ALWAYS(map.GetValue(field).GetType() == Property::STRING && "Image filename property is not a string" );
237 filename = map.GetValue(field).Get<std::string>();
240 field = "load-policy";
241 if( map.HasKey(field) )
243 DALI_ASSERT_ALWAYS(map.GetValue(field).GetType() == Property::STRING && "Image load-policy property is not a string" );
244 std::string v(map.GetValue(field).Get<std::string>());
245 loadPolicy = GetEnumeration< ResourceImage::LoadPolicy >( v.c_str(), IMAGE_LOAD_POLICY_TABLE, IMAGE_LOAD_POLICY_TABLE_COUNT );
248 field = "release-policy";
249 if( map.HasKey(field) )
251 DALI_ASSERT_ALWAYS(map.GetValue(field).GetType() == Property::STRING && "Image release-policy property is not a string" );
252 std::string v(map.GetValue(field).Get<std::string>());
253 releasePolicy = GetEnumeration< Image::ReleasePolicy >( v.c_str(), IMAGE_RELEASE_POLICY_TABLE, IMAGE_RELEASE_POLICY_TABLE_COUNT );
256 if( map.HasKey("width") && map.HasKey("height") )
258 Property::Value &value = map.GetValue("width");
259 unsigned int w = 0, h = 0;
260 // handle floats and integer the same for json script
261 if( value.GetType() == Property::FLOAT)
263 w = static_cast<unsigned int>( value.Get<float>() );
267 DALI_ASSERT_ALWAYS(value.GetType() == Property::INTEGER && "Image width property is not a number" );
268 w = value.Get<int>();
271 value = map.GetValue("height");
272 if( value.GetType() == Property::FLOAT)
274 h = static_cast<unsigned int>( value.Get<float>() );
278 DALI_ASSERT_ALWAYS(value.GetType() == Property::INTEGER && "Image width property is not a number" );
279 h = value.Get<int>();
282 attributes.SetSize( w, h );
285 field = "pixel-format";
286 Pixel::Format pixelFormat = Pixel::RGBA8888;
287 if( map.HasKey(field) )
289 DALI_ASSERT_ALWAYS(map.GetValue(field).GetType() == Property::STRING && "Image release-policy property is not a string" );
290 std::string s(map.GetValue(field).Get<std::string>());
291 pixelFormat = GetEnumeration< Pixel::Format >( s.c_str(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT );
294 field = "scaling-mode";
295 if( map.HasKey(field) )
297 DALI_ASSERT_ALWAYS(map.GetValue(field).GetType() == Property::STRING && "Image release-policy property is not a string" );
298 std::string s(map.GetValue(field).Get<std::string>());
299 attributes.SetScalingMode( GetEnumeration< ImageAttributes::ScalingMode >( s.c_str(), IMAGE_SCALING_MODE_TABLE, IMAGE_SCALING_MODE_TABLE_COUNT ) );
302 if( map.HasKey("type") )
304 DALI_ASSERT_ALWAYS( map.GetValue("type").GetType() == Property::STRING );
305 std::string s(map.GetValue("type").Get<std::string>());
306 if("FrameBufferImage" == s)
308 ret = FrameBufferImage::New(attributes.GetWidth(),
309 attributes.GetHeight(),
313 else if("BufferImage" == s)
315 ret = BufferImage::New(attributes.GetWidth(),
316 attributes.GetHeight(),
320 else if("ResourceImage" == s)
322 ret = ResourceImage::New(filename, attributes, loadPolicy, releasePolicy);
326 DALI_ASSERT_ALWAYS( !"Unknown image type" );
331 ret = ResourceImage::New(filename, attributes, loadPolicy, releasePolicy);
337 } // Image NewImage( Property::Value map )
340 ShaderEffect NewShaderEffect( const Property::Value& map )
342 Internal::ShaderEffectPtr ret;
344 if( map.GetType() == Property::MAP )
346 ret = Internal::ShaderEffect::New(Dali::ShaderEffect::HINT_NONE); // hint can be reset if in map
348 if( map.HasKey("program") )
350 Property::Index index = ret->GetPropertyIndex("program");
351 DALI_ASSERT_DEBUG( map.GetValue("program").GetType() == Property::MAP );
352 ret->SetProperty(index, map.GetValue("program"));
355 for(int i = 0; i < map.GetSize(); ++i)
357 const std::string& key = map.GetKey(i);
360 Property::Index index = ret->GetPropertyIndex( key );
362 if( Property::INVALID_INDEX != index )
364 ret->SetProperty(index, map.GetItem(i));
368 // if its not a property then register it as a uniform (making a custom property)
370 if(map.GetItem(i).GetType() == Property::INTEGER)
372 // valid uniforms are floats, vec3's etc so we recast if the user accidentally
373 // set as integer. Note the map could have come from json script.
374 Property::Value asFloat( static_cast<float>( map.GetItem(i).Get<int>() ) );
375 ret->SetUniform( key, asFloat, Dali::ShaderEffect::COORDINATE_TYPE_DEFAULT );
379 ret->SetUniform( key, map.GetItem(i), Dali::ShaderEffect::COORDINATE_TYPE_DEFAULT );
386 return Dali::ShaderEffect(ret.Get());
390 Actor NewActor( const Property::Map& map )
394 // First find type and create Actor
395 Property::Value* typeValue = map.Find( "type" );
398 TypeInfo type = TypeRegistry::Get().GetTypeInfo( typeValue->Get< std::string >() );
401 handle = type.CreateInstance();
407 DALI_LOG_ERROR( "Actor type not provided, returning empty handle" );
411 Actor actor( Actor::DownCast( handle ) );
415 // Now set the properties, or create children
416 for ( unsigned int i = 0, mapCount = map.Count(); i < mapCount; ++i )
418 const StringValuePair& pair( map.GetPair( i ) );
419 const std::string& key( pair.first );
425 const Property::Value& value( pair.second );
427 if ( key == "actors" )
431 Property::Array actorArray = value.Get< Property::Array >();
432 for ( Property::Array::iterator arrayIter = actorArray.begin(), arrayEndIter = actorArray.end(); arrayIter != arrayEndIter; ++arrayIter )
434 actor.Add( NewActor( arrayIter->Get< Property::Map >() ) );
437 else if ( key == "signals" )
439 DALI_LOG_ERROR( "signals not supported" );
441 else if( key == "parent-origin" )
443 // Parent Origin can be a string constant as well as a Vector3
445 const Property::Type type( value.GetType() );
446 if ( type == Property::VECTOR3 )
448 actor.SetParentOrigin( value.Get< Vector3 >() );
450 else if( type == Property::STRING )
452 actor.SetParentOrigin( GetAnchorConstant( value.Get< std::string >() ) );
455 else if( key == "anchor-point" )
457 // Anchor Point can be a string constant as well as a Vector3
459 const Property::Type type( value.GetType() );
460 if ( type == Property::VECTOR3 )
462 actor.SetAnchorPoint( value.Get< Vector3 >() );
464 else if( type == Property::STRING )
466 actor.SetAnchorPoint( GetAnchorConstant( value.Get< std::string >() ) );
471 Property::Index index( actor.GetPropertyIndex( key ) );
473 if ( index != Property::INVALID_INDEX )
475 actor.SetProperty( index, value );
484 void CreatePropertyMap( Actor actor, Property::Map& map )
490 map[ "type" ] = actor.GetTypeName();
492 // Default properties
493 Property::IndexContainer indices;
494 actor.GetPropertyIndices( indices );
495 const Property::IndexContainer::const_iterator endIter = indices.end();
496 for ( Property::IndexContainer::iterator iter = indices.begin(); iter != endIter; ++iter )
498 map[ actor.GetPropertyName( *iter ) ] = actor.GetProperty( *iter );
502 unsigned int childCount( actor.GetChildCount() );
505 Property::Array childArray;
506 for ( unsigned int child = 0; child < childCount; ++child )
508 Property::Map childMap;
509 CreatePropertyMap( actor.GetChildAt( child ), childMap );
510 childArray.push_back( childMap );
512 map[ "actors" ] = childArray;
517 void CreatePropertyMap( Image image, Property::Map& map )
523 std::string imageType( "ResourceImage" );
525 // Get Type - cannot use TypeRegistry as Image is not an Object and thus, not registered
526 BufferImage bufferImage = BufferImage::DownCast( image );
529 imageType = "BufferImage";
530 map[ "pixel-format" ] = GetEnumerationName< Pixel::Format >( bufferImage.GetPixelFormat(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT );
532 else if ( FrameBufferImage::DownCast( image ) )
534 imageType = "FrameBufferImage";
537 map[ "type" ] = imageType;
538 map[ "release-policy" ] = GetEnumerationName< Image::ReleasePolicy >( image.GetReleasePolicy(), IMAGE_RELEASE_POLICY_TABLE, IMAGE_RELEASE_POLICY_TABLE_COUNT );
540 ResourceImage resourceImage = ResourceImage::DownCast( image );
543 map[ "filename" ] = resourceImage.GetUrl();
544 map[ "load-policy" ] = GetEnumerationName< ResourceImage::LoadPolicy >( resourceImage.GetLoadPolicy(), IMAGE_LOAD_POLICY_TABLE, IMAGE_LOAD_POLICY_TABLE_COUNT );
546 ImageAttributes attributes( resourceImage.GetAttributes() );
547 map[ "scaling-mode" ] = GetEnumerationName< ImageAttributes::ScalingMode >( attributes.GetScalingMode(), IMAGE_SCALING_MODE_TABLE, IMAGE_SCALING_MODE_TABLE_COUNT );
550 int width( image.GetWidth() );
551 int height( image.GetHeight() );
553 if ( width && height )
555 map[ "width" ] = width;
556 map[ "height" ] = height;
561 } // namespace scripting