use modern construct 'nullptr' instead of 'NULL' or '0'
[platform/core/uifw/dali-core.git] / dali / devel-api / scripting / scripting.h
index b0cd9ad..c2f79d2 100644 (file)
@@ -1,8 +1,8 @@
-#ifndef __DALI_SCRIPTING_H__
-#define __DALI_SCRIPTING_H__
+#ifndef DALI_SCRIPTING_H
+#define DALI_SCRIPTING_H
 
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,8 +21,8 @@
 // INTERNAL INCLUDES
 #include <dali/public-api/actors/actor-enumerations.h>
 #include <dali/public-api/actors/draw-mode.h>
-#include <dali/public-api/images/image.h>
-#include <dali/public-api/shader-effects/shader-effect.h>
+#include <dali/devel-api/animation/animation-data.h>
+#include <dali/public-api/object/property-array.h>
 #include <dali/public-api/object/property-map.h>
 #include <dali/public-api/object/property-value.h>
 
@@ -38,236 +38,218 @@ namespace Scripting
 {
 
 /**
- * @brief Template structure which stores an enumeration and its string equivalent.
+ * @brief Structure which stores an enumeration and its string equivalent.
  */
-template< typename T >
 struct StringEnum
 {
-  const char* string; ///< The string representation
-  const T value;      ///< The actual enumeration
+  const char* string;  ///< The string representation
+  const int32_t value; ///< The enumeration value wrapped in int
 };
 
 /**
- * @brief Permissive comparison for string enums.
+ * @brief Find the given enum index from the table
  *
- * Case insensitive and ignores '_', '-' in either string when comparing.
+ * @param[in]  value       The string equivalent (case-insensitive).
+ * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
+ * @param[in]  tableCount  Number of items in the array.
+ * @return     The index of the enumeration. If enumeration is not found, logs an error and returns tableCount.
+ */
+DALI_CORE_API uint32_t FindEnumIndex( const char* value, const StringEnum* table, uint32_t tableCount );
+
+/**
+ * @brief Find the enum as an integer from the table
  *
- * @note If both strings are empty return true;
+ * @SINCE_1_1.12
  *
- * @param[in] a The first string
- * @param[in] b The string to compare
- * @return true if the strings are equal as defined above. If both empty, then return true.
+ * @param[in]  value       The string equivalent (case-insensitive, comma separate to OR values).
+ * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
+ * @param[in]  tableCount  Number of items in the array.
+ * @param[out] integerEnum The value of the enum.
+ * @return     true if one or more enums in value.
  */
-DALI_IMPORT_API bool CompareEnums( const char * a, const char * b );
+DALI_CORE_API bool EnumStringToInteger( const char* const value, const StringEnum* const table, uint32_t tableCount, int& integerEnum );
 
 /**
- * @brief Set the value if strings pass a permissive compare.
+ * @brief Chooses the appropriate enumeration for the provided string from the given table.
+ *
+ * @param[in]  value       The string equivalent (case-insensitive, comma separate to OR values).
+ * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
+ * @param[in]  tableCount  Number of items in the array.
+ * @param[out] result      The enum value
  *
- * @param[in] a The input string
- * @param[in] b The string to compare
- * @param[in] set The variable to set
- * @param[in] value The value to set
- * @return true if the strings pass the permissive compare
+ * @return     True if the value was found from the table
  */
-template <typename T>
-bool SetIfEqual(const char * a, const char * b, T& set, const T& value)
+template< typename T >
+bool GetEnumeration( const char* value, const StringEnum* table, uint32_t tableCount, T& result )
 {
-  if( CompareEnums( a, b ) )
+  bool retVal( false );
+  if( table )
   {
-    set = value;
-    return true;
+    int integerEnum = 0;
+    // check to avoid crash, not asserting on purpose, error is logged instead
+    if( EnumStringToInteger( value, table, tableCount, integerEnum ) )
+    {
+      result = static_cast<T>( integerEnum );
+      retVal = true;
+    }
   }
-
-  return false;
+  return retVal;
 }
 
 /**
- * @brief Chooses the appropriate enumeration for the provided string from the given table.
+ * @brief Gets the enumeration value from an enumeration property.
+ * An enumeration property is a property that can be set with either an INTEGER or STRING.
  *
- * @param[in]  value       The string equivalent (case-insensitive).
+ * @param[in]  propertyValue The property containing the int or string value.
  * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
  * @param[in]  tableCount  Number of items in the array.
- *
- * @return The equivalent enumeration for the given string.
+ * @param[out] result      The enum value. This is not modified if the enumeration could not be converted.
+ * @return     True if the value was found successfully AND the value has changed. This is to allow the caller to do nothing if there is no change.
  */
 template< typename T >
-T GetEnumeration( const char * value, const StringEnum< T >* table, unsigned int tableCount )
+bool GetEnumerationProperty( const Property::Value& propertyValue, const StringEnum* table, uint32_t tableCount, T& result )
 {
-  T retVal( table->value );
-  bool set( false );
+  int newValue;
+  bool set = false;
+  Property::Type type = propertyValue.GetType();
 
-  for ( unsigned int i = 0; ( i < tableCount ) && ( !set ); ++i )
+  if( type == Property::INTEGER )
   {
-    set = SetIfEqual( value, table->string, retVal, table->value );
-    ++table;
+    // Attempt to fetch the property as an INTEGER type.
+    if( propertyValue.Get( newValue ) )
+    {
+      // Success.
+      set = true;
+    }
+  }
+  else if( type == Property::STRING )
+  {
+    // Attempt to fetch the property as an STRING type, and convert it from string to enumeration value.
+    std::string propertyString;
+    if( table && propertyValue.Get( propertyString ) && EnumStringToInteger( propertyString.c_str(), table, tableCount, newValue ) )
+    {
+      // Success.
+      set = true;
+    }
   }
 
-  if ( !set )
+  // If the property was converted OK, AND the value has changed, update the result and return true.
+  if( set && ( result != static_cast<T>( newValue ) ) )
   {
-    DALI_ASSERT_ALWAYS( !"Unknown enumeration string" );
+    result = static_cast<T>( newValue );
+    return true;
   }
 
-  return retVal;
+  // No change.
+  return false;
 }
 
 /**
- * @brief Chooses the appropriate string for the provided enumeration from the given table.
+ * @brief Gets the enumeration value from a bitmask enumeration property.
+ * An enumeration property is a property that can be set with either an INTEGER, STRING or an ARRAY of STRING.
  *
- * @param[in]  value       The enumeration.
+ * @param[in]  propertyValue The property containing the int, string or and array of string values.
  * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
  * @param[in]  tableCount  Number of items in the array.
- *
- * @return The equivalent enumeration for the given string. Will return NULL if the value does not exist
- *
- * @note The caller is NOT responsible for cleaning up the returned pointer as it is statically allocated.
+ * @param[out] result      The enum value. This is not modified if the enumeration could not be converted.
+ * @return     True if the value was found successfully AND the value has changed. This is to allow the caller to do nothing if there is no change.
  */
 template< typename T >
-const char * GetEnumerationName( T value, const StringEnum< T >* table, unsigned int tableCount )
+bool GetBitmaskEnumerationProperty( const Property::Value& propertyValue, const Scripting::StringEnum* table, uint32_t tableCount, T& result )
 {
-  for ( unsigned int i = 0; i < tableCount; ++i )
+  bool returnValue = true;
+
+  // Evaluate as a single INTEGER or STRING first.
+  if( !GetEnumerationProperty( propertyValue, table, tableCount, result ) )
   {
-    if ( value == table[ i ].value )
+    // If not, then check if it's an ARRAY
+    if ( propertyValue.GetType() == Property::ARRAY )
+    {
+      int newValue = 0;
+      Property::Array array;
+      propertyValue.Get( array );
+      for( Property::Array::SizeType i = 0; i < array.Count(); ++i )
+      {
+        Property::Value currentValue = array[ i ];
+        // Use an initial value of -1 so any successful property conversion
+        // causes a change (and true to be returned).
+        T current = static_cast< T >( -1 );
+        if( GetEnumerationProperty( currentValue, table, tableCount, current ) )
+        {
+          newValue |= current;
+        }
+        else
+        {
+          // We hit an invalid type.
+          returnValue = false;
+          break;
+        }
+      }
+
+      // If we didn't hit an invalid type and the value has changed, update the result.
+      if( returnValue && ( result != static_cast<T>( newValue ) ) )
+      {
+        result = static_cast<T>( newValue );
+      }
+    }
+    else
     {
-      return table[ i ].string;
+      // Property type was not ARRAY, and the single property evaluation also failed.
+      returnValue = false;
     }
   }
 
-  return NULL;
+  return returnValue;
 }
 
 /**
  * @brief Chooses the appropriate string for the provided enumeration from the given table.
- * This is an optimised version that handles enumerations that start at 0 and are linear only.
  *
  * @param[in]  value       The enumeration.
  * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
  * @param[in]  tableCount  Number of items in the array.
  *
- * @return The equivalent enumeration for the given string. Will return NULL if the value does not exist
+ * @return     The equivalent enumeration for the given string. Will return NULL if the value does not exist
  *
  * @note The caller is NOT responsible for cleaning up the returned pointer as it is statically allocated.
  */
 template< typename T >
-const char * GetLinearEnumerationName( T value, const StringEnum< T >* table, unsigned int tableCount )
+const char* GetEnumerationName( T value, const StringEnum* table, uint32_t tableCount )
 {
-  if ( value < 0 || value >= (int)tableCount )
+  if( table )
   {
-    return NULL;
+    for ( uint32_t i = 0; i < tableCount; ++i )
+    {
+      if ( value == T(table[ i ].value) )
+      {
+        return table[ i ].string;
+      }
+    }
   }
-
-  return table[value].string;
+  return nullptr;
 }
 
 /**
- * @brief Takes a string and returns the appropriate color mode.
- *
- * @param[in] value The input string
- * @return The corresponding color-mode.
- */
-DALI_IMPORT_API ColorMode GetColorMode( const std::string& value );
-
-/**
- * @brief Takes a color mode and returns the appropriate string equivalent.
- *
- * @param[in] value The color mode
- * @return The corresponding string.
- */
-DALI_IMPORT_API std::string GetColorMode( ColorMode value );
-
-/**
- * @brief Takes a string and returns the appropriate position inheritance mode.
- *
- * @param[in] value The input string
- * @return The corresponding position-inheritance-mode.
- */
-DALI_IMPORT_API PositionInheritanceMode GetPositionInheritanceMode( const std::string& value );
-
-/**
- * @brief Takes a position inheritance mode and returns the string equivalent.
- *
- * @param[in] value The position-inheritance-mode.
- * @return The corresponding string.
- */
-DALI_IMPORT_API std::string GetPositionInheritanceMode( PositionInheritanceMode value );
-
-/**
- * @brief Takes a string and returns the appropriate draw mode.
- *
- * @param[in] value The input string
- * @return The corresponding draw-mode.
- */
-DALI_IMPORT_API DrawMode::Type GetDrawMode( const std::string& value );
-
-/**
- * @brief Takes a draw-mode and returns the string equivalent.
- *
- * @param[in] value The draw-mode.
- * @return The corresponding string.
- */
-DALI_IMPORT_API std::string GetDrawMode( DrawMode::Type value );
-
-/**
- * @brief Takes a string and returns the appropriate anchor-point or parent-origin constant.
- *
- * @param[in] value The input string
- * @return The corresponding anchor-point or parent-origin constant.
- */
-DALI_IMPORT_API Vector3 GetAnchorConstant( const std::string& value );
-
-/**
- * @brief Creates object with data from the property value map.
- *
- * @param[in] map The property value map with the following valid fields:
- * @code
- * "filename":       type std::string
- * "load-policy"     type std::string (enum)
- * "release-policy"  type std::string (enum)
- * "width"           type float
- * "height"          type float
- * "pixel-format"    type std::string (enum)
- * "fitting-mode"    type std::string (enum)
- * "sampling-mode"   type std::string (enum)
- * "orientation"     type bool
- * "type"            type std::string (FrameBufferImage|BufferImage|ResourceImage(default))
- * @endcode
- * Some fields are optional and some only pertain to a specific type.
+ * @brief Chooses the appropriate string for the provided enumeration from the given table.
+ * This is an optimised version that handles enumerations that start at 0 and are linear only.
  *
- * @return a pointer to a newly created object.
- */
-DALI_IMPORT_API Image NewImage( const Property::Value& map );
-
-/**
- * @brief Creates object with data from the property value map.
+ * @param[in]  value       The enumeration.
+ * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
+ * @param[in]  tableCount  Number of items in the array.
  *
- * @param[in] map The property value map with the following valid fields:
- * @code
- * // a program can be specified as string or a filename.
- * // some fields may be ignored depending on the geometry-type
- * "program":        type Map
- * {
- *   "vertex":                   type std::string
- *   "fragment":                 type std::string
- *   "vertex-prefix":            type std::string
- *   "fragment-prefix":          type std::string
- *   "text-vertex":              type std::string
- *   "text-fragment":            type std::string
- *   "vertex-filename":          type std::string
- *   "fragment-filename":        type std::string
- *   "vertex-prefix-filename":   type std::string
- *   "fragment-prefix-filename": type std::string
- *   "text-vertex-filename":     type std::string
- *   "text-fragment-filename":   type std::string
- *   "geometry-type":            type std::string (enum)
- *   "geometry-hints":           type std::string (enum)
- * }
- * // uniforms must be specified to be registered
- * "uUniform1":       type float,
- * "uUniform2":       type float, etc
- * @endcode
+ * @return     The equivalent enumeration for the given string. Will return NULL if the value does not exist
  *
- * @return a pointer to a newly created object.
+ * @note The caller is NOT responsible for cleaning up the returned pointer as it is statically allocated.
  */
-DALI_IMPORT_API ShaderEffect NewShaderEffect( const Property::Value& map );
+template< typename T >
+const char * GetLinearEnumerationName( T value, const StringEnum* table, uint32_t tableCount )
+{
+  if ( table && ( value > 0 || value <= static_cast<int>( tableCount ) ) )
+  {
+    return table[value].string;
+  }
+  return nullptr;
+}
 
 /**
  * @brief Creates an actor with the date from the property value map.
@@ -276,11 +258,8 @@ DALI_IMPORT_API ShaderEffect NewShaderEffect( const Property::Value& map );
  *                 For example:
  * @code
  * {
- *   "type": "ImageActor",
- *   "image":
- *   {
- *     "filename":"my-image-path.png"
- *   },
+ *   "type": "Actor",
+ *   "position": [ 100, 100, 0 ],
  *   "actors":
  *   [
  *     {
@@ -291,37 +270,28 @@ DALI_IMPORT_API ShaderEffect NewShaderEffect( const Property::Value& map );
  * }
  * @endcode
  *
- * @return Handle to the newly created actor.
+ * @return A handle to the newly created actor.
  */
-DALI_IMPORT_API Actor NewActor( const Property::Map& map );
+DALI_CORE_API Actor NewActor( const Property::Map& map );
 
 /**
  * @brief Creates a Property::Map from the actor provided.
  *
- * @param[in] actor The base-actor from which a Property::Map should be created
+ * @param[in]  actor The base-actor from which a Property::Map should be created
  * @param[out] map This map is cleared and a property map of actor and its children is filled in
  */
-DALI_IMPORT_API void CreatePropertyMap( Actor actor, Property::Map& map );
-
-/**
- * @brief Creates a Property::Map from the image provided.
- *
- * @param[in] image The image from which a Property::Map should be created
- * @param[out] map This map is cleared and a property map of the image is filled in
- */
-DALI_IMPORT_API void CreatePropertyMap( Image image, Property::Map& map );
+DALI_CORE_API void CreatePropertyMap( Actor actor, Property::Map& map );
 
 /**
- * @brief Sets a quaterion rotation from a script provided value (quaternion,euler vector3,quaternion vector4)
+ * @brief Creates description data required to create an Animation object from a property map.
  *
- * @param[in] value Propery value
- * @param[out] quaternion Output Rotation
- * @return true if quaternion was set
+ * @param[in]  map The property value map containing the animation description
+ * @param[out] outputAnimationData Resultant data retrieved from the property map is written here
  */
-DALI_IMPORT_API bool SetRotation( const Property::Value& value, Quaternion& quaternion );
+DALI_CORE_API void NewAnimation( const Property::Map& map, Dali::AnimationData& outputAnimationData );
 
-}
+} // namespace Scripting
 
 } // namespace Dali
 
-#endif // __DALI_SCRIPTING_H__
+#endif // DALI_SCRIPTING_H