2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/controls/page-turn-view/page-turn-portrait-view-impl.h>
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>
28 #include <dali-toolkit/internal/controls/page-turn-view/page-turn-effect.h>
41 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PageTurnPortraitView, Toolkit::PageTurnView, NULL )
42 DALI_TYPE_REGISTRATION_END()
44 // 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
45 const float GESTURE_SPEED_THRESHOLD(0.3f);
47 // the animation duration of turning the previous page back when an outwards flick is detected
48 const float PAGE_TURN_OVER_ANIMATION_DURATION(0.5f);
52 PageTurnPortraitView::PageTurnPortraitView( PageFactory& pageFactory, const Vector2& pageSize )
53 : PageTurnView( pageFactory, pageSize )
57 PageTurnPortraitView::~PageTurnPortraitView()
61 Toolkit::PageTurnPortraitView PageTurnPortraitView::New( PageFactory& pageFactory, const Vector2& pageSize )
63 // Create the implementation, temporarily owned on stack
64 IntrusivePtr< PageTurnPortraitView > internalPageTurnView = new PageTurnPortraitView( pageFactory, pageSize );
66 // Pass ownership to CustomActor
67 Dali::Toolkit::PageTurnPortraitView pageTurnView( *internalPageTurnView );
69 // Second-phase init of the implementation
70 // This can only be done after the CustomActor connection has been made...
71 internalPageTurnView->Initialize();
76 void PageTurnPortraitView::OnPageTurnViewInitialize()
78 mTurnEffectShader.RegisterProperty(PROPERTY_TEXTURE_WIDTH, 1.f );
79 mSpineEffectShader.RegisterProperty(PROPERTY_TEXTURE_WIDTH, 1.f );
81 mControlSize = mPageSize;
82 Self().SetSize( mPageSize );
83 mTurningPageLayer.SetParentOrigin( ParentOrigin::CENTER_LEFT );
86 Vector2 PageTurnPortraitView::SetPanPosition( const Vector2& gesturePosition )
88 return gesturePosition;
91 void PageTurnPortraitView::SetPanActor( const Vector2& panPosition )
93 if( mCurrentPageIndex < mTotalPageCount )
95 mTurningPageIndex = mCurrentPageIndex;
99 mTurningPageIndex = -1;
103 void PageTurnPortraitView::OnPossibleOutwardsFlick( const Vector2& panPosition, float gestureSpeed )
105 Vector2 offset = panPosition - mPressDownPosition;
106 // There is previous page and an outwards flick is detected
107 if( mCurrentPageIndex > 0 && gestureSpeed > GESTURE_SPEED_THRESHOLD && offset.x > fabs( offset.y ))
109 int actorIndex = (mCurrentPageIndex-1) % NUMBER_OF_CACHED_PAGES;
110 Actor actor = mPages[ actorIndex ].actor;
111 if(actor.GetParent() != Self())
116 // Guard against destruction during signal emission
117 //Emit signal, to notify that page[mCurrentPageIndex-1] is turning backwards
118 Toolkit::PageTurnView handle( GetOwner() );
119 mTurningPageIndex = mCurrentPageIndex-1;
120 mPageTurnStartedSignal.Emit( handle, static_cast<unsigned int>(mTurningPageIndex), false );
124 RemovePage( mCurrentPageIndex+NUMBER_OF_CACHED_PAGES_EACH_SIDE );
125 AddPage( mCurrentPageIndex-NUMBER_OF_CACHED_PAGES_EACH_SIDE );
129 actor.SetVisible(true);
131 // Add the page to tuning page layer and set up PageTurnEffect
132 mShadowView.Add( actor );
133 mPages[actorIndex].UseEffect( mTurnEffectShader );
135 Vector2 originalCenter( mPageSize.width*1.5f, 0.5f*mPageSize.height );
136 mPages[actorIndex].SetOriginalCenter( originalCenter );
137 mPages[actorIndex].SetCurrentCenter( Vector2( mPageSize.width*0.5f, mPageSize.height*0.5f ) );
138 PageTurnApplyInternalConstraint(actor, mPageSize.height);
140 // Start an animation to turn the previous page back
141 Animation animation = Animation::New( PAGE_TURN_OVER_ANIMATION_DURATION );
142 mAnimationPageIdPair[animation] = mCurrentPageIndex;
144 animation.AnimateTo( Property( actor, mPages[actorIndex].propertyCurrentCenter ),
146 AlphaFunction::EASE_OUT, TimePeriod(PAGE_TURN_OVER_ANIMATION_DURATION*0.75f) );
147 animation.AnimateBy( Property( actor, Actor::Property::ORIENTATION ), AngleAxis( Degree( 180.0f ), Vector3::YAXIS ) ,AlphaFunction::EASE_OUT );
150 animation.FinishedSignal().Connect( this, &PageTurnPortraitView::TurnedOverBackwards );
154 void PageTurnPortraitView::OnTurnedOver( Actor actor, bool isLeftSide )
158 actor.SetVisible( false );
162 void PageTurnPortraitView::TurnedOverBackwards( Animation& animation )
164 TurnedOver( animation );
167 } // namespace Internal
169 } // namespace Toolkit