2 * Copyright (c) 2020 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-view-impl.h>
22 #include <cstring> // for strcmp
23 #include <dali/public-api/animation/animation.h>
24 #include <dali/public-api/animation/constraint.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/public-api/object/type-registry-helper.h>
27 #include <dali/integration-api/debug.h>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/internal/controls/page-turn-view/page-turn-effect.h>
32 #include <dali-toolkit/internal/controls/page-turn-view/page-turn-book-spine-effect.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
34 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
35 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
39 namespace //Unnamed namespace
41 // properties set on shader, these properties have the constant value in regardless of the page status
42 const char * const PROPERTY_SPINE_SHADOW ( "uSpineShadowParameter" ); // uniform for both spine and turn effect
44 // properties set on actor, the value of these properties varies depending on the page status
45 // properties used in turn effect
46 const char * const PROPERTY_TURN_DIRECTION( "uIsTurningBack" ); // uniform
47 const char * const PROPERTY_COMMON_PARAMETERS( "uCommonParameters" ); //uniform
49 const char * const PROPERTY_PAN_DISPLACEMENT( "panDisplacement" );// property used to constrain the uniforms
50 const char * const PROPERTY_PAN_CENTER( "panCenter" );// property used to constrain the uniforms
52 // default grid density for page turn effect, 20 pixels by 20 pixels
53 const float DEFAULT_GRID_DENSITY(20.0f);
55 // to bent the page, the minimal horizontal pan start position is viewPageSize.x * MINIMUM_START_POSITION_RATIO
56 const float MINIMUM_START_POSITION_RATIO(0.6f);
58 // the maximum vertical displacement of pan gesture, if exceed, will reduce it: viewPageSize.y * MAXIMUM_VERTICAL_MOVEMENT_RATIO
59 const float MAXIMUM_VERTICAL_MOVEMENT_RATIO(0.15f);
61 // when the x component of pan position reaches viewPageSize.x * PAGE_TURN_OVER_THRESHOLD_RATIO, page starts to turn over
62 const float PAGE_TURN_OVER_THRESHOLD_RATIO(0.5f);
64 // duration of animation, shorter for faster speed
65 const float PAGE_SLIDE_BACK_ANIMATION_DURATION(1.0f);
66 const float PAGE_TURN_OVER_ANIMATION_DURATION(1.2f);
68 // the major&minor radius (in pixels) to form an ellipse shape
69 // the top-left quarter of this ellipse is used to calculate spine normal for simulating shadow
70 const Vector2 DEFAULT_SPINE_SHADOW_PARAMETER(50.0f, 20.0f);
72 // constants for shadow casting
73 const float POINT_LIGHT_HEIGHT_RATIO(2.f);
74 const Vector4 DEFAULT_SHADOW_COLOR = Vector4(0.2f, 0.2f, 0.2f, 0.5f);
76 // constraints ////////////////////////////////////////////////////////////////
78 * Original Center Constraint
80 * This constraint adjusts the original center property of the page turn shader effect
81 * based on the X-direction displacement of the pan gesture
83 struct OriginalCenterConstraint
85 OriginalCenterConstraint(const Vector2& originalCenter, const Vector2& offset)
86 : mOldCenter( originalCenter )
88 mNewCenter = originalCenter + offset;
89 mDistance = offset.Length() * 0.5f;
90 mDirection = offset / mDistance;
93 void operator()( Vector2& current, const PropertyInputContainer& inputs )
95 float displacement = inputs[0]->GetFloat();
97 if( displacement < mDistance )
99 current = mOldCenter + mDirection * displacement;
103 current = mNewCenter + Vector2(0.25f*(displacement-mDistance), 0.f);
114 * Rotation Constraint
116 * This constraint adjusts the rotation property of the page actor
117 * based on the X-direction displacement of the pan gesture
119 struct RotationConstraint
121 RotationConstraint( float distance, float pageWidth, bool isTurnBack )
122 : mDistance( distance*0.5f )
124 mStep = 1.f / pageWidth;
125 mSign = isTurnBack ? -1.0f : 1.0f;
126 mConst = isTurnBack ? -1.0f : 0.f;
127 mRotation = isTurnBack ? Quaternion( Radian( -Math::PI ), Vector3::YAXIS ) : Quaternion( Radian(0.f), Vector3::YAXIS );
130 void operator()( Quaternion& current, const PropertyInputContainer& inputs )
132 float displacement = inputs[0]->GetFloat();
133 if( displacement < mDistance)
139 float coef = std::max(-1.0f, mStep*(mDistance-displacement));
140 float angle = Math::PI * ( mConst + mSign * coef );
141 current = Quaternion( Radian( angle ), Vector3::YAXIS );
149 Quaternion mRotation;
153 * Current Center Constraint
155 * This constraint adjusts the current center property of the page turn shader effect
156 * based on the pan position and the original center position
158 struct CurrentCenterConstraint
160 CurrentCenterConstraint( float pageWidth )
161 : mPageWidth( pageWidth )
163 mThres = pageWidth * PAGE_TURN_OVER_THRESHOLD_RATIO * 0.5f;
166 void operator()( Vector2& current, const PropertyInputContainer& inputs )
168 const Vector2& centerPosition = inputs[0]->GetVector2();
169 if( centerPosition.x > 0.f )
171 current.x = mThres+centerPosition.x * 0.5f;
172 current.y = centerPosition.y;
176 const Vector2& centerOrigin = inputs[1]->GetVector2();
177 Vector2 direction = centerOrigin - Vector2(mThres, centerPosition.y);
178 float coef = 1.f+(centerPosition.x*2.f / mPageWidth);
179 // when coef <= 0, the page is flat, slow down the last moment of the page stretch by 10 times to avoid a small bounce
182 coef = (coef+0.225f)/10.0f;
184 current = centerOrigin - direction * coef;
192 struct ShadowBlurStrengthConstraint
194 ShadowBlurStrengthConstraint( float thres )
198 void operator()( float& blurStrength, const PropertyInputContainer& inputs )
200 float displacement = inputs[2]->GetFloat();
201 if( EqualsZero(displacement))
203 const Vector2& cur = inputs[0]->GetVector2();
204 const Vector2& ori = inputs[1]->GetVector2();
205 blurStrength = 5.f*(ori-cur).Length() / mThres;
209 blurStrength = 1.f - (displacement-2.f*mThres)/mThres;
212 blurStrength = blurStrength > 1.f ? 1.f : ( blurStrength < 0.f ? 0.f : blurStrength );
218 } //unnamed namespace
234 // empty handle as we cannot create PageTurnView(but type registered for page turn signal)
238 // Setup properties, signals and actions using the type-registry.
239 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PageTurnView, Toolkit::Control, Create );
241 DALI_PROPERTY_REGISTRATION( Toolkit, PageTurnView, "viewPageSize", VECTOR2, VIEW_PAGE_SIZE )
242 DALI_PROPERTY_REGISTRATION( Toolkit, PageTurnView, "currentPageId", INTEGER, CURRENT_PAGE_ID )
243 DALI_PROPERTY_REGISTRATION( Toolkit, PageTurnView, "spineShadow", VECTOR2, SPINE_SHADOW )
245 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pageTurnStarted", SIGNAL_PAGE_TURN_STARTED )
246 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pageTurnFinished", SIGNAL_PAGE_TURN_FINISHED )
247 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pagePanStarted", SIGNAL_PAGE_PAN_STARTED )
248 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pagePanFinished", SIGNAL_PAGE_PAN_FINISHED )
250 DALI_TYPE_REGISTRATION_END()
254 // these several constants are also used in the derived classes
255 const char * const PageTurnView::PROPERTY_TEXTURE_WIDTH( "uTextureWidth" ); // uniform name
256 const char * const PageTurnView::PROPERTY_ORIGINAL_CENTER( "originalCenter" ); // property used to constrain the uniform
257 const char * const PageTurnView::PROPERTY_CURRENT_CENTER( "currentCenter" );// property used to constrain the uniform
258 const int PageTurnView::MAXIMUM_TURNING_NUM = 4;
259 const int PageTurnView::NUMBER_OF_CACHED_PAGES_EACH_SIDE = MAXIMUM_TURNING_NUM + 1;
260 const int PageTurnView::NUMBER_OF_CACHED_PAGES = NUMBER_OF_CACHED_PAGES_EACH_SIDE*2;
261 const float PageTurnView::STATIC_PAGE_INTERVAL_DISTANCE = 1.0f;
263 PageTurnView::Page::Page()
264 : isTurnBack( false )
266 actor = Actor::New();
267 actor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT );
268 actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER_LEFT );
269 actor.SetProperty( Actor::Property::VISIBLE, false );
271 propertyPanDisplacement = actor.RegisterProperty( PROPERTY_PAN_DISPLACEMENT, 0.f );
272 propertyPanCenter = actor.RegisterProperty(PROPERTY_PAN_CENTER, Vector2::ZERO);
274 propertyOriginalCenter = actor.RegisterProperty(PROPERTY_ORIGINAL_CENTER, Vector2::ZERO);
275 propertyCurrentCenter = actor.RegisterProperty(PROPERTY_CURRENT_CENTER, Vector2::ZERO);
276 Matrix zeroMatrix(true);
277 actor.RegisterProperty(PROPERTY_COMMON_PARAMETERS, zeroMatrix);
278 propertyTurnDirection = actor.RegisterProperty(PROPERTY_TURN_DIRECTION, -1.f);
281 void PageTurnView::Page::SetTexture( Texture texture )
285 textureSet = TextureSet::New();
287 textureSet.SetTexture( 0u, texture );
290 void PageTurnView::Page::UseEffect(Shader newShader)
295 renderer.SetShader( shader );
299 void PageTurnView::Page::UseEffect(Shader newShader, Geometry geometry)
301 UseEffect( newShader );
305 renderer = Renderer::New( geometry, shader );
309 textureSet = TextureSet::New();
312 renderer.SetTextures( textureSet );
313 renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
314 actor.AddRenderer( renderer );
318 void PageTurnView::Page::ChangeTurnDirection()
320 isTurnBack = !isTurnBack;
321 actor.SetProperty( propertyTurnDirection, isTurnBack ? 1.f : -1.f );
324 void PageTurnView::Page::SetPanDisplacement(float value)
326 actor.SetProperty( propertyPanDisplacement, value );
329 void PageTurnView::Page::SetPanCenter( const Vector2& value )
331 actor.SetProperty( propertyPanCenter, value );
334 void PageTurnView::Page::SetOriginalCenter( const Vector2& value )
336 actor.SetProperty( propertyOriginalCenter, value );
339 void PageTurnView::Page::SetCurrentCenter( const Vector2& value )
341 actor.SetProperty( propertyCurrentCenter, value );
344 PageTurnView::PageTurnView( PageFactory& pageFactory, const Vector2& viewPageSize )
345 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
346 mPageFactory( &pageFactory ),
347 mPageSize( viewPageSize ),
348 mSpineShadowParameter( DEFAULT_SPINE_SHADOW_PARAMETER ),
349 mDistanceUpCorner( 0.f ),
350 mDistanceBottomCorner( 0.f ),
351 mPanDisplacement( 0.f ),
352 mTotalPageCount( 0 ),
353 mCurrentPageIndex( 0 ),
354 mTurningPageIndex( 0 ),
357 mAnimatingCount( 0 ),
358 mConstraints( false ),
360 mPageUpdated( true ),
361 mPageTurnStartedSignal(),
362 mPageTurnFinishedSignal(),
363 mPagePanStartedSignal(),
364 mPagePanFinishedSignal()
366 DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor ) {
367 return std::unique_ptr< Dali::Accessibility::Accessible >(
368 new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::PAGE_TAB_LIST ) );
372 PageTurnView::~PageTurnView()
376 void PageTurnView::OnInitialize()
378 // create the book spine effect for static pages
379 Property::Map spineEffectMap = CreatePageTurnBookSpineEffect();
380 mSpineEffectShader = CreateShader( spineEffectMap );
381 mSpineEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
382 // create the turn effect for turning pages
383 Property::Map turnEffectMap = CreatePageTurnEffect();
384 mTurnEffectShader = CreateShader( turnEffectMap );
385 mTurnEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
387 // create the grid geometry for pages
388 uint16_t width = static_cast<uint16_t>(mPageSize.width / DEFAULT_GRID_DENSITY + 0.5f);
389 uint16_t height = static_cast<uint16_t>(mPageSize.height / DEFAULT_GRID_DENSITY + 0.5f);
390 mGeometry = VisualFactoryCache::CreateGridGeometry( Uint16Pair( width, height ) );
392 mPages.reserve( NUMBER_OF_CACHED_PAGES );
393 for( int i = 0; i < NUMBER_OF_CACHED_PAGES; i++ )
395 mPages.push_back( Page() );
396 mPages[i].actor.SetProperty( Actor::Property::SIZE, mPageSize );
397 Self().Add( mPages[i].actor );
400 // create the layer for turning pages
401 mTurningPageLayer = Layer::New();
402 mTurningPageLayer.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT );
403 mTurningPageLayer.SetProperty( Layer::Property::BEHAVIOR, Layer::LAYER_3D );
404 mTurningPageLayer.Raise();
406 // Set control size and the parent origin of page layers
407 OnPageTurnViewInitialize();
409 Self().Add(mTurningPageLayer);
411 mTotalPageCount = static_cast<int>( mPageFactory->GetNumberOfPages() );
412 // add pages to the scene, and set depth for the stacked pages
413 for( int i = 0; i < NUMBER_OF_CACHED_PAGES_EACH_SIDE; i++ )
416 mPages[i].actor.SetProperty( Actor::Property::POSITION_Z, -static_cast<float>( i )*STATIC_PAGE_INTERVAL_DISTANCE );
418 mPages[0].actor.SetProperty( Actor::Property::VISIBLE,true);
420 // enable the pan gesture which is attached to the control
421 EnableGestureDetection(GestureType::Value(GestureType::PAN));
424 Shader PageTurnView::CreateShader( const Property::Map& shaderMap )
427 Property::Value* shaderValue = shaderMap.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
428 Property::Map shaderSource;
429 if( shaderValue && shaderValue->Get( shaderSource ) )
431 std::string vertexShader;
432 Property::Value* vertexShaderValue = shaderSource.Find( Toolkit::Visual::Shader::Property::VERTEX_SHADER, CUSTOM_VERTEX_SHADER );
433 if( !vertexShaderValue || !vertexShaderValue->Get( vertexShader ) )
435 DALI_LOG_ERROR("PageTurnView::CreateShader failed: vertex shader source is not available.\n");
437 std::string fragmentShader;
438 Property::Value* fragmentShaderValue = shaderSource.Find( Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, CUSTOM_FRAGMENT_SHADER );
439 if( !fragmentShaderValue || !fragmentShaderValue->Get( fragmentShader ) )
441 DALI_LOG_ERROR("PageTurnView::CreateShader failed: fragment shader source is not available.\n");
443 shader = Shader::New( vertexShader, fragmentShader );
447 DALI_LOG_ERROR("PageTurnView::CreateShader failed: shader source is not available.\n");
453 void PageTurnView::SetupShadowView()
455 mShadowView = Toolkit::ShadowView::New( 0.25f, 0.25f );
456 Vector3 origin = mTurningPageLayer.GetCurrentProperty< Vector3 >( Actor::Property::PARENT_ORIGIN );
457 mShadowView.SetProperty( Actor::Property::PARENT_ORIGIN, origin );
458 mShadowView.SetProperty( Actor::Property::ANCHOR_POINT, origin );
459 mShadowView.SetPointLightFieldOfView( Math::PI / 2.0f);
460 mShadowView.SetShadowColor(DEFAULT_SHADOW_COLOR);
462 mShadowPlaneBackground = Actor::New();
463 mShadowPlaneBackground.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
464 mShadowPlaneBackground.SetProperty( Actor::Property::SIZE, mControlSize );
465 Self().Add( mShadowPlaneBackground );
466 mShadowView.SetShadowPlaneBackground( mShadowPlaneBackground );
468 mPointLight = Actor::New();
469 mPointLight.SetProperty( Actor::Property::ANCHOR_POINT, origin );
470 mPointLight.SetProperty( Actor::Property::PARENT_ORIGIN, origin );
471 mPointLight.SetProperty( Actor::Property::POSITION, Vector3( 0.f, 0.f, mPageSize.width*POINT_LIGHT_HEIGHT_RATIO ));
472 Self().Add( mPointLight );
473 mShadowView.SetPointLight( mPointLight );
475 mTurningPageLayer.Add( mShadowView );
476 mShadowView.Activate();
479 void PageTurnView::OnSceneConnection( int depth )
483 Control::OnSceneConnection( depth );
486 void PageTurnView::OnSceneDisconnection()
490 mShadowView.RemoveConstraints();
491 mPointLight.Unparent();
492 mShadowPlaneBackground.Unparent();
493 mShadowView.Unparent();
496 // make sure the status of the control is updated correctly when the pan gesture is interrupted
499 Control::OnSceneDisconnection();
502 void PageTurnView::SetPageSize( const Vector2& viewPageSize )
504 mPageSize = viewPageSize;
508 mPointLight.SetProperty( Actor::Property::POSITION, Vector3( 0.f, 0.f, mPageSize.width*POINT_LIGHT_HEIGHT_RATIO ));
511 for( size_t i=0; i<mPages.size(); i++ )
513 mPages[i].actor.SetProperty( Actor::Property::SIZE, mPageSize );
516 OnPageTurnViewInitialize();
518 if( mShadowPlaneBackground )
520 mShadowPlaneBackground.SetProperty( Actor::Property::SIZE, mControlSize );
524 Vector2 PageTurnView::GetPageSize()
529 void PageTurnView::SetSpineShadowParameter( const Vector2& spineShadowParameter )
531 mSpineShadowParameter = spineShadowParameter;
533 // set spine shadow parameter to all the shader effects
534 mSpineEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
535 mTurnEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
538 Vector2 PageTurnView::GetSpineShadowParameter()
540 return mSpineShadowParameter;
543 void PageTurnView::GoToPage( unsigned int pageId )
545 int pageIdx = Clamp( static_cast<int>(pageId), 0, mTotalPageCount-1);
547 if( mCurrentPageIndex == pageIdx )
552 // if any animation ongoing, stop it.
555 // record the new current page index
556 mCurrentPageIndex = pageIdx;
559 // add the current page and the pages right before and after it
560 for( int i = pageIdx - NUMBER_OF_CACHED_PAGES_EACH_SIDE; i < pageIdx + NUMBER_OF_CACHED_PAGES_EACH_SIDE; i++ )
565 mPages[pageId%NUMBER_OF_CACHED_PAGES].actor.SetProperty( Actor::Property::VISIBLE,true);
568 mPages[(pageId-1)%NUMBER_OF_CACHED_PAGES].actor.SetProperty( Actor::Property::VISIBLE,true);
570 // set ordered depth to the stacked pages
574 unsigned int PageTurnView::GetCurrentPage()
576 DALI_ASSERT_ALWAYS( mCurrentPageIndex >= 0 );
577 return static_cast< unsigned int >( mCurrentPageIndex );
580 void PageTurnView::AddPage( int pageIndex )
582 if(pageIndex > -1 && pageIndex < mTotalPageCount) // whether the page is available from the page factory
584 int index = pageIndex % NUMBER_OF_CACHED_PAGES;
587 newPage = mPageFactory->NewPage( pageIndex );
588 DALI_ASSERT_ALWAYS( newPage && "must pass in valid texture" );
590 bool isLeftSide = ( pageIndex < mCurrentPageIndex );
591 if( mPages[index].isTurnBack != isLeftSide )
593 mPages[index].ChangeTurnDirection();
596 float degree = isLeftSide ? 180.f :0.f;
597 mPages[index].actor.SetProperty( Actor::Property::ORIENTATION, Quaternion( Degree( degree ), Vector3::YAXIS ) );
598 mPages[index].actor.SetProperty( Actor::Property::VISIBLE, false );
599 mPages[index].UseEffect( mSpineEffectShader, mGeometry );
600 mPages[index].SetTexture( newPage );
602 // For Portrait, nothing to do
603 // For Landscape, set the parent origin to CENTER
604 OnAddPage( mPages[index].actor, isLeftSide );
608 void PageTurnView::RemovePage( int pageIndex )
610 if( pageIndex > -1 && pageIndex < mTotalPageCount)
612 int index = pageIndex % NUMBER_OF_CACHED_PAGES;
613 mPages[index].actor.SetProperty( Actor::Property::VISIBLE,false);
617 void PageTurnView::OnPan( const PanGesture& gesture )
619 // the pan gesture is attached to control itself instead of each page
620 switch( gesture.GetState() )
622 case GestureState::STARTED:
624 // check whether the undergoing turning page number already reaches the maximum allowed
625 if( mPageUpdated && mAnimatingCount< MAXIMUM_TURNING_NUM && mSlidingCount < 1 )
627 const Vector2& position = gesture.GetPosition();
628 SetPanActor( position ); // determine which page actor is panned
629 if( mTurningPageIndex != -1 && mPages[mTurningPageIndex % NUMBER_OF_CACHED_PAGES].actor.GetParent() != Self()) // if the page is added to turning layer,it is undergoing an animation currently
631 mTurningPageIndex = -1;
633 PanStarted( SetPanPosition( position ) ); // pass in the pan position in the local page coordinate
637 mTurningPageIndex = -1;
641 case GestureState::CONTINUING:
643 PanContinuing( SetPanPosition( gesture.GetPosition() ) ); // pass in the pan position in the local page coordinate
646 case GestureState::FINISHED:
647 case GestureState::CANCELLED:
649 PanFinished( SetPanPosition( gesture.GetPosition() ), gesture.GetSpeed() );
652 case GestureState::CLEAR:
653 case GestureState::POSSIBLE:
661 void PageTurnView::PanStarted( const Vector2& gesturePosition )
663 mPressDownPosition = gesturePosition;
665 if( mTurningPageIndex == -1 )
670 mIndex = mTurningPageIndex % NUMBER_OF_CACHED_PAGES;
672 mOriginalCenter = gesturePosition;
674 mPageUpdated = false;
676 // Guard against destruction during signal emission
677 Toolkit::PageTurnView handle( GetOwner() );
678 mPagePanStartedSignal.Emit( handle );
681 void PageTurnView::PanContinuing( const Vector2& gesturePosition )
683 if( mTurningPageIndex == -1 )
688 // Guard against destruction during signal emission
689 Toolkit::PageTurnView handle( GetOwner() );
693 // when the touch down position is near the spine
694 // or when the panning goes outwards or some other position which would tear the paper in real situation
695 // we change the start position into the current panning position and update the shader parameters
696 if( mOriginalCenter.x < mPageSize.width*MINIMUM_START_POSITION_RATIO
697 || gesturePosition.x > mOriginalCenter.x-1.0f
698 || ( ( gesturePosition.x/mOriginalCenter.x > gesturePosition.y/mOriginalCenter.y ) &&
699 ( gesturePosition.x/mOriginalCenter.x > (gesturePosition.y-mPageSize.height )/(mOriginalCenter.y-mPageSize.height ) ) ) )
701 mOriginalCenter = gesturePosition;
705 mDistanceUpCorner = mOriginalCenter.Length();
706 mDistanceBottomCorner = ( mOriginalCenter - Vector2( 0.0f, mPageSize.height ) ).Length();
707 mShadowView.Add( mPages[mIndex].actor );
708 mPages[mIndex].UseEffect( mTurnEffectShader );
709 mPages[mIndex].SetOriginalCenter( mOriginalCenter );
710 mCurrentCenter = mOriginalCenter;
711 mPages[mIndex].SetCurrentCenter( mCurrentCenter );
712 mPanDisplacement = 0.f;
713 mConstraints = false;
717 mPageTurnStartedSignal.Emit( handle, static_cast<unsigned int>(mTurningPageIndex), !mPages[mIndex].isTurnBack );
718 int id = mTurningPageIndex + (mPages[mIndex].isTurnBack ? -1 : 1);
719 if( id >=0 && id < mTotalPageCount )
721 mPages[id%NUMBER_OF_CACHED_PAGES].actor.SetProperty( Actor::Property::VISIBLE,true);
724 mShadowView.RemoveConstraints();
726 mPages[mIndex].SetPanDisplacement( 0.f );
728 Constraint shadowBlurStrengthConstraint = Constraint::New<float>( mShadowView, mShadowView.GetBlurStrengthPropertyIndex(), ShadowBlurStrengthConstraint( mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO ) );
729 shadowBlurStrengthConstraint.AddSource( Source(mPages[mIndex].actor, mPages[mIndex].propertyCurrentCenter) );
730 shadowBlurStrengthConstraint.AddSource( Source(mPages[mIndex].actor, mPages[mIndex].propertyOriginalCenter) );
731 shadowBlurStrengthConstraint.AddSource( Source(mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement) );
732 shadowBlurStrengthConstraint.Apply();
737 Vector2 currentCenter = gesturePosition;
739 // Test whether the new current center would tear the paper from the top pine in real situation
740 // we do not forbid this totally, which would restrict the panning gesture too much
741 // instead, set it to the nearest allowable position
742 float distanceUpCorner = currentCenter.Length();
743 float distanceBottomCorner = ( currentCenter-Vector2( 0.0f, mPageSize.height ) ).Length();
744 if( distanceUpCorner > mDistanceUpCorner )
746 currentCenter = currentCenter*mDistanceUpCorner/distanceUpCorner;
748 // would tear the paper from the bottom spine in real situation
749 if( distanceBottomCorner > mDistanceBottomCorner )
751 currentCenter = ( ( currentCenter - Vector2( 0.0f, mPageSize.height ) )*mDistanceBottomCorner/distanceBottomCorner + Vector2(0.0f,mPageSize.height ) );
753 // If direction has a very high y component, reduce it.
754 Vector2 curveDirection = currentCenter - mOriginalCenter;
755 if( fabs( curveDirection.y ) > fabs( curveDirection.x ) )
757 currentCenter.y = mOriginalCenter.y + ( currentCenter.y - mOriginalCenter.y ) * fabs( curveDirection.x/curveDirection.y );
759 // If the vertical distance is high, reduce it
760 float yShift = currentCenter.y - mOriginalCenter.y;
761 if( fabs( yShift ) > mPageSize.height * MAXIMUM_VERTICAL_MOVEMENT_RATIO )
763 currentCenter.y = mOriginalCenter.y + yShift*mPageSize.height*MAXIMUM_VERTICAL_MOVEMENT_RATIO/fabs(yShift );
766 // use contraints to control the page shape and rotation when the pan position is near the spine
767 if( currentCenter.x <= mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO && mOriginalCenter.x > mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO )
769 // set the property values used by the constraints
770 mPanDisplacement = mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO - currentCenter.x;
771 mPages[mIndex].SetPanDisplacement( mPanDisplacement );
772 mPages[mIndex].SetPanCenter( currentCenter );
774 // set up the OriginalCenterConstraint and CurrentCebterConstraint to the PageTurnEdffect
775 // also set up the RotationConstraint to the page actor
779 // the corner position need to be a little far away from the page edge to ensure the whole page is lift up
780 if( currentCenter.y >= mOriginalCenter.y )
782 corner = Vector2( 1.1f*mPageSize.width, 0.f );
786 corner = mPageSize*1.1f;
789 Vector2 offset( currentCenter-mOriginalCenter );
790 float k = - ( (mOriginalCenter.x-corner.x)*offset.x + (mOriginalCenter.y-corner.y)*offset.y )
791 /( offset.x*offset.x + offset.y*offset.y );
795 Constraint originalCenterConstraint = Constraint::New<Vector2>( mPages[mIndex].actor, mPages[mIndex].propertyOriginalCenter, OriginalCenterConstraint( mOriginalCenter, offset ));
796 originalCenterConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement ) );
797 originalCenterConstraint.Apply();
799 Constraint currentCenterConstraint = Constraint::New<Vector2>( mPages[mIndex].actor, mPages[mIndex].propertyCurrentCenter, CurrentCenterConstraint(mPageSize.width));
800 currentCenterConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyPanCenter ) );
801 currentCenterConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyOriginalCenter ) );
802 currentCenterConstraint.Apply();
804 PageTurnApplyInternalConstraint( mPages[mIndex].actor, mPageSize.height );
806 float distance = offset.Length();
807 Constraint rotationConstraint = Constraint::New<Quaternion>( mPages[mIndex].actor, Actor::Property::ORIENTATION, RotationConstraint(distance, mPageSize.width, mPages[mIndex].isTurnBack));
808 rotationConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement ) );
809 rotationConstraint.Apply();
816 if(mConstraints) // remove the constraint is the pan position move back to far away from the spine
818 mPages[mIndex].actor.RemoveConstraints();
819 mPages[mIndex].SetOriginalCenter(mOriginalCenter );
820 mConstraints = false;
821 mPanDisplacement = 0.f;
824 mPages[mIndex].SetCurrentCenter( currentCenter );
825 mCurrentCenter = currentCenter;
826 PageTurnApplyInternalConstraint(mPages[mIndex].actor, mPageSize.height );
831 void PageTurnView::PanFinished( const Vector2& gesturePosition, float gestureSpeed )
833 // Guard against destruction during signal emission
834 Toolkit::PageTurnView handle( GetOwner() );
836 if( mTurningPageIndex == -1 )
838 if( mAnimatingCount< MAXIMUM_TURNING_NUM && mSlidingCount < 1)
840 OnPossibleOutwardsFlick( gesturePosition, gestureSpeed );
846 mPagePanFinishedSignal.Emit( handle );
850 if(mConstraints) // if with constraints, the pan finished position is near spine, set up an animation to turn the page over
852 // update the pages here instead of in the TurnedOver callback function
853 // as new page is allowed to respond to the pan gesture before other pages finishing animation
854 if(mPages[mIndex].isTurnBack)
857 RemovePage( mCurrentPageIndex+NUMBER_OF_CACHED_PAGES_EACH_SIDE );
858 AddPage( mCurrentPageIndex-NUMBER_OF_CACHED_PAGES_EACH_SIDE );
863 RemovePage( mCurrentPageIndex-NUMBER_OF_CACHED_PAGES_EACH_SIDE-1 );
864 AddPage( mCurrentPageIndex+NUMBER_OF_CACHED_PAGES_EACH_SIDE-1 );
868 // set up an animation to turn the page over
869 float width = mPageSize.width*(1.f+PAGE_TURN_OVER_THRESHOLD_RATIO);
870 Animation animation = Animation::New( std::max(0.1f,PAGE_TURN_OVER_ANIMATION_DURATION * (1.0f - mPanDisplacement / width)) );
871 animation.AnimateTo( Property(mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement),
872 width,AlphaFunction::EASE_OUT_SINE);
873 animation.AnimateTo( Property(mPages[mIndex].actor, mPages[mIndex].propertyPanCenter),
874 Vector2(-mPageSize.width*1.1f, 0.5f*mPageSize.height), AlphaFunction::EASE_OUT_SINE);
875 mAnimationPageIdPair[animation] = mTurningPageIndex;
877 animation.FinishedSignal().Connect( this, &PageTurnView::TurnedOver );
879 else // the pan finished position is far away from the spine, set up an animation to slide the page back instead of turning over
881 Animation animation= Animation::New( PAGE_SLIDE_BACK_ANIMATION_DURATION * (mOriginalCenter.x - mCurrentCenter.x) / mPageSize.width / PAGE_TURN_OVER_THRESHOLD_RATIO );
882 animation.AnimateTo( Property( mPages[mIndex].actor, mPages[mIndex].propertyCurrentCenter ),
883 mOriginalCenter, AlphaFunction::LINEAR );
884 mAnimationPageIdPair[animation] = mTurningPageIndex;
887 animation.FinishedSignal().Connect( this, &PageTurnView::SliddenBack );
889 mPageTurnStartedSignal.Emit( handle, static_cast<unsigned int>(mTurningPageIndex), mPages[mIndex].isTurnBack );
894 // In portrait view, an outwards flick should turn the previous page back
895 // In landscape view, nothing to do
896 OnPossibleOutwardsFlick( gesturePosition, gestureSpeed );
901 void PageTurnView::TurnedOver( Animation& animation )
903 int pageId = mAnimationPageIdPair[animation];
904 int index = pageId%NUMBER_OF_CACHED_PAGES;
906 mPages[index].ChangeTurnDirection();
907 mPages[index].actor.RemoveConstraints();
908 Self().Add(mPages[index].actor);
910 mAnimationPageIdPair.erase( animation );
912 float degree = mPages[index].isTurnBack ? 180.f : 0.f;
913 mPages[index].actor.SetProperty( Actor::Property::ORIENTATION, Quaternion( Degree(degree), Vector3::YAXIS ) );
914 mPages[index].UseEffect( mSpineEffectShader );
916 int id = pageId + (mPages[index].isTurnBack ? -1 : 1);
917 if( id >=0 && id < mTotalPageCount )
919 mPages[id%NUMBER_OF_CACHED_PAGES].actor.SetProperty( Actor::Property::VISIBLE,false);
922 OnTurnedOver( mPages[index].actor, mPages[index].isTurnBack );
924 // Guard against destruction during signal emission
925 Toolkit::PageTurnView handle( GetOwner() );
926 mPageTurnFinishedSignal.Emit( handle, static_cast<unsigned int>(pageId), mPages[index].isTurnBack );
929 void PageTurnView::SliddenBack( Animation& animation )
931 int pageId = mAnimationPageIdPair[animation];
932 int index = pageId%NUMBER_OF_CACHED_PAGES;
933 Self().Add(mPages[index].actor);
936 mAnimationPageIdPair.erase( animation );
938 mPages[index].UseEffect( mSpineEffectShader );
940 int id = pageId + (mPages[index].isTurnBack ? -1 : 1);
941 if( id >=0 && id < mTotalPageCount )
943 mPages[id%NUMBER_OF_CACHED_PAGES].actor.SetProperty( Actor::Property::VISIBLE,false);
946 // Guard against destruction during signal emission
947 Toolkit::PageTurnView handle( GetOwner() );
948 mPageTurnFinishedSignal.Emit( handle, static_cast<unsigned int>(pageId), mPages[index].isTurnBack );
951 void PageTurnView::OrganizePageDepth()
953 for( int i=0; i<NUMBER_OF_CACHED_PAGES_EACH_SIDE;i++ )
955 if(mCurrentPageIndex+i < mTotalPageCount)
957 mPages[( mCurrentPageIndex+i )%NUMBER_OF_CACHED_PAGES].actor.SetProperty( Actor::Property::POSITION_Z, -static_cast<float>( i )*STATIC_PAGE_INTERVAL_DISTANCE );
959 if( mCurrentPageIndex >= i + 1 )
961 mPages[( mCurrentPageIndex-i-1 )%NUMBER_OF_CACHED_PAGES].actor.SetProperty( Actor::Property::POSITION_Z, -static_cast<float>( i )*STATIC_PAGE_INTERVAL_DISTANCE );
966 void PageTurnView::StopTurning()
973 int index = mTurningPageIndex % NUMBER_OF_CACHED_PAGES;
974 Self().Add( mPages[ index ].actor );
975 mPages[ index ].actor.RemoveConstraints();
976 mPages[ index ].UseEffect( mSpineEffectShader );
977 float degree = mTurningPageIndex==mCurrentPageIndex ? 0.f :180.f;
978 mPages[index].actor.SetProperty( Actor::Property::ORIENTATION, Quaternion( Degree(degree), Vector3::YAXIS ) );
982 if( !mAnimationPageIdPair.empty() )
984 for (std::map<Animation,int>::iterator it=mAnimationPageIdPair.begin(); it!=mAnimationPageIdPair.end(); ++it)
986 static_cast<Animation>(it->first).SetCurrentProgress( 1.f );
991 Toolkit::PageTurnView::PageTurnSignal& PageTurnView::PageTurnStartedSignal()
993 return mPageTurnStartedSignal;
996 Toolkit::PageTurnView::PageTurnSignal& PageTurnView::PageTurnFinishedSignal()
998 return mPageTurnFinishedSignal;
1001 Toolkit::PageTurnView::PagePanSignal& PageTurnView::PagePanStartedSignal()
1003 return mPagePanStartedSignal;
1006 Toolkit::PageTurnView::PagePanSignal& PageTurnView::PagePanFinishedSignal()
1008 return mPagePanFinishedSignal;
1011 bool PageTurnView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1013 Dali::BaseHandle handle( object );
1015 bool connected( true );
1016 Toolkit::PageTurnView pageTurnView = Toolkit::PageTurnView::DownCast( handle );
1018 if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_TURN_STARTED ) )
1020 pageTurnView.PageTurnStartedSignal().Connect( tracker, functor );
1022 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_TURN_FINISHED ) )
1024 pageTurnView.PageTurnFinishedSignal().Connect( tracker, functor );
1026 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_PAN_STARTED ) )
1028 pageTurnView.PagePanStartedSignal().Connect( tracker, functor );
1030 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_PAN_FINISHED ) )
1032 pageTurnView.PagePanFinishedSignal().Connect( tracker, functor );
1036 // signalName does not match any signal
1043 void PageTurnView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1045 Toolkit::PageTurnView pageTurnView = Toolkit::PageTurnView::DownCast( Dali::BaseHandle( object ) );
1049 PageTurnView& pageTurnViewImpl( GetImplementation( pageTurnView ) );
1053 case Toolkit::PageTurnView::Property::VIEW_PAGE_SIZE:
1055 pageTurnViewImpl.SetPageSize( value.Get<Vector2>() );
1058 case Toolkit::PageTurnView::Property::CURRENT_PAGE_ID:
1060 pageTurnViewImpl.GoToPage( value.Get<int>() );
1063 case Toolkit::PageTurnView::Property::SPINE_SHADOW:
1065 pageTurnViewImpl.SetSpineShadowParameter( value.Get<Vector2>() );
1072 Property::Value PageTurnView::GetProperty( BaseObject* object, Property::Index index )
1074 Property::Value value;
1076 Toolkit::PageTurnView pageTurnView = Toolkit::PageTurnView::DownCast( Dali::BaseHandle( object ) );
1080 PageTurnView& pageTurnViewImpl( GetImplementation( pageTurnView ) );
1084 case Toolkit::PageTurnView::Property::VIEW_PAGE_SIZE:
1086 value = pageTurnViewImpl.GetPageSize();
1089 case Toolkit::PageTurnView::Property::CURRENT_PAGE_ID:
1091 value = static_cast<int>( pageTurnViewImpl.GetCurrentPage() );
1094 case Toolkit::PageTurnView::Property::SPINE_SHADOW:
1096 value = pageTurnViewImpl.GetSpineShadowParameter();
1104 } // namespace Internal
1106 } // namespace Toolkit