2 * Copyright (c) 2017 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/devel-api/visuals/visual-properties-devel.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>
38 namespace //Unnamed namespace
40 // properties set on shader, these properties have the constant value in regardless of the page status
41 const char * const PROPERTY_SPINE_SHADOW ( "uSpineShadowParameter" ); // uniform for both spine and turn effect
43 // properties set on actor, the value of these properties varies depending on the page status
44 // properties used in turn effect
45 const char * const PROPERTY_TURN_DIRECTION( "uIsTurningBack" ); // uniform
46 const char * const PROPERTY_COMMON_PARAMETERS( "uCommonParameters" ); //uniform
48 const char * const PROPERTY_PAN_DISPLACEMENT( "panDisplacement" );// property used to constrain the uniforms
49 const char * const PROPERTY_PAN_CENTER( "panCenter" );// property used to constrain the uniforms
51 // default grid density for page turn effect, 20 pixels by 20 pixels
52 const float DEFAULT_GRID_DENSITY(20.0f);
54 // to bent the page, the minimal horizontal pan start position is pageSize.x * MINIMUM_START_POSITION_RATIO
55 const float MINIMUM_START_POSITION_RATIO(0.6f);
57 // the maximum vertical displacement of pan gesture, if exceed, will reduce it: pageSize.y * MAXIMUM_VERTICAL_MOVEMENT_RATIO
58 const float MAXIMUM_VERTICAL_MOVEMENT_RATIO(0.15f);
60 // when the x component of pan position reaches pageSize.x * PAGE_TURN_OVER_THRESHOLD_RATIO, page starts to turn over
61 const float PAGE_TURN_OVER_THRESHOLD_RATIO(0.5f);
63 // duration of animation, shorter for faster speed
64 const float PAGE_SLIDE_BACK_ANIMATION_DURATION(1.0f);
65 const float PAGE_TURN_OVER_ANIMATION_DURATION(1.2f);
67 // the major&minor radius (in pixels) to form an ellipse shape
68 // the top-left quarter of this ellipse is used to calculate spine normal for simulating shadow
69 const Vector2 DEFAULT_SPINE_SHADOW_PARAMETER(50.0f, 20.0f);
71 // constants for shadow casting
72 const float POINT_LIGHT_HEIGHT_RATIO(2.f);
73 const Vector4 DEFAULT_SHADOW_COLOR = Vector4(0.2f, 0.2f, 0.2f, 0.5f);
75 // constraints ////////////////////////////////////////////////////////////////
77 * Original Center Constraint
79 * This constraint adjusts the original center property of the page turn shader effect
80 * based on the X-direction displacement of the pan gesture
82 struct OriginalCenterConstraint
84 OriginalCenterConstraint(const Vector2& originalCenter, const Vector2& offset)
85 : mOldCenter( originalCenter )
87 mNewCenter = originalCenter + offset;
88 mDistance = offset.Length() * 0.5f;
89 mDirection = offset / mDistance;
92 void operator()( Vector2& current, const PropertyInputContainer& inputs )
94 float displacement = inputs[0]->GetFloat();
96 if( displacement < mDistance )
98 current = mOldCenter + mDirection * displacement;
102 current = mNewCenter + Vector2(0.25f*(displacement-mDistance), 0.f);
113 * Rotation Constraint
115 * This constraint adjusts the rotation property of the page actor
116 * based on the X-direction displacement of the pan gesture
118 struct RotationConstraint
120 RotationConstraint( float distance, float pageWidth, bool isTurnBack )
121 : mDistance( distance*0.5f )
123 mStep = 1.f / pageWidth;
124 mSign = isTurnBack ? -1.0f : 1.0f;
125 mConst = isTurnBack ? -1.0f : 0.f;
126 mRotation = isTurnBack ? Quaternion( Radian( -Math::PI ), Vector3::YAXIS ) : Quaternion( Radian(0.f), Vector3::YAXIS );
129 void operator()( Quaternion& current, const PropertyInputContainer& inputs )
131 float displacement = inputs[0]->GetFloat();
132 if( displacement < mDistance)
138 float coef = std::max(-1.0f, mStep*(mDistance-displacement));
139 float angle = Math::PI * ( mConst + mSign * coef );
140 current = Quaternion( Radian( angle ), Vector3::YAXIS );
148 Quaternion mRotation;
152 * Current Center Constraint
154 * This constraint adjusts the current center property of the page turn shader effect
155 * based on the pan position and the original center position
157 struct CurrentCenterConstraint
159 CurrentCenterConstraint( float pageWidth )
160 : mPageWidth( pageWidth )
162 mThres = pageWidth * PAGE_TURN_OVER_THRESHOLD_RATIO * 0.5f;
165 void operator()( Vector2& current, const PropertyInputContainer& inputs )
167 const Vector2& centerPosition = inputs[0]->GetVector2();
168 if( centerPosition.x > 0.f )
170 current.x = mThres+centerPosition.x * 0.5f;
171 current.y = centerPosition.y;
175 const Vector2& centerOrigin = inputs[1]->GetVector2();
176 Vector2 direction = centerOrigin - Vector2(mThres, centerPosition.y);
177 float coef = 1.f+(centerPosition.x*2.f / mPageWidth);
178 // when coef <= 0, the page is flat, slow down the last moment of the page stretch by 10 times to avoid a small bounce
181 coef = (coef+0.225f)/10.0f;
183 current = centerOrigin - direction * coef;
191 struct ShadowBlurStrengthConstraint
193 ShadowBlurStrengthConstraint( float thres )
197 void operator()( float& blurStrength, const PropertyInputContainer& inputs )
199 float displacement = inputs[2]->GetFloat();
200 if( EqualsZero(displacement))
202 const Vector2& cur = inputs[0]->GetVector2();
203 const Vector2& ori = inputs[1]->GetVector2();
204 blurStrength = 5.f*(ori-cur).Length() / mThres;
208 blurStrength = 1.f - (displacement-2.f*mThres)/mThres;
211 blurStrength = blurStrength > 1.f ? 1.f : ( blurStrength < 0.f ? 0.f : blurStrength );
217 } //unnamed namespace
233 // empty handle as we cannot create PageTurnView(but type registered for page turn signal)
237 // Setup properties, signals and actions using the type-registry.
238 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PageTurnView, Toolkit::Control, Create );
240 DALI_PROPERTY_REGISTRATION( Toolkit, PageTurnView, "pageSize", VECTOR2, PAGE_SIZE )
241 DALI_PROPERTY_REGISTRATION( Toolkit, PageTurnView, "currentPageId", INTEGER, CURRENT_PAGE_ID )
242 DALI_PROPERTY_REGISTRATION( Toolkit, PageTurnView, "spineShadow", VECTOR2, SPINE_SHADOW )
244 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pageTurnStarted", SIGNAL_PAGE_TURN_STARTED )
245 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pageTurnFinished", SIGNAL_PAGE_TURN_FINISHED )
246 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pagePanStarted", SIGNAL_PAGE_PAN_STARTED )
247 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pagePanFinished", SIGNAL_PAGE_PAN_FINISHED )
249 DALI_TYPE_REGISTRATION_END()
253 // these several constants are also used in the derived classes
254 const char * const PageTurnView::PROPERTY_TEXTURE_WIDTH( "uTextureWidth" ); // uniform name
255 const char * const PageTurnView::PROPERTY_ORIGINAL_CENTER( "originalCenter" ); // property used to constrain the uniform
256 const char * const PageTurnView::PROPERTY_CURRENT_CENTER( "currentCenter" );// property used to constrain the uniform
257 const int PageTurnView::MAXIMUM_TURNING_NUM = 4;
258 const int PageTurnView::NUMBER_OF_CACHED_PAGES_EACH_SIDE = MAXIMUM_TURNING_NUM + 1;
259 const int PageTurnView::NUMBER_OF_CACHED_PAGES = NUMBER_OF_CACHED_PAGES_EACH_SIDE*2;
260 const float PageTurnView::STATIC_PAGE_INTERVAL_DISTANCE = 1.0f;
262 PageTurnView::Page::Page()
263 : isTurnBack( false )
265 actor = Actor::New();
266 actor.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
267 actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
268 actor.SetVisible( false );
270 propertyPanDisplacement = actor.RegisterProperty( PROPERTY_PAN_DISPLACEMENT, 0.f );
271 propertyPanCenter = actor.RegisterProperty(PROPERTY_PAN_CENTER, Vector2::ZERO);
273 propertyOriginalCenter = actor.RegisterProperty(PROPERTY_ORIGINAL_CENTER, Vector2::ZERO);
274 propertyCurrentCenter = actor.RegisterProperty(PROPERTY_CURRENT_CENTER, Vector2::ZERO);
275 Matrix zeroMatrix(true);
276 actor.RegisterProperty(PROPERTY_COMMON_PARAMETERS, zeroMatrix);
277 propertyTurnDirection = actor.RegisterProperty(PROPERTY_TURN_DIRECTION, -1.f);
280 void PageTurnView::Page::SetTexture( Texture texture )
284 textureSet = TextureSet::New();
286 textureSet.SetTexture( 0u, texture );
289 void PageTurnView::Page::UseEffect(Shader newShader)
294 renderer.SetShader( shader );
298 void PageTurnView::Page::UseEffect(Shader newShader, Geometry geometry)
300 UseEffect( newShader );
304 renderer = Renderer::New( geometry, shader );
308 textureSet = TextureSet::New();
311 renderer.SetTextures( textureSet );
312 renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
313 actor.AddRenderer( renderer );
317 void PageTurnView::Page::ChangeTurnDirection()
319 isTurnBack = !isTurnBack;
320 actor.SetProperty( propertyTurnDirection, isTurnBack ? 1.f : -1.f );
323 void PageTurnView::Page::SetPanDisplacement(float value)
325 actor.SetProperty( propertyPanDisplacement, value );
328 void PageTurnView::Page::SetPanCenter( const Vector2& value )
330 actor.SetProperty( propertyPanCenter, value );
333 void PageTurnView::Page::SetOriginalCenter( const Vector2& value )
335 actor.SetProperty( propertyOriginalCenter, value );
338 void PageTurnView::Page::SetCurrentCenter( const Vector2& value )
340 actor.SetProperty( propertyCurrentCenter, value );
343 PageTurnView::PageTurnView( PageFactory& pageFactory, const Vector2& pageSize )
344 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
345 mPageFactory( &pageFactory ),
346 mPageSize( pageSize ),
347 mSpineShadowParameter( DEFAULT_SPINE_SHADOW_PARAMETER ),
348 mDistanceUpCorner( 0.f ),
349 mDistanceBottomCorner( 0.f ),
350 mPanDisplacement( 0.f ),
351 mTotalPageCount( 0 ),
352 mCurrentPageIndex( 0 ),
353 mTurningPageIndex( 0 ),
356 mAnimatingCount( 0 ),
357 mConstraints( false ),
359 mPageUpdated( true ),
360 mPageTurnStartedSignal(),
361 mPageTurnFinishedSignal(),
362 mPagePanStartedSignal(),
363 mPagePanFinishedSignal()
367 PageTurnView::~PageTurnView()
371 void PageTurnView::OnInitialize()
373 // create the book spine effect for static pages
374 Property::Map spineEffectMap = CreatePageTurnBookSpineEffect();
375 mSpineEffectShader = CreateShader( spineEffectMap );
376 mSpineEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
377 // create the turn effect for turning pages
378 Property::Map turnEffectMap = CreatePageTurnEffect();
379 mTurnEffectShader = CreateShader( turnEffectMap );
380 mTurnEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
382 // create the grid geometry for pages
383 uint16_t width = static_cast<uint16_t>(mPageSize.width / DEFAULT_GRID_DENSITY + 0.5f);
384 uint16_t height = static_cast<uint16_t>(mPageSize.height / DEFAULT_GRID_DENSITY + 0.5f);
385 mGeometry = VisualFactoryCache::CreateGridGeometry( Uint16Pair( width, height ) );
387 mPages.reserve( NUMBER_OF_CACHED_PAGES );
388 for( int i = 0; i < NUMBER_OF_CACHED_PAGES; i++ )
390 mPages.push_back( Page() );
391 mPages[i].actor.SetSize( mPageSize );
392 Self().Add( mPages[i].actor );
395 // create the layer for turning pages
396 mTurningPageLayer = Layer::New();
397 mTurningPageLayer.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
398 mTurningPageLayer.SetBehavior(Layer::LAYER_3D);
399 mTurningPageLayer.Raise();
401 // Set control size and the parent origin of page layers
402 OnPageTurnViewInitialize();
404 Self().Add(mTurningPageLayer);
406 mTotalPageCount = static_cast<int>( mPageFactory->GetNumberOfPages() );
407 // add pages to the scene, and set depth for the stacked pages
408 for( int i = 0; i < NUMBER_OF_CACHED_PAGES_EACH_SIDE; i++ )
411 mPages[i].actor.SetZ( -static_cast<float>( i )*STATIC_PAGE_INTERVAL_DISTANCE );
413 mPages[0].actor.SetVisible(true);
415 // enable the pan gesture which is attached to the control
416 EnableGestureDetection(Gesture::Type(Gesture::Pan));
419 Shader PageTurnView::CreateShader( const Property::Map& shaderMap )
422 Property::Value* shaderValue = shaderMap.Find( Toolkit::DevelVisual::Property::SHADER, CUSTOM_SHADER );
423 Property::Map shaderSource;
424 if( shaderValue && shaderValue->Get( shaderSource ) )
426 std::string vertexShader;
427 Property::Value* vertexShaderValue = shaderSource.Find( Toolkit::Visual::Shader::Property::VERTEX_SHADER, CUSTOM_VERTEX_SHADER );
428 if( !vertexShaderValue || !vertexShaderValue->Get( vertexShader ) )
430 DALI_LOG_ERROR("PageTurnView::CreateShader failed: vertex shader source is not available.\n");
432 std::string fragmentShader;
433 Property::Value* fragmentShaderValue = shaderSource.Find( Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, CUSTOM_FRAGMENT_SHADER );
434 if( !fragmentShaderValue || !fragmentShaderValue->Get( fragmentShader ) )
436 DALI_LOG_ERROR("PageTurnView::CreateShader failed: fragment shader source is not available.\n");
438 shader = Shader::New( vertexShader, fragmentShader );
442 DALI_LOG_ERROR("PageTurnView::CreateShader failed: shader source is not available.\n");
448 void PageTurnView::SetupShadowView()
450 mShadowView = Toolkit::ShadowView::New( 0.25f, 0.25f );
451 Vector3 origin = mTurningPageLayer.GetCurrentParentOrigin();
452 mShadowView.SetParentOrigin( origin );
453 mShadowView.SetAnchorPoint( origin );
454 mShadowView.SetPointLightFieldOfView( Math::PI / 2.0f);
455 mShadowView.SetShadowColor(DEFAULT_SHADOW_COLOR);
457 mShadowPlaneBackground = Actor::New();
458 mShadowPlaneBackground.SetParentOrigin( ParentOrigin::CENTER );
459 mShadowPlaneBackground.SetSize( mControlSize );
460 Self().Add( mShadowPlaneBackground );
461 mShadowView.SetShadowPlaneBackground( mShadowPlaneBackground );
463 mPointLight = Actor::New();
464 mPointLight.SetAnchorPoint( origin );
465 mPointLight.SetParentOrigin( origin );
466 mPointLight.SetPosition( 0.f, 0.f, mPageSize.width*POINT_LIGHT_HEIGHT_RATIO );
467 Self().Add( mPointLight );
468 mShadowView.SetPointLight( mPointLight );
470 mTurningPageLayer.Add( mShadowView );
471 mShadowView.Activate();
474 void PageTurnView::OnStageConnection( int depth )
478 Control::OnStageConnection( depth );
481 void PageTurnView::OnStageDisconnection()
485 mShadowView.RemoveConstraints();
486 mPointLight.Unparent();
487 mShadowPlaneBackground.Unparent();
488 mShadowView.Unparent();
491 // make sure the status of the control is updated correctly when the pan gesture is interrupted
494 Control::OnStageDisconnection();
497 void PageTurnView::SetPageSize( const Vector2& pageSize )
499 mPageSize = pageSize;
503 mPointLight.SetPosition( 0.f, 0.f, mPageSize.width*POINT_LIGHT_HEIGHT_RATIO );
506 for( size_t i=0; i<mPages.size(); i++ )
508 mPages[i].actor.SetSize( mPageSize );
511 OnPageTurnViewInitialize();
513 if( mShadowPlaneBackground )
515 mShadowPlaneBackground.SetSize( mControlSize );
519 Vector2 PageTurnView::GetPageSize()
524 void PageTurnView::SetSpineShadowParameter( const Vector2& spineShadowParameter )
526 mSpineShadowParameter = spineShadowParameter;
528 // set spine shadow parameter to all the shader effects
529 mSpineEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
530 mTurnEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
533 Vector2 PageTurnView::GetSpineShadowParameter()
535 return mSpineShadowParameter;
538 void PageTurnView::GoToPage( unsigned int pageId )
540 int pageIdx = Clamp( static_cast<int>(pageId), 0, mTotalPageCount-1);
542 if( mCurrentPageIndex == pageIdx )
547 // if any animation ongoing, stop it.
550 // record the new current page index
551 mCurrentPageIndex = pageIdx;
554 // add the current page and the pages right before and after it
555 for( int i = pageIdx - NUMBER_OF_CACHED_PAGES_EACH_SIDE; i < pageIdx + NUMBER_OF_CACHED_PAGES_EACH_SIDE; i++ )
560 mPages[pageId%NUMBER_OF_CACHED_PAGES].actor.SetVisible(true);
563 mPages[(pageId-1)%NUMBER_OF_CACHED_PAGES].actor.SetVisible(true);
565 // set ordered depth to the stacked pages
569 unsigned int PageTurnView::GetCurrentPage()
571 DALI_ASSERT_ALWAYS( mCurrentPageIndex >= 0 );
572 return static_cast< unsigned int >( mCurrentPageIndex );
575 void PageTurnView::AddPage( int pageIndex )
577 if(pageIndex > -1 && pageIndex < mTotalPageCount) // whether the page is available from the page factory
579 int index = pageIndex % NUMBER_OF_CACHED_PAGES;
582 newPage = mPageFactory->NewPage( pageIndex );
583 DALI_ASSERT_ALWAYS( newPage && "must pass in valid texture" );
585 bool isLeftSide = ( pageIndex < mCurrentPageIndex );
586 if( mPages[index].isTurnBack != isLeftSide )
588 mPages[index].ChangeTurnDirection();
591 float degree = isLeftSide ? 180.f :0.f;
592 mPages[index].actor.SetOrientation( Degree( degree ), Vector3::YAXIS );
593 mPages[index].actor.SetVisible( false );
594 mPages[index].UseEffect( mSpineEffectShader, mGeometry );
595 mPages[index].SetTexture( newPage );
597 // For Portrait, nothing to do
598 // For Landscape, set the parent origin to CENTER
599 OnAddPage( mPages[index].actor, isLeftSide );
603 void PageTurnView::RemovePage( int pageIndex )
605 if( pageIndex > -1 && pageIndex < mTotalPageCount)
607 int index = pageIndex % NUMBER_OF_CACHED_PAGES;
608 mPages[index].actor.SetVisible(false);
612 void PageTurnView::OnPan( const PanGesture& gesture )
614 // the pan gesture is attached to control itself instead of each page
615 switch( gesture.state )
617 case Gesture::Started:
619 // check whether the undergoing turning page number already reaches the maximum allowed
620 if( mPageUpdated && mAnimatingCount< MAXIMUM_TURNING_NUM && mSlidingCount < 1 )
622 SetPanActor( gesture.position ); // determine which page actor is panned
623 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
625 mTurningPageIndex = -1;
627 PanStarted( SetPanPosition( gesture.position ) ); // pass in the pan position in the local page coordinate
631 mTurningPageIndex = -1;
635 case Gesture::Continuing:
637 PanContinuing( SetPanPosition( gesture.position ) ); // pass in the pan position in the local page coordinate
640 case Gesture::Finished:
641 case Gesture::Cancelled:
643 PanFinished( SetPanPosition( gesture.position ), gesture.GetSpeed() );
647 case Gesture::Possible:
655 void PageTurnView::PanStarted( const Vector2& gesturePosition )
657 mPressDownPosition = gesturePosition;
659 if( mTurningPageIndex == -1 )
664 mIndex = mTurningPageIndex % NUMBER_OF_CACHED_PAGES;
666 mOriginalCenter = gesturePosition;
668 mPageUpdated = false;
670 // Guard against destruction during signal emission
671 Toolkit::PageTurnView handle( GetOwner() );
672 mPagePanStartedSignal.Emit( handle );
675 void PageTurnView::PanContinuing( const Vector2& gesturePosition )
677 if( mTurningPageIndex == -1 )
682 // Guard against destruction during signal emission
683 Toolkit::PageTurnView handle( GetOwner() );
687 // when the touch down position is near the spine
688 // or when the panning goes outwards or some other position which would tear the paper in real situation
689 // we change the start position into the current panning position and update the shader parameters
690 if( mOriginalCenter.x < mPageSize.width*MINIMUM_START_POSITION_RATIO
691 || gesturePosition.x > mOriginalCenter.x-1.0f
692 || ( ( gesturePosition.x/mOriginalCenter.x > gesturePosition.y/mOriginalCenter.y ) &&
693 ( gesturePosition.x/mOriginalCenter.x > (gesturePosition.y-mPageSize.height )/(mOriginalCenter.y-mPageSize.height ) ) ) )
695 mOriginalCenter = gesturePosition;
699 mDistanceUpCorner = mOriginalCenter.Length();
700 mDistanceBottomCorner = ( mOriginalCenter - Vector2( 0.0f, mPageSize.height ) ).Length();
701 mShadowView.Add( mPages[mIndex].actor );
702 mPages[mIndex].UseEffect( mTurnEffectShader );
703 mPages[mIndex].SetOriginalCenter( mOriginalCenter );
704 mCurrentCenter = mOriginalCenter;
705 mPages[mIndex].SetCurrentCenter( mCurrentCenter );
706 mPanDisplacement = 0.f;
707 mConstraints = false;
711 mPageTurnStartedSignal.Emit( handle, static_cast<unsigned int>(mTurningPageIndex), !mPages[mIndex].isTurnBack );
712 int id = mTurningPageIndex + (mPages[mIndex].isTurnBack ? -1 : 1);
713 if( id >=0 && id < mTotalPageCount )
715 mPages[id%NUMBER_OF_CACHED_PAGES].actor.SetVisible(true);
718 mShadowView.RemoveConstraints();
720 mPages[mIndex].SetPanDisplacement( 0.f );
722 Constraint shadowBlurStrengthConstraint = Constraint::New<float>( mShadowView, mShadowView.GetBlurStrengthPropertyIndex(), ShadowBlurStrengthConstraint( mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO ) );
723 shadowBlurStrengthConstraint.AddSource( Source(mPages[mIndex].actor, mPages[mIndex].propertyCurrentCenter) );
724 shadowBlurStrengthConstraint.AddSource( Source(mPages[mIndex].actor, mPages[mIndex].propertyOriginalCenter) );
725 shadowBlurStrengthConstraint.AddSource( Source(mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement) );
726 shadowBlurStrengthConstraint.Apply();
731 Vector2 currentCenter = gesturePosition;
733 // Test whether the new current center would tear the paper from the top pine in real situation
734 // we do not forbid this totally, which would restrict the panning gesture too much
735 // instead, set it to the nearest allowable position
736 float distanceUpCorner = currentCenter.Length();
737 float distanceBottomCorner = ( currentCenter-Vector2( 0.0f, mPageSize.height ) ).Length();
738 if( distanceUpCorner > mDistanceUpCorner )
740 currentCenter = currentCenter*mDistanceUpCorner/distanceUpCorner;
742 // would tear the paper from the bottom spine in real situation
743 if( distanceBottomCorner > mDistanceBottomCorner )
745 currentCenter = ( ( currentCenter - Vector2( 0.0f, mPageSize.height ) )*mDistanceBottomCorner/distanceBottomCorner + Vector2(0.0f,mPageSize.height ) );
747 // If direction has a very high y component, reduce it.
748 Vector2 curveDirection = currentCenter - mOriginalCenter;
749 if( fabs( curveDirection.y ) > fabs( curveDirection.x ) )
751 currentCenter.y = mOriginalCenter.y + ( currentCenter.y - mOriginalCenter.y ) * fabs( curveDirection.x/curveDirection.y );
753 // If the vertical distance is high, reduce it
754 float yShift = currentCenter.y - mOriginalCenter.y;
755 if( fabs( yShift ) > mPageSize.height * MAXIMUM_VERTICAL_MOVEMENT_RATIO )
757 currentCenter.y = mOriginalCenter.y + yShift*mPageSize.height*MAXIMUM_VERTICAL_MOVEMENT_RATIO/fabs(yShift );
760 // use contraints to control the page shape and rotation when the pan position is near the spine
761 if( currentCenter.x <= mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO && mOriginalCenter.x > mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO )
763 // set the property values used by the constraints
764 mPanDisplacement = mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO - currentCenter.x;
765 mPages[mIndex].SetPanDisplacement( mPanDisplacement );
766 mPages[mIndex].SetPanCenter( currentCenter );
768 // set up the OriginalCenterConstraint and CurrentCebterConstraint to the PageTurnEdffect
769 // also set up the RotationConstraint to the page actor
773 // the corner position need to be a little far away from the page edge to ensure the whole page is lift up
774 if( currentCenter.y >= mOriginalCenter.y )
776 corner = Vector2( 1.1f*mPageSize.width, 0.f );
780 corner = mPageSize*1.1f;
783 Vector2 offset( currentCenter-mOriginalCenter );
784 float k = - ( (mOriginalCenter.x-corner.x)*offset.x + (mOriginalCenter.y-corner.y)*offset.y )
785 /( offset.x*offset.x + offset.y*offset.y );
789 Constraint originalCenterConstraint = Constraint::New<Vector2>( mPages[mIndex].actor, mPages[mIndex].propertyOriginalCenter, OriginalCenterConstraint( mOriginalCenter, offset ));
790 originalCenterConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement ) );
791 originalCenterConstraint.Apply();
793 Constraint currentCenterConstraint = Constraint::New<Vector2>( mPages[mIndex].actor, mPages[mIndex].propertyCurrentCenter, CurrentCenterConstraint(mPageSize.width));
794 currentCenterConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyPanCenter ) );
795 currentCenterConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyOriginalCenter ) );
796 currentCenterConstraint.Apply();
798 PageTurnApplyInternalConstraint( mPages[mIndex].actor, mPageSize.height );
800 float distance = offset.Length();
801 Constraint rotationConstraint = Constraint::New<Quaternion>( mPages[mIndex].actor, Actor::Property::ORIENTATION, RotationConstraint(distance, mPageSize.width, mPages[mIndex].isTurnBack));
802 rotationConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement ) );
803 rotationConstraint.Apply();
810 if(mConstraints) // remove the constraint is the pan position move back to far away from the spine
812 mPages[mIndex].actor.RemoveConstraints();
813 mPages[mIndex].SetOriginalCenter(mOriginalCenter );
814 mConstraints = false;
815 mPanDisplacement = 0.f;
818 mPages[mIndex].SetCurrentCenter( currentCenter );
819 mCurrentCenter = currentCenter;
820 PageTurnApplyInternalConstraint(mPages[mIndex].actor, mPageSize.height );
825 void PageTurnView::PanFinished( const Vector2& gesturePosition, float gestureSpeed )
827 // Guard against destruction during signal emission
828 Toolkit::PageTurnView handle( GetOwner() );
830 if( mTurningPageIndex == -1 )
832 if( mAnimatingCount< MAXIMUM_TURNING_NUM && mSlidingCount < 1)
834 OnPossibleOutwardsFlick( gesturePosition, gestureSpeed );
840 mPagePanFinishedSignal.Emit( handle );
844 if(mConstraints) // if with constraints, the pan finished position is near spine, set up an animation to turn the page over
846 // update the pages here instead of in the TurnedOver callback function
847 // as new page is allowed to respond to the pan gesture before other pages finishing animation
848 if(mPages[mIndex].isTurnBack)
851 RemovePage( mCurrentPageIndex+NUMBER_OF_CACHED_PAGES_EACH_SIDE );
852 AddPage( mCurrentPageIndex-NUMBER_OF_CACHED_PAGES_EACH_SIDE );
857 RemovePage( mCurrentPageIndex-NUMBER_OF_CACHED_PAGES_EACH_SIDE-1 );
858 AddPage( mCurrentPageIndex+NUMBER_OF_CACHED_PAGES_EACH_SIDE-1 );
862 // set up an animation to turn the page over
863 float width = mPageSize.width*(1.f+PAGE_TURN_OVER_THRESHOLD_RATIO);
864 Animation animation = Animation::New( std::max(0.1f,PAGE_TURN_OVER_ANIMATION_DURATION * (1.0f - mPanDisplacement / width)) );
865 animation.AnimateTo( Property(mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement),
866 width,AlphaFunction::EASE_OUT_SINE);
867 animation.AnimateTo( Property(mPages[mIndex].actor, mPages[mIndex].propertyPanCenter),
868 Vector2(-mPageSize.width*1.1f, 0.5f*mPageSize.height), AlphaFunction::EASE_OUT_SINE);
869 mAnimationPageIdPair[animation] = mTurningPageIndex;
871 animation.FinishedSignal().Connect( this, &PageTurnView::TurnedOver );
873 else // the pan finished position is far away from the spine, set up an animation to slide the page back instead of turning over
875 Animation animation= Animation::New( PAGE_SLIDE_BACK_ANIMATION_DURATION * (mOriginalCenter.x - mCurrentCenter.x) / mPageSize.width / PAGE_TURN_OVER_THRESHOLD_RATIO );
876 animation.AnimateTo( Property( mPages[mIndex].actor, mPages[mIndex].propertyCurrentCenter ),
877 mOriginalCenter, AlphaFunction::LINEAR );
878 mAnimationPageIdPair[animation] = mTurningPageIndex;
881 animation.FinishedSignal().Connect( this, &PageTurnView::SliddenBack );
883 mPageTurnStartedSignal.Emit( handle, static_cast<unsigned int>(mTurningPageIndex), mPages[mIndex].isTurnBack );
888 // In portrait view, an outwards flick should turn the previous page back
889 // In landscape view, nothing to do
890 OnPossibleOutwardsFlick( gesturePosition, gestureSpeed );
895 void PageTurnView::TurnedOver( Animation& animation )
897 int pageId = mAnimationPageIdPair[animation];
898 int index = pageId%NUMBER_OF_CACHED_PAGES;
900 mPages[index].ChangeTurnDirection();
901 mPages[index].actor.RemoveConstraints();
902 Self().Add(mPages[index].actor);
904 mAnimationPageIdPair.erase( animation );
906 float degree = mPages[index].isTurnBack ? 180.f : 0.f;
907 mPages[index].actor.SetOrientation( Degree(degree), Vector3::YAXIS );
908 mPages[index].UseEffect( mSpineEffectShader );
910 int id = pageId + (mPages[index].isTurnBack ? -1 : 1);
911 if( id >=0 && id < mTotalPageCount )
913 mPages[id%NUMBER_OF_CACHED_PAGES].actor.SetVisible(false);
916 OnTurnedOver( mPages[index].actor, mPages[index].isTurnBack );
918 // Guard against destruction during signal emission
919 Toolkit::PageTurnView handle( GetOwner() );
920 mPageTurnFinishedSignal.Emit( handle, static_cast<unsigned int>(pageId), mPages[index].isTurnBack );
923 void PageTurnView::SliddenBack( Animation& animation )
925 int pageId = mAnimationPageIdPair[animation];
926 int index = pageId%NUMBER_OF_CACHED_PAGES;
927 Self().Add(mPages[index].actor);
930 mAnimationPageIdPair.erase( animation );
932 mPages[index].UseEffect( mSpineEffectShader );
934 int id = pageId + (mPages[index].isTurnBack ? -1 : 1);
935 if( id >=0 && id < mTotalPageCount )
937 mPages[id%NUMBER_OF_CACHED_PAGES].actor.SetVisible(false);
940 // Guard against destruction during signal emission
941 Toolkit::PageTurnView handle( GetOwner() );
942 mPageTurnFinishedSignal.Emit( handle, static_cast<unsigned int>(pageId), mPages[index].isTurnBack );
945 void PageTurnView::OrganizePageDepth()
947 for( int i=0; i<NUMBER_OF_CACHED_PAGES_EACH_SIDE;i++ )
949 if(mCurrentPageIndex+i < mTotalPageCount)
951 mPages[( mCurrentPageIndex+i )%NUMBER_OF_CACHED_PAGES].actor.SetZ( -static_cast<float>( i )*STATIC_PAGE_INTERVAL_DISTANCE );
953 if( mCurrentPageIndex >= i + 1 )
955 mPages[( mCurrentPageIndex-i-1 )%NUMBER_OF_CACHED_PAGES].actor.SetZ( -static_cast<float>( i )*STATIC_PAGE_INTERVAL_DISTANCE );
960 void PageTurnView::StopTurning()
967 int index = mTurningPageIndex % NUMBER_OF_CACHED_PAGES;
968 Self().Add( mPages[ index ].actor );
969 mPages[ index ].actor.RemoveConstraints();
970 mPages[ index ].UseEffect( mSpineEffectShader );
971 float degree = mTurningPageIndex==mCurrentPageIndex ? 0.f :180.f;
972 mPages[index].actor.SetOrientation( Degree(degree), Vector3::YAXIS );
976 if( !mAnimationPageIdPair.empty() )
978 for (std::map<Animation,int>::iterator it=mAnimationPageIdPair.begin(); it!=mAnimationPageIdPair.end(); ++it)
980 static_cast<Animation>(it->first).SetCurrentProgress( 1.f );
985 Toolkit::PageTurnView::PageTurnSignal& PageTurnView::PageTurnStartedSignal()
987 return mPageTurnStartedSignal;
990 Toolkit::PageTurnView::PageTurnSignal& PageTurnView::PageTurnFinishedSignal()
992 return mPageTurnFinishedSignal;
995 Toolkit::PageTurnView::PagePanSignal& PageTurnView::PagePanStartedSignal()
997 return mPagePanStartedSignal;
1000 Toolkit::PageTurnView::PagePanSignal& PageTurnView::PagePanFinishedSignal()
1002 return mPagePanFinishedSignal;
1005 bool PageTurnView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1007 Dali::BaseHandle handle( object );
1009 bool connected( true );
1010 Toolkit::PageTurnView pageTurnView = Toolkit::PageTurnView::DownCast( handle );
1012 if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_TURN_STARTED ) )
1014 pageTurnView.PageTurnStartedSignal().Connect( tracker, functor );
1016 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_TURN_FINISHED ) )
1018 pageTurnView.PageTurnFinishedSignal().Connect( tracker, functor );
1020 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_PAN_STARTED ) )
1022 pageTurnView.PagePanStartedSignal().Connect( tracker, functor );
1024 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_PAN_FINISHED ) )
1026 pageTurnView.PagePanFinishedSignal().Connect( tracker, functor );
1030 // signalName does not match any signal
1037 void PageTurnView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1039 Toolkit::PageTurnView pageTurnView = Toolkit::PageTurnView::DownCast( Dali::BaseHandle( object ) );
1043 PageTurnView& pageTurnViewImpl( GetImplementation( pageTurnView ) );
1047 case Toolkit::PageTurnView::Property::PAGE_SIZE:
1049 pageTurnViewImpl.SetPageSize( value.Get<Vector2>() );
1052 case Toolkit::PageTurnView::Property::CURRENT_PAGE_ID:
1054 pageTurnViewImpl.GoToPage( value.Get<int>() );
1057 case Toolkit::PageTurnView::Property::SPINE_SHADOW:
1059 pageTurnViewImpl.SetSpineShadowParameter( value.Get<Vector2>() );
1066 Property::Value PageTurnView::GetProperty( BaseObject* object, Property::Index index )
1068 Property::Value value;
1070 Toolkit::PageTurnView pageTurnView = Toolkit::PageTurnView::DownCast( Dali::BaseHandle( object ) );
1074 PageTurnView& pageTurnViewImpl( GetImplementation( pageTurnView ) );
1078 case Toolkit::PageTurnView::Property::PAGE_SIZE:
1080 value = pageTurnViewImpl.GetPageSize();
1083 case Toolkit::PageTurnView::Property::CURRENT_PAGE_ID:
1085 value = static_cast<int>( pageTurnViewImpl.GetCurrentPage() );
1088 case Toolkit::PageTurnView::Property::SPINE_SHADOW:
1090 value = pageTurnViewImpl.GetSpineShadowParameter();
1098 } // namespace Internal
1100 } // namespace Toolkit