Remove non-public APIs of Animation
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / cluster / cluster-style-impl.cpp
index a71652a..4974ce1 100644 (file)
@@ -1,22 +1,27 @@
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2014 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
-// INTERNAL INCLUDES
-#include <dali/dali.h>
+// CLASS HEADER
 #include <dali-toolkit/internal/controls/cluster/cluster-style-impl.h>
+
+// EXTERNAL INCLUDES
+#include <dali/public-api/animation/animation.h>
+
+// INTERNAL INCLUDES
 #include <dali-toolkit/public-api/controls/cluster/cluster.h>
 
 namespace Dali
@@ -171,110 +176,10 @@ const unsigned int CLUSTER_RANDOM_SEED(0x17eac9f3);         ///< Random seed for
 
 const int STYLE_RANDOM_CHILDREN_NUMBER = 16;
 
-// Constraints
-
-/**
- * First order equation of the form y = Mx + C
- * current' = current * relative + offset
- */
-struct FirstOrderEquationConstraint
-{
-  /**
-   * @param relative The relative multiplier of the source property
-   * @param offset The offset to add onto the result.
-   */
-  FirstOrderEquationConstraint(Vector3 relative, Vector3 offset = Vector3::ZERO)
-  : mRelative(relative),
-    mOffset(offset)
-  {
-  }
-
-  Vector3 operator()(const Vector3&    current,
-                     const PropertyInput& sourceProperty)
-  {
-    const Vector3 source = sourceProperty.GetVector3();
-
-    return source * mRelative + mOffset;
-  }
-
-public:
-
-  Vector3 mRelative;
-  Vector3 mOffset;
-};
-
-/**
- * Depth Constraint.
- * current' = current.xy | + Vector3::ONE.z
- */
-struct DepthConstraint
-{
-  /**
-   * constructor
-   */
-  DepthConstraint()
-  {
-  }
-
-  Vector3 operator()(const Vector3&    current,
-                     const PropertyInput& depthProperty)
-  {
-    Vector3 position(current);
-    position.z = depthProperty.GetFloat();
-    return position;
-  }
-};
-
-
-/**
- * Position Constraint.
- * current' = current * relative + offset
- */
-struct PositionConstraint
-{
-  /**
-   * @param relative The relative multiplier of the source property
-   * @param offset The offset to add onto the result.
-   */
-  PositionConstraint(Vector3 relative, Vector3 offset = Vector3::ZERO)
-  : mRelative(relative),
-    mOffset(offset)
-  {
-  }
-
-  Vector3 operator()(const Vector3&    current,
-                     const PropertyInput& sourceProperty,
-                     const PropertyInput& depthProperty)
-  {
-    const Vector3 source = sourceProperty.GetVector3();
-
-    Vector3 position(source * mRelative + mOffset);
-    position.z += depthProperty.GetFloat();
-    return position;
-  }
-
-public:
-
-  Vector3 mRelative;
-  Vector3 mOffset;
-};
-
-template <class T>
-struct SetConstraint
+Vector3 FirstOrderEquation( const Vector3& source, const Vector3& relative, const Vector3& offset = Vector3::ZERO  )
 {
-  SetConstraint(T value)
-  : mValue(value)
-  {
-
-  }
-
-  T operator()(const T&    current)
-  {
-    return mValue;
-  }
-
-  T mValue;
-};
+  return source * relative + offset;
+}
 
 // random data generator //////////////////////////////////////////////////////
 
@@ -291,11 +196,6 @@ unsigned int genRandom(unsigned int& seed, unsigned int offset)
   return seed;
 }
 
