Synchronous Set/Get Behaviour for custom & animatable properties
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-metadata.h
index 61b3c82..a06f262 100644 (file)
@@ -2,7 +2,7 @@
 #define __DALI_INTERNAL_PROPERTY_METADATA_H__
 
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 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.
@@ -25,6 +25,7 @@
 // INTERNAL INCLUDES
 #include <dali/public-api/common/constants.h>
 #include <dali/public-api/object/property.h>
+#include <dali/public-api/object/property-value.h>
 
 namespace Dali
 {
@@ -38,13 +39,14 @@ class PropertyBase;
 }
 
 /**
- * An entry in a property metadata lookup.
- * The type field should be queried, before accessing the scene-graph property:
+ * @brief Base class for the Metadata required by custom and registered animatable properties.
+ *
+ * The value type field should be queried, before accessing the scene-graph property:
  *
  * @code
  * void Example(PropertyEntry entry)
  * {
- *   if (entry.type == Property::VECTOR3)
+ *   if (entry.value.GetType() == Property::VECTOR3)
  *   {
  *     SceneGraph::AnimatableProperty<Vector3>* property = dynamic_cast< SceneGraph::AnimatableProperty<Vector3>* >( entry.property );
  *     ...
@@ -57,141 +59,188 @@ class PropertyMetadata
 public:
 
   /**
-   * Constructor for an uninitalized property metadata
+   * @brief Virtual Destructor.
    */
-  PropertyMetadata()
-  : type(Property::NONE),
-    value(),
-    componentIndex(Property::INVALID_COMPONENT_INDEX),
-    mProperty(NULL)
+  virtual ~PropertyMetadata()
   {
   }
 
   /**
-   * Constructor for property metadata
-   * @param [in] newProperty A pointer to the property metadata.
+   * @brief Returns whether the property is animatable (i.e. if its a scene graph property).
+   * @return True if animatable, false otherwise
    */
-  PropertyMetadata(const SceneGraph::PropertyBase* newProperty)
-  : type(Property::NONE),
-    value(), // value is held by newProperty
-    componentIndex(Property::INVALID_COMPONENT_INDEX),
-    mProperty(newProperty)
+  bool IsAnimatable( void ) const
   {
-    DALI_ASSERT_DEBUG(mProperty && "Uninitialized scenegraph property") ;
+    return NULL != mSceneGraphProperty;
   }
 
   /**
-   * Constructor for property metadata
-   * @param [in] newValue The value of the scene-graph owned property.
+   * @brief Whether the property can be written to.
+   * @return True if the property can be written to, false otherwise
    */
-  PropertyMetadata(Property::Value newValue)
-  : type(newValue.GetType()),
-    value(newValue),
-    componentIndex(Property::INVALID_COMPONENT_INDEX),
-    mProperty(NULL)
+  bool IsWritable( void ) const
   {
+    return mWritable;
   }
 
   /**
-   * Destructor for property metadata
+   * @brief Retrieves the scene-graph property.
+   * @return The scene graph property
+   *
+   * @note Should only be called if the scene-graph property was passed in originally. Will debug assert if the stored property is NULL.
    */
-  virtual ~PropertyMetadata()
+  const SceneGraph::PropertyBase* GetSceneGraphProperty() const
   {
+    DALI_ASSERT_DEBUG( mSceneGraphProperty && "Accessing uninitialized SceneGraph property" );
+    return mSceneGraphProperty;
   }
 
   /**
-   * @return true if the property is animatable (i.e. if its a scene graph property)
+   * @brief Retrieve the type of the property.
+   * @return Type of the held property value
    */
-  bool IsAnimatable(void) const
+  inline Property::Type GetType() const
   {
-    return NULL != mProperty;
+    return value.GetType();
   }
 
   /**
-   * @return true if the property can be written to
+   * Set the cached value of the property.
+   * @param[in] value The propertyValue to set.
    */
-  virtual bool IsWritable(void) const = 0;
+  void SetPropertyValue( const Property::Value& propertyValue );
 
   /**
-   * @return the scene graph property
+   * Get the cached value of a the property.
+   * @return The cached value of the property.
    */
-  const SceneGraph::PropertyBase* GetSceneGraphProperty() const
+  Property::Value GetPropertyValue() const;
+
+protected:
+
+  /**
+   * @brief Constructor to create Metadata for a property.
+   * @param[in] propertyValue       The value of the property (this is used by the event thread)
+   * @param[in] sceneGraphProperty  A pointer to the scene-graph owned property
+   * @param[in] writable            Whether the property is writable
+   */
+  PropertyMetadata( const Property::Value& propertyValue,
+                    const SceneGraph::PropertyBase* sceneGraphProperty,
+                    bool writable )
+  : value( mStoredValue ),
+    componentIndex( Property::INVALID_COMPONENT_INDEX ),
+    mStoredValue( propertyValue ),
+    mSceneGraphProperty( sceneGraphProperty ),
+    mWritable( writable )
   {
-    DALI_ASSERT_DEBUG(mProperty && "Accessing uninitialized SceneGraph property") ;
-    return mProperty;
   }
 
