Helper method for getting enum properties as ints and strings
[platform/core/uifw/dali-core.git] / dali / devel-api / scripting / scripting.h
index 1a42e17..e688435 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) 2016 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.
@@ -60,7 +60,7 @@ DALI_IMPORT_API unsigned int FindEnumIndex( const char* value, const StringEnum*
 /**
  * @brief Find the enum as an integer from the table
  *
- * @since 1.1.12
+ * @SINCE_1_1.12
  *
  * @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.
@@ -68,7 +68,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_IMPORT_API bool EnumStringToInteger( const char* const value, const StringEnum* const table, unsigned int tableCount, int& integerEnum );
 
 /**
  * @brief Chooses the appropriate enumeration for the provided string from the given table.
@@ -86,7 +86,7 @@ bool GetEnumeration( const char* value, const StringEnum* table, unsigned int ta
   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 +98,54 @@ 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, unsigned int 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 Chooses the appropriate string for the provided enumeration from the given table.
  *
  * @param[in]  value       The enumeration.
@@ -147,62 +195,6 @@ const char * GetLinearEnumerationName( T value, const StringEnum* table, unsigne
 }
 
 /**
- * @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] property The property value map with the following valid fields:
@@ -225,39 +217,6 @@ DALI_IMPORT_API Vector3 GetAnchorConstant( const std::string& value );
 DALI_IMPORT_API Image NewImage( const Property::Value& property );
 
 /**
- * @brief Creates object with data from the property value map.
- *
- * @param[in] property 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
- *   "vertexPrefix":           type std::string
- *   "fragmentPrefix":         type std::string
- *   "textVertex":             type std::string
- *   "textFragment":           type std::string
- *   "vertexFilename":         type std::string
- *   "fragmentFilename":       type std::string
- *   "vertexPrefixFilename":   type std::string
- *   "fragmentPrefixFilename": type std::string
- *   "textVertexFilename":     type std::string
- *   "textFragmentFilename":   type std::string
- *   "geometryType":           type std::string (enum)
- *   "geometryHints":          type std::string (enum)
- * }
- * // uniforms must be specified to be registered
- * "uUniform1":       type float,
- * "uUniform2":       type float, etc
- * @endcode
- *
- * @return A pointer to a newly created object.
- */
-DALI_IMPORT_API ShaderEffect NewShaderEffect( 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
@@ -311,4 +270,4 @@ DALI_IMPORT_API void NewAnimation( const Property::Map& map, Dali::AnimationData
 
 } // namespace Dali
 
-#endif // __DALI_SCRIPTING_H__
+#endif // DALI_SCRIPTING_H