License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-toolkit.git] / optional / 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 namespace Dali
22 {
23
24 namespace Toolkit
25 {
26
27 namespace Internal
28 {
29
30 namespace
31 {
32 using namespace Dali;
33 TypeRegistration mType( typeid(Toolkit::PageTurnPortraitView), typeid(Toolkit::PageTurnView), NULL );
34
35 // 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
36 const float GESTURE_SPEED_THRESHOLD(0.3f);
37
38 // the animation duration of turning the previous page back when an outwards flick is detected
39 const float PAGE_TURN_OVER_ANIMATION_DURATION(0.5f);
40 }
41
42 PageTurnPortraitView::PageTurnPortraitView( PageFactory& pageFactory, const Vector2& pageSize )
43 : PageTurnView( pageFactory, pageSize )
44 {
45
46 }
47
48 PageTurnPortraitView::~PageTurnPortraitView()
49 {
50 }
51
52 Toolkit::PageTurnPortraitView PageTurnPortraitView::New( PageFactory& pageFactory, const Vector2& pageSize )
53 {
54   // Create the implementation, temporarily owned on stack
55   IntrusivePtr< PageTurnPortraitView > internalPageTurnView = new PageTurnPortraitView( pageFactory, pageSize );
56
57   // Pass ownership to CustomActor
58   Dali::Toolkit::PageTurnPortraitView pageTurnView( *internalPageTurnView );
59
60   // Second-phase init of the implementation
61   // This can only be done after the CustomActor connection has been made...
62   internalPageTurnView->Initialize();
63
64   return pageTurnView;
65 }
66
67 void PageTurnPortraitView::OnPageTurnViewInitialize()
68 {
69   mControlSize = mPageSize;
70   Self().SetSize( mPageSize );
71   mTurningPageLayer.SetParentOrigin( ParentOrigin::CENTER_LEFT );
72 }
73
74 ImageActor PageTurnPortraitView::NewPageFromRenderBuffer( int pageIndex )
75 {
76   return ImageActor::New(mRenderedPage[pageIndex % NUMBER_OF_CACHED_PAGES]);
77 }
78
79 Vector2 PageTurnPortraitView::SetPanPosition( const Vector2& gesturePosition )
80 {
81   return gesturePosition;
82 }
83
84 void PageTurnPortraitView::SetPanActor( const Vector2& panPosition )
85 {
86   if( mCurrentPageIndex < mTotalPageCount )
87   {
88     mPanActor = mPageActors[mCurrentPageIndex%NUMBER_OF_CACHED_PAGES];
89   }
90   else
91   {
92     mPanActor.Reset();
93   }
94 }
95
96 void PageTurnPortraitView::SetSpineEffect(Actor actor, bool isLeftSide)
97 {
98   if(isLeftSide)
99   {
100     actor.RemoveShaderEffect();
101   }
102   else
103   {
104     actor.SetShaderEffect( mSpineEffectFront );
105   }
106 }
107
108 void PageTurnPortraitView::OnPossibleOutwardsFlick( const Vector2& panPosition, float gestureSpeed )
109 {
110   Vector2 offset = panPosition - mPressDownPosition;
111   // There is previous page and an outwards flick is detected
112   if( mCurrentPageIndex > 0 && gestureSpeed > GESTURE_SPEED_THRESHOLD && offset.x > fabs( offset.y ))
113   {
114     Actor actor = mPageActors[ (mCurrentPageIndex-1) % NUMBER_OF_CACHED_PAGES ];
115     if(actor.GetParent() != mRootOnScreen)
116     {
117       return;
118     }
119
120     // Guard against destruction during signal emission
121     //Emit signal, to notify that page[mCurrentPageIndex-1] is turning backwards
122     Toolkit::PageTurnView handle( GetOwner() );
123     mPageTurnStartedSignal.Emit( handle, static_cast<unsigned int>(mCurrentPageIndex-1), false );
124
125     //update pages
126     mCurrentPageIndex--;
127     RemovePage( mCurrentPageIndex+NUMBER_OF_CACHED_PAGES_EACH_SIDE );
128     AddPage( mCurrentPageIndex-NUMBER_OF_CACHED_PAGES_EACH_SIDE );
129     OrganizePageDepth();
130
131     // Add the page to tuning page layer and set up PageTurnEffect
132     mShadowView.Add( actor );
133     actor.SetShaderEffect( mTurnEffect[mIndex] );
134     GetImpl( mTurnEffect[mIndex] ).ApplyInternalConstraint();
135     mIsAnimating[mIndex] = true;
136     mTurnEffect[mIndex].SetIsTurningBack( true );
137     Vector2 originalCenter( mPageSize.width*1.5f, 0.5f*mPageSize.height );
138     mTurnEffect[mIndex].SetOriginalCenter( originalCenter );
139     mTurnEffect[mIndex].SetCurrentCenter( Vector2( mPageSize.width*0.5f, mPageSize.height*0.5f ));
140
141     // Start an animation to turn the previous page back
142     Animation animation = Animation::New( PAGE_TURN_OVER_ANIMATION_DURATION );
143     mAnimationActorPair[animation] = actor;
144     mAnimationIndexPair[animation] = mIndex;
145
146     animation.AnimateTo( Property( mTurnEffect[mIndex], mTurnEffect[mIndex].PageTurnEffect::GetCurrentCenterPropertyName() ),
147                          originalCenter,
148                          AlphaFunctions::EaseOut, PAGE_TURN_OVER_ANIMATION_DURATION*0.75f );
149     animation.AnimateBy( Property( actor, Actor::ROTATION ), AngleAxis( Degree( 180.0f ), Vector3::YAXIS ) ,AlphaFunctions::EaseOut );
150     animation.Play();
151     ImageActor::DownCast(actor).SetCullFace( CullBack );
152     animation.FinishedSignal().Connect( this, &PageTurnPortraitView::OnTurnedOver );
153   }
154 }
155
156 void PageTurnPortraitView::OnTurnedOver( Animation& animation )
157 {
158   ImageActor::DownCast(mAnimationActorPair[animation]).SetCullFace( CullNone );
159   TurnedOver( animation );
160 }
161
162 } // namespace Internal
163
164 } // namespace Toolkit
165
166 } // namespace Dali