-float genRandomFloat(unsigned int& seed, unsigned int offset)
-{
-  return static_cast<float>(genRandom(seed, offset)) / 0xffffffff;
-}
-
 float genRandomFloat(unsigned int& seed, unsigned int offset, float min, float max)
 {
   const float f = static_cast<float>(genRandom(seed, offset)) / 0xffffffff;
@@ -319,7 +219,8 @@ ClusterStyle::ClusterStyle()
   mTitleSize(Vector3::ONE),
   mBackgroundPositionRelative(Vector3::ONE),
   mBackgroundPositionOffset(Vector3::ZERO),
-  mBackgroundSize(Vector3::ONE)
+  mBackgroundSize(Vector3::ONE),
+  mClusterSize( Vector3::ZERO )
 {
 }
 
@@ -338,6 +239,24 @@ unsigned int ClusterStyle::GetMaximumNumberOfChildren() const
   return mMaxChildren;
 }
 
+void ClusterStyle::ApplyStyleToBackground(Actor background, AlphaFunction alpha, const TimePeriod& durationSeconds)
+{
+  Apply( background,
+         FirstOrderEquation( GetClusterSize(), mBackgroundPositionRelative, mBackgroundPositionOffset ),
+         FirstOrderEquation( GetClusterSize(), mBackgroundSize ),
+         alpha,
+         durationSeconds);
+}
+
+void ClusterStyle::ApplyStyleToTitle(Actor title, AlphaFunction alpha, const TimePeriod& durationSeconds)
+{
+  Apply( title,
+         FirstOrderEquation( GetClusterSize(), mTitlePositionRelative, mTitlePositionOffset ),
+         FirstOrderEquation( GetClusterSize(), mTitleSize ),
+         alpha,
+         durationSeconds);
+}
+
 void ClusterStyle::SetTitleProperties(const Vector3& relativePosition,
                                       const Vector3& offsetPosition,
                                       const Vector3& size)
@@ -356,6 +275,64 @@ void ClusterStyle::SetBackgroundProperties(const Vector3& relativePosition,
   mBackgroundSize = size;
 }
 
+void ClusterStyle::SetClusterSize( const Vector3& clusterSize )
+{
+  mClusterSize = clusterSize;
+}
+
+Vector3 ClusterStyle::GetClusterSize() const
+{
+  return mClusterSize;
+}
+
+void ClusterStyle::Apply( Actor actor,
+                          const Vector3& position,
+                          const Vector3& size,
+                          AlphaFunction alpha,
+                          const TimePeriod& durationSeconds)
+{
+  float animationDuration = durationSeconds.delaySeconds + durationSeconds.durationSeconds;
+  if( animationDuration > 0.f )
+  {
+    Animation animation = Animation::New(animationDuration);
+    animation.AnimateTo( Property( actor, Actor::Property::POSITION ), position, alpha, TimePeriod( durationSeconds.delaySeconds, durationSeconds.durationSeconds ) );
+    animation.AnimateTo( Property( actor, Actor::Property::SIZE ), size, alpha, TimePeriod( durationSeconds.delaySeconds, durationSeconds.durationSeconds ) );
+    animation.Play();
+  }
+  else
+  {
+    actor.SetPosition( position );
+    actor.SetSize( size );
+  }
+}
+
+void ClusterStyle::Apply( Actor actor,
+                          const Vector3& position,
+                          const Vector3& size,
+                          const Quaternion& rotation,
+                          const Vector3& scale,
+                          AlphaFunction alpha,
+                          const TimePeriod& durationSeconds)
+{
+  float animationDuration = durationSeconds.delaySeconds + durationSeconds.durationSeconds;
+  if( animationDuration > 0.f )
+  {
+    Animation animation = Animation::New(animationDuration);
+    animation.AnimateTo( Property( actor, Actor::Property::POSITION ), position, alpha, TimePeriod( durationSeconds.delaySeconds, durationSeconds.durationSeconds ) );
+    animation.AnimateTo( Property( actor, Actor::Property::SIZE ), size, alpha, TimePeriod( durationSeconds.delaySeconds, durationSeconds.durationSeconds ) );
+    animation.AnimateTo( Property( actor, Actor::Property::ORIENTATION ), rotation, alpha, TimePeriod( durationSeconds.delaySeconds, durationSeconds.durationSeconds ) );
+    animation.AnimateTo( Property( actor, Actor::Property::SCALE ), scale, alpha, TimePeriod( durationSeconds.delaySeconds, durationSeconds.durationSeconds ) );
+    animation.Play();
+  }
+  else
+  {
+    actor.SetPosition( position );
+    actor.SetSize( size );
+    actor.SetOrientation( rotation );
+    actor.SetScale( scale );
+  }
+}
+
 // ClusterStyleStandard ///////////////////////////////////////////////////////
 
 ClusterStylePtr ClusterStyleStandard::New(StyleType style)
