Alpha function changes
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / page-turn-view / page-turn-view-impl.cpp
index 078be1c..6ede563 100644 (file)
@@ -1,31 +1,42 @@
-//
-// 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.
+ *
+ */
 
 // CLASS HEADER
 #include <dali-toolkit/internal/controls/page-turn-view/page-turn-view-impl.h>
 
+// EXTERNAL INCLUDES
+#include <dali/public-api/animation/animation.h>
+#include <dali/public-api/animation/constraint.h>
+#include <dali/public-api/events/hit-test-algorithm.h>
+#include <dali/public-api/object/type-registry.h>
+#include <dali/public-api/object/type-registry-helper.h>
+#include <dali/public-api/render-tasks/render-task-list.h>
+
 // INTERNAL INCLUDES
 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
 
 using namespace Dali;
 
-namespace //unnamed namespace
+namespace //Unnamed namespace
 {
 // To register type
-TypeRegistration mType( typeid(Toolkit::PageTurnView), typeid(Toolkit::Control), NULL );
+
+DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PageTurnView, Toolkit::Control, NULL )
+DALI_TYPE_REGISTRATION_END()
 
 // default grid density for page turn effect, 10 pixels by 10 pixels
 const float DEFAULT_GRID_DENSITY(10.0f);
@@ -68,17 +79,17 @@ struct OriginalCenterConstraint
     mDirection = offset  / mDistance;
   }
 
-  Vector2 operator()(const Vector2& current, const PropertyInput& panDisplacement)
+  void operator()( Vector2& current, const PropertyInputContainer& inputs )
   {
-    float displacement = panDisplacement.GetFloat();
+    float displacement = inputs[0]->GetFloat();
 
     if( displacement < mDistance )
     {
-      return mOldCenter + mDirection * displacement;
+      current = mOldCenter + mDirection * displacement;
     }
     else
     {
-      return mNewCenter + Vector2(0.25f*(displacement-mDistance), 0.f);
+      current = mNewCenter + Vector2(0.25f*(displacement-mDistance), 0.f);
     }
   }
 
@@ -102,22 +113,21 @@ struct RotationConstraint
     mStep = 1.f / pageWidth;
     mSign = isTurnBack ? -1.0f : 1.0f;
     mConst = isTurnBack ? -1.0f : 0.f;
-    mRotation = isTurnBack ? Quaternion( -Math::PI, Vector3::YAXIS ) : Quaternion( 0.f, Vector3::YAXIS );
+    mRotation = isTurnBack ? Quaternion( Radian( -Math::PI ), Vector3::YAXIS ) : Quaternion( Radian(0.f), Vector3::YAXIS );
   }
 
-  Quaternion operator()( const Quaternion& current, const PropertyInput& panDisplacement )
+  void operator()( Quaternion& current, const PropertyInputContainer& inputs )
   {
-    float displacement = panDisplacement.GetFloat();
-    float angle;
+    float displacement = inputs[0]->GetFloat();
     if( displacement < mDistance)
     {
-      return mRotation;
+      current = mRotation;
     }
     else
     {
       float coef = std::max(-1.0f, mStep*(mDistance-displacement));
-      angle = Math::PI*( mConst + mSign*coef );
-      return Quaternion( angle, Vector3::YAXIS );
+      float angle = Math::PI * ( mConst + mSign * coef );
+      current = Quaternion( Radian( angle ), Vector3::YAXIS );
     }
   }
 
