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 //#define ENABLED_SCROLL_STATE_LOGGING
30 #ifdef ENABLED_SCROLL_STATE_LOGGING
31 #define DALI_LOG_SCROLL_STATE(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d " format "\n", __PRETTY_FUNCTION__, __LINE__, ## args)
33 #define DALI_LOG_SCROLL_STATE(format, args...)
36 // TODO: Change to two class system:
37 // 1. DraggableActor (is an actor which can be dragged anywhere/scaled/rotated, can be set to range using the ruler)
38 // 2. ScrollView (contains a draggable actor that can a) be dragged in the negative X, and Y domain, b) has a hitArea for touches)
40 // TODO: Asymetrical scaling
41 // TODO: external components (page and status overlays).
43 // TODO: upgrade Vector2/3 to support returning Unit vectors, normals, & cross product (dot product is already provided)
50 const int DEFAULT_REFRESH_INTERVAL_MILLISECONDS = 50; ///< Refresh rate TODO: Animation should have an update signal (and see item-view-impl)
51 const float FLICK_SPEED_THRESHOLD = 500.0f; ///< Flick threshold in pixels/ms
52 const float FREE_FLICK_SPEED_THRESHOLD = 200.0f; ///< Free-Flick threshold in pixels/ms
53 const float AUTOLOCK_AXIS_MINIMUM_DISTANCE2 = 100.0f; ///< Auto-lock axis after minimum distance squared.
54 const float FLICK_ORTHO_ANGLE_RANGE = 60.0f; ///< degrees. (if >45, then supports diagonal flicking)
55 const unsigned int MAXIMUM_NUMBER_OF_VALUES = 5; ///< Number of values to use for weighted pan calculation.
56 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.
57 const unsigned long MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET( 150u );
58 const float DEFAULT_OVERSHOOT_ANIMATION_DURATION = 0.35f; // time in seconds
59 const Vector2 OVERSCROLL_CLAMP(1.0f, 1.0f); // maximum overscroll allowed in pixels when overshoot indicator is being used
60 const float TOUCH_DOWN_TIMER_INTERVAL = 100.0f;
61 const float DEFAULT_SCROLL_UPDATE_DISTANCE( 30.0f ); ///< Default distance to travel in pixels for scroll update signal
63 // predefined effect values
64 const Vector3 ANGLE_CAROUSEL_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
65 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
66 const Vector2 ANGLE_CUSTOM_CUBE_SWING(-Math::PI * 0.45f, -Math::PI * 0.45f); ///< outer cube pages swing 90 degrees as they pan offscreen
67 const Vector2 ANGLE_SPIRAL_SWING_IN(Math::PI * 0.5f, Math::PI * 0.5f);
68 const Vector2 ANGLE_SPIRAL_SWING_OUT(Math::PI * 0.35f, Math::PI * 0.35f);
69 const Vector2 ANGLE_OUTER_CUBE_SWING(Math::PI * 0.5f, Math::PI * 0.5f); ///< outer cube pages swing 90 degrees as they pan offscreen
71 // Helpers ////////////////////////////////////////////////////////////////////////////////////////
73 // TODO: GetAngle for Vector2 can be moved.
74 // GetAngle for Vector3 needs to be measured against a normal/plane.
77 * @param[in] vector The 3D vector to be measured
78 * @return angle in radians from 0 to 2PI
80 float GetAngle(const Vector3& vector)
82 return atan2(vector.y, vector.x) + Math::PI;
86 * @param[in] vector The 2D vector to be measured
87 * @return angle in radians from 0 to 2PI
89 float GetAngle(const Vector2& vector)
91 return atan2(vector.y, vector.x) + Math::PI;
95 * Find the vector (distance) from (a) to (b)
96 * in domain (start) to (end)
97 * (\ / start) (\ / end)
100 * @note assumes both (a) and (b) are already with the domain
103 * @param[in] a the current point
104 * @param[in] b the target point
105 * @param[in] start the start of the domain
106 * @param[in] end the end of the domain
107 * @param[in] bias whether to only take the right direction or the left direction,
108 * or the shortest direction.
109 * @return the shortest direction and distance
111 float VectorInDomain(float a, float b, float start, float end, Dali::Toolkit::DirectionBias bias)
113 if(bias == Dali::Toolkit::DirectionBiasNone)
115 return ShortestDistanceInDomain( a, b, start, end );
118 float size = end-start;
124 if(bias == Dali::Toolkit::DirectionBiasRight) // going right, take the vector.
130 float aRight = a+size;
137 if(bias == Dali::Toolkit::DirectionBiasLeft) // going left, take the vector.
143 float aLeft = a-size;
150 * Returns the position of the anchor within actor
152 * @param actor The Actor
153 * @param anchor The Anchor point of interest.
154 * @return The position of the Anchor
156 Vector3 GetPositionOfAnchor(Actor &actor, const Vector3 &anchor)
158 Vector3 childPosition = actor.GetCurrentPosition();
159 Vector3 childAnchor = - actor.GetCurrentAnchorPoint() + anchor;
160 Vector3 childSize = actor.GetCurrentSize();
162 return childPosition + childAnchor * childSize;
165 // AlphaFunctions /////////////////////////////////////////////////////////////////////////////////
167 float FinalDefaultAlphaFunction(float offset)
169 return offset * 0.5f;
173 * ConstantDecelerationAlphaFunction
174 * Newtoninan distance for constant deceleration
175 * v = 1 - t, s = t - 1/2 t^2
176 * when t = 0, s = 0.0 (min distance)
177 * when t = 1, s = 0.5 (max distance)
178 * progress = s / (max-min) = 2t - t^2
180 * @param[in] offset The input progress
181 * @return The output progress
183 float ConstantDecelerationAlphaFunction(float progress)
185 return progress * 2.0f - progress * progress;
188 // Internal Constraints ///////////////////////////////////////////////////////////////////////////
191 * Internal Relative position Constraint
192 * Generates the relative position value of the scroll view
193 * based on the absolute position, and it's relation to the
194 * scroll domain. This is a value from 0.0f to 1.0f in each
195 * scroll position axis.
197 Vector3 InternalRelativePositionConstraint(const Vector3& current,
198 const PropertyInput& scrollPositionProperty,
199 const PropertyInput& scrollMinProperty,
200 const PropertyInput& scrollMaxProperty,
201 const PropertyInput& scrollSizeProperty)
203 Vector3 position = -scrollPositionProperty.GetVector3();
204 const Vector3& min = scrollMinProperty.GetVector3();
205 const Vector3& max = scrollMaxProperty.GetVector3();
206 const Vector3& size = scrollSizeProperty.GetVector3();
208 position.x = WrapInDomain(position.x, min.x, max.x);
209 position.y = WrapInDomain(position.y, min.y, max.y);
211 Vector3 relativePosition;
212 Vector3 domainSize = (max - min) - size;
214 relativePosition.x = domainSize.x > Math::MACHINE_EPSILON_1 ? fabsf((position.x - min.x) / domainSize.x) : 0.0f;
215 relativePosition.y = domainSize.y > Math::MACHINE_EPSILON_1 ? fabsf((position.y - min.y) / domainSize.y) : 0.0f;
217 return relativePosition;
220 } // unnamed namespace
235 * Returns whether to lock scrolling to a particular axis
237 * @param[in] panDelta Distance panned since gesture started
238 * @param[in] currentLockAxis The current lock axis value
239 * @param[in] lockGradient How quickly to lock to a particular axis
241 * @return The new axis lock state
243 ScrollView::LockAxis GetLockAxis(const Vector2& panDelta, ScrollView::LockAxis currentLockAxis, float lockGradient)
245 if(panDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 &&
246 currentLockAxis == ScrollView::LockPossible)
248 float dx = fabsf(panDelta.x);
249 float dy = fabsf(panDelta.y);
250 if(dx * lockGradient >= dy)
252 // 0.36:1 gradient to the horizontal (deviate < 20 degrees)
253 currentLockAxis = ScrollView::LockVertical;
255 else if(dy * lockGradient > dx)
257 // 0.36:1 gradient to the vertical (deviate < 20 degrees)
258 currentLockAxis = ScrollView::LockHorizontal;
262 currentLockAxis = ScrollView::LockNone;
265 return currentLockAxis;
269 * Internal Pre-Position Property Constraint.
271 * Generates position property based on current position + gesture displacement.
272 * Or generates position property based on positionX/Y.
273 * Note: This is the position prior to any clamping at scroll boundaries.
274 * TODO: Scale & Rotation Transforms.
276 struct InternalPrePositionConstraint
278 InternalPrePositionConstraint(const Vector2& initialPanMask,
280 float axisAutoLockGradient,
281 ScrollView::LockAxis initialLockAxis,
282 const Vector2& maxOvershoot,
283 const RulerDomain& domainX, const RulerDomain& domainY)
284 : mInitialPanMask(initialPanMask),
285 mDomainMin( -domainX.min, -domainY.min ),
286 mDomainMax( -domainX.max, -domainY.max ),
287 mMaxOvershoot(maxOvershoot),
288 mAxisAutoLockGradient(axisAutoLockGradient),
289 mLockAxis(initialLockAxis),
290 mAxisAutoLock(axisAutoLock),
292 mClampX( domainX.enabled ),
293 mClampY( domainY.enabled )
297 Vector3 operator()(const Vector3& current,
298 const PropertyInput& gesturePositionProperty,
299 const PropertyInput& gestureDisplacementProperty,
300 const PropertyInput& sizeProperty)
302 Vector3 scrollPostPosition = current;
303 Vector2 panPosition = gesturePositionProperty.GetVector2();
307 mLocalStart = gesturePositionProperty.GetVector2() - gestureDisplacementProperty.GetVector2();
308 mPrePosition = current;
309 mCurrentPanMask = mInitialPanMask;
313 // Calculate Deltas...
314 Vector2 currentPosition = gesturePositionProperty.GetVector2();
315 Vector2 panDelta( currentPosition - mLocalStart );
317 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
318 // appears mostly horizontal or mostly vertical respectively...
321 mLockAxis = GetLockAxis(panDelta, mLockAxis, mAxisAutoLockGradient);
322 if( mLockAxis == ScrollView::LockVertical )
324 mCurrentPanMask.y = 0.0f;
326 else if( mLockAxis == ScrollView::LockHorizontal )
328 mCurrentPanMask.x = 0.0f;
332 // Restrict deltas based on ruler enable/disable and axis-lock state...
333 panDelta *= mCurrentPanMask;
335 // Perform Position transform based on input deltas...
336 scrollPostPosition = mPrePosition;
337 scrollPostPosition.GetVectorXY() += panDelta;
339 // if no wrapping then clamp preposition to maximum overshoot amount
340 const Vector3& size = sizeProperty.GetVector3();
343 float newXPosition = Clamp(scrollPostPosition.x, (mDomainMax.x + size.x) - mMaxOvershoot.x, mDomainMin.x + mMaxOvershoot.x );
344 if( (newXPosition < scrollPostPosition.x - Math::MACHINE_EPSILON_1)
345 || (newXPosition > scrollPostPosition.x + Math::MACHINE_EPSILON_1) )
347 mPrePosition.x = newXPosition;
348 mLocalStart.x = panPosition.x;
350 scrollPostPosition.x = newXPosition;
354 float newYPosition = Clamp(scrollPostPosition.y, (mDomainMax.y + size.y) - mMaxOvershoot.y, mDomainMin.y + mMaxOvershoot.y );
355 if( (newYPosition < scrollPostPosition.y - Math::MACHINE_EPSILON_1)
356 || (newYPosition > scrollPostPosition.y + Math::MACHINE_EPSILON_1) )
358 mPrePosition.y = newYPosition;
359 mLocalStart.y = panPosition.y;
361 scrollPostPosition.y = newYPosition;
364 return scrollPostPosition;
367 Vector3 mPrePosition;
369 Vector2 mInitialPanMask; ///< Initial pan mask (based on ruler settings)
370 Vector2 mCurrentPanMask; ///< Current pan mask that can be altered by axis lock mode.
373 Vector2 mMaxOvershoot;
375 float mAxisAutoLockGradient; ///< Set by ScrollView
376 ScrollView::LockAxis mLockAxis;
378 bool mAxisAutoLock:1; ///< Set by ScrollView
385 * Internal Position Property Constraint.
387 * Generates position property based on pre-position
388 * Note: This is the position after clamping.
389 * (uses result of InternalPrePositionConstraint)
391 struct InternalPositionConstraint
393 InternalPositionConstraint(const RulerDomain& domainX, const RulerDomain& domainY, bool wrap)
394 : mDomainMin( -domainX.min, -domainY.min ),
395 mDomainMax( -domainX.max, -domainY.max ),
396 mClampX( domainX.enabled ),
397 mClampY( domainY.enabled ),
402 Vector3 operator()(const Vector3& current,
403 const PropertyInput& scrollPositionProperty,
404 const PropertyInput& scrollMinProperty,
405 const PropertyInput& scrollMaxProperty,
406 const PropertyInput& scrollSizeProperty)
408 Vector3 position = scrollPositionProperty.GetVector3();
409 const Vector2& size = scrollSizeProperty.GetVector3().GetVectorXY();
410 const Vector3& min = scrollMinProperty.GetVector3();
411 const Vector3& max = scrollMaxProperty.GetVector3();
415 position.x = -WrapInDomain(-position.x, min.x, max.x);
416 position.y = -WrapInDomain(-position.y, min.y, max.y);
420 // clamp post position to domain
421 position.x = mClampX ? Clamp(position.x, mDomainMax.x + size.x, mDomainMin.x ) : position.x;
422 position.y = mClampY ? Clamp(position.y, mDomainMax.y + size.y, mDomainMin.y ) : position.y;
437 * This constraint updates the X overshoot property using the difference
438 * mPropertyPrePosition.x and mPropertyPosition.x, returning a relative value between 0.0f and 1.0f
440 struct OvershootXConstraint
442 OvershootXConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
444 float operator()(const float& current,
445 const PropertyInput& scrollPrePositionProperty,
446 const PropertyInput& scrollPostPositionProperty,
447 const PropertyInput& canScrollProperty)
449 if( canScrollProperty.GetBoolean() )
451 const Vector3& scrollPrePosition = scrollPrePositionProperty.GetVector3();
452 const Vector3& scrollPostPosition = scrollPostPositionProperty.GetVector3();
453 float newOvershoot = scrollPrePosition.x - scrollPostPosition.x;
454 return (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
463 * This constraint updates the Y overshoot property using the difference
464 * mPropertyPrePosition.y and mPropertyPosition.y, returning a relative value between 0.0f and 1.0f
466 struct OvershootYConstraint
468 OvershootYConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
470 float operator()(const float& current,
471 const PropertyInput& scrollPrePositionProperty,
472 const PropertyInput& scrollPostPositionProperty,
473 const PropertyInput& canScrollProperty)
475 if( canScrollProperty.GetBoolean() )
477 const Vector3& scrollPrePosition = scrollPrePositionProperty.GetVector3();
478 const Vector3& scrollPostPosition = scrollPostPositionProperty.GetVector3();
479 float newOvershoot = scrollPrePosition.y - scrollPostPosition.y;
480 return (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
489 * When panning, this constraint updates the X property, otherwise
490 * it has no effect on the X property.
492 float InternalXConstraint(const float& current,
493 const PropertyInput& scrollPosition)
495 return scrollPosition.GetVector3().x;
499 * When panning, this constraint updates the Y property, otherwise
500 * it has no effect on the Y property.
502 float InternalYConstraint(const float& current,
503 const PropertyInput& scrollPosition)
505 return scrollPosition.GetVector3().y;
509 * Internal Position-Delta Property Constraint.
511 * Generates position-delta property based on scroll-position + scroll-offset properties.
513 Vector3 InternalPositionDeltaConstraint(const Vector3& current,
514 const PropertyInput& scrollPositionProperty,
515 const PropertyInput& scrollOffsetProperty)
517 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
518 const Vector3& scrollOffset = scrollOffsetProperty.GetVector3();
520 return scrollPosition + scrollOffset;
524 * Internal Final Position Constraint
525 * The position of content is:
526 * of scroll-position + f(scroll-overshoot)
527 * where f(...) function defines how overshoot
528 * should affect final-position.
530 struct InternalFinalConstraint
532 InternalFinalConstraint(AlphaFunction functionX,
533 AlphaFunction functionY)
534 : mFunctionX(functionX),
535 mFunctionY(functionY)
539 Vector3 operator()(const Vector3& current,
540 const PropertyInput& scrollPositionProperty,
541 const PropertyInput& scrollOvershootXProperty,
542 const PropertyInput& scrollOvershootYProperty)
544 const float& overshootx = scrollOvershootXProperty.GetFloat();
545 const float& overshooty = scrollOvershootYProperty.GetFloat();
546 Vector3 offset( mFunctionX(overshootx),
547 mFunctionY(overshooty),
550 return scrollPositionProperty.GetVector3() - offset;
553 AlphaFunction mFunctionX;
554 AlphaFunction mFunctionY;
560 return Toolkit::ScrollView::New();
563 TypeRegistration typeRegistration( typeid(Toolkit::ScrollView), typeid(Toolkit::Scrollable), Create );
565 SignalConnectorType signalConnector1( typeRegistration, Toolkit::ScrollView::SIGNAL_SNAP_STARTED, &ScrollView::DoConnectSignal );
570 ///////////////////////////////////////////////////////////////////////////////////////////////////
572 ///////////////////////////////////////////////////////////////////////////////////////////////////
574 Dali::Toolkit::ScrollView ScrollView::New()
576 // Create the implementation
577 ScrollViewPtr scrollView(new ScrollView());
579 // Pass ownership to CustomActor via derived handle
580 Dali::Toolkit::ScrollView handle(*scrollView);
582 // Second-phase init of the implementation
583 // This can only be done after the CustomActor connection has been made...
584 scrollView->Initialize();
589 ScrollView::ScrollView()
592 mGestureStackDepth(0),
593 mRotationDelta(0.0f),
594 mScrollStateFlags(0),
595 mScrollPreRotation(0.0f),
596 mScrollPostRotation(0.0f),
597 mMinTouchesForPanning(1),
598 mMaxTouchesForPanning(1),
599 mLockAxis(LockPossible),
600 mScrollUpdateDistance(DEFAULT_SCROLL_UPDATE_DISTANCE),
601 mOvershootDelay(1.0f),
602 mMaxOvershoot(Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT, Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT),
603 mUserMaxOvershoot(Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT, Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT),
604 mSnapOvershootDuration(Toolkit::ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION),
605 mSnapOvershootAlphaFunction(AlphaFunctions::EaseOut),
606 mSnapDuration(Toolkit::ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION),
607 mSnapAlphaFunction(AlphaFunctions::EaseOut),
608 mFlickDuration(Toolkit::ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION),
609 mFlickAlphaFunction(AlphaFunctions::EaseOut),
610 mAxisAutoLockGradient(Toolkit::ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT),
611 mFrictionCoefficient(Toolkit::ScrollView::DEFAULT_FRICTION_COEFFICIENT),
612 mFlickSpeedCoefficient(Toolkit::ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT),
613 mMaxFlickSpeed(Toolkit::ScrollView::DEFAULT_MAX_FLICK_SPEED),
614 mPropertiesUpdated(false),
615 mInAccessibilityPan(false),
618 mScrollInterrupted(false),
621 mTouchDownTimeoutReached(false),
622 mActorAutoSnapEnabled(false),
623 mAutoResizeContainerEnabled(false),
625 mAxisAutoLock(false),
627 mDefaultMaxOvershoot(true),
628 mCanScrollHorizontal(true),
629 mCanScrollVertical(true)
631 SetRequiresMouseWheelEvents(true);
634 void ScrollView::OnInitialize()
638 // Internal Actor, used to hide actors from enumerations.
639 // Also actors added to Internal actor appear as overlays e.g. ScrollBar components.
640 mInternalActor = Actor::New();
641 mInternalActor.SetDrawMode(DrawMode::OVERLAY);
642 self.Add(mInternalActor);
643 mInternalActor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
644 mInternalActor.SetParentOrigin(ParentOrigin::CENTER);
645 mInternalActor.SetAnchorPoint(AnchorPoint::CENTER);
649 // Register Scroll Properties.
650 RegisterProperties();
652 mScrollPostPosition = mScrollPrePosition = Vector3::ZERO;
653 mScrollPostScale = mScrollPreScale = Vector3::ONE;
654 mScrollPostRotation = mScrollPreRotation = 0.0f;
656 mMouseWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
660 mGestureStackDepth = 0;
662 EnableGestureDetection( Gesture::Type( Gesture::Pan ) );
664 // For pan, default to only 1 touch required, ignoring touches outside this range.
665 SetTouchesRequiredForPanning(1, 1, false);
667 // By default we'll allow the user to freely drag the scroll view,
668 // while disabling the other rulers.
669 RulerPtr ruler = new DefaultRuler();
670 RulerPtr rulerDisabled = new DefaultRuler();
671 rulerDisabled->Disable();
674 mRulerScaleX = rulerDisabled;
675 mRulerScaleY = rulerDisabled;
676 mRulerRotation = rulerDisabled;
678 EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator);
680 Vector3 size = GetControlSize();
681 UpdatePropertyDomain(size);
682 SetInternalConstraints();
685 void ScrollView::OnControlStageConnection()
687 DALI_LOG_SCROLL_STATE("[0x%X]", this);
691 SetScrollSensitive( false );
692 SetScrollSensitive( true );
694 if(IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator))
696 // try and make sure property notifications are set
697 EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator);
701 void ScrollView::OnControlStageDisconnection()
703 DALI_LOG_SCROLL_STATE("[0x%X]", this);
708 ScrollView::~ScrollView()
710 DALI_LOG_SCROLL_STATE("[0x%X]", this);
713 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
715 return mSnapAlphaFunction;
718 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
720 mSnapAlphaFunction = alpha;
723 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
725 return mFlickAlphaFunction;
728 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
730 mFlickAlphaFunction = alpha;
733 float ScrollView::GetScrollSnapDuration() const
735 return mSnapDuration;
738 void ScrollView::SetScrollSnapDuration(float time)
740 mSnapDuration = time;
743 float ScrollView::GetScrollFlickDuration() const
745 return mFlickDuration;
748 void ScrollView::SetScrollFlickDuration(float time)
750 mFlickDuration = time;
753 void ScrollView::ApplyEffect(Toolkit::ScrollViewEffect effect)
755 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
757 // Assertion check to ensure effect doesn't already exist in this scrollview
758 bool effectAlreadyExistsInScrollView(false);
759 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
763 effectAlreadyExistsInScrollView = true;
768 DALI_ASSERT_ALWAYS(!effectAlreadyExistsInScrollView);
770 // add effect to effects list
771 mEffects.push_back(effect);
773 // invoke Attachment request to ScrollView first
774 GetImpl(effect).Attach(self);
777 Toolkit::ScrollViewEffect ScrollView::ApplyEffect(Toolkit::ScrollView::PageEffect effect)
779 Toolkit::ScrollViewEffect scrollEffect;
782 case Toolkit::ScrollView::PageEffectNone:
786 case Toolkit::ScrollView::PageEffectOuterCube:
788 Toolkit::ScrollViewCustomEffect customEffect;
789 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
790 Vector2 pageSize = Stage::GetCurrent().GetSize();
791 // 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
792 // 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)
793 // the page moving off screen will slowly build a spacing from 0.0f to 20.0f
794 // the spacing from each page is added together for the final spacing between the two pages.
795 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));
796 customEffect.SetSwingAngleOut(ANGLE_CUSTOM_CUBE_SWING.x, Vector3(0.0f, -1.0f, 0.0f));
797 customEffect.SetSwingAnchor(AnchorPoint::CENTER, AnchorPoint::CENTER_LEFT);
798 customEffect.SetOpacityThreshold(0.7f);
801 case Toolkit::ScrollView::PageEffectDepth:
803 Toolkit::ScrollViewCustomEffect customEffect;
804 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
807 case Toolkit::ScrollView::PageEffectInnerCube:
809 Toolkit::ScrollViewCustomEffect customEffect;
810 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
811 customEffect.SetPageSpacing(Vector2(30.0f, 30.0f));
812 customEffect.SetAngledOriginPageRotation(ANGLE_CUBE_PAGE_ROTATE);
813 customEffect.SetSwingAngle(ANGLE_CUBE_PAGE_ROTATE.x, Vector3(0,-1,0));
814 customEffect.SetOpacityThreshold(0.5f);
817 case Toolkit::ScrollView::PageEffectCarousel:
819 Toolkit::ScrollViewCustomEffect customEffect;
820 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
821 customEffect.SetPageTranslation(Vector3(0,0,0), Vector3(-30, 0, 0));
822 customEffect.SetPageSpacing(Vector2(60.0f, 60.0f));
823 customEffect.SetAngledOriginPageRotation(-ANGLE_CUBE_PAGE_ROTATE);
824 customEffect.SetOpacityThreshold(0.2f, 0.6f);
827 case Toolkit::ScrollView::PageEffectSpiral:
829 Toolkit::ScrollViewCustomEffect customEffect;
830 scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New();
832 Vector2 pageSize = Stage::GetCurrent().GetSize();
833 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));
834 //customEffect.SetSwingAngleAlphaFunctionOut(AlphaFunctions::EaseOut);
835 customEffect.SetSwingAnchor(AnchorPoint::CENTER_RIGHT);
836 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);
837 //customEffect.SetPageTranslateAlphaFunctionOut(AlphaFunctions::EaseOut);
838 customEffect.SetOpacityThreshold(0.75f, 0.6f);
839 customEffect.SetOpacityAlphaFunctionIn(AlphaFunctions::EaseInOut);
844 DALI_ASSERT_DEBUG(0 && "unknown scroll view effect");
847 RemoveConstraintsFromChildren();
850 ApplyEffect(scrollEffect);
855 void ScrollView::RemoveEffect(Toolkit::ScrollViewEffect effect)
857 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
859 // remove effect from effects list
860 bool effectExistedInScrollView(false);
861 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
865 mEffects.erase(iter);
866 effectExistedInScrollView = true;
871 // Assertion check to ensure effect existed.
872 DALI_ASSERT_ALWAYS(effectExistedInScrollView);
874 // invoke Detachment request to ScrollView last
875 GetImpl(effect).Detach(self);
878 void ScrollView::RemoveAllEffects()
880 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
882 for (ScrollViewEffectIter effectIter = mEffects.begin(); effectIter != mEffects.end(); ++effectIter)
884 Toolkit::ScrollViewEffect effect = *effectIter;
886 // invoke Detachment request to ScrollView last
887 GetImpl(effect).Detach(self);
893 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
895 ApplyConstraintToBoundActors(constraint);
898 void ScrollView::RemoveConstraintsFromChildren()
900 RemoveConstraintsFromBoundActors();
903 const RulerPtr ScrollView::GetRulerX() const
908 const RulerPtr ScrollView::GetRulerY() const
913 void ScrollView::SetRulerX(RulerPtr ruler)
917 Vector3 size = GetControlSize();
918 UpdatePropertyDomain(size);
919 UpdateMainInternalConstraint();
922 void ScrollView::SetRulerY(RulerPtr ruler)
926 Vector3 size = GetControlSize();
927 UpdatePropertyDomain(size);
928 UpdateMainInternalConstraint();
931 void ScrollView::UpdatePropertyDomain(const Vector3& size)
934 Vector3 min = mMinScroll;
935 Vector3 max = mMaxScroll;
936 bool scrollPositionChanged = false;
937 bool domainChanged = false;
939 bool canScrollVertical = false;
940 bool canScrollHorizontal = false;
941 UpdateLocalScrollProperties();
942 if(mRulerX->IsEnabled())
944 const Toolkit::RulerDomain& rulerDomain = mRulerX->GetDomain();
945 if( fabsf(min.x - rulerDomain.min) > Math::MACHINE_EPSILON_100
946 || fabsf(max.x - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
948 domainChanged = true;
949 min.x = rulerDomain.min;
950 max.x = rulerDomain.max;
952 // make sure new scroll value is within new domain
953 if( mScrollPrePosition.x < min.x
954 || mScrollPrePosition.x > max.x )
956 scrollPositionChanged = true;
957 mScrollPrePosition.x = Clamp(mScrollPrePosition.x, -(max.x - size.x), -min.x);
960 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.x) > Math::MACHINE_EPSILON_100 )
962 canScrollHorizontal = true;
965 else if( fabs(min.x) > Math::MACHINE_EPSILON_100
966 || fabs(max.x) > Math::MACHINE_EPSILON_100 )
968 // need to reset to 0
969 domainChanged = true;
972 canScrollHorizontal = false;
975 if(mRulerY->IsEnabled())
977 const Toolkit::RulerDomain& rulerDomain = mRulerY->GetDomain();
978 if( fabsf(min.y - rulerDomain.min) > Math::MACHINE_EPSILON_100
979 || fabsf(max.y - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
981 domainChanged = true;
982 min.y = rulerDomain.min;
983 max.y = rulerDomain.max;
985 // make sure new scroll value is within new domain
986 if( mScrollPrePosition.y < min.y
987 || mScrollPrePosition.y > max.y )
989 scrollPositionChanged = true;
990 mScrollPrePosition.y = Clamp(mScrollPrePosition.y, -(max.y - size.y), -min.y);
993 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.y) > Math::MACHINE_EPSILON_100 )
995 canScrollVertical = true;
998 else if( fabs(min.y) > Math::MACHINE_EPSILON_100
999 || fabs(max.y) > Math::MACHINE_EPSILON_100 )
1001 // need to reset to 0
1002 domainChanged = true;
1005 canScrollHorizontal = false;
1008 // avoid setting properties if possible, otherwise this will cause an entire update as well as triggering constraints using each property we update
1009 if( mCanScrollVertical != canScrollVertical )
1011 mCanScrollVertical = canScrollVertical;
1012 self.SetProperty(mPropertyCanScrollVertical, canScrollVertical);
1014 if( mCanScrollHorizontal != canScrollHorizontal )
1016 mCanScrollHorizontal = canScrollHorizontal;
1017 self.SetProperty(mPropertyCanScrollHorizontal, canScrollHorizontal);
1019 if( scrollPositionChanged )
1021 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
1027 self.SetProperty(mPropertyPositionMin, mMinScroll );
1028 self.SetProperty(mPropertyPositionMax, mMaxScroll );
1032 void ScrollView::SetRulerScaleX(RulerPtr ruler)
1034 mRulerScaleX = ruler;
1035 UpdateMainInternalConstraint();
1038 void ScrollView::SetRulerScaleY(RulerPtr ruler)
1040 mRulerScaleY = ruler;
1041 UpdateMainInternalConstraint();
1044 void ScrollView::SetRulerRotation(RulerPtr ruler)
1046 mRulerRotation = ruler;
1047 UpdateMainInternalConstraint();
1050 void ScrollView::SetScrollSensitive(bool sensitive)
1052 Actor self = Self();
1053 PanGestureDetector panGesture( GetPanGestureDetector() );
1055 DALI_LOG_SCROLL_STATE("[0x%X] sensitive[%d]", this, int(sensitive));
1057 if((!mSensitive) && (sensitive))
1059 mSensitive = sensitive;
1060 panGesture.Attach(self);
1062 else if((mSensitive) && (!sensitive))
1064 // while the scroll view is panning, the state needs to be reset.
1065 bool isPanning = self.GetProperty<bool>( mPropertyPanning );
1068 PanGesture cancelGesture( Gesture::Cancelled );
1069 OnPan( cancelGesture );
1072 panGesture.Detach(self);
1073 mSensitive = sensitive;
1075 mGestureStackDepth = 0;
1079 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
1081 mMaxOvershoot.x = overshootX;
1082 mMaxOvershoot.y = overshootY;
1083 mUserMaxOvershoot = mMaxOvershoot;
1084 mDefaultMaxOvershoot = false;
1085 UpdateMainInternalConstraint();
1088 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
1090 mSnapOvershootAlphaFunction = alpha;
1093 void ScrollView::SetSnapOvershootDuration(float duration)
1095 mSnapOvershootDuration = duration;
1098 void ScrollView::SetTouchesRequiredForPanning(unsigned int minTouches, unsigned int maxTouches, bool endOutside)
1100 PanGestureDetector panGesture( GetPanGestureDetector() );
1102 mMinTouchesForPanning = minTouches;
1103 mMaxTouchesForPanning = maxTouches;
1107 panGesture.SetMinimumTouchesRequired(minTouches);
1108 panGesture.SetMaximumTouchesRequired(maxTouches);
1112 panGesture.SetMinimumTouchesRequired(1);
1113 panGesture.SetMaximumTouchesRequired(UINT_MAX);
1117 void ScrollView::SetActorAutoSnap(bool enable)
1119 mActorAutoSnapEnabled = enable;
1122 void ScrollView::SetAutoResize(bool enable)
1124 mAutoResizeContainerEnabled = enable;
1125 // TODO: This needs a lot of issues to be addressed before working.
1128 bool ScrollView::GetWrapMode() const
1133 void ScrollView::SetWrapMode(bool enable)
1136 Self().SetProperty(mPropertyWrap, enable);
1139 int ScrollView::GetRefreshInterval() const
1141 return mScrollUpdateDistance;
1144 void ScrollView::SetRefreshInterval(int milliseconds)
1146 mScrollUpdateDistance = milliseconds;
1149 int ScrollView::GetScrollUpdateDistance() const
1151 return mScrollUpdateDistance;
1154 void ScrollView::SetScrollUpdateDistance(int distance)
1156 mScrollUpdateDistance = distance;
1159 bool ScrollView::GetAxisAutoLock() const
1161 return mAxisAutoLock;
1164 void ScrollView::SetAxisAutoLock(bool enable)
1166 mAxisAutoLock = enable;
1167 UpdateMainInternalConstraint();
1170 float ScrollView::GetAxisAutoLockGradient() const
1172 return mAxisAutoLockGradient;
1175 void ScrollView::SetAxisAutoLockGradient(float gradient)
1177 DALI_ASSERT_DEBUG( gradient >= 0.0f && gradient <= 1.0f );
1178 mAxisAutoLockGradient = gradient;
1179 UpdateMainInternalConstraint();
1182 float ScrollView::GetFrictionCoefficient() const
1184 return mFrictionCoefficient;
1187 void ScrollView::SetFrictionCoefficient(float friction)
1189 DALI_ASSERT_DEBUG( friction > 0.0f );
1190 mFrictionCoefficient = friction;
1193 float ScrollView::GetFlickSpeedCoefficient() const
1195 return mFlickSpeedCoefficient;
1198 void ScrollView::SetFlickSpeedCoefficient(float speed)
1200 mFlickSpeedCoefficient = speed;
1203 float ScrollView::GetMaxFlickSpeed() const
1205 return mMaxFlickSpeed;
1208 void ScrollView::SetMaxFlickSpeed(float speed)
1210 mMaxFlickSpeed = speed;
1213 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
1215 mMouseWheelScrollDistanceStep = step;
1218 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
1220 return mMouseWheelScrollDistanceStep;
1223 unsigned int ScrollView::GetCurrentPage() const
1225 // in case animation is currently taking place.
1226 Vector3 position = GetCurrentScrollPosition();
1228 Actor self = Self();
1229 unsigned int page = 0;
1230 unsigned int pagesPerVolume = 1;
1231 unsigned int volume = 0;
1233 // if rulerX is enabled, then get page count (columns)
1234 page = mRulerX->GetPageFromPosition(position.x, mWrapMode);
1235 volume = mRulerY->GetPageFromPosition(position.y, mWrapMode);
1236 pagesPerVolume = mRulerX->GetTotalPages();
1238 return volume * pagesPerVolume + page;
1241 Vector3 ScrollView::GetCurrentScrollPosition() const
1243 if( mPropertiesUpdated )
1245 return -mScrollPostPosition;
1247 return -GetPropertyPosition();
1250 void ScrollView::SetScrollPosition(const Vector3& position)
1252 mScrollPrePosition = position;
1255 Vector3 ScrollView::GetCurrentScrollScale() const
1257 // in case animation is currently taking place.
1258 return GetPropertyScale();
1261 Vector3 ScrollView::GetDomainSize() const
1263 Vector3 size = Self().GetCurrentSize();
1265 const RulerDomain& xDomain = GetRulerX()->GetDomain();
1266 const RulerDomain& yDomain = GetRulerY()->GetDomain();
1268 Vector3 domainSize = Vector3( xDomain.max - xDomain.min, yDomain.max - yDomain.min, 0.0f ) - size;
1272 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation,
1273 DirectionBias horizontalBias, DirectionBias verticalBias)
1275 TransformTo(position, scale, rotation, mSnapDuration, horizontalBias, verticalBias);
1278 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation, float duration,
1279 DirectionBias horizontalBias, DirectionBias verticalBias)
1281 // Guard against destruction during signal emission
1282 // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo()
1283 Toolkit::ScrollView handle( GetOwner() );
1285 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1286 Self().SetProperty( mPropertyScrollStartPagePosition, currentScrollPosition );
1288 PreAnimatedScrollSetup();
1290 Vector3 scrollStartPosition = GetCurrentScrollPosition();
1292 bool animating = AnimateTo(-position,
1293 Vector3::ONE * duration,
1295 Vector3::ONE * duration,
1306 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignalV2 1 [%.2f, %.2f]", this, scrollStartPosition.x, scrollStartPosition.y);
1307 ScrollingStarted(scrollStartPosition);
1312 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignalV2 1 [%.2f, %.2f]", this, -mScrollPostPosition.x, -mScrollPostPosition.y);
1313 ScrollingStopped(-mScrollPostPosition);
1317 void ScrollView::ScrollTo(const Vector3& position)
1319 ScrollTo(position, mSnapDuration );
1322 void ScrollView::ScrollTo(const Vector3& position, float duration)
1324 ScrollTo(position, duration, DirectionBiasNone, DirectionBiasNone);
1327 void ScrollView::ScrollTo(const Vector3& position, float duration,
1328 DirectionBias horizontalBias, DirectionBias verticalBias)
1330 TransformTo(position, mScrollPostScale, mScrollPostRotation, duration, horizontalBias, verticalBias);
1333 void ScrollView::ScrollTo(unsigned int page)
1335 ScrollTo(page, mSnapDuration);
1338 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
1341 unsigned int volume;
1342 unsigned int libraries;
1344 // The position to scroll to is continuous and linear
1345 // unless a domain has been enabled on the X axis.
1346 // or if WrapMode has been enabled.
1347 bool carryX = mRulerX->GetDomain().enabled | mWrapMode;
1348 bool carryY = mRulerY->GetDomain().enabled | mWrapMode;
1350 position.x = mRulerX->GetPositionFromPage(page, volume, carryX);
1351 position.y = mRulerY->GetPositionFromPage(volume, libraries, carryY);
1353 ScrollTo(position, duration, bias, bias);
1356 void ScrollView::ScrollTo(Actor &actor)
1358 ScrollTo(actor, mSnapDuration);
1361 void ScrollView::ScrollTo(Actor &actor, float duration)
1363 DALI_ASSERT_ALWAYS(actor.GetParent() == Self());
1365 Actor self = Self();
1366 Vector3 size = self.GetCurrentSize();
1367 Vector3 position = actor.GetCurrentPosition();
1368 position -= GetPropertyPrePosition();
1370 ScrollTo(Vector3(position.x - size.width * 0.5f, position.y - size.height * 0.5f, 0.0f), duration);
1373 Actor ScrollView::FindClosestActor()
1375 Actor self = Self();
1376 Vector3 size = self.GetCurrentSize();
1378 return FindClosestActorToPosition(Vector3(size.width * 0.5f,size.height * 0.5f,0.0f));
1381 Actor ScrollView::FindClosestActorToPosition(const Vector3& position, FindDirection dirX, FindDirection dirY, FindDirection dirZ)
1384 float closestDistance2 = 0.0f;
1385 Vector3 actualPosition = position;
1387 unsigned int numChildren = Self().GetChildCount();
1389 for(unsigned int i = 0; i < numChildren; ++i)
1391 Actor child = Self().GetChildAt(i);
1393 if(mInternalActor == child) // ignore internal actor.
1398 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1400 Vector3 delta = childPosition - actualPosition;
1402 // X-axis checking (only find Actors to the [dirX] of actualPosition)
1403 if(dirX > All) // != All,None
1405 FindDirection deltaH = delta.x > 0 ? Right : Left;
1412 // Y-axis checking (only find Actors to the [dirY] of actualPosition)
1413 if(dirY > All) // != All,None
1415 FindDirection deltaV = delta.y > 0 ? Down : Up;
1422 // Z-axis checking (only find Actors to the [dirZ] of actualPosition)
1423 if(dirZ > All) // != All,None
1425 FindDirection deltaV = delta.y > 0 ? In : Out;
1432 // compare child to closest child in terms of distance.
1433 float distance2 = 0.0f;
1435 // distance2 = the Square of the relevant dimensions of delta
1438 distance2 += delta.x * delta.x;
1443 distance2 += delta.y * delta.y;
1448 distance2 += delta.z * delta.z;
1451 if(closestChild) // Next time.
1453 if(distance2 < closestDistance2)
1455 closestChild = child;
1456 closestDistance2 = distance2;
1461 closestChild = child;
1462 closestDistance2 = distance2;
1466 return closestChild;
1469 bool ScrollView::ScrollToSnapPoint()
1471 Vector2 stationaryVelocity = Vector2(0.0f, 0.0f);
1472 return SnapWithVelocity( stationaryVelocity );
1475 void ScrollView::ScaleTo(const Vector3& scale)
1477 ScaleTo(scale, mSnapDuration);
1480 void ScrollView::ScaleTo(const Vector3& scale, float duration)
1482 TransformTo(mScrollPostPosition, scale, mScrollPostRotation, duration);
1486 // TODO: In situations where axes are different (X snap, Y free)
1487 // Each axis should really have their own independent animation (time and equation)
1488 // Consider, X axis snapping to nearest grid point (EaseOut over fixed time)
1489 // Consider, Y axis simulating physics to arrive at a point (Physics equation over variable time)
1490 // Currently, the axes have been split however, they both use the same EaseOut equation.
1491 bool ScrollView::SnapWithVelocity(Vector2 velocity)
1493 // Animator takes over now, touches are assumed not to interfere.
1494 // And if touches do interfere, then we'll stop animation, update PrePosition
1495 // to current mScroll's properties, and then resume.
1496 // Note: For Flicking this may work a bit different...
1498 float angle = atan2(velocity.y, velocity.x);
1499 float speed2 = velocity.LengthSquared();
1500 AlphaFunction alphaFunction = mSnapAlphaFunction;
1501 Vector3 positionDuration = Vector3::ONE * mSnapDuration;
1502 Vector3 scaleDuration = Vector3::ONE * mSnapDuration;
1503 float rotationDuration = mSnapDuration;
1506 FindDirection horizontal = None;
1507 FindDirection vertical = None;
1509 // orthoAngleRange = Angle tolerance within the Exact N,E,S,W direction
1510 // that will be accepted as a general N,E,S,W flick direction.
1512 const float orthoAngleRange = FLICK_ORTHO_ANGLE_RANGE * M_PI / 180.0f;
1513 const float flickSpeedThreshold2 = FLICK_SPEED_THRESHOLD*FLICK_SPEED_THRESHOLD;
1515 Vector3 positionSnap = mScrollPrePosition;
1517 // Flick logic X Axis
1519 if(mRulerX->IsEnabled() && mLockAxis != LockHorizontal)
1523 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1524 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1526 if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East
1528 biasX = 0.0f, horizontal = Left;
1530 // This guards against an error where no movement occurs, due to the flick finishing
1531 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1532 positionSnap.x += 1.0f;
1534 else if((angle >= M_PI-orthoAngleRange) || (angle < -M_PI+orthoAngleRange)) // Swiping West
1536 biasX = 1.0f, horizontal = Right;
1538 // This guards against an error where no movement occurs, due to the flick finishing
1539 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1540 positionSnap.x -= 1.0f;
1545 // Flick logic Y Axis
1547 if(mRulerY->IsEnabled() && mLockAxis != LockVertical)
1551 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1552 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1554 if((angle >= M_PI_2-orthoAngleRange) && (angle < M_PI_2+orthoAngleRange)) // Swiping South
1556 biasY = 0.0f, vertical = Up;
1558 else if((angle >= -M_PI_2-orthoAngleRange) && (angle < -M_PI_2+orthoAngleRange)) // Swiping North
1560 biasY = 1.0f, vertical = Down;
1565 // isFlick: Whether this gesture is a flick or not.
1566 bool isFlick = (horizontal != All || vertical != All);
1567 // isFreeFlick: Whether this gesture is a flick under free panning criteria.
1568 bool isFreeFlick = velocity.LengthSquared() > (FREE_FLICK_SPEED_THRESHOLD*FREE_FLICK_SPEED_THRESHOLD);
1570 if(isFlick || isFreeFlick)
1572 positionDuration = Vector3::ONE * mFlickDuration;
1573 alphaFunction = mFlickAlphaFunction;
1576 // Calculate next positionSnap ////////////////////////////////////////////////////////////
1578 if(mActorAutoSnapEnabled)
1580 Vector3 size = Self().GetCurrentSize();
1582 Actor child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f), horizontal, vertical );
1584 if(!child && isFlick )
1586 // If we conducted a direction limited search and found no actor, then just snap to the closest actor.
1587 child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f) );
1592 Vector3 position = Self().GetProperty<Vector3>(mPropertyPosition);
1594 // Get center-point of the Actor.
1595 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1597 if(mRulerX->IsEnabled())
1599 positionSnap.x = position.x - childPosition.x + size.width * 0.5f;
1601 if(mRulerY->IsEnabled())
1603 positionSnap.y = position.y - childPosition.y + size.height * 0.5f;
1608 Vector3 startPosition = positionSnap;
1609 positionSnap.x = -mRulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system.
1610 positionSnap.y = -mRulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
1612 Vector3 clampDelta(Vector3::ZERO);
1613 ClampPosition(positionSnap);
1615 if( (mRulerX->GetType() == Ruler::Free || mRulerY->GetType() == Ruler::Free)
1616 && isFreeFlick && !mActorAutoSnapEnabled)
1618 // Calculate target position based on velocity of flick.
1620 // a = Deceleration (Set to diagonal stage length * friction coefficient)
1621 // u = Initial Velocity (Flick velocity)
1622 // v = 0 (Final Velocity)
1623 // t = Time (Velocity / Deceleration)
1624 Vector2 stageSize = Stage::GetCurrent().GetSize();
1625 float stageLength = Vector3(stageSize.x, stageSize.y, 0.0f).Length();
1626 float a = (stageLength * mFrictionCoefficient);
1627 Vector3 u = Vector3(velocity.x, velocity.y, 0.0f) * mFlickSpeedCoefficient;
1628 float speed = u.Length();
1631 // TODO: Change this to a decay function. (faster you flick, the slower it should be)
1632 speed = std::min(speed, stageLength * mMaxFlickSpeed );
1634 alphaFunction = ConstantDecelerationAlphaFunction;
1636 float t = speed / a;
1638 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1640 positionSnap.x += t*u.x*0.5f;
1643 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1645 positionSnap.y += t*u.y*0.5f;
1648 clampDelta = positionSnap;
1649 ClampPosition(positionSnap);
1650 if((positionSnap - startPosition).LengthSquared() > Math::MACHINE_EPSILON_0)
1652 clampDelta -= positionSnap;
1653 clampDelta.x = clampDelta.x > 0.0f ? std::min(clampDelta.x, mMaxOvershoot.x) : std::max(clampDelta.x, -mMaxOvershoot.x);
1654 clampDelta.y = clampDelta.y > 0.0f ? std::min(clampDelta.y, mMaxOvershoot.y) : std::max(clampDelta.y, -mMaxOvershoot.y);
1658 clampDelta = Vector3::ZERO;
1661 // If Axis is Free and has velocity, then calculate time taken
1662 // to reach target based on velocity in axis.
1663 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1665 float deltaX = fabsf(startPosition.x - positionSnap.x);
1667 if(fabsf(u.x) > Math::MACHINE_EPSILON_1)
1669 positionDuration.x = fabsf(deltaX / u.x);
1673 positionDuration.x = 0;
1677 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1679 float deltaY = fabsf(startPosition.y - positionSnap.y);
1681 if(fabsf(u.y) > Math::MACHINE_EPSILON_1)
1683 positionDuration.y = fabsf(deltaY / u.y);
1687 positionDuration.y = 0;
1691 positionSnap += clampDelta;
1693 // Scale Snap ///////////////////////////////////////////////////////////////
1694 Vector3 scaleSnap = mScrollPostScale;
1696 scaleSnap.x = mRulerScaleX->Snap(scaleSnap.x);
1697 scaleSnap.y = mRulerScaleY->Snap(scaleSnap.y);
1699 ClampScale(scaleSnap);
1701 // Rotation Snap ////////////////////////////////////////////////////////////
1702 float rotationSnap = mScrollPostRotation;
1703 // TODO: implement rotation snap
1705 bool animating = AnimateTo(positionSnap, positionDuration,
1706 scaleSnap, scaleDuration,
1707 rotationSnap, rotationDuration,
1708 alphaFunction, false,
1709 DirectionBiasNone, DirectionBiasNone,
1710 isFlick || isFreeFlick ? Flick : Snap);
1715 void ScrollView::StopAnimation(void)
1717 // Clear Snap animation if exists.
1718 StopAnimation(mSnapAnimation);
1719 StopAnimation(mInternalXAnimation);
1720 StopAnimation(mInternalYAnimation);
1722 // clear scrolling flags
1723 mScrollStateFlags = 0;
1725 // remove scroll animation flags
1726 HandleStoppedAnimation();
1729 void ScrollView::StopAnimation(Animation& animation)
1738 bool ScrollView::AnimateTo(const Vector3& position, const Vector3& positionDuration,
1739 const Vector3& scale, const Vector3& scaleDuration,
1740 float rotation, float rotationDuration,
1741 AlphaFunction alpha, bool findShortcuts,
1742 DirectionBias horizontalBias, DirectionBias verticalBias,
1745 // Here we perform an animation on a number of properties (depending on which have changed)
1746 // The animation is applied to all ScrollBases
1747 Actor self = Self();
1748 mScrollTargetPosition = position;
1749 float totalDuration = 0.0f;
1751 bool positionChanged = (mScrollTargetPosition != mScrollPostPosition);
1752 bool scaleChanged = (scale != mScrollPostScale);
1753 bool rotationChanged = fabsf(rotation - mScrollPostRotation) > Math::MACHINE_EPSILON_0;
1757 totalDuration = std::max(totalDuration, positionDuration.x);
1758 totalDuration = std::max(totalDuration, positionDuration.y);
1763 totalDuration = std::max(totalDuration, scaleDuration.x);
1764 totalDuration = std::max(totalDuration, scaleDuration.y);
1769 totalDuration = std::max(totalDuration, rotationDuration);
1773 // Position Delta ///////////////////////////////////////////////////////
1776 if(mWrapMode && findShortcuts)
1778 // In Wrap Mode, the shortest distance is a little less intuitive...
1779 const RulerDomain rulerDomainX = mRulerX->GetDomain();
1780 const RulerDomain rulerDomainY = mRulerY->GetDomain();
1782 if(mRulerX->IsEnabled())
1784 float dir = VectorInDomain(-mScrollPrePosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
1785 mScrollTargetPosition.x = mScrollPrePosition.x + -dir;
1788 if(mRulerY->IsEnabled())
1790 float dir = VectorInDomain(-mScrollPrePosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
1791 mScrollTargetPosition.y = mScrollPrePosition.y + -dir;
1795 // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting
1796 // a horizonal/vertical wall.delay
1797 AnimateInternalXTo(mScrollTargetPosition.x, positionDuration.x, alpha);
1798 AnimateInternalYTo(mScrollTargetPosition.y, positionDuration.y, alpha);
1801 if( !(mScrollStateFlags & SCROLL_ANIMATION_FLAGS) )
1803 mScrollTargetPosition = position;
1804 WrapPosition(mScrollTargetPosition);
1805 self.SetProperty(mPropertyPrePosition, mScrollTargetPosition);
1806 mScrollPrePosition = mScrollPostPosition = mScrollTargetPosition;
1809 // Scale Delta ///////////////////////////////////////////////////////
1812 if(totalDuration > Math::MACHINE_EPSILON_1)
1814 mSnapAnimation = Animation::New(totalDuration);
1815 mSnapAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
1816 // TODO: for non-uniform scaling to different bounds e.g. scaling a square to a 4:3 aspect ratio screen with a velocity
1817 // the height will hit first, and then the width, so that would require two different animation times just like position.
1818 mSnapAnimation.AnimateTo( Property(self, mPropertyScale), scale, alpha, TimePeriod(0.0f, scaleDuration.x));
1820 mSnapAnimation.AnimateTo( Property(self, mPropertyTime), totalDuration, AlphaFunctions::Linear );
1821 mSnapAnimation.Play();
1825 self.SetProperty(mPropertyScale, scale);
1827 mScrollPreScale = mScrollPostScale = scale;
1830 SetScrollUpdateNotification(true);
1832 // Always send a snap event when AnimateTo is called.
1833 Toolkit::ScrollView::SnapEvent snapEvent;
1834 snapEvent.type = snapType;
1835 snapEvent.position = -mScrollTargetPosition;
1836 snapEvent.scale = scale;
1837 snapEvent.rotation = rotation;
1838 snapEvent.duration = totalDuration;
1840 DALI_LOG_SCROLL_STATE("[0x%X] mSnapStartedSignalV2 [%.2f, %.2f]", this, snapEvent.position.x, snapEvent.position.y);
1841 mSnapStartedSignalV2.Emit( snapEvent );
1843 return (mScrollStateFlags & SCROLL_ANIMATION_FLAGS) != 0;
1846 void ScrollView::SetOvershootEnabled(bool enabled)
1848 if(enabled && !mOvershootIndicator)
1850 mOvershootIndicator = ScrollOvershootIndicator::New();
1854 mMaxOvershoot = OVERSCROLL_CLAMP;
1855 mOvershootIndicator->AttachToScrollable(*this);
1859 mMaxOvershoot = mUserMaxOvershoot;
1860 mOvershootIndicator->DetachFromScrollable(*this);
1862 UpdateMainInternalConstraint();
1865 void ScrollView::AddOverlay(Actor actor)
1867 mInternalActor.Add( actor );
1870 void ScrollView::RemoveOverlay(Actor actor)
1872 mInternalActor.Remove( actor );
1875 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
1877 PanGestureDetector panGesture( GetPanGestureDetector() );
1879 // First remove just in case we have some set, then add.
1880 panGesture.RemoveDirection( direction );
1881 panGesture.AddDirection( direction, threshold );
1884 void ScrollView::RemoveScrollingDirection( Radian direction )
1886 PanGestureDetector panGesture( GetPanGestureDetector() );
1887 panGesture.RemoveDirection( direction );
1890 Toolkit::ScrollView::SnapStartedSignalV2& ScrollView::SnapStartedSignal()
1892 return mSnapStartedSignalV2;
1895 void ScrollView::FindAndUnbindActor(Actor child)
1900 Vector3 ScrollView::GetPropertyPrePosition() const
1902 return Self().GetProperty<Vector3>(mPropertyPrePosition);
1905 Vector3 ScrollView::GetPropertyPosition() const
1907 return Self().GetProperty<Vector3>(mPropertyPosition);
1910 Vector3 ScrollView::GetPropertyScale() const
1912 return Self().GetProperty<Vector3>(mPropertyScale);
1915 void ScrollView::HandleStoppedAnimation()
1917 SetScrollUpdateNotification(false);
1920 void ScrollView::HandleSnapAnimationFinished()
1922 // Emit Signal that scrolling has completed.
1923 Actor self = Self();
1924 self.SetProperty(mPropertyScrolling, false);
1926 Vector3 deltaPosition(mScrollPrePosition);
1928 WrapPosition(mScrollPrePosition);
1929 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
1931 mDomainOffset += deltaPosition - mScrollPostPosition;
1932 self.SetProperty(mPropertyDomainOffset, mDomainOffset);
1933 HandleStoppedAnimation();
1935 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignalV2 2 [%.2f, %.2f]", this, GetCurrentScrollPosition().x, GetCurrentScrollPosition().y);
1936 ScrollingStopped(GetCurrentScrollPosition());
1939 void ScrollView::SetScrollUpdateNotification( bool enabled )
1941 Actor self = Self();
1942 if( mScrollXUpdateNotification )
1944 // disconnect now to avoid a notification before removed from update thread
1945 mScrollXUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1946 self.RemovePropertyNotification(mScrollXUpdateNotification);
1947 mScrollXUpdateNotification.Reset();
1951 mScrollXUpdateNotification = self.AddPropertyNotification(mPropertyPosition, 0, StepCondition(mScrollUpdateDistance, 0.0f));
1952 mScrollXUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1954 if( mScrollYUpdateNotification )
1956 // disconnect now to avoid a notification before removed from update thread
1957 mScrollYUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1958 self.RemovePropertyNotification(mScrollYUpdateNotification);
1959 mScrollYUpdateNotification.Reset();
1963 mScrollYUpdateNotification = self.AddPropertyNotification(mPropertyPosition, 1, StepCondition(mScrollUpdateDistance, 0.0f));
1964 mScrollYUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1968 void ScrollView::OnScrollUpdateNotification(Dali::PropertyNotification& source)
1970 // Guard against destruction during signal emission
1971 Toolkit::ScrollView handle( GetOwner() );
1973 // sometimes notification isnt removed quickly enough, dont emit signal if not scrolling
1976 Vector3 currentScrollPosition = GetCurrentScrollPosition();
1977 mScrollUpdatedSignalV2.Emit( currentScrollPosition );
1981 bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1983 Dali::BaseHandle handle( object );
1985 bool connected( true );
1986 Toolkit::ScrollView view = Toolkit::ScrollView::DownCast( handle );
1988 if( Toolkit::ScrollView::SIGNAL_SNAP_STARTED == signalName )
1990 view.SnapStartedSignal().Connect( tracker, functor );
1994 // signalName does not match any signal
2001 void ScrollView::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
2003 // need to update domain properties for new size
2004 UpdatePropertyDomain(targetSize);
2007 void ScrollView::OnControlSizeSet( const Vector3& size )
2009 // need to update domain properties for new size
2010 if( mDefaultMaxOvershoot )
2012 mUserMaxOvershoot.x = size.x * 0.5f;
2013 mUserMaxOvershoot.y = size.y * 0.5f;
2014 if( !IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) )
2016 mMaxOvershoot = mUserMaxOvershoot;
2019 UpdatePropertyDomain(size);
2020 UpdateMainInternalConstraint();
2021 if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) )
2023 mOvershootIndicator->Reset();
2027 void ScrollView::OnChildAdd(Actor& child)
2035 void ScrollView::OnChildRemove(Actor& child)
2037 // TODO: Actor needs a RemoveConstraint method to take out an individual constraint.
2041 void ScrollView::OnPropertySet( Property::Index index, Property::Value propertyValue )
2043 Actor self = Self();
2044 if( index == mPropertyX )
2046 self.GetProperty(mPropertyPrePosition).Get(mScrollPrePosition);
2047 propertyValue.Get(mScrollPrePosition.x);
2048 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
2050 else if( index == mPropertyY )
2052 self.GetProperty(mPropertyPrePosition).Get(mScrollPrePosition);
2053 propertyValue.Get(mScrollPrePosition.y);
2054 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
2056 else if( index == mPropertyPrePosition )
2058 DALI_LOG_SCROLL_STATE("[0x%X]", this);
2059 propertyValue.Get(mScrollPrePosition);
2063 void ScrollView::StartTouchDownTimer()
2065 if ( !mTouchDownTimer )
2067 mTouchDownTimer = Timer::New( TOUCH_DOWN_TIMER_INTERVAL );
2068 mTouchDownTimer.TickSignal().Connect( this, &ScrollView::OnTouchDownTimeout );
2071 mTouchDownTimer.Start();
2074 void ScrollView::StopTouchDownTimer()
2076 if ( mTouchDownTimer )
2078 mTouchDownTimer.Stop();
2082 bool ScrollView::OnTouchDownTimeout()
2084 mTouchDownTimeoutReached = true;
2086 if( mScrollStateFlags & (SCROLL_ANIMATION_FLAGS | SNAP_ANIMATION_FLAGS) )
2089 if( mScrollStateFlags & SCROLL_ANIMATION_FLAGS )
2091 mScrollInterrupted = true;
2092 // reset domain offset as scrolling from original plane.
2093 mDomainOffset = Vector3::ZERO;
2094 Self().SetProperty(mPropertyDomainOffset, Vector3::ZERO);
2096 UpdateLocalScrollProperties();
2097 mPropertiesUpdated = true;
2098 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignalV2 3 [%.2f, %.2f]", this, GetCurrentScrollPosition().x, GetCurrentScrollPosition().y);
2099 ScrollingStopped(GetCurrentScrollPosition());
2101 // make sure scroll view returns actual property value and not locally stored value once we return from this function
2102 mPropertiesUpdated = false;
2109 bool ScrollView::OnTouchEvent(const TouchEvent& event)
2113 // Ignore this touch event, if scrollview is insensitive.
2117 // Ignore events with multiple-touch points
2118 if (event.GetPointCount() != 1)
2123 UpdateLocalScrollProperties();
2124 mPropertiesUpdated = true;
2126 if( event.GetPoint(0).state == TouchPoint::Down )
2128 if(mGestureStackDepth==0)
2130 mTouchDownTime = event.time;
2132 // This allows time for a pan-gesture to start, to avoid breaking snap-animation behavior with fast flicks.
2133 // If touch-down does not become a pan (after timeout interval), then snap-animation can be interrupted.
2134 StartTouchDownTimer();
2137 else if( event.GetPoint(0).state == TouchPoint::Up )
2139 StopTouchDownTimer();
2141 // if the user touches and releases without enough movement to go
2142 // into a gesture state, then we should snap to nearest point.
2143 // otherwise our scroll could be stopped (interrupted) half way through an animation.
2144 if(mGestureStackDepth==0 && mTouchDownTimeoutReached)
2146 unsigned timeDelta( event.time - mTouchDownTime );
2147 if ( timeDelta >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET )
2149 // Reset the velocity only if down was received a while ago
2150 mLastVelocity = Vector2( 0.0f, 0.0f );
2153 // Only finish the transform if scrolling was interrupted on down or if we are scrolling
2154 if ( mScrollInterrupted || mScrolling )
2159 mTouchDownTimeoutReached = false;
2160 mScrollInterrupted = false;
2163 // no longer guarantee local properties are up to date
2164 mPropertiesUpdated = false;
2169 bool ScrollView::OnMouseWheelEvent(const MouseWheelEvent& event)
2173 // Ignore this mouse wheel event, if scrollview is insensitive.
2177 Vector3 targetScrollPosition = GetPropertyPosition();
2179 if(mRulerX->IsEnabled() && !mRulerY->IsEnabled())
2181 // If only the ruler in the X axis is enabled, scroll in the X axis.
2182 if(mRulerX->GetType() == Ruler::Free)
2184 // Free panning mode
2185 targetScrollPosition.x += event.z * mMouseWheelScrollDistanceStep.x;
2186 ClampPosition(targetScrollPosition);
2187 ScrollTo(-targetScrollPosition);
2189 else if(!mScrolling)
2191 // Snap mode, only respond to the event when the previous snap animation is finished.
2192 ScrollTo(GetCurrentPage() - event.z);
2197 // If the ruler in the Y axis is enabled, scroll in the Y axis.
2198 if(mRulerY->GetType() == Ruler::Free)
2200 // Free panning mode
2201 targetScrollPosition.y += event.z * mMouseWheelScrollDistanceStep.y;
2202 ClampPosition(targetScrollPosition);
2203 ScrollTo(-targetScrollPosition);
2205 else if(!mScrolling)
2207 // Snap mode, only respond to the event when the previous snap animation is finished.
2208 ScrollTo(GetCurrentPage() - event.z * mRulerX->GetTotalPages());
2215 void ScrollView::UpdateLocalScrollProperties()
2217 if( !mPropertiesUpdated )
2219 // only update local properties if we are not currently handler an event originating from this scroll view
2220 Actor self = Self();
2221 self.GetProperty(mPropertyPrePosition).Get(mScrollPrePosition);
2222 self.GetProperty(mPropertyPosition).Get(mScrollPostPosition);
2226 // private functions
2228 void ScrollView::PreAnimatedScrollSetup()
2230 // mPropertyPrePosition is our unclamped property with wrapping
2231 // mPropertyPosition is our final scroll position after clamping
2233 Actor self = Self();
2235 Vector3 deltaPosition(mScrollPrePosition);
2236 mDomainOffset += deltaPosition - mScrollPostPosition;
2237 Self().SetProperty(mPropertyDomainOffset, mDomainOffset);
2240 mScrollPostScale = GetPropertyScale();
2242 // Update Actor position with this wrapped value.
2245 mScrollPreScale = mScrollPostScale;
2246 mScrollPreRotation = mScrollPostRotation;
2248 // animated scroll needs update notification
2249 SetScrollUpdateNotification(true);
2252 void ScrollView::FinaliseAnimatedScroll()
2254 // TODO - common animation finishing code in here
2257 void ScrollView::AnimateInternalXTo( float position, float duration, AlphaFunction alpha )
2259 StopAnimation(mInternalXAnimation);
2261 if( duration > Math::MACHINE_EPSILON_10 )
2263 Actor self = Self();
2264 mInternalXAnimation = Animation::New(duration);
2265 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2266 mInternalXAnimation.AnimateTo( Property(self, mPropertyPrePosition, 0), position, alpha, duration);
2267 mInternalXAnimation.Play();
2269 // erase current state flags
2270 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2271 // add internal animation state flag
2272 mScrollStateFlags |= AnimatingInternalX;
2276 void ScrollView::AnimateInternalYTo( float position, float duration, AlphaFunction alpha )
2278 StopAnimation(mInternalYAnimation);
2280 if( duration > Math::MACHINE_EPSILON_10 )
2282 Actor self = Self();
2283 mInternalYAnimation = Animation::New(duration);
2284 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2285 mInternalYAnimation.AnimateTo( Property(self, mPropertyPrePosition, 1), position, alpha, TimePeriod(duration));
2286 mInternalYAnimation.Play();
2288 // erase current state flags
2289 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2290 // add internal animation state flag
2291 mScrollStateFlags |= AnimatingInternalY;
2295 void ScrollView::OnScrollAnimationFinished( Animation& source )
2297 // Guard against destruction during signal emission
2298 // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished()
2299 Toolkit::ScrollView handle( GetOwner() );
2301 bool scrollingFinished = false;
2303 // update our local scroll positions
2304 UpdateLocalScrollProperties();
2305 mPropertiesUpdated = true;
2307 if( source == mSnapAnimation )
2309 // generic snap animation used for scaling and rotation
2310 mSnapAnimation.Reset();
2313 if( source == mInternalXAnimation )
2315 if( !(mScrollStateFlags & AnimatingInternalY) )
2317 scrollingFinished = true;
2319 mInternalXAnimation.Reset();
2320 // wrap pre scroll x position and set it
2323 const RulerDomain rulerDomain = mRulerX->GetDomain();
2324 mScrollPrePosition.x = -WrapInDomain(-mScrollPrePosition.x, rulerDomain.min, rulerDomain.max);
2325 handle.SetProperty(mPropertyPrePosition, mScrollPrePosition);
2327 SnapInternalXTo(mScrollPostPosition.x);
2330 if( source == mInternalYAnimation )
2332 if( !(mScrollStateFlags & AnimatingInternalX) )
2334 scrollingFinished = true;
2336 mInternalYAnimation.Reset();
2339 // wrap pre scroll y position and set it
2340 const RulerDomain rulerDomain = mRulerY->GetDomain();
2341 mScrollPrePosition.y = -WrapInDomain(-mScrollPrePosition.y, rulerDomain.min, rulerDomain.max);
2342 handle.SetProperty(mPropertyPrePosition, mScrollPrePosition);
2344 SnapInternalYTo(mScrollPostPosition.y);
2347 if(scrollingFinished)
2349 HandleSnapAnimationFinished();
2351 mPropertiesUpdated = false;
2354 void ScrollView::OnSnapInternalPositionFinished( Animation& source )
2356 Actor self = Self();
2357 UpdateLocalScrollProperties();
2358 if( source == mInternalXAnimation )
2360 // clear internal x animation flags
2361 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2362 mInternalXAnimation.Reset();
2363 WrapPosition(mScrollPrePosition);
2365 if( source == mInternalYAnimation )
2367 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2368 mInternalYAnimation.Reset();
2369 WrapPosition(mScrollPrePosition);
2373 void ScrollView::SnapInternalXTo(float position)
2375 Actor self = Self();
2377 StopAnimation(mInternalXAnimation);
2379 // erase current state flags
2380 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2382 // if internal x not equal to inputed parameter, animate it
2383 float duration = std::min(fabsf((position - mScrollPrePosition.x) / mMaxOvershoot.x) * mSnapOvershootDuration, mSnapOvershootDuration);
2384 if( duration > Math::MACHINE_EPSILON_1 )
2386 mInternalXAnimation = Animation::New(duration);
2387 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2388 mInternalXAnimation.AnimateTo(Property(self, mPropertyPrePosition, 0), position);
2389 mInternalXAnimation.Play();
2391 // add internal animation state flag
2392 mScrollStateFlags |= SnappingInternalX;
2396 void ScrollView::SnapInternalYTo(float position)
2398 Actor self = Self();
2400 StopAnimation(mInternalYAnimation);
2402 // erase current state flags
2403 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2405 // if internal y not equal to inputed parameter, animate it
2406 float duration = std::min(fabsf((position - mScrollPrePosition.y) / mMaxOvershoot.y) * mSnapOvershootDuration, mSnapOvershootDuration);
2407 if( duration > Math::MACHINE_EPSILON_1 )
2409 mInternalYAnimation = Animation::New(duration);
2410 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2411 mInternalYAnimation.AnimateTo(Property(self, mPropertyPrePosition, 1), position);
2412 mInternalYAnimation.Play();
2414 // add internal animation state flag
2415 mScrollStateFlags |= SnappingInternalY;
2419 void ScrollView::GestureStarted()
2421 // we handle the first gesture.
2422 // if we're currently doing a gesture and receive another
2423 // we continue and combine the effects of the gesture instead of reseting.
2424 if(mGestureStackDepth++==0)
2426 Actor self = Self();
2427 StopTouchDownTimer();
2429 mPanDelta = Vector3::ZERO;
2430 mScaleDelta = Vector3::ONE;
2431 mRotationDelta = 0.0f;
2432 mLastVelocity = Vector2(0.0f, 0.0f);
2434 // inform application that animated scroll has been interrupted
2437 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignalV2 4 [%.2f, %.2f]", this, GetCurrentScrollPosition().x, GetCurrentScrollPosition().y);
2438 ScrollingStopped(GetCurrentScrollPosition());
2440 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignalV2 2 [%.2f, %.2f]", this, GetCurrentScrollPosition().x, GetCurrentScrollPosition().y);
2441 ScrollingStarted(GetCurrentScrollPosition());
2445 void ScrollView::GestureContinuing(const Vector2& panDelta, const Vector2& scaleDelta, float rotationDelta)
2447 mPanDelta.x+= panDelta.x;
2448 mPanDelta.y+= panDelta.y;
2449 mScaleDelta.x*= scaleDelta.x;
2450 mScaleDelta.y*= scaleDelta.y;
2451 mRotationDelta+= rotationDelta;
2453 // Save the velocity, there is a bug in PanGesture
2454 // Whereby the Gesture::Finished's velocity is either:
2455 // NaN (due to time delta of zero between the last two events)
2456 // or 0 (due to position being the same between the last two events)
2458 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
2459 // appears mostly horizontal or mostly vertical respectively.
2462 mLockAxis = GetLockAxis(mPanDelta.GetVectorXY(), mLockAxis, mAxisAutoLockGradient);
2463 } // end if mAxisAutoLock
2466 void ScrollView::ScrollingStarted( const Vector3& position )
2468 Self().SetProperty(mPropertyScrolling, true);
2470 mLockAxis = LockPossible;
2472 mScrollStartedSignalV2.Emit( position );
2475 void ScrollView::ScrollingStopped( const Vector3& position )
2477 Self().SetProperty(mPropertyScrolling, false);
2479 SetScrollUpdateNotification(false);
2481 mScrollCompletedSignalV2.Emit( position );
2484 // TODO: Upgrade to use a more powerful gesture detector (one that supports multiple touches on pan - so works as pan and flick gesture)
2485 // TODO: Reimplement Scaling (pinching 2+ points)
2486 // TODO: Reimplment Rotation (pinching 2+ points)
2487 // BUG: Gesture::Finished doesn't always return velocity on release (due to
2488 // timeDelta between last two events being 0 sometimes, or posiiton being the same)
2489 void ScrollView::OnPan(PanGesture gesture)
2491 // Guard against destruction during signal emission
2492 // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx()
2493 Actor self( Self() );
2497 // If another callback on the same original signal disables sensitivity,
2498 // this callback will still be called, so we must suppress it.
2501 UpdateLocalScrollProperties();
2502 mPropertiesUpdated = true;
2504 // translate Gesture input to get useful data...
2505 switch(gesture.state)
2507 case Gesture::Started:
2511 self.SetProperty( mPropertyPanning, true );
2512 self.SetProperty( mPropertyScrollStartPagePosition, Vector3(gesture.position.x, gesture.position.y, 0.0f) );
2514 UpdateMainInternalConstraint();
2518 case Gesture::Continuing:
2520 GestureContinuing(gesture.screenDisplacement, Vector2::ZERO, 0.0f);
2524 case Gesture::Finished:
2525 case Gesture::Cancelled:
2527 mLastVelocity = gesture.velocity;
2529 self.SetProperty( mPropertyPanning, false );
2531 if( mScrollMainInternalPrePositionConstraint )
2533 self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
2535 mGestureStackDepth--;
2536 if(mGestureStackDepth==0)
2543 case Gesture::Possible:
2544 case Gesture::Clear:
2546 // Nothing to do, not needed.
2549 } // end switch(gesture.state)
2551 // can no longer guarantee local properties are latest
2552 mPropertiesUpdated = false;
2555 void ScrollView::UpdateTransform()
2557 // TODO: notify clamps using property notifications (or see if we need this, can deprecate it)
2560 void ScrollView::FinishTransform()
2562 // at this stage internal x and x scroll position should have followed prescroll position exactly
2563 Actor self = Self();
2565 PreAnimatedScrollSetup();
2567 bool animating = SnapWithVelocity(mLastVelocity * 1000.0f);
2571 // if not animating, then this pan has completed right now.
2572 SetScrollUpdateNotification(false);
2574 Self().SetProperty(mPropertyScrolling, false);
2576 if( fabs(mScrollPrePosition.x - mScrollTargetPosition.x) > Math::MACHINE_EPSILON_10 )
2578 SnapInternalXTo(mScrollTargetPosition.x);
2580 if( fabs(mScrollPrePosition.y - mScrollTargetPosition.y) > Math::MACHINE_EPSILON_10 )
2582 SnapInternalYTo(mScrollTargetPosition.y);
2584 if( !(mScrollStateFlags & SNAP_ANIMATION_FLAGS) )
2586 // set final position
2587 WrapPosition(mScrollTargetPosition);
2588 mScrollPrePosition = mScrollTargetPosition;
2589 self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
2591 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignalV2 5 [%.2f, %.2f]", this, GetCurrentScrollPosition().x, GetCurrentScrollPosition().y);
2592 ScrollingStopped(GetCurrentScrollPosition());
2596 Vector3 ScrollView::GetOvershoot(Vector3& position) const
2598 Vector3 size = Self().GetCurrentSize();
2601 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2602 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2604 if(mRulerX->IsEnabled() && rulerDomainX.enabled)
2606 const float left = rulerDomainX.min - position.x;
2607 const float right = size.width - rulerDomainX.max - position.x;
2614 overshoot.x = right;
2618 if(mRulerY->IsEnabled() && rulerDomainY.enabled)
2620 const float top = rulerDomainY.min - position.y;
2621 const float bottom = size.height - rulerDomainY.max - position.y;
2628 overshoot.y = bottom;
2635 bool ScrollView::OnAccessibilityPan(PanGesture gesture)
2637 // Keep track of whether this is an AccessibilityPan
2638 mInAccessibilityPan = true;
2640 mInAccessibilityPan = false;
2645 void ScrollView::ClampPosition(Vector3& position) const
2647 ClampState3 clamped;
2648 ClampPosition(position, clamped);
2651 void ScrollView::ClampPosition(Vector3& position, ClampState3 &clamped) const
2653 Vector3 size = Self().GetCurrentSize();
2655 // determine size of viewport relative to current scaled size.
2656 // e.g. if you're zoomed in 200%, then each pixel on screen is only 0.5 pixels on subject.
2657 if(fabsf(mScrollPostScale.x) > Math::MACHINE_EPSILON_0)
2659 size.x /= mScrollPostScale.x;
2662 if(fabsf(mScrollPostScale.y) > Math::MACHINE_EPSILON_0)
2664 size.y /= mScrollPostScale.y;
2667 position.x = -mRulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system.
2668 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.
2670 clamped.z = NotClamped;
2673 void ScrollView::WrapPosition(Vector3& position) const
2677 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2678 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2680 if(mRulerX->IsEnabled())
2682 position.x = -WrapInDomain(-position.x, rulerDomainX.min, rulerDomainX.max);
2685 if(mRulerY->IsEnabled())
2687 position.y = -WrapInDomain(-position.y, rulerDomainY.min, rulerDomainY.max);
2692 void ScrollView::ClampScale(Vector3& scale) const
2694 ClampState3 clamped;
2695 ClampScale(scale, clamped);
2698 void ScrollView::ClampScale(Vector3& scale, ClampState3 &clamped) const
2700 scale.x = mRulerScaleX->Clamp(scale.x, 0.0f, 1.0f, clamped.x);
2701 scale.y = mRulerScaleY->Clamp(scale.y, 0.0f, 1.0f, clamped.y);
2702 clamped.z = NotClamped;
2705 void ScrollView::UpdateMainInternalConstraint()
2707 // TODO: Only update the constraints which have changed, rather than remove all and add all again.
2708 // Requires a dali-core ApplyConstraintAt, or a ReplaceConstraint. The former is probably more flexible.
2709 Actor self = Self();
2710 PanGestureDetector detector( GetPanGestureDetector() );
2712 if(mScrollMainInternalPositionConstraint)
2714 self.RemoveConstraint(mScrollMainInternalPositionConstraint);
2715 self.RemoveConstraint(mScrollMainInternalDeltaConstraint);
2716 self.RemoveConstraint(mScrollMainInternalFinalConstraint);
2717 self.RemoveConstraint(mScrollMainInternalRelativeConstraint);
2718 self.RemoveConstraint(mScrollMainInternalXConstraint);
2719 self.RemoveConstraint(mScrollMainInternalYConstraint);
2721 if( mScrollMainInternalPrePositionConstraint )
2723 self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
2726 // TODO: It's probably better to use a local displacement value as this will give a displacement when scrolling just commences
2727 // but we need to make sure than the gesture system gives displacement since last frame (60Hz), not displacement since last touch event (90Hz).
2729 // 1. First calculate the pre-position (this is the scroll position if no clamping has taken place)
2730 Vector2 initialPanMask = Vector2(mRulerX->IsEnabled() ? 1.0f : 0.0f, mRulerY->IsEnabled() ? 1.0f : 0.0f);
2732 if( mLockAxis == LockVertical )
2734 initialPanMask.y = 0.0f;
2736 else if( mLockAxis == LockHorizontal )
2738 initialPanMask.x = 0.0f;
2740 Constraint constraint;
2744 constraint = Constraint::New<Vector3>( mPropertyPrePosition,
2745 Source( detector, PanGestureDetector::LOCAL_POSITION ),
2746 Source( detector, PanGestureDetector::LOCAL_DISPLACEMENT ),
2747 Source( self, Actor::SIZE ),
2748 InternalPrePositionConstraint( initialPanMask, mAxisAutoLock, mAxisAutoLockGradient, mLockAxis, mMaxOvershoot, mRulerX->GetDomain(), mRulerY->GetDomain() ) );
2749 mScrollMainInternalPrePositionConstraint = self.ApplyConstraint( constraint );
2752 // 2. Second calculate the clamped position (actual position)
2753 constraint = Constraint::New<Vector3>( mPropertyPosition,
2754 LocalSource( mPropertyPrePosition ),
2755 LocalSource( mPropertyPositionMin ),
2756 LocalSource( mPropertyPositionMax ),
2757 Source( self, Actor::SIZE ),
2758 InternalPositionConstraint( mRulerX->GetDomain(),
2759 mRulerY->GetDomain(), mWrapMode ) );
2760 mScrollMainInternalPositionConstraint = self.ApplyConstraint( constraint );
2762 constraint = Constraint::New<Vector3>( mPropertyPositionDelta,
2763 LocalSource( mPropertyPosition ),
2764 LocalSource( mPropertyDomainOffset ),
2765 InternalPositionDeltaConstraint );
2766 mScrollMainInternalDeltaConstraint = self.ApplyConstraint( constraint );
2768 constraint = Constraint::New<Vector3>( mPropertyFinal,
2769 LocalSource( mPropertyPosition ),
2770 LocalSource( mPropertyOvershootX ),
2771 LocalSource( mPropertyOvershootY ),
2772 InternalFinalConstraint( FinalDefaultAlphaFunction,
2773 FinalDefaultAlphaFunction ) );
2774 mScrollMainInternalFinalConstraint = self.ApplyConstraint( constraint );
2776 constraint = Constraint::New<Vector3>( mPropertyRelativePosition,
2777 LocalSource( mPropertyPosition ),
2778 LocalSource( mPropertyPositionMin ),
2779 LocalSource( mPropertyPositionMax ),
2780 LocalSource( Actor::SIZE ),
2781 InternalRelativePositionConstraint );
2782 mScrollMainInternalRelativeConstraint = self.ApplyConstraint( constraint );
2784 constraint = Constraint::New<float>( mPropertyX,
2785 LocalSource( mPropertyPrePosition ),
2786 InternalXConstraint );
2787 mScrollMainInternalXConstraint = self.ApplyConstraint( constraint );
2789 constraint = Constraint::New<float>( mPropertyY,
2790 LocalSource( mPropertyPrePosition ),
2791 InternalYConstraint );
2792 mScrollMainInternalYConstraint = self.ApplyConstraint( constraint );
2794 // When panning we want to make sure overshoot values are affected by pre position and post position
2795 SetOvershootConstraintsEnabled(!mWrapMode);
2798 void ScrollView::SetOvershootConstraintsEnabled(bool enabled)
2800 Actor self( Self() );
2801 // remove and reset, it may now be in wrong order with the main internal constraints
2802 if( mScrollMainInternalOvershootXConstraint )
2804 self.RemoveConstraint(mScrollMainInternalOvershootXConstraint);
2805 mScrollMainInternalOvershootXConstraint.Reset();
2806 self.RemoveConstraint(mScrollMainInternalOvershootYConstraint);
2807 mScrollMainInternalOvershootYConstraint.Reset();
2811 Constraint constraint = Constraint::New<float>( mPropertyOvershootX,
2812 LocalSource( mPropertyPrePosition ),
2813 LocalSource( mPropertyPosition ),
2814 LocalSource( mPropertyCanScrollHorizontal ),
2815 OvershootXConstraint(mMaxOvershoot.x) );
2816 mScrollMainInternalOvershootXConstraint = self.ApplyConstraint( constraint );
2818 constraint = Constraint::New<float>( mPropertyOvershootY,
2819 LocalSource( mPropertyPrePosition ),
2820 LocalSource( mPropertyPosition ),
2821 LocalSource( mPropertyCanScrollVertical ),
2822 OvershootYConstraint(mMaxOvershoot.y) );
2823 mScrollMainInternalOvershootYConstraint = self.ApplyConstraint( constraint );
2827 self.SetProperty(mPropertyOvershootX, 0.0f);
2828 self.SetProperty(mPropertyOvershootY, 0.0f);
2832 void ScrollView::SetInternalConstraints()
2834 // Internal constraints (applied to target ScrollBase Actor itself) /////////
2835 UpdateMainInternalConstraint();
2837 // User definable constraints to apply to all child actors //////////////////
2838 Actor self = Self();
2840 // LocalSource - The Actors to be moved.
2841 // self - The ScrollView
2843 // Apply some default constraints to ScrollView.
2844 // Movement + Scaling + Wrap function
2846 Constraint constraint;
2848 // MoveScaledActor (scrolling/zooming)
2849 constraint = Constraint::New<Vector3>( Actor::POSITION,
2850 Source( self, mPropertyPosition ),
2851 Source( self, mPropertyScale ),
2852 MoveScaledActorConstraint );
2853 constraint.SetRemoveAction(Constraint::Discard);
2854 ApplyConstraintToBoundActors(constraint);
2856 // ScaleActor (scrolling/zooming)
2857 constraint = Constraint::New<Vector3>( Actor::SCALE,
2858 Source( self, mPropertyScale ),
2859 ScaleActorConstraint );
2860 constraint.SetRemoveAction(Constraint::Discard);
2861 ApplyConstraintToBoundActors(constraint);
2863 // WrapActor (wrap functionality)
2864 constraint = Constraint::New<Vector3>( Actor::POSITION,
2865 LocalSource( Actor::SCALE ),
2866 LocalSource( Actor::ANCHOR_POINT ),
2867 LocalSource( Actor::SIZE ),
2868 Source( self, mPropertyPositionMin ),
2869 Source( self, mPropertyPositionMax ),
2870 Source( self, mPropertyWrap ),
2871 WrapActorConstraint );
2872 constraint.SetRemoveAction(Constraint::Discard);
2873 ApplyConstraintToBoundActors(constraint);
2876 } // namespace Internal
2878 } // namespace Toolkit