@@ -450,78 +427,16 @@ void ClusterStyleStandard::ApplyStyle(Actor child, unsigned int index, AlphaFunc
     // counter top-left parent origin and top-left anchor point.
     const Vector3 position = mPositions[index] - Vector3(0.5f, 0.5f, 0.0f) + Vector3(size, size, 0.0f) * 0.5f;
 
-    Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
-                                                      ParentSource( Actor::SIZE ),
-                                                      FirstOrderEquationConstraint(Vector3(position.x, position.y, 0.0f),
-                                                                                   Vector3(0.0f, 0.0f, position.z)) );
-
-    constraint.SetApplyTime(durationSeconds);
-    constraint.SetAlphaFunction(alpha);
-    constraint.SetRemoveAction(Constraint::Bake);
-    child.ApplyConstraint(constraint);
-
-    constraint = Constraint::New<Vector3>( Actor::SIZE,
-                                           ParentSource( Actor::SIZE ),
-                                           FirstOrderEquationConstraint(Vector3::ONE * size) );
-    constraint.SetApplyTime(durationSeconds);
-    constraint.SetAlphaFunction(alpha);
-    constraint.SetRemoveAction(Constraint::Bake);
-    child.ApplyConstraint(constraint);
-
-    constraint = Constraint::New<Quaternion>( Actor::ROTATION,
-                                              SetConstraint<Quaternion>(Quaternion()) );
-    constraint.SetApplyTime(durationSeconds);
-    constraint.SetAlphaFunction(alpha);
-    constraint.SetRemoveAction(Constraint::Bake);
-    child.ApplyConstraint(constraint);
-
-    constraint = Constraint::New<Vector3>( Actor::SCALE,
-                                           SetConstraint<Vector3>(Vector3::ONE) );
-    constraint.SetApplyTime(durationSeconds);
-    constraint.SetAlphaFunction(alpha);
-    constraint.SetRemoveAction(Constraint::Bake);
-    child.ApplyConstraint(constraint);
+    Apply( child,
+           FirstOrderEquation( GetClusterSize(), Vector3(position.x, position.y, 0.0f), Vector3(0.0f, 0.0f, position.z) ),
+           FirstOrderEquation( GetClusterSize(), Vector3::ONE * size ),
+           Quaternion(),
+           Vector3::ONE,
+           alpha,
+           durationSeconds);
   }
 }
 
