2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.h>
22 #include <dali/public-api/animation/constraints.h>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/public-api/events/mouse-wheel-event.h>
25 #include <dali/public-api/events/touch-event.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/public-api/controls/scrollable/scroll-component-impl.h>
32 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view-constraints.h>
33 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
34 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-effect-impl.h>
36 //#define ENABLED_SCROLL_STATE_LOGGING
38 #ifdef ENABLED_SCROLL_STATE_LOGGING
39 #define DALI_LOG_SCROLL_STATE(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d " format "\n", __PRETTY_FUNCTION__, __LINE__, ## args)
41 #define DALI_LOG_SCROLL_STATE(format, args...)
44 // TODO: Change to two class system:
45 // 1. DraggableActor (is an actor which can be dragged anywhere, can be set to range using the ruler)
46 // 2. ScrollView (contains a draggable actor that can a) be dragged in the negative X, and Y domain, b) has a hitArea for touches)
47 // TODO: external components (page and status overlays).
49 // TODO: upgrade Vector2/3 to support returning Unit vectors, normals, & cross product (dot product is already provided)
56 const Vector2 DEFAULT_MIN_FLICK_DISTANCE(30.0f, 30.0f); ///< minimum distance for pan before flick allowed
57 const float DEFAULT_MIN_FLICK_SPEED_THRESHOLD(500.0f); ///< Minimum pan speed required for flick in pixels/s
58 const float FREE_FLICK_SPEED_THRESHOLD = 200.0f; ///< Free-Flick threshold in pixels/ms
59 const float AUTOLOCK_AXIS_MINIMUM_DISTANCE2 = 100.0f; ///< Auto-lock axis after minimum distance squared.
60 const float FLICK_ORTHO_ANGLE_RANGE = 75.0f; ///< degrees. (if >45, then supports diagonal flicking)
61 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.
62 const unsigned long MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET( 150u );
63 const float TOUCH_DOWN_TIMER_INTERVAL = 100.0f;
64 const float DEFAULT_SCROLL_UPDATE_DISTANCE( 30.0f ); ///< Default distance to travel in pixels for scroll update signal
66 // Helpers ////////////////////////////////////////////////////////////////////////////////////////
69 * Find the vector (distance) from (a) to (b)
70 * in domain (start) to (end)
71 * (\ / start) (\ / end)
74 * @note assumes both (a) and (b) are already with the domain
77 * @param[in] a the current point
78 * @param[in] b the target point
79 * @param[in] start the start of the domain
80 * @param[in] end the end of the domain
81 * @param[in] bias whether to only take the right direction or the left direction,
82 * or the shortest direction.
83 * @return the shortest direction and distance
85 float VectorInDomain(float a, float b, float start, float end, Dali::Toolkit::DirectionBias bias)
87 if(bias == Dali::Toolkit::DirectionBiasNone)
89 return ShortestDistanceInDomain( a, b, start, end );
92 float size = end-start;
98 if(bias == Dali::Toolkit::DirectionBiasRight) // going right, take the vector.
104 float aRight = a+size;
111 if(bias == Dali::Toolkit::DirectionBiasLeft) // going left, take the vector.
117 float aLeft = a-size;
124 * Returns the position of the anchor within actor
126 * @param actor The Actor
127 * @param anchor The Anchor point of interest.
128 * @return The position of the Anchor
130 Vector3 GetPositionOfAnchor(Actor &actor, const Vector3 &anchor)
132 Vector3 childPosition = actor.GetCurrentPosition();
133 Vector3 childAnchor = - actor.GetCurrentAnchorPoint() + anchor;
134 Vector3 childSize = actor.GetCurrentSize();
136 return childPosition + childAnchor * childSize;
139 // AlphaFunctions /////////////////////////////////////////////////////////////////////////////////
141 float FinalDefaultAlphaFunction(float offset)
143 return offset * 0.5f;
147 * ConstantDecelerationAlphaFunction
148 * Newtoninan distance for constant deceleration
149 * v = 1 - t, s = t - 1/2 t^2
150 * when t = 0, s = 0.0 (min distance)
151 * when t = 1, s = 0.5 (max distance)
152 * progress = s / (max-min) = 2t - t^2
154 * @param[in] offset The input progress
155 * @return The output progress
157 float ConstantDecelerationAlphaFunction(float progress)
159 return progress * 2.0f - progress * progress;
162 // Internal Constraints ///////////////////////////////////////////////////////////////////////////
165 * Internal Relative position Constraint
166 * Generates the relative position value of the scroll view
167 * based on the absolute position, and it's relation to the
168 * scroll domain. This is a value from 0.0f to 1.0f in each
169 * scroll position axis.
171 Vector3 InternalRelativePositionConstraint(const Vector3& current,
172 const PropertyInput& scrollPositionProperty,
173 const PropertyInput& scrollMinProperty,
174 const PropertyInput& scrollMaxProperty,
175 const PropertyInput& scrollSizeProperty)
177 Vector3 position = -scrollPositionProperty.GetVector3();
178 const Vector3& min = scrollMinProperty.GetVector3();
179 const Vector3& max = scrollMaxProperty.GetVector3();
180 const Vector3& size = scrollSizeProperty.GetVector3();
182 position.x = WrapInDomain(position.x, min.x, max.x);
183 position.y = WrapInDomain(position.y, min.y, max.y);
185 Vector3 relativePosition;
186 Vector3 domainSize = (max - min) - size;
188 relativePosition.x = domainSize.x > Math::MACHINE_EPSILON_1 ? fabsf((position.x - min.x) / domainSize.x) : 0.0f;
189 relativePosition.y = domainSize.y > Math::MACHINE_EPSILON_1 ? fabsf((position.y - min.y) / domainSize.y) : 0.0f;
191 return relativePosition;
194 } // unnamed namespace
210 return Toolkit::ScrollView::New();
213 // Setup properties, signals and actions using the type-registry.
214 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ScrollView, Toolkit::Scrollable, Create )
216 DALI_SIGNAL_REGISTRATION( ScrollView, "value-changed", SIGNAL_SNAP_STARTED )
218 DALI_TYPE_REGISTRATION_END()
221 * Returns whether to lock scrolling to a particular axis
223 * @param[in] panDelta Distance panned since gesture started
224 * @param[in] currentLockAxis The current lock axis value
225 * @param[in] lockGradient How quickly to lock to a particular axis
227 * @return The new axis lock state
229 ScrollView::LockAxis GetLockAxis(const Vector2& panDelta, ScrollView::LockAxis currentLockAxis, float lockGradient)
231 if(panDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 &&
232 currentLockAxis == ScrollView::LockPossible)
234 float dx = fabsf(panDelta.x);
235 float dy = fabsf(panDelta.y);
236 if(dx * lockGradient >= dy)
238 // 0.36:1 gradient to the horizontal (deviate < 20 degrees)
239 currentLockAxis = ScrollView::LockVertical;
241 else if(dy * lockGradient > dx)
243 // 0.36:1 gradient to the vertical (deviate < 20 degrees)
244 currentLockAxis = ScrollView::LockHorizontal;
248 currentLockAxis = ScrollView::LockNone;
251 return currentLockAxis;
255 * Internal Pre-Position Property Constraint.
257 * Generates position property based on current position + gesture displacement.
258 * Or generates position property based on positionX/Y.
259 * Note: This is the position prior to any clamping at scroll boundaries.
261 struct InternalPrePositionConstraint
263 InternalPrePositionConstraint(const Vector2& initialPanPosition,
264 const Vector2& initialPanMask,
266 float axisAutoLockGradient,
267 ScrollView::LockAxis initialLockAxis,
268 const Vector2& maxOvershoot,
269 const RulerDomain& domainX, const RulerDomain& domainY)
270 : mLocalStart(initialPanPosition),
271 mInitialPanMask(initialPanMask),
272 mDomainMin( -domainX.min, -domainY.min ),
273 mDomainMax( -domainX.max, -domainY.max ),
274 mMaxOvershoot(maxOvershoot),
275 mAxisAutoLockGradient(axisAutoLockGradient),
276 mLockAxis(initialLockAxis),
277 mAxisAutoLock(axisAutoLock),
279 mClampX( domainX.enabled ),
280 mClampY( domainY.enabled )
284 Vector3 operator()(const Vector3& current,
285 const PropertyInput& gesturePositionProperty,
286 const PropertyInput& sizeProperty)
288 Vector3 scrollPostPosition = current;
289 Vector2 panPosition = gesturePositionProperty.GetVector2();
293 mPrePosition = current;
294 mCurrentPanMask = mInitialPanMask;
298 // Calculate Deltas...
299 Vector2 currentPosition = gesturePositionProperty.GetVector2();
300 Vector2 panDelta( currentPosition - mLocalStart );
302 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
303 // appears mostly horizontal or mostly vertical respectively...
306 mLockAxis = GetLockAxis(panDelta, mLockAxis, mAxisAutoLockGradient);
307 if( mLockAxis == ScrollView::LockVertical )
309 mCurrentPanMask.y = 0.0f;
311 else if( mLockAxis == ScrollView::LockHorizontal )
313 mCurrentPanMask.x = 0.0f;
317 // Restrict deltas based on ruler enable/disable and axis-lock state...
318 panDelta *= mCurrentPanMask;
320 // Perform Position transform based on input deltas...
321 scrollPostPosition = mPrePosition;
322 scrollPostPosition.GetVectorXY() += panDelta;
324 // if no wrapping then clamp preposition to maximum overshoot amount
325 const Vector3& size = sizeProperty.GetVector3();
328 float newXPosition = Clamp(scrollPostPosition.x, (mDomainMax.x + size.x) - mMaxOvershoot.x, mDomainMin.x + mMaxOvershoot.x );
329 if( (newXPosition < scrollPostPosition.x - Math::MACHINE_EPSILON_1)
330 || (newXPosition > scrollPostPosition.x + Math::MACHINE_EPSILON_1) )
332 mPrePosition.x = newXPosition;
333 mLocalStart.x = panPosition.x;
335 scrollPostPosition.x = newXPosition;
339 float newYPosition = Clamp(scrollPostPosition.y, (mDomainMax.y + size.y) - mMaxOvershoot.y, mDomainMin.y + mMaxOvershoot.y );
340 if( (newYPosition < scrollPostPosition.y - Math::MACHINE_EPSILON_1)
341 || (newYPosition > scrollPostPosition.y + Math::MACHINE_EPSILON_1) )
343 mPrePosition.y = newYPosition;
344 mLocalStart.y = panPosition.y;
346 scrollPostPosition.y = newYPosition;
349 return scrollPostPosition;
352 Vector3 mPrePosition;
354 Vector2 mInitialPanMask; ///< Initial pan mask (based on ruler settings)
355 Vector2 mCurrentPanMask; ///< Current pan mask that can be altered by axis lock mode.
358 Vector2 mMaxOvershoot;
360 float mAxisAutoLockGradient; ///< Set by ScrollView
361 ScrollView::LockAxis mLockAxis;
363 bool mAxisAutoLock:1; ///< Set by ScrollView
370 * Internal Position Property Constraint.
372 * Generates position property based on pre-position
373 * Note: This is the position after clamping.
374 * (uses result of InternalPrePositionConstraint)
376 struct InternalPositionConstraint
378 InternalPositionConstraint(const RulerDomain& domainX, const RulerDomain& domainY, bool wrap)
379 : mDomainMin( -domainX.min, -domainY.min ),
380 mDomainMax( -domainX.max, -domainY.max ),
381 mClampX( domainX.enabled ),
382 mClampY( domainY.enabled ),
387 Vector3 operator()(const Vector3& current,
388 const PropertyInput& scrollPositionProperty,
389 const PropertyInput& scrollMinProperty,
390 const PropertyInput& scrollMaxProperty,
391 const PropertyInput& scrollSizeProperty)
393 Vector3 position = scrollPositionProperty.GetVector3();
394 const Vector2& size = scrollSizeProperty.GetVector3().GetVectorXY();
395 const Vector3& min = scrollMinProperty.GetVector3();
396 const Vector3& max = scrollMaxProperty.GetVector3();
400 position.x = -WrapInDomain(-position.x, min.x, max.x);
401 position.y = -WrapInDomain(-position.y, min.y, max.y);
405 // clamp post position to domain
406 position.x = mClampX ? Clamp(position.x, mDomainMax.x + size.x, mDomainMin.x ) : position.x;
407 position.y = mClampY ? Clamp(position.y, mDomainMax.y + size.y, mDomainMin.y ) : position.y;
422 * This constraint updates the X overshoot property using the difference
423 * mPropertyPrePosition.x and mPropertyPosition.x, returning a relative value between 0.0f and 1.0f
425 struct OvershootXConstraint
427 OvershootXConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
429 float operator()(const float& current,
430 const PropertyInput& scrollPrePositionProperty,
431 const PropertyInput& scrollPostPositionProperty,
432 const PropertyInput& canScrollProperty)
434 if( canScrollProperty.GetBoolean() )
436 const Vector3& scrollPrePosition = scrollPrePositionProperty.GetVector3();
437 const Vector3& scrollPostPosition = scrollPostPositionProperty.GetVector3();
438 float newOvershoot = scrollPrePosition.x - scrollPostPosition.x;
439 return (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
448 * This constraint updates the Y overshoot property using the difference
449 * mPropertyPrePosition.y and mPropertyPosition.y, returning a relative value between 0.0f and 1.0f
451 struct OvershootYConstraint
453 OvershootYConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
455 float operator()(const float& current,
456 const PropertyInput& scrollPrePositionProperty,
457 const PropertyInput& scrollPostPositionProperty,
458 const PropertyInput& canScrollProperty)
460 if( canScrollProperty.GetBoolean() )
462 const Vector3& scrollPrePosition = scrollPrePositionProperty.GetVector3();
463 const Vector3& scrollPostPosition = scrollPostPositionProperty.GetVector3();
464 float newOvershoot = scrollPrePosition.y - scrollPostPosition.y;
465 return (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
474 * Internal Position-Delta Property Constraint.
476 * Generates position-delta property based on scroll-position + scroll-offset properties.
478 Vector3 InternalPositionDeltaConstraint(const Vector3& current,
479 const PropertyInput& scrollPositionProperty,
480 const PropertyInput& scrollOffsetProperty)
482 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
483 const Vector3& scrollOffset = scrollOffsetProperty.GetVector3();
485 return scrollPosition + scrollOffset;
489 * Internal Final Position Constraint
490 * The position of content is:
491 * of scroll-position + f(scroll-overshoot)
492 * where f(...) function defines how overshoot
493 * should affect final-position.
495 struct InternalFinalConstraint
497 InternalFinalConstraint(AlphaFunction functionX,
498 AlphaFunction functionY)
499 : mFunctionX(functionX),
500 mFunctionY(functionY)
504 Vector3 operator()(const Vector3& current,
505 const PropertyInput& scrollPositionProperty,
506 const PropertyInput& scrollOvershootXProperty,
507 const PropertyInput& scrollOvershootYProperty)
509 const float& overshootx = scrollOvershootXProperty.GetFloat();
510 const float& overshooty = scrollOvershootYProperty.GetFloat();
511 Vector3 offset( mFunctionX(overshootx),
512 mFunctionY(overshooty),
515 return scrollPositionProperty.GetVector3() - offset;
518 AlphaFunction mFunctionX;
519 AlphaFunction mFunctionY;
525 ///////////////////////////////////////////////////////////////////////////////////////////////////
527 ///////////////////////////////////////////////////////////////////////////////////////////////////
529 Dali::Toolkit::ScrollView ScrollView::New()
531 // Create the implementation
532 ScrollViewPtr scrollView(new ScrollView());
534 // Pass ownership to CustomActor via derived handle
535 Dali::Toolkit::ScrollView handle(*scrollView);
537 // Second-phase init of the implementation
538 // This can only be done after the CustomActor connection has been made...
539 scrollView->Initialize();
544 ScrollView::ScrollView()
547 mGestureStackDepth(0),
548 mScrollStateFlags(0),
549 mLockAxis(LockPossible),
550 mScrollUpdateDistance(DEFAULT_SCROLL_UPDATE_DISTANCE),
551 mMaxOvershoot(Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT, Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT),
552 mUserMaxOvershoot(Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT, Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT),
553 mSnapOvershootDuration(Toolkit::ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION),
554 mSnapOvershootAlphaFunction(AlphaFunctions::EaseOut),
555 mSnapDuration(Toolkit::ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION),
556 mSnapAlphaFunction(AlphaFunctions::EaseOut),
557 mMinFlickDistance(DEFAULT_MIN_FLICK_DISTANCE),
558 mFlickSpeedThreshold(DEFAULT_MIN_FLICK_SPEED_THRESHOLD),
559 mFlickDuration(Toolkit::ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION),
560 mFlickAlphaFunction(AlphaFunctions::EaseOut),
561 mAxisAutoLockGradient(Toolkit::ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT),
562 mFrictionCoefficient(Toolkit::ScrollView::DEFAULT_FRICTION_COEFFICIENT),
563 mFlickSpeedCoefficient(Toolkit::ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT),
564 mMaxFlickSpeed(Toolkit::ScrollView::DEFAULT_MAX_FLICK_SPEED),
565 mInAccessibilityPan(false),
568 mScrollInterrupted(false),
571 mTouchDownTimeoutReached(false),
572 mActorAutoSnapEnabled(false),
573 mAutoResizeContainerEnabled(false),
575 mAxisAutoLock(false),
577 mDefaultMaxOvershoot(true),
578 mCanScrollHorizontal(true),
579 mCanScrollVertical(true)
581 SetRequiresMouseWheelEvents(true);
584 void ScrollView::OnInitialize()
588 // Internal Actor, used to hide actors from enumerations.
589 // Also actors added to Internal actor appear as overlays e.g. ScrollBar components.
590 mInternalActor = Actor::New();
591 mInternalActor.SetDrawMode(DrawMode::OVERLAY);
592 self.Add(mInternalActor);
593 mInternalActor.ApplyConstraint( Constraint::New<Vector3>( Actor::Property::SIZE, ParentSource( Actor::Property::SIZE ), EqualToConstraint() ) );
594 mInternalActor.SetParentOrigin(ParentOrigin::CENTER);
595 mInternalActor.SetAnchorPoint(AnchorPoint::CENTER);
599 // Register Scroll Properties.
600 RegisterProperties();
602 mScrollPostPosition = mScrollPrePosition = Vector3::ZERO;
604 mMouseWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
608 mGestureStackDepth = 0;
610 EnableGestureDetection( Gesture::Type( Gesture::Pan ) );
612 // By default we'll allow the user to freely drag the scroll view,
613 // while disabling the other rulers.
614 RulerPtr ruler = new DefaultRuler();
618 EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator);
620 Vector3 size = GetControlSize();
621 UpdatePropertyDomain(size);
622 SetInternalConstraints();
625 void ScrollView::OnControlStageConnection()
627 DALI_LOG_SCROLL_STATE("[0x%X]", this);
631 SetScrollSensitive( false );
632 SetScrollSensitive( true );
634 if(IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator))
636 // try and make sure property notifications are set
637 EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator);
641 void ScrollView::OnControlStageDisconnection()
643 DALI_LOG_SCROLL_STATE("[0x%X]", this);
648 ScrollView::~ScrollView()
650 DALI_LOG_SCROLL_STATE("[0x%X]", this);
653 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
655 return mSnapAlphaFunction;
658 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
660 mSnapAlphaFunction = alpha;
663 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
665 return mFlickAlphaFunction;
668 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
670 mFlickAlphaFunction = alpha;
673 float ScrollView::GetScrollSnapDuration() const
675 return mSnapDuration;
678 void ScrollView::SetScrollSnapDuration(float time)
680 mSnapDuration = time;
683 float ScrollView::GetScrollFlickDuration() const
685 return mFlickDuration;
688 void ScrollView::SetScrollFlickDuration(float time)
690 mFlickDuration = time;
693 void ScrollView::ApplyEffect(Toolkit::ScrollViewEffect effect)
695 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
697 // Assertion check to ensure effect doesn't already exist in this scrollview
698 bool effectAlreadyExistsInScrollView(false);
699 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
703 effectAlreadyExistsInScrollView = true;
708 DALI_ASSERT_ALWAYS(!effectAlreadyExistsInScrollView);
710 // add effect to effects list
711 mEffects.push_back(effect);
713 // invoke Attachment request to ScrollView first
714 GetImpl(effect).Attach(self);
717 void ScrollView::RemoveEffect(Toolkit::ScrollViewEffect effect)
719 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
721 // remove effect from effects list
722 bool effectExistedInScrollView(false);
723 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
727 mEffects.erase(iter);
728 effectExistedInScrollView = true;
733 // Assertion check to ensure effect existed.
734 DALI_ASSERT_ALWAYS(effectExistedInScrollView);
736 // invoke Detachment request to ScrollView last
737 GetImpl(effect).Detach(self);
740 void ScrollView::RemoveAllEffects()
742 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
744 for (ScrollViewEffectIter effectIter = mEffects.begin(); effectIter != mEffects.end(); ++effectIter)
746 Toolkit::ScrollViewEffect effect = *effectIter;
748 // invoke Detachment request to ScrollView last
749 GetImpl(effect).Detach(self);
755 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
757 ApplyConstraintToBoundActors(constraint);
760 void ScrollView::RemoveConstraintsFromChildren()
762 RemoveConstraintsFromBoundActors();
765 const RulerPtr ScrollView::GetRulerX() const
770 const RulerPtr ScrollView::GetRulerY() const
775 void ScrollView::SetRulerX(RulerPtr ruler)
779 Vector3 size = GetControlSize();
780 UpdatePropertyDomain(size);
781 UpdateMainInternalConstraint();
784 void ScrollView::SetRulerY(RulerPtr ruler)
788 Vector3 size = GetControlSize();
789 UpdatePropertyDomain(size);
790 UpdateMainInternalConstraint();
793 void ScrollView::UpdatePropertyDomain(const Vector3& size)
796 Vector3 min = mMinScroll;
797 Vector3 max = mMaxScroll;
798 bool scrollPositionChanged = false;
799 bool domainChanged = false;
801 bool canScrollVertical = false;
802 bool canScrollHorizontal = false;
803 UpdateLocalScrollProperties();
804 if(mRulerX->IsEnabled())
806 const Toolkit::RulerDomain& rulerDomain = mRulerX->GetDomain();
807 if( fabsf(min.x - rulerDomain.min) > Math::MACHINE_EPSILON_100
808 || fabsf(max.x - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
810 domainChanged = true;
811 min.x = rulerDomain.min;
812 max.x = rulerDomain.max;
814 // make sure new scroll value is within new domain
815 if( mScrollPrePosition.x < min.x
816 || mScrollPrePosition.x > max.x )
818 scrollPositionChanged = true;
819 mScrollPrePosition.x = Clamp(mScrollPrePosition.x, -(max.x - size.x), -min.x);
822 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.x) > Math::MACHINE_EPSILON_100 )
824 canScrollHorizontal = true;
827 else if( fabs(min.x) > Math::MACHINE_EPSILON_100
828 || fabs(max.x) > Math::MACHINE_EPSILON_100 )
830 // need to reset to 0
831 domainChanged = true;
834 canScrollHorizontal = false;
837 if(mRulerY->IsEnabled())
839 const Toolkit::RulerDomain& rulerDomain = mRulerY->GetDomain();
840 if( fabsf(min.y - rulerDomain.min) > Math::MACHINE_EPSILON_100
841 || fabsf(max.y - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
843 domainChanged = true;
844 min.y = rulerDomain.min;
845 max.y = rulerDomain.max;
847 // make sure new scroll value is within new domain
848 if( mScrollPrePosition.y < min.y
849 || mScrollPrePosition.y > max.y )
851 scrollPositionChanged = true;
852 mScrollPrePosition.y = Clamp(mScrollPrePosition.y, -(max.y - size.y), -min.y);
855 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.y) > Math::MACHINE_EPSILON_100 )
857 canScrollVertical = true;
860 else if( fabs(min.y) > Math::MACHINE_EPSILON_100
861 || fabs(max.y) > Math::MACHINE_EPSILON_100 )
863 // need to reset to 0
864 domainChanged = true;
867 canScrollVertical = false;
870 // avoid setting properties if possible, otherwise this will cause an entire update as well as triggering constraints using each property we update
871 if( mCanScrollVertical != canScrollVertical )
873 mCanScrollVertical = canScrollVertical;
874 self.SetProperty(mPropertyCanScrollVertical, canScrollVertical);
876 if( mCanScrollHorizontal != canScrollHorizontal )
878 mCanScrollHorizontal = canScrollHorizontal;
879 self.SetProperty(mPropertyCanScrollHorizontal, canScrollHorizontal);
881 if( scrollPositionChanged )
883 DALI_LOG_SCROLL_STATE("[0x%X] Domain Changed, setting mPropertyPrePosition To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
884 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
890 self.SetProperty(mPropertyPositionMin, mMinScroll );
891 self.SetProperty(mPropertyPositionMax, mMaxScroll );
895 void ScrollView::SetScrollSensitive(bool sensitive)
898 PanGestureDetector panGesture( GetPanGestureDetector() );
900 DALI_LOG_SCROLL_STATE("[0x%X] sensitive: before:[%d] setting[%d]", this, int(mSensitive), int(sensitive));
902 if((!mSensitive) && (sensitive))
904 mSensitive = sensitive;
905 panGesture.Attach(self);
907 else if((mSensitive) && (!sensitive))
909 DALI_LOG_SCROLL_STATE("[0x%X] BEFORE: panning:[%d]", this, int(mPanning));
911 // while the scroll view is panning, the state needs to be reset.
914 PanGesture cancelGesture( Gesture::Cancelled );
915 OnPan( cancelGesture );
918 panGesture.Detach(self);
919 mSensitive = sensitive;
921 mGestureStackDepth = 0;
922 DALI_LOG_SCROLL_STATE("[0x%X] AFTER: panning:[%d]", this, int(mPanning));
926 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
928 mMaxOvershoot.x = overshootX;
929 mMaxOvershoot.y = overshootY;
930 mUserMaxOvershoot = mMaxOvershoot;
931 mDefaultMaxOvershoot = false;
932 UpdateMainInternalConstraint();
935 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
937 mSnapOvershootAlphaFunction = alpha;
940 void ScrollView::SetSnapOvershootDuration(float duration)
942 mSnapOvershootDuration = duration;
945 void ScrollView::SetActorAutoSnap(bool enable)
947 mActorAutoSnapEnabled = enable;
950 void ScrollView::SetAutoResize(bool enable)
952 mAutoResizeContainerEnabled = enable;
953 // TODO: This needs a lot of issues to be addressed before working.
956 bool ScrollView::GetWrapMode() const
961 void ScrollView::SetWrapMode(bool enable)
964 Self().SetProperty(mPropertyWrap, enable);
967 int ScrollView::GetScrollUpdateDistance() const
969 return mScrollUpdateDistance;
972 void ScrollView::SetScrollUpdateDistance(int distance)
974 mScrollUpdateDistance = distance;
977 bool ScrollView::GetAxisAutoLock() const
979 return mAxisAutoLock;
982 void ScrollView::SetAxisAutoLock(bool enable)
984 mAxisAutoLock = enable;
985 UpdateMainInternalConstraint();
988 float ScrollView::GetAxisAutoLockGradient() const
990 return mAxisAutoLockGradient;
993 void ScrollView::SetAxisAutoLockGradient(float gradient)
995 DALI_ASSERT_DEBUG( gradient >= 0.0f && gradient <= 1.0f );
996 mAxisAutoLockGradient = gradient;
997 UpdateMainInternalConstraint();
1000 float ScrollView::GetFrictionCoefficient() const
1002 return mFrictionCoefficient;
1005 void ScrollView::SetFrictionCoefficient(float friction)
1007 DALI_ASSERT_DEBUG( friction > 0.0f );
1008 mFrictionCoefficient = friction;
1011 float ScrollView::GetFlickSpeedCoefficient() const
1013 return mFlickSpeedCoefficient;
1016 void ScrollView::SetFlickSpeedCoefficient(float speed)
1018 mFlickSpeedCoefficient = speed;
1021 Vector2 ScrollView::GetMinimumDistanceForFlick() const
1023 return mMinFlickDistance;
1026 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
1028 mMinFlickDistance = distance;
1031 float ScrollView::GetMinimumSpeedForFlick() const
1033 return mFlickSpeedThreshold;
1036 void ScrollView::SetMinimumSpeedForFlick( float speed )
1038 mFlickSpeedThreshold = speed;
1041 float ScrollView::GetMaxFlickSpeed() const
1043 return mMaxFlickSpeed;
1046 void ScrollView::SetMaxFlickSpeed(float speed)
1048 mMaxFlickSpeed = speed;
1051 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
1053 mMouseWheelScrollDistanceStep = step;
1056 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
1058 return mMouseWheelScrollDistanceStep;
1061 unsigned int ScrollView::GetCurrentPage() const
1063 // in case animation is currently taking place.
1064 Vector3 position = GetPropertyPosition();
1066 Actor self = Self();
1067 unsigned int page = 0;
1068 unsigned int pagesPerVolume = 1;
1069 unsigned int volume = 0;
1071 // if rulerX is enabled, then get page count (columns)
1072 page = mRulerX->GetPageFromPosition(-position.x, mWrapMode);
1073 volume = mRulerY->GetPageFromPosition(-position.y, mWrapMode);
1074 pagesPerVolume = mRulerX->GetTotalPages();
1076 return volume * pagesPerVolume + page;
1079 Vector3 ScrollView::GetCurrentScrollPosition() const
1081 return -GetPropertyPosition();
1084 void ScrollView::SetScrollPosition(const Vector3& position)
1086 mScrollPrePosition = position;
1089 Vector3 ScrollView::GetDomainSize() const
1091 Vector3 size = Self().GetCurrentSize();
1093 const RulerDomain& xDomain = GetRulerX()->GetDomain();
1094 const RulerDomain& yDomain = GetRulerY()->GetDomain();
1096 Vector3 domainSize = Vector3( xDomain.max - xDomain.min, yDomain.max - yDomain.min, 0.0f ) - size;
1100 void ScrollView::TransformTo(const Vector3& position,
1101 DirectionBias horizontalBias, DirectionBias verticalBias)
1103 TransformTo(position, mSnapDuration, mSnapAlphaFunction, horizontalBias, verticalBias);
1106 void ScrollView::TransformTo(const Vector3& position, float duration, AlphaFunction alpha,
1107 DirectionBias horizontalBias, DirectionBias verticalBias)
1109 // If this is called while the timer is running, then cancel it
1110 StopTouchDownTimer();
1112 Actor self( Self() );
1114 // Guard against destruction during signal emission
1115 // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo()
1116 Toolkit::ScrollView handle( GetOwner() );
1118 DALI_LOG_SCROLL_STATE("[0x%X] pos[%.2f,%.2f], duration[%.2f] bias[%d, %d]",
1119 this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1121 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1122 self.SetProperty( mPropertyScrollStartPagePosition, currentScrollPosition );
1124 if( mScrolling ) // are we interrupting a current scroll?
1126 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
1128 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1129 mScrollCompletedSignal.Emit( currentScrollPosition );
1132 if( mPanning ) // are we interrupting a current pan?
1134 DALI_LOG_SCROLL_STATE("[0x%X] Interrupting Pan, set to false", this );
1136 mGestureStackDepth = 0;
1137 self.SetProperty( mPropertyPanning, false );
1139 if( mScrollMainInternalPrePositionConstraint )
1141 self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
1145 self.SetProperty(mPropertyScrolling, true);
1148 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1149 mScrollStartedSignal.Emit( currentScrollPosition );
1150 bool animating = AnimateTo(-position,
1151 Vector3::ONE * duration,
1160 // if not animating, then this pan has completed right now.
1161 self.SetProperty(mPropertyScrolling, false);
1164 // If we have no duration, then in the next update frame, we will be at the position specified as we just set.
1165 // In this scenario, we cannot return the currentScrollPosition as this is out-of-date and should instead return the requested final position
1166 Vector3 completedPosition( currentScrollPosition );
1167 if( duration <= Math::MACHINE_EPSILON_10 )
1169 completedPosition = position;
1172 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 2 [%.2f, %.2f]", this, completedPosition.x, completedPosition.y);
1173 SetScrollUpdateNotification(false);
1174 mScrollCompletedSignal.Emit( completedPosition );
1178 void ScrollView::ScrollTo(const Vector3& position)
1180 ScrollTo(position, mSnapDuration );
1183 void ScrollView::ScrollTo(const Vector3& position, float duration)
1185 ScrollTo(position, duration, DirectionBiasNone, DirectionBiasNone);
1188 void ScrollView::ScrollTo(const Vector3& position, float duration, AlphaFunction alpha)
1190 ScrollTo(position, duration, alpha, DirectionBiasNone, DirectionBiasNone);
1193 void ScrollView::ScrollTo(const Vector3& position, float duration,
1194 DirectionBias horizontalBias, DirectionBias verticalBias)
1196 ScrollTo(position, duration, mSnapAlphaFunction, horizontalBias, verticalBias);
1199 void ScrollView::ScrollTo(const Vector3& position, float duration, AlphaFunction alpha,
1200 DirectionBias horizontalBias, DirectionBias verticalBias)
1202 DALI_LOG_SCROLL_STATE("[0x%X] position[%.2f, %.2f] duration[%.2f], bias[%d, %d]", this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1203 TransformTo(position, duration, alpha, horizontalBias, verticalBias);
1206 void ScrollView::ScrollTo(unsigned int page)
1208 ScrollTo(page, mSnapDuration);
1211 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
1214 unsigned int volume;
1215 unsigned int libraries;
1217 // The position to scroll to is continuous and linear
1218 // unless a domain has been enabled on the X axis.
1219 // or if WrapMode has been enabled.
1220 bool carryX = mRulerX->GetDomain().enabled | mWrapMode;
1221 bool carryY = mRulerY->GetDomain().enabled | mWrapMode;
1223 position.x = mRulerX->GetPositionFromPage(page, volume, carryX);
1224 position.y = mRulerY->GetPositionFromPage(volume, libraries, carryY);
1226 ScrollTo(position, duration, bias, bias);
1229 void ScrollView::ScrollTo(Actor &actor)
1231 ScrollTo(actor, mSnapDuration);
1234 void ScrollView::ScrollTo(Actor &actor, float duration)
1236 DALI_ASSERT_ALWAYS(actor.GetParent() == Self());
1238 Actor self = Self();
1239 Vector3 size = self.GetCurrentSize();
1240 Vector3 position = actor.GetCurrentPosition();
1241 position -= GetPropertyPrePosition();
1243 ScrollTo(Vector3(position.x - size.width * 0.5f, position.y - size.height * 0.5f, 0.0f), duration);
1246 Actor ScrollView::FindClosestActor()
1248 Actor self = Self();
1249 Vector3 size = self.GetCurrentSize();
1251 return FindClosestActorToPosition(Vector3(size.width * 0.5f,size.height * 0.5f,0.0f));
1254 Actor ScrollView::FindClosestActorToPosition(const Vector3& position, FindDirection dirX, FindDirection dirY, FindDirection dirZ)
1257 float closestDistance2 = 0.0f;
1258 Vector3 actualPosition = position;
1260 unsigned int numChildren = Self().GetChildCount();
1262 for(unsigned int i = 0; i < numChildren; ++i)
1264 Actor child = Self().GetChildAt(i);
1266 if(mInternalActor == child) // ignore internal actor.
1271 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1273 Vector3 delta = childPosition - actualPosition;
1275 // X-axis checking (only find Actors to the [dirX] of actualPosition)
1276 if(dirX > All) // != All,None
1278 FindDirection deltaH = delta.x > 0 ? Right : Left;
1285 // Y-axis checking (only find Actors to the [dirY] of actualPosition)
1286 if(dirY > All) // != All,None
1288 FindDirection deltaV = delta.y > 0 ? Down : Up;
1295 // Z-axis checking (only find Actors to the [dirZ] of actualPosition)
1296 if(dirZ > All) // != All,None
1298 FindDirection deltaV = delta.y > 0 ? In : Out;
1305 // compare child to closest child in terms of distance.
1306 float distance2 = 0.0f;
1308 // distance2 = the Square of the relevant dimensions of delta
1311 distance2 += delta.x * delta.x;
1316 distance2 += delta.y * delta.y;
1321 distance2 += delta.z * delta.z;
1324 if(closestChild) // Next time.
1326 if(distance2 < closestDistance2)
1328 closestChild = child;
1329 closestDistance2 = distance2;
1334 closestChild = child;
1335 closestDistance2 = distance2;
1339 return closestChild;
1342 bool ScrollView::ScrollToSnapPoint()
1344 DALI_LOG_SCROLL_STATE("[0x%X]", this );
1345 Vector2 stationaryVelocity = Vector2(0.0f, 0.0f);
1346 return SnapWithVelocity( stationaryVelocity );
1349 // TODO: In situations where axes are different (X snap, Y free)
1350 // Each axis should really have their own independent animation (time and equation)
1351 // Consider, X axis snapping to nearest grid point (EaseOut over fixed time)
1352 // Consider, Y axis simulating physics to arrive at a point (Physics equation over variable time)
1353 // Currently, the axes have been split however, they both use the same EaseOut equation.
1354 bool ScrollView::SnapWithVelocity(Vector2 velocity)
1356 // Animator takes over now, touches are assumed not to interfere.
1357 // And if touches do interfere, then we'll stop animation, update PrePosition
1358 // to current mScroll's properties, and then resume.
1359 // Note: For Flicking this may work a bit different...
1361 float angle = atan2(velocity.y, velocity.x);
1362 float speed2 = velocity.LengthSquared();
1363 AlphaFunction alphaFunction = mSnapAlphaFunction;
1364 Vector3 positionDuration = Vector3::ONE * mSnapDuration;
1367 FindDirection horizontal = None;
1368 FindDirection vertical = None;
1370 // orthoAngleRange = Angle tolerance within the Exact N,E,S,W direction
1371 // that will be accepted as a general N,E,S,W flick direction.
1373 const float orthoAngleRange = FLICK_ORTHO_ANGLE_RANGE * M_PI / 180.0f;
1374 const float flickSpeedThreshold2 = mFlickSpeedThreshold * mFlickSpeedThreshold;
1376 Vector3 positionSnap = mScrollPrePosition;
1378 // Flick logic X Axis
1380 if(mRulerX->IsEnabled() && mLockAxis != LockHorizontal)
1384 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1385 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1387 if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East
1389 biasX = 0.0f, horizontal = Left;
1391 // This guards against an error where no movement occurs, due to the flick finishing
1392 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1393 positionSnap.x += 1.0f;
1395 else if((angle >= M_PI-orthoAngleRange) || (angle < -M_PI+orthoAngleRange)) // Swiping West
1397 biasX = 1.0f, horizontal = Right;
1399 // This guards against an error where no movement occurs, due to the flick finishing
1400 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1401 positionSnap.x -= 1.0f;
1406 // Flick logic Y Axis
1408 if(mRulerY->IsEnabled() && mLockAxis != LockVertical)
1412 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1413 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1415 if((angle >= M_PI_2-orthoAngleRange) && (angle < M_PI_2+orthoAngleRange)) // Swiping South
1417 biasY = 0.0f, vertical = Up;
1419 else if((angle >= -M_PI_2-orthoAngleRange) && (angle < -M_PI_2+orthoAngleRange)) // Swiping North
1421 biasY = 1.0f, vertical = Down;
1426 // isFlick: Whether this gesture is a flick or not.
1427 bool isFlick = (horizontal != All || vertical != All);
1428 // isFreeFlick: Whether this gesture is a flick under free panning criteria.
1429 bool isFreeFlick = velocity.LengthSquared() > (FREE_FLICK_SPEED_THRESHOLD*FREE_FLICK_SPEED_THRESHOLD);
1431 if(isFlick || isFreeFlick)
1433 positionDuration = Vector3::ONE * mFlickDuration;
1434 alphaFunction = mFlickAlphaFunction;
1437 // Calculate next positionSnap ////////////////////////////////////////////////////////////
1439 if(mActorAutoSnapEnabled)
1441 Vector3 size = Self().GetCurrentSize();
1443 Actor child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f), horizontal, vertical );
1445 if(!child && isFlick )
1447 // If we conducted a direction limited search and found no actor, then just snap to the closest actor.
1448 child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f) );
1453 Vector3 position = Self().GetProperty<Vector3>(mPropertyPosition);
1455 // Get center-point of the Actor.
1456 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1458 if(mRulerX->IsEnabled())
1460 positionSnap.x = position.x - childPosition.x + size.width * 0.5f;
1462 if(mRulerY->IsEnabled())
1464 positionSnap.y = position.y - childPosition.y + size.height * 0.5f;
1469 Vector3 startPosition = positionSnap;
1470 positionSnap.x = -mRulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system.
1471 positionSnap.y = -mRulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
1473 Vector3 clampDelta(Vector3::ZERO);
1474 ClampPosition(positionSnap);
1476 if( (mRulerX->GetType() == Ruler::Free || mRulerY->GetType() == Ruler::Free)
1477 && isFreeFlick && !mActorAutoSnapEnabled)
1479 // Calculate target position based on velocity of flick.
1481 // a = Deceleration (Set to diagonal stage length * friction coefficient)
1482 // u = Initial Velocity (Flick velocity)
1483 // v = 0 (Final Velocity)
1484 // t = Time (Velocity / Deceleration)
1485 Vector2 stageSize = Stage::GetCurrent().GetSize();
1486 float stageLength = Vector3(stageSize.x, stageSize.y, 0.0f).Length();
1487 float a = (stageLength * mFrictionCoefficient);
1488 Vector3 u = Vector3(velocity.x, velocity.y, 0.0f) * mFlickSpeedCoefficient;
1489 float speed = u.Length();
1492 // TODO: Change this to a decay function. (faster you flick, the slower it should be)
1493 speed = std::min(speed, stageLength * mMaxFlickSpeed );
1495 alphaFunction = ConstantDecelerationAlphaFunction;
1497 float t = speed / a;
1499 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1501 positionSnap.x += t*u.x*0.5f;
1504 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1506 positionSnap.y += t*u.y*0.5f;
1509 clampDelta = positionSnap;
1510 ClampPosition(positionSnap);
1511 if((positionSnap - startPosition).LengthSquared() > Math::MACHINE_EPSILON_0)
1513 clampDelta -= positionSnap;
1514 clampDelta.x = clampDelta.x > 0.0f ? std::min(clampDelta.x, mMaxOvershoot.x) : std::max(clampDelta.x, -mMaxOvershoot.x);
1515 clampDelta.y = clampDelta.y > 0.0f ? std::min(clampDelta.y, mMaxOvershoot.y) : std::max(clampDelta.y, -mMaxOvershoot.y);
1519 clampDelta = Vector3::ZERO;
1522 // If Axis is Free and has velocity, then calculate time taken
1523 // to reach target based on velocity in axis.
1524 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1526 float deltaX = fabsf(startPosition.x - positionSnap.x);
1528 if(fabsf(u.x) > Math::MACHINE_EPSILON_1)
1530 positionDuration.x = fabsf(deltaX / u.x);
1534 positionDuration.x = 0;
1538 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1540 float deltaY = fabsf(startPosition.y - positionSnap.y);
1542 if(fabsf(u.y) > Math::MACHINE_EPSILON_1)
1544 positionDuration.y = fabsf(deltaY / u.y);
1548 positionDuration.y = 0;
1553 if(IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator))
1555 // Scroll to the end of the overshoot only when overshoot is enabled.
1556 positionSnap += clampDelta;
1559 bool animating = AnimateTo(positionSnap, positionDuration,
1560 alphaFunction, false,
1561 DirectionBiasNone, DirectionBiasNone,
1562 isFlick || isFreeFlick ? Flick : Snap);
1567 void ScrollView::StopAnimation(void)
1569 // Clear Snap animation if exists.
1570 StopAnimation(mInternalXAnimation);
1571 StopAnimation(mInternalYAnimation);
1572 mScrollStateFlags = 0;
1573 // remove scroll animation flags
1574 HandleStoppedAnimation();
1577 void ScrollView::StopAnimation(Animation& animation)
1586 bool ScrollView::AnimateTo(const Vector3& position, const Vector3& positionDuration,
1587 AlphaFunction alpha, bool findShortcuts,
1588 DirectionBias horizontalBias, DirectionBias verticalBias,
1591 // Here we perform an animation on a number of properties (depending on which have changed)
1592 // The animation is applied to all ScrollBases
1593 Actor self = Self();
1594 mScrollTargetPosition = position;
1595 float totalDuration = 0.0f;
1597 bool positionChanged = (mScrollTargetPosition != mScrollPostPosition);
1601 totalDuration = std::max(totalDuration, positionDuration.x);
1602 totalDuration = std::max(totalDuration, positionDuration.y);
1606 // try to animate for a frame, on some occasions update will be changing scroll value while event side thinks it hasnt changed
1607 totalDuration = 0.01f;
1608 positionChanged = true;
1613 // Position Delta ///////////////////////////////////////////////////////
1616 if(mWrapMode && findShortcuts)
1618 // In Wrap Mode, the shortest distance is a little less intuitive...
1619 const RulerDomain rulerDomainX = mRulerX->GetDomain();
1620 const RulerDomain rulerDomainY = mRulerY->GetDomain();
1622 if(mRulerX->IsEnabled())
1624 float dir = VectorInDomain(-mScrollPrePosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
1625 mScrollTargetPosition.x = mScrollPrePosition.x + -dir;
1628 if(mRulerY->IsEnabled())
1630 float dir = VectorInDomain(-mScrollPrePosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
1631 mScrollTargetPosition.y = mScrollPrePosition.y + -dir;
1635 // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting
1636 // a horizonal/vertical wall.delay
1637 AnimateInternalXTo(mScrollTargetPosition.x, positionDuration.x, alpha);
1638 AnimateInternalYTo(mScrollTargetPosition.y, positionDuration.y, alpha);
1640 if( !(mScrollStateFlags & SCROLL_ANIMATION_FLAGS) )
1642 DALI_LOG_SCROLL_STATE("[0x%X] Setting mPropertyPrePosition To[%.2f, %.2f]", this, mScrollTargetPosition.x, mScrollTargetPosition.y );
1643 self.SetProperty(mPropertyPrePosition, mScrollTargetPosition);
1644 mScrollPrePosition = mScrollTargetPosition;
1645 mScrollPostPosition = mScrollTargetPosition;
1646 WrapPosition(mScrollPostPosition);
1649 DALI_LOG_SCROLL_STATE("[0x%X] position-changed, mScrollTargetPosition[%.2f, %.2f], mScrollPrePosition[%.2f, %.2f], mScrollPostPosition[%.2f, %.2f]", this, mScrollTargetPosition.x, mScrollTargetPosition.y, mScrollPrePosition.x, mScrollPrePosition.y, mScrollPostPosition.x, mScrollPostPosition.y );
1650 DALI_LOG_SCROLL_STATE("[0x%X] mPropertyPrePosition[%.2f, %.2f], mPropertyPosition[%.2f, %.2f]", this, self.GetProperty( mPropertyPrePosition ).Get<Vector3>().x, self.GetProperty( mPropertyPrePosition ).Get<Vector3>().y, self.GetProperty( mPropertyPosition ).Get<Vector3>().x, self.GetProperty( mPropertyPosition ).Get<Vector3>().y );
1653 SetScrollUpdateNotification(true);
1655 // Always send a snap event when AnimateTo is called.
1656 Toolkit::ScrollView::SnapEvent snapEvent;
1657 snapEvent.type = snapType;
1658 snapEvent.position = -mScrollTargetPosition;
1659 snapEvent.duration = totalDuration;
1661 DALI_LOG_SCROLL_STATE("[0x%X] mSnapStartedSignal [%.2f, %.2f]", this, snapEvent.position.x, snapEvent.position.y);
1662 mSnapStartedSignal.Emit( snapEvent );
1664 return (mScrollStateFlags & SCROLL_ANIMATION_FLAGS) != 0;
1667 void ScrollView::SetOvershootEnabled(bool enabled)
1669 if(enabled && !mOvershootIndicator)
1671 mOvershootIndicator = ScrollOvershootIndicator::New();
1675 mOvershootIndicator->AttachToScrollable(*this);
1679 mMaxOvershoot = mUserMaxOvershoot;
1680 mOvershootIndicator->DetachFromScrollable(*this);
1682 UpdateMainInternalConstraint();
1685 void ScrollView::AddOverlay(Actor actor)
1687 mInternalActor.Add( actor );
1690 void ScrollView::RemoveOverlay(Actor actor)
1692 mInternalActor.Remove( actor );
1695 void ScrollView::SetOvershootEffectColor( const Vector4& color )
1697 mOvershootEffectColor = color;
1698 if( mOvershootIndicator )
1700 mOvershootIndicator->SetOvershootEffectColor( color );
1704 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
1706 PanGestureDetector panGesture( GetPanGestureDetector() );
1708 // First remove just in case we have some set, then add.
1709 panGesture.RemoveDirection( direction );
1710 panGesture.AddDirection( direction, threshold );
1713 void ScrollView::RemoveScrollingDirection( Radian direction )
1715 PanGestureDetector panGesture( GetPanGestureDetector() );
1716 panGesture.RemoveDirection( direction );
1719 Toolkit::ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
1721 return mSnapStartedSignal;
1724 void ScrollView::FindAndUnbindActor(Actor child)
1729 Vector3 ScrollView::GetPropertyPrePosition() const
1731 Vector3 position = Self().GetProperty<Vector3>(mPropertyPrePosition);
1732 WrapPosition(position);
1736 Vector3 ScrollView::GetPropertyPosition() const
1738 Vector3 position = Self().GetProperty<Vector3>(mPropertyPosition);
1739 WrapPosition(position);
1744 void ScrollView::HandleStoppedAnimation()
1746 SetScrollUpdateNotification(false);
1749 void ScrollView::HandleSnapAnimationFinished()
1751 // Emit Signal that scrolling has completed.
1753 Actor self = Self();
1754 self.SetProperty(mPropertyScrolling, false);
1756 Vector3 deltaPosition(mScrollPrePosition);
1758 UpdateLocalScrollProperties();
1759 WrapPosition(mScrollPrePosition);
1760 DALI_LOG_SCROLL_STATE("[0x%X] Setting mPropertyPrePosition To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
1761 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
1763 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1764 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 3 current[%.2f, %.2f], mScrollTargetPosition[%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y, -mScrollTargetPosition.x, -mScrollTargetPosition.y );
1765 mScrollCompletedSignal.Emit( currentScrollPosition );
1767 mDomainOffset += deltaPosition - mScrollPostPosition;
1768 self.SetProperty(mPropertyDomainOffset, mDomainOffset);
1769 HandleStoppedAnimation();
1772 void ScrollView::SetScrollUpdateNotification( bool enabled )
1774 Actor self = Self();
1775 if( mScrollXUpdateNotification )
1777 // disconnect now to avoid a notification before removed from update thread
1778 mScrollXUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1779 self.RemovePropertyNotification(mScrollXUpdateNotification);
1780 mScrollXUpdateNotification.Reset();
1782 if( enabled && !mScrollUpdatedSignal.Empty())
1784 // Only set up the notification when the application has connected to the updated signal
1785 mScrollXUpdateNotification = self.AddPropertyNotification(mPropertyPosition, 0, StepCondition(mScrollUpdateDistance, 0.0f));
1786 mScrollXUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1788 if( mScrollYUpdateNotification )
1790 // disconnect now to avoid a notification before removed from update thread
1791 mScrollYUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1792 self.RemovePropertyNotification(mScrollYUpdateNotification);
1793 mScrollYUpdateNotification.Reset();
1795 if( enabled && !mScrollUpdatedSignal.Empty())
1797 // Only set up the notification when the application has connected to the updated signal
1798 mScrollYUpdateNotification = self.AddPropertyNotification(mPropertyPosition, 1, StepCondition(mScrollUpdateDistance, 0.0f));
1799 mScrollYUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1803 void ScrollView::OnScrollUpdateNotification(Dali::PropertyNotification& source)
1805 // Guard against destruction during signal emission
1806 Toolkit::ScrollView handle( GetOwner() );
1808 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1809 mScrollUpdatedSignal.Emit( currentScrollPosition );
1812 bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1814 Dali::BaseHandle handle( object );
1816 bool connected( true );
1817 Toolkit::ScrollView view = Toolkit::ScrollView::DownCast( handle );
1819 if( 0 == strcmp( signalName.c_str(), SIGNAL_SNAP_STARTED ) )
1821 view.SnapStartedSignal().Connect( tracker, functor );
1825 // signalName does not match any signal
1832 void ScrollView::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1834 // need to update domain properties for new size
1835 UpdatePropertyDomain(targetSize);
1838 void ScrollView::OnControlSizeSet( const Vector3& size )
1840 // need to update domain properties for new size
1841 if( mDefaultMaxOvershoot )
1843 mUserMaxOvershoot.x = size.x * 0.5f;
1844 mUserMaxOvershoot.y = size.y * 0.5f;
1845 if( !IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) )
1847 mMaxOvershoot = mUserMaxOvershoot;
1850 UpdatePropertyDomain(size);
1851 UpdateMainInternalConstraint();
1852 if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) )
1854 mOvershootIndicator->Reset();
1858 void ScrollView::OnChildAdd(Actor& child)
1866 void ScrollView::OnChildRemove(Actor& child)
1868 // TODO: Actor needs a RemoveConstraint method to take out an individual constraint.
1872 void ScrollView::OnPropertySet( Property::Index index, Property::Value propertyValue )
1874 Actor self = Self();
1875 if( index == mPropertyPrePosition )
1877 DALI_LOG_SCROLL_STATE("[0x%X]: mPropertyPrePosition[%.2f, %.2f]", this, propertyValue.Get<Vector3>().x, propertyValue.Get<Vector3>().y);
1878 propertyValue.Get(mScrollPrePosition);
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 DALI_LOG_SCROLL_STATE("[0x%X]", this);
1905 mTouchDownTimeoutReached = true;
1907 unsigned int currentScrollStateFlags( mScrollStateFlags ); // Cleared in StopAnimation so keep local copy for comparison
1908 if( currentScrollStateFlags & (SCROLL_ANIMATION_FLAGS | SNAP_ANIMATION_FLAGS) )
1910 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling Or snapping flags set, stopping animation", this);
1913 if( currentScrollStateFlags & SCROLL_ANIMATION_FLAGS )
1915 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling flags set, emitting signal", this);
1917 mScrollInterrupted = true;
1918 // reset domain offset as scrolling from original plane.
1919 mDomainOffset = Vector3::ZERO;
1920 Self().SetProperty(mPropertyDomainOffset, Vector3::ZERO);
1922 UpdateLocalScrollProperties();
1923 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1924 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 4 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1925 mScrollCompletedSignal.Emit( currentScrollPosition );
1932 bool ScrollView::OnTouchEvent(const TouchEvent& event)
1936 DALI_LOG_SCROLL_STATE("[0x%X], Not Sensitive, ignoring", this);
1938 // Ignore this touch event, if scrollview is insensitive.
1942 // Ignore events with multiple-touch points
1943 if (event.GetPointCount() != 1)
1945 DALI_LOG_SCROLL_STATE("[0x%X], multiple touch, ignoring", this);
1950 const TouchPoint::State pointState = event.GetPoint(0).state;
1951 if( pointState == TouchPoint::Down )
1953 DALI_LOG_SCROLL_STATE("[0x%X] Down", this);
1955 if(mGestureStackDepth==0)
1957 mTouchDownTime = event.time;
1959 // This allows time for a pan-gesture to start, to avoid breaking snap-animation behavior with fast flicks.
1960 // If touch-down does not become a pan (after timeout interval), then snap-animation can be interrupted.
1961 mTouchDownTimeoutReached = false;
1962 mScrollInterrupted = false;
1963 StartTouchDownTimer();
1966 else if( ( pointState == TouchPoint::Up ) ||
1967 ( ( pointState == TouchPoint::Interrupted ) && ( event.GetPoint(0).hitActor == Self() ) ) )
1969 DALI_LOG_SCROLL_STATE("[0x%X] %s", this, ( ( pointState == TouchPoint::Up ) ? "Up" : "Interrupted" ) );
1971 StopTouchDownTimer();
1973 // if the user touches and releases without enough movement to go
1974 // into a gesture state, then we should snap to nearest point.
1975 // otherwise our scroll could be stopped (interrupted) half way through an animation.
1976 if(mGestureStackDepth==0 && mTouchDownTimeoutReached)
1978 if( ( event.GetPoint(0).state == TouchPoint::Interrupted ) ||
1979 ( ( event.time - mTouchDownTime ) >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET ) )
1981 // Reset the velocity only if down was received a while ago
1982 mLastVelocity = Vector2( 0.0f, 0.0f );
1985 UpdateLocalScrollProperties();
1986 // Only finish the transform if scrolling was interrupted on down or if we are scrolling
1987 if ( mScrollInterrupted || mScrolling )
1989 DALI_LOG_SCROLL_STATE("[0x%X] Calling FinishTransform", this);
1994 mTouchDownTimeoutReached = false;
1995 mScrollInterrupted = false;
2001 bool ScrollView::OnMouseWheelEvent(const MouseWheelEvent& event)
2005 // Ignore this mouse wheel event, if scrollview is insensitive.
2009 Vector3 targetScrollPosition = GetPropertyPosition();
2011 if(mRulerX->IsEnabled() && !mRulerY->IsEnabled())
2013 // If only the ruler in the X axis is enabled, scroll in the X axis.
2014 if(mRulerX->GetType() == Ruler::Free)
2016 // Free panning mode
2017 targetScrollPosition.x += event.z * mMouseWheelScrollDistanceStep.x;
2018 ClampPosition(targetScrollPosition);
2019 ScrollTo(-targetScrollPosition);
2021 else if(!mScrolling)
2023 // Snap mode, only respond to the event when the previous snap animation is finished.
2024 ScrollTo(GetCurrentPage() - event.z);
2029 // If the ruler in the Y axis is enabled, scroll in the Y axis.
2030 if(mRulerY->GetType() == Ruler::Free)
2032 // Free panning mode
2033 targetScrollPosition.y += event.z * mMouseWheelScrollDistanceStep.y;
2034 ClampPosition(targetScrollPosition);
2035 ScrollTo(-targetScrollPosition);
2037 else if(!mScrolling)
2039 // Snap mode, only respond to the event when the previous snap animation is finished.
2040 ScrollTo(GetCurrentPage() - event.z * mRulerX->GetTotalPages());
2047 void ScrollView::ResetScrolling()
2049 Actor self = Self();
2050 self.GetProperty(mPropertyPosition).Get(mScrollPostPosition);
2051 mScrollPrePosition = mScrollPostPosition;
2052 DALI_LOG_SCROLL_STATE("[0x%X] Setting mPropertyPrePosition To[%.2f, %.2f]", this, mScrollPostPosition.x, mScrollPostPosition.y );
2053 self.SetProperty(mPropertyPrePosition, mScrollPostPosition);
2056 void ScrollView::UpdateLocalScrollProperties()
2058 Actor self = Self();
2059 self.GetProperty(mPropertyPrePosition).Get(mScrollPrePosition);
2060 self.GetProperty(mPropertyPosition).Get(mScrollPostPosition);
2063 // private functions
2065 void ScrollView::PreAnimatedScrollSetup()
2067 // mPropertyPrePosition is our unclamped property with wrapping
2068 // mPropertyPosition is our final scroll position after clamping
2070 Actor self = Self();
2072 Vector3 deltaPosition(mScrollPostPosition);
2073 WrapPosition(mScrollPostPosition);
2074 mDomainOffset += deltaPosition - mScrollPostPosition;
2075 Self().SetProperty(mPropertyDomainOffset, mDomainOffset);
2077 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2079 // already performing animation on internal x position
2080 StopAnimation(mInternalXAnimation);
2083 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2085 // already performing animation on internal y position
2086 StopAnimation(mInternalYAnimation);
2089 mScrollStateFlags = 0;
2091 // Update Actor position with this wrapped value.
2094 void ScrollView::FinaliseAnimatedScroll()
2096 // TODO - common animation finishing code in here
2099 void ScrollView::AnimateInternalXTo( float position, float duration, AlphaFunction alpha )
2101 StopAnimation(mInternalXAnimation);
2103 if( duration > Math::MACHINE_EPSILON_10 )
2105 Actor self = Self();
2106 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetProperty(mPropertyPrePosition).Get<Vector3>().x, position );
2107 mInternalXAnimation = Animation::New(duration);
2108 DALI_LOG_SCROLL_STATE("[0x%X], mInternalXAnimation[0x%X]", this, mInternalXAnimation.GetObjectPtr() );
2109 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2110 mInternalXAnimation.AnimateTo( Property(self, mPropertyPrePosition, 0), position, alpha, duration);
2111 mInternalXAnimation.Play();
2113 // erase current state flags
2114 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2115 // add internal animation state flag
2116 mScrollStateFlags |= AnimatingInternalX;
2120 void ScrollView::AnimateInternalYTo( float position, float duration, AlphaFunction alpha )
2122 StopAnimation(mInternalYAnimation);
2124 if( duration > Math::MACHINE_EPSILON_10 )
2126 Actor self = Self();
2127 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetProperty(mPropertyPrePosition).Get<Vector3>().y, position );
2128 mInternalYAnimation = Animation::New(duration);
2129 DALI_LOG_SCROLL_STATE("[0x%X], mInternalYAnimation[0x%X]", this, mInternalYAnimation.GetObjectPtr() );
2130 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2131 mInternalYAnimation.AnimateTo( Property(self, mPropertyPrePosition, 1), position, alpha, TimePeriod(duration));
2132 mInternalYAnimation.Play();
2134 // erase current state flags
2135 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2136 // add internal animation state flag
2137 mScrollStateFlags |= AnimatingInternalY;
2141 void ScrollView::OnScrollAnimationFinished( Animation& source )
2143 // Guard against destruction during signal emission
2144 // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished()
2145 Toolkit::ScrollView handle( GetOwner() );
2147 bool scrollingFinished = false;
2149 // update our local scroll positions
2150 UpdateLocalScrollProperties();
2152 if( source == mInternalXAnimation )
2154 DALI_LOG_SCROLL_STATE("[0x%X] mInternalXAnimation[0x%X], expected[%.2f], actual[%.2f], post[%.2f]", this, mInternalXAnimation.GetObjectPtr(), mScrollTargetPosition.x, Self().GetProperty(mPropertyPrePosition).Get<Vector3>().x, mScrollPostPosition.x );
2156 if( !(mScrollStateFlags & AnimatingInternalY) )
2158 scrollingFinished = true;
2160 mInternalXAnimation.Reset();
2161 // wrap pre scroll x position and set it
2164 const RulerDomain rulerDomain = mRulerX->GetDomain();
2165 mScrollPrePosition.x = -WrapInDomain(-mScrollPrePosition.x, rulerDomain.min, rulerDomain.max);
2166 DALI_LOG_SCROLL_STATE("[0x%X] Setting mPropertyPrePosition To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
2167 handle.SetProperty(mPropertyPrePosition, mScrollPrePosition);
2169 SnapInternalXTo(mScrollPostPosition.x);
2172 if( source == mInternalYAnimation )
2174 DALI_LOG_SCROLL_STATE("[0x%X] mInternalYAnimation[0x%X], expected[%.2f], actual[%.2f], post[%.2f]", this, mInternalYAnimation.GetObjectPtr(), mScrollTargetPosition.y, Self().GetProperty(mPropertyPrePosition).Get<Vector3>().y, mScrollPostPosition.y );
2176 if( !(mScrollStateFlags & AnimatingInternalX) )
2178 scrollingFinished = true;
2180 mInternalYAnimation.Reset();
2183 // wrap pre scroll y position and set it
2184 const RulerDomain rulerDomain = mRulerY->GetDomain();
2185 mScrollPrePosition.y = -WrapInDomain(-mScrollPrePosition.y, rulerDomain.min, rulerDomain.max);
2186 DALI_LOG_SCROLL_STATE("[0x%X] Setting mPropertyPrePosition To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
2187 handle.SetProperty(mPropertyPrePosition, mScrollPrePosition);
2189 SnapInternalYTo(mScrollPostPosition.y);
2192 DALI_LOG_SCROLL_STATE("[0x%X] scrollingFinished[%d] Animation[0x%X]", this, scrollingFinished, source.GetObjectPtr());
2194 if(scrollingFinished)
2196 HandleSnapAnimationFinished();
2200 void ScrollView::OnSnapInternalPositionFinished( Animation& source )
2202 Actor self = Self();
2203 UpdateLocalScrollProperties();
2204 if( source == mInternalXAnimation )
2206 DALI_LOG_SCROLL_STATE("[0x%X] Finished X PostPosition Animation", this );
2208 // clear internal x animation flags
2209 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2210 mInternalXAnimation.Reset();
2211 WrapPosition(mScrollPrePosition);
2213 if( source == mInternalYAnimation )
2215 DALI_LOG_SCROLL_STATE("[0x%X] Finished Y PostPosition Animation", this );
2217 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2218 mInternalYAnimation.Reset();
2219 WrapPosition(mScrollPrePosition);
2223 void ScrollView::SnapInternalXTo(float position)
2225 Actor self = Self();
2227 StopAnimation(mInternalXAnimation);
2229 // erase current state flags
2230 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2232 // if internal x not equal to inputed parameter, animate it
2233 float duration = std::min(fabsf((position - mScrollPrePosition.x) / mMaxOvershoot.x) * mSnapOvershootDuration, mSnapOvershootDuration);
2234 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration );
2235 if( duration > Math::MACHINE_EPSILON_1 )
2237 DALI_LOG_SCROLL_STATE("[0x%X] Starting X Snap Animation to[%.2f]", this, position );
2239 mInternalXAnimation = Animation::New(duration);
2240 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2241 mInternalXAnimation.AnimateTo(Property(self, mPropertyPrePosition, 0), position);
2242 mInternalXAnimation.Play();
2244 // add internal animation state flag
2245 mScrollStateFlags |= SnappingInternalX;
2249 void ScrollView::SnapInternalYTo(float position)
2251 Actor self = Self();
2253 StopAnimation(mInternalYAnimation);
2255 // erase current state flags
2256 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2258 // if internal y not equal to inputed parameter, animate it
2259 float duration = std::min(fabsf((position - mScrollPrePosition.y) / mMaxOvershoot.y) * mSnapOvershootDuration, mSnapOvershootDuration);
2260 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration );
2261 if( duration > Math::MACHINE_EPSILON_1 )
2263 DALI_LOG_SCROLL_STATE("[0x%X] Starting Y Snap Animation to[%.2f]", this, position );
2265 mInternalYAnimation = Animation::New(duration);
2266 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2267 mInternalYAnimation.AnimateTo(Property(self, mPropertyPrePosition, 1), position);
2268 mInternalYAnimation.Play();
2270 // add internal animation state flag
2271 mScrollStateFlags |= SnappingInternalY;
2275 void ScrollView::GestureStarted()
2277 // we handle the first gesture.
2278 // if we're currently doing a gesture and receive another
2279 // we continue and combine the effects of the gesture instead of reseting.
2280 if(mGestureStackDepth++==0)
2282 Actor self = Self();
2283 StopTouchDownTimer();
2285 mPanDelta = Vector3::ZERO;
2286 mLastVelocity = Vector2(0.0f, 0.0f);
2289 mLockAxis = LockPossible;
2292 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2294 StopAnimation(mInternalXAnimation);
2296 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2298 StopAnimation(mInternalYAnimation);
2300 mScrollStateFlags = 0;
2302 if(mScrolling) // are we interrupting a current scroll?
2304 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
2306 // send negative scroll position since scroll internal scroll position works as an offset for actors,
2307 // give applications the position within the domain from the scroll view's anchor position
2308 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 5 [%.2f, %.2f]", this, -mScrollPostPosition.x, -mScrollPostPosition.y);
2309 mScrollCompletedSignal.Emit( -mScrollPostPosition );
2314 void ScrollView::GestureContinuing(const Vector2& panDelta)
2316 mPanDelta.x+= panDelta.x;
2317 mPanDelta.y+= panDelta.y;
2319 // Save the velocity, there is a bug in PanGesture
2320 // Whereby the Gesture::Finished's velocity is either:
2321 // NaN (due to time delta of zero between the last two events)
2322 // or 0 (due to position being the same between the last two events)
2324 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
2325 // appears mostly horizontal or mostly vertical respectively.
2328 mLockAxis = GetLockAxis(mPanDelta.GetVectorXY(), mLockAxis, mAxisAutoLockGradient);
2329 } // end if mAxisAutoLock
2332 // TODO: Upgrade to use a more powerful gesture detector (one that supports multiple touches on pan - so works as pan and flick gesture)
2333 // BUG: Gesture::Finished doesn't always return velocity on release (due to
2334 // timeDelta between last two events being 0 sometimes, or posiiton being the same)
2335 void ScrollView::OnPan( const PanGesture& gesture )
2337 // Guard against destruction during signal emission
2338 // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx()
2339 Actor self( Self() );
2343 DALI_LOG_SCROLL_STATE("[0x%X] Pan Ignored, Insensitive", this);
2345 // If another callback on the same original signal disables sensitivity,
2346 // this callback will still be called, so we must suppress it.
2350 // translate Gesture input to get useful data...
2351 switch(gesture.state)
2353 case Gesture::Started:
2355 DALI_LOG_SCROLL_STATE("[0x%X] Pan Started", this);
2356 mPanStartPosition = gesture.position - gesture.displacement;
2357 UpdateLocalScrollProperties();
2360 self.SetProperty( mPropertyPanning, true );
2361 self.SetProperty( mPropertyScrollStartPagePosition, Vector3(gesture.position.x, gesture.position.y, 0.0f) );
2363 UpdateMainInternalConstraint();
2367 case Gesture::Continuing:
2371 DALI_LOG_SCROLL_STATE("[0x%X] Pan Continuing", this);
2372 GestureContinuing(gesture.screenDisplacement);
2376 // If we do not think we are panning, then we should not do anything here
2382 case Gesture::Finished:
2383 case Gesture::Cancelled:
2387 DALI_LOG_SCROLL_STATE("[0x%X] Pan %s", this, ( ( gesture.state == Gesture::Finished ) ? "Finished" : "Cancelled" ) );
2389 UpdateLocalScrollProperties();
2390 mLastVelocity = gesture.velocity;
2392 self.SetProperty( mPropertyPanning, false );
2394 if( mScrollMainInternalPrePositionConstraint )
2396 self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
2399 if( mOvershootIndicator )
2401 mOvershootIndicator->ClearOvershoot();
2406 // If we do not think we are panning, then we should not do anything here
2412 case Gesture::Possible:
2413 case Gesture::Clear:
2415 // Nothing to do, not needed.
2419 } // end switch(gesture.state)
2421 OnGestureEx(gesture.state);
2424 void ScrollView::OnGestureEx(Gesture::State state)
2426 // call necessary signals for application developer
2428 if(state == Gesture::Started)
2430 Vector3 currentScrollPosition = GetCurrentScrollPosition();
2431 Self().SetProperty(mPropertyScrolling, true);
2433 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 2 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2434 mScrollStartedSignal.Emit( currentScrollPosition );
2436 else if( (state == Gesture::Finished) ||
2437 (state == Gesture::Cancelled) ) // Finished/default
2439 // when all the gestures have finished, we finish the transform.
2440 // so if a user decides to pan (1 gesture), and then pan+zoom (2 gestures)
2441 // then stop panning (back to 1 gesture), and then stop zooming (0 gestures).
2442 // this is the point we end, and perform necessary snapping.
2443 mGestureStackDepth--;
2444 if(mGestureStackDepth==0)
2446 // no flick if we have not exceeded min flick distance
2447 if( (fabsf(mPanDelta.x) < mMinFlickDistance.x)
2448 && (fabsf(mPanDelta.y) < mMinFlickDistance.y) )
2450 // reset flick velocity
2451 mLastVelocity = Vector2::ZERO;
2457 DALI_LOG_SCROLL_STATE("[0x%X] mGestureStackDepth[%d]", this, mGestureStackDepth);
2462 void ScrollView::FinishTransform()
2464 // at this stage internal x and x scroll position should have followed prescroll position exactly
2465 Actor self = Self();
2467 PreAnimatedScrollSetup();
2469 // convert pixels/millisecond to pixels per second
2470 bool animating = SnapWithVelocity(mLastVelocity * 1000.0f);
2474 // if not animating, then this pan has completed right now.
2475 SetScrollUpdateNotification(false);
2477 Self().SetProperty(mPropertyScrolling, false);
2479 if( fabs(mScrollPrePosition.x - mScrollTargetPosition.x) > Math::MACHINE_EPSILON_10 )
2481 SnapInternalXTo(mScrollTargetPosition.x);
2483 if( fabs(mScrollPrePosition.y - mScrollTargetPosition.y) > Math::MACHINE_EPSILON_10 )
2485 SnapInternalYTo(mScrollTargetPosition.y);
2487 Vector3 currentScrollPosition = GetCurrentScrollPosition();
2488 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 6 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2489 mScrollCompletedSignal.Emit( currentScrollPosition );
2493 Vector3 ScrollView::GetOvershoot(Vector3& position) const
2495 Vector3 size = Self().GetCurrentSize();
2498 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2499 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2501 if(mRulerX->IsEnabled() && rulerDomainX.enabled)
2503 const float left = rulerDomainX.min - position.x;
2504 const float right = size.width - rulerDomainX.max - position.x;
2511 overshoot.x = right;
2515 if(mRulerY->IsEnabled() && rulerDomainY.enabled)
2517 const float top = rulerDomainY.min - position.y;
2518 const float bottom = size.height - rulerDomainY.max - position.y;
2525 overshoot.y = bottom;
2532 bool ScrollView::OnAccessibilityPan(PanGesture gesture)
2534 // Keep track of whether this is an AccessibilityPan
2535 mInAccessibilityPan = true;
2537 mInAccessibilityPan = false;
2542 void ScrollView::ClampPosition(Vector3& position) const
2544 ClampState3D clamped;
2545 ClampPosition(position, clamped);
2548 void ScrollView::ClampPosition(Vector3& position, ClampState3D &clamped) const
2550 Vector3 size = Self().GetCurrentSize();
2552 position.x = -mRulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system.
2553 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.
2555 clamped.z = NotClamped;
2558 void ScrollView::WrapPosition(Vector3& position) const
2562 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2563 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2565 if(mRulerX->IsEnabled())
2567 position.x = -WrapInDomain(-position.x, rulerDomainX.min, rulerDomainX.max);
2570 if(mRulerY->IsEnabled())
2572 position.y = -WrapInDomain(-position.y, rulerDomainY.min, rulerDomainY.max);
2577 void ScrollView::UpdateMainInternalConstraint()
2579 // TODO: Only update the constraints which have changed, rather than remove all and add all again.
2580 // Requires a dali-core ApplyConstraintAt, or a ReplaceConstraint. The former is probably more flexible.
2581 Actor self = Self();
2582 PanGestureDetector detector( GetPanGestureDetector() );
2584 if(mScrollMainInternalPositionConstraint)
2586 self.RemoveConstraint(mScrollMainInternalPositionConstraint);
2587 self.RemoveConstraint(mScrollMainInternalDeltaConstraint);
2588 self.RemoveConstraint(mScrollMainInternalFinalConstraint);
2589 self.RemoveConstraint(mScrollMainInternalRelativeConstraint);
2591 if( mScrollMainInternalPrePositionConstraint )
2593 self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
2596 // TODO: It's probably better to use a local displacement value as this will give a displacement when scrolling just commences
2597 // but we need to make sure than the gesture system gives displacement since last frame (60Hz), not displacement since last touch event (90Hz).
2599 // 1. First calculate the pre-position (this is the scroll position if no clamping has taken place)
2600 Vector2 initialPanMask = Vector2(mRulerX->IsEnabled() ? 1.0f : 0.0f, mRulerY->IsEnabled() ? 1.0f : 0.0f);
2602 if( mLockAxis == LockVertical )
2604 initialPanMask.y = 0.0f;
2606 else if( mLockAxis == LockHorizontal )
2608 initialPanMask.x = 0.0f;
2610 Constraint constraint;
2614 constraint = Constraint::New<Vector3>( mPropertyPrePosition,
2615 Source( detector, PanGestureDetector::Property::LOCAL_POSITION ),
2616 Source( self, Actor::Property::SIZE ),
2617 InternalPrePositionConstraint( mPanStartPosition, initialPanMask, mAxisAutoLock, mAxisAutoLockGradient, mLockAxis, mMaxOvershoot, mRulerX->GetDomain(), mRulerY->GetDomain() ) );
2618 mScrollMainInternalPrePositionConstraint = self.ApplyConstraint( constraint );
2621 // 2. Second calculate the clamped position (actual position)
2622 constraint = Constraint::New<Vector3>( mPropertyPosition,
2623 LocalSource( mPropertyPrePosition ),
2624 LocalSource( mPropertyPositionMin ),
2625 LocalSource( mPropertyPositionMax ),
2626 Source( self, Actor::Property::SIZE ),
2627 InternalPositionConstraint( mRulerX->GetDomain(),
2628 mRulerY->GetDomain(), mWrapMode ) );
2629 mScrollMainInternalPositionConstraint = self.ApplyConstraint( constraint );
2631 constraint = Constraint::New<Vector3>( mPropertyPositionDelta,
2632 LocalSource( mPropertyPosition ),
2633 LocalSource( mPropertyDomainOffset ),
2634 InternalPositionDeltaConstraint );
2635 mScrollMainInternalDeltaConstraint = self.ApplyConstraint( constraint );
2637 constraint = Constraint::New<Vector3>( mPropertyFinal,
2638 LocalSource( mPropertyPosition ),
2639 LocalSource( mPropertyOvershootX ),
2640 LocalSource( mPropertyOvershootY ),
2641 InternalFinalConstraint( FinalDefaultAlphaFunction,
2642 FinalDefaultAlphaFunction ) );
2643 mScrollMainInternalFinalConstraint = self.ApplyConstraint( constraint );
2645 constraint = Constraint::New<Vector3>( mPropertyRelativePosition,
2646 LocalSource( mPropertyPosition ),
2647 LocalSource( mPropertyPositionMin ),
2648 LocalSource( mPropertyPositionMax ),
2649 LocalSource( Actor::Property::SIZE ),
2650 InternalRelativePositionConstraint );
2651 mScrollMainInternalRelativeConstraint = self.ApplyConstraint( constraint );
2653 // When panning we want to make sure overshoot values are affected by pre position and post position
2654 SetOvershootConstraintsEnabled(!mWrapMode);
2657 void ScrollView::SetOvershootConstraintsEnabled(bool enabled)
2659 Actor self( Self() );
2660 // remove and reset, it may now be in wrong order with the main internal constraints
2661 if( mScrollMainInternalOvershootXConstraint )
2663 self.RemoveConstraint(mScrollMainInternalOvershootXConstraint);
2664 mScrollMainInternalOvershootXConstraint.Reset();
2665 self.RemoveConstraint(mScrollMainInternalOvershootYConstraint);
2666 mScrollMainInternalOvershootYConstraint.Reset();
2670 Constraint constraint = Constraint::New<float>( mPropertyOvershootX,
2671 LocalSource( mPropertyPrePosition ),
2672 LocalSource( mPropertyPosition ),
2673 LocalSource( mPropertyCanScrollHorizontal ),
2674 OvershootXConstraint(mMaxOvershoot.x) );
2675 mScrollMainInternalOvershootXConstraint = self.ApplyConstraint( constraint );
2677 constraint = Constraint::New<float>( mPropertyOvershootY,
2678 LocalSource( mPropertyPrePosition ),
2679 LocalSource( mPropertyPosition ),
2680 LocalSource( mPropertyCanScrollVertical ),
2681 OvershootYConstraint(mMaxOvershoot.y) );
2682 mScrollMainInternalOvershootYConstraint = self.ApplyConstraint( constraint );
2686 self.SetProperty(mPropertyOvershootX, 0.0f);
2687 self.SetProperty(mPropertyOvershootY, 0.0f);
2691 void ScrollView::SetInternalConstraints()
2693 // Internal constraints (applied to target ScrollBase Actor itself) /////////
2694 UpdateMainInternalConstraint();
2696 // User definable constraints to apply to all child actors //////////////////
2697 Actor self = Self();
2699 // Apply some default constraints to ScrollView & its bound actors
2700 // Movement + Wrap function
2702 Constraint constraint;
2704 // MoveActor (scrolling)
2705 constraint = Constraint::New<Vector3>( Actor::Property::POSITION,
2706 Source( self, mPropertyPosition ),
2707 MoveActorConstraint );
2708 constraint.SetRemoveAction(Constraint::Discard);
2709 ApplyConstraintToBoundActors(constraint);
2711 // WrapActor (wrap functionality)
2712 constraint = Constraint::New<Vector3>( Actor::Property::POSITION,
2713 LocalSource( Actor::Property::SCALE ),
2714 LocalSource( Actor::Property::ANCHOR_POINT ),
2715 LocalSource( Actor::Property::SIZE ),
2716 Source( self, mPropertyPositionMin ),
2717 Source( self, mPropertyPositionMax ),
2718 Source( self, mPropertyWrap ),
2719 WrapActorConstraint );
2720 constraint.SetRemoveAction(Constraint::Discard);
2721 ApplyConstraintToBoundActors(constraint);
2724 } // namespace Internal
2726 } // namespace Toolkit