-  Property::Type type;    ///< The type of the property
-  Property::Value value;  ///< The property value for a non animatable and custom property
-  int componentIndex;     ///< The index of the property component
+  /**
+   * @brief Constructor to create Metadata for a component of another property.
+   * @param[in] sceneGraphProperty      A pointer to the scene-graph owned property
+   * @param[in] writable                Whether the property is writable
+   * @param[in] baseValueRef            A reference to the metadata of the base animatable property
+   * @param[in] propertyComponentIndex  The component index of the property
+   */
+  PropertyMetadata( const SceneGraph::PropertyBase* sceneGraphProperty, bool writable, Property::Value& baseValueRef, int propertyComponentIndex )
+  : value( baseValueRef ),
+    componentIndex( propertyComponentIndex ),
+    mStoredValue(),
+    mSceneGraphProperty( sceneGraphProperty ),
+    mWritable( writable )
+  {
+  }
 
-protected:
+private:
 
   // Not implemented
   PropertyMetadata( const PropertyMetadata& );
   PropertyMetadata& operator=( const PropertyMetadata& );
 
-  const SceneGraph::PropertyBase* mProperty; ///< A pointer to a scene-graph property; should not be modified from actor-thread.
-};
+public: // Data
+
+  /**
+   * @brief The value of this property used to read/write by the event thread.
+   *
+   * If a component index, then refers to the value in the PropertyMetatdata of the base property
+   * to ensure the components are kept in sync with the overall value on the event thread.
+   * Otherwise, this just refers to the storedValue.
+   */
+  Property::Value& value;
+
+  /**
+   * @brief The index of the property component.
+   */
+  int componentIndex;
+
+private:
 
+  Property::Value mStoredValue;                         ///< The stored property value used to read/write by the event thread
+  const SceneGraph::PropertyBase* mSceneGraphProperty;  ///< A pointer to a scene-graph property; should not be modified from actor-thread
+  bool mWritable:1;                                     ///< Whether the property is writable
+};
 
 /**
- * An entry in an animatable property metadata lookup.
- * The type field should be queried, before accessing the animatable property:
+ * @brief Metadata for a registered animatable property.
  */
 class AnimatablePropertyMetadata : public PropertyMetadata
 {
 public:
 
   /**
-   * Constructor for metadata of animatable property
-   * @param [in] newIndex The index of the animatable property.
-   * @param [in] newType The type ID of the animatable property.
-   * @param [in] newProperty A pointer to the scene-graph owned property.
+   * @brief Constructs metadata for a registered animatable property.
+   * @param[in] propertyIndex           The index of the animatable property
+   * @param[in] propertyComponentIndex  The component index of the animatable property
+   * @param[in] propertyValue           The value of the property (this is used by the event thread)
+   * @param[in] sceneGraphProperty      A pointer to the scene-graph owned property
+   *
+   * @note The base animatable property MUST be created before the component animatable property.
    */
-  AnimatablePropertyMetadata( Property::Index newIndex,
-                        int newComponentIndex,
-                        Property::Type newType,
-                        const SceneGraph::PropertyBase* newProperty )
-  : index(newIndex)
+  AnimatablePropertyMetadata( Property::Index propertyIndex,
+                              const Property::Value& propertyValue,
+                              const SceneGraph::PropertyBase* sceneGraphProperty )
+  : PropertyMetadata( propertyValue, sceneGraphProperty, true ),
+    index( propertyIndex )
   {
-    componentIndex = newComponentIndex;
-    type = newType;
-    mProperty = newProperty;
-    DALI_ASSERT_DEBUG(mProperty && "Uninitialized scenegraph property") ;
+    DALI_ASSERT_DEBUG( sceneGraphProperty && "Uninitialized scene-graph property" );
   }
 
   /**
-   * Constructor for metadata of animatable property
-   * @param [in] newIndex The index of the animatable property.
-   * @param [in] newValue The value of the scene-graph owned property.
+   * @brief Constructs metadata for a registered animatable component of another property.
+   * @param[in] propertyIndex           The index of the animatable property
+   * @param[in] propertyComponentIndex  The component index of the animatable property
+   * @param[in] baseValueRef            A reference to the metadata of the base animatable property
+   * @param[in] sceneGraphProperty      A pointer to the scene-graph owned property
+   *
+   * @note The base animatable property MUST be created before the component animatable property.
    */
-  AnimatablePropertyMetadata( Property::Index newIndex,
-                        int newComponentIndex,
-                        Property::Value newValue )
-  : index(newIndex)
+  AnimatablePropertyMetadata( Property::Index propertyIndex,
+                              int propertyComponentIndex,
+                              Property::Value& baseValueRef,
+                              const SceneGraph::PropertyBase* sceneGraphProperty )
+  : PropertyMetadata( sceneGraphProperty, true, baseValueRef, propertyComponentIndex ),
+    index( propertyIndex )
   {
-    componentIndex = newComponentIndex;
-    type = newValue.GetType();
-    value = newValue;
+    DALI_ASSERT_DEBUG( sceneGraphProperty && "Uninitialized scene-graph property" );
   }
 
   /**
-   * @return true if the property can be written to
+   * @brief Destructor.
    */
-  virtual bool IsWritable(void) const
+  virtual ~AnimatablePropertyMetadata()
   {
-    return true ;
   }
 
-  Property::Index index;       ///< The index of the property
-
 private:
 
   // Not implemented
   AnimatablePropertyMetadata();
   AnimatablePropertyMetadata( const AnimatablePropertyMetadata& );
   AnimatablePropertyMetadata& operator=( const AnimatablePropertyMetadata& );
+
+public: // Data
+
+  Property::Index   index;    ///< The index of the property.
 };
 
 class CustomPropertyMetadata : public PropertyMetadata
