Remove unused constants, don't export constant that have fixed values
[platform/core/uifw/dali-core.git] / dali / public-api / scripting / scripting.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/public-api/scripting/scripting.h>
20
21 // INTERNAL INCLUDES
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>
30
31 namespace Dali
32 {
33
34 namespace Scripting
35 {
36
37 namespace
38 {
39
40 // Tables used here for converting strings to the enumerations and vice versa
41
42 const StringEnum< ColorMode > COLOR_MODE_TABLE[] =
43 {
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    },
48 };
49 const unsigned int COLOR_MODE_TABLE_COUNT = sizeof( COLOR_MODE_TABLE ) / sizeof( COLOR_MODE_TABLE[0] );
50
51 const StringEnum< PositionInheritanceMode > POSITION_INHERITANCE_MODE_TABLE[] =
52 {
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                      },
57 };
58 const unsigned int POSITION_INHERITANCE_MODE_TABLE_COUNT = sizeof( POSITION_INHERITANCE_MODE_TABLE ) / sizeof( POSITION_INHERITANCE_MODE_TABLE[0] );
59
60 const StringEnum< DrawMode::Type > DRAW_MODE_TABLE[] =
61 {
62   { "NORMAL",     DrawMode::NORMAL     },
63   { "OVERLAY",    DrawMode::OVERLAY    },
64   { "STENCIL",    DrawMode::STENCIL    },
65 };
66 const unsigned int DRAW_MODE_TABLE_COUNT = sizeof( DRAW_MODE_TABLE ) / sizeof( DRAW_MODE_TABLE[0] );
67
68 const StringEnum< Vector3 > ANCHOR_CONSTANT_TABLE[] =
69 {
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           },
79 };
80 const unsigned int ANCHOR_CONSTANT_TABLE_COUNT = sizeof( ANCHOR_CONSTANT_TABLE ) / sizeof( ANCHOR_CONSTANT_TABLE[0] );
81
82 const StringEnum< ResourceImage::LoadPolicy > IMAGE_LOAD_POLICY_TABLE[] =
83 {
84   { "IMMEDIATE", ResourceImage::IMMEDIATE },
85   { "ON_DEMAND", ResourceImage::ON_DEMAND },
86 };
87 const unsigned int IMAGE_LOAD_POLICY_TABLE_COUNT = sizeof( IMAGE_LOAD_POLICY_TABLE ) / sizeof( IMAGE_LOAD_POLICY_TABLE[0] );
88
89 const StringEnum< Image::ReleasePolicy > IMAGE_RELEASE_POLICY_TABLE[] =
90 {
91   { "UNUSED", Image::UNUSED },
92   { "NEVER",  Image::NEVER  },
93 };
94 const unsigned int IMAGE_RELEASE_POLICY_TABLE_COUNT = sizeof( IMAGE_RELEASE_POLICY_TABLE ) / sizeof( IMAGE_RELEASE_POLICY_TABLE[0] );
95
96 const StringEnum< Pixel::Format > PIXEL_FORMAT_TABLE[] =
97 {
98   { "A8",                                           Pixel::A8                                           },
99   { "L8",                                           Pixel::L8                                           },
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                  },
124 };
125 const unsigned int PIXEL_FORMAT_TABLE_COUNT = sizeof( PIXEL_FORMAT_TABLE ) / sizeof( PIXEL_FORMAT_TABLE[0] );
126
127 const StringEnum< ImageAttributes::ScalingMode > IMAGE_SCALING_MODE_TABLE[] =
128 {
129   { "SHRINK_TO_FIT", ImageAttributes::ShrinkToFit },
130   { "SCALE_TO_FILL", ImageAttributes::ScaleToFill },
131   { "FIT_WIDTH",     ImageAttributes::FitWidth    },
132   { "FIT_HEIGHT",    ImageAttributes::FitHeight   },
133 };
134 const unsigned int IMAGE_SCALING_MODE_TABLE_COUNT = sizeof( IMAGE_SCALING_MODE_TABLE ) / sizeof( IMAGE_SCALING_MODE_TABLE[0] );
135
136 } // unnamed namespace
137
138 bool CompareEnums( const char * a, const char * b )
139 {
140   while( ( *a != '\0' ) && ( *b != '\0' ) )
141   {
142     char ca = *a;
143     char cb = *b;
144
145     if( ( ( ca == '-' ) || ( ca == '_') ) &&
146         ( ( cb == '-' ) || ( cb == '_') ) )
147     {
148       ++a;
149       ++b;
150       continue;
151     }
152
153     if( ( 'A' <= ca ) && ( ca <= 'Z') )
154     {
155       ca = ca + ( 'a' - 'A' );
156     }
157
158     if( ( 'A' <= cb ) && ( cb <= 'Z') )
159     {
160       cb = cb + ( 'a' - 'A' );
161     }
162
163     if( ca != cb )
164     {
165       return false;
166     }
167
168     ++a;
169     ++b;
170   }
171
172   if( ( *a == '\0' ) && ( *b == '\0' ) )
173   {
174     return true;
175   }
176
177   return false;
178 }
179
180
181 ColorMode GetColorMode( const std::string& value )
182 {
183   return GetEnumeration< ColorMode >( value.c_str(), COLOR_MODE_TABLE, COLOR_MODE_TABLE_COUNT );
184 }
185
186
187 std::string GetColorMode( ColorMode value )
188 {
189   return GetEnumerationName< ColorMode >( value, COLOR_MODE_TABLE, COLOR_MODE_TABLE_COUNT );
190 }
191
192 PositionInheritanceMode GetPositionInheritanceMode( const std::string& value )
193 {
194   return GetEnumeration< PositionInheritanceMode >( value.c_str(), POSITION_INHERITANCE_MODE_TABLE, POSITION_INHERITANCE_MODE_TABLE_COUNT );
195 }
196
197
198 std::string GetPositionInheritanceMode( PositionInheritanceMode value )
199 {
200   return GetEnumerationName< PositionInheritanceMode >( value, POSITION_INHERITANCE_MODE_TABLE, POSITION_INHERITANCE_MODE_TABLE_COUNT );
201 }
202
203
204 DrawMode::Type GetDrawMode( const std::string& value )
205 {
206   return GetEnumeration< DrawMode::Type >( value.c_str(), DRAW_MODE_TABLE, DRAW_MODE_TABLE_COUNT );
207 }
208
209
210 std::string GetDrawMode( DrawMode::Type value )
211 {
212   return GetEnumerationName< DrawMode::Type >( value, DRAW_MODE_TABLE, DRAW_MODE_TABLE_COUNT );
213 }
214
215
216 Vector3 GetAnchorConstant( const std::string& value )
217 {
218   return GetEnumeration< Vector3 >( value.c_str(), ANCHOR_CONSTANT_TABLE, ANCHOR_CONSTANT_TABLE_COUNT );
219 }
220
221
222 Image NewImage( const Property::Value& map )
223 {
224   Image ret;
225
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();
230
231   if( Property::MAP == map.GetType() )
232   {
233     std::string field = "filename";
234     if( map.HasKey(field) )
235     {
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>();
238     }
239
240     field = "load-policy";
241     if( map.HasKey(field) )
242     {
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 );
246     }
247
248     field = "release-policy";
249     if( map.HasKey(field) )
250     {
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 );
254     }
255
256     if( map.HasKey("width") && map.HasKey("height") )
257     {
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)
262       {
263         w = static_cast<unsigned int>( value.Get<float>() );
264       }
265       else
266       {
267         DALI_ASSERT_ALWAYS(value.GetType() == Property::INTEGER && "Image width property is not a number" );
268         w = value.Get<int>();
269       }
270
271       value = map.GetValue("height");
272       if( value.GetType() == Property::FLOAT)
273       {
274         h = static_cast<unsigned int>( value.Get<float>() );
275       }
276       else
277       {
278         DALI_ASSERT_ALWAYS(value.GetType() == Property::INTEGER && "Image width property is not a number" );
279         h = value.Get<int>();
280       }
281
282       attributes.SetSize( w, h );
283     }
284
285     field = "pixel-format";
286     Pixel::Format pixelFormat = Pixel::RGBA8888;
287     if( map.HasKey(field) )
288     {
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 );
292     }
293
294     field = "scaling-mode";
295     if( map.HasKey(field) )
296     {
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 ) );
300     }
301
302     if( map.HasKey("type") )
303     {
304       DALI_ASSERT_ALWAYS( map.GetValue("type").GetType() == Property::STRING );
305       std::string s(map.GetValue("type").Get<std::string>());
306       if("FrameBufferImage" == s)
307       {
308         ret = FrameBufferImage::New(attributes.GetWidth(),
309                                     attributes.GetHeight(),
310                                     pixelFormat,
311                                     releasePolicy);
312       }
313       else if("BufferImage" == s)
314       {
315         ret = BufferImage::New(attributes.GetWidth(),
316                                attributes.GetHeight(),
317                                pixelFormat,
318                                releasePolicy);
319       }
320       else if("ResourceImage" == s)
321       {
322         ret = ResourceImage::New(filename, attributes, loadPolicy, releasePolicy);
323       }
324       else
325       {
326         DALI_ASSERT_ALWAYS( !"Unknown image type" );
327       }
328     }
329     else
330     {
331       ret = ResourceImage::New(filename, attributes, loadPolicy, releasePolicy);
332     }
333   }
334
335   return ret;
336
337 } // Image NewImage( Property::Value map )
338
339
340 ShaderEffect NewShaderEffect( const Property::Value& map )
341 {
342   Internal::ShaderEffectPtr ret;
343
344   if( map.GetType() == Property::MAP )
345   {
346     ret = Internal::ShaderEffect::New(Dali::ShaderEffect::HINT_NONE); // hint can be reset if in map
347
348     if( map.HasKey("program") )
349     {
350       Property::Index index = ret->GetPropertyIndex("program");
351       DALI_ASSERT_DEBUG( map.GetValue("program").GetType() == Property::MAP );
352       ret->SetProperty(index, map.GetValue("program"));
353     }
354
355     for(int i = 0; i < map.GetSize(); ++i)
356     {
357       const std::string& key = map.GetKey(i);
358       if(key != "program")
359       {
360         Property::Index index = ret->GetPropertyIndex( key );
361
362         if( Property::INVALID_INDEX != index )
363         {
364           ret->SetProperty(index, map.GetItem(i));
365         }
366         else
367         {
368           // if its not a property then register it as a uniform (making a custom property)
369
370           if(map.GetItem(i).GetType() == Property::INTEGER)
371           {
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 );
376           }
377           else
378           {
379             ret->SetUniform( key, map.GetItem(i), Dali::ShaderEffect::COORDINATE_TYPE_DEFAULT );
380           }
381         }
382       }
383     }
384   }
385
386   return Dali::ShaderEffect(ret.Get());
387 }
388
389
390 Actor NewActor( const Property::Map& map )
391 {
392   BaseHandle handle;
393
394   // First find type and create Actor
395   Property::Value* typeValue = map.Find( "type" );
396   if ( typeValue )
397   {
398     TypeInfo type = TypeRegistry::Get().GetTypeInfo( typeValue->Get< std::string >() );
399     if ( type )
400     {
401       handle = type.CreateInstance();
402     }
403   }
404
405   if ( !handle )
406   {
407     DALI_LOG_ERROR( "Actor type not provided, returning empty handle" );
408     return Actor();
409   }
410
411   Actor actor( Actor::DownCast( handle ) );
412
413   if ( actor )
414   {
415     // Now set the properties, or create children
416     for ( unsigned int i = 0, mapCount = map.Count(); i < mapCount; ++i )
417     {
418       const StringValuePair& pair( map.GetPair( i ) );
419       const std::string& key( pair.first );
420       if ( key == "type" )
421       {
422         continue;
423       }
424
425       const Property::Value& value( pair.second );
426
427       if ( key == "actors" )
428       {
429         // Create children
430
431         Property::Array actorArray = value.Get< Property::Array >();
432         for ( Property::Array::iterator arrayIter = actorArray.begin(), arrayEndIter = actorArray.end(); arrayIter != arrayEndIter; ++arrayIter )
433         {
434           actor.Add( NewActor( arrayIter->Get< Property::Map >() ) );
435         }
436       }
437       else if ( key == "signals" )
438       {
439         DALI_LOG_ERROR( "signals not supported" );
440       }
441       else if( key ==  "parent-origin" )
442       {
443         // Parent Origin can be a string constant as well as a Vector3
444
445         const Property::Type type( value.GetType() );
446         if ( type == Property::VECTOR3 )
447         {
448           actor.SetParentOrigin( value.Get< Vector3 >() );
449         }
450         else if( type == Property::STRING )
451         {
452           actor.SetParentOrigin( GetAnchorConstant( value.Get< std::string >() ) );
453         }
454       }
455       else if( key ==  "anchor-point" )
456       {
457         // Anchor Point can be a string constant as well as a Vector3
458
459         const Property::Type type( value.GetType() );
460         if ( type == Property::VECTOR3 )
461         {
462           actor.SetAnchorPoint( value.Get< Vector3 >() );
463         }
464         else if( type == Property::STRING )
465         {
466           actor.SetAnchorPoint( GetAnchorConstant( value.Get< std::string >() ) );
467         }
468       }
469       else
470       {
471         Property::Index index( actor.GetPropertyIndex( key ) );
472
473         if ( index != Property::INVALID_INDEX )
474         {
475           actor.SetProperty( index, value );
476         }
477       }
478     }
479   }
480
481   return actor;
482 }
483
484 void CreatePropertyMap( Actor actor, Property::Map& map )
485 {
486   map.Clear();
487
488   if ( actor )
489   {
490     map[ "type" ] = actor.GetTypeName();
491
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 )
497     {
498       map[ actor.GetPropertyName( *iter ) ] = actor.GetProperty( *iter );
499     }
500
501     // Children
502     unsigned int childCount( actor.GetChildCount() );
503     if ( childCount )
504     {
505       Property::Array childArray;
506       for ( unsigned int child = 0; child < childCount; ++child )
507       {
508         Property::Map childMap;
509         CreatePropertyMap( actor.GetChildAt( child ), childMap );
510         childArray.push_back( childMap );
511       }
512       map[ "actors" ] = childArray;
513     }
514   }
515 }
516
517 void CreatePropertyMap( Image image, Property::Map& map )
518 {
519   map.Clear();
520
521   if ( image )
522   {
523     std::string imageType( "ResourceImage" );
524
525     // Get Type - cannot use TypeRegistry as Image is not an Object and thus, not registered
526     BufferImage bufferImage = BufferImage::DownCast( image );
527     if ( bufferImage )
528     {
529       imageType = "BufferImage";
530       map[ "pixel-format" ] = GetEnumerationName< Pixel::Format >( bufferImage.GetPixelFormat(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT );
531     }
532     else if ( FrameBufferImage::DownCast( image ) )
533     {
534       imageType = "FrameBufferImage";
535     }
536
537     map[ "type" ] = imageType;
538     map[ "release-policy" ] = GetEnumerationName< Image::ReleasePolicy >( image.GetReleasePolicy(), IMAGE_RELEASE_POLICY_TABLE, IMAGE_RELEASE_POLICY_TABLE_COUNT );
539
540     ResourceImage resourceImage = ResourceImage::DownCast( image );
541     if( resourceImage )
542     {
543       map[ "filename" ] = resourceImage.GetUrl();
544       map[ "load-policy" ] = GetEnumerationName< ResourceImage::LoadPolicy >( resourceImage.GetLoadPolicy(), IMAGE_LOAD_POLICY_TABLE, IMAGE_LOAD_POLICY_TABLE_COUNT );
545
546       ImageAttributes attributes( resourceImage.GetAttributes() );
547       map[ "scaling-mode" ] = GetEnumerationName< ImageAttributes::ScalingMode >( attributes.GetScalingMode(), IMAGE_SCALING_MODE_TABLE, IMAGE_SCALING_MODE_TABLE_COUNT );
548     }
549
550     int width( image.GetWidth() );
551     int height( image.GetHeight() );
552
553     if ( width && height )
554     {
555       map[ "width" ] = width;
556       map[ "height" ] = height;
557     }
558   }
559 }
560
561 } // namespace scripting
562
563 } // namespace Dali
564
565
566
567