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/public-api/events/mouse-wheel-event.h>
21 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view-constraints.h>
22 #include <dali-toolkit/public-api/controls/scrollable/scroll-component-impl.h>
23 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.h>
24 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-effect-impl.h>
25 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
26 #include <dali/integration-api/debug.h>
28 // TODO: Change to two class system:
29 // 1. DraggableActor (is an actor which can be dragged anywhere/scaled/rotated, can be set to range using the ruler)
30 // 2. ScrollView (contains a draggable actor that can a) be dragged in the negative X, and Y domain, b) has a hitArea for touches)
32 // TODO: Asymetrical scaling
33 // TODO: external components (page and status overlays).
35 // TODO: upgrade Vector2/3 to support returning Unit vectors, normals, & cross product (dot product is already provided)
42 const int DEFAULT_REFRESH_INTERVAL_MILLISECONDS = 50; ///< Refresh rate TODO: Animation should have an update signal (and see item-view-impl)
43 const float FLICK_SPEED_THRESHOLD = 500.0f; ///< Flick threshold in pixels/ms
44 const float FREE_FLICK_SPEED_THRESHOLD = 200.0f; ///< Free-Flick threshold in pixels/ms
45 const float AUTOLOCK_AXIS_MINIMUM_DISTANCE2 = 100.0f; ///< Auto-lock axis after minimum distance squared.
46 const float FLICK_ORTHO_ANGLE_RANGE = 60.0f; ///< degrees. (if >45, then supports diagonal flicking)
47 const unsigned int MAXIMUM_NUMBER_OF_VALUES = 5; ///< Number of values to use for weighted pan calculation.
48 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.
49 const unsigned long MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET( 150u );
50 const float DEFAULT_OVERSHOOT_ANIMATION_DURATION = 0.35f; // time in seconds
51 const Vector2 OVERSCROLL_CLAMP(1.0f, 1.0f); // maximum overscroll allowed in pixels when overshoot indicator is being used
52 const float TOUCH_DOWN_TIMER_INTERVAL = 100.0f;
54 // predefined effect values
55 const Vector3 ANGLE_CAROUSEL_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
56 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
57 const Vector2 ANGLE_CUSTOM_CUBE_SWING(-Math::PI * 0.45f, -Math::PI * 0.45f); ///< outer cube pages swing 90 degrees as they pan offscreen
58 const Vector2 ANGLE_SPIRAL_SWING_IN(Math::PI * 0.5f, Math::PI * 0.5f);
59 const Vector2 ANGLE_SPIRAL_SWING_OUT(Math::PI * 0.35f, Math::PI * 0.35f);
60 const Vector2 ANGLE_OUTER_CUBE_SWING(Math::PI * 0.5f, Math::PI * 0.5f); ///< outer cube pages swing 90 degrees as they pan offscreen
62 // Helpers ////////////////////////////////////////////////////////////////////////////////////////
64 // TODO: GetAngle for Vector2 can be moved.
65 // GetAngle for Vector3 needs to be measured against a normal/plane.
68 * @param[in] vector The 3D vector to be measured
69 * @return angle in radians from 0 to 2PI
71 float GetAngle(const Vector3& vector)
73 return atan2(vector.y, vector.x) + Math::PI;
77 * @param[in] vector The 2D vector to be measured
78 * @return angle in radians from 0 to 2PI
80 float GetAngle(const Vector2& vector)
82 return atan2(vector.y, vector.x) + Math::PI;
86 * Find the vector (distance) from (a) to (b)
87 * in domain (start) to (end)
88 * (\ / start) (\ / end)
91 * @note assumes both (a) and (b) are already with the domain
94 * @param[in] a the current point
95 * @param[in] b the target point
96 * @param[in] start the start of the domain
97 * @param[in] end the end of the domain
98 * @param[in] bias whether to only take the right direction or the left direction,
99 * or the shortest direction.
100 * @return the shortest direction and distance
102 float VectorInDomain(float a, float b, float start, float end, Dali::Toolkit::DirectionBias bias)
104 if(bias == Dali::Toolkit::DirectionBiasNone)
106 return ShortestDistanceInDomain( a, b, start, end );
109 float size = end-start;
115 if(bias == Dali::Toolkit::DirectionBiasRight) // going right, take the vector.
121 float aRight = a+size;
128 if(bias == Dali::Toolkit::DirectionBiasLeft) // going left, take the vector.
134 float aLeft = a-size;
141 * Returns the position of the anchor within actor
143 * @param actor The Actor
144 * @param anchor The Anchor point of interest.
145 * @return The position of the Anchor
147 Vector3 GetPositionOfAnchor(Actor &actor, const Vector3 &anchor)
149 Vector3 childPosition = actor.GetCurrentPosition();
150 Vector3 childAnchor = - actor.GetCurrentAnchorPoint() + anchor;
151 Vector3 childSize = actor.GetCurrentSize();
153 return childPosition + childAnchor * childSize;
156 // AlphaFunctions /////////////////////////////////////////////////////////////////////////////////
158 float FinalDefaultAlphaFunction(float offset)
160 return offset * 0.5f;
164 * ConstantDecelerationAlphaFunction
165 * Newtoninan distance for constant deceleration
166 * v = 1 - t, s = t - 1/2 t^2
167 * when t = 0, s = 0.0 (min distance)
168 * when t = 1, s = 0.5 (max distance)
169 * progress = s / (max-min) = 2t - t^2
171 * @param[in] offset The input progress
172 * @return The output progress
174 float ConstantDecelerationAlphaFunction(float progress)
176 return progress * 2.0f - progress * progress;
179 // Internal Constraints ///////////////////////////////////////////////////////////////////////////
182 * Internal Relative position Constraint
183 * Generates the relative position value of the scroll view
184 * based on the absolute position, and it's relation to the
185 * scroll domain. This is a value from 0.0f to 1.0f in each
186 * scroll position axis.
188 Vector3 InternalRelativePositionConstraint(const Vector3& current,
189 const PropertyInput& scrollPositionProperty,
190 const PropertyInput& scrollMinProperty,
191 const PropertyInput& scrollMaxProperty,
192 const PropertyInput& scrollSizeProperty)
194 Vector3 position = -scrollPositionProperty.GetVector3();
195 const Vector3& min = scrollMinProperty.GetVector3();
196 const Vector3& max = scrollMaxProperty.GetVector3();
197 const Vector3& size = scrollSizeProperty.GetVector3();
199 position.x = WrapInDomain(position.x, min.x, max.x);
200 position.y = WrapInDomain(position.y, min.y, max.y);
202 Vector3 relativePosition;
203 Vector3 domainSize = (max - min) - size;
205 relativePosition.x = domainSize.x > Math::MACHINE_EPSILON_1 ? fabsf((position.x - min.x) / domainSize.x) : 0.0f;
206 relativePosition.y = domainSize.y > Math::MACHINE_EPSILON_1 ? fabsf((position.y - min.y) / domainSize.y) : 0.0f;
208 return relativePosition;
211 } // unnamed namespace
226 * Returns whether to lock scrolling to a particular axis
228 * @param[in] panDelta Distance panned since gesture started
229 * @param[in] currentLockAxis The current lock axis value
230 * @param[in] lockGradient How quickly to lock to a particular axis
232 * @return The new axis lock state
234 ScrollView::LockAxis GetLockAxis(const Vector2& panDelta, ScrollView::LockAxis currentLockAxis, float lockGradient)
236 if(panDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 &&
237 currentLockAxis == ScrollView::LockPossible)
239 float dx = fabsf(panDelta.x);
240 float dy = fabsf(panDelta.y);
241 if(dx * lockGradient >= dy)
243 // 0.36:1 gradient to the horizontal (deviate < 20 degrees)
244 currentLockAxis = ScrollView::LockVertical;
246 else if(dy * lockGradient > dx)
248 // 0.36:1 gradient to the vertical (deviate < 20 degrees)
249 currentLockAxis = ScrollView::LockHorizontal;
253 currentLockAxis = ScrollView::LockNone;
256 return currentLockAxis;
260 * Internal Pre-Position Property Constraint.
262 * Generates position property based on current position + gesture displacement.
263 * Or generates position property based on positionX/Y.
264 * Note: This is the position prior to any clamping at scroll boundaries.
265 * TODO: Scale & Rotation Transforms.
267 struct InternalPrePositionConstraint
269 InternalPrePositionConstraint(const Vector2& initialPanMask,
271 float axisAutoLockGradient,
272 ScrollView::LockAxis initialLockAxis,
273 const Vector2& maxOvershoot,
274 const RulerDomain& domainX, const RulerDomain& domainY)
275 : mInitialPanMask(initialPanMask),
276 mDomainMin( -domainX.min, -domainY.min ),
277 mDomainMax( -domainX.max, -domainY.max ),
278 mMaxOvershoot(maxOvershoot),
279 mAxisAutoLockGradient(axisAutoLockGradient),
280 mLockAxis(initialLockAxis),
281 mAxisAutoLock(axisAutoLock),
283 mClampX( domainX.enabled ),
284 mClampY( domainY.enabled )
288 Vector3 operator()(const Vector3& current,
289 const PropertyInput& gesturePositionProperty,
290 const PropertyInput& gestureDisplacementProperty,
291 const PropertyInput& sizeProperty)
293 Vector3 scrollPostPosition = current;
294 Vector2 panPosition = gesturePositionProperty.GetVector2();
298 mLocalStart = gesturePositionProperty.GetVector2() - gestureDisplacementProperty.GetVector2();
299 mPrePosition = current;
300 mCurrentPanMask = mInitialPanMask;
304 // Calculate Deltas...
305 Vector2 currentPosition = gesturePositionProperty.GetVector2();
306 Vector2 panDelta( currentPosition - mLocalStart );
308 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
309 // appears mostly horizontal or mostly vertical respectively...
312 mLockAxis = GetLockAxis(panDelta, mLockAxis, mAxisAutoLockGradient);
313 if( mLockAxis == ScrollView::LockVertical )
315 mCurrentPanMask.y = 0.0f;
317 else if( mLockAxis == ScrollView::LockHorizontal )
319 mCurrentPanMask.x = 0.0f;
323 // Restrict deltas based on ruler enable/disable and axis-lock state...
324 panDelta *= mCurrentPanMask;
326 // Perform Position transform based on input deltas...
327 scrollPostPosition = mPrePosition;
328 scrollPostPosition.GetVectorXY() += panDelta;
330 // if no wrapping then clamp preposition to maximum overshoot amount
331 const Vector3& size = sizeProperty.GetVector3();
334 float newXPosition = Clamp(scrollPostPosition.x, (mDomainMax.x + size.x) - mMaxOvershoot.x, mDomainMin.x + mMaxOvershoot.x );
335 if( (newXPosition < scrollPostPosition.x - Math::MACHINE_EPSILON_1)
336 || (newXPosition > scrollPostPosition.x + Math::MACHINE_EPSILON_1) )
338 mPrePosition.x = newXPosition;
339 mLocalStart.x = panPosition.x;
341 scrollPostPosition.x = newXPosition;
345 float newYPosition = Clamp(scrollPostPosition.y, (mDomainMax.y + size.y) - mMaxOvershoot.y, mDomainMin.y + mMaxOvershoot.y );
346 if( (newYPosition < scrollPostPosition.y - Math::MACHINE_EPSILON_1)
347 || (newYPosition > scrollPostPosition.y + Math::MACHINE_EPSILON_1) )
349 mPrePosition.y = newYPosition;
350 mLocalStart.y = panPosition.y;
352 scrollPostPosition.y = newYPosition;
355 return scrollPostPosition;
358 Vector3 mPrePosition;
360 Vector2 mInitialPanMask; ///< Initial pan mask (based on ruler settings)
361 Vector2 mCurrentPanMask; ///< Current pan mask that can be altered by axis lock mode.
364 Vector2 mMaxOvershoot;
366 float mAxisAutoLockGradient; ///< Set by ScrollView
367 ScrollView::LockAxis mLockAxis;
369 bool mAxisAutoLock:1; ///< Set by ScrollView
376 * Internal Position Property Constraint.
378 * Generates position property based on pre-position
379 * Note: This is the position after clamping.
380 * (uses result of InternalPrePositionConstraint)
382 struct InternalPositionConstraint
384 InternalPositionConstraint(const RulerDomain& domainX, const RulerDomain& domainY, bool wrap)
385 : mDomainMin( -domainX.min, -domainY.min ),
386 mDomainMax( -domainX.max, -domainY.max ),
387 mClampX( domainX.enabled ),
388 mClampY( domainY.enabled ),
393 Vector3 operator()(const Vector3& current,
394 const PropertyInput& scrollPositionProperty,
395 const PropertyInput& scrollMinProperty,
396 const PropertyInput& scrollMaxProperty,
397 const PropertyInput& scrollSizeProperty)
399 Vector3 position = scrollPositionProperty.GetVector3();
400 const Vector2& size = scrollSizeProperty.GetVector3().GetVectorXY();
401 const Vector3& min = scrollMinProperty.GetVector3();
402 const Vector3& max = scrollMaxProperty.GetVector3();
406 position.x = -WrapInDomain(-position.x, min.x, max.x);
407 position.y = -WrapInDomain(-position.y, min.y, max.y);
411 // clamp post position to domain
412 position.x = mClampX ? Clamp(position.x, mDomainMax.x + size.x, mDomainMin.x ) : position.x;
413 position.y = mClampY ? Clamp(position.y, mDomainMax.y + size.y, mDomainMin.y ) : position.y;
428 * This constraint updates the X overshoot property using the difference
429 * mPropertyPrePosition.x and mPropertyPosition.x, returning a relative value between 0.0f and 1.0f
431 struct OvershootXConstraint
433 OvershootXConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
435 float operator()(const float& current,
436 const PropertyInput& scrollPrePositionProperty,
437 const PropertyInput& scrollPostPositionProperty,
438 const PropertyInput& canScrollProperty)
440 if( canScrollProperty.GetBoolean() )
442 const Vector3& scrollPrePosition = scrollPrePositionProperty.GetVector3();
443 const Vector3& scrollPostPosition = scrollPostPositionProperty.GetVector3();
444 float newOvershoot = scrollPrePosition.x - scrollPostPosition.x;
445 return (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
454 * This constraint updates the Y overshoot property using the difference
455 * mPropertyPrePosition.y and mPropertyPosition.y, returning a relative value between 0.0f and 1.0f
457 struct OvershootYConstraint
459 OvershootYConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
461 float operator()(const float& current,
462 const PropertyInput& scrollPrePositionProperty,
463 const PropertyInput& scrollPostPositionProperty,
464 const PropertyInput& canScrollProperty)
466 if( canScrollProperty.GetBoolean() )
468 const Vector3& scrollPrePosition = scrollPrePositionProperty.GetVector3();
469 const Vector3& scrollPostPosition = scrollPostPositionProperty.GetVector3();
470 float newOvershoot = scrollPrePosition.y - scrollPostPosition.y;
471 return (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
480 * When panning, this constraint updates the X property, otherwise
481 * it has no effect on the X property.
483 float InternalXConstraint(const float& current,
484 const PropertyInput& scrollPosition)
486 return scrollPosition.GetVector3().x;
490 * When panning, this constraint updates the Y property, otherwise
491 * it has no effect on the Y property.
493 float InternalYConstraint(const float& current,
494 const PropertyInput& scrollPosition)
496 return scrollPosition.GetVector3().y;
500 * Internal Position-Delta Property Constraint.
502 * Generates position-delta property based on scroll-position + scroll-offset properties.
504 Vector3 InternalPositionDeltaConstraint(const Vector3& current,
505 const PropertyInput& scrollPositionProperty,
506 const PropertyInput& scrollOffsetProperty)
508 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
509 const Vector3& scrollOffset = scrollOffsetProperty.GetVector3();
511 return scrollPosition + scrollOffset;
515 * Internal Final Position Constraint
516 * The position of content is:
517 * of scroll-position + f(scroll-overshoot)
518 * where f(...) function defines how overshoot
519 * should affect final-position.
521 struct InternalFinalConstraint
523 InternalFinalConstraint(AlphaFunction functionX,
524 AlphaFunction functionY)
525 : mFunctionX(functionX),
526 mFunctionY(functionY)
530 Vector3 operator()(const Vector3& current,
531 const PropertyInput& scrollPositionProperty,
532 const PropertyInput& scrollOvershootXProperty,
533 const PropertyInput& scrollOvershootYProperty)
535 const float& overshootx = scrollOvershootXProperty.GetFloat();
536 const float& overshooty = scrollOvershootYProperty.GetFloat();
537 Vector3 offset( mFunctionX(overshootx),
538 mFunctionY(overshooty),
541 return scrollPositionProperty.GetVector3() - offset;
544 AlphaFunction mFunctionX;
545 AlphaFunction mFunctionY;
551 return Toolkit::ScrollView::New();
554 TypeRegistration typeRegistration( typeid(Toolkit::ScrollView), typeid(Toolkit::Scrollable), Create );
556 SignalConnectorType signalConnector1( typeRegistration, Toolkit::ScrollView::SIGNAL_SNAP_STARTED, &ScrollView::DoConnectSignal );
561 ///////////////////////////////////////////////////////////////////////////////////////////////////
563 ///////////////////////////////////////////////////////////////////////////////////////////////////
565 Dali::Toolkit::ScrollView ScrollView::New()
567 // Create the implementation
568 ScrollViewPtr scrollView(new ScrollView());
570 // Pass ownership to CustomActor via derived handle
571 Dali::Toolkit::ScrollView handle(*scrollView);
573 // Second-phase init of the implementation
574 // This can only be done after the CustomActor connection has been made...
575 scrollView->Initialize();
580 ScrollView::ScrollView()
583 mGestureStackDepth(0),
584 mRotationDelta(0.0f),
585 mScrollStateFlags(0),
586 mScrollPreRotation(0.0f),
587 mScrollPostRotation(0.0f),
588 mMinTouchesForPanning(1),
589 mMaxTouchesForPanning(1),
590 mLockAxis(LockPossible),
591 mRefreshIntervalMilliseconds(DEFAULT_REFRESH_INTERVAL_MILLISECONDS),
592 mOvershootDelay(1.0f),
593 mMaxOvershoot(Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT, Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT),
594 mUserMaxOvershoot(Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT, Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT),
595 mSnapOvershootDuration(Toolkit::ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION),
596 mSnapOvershootAlphaFunction(AlphaFunctions::EaseOut),
597 mSnapDuration(Toolkit::ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION),
598 mSnapAlphaFunction(AlphaFunctions::EaseOut),
599 mFlickDuration(Toolkit::ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION),
600 mFlickAlphaFunction(AlphaFunctions::EaseOut),
601 mAxisAutoLockGradient(Toolkit::ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT),
602 mFrictionCoefficient(Toolkit::ScrollView::DEFAULT_FRICTION_COEFFICIENT),
603 mFlickSpeedCoefficient(Toolkit::ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT),
604 mMaxFlickSpeed(Toolkit::ScrollView::DEFAULT_MAX_FLICK_SPEED),
605 mInAccessibilityPan(false),
608 mScrollInterrupted(false),
611 mTouchDownTimeoutReached(false),
612 mActorAutoSnapEnabled(false),
613 mAutoResizeContainerEnabled(false),
615 mAxisAutoLock(false),
617 mDefaultMaxOvershoot(true)
619 SetRequiresMouseWheelEvents(true);
622 void ScrollView::OnInitialize()
626 // Internal Actor, used to hide actors from enumerations.
627 // Also actors added to Internal actor appear as overlays e.g. ScrollBar components.
628 mInternalActor = Actor::New();
629 mInternalActor.SetDrawMode(DrawMode::OVERLAY);
630 self.Add(mInternalActor);
631 mInternalActor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
632 mInternalActor.SetParentOrigin(ParentOrigin::CENTER);
633 mInternalActor.SetAnchorPoint(AnchorPoint::CENTER);
637 // Register Scroll Properties.
638 RegisterProperties();
640 mScrollPostPosition = mScrollPrePosition = Vector3::ZERO;
641 mScrollPostScale = mScrollPreScale = Vector3::ONE;
642 mScrollPostRotation = mScrollPreRotation = 0.0f;
644 mMouseWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
648 mGestureStackDepth = 0;
650 EnableGestureDetection( Gesture::Type( Gesture::Pan ) );
652 // For pan, default to only 1 touch required, ignoring touches outside this range.
653 SetTouchesRequiredForPanning(1, 1, false);
655 // By default we'll allow the user to freely drag the scroll view,
656 // while disabling the other rulers.
657 RulerPtr ruler = new DefaultRuler();
658 RulerPtr rulerDisabled = new DefaultRuler();
659 rulerDisabled->Disable();
662 mRulerScaleX = rulerDisabled;
663 mRulerScaleY = rulerDisabled;
664 mRulerRotation = rulerDisabled;
666 EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator);
668 Vector3 size = GetControlSize();
669 UpdatePropertyDomain(size);
670 SetInternalConstraints();
673 void ScrollView::OnControlStageConnection()
677 SetScrollSensitive( false );
678 SetScrollSensitive( true );
680 if(IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator))
682 // try and make sure property notifications are set
683 EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator);
687 void ScrollView::OnControlStageDisconnection()
692 ScrollView::~ScrollView()
696 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
698 return mSnapAlphaFunction;
701 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
703 mSnapAlphaFunction = alpha;
706 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
708 return mFlickAlphaFunction;
711 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
713 mFlickAlphaFunction = alpha;
716 float ScrollView::GetScrollSnapDuration() const
718 return mSnapDuration;
721 void ScrollView::SetScrollSnapDuration(float time)
723 mSnapDuration = time;
726 float ScrollView::GetScrollFlickDuration() const
728 return mFlickDuration;
731 void ScrollView::SetScrollFlickDuration(float time)
733 mFlickDuration = time;
736 void ScrollView::ApplyEffect(Toolkit::ScrollViewEffect effect)
738 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
740 // Assertion check to ensure effect doesn't already exist in this scrollview
741 bool effectAlreadyExistsInScrollView(false);
742 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
746 effectAlreadyExistsInScrollView = true;
751 DALI_ASSERT_ALWAYS(!effectAlreadyExistsInScrollView);
753 // add effect to effects list
754 mEffects.push_back(effect);
756 // invoke Attachment request to ScrollView first
757 GetImpl(effect).Attach(self);
760 Toolkit::ScrollViewEffect ScrollView::ApplyEffect(Toolkit::ScrollView::PageEffect effect)
762 Toolkit::ScrollViewEffect scrollEffect;
765 case Toolkit::ScrollView::PageEffectNone:
769 case Toolkit::ScrollView::PageEffectOuterCube:
771 Toolkit::ScrollViewCustomEffect customEffect;
772 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
773 Vector2 pageSize = Stage::GetCurrent().GetSize();
774 // 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
775 // 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)
776 // the page moving off screen will slowly build a spacing from 0.0f to 20.0f
777 // the spacing from each page is added together for the final spacing between the two pages.
778 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));
779 customEffect.SetSwingAngleOut(ANGLE_CUSTOM_CUBE_SWING.x, Vector3(0.0f, -1.0f, 0.0f));
780 customEffect.SetSwingAnchor(AnchorPoint::CENTER, AnchorPoint::CENTER_LEFT);
781 customEffect.SetOpacityThreshold(0.7f);
784 case Toolkit::ScrollView::PageEffectDepth:
786 Toolkit::ScrollViewCustomEffect customEffect;
787 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
790 case Toolkit::ScrollView::PageEffectInnerCube:
792 Toolkit::ScrollViewCustomEffect customEffect;
793 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
794 customEffect.SetPageSpacing(Vector2(30.0f, 30.0f));
795 customEffect.SetAngledOriginPageRotation(ANGLE_CUBE_PAGE_ROTATE);
796 customEffect.SetSwingAngle(ANGLE_CUBE_PAGE_ROTATE.x, Vector3(0,-1,0));
797 customEffect.SetOpacityThreshold(0.5f);
800 case Toolkit::ScrollView::PageEffectCarousel:
802 Toolkit::ScrollViewCustomEffect customEffect;
803 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
804 customEffect.SetPageTranslation(Vector3(0,0,0), Vector3(-30, 0, 0));
805 customEffect.SetPageSpacing(Vector2(60.0f, 60.0f));
806 customEffect.SetAngledOriginPageRotation(-ANGLE_CUBE_PAGE_ROTATE);
807 customEffect.SetOpacityThreshold(0.2f, 0.6f);
810 case Toolkit::ScrollView::PageEffectSpiral:
812 Toolkit::ScrollViewCustomEffect customEffect;
813 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
815 Vector2 pageSize = Stage::GetCurrent().GetSize();
816 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));
817 //customEffect.SetSwingAngleAlphaFunctionOut(AlphaFunctions::EaseOut);
818 customEffect.SetSwingAnchor(AnchorPoint::CENTER_RIGHT);
819 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);
820 //customEffect.SetPageTranslateAlphaFunctionOut(AlphaFunctions::EaseOut);
821 customEffect.SetOpacityThreshold(0.75f, 0.6f);
822 customEffect.SetOpacityAlphaFunctionIn(AlphaFunctions::EaseInOut);
827 DALI_ASSERT_DEBUG(0 && "unknown scroll view effect");
830 RemoveConstraintsFromChildren();
833 ApplyEffect(scrollEffect);
838 void ScrollView::RemoveEffect(Toolkit::ScrollViewEffect effect)
840 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
842 // remove effect from effects list
843 bool effectExistedInScrollView(false);
844 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
848 mEffects.erase(iter);
849 effectExistedInScrollView = true;
854 // Assertion check to ensure effect existed.
855 DALI_ASSERT_ALWAYS(effectExistedInScrollView);
857 // invoke Detachment request to ScrollView last
858 GetImpl(effect).Detach(self);
861 void ScrollView::RemoveAllEffects()
863 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
865 for (ScrollViewEffectIter effectIter = mEffects.begin(); effectIter != mEffects.end(); ++effectIter)
867 Toolkit::ScrollViewEffect effect = *effectIter;
869 // invoke Detachment request to ScrollView last
870 GetImpl(effect).Detach(self);
876 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
878 ApplyConstraintToBoundActors(constraint);
881 void ScrollView::RemoveConstraintsFromChildren()
883 RemoveConstraintsFromBoundActors();
886 const RulerPtr ScrollView::GetRulerX() const
891 const RulerPtr ScrollView::GetRulerY() const
896 void ScrollView::SetRulerX(RulerPtr ruler)
900 Vector3 size = GetControlSize();
901 UpdatePropertyDomain(size);
902 UpdateMainInternalConstraint();
905 void ScrollView::SetRulerY(RulerPtr ruler)
909 Vector3 size = GetControlSize();
910 UpdatePropertyDomain(size);
911 UpdateMainInternalConstraint();
914 void ScrollView::UpdatePropertyDomain(const Vector3& size)
917 Vector3 min = mMinScroll;
918 Vector3 max = mMaxScroll;
919 bool scrollPositionChanged = false;
920 bool domainChanged = false;
922 bool canScrollVertical = false;
923 bool canScrollHorizontal = false;
924 UpdateLocalScrollProperties();
925 if(mRulerX->IsEnabled())
927 const Toolkit::RulerDomain& rulerDomain = mRulerX->GetDomain();
928 if( fabsf(min.x - rulerDomain.min) > Math::MACHINE_EPSILON_10000
929 || fabsf(max.x - rulerDomain.max) > Math::MACHINE_EPSILON_10000 )
931 domainChanged = true;
932 min.x = rulerDomain.min;
933 max.x = rulerDomain.max;
935 // make sure new scroll value is within new domain
936 if( mScrollPrePosition.x < min.x
937 || mScrollPrePosition.x > max.x )
939 scrollPositionChanged = true;
940 mScrollPrePosition.x = Clamp(mScrollPrePosition.x, -(max.x - size.x), -min.x);
943 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.x) > Math::MACHINE_EPSILON_10000 )
945 canScrollHorizontal = true;
949 if(mRulerY->IsEnabled())
951 const Toolkit::RulerDomain& rulerDomain = mRulerY->GetDomain();
952 if( fabsf(min.y - rulerDomain.min) > Math::MACHINE_EPSILON_10000
953 || fabsf(max.y - rulerDomain.max) > Math::MACHINE_EPSILON_10000 )
955 domainChanged = true;
956 min.y = rulerDomain.min;
957 max.y = rulerDomain.max;
959 // make sure new scroll value is within new domain
960 if( mScrollPrePosition.y < min.y
961 || mScrollPrePosition.y > max.y )
963 scrollPositionChanged = true;
964 mScrollPrePosition.y = Clamp(mScrollPrePosition.y, -(max.y - size.y), -min.y);
967 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.y) > Math::MACHINE_EPSILON_10000 )
969 canScrollVertical = true;
972 // avoid setting properties if possible, otherwise this will cause an entire update as well as triggering constraints using each property we update
973 if( self.GetProperty<bool>(mPropertyCanScrollVertical) != canScrollVertical )
975 self.SetProperty(mPropertyCanScrollVertical, canScrollVertical);
977 if( self.GetProperty<bool>(mPropertyCanScrollHorizontal) != canScrollHorizontal )
979 self.SetProperty(mPropertyCanScrollHorizontal, canScrollHorizontal);
981 if( scrollPositionChanged )
983 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
989 self.SetProperty(mPropertyPositionMin, min );
990 self.SetProperty(mPropertyPositionMax, max );
994 void ScrollView::SetRulerScaleX(RulerPtr ruler)
996 mRulerScaleX = ruler;
997 UpdateMainInternalConstraint();
1000 void ScrollView::SetRulerScaleY(RulerPtr ruler)
1002 mRulerScaleY = ruler;
1003 UpdateMainInternalConstraint();
1006 void ScrollView::SetRulerRotation(RulerPtr ruler)
1008 mRulerRotation = ruler;
1009 UpdateMainInternalConstraint();
1012 void ScrollView::SetScrollSensitive(bool sensitive)
1014 Actor self = Self();
1015 PanGestureDetector panGesture( GetPanGestureDetector() );
1017 if((!mSensitive) && (sensitive))
1019 mSensitive = sensitive;
1020 panGesture.Attach(self);
1022 else if((mSensitive) && (!sensitive))
1024 // while the scroll view is panning, the state needs to be reset.
1025 bool isPanning = self.GetProperty<bool>( mPropertyPanning );
1028 PanGesture cancelGesture( Gesture::Cancelled );
1029 OnPan( cancelGesture );
1032 panGesture.Detach(self);
1033 mSensitive = sensitive;
1035 mGestureStackDepth = 0;
1039 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
1041 mMaxOvershoot.x = overshootX;
1042 mMaxOvershoot.y = overshootY;
1043 mUserMaxOvershoot = mMaxOvershoot;
1044 mDefaultMaxOvershoot = false;
1045 UpdateMainInternalConstraint();
1048 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
1050 mSnapOvershootAlphaFunction = alpha;
1053 void ScrollView::SetSnapOvershootDuration(float duration)
1055 mSnapOvershootDuration = duration;
1058 void ScrollView::SetTouchesRequiredForPanning(unsigned int minTouches, unsigned int maxTouches, bool endOutside)
1060 PanGestureDetector panGesture( GetPanGestureDetector() );
1062 mMinTouchesForPanning = minTouches;
1063 mMaxTouchesForPanning = maxTouches;
1067 panGesture.SetMinimumTouchesRequired(minTouches);
1068 panGesture.SetMaximumTouchesRequired(maxTouches);
1072 panGesture.SetMinimumTouchesRequired(1);
1073 panGesture.SetMaximumTouchesRequired(UINT_MAX);
1077 void ScrollView::SetActorAutoSnap(bool enable)
1079 mActorAutoSnapEnabled = enable;
1082 void ScrollView::SetAutoResize(bool enable)
1084 mAutoResizeContainerEnabled = enable;
1085 // TODO: This needs a lot of issues to be addressed before working.
1088 bool ScrollView::GetWrapMode() const
1093 void ScrollView::SetWrapMode(bool enable)
1096 Self().SetProperty(mPropertyWrap, enable);
1099 int ScrollView::GetRefreshInterval() const
1101 return mRefreshIntervalMilliseconds;
1104 void ScrollView::SetRefreshInterval(int milliseconds)
1106 mRefreshIntervalMilliseconds = milliseconds;
1109 bool ScrollView::GetAxisAutoLock() const
1111 return mAxisAutoLock;
1114 void ScrollView::SetAxisAutoLock(bool enable)
1116 mAxisAutoLock = enable;
1117 UpdateMainInternalConstraint();
1120 float ScrollView::GetAxisAutoLockGradient() const
1122 return mAxisAutoLockGradient;
1125 void ScrollView::SetAxisAutoLockGradient(float gradient)
1127 DALI_ASSERT_DEBUG( gradient >= 0.0f && gradient <= 1.0f );
1128 mAxisAutoLockGradient = gradient;
1129 UpdateMainInternalConstraint();
1132 float ScrollView::GetFrictionCoefficient() const
1134 return mFrictionCoefficient;
1137 void ScrollView::SetFrictionCoefficient(float friction)
1139 DALI_ASSERT_DEBUG( friction > 0.0f );
1140 mFrictionCoefficient = friction;
1143 float ScrollView::GetFlickSpeedCoefficient() const
1145 return mFlickSpeedCoefficient;
1148 void ScrollView::SetFlickSpeedCoefficient(float speed)
1150 mFlickSpeedCoefficient = speed;
1153 float ScrollView::GetMaxFlickSpeed() const
1155 return mMaxFlickSpeed;
1158 void ScrollView::SetMaxFlickSpeed(float speed)
1160 mMaxFlickSpeed = speed;
1163 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
1165 mMouseWheelScrollDistanceStep = step;
1168 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
1170 return mMouseWheelScrollDistanceStep;
1173 unsigned int ScrollView::GetCurrentPage() const
1175 // in case animation is currently taking place.
1176 Vector3 position = GetPropertyPosition();
1178 Actor self = Self();
1179 unsigned int page = 0;
1180 unsigned int pagesPerVolume = 1;
1181 unsigned int volume = 0;
1183 // if rulerX is enabled, then get page count (columns)
1184 page = mRulerX->GetPageFromPosition(-position.x, mWrapMode);
1185 volume = mRulerY->GetPageFromPosition(-position.y, mWrapMode);
1186 pagesPerVolume = mRulerX->GetTotalPages();
1188 return volume * pagesPerVolume + page;
1191 Vector3 ScrollView::GetCurrentScrollPosition() const
1193 return -GetPropertyPosition();
1196 void ScrollView::SetScrollPosition(const Vector3& position)
1198 mScrollPrePosition = position;
1201 Vector3 ScrollView::GetCurrentScrollScale() const
1203 // in case animation is currently taking place.
1204 return GetPropertyScale();
1207 Vector3 ScrollView::GetDomainSize() const
1209 Vector3 size = Self().GetCurrentSize();
1211 const RulerDomain& xDomain = GetRulerX()->GetDomain();
1212 const RulerDomain& yDomain = GetRulerY()->GetDomain();
1214 Vector3 domainSize = Vector3( xDomain.max - xDomain.min, yDomain.max - yDomain.min, 0.0f ) - size;
1218 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation,
1219 DirectionBias horizontalBias, DirectionBias verticalBias)
1221 TransformTo(position, scale, rotation, mSnapDuration, horizontalBias, verticalBias);
1224 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation, float duration,
1225 DirectionBias horizontalBias, DirectionBias verticalBias)
1227 // Guard against destruction during signal emission
1228 // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo()
1229 Toolkit::ScrollView handle( GetOwner() );
1231 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1232 Self().SetProperty( mPropertyScrollStartPagePosition, currentScrollPosition );
1234 if(mScrolling) // are we interrupting a current scroll?
1236 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
1238 mScrollCompletedSignalV2.Emit( currentScrollPosition );
1241 Self().SetProperty(mPropertyScrolling, true);
1243 mScrollStartedSignalV2.Emit( currentScrollPosition );
1244 bool animating = AnimateTo(-position,
1245 Vector3::ONE * duration,
1247 Vector3::ONE * duration,
1258 // if not animating, then this pan has completed right now.
1259 Self().SetProperty(mPropertyScrolling, false);
1261 mScrollCompletedSignalV2.Emit( currentScrollPosition );
1265 void ScrollView::ScrollTo(const Vector3& position)
1267 ScrollTo(position, mSnapDuration );
1270 void ScrollView::ScrollTo(const Vector3& position, float duration)
1272 ScrollTo(position, duration, DirectionBiasNone, DirectionBiasNone);
1275 void ScrollView::ScrollTo(const Vector3& position, float duration,
1276 DirectionBias horizontalBias, DirectionBias verticalBias)
1278 TransformTo(position, mScrollPostScale, mScrollPostRotation, duration, horizontalBias, verticalBias);
1281 void ScrollView::ScrollTo(unsigned int page)
1283 ScrollTo(page, mSnapDuration);
1286 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
1289 unsigned int volume;
1290 unsigned int libraries;
1292 // The position to scroll to is continuous and linear
1293 // unless a domain has been enabled on the X axis.
1294 // or if WrapMode has been enabled.
1295 bool carryX = mRulerX->GetDomain().enabled | mWrapMode;
1296 bool carryY = mRulerY->GetDomain().enabled | mWrapMode;
1298 position.x = mRulerX->GetPositionFromPage(page, volume, carryX);
1299 position.y = mRulerY->GetPositionFromPage(volume, libraries, carryY);
1301 ScrollTo(position, duration, bias, bias);
1304 void ScrollView::ScrollTo(Actor &actor)
1306 ScrollTo(actor, mSnapDuration);
1309 void ScrollView::ScrollTo(Actor &actor, float duration)
1311 DALI_ASSERT_ALWAYS(actor.GetParent() == Self());
1313 Actor self = Self();
1314 Vector3 size = self.GetCurrentSize();
1315 Vector3 position = actor.GetCurrentPosition();
1316 position -= GetPropertyPrePosition();
1318 ScrollTo(Vector3(position.x - size.width * 0.5f, position.y - size.height * 0.5f, 0.0f), duration);
1321 Actor ScrollView::FindClosestActor()
1323 Actor self = Self();
1324 Vector3 size = self.GetCurrentSize();
1326 return FindClosestActorToPosition(Vector3(size.width * 0.5f,size.height * 0.5f,0.0f));
1329 Actor ScrollView::FindClosestActorToPosition(const Vector3& position, FindDirection dirX, FindDirection dirY, FindDirection dirZ)
1332 float closestDistance2 = 0.0f;
1333 Vector3 actualPosition = position;
1335 unsigned int numChildren = Self().GetChildCount();
1337 for(unsigned int i = 0; i < numChildren; ++i)
1339 Actor child = Self().GetChildAt(i);
1341 if(mInternalActor == child) // ignore internal actor.
1346 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1348 Vector3 delta = childPosition - actualPosition;
1350 // X-axis checking (only find Actors to the [dirX] of actualPosition)
1351 if(dirX > All) // != All,None
1353 FindDirection deltaH = delta.x > 0 ? Right : Left;
1360 // Y-axis checking (only find Actors to the [dirY] of actualPosition)
1361 if(dirY > All) // != All,None
1363 FindDirection deltaV = delta.y > 0 ? Down : Up;
1370 // Z-axis checking (only find Actors to the [dirZ] of actualPosition)
1371 if(dirZ > All) // != All,None
1373 FindDirection deltaV = delta.y > 0 ? In : Out;
1380 // compare child to closest child in terms of distance.
1381 float distance2 = 0.0f;
1383 // distance2 = the Square of the relevant dimensions of delta
1386 distance2 += delta.x * delta.x;
1391 distance2 += delta.y * delta.y;
1396 distance2 += delta.z * delta.z;
1399 if(closestChild) // Next time.
1401 if(distance2 < closestDistance2)
1403 closestChild = child;
1404 closestDistance2 = distance2;
1409 closestChild = child;
1410 closestDistance2 = distance2;
1414 return closestChild;
1417 bool ScrollView::ScrollToSnapPoint()
1419 Vector2 stationaryVelocity = Vector2(0.0f, 0.0f);
1420 return SnapWithVelocity( stationaryVelocity );
1423 void ScrollView::ScaleTo(const Vector3& scale)
1425 ScaleTo(scale, mSnapDuration);
1428 void ScrollView::ScaleTo(const Vector3& scale, float duration)
1430 TransformTo(mScrollPostPosition, scale, mScrollPostRotation, duration);
1434 // TODO: In situations where axes are different (X snap, Y free)
1435 // Each axis should really have their own independent animation (time and equation)
1436 // Consider, X axis snapping to nearest grid point (EaseOut over fixed time)
1437 // Consider, Y axis simulating physics to arrive at a point (Physics equation over variable time)
1438 // Currently, the axes have been split however, they both use the same EaseOut equation.
1439 bool ScrollView::SnapWithVelocity(Vector2 velocity)
1441 // Animator takes over now, touches are assumed not to interfere.
1442 // And if touches do interfere, then we'll stop animation, update PrePosition
1443 // to current mScroll's properties, and then resume.
1444 // Note: For Flicking this may work a bit different...
1446 float angle = atan2(velocity.y, velocity.x);
1447 float speed2 = velocity.LengthSquared();
1448 AlphaFunction alphaFunction = mSnapAlphaFunction;
1449 Vector3 positionDuration = Vector3::ONE * mSnapDuration;
1450 Vector3 scaleDuration = Vector3::ONE * mSnapDuration;
1451 float rotationDuration = mSnapDuration;
1454 FindDirection horizontal = None;
1455 FindDirection vertical = None;
1457 // orthoAngleRange = Angle tolerance within the Exact N,E,S,W direction
1458 // that will be accepted as a general N,E,S,W flick direction.
1460 const float orthoAngleRange = FLICK_ORTHO_ANGLE_RANGE * M_PI / 180.0f;
1461 const float flickSpeedThreshold2 = FLICK_SPEED_THRESHOLD*FLICK_SPEED_THRESHOLD;
1463 Vector3 positionSnap = mScrollPrePosition;
1465 // Flick logic X Axis
1467 if(mRulerX->IsEnabled() && mLockAxis != LockHorizontal)
1471 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1472 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1474 if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East
1476 biasX = 0.0f, horizontal = Left;
1478 // This guards against an error where no movement occurs, due to the flick finishing
1479 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1480 positionSnap.x += 1.0f;
1482 else if((angle >= M_PI-orthoAngleRange) || (angle < -M_PI+orthoAngleRange)) // Swiping West
1484 biasX = 1.0f, horizontal = Right;
1486 // This guards against an error where no movement occurs, due to the flick finishing
1487 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1488 positionSnap.x -= 1.0f;
1493 // Flick logic Y Axis
1495 if(mRulerY->IsEnabled() && mLockAxis != LockVertical)
1499 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1500 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1502 if((angle >= M_PI_2-orthoAngleRange) && (angle < M_PI_2+orthoAngleRange)) // Swiping South
1504 biasY = 0.0f, vertical = Up;
1506 else if((angle >= -M_PI_2-orthoAngleRange) && (angle < -M_PI_2+orthoAngleRange)) // Swiping North
1508 biasY = 1.0f, vertical = Down;
1513 // isFlick: Whether this gesture is a flick or not.
1514 bool isFlick = (horizontal != All || vertical != All);
1515 // isFreeFlick: Whether this gesture is a flick under free panning criteria.
1516 bool isFreeFlick = velocity.LengthSquared() > (FREE_FLICK_SPEED_THRESHOLD*FREE_FLICK_SPEED_THRESHOLD);
1518 if(isFlick || isFreeFlick)
1520 positionDuration = Vector3::ONE * mFlickDuration;
1521 alphaFunction = mFlickAlphaFunction;
1524 // Calculate next positionSnap ////////////////////////////////////////////////////////////
1526 if(mActorAutoSnapEnabled)
1528 Vector3 size = Self().GetCurrentSize();
1530 Actor child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f), horizontal, vertical );
1532 if(!child && isFlick )
1534 // If we conducted a direction limited search and found no actor, then just snap to the closest actor.
1535 child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f) );
1540 Vector3 position = Self().GetProperty<Vector3>(mPropertyPosition);
1542 // Get center-point of the Actor.
1543 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1545 if(mRulerX->IsEnabled())
1547 positionSnap.x = position.x - childPosition.x + size.width * 0.5f;
1549 if(mRulerY->IsEnabled())
1551 positionSnap.y = position.y - childPosition.y + size.height * 0.5f;
1556 Vector3 startPosition = positionSnap;
1557 positionSnap.x = -mRulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system.
1558 positionSnap.y = -mRulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
1560 Vector3 clampDelta(Vector3::ZERO);
1561 ClampPosition(positionSnap);
1563 if( (mRulerX->GetType() == Ruler::Free || mRulerY->GetType() == Ruler::Free)
1564 && isFreeFlick && !mActorAutoSnapEnabled)
1566 // Calculate target position based on velocity of flick.
1568 // a = Deceleration (Set to diagonal stage length * friction coefficient)
1569 // u = Initial Velocity (Flick velocity)
1570 // v = 0 (Final Velocity)
1571 // t = Time (Velocity / Deceleration)
1572 Vector2 stageSize = Stage::GetCurrent().GetSize();
1573 float stageLength = Vector3(stageSize.x, stageSize.y, 0.0f).Length();
1574 float a = (stageLength * mFrictionCoefficient);
1575 Vector3 u = Vector3(velocity.x, velocity.y, 0.0f) * mFlickSpeedCoefficient;
1576 float speed = u.Length();
1579 // TODO: Change this to a decay function. (faster you flick, the slower it should be)
1580 speed = std::min(speed, stageLength * mMaxFlickSpeed );
1582 alphaFunction = ConstantDecelerationAlphaFunction;
1584 float t = speed / a;
1586 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1588 positionSnap.x += t*u.x*0.5f;
1591 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1593 positionSnap.y += t*u.y*0.5f;
1596 clampDelta = positionSnap;
1597 ClampPosition(positionSnap);
1598 if((positionSnap - startPosition).LengthSquared() > Math::MACHINE_EPSILON_0)
1600 clampDelta -= positionSnap;
1601 clampDelta.x = clampDelta.x > 0.0f ? std::min(clampDelta.x, mMaxOvershoot.x) : std::max(clampDelta.x, -mMaxOvershoot.x);
1602 clampDelta.y = clampDelta.y > 0.0f ? std::min(clampDelta.y, mMaxOvershoot.y) : std::max(clampDelta.y, -mMaxOvershoot.y);
1606 clampDelta = Vector3::ZERO;
1609 // If Axis is Free and has velocity, then calculate time taken
1610 // to reach target based on velocity in axis.
1611 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1613 float deltaX = fabsf(startPosition.x - positionSnap.x);
1615 if(fabsf(u.x) > Math::MACHINE_EPSILON_1)
1617 positionDuration.x = fabsf(deltaX / u.x);
1621 positionDuration.x = 0;
1625 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1627 float deltaY = fabsf(startPosition.y - positionSnap.y);
1629 if(fabsf(u.y) > Math::MACHINE_EPSILON_1)
1631 positionDuration.y = fabsf(deltaY / u.y);
1635 positionDuration.y = 0;
1639 positionSnap += clampDelta;
1641 // Scale Snap ///////////////////////////////////////////////////////////////
1642 Vector3 scaleSnap = mScrollPostScale;
1644 scaleSnap.x = mRulerScaleX->Snap(scaleSnap.x);
1645 scaleSnap.y = mRulerScaleY->Snap(scaleSnap.y);
1647 ClampScale(scaleSnap);
1649 // Rotation Snap ////////////////////////////////////////////////////////////
1650 float rotationSnap = mScrollPostRotation;
1651 // TODO: implement rotation snap
1653 bool animating = AnimateTo(positionSnap, positionDuration,
1654 scaleSnap, scaleDuration,
1655 rotationSnap, rotationDuration,
1656 alphaFunction, false,
1657 DirectionBiasNone, DirectionBiasNone,
1658 isFlick || isFreeFlick ? Flick : Snap);
1663 void ScrollView::StopAnimation(void)
1665 // Clear Snap animation if exists.
1666 StopAnimation(mSnapAnimation);
1667 StopAnimation(mInternalXAnimation);
1668 StopAnimation(mInternalYAnimation);
1669 mScrollStateFlags = 0;
1670 // remove scroll animation flags
1671 HandleStoppedAnimation();
1674 void ScrollView::StopAnimation(Animation& animation)
1683 bool ScrollView::AnimateTo(const Vector3& position, const Vector3& positionDuration,
1684 const Vector3& scale, const Vector3& scaleDuration,
1685 float rotation, float rotationDuration,
1686 AlphaFunction alpha, bool findShortcuts,
1687 DirectionBias horizontalBias, DirectionBias verticalBias,
1690 // Here we perform an animation on a number of properties (depending on which have changed)
1691 // The animation is applied to all ScrollBases
1692 Actor self = Self();
1693 mScrollTargetPosition = position;
1694 float totalDuration = 0.0f;
1696 bool positionChanged = (mScrollTargetPosition != mScrollPostPosition);
1697 bool scaleChanged = (scale != mScrollPostScale);
1698 bool rotationChanged = fabsf(rotation - mScrollPostRotation) > Math::MACHINE_EPSILON_0;
1702 totalDuration = std::max(totalDuration, positionDuration.x);
1703 totalDuration = std::max(totalDuration, positionDuration.y);
1707 // try to animate for a frame, on some occasions update will be changing scroll value while event side thinks it hasnt changed
1708 totalDuration = 0.01f;
1709 positionChanged = true;
1714 totalDuration = std::max(totalDuration, scaleDuration.x);
1715 totalDuration = std::max(totalDuration, scaleDuration.y);
1720 totalDuration = std::max(totalDuration, rotationDuration);
1724 // Position Delta ///////////////////////////////////////////////////////
1727 if(mWrapMode && findShortcuts)
1729 // In Wrap Mode, the shortest distance is a little less intuitive...
1730 const RulerDomain rulerDomainX = mRulerX->GetDomain();
1731 const RulerDomain rulerDomainY = mRulerY->GetDomain();
1733 if(mRulerX->IsEnabled())
1735 float dir = VectorInDomain(-mScrollPostPosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
1736 mScrollTargetPosition.x = mScrollPostPosition.x + -dir;
1739 if(mRulerY->IsEnabled())
1741 float dir = VectorInDomain(-mScrollPostPosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
1742 mScrollTargetPosition.y = mScrollPostPosition.y + -dir;
1746 // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting
1747 // a horizonal/vertical wall.delay
1748 AnimateInternalXTo(mScrollTargetPosition.x, positionDuration.x, alpha);
1749 AnimateInternalYTo(mScrollTargetPosition.y, positionDuration.y, alpha);
1751 if( !(mScrollStateFlags & SCROLL_ANIMATION_FLAGS) )
1753 self.SetProperty(mPropertyPrePosition, mScrollTargetPosition);
1754 mScrollPrePosition = mScrollTargetPosition;
1758 // Scale Delta ///////////////////////////////////////////////////////
1761 if(totalDuration > Math::MACHINE_EPSILON_1)
1763 mSnapAnimation = Animation::New(totalDuration);
1764 mSnapAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
1765 // TODO: for non-uniform scaling to different bounds e.g. scaling a square to a 4:3 aspect ratio screen with a velocity
1766 // the height will hit first, and then the width, so that would require two different animation times just like position.
1767 mSnapAnimation.AnimateTo( Property(self, mPropertyScale), scale, alpha, TimePeriod(0.0f, scaleDuration.x));
1769 mSnapAnimation.AnimateTo( Property(self, mPropertyTime), totalDuration, AlphaFunctions::Linear );
1770 mSnapAnimation.Play();
1774 self.SetProperty(mPropertyScale, scale);
1776 mScrollPreScale = mScrollPostScale = scale;
1779 StartRefreshTimer();
1781 // Always send a snap event when AnimateTo is called.
1782 Toolkit::ScrollView::SnapEvent snapEvent;
1783 snapEvent.type = snapType;
1784 snapEvent.position = -mScrollTargetPosition;
1785 snapEvent.scale = scale;
1786 snapEvent.rotation = rotation;
1787 snapEvent.duration = totalDuration;
1789 mSnapStartedSignalV2.Emit( snapEvent );
1791 return (mScrollStateFlags & SCROLL_ANIMATION_FLAGS) != 0;
1794 void ScrollView::SetOvershootEnabled(bool enabled)
1796 if(enabled && !mOvershootIndicator)
1798 mOvershootIndicator = ScrollOvershootIndicator::New();
1802 mMaxOvershoot = OVERSCROLL_CLAMP;
1803 mOvershootIndicator->AttachToScrollable(*this);
1807 mMaxOvershoot = mUserMaxOvershoot;
1808 mOvershootIndicator->DetachFromScrollable(*this);
1810 UpdateMainInternalConstraint();
1813 void ScrollView::AddOverlay(Actor actor)
1815 mInternalActor.Add( actor );
1818 void ScrollView::RemoveOverlay(Actor actor)
1820 mInternalActor.Remove( actor );
1823 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
1825 PanGestureDetector panGesture( GetPanGestureDetector() );
1827 // First remove just in case we have some set, then add.
1828 panGesture.RemoveDirection( direction );
1829 panGesture.AddDirection( direction, threshold );
1832 void ScrollView::RemoveScrollingDirection( Radian direction )
1834 PanGestureDetector panGesture( GetPanGestureDetector() );
1835 panGesture.RemoveDirection( direction );
1838 Toolkit::ScrollView::SnapStartedSignalV2& ScrollView::SnapStartedSignal()
1840 return mSnapStartedSignalV2;
1843 void ScrollView::FindAndUnbindActor(Actor child)
1848 Vector3 ScrollView::GetPropertyPrePosition() const
1850 Vector3 position = Self().GetProperty<Vector3>(mPropertyPrePosition);
1851 WrapPosition(position);
1855 Vector3 ScrollView::GetPropertyPosition() const
1857 Vector3 position = Self().GetProperty<Vector3>(mPropertyPosition);
1858 WrapPosition(position);
1863 Vector3 ScrollView::GetPropertyScale() const
1865 return Self().GetProperty<Vector3>(mPropertyScale);
1868 void ScrollView::HandleStoppedAnimation()
1870 // Animation has stopped, so stop sending the scroll-update signal.
1871 CancelRefreshTimer();
1874 void ScrollView::HandleSnapAnimationFinished()
1876 // Emit Signal that scrolling has completed.
1878 Actor self = Self();
1879 self.SetProperty(mPropertyScrolling, false);
1881 Vector3 deltaPosition(mScrollPrePosition);
1883 UpdateLocalScrollProperties();
1884 WrapPosition(mScrollPrePosition);
1885 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
1887 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1888 mScrollCompletedSignalV2.Emit( currentScrollPosition );
1890 mDomainOffset += deltaPosition - mScrollPostPosition;
1891 self.SetProperty(mPropertyDomainOffset, mDomainOffset);
1892 HandleStoppedAnimation();
1895 bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1897 Dali::BaseHandle handle( object );
1899 bool connected( true );
1900 Toolkit::ScrollView view = Toolkit::ScrollView::DownCast( handle );
1902 if( Toolkit::ScrollView::SIGNAL_SNAP_STARTED == signalName )
1904 view.SnapStartedSignal().Connect( tracker, functor );
1908 // signalName does not match any signal
1915 void ScrollView::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1917 // need to update domain properties for new size
1918 UpdatePropertyDomain(targetSize);
1921 void ScrollView::OnControlSizeSet( const Vector3& size )
1923 // need to update domain properties for new size
1924 if( mDefaultMaxOvershoot )
1926 mUserMaxOvershoot.x = size.x * 0.5f;
1927 mUserMaxOvershoot.y = size.y * 0.5f;
1928 if( !IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) )
1930 mMaxOvershoot = mUserMaxOvershoot;
1933 UpdatePropertyDomain(size);
1934 UpdateMainInternalConstraint();
1935 if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) )
1937 mOvershootIndicator->Reset();
1941 void ScrollView::OnChildAdd(Actor& child)
1949 void ScrollView::OnChildRemove(Actor& child)
1951 // TODO: Actor needs a RemoveConstraint method to take out an individual constraint.
1955 void ScrollView::OnPropertySet( Property::Index index, Property::Value propertyValue )
1957 Actor self = Self();
1958 if( index == mPropertyX )
1960 self.GetProperty(mPropertyPrePosition).Get(mScrollPrePosition);
1961 propertyValue.Get(mScrollPrePosition.x);
1962 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
1964 else if( index == mPropertyY )
1966 self.GetProperty(mPropertyPrePosition).Get(mScrollPrePosition);
1967 propertyValue.Get(mScrollPrePosition.y);
1968 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
1970 else if( index == mPropertyPrePosition )
1972 propertyValue.Get(mScrollPrePosition);
1976 void ScrollView::StartTouchDownTimer()
1978 if ( !mTouchDownTimer )
1980 mTouchDownTimer = Timer::New( TOUCH_DOWN_TIMER_INTERVAL );
1981 mTouchDownTimer.TickSignal().Connect( this, &ScrollView::OnTouchDownTimeout );
1984 mTouchDownTimer.Start();
1987 void ScrollView::StopTouchDownTimer()
1989 if ( mTouchDownTimer )
1991 mTouchDownTimer.Stop();
1995 bool ScrollView::OnTouchDownTimeout()
1997 mTouchDownTimeoutReached = true;
1999 if( mScrollStateFlags & (SCROLL_ANIMATION_FLAGS | SNAP_ANIMATION_FLAGS) )
2002 if( mScrollStateFlags & SCROLL_ANIMATION_FLAGS )
2004 mScrollInterrupted = true;
2005 // reset domain offset as scrolling from original plane.
2006 mDomainOffset = Vector3::ZERO;
2007 Self().SetProperty(mPropertyDomainOffset, Vector3::ZERO);
2009 UpdateLocalScrollProperties();
2010 Vector3 currentScrollPosition = GetCurrentScrollPosition();
2011 mScrollCompletedSignalV2.Emit( currentScrollPosition );
2018 bool ScrollView::OnTouchEvent(const TouchEvent& event)
2022 // Ignore this touch event, if scrollview is insensitive.
2026 // Ignore events with multiple-touch points
2027 if (event.GetPointCount() != 1)
2032 if( event.GetPoint(0).state == TouchPoint::Down )
2034 if(mGestureStackDepth==0)
2036 mTouchDownTime = event.time;
2038 // This allows time for a pan-gesture to start, to avoid breaking snap-animation behavior with fast flicks.
2039 // If touch-down does not become a pan (after timeout interval), then snap-animation can be interrupted.
2040 StartTouchDownTimer();
2043 else if( event.GetPoint(0).state == TouchPoint::Up )
2045 StopTouchDownTimer();
2047 // if the user touches and releases without enough movement to go
2048 // into a gesture state, then we should snap to nearest point.
2049 // otherwise our scroll could be stopped (interrupted) half way through an animation.
2050 if(mGestureStackDepth==0 && mTouchDownTimeoutReached)
2052 unsigned timeDelta( event.time - mTouchDownTime );
2053 if ( timeDelta >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET )
2055 // Reset the velocity only if down was received a while ago
2056 mLastVelocity = Vector2( 0.0f, 0.0f );
2059 UpdateLocalScrollProperties();
2060 // Only finish the transform if scrolling was interrupted on down or if we are scrolling
2061 if ( mScrollInterrupted || mScrolling )
2066 mTouchDownTimeoutReached = false;
2067 mScrollInterrupted = false;
2073 bool ScrollView::OnMouseWheelEvent(const MouseWheelEvent& event)
2077 // Ignore this mouse wheel event, if scrollview is insensitive.
2081 Vector3 targetScrollPosition = GetPropertyPosition();
2083 if(mRulerX->IsEnabled() && !mRulerY->IsEnabled())
2085 // If only the ruler in the X axis is enabled, scroll in the X axis.
2086 if(mRulerX->GetType() == Ruler::Free)
2088 // Free panning mode
2089 targetScrollPosition.x += event.z * mMouseWheelScrollDistanceStep.x;
2090 ClampPosition(targetScrollPosition);
2091 ScrollTo(-targetScrollPosition);
2093 else if(!mScrolling)
2095 // Snap mode, only respond to the event when the previous snap animation is finished.
2096 ScrollTo(GetCurrentPage() - event.z);
2101 // If the ruler in the Y axis is enabled, scroll in the Y axis.
2102 if(mRulerY->GetType() == Ruler::Free)
2104 // Free panning mode
2105 targetScrollPosition.y += event.z * mMouseWheelScrollDistanceStep.y;
2106 ClampPosition(targetScrollPosition);
2107 ScrollTo(-targetScrollPosition);
2109 else if(!mScrolling)
2111 // Snap mode, only respond to the event when the previous snap animation is finished.
2112 ScrollTo(GetCurrentPage() - event.z * mRulerX->GetTotalPages());
2119 void ScrollView::ResetScrolling()
2121 Actor self = Self();
2122 self.GetProperty(mPropertyPosition).Get(mScrollPostPosition);
2123 mScrollPrePosition = mScrollPostPosition;
2124 self.SetProperty(mPropertyPrePosition, mScrollPostPosition);
2127 void ScrollView::UpdateLocalScrollProperties()
2129 Actor self = Self();
2130 self.GetProperty(mPropertyPrePosition).Get(mScrollPrePosition);
2131 self.GetProperty(mPropertyPosition).Get(mScrollPostPosition);
2134 // private functions
2136 void ScrollView::PreAnimatedScrollSetup()
2138 // mPropertyPrePosition is our unclamped property with wrapping
2139 // mPropertyPosition is our final scroll position after clamping
2141 Actor self = Self();
2143 Vector3 deltaPosition(mScrollPostPosition);
2144 WrapPosition(mScrollPostPosition);
2145 mDomainOffset += deltaPosition - mScrollPostPosition;
2146 Self().SetProperty(mPropertyDomainOffset, mDomainOffset);
2148 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2150 // already performing animation on internal x position
2151 StopAnimation(mInternalXAnimation);
2154 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2156 // already performing animation on internal y position
2157 StopAnimation(mInternalYAnimation);
2160 mScrollStateFlags = 0;
2162 mScrollPostScale = GetPropertyScale();
2164 // Update Actor position with this wrapped value.
2167 mScrollPreScale = mScrollPostScale;
2168 mScrollPreRotation = mScrollPostRotation;
2171 void ScrollView::FinaliseAnimatedScroll()
2173 // TODO - common animation finishing code in here
2176 void ScrollView::AnimateInternalXTo( float position, float duration, AlphaFunction alpha )
2178 StopAnimation(mInternalXAnimation);
2180 if( duration > Math::MACHINE_EPSILON_10 )
2182 Actor self = Self();
2183 mInternalXAnimation = Animation::New(duration);
2184 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2185 mInternalXAnimation.AnimateTo( Property(self, mPropertyPrePosition, 0), position, alpha, duration);
2186 mInternalXAnimation.Play();
2188 // erase current state flags
2189 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2190 // add internal animation state flag
2191 mScrollStateFlags |= AnimatingInternalX;
2195 void ScrollView::AnimateInternalYTo( float position, float duration, AlphaFunction alpha )
2197 StopAnimation(mInternalYAnimation);
2199 if( duration > Math::MACHINE_EPSILON_10 )
2201 Actor self = Self();
2202 mInternalYAnimation = Animation::New(duration);
2203 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2204 mInternalYAnimation.AnimateTo( Property(self, mPropertyPrePosition, 1), position, alpha, TimePeriod(duration));
2205 mInternalYAnimation.Play();
2207 // erase current state flags
2208 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2209 // add internal animation state flag
2210 mScrollStateFlags |= AnimatingInternalY;
2214 void ScrollView::OnScrollAnimationFinished( Animation& source )
2216 // Guard against destruction during signal emission
2217 // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished()
2218 Toolkit::ScrollView handle( GetOwner() );
2220 bool scrollingFinished = false;
2222 // update our local scroll positions
2223 UpdateLocalScrollProperties();
2225 if( source == mSnapAnimation )
2227 // generic snap animation used for scaling and rotation
2228 mSnapAnimation.Reset();
2231 if( source == mInternalXAnimation )
2233 if( !(mScrollStateFlags & AnimatingInternalY) )
2235 scrollingFinished = true;
2237 mInternalXAnimation.Reset();
2238 SnapInternalXTo(mScrollPostPosition.x);
2241 if( source == mInternalYAnimation )
2243 if( !(mScrollStateFlags & AnimatingInternalX) )
2245 scrollingFinished = true;
2247 mInternalYAnimation.Reset();
2248 SnapInternalYTo(mScrollPostPosition.y);
2251 if(scrollingFinished)
2253 HandleSnapAnimationFinished();
2257 void ScrollView::OnSnapInternalPositionFinished( Animation& source )
2259 Actor self = Self();
2260 UpdateLocalScrollProperties();
2261 if( source == mInternalXAnimation )
2263 // clear internal x animation flags
2264 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2265 mInternalXAnimation.Reset();
2266 WrapPosition(mScrollPrePosition);
2268 if( source == mInternalYAnimation )
2270 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2271 mInternalYAnimation.Reset();
2272 WrapPosition(mScrollPrePosition);
2276 void ScrollView::SnapInternalXTo(float position)
2278 Actor self = Self();
2280 StopAnimation(mInternalXAnimation);
2282 // erase current state flags
2283 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2285 // if internal x not equal to inputed parameter, animate it
2286 float duration = std::min(fabsf((position - mScrollPrePosition.x) / mMaxOvershoot.x) * mSnapOvershootDuration, mSnapOvershootDuration);
2287 if( duration > Math::MACHINE_EPSILON_1 )
2289 mInternalXAnimation = Animation::New(duration);
2290 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2291 mInternalXAnimation.AnimateTo(Property(self, mPropertyPrePosition, 0), position);
2292 mInternalXAnimation.Play();
2294 // add internal animation state flag
2295 mScrollStateFlags |= SnappingInternalX;
2299 void ScrollView::SnapInternalYTo(float position)
2301 Actor self = Self();
2303 StopAnimation(mInternalYAnimation);
2305 // erase current state flags
2306 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2308 // if internal y not equal to inputed parameter, animate it
2309 float duration = std::min(fabsf((position - mScrollPrePosition.y) / mMaxOvershoot.y) * mSnapOvershootDuration, mSnapOvershootDuration);
2310 if( duration > Math::MACHINE_EPSILON_1 )
2312 mInternalYAnimation = Animation::New(duration);
2313 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2314 mInternalYAnimation.AnimateTo(Property(self, mPropertyPrePosition, 1), position);
2315 mInternalYAnimation.Play();
2317 // add internal animation state flag
2318 mScrollStateFlags |= SnappingInternalY;
2322 void ScrollView::GestureStarted()
2324 // we handle the first gesture.
2325 // if we're currently doing a gesture and receive another
2326 // we continue and combine the effects of the gesture instead of reseting.
2327 if(mGestureStackDepth++==0)
2329 Actor self = Self();
2330 StopTouchDownTimer();
2332 mPanDelta = Vector3::ZERO;
2333 mScaleDelta = Vector3::ONE;
2334 mRotationDelta = 0.0f;
2335 mLastVelocity = Vector2(0.0f, 0.0f);
2338 mLockAxis = LockPossible;
2341 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2343 StopAnimation(mInternalXAnimation);
2345 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2347 StopAnimation(mInternalYAnimation);
2349 mScrollStateFlags = 0;
2351 if(mScrolling) // are we interrupting a current scroll?
2353 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
2355 // send negative scroll position since scroll internal scroll position works as an offset for actors,
2356 // give applications the position within the domain from the scroll view's anchor position
2357 mScrollCompletedSignalV2.Emit( -mScrollPostPosition );
2362 void ScrollView::GestureContinuing(const Vector2& panDelta, const Vector2& scaleDelta, float rotationDelta)
2364 mPanDelta.x+= panDelta.x;
2365 mPanDelta.y+= panDelta.y;
2366 mScaleDelta.x*= scaleDelta.x;
2367 mScaleDelta.y*= scaleDelta.y;
2368 mRotationDelta+= rotationDelta;
2370 // Save the velocity, there is a bug in PanGesture
2371 // Whereby the Gesture::Finished's velocity is either:
2372 // NaN (due to time delta of zero between the last two events)
2373 // or 0 (due to position being the same between the last two events)
2375 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
2376 // appears mostly horizontal or mostly vertical respectively.
2379 mLockAxis = GetLockAxis(mPanDelta.GetVectorXY(), mLockAxis, mAxisAutoLockGradient);
2380 } // end if mAxisAutoLock
2383 // TODO: Upgrade to use a more powerful gesture detector (one that supports multiple touches on pan - so works as pan and flick gesture)
2384 // TODO: Reimplement Scaling (pinching 2+ points)
2385 // TODO: Reimplment Rotation (pinching 2+ points)
2386 // BUG: Gesture::Finished doesn't always return velocity on release (due to
2387 // timeDelta between last two events being 0 sometimes, or posiiton being the same)
2388 void ScrollView::OnPan(PanGesture gesture)
2390 // Guard against destruction during signal emission
2391 // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx()
2392 Actor self( Self() );
2396 // If another callback on the same original signal disables sensitivity,
2397 // this callback will still be called, so we must suppress it.
2401 // translate Gesture input to get useful data...
2402 switch(gesture.state)
2404 case Gesture::Started:
2406 UpdateLocalScrollProperties();
2409 self.SetProperty( mPropertyPanning, true );
2410 self.SetProperty( mPropertyScrollStartPagePosition, Vector3(gesture.position.x, gesture.position.y, 0.0f) );
2412 UpdateMainInternalConstraint();
2416 case Gesture::Continuing:
2418 GestureContinuing(gesture.screenDisplacement, Vector2::ZERO, 0.0f);
2422 case Gesture::Finished:
2423 case Gesture::Cancelled:
2425 UpdateLocalScrollProperties();
2426 mLastVelocity = gesture.velocity;
2428 self.SetProperty( mPropertyPanning, false );
2430 if( mScrollMainInternalPrePositionConstraint )
2432 self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
2437 case Gesture::Possible:
2438 case Gesture::Clear:
2440 // Nothing to do, not needed.
2444 } // end switch(gesture.state)
2446 OnGestureEx(gesture.state);
2449 void ScrollView::OnGestureEx(Gesture::State state)
2451 // call necessary signals for application developer
2453 if(state == Gesture::Started)
2455 Vector3 currentScrollPosition = GetCurrentScrollPosition();
2456 Self().SetProperty(mPropertyScrolling, true);
2458 mScrollStartedSignalV2.Emit( currentScrollPosition );
2460 else if( (state == Gesture::Finished) ||
2461 (state == Gesture::Cancelled) ) // Finished/default
2463 // when all the gestures have finished, we finish the transform.
2464 // so if a user decides to pan (1 gesture), and then pan+zoom (2 gestures)
2465 // then stop panning (back to 1 gesture), and then stop zooming (0 gestures).
2466 // this is the point we end, and perform necessary snapping.
2467 mGestureStackDepth--;
2468 if(mGestureStackDepth==0)
2475 void ScrollView::UpdateTransform()
2477 // TODO: notify clamps using property notifications (or see if we need this, can deprecate it)
2480 void ScrollView::FinishTransform()
2482 // at this stage internal x and x scroll position should have followed prescroll position exactly
2483 Actor self = Self();
2485 PreAnimatedScrollSetup();
2487 bool animating = SnapWithVelocity(mLastVelocity * 1000.0f);
2491 // if not animating, then this pan has completed right now.
2493 Self().SetProperty(mPropertyScrolling, false);
2494 Vector3 currentScrollPosition = GetCurrentScrollPosition();
2495 mScrollCompletedSignalV2.Emit( currentScrollPosition );
2499 Vector3 ScrollView::GetOvershoot(Vector3& position) const
2501 Vector3 size = Self().GetCurrentSize();
2504 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2505 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2507 if(mRulerX->IsEnabled() && rulerDomainX.enabled)
2509 const float left = rulerDomainX.min - position.x;
2510 const float right = size.width - rulerDomainX.max - position.x;
2517 overshoot.x = right;
2521 if(mRulerY->IsEnabled() && rulerDomainY.enabled)
2523 const float top = rulerDomainY.min - position.y;
2524 const float bottom = size.height - rulerDomainY.max - position.y;
2531 overshoot.y = bottom;
2538 bool ScrollView::OnAccessibilityPan(PanGesture gesture)
2540 // Keep track of whether this is an AccessibilityPan
2541 mInAccessibilityPan = true;
2543 mInAccessibilityPan = false;
2548 void ScrollView::ClampPosition(Vector3& position) const
2550 ClampState3 clamped;
2551 ClampPosition(position, clamped);
2554 void ScrollView::ClampPosition(Vector3& position, ClampState3 &clamped) const
2556 Vector3 size = Self().GetCurrentSize();
2558 // determine size of viewport relative to current scaled size.
2559 // e.g. if you're zoomed in 200%, then each pixel on screen is only 0.5 pixels on subject.
2560 if(fabsf(mScrollPostScale.x) > Math::MACHINE_EPSILON_0)
2562 size.x /= mScrollPostScale.x;
2565 if(fabsf(mScrollPostScale.y) > Math::MACHINE_EPSILON_0)
2567 size.y /= mScrollPostScale.y;
2570 position.x = -mRulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system.
2571 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.
2573 clamped.z = NotClamped;
2576 void ScrollView::WrapPosition(Vector3& position) const
2580 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2581 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2583 if(mRulerX->IsEnabled())
2585 position.x = -WrapInDomain(-position.x, rulerDomainX.min, rulerDomainX.max);
2588 if(mRulerY->IsEnabled())
2590 position.y = -WrapInDomain(-position.y, rulerDomainY.min, rulerDomainY.max);
2595 void ScrollView::ClampScale(Vector3& scale) const
2597 ClampState3 clamped;
2598 ClampScale(scale, clamped);
2601 void ScrollView::ClampScale(Vector3& scale, ClampState3 &clamped) const
2603 scale.x = mRulerScaleX->Clamp(scale.x, 0.0f, 1.0f, clamped.x);
2604 scale.y = mRulerScaleY->Clamp(scale.y, 0.0f, 1.0f, clamped.y);
2605 clamped.z = NotClamped;
2608 void ScrollView::UpdateMainInternalConstraint()
2610 // TODO: Only update the constraints which have changed, rather than remove all and add all again.
2611 // Requires a dali-core ApplyConstraintAt, or a ReplaceConstraint. The former is probably more flexible.
2612 Actor self = Self();
2613 PanGestureDetector detector( GetPanGestureDetector() );
2615 if(mScrollMainInternalPositionConstraint)
2617 self.RemoveConstraint(mScrollMainInternalPositionConstraint);
2618 self.RemoveConstraint(mScrollMainInternalDeltaConstraint);
2619 self.RemoveConstraint(mScrollMainInternalFinalConstraint);
2620 self.RemoveConstraint(mScrollMainInternalRelativeConstraint);
2621 self.RemoveConstraint(mScrollMainInternalXConstraint);
2622 self.RemoveConstraint(mScrollMainInternalYConstraint);
2624 if( mScrollMainInternalPrePositionConstraint )
2626 self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
2629 // TODO: It's probably better to use a local displacement value as this will give a displacement when scrolling just commences
2630 // but we need to make sure than the gesture system gives displacement since last frame (60Hz), not displacement since last touch event (90Hz).
2632 // 1. First calculate the pre-position (this is the scroll position if no clamping has taken place)
2633 Vector2 initialPanMask = Vector2(mRulerX->IsEnabled() ? 1.0f : 0.0f, mRulerY->IsEnabled() ? 1.0f : 0.0f);
2635 if( mLockAxis == LockVertical )
2637 initialPanMask.y = 0.0f;
2639 else if( mLockAxis == LockHorizontal )
2641 initialPanMask.x = 0.0f;
2643 Constraint constraint;
2647 constraint = Constraint::New<Vector3>( mPropertyPrePosition,
2648 Source( detector, PanGestureDetector::LOCAL_POSITION ),
2649 Source( detector, PanGestureDetector::LOCAL_DISPLACEMENT ),
2650 Source( self, Actor::SIZE ),
2651 InternalPrePositionConstraint( initialPanMask, mAxisAutoLock, mAxisAutoLockGradient, mLockAxis, mMaxOvershoot, mRulerX->GetDomain(), mRulerY->GetDomain() ) );
2652 mScrollMainInternalPrePositionConstraint = self.ApplyConstraint( constraint );
2655 // 2. Second calculate the clamped position (actual position)
2656 constraint = Constraint::New<Vector3>( mPropertyPosition,
2657 LocalSource( mPropertyPrePosition ),
2658 LocalSource( mPropertyPositionMin ),
2659 LocalSource( mPropertyPositionMax ),
2660 Source( self, Actor::SIZE ),
2661 InternalPositionConstraint( mRulerX->GetDomain(),
2662 mRulerY->GetDomain(), mWrapMode ) );
2663 mScrollMainInternalPositionConstraint = self.ApplyConstraint( constraint );
2665 constraint = Constraint::New<Vector3>( mPropertyPositionDelta,
2666 LocalSource( mPropertyPosition ),
2667 LocalSource( mPropertyDomainOffset ),
2668 InternalPositionDeltaConstraint );
2669 mScrollMainInternalDeltaConstraint = self.ApplyConstraint( constraint );
2671 constraint = Constraint::New<Vector3>( mPropertyFinal,
2672 LocalSource( mPropertyPosition ),
2673 LocalSource( mPropertyOvershootX ),
2674 LocalSource( mPropertyOvershootY ),
2675 InternalFinalConstraint( FinalDefaultAlphaFunction,
2676 FinalDefaultAlphaFunction ) );
2677 mScrollMainInternalFinalConstraint = self.ApplyConstraint( constraint );
2679 constraint = Constraint::New<Vector3>( mPropertyRelativePosition,
2680 LocalSource( mPropertyPosition ),
2681 LocalSource( mPropertyPositionMin ),
2682 LocalSource( mPropertyPositionMax ),
2683 LocalSource( Actor::SIZE ),
2684 InternalRelativePositionConstraint );
2685 mScrollMainInternalRelativeConstraint = self.ApplyConstraint( constraint );
2687 constraint = Constraint::New<float>( mPropertyX,
2688 LocalSource( mPropertyPrePosition ),
2689 InternalXConstraint );
2690 mScrollMainInternalXConstraint = self.ApplyConstraint( constraint );
2692 constraint = Constraint::New<float>( mPropertyY,
2693 LocalSource( mPropertyPrePosition ),
2694 InternalYConstraint );
2695 mScrollMainInternalYConstraint = self.ApplyConstraint( constraint );
2697 // When panning we want to make sure overshoot values are affected by pre position and post position
2698 SetOvershootConstraintsEnabled(!mWrapMode);
2701 void ScrollView::SetOvershootConstraintsEnabled(bool enabled)
2703 Actor self( Self() );
2704 // remove and reset, it may now be in wrong order with the main internal constraints
2705 if( mScrollMainInternalOvershootXConstraint )
2707 self.RemoveConstraint(mScrollMainInternalOvershootXConstraint);
2708 mScrollMainInternalOvershootXConstraint.Reset();
2709 self.RemoveConstraint(mScrollMainInternalOvershootYConstraint);
2710 mScrollMainInternalOvershootYConstraint.Reset();
2714 Constraint constraint = Constraint::New<float>( mPropertyOvershootX,
2715 LocalSource( mPropertyPrePosition ),
2716 LocalSource( mPropertyPosition ),
2717 LocalSource( mPropertyCanScrollHorizontal ),
2718 OvershootXConstraint(mMaxOvershoot.x) );
2719 mScrollMainInternalOvershootXConstraint = self.ApplyConstraint( constraint );
2721 constraint = Constraint::New<float>( mPropertyOvershootY,
2722 LocalSource( mPropertyPrePosition ),
2723 LocalSource( mPropertyPosition ),
2724 LocalSource( mPropertyCanScrollVertical ),
2725 OvershootYConstraint(mMaxOvershoot.y) );
2726 mScrollMainInternalOvershootYConstraint = self.ApplyConstraint( constraint );
2730 self.SetProperty(mPropertyOvershootX, 0.0f);
2731 self.SetProperty(mPropertyOvershootY, 0.0f);
2735 void ScrollView::SetInternalConstraints()
2737 // Internal constraints (applied to target ScrollBase Actor itself) /////////
2738 UpdateMainInternalConstraint();
2740 // User definable constraints to apply to all child actors //////////////////
2741 Actor self = Self();
2743 // LocalSource - The Actors to be moved.
2744 // self - The ScrollView
2746 // Apply some default constraints to ScrollView.
2747 // Movement + Scaling + Wrap function
2749 Constraint constraint;
2751 // MoveScaledActor (scrolling/zooming)
2752 constraint = Constraint::New<Vector3>( Actor::POSITION,
2753 Source( self, mPropertyPosition ),
2754 Source( self, mPropertyScale ),
2755 MoveScaledActorConstraint );
2756 constraint.SetRemoveAction(Constraint::Discard);
2757 ApplyConstraintToBoundActors(constraint);
2759 // ScaleActor (scrolling/zooming)
2760 constraint = Constraint::New<Vector3>( Actor::SCALE,
2761 Source( self, mPropertyScale ),
2762 ScaleActorConstraint );
2763 constraint.SetRemoveAction(Constraint::Discard);
2764 ApplyConstraintToBoundActors(constraint);
2766 // WrapActor (wrap functionality)
2767 constraint = Constraint::New<Vector3>( Actor::POSITION,
2768 LocalSource( Actor::SCALE ),
2769 LocalSource( Actor::ANCHOR_POINT ),
2770 LocalSource( Actor::SIZE ),
2771 Source( self, mPropertyPositionMin ),
2772 Source( self, mPropertyPositionMax ),
2773 Source( self, mPropertyWrap ),
2774 WrapActorConstraint );
2775 constraint.SetRemoveAction(Constraint::Discard);
2776 ApplyConstraintToBoundActors(constraint);
2779 void ScrollView::StartRefreshTimer()
2781 if(mRefreshIntervalMilliseconds > 0)
2785 mRefreshTimer = Timer::New( mRefreshIntervalMilliseconds );
2786 mRefreshTimer.TickSignal().Connect( this, &ScrollView::OnRefreshTick );
2789 if (!mRefreshTimer.IsRunning())
2791 mRefreshTimer.Start();
2796 void ScrollView::CancelRefreshTimer()
2800 mRefreshTimer.Stop();
2804 bool ScrollView::OnRefreshTick()
2806 // Guard against destruction during signal emission
2807 Toolkit::ScrollView handle( GetOwner() );
2809 Vector3 currentScrollPosition = GetCurrentScrollPosition();
2810 mScrollUpdatedSignalV2.Emit( currentScrollPosition );
2815 } // namespace Internal
2817 } // namespace Toolkit