@@ -199,49 +248,52 @@ class CustomPropertyMetadata : public PropertyMetadata
 public:
 
   /**
-   * Constructor for metadata of scene graph based properties
-   * @param [in] newName The name of the custom property.
-   * @param [in] newType The type ID of the custom property.
-   * @param [in] newProperty A pointer to the scene-graph owned property.
+   * Constructs Metadata for scene-graph-based custom properties, i.e. animatable custom properties.
+   * @param[in] propertyName        The name of the custom property
+   * @param[in] propertyKey         The key of the custom property
+   * @param[in] propertyValue       The value of the property (this is used by the event thread)
+   * @param[in] sceneGraphProperty  A pointer to the scene-graph owned property
+   *
+   * @note A valid sceneGraphProperty is mandatory otherwise this will debug assert.
    */
-  CustomPropertyMetadata( const std::string& newName,
-                          Property::Type newType,
-                          const SceneGraph::PropertyBase* newProperty)
-  : name(newName),
-    mAccessMode(Property::ANIMATABLE)
+  CustomPropertyMetadata( const std::string& propertyName,
+                          Property::Index propertyKey,
+                          const Property::Value& propertyValue,
+                          const SceneGraph::PropertyBase* sceneGraphProperty )
+  : PropertyMetadata( propertyValue, sceneGraphProperty, true ),
+    name( propertyName ),
+    key( propertyKey ),
+    childPropertyIndex( Property::INVALID_INDEX )
   {
-    type = newType;
-    mProperty = newProperty;
-    DALI_ASSERT_DEBUG(mProperty && "Uninitialized scenegraph property") ;
+    DALI_ASSERT_DEBUG( sceneGraphProperty && "Uninitialized scene-graph property" );
   }
 
   /**
-   * Constructor for metadata of event side only properties
-   * @param [in] newName The name of the custom property.
-   * @param [in] newValue The value of the custom property.
-   * @param [in] accessMode The access mode of the custom property (writable, animatable etc).
+   * Constructs metadata for event side only custom properties.
+   * @param[in] propertyName   The name of the custom property
+   * @param[in] propertyValue  The value of the property (this is used by the event thread)
+   * @param[in] accessMode     The access mode of the custom property (writable, animatable etc)
+   *
+   * @note The access mode MUST NOT be animatable otherwise this will debug assert.
    */
-  CustomPropertyMetadata( const std::string& newName,
-                          Property::Value newValue,
+  CustomPropertyMetadata( const std::string& propertyName,
+                          const Property::Value& propertyValue,
                           Property::AccessMode accessMode )
-  : name(newName),
-    mAccessMode(accessMode)
+  : PropertyMetadata( propertyValue, NULL, ( accessMode != Property::READ_ONLY ) ),
+    name( propertyName ),
+    key( Property::INVALID_KEY ),
+    childPropertyIndex( Property::INVALID_INDEX )
   {
-    type = newValue.GetType();
-    value = newValue;
-    DALI_ASSERT_DEBUG(accessMode != Property::ANIMATABLE && "Animatable must have scenegraph property") ;
+    DALI_ASSERT_DEBUG( accessMode != Property::ANIMATABLE && "Event side only properties should not be animatable" );
   }
 
   /**
-   * @return true if the property can be written to
+   * @brief Destructor.
    */
-  virtual bool IsWritable(void) const
+  virtual ~CustomPropertyMetadata()
   {
-    return (mAccessMode == Property::ANIMATABLE) || (mAccessMode == Property::READ_WRITE) ;
   }
 
-  std::string name;       ///< The name of the property
-
 private:
 
   // Not implemented
@@ -249,8 +301,11 @@ private:
   CustomPropertyMetadata( const CustomPropertyMetadata& );
   CustomPropertyMetadata& operator=( const CustomPropertyMetadata& );
 
-private:
-  Property::AccessMode mAccessMode; ///< The mode of the property
+public: // Data
+
+  std::string       name;                 ///< The name of the property.
+  Property::Index   key;                  ///< The key of the property.
+  Property::Index   childPropertyIndex;   ///< The index as a child property.
 };
 
 } // namespace Internal