Allow reading Vector2/3/4 values from Property::Value with type Vector2/3/4, matching...
[platform/core/uifw/dali-core.git] / dali / public-api / object / property-value.cpp
index 6a46abb..ffd0f70 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -42,11 +42,11 @@ namespace Dali
 namespace
 {
 /**
- * Helper to check if the property value can be read as int/unsigned int/bool
+ * Helper to check if the property value can be read as int/bool
  */
 inline bool IsIntegerType( Property::Type type )
 {
-  return ( Property::BOOLEAN == type )||( Property::INTEGER == type )||(Property::UNSIGNED_INTEGER == type );
+  return ( Property::BOOLEAN == type )||( Property::INTEGER == type );
 }
 }
 
@@ -72,11 +72,6 @@ struct Property::Value::Impl
     integerValue( integerValue )
   { }
 
-  Impl( unsigned int unsignedIntegerValue )
-  : type( Property::UNSIGNED_INTEGER ),
-    unsignedIntegerValue( unsignedIntegerValue )
-  { }
-
   Impl( const Vector2& vectorValue )
   : type( Property::VECTOR2 ),
     vector2Value( new Vector2( vectorValue ) )
@@ -106,14 +101,15 @@ struct Property::Value::Impl
 
   Impl( const AngleAxis& angleAxisValue )
   : type( Property::ROTATION ),
-    quaternionValue( new Quaternion( angleAxisValue.angle, angleAxisValue.axis ) )
+    angleAxisValue( new AngleAxis(angleAxisValue) )
   {
   }
 
   Impl( const Quaternion& quaternionValue )
   : type( Property::ROTATION ),
-    quaternionValue( new Quaternion( quaternionValue ) )
+    angleAxisValue( new AngleAxis() )
   {
+    quaternionValue.ToAxisAngle( angleAxisValue->axis, angleAxisValue->angle );
   }
 
   Impl(const std::string& stringValue)
@@ -150,8 +146,7 @@ struct Property::Value::Impl
       case Property::NONE :             // FALLTHROUGH
       case Property::BOOLEAN :          // FALLTHROUGH
       case Property::FLOAT :            // FALLTHROUGH
-      case Property::INTEGER :          // FALLTHROUGH
-      case Property::UNSIGNED_INTEGER :
+      case Property::INTEGER :
       {
         break; // nothing to do
       }
@@ -187,7 +182,7 @@ struct Property::Value::Impl
       }
       case Property::ROTATION:
       {
-        delete quaternionValue;
+        delete angleAxisValue;
         break;
       }
       case Property::STRING:
@@ -215,14 +210,13 @@ public: // Data
   {
     int integerValue;
     float floatValue;
-    unsigned int unsignedIntegerValue;
     // must use pointers for any class value pre c++ 11
     Vector2* vector2Value;
     Vector3* vector3Value;
     Vector4* vector4Value;
     Matrix3* matrix3Value;
     Matrix* matrixValue;
-    Quaternion* quaternionValue;
+    AngleAxis* angleAxisValue;
     std::string* stringValue;
     Rect<int>* rectValue;
     Property::Array* arrayValue;
@@ -250,11 +244,6 @@ Property::Value::Value( int integerValue )
 {
 }
 
-Property::Value::Value( unsigned int unsignedIntegerValue )
-: mImpl( new Impl( unsignedIntegerValue ) )
-{
-}
-
 Property::Value::Value( const Vector2& vectorValue )
 : mImpl( new Impl( vectorValue ) )
 {
@@ -342,11 +331,6 @@ Property::Value::Value( Type type )
       mImpl = new Impl( 0 );
       break;
     }
-    case Property::UNSIGNED_INTEGER:
-    {
-      mImpl = new Impl( 0U );
-      break;
-    }
     case Property::VECTOR2:
     {
       mImpl = new Impl( Vector2::ZERO );
@@ -369,7 +353,7 @@ Property::Value::Value( Type type )
     }
     case Property::ROTATION:
     {
-      mImpl = new Impl( Quaternion() );
+      mImpl = new Impl( AngleAxis() );
       break;
     }
     case Property::STRING:
@@ -404,6 +388,7 @@ Property::Value::Value( Type type )
     }
   }
 }
