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