use modern construct 'nullptr' instead of 'NULL' or '0'
[platform/core/uifw/dali-core.git] / dali / devel-api / scripting / scripting.h
index 544c48f..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.
@@ -22,8 +22,7 @@
 #include <dali/public-api/actors/actor-enumerations.h>
 #include <dali/public-api/actors/draw-mode.h>
 #include <dali/devel-api/animation/animation-data.h>
-#include <dali/public-api/images/image.h>
-#include <dali/public-api/shader-effects/shader-effect.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>
 
@@ -43,8 +42,8 @@ namespace Scripting
  */
 struct StringEnum
 {
-  const char* string; ///< The string representation
-  const int value;    ///< The enumeration value wrapped in int
+  const char* string;  ///< The string representation
+  const int32_t value; ///< The enumeration value wrapped in int
 };
 
 /**
@@ -55,7 +54,7 @@ struct StringEnum
  * @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_IMPORT_API unsigned int FindEnumIndex( const char* value, const StringEnum* table, unsigned int 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
@@ -68,7 +67,7 @@ DALI_IMPORT_API unsigned int FindEnumIndex( const char* value, const StringEnum*
  * @param[out] integerEnum The value of the enum.
  * @return     true if one or more enums in value.
  */
-DALI_IMPORT_API bool EnumStringToInteger( const char* const value, const StringEnum* const table, unsigned int tableCount, unsigned int& integerEnum );
+DALI_CORE_API bool EnumStringToInteger( const char* const value, const StringEnum* const table, uint32_t tableCount, int& integerEnum );
 
 /**
  * @brief Chooses the appropriate enumeration for the provided string from the given table.
@@ -81,12 +80,12 @@ DALI_IMPORT_API bool EnumStringToInteger( const char* const value, const StringE
  * @return     True if the value was found from the table
  */
 template< typename T >
-bool GetEnumeration( const char* value, const StringEnum* table, unsigned int tableCount, T& result )
+bool GetEnumeration( const char* value, const StringEnum* table, uint32_t tableCount, T& result )
 {
   bool retVal( false );
   if( table )
   {
-    unsigned int integerEnum = 0;
+    int integerEnum = 0;
     // check to avoid crash, not asserting on purpose, error is logged instead
     if( EnumStringToInteger( value, table, tableCount, integerEnum ) )
     {
@@ -98,6 +97,112 @@ bool GetEnumeration( const char* value, const StringEnum* table, unsigned int ta
 }
 
 /**
+ * @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]  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.
+ * @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 >
+bool GetEnumerationProperty( const Property::Value& propertyValue, const StringEnum* table, uint32_t tableCount, T& result )
+{
+  int newValue;
+  bool set = false;
+  Property::Type type = propertyValue.GetType();
+
+  if( type == Property::INTEGER )
+  {
+    // 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 the property was converted OK, AND the value has changed, update the result and return true.
+  if( set && ( result != static_cast<T>( newValue ) ) )
+  {
+    result = static_cast<T>( newValue );
+    return true;
+  }
+
+  // No change.
+  return false;
+}
+
+/**
+ * @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]  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.
+ * @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 >
+bool GetBitmaskEnumerationProperty( const Property::Value& propertyValue, const Scripting::StringEnum* table, uint32_t tableCount, T& result )
+{
+  bool returnValue = true;
+
+  // Evaluate as a single INTEGER or STRING first.
+  if( !GetEnumerationProperty( propertyValue, table, tableCount, result ) )
+  {
+    // 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
+    {
+      // Property type was not ARRAY, and the single property evaluation also failed.
+      returnValue = false;
+    }
+  }
+
+  return returnValue;
+}
+
+/**
  * @brief Chooses the appropriate string for the provided enumeration from the given table.
  *
  * @param[in]  value       The enumeration.
@@ -109,11 +214,11 @@ bool GetEnumeration( const char* value, const StringEnum* table, unsigned int ta
  * @note The caller is NOT responsible for cleaning up the returned pointer as it is statically allocated.
  */
 template< typename T >
-const char* GetEnumerationName( T value, const StringEnum* table, unsigned int tableCount )
+const char* GetEnumerationName( T value, const StringEnum* table, uint32_t tableCount )
 {
   if( table )
   {
-    for ( unsigned int i = 0; i < tableCount; ++i )
+    for ( uint32_t i = 0; i < tableCount; ++i )
     {
       if ( value == T(table[ i ].value) )
       {
@@ -121,7 +226,7 @@ const char* GetEnumerationName( T value, const StringEnum* table, unsigned int t
       }
     }
   }
-  return NULL;
+  return nullptr;
 }
 
 /**
@@ -137,49 +242,24 @@ const char* GetEnumerationName( T value, const StringEnum* table, unsigned int t
  * @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* table, unsigned int tableCount )
+const char * GetLinearEnumerationName( T value, const StringEnum* table, uint32_t tableCount )
 {
-  if ( table && ( value > 0 || value <= (int)tableCount ) )
+  if ( table && ( value > 0 || value <= static_cast<int>( tableCount ) ) )
   {
     return table[value].string;
   }
-  return NULL;
+  return nullptr;
 }
 
 /**
- * @brief Creates object with data from the property value map.
- *
- * @param[in] property The property value map with the following valid fields:
- * @code
- * "filename":      type std::string
- * "loadPolicy"     type std::string (enum)
- * "releasePolicy"  type std::string (enum)
- * "width"          type float
- * "height"         type float
- * "pixelFormat"    type std::string (enum)
- * "fittingMode"    type std::string (enum)
- * "samplingMode"   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.
- *
- * @return A pointer to a newly created object.
- */
-DALI_IMPORT_API Image NewImage( const Property::Value& property );
-
-/**
  * @brief Creates an actor with the date from the property value map.
  *
  * @param[in] map The property value map with the properties (and hierarchy) of the actor required
  *                 For example:
  * @code
  * {
- *   "type": "ImageActor",
- *   "image":
- *   {
- *     "filename":"my-image-path.png"
- *   },
+ *   "type": "Actor",
+ *   "position": [ 100, 100, 0 ],
  *   "actors":
  *   [
  *     {
@@ -192,7 +272,7 @@ DALI_IMPORT_API Image NewImage( const Property::Value& property );
  *
  * @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.
@@ -200,15 +280,7 @@ DALI_IMPORT_API Actor NewActor( const Property::Map& map );
  * @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 Creates description data required to create an Animation object from a property map.
@@ -216,10 +288,10 @@ DALI_IMPORT_API void CreatePropertyMap( Image image, Property::Map& map );
  * @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 void NewAnimation( const Property::Map& map, Dali::AnimationData& outputAnimationData );
+DALI_CORE_API void NewAnimation( const Property::Map& map, Dali::AnimationData& outputAnimationData );
 
 } // namespace Scripting
 
 } // namespace Dali
 
-#endif // __DALI_SCRIPTING_H__
+#endif // DALI_SCRIPTING_H