-void ClusterStyleStandard::ApplyStyleToBackground(Actor background, AlphaFunction alpha, const TimePeriod& durationSeconds)
-{
-  Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
-                                         ParentSource( Actor::SIZE ),
-                                         FirstOrderEquationConstraint(mBackgroundPositionRelative, mBackgroundPositionOffset) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  background.ApplyConstraint(constraint);
-
-  constraint = Constraint::New<Vector3>( Actor::SIZE,
-                                         ParentSource( Actor::SIZE ),
-                                         FirstOrderEquationConstraint(mBackgroundSize) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  background.ApplyConstraint(constraint);
-}
-
-void ClusterStyleStandard::ApplyStyleToTitle(Actor title, AlphaFunction alpha, const TimePeriod& durationSeconds)
-{
-  Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
-                                         ParentSource( Actor::SIZE ),
-                                         FirstOrderEquationConstraint(mTitlePositionRelative, mTitlePositionOffset) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  title.ApplyConstraint(constraint);
-
-  constraint = Constraint::New<Vector3>( Actor::SIZE,
-                                         ParentSource( Actor::SIZE ),
-                                         FirstOrderEquationConstraint(mTitleSize) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  title.ApplyConstraint(constraint);
-}
-
 // ClusterStyleRandom /////////////////////////////////////////////////////////
 
 ClusterStylePtr ClusterStyleRandom::New()
@@ -553,85 +468,15 @@ void ClusterStyleRandom::ApplyStyle(Actor child, unsigned int index, AlphaFuncti
                          0.0f);
 
   Property::Index depthProperty = child.GetPropertyIndex(Toolkit::Cluster::CLUSTER_ACTOR_DEPTH);
-
-  Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
-                                                    ParentSource( Actor::SIZE ),
-                                                    FirstOrderEquationConstraint( Vector3(position.x, position.y, 0.0f),
-                                                                                  Vector3(0.0f, 0.0f, position.z) ) );
-
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  child.ApplyConstraint(constraint);
-
-  // this constraint overrides the Z position. setting it to cluster-actor-depth
-  constraint = Constraint::New<Vector3>( Actor::POSITION,
-                                         LocalSource( depthProperty ),
-                                         DepthConstraint() );
-
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  child.ApplyConstraint(constraint);
-
-  constraint = Constraint::New<Vector3>( Actor::SIZE,
-                                         ParentSource( Actor::SIZE ),
-                                         FirstOrderEquationConstraint(Vector3::ONE * size) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  child.ApplyConstraint(constraint);
-
-  constraint = Constraint::New<Quaternion>( Actor::ROTATION,
-                                            SetConstraint<Quaternion>(Quaternion(rotation, Vector3::ZAXIS)) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  child.ApplyConstraint(constraint);
-
-  constraint = Constraint::New<Vector3>( Actor::SCALE,
-                                         SetConstraint<Vector3>(Vector3::ONE) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  child.ApplyConstraint(constraint);
-}
-
-void ClusterStyleRandom::ApplyStyleToBackground(Actor background, AlphaFunction alpha, const TimePeriod& durationSeconds)
-{
-  Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
-                                         ParentSource( Actor::SIZE ),
-                                         FirstOrderEquationConstraint(mBackgroundPositionRelative, mBackgroundPositionOffset) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  background.ApplyConstraint(constraint);
-
-  constraint = Constraint::New<Vector3>( Actor::SIZE,
-                                         ParentSource( Actor::SIZE ),
-                                         FirstOrderEquationConstraint(mBackgroundSize) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  background.ApplyConstraint(constraint);
-}
-
-void ClusterStyleRandom::ApplyStyleToTitle(Actor title, AlphaFunction alpha, const TimePeriod& durationSeconds)
-{
-  Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
-                                         ParentSource( Actor::SIZE ),
-                                         FirstOrderEquationConstraint(mTitlePositionRelative, mTitlePositionOffset) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  title.ApplyConstraint(constraint);
-
-  constraint = Constraint::New<Vector3>( Actor::SIZE,
-                                         ParentSource( Actor::SIZE ),
-                                         FirstOrderEquationConstraint(mTitleSize) );
-  constraint.SetApplyTime(durationSeconds);
-  constraint.SetAlphaFunction(alpha);
-  constraint.SetRemoveAction(Constraint::Bake);
-  title.ApplyConstraint(constraint);
+  float depthPropertyValue = child.GetProperty<float>( depthProperty );
+
+  Apply( child,
+         FirstOrderEquation( GetClusterSize(), Vector3(position.x, position.y, 0.0f), Vector3(0.0f, 0.0f, depthPropertyValue) ),
+         FirstOrderEquation( GetClusterSize(), Vector3::ONE * size),
+         Quaternion(rotation, Vector3::ZAXIS),
+         Vector3::ONE,
+         alpha,
+         durationSeconds);
 }
 
 } // namespace Internal