+
 Property::Value::Value( const Property::Value& value )
 : mImpl( NULL )
 {
@@ -445,11 +430,6 @@ Property::Value& Property::Value::operator=( const Property::Value& value )
         mImpl->integerValue = value.mImpl->integerValue;
         break;
       }
-      case Property::UNSIGNED_INTEGER:
-      {
-        mImpl->unsignedIntegerValue = value.mImpl->unsignedIntegerValue;
-        break;
-      }
       case Property::VECTOR2:
       {
         *mImpl->vector2Value = *value.mImpl->vector2Value; // type cannot change in mImpl so vector is allocated
@@ -472,7 +452,7 @@ Property::Value& Property::Value::operator=( const Property::Value& value )
       }
       case Property::ROTATION:
       {
-        *mImpl->quaternionValue = *value.mImpl->quaternionValue; // type cannot change in mImpl so quaternion is allocated
+        *mImpl->angleAxisValue = *value.mImpl->angleAxisValue; // type cannot change in mImpl so quaternion is allocated
         break;
       }
       case Property::STRING:
@@ -526,11 +506,6 @@ Property::Value& Property::Value::operator=( const Property::Value& value )
         newImpl = new Impl( value.mImpl->integerValue );
         break;
       }
-      case Property::UNSIGNED_INTEGER:
-      {
-        newImpl = new Impl( value.mImpl->unsignedIntegerValue );
-        break;
-      }
       case Property::VECTOR2:
       {
         newImpl = new Impl( *value.mImpl->vector2Value ); // type cannot change in mImpl so vector is allocated
@@ -553,7 +528,7 @@ Property::Value& Property::Value::operator=( const Property::Value& value )
       }
       case Property::ROTATION:
       {
-        newImpl = new Impl( *value.mImpl->quaternionValue ); // type cannot change in mImpl so quaternion is allocated
+        newImpl = new Impl( *value.mImpl->angleAxisValue ); // type cannot change in mImpl so quaternion is allocated
         break;
       }
       case Property::MATRIX3:
@@ -621,10 +596,18 @@ bool Property::Value::Get( bool& booleanValue ) const
 bool Property::Value::Get( float& floatValue ) const
 {
   bool converted = false;
-  if( mImpl && mImpl->type == FLOAT )
+  if( mImpl )
   {
-    floatValue = mImpl->floatValue;
-    converted = true;
+    if( mImpl->type == FLOAT )
+    {
+      floatValue = mImpl->floatValue;
+      converted = true;
+    }
+    else if( IsIntegerType( mImpl->type ) )
+    {
+      floatValue = static_cast< float >( mImpl->integerValue );
+      converted = true;
+    }
   }
   return converted;
 }
@@ -632,21 +615,18 @@ bool Property::Value::Get( float& floatValue ) const
 bool Property::Value::Get( int& integerValue ) const
 {
   bool converted = false;
-  if( mImpl && IsIntegerType( mImpl->type ) )
-  {
-    integerValue = mImpl->integerValue;
-    converted = true;
-  }
-  return converted;
-}
-
-bool Property::Value::Get( unsigned int& unsignedIntegerValue ) const
-{
-  bool converted = false;
-  if( mImpl && IsIntegerType( mImpl->type ) )
+  if( mImpl )
   {
-    unsignedIntegerValue = mImpl->unsignedIntegerValue;
-    converted = true;
+    if( IsIntegerType( mImpl->type ) )
+    {
+      integerValue = mImpl->integerValue;
+      converted = true;
+    }
+    else if( mImpl->type == FLOAT )
+    {
+      integerValue = static_cast< int >( mImpl->floatValue );
+      converted = true;
+    }
   }
   return converted;
 }
@@ -654,10 +634,14 @@ bool Property::Value::Get( unsigned int& unsignedIntegerValue ) const
 bool Property::Value::Get( Vector2& vectorValue ) const
 {
   bool converted = false;
-  if( mImpl && (mImpl->type == VECTOR2) ) // type cannot change in mImpl so vector is allocated
+  if( mImpl )
   {
-    vectorValue = *(mImpl->vector2Value);
-    converted = true;
+    // type cannot change in mImpl so vector is allocated
+    if( mImpl->type == VECTOR2 || mImpl->type == VECTOR3 || mImpl->type == VECTOR4 )
+    {
+      vectorValue = *(mImpl->vector2Value); // if Vector3 or 4 only x and y are assigned
+      converted = true;
+    }
   }
   return converted;
 }
