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