@@ -136,22 +146,23 @@ struct RotationConstraint
  */
 struct CurrentCenterConstraint
 {
-  CurrentCenterConstraint( float pageWidth)
+  CurrentCenterConstraint( float pageWidth )
   : mPageWidth( pageWidth )
   {
     mThres = pageWidth * PAGE_TURN_OVER_THRESHOLD_RATIO * 0.5f;
   }
 
-  Vector2 operator()( const Vector2& current, const PropertyInput& center, const PropertyInput& originalCenter )
+  void operator()( Vector2& current, const PropertyInputContainer& inputs )
   {
-    Vector2 centerPosition = center.GetVector2();
+    const Vector2& centerPosition = inputs[0]->GetVector2();
     if( centerPosition.x > 0.f )
     {
-      return Vector2( mThres+centerPosition.x*0.5f , centerPosition.y);
+      current.x = mThres+centerPosition.x * 0.5f;
+      current.y = centerPosition.y;
     }
     else
     {
-      Vector2 centerOrigin = originalCenter.GetVector2();
+      const Vector2& centerOrigin = inputs[1]->GetVector2();
       Vector2 direction = centerOrigin - Vector2(mThres, centerPosition.y);
       float coef = 1.f+(centerPosition.x*2.f / mPageWidth);
       // Todo: when coef <= 0, the page is flat, slow down the last moment of the page stretch by 10 times to avoid a small bounce
@@ -159,7 +170,7 @@ struct CurrentCenterConstraint
       {
         coef = (coef+0.225f)/10.0f;
       }
-      return centerOrigin - direction * coef;
+      current = centerOrigin - direction * coef;
     }
   }
 
@@ -173,14 +184,13 @@ struct ShadowBlurStrengthConstraint
   : mThres( thres )
   {}
 
-  float operator()( const float current,  const PropertyInput& currentCenter, const PropertyInput& originalCenter, const PropertyInput& panDisplacement)
+  void operator()( float& blurStrength,  const PropertyInputContainer& inputs )
   {
-    float displacement = panDisplacement.GetFloat();
-    float blurStrength;
+    float displacement = inputs[2]->GetFloat();
     if( EqualsZero(displacement))
     {
-      Vector2 cur = currentCenter.GetVector2();
-      Vector2 ori = originalCenter.GetVector2();
+      const Vector2& cur = inputs[0]->GetVector2();
+      const Vector2& ori = inputs[1]->GetVector2();
       blurStrength =  5.f*(ori-cur).Length() / mThres;
     }
     else
@@ -189,7 +199,6 @@ struct ShadowBlurStrengthConstraint
     }
 
     blurStrength = blurStrength > 1.f ? 1.f : ( blurStrength < 0.f ? 0.f : blurStrength );
-    return blurStrength;
   }
 
   float mThres;
@@ -250,16 +259,22 @@ const int PageTurnView::NUMBER_OF_CACHED_PAGES = NUMBER_OF_CACHED_PAGES_EACH_SID
 const float PageTurnView::STATIC_PAGE_INTERVAL_DISTANCE = 1.0f;
 
 PageTurnView::PageTurnView( PageFactory& pageFactory, const Vector2& pageSize )
-: ControlImpl( true ),
+: Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
   mPageFactory( pageFactory ),
   mPageSize( pageSize ),
+  mTotalPageCount( 0 ),
   mIsEditMode( false ),
+  mNeedOffscreenRendering( false ),
   mPanning( false ),
   mSpineShadowParameter( DEFAULT_SPINE_SHADOW_PARAMETER ),
   mCurrentPageIndex( 0 ),
   mIndex( 0 ),
   mPress( false ),
   mPageUpdated( true ),
+  mDistanceUpCorner( 0.f ),
+  mDistanceBottomCorner( 0.f ),
+  mPanDisplacement( 0.f ),
+  mConstraints( false ),
   mPageTurnStartedSignal(),
   mPageTurnFinishedSignal(),
   mPagePanStartedSignal(),
@@ -296,7 +311,7 @@ void PageTurnView::OnInitialize()
   for( int i = 0; i < MAXIMUM_TURNING_NUM; i++ )
   {
     mTurnEffect[i] = Toolkit::PageTurnEffect::New( false );
-    mTurnEffect[i].SetProperty( ShaderEffect::GRID_DENSITY, Property::Value( DEFAULT_GRID_DENSITY ) );
+    mTurnEffect[i].SetProperty( ShaderEffect::Property::GRID_DENSITY, Property::Value( DEFAULT_GRID_DENSITY ) );
     mTurnEffect[i].SetPageSize( mPageSize );
     mTurnEffect[i].SetShadowWidth(0.f);
     mTurnEffect[i].SetSpineShadowParameter( mSpineShadowParameter );
@@ -350,7 +365,6 @@ void PageTurnView::SetupRenderTasks()
   mCameraActor.SetParentOrigin(ParentOrigin::CENTER);
   mCameraActor.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
   mCameraActor.SetInheritScale( false );
-  mCameraActor.SetInvertYAxis(false);
   Self().Add(mCameraActor);
 
   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
@@ -364,7 +378,7 @@ void PageTurnView::SetupRenderTasks()
     Self().Add( mPageSourceActor[i] );
     mPageSourceActor[i].SetSensitive( false );
 
-    mRenderedPage[i] = FrameBufferImage::New( mControlSize.width, mControlSize.height, Pixel::RGB8888, Image::Unused );
+    mRenderedPage[i] = FrameBufferImage::New( mControlSize.width, mControlSize.height, Pixel::RGB8888, Image::UNUSED );
     mOffscreenTask[i] = taskList.CreateTask();
     mOffscreenTask[i].SetRefreshRate( RenderTask::REFRESH_ONCE );
     mOffscreenTask[i].SetCameraActor(mCameraActor);
@@ -654,7 +668,7 @@ void PageTurnView::RefreshCurrentPage()
   RenderPage( mCurrentPageIndex );
 }
 
-void PageTurnView::OnPan( PanGesture gesture )
+void PageTurnView::OnPan( const PanGesture& gesture )
 {
   if( mIsEditMode )
   {
@@ -790,12 +804,12 @@ void PageTurnView::PanContinuing( const Vector2& gesturePosition )
       mShadowView.RemoveConstraints();
       Actor self = Self();
       self.SetProperty( mPropertyPanDisplacement[mIndex], 0.f );
-      Constraint shadowBlurStrengthConstraint = Constraint::New<float>( mShadowView.GetBlurStrengthPropertyIndex(),
-                                                                        Source(mTurnEffect[mIndex],  mTurnEffect[mIndex].GetPropertyIndex(mTurnEffect[mIndex].PageTurnEffect::GetCurrentCenterPropertyName())),
-                                                                        Source(mTurnEffect[mIndex],  mTurnEffect[mIndex].GetPropertyIndex(mTurnEffect[mIndex].PageTurnEffect::GetOriginalCenterPropertyName())),
-                                                                        Source( self, mPropertyPanDisplacement[mIndex] ),
-                                                                        ShadowBlurStrengthConstraint( mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO ) );
-      mShadowView.ApplyConstraint( shadowBlurStrengthConstraint  );
+
+      Constraint shadowBlurStrengthConstraint = Constraint::New<float>( mShadowView, mShadowView.GetBlurStrengthPropertyIndex(), ShadowBlurStrengthConstraint( mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO ) );
+      shadowBlurStrengthConstraint.AddSource( Source(mTurnEffect[mIndex],  mTurnEffect[mIndex].GetPropertyIndex(mTurnEffect[mIndex].PageTurnEffect::GetCurrentCenterPropertyName())) );
+      shadowBlurStrengthConstraint.AddSource( Source(mTurnEffect[mIndex],  mTurnEffect[mIndex].GetPropertyIndex(mTurnEffect[mIndex].PageTurnEffect::GetOriginalCenterPropertyName())) );
+      shadowBlurStrengthConstraint.AddSource( Source( self, mPropertyPanDisplacement[mIndex] ) );
+      shadowBlurStrengthConstraint.Apply();
     }
   }
   else
@@ -857,28 +871,24 @@ void PageTurnView::PanContinuing( const Vector2& gesturePosition )
                    /( offset.x*offset.x + offset.y*offset.y );
         offset *= k;
         Actor self = Self();
-        Source source(self, mPropertyPanDisplacement[mIndex]);
 
         Property::Index shaderOriginalCenterPropertyIndex = mTurnEffect[mIndex].GetPropertyIndex(mTurnEffect[mIndex].PageTurnEffect::GetOriginalCenterPropertyName());
-        Constraint originalCenterConstraint = Constraint::New<Vector2>( shaderOriginalCenterPropertyIndex ,
-                                                                        source,
-                                                                        OriginalCenterConstraint( mOriginalCenter, offset ));
-        mTurnEffect[mIndex].ApplyConstraint( originalCenterConstraint );
+        Constraint originalCenterConstraint = Constraint::New<Vector2>( mTurnEffect[mIndex], shaderOriginalCenterPropertyIndex, OriginalCenterConstraint( mOriginalCenter, offset ));
+        originalCenterConstraint.AddSource( Source( self, mPropertyPanDisplacement[mIndex] ) );
+        originalCenterConstraint.Apply();
 
         Property::Index shaderCurrentCenterPropertyIndex = mTurnEffect[mIndex].GetPropertyIndex(mTurnEffect[mIndex].PageTurnEffect::GetCurrentCenterPropertyName());
-        Constraint currentCenterConstraint = Constraint::New<Vector2>( shaderCurrentCenterPropertyIndex,
-                                                                       Source(self, mPropertyCurrentCenter[mIndex]),
-                                                                       Source(mTurnEffect[mIndex], shaderOriginalCenterPropertyIndex),
-                                                                       CurrentCenterConstraint(mPageSize.width));
-        mTurnEffect[mIndex].ApplyConstraint( currentCenterConstraint );
+        Constraint currentCenterConstraint = Constraint::New<Vector2>( mTurnEffect[mIndex], shaderCurrentCenterPropertyIndex, CurrentCenterConstraint(mPageSize.width));
+        currentCenterConstraint.AddSource( Source(self, mPropertyCurrentCenter[mIndex]) );
+        currentCenterConstraint.AddSource( Source(mTurnEffect[mIndex], shaderOriginalCenterPropertyIndex) );
+        currentCenterConstraint.Apply();
 
         GetImpl( mTurnEffect[mIndex] ).ApplyInternalConstraint();
 
         float distance = offset.Length();
-        Constraint rotationConstraint = Constraint::New<Quaternion>( Actor::ROTATION,
-                                                                     Source( self, mPropertyPanDisplacement[mIndex] ),
-                                                                     RotationConstraint(distance, mPageSize.width, mIsTurnBack[mPanActor]));
-        mPanActor.ApplyConstraint( rotationConstraint );
+        Constraint rotationConstraint = Constraint::New<Quaternion>( mPanActor, Actor::Property::ORIENTATION, RotationConstraint(distance, mPageSize.width, mIsTurnBack[mPanActor]));
+        rotationConstraint.AddSource( Source( self, mPropertyPanDisplacement[mIndex] ) );
+        rotationConstraint.Apply();
 
         mConstraints = false;
       }
@@ -917,7 +927,7 @@ void PageTurnView::PanFinished( const Vector2& gesturePosition, float gestureSpe
 
   mPagePanFinishedSignal.Emit( handle );
 
-  Actor actor = mPanActor;
+  ImageActor actor = mPanActor;
   if(mPress)
   {
     if(!mConstraints) // if with constraints, the pan finished position is near spine, set up an animation to turn the page over
@@ -943,9 +953,9 @@ void PageTurnView::PanFinished( const Vector2& gesturePosition, float gestureSpe
       float width = mPageSize.width*(1.f+PAGE_TURN_OVER_THRESHOLD_RATIO);
       Animation animation = Animation::New( std::max(0.1f,PAGE_TURN_OVER_ANIMATION_DURATION * (1.0f - mPanDisplacement / width)) );
       animation.AnimateTo( Property(self, mPropertyPanDisplacement[mIndex]),
-                           width,AlphaFunctions::EaseOutSine33);
+                           width,AlphaFunction::EASE_OUT_SINE);
       animation.AnimateTo( Property(self, mPropertyCurrentCenter[mIndex]),
-                           Vector2(-mPageSize.width, 0.5f*mPageSize.height), AlphaFunctions::EaseOutSine33);
+                           Vector2(-mPageSize.width, 0.5f*mPageSize.height), AlphaFunction::EASE_OUT_SINE);
       mAnimationActorPair[animation] = actor;
       mAnimationIndexPair[animation] = mIndex;
       animation.Play();
@@ -955,7 +965,7 @@ void PageTurnView::PanFinished( const Vector2& gesturePosition, float gestureSpe
     {
       Animation animation= Animation::New( PAGE_SLIDE_BACK_ANIMATION_DURATION * (mOriginalCenter.x - mCurrentCenter.x) / mPageSize.width / PAGE_TURN_OVER_THRESHOLD_RATIO );
       animation.AnimateTo( Property( mTurnEffect[mIndex], mTurnEffect[mIndex].PageTurnEffect::GetCurrentCenterPropertyName() ),
-                           mOriginalCenter, AlphaFunctions::Linear );
+                           mOriginalCenter, AlphaFunction::LINEAR );
       mAnimationActorPair[animation] = actor;
       mAnimationIndexPair[animation] = mIndex;
       animation.Play();
@@ -977,7 +987,7 @@ void PageTurnView::PanFinished( const Vector2& gesturePosition, float gestureSpe
 
 void PageTurnView::TurnedOver( Animation& animation )
 {
-  Actor actor = mAnimationActorPair[animation];
+  ImageActor actor = mAnimationActorPair[animation];
   mIsTurnBack[actor] = !mIsTurnBack[actor];
   actor.RemoveConstraints();
   mRootOnScreen.Add(actor);
@@ -996,7 +1006,7 @@ void PageTurnView::TurnedOver( Animation& animation )
 
 void PageTurnView::SliddenBack( Animation& animation )
 {
-  Actor actor = mAnimationActorPair[animation];
+  ImageActor actor = mAnimationActorPair[animation];
   mRootOnScreen.Add(actor);
   int index = mAnimationIndexPair[animation];
   mIsSliding[index] = false;
@@ -1026,14 +1036,9 @@ void PageTurnView::OrganizePageDepth()
   }
 }
 
-void PageTurnView::SetShaderEffect( Actor actor, ShaderEffect shaderEffect )
+void PageTurnView::SetShaderEffect( ImageActor actor, ShaderEffect shaderEffect )
 {
-  actor.SetShaderEffect( shaderEffect );
-
-  if( actor.GetChildCount() > 0 )
-  {
-    actor.GetChildAt( 0 ).SetShaderEffect(shaderEffect);
-  }
+  SetShaderEffectRecursively( actor, shaderEffect );
 }
 
 Toolkit::PageTurnView::PageTurnSignal& PageTurnView::PageTurnStartedSignal()