[dali_1.0.32] Merge branch 'tizen'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / page-turn-view / page-turn-portrait-view-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/page-turn-view/page-turn-portrait-view-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/animation/animation.h>
23 #include <dali/public-api/object/type-registry.h>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Internal
32 {
33
34 namespace
35 {
36 using namespace Dali;
37 TypeRegistration mType( typeid(Toolkit::PageTurnPortraitView), typeid(Toolkit::PageTurnView), NULL );
38
39 // the panning speed threshold, no matter how far is the pan displacement, pan fast to left/right quickly (speed > 0.3) will turn over/back the page
40 const float GESTURE_SPEED_THRESHOLD(0.3f);
41
42 // the animation duration of turning the previous page back when an outwards flick is detected
43 const float PAGE_TURN_OVER_ANIMATION_DURATION(0.5f);
44 }
45
46 PageTurnPortraitView::PageTurnPortraitView( PageFactory& pageFactory, const Vector2& pageSize )
47 : PageTurnView( pageFactory, pageSize )
48 {
49
50 }
51
52 PageTurnPortraitView::~PageTurnPortraitView()
53 {
54 }
55
56 Toolkit::PageTurnPortraitView PageTurnPortraitView::New( PageFactory& pageFactory, const Vector2& pageSize )
57 {
58   // Create the implementation, temporarily owned on stack
59   IntrusivePtr< PageTurnPortraitView > internalPageTurnView = new PageTurnPortraitView( pageFactory, pageSize );
60
61   // Pass ownership to CustomActor
62   Dali::Toolkit::PageTurnPortraitView pageTurnView( *internalPageTurnView );
63
64   // Second-phase init of the implementation
65   // This can only be done after the CustomActor connection has been made...
66   internalPageTurnView->Initialize();
67
68   return pageTurnView;
69 }
70
71 void PageTurnPortraitView::OnPageTurnViewInitialize()
72 {
73   mControlSize = mPageSize;
74   Self().SetSize( mPageSize );
75   mTurningPageLayer.SetParentOrigin( ParentOrigin::CENTER_LEFT );
76 }
77
78 ImageActor PageTurnPortraitView::NewPageFromRenderBuffer( int pageIndex )
79 {
80   return ImageActor::New(mRenderedPage[pageIndex % NUMBER_OF_CACHED_PAGES]);
81 }
82
83 Vector2 PageTurnPortraitView::SetPanPosition( const Vector2& gesturePosition )
84 {
85   return gesturePosition;
86 }
87
88 void PageTurnPortraitView::SetPanActor( const Vector2& panPosition )
89 {
90   if( mCurrentPageIndex < mTotalPageCount )
91   {
92     mPanActor = mPageActors[mCurrentPageIndex%NUMBER_OF_CACHED_PAGES];
93   }
94   else
95   {
96     mPanActor.Reset();
97   }
98 }
99
100 void PageTurnPortraitView::SetSpineEffect(ImageActor actor, bool isLeftSide)
101 {
102   if(isLeftSide)
103   {
104     actor.RemoveShaderEffect();
105   }
106   else
107   {
108     actor.SetShaderEffect( mSpineEffectFront );
109   }
110 }
111
112 void PageTurnPortraitView::OnPossibleOutwardsFlick( const Vector2& panPosition, float gestureSpeed )
113 {
114   Vector2 offset = panPosition - mPressDownPosition;
115   // There is previous page and an outwards flick is detected
116   if( mCurrentPageIndex > 0 && gestureSpeed > GESTURE_SPEED_THRESHOLD && offset.x > fabs( offset.y ))
117   {
118     ImageActor actor = mPageActors[ (mCurrentPageIndex-1) % NUMBER_OF_CACHED_PAGES ];
119     if(actor.GetParent() != mRootOnScreen)
120     {
121       return;
122     }
123
124     // Guard against destruction during signal emission
125     //Emit signal, to notify that page[mCurrentPageIndex-1] is turning backwards
126     Toolkit::PageTurnView handle( GetOwner() );
127     mPageTurnStartedSignal.Emit( handle, static_cast<unsigned int>(mCurrentPageIndex-1), false );
128
129     //update pages
130     mCurrentPageIndex--;
131     RemovePage( mCurrentPageIndex+NUMBER_OF_CACHED_PAGES_EACH_SIDE );
132     AddPage( mCurrentPageIndex-NUMBER_OF_CACHED_PAGES_EACH_SIDE );
133     OrganizePageDepth();
134
135     // Add the page to tuning page layer and set up PageTurnEffect
136     mShadowView.Add( actor );
137     actor.SetShaderEffect( mTurnEffect[mIndex] );
138     GetImpl( mTurnEffect[mIndex] ).ApplyInternalConstraint();
139     mIsAnimating[mIndex] = true;
140     mTurnEffect[mIndex].SetIsTurningBack( true );
141     Vector2 originalCenter( mPageSize.width*1.5f, 0.5f*mPageSize.height );
142     mTurnEffect[mIndex].SetOriginalCenter( originalCenter );
143     mTurnEffect[mIndex].SetCurrentCenter( Vector2( mPageSize.width*0.5f, mPageSize.height*0.5f ));
144
145     // Start an animation to turn the previous page back
146     Animation animation = Animation::New( PAGE_TURN_OVER_ANIMATION_DURATION );
147     mAnimationActorPair[animation] = actor;
148     mAnimationIndexPair[animation] = mIndex;
149
150     animation.AnimateTo( Property( mTurnEffect[mIndex], mTurnEffect[mIndex].PageTurnEffect::GetCurrentCenterPropertyName() ),
151                          originalCenter,
152                          AlphaFunctions::EaseOut, PAGE_TURN_OVER_ANIMATION_DURATION*0.75f );
153     animation.AnimateBy( Property( actor, Actor::Property::Rotation ), AngleAxis( Degree( 180.0f ), Vector3::YAXIS ) ,AlphaFunctions::EaseOut );
154     animation.Play();
155     ImageActor::DownCast(actor).SetCullFace( CullBack );
156     animation.FinishedSignal().Connect( this, &PageTurnPortraitView::OnTurnedOver );
157   }
158 }
159
160 void PageTurnPortraitView::OnTurnedOver( Animation& animation )
161 {
162   ImageActor::DownCast(mAnimationActorPair[animation]).SetCullFace( CullNone );
163   TurnedOver( animation );
164 }
165
166 } // namespace Internal
167
168 } // namespace Toolkit
169
170 } // namespace Dali