@@ -665,10 +649,19 @@ bool Property::Value::Get( Vector2& vectorValue ) const
 bool Property::Value::Get( Vector3& vectorValue ) const
 {
   bool converted = false;
-  if( mImpl && (mImpl->type == VECTOR3) ) // type cannot change in mImpl so vector is allocated
+  if( mImpl )
   {
-    vectorValue = *(mImpl->vector3Value);
-    converted = true;
+    // type cannot change in mImpl so vector is allocated
+    if ( mImpl->type == VECTOR3 || mImpl->type == VECTOR4 )
+    {
+      vectorValue = *(mImpl->vector3Value); // if Vector4 only x,y,z are assigned
+      converted = true;
+    }
+    else if( mImpl->type == VECTOR2 )
+    {
+      vectorValue = *(mImpl->vector2Value);
+      converted = true;
+    }
   }
   return converted;
 }
@@ -676,10 +669,23 @@ bool Property::Value::Get( Vector3& vectorValue ) const
 bool Property::Value::Get( Vector4& vectorValue ) const
 {
   bool converted = false;
-  if( mImpl && (mImpl->type == VECTOR4) ) // type cannot change in mImpl so vector is allocated
+  if( mImpl )
   {
-    vectorValue = *(mImpl->vector4Value);
-    converted = true;
+    if( mImpl->type == VECTOR4 ) // type cannot change in mImpl so vector is allocated
+    {
+      vectorValue = *(mImpl->vector4Value);
+      converted = true;
+    }
+    else if( mImpl->type == VECTOR2 )
+    {
+      vectorValue = *(mImpl->vector2Value);
+      converted = true;
+    }
+    else if( mImpl->type == VECTOR3 )
+    {
+      vectorValue = *(mImpl->vector3Value);
+      converted = true;
+    }
   }
   return converted;
 }
@@ -720,9 +726,9 @@ bool Property::Value::Get( Rect<int>& rectValue ) const
 bool Property::Value::Get( AngleAxis& angleAxisValue ) const
 {
   bool converted = false;
-  if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so quaternion is allocated
+  if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so angleAxis is allocated
   {
-    mImpl->quaternionValue->ToAxisAngle( angleAxisValue.axis, angleAxisValue.angle );
+    angleAxisValue = *(mImpl->angleAxisValue);
     converted = true;
   }
   return converted;
@@ -731,9 +737,9 @@ bool Property::Value::Get( AngleAxis& angleAxisValue ) const
 bool Property::Value::Get( Quaternion& quaternionValue ) const
 {
   bool converted = false;
-  if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so quaternion is allocated
+  if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so angleAxis is allocated
   {
-    quaternionValue = *(mImpl->quaternionValue);
+    quaternionValue = Quaternion(mImpl->angleAxisValue->angle, mImpl->angleAxisValue->axis );
     converted = true;
   }
   return converted;
@@ -815,11 +821,6 @@ std::ostream& operator<<( std::ostream& stream, const Property::Value& value )
          stream << impl.integerValue;
          break;
       }
-      case Dali::Property::UNSIGNED_INTEGER:
-      {
-        stream << impl.unsignedIntegerValue;
-        break;
-      }
       case Dali::Property::VECTOR2:
       {
         stream << *impl.vector2Value;
@@ -852,7 +853,7 @@ std::ostream& operator<<( std::ostream& stream, const Property::Value& value )
       }
       case Dali::Property::ROTATION:
       {
-        stream << *impl.quaternionValue;
+        stream << *impl.angleAxisValue;
         break;
       }
       case Dali::Property::STRING:
@@ -862,12 +863,12 @@ std::ostream& operator<<( std::ostream& stream, const Property::Value& value )
       }
       case Dali::Property::ARRAY:
       {
-        stream << "Array containing" << impl.arrayValue->Count() << " elements"; // TODO add ostream<< operator in array
+        stream << *(value.GetArray());
         break;
       }
       case Dali::Property::MAP:
       {
-        stream << "Map containing " << impl.mapValue->Count() << " elements"; // TODO add ostream<< operator in map
+        stream << *(value.GetMap());
         break;
       }
       case Dali::Property::NONE: