2 * Copyright (c) 2015 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/images/resource-image.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali/integration-api/debug.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/controls/renderers/renderer-factory-cache.h>
37 namespace //Unnamed namespace
39 // broken image is loaded if there is no valid image provided for the page
40 const char * const BROKEN_IMAGE_URL( DALI_IMAGE_DIR "broken.png");
42 // names of shader property map
43 const char * const CUSTOM_SHADER( "shader" );
44 const char * const CUSTOM_VERTEX_SHADER( "vertexShader" );
45 const char * const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
47 // properties set on shader, these properties have the constant value in regardless of the page status
48 const char * const PROPERTY_SPINE_SHADOW ( "uSpineShadowParameter" ); // uniform for both spine and turn effect
50 // properties set on actor, the value of these properties varies depending on the page status
51 // properties used in turn effect
52 const char * const PROPERTY_TURN_DIRECTION( "uIsTurningBack" ); // uniform
53 const char * const PROPERTY_COMMON_PARAMETERS( "uCommonParameters" ); //uniform
55 const char * const PROPERTY_PAN_DISPLACEMENT( "panDisplacement" );// property used to constrain the uniforms
56 const char * const PROPERTY_PAN_CENTER( "panCenter" );// property used to constrain the uniforms
58 // default grid density for page turn effect, 20 pixels by 20 pixels
59 const float DEFAULT_GRID_DENSITY(20.0f);
61 // to bent the page, the minimal horizontal pan start position is pageSize.x * MINIMUM_START_POSITION_RATIO
62 const float MINIMUM_START_POSITION_RATIO(0.6f);
64 // the maximum vertical displacement of pan gesture, if exceed, will reduce it: pageSize.y * MAXIMUM_VERTICAL_MOVEMENT_RATIO
65 const float MAXIMUM_VERTICAL_MOVEMENT_RATIO(0.15f);
67 // when the x component of pan position reaches pageSize.x * PAGE_TURN_OVER_THRESHOLD_RATIO, page starts to turn over
68 const float PAGE_TURN_OVER_THRESHOLD_RATIO(0.5f);
70 // duration of animation, shorter for faster speed
71 const float PAGE_SLIDE_BACK_ANIMATION_DURATION(1.0f);
72 const float PAGE_TURN_OVER_ANIMATION_DURATION(1.2f);
74 // the major&minor radius (in pixels) to form an ellipse shape
75 // the top-left quarter of this ellipse is used to calculate spine normal for simulating shadow
76 const Vector2 DEFAULT_SPINE_SHADOW_PARAMETER(50.0f, 20.0f);
78 // constants for shadow casting
79 const float POINT_LIGHT_HEIGHT_RATIO(2.f);
80 const Vector4 DEFAULT_SHADOW_COLOR = Vector4(0.2f, 0.2f, 0.2f, 0.5f);
82 // constraints ////////////////////////////////////////////////////////////////
84 * Original Center Constraint
86 * This constraint adjusts the original center property of the page turn shader effect
87 * based on the X-direction displacement of the pan gesture
89 struct OriginalCenterConstraint
91 OriginalCenterConstraint(const Vector2& originalCenter, const Vector2& offset)
92 : mOldCenter( originalCenter )
94 mNewCenter = originalCenter + offset;
95 mDistance = offset.Length() * 0.5f;
96 mDirection = offset / mDistance;
99 void operator()( Vector2& current, const PropertyInputContainer& inputs )
101 float displacement = inputs[0]->GetFloat();
103 if( displacement < mDistance )
105 current = mOldCenter + mDirection * displacement;
109 current = mNewCenter + Vector2(0.25f*(displacement-mDistance), 0.f);
120 * Rotation Constraint
122 * This constraint adjusts the rotation property of the page actor
123 * based on the X-direction displacement of the pan gesture
125 struct RotationConstraint
127 RotationConstraint( float distance, float pageWidth, bool isTurnBack )
128 : mDistance( distance*0.5f )
130 mStep = 1.f / pageWidth;
131 mSign = isTurnBack ? -1.0f : 1.0f;
132 mConst = isTurnBack ? -1.0f : 0.f;
133 mRotation = isTurnBack ? Quaternion( Radian( -Math::PI ), Vector3::YAXIS ) : Quaternion( Radian(0.f), Vector3::YAXIS );
136 void operator()( Quaternion& current, const PropertyInputContainer& inputs )
138 float displacement = inputs[0]->GetFloat();
139 if( displacement < mDistance)
145 float coef = std::max(-1.0f, mStep*(mDistance-displacement));
146 float angle = Math::PI * ( mConst + mSign * coef );
147 current = Quaternion( Radian( angle ), Vector3::YAXIS );
155 Quaternion mRotation;
159 * Current Center Constraint
161 * This constraint adjusts the current center property of the page turn shader effect
162 * based on the pan position and the original center position
164 struct CurrentCenterConstraint
166 CurrentCenterConstraint( float pageWidth )
167 : mPageWidth( pageWidth )
169 mThres = pageWidth * PAGE_TURN_OVER_THRESHOLD_RATIO * 0.5f;
172 void operator()( Vector2& current, const PropertyInputContainer& inputs )
174 const Vector2& centerPosition = inputs[0]->GetVector2();
175 if( centerPosition.x > 0.f )
177 current.x = mThres+centerPosition.x * 0.5f;
178 current.y = centerPosition.y;
182 const Vector2& centerOrigin = inputs[1]->GetVector2();
183 Vector2 direction = centerOrigin - Vector2(mThres, centerPosition.y);
184 float coef = 1.f+(centerPosition.x*2.f / mPageWidth);
185 // when coef <= 0, the page is flat, slow down the last moment of the page stretch by 10 times to avoid a small bounce
188 coef = (coef+0.225f)/10.0f;
190 current = centerOrigin - direction * coef;
198 struct ShadowBlurStrengthConstraint
200 ShadowBlurStrengthConstraint( float thres )
204 void operator()( float& blurStrength, const PropertyInputContainer& inputs )
206 float displacement = inputs[2]->GetFloat();
207 if( EqualsZero(displacement))
209 const Vector2& cur = inputs[0]->GetVector2();
210 const Vector2& ori = inputs[1]->GetVector2();
211 blurStrength = 5.f*(ori-cur).Length() / mThres;
215 blurStrength = 1.f - (displacement-2.f*mThres)/mThres;
218 blurStrength = blurStrength > 1.f ? 1.f : ( blurStrength < 0.f ? 0.f : blurStrength );
224 } //unnamed namespace
240 // empty handle as we cannot create PageTurnView(but type registered for page turn signal)
244 // Setup properties, signals and actions using the type-registry.
245 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PageTurnView, Toolkit::Control, Create );
247 DALI_PROPERTY_REGISTRATION( Toolkit, PageTurnView, "pageSize", VECTOR2, PAGE_SIZE )
248 DALI_PROPERTY_REGISTRATION( Toolkit, PageTurnView, "currentPageId", INTEGER, CURRENT_PAGE_ID )
249 DALI_PROPERTY_REGISTRATION( Toolkit, PageTurnView, "spineShadow", VECTOR2, SPINE_SHADOW )
251 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pageTurnStarted", SIGNAL_PAGE_TURN_STARTED )
252 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pageTurnFinished", SIGNAL_PAGE_TURN_FINISHED )
253 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pagePanStarted", SIGNAL_PAGE_PAN_STARTED )
254 DALI_SIGNAL_REGISTRATION( Toolkit, PageTurnView, "pagePanFinished", SIGNAL_PAGE_PAN_FINISHED )
256 DALI_TYPE_REGISTRATION_END()
260 // these several constants are also used in the derived classes
261 const char * const PageTurnView::PROPERTY_TEXTURE_WIDTH( "uTextureWidth" ); // uniform name
262 const char * const PageTurnView::PROPERTY_ORIGINAL_CENTER( "originalCenter" ); // property used to constrain the uniform
263 const char * const PageTurnView::PROPERTY_CURRENT_CENTER( "currentCenter" );// property used to constrain the uniform
264 const int PageTurnView::MAXIMUM_TURNING_NUM = 4;
265 const int PageTurnView::NUMBER_OF_CACHED_PAGES_EACH_SIDE = MAXIMUM_TURNING_NUM + 1;
266 const int PageTurnView::NUMBER_OF_CACHED_PAGES = NUMBER_OF_CACHED_PAGES_EACH_SIDE*2;
267 const float PageTurnView::STATIC_PAGE_INTERVAL_DISTANCE = 1.0f;
269 PageTurnView::Page::Page()
270 : isTurnBack( false )
272 actor = Actor::New();
273 actor.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
274 actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
275 actor.SetVisible( false );
277 propertyPanDisplacement = actor.RegisterProperty( PROPERTY_PAN_DISPLACEMENT, 0.f );
278 propertyPanCenter = actor.RegisterProperty(PROPERTY_PAN_CENTER, Vector2::ZERO);
280 propertyOriginalCenter = actor.RegisterProperty(PROPERTY_ORIGINAL_CENTER, Vector2::ZERO);
281 propertyCurrentCenter = actor.RegisterProperty(PROPERTY_CURRENT_CENTER, Vector2::ZERO);
282 Matrix zeroMatrix(true);
283 actor.RegisterProperty(PROPERTY_COMMON_PARAMETERS, zeroMatrix);
284 propertyTurnDirection = actor.RegisterProperty(PROPERTY_TURN_DIRECTION, -1.f);
287 void PageTurnView::Page::SetImage( Image image )
291 textureSet = TextureSet::New();
294 textureSet.SetImage( 0u, image );
297 void PageTurnView::Page::UseEffect(Shader newShader)
302 renderer.SetShader( shader );
306 void PageTurnView::Page::UseEffect(Shader newShader, Geometry geometry)
308 UseEffect( newShader );
312 renderer = Renderer::New( geometry, shader );
316 textureSet = TextureSet::New();
319 renderer.SetTextures( textureSet );
320 renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
321 actor.AddRenderer( renderer );
325 void PageTurnView::Page::ChangeTurnDirection()
327 isTurnBack = !isTurnBack;
328 actor.SetProperty( propertyTurnDirection, isTurnBack ? 1.f : -1.f );
331 void PageTurnView::Page::SetPanDisplacement(float value)
333 actor.SetProperty( propertyPanDisplacement, value );
336 void PageTurnView::Page::SetPanCenter( const Vector2& value )
338 actor.SetProperty( propertyPanCenter, value );
341 void PageTurnView::Page::SetOriginalCenter( const Vector2& value )
343 actor.SetProperty( propertyOriginalCenter, value );
346 void PageTurnView::Page::SetCurrentCenter( const Vector2& value )
348 actor.SetProperty( propertyCurrentCenter, value );
351 PageTurnView::PageTurnView( PageFactory& pageFactory, const Vector2& pageSize )
352 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
353 mPageFactory( &pageFactory ),
354 mPageSize( pageSize ),
355 mSpineShadowParameter( DEFAULT_SPINE_SHADOW_PARAMETER ),
356 mDistanceUpCorner( 0.f ),
357 mDistanceBottomCorner( 0.f ),
358 mPanDisplacement( 0.f ),
359 mTotalPageCount( 0 ),
360 mCurrentPageIndex( 0 ),
361 mTurningPageIndex( 0 ),
364 mAnimatingCount( 0 ),
365 mConstraints( false ),
367 mPageUpdated( true ),
368 mPageTurnStartedSignal(),
369 mPageTurnFinishedSignal(),
370 mPagePanStartedSignal(),
371 mPagePanFinishedSignal()
375 PageTurnView::~PageTurnView()
379 void PageTurnView::OnInitialize()
381 // create the book spine effect for static pages
382 Property::Map spineEffectMap = CreatePageTurnBookSpineEffect();
383 mSpineEffectShader = CreateShader( spineEffectMap );
384 mSpineEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
385 // create the turn effect for turning pages
386 Property::Map turnEffectMap = CreatePageTurnEffect();
387 mTurnEffectShader = CreateShader( turnEffectMap );
388 mTurnEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
390 // create the grid geometry for pages
391 uint16_t width = static_cast<uint16_t>(mPageSize.width / DEFAULT_GRID_DENSITY + 0.5f);
392 uint16_t height = static_cast<uint16_t>(mPageSize.height / DEFAULT_GRID_DENSITY + 0.5f);
393 mGeometry = RendererFactoryCache::CreateGridGeometry( Uint16Pair( width, height ) );
395 mPages.reserve( NUMBER_OF_CACHED_PAGES );
396 for( int i = 0; i < NUMBER_OF_CACHED_PAGES; i++ )
398 mPages.push_back( Page() );
399 mPages[i].actor.SetSize( mPageSize );
400 Self().Add( mPages[i].actor );
403 // create the layer for turning images
404 mTurningPageLayer = Layer::New();
405 mTurningPageLayer.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
406 mTurningPageLayer.SetBehavior(Layer::LAYER_3D);
407 mTurningPageLayer.Raise();
409 // Set control size and the parent origin of page layers
410 OnPageTurnViewInitialize();
412 Self().Add(mTurningPageLayer);
414 mTotalPageCount = static_cast<int>( mPageFactory->GetNumberOfPages() );
415 // add pages to the scene, and set depth for the stacked pages
416 for( int i = 0; i < NUMBER_OF_CACHED_PAGES_EACH_SIDE; i++ )
419 mPages[i].actor.SetZ( -static_cast<float>( i )*STATIC_PAGE_INTERVAL_DISTANCE );
421 mPages[0].actor.SetVisible(true);
423 // enable the pan gesture which is attached to the control
424 EnableGestureDetection(Gesture::Type(Gesture::Pan));
427 Shader PageTurnView::CreateShader( const Property::Map& shaderMap )
430 Property::Value* shaderValue = shaderMap.Find( CUSTOM_SHADER );
431 Property::Map shaderSource;
432 if( shaderValue && shaderValue->Get( shaderSource ) )
434 std::string vertexShader;
435 Property::Value* vertexShaderValue = shaderSource.Find( CUSTOM_VERTEX_SHADER );
436 if( !vertexShaderValue || !vertexShaderValue->Get( vertexShader ) )
438 DALI_LOG_ERROR("PageTurnView::CreateShader failed: vertex shader source is not available.\n");
440 std::string fragmentShader;
441 Property::Value* fragmentShaderValue = shaderSource.Find( CUSTOM_FRAGMENT_SHADER );
442 if( !fragmentShaderValue || !fragmentShaderValue->Get( fragmentShader ) )
444 DALI_LOG_ERROR("PageTurnView::CreateShader failed: fragment shader source is not available.\n");
446 shader = Shader::New( vertexShader, fragmentShader );
450 DALI_LOG_ERROR("PageTurnView::CreateShader failed: shader source is not available.\n");
456 void PageTurnView::SetupShadowView()
458 mShadowView = Toolkit::ShadowView::New( 0.25f, 0.25f );
459 Vector3 origin = mTurningPageLayer.GetCurrentParentOrigin();
460 mShadowView.SetParentOrigin( origin );
461 mShadowView.SetAnchorPoint( origin );
462 mShadowView.SetPointLightFieldOfView( Math::PI / 2.0f);
463 mShadowView.SetShadowColor(DEFAULT_SHADOW_COLOR);
465 mShadowPlaneBackground = Actor::New();
466 mShadowPlaneBackground.SetParentOrigin( ParentOrigin::CENTER );
467 mShadowPlaneBackground.SetSize( mControlSize );
468 Self().Add( mShadowPlaneBackground );
469 mShadowView.SetShadowPlaneBackground( mShadowPlaneBackground );
471 mPointLight = Actor::New();
472 mPointLight.SetAnchorPoint( origin );
473 mPointLight.SetParentOrigin( origin );
474 mPointLight.SetPosition( 0.f, 0.f, mPageSize.width*POINT_LIGHT_HEIGHT_RATIO );
475 Self().Add( mPointLight );
476 mShadowView.SetPointLight( mPointLight );
478 mTurningPageLayer.Add( mShadowView );
479 mShadowView.Activate();
482 void PageTurnView::OnStageConnection( int depth )
484 Control::OnStageConnection( depth );
489 void PageTurnView::OnStageDisconnection()
493 mShadowView.RemoveConstraints();
494 mPointLight.Unparent();
495 mShadowPlaneBackground.Unparent();
496 mShadowView.Unparent();
499 // make sure the status of the control is updated correctly when the pan gesture is interrupted
502 Control::OnStageDisconnection();
505 void PageTurnView::SetPageSize( const Vector2& pageSize )
507 mPageSize = pageSize;
511 mPointLight.SetPosition( 0.f, 0.f, mPageSize.width*POINT_LIGHT_HEIGHT_RATIO );
514 for( size_t i=0; i<mPages.size(); i++ )
516 mPages[i].actor.SetSize( mPageSize );
519 OnPageTurnViewInitialize();
521 if( mShadowPlaneBackground )
523 mShadowPlaneBackground.SetSize( mControlSize );
527 Vector2 PageTurnView::GetPageSize()
532 void PageTurnView::SetSpineShadowParameter( const Vector2& spineShadowParameter )
534 mSpineShadowParameter = spineShadowParameter;
536 // set spine shadow parameter to all the shader effects
537 mSpineEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
538 mTurnEffectShader.RegisterProperty(PROPERTY_SPINE_SHADOW, mSpineShadowParameter );
541 Vector2 PageTurnView::GetSpineShadowParameter()
543 return mSpineShadowParameter;
546 void PageTurnView::GoToPage( unsigned int pageId )
548 int pageIdx = Clamp( static_cast<int>(pageId), 0, mTotalPageCount-1);
550 if( mCurrentPageIndex == pageIdx )
555 // if any animation ongoing, stop it.
558 // record the new current page index
559 mCurrentPageIndex = pageIdx;
562 // add the current page and the pages right before and after it
563 for( int i = pageIdx - NUMBER_OF_CACHED_PAGES_EACH_SIDE; i < pageIdx + NUMBER_OF_CACHED_PAGES_EACH_SIDE; i++ )
568 mPages[pageId%NUMBER_OF_CACHED_PAGES].actor.SetVisible(true);
571 mPages[(pageId-1)%NUMBER_OF_CACHED_PAGES].actor.SetVisible(true);
573 // set ordered depth to the stacked pages
577 unsigned int PageTurnView::GetCurrentPage()
579 DALI_ASSERT_ALWAYS( mCurrentPageIndex >= 0 );
580 return static_cast< unsigned int >( mCurrentPageIndex );
583 void PageTurnView::AddPage( int pageIndex )
585 if(pageIndex > -1 && pageIndex < mTotalPageCount) // whether the page is available from the page factory
587 int index = pageIndex % NUMBER_OF_CACHED_PAGES;
590 newPageImage = mPageFactory->NewPage( pageIndex );
592 if( !newPageImage ) // load the broken image
594 newPageImage = ResourceImage::New( BROKEN_IMAGE_URL );
597 bool isLeftSide = ( pageIndex < mCurrentPageIndex );
598 if( mPages[index].isTurnBack != isLeftSide )
600 mPages[index].ChangeTurnDirection();
603 float degree = isLeftSide ? 180.f :0.f;
604 mPages[index].actor.SetOrientation( Degree( degree ), Vector3::YAXIS );
605 mPages[index].actor.SetVisible( false );
606 mPages[index].UseEffect( mSpineEffectShader, mGeometry );
607 mPages[index].SetImage( newPageImage );
609 // For Portrait, nothing to do
610 // For Landscape, set the parent origin to CENTER
611 OnAddPage( mPages[index].actor, isLeftSide );
615 void PageTurnView::RemovePage( int pageIndex )
617 if( pageIndex > -1 && pageIndex < mTotalPageCount)
619 int index = pageIndex % NUMBER_OF_CACHED_PAGES;
620 mPages[index].actor.SetVisible(false);
624 void PageTurnView::OnPan( const PanGesture& gesture )
626 // the pan gesture is attached to control itself instead of each page
627 switch( gesture.state )
629 case Gesture::Started:
631 // check whether the undergoing turning page number already reaches the maximum allowed
632 if( mPageUpdated && mAnimatingCount< MAXIMUM_TURNING_NUM && mSlidingCount < 1 )
634 SetPanActor( gesture.position ); // determine which page actor is panned
635 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
637 mTurningPageIndex = -1;
639 PanStarted( SetPanPosition( gesture.position ) ); // pass in the pan position in the local page coordinate
643 mTurningPageIndex = -1;
647 case Gesture::Continuing:
649 PanContinuing( SetPanPosition( gesture.position ) ); // pass in the pan position in the local page coordinate
652 case Gesture::Finished:
653 case Gesture::Cancelled:
655 PanFinished( SetPanPosition( gesture.position ), gesture.GetSpeed() );
659 case Gesture::Possible:
667 void PageTurnView::PanStarted( const Vector2& gesturePosition )
669 mPressDownPosition = gesturePosition;
671 if( mTurningPageIndex == -1 )
676 mIndex = mTurningPageIndex % NUMBER_OF_CACHED_PAGES;
678 mOriginalCenter = gesturePosition;
680 mPageUpdated = false;
682 // Guard against destruction during signal emission
683 Toolkit::PageTurnView handle( GetOwner() );
684 mPagePanStartedSignal.Emit( handle );
687 void PageTurnView::PanContinuing( const Vector2& gesturePosition )
689 if( mTurningPageIndex == -1 )
694 // Guard against destruction during signal emission
695 Toolkit::PageTurnView handle( GetOwner() );
699 // when the touch down position is near the spine
700 // or when the panning goes outwards or some other position which would tear the paper in real situation
701 // we change the start position into the current panning position and update the shader parameters
702 if( mOriginalCenter.x < mPageSize.width*MINIMUM_START_POSITION_RATIO
703 || gesturePosition.x > mOriginalCenter.x-1.0f
704 || ( ( gesturePosition.x/mOriginalCenter.x > gesturePosition.y/mOriginalCenter.y ) &&
705 ( gesturePosition.x/mOriginalCenter.x > (gesturePosition.y-mPageSize.height )/(mOriginalCenter.y-mPageSize.height ) ) ) )
707 mOriginalCenter = gesturePosition;
711 mDistanceUpCorner = mOriginalCenter.Length();
712 mDistanceBottomCorner = ( mOriginalCenter - Vector2( 0.0f, mPageSize.height ) ).Length();
713 mShadowView.Add( mPages[mIndex].actor );
714 mPages[mIndex].UseEffect( mTurnEffectShader );
715 mPages[mIndex].SetOriginalCenter( mOriginalCenter );
716 mCurrentCenter = mOriginalCenter;
717 mPages[mIndex].SetCurrentCenter( mCurrentCenter );
718 mPanDisplacement = 0.f;
719 mConstraints = false;
723 mPageTurnStartedSignal.Emit( handle, static_cast<unsigned int>(mTurningPageIndex), !mPages[mIndex].isTurnBack );
724 int id = mTurningPageIndex + (mPages[mIndex].isTurnBack ? -1 : 1);
725 if( id >=0 && id < mTotalPageCount )
727 mPages[id%NUMBER_OF_CACHED_PAGES].actor.SetVisible(true);
730 mShadowView.RemoveConstraints();
732 mPages[mIndex].SetPanDisplacement( 0.f );
734 Constraint shadowBlurStrengthConstraint = Constraint::New<float>( mShadowView, mShadowView.GetBlurStrengthPropertyIndex(), ShadowBlurStrengthConstraint( mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO ) );
735 shadowBlurStrengthConstraint.AddSource( Source(mPages[mIndex].actor, mPages[mIndex].propertyCurrentCenter) );
736 shadowBlurStrengthConstraint.AddSource( Source(mPages[mIndex].actor, mPages[mIndex].propertyOriginalCenter) );
737 shadowBlurStrengthConstraint.AddSource( Source(mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement) );
738 shadowBlurStrengthConstraint.Apply();
743 Vector2 currentCenter = gesturePosition;
745 // Test whether the new current center would tear the paper from the top pine in real situation
746 // we do not forbid this totally, which would restrict the panning gesture too much
747 // instead, set it to the nearest allowable position
748 float distanceUpCorner = currentCenter.Length();
749 float distanceBottomCorner = ( currentCenter-Vector2( 0.0f, mPageSize.height ) ).Length();
750 if( distanceUpCorner > mDistanceUpCorner )
752 currentCenter = currentCenter*mDistanceUpCorner/distanceUpCorner;
754 // would tear the paper from the bottom spine in real situation
755 if( distanceBottomCorner > mDistanceBottomCorner )
757 currentCenter = ( ( currentCenter - Vector2( 0.0f, mPageSize.height ) )*mDistanceBottomCorner/distanceBottomCorner + Vector2(0.0f,mPageSize.height ) );
759 // If direction has a very high y component, reduce it.
760 Vector2 curveDirection = currentCenter - mOriginalCenter;
761 if( fabs( curveDirection.y ) > fabs( curveDirection.x ) )
763 currentCenter.y = mOriginalCenter.y + ( currentCenter.y - mOriginalCenter.y ) * fabs( curveDirection.x/curveDirection.y );
765 // If the vertical distance is high, reduce it
766 float yShift = currentCenter.y - mOriginalCenter.y;
767 if( fabs( yShift ) > mPageSize.height * MAXIMUM_VERTICAL_MOVEMENT_RATIO )
769 currentCenter.y = mOriginalCenter.y + yShift*mPageSize.height*MAXIMUM_VERTICAL_MOVEMENT_RATIO/fabs(yShift );
772 // use contraints to control the page shape and rotation when the pan position is near the spine
773 if( currentCenter.x <= mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO && mOriginalCenter.x > mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO )
775 // set the property values used by the constraints
776 mPanDisplacement = mPageSize.width*PAGE_TURN_OVER_THRESHOLD_RATIO - currentCenter.x;
777 mPages[mIndex].SetPanDisplacement( mPanDisplacement );
778 mPages[mIndex].SetPanCenter( currentCenter );
780 // set up the OriginalCenterConstraint and CurrentCebterConstraint to the PageTurnEdffect
781 // also set up the RotationConstraint to the page actor
785 // the corner position need to be a little far away from the page edge to ensure the whole page is lift up
786 if( currentCenter.y >= mOriginalCenter.y )
788 corner = Vector2( 1.1f*mPageSize.width, 0.f );
792 corner = mPageSize*1.1f;
795 Vector2 offset( currentCenter-mOriginalCenter );
796 float k = - ( (mOriginalCenter.x-corner.x)*offset.x + (mOriginalCenter.y-corner.y)*offset.y )
797 /( offset.x*offset.x + offset.y*offset.y );
801 Constraint originalCenterConstraint = Constraint::New<Vector2>( mPages[mIndex].actor, mPages[mIndex].propertyOriginalCenter, OriginalCenterConstraint( mOriginalCenter, offset ));
802 originalCenterConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement ) );
803 originalCenterConstraint.Apply();
805 Constraint currentCenterConstraint = Constraint::New<Vector2>( mPages[mIndex].actor, mPages[mIndex].propertyCurrentCenter, CurrentCenterConstraint(mPageSize.width));
806 currentCenterConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyPanCenter ) );
807 currentCenterConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyOriginalCenter ) );
808 currentCenterConstraint.Apply();
810 PageTurnApplyInternalConstraint( mPages[mIndex].actor, mPageSize.height );
812 float distance = offset.Length();
813 Constraint rotationConstraint = Constraint::New<Quaternion>( mPages[mIndex].actor, Actor::Property::ORIENTATION, RotationConstraint(distance, mPageSize.width, mPages[mIndex].isTurnBack));
814 rotationConstraint.AddSource( Source( mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement ) );
815 rotationConstraint.Apply();
822 if(mConstraints) // remove the constraint is the pan position move back to far away from the spine
824 mPages[mIndex].actor.RemoveConstraints();
825 mPages[mIndex].SetOriginalCenter(mOriginalCenter );
826 mConstraints = false;
827 mPanDisplacement = 0.f;
830 mPages[mIndex].SetCurrentCenter( currentCenter );
831 mCurrentCenter = currentCenter;
832 PageTurnApplyInternalConstraint(mPages[mIndex].actor, mPageSize.height );
837 void PageTurnView::PanFinished( const Vector2& gesturePosition, float gestureSpeed )
839 // Guard against destruction during signal emission
840 Toolkit::PageTurnView handle( GetOwner() );
842 if( mTurningPageIndex == -1 )
844 if( mAnimatingCount< MAXIMUM_TURNING_NUM && mSlidingCount < 1)
846 OnPossibleOutwardsFlick( gesturePosition, gestureSpeed );
852 mPagePanFinishedSignal.Emit( handle );
856 if(mConstraints) // if with constraints, the pan finished position is near spine, set up an animation to turn the page over
858 // update the pages here instead of in the TurnedOver callback function
859 // as new page is allowed to respond to the pan gesture before other pages finishing animation
860 if(mPages[mIndex].isTurnBack)
863 RemovePage( mCurrentPageIndex+NUMBER_OF_CACHED_PAGES_EACH_SIDE );
864 AddPage( mCurrentPageIndex-NUMBER_OF_CACHED_PAGES_EACH_SIDE );
869 RemovePage( mCurrentPageIndex-NUMBER_OF_CACHED_PAGES_EACH_SIDE-1 );
870 AddPage( mCurrentPageIndex+NUMBER_OF_CACHED_PAGES_EACH_SIDE-1 );
874 // set up an animation to turn the page over
875 float width = mPageSize.width*(1.f+PAGE_TURN_OVER_THRESHOLD_RATIO);
876 Animation animation = Animation::New( std::max(0.1f,PAGE_TURN_OVER_ANIMATION_DURATION * (1.0f - mPanDisplacement / width)) );
877 animation.AnimateTo( Property(mPages[mIndex].actor, mPages[mIndex].propertyPanDisplacement),
878 width,AlphaFunction::EASE_OUT_SINE);
879 animation.AnimateTo( Property(mPages[mIndex].actor, mPages[mIndex].propertyPanCenter),
880 Vector2(-mPageSize.width*1.1f, 0.5f*mPageSize.height), AlphaFunction::EASE_OUT_SINE);
881 mAnimationPageIdPair[animation] = mTurningPageIndex;
883 animation.FinishedSignal().Connect( this, &PageTurnView::TurnedOver );
885 else // the pan finished position is far away from the spine, set up an animation to slide the page back instead of turning over
887 Animation animation= Animation::New( PAGE_SLIDE_BACK_ANIMATION_DURATION * (mOriginalCenter.x - mCurrentCenter.x) / mPageSize.width / PAGE_TURN_OVER_THRESHOLD_RATIO );
888 animation.AnimateTo( Property( mPages[mIndex].actor, mPages[mIndex].propertyCurrentCenter ),
889 mOriginalCenter, AlphaFunction::LINEAR );
890 mAnimationPageIdPair[animation] = mTurningPageIndex;
893 animation.FinishedSignal().Connect( this, &PageTurnView::SliddenBack );
895 mPageTurnStartedSignal.Emit( handle, static_cast<unsigned int>(mTurningPageIndex), mPages[mIndex].isTurnBack );
900 // In portrait view, an outwards flick should turn the previous page back
901 // In landscape view, nothing to do
902 OnPossibleOutwardsFlick( gesturePosition, gestureSpeed );
907 void PageTurnView::TurnedOver( Animation& animation )
909 int pageId = mAnimationPageIdPair[animation];
910 int index = pageId%NUMBER_OF_CACHED_PAGES;
912 mPages[index].ChangeTurnDirection();
913 mPages[index].actor.RemoveConstraints();
914 Self().Add(mPages[index].actor);
916 mAnimationPageIdPair.erase( animation );
918 float degree = mPages[index].isTurnBack ? 180.f : 0.f;
919 mPages[index].actor.SetOrientation( Degree(degree), Vector3::YAXIS );
920 mPages[index].UseEffect( mSpineEffectShader );
922 int id = pageId + (mPages[index].isTurnBack ? -1 : 1);
923 if( id >=0 && id < mTotalPageCount )
925 mPages[id%NUMBER_OF_CACHED_PAGES].actor.SetVisible(false);
928 OnTurnedOver( mPages[index].actor, mPages[index].isTurnBack );
930 // Guard against destruction during signal emission
931 Toolkit::PageTurnView handle( GetOwner() );
932 mPageTurnFinishedSignal.Emit( handle, static_cast<unsigned int>(pageId), mPages[index].isTurnBack );
935 void PageTurnView::SliddenBack( Animation& animation )
937 int pageId = mAnimationPageIdPair[animation];
938 int index = pageId%NUMBER_OF_CACHED_PAGES;
939 Self().Add(mPages[index].actor);
942 mAnimationPageIdPair.erase( animation );
944 mPages[index].UseEffect( mSpineEffectShader );
946 int id = pageId + (mPages[index].isTurnBack ? -1 : 1);
947 if( id >=0 && id < mTotalPageCount )
949 mPages[id%NUMBER_OF_CACHED_PAGES].actor.SetVisible(false);
952 // Guard against destruction during signal emission
953 Toolkit::PageTurnView handle( GetOwner() );
954 mPageTurnFinishedSignal.Emit( handle, static_cast<unsigned int>(pageId), mPages[index].isTurnBack );
957 void PageTurnView::OrganizePageDepth()
959 for( int i=0; i<NUMBER_OF_CACHED_PAGES_EACH_SIDE;i++ )
961 if(mCurrentPageIndex+i < mTotalPageCount)
963 mPages[( mCurrentPageIndex+i )%NUMBER_OF_CACHED_PAGES].actor.SetZ( -static_cast<float>( i )*STATIC_PAGE_INTERVAL_DISTANCE );
965 if( mCurrentPageIndex >= i + 1 )
967 mPages[( mCurrentPageIndex-i-1 )%NUMBER_OF_CACHED_PAGES].actor.SetZ( -static_cast<float>( i )*STATIC_PAGE_INTERVAL_DISTANCE );
972 void PageTurnView::StopTurning()
979 int index = mTurningPageIndex % NUMBER_OF_CACHED_PAGES;
980 Self().Add( mPages[ index ].actor );
981 mPages[ index ].actor.RemoveConstraints();
982 mPages[ index ].UseEffect( mSpineEffectShader );
983 float degree = mTurningPageIndex==mCurrentPageIndex ? 0.f :180.f;
984 mPages[index].actor.SetOrientation( Degree(degree), Vector3::YAXIS );
988 if( !mAnimationPageIdPair.empty() )
990 for (std::map<Animation,int>::iterator it=mAnimationPageIdPair.begin(); it!=mAnimationPageIdPair.end(); ++it)
992 static_cast<Animation>(it->first).SetCurrentProgress( 1.f );
997 Toolkit::PageTurnView::PageTurnSignal& PageTurnView::PageTurnStartedSignal()
999 return mPageTurnStartedSignal;
1002 Toolkit::PageTurnView::PageTurnSignal& PageTurnView::PageTurnFinishedSignal()
1004 return mPageTurnFinishedSignal;
1007 Toolkit::PageTurnView::PagePanSignal& PageTurnView::PagePanStartedSignal()
1009 return mPagePanStartedSignal;
1012 Toolkit::PageTurnView::PagePanSignal& PageTurnView::PagePanFinishedSignal()
1014 return mPagePanFinishedSignal;
1017 bool PageTurnView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1019 Dali::BaseHandle handle( object );
1021 bool connected( true );
1022 Toolkit::PageTurnView pageTurnView = Toolkit::PageTurnView::DownCast( handle );
1024 if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_TURN_STARTED ) )
1026 pageTurnView.PageTurnStartedSignal().Connect( tracker, functor );
1028 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_TURN_FINISHED ) )
1030 pageTurnView.PageTurnFinishedSignal().Connect( tracker, functor );
1032 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_PAN_STARTED ) )
1034 pageTurnView.PagePanStartedSignal().Connect( tracker, functor );
1036 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PAGE_PAN_FINISHED ) )
1038 pageTurnView.PagePanFinishedSignal().Connect( tracker, functor );
1042 // signalName does not match any signal
1049 void PageTurnView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1051 Toolkit::PageTurnView pageTurnView = Toolkit::PageTurnView::DownCast( Dali::BaseHandle( object ) );
1055 PageTurnView& pageTurnViewImpl( GetImplementation( pageTurnView ) );
1059 case Toolkit::PageTurnView::Property::PAGE_SIZE:
1061 pageTurnViewImpl.SetPageSize( value.Get<Vector2>() );
1064 case Toolkit::PageTurnView::Property::CURRENT_PAGE_ID:
1066 pageTurnViewImpl.GoToPage( value.Get<int>() );
1069 case Toolkit::PageTurnView::Property::SPINE_SHADOW:
1071 pageTurnViewImpl.SetSpineShadowParameter( value.Get<Vector2>() );
1078 Property::Value PageTurnView::GetProperty( BaseObject* object, Property::Index index )
1080 Property::Value value;
1082 Toolkit::PageTurnView pageTurnView = Toolkit::PageTurnView::DownCast( Dali::BaseHandle( object ) );
1086 PageTurnView& pageTurnViewImpl( GetImplementation( pageTurnView ) );
1090 case Toolkit::PageTurnView::Property::PAGE_SIZE:
1092 value = pageTurnViewImpl.GetPageSize();
1095 case Toolkit::PageTurnView::Property::CURRENT_PAGE_ID:
1097 value = static_cast<int>( pageTurnViewImpl.GetCurrentPage() );
1100 case Toolkit::PageTurnView::Property::SPINE_SHADOW:
1102 value = pageTurnViewImpl.GetSpineShadowParameter();
1110 } // namespace Internal
1112 } // namespace Toolkit