2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/public-api/events/mouse-wheel-event.h>
20 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view-constraints.h>
21 #include <dali-toolkit/public-api/controls/scrollable/scroll-component-impl.h>
22 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.h>
23 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-effect-impl.h>
24 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
26 // TODO: Change to two class system:
27 // 1. DraggableActor (is an actor which can be dragged anywhere/scaled/rotated, can be set to range using the ruler)
28 // 2. ScrollView (contains a draggable actor that can a) be dragged in the negative X, and Y domain, b) has a hitArea for touches)
30 // TODO: Asymetrical scaling
31 // TODO: external components (page and status overlays).
33 // TODO: upgrade Vector2/3 to support returning Unit vectors, normals, & cross product (dot product is already provided)
40 const int DEFAULT_REFRESH_INTERVAL_MILLISECONDS = 50; ///< Refresh rate TODO: Animation should have an update signal (and see item-view-impl)
41 const float FLICK_SPEED_THRESHOLD = 500.0f; ///< Flick threshold in pixels/ms
42 const float FREE_FLICK_SPEED_THRESHOLD = 200.0f; ///< Free-Flick threshold in pixels/ms
43 const float AUTOLOCK_AXIS_MINIMUM_DISTANCE2 = 100.0f; ///< Auto-lock axis after minimum distance squared.
44 const float FLICK_ORTHO_ANGLE_RANGE = 60.0f; ///< degrees. (if >45, then supports diagonal flicking)
45 const unsigned int MAXIMUM_NUMBER_OF_VALUES = 5; ///< Number of values to use for weighted pan calculation.
46 const Vector2 DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = Vector2(0.17f, 0.1f); ///< The step of horizontal scroll distance in the proportion of stage size for each mouse wheel event received.
47 const unsigned long MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET( 150u ); ///< Determines whether velocity is reset after tap interrupts snap animation
48 const float TOUCH_DOWN_TIMER_INTERVAL = 100.0f; ///< After this time interval with touch-down, the snap animation will be interrupted (if no gesture has started).
50 // predefined effect values
51 const Vector3 ANGLE_CAROUSEL_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
52 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.2f, Math::PI * 0.2f, 0.0f); ///< Cube page rotates as if it has ten sides with the camera positioned inside
53 const Vector2 ANGLE_CUSTOM_CUBE_SWING(-Math::PI * 0.45f, -Math::PI * 0.45f); ///< outer cube pages swing 90 degrees as they pan offscreen
54 const Vector2 ANGLE_SPIRAL_SWING_IN(Math::PI * 0.5f, Math::PI * 0.5f);
55 const Vector2 ANGLE_SPIRAL_SWING_OUT(Math::PI * 0.35f, Math::PI * 0.35f);
56 const Vector2 ANGLE_OUTER_CUBE_SWING(Math::PI * 0.5f, Math::PI * 0.5f); ///< outer cube pages swing 90 degrees as they pan offscreen
58 // Helpers ////////////////////////////////////////////////////////////////////////////////////////
60 // TODO: GetAngle for Vector2 can be moved.
61 // GetAngle for Vector3 needs to be measured against a normal/plane.
64 * @param[in] vector The 3D vector to be measured
65 * @return angle in radians from 0 to 2PI
67 float GetAngle(const Vector3& vector)
69 return atan2(vector.y, vector.x) + Math::PI;
73 * @param[in] vector The 2D vector to be measured
74 * @return angle in radians from 0 to 2PI
76 float GetAngle(const Vector2& vector)
78 return atan2(vector.y, vector.x) + Math::PI;
82 * Find the vector (distance) from (a) to (b)
83 * in domain (start) to (end)
84 * (\ / start) (\ / end)
87 * @note assumes both (a) and (b) are already with the domain
90 * @param[in] a the current point
91 * @param[in] b the target point
92 * @param[in] start the start of the domain
93 * @param[in] end the end of the domain
94 * @param[in] bias whether to only take the right direction or the left direction,
95 * or the shortest direction.
96 * @return the shortest direction and distance
98 float VectorInDomain(float a, float b, float start, float end, Dali::Toolkit::DirectionBias bias)
100 if(bias == Dali::Toolkit::DirectionBiasNone)
102 return ShortestDistanceInDomain( a, b, start, end );
105 float size = end-start;
111 if(bias == Dali::Toolkit::DirectionBiasRight) // going right, take the vector.
117 float aRight = a+size;
124 if(bias == Dali::Toolkit::DirectionBiasLeft) // going left, take the vector.
130 float aLeft = a-size;
137 * Returns the position of the anchor within actor
139 * @param actor The Actor
140 * @param anchor The Anchor point of interest.
141 * @return The position of the Anchor
143 Vector3 GetPositionOfAnchor(Actor &actor, const Vector3 &anchor)
145 Vector3 childPosition = actor.GetCurrentPosition();
146 Vector3 childAnchor = - actor.GetCurrentAnchorPoint() + anchor;
147 Vector3 childSize = actor.GetCurrentSize();
149 return childPosition + childAnchor * childSize;
152 // AlphaFunctions /////////////////////////////////////////////////////////////////////////////////
154 float FinalDefaultAlphaFunction(float offset)
156 return offset * 0.5f;
160 * ConstantDecelerationAlphaFunction
161 * Newtoninan distance for constant deceleration
162 * v = 1 - t, s = t - 1/2 t^2
163 * when t = 0, s = 0.0 (min distance)
164 * when t = 1, s = 0.5 (max distance)
165 * progress = s / (max-min) = 2t - t^2
167 * @param[in] offset The input progress
168 * @return The output progress
170 float ConstantDecelerationAlphaFunction(float progress)
172 return progress * 2.0f - progress * progress;
175 // Internal Constraints ///////////////////////////////////////////////////////////////////////////
178 * Internal Relative position Constraint
179 * Generates the relative position value of the scroll view
180 * based on the absolute position, and it's relation to the
181 * scroll domain. This is a value from 0.0f to 1.0f in each
182 * scroll position axis.
184 Vector3 InternalRelativePositionConstraint(const Vector3& current,
185 const PropertyInput& scrollPositionProperty,
186 const PropertyInput& scrollMinProperty,
187 const PropertyInput& scrollMaxProperty,
188 const PropertyInput& scrollSizeProperty)
190 const Vector3& position = -scrollPositionProperty.GetVector3();
191 const Vector3& min = scrollMinProperty.GetVector3();
192 const Vector3& max = scrollMaxProperty.GetVector3();
193 const Vector3& size = scrollSizeProperty.GetVector3();
195 Vector3 relativePosition;
196 Vector3 domainSize = (max - min) - size;
198 relativePosition.x = domainSize.x > Math::MACHINE_EPSILON_1 ? fabsf((position.x - min.x) / domainSize.x) : 0.0f;
199 relativePosition.y = domainSize.y > Math::MACHINE_EPSILON_1 ? fabsf((position.y - min.y) / domainSize.y) : 0.0f;
201 return relativePosition;
204 } // unnamed namespace
219 * Internal Pre-Position Property Constraint.
221 * Generates position property based on current position + gesture displacement.
222 * Or generates position property based on positionX/Y.
223 * Note: This is the position prior to any clamping at scroll boundaries.
224 * TODO: Scale & Rotation Transforms.
226 struct InternalPrePositionConstraint
228 InternalPrePositionConstraint(const Vector2& initialPanMask,
230 float axisAutoLockGradient)
231 : mInitialPanMask(initialPanMask),
232 mAxisAutoLock(axisAutoLock),
233 mLockAxis(ScrollView::LockPossible),
234 mAxisAutoLockGradient(axisAutoLockGradient),
235 mPrePosition(Vector3::ZERO),
240 Vector3 operator()(const Vector3& current,
241 const PropertyInput& gesturePositionProperty,
242 const PropertyInput& gestureDisplacementProperty,
243 const PropertyInput& scrollPositionXProperty,
244 const PropertyInput& scrollPositionYProperty,
245 const PropertyInput& panningProperty)
247 const bool panning = panningProperty.GetBoolean();
248 Vector3 scrollPostPosition;
252 // Check if panning has just started...
255 mLocalStart = gesturePositionProperty.GetVector2() - gestureDisplacementProperty.GetVector2();
256 mPrePosition = current;
257 mLockAxis = ScrollView::LockPossible;
259 mCurrentPanMask = mInitialPanMask;
262 // Calculate Deltas...
263 Vector2 currentPosition = gesturePositionProperty.GetVector2();
264 Vector2 panDelta( currentPosition - mLocalStart );
266 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
267 // appears mostly horizontal or mostly vertical respectively...
268 AxisAutoLock(panDelta);
270 // Restrict deltas based on ruler enable/disable and axis-lock state...
271 panDelta *= mCurrentPanMask;
273 // Perform Position transform based on input deltas...
274 scrollPostPosition = mPrePosition;
275 scrollPostPosition.GetVectorXY() += panDelta;
279 scrollPostPosition.x = scrollPositionXProperty.GetFloat();
280 scrollPostPosition.y = scrollPositionYProperty.GetFloat();
283 mWasPanning = panning;
284 return scrollPostPosition;
287 void AxisAutoLock(Vector2& panDelta)
291 if(panDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 &&
292 mLockAxis == ScrollView::LockPossible)
294 float dx = fabsf(panDelta.x);
295 float dy = fabsf(panDelta.y);
296 if(dx * mAxisAutoLockGradient >= dy)
298 // 0.36:1 gradient to the horizontal (deviate < 20 degrees)
299 mLockAxis = ScrollView::LockVertical;
300 mCurrentPanMask.y = 0.0f;
302 else if(dy * mAxisAutoLockGradient > dx)
304 // 0.36:1 gradient to the vertical (deviate < 20 degrees)
305 mLockAxis = ScrollView::LockHorizontal;
306 mCurrentPanMask.x = 0.0f;
310 mLockAxis = ScrollView::LockNone;
313 } // end if mAxisAutoLock
317 Vector2 mInitialPanMask; ///< Initial pan mask (based on ruler settings)
318 Vector2 mCurrentPanMask; ///< Current pan mask that can be altered by axis lock mode.
320 bool mAxisAutoLock; ///< Set by ScrollView
321 ScrollView::LockAxis mLockAxis;
322 float mAxisAutoLockGradient; ///< Set by ScrollView
323 Vector3 mPrePosition;
328 * Internal Position Property Constraint.
330 * Generates position property based on pre-position
331 * Note: This is the position after clamping.
332 * (uses result of InternalPrePositionConstraint)
334 struct InternalPositionConstraint
336 InternalPositionConstraint(const RulerDomain& domainX, const RulerDomain& domainY)
337 : mDomainMin( -domainX.min, -domainY.min ),
338 mDomainMax( -domainX.max, -domainY.max ),
339 mClampX( domainX.enabled ),
340 mClampY( domainY.enabled )
344 Vector3 operator()(const Vector3& current,
345 const PropertyInput& scrollPositionProperty,
346 const PropertyInput& scrollSizeProperty)
348 Vector3 position = scrollPositionProperty.GetVector3();
349 const Vector2& size = scrollSizeProperty.GetVector3().GetVectorXY();
351 position.x = mClampX ? Clamp(position.x, mDomainMax.x + size.x, mDomainMin.x ) : position.x;
352 position.y = mClampY ? Clamp(position.y, mDomainMax.y + size.y, mDomainMin.y ) : position.y;
365 * This constraint updates the X overshoot property using the difference
366 * mPropertyPrePosition.x and mPropertyPosition.x, returning a relative value between 0.0f and 1.0f
368 struct OvershootXConstraint
370 OvershootXConstraint(float maxOvershoot) : mLastOvershoot(0.0f), mMaxOvershoot(maxOvershoot) {}
372 float operator()(const float& current,
373 const PropertyInput& scrollPrePositionProperty,
374 const PropertyInput& scrollPostPositionProperty)
376 Vector3 scrollPrePosition = scrollPrePositionProperty.GetVector3();
377 Vector3 scrollPostPosition = scrollPostPositionProperty.GetVector3();
378 float newOvershoot = scrollPrePosition.x - scrollPostPosition.x;
379 return (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
382 float mLastOvershoot;
387 * This constraint updates the Y overshoot property using the difference
388 * mPropertyPrePosition.y and mPropertyPosition.y, returning a relative value between 0.0f and 1.0f
390 struct OvershootYConstraint
392 OvershootYConstraint(float maxOvershoot) : mLastOvershoot(0.0f), mMaxOvershoot(maxOvershoot) {}
394 float operator()(const float& current,
395 const PropertyInput& scrollPrePositionProperty,
396 const PropertyInput& scrollPostPositionProperty)
398 Vector3 scrollPrePosition = scrollPrePositionProperty.GetVector3();
399 Vector3 scrollPostPosition = scrollPostPositionProperty.GetVector3();
400 float newOvershoot = scrollPrePosition.y - scrollPostPosition.y;
401 return (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
404 float mLastOvershoot;
409 * When panning, this constraint updates the X property, otherwise
410 * it has no effect on the X property.
412 float InternalXConstraint(const float& current,
413 const PropertyInput& scrollPosition,
414 const PropertyInput& panningProperty)
416 return scrollPosition.GetVector3().x;
420 * When panning, this constraint updates the Y property, otherwise
421 * it has no effect on the Y property.
423 float InternalYConstraint(const float& current,
424 const PropertyInput& scrollPosition,
425 const PropertyInput& panningProperty)
427 return scrollPosition.GetVector3().y;
431 * Internal Position-Delta Property Constraint.
433 * Generates position-delta property based on scroll-position + scroll-offset properties.
435 Vector3 InternalPositionDeltaConstraint(const Vector3& current,
436 const PropertyInput& scrollPositionProperty,
437 const PropertyInput& scrollOffsetProperty)
439 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
440 const Vector3& scrollOffset = scrollOffsetProperty.GetVector3();
442 return scrollPosition + scrollOffset;
446 * Internal Final Position Constraint
447 * The position of content is:
448 * of scroll-position + f(scroll-overshoot)
449 * where f(...) function defines how overshoot
450 * should affect final-position.
452 struct InternalFinalConstraint
454 InternalFinalConstraint(AlphaFunction functionX,
455 AlphaFunction functionY)
456 : mFunctionX(functionX),
457 mFunctionY(functionY)
461 Vector3 operator()(const Vector3& current,
462 const PropertyInput& scrollPositionProperty,
463 const PropertyInput& scrollOvershootXProperty,
464 const PropertyInput& scrollOvershootYProperty)
466 const float& overshootx = scrollOvershootXProperty.GetFloat();
467 const float& overshooty = scrollOvershootYProperty.GetFloat();
468 Vector3 offset( mFunctionX(overshootx),
469 mFunctionY(overshooty),
472 return scrollPositionProperty.GetVector3() - offset;
475 AlphaFunction mFunctionX;
476 AlphaFunction mFunctionY;
482 return Toolkit::ScrollView::New();
485 TypeRegistration typeRegistration( typeid(Toolkit::ScrollView), typeid(Toolkit::Scrollable), Create );
487 SignalConnectorType signalConnector1( typeRegistration, Toolkit::ScrollView::SIGNAL_SNAP_STARTED, &ScrollView::DoConnectSignal );
492 ///////////////////////////////////////////////////////////////////////////////////////////////////
494 ///////////////////////////////////////////////////////////////////////////////////////////////////
496 Dali::Toolkit::ScrollView ScrollView::New()
498 // Create the implementation
499 ScrollViewPtr scrollView(new ScrollView());
501 // Pass ownership to CustomActor via derived handle
502 Dali::Toolkit::ScrollView handle(*scrollView);
504 // Second-phase init of the implementation
505 // This can only be done after the CustomActor connection has been made...
506 scrollView->Initialize();
511 ScrollView::ScrollView()
515 mScrollInterrupted(false),
518 mGestureStackDepth(0),
519 mRotationDelta(0.0f),
520 mScrollPreRotation(0.0f),
521 mScrollPostRotation(0.0f),
522 mTouchDownTimeoutReached(false),
523 mActorAutoSnapEnabled(false),
524 mAutoResizeContainerEnabled(false),
526 mAxisAutoLock(false),
527 mMinTouchesForPanning(1),
528 mMaxTouchesForPanning(1),
529 mLockAxis(LockPossible),
530 mRefreshIntervalMilliseconds(DEFAULT_REFRESH_INTERVAL_MILLISECONDS),
532 mOvershootDelay(1.0f),
533 mMaxOvershoot(Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT, Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT),
534 mDefaultMaxOvershoot(true),
535 mSnapOvershootDuration(Toolkit::ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION),
536 mSnapOvershootAlphaFunction(AlphaFunctions::EaseOut),
537 mSnapDuration(Toolkit::ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION),
538 mSnapAlphaFunction(AlphaFunctions::EaseOut),
539 mFlickDuration(Toolkit::ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION),
540 mFlickAlphaFunction(AlphaFunctions::EaseOut),
541 mAxisAutoLockGradient(Toolkit::ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT),
542 mFrictionCoefficient(Toolkit::ScrollView::DEFAULT_FRICTION_COEFFICIENT),
543 mFlickSpeedCoefficient(Toolkit::ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT),
544 mMaxFlickSpeed(Toolkit::ScrollView::DEFAULT_MAX_FLICK_SPEED),
545 mInAccessibilityPan(false)
547 SetRequiresMouseWheelEvents(true);
550 void ScrollView::OnInitialize()
553 self.SetLeaveRequired(true);
555 // Internal Actor, used to hide actors from enumerations.
556 // Also actors added to Internal actor appear as overlays e.g. ScrollBar components.
557 mInternalActor = Actor::New();
558 mInternalActor.SetDrawMode(DrawMode::OVERLAY);
559 self.Add(mInternalActor);
560 mInternalActor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
561 mInternalActor.SetParentOrigin(ParentOrigin::CENTER);
562 mInternalActor.SetAnchorPoint(AnchorPoint::CENTER);
566 // Register Scroll Properties.
567 RegisterProperties();
569 mScrollPostPosition = mScrollPrePosition = Vector3::ZERO;
570 mScrollPostScale = mScrollPreScale = Vector3::ONE;
571 mScrollPostRotation = mScrollPreRotation = 0.0f;
573 mMouseWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
577 mGestureStackDepth = 0;
579 EnableGestureDetection( Gesture::Type( Gesture::Pan ) );
581 // For pan, default to only 1 touch required, ignoring touches outside this range.
582 SetTouchesRequiredForPanning(1, 1, false);
584 // By default we'll allow the user to freely drag the scroll view,
585 // while disabling the other rulers.
586 RulerPtr ruler = new DefaultRuler();
587 RulerPtr rulerDisabled = new DefaultRuler();
588 rulerDisabled->Disable();
591 mRulerScaleX = rulerDisabled;
592 mRulerScaleY = rulerDisabled;
593 mRulerRotation = rulerDisabled;
595 EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator);
597 Vector3 size = GetControlSize();
598 UpdatePropertyDomain(size);
599 SetInternalConstraints();
602 void ScrollView::OnControlStageConnection()
606 SetScrollSensitive( false );
607 SetScrollSensitive( true );
609 if(IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator))
611 // try and make sure property notifications are set
612 EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator);
616 void ScrollView::OnControlStageDisconnection()
618 if ( mSnapOvershootAnimation )
620 SetOvershootToOrigin();
626 ScrollView::~ScrollView()
630 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
632 return mSnapAlphaFunction;
635 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
637 mSnapAlphaFunction = alpha;
640 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
642 return mFlickAlphaFunction;
645 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
647 mFlickAlphaFunction = alpha;
650 float ScrollView::GetScrollSnapDuration() const
652 return mSnapDuration;
655 void ScrollView::SetScrollSnapDuration(float time)
657 mSnapDuration = time;
660 float ScrollView::GetScrollFlickDuration() const
662 return mFlickDuration;
665 void ScrollView::SetScrollFlickDuration(float time)
667 mFlickDuration = time;
670 void ScrollView::ApplyEffect(Toolkit::ScrollViewEffect effect)
672 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
674 // Assertion check to ensure effect doesn't already exist in this scrollview
675 bool effectAlreadyExistsInScrollView(false);
676 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
680 effectAlreadyExistsInScrollView = true;
685 DALI_ASSERT_ALWAYS(!effectAlreadyExistsInScrollView);
687 // add effect to effects list
688 mEffects.push_back(effect);
690 // invoke Attachment request to ScrollView first
691 GetImpl(effect).Attach(self);
694 Toolkit::ScrollViewEffect ScrollView::ApplyEffect(Toolkit::ScrollView::PageEffect effect)
696 Toolkit::ScrollViewEffect scrollEffect;
699 case Toolkit::ScrollView::PageEffectNone:
703 case Toolkit::ScrollView::PageEffectOuterCube:
705 Toolkit::ScrollViewCustomEffect customEffect;
706 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
707 Vector2 pageSize = Stage::GetCurrent().GetSize();
708 // set the page translation to the slide off distance, also add an extra value to space the pages, having a smaller spacing on translationOut will allow the spacing to reduce over time
709 // the page moving onto screen will start 50.0f further out (1.0f * 50.0f) and the spacing will reduce as its position reaches the centre (0.0f * 50.0f)
710 // the page moving off screen will slowly build a spacing from 0.0f to 20.0f
711 // the spacing from each page is added together for the final spacing between the two pages.
712 customEffect.SetPageTranslation(Vector3(pageSize.x, pageSize.y, 0) + Vector3(50.0f, 50.0f, 0.0f), Vector3(pageSize.x, pageSize.y, 0) + Vector3(20.0f, 20.0f, 0.0f));
713 customEffect.SetSwingAngleOut(ANGLE_CUSTOM_CUBE_SWING.x, Vector3(0.0f, -1.0f, 0.0f));
714 customEffect.SetSwingAnchor(AnchorPoint::CENTER, AnchorPoint::CENTER_LEFT);
715 customEffect.SetOpacityThreshold(0.7f);
718 case Toolkit::ScrollView::PageEffectDepth:
720 Toolkit::ScrollViewCustomEffect customEffect;
721 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
724 case Toolkit::ScrollView::PageEffectInnerCube:
726 Toolkit::ScrollViewCustomEffect customEffect;
727 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
728 customEffect.SetPageSpacing(Vector2(30.0f, 30.0f));
729 customEffect.SetAngledOriginPageRotation(ANGLE_CUBE_PAGE_ROTATE);
730 customEffect.SetSwingAngle(ANGLE_CUBE_PAGE_ROTATE.x, Vector3(0,-1,0));
731 customEffect.SetOpacityThreshold(0.5f);
734 case Toolkit::ScrollView::PageEffectCarousel:
736 Toolkit::ScrollViewCustomEffect customEffect;
737 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
738 customEffect.SetPageTranslation(Vector3(0,0,0), Vector3(-30, 0, 0));
739 customEffect.SetPageSpacing(Vector2(60.0f, 60.0f));
740 customEffect.SetAngledOriginPageRotation(-ANGLE_CUBE_PAGE_ROTATE);
741 customEffect.SetOpacityThreshold(0.2f, 0.6f);
744 case Toolkit::ScrollView::PageEffectSpiral:
746 Toolkit::ScrollViewCustomEffect customEffect;
747 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
749 Vector2 pageSize = Stage::GetCurrent().GetSize();
750 customEffect.SetSwingAngle(-ANGLE_SPIRAL_SWING_IN.x, Vector3(0.0f, -1.0f, 0.0f), ANGLE_SPIRAL_SWING_OUT.x, Vector3(0.0f, -1.0f, 0.0f));
751 //customEffect.SetSwingAngleAlphaFunctionOut(AlphaFunctions::EaseOut);
752 customEffect.SetSwingAnchor(AnchorPoint::CENTER_RIGHT);
753 customEffect.SetPageTranslation(Vector3(pageSize.x, pageSize.y, 0) + Vector3(100.0f, 100.0f, 0.0f), Vector3(pageSize.x, pageSize.y, -pageSize.y * 2.0f) * 0.33f);
754 //customEffect.SetPageTranslateAlphaFunctionOut(AlphaFunctions::EaseOut);
755 customEffect.SetOpacityThreshold(0.75f, 0.6f);
756 customEffect.SetOpacityAlphaFunctionIn(AlphaFunctions::EaseInOut);
761 DALI_ASSERT_DEBUG(0 && "unknown scroll view effect");
764 RemoveConstraintsFromChildren();
767 ApplyEffect(scrollEffect);
772 void ScrollView::RemoveEffect(Toolkit::ScrollViewEffect effect)
774 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
776 // remove effect from effects list
777 bool effectExistedInScrollView(false);
778 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
782 mEffects.erase(iter);
783 effectExistedInScrollView = true;
788 // Assertion check to ensure effect existed.
789 DALI_ASSERT_ALWAYS(effectExistedInScrollView);
791 // invoke Detachment request to ScrollView last
792 GetImpl(effect).Detach(self);
795 void ScrollView::RemoveAllEffects()
797 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
799 for (ScrollViewEffectIter effectIter = mEffects.begin(); effectIter != mEffects.end(); ++effectIter)
801 Toolkit::ScrollViewEffect effect = *effectIter;
803 // invoke Detachment request to ScrollView last
804 GetImpl(effect).Detach(self);
810 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
812 ApplyConstraintToBoundActors(constraint);
815 void ScrollView::RemoveConstraintsFromChildren()
817 RemoveConstraintsFromBoundActors();
820 const RulerPtr ScrollView::GetRulerX() const
825 const RulerPtr ScrollView::GetRulerY() const
830 void ScrollView::SetRulerX(RulerPtr ruler)
834 Vector3 size = GetControlSize();
835 UpdatePropertyDomain(size);
836 UpdateMainInternalConstraint();
839 void ScrollView::SetRulerY(RulerPtr ruler)
843 Vector3 size = GetControlSize();
844 UpdatePropertyDomain(size);
845 UpdateMainInternalConstraint();
848 void ScrollView::UpdatePropertyDomain(const Vector3& size)
853 bool canScrollVertical = false;
854 bool canScrollHorizontal = false;
856 if(mRulerX->IsEnabled())
858 const Toolkit::RulerDomain& rulerDomain = mRulerX->GetDomain();
859 min.x = rulerDomain.min;
860 max.x = rulerDomain.max;
862 // make sure new scroll value is within new domain
863 float newScroll = min.x;
864 int scrollXPropertyIndex = self.GetPropertyIndex(Toolkit::ScrollView::SCROLL_X_PROPERTY_NAME);
865 if((fabsf(max.x - min.x) - size.x) > Math::MACHINE_EPSILON_1)
867 canScrollHorizontal = true;
868 float currentScroll = self.GetProperty<float>(scrollXPropertyIndex);
869 newScroll = Clamp(currentScroll, -(max.x - size.x), -min.x);
871 self.SetProperty(scrollXPropertyIndex, newScroll);
874 if(mRulerY->IsEnabled())
876 const Toolkit::RulerDomain& rulerDomain = mRulerY->GetDomain();
877 min.y = rulerDomain.min;
878 max.y = rulerDomain.max;
880 // make sure new scroll value is within new domain
881 float newScroll = min.y;
882 int scrollYPropertyIndex = self.GetPropertyIndex(Toolkit::ScrollView::SCROLL_Y_PROPERTY_NAME);
883 if((fabsf(max.y - min.y) - size.y) > Math::MACHINE_EPSILON_1)
885 canScrollVertical = true;
886 float currentScroll = self.GetProperty<float>(scrollYPropertyIndex);
887 newScroll = Clamp(currentScroll, -(max.y - size.y), -min.y);
889 self.SetProperty(scrollYPropertyIndex, newScroll);
891 self.SetProperty(mPropertyCanScrollVertical, canScrollVertical);
892 self.SetProperty(mPropertyCanScrollHorizontal, canScrollHorizontal);
894 self.SetProperty(mPropertyPositionMin, min );
895 self.SetProperty(mPropertyPositionMax, max );
898 void ScrollView::SetRulerScaleX(RulerPtr ruler)
900 mRulerScaleX = ruler;
901 UpdateMainInternalConstraint();
904 void ScrollView::SetRulerScaleY(RulerPtr ruler)
906 mRulerScaleY = ruler;
907 UpdateMainInternalConstraint();
910 void ScrollView::SetRulerRotation(RulerPtr ruler)
912 mRulerRotation = ruler;
913 UpdateMainInternalConstraint();
916 void ScrollView::SetScrollSensitive(bool sensitive)
919 PanGestureDetector panGesture( GetPanGestureDetector() );
921 if((!mSensitive) && (sensitive))
923 mSensitive = sensitive;
924 panGesture.Attach(self);
926 else if((mSensitive) && (!sensitive))
928 // while the scroll view is panning, the state needs to be reset.
929 bool isPanning = self.GetProperty<bool>( mPropertyPanning );
932 PanGesture cancelGesture( Gesture::Cancelled );
933 OnPan( cancelGesture );
936 panGesture.Detach(self);
937 mSensitive = sensitive;
939 mGestureStackDepth = 0;
943 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
945 mMaxOvershoot.x = overshootX;
946 mMaxOvershoot.y = overshootY;
947 mDefaultMaxOvershoot = false;
948 UpdateMainInternalConstraint();
951 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
953 mSnapOvershootAlphaFunction = alpha;
956 void ScrollView::SetSnapOvershootDuration(float duration)
958 mSnapOvershootDuration = duration;
961 void ScrollView::SetTouchesRequiredForPanning(unsigned int minTouches, unsigned int maxTouches, bool endOutside)
963 PanGestureDetector panGesture( GetPanGestureDetector() );
965 mMinTouchesForPanning = minTouches;
966 mMaxTouchesForPanning = maxTouches;
970 panGesture.SetMinimumTouchesRequired(minTouches);
971 panGesture.SetMaximumTouchesRequired(maxTouches);
975 panGesture.SetMinimumTouchesRequired(1);
976 panGesture.SetMaximumTouchesRequired(UINT_MAX);
980 void ScrollView::SetActorAutoSnap(bool enable)
982 mActorAutoSnapEnabled = enable;
985 void ScrollView::SetAutoResize(bool enable)
987 mAutoResizeContainerEnabled = enable;
988 // TODO: This needs a lot of issues to be addressed before working.
991 bool ScrollView::GetWrapMode() const
996 void ScrollView::SetWrapMode(bool enable)
999 Self().SetProperty(mPropertyWrap, enable);
1002 int ScrollView::GetRefreshInterval() const
1004 return mRefreshIntervalMilliseconds;
1007 void ScrollView::SetRefreshInterval(int milliseconds)
1009 mRefreshIntervalMilliseconds = milliseconds;
1012 bool ScrollView::GetAxisAutoLock() const
1014 return mAxisAutoLock;
1017 void ScrollView::SetAxisAutoLock(bool enable)
1019 mAxisAutoLock = enable;
1020 UpdateMainInternalConstraint();
1023 float ScrollView::GetAxisAutoLockGradient() const
1025 return mAxisAutoLockGradient;
1028 void ScrollView::SetAxisAutoLockGradient(float gradient)
1030 DALI_ASSERT_DEBUG( gradient >= 0.0f && gradient <= 1.0f );
1031 mAxisAutoLockGradient = gradient;
1032 UpdateMainInternalConstraint();
1035 float ScrollView::GetFrictionCoefficient() const
1037 return mFrictionCoefficient;
1040 void ScrollView::SetFrictionCoefficient(float friction)
1042 DALI_ASSERT_DEBUG( friction > 0.0f );
1043 mFrictionCoefficient = friction;
1046 float ScrollView::GetFlickSpeedCoefficient() const
1048 return mFlickSpeedCoefficient;
1051 void ScrollView::SetFlickSpeedCoefficient(float speed)
1053 mFlickSpeedCoefficient = speed;
1056 float ScrollView::GetMaxFlickSpeed() const
1058 return mMaxFlickSpeed;
1061 void ScrollView::SetMaxFlickSpeed(float speed)
1063 mMaxFlickSpeed = speed;
1066 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
1068 mMouseWheelScrollDistanceStep = step;
1071 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
1073 return mMouseWheelScrollDistanceStep;
1076 unsigned int ScrollView::GetCurrentPage() const
1078 // in case animation is currently taking place.
1079 Vector3 position = GetPropertyPrePosition();
1081 Actor self = Self();
1082 unsigned int page = 0;
1083 unsigned int pagesPerVolume = 1;
1084 unsigned int volume = 0;
1086 // if rulerX is enabled, then get page count (columns)
1087 page = mRulerX->GetPageFromPosition(-position.x, mWrapMode);
1088 volume = mRulerY->GetPageFromPosition(-position.y, mWrapMode);
1089 pagesPerVolume = mRulerX->GetTotalPages();
1091 return volume * pagesPerVolume + page;
1094 Vector3 ScrollView::GetCurrentScrollPosition() const
1096 // in case animation is currently taking place.
1097 return -GetPropertyPrePosition();
1100 Vector3 ScrollView::GetCurrentScrollScale() const
1102 // in case animation is currently taking place.
1103 return GetPropertyScale();
1106 Vector3 ScrollView::GetDomainSize() const
1108 Vector3 size = Self().GetCurrentSize();
1110 const RulerDomain& xDomain = GetRulerX()->GetDomain();
1111 const RulerDomain& yDomain = GetRulerY()->GetDomain();
1113 Vector3 domainSize = Vector3( xDomain.max - xDomain.min, yDomain.max - yDomain.min, 0.0f ) - size;
1117 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation,
1118 DirectionBias horizontalBias, DirectionBias verticalBias)
1120 TransformTo(position, scale, rotation, mSnapDuration, horizontalBias, verticalBias);
1123 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation, float duration,
1124 DirectionBias horizontalBias, DirectionBias verticalBias)
1126 // Guard against destruction during signal emission
1127 // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo()
1128 Toolkit::ScrollView handle( GetOwner() );
1130 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1131 Self().SetProperty( mPropertyScrollStartPagePosition, currentScrollPosition );
1133 if(mScrolling) // are we interrupting a current scroll?
1135 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
1137 mScrollCompletedSignalV2.Emit( currentScrollPosition );
1140 Self().SetProperty(mPropertyScrolling, true);
1142 mScrollStartedSignalV2.Emit( currentScrollPosition );
1143 bool animating = AnimateTo(-position,
1144 Vector3::ONE * duration,
1146 Vector3::ONE * duration,
1157 // if not animating, then this pan has completed right now.
1158 Self().SetProperty(mPropertyScrolling, false);
1160 mScrollCompletedSignalV2.Emit( currentScrollPosition );
1164 void ScrollView::ScrollTo(const Vector3& position)
1166 ScrollTo(position, mSnapDuration );
1169 void ScrollView::ScrollTo(const Vector3& position, float duration)
1171 ScrollTo(position, duration, DirectionBiasNone, DirectionBiasNone);
1174 void ScrollView::ScrollTo(const Vector3& position, float duration,
1175 DirectionBias horizontalBias, DirectionBias verticalBias)
1177 TransformTo(position, mScrollPostScale, mScrollPostRotation, duration, horizontalBias, verticalBias);
1180 void ScrollView::ScrollTo(unsigned int page)
1182 ScrollTo(page, mSnapDuration);
1185 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
1188 unsigned int volume;
1189 unsigned int libraries;
1191 // The position to scroll to is continuous and linear
1192 // unless a domain has been enabled on the X axis.
1193 // or if WrapMode has been enabled.
1194 bool carryX = mRulerX->GetDomain().enabled | mWrapMode;
1195 bool carryY = mRulerY->GetDomain().enabled | mWrapMode;
1197 position.x = mRulerX->GetPositionFromPage(page, volume, carryX);
1198 position.y = mRulerY->GetPositionFromPage(volume, libraries, carryY);
1200 ScrollTo(position, duration, bias, bias);
1203 void ScrollView::ScrollTo(Actor &actor)
1205 ScrollTo(actor, mSnapDuration);
1208 void ScrollView::ScrollTo(Actor &actor, float duration)
1210 DALI_ASSERT_ALWAYS(actor.GetParent() == Self());
1212 Actor self = Self();
1213 Vector3 size = self.GetCurrentSize();
1214 Vector3 position = actor.GetCurrentPosition();
1215 position -= GetPropertyPrePosition();
1217 ScrollTo(Vector3(position.x - size.width * 0.5f, position.y - size.height * 0.5f, 0.0f), duration);
1220 Actor ScrollView::FindClosestActor()
1222 Actor self = Self();
1223 Vector3 size = self.GetCurrentSize();
1225 return FindClosestActorToPosition(Vector3(size.width * 0.5f,size.height * 0.5f,0.0f));
1228 Actor ScrollView::FindClosestActorToPosition(const Vector3& position, FindDirection dirX, FindDirection dirY, FindDirection dirZ)
1231 float closestDistance2 = 0.0f;
1232 Vector3 actualPosition = position;
1234 unsigned int numChildren = Self().GetChildCount();
1236 for(unsigned int i = 0; i < numChildren; ++i)
1238 Actor child = Self().GetChildAt(i);
1240 if(mInternalActor == child) // ignore internal actor.
1245 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1247 Vector3 delta = childPosition - actualPosition;
1249 // X-axis checking (only find Actors to the [dirX] of actualPosition)
1250 if(dirX > All) // != All,None
1252 FindDirection deltaH = delta.x > 0 ? Right : Left;
1259 // Y-axis checking (only find Actors to the [dirY] of actualPosition)
1260 if(dirY > All) // != All,None
1262 FindDirection deltaV = delta.y > 0 ? Down : Up;
1269 // Z-axis checking (only find Actors to the [dirZ] of actualPosition)
1270 if(dirZ > All) // != All,None
1272 FindDirection deltaV = delta.y > 0 ? In : Out;
1279 // compare child to closest child in terms of distance.
1280 float distance2 = 0.0f;
1282 // distance2 = the Square of the relevant dimensions of delta
1285 distance2 += delta.x * delta.x;
1290 distance2 += delta.y * delta.y;
1295 distance2 += delta.z * delta.z;
1298 if(closestChild) // Next time.
1300 if(distance2 < closestDistance2)
1302 closestChild = child;
1303 closestDistance2 = distance2;
1308 closestChild = child;
1309 closestDistance2 = distance2;
1313 return closestChild;
1316 bool ScrollView::ScrollToSnapPoint()
1318 Vector2 stationaryVelocity = Vector2(0.0f, 0.0f);
1319 return SnapWithVelocity( stationaryVelocity );
1322 void ScrollView::ScaleTo(const Vector3& scale)
1324 ScaleTo(scale, mSnapDuration);
1327 void ScrollView::ScaleTo(const Vector3& scale, float duration)
1329 TransformTo(mScrollPostPosition, scale, mScrollPostRotation, duration);
1333 // TODO: In situations where axes are different (X snap, Y free)
1334 // Each axis should really have their own independent animation (time and equation)
1335 // Consider, X axis snapping to nearest grid point (EaseOut over fixed time)
1336 // Consider, Y axis simulating physics to arrive at a point (Physics equation over variable time)
1337 // Currently, the axes have been split however, they both use the same EaseOut equation.
1338 bool ScrollView::SnapWithVelocity(Vector2 velocity)
1340 // Animator takes over now, touches are assumed not to interfere.
1341 // And if touches do interfere, then we'll stop animation, update PrePosition
1342 // to current mScroll's properties, and then resume.
1343 // Note: For Flicking this may work a bit different...
1345 float angle = atan2(velocity.y, velocity.x);
1346 float speed2 = velocity.LengthSquared();
1347 AlphaFunction alphaFunction = mSnapAlphaFunction;
1348 Vector3 positionDuration = Vector3::ONE * mSnapDuration;
1349 Vector3 scaleDuration = Vector3::ONE * mSnapDuration;
1350 float rotationDuration = mSnapDuration;
1353 FindDirection horizontal = None;
1354 FindDirection vertical = None;
1356 // orthoAngleRange = Angle tolerance within the Exact N,E,S,W direction
1357 // that will be accepted as a general N,E,S,W flick direction.
1359 const float orthoAngleRange = FLICK_ORTHO_ANGLE_RANGE * M_PI / 180.0f;
1360 const float flickSpeedThreshold2 = FLICK_SPEED_THRESHOLD*FLICK_SPEED_THRESHOLD;
1362 Vector3 positionSnap = mScrollPostPosition;
1364 // Flick logic X Axis
1366 if(mRulerX->IsEnabled())
1370 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1371 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1373 if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East
1375 biasX = 0.0f, horizontal = Left;
1377 // This guards against an error where no movement occurs, due to the flick finishing
1378 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1379 positionSnap.x += 1.0f;
1381 else if((angle >= M_PI-orthoAngleRange) || (angle < -M_PI+orthoAngleRange)) // Swiping West
1383 biasX = 1.0f, horizontal = Right;
1385 // This guards against an error where no movement occurs, due to the flick finishing
1386 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1387 positionSnap.x -= 1.0f;
1392 // Flick logic Y Axis
1394 if(mRulerY->IsEnabled())
1398 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1399 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1401 if((angle >= M_PI_2-orthoAngleRange) && (angle < M_PI_2+orthoAngleRange)) // Swiping South
1403 biasY = 0.0f, vertical = Up;
1405 else if((angle >= -M_PI_2-orthoAngleRange) && (angle < -M_PI_2+orthoAngleRange)) // Swiping North
1407 biasY = 1.0f, vertical = Down;
1412 // isFlick: Whether this gesture is a flick or not.
1413 bool isFlick = (horizontal != All || vertical != All);
1414 // isFreeFlick: Whether this gesture is a flick under free panning criteria.
1415 bool isFreeFlick = velocity.LengthSquared() > (FREE_FLICK_SPEED_THRESHOLD*FREE_FLICK_SPEED_THRESHOLD);
1417 if(isFlick || isFreeFlick)
1419 positionDuration = Vector3::ONE * mFlickDuration;
1420 alphaFunction = mFlickAlphaFunction;
1423 // Calculate next positionSnap ////////////////////////////////////////////////////////////
1425 if(mActorAutoSnapEnabled)
1427 Vector3 size = Self().GetCurrentSize();
1429 Actor child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f), horizontal, vertical );
1431 if(!child && isFlick )
1433 // If we conducted a direction limited search and found no actor, then just snap to the closest actor.
1434 child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f) );
1439 Vector3 position = Self().GetProperty<Vector3>(mPropertyPosition);
1441 // Get center-point of the Actor.
1442 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1444 if(mRulerX->IsEnabled())
1446 positionSnap.x = position.x - childPosition.x + size.width * 0.5f;
1448 if(mRulerY->IsEnabled())
1450 positionSnap.y = position.y - childPosition.y + size.height * 0.5f;
1455 Vector3 startPosition = positionSnap;
1456 positionSnap.x = -mRulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system.
1457 positionSnap.y = -mRulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
1459 Vector3 clampDelta(Vector3::ZERO);
1460 ClampPosition(positionSnap);
1462 if( (mRulerX->GetType() == Ruler::Free || mRulerY->GetType() == Ruler::Free)
1463 && isFreeFlick && !mActorAutoSnapEnabled)
1465 // Calculate target position based on velocity of flick.
1467 // a = Deceleration (Set to diagonal stage length * friction coefficient)
1468 // u = Initial Velocity (Flick velocity)
1469 // v = 0 (Final Velocity)
1470 // t = Time (Velocity / Deceleration)
1471 Vector2 stageSize = Stage::GetCurrent().GetSize();
1472 float stageLength = Vector3(stageSize.x, stageSize.y, 0.0f).Length();
1473 float a = (stageLength * mFrictionCoefficient);
1474 Vector3 u = Vector3(velocity.x, velocity.y, 0.0f) * mFlickSpeedCoefficient;
1475 float speed = u.Length();
1478 // TODO: Change this to a decay function. (faster you flick, the slower it should be)
1479 speed = std::min(speed, stageLength * mMaxFlickSpeed );
1481 alphaFunction = ConstantDecelerationAlphaFunction;
1483 float t = speed / a;
1485 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1487 positionSnap.x += t*u.x*0.5f;
1490 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1492 positionSnap.y += t*u.y*0.5f;
1495 clampDelta = positionSnap;
1496 ClampPosition(positionSnap);
1497 if((positionSnap - startPosition).LengthSquared() > Math::MACHINE_EPSILON_0)
1499 clampDelta -= positionSnap;
1500 clampDelta.x = clampDelta.x > 0.0f ? std::min(clampDelta.x, mMaxOvershoot.x) : std::max(clampDelta.x, -mMaxOvershoot.x);
1501 clampDelta.y = clampDelta.y > 0.0f ? std::min(clampDelta.y, mMaxOvershoot.y) : std::max(clampDelta.y, -mMaxOvershoot.y);
1505 clampDelta = Vector3::ZERO;
1508 // If Axis is Free and has velocity, then calculate time taken
1509 // to reach target based on velocity in axis.
1510 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1512 float deltaX = fabsf(startPosition.x - positionSnap.x);
1514 if(fabsf(u.x) > Math::MACHINE_EPSILON_1)
1516 positionDuration.x = fabsf(deltaX / u.x);
1520 positionDuration.x = 0;
1524 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1526 float deltaY = fabsf(startPosition.y - positionSnap.y);
1528 if(fabsf(u.y) > Math::MACHINE_EPSILON_1)
1530 positionDuration.y = fabsf(deltaY / u.y);
1534 positionDuration.y = 0;
1538 positionSnap += clampDelta;
1540 // Scale Snap ///////////////////////////////////////////////////////////////
1541 Vector3 scaleSnap = mScrollPostScale;
1543 scaleSnap.x = mRulerScaleX->Snap(scaleSnap.x);
1544 scaleSnap.y = mRulerScaleY->Snap(scaleSnap.y);
1546 ClampScale(scaleSnap);
1548 // Rotation Snap ////////////////////////////////////////////////////////////
1549 float rotationSnap = mScrollPostRotation;
1550 // TODO: implement rotation snap
1552 bool animating = AnimateTo(positionSnap, positionDuration,
1553 scaleSnap, scaleDuration,
1554 rotationSnap, rotationDuration,
1555 alphaFunction, false,
1556 DirectionBiasNone, DirectionBiasNone,
1557 isFlick || isFreeFlick ? Flick : Snap);
1561 AnimateOvershootToOrigin(positionDuration.x, positionDuration.y);
1567 void ScrollView::StopAnimation(void)
1569 // Clear Snap animation if exists.
1572 mSnapAnimation.Stop();
1573 mSnapAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapAnimationFinished);
1574 mSnapAnimation.Clear();
1575 mSnapAnimation = NULL;
1579 mSnapXAnimation.Stop();
1580 mSnapXAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapXAnimationFinished);
1581 mSnapXAnimation.Clear();
1582 mSnapXAnimation = NULL;
1586 mSnapYAnimation.Stop();
1587 mSnapYAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapYAnimationFinished);
1588 mSnapYAnimation.Clear();
1589 mSnapYAnimation = NULL;
1591 if(mSnapOvershootAnimation)
1593 mSnapOvershootAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapOvershootAnimationFinished);
1594 mSnapOvershootAnimation.Stop();
1595 mSnapOvershootAnimation.Clear();
1596 mSnapOvershootAnimation = NULL;
1598 HandleStoppedAnimation();
1601 bool ScrollView::AnimateTo(const Vector3& position, const Vector3& positionDuration,
1602 const Vector3& scale, const Vector3& scaleDuration,
1603 float rotation, float rotationDuration,
1604 AlphaFunction alpha, bool findShortcuts,
1605 DirectionBias horizontalBias, DirectionBias verticalBias,
1608 // Here we perform an animation on a number of properties (depending on which have changed)
1609 // The animation is applied to all ScrollBases
1610 Actor self = Self();
1611 bool startAnimation = false;
1612 Vector3 positionTransformed = position;
1613 float totalDuration = 0.0f;
1615 bool positionChanged = (positionTransformed != mScrollPostPosition);
1616 bool scaleChanged = (scale != mScrollPostScale);
1617 bool rotationChanged = fabsf(rotation - mScrollPostRotation) > Math::MACHINE_EPSILON_0;
1621 totalDuration = std::max(totalDuration, positionDuration.x);
1622 totalDuration = std::max(totalDuration, positionDuration.y);
1627 totalDuration = std::max(totalDuration, scaleDuration.x);
1628 totalDuration = std::max(totalDuration, scaleDuration.y);
1633 totalDuration = std::max(totalDuration, rotationDuration);
1636 if(totalDuration > Math::MACHINE_EPSILON_1)
1639 mSnapAnimation = Animation::New(totalDuration);
1640 mSnapAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapAnimationFinished);
1641 mSnapXAnimation = Animation::New(positionDuration.x);
1642 mSnapXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapXAnimationFinished);
1643 mSnapYAnimation = Animation::New(positionDuration.y);
1644 mSnapYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapYAnimationFinished);
1645 startAnimation = true;
1647 // Position Delta ///////////////////////////////////////////////////////
1650 if(mWrapMode && findShortcuts)
1652 // In Wrap Mode, the shortest distance is a little less intuitive...
1653 const RulerDomain rulerDomainX = mRulerX->GetDomain();
1654 const RulerDomain rulerDomainY = mRulerY->GetDomain();
1656 if(mRulerX->IsEnabled())
1658 float dir = VectorInDomain(-mScrollPostPosition.x, -positionTransformed.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
1659 positionTransformed.x = mScrollPostPosition.x + -dir;
1662 if(mRulerY->IsEnabled())
1664 float dir = VectorInDomain(-mScrollPostPosition.y, -positionTransformed.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
1665 positionTransformed.y = mScrollPostPosition.y + -dir;
1669 // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting
1670 // a horizonal/vertical wall.delay
1671 mSnapXAnimation.AnimateTo( Property(self, mPropertyX), positionTransformed.x, alpha, TimePeriod(0.0f, positionDuration.x));
1672 mSnapYAnimation.AnimateTo( Property(self, mPropertyY), positionTransformed.y, alpha, TimePeriod(0.0f, positionDuration.y));
1675 // Scale Delta ///////////////////////////////////////////////////////
1678 // TODO: for non-uniform scaling to different bounds e.g. scaling a square to a 4:3 aspect ratio screen with a velocity
1679 // the height will hit first, and then the width, so that would require two different animation times just like position.
1680 mSnapAnimation.AnimateTo( Property(self, mPropertyScale), scale, alpha, TimePeriod(0.0f, scaleDuration.x));
1683 mSnapAnimation.AnimateTo( Property(self, mPropertyTime), totalDuration, AlphaFunctions::Linear );
1685 mSnapAnimation.Play();
1686 mSnapXAnimation.Play();
1687 mSnapYAnimation.Play();
1688 StartRefreshTimer();
1689 } // end if(totalDuration > Math::MACHINE_EPSILON_1)
1690 else // totalDuration == 0
1692 // instantly set transform.
1695 self.SetProperty(mPropertyX, positionTransformed.x);
1696 self.SetProperty(mPropertyY, positionTransformed.y);
1698 mScrollPrePosition = mScrollPostPosition = positionTransformed;
1703 self.SetProperty(mPropertyScale, scale);
1705 mScrollPreScale = mScrollPostScale = scale;
1709 // Always send a snap event when AnimateTo is called.
1710 Toolkit::ScrollView::SnapEvent snapEvent;
1711 snapEvent.type = snapType;
1712 snapEvent.position = positionTransformed;
1713 snapEvent.scale = scale;
1714 snapEvent.rotation = rotation;
1715 snapEvent.duration = totalDuration;
1717 mSnapStartedSignalV2.Emit( snapEvent );
1719 return startAnimation;
1722 void ScrollView::SetOvershootEnabled(bool enabled)
1724 if(enabled && !mOvershootIndicator)
1726 mOvershootIndicator = ScrollOvershootIndicator::New(*this);
1728 mOvershootIndicator->Enable(enabled);
1731 void ScrollView::AddOverlay(Actor actor)
1733 mInternalActor.Add( actor );
1736 void ScrollView::RemoveOverlay(Actor actor)
1738 mInternalActor.Remove( actor );
1741 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
1743 PanGestureDetector panGesture( GetPanGestureDetector() );
1745 // First remove just in case we have some set, then add.
1746 panGesture.RemoveDirection( direction );
1747 panGesture.AddDirection( direction, threshold );
1750 void ScrollView::RemoveScrollingDirection( Radian direction )
1752 PanGestureDetector panGesture( GetPanGestureDetector() );
1753 panGesture.RemoveDirection( direction );
1756 Toolkit::ScrollView::SnapStartedSignalV2& ScrollView::SnapStartedSignal()
1758 return mSnapStartedSignalV2;
1761 void ScrollView::FindAndUnbindActor(Actor child)
1766 Vector3 ScrollView::GetPropertyPrePosition() const
1768 Vector3 position(Self().GetProperty<float>(mPropertyX), Self().GetProperty<float>(mPropertyY), 0.0f);
1769 WrapPosition(position);
1774 Vector3 ScrollView::GetPropertyPosition() const
1776 Vector3 position = Self().GetProperty<Vector3>(mPropertyPosition);
1777 WrapPosition(position);
1782 Vector3 ScrollView::GetPropertyScale() const
1784 return Self().GetProperty<Vector3>(mPropertyScale);
1787 void ScrollView::HandleStoppedAnimation()
1789 // Animation has stopped, so stop sending the scroll-update signal.
1790 CancelRefreshTimer();
1792 // cement transform now, and allow interactivity to resume.
1793 mScrollPostPosition = GetPropertyPosition();
1795 mScrollPostScale = GetPropertyScale();
1797 // Update Actor position with this wrapped value.
1799 Self().SetProperty(mPropertyX, mScrollPostPosition.x);
1800 Self().SetProperty(mPropertyY, mScrollPostPosition.y);
1803 mScrollPrePosition = mScrollPostPosition;
1804 mScrollPreScale = mScrollPostScale;
1805 mScrollPreRotation = mScrollPostRotation;
1808 void ScrollView::HandleSnapAnimationFinished()
1810 // Emit Signal that scrolling has completed.
1812 Self().SetProperty(mPropertyScrolling, false);
1814 Vector3 deltaPosition(Self().GetProperty<float>(mPropertyX),
1815 Self().GetProperty<float>(mPropertyY),
1818 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1819 mScrollCompletedSignalV2.Emit( currentScrollPosition );
1821 mDomainOffset += deltaPosition - mScrollPostPosition;
1822 Self().SetProperty(mPropertyDomainOffset, mDomainOffset);
1823 HandleStoppedAnimation();
1826 bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1828 Dali::BaseHandle handle( object );
1830 bool connected( true );
1831 Toolkit::ScrollView view = Toolkit::ScrollView::DownCast( handle );
1833 if( Toolkit::ScrollView::SIGNAL_SNAP_STARTED == signalName )
1835 view.SnapStartedSignal().Connect( tracker, functor );
1839 // signalName does not match any signal
1846 void ScrollView::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1848 // need to update domain properties for new size
1849 UpdatePropertyDomain(targetSize);
1852 void ScrollView::OnControlSizeSet( const Vector3& size )
1854 // need to update domain properties for new size
1855 if( mDefaultMaxOvershoot )
1857 mMaxOvershoot.x = size.x * 0.5f;
1858 mMaxOvershoot.y = size.y * 0.5f;
1860 UpdatePropertyDomain(size);
1861 UpdateMainInternalConstraint();
1862 if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) )
1864 mOvershootIndicator->Reset();
1868 void ScrollView::OnChildAdd(Actor& child)
1876 void ScrollView::OnChildRemove(Actor& child)
1878 // TODO: Actor needs a RemoveConstraint method to take out an individual constraint.
1882 void ScrollView::StartTouchDownTimer()
1884 if ( !mTouchDownTimer )
1886 mTouchDownTimer = Timer::New( TOUCH_DOWN_TIMER_INTERVAL );
1887 mTouchDownTimer.TickSignal().Connect( this, &ScrollView::OnTouchDownTimeout );
1890 mTouchDownTimer.Start();
1893 void ScrollView::StopTouchDownTimer()
1895 if ( mTouchDownTimer )
1897 mTouchDownTimer.Stop();
1901 bool ScrollView::OnTouchDownTimeout()
1903 mTouchDownTimeoutReached = true;
1905 if( mSnapAnimation || mSnapXAnimation || mSnapYAnimation || mSnapOvershootAnimation )
1907 mScrollInterrupted = true;
1911 if(mScrolling) // are we interrupting a current scroll?
1913 // reset domain offset as scrolling from original plane.
1914 mDomainOffset = Vector3::ZERO;
1915 Self().SetProperty(mPropertyDomainOffset, Vector3::ZERO);
1918 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1919 mScrollCompletedSignalV2.Emit( currentScrollPosition );
1925 bool ScrollView::OnTouchEvent(const TouchEvent& event)
1929 // Ignore this touch event, if scrollview is insensitive.
1933 // Ignore events with multiple-touch points
1934 if (event.GetPointCount() != 1)
1939 if (event.GetPoint(0).state == TouchPoint::Down)
1941 if(mGestureStackDepth==0)
1943 mTouchDownTime = event.time;
1945 // This allows time for a pan-gesture to start, to avoid breaking snap-animation behavior with fast flicks.
1946 // If touch-down does not become a pan (after timeout interval), then snap-animation can be interrupted.
1947 StartTouchDownTimer();
1950 else if(event.GetPoint(0).state == TouchPoint::Up)
1952 StopTouchDownTimer();
1954 // if the user touches and releases without enough movement to go
1955 // into a gesture state, then we should snap to nearest point.
1956 // otherwise our scroll could be stopped (interrupted) half way through an animation.
1957 if(mGestureStackDepth==0 && mTouchDownTimeoutReached)
1959 unsigned timeDelta( event.time - mTouchDownTime );
1960 if ( timeDelta >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET )
1962 // Reset the velocity only if down was received a while ago
1963 mLastVelocity = Vector2( 0.0f, 0.0f );
1966 // Only finish the transform if scrolling was interrupted on down or if we are scrolling
1967 if ( mSnapAnimation || mSnapXAnimation || mSnapYAnimation || mSnapOvershootAnimation || mScrollInterrupted || mScrolling )
1972 mTouchDownTimeoutReached = false;
1973 mScrollInterrupted = false;
1976 return true; // consume since we're potentially scrolling
1979 bool ScrollView::OnMouseWheelEvent(const MouseWheelEvent& event)
1983 // Ignore this mouse wheel event, if scrollview is insensitive.
1987 Vector3 targetScrollPosition = GetPropertyPosition();
1989 if(mRulerX->IsEnabled() && !mRulerY->IsEnabled())
1991 // If only the ruler in the X axis is enabled, scroll in the X axis.
1992 if(mRulerX->GetType() == Ruler::Free)
1994 // Free panning mode
1995 targetScrollPosition.x -= event.z * mMouseWheelScrollDistanceStep.x;
1996 ClampPosition(targetScrollPosition);
1997 ScrollTo(-targetScrollPosition);
1999 else if(!mScrolling)
2001 // Snap mode, only respond to the event when the previous snap animation is finished.
2002 ScrollTo(GetCurrentPage() + event.z);
2007 // If the ruler in the Y axis is enabled, scroll in the Y axis.
2008 if(mRulerY->GetType() == Ruler::Free)
2010 // Free panning mode
2011 targetScrollPosition.y -= event.z * mMouseWheelScrollDistanceStep.y;
2012 ClampPosition(targetScrollPosition);
2013 ScrollTo(-targetScrollPosition);
2015 else if(!mScrolling)
2017 // Snap mode, only respond to the event when the previous snap animation is finished.
2018 ScrollTo(GetCurrentPage() + event.z * mRulerX->GetTotalPages());
2025 void ScrollView::OnSnapAnimationFinished( Animation& source )
2027 mSnapAnimation.FinishedSignal().Disconnect( this, &ScrollView::OnSnapAnimationFinished );
2028 mSnapAnimation = NULL;
2031 void ScrollView::OnSnapXAnimationFinished( Animation& source )
2033 // Guard against destruction during signal emission
2034 // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished()
2035 Toolkit::ScrollView handle( GetOwner() );
2037 if(!mSnapYAnimation)
2039 HandleSnapAnimationFinished();
2041 if(mScrollMainInternalOvershootXConstraint)
2043 Self().RemoveConstraint(mScrollMainInternalOvershootXConstraint);
2044 mScrollMainInternalOvershootXConstraint.Reset();
2045 mScrollMainInternalOvershootXConstraint = 0;
2047 mSnapXAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapXAnimationFinished);
2048 mSnapXAnimation.Reset();
2049 mSnapXAnimation = NULL;
2050 if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) )
2052 // kick start animation to 0
2053 Self().SetProperty(mPropertyOvershootX, 0.0f);
2057 void ScrollView::OnSnapYAnimationFinished( Animation& source )
2059 // Guard against destruction during signal emission
2060 // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished()
2061 Toolkit::ScrollView handle( GetOwner() );
2063 if(!mSnapXAnimation)
2065 HandleSnapAnimationFinished();
2067 if(mScrollMainInternalOvershootYConstraint)
2069 Self().RemoveConstraint(mScrollMainInternalOvershootYConstraint);
2070 mScrollMainInternalOvershootYConstraint.Reset();
2071 mScrollMainInternalOvershootYConstraint = 0;
2073 mSnapYAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapYAnimationFinished);
2074 mSnapYAnimation.Reset();
2075 mSnapYAnimation = NULL;
2076 if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) )
2078 // kick start animation to 0
2079 Self().SetProperty(mPropertyOvershootY, 0.0f);
2083 void ScrollView::GestureStarted()
2085 // we handle the first gesture.
2086 // if we're currently doing a gesture and receive another
2087 // we continue and combine the effects of the gesture instead of reseting.
2088 if(mGestureStackDepth++==0)
2090 StopTouchDownTimer();
2092 mPanDelta = Vector3::ZERO;
2093 mScaleDelta = Vector3::ONE;
2094 mRotationDelta = 0.0f;
2095 mLastVelocity = Vector2(0.0f, 0.0f);
2096 mLockAxis = LockPossible;
2098 if(mScrolling) // are we interrupting a current scroll?
2100 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
2102 Vector3 currentScrollPosition = GetCurrentScrollPosition();
2103 mScrollCompletedSignalV2.Emit( currentScrollPosition );
2108 void ScrollView::GestureContinuing(Vector2 panDelta, Vector2 scaleDelta, float rotationDelta)
2110 mPanDelta.x+= panDelta.x;
2111 mPanDelta.y+= panDelta.y;
2112 mScaleDelta.x*= scaleDelta.x;
2113 mScaleDelta.y*= scaleDelta.y;
2114 mRotationDelta+= rotationDelta;
2116 // Save the velocity, there is a bug in PanGesture
2117 // Whereby the Gesture::Finished's velocity is either:
2118 // NaN (due to time delta of zero between the last two events)
2119 // or 0 (due to position being the same between the last two events)
2121 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
2122 // appears mostly horizontal or mostly vertical respectively.
2125 if(mPanDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 &&
2126 mLockAxis == LockPossible)
2128 float dx = fabsf(mPanDelta.x);
2129 float dy = fabsf(mPanDelta.y);
2130 if(dx * mAxisAutoLockGradient >= dy)
2132 // 0.36:1 gradient to the horizontal (deviate < 20 degrees)
2133 mLockAxis = LockVertical;
2135 else if(dy * mAxisAutoLockGradient > dx)
2137 // 0.36:1 gradient to the vertical (deviate < 20 degrees)
2138 mLockAxis = LockHorizontal;
2142 mLockAxis = LockNone;
2145 } // end if mAxisAutoLock
2148 // TODO: Upgrade to use a more powerful gesture detector (one that supports multiple touches on pan - so works as pan and flick gesture)
2149 // TODO: Reimplement Scaling (pinching 2+ points)
2150 // TODO: Reimplment Rotation (pinching 2+ points)
2151 // BUG: Gesture::Finished doesn't always return velocity on release (due to
2152 // timeDelta between last two events being 0 sometimes, or posiiton being the same)
2153 void ScrollView::OnPan(PanGesture gesture)
2155 // Guard against destruction during signal emission
2156 // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx()
2157 Actor self( Self() );
2161 // If another callback on the same original signal disables sensitivity,
2162 // this callback will still be called, so we must suppress it.
2166 // translate Gesture input to get useful data...
2167 switch(gesture.state)
2169 case Gesture::Started:
2172 self.SetProperty( mPropertyPanning, true );
2173 self.SetProperty( mPropertyScrollStartPagePosition, GetCurrentScrollPosition() );
2175 // Update property: X & Y = Position (only when in panning mode - in snapping mode, X & Y are animated).
2176 if( ! mScrollMainInternalXConstraint )
2178 Constraint constraint = Constraint::New<float>( mPropertyX,
2179 LocalSource( mPropertyPosition ),
2180 Source( self, mPropertyPanning ),
2181 InternalXConstraint );
2182 mScrollMainInternalXConstraint = self.ApplyConstraint( constraint );
2184 if( ! mScrollMainInternalYConstraint )
2186 Constraint constraint = Constraint::New<float>( mPropertyY,
2187 LocalSource( mPropertyPosition ),
2188 Source( self, mPropertyPanning ),
2189 InternalYConstraint );
2190 mScrollMainInternalYConstraint = self.ApplyConstraint( constraint );
2193 // When panning we want to make sure overshoot values are affected by pre position and post position
2194 SetOvershootConstraintsEnabled(true);
2198 case Gesture::Continuing:
2200 // Nothing to do, handled in constraint.
2204 case Gesture::Finished:
2205 case Gesture::Cancelled:
2207 mLastVelocity = gesture.velocity;
2208 self.SetProperty( mPropertyPanning, false );
2210 // Remove X & Y position constraints as they are not required when we are not panning.
2211 self.RemoveConstraint(mScrollMainInternalXConstraint);
2212 self.RemoveConstraint(mScrollMainInternalYConstraint);
2213 mScrollMainInternalXConstraint.Reset();
2214 mScrollMainInternalYConstraint.Reset();
2218 case Gesture::Possible:
2219 case Gesture::Clear:
2221 // Nothing to do, not needed.
2225 } // end switch(gesture.state)
2227 OnGestureEx(gesture.state);
2230 void ScrollView::OnGestureEx(Gesture::State state)
2232 // call necessary signals for application developer
2234 if(state == Gesture::Started)
2236 Vector3 currentScrollPosition = GetCurrentScrollPosition();
2237 Self().SetProperty(mPropertyScrolling, true);
2239 mScrollStartedSignalV2.Emit( currentScrollPosition );
2241 else if( (state == Gesture::Finished) ||
2242 (state == Gesture::Cancelled) ) // Finished/default
2244 // when all the gestures have finished, we finish the transform.
2245 // so if a user decides to pan (1 gesture), and then pan+zoom (2 gestures)
2246 // then stop panning (back to 1 gesture), and then stop zooming (0 gestures).
2247 // this is the point we end, and perform necessary snapping.
2248 mGestureStackDepth--;
2249 if(mGestureStackDepth==0)
2256 void ScrollView::UpdateTransform()
2258 // TODO: notify clamps using property notifications (or see if we need this, can deprecate it)
2261 void ScrollView::FinishTransform()
2263 const Vector3& scrollPosition = Self().GetProperty<Vector3>(mPropertyPosition);
2265 mScrollPostPosition.x = scrollPosition.x;
2266 mScrollPostPosition.y = scrollPosition.y;
2268 Vector3 deltaPosition(mScrollPostPosition);
2269 // Cement PRE transform (PRE = POST), and Begin Snap Animation if necessary.
2270 WrapPosition(mScrollPostPosition);
2272 mDomainOffset += deltaPosition - mScrollPostPosition;
2273 Self().SetProperty(mPropertyDomainOffset, mDomainOffset);
2275 bool animating = SnapWithVelocity(mLastVelocity * 1000.0f);
2279 AnimateOvershootToOrigin(0.0f, 0.0f);
2280 // if not animating, then this pan has completed right now.
2282 Self().SetProperty(mPropertyScrolling, false);
2283 Vector3 currentScrollPosition = GetCurrentScrollPosition();
2284 mScrollCompletedSignalV2.Emit( currentScrollPosition );
2288 Vector3 ScrollView::GetOvershoot(Vector3& position) const
2290 Vector3 size = Self().GetCurrentSize();
2293 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2294 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2296 if(mRulerX->IsEnabled() && rulerDomainX.enabled)
2298 const float left = rulerDomainX.min - position.x;
2299 const float right = size.width - rulerDomainX.max - position.x;
2306 overshoot.x = right;
2310 if(mRulerY->IsEnabled() && rulerDomainY.enabled)
2312 const float top = rulerDomainY.min - position.y;
2313 const float bottom = size.height - rulerDomainY.max - position.y;
2320 overshoot.y = bottom;
2327 bool ScrollView::OnAccessibilityPan(PanGesture gesture)
2329 // Keep track of whether this is an AccessibilityPan
2330 mInAccessibilityPan = true;
2332 mInAccessibilityPan = false;
2337 void ScrollView::ClampPosition(Vector3& position) const
2339 ClampState3 clamped;
2340 ClampPosition(position, clamped);
2343 void ScrollView::ClampPosition(Vector3& position, ClampState3 &clamped) const
2345 Vector3 size = Self().GetCurrentSize();
2347 // determine size of viewport relative to current scaled size.
2348 // e.g. if you're zoomed in 200%, then each pixel on screen is only 0.5 pixels on subject.
2349 if(fabsf(mScrollPostScale.x) > Math::MACHINE_EPSILON_0)
2351 size.x /= mScrollPostScale.x;
2354 if(fabsf(mScrollPostScale.y) > Math::MACHINE_EPSILON_0)
2356 size.y /= mScrollPostScale.y;
2359 position.x = -mRulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system.
2360 position.y = -mRulerY->Clamp(-position.y, size.height, 1.0f, clamped.y); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
2362 clamped.z = NotClamped;
2365 void ScrollView::WrapPosition(Vector3& position) const
2369 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2370 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2372 if(mRulerX->IsEnabled())
2374 position.x = -WrapInDomain(-position.x, rulerDomainX.min, rulerDomainX.max);
2377 if(mRulerY->IsEnabled())
2379 position.y = -WrapInDomain(-position.y, rulerDomainY.min, rulerDomainY.max);
2384 void ScrollView::ClampScale(Vector3& scale) const
2386 ClampState3 clamped;
2387 ClampScale(scale, clamped);
2390 void ScrollView::ClampScale(Vector3& scale, ClampState3 &clamped) const
2392 scale.x = mRulerScaleX->Clamp(scale.x, 0.0f, 1.0f, clamped.x);
2393 scale.y = mRulerScaleY->Clamp(scale.y, 0.0f, 1.0f, clamped.y);
2394 clamped.z = NotClamped;
2397 void ScrollView::UpdateMainInternalConstraint()
2399 // TODO: Only update the constraints which have changed, rather than remove all and add all again.
2400 // Requires a dali-core ApplyConstraintAt, or a ReplaceConstraint. The former is probably more flexible.
2401 Actor self = Self();
2402 PanGestureDetector detector( GetPanGestureDetector() );
2404 if(mScrollMainInternalPrePositionConstraint)
2406 self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
2407 self.RemoveConstraint(mScrollMainInternalPositionConstraint);
2408 self.RemoveConstraint(mScrollMainInternalDeltaConstraint);
2409 self.RemoveConstraint(mScrollMainInternalFinalConstraint);
2410 self.RemoveConstraint(mScrollMainInternalRelativeConstraint);
2413 // TODO: It's probably better to use a local displacement value as this will give a displacement when scrolling just commences
2414 // but we need to make sure than the gesture system gives displacement since last frame (60Hz), not displacement since last touch event (90Hz).
2416 // 1. First calculate the pre-position (this is the scroll position if no clamping has taken place)
2417 Vector2 initialPanMask = Vector2(mRulerX->IsEnabled() ? 1.0f : 0.0f, mRulerY->IsEnabled() ? 1.0f : 0.0f);
2419 Constraint constraint = Constraint::New<Vector3>( mPropertyPrePosition,
2420 Source( detector, PanGestureDetector::LOCAL_POSITION ),
2421 Source( detector, PanGestureDetector::LOCAL_DISPLACEMENT ),
2422 LocalSource( mPropertyX ),
2423 LocalSource( mPropertyY ),
2424 Source( self, mPropertyPanning ),
2425 InternalPrePositionConstraint( initialPanMask, mAxisAutoLock, mAxisAutoLockGradient ) );
2426 mScrollMainInternalPrePositionConstraint = self.ApplyConstraint(constraint);
2428 // 2. Second calculate the clamped position (actual position)
2429 constraint = Constraint::New<Vector3>( mPropertyPosition,
2430 LocalSource( mPropertyPrePosition ),
2431 Source( self, Actor::SIZE ),
2432 InternalPositionConstraint( mRulerX->GetDomain(),
2433 mRulerY->GetDomain()) );
2434 mScrollMainInternalPositionConstraint = self.ApplyConstraint(constraint);
2436 constraint = Constraint::New<Vector3>( mPropertyPositionDelta,
2437 LocalSource( mPropertyPosition ),
2438 LocalSource( mPropertyDomainOffset ),
2439 InternalPositionDeltaConstraint );
2440 mScrollMainInternalDeltaConstraint = self.ApplyConstraint(constraint);
2442 constraint = Constraint::New<Vector3>( mPropertyFinal,
2443 LocalSource( mPropertyPosition ),
2444 LocalSource( mPropertyOvershootX ),
2445 LocalSource( mPropertyOvershootY ),
2446 InternalFinalConstraint( FinalDefaultAlphaFunction,
2447 FinalDefaultAlphaFunction ) );
2448 mScrollMainInternalFinalConstraint = self.ApplyConstraint(constraint);
2450 constraint = Constraint::New<Vector3>( mPropertyRelativePosition,
2451 LocalSource( mPropertyPosition ),
2452 LocalSource( mPropertyPositionMin ),
2453 LocalSource( mPropertyPositionMax ),
2454 LocalSource( Actor::SIZE ),
2455 InternalRelativePositionConstraint );
2456 mScrollMainInternalRelativeConstraint = self.ApplyConstraint(constraint);
2458 if(mScrollMainInternalOvershootXConstraint)
2460 // reset these constraints in correct order
2461 self.RemoveConstraint(mScrollMainInternalOvershootXConstraint);
2462 mScrollMainInternalOvershootXConstraint.Reset();
2464 Constraint constraint = Constraint::New<float>( mPropertyOvershootX,
2465 LocalSource( mPropertyPrePosition ),
2466 LocalSource( mPropertyPosition ),
2467 OvershootXConstraint(mMaxOvershoot.x) );
2468 mScrollMainInternalOvershootXConstraint = self.ApplyConstraint(constraint);
2471 if(mScrollMainInternalOvershootYConstraint)
2473 // reset these constraints in correct order
2474 self.RemoveConstraint(mScrollMainInternalOvershootYConstraint);
2475 mScrollMainInternalOvershootYConstraint.Reset();
2477 Constraint constraint = Constraint::New<float>( mPropertyOvershootY,
2478 LocalSource( mPropertyPrePosition ),
2479 LocalSource( mPropertyPosition ),
2480 OvershootXConstraint(mMaxOvershoot.y) );
2481 mScrollMainInternalOvershootYConstraint = self.ApplyConstraint(constraint);
2485 void ScrollView::SetOvershootConstraintsEnabled(bool enabled)
2487 Actor self( Self() );
2488 // remove and reset, it may now be in wrong order with the main internal constraints
2489 if(mScrollMainInternalOvershootXConstraint)
2491 self.RemoveConstraint(mScrollMainInternalOvershootXConstraint);
2492 mScrollMainInternalOvershootXConstraint.Reset();
2494 if(mScrollMainInternalOvershootYConstraint)
2496 self.RemoveConstraint(mScrollMainInternalOvershootYConstraint);
2497 mScrollMainInternalOvershootYConstraint.Reset();
2501 Constraint constraint = Constraint::New<float>( mPropertyOvershootX,
2502 LocalSource( mPropertyPrePosition ),
2503 LocalSource( mPropertyPosition ),
2504 OvershootXConstraint(mMaxOvershoot.x) );
2505 mScrollMainInternalOvershootXConstraint = self.ApplyConstraint(constraint);
2506 constraint = Constraint::New<float>( mPropertyOvershootY,
2507 LocalSource( mPropertyPrePosition ),
2508 LocalSource( mPropertyPosition ),
2509 OvershootYConstraint(mMaxOvershoot.y) );
2510 mScrollMainInternalOvershootYConstraint = self.ApplyConstraint(constraint);
2514 void ScrollView::SetInternalConstraints()
2516 // Internal constraints (applied to target ScrollBase Actor itself) /////////
2517 UpdateMainInternalConstraint();
2519 // User definable constraints to apply to all child actors //////////////////
2520 Actor self = Self();
2522 // LocalSource - The Actors to be moved.
2523 // self - The ScrollView
2525 // Apply some default constraints to ScrollView.
2526 // Movement + Scaling + Wrap function
2528 Constraint constraint;
2530 // MoveScaledActor (scrolling/zooming)
2531 constraint = Constraint::New<Vector3>( Actor::POSITION,
2532 Source( self, mPropertyPosition ),
2533 Source( self, mPropertyScale ),
2534 MoveScaledActorConstraint );
2535 constraint.SetRemoveAction(Constraint::Discard);
2536 ApplyConstraintToBoundActors(constraint);
2538 // ScaleActor (scrolling/zooming)
2539 constraint = Constraint::New<Vector3>( Actor::SCALE,
2540 Source( self, mPropertyScale ),
2541 ScaleActorConstraint );
2542 constraint.SetRemoveAction(Constraint::Discard);
2543 ApplyConstraintToBoundActors(constraint);
2545 // WrapActor (wrap functionality)
2546 constraint = Constraint::New<Vector3>( Actor::POSITION,
2547 LocalSource( Actor::SCALE ),
2548 LocalSource( Actor::ANCHOR_POINT ),
2549 LocalSource( Actor::SIZE ),
2550 Source( self, mPropertyPositionMin ),
2551 Source( self, mPropertyPositionMax ),
2552 Source( self, mPropertyWrap ),
2553 WrapActorConstraint );
2554 constraint.SetRemoveAction(Constraint::Discard);
2555 ApplyConstraintToBoundActors(constraint);
2558 void ScrollView::SetOvershootToOrigin()
2560 // Clear Snap animation if exists.
2561 if(mSnapOvershootAnimation)
2563 mSnapOvershootAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapOvershootAnimationFinished);
2564 mSnapOvershootAnimation.Stop();
2565 mSnapOvershootAnimation.Clear();
2566 mSnapOvershootAnimation = NULL;
2568 SetOvershootConstraintsEnabled(false);
2569 Self().SetProperty(mPropertyOvershootX, 0.0f);
2570 Self().SetProperty(mPropertyOvershootY, 0.0f);
2573 void ScrollView::AnimateOvershootToOrigin(float xDelay, float yDelay)
2575 if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) )
2577 if(xDelay < Math::MACHINE_EPSILON_1)
2579 // kick start animation to 0
2580 Self().SetProperty(mPropertyOvershootX, 0.0f);
2582 if(yDelay < Math::MACHINE_EPSILON_1)
2584 // kick start animation to 0
2585 Self().SetProperty(mPropertyOvershootY, 0.0f);
2589 // When we need to animate overshoot to 0
2590 if(mSnapOvershootDuration > Math::MACHINE_EPSILON_1)
2592 Actor self = Self();
2593 // Clear Snap animation if exists.
2594 if(mSnapOvershootAnimation)
2596 mSnapOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollView::OnSnapOvershootAnimationFinished );
2597 mSnapOvershootAnimation.Stop();
2598 mSnapOvershootAnimation.Clear();
2599 mSnapOvershootAnimation = NULL;
2601 if(!mSnapXAnimation && mScrollMainInternalOvershootXConstraint)
2603 // need to remove the x overshoot constraint now or it will override animation to 0
2604 Self().RemoveConstraint(mScrollMainInternalOvershootXConstraint);
2605 mScrollMainInternalOvershootXConstraint.Reset();
2606 mScrollMainInternalOvershootXConstraint = 0;
2608 if(!mSnapYAnimation && mScrollMainInternalOvershootYConstraint)
2610 // need to remove the y overshoot constraint now or it will override animation to 0
2611 Self().RemoveConstraint(mScrollMainInternalOvershootYConstraint);
2612 mScrollMainInternalOvershootYConstraint.Reset();
2613 mScrollMainInternalOvershootYConstraint = 0;
2615 // setup the new overshoot to 0 animation
2616 float totalDuration = (xDelay > yDelay ? xDelay : yDelay) + mSnapOvershootDuration;
2617 mSnapOvershootAnimation = Animation::New(totalDuration);
2618 mSnapOvershootAnimation.FinishedSignal().Connect( this, &ScrollView::OnSnapOvershootAnimationFinished );
2620 mSnapOvershootAnimation.AnimateTo( Property(self, mPropertyOvershootX), 0.0f, mSnapOvershootAlphaFunction, TimePeriod(xDelay, mSnapOvershootDuration) );
2621 mSnapOvershootAnimation.AnimateTo( Property(self, mPropertyOvershootY), 0.0f, mSnapOvershootAlphaFunction, TimePeriod(yDelay, mSnapOvershootDuration) );
2623 mSnapOvershootAnimation.SetDuration(totalDuration);
2624 mSnapOvershootAnimation.Play();
2628 SetOvershootToOrigin();
2632 void ScrollView::OnSnapOvershootAnimationFinished( Animation& source )
2634 mSnapOvershootAnimation = NULL;
2637 void ScrollView::StartRefreshTimer()
2639 if(mRefreshIntervalMilliseconds > 0)
2643 mRefreshTimer = Timer::New( mRefreshIntervalMilliseconds );
2644 mRefreshTimer.TickSignal().Connect( this, &ScrollView::OnRefreshTick );
2647 if (!mRefreshTimer.IsRunning())
2649 mRefreshTimer.Start();
2654 void ScrollView::CancelRefreshTimer()
2658 mRefreshTimer.Stop();
2662 bool ScrollView::OnRefreshTick()
2664 // Guard against destruction during signal emission
2665 Toolkit::ScrollView handle( GetOwner() );
2667 Vector3 currentScrollPosition = GetCurrentScrollPosition();
2668 mScrollUpdatedSignalV2.Emit( currentScrollPosition );
2673 } // namespace Internal
2675 } // namespace Toolkit