PageTurnView cleanup
[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 #include <dali/devel-api/object/type-registry-helper.h>
25 #include <dali/devel-api/rendering/cull-face.h>
26
27 //INTERNAL INCLUDES
28 #include <dali-toolkit/internal/controls/page-turn-view/page-turn-effect.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 namespace
40 {
41 using namespace Dali;
42
43 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PageTurnPortraitView, Toolkit::PageTurnView, NULL )
44 DALI_TYPE_REGISTRATION_END()
45
46 // 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
47 const float GESTURE_SPEED_THRESHOLD(0.3f);
48
49 // the animation duration of turning the previous page back when an outwards flick is detected
50 const float PAGE_TURN_OVER_ANIMATION_DURATION(0.5f);
51
52 }
53
54 PageTurnPortraitView::PageTurnPortraitView( PageFactory& pageFactory, const Vector2& pageSize )
55 : PageTurnView( pageFactory, pageSize )
56 {
57
58 }
59
60 PageTurnPortraitView::~PageTurnPortraitView()
61 {
62 }
63
64 Toolkit::PageTurnPortraitView PageTurnPortraitView::New( PageFactory& pageFactory, const Vector2& pageSize )
65 {
66   // Create the implementation, temporarily owned on stack
67   IntrusivePtr< PageTurnPortraitView > internalPageTurnView = new PageTurnPortraitView( pageFactory, pageSize );
68
69   // Pass ownership to CustomActor
70   Dali::Toolkit::PageTurnPortraitView pageTurnView( *internalPageTurnView );
71
72   // Second-phase init of the implementation
73   // This can only be done after the CustomActor connection has been made...
74   internalPageTurnView->Initialize();
75
76   return pageTurnView;
77 }
78
79 void PageTurnPortraitView::OnPageTurnViewInitialize()
80 {
81   mControlSize = mPageSize;
82   Self().SetSize( mPageSize );
83   mTurningPageLayer.SetParentOrigin( ParentOrigin::CENTER_LEFT );
84 }
85
86 Vector2 PageTurnPortraitView::SetPanPosition( const Vector2& gesturePosition )
87 {
88   return gesturePosition;
89 }
90
91 void PageTurnPortraitView::SetPanActor( const Vector2& panPosition )
92 {
93   if( mCurrentPageIndex < mTotalPageCount )
94   {
95     mPanActor = mPageActors[mCurrentPageIndex%NUMBER_OF_CACHED_PAGES];
96     mTurningPageIndex = mCurrentPageIndex;
97   }
98   else
99   {
100     mPanActor.Reset();
101   }
102 }
103
104 void PageTurnPortraitView::SetSpineEffect(ImageActor actor, bool isLeftSide)
105 {
106   if(isLeftSide)
107   {
108     actor.RemoveShaderEffect();
109   }
110   else
111   {
112     actor.SetShaderEffect( mSpineEffectFront );
113   }
114 }
115
116 void PageTurnPortraitView::OnPossibleOutwardsFlick( const Vector2& panPosition, float gestureSpeed )
117 {
118   Vector2 offset = panPosition - mPressDownPosition;
119   // There is previous page and an outwards flick is detected
120   if( mCurrentPageIndex > 0 && gestureSpeed > GESTURE_SPEED_THRESHOLD && offset.x > fabs( offset.y ))
121   {
122     ImageActor actor = mPageActors[ (mCurrentPageIndex-1) % NUMBER_OF_CACHED_PAGES ];
123     if(actor.GetParent() != Self())
124     {
125       return;
126     }
127
128     // Guard against destruction during signal emission
129     //Emit signal, to notify that page[mCurrentPageIndex-1] is turning backwards
130     Toolkit::PageTurnView handle( GetOwner() );
131     mTurningPageIndex = mCurrentPageIndex-1;
132     mPageTurnStartedSignal.Emit( handle, static_cast<unsigned int>(mTurningPageIndex), false );
133
134     //update pages
135     mCurrentPageIndex--;
136     RemovePage( mCurrentPageIndex+NUMBER_OF_CACHED_PAGES_EACH_SIDE );
137     AddPage( mCurrentPageIndex-NUMBER_OF_CACHED_PAGES_EACH_SIDE );
138     OrganizePageDepth();
139
140     mPageActors[mTurningPageIndex%NUMBER_OF_CACHED_PAGES].SetVisible(true);
141
142     // Add the page to tuning page layer and set up PageTurnEffect
143     mShadowView.Add( actor );
144     actor.SetShaderEffect( mTurnEffect[mIndex] );
145     PageTurnApplyInternalConstraint(mTurnEffect[mIndex]);
146     mIsAnimating[mIndex] = true;
147     mTurnEffect[mIndex].SetUniform("uIsTurningBack", 1.f );
148     Vector2 originalCenter( mPageSize.width*1.5f, 0.5f*mPageSize.height );
149     mTurnEffect[mIndex].SetUniform("uOriginalCenter", originalCenter );
150     mTurnEffect[mIndex].SetUniform("uCurrentCenter", Vector2( mPageSize.width*0.5f, mPageSize.height*0.5f ) );
151
152     // Start an animation to turn the previous page back
153     Animation animation = Animation::New( PAGE_TURN_OVER_ANIMATION_DURATION );
154     mAnimationPageIdPair[animation] = mCurrentPageIndex;
155     mAnimationIndexPair[animation] = mIndex;
156
157     animation.AnimateTo( Property( mTurnEffect[mIndex], "uCurrentCenter" ),
158                          originalCenter,
159                          AlphaFunction::EASE_OUT, TimePeriod(PAGE_TURN_OVER_ANIMATION_DURATION*0.75f) );
160     animation.AnimateBy( Property( actor, Actor::Property::ORIENTATION ), AngleAxis( Degree( 180.0f ), Vector3::YAXIS ) ,AlphaFunction::EASE_OUT );
161     animation.Play();
162
163     ImageActor imageActor = ImageActor::DownCast(actor);
164     if( imageActor )
165     {
166       SetCullFace( imageActor, CullBack );
167     }
168     animation.FinishedSignal().Connect( this, &PageTurnPortraitView::TurnedOverBackwards );
169   }
170 }
171
172 void PageTurnPortraitView::OnTurnedOver( ImageActor actor, bool isLeftSide )
173 {
174   if( isLeftSide )
175   {
176     actor.SetVisible( false );
177   }
178 }
179
180 void PageTurnPortraitView::TurnedOverBackwards( Animation& animation )
181 {
182   ImageActor imageActor =  mPageActors[mAnimationPageIdPair[animation] % NUMBER_OF_CACHED_PAGES];
183   if( imageActor )
184   {
185     SetCullFace( imageActor, CullNone );
186   }
187   TurnedOver( animation );
188 }
189
190 } // namespace Internal
191
192 } // namespace Toolkit
193
194 } // namespace Dali