Revert "[Tizen] Implement partial update"
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / transform-manager.cpp
index b289bf4..c2cc4bb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
 //EXTERNAL INCLUDES
 #include <algorithm>
 #include <cstring>
+#include <type_traits>
 
 //INTERNAL INCLUDES
 #include <dali/public-api/common/constants.h>
-#include <dali/public-api/common/compile-time-assert.h>
 #include <dali/internal/common/math.h>
 
 namespace Dali
@@ -42,12 +42,41 @@ namespace
 static const float gDefaultTransformComponentAnimatableData[] = { 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f };
 
 //Default values for anchor point (CENTER) and parent origin (TOP_LEFT)
-static const float gDefaultTransformComponentStaticData[] = { 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.5f };
-
-DALI_COMPILE_TIME_ASSERT( sizeof(gDefaultTransformComponentAnimatableData) == sizeof(TransformComponentAnimatable) );
-DALI_COMPILE_TIME_ASSERT( sizeof(gDefaultTransformComponentStaticData) == sizeof(TransformComponentStatic) );
+static const float gDefaultTransformComponentStaticData[] = { 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.5f, true };
+
+static_assert( sizeof(gDefaultTransformComponentAnimatableData) == sizeof(TransformComponentAnimatable), "gDefaultTransformComponentAnimatableData should have the same number of floats as specified in TransformComponentAnimatable" );
+static_assert( sizeof(gDefaultTransformComponentStaticData) == sizeof(TransformComponentStatic), "gDefaultTransformComponentStaticData should have the same number of floats as specified in TransformComponentStatic" );
+
+/**
+ * @brief Calculates the center position for the transform component
+ * @param[out] centerPosition The calculated center-position of the transform component
+ * @param[in] transformComponentStatic A const reference to the static component transform struct
+ * @param[in] transformComponentAnimatable A const reference to the animatable component transform struct
+ * @param[in] size The size of the current transform component
+ * @param[in] half Halfway point of the transform
+ * @param[in] topLeft The top-left coords of the transform
+ */
+inline void CalculateCenterPosition(
+  Vector3& centerPosition,
+  const TransformComponentStatic& transformComponentStatic,
+  const TransformComponentAnimatable& transformComponentAnimatable,
+  const Vector3& size,
+  const Vector3& half,
+  const Vector3& topLeft )
+{
+  // Calculate the center-point by applying the scale and rotation on the anchor point.
+  centerPosition = ( half - transformComponentStatic.mAnchorPoint ) * size * transformComponentAnimatable.mScale;
+  centerPosition *= transformComponentAnimatable.mOrientation;
+
+  // If the position is ignoring the anchor-point, then remove the anchor-point shift from the position.
+  if( ! transformComponentStatic.mPositionUsesAnchorPoint )
+  {
+    centerPosition -= ( topLeft - transformComponentStatic.mAnchorPoint ) * size;
+  }
 }
 
+} // unnamed namespace
+
 TransformManager::TransformManager()
 :mComponentCount(0),
  mReorder(false)
@@ -104,7 +133,7 @@ void TransformManager::RemoveTransform(TransformId id)
 {
   //Move the last element to the gap
   mComponentCount--;
-  unsigned int index = mIds[id];
+  TransformId index = mIds[id];
   mTxComponentAnimatable[index] = mTxComponentAnimatable[mComponentCount];
   mTxComponentStatic[index] = mTxComponentStatic[mComponentCount];
   mInheritanceMode[index] = mInheritanceMode[mComponentCount];
@@ -129,7 +158,7 @@ void TransformManager::RemoveTransform(TransformId id)
 void TransformManager::SetParent( TransformId id, TransformId parentId )
 {
   DALI_ASSERT_ALWAYS( id != parentId );
-  unsigned int index = mIds[id];
+  TransformId index = mIds[id];
   mParent[ index ] = parentId;
   mComponentDirty[ index ] = true;
   mReorder = true;
@@ -147,7 +176,7 @@ Matrix& TransformManager::GetWorldMatrix( TransformId id )
 
 void TransformManager::SetInheritPosition( TransformId id, bool inherit )
 {
-  unsigned int index = mIds[id];
+  TransformId index = mIds[id];
   if( inherit )
   {
     mInheritanceMode[ index ] |= INHERIT_POSITION;
@@ -162,7 +191,7 @@ void TransformManager::SetInheritPosition( TransformId id, bool inherit )
 
 void TransformManager::SetInheritScale( TransformId id, bool inherit )
 {
-  unsigned int index = mIds[id];
+  TransformId index = mIds[id];
   if( inherit )
   {
     mInheritanceMode[ index ] |= INHERIT_SCALE;
@@ -177,7 +206,7 @@ void TransformManager::SetInheritScale( TransformId id, bool inherit )
 
 void TransformManager::SetInheritOrientation( TransformId id, bool inherit )
 {
-  unsigned int index = mIds[id];
+  TransformId index = mIds[id];
   if( inherit )
   {
     mInheritanceMode[ index ] |= INHERIT_ORIENTATION;
@@ -211,25 +240,24 @@ void TransformManager::Update()
   }
 
   //Iterate through all components to compute its world matrix
-  Vector3 anchorPosition;
+  Vector3 centerPosition;
   Vector3 localPosition;
-  Vector3 half( 0.5f,0.5f,0.5f );
+  const Vector3 half( 0.5f,0.5f,0.5f );
+  const Vector3 topLeft( 0.0f, 0.0f, 0.5f );
   for( unsigned int i(0); i<mComponentCount; ++i )
   {
     if( DALI_LIKELY( mInheritanceMode[i] != DONT_INHERIT_TRANSFORM && mParent[i] != INVALID_TRANSFORM_ID ) )
     {
-      const unsigned int& parentIndex = mIds[mParent[i] ];
+      const TransformId& parentIndex = mIds[mParent[i] ];
       if( DALI_LIKELY( mInheritanceMode[i] == INHERIT_ALL ) )
       {
         if( mComponentDirty[i] || mLocalMatrixDirty[parentIndex])
         {
           //Full transform inherited
           mLocalMatrixDirty[i] = true;
-
-          anchorPosition = ( half - mTxComponentStatic[i].mAnchorPoint ) * mSize[i] * mTxComponentAnimatable[i].mScale;
-          anchorPosition *= mTxComponentAnimatable[i].mOrientation;
-          localPosition = mTxComponentAnimatable[i].mPosition + anchorPosition + ( mTxComponentStatic[i].mParentOrigin - half ) *  mSize[parentIndex];
-          mLocal[i].SetTransformComponents( mTxComponentAnimatable[i].mScale,mTxComponentAnimatable[i].mOrientation, localPosition );
+          CalculateCenterPosition( centerPosition, mTxComponentStatic[ i ], mTxComponentAnimatable[ i ], mSize[ i ], half, topLeft );
+          localPosition = mTxComponentAnimatable[i].mPosition + centerPosition + ( mTxComponentStatic[i].mParentOrigin - half ) *  mSize[parentIndex];
+          mLocal[i].SetTransformComponents( mTxComponentAnimatable[i].mScale, mTxComponentAnimatable[i].mOrientation, localPosition );
         }
 
         //Update the world matrix
@@ -261,15 +289,15 @@ void TransformManager::Update()
         if( (mInheritanceMode[i] & INHERIT_POSITION) == 0 )
         {
           //Don't inherit position
+          CalculateCenterPosition( centerPosition, mTxComponentStatic[ i ], mTxComponentAnimatable[ i ], mSize[ i ], half, topLeft );
           mLocal[i].SetTransformComponents( localScale, localOrientation, Vector3::ZERO );
           Matrix::Multiply( mWorld[i], mLocal[i], parentMatrix );
-          mWorld[i].SetTranslation( mTxComponentAnimatable[i].mPosition);
+          mWorld[i].SetTranslation( mTxComponentAnimatable[i].mPosition + centerPosition );
         }
         else
         {
-          anchorPosition = ( half - mTxComponentStatic[i].mAnchorPoint ) * mSize[i] * mTxComponentAnimatable[i].mScale;
-          anchorPosition *= mTxComponentAnimatable[i].mOrientation;
-          localPosition = mTxComponentAnimatable[i].mPosition + anchorPosition + ( mTxComponentStatic[i].mParentOrigin - half ) *  mSize[parentIndex];
+          CalculateCenterPosition( centerPosition, mTxComponentStatic[ i ], mTxComponentAnimatable[ i ], mSize[ i ], half, topLeft );
+          localPosition = mTxComponentAnimatable[i].mPosition + centerPosition + ( mTxComponentStatic[i].mParentOrigin - half ) *  mSize[parentIndex];
           mLocal[i].SetTransformComponents( localScale, localOrientation, localPosition );
           Matrix::Multiply( mWorld[i], mLocal[i], parentMatrix );
         }
@@ -279,9 +307,8 @@ void TransformManager::Update()
     }
     else  //Component has no parent or doesn't inherit transform
     {
-      anchorPosition = ( half - mTxComponentStatic[i].mAnchorPoint ) * mSize[i] * mTxComponentAnimatable[i].mScale;
-      anchorPosition *= mTxComponentAnimatable[i].mOrientation;
-      localPosition = mTxComponentAnimatable[i].mPosition + anchorPosition;
+      CalculateCenterPosition( centerPosition, mTxComponentStatic[ i ], mTxComponentAnimatable[ i ], mSize[ i ], half, topLeft );
+      localPosition = mTxComponentAnimatable[i].mPosition + centerPosition;
       mLocal[i].SetTransformComponents( mTxComponentAnimatable[i].mScale, mTxComponentAnimatable[i].mOrientation, localPosition );
       mWorld[i] = mLocal[i];
       mLocalMatrixDirty[i] = true;
@@ -323,7 +350,7 @@ void TransformManager::ReorderComponents()
   mOrderedComponents.Resize(mComponentCount);
 
   TransformId parentId;
-  for( size_t i(0); i<mComponentCount; ++i )
+  for( TransformId i = 0; i<mComponentCount; ++i )
   {
     mOrderedComponents[i].id = mComponentId[i];
     mOrderedComponents[i].level = 0u;
@@ -337,8 +364,8 @@ void TransformManager::ReorderComponents()
   }
 
   std::stable_sort( mOrderedComponents.Begin(), mOrderedComponents.End());
-  unsigned int previousIndex = 0;
-  for( size_t newIndex(0); newIndex<mComponentCount-1; ++newIndex )
+  TransformId previousIndex = 0;
+  for( TransformId newIndex = 0; newIndex < mComponentCount-1; ++newIndex )
   {
     previousIndex = mIds[mOrderedComponents[newIndex].id];
     if( previousIndex != newIndex )
@@ -354,31 +381,31 @@ Vector3& TransformManager::GetVector3PropertyValue( TransformId id, TransformMan
   {
     case TRANSFORM_PROPERTY_POSITION:
     {
-      unsigned int index( mIds[id] );
+      TransformId index( mIds[id] );
       mComponentDirty[ index ] = true;
       return mTxComponentAnimatable[ index ].mPosition;
     }
     case TRANSFORM_PROPERTY_SCALE:
     {
-      unsigned int index( mIds[id] );
+      TransformId index( mIds[id] );
       mComponentDirty[ index ] = true;
       return mTxComponentAnimatable[ index ].mScale;
     }
     case TRANSFORM_PROPERTY_PARENT_ORIGIN:
     {
-      unsigned int index( mIds[id] );
+      TransformId index( mIds[id] );
       mComponentDirty[ index ] = true;
       return mTxComponentStatic[ index ].mParentOrigin;
     }
     case TRANSFORM_PROPERTY_ANCHOR_POINT:
     {
-      unsigned int index( mIds[id] );
+      TransformId index( mIds[id] );
       mComponentDirty[ index ] = true;
       return mTxComponentStatic[ index ].mAnchorPoint;
     }
     case TRANSFORM_PROPERTY_SIZE:
     {
-      unsigned int index( mIds[id] );
+      TransformId index( mIds[id] );
       mComponentDirty[ index ] = true;
       return mSize[ index ];
     }
@@ -456,7 +483,7 @@ const float& TransformManager::GetVector3PropertyComponentValue(TransformId id,
 
 void TransformManager::SetVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mComponentDirty[ index ] = true;
 
   switch( property )
@@ -495,7 +522,7 @@ void TransformManager::SetVector3PropertyValue( TransformId id, TransformManager
 
 void TransformManager::SetVector3PropertyComponentValue( TransformId id, TransformManagerProperty property, float value, unsigned int component )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mComponentDirty[ index ] = true;
 
   switch( property )
@@ -534,7 +561,7 @@ void TransformManager::SetVector3PropertyComponentValue( TransformId id, Transfo
 
 void TransformManager::BakeVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mComponentDirty[ index ] = true;
 
   switch( property )
@@ -573,7 +600,7 @@ void TransformManager::BakeVector3PropertyValue( TransformId id, TransformManage
 
 void TransformManager::BakeRelativeVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mComponentDirty[ index ] = true;
 
   switch( property )
@@ -612,7 +639,7 @@ void TransformManager::BakeRelativeVector3PropertyValue( TransformId id, Transfo
 
 void TransformManager::BakeMultiplyVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mComponentDirty[ index ] = true;
 
   switch( property )
@@ -651,7 +678,7 @@ void TransformManager::BakeMultiplyVector3PropertyValue( TransformId id, Transfo
 
 void TransformManager::BakeVector3PropertyComponentValue( TransformId id, TransformManagerProperty property, float value, unsigned int component )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mComponentDirty[ index ] = true;
 
   switch( property )
@@ -690,7 +717,7 @@ void TransformManager::BakeVector3PropertyComponentValue( TransformId id, Transf
 
 void TransformManager::BakeXVector3PropertyValue( TransformId id, TransformManagerProperty property, float value )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mComponentDirty[ index ] = true;
 
   switch( property )
@@ -729,7 +756,7 @@ void TransformManager::BakeXVector3PropertyValue( TransformId id, TransformManag
 
 void TransformManager::BakeYVector3PropertyValue( TransformId id, TransformManagerProperty property, float value )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mComponentDirty[ index ] = true;
 
   switch( property )
@@ -768,7 +795,7 @@ void TransformManager::BakeYVector3PropertyValue( TransformId id, TransformManag
 
 void TransformManager::BakeZVector3PropertyValue( TransformId id, TransformManagerProperty property, float value )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mComponentDirty[ index ] = true;
 
   switch( property )
@@ -807,7 +834,7 @@ void TransformManager::BakeZVector3PropertyValue( TransformId id, TransformManag
 
 Quaternion& TransformManager::GetQuaternionPropertyValue( TransformId id )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mComponentDirty[ index ] = true;
   return mTxComponentAnimatable[ index ].mOrientation;
 }
@@ -819,21 +846,21 @@ const Quaternion& TransformManager::GetQuaternionPropertyValue( TransformId id )
 
 void TransformManager::SetQuaternionPropertyValue( TransformId id, const Quaternion& q )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mTxComponentAnimatable[ index ].mOrientation = q;
   mComponentDirty[ index ] = true;
 }
 
 void TransformManager::BakeQuaternionPropertyValue( TransformId id, const Quaternion& q )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mTxComponentAnimatable[ index ].mOrientation = mTxComponentAnimatableBaseValue[index].mOrientation = q;
   mComponentDirty[ index ] = true;
 }
 
 void TransformManager::BakeRelativeQuaternionPropertyValue( TransformId id, const Quaternion& q )
 {
-  unsigned int index( mIds[id] );
+  TransformId index( mIds[id] );
   mTxComponentAnimatable[ index ].mOrientation = mTxComponentAnimatableBaseValue[index].mOrientation = mTxComponentAnimatable[ index ].mOrientation * q;
   mComponentDirty[ index ] = true;
 }
@@ -845,11 +872,18 @@ const Vector4& TransformManager::GetBoundingSphere( TransformId id ) const
 
 void TransformManager::GetWorldMatrixAndSize( TransformId id, Matrix& worldMatrix, Vector3& size ) const
 {
-  unsigned int index = mIds[id];
+  TransformId index = mIds[id];
   worldMatrix = mWorld[index];
   size = mSize[index];
 }
 
+void TransformManager::SetPositionUsesAnchorPoint( TransformId id, bool value )
+{
+  TransformId index( mIds[ id ] );
+  mComponentDirty[ index ] = true;
+  mTxComponentStatic[ index ].mPositionUsesAnchorPoint = value;
+}
+
 } //namespace SceneGraph
 } //namespace Internal
 } //namespace Dali