Merge "Add KeyFrames APIs" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / key-frames-impl.cpp
index a111c17..bd486e5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2021 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,23 +22,11 @@ namespace Dali
 {
 namespace Internal
 {
-
 KeyFrames* KeyFrames::New()
 {
   return new KeyFrames();
 }
 
-KeyFrames::KeyFrames()
-  : mType(Property::NONE),
-    mKeyFrames(NULL)
-{
-}
-
-KeyFrames::~KeyFrames()
-{
-}
-
-
 void KeyFrames::CreateKeyFramesSpec(Property::Type type)
 {
   mType = type;
@@ -47,42 +35,42 @@ void KeyFrames::CreateKeyFramesSpec(Property::Type type)
   {
     case Property::BOOLEAN:
     {
-      mKeyFrames = Internal::KeyFrameBoolean::New();
+      mKeyFrames = std::make_unique<Internal::KeyFrameBoolean>();
       break;
     }
     case Property::INTEGER:
     {
-      mKeyFrames = Internal::KeyFrameInteger::New();
+      mKeyFrames = std::make_unique<Internal::KeyFrameInteger>();
       break;
     }
     case Property::FLOAT:
     {
-      mKeyFrames = Internal::KeyFrameNumber::New();
+      mKeyFrames = std::make_unique<Internal::KeyFrameNumber>();
       break;
     }
     case Property::VECTOR2:
     {
-      mKeyFrames = Internal::KeyFrameVector2::New();
+      mKeyFrames = std::make_unique<Internal::KeyFrameVector2>();
       break;
     }
     case Property::VECTOR3:
     {
-      mKeyFrames = Internal::KeyFrameVector3::New();
+      mKeyFrames = std::make_unique<Internal::KeyFrameVector3>();
       break;
     }
     case Property::VECTOR4:
     {
-      mKeyFrames = Internal::KeyFrameVector4::New();
+      mKeyFrames = std::make_unique<Internal::KeyFrameVector4>();
       break;
     }
     case Property::ROTATION:
     {
-      mKeyFrames = Internal::KeyFrameQuaternion::New();
+      mKeyFrames = std::make_unique<Internal::KeyFrameQuaternion>();
       break;
     }
     default:
     {
-      DALI_ABORT( "Type not animateable" );
+      DALI_ABORT("Property type is not animatable");
       break;
     }
   }
@@ -93,7 +81,7 @@ Property::Type KeyFrames::GetType() const
   return mType;
 }
 
-void KeyFrames::Add(float time, Property::Value value, AlphaFunction alpha)
+void KeyFrames::Add(float time, const Property::Value& value, AlphaFunction alpha)
 {
   if(mType == Property::NONE)
   {
@@ -101,51 +89,53 @@ void KeyFrames::Add(float time, Property::Value value, AlphaFunction alpha)
   }
 
   // Once we have created a type, can only add values of the same type
-  DALI_ASSERT_ALWAYS( mType == value.GetType() && "Can only add values of the same type to a KeyFrame" );
+  DALI_ASSERT_ALWAYS(mType == value.GetType() && "Can only add values of the same type to a KeyFrame");
+
+  auto keyframes = mKeyFrames.get();
 
-  DALI_ASSERT_DEBUG(mKeyFrames);
+  DALI_ASSERT_DEBUG(keyframes);
 
   switch(mType)
   {
     case Property::BOOLEAN:
     {
-      Internal::KeyFrameBoolean* kf = static_cast<Internal::KeyFrameBoolean*>(mKeyFrames.Get());
+      Internal::KeyFrameBoolean* kf = static_cast<Internal::KeyFrameBoolean*>(keyframes);
       kf->AddKeyFrame(time, value.Get<bool>(), alpha);
       break;
     }
     case Property::INTEGER:
     {
-      Internal::KeyFrameInteger* kf = static_cast<Internal::KeyFrameInteger*>(mKeyFrames.Get());
+      Internal::KeyFrameInteger* kf = static_cast<Internal::KeyFrameInteger*>(keyframes);
       kf->AddKeyFrame(time, value.Get<int>(), alpha);
       break;
     }
     case Property::FLOAT:
     {
-      Internal::KeyFrameNumber* kf = static_cast<Internal::KeyFrameNumber*>(mKeyFrames.Get());
+      Internal::KeyFrameNumber* kf = static_cast<Internal::KeyFrameNumber*>(keyframes);
       kf->AddKeyFrame(time, value.Get<float>(), alpha);
       break;
     }
     case Property::VECTOR2:
     {
-      Internal::KeyFrameVector2* kf = static_cast<Internal::KeyFrameVector2*>(mKeyFrames.Get());
+      Internal::KeyFrameVector2* kf = static_cast<Internal::KeyFrameVector2*>(keyframes);
       kf->AddKeyFrame(time, value.Get<Vector2>(), alpha);
       break;
     }
     case Property::VECTOR3:
     {
-      Internal::KeyFrameVector3* kf = static_cast<Internal::KeyFrameVector3*>(mKeyFrames.Get());
+      Internal::KeyFrameVector3* kf = static_cast<Internal::KeyFrameVector3*>(keyframes);
       kf->AddKeyFrame(time, value.Get<Vector3>(), alpha);
       break;
     }
     case Property::VECTOR4:
     {
-      Internal::KeyFrameVector4* kf = static_cast<Internal::KeyFrameVector4*>(mKeyFrames.Get());
+      Internal::KeyFrameVector4* kf = static_cast<Internal::KeyFrameVector4*>(keyframes);
       kf->AddKeyFrame(time, value.Get<Vector4>(), alpha);
       break;
     }
     case Property::ROTATION:
     {
-      Internal::KeyFrameQuaternion* kf = static_cast<Internal::KeyFrameQuaternion*>(mKeyFrames.Get());
+      Internal::KeyFrameQuaternion* kf = static_cast<Internal::KeyFrameQuaternion*>(keyframes);
       kf->AddKeyFrame(time, value.Get<Quaternion>(), alpha);
       break;
     }
@@ -157,21 +147,35 @@ void KeyFrames::Add(float time, Property::Value value, AlphaFunction alpha)
 
 KeyFrameSpec* KeyFrames::GetKeyFramesBase() const
 {
-  return mKeyFrames.Get();
+  return mKeyFrames.get();
 }
 
 Property::Value KeyFrames::GetLastKeyFrameValue() const
 {
   Property::Value value;
 
-  unsigned int noOfKeyFrames = mKeyFrames->GetNumberOfKeyFrames();
-  if( noOfKeyFrames )
+  std::size_t noOfKeyFrames = mKeyFrames->GetNumberOfKeyFrames();
+  if(noOfKeyFrames)
   {
-    mKeyFrames->GetKeyFrameAsValue( noOfKeyFrames - 1, value );
+    float time;
+    mKeyFrames->GetKeyFrameAsValue(noOfKeyFrames - 1, time, value);
   }
 
   return value;
 }
 
-} // Internal
-} // Dali
+std::size_t KeyFrames::GetKeyFrameCount() const
+{
+  return mKeyFrames->GetNumberOfKeyFrames();
+}
+
+void KeyFrames::GetKeyFrame(std::size_t index, float& time, Property::Value& value) const
+{
+  if(index < mKeyFrames->GetNumberOfKeyFrames())
+  {
+    mKeyFrames->GetKeyFrameAsValue(index, time, value);
+  }
+}
+
+} // namespace Internal
+} // namespace Dali