2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.h>
22 #include <cstring> // for strcmp
23 #include <dali/public-api/animation/constraints.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/events/mouse-wheel-event.h>
26 #include <dali/public-api/events/touch-event.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/devel-api/object/type-registry-helper.h>
29 #include <dali/integration-api/debug.h>
32 #include <dali-toolkit/public-api/controls/scroll-bar/scroll-bar.h>
33 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view.h>
34 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view-constraints.h>
35 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
36 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-effect-impl.h>
38 //#define ENABLED_SCROLL_STATE_LOGGING
40 #ifdef ENABLED_SCROLL_STATE_LOGGING
41 #define DALI_LOG_SCROLL_STATE(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d " format "\n", __PRETTY_FUNCTION__, __LINE__, ## args)
43 #define DALI_LOG_SCROLL_STATE(format, args...)
46 // TODO: Change to two class system:
47 // 1. DraggableActor (is an actor which can be dragged anywhere, can be set to range using the ruler)
48 // 2. ScrollView (contains a draggable actor that can a) be dragged in the negative X, and Y domain, b) has a hitArea for touches)
49 // TODO: external components (page and status overlays).
51 // TODO: upgrade Vector2/3 to support returning Unit vectors, normals, & cross product (dot product is already provided)
57 const float DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f); ///< Default Drag-Release animation time.
58 const float DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f); ///< Default Drag-Flick animation time.
59 const float DEFAULT_SNAP_OVERSHOOT_DURATION(0.5f); ///< Default Overshoot snapping animation time.
60 const float DEFAULT_MAX_OVERSHOOT(100.0f); ///< Default maximum allowed overshoot in pixels
62 const float DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f); ///< Default Axis-AutoLock gradient threshold. default is 0.36:1 (20 degrees)
63 const float DEFAULT_FRICTION_COEFFICIENT(1.0f); ///< Default Friction Co-efficient. (in stage diagonals per second)
64 const float DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f); ///< Default Flick speed coefficient (multiples input touch velocity)
65 const float DEFAULT_MAX_FLICK_SPEED(3.0f); ///< Default Maximum flick speed. (in stage diagonals per second)
67 const Vector2 DEFAULT_MIN_FLICK_DISTANCE(30.0f, 30.0f); ///< minimum distance for pan before flick allowed
68 const float DEFAULT_MIN_FLICK_SPEED_THRESHOLD(500.0f); ///< Minimum pan speed required for flick in pixels/s
69 const float FREE_FLICK_SPEED_THRESHOLD = 200.0f; ///< Free-Flick threshold in pixels/ms
70 const float AUTOLOCK_AXIS_MINIMUM_DISTANCE2 = 100.0f; ///< Auto-lock axis after minimum distance squared.
71 const float FLICK_ORTHO_ANGLE_RANGE = 75.0f; ///< degrees. (if >45, then supports diagonal flicking)
72 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.
73 const unsigned long MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET( 150u );
74 const float TOUCH_DOWN_TIMER_INTERVAL = 100.0f;
75 const float DEFAULT_SCROLL_UPDATE_DISTANCE( 30.0f ); ///< Default distance to travel in pixels for scroll update signal
77 const std::string INTERNAL_MAX_POSITION_PROPERTY_NAME( "internal-max-position" );
79 // Helpers ////////////////////////////////////////////////////////////////////////////////////////
82 * Find the vector (distance) from (a) to (b)
83 * in domain (start) to (end)
84 * (\ / start) (\ / end)
87 * @note assumes both (a) and (b) are already with the domain
90 * @param[in] a the current point
91 * @param[in] b the target point
92 * @param[in] start the start of the domain
93 * @param[in] end the end of the domain
94 * @param[in] bias whether to only take the right direction or the left direction,
95 * or the shortest direction.
96 * @return the shortest direction and distance
98 float VectorInDomain(float a, float b, float start, float end, Dali::Toolkit::DirectionBias bias)
100 if(bias == Dali::Toolkit::DirectionBiasNone)
102 return ShortestDistanceInDomain( a, b, start, end );
105 float size = end-start;
111 if(bias == Dali::Toolkit::DirectionBiasRight) // going right, take the vector.
117 float aRight = a+size;
124 if(bias == Dali::Toolkit::DirectionBiasLeft) // going left, take the vector.
130 float aLeft = a-size;
137 * Returns the position of the anchor within actor
139 * @param actor The Actor
140 * @param anchor The Anchor point of interest.
141 * @return The position of the Anchor
143 Vector3 GetPositionOfAnchor(Actor &actor, const Vector3 &anchor)
145 Vector3 childPosition = actor.GetCurrentPosition();
146 Vector3 childAnchor = - actor.GetCurrentAnchorPoint() + anchor;
147 Vector3 childSize = actor.GetCurrentSize();
149 return childPosition + childAnchor * childSize;
152 // AlphaFunctions /////////////////////////////////////////////////////////////////////////////////
154 float FinalDefaultAlphaFunction(float offset)
156 return offset * 0.5f;
160 * ConstantDecelerationAlphaFunction
161 * Newtoninan distance for constant deceleration
162 * v = 1 - t, s = t - 1/2 t^2
163 * when t = 0, s = 0.0 (min distance)
164 * when t = 1, s = 0.5 (max distance)
165 * progress = s / (max-min) = 2t - t^2
167 * @param[in] offset The input progress
168 * @return The output progress
170 float ConstantDecelerationAlphaFunction(float progress)
172 return progress * 2.0f - progress * progress;
175 // Internal Constraints ///////////////////////////////////////////////////////////////////////////
178 * Internal Relative position Constraint
179 * Generates the relative position value of the scroll view
180 * based on the absolute position, and it's relation to the
181 * scroll domain. This is a value from 0.0f to 1.0f in each
182 * scroll position axis.
184 void InternalRelativePositionConstraint( Vector2& relativePosition, const PropertyInputContainer& inputs)
186 Vector2 position = -inputs[0]->GetVector2();
187 const Vector2& min = inputs[1]->GetVector2();
188 const Vector2& max = inputs[2]->GetVector2();
189 const Vector3& size = inputs[3]->GetVector3();
191 position.x = WrapInDomain(position.x, min.x, max.x);
192 position.y = WrapInDomain(position.y, min.y, max.y);
194 Vector2 domainSize = (max - min) - size.GetVectorXY();
196 relativePosition.x = domainSize.x > Math::MACHINE_EPSILON_1 ? fabsf((position.x - min.x) / domainSize.x) : 0.0f;
197 relativePosition.y = domainSize.y > Math::MACHINE_EPSILON_1 ? fabsf((position.y - min.y) / domainSize.y) : 0.0f;
201 * Internal scroll domain Constraint
202 * Generates the scroll domain of the scroll view.
204 void InternalScrollDomainConstraint( Vector2& scrollDomain, const PropertyInputContainer& inputs)
206 const Vector2& min = inputs[0]->GetVector2();
207 const Vector2& max = inputs[1]->GetVector2();
208 const Vector3& size = inputs[2]->GetVector3();
210 scrollDomain = (max - min) - size.GetVectorXY();
214 * Internal maximum scroll position Constraint
215 * Generates the maximum scroll position of the scroll view.
217 void InternalPrePositionMaxConstraint( Vector2& scrollMax, const PropertyInputContainer& inputs)
219 const Vector2& max = inputs[0]->GetVector2();
220 const Vector3& size = inputs[1]->GetVector3();
222 scrollMax = max - size.GetVectorXY();
225 } // unnamed namespace
241 return Toolkit::ScrollView::New();
244 // Setup properties, signals and actions using the type-registry.
245 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ScrollView, Toolkit::Scrollable, Create )
247 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-position", VECTOR2, SCROLL_POSITION)
248 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position", VECTOR2, SCROLL_PRE_POSITION)
249 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position-x", SCROLL_PRE_POSITION_X, SCROLL_PRE_POSITION, 0)
250 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position-y", SCROLL_PRE_POSITION_Y, SCROLL_PRE_POSITION, 1)
251 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position-max", VECTOR2, SCROLL_PRE_POSITION_MAX)
252 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position-max-x", SCROLL_PRE_POSITION_MAX_X, SCROLL_PRE_POSITION_MAX, 0)
253 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position-max-y", SCROLL_PRE_POSITION_MAX_Y, SCROLL_PRE_POSITION_MAX, 1)
254 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "overshoot-x", FLOAT, OVERSHOOT_X)
255 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "overshoot-y", FLOAT, OVERSHOOT_Y)
256 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-final", VECTOR2, SCROLL_FINAL)
257 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-final-x", SCROLL_FINAL_X, SCROLL_FINAL,0)
258 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-final-y", SCROLL_FINAL_Y, SCROLL_FINAL,1)
259 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "wrap", BOOLEAN, WRAP)
260 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "panning", BOOLEAN, PANNING)
261 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrolling", BOOLEAN, SCROLLING)
262 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-domain-size", VECTOR2, SCROLL_DOMAIN_SIZE)
263 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-domain-size-x", SCROLL_DOMAIN_SIZE_X, SCROLL_DOMAIN_SIZE, 0)
264 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-domain-size-y", SCROLL_DOMAIN_SIZE_Y, SCROLL_DOMAIN_SIZE, 1)
265 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-domain-offset", VECTOR2, SCROLL_DOMAIN_OFFSET)
266 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-position-delta", VECTOR2, SCROLL_POSITION_DELTA)
267 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "start-page-position", VECTOR3, START_PAGE_POSITION)
269 DALI_SIGNAL_REGISTRATION( Toolkit, ScrollView, "value-changed", SIGNAL_SNAP_STARTED )
271 DALI_TYPE_REGISTRATION_END()
274 * Returns whether to lock scrolling to a particular axis
276 * @param[in] panDelta Distance panned since gesture started
277 * @param[in] currentLockAxis The current lock axis value
278 * @param[in] lockGradient How quickly to lock to a particular axis
280 * @return The new axis lock state
282 ScrollView::LockAxis GetLockAxis(const Vector2& panDelta, ScrollView::LockAxis currentLockAxis, float lockGradient)
284 if(panDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 &&
285 currentLockAxis == ScrollView::LockPossible)
287 float dx = fabsf(panDelta.x);
288 float dy = fabsf(panDelta.y);
289 if(dx * lockGradient >= dy)
291 // 0.36:1 gradient to the horizontal (deviate < 20 degrees)
292 currentLockAxis = ScrollView::LockVertical;
294 else if(dy * lockGradient > dx)
296 // 0.36:1 gradient to the vertical (deviate < 20 degrees)
297 currentLockAxis = ScrollView::LockHorizontal;
301 currentLockAxis = ScrollView::LockNone;
304 return currentLockAxis;
308 * Internal Pre-Position Property Constraint.
310 * Generates position property based on current position + gesture displacement.
311 * Or generates position property based on positionX/Y.
312 * Note: This is the position prior to any clamping at scroll boundaries.
314 struct InternalPrePositionConstraint
316 InternalPrePositionConstraint(const Vector2& initialPanPosition,
317 const Vector2& initialPanMask,
319 float axisAutoLockGradient,
320 ScrollView::LockAxis initialLockAxis,
321 const Vector2& maxOvershoot,
322 const RulerDomain& domainX, const RulerDomain& domainY)
323 : mLocalStart(initialPanPosition),
324 mInitialPanMask(initialPanMask),
325 mDomainMin( -domainX.min, -domainY.min ),
326 mDomainMax( -domainX.max, -domainY.max ),
327 mMaxOvershoot(maxOvershoot),
328 mAxisAutoLockGradient(axisAutoLockGradient),
329 mLockAxis(initialLockAxis),
330 mAxisAutoLock(axisAutoLock),
332 mClampX( domainX.enabled ),
333 mClampY( domainY.enabled )
337 void operator()( Vector2& scrollPostPosition, const PropertyInputContainer& inputs )
339 const Vector2& panPosition = inputs[0]->GetVector2();
343 mPrePosition = scrollPostPosition;
344 mCurrentPanMask = mInitialPanMask;
348 // Calculate Deltas...
349 const Vector2& currentPosition = panPosition;
350 Vector2 panDelta( currentPosition - mLocalStart );
352 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
353 // appears mostly horizontal or mostly vertical respectively...
356 mLockAxis = GetLockAxis(panDelta, mLockAxis, mAxisAutoLockGradient);
357 if( mLockAxis == ScrollView::LockVertical )
359 mCurrentPanMask.y = 0.0f;
361 else if( mLockAxis == ScrollView::LockHorizontal )
363 mCurrentPanMask.x = 0.0f;
367 // Restrict deltas based on ruler enable/disable and axis-lock state...
368 panDelta *= mCurrentPanMask;
370 // Perform Position transform based on input deltas...
371 scrollPostPosition = mPrePosition;
372 scrollPostPosition += panDelta;
374 // if no wrapping then clamp preposition to maximum overshoot amount
375 const Vector3& size = inputs[1]->GetVector3();
378 float newXPosition = Clamp(scrollPostPosition.x, (mDomainMax.x + size.x) - mMaxOvershoot.x, mDomainMin.x + mMaxOvershoot.x );
379 if( (newXPosition < scrollPostPosition.x - Math::MACHINE_EPSILON_1)
380 || (newXPosition > scrollPostPosition.x + Math::MACHINE_EPSILON_1) )
382 mPrePosition.x = newXPosition;
383 mLocalStart.x = panPosition.x;
385 scrollPostPosition.x = newXPosition;
389 float newYPosition = Clamp(scrollPostPosition.y, (mDomainMax.y + size.y) - mMaxOvershoot.y, mDomainMin.y + mMaxOvershoot.y );
390 if( (newYPosition < scrollPostPosition.y - Math::MACHINE_EPSILON_1)
391 || (newYPosition > scrollPostPosition.y + Math::MACHINE_EPSILON_1) )
393 mPrePosition.y = newYPosition;
394 mLocalStart.y = panPosition.y;
396 scrollPostPosition.y = newYPosition;
400 Vector2 mPrePosition;
402 Vector2 mInitialPanMask; ///< Initial pan mask (based on ruler settings)
403 Vector2 mCurrentPanMask; ///< Current pan mask that can be altered by axis lock mode.
406 Vector2 mMaxOvershoot;
408 float mAxisAutoLockGradient; ///< Set by ScrollView
409 ScrollView::LockAxis mLockAxis;
411 bool mAxisAutoLock:1; ///< Set by ScrollView
418 * Internal Position Property Constraint.
420 * Generates position property based on pre-position
421 * Note: This is the position after clamping.
422 * (uses result of InternalPrePositionConstraint)
424 struct InternalPositionConstraint
426 InternalPositionConstraint(const RulerDomain& domainX, const RulerDomain& domainY, bool wrap)
427 : mDomainMin( -domainX.min, -domainY.min ),
428 mDomainMax( -domainX.max, -domainY.max ),
429 mClampX( domainX.enabled ),
430 mClampY( domainY.enabled ),
435 void operator()( Vector2& position, const PropertyInputContainer& inputs )
437 position = inputs[0]->GetVector2();
438 const Vector2& size = inputs[3]->GetVector3().GetVectorXY();
439 const Vector2& min = inputs[1]->GetVector2();
440 const Vector2& max = inputs[2]->GetVector2();
444 position.x = -WrapInDomain(-position.x, min.x, max.x);
445 position.y = -WrapInDomain(-position.y, min.y, max.y);
449 // clamp post position to domain
450 position.x = mClampX ? Clamp(position.x, mDomainMax.x + size.x, mDomainMin.x ) : position.x;
451 position.y = mClampY ? Clamp(position.y, mDomainMax.y + size.y, mDomainMin.y ) : position.y;
464 * This constraint updates the X overshoot property using the difference
465 * SCROLL_PRE_POSITION.x and SCROLL_POSITION.x, returning a relative value between 0.0f and 1.0f
467 struct OvershootXConstraint
469 OvershootXConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
471 void operator()( float& current, const PropertyInputContainer& inputs )
473 if( inputs[2]->GetBoolean() )
475 const Vector2& scrollPrePosition = inputs[0]->GetVector2();
476 const Vector2& scrollPostPosition = inputs[1]->GetVector2();
477 float newOvershoot = scrollPrePosition.x - scrollPostPosition.x;
478 current = (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
490 * This constraint updates the Y overshoot property using the difference
491 * SCROLL_PRE_POSITION.y and SCROLL_POSITION.y, returning a relative value between 0.0f and 1.0f
493 struct OvershootYConstraint
495 OvershootYConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
497 void operator()( float& current, const PropertyInputContainer& inputs )
499 if( inputs[2]->GetBoolean() )
501 const Vector2& scrollPrePosition = inputs[0]->GetVector2();
502 const Vector2& scrollPostPosition = inputs[1]->GetVector2();
503 float newOvershoot = scrollPrePosition.y - scrollPostPosition.y;
504 current = (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
516 * Internal Position-Delta Property Constraint.
518 * Generates position-delta property based on scroll-position + scroll-offset properties.
520 void InternalPositionDeltaConstraint( Vector2& current, const PropertyInputContainer& inputs )
522 const Vector2& scrollPosition = inputs[0]->GetVector2();
523 const Vector2& scrollOffset = inputs[1]->GetVector2();
525 current = scrollPosition + scrollOffset;
529 * Internal Final Position Constraint
530 * The position of content is:
531 * of scroll-position + f(scroll-overshoot)
532 * where f(...) function defines how overshoot
533 * should affect final-position.
535 struct InternalFinalConstraint
537 InternalFinalConstraint(AlphaFunctionPrototype functionX,
538 AlphaFunctionPrototype functionY)
539 : mFunctionX(functionX),
540 mFunctionY(functionY)
544 void operator()( Vector2& current, const PropertyInputContainer& inputs )
546 const float& overshootx = inputs[1]->GetFloat();
547 const float& overshooty = inputs[2]->GetFloat();
548 Vector2 offset( mFunctionX(overshootx),
549 mFunctionY(overshooty) );
551 current = inputs[0]->GetVector2() - offset;
554 AlphaFunctionPrototype mFunctionX;
555 AlphaFunctionPrototype mFunctionY;
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()
581 : ScrollBase( ControlBehaviour( REQUIRES_MOUSE_WHEEL_EVENTS ) ), // Enable size negotiation
583 mGestureStackDepth(0),
584 mScrollStateFlags(0),
585 mLockAxis(LockPossible),
586 mScrollUpdateDistance(DEFAULT_SCROLL_UPDATE_DISTANCE),
587 mMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT),
588 mUserMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT),
589 mSnapOvershootDuration(DEFAULT_SNAP_OVERSHOOT_DURATION),
590 mSnapOvershootAlphaFunction(AlphaFunction::EASE_OUT),
591 mSnapDuration(DEFAULT_SLOW_SNAP_ANIMATION_DURATION),
592 mSnapAlphaFunction(AlphaFunction::EASE_OUT),
593 mMinFlickDistance(DEFAULT_MIN_FLICK_DISTANCE),
594 mFlickSpeedThreshold(DEFAULT_MIN_FLICK_SPEED_THRESHOLD),
595 mFlickDuration(DEFAULT_FAST_SNAP_ANIMATION_DURATION),
596 mFlickAlphaFunction(AlphaFunction::EASE_OUT),
597 mAxisAutoLockGradient(DEFAULT_AXIS_AUTO_LOCK_GRADIENT),
598 mFrictionCoefficient(DEFAULT_FRICTION_COEFFICIENT),
599 mFlickSpeedCoefficient(DEFAULT_FLICK_SPEED_COEFFICIENT),
600 mMaxFlickSpeed(DEFAULT_MAX_FLICK_SPEED),
601 mMouseWheelScrollDistanceStep(Vector2::ZERO),
602 mInAccessibilityPan(false),
605 mScrollInterrupted(false),
608 mTouchDownTimeoutReached(false),
609 mActorAutoSnapEnabled(false),
610 mAutoResizeContainerEnabled(false),
612 mAxisAutoLock(false),
614 mDefaultMaxOvershoot(true),
615 mCanScrollHorizontal(true),
616 mCanScrollVertical(true)
620 void ScrollView::OnInitialize()
624 // Internal Actor, used to hide actors from enumerations.
625 // Also actors added to Internal actor appear as overlays e.g. ScrollBar components.
626 mInternalActor = Actor::New();
627 mInternalActor.SetDrawMode(DrawMode::OVERLAY);
628 self.Add(mInternalActor);
630 mInternalActor.SetParentOrigin(ParentOrigin::CENTER);
631 mInternalActor.SetAnchorPoint(AnchorPoint::CENTER);
632 mInternalActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
636 mScrollPostPosition = mScrollPrePosition = Vector2::ZERO;
638 mMouseWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
642 mGestureStackDepth = 0;
644 EnableGestureDetection( Gesture::Type( Gesture::Pan ) );
646 // By default we'll allow the user to freely drag the scroll view,
647 // while disabling the other rulers.
648 RulerPtr ruler = new DefaultRuler();
652 SetOvershootEnabled(true);
654 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, mCanScrollVertical);
655 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, mCanScrollHorizontal);
657 Vector3 size = GetControlSize();
658 UpdatePropertyDomain(size);
659 SetInternalConstraints();
662 void ScrollView::OnControlStageConnection()
664 DALI_LOG_SCROLL_STATE("[0x%X]", this);
668 SetScrollSensitive( false );
669 SetScrollSensitive( true );
671 if(IsOvershootEnabled())
673 // try and make sure property notifications are set
674 EnableScrollOvershoot(true);
678 void ScrollView::OnControlStageDisconnection()
680 DALI_LOG_SCROLL_STATE("[0x%X]", this);
685 ScrollView::~ScrollView()
687 DALI_LOG_SCROLL_STATE("[0x%X]", this);
690 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
692 return mSnapAlphaFunction;
695 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
697 mSnapAlphaFunction = alpha;
700 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
702 return mFlickAlphaFunction;
705 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
707 mFlickAlphaFunction = alpha;
710 float ScrollView::GetScrollSnapDuration() const
712 return mSnapDuration;
715 void ScrollView::SetScrollSnapDuration(float time)
717 mSnapDuration = time;
720 float ScrollView::GetScrollFlickDuration() const
722 return mFlickDuration;
725 void ScrollView::SetScrollFlickDuration(float time)
727 mFlickDuration = time;
730 void ScrollView::ApplyEffect(Toolkit::ScrollViewEffect effect)
732 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
734 // Assertion check to ensure effect doesn't already exist in this scrollview
735 bool effectAlreadyExistsInScrollView(false);
736 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
740 effectAlreadyExistsInScrollView = true;
745 DALI_ASSERT_ALWAYS(!effectAlreadyExistsInScrollView);
747 // add effect to effects list
748 mEffects.push_back(effect);
750 // invoke Attachment request to ScrollView first
751 GetImpl(effect).Attach(self);
754 void ScrollView::RemoveEffect(Toolkit::ScrollViewEffect effect)
756 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
758 // remove effect from effects list
759 bool effectExistedInScrollView(false);
760 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
764 mEffects.erase(iter);
765 effectExistedInScrollView = true;
770 // Assertion check to ensure effect existed.
771 DALI_ASSERT_ALWAYS(effectExistedInScrollView);
773 // invoke Detachment request to ScrollView last
774 GetImpl(effect).Detach(self);
777 void ScrollView::RemoveAllEffects()
779 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
781 for (ScrollViewEffectIter effectIter = mEffects.begin(); effectIter != mEffects.end(); ++effectIter)
783 Toolkit::ScrollViewEffect effect = *effectIter;
785 // invoke Detachment request to ScrollView last
786 GetImpl(effect).Detach(self);
792 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
794 ApplyConstraintToBoundActors(constraint);
797 void ScrollView::RemoveConstraintsFromChildren()
799 RemoveConstraintsFromBoundActors();
802 const RulerPtr ScrollView::GetRulerX() const
807 const RulerPtr ScrollView::GetRulerY() const
812 void ScrollView::SetRulerX(RulerPtr ruler)
816 Vector3 size = GetControlSize();
817 UpdatePropertyDomain(size);
818 UpdateMainInternalConstraint();
821 void ScrollView::SetRulerY(RulerPtr ruler)
825 Vector3 size = GetControlSize();
826 UpdatePropertyDomain(size);
827 UpdateMainInternalConstraint();
830 void ScrollView::UpdatePropertyDomain(const Vector3& size)
833 Vector2 min = mMinScroll;
834 Vector2 max = mMaxScroll;
835 bool scrollPositionChanged = false;
836 bool domainChanged = false;
838 bool canScrollVertical = false;
839 bool canScrollHorizontal = false;
840 UpdateLocalScrollProperties();
841 if(mRulerX->IsEnabled())
843 const Toolkit::RulerDomain& rulerDomain = mRulerX->GetDomain();
844 if( fabsf(min.x - rulerDomain.min) > Math::MACHINE_EPSILON_100
845 || fabsf(max.x - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
847 domainChanged = true;
848 min.x = rulerDomain.min;
849 max.x = rulerDomain.max;
851 // make sure new scroll value is within new domain
852 if( mScrollPrePosition.x < min.x
853 || mScrollPrePosition.x > max.x )
855 scrollPositionChanged = true;
856 mScrollPrePosition.x = Clamp(mScrollPrePosition.x, -(max.x - size.x), -min.x);
859 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.x) > Math::MACHINE_EPSILON_100 )
861 canScrollHorizontal = true;
864 else if( fabs(min.x) > Math::MACHINE_EPSILON_100
865 || fabs(max.x) > Math::MACHINE_EPSILON_100 )
867 // need to reset to 0
868 domainChanged = true;
871 canScrollHorizontal = false;
874 if(mRulerY->IsEnabled())
876 const Toolkit::RulerDomain& rulerDomain = mRulerY->GetDomain();
877 if( fabsf(min.y - rulerDomain.min) > Math::MACHINE_EPSILON_100
878 || fabsf(max.y - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
880 domainChanged = true;
881 min.y = rulerDomain.min;
882 max.y = rulerDomain.max;
884 // make sure new scroll value is within new domain
885 if( mScrollPrePosition.y < min.y
886 || mScrollPrePosition.y > max.y )
888 scrollPositionChanged = true;
889 mScrollPrePosition.y = Clamp(mScrollPrePosition.y, -(max.y - size.y), -min.y);
892 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.y) > Math::MACHINE_EPSILON_100 )
894 canScrollVertical = true;
897 else if( fabs(min.y) > Math::MACHINE_EPSILON_100
898 || fabs(max.y) > Math::MACHINE_EPSILON_100 )
900 // need to reset to 0
901 domainChanged = true;
904 canScrollVertical = false;
907 // avoid setting properties if possible, otherwise this will cause an entire update as well as triggering constraints using each property we update
908 if( mCanScrollVertical != canScrollVertical )
910 mCanScrollVertical = canScrollVertical;
911 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, canScrollVertical);
913 if( mCanScrollHorizontal != canScrollHorizontal )
915 mCanScrollHorizontal = canScrollHorizontal;
916 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, canScrollHorizontal);
918 if( scrollPositionChanged )
920 DALI_LOG_SCROLL_STATE("[0x%X] Domain Changed, setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
921 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
927 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, mMinScroll );
928 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, mMaxScroll );
932 void ScrollView::SetScrollSensitive(bool sensitive)
935 PanGestureDetector panGesture( GetPanGestureDetector() );
937 DALI_LOG_SCROLL_STATE("[0x%X] sensitive: before:[%d] setting[%d]", this, int(mSensitive), int(sensitive));
939 if((!mSensitive) && (sensitive))
941 mSensitive = sensitive;
942 panGesture.Attach(self);
944 else if((mSensitive) && (!sensitive))
946 DALI_LOG_SCROLL_STATE("[0x%X] BEFORE: panning:[%d]", this, int(mPanning));
948 // while the scroll view is panning, the state needs to be reset.
951 PanGesture cancelGesture( Gesture::Cancelled );
952 OnPan( cancelGesture );
955 panGesture.Detach(self);
956 mSensitive = sensitive;
958 mGestureStackDepth = 0;
959 DALI_LOG_SCROLL_STATE("[0x%X] AFTER: panning:[%d]", this, int(mPanning));
963 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
965 mMaxOvershoot.x = overshootX;
966 mMaxOvershoot.y = overshootY;
967 mUserMaxOvershoot = mMaxOvershoot;
968 mDefaultMaxOvershoot = false;
969 UpdateMainInternalConstraint();
972 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
974 mSnapOvershootAlphaFunction = alpha;
977 void ScrollView::SetSnapOvershootDuration(float duration)
979 mSnapOvershootDuration = duration;
982 void ScrollView::SetActorAutoSnap(bool enable)
984 mActorAutoSnapEnabled = enable;
987 void ScrollView::SetAutoResize(bool enable)
989 mAutoResizeContainerEnabled = enable;
990 // TODO: This needs a lot of issues to be addressed before working.
993 bool ScrollView::GetWrapMode() const
998 void ScrollView::SetWrapMode(bool enable)
1001 Self().SetProperty(Toolkit::ScrollView::Property::WRAP, enable);
1004 int ScrollView::GetScrollUpdateDistance() const
1006 return mScrollUpdateDistance;
1009 void ScrollView::SetScrollUpdateDistance(int distance)
1011 mScrollUpdateDistance = distance;
1014 bool ScrollView::GetAxisAutoLock() const
1016 return mAxisAutoLock;
1019 void ScrollView::SetAxisAutoLock(bool enable)
1021 mAxisAutoLock = enable;
1022 UpdateMainInternalConstraint();
1025 float ScrollView::GetAxisAutoLockGradient() const
1027 return mAxisAutoLockGradient;
1030 void ScrollView::SetAxisAutoLockGradient(float gradient)
1032 DALI_ASSERT_DEBUG( gradient >= 0.0f && gradient <= 1.0f );
1033 mAxisAutoLockGradient = gradient;
1034 UpdateMainInternalConstraint();
1037 float ScrollView::GetFrictionCoefficient() const
1039 return mFrictionCoefficient;
1042 void ScrollView::SetFrictionCoefficient(float friction)
1044 DALI_ASSERT_DEBUG( friction > 0.0f );
1045 mFrictionCoefficient = friction;
1048 float ScrollView::GetFlickSpeedCoefficient() const
1050 return mFlickSpeedCoefficient;
1053 void ScrollView::SetFlickSpeedCoefficient(float speed)
1055 mFlickSpeedCoefficient = speed;
1058 Vector2 ScrollView::GetMinimumDistanceForFlick() const
1060 return mMinFlickDistance;
1063 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
1065 mMinFlickDistance = distance;
1068 float ScrollView::GetMinimumSpeedForFlick() const
1070 return mFlickSpeedThreshold;
1073 void ScrollView::SetMinimumSpeedForFlick( float speed )
1075 mFlickSpeedThreshold = speed;
1078 float ScrollView::GetMaxFlickSpeed() const
1080 return mMaxFlickSpeed;
1083 void ScrollView::SetMaxFlickSpeed(float speed)
1085 mMaxFlickSpeed = speed;
1088 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
1090 mMouseWheelScrollDistanceStep = step;
1093 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
1095 return mMouseWheelScrollDistanceStep;
1098 unsigned int ScrollView::GetCurrentPage() const
1100 // in case animation is currently taking place.
1101 Vector2 position = GetPropertyPosition();
1103 Actor self = Self();
1104 unsigned int page = 0;
1105 unsigned int pagesPerVolume = 1;
1106 unsigned int volume = 0;
1108 // if rulerX is enabled, then get page count (columns)
1109 page = mRulerX->GetPageFromPosition(-position.x, mWrapMode);
1110 volume = mRulerY->GetPageFromPosition(-position.y, mWrapMode);
1111 pagesPerVolume = mRulerX->GetTotalPages();
1113 return volume * pagesPerVolume + page;
1116 Vector2 ScrollView::GetCurrentScrollPosition() const
1118 return -GetPropertyPosition();
1121 void ScrollView::SetScrollPosition(const Vector2& position)
1123 mScrollPrePosition = position;
1126 Vector2 ScrollView::GetDomainSize() const
1128 Vector3 size = Self().GetCurrentSize();
1130 const RulerDomain& xDomain = GetRulerX()->GetDomain();
1131 const RulerDomain& yDomain = GetRulerY()->GetDomain();
1134 domainSize.x = xDomain.max - xDomain.min - size.x;
1135 domainSize.y = yDomain.max - yDomain.min - size.y;
1139 void ScrollView::TransformTo(const Vector2& position,
1140 DirectionBias horizontalBias, DirectionBias verticalBias)
1142 TransformTo(position, mSnapDuration, mSnapAlphaFunction, horizontalBias, verticalBias);
1145 void ScrollView::TransformTo(const Vector2& position, float duration, AlphaFunction alpha,
1146 DirectionBias horizontalBias, DirectionBias verticalBias)
1148 // If this is called while the timer is running, then cancel it
1149 StopTouchDownTimer();
1151 Actor self( Self() );
1153 // Guard against destruction during signal emission
1154 // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo()
1155 Toolkit::ScrollView handle( GetOwner() );
1157 DALI_LOG_SCROLL_STATE("[0x%X] pos[%.2f,%.2f], duration[%.2f] bias[%d, %d]",
1158 this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1160 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1161 self.SetProperty( Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(currentScrollPosition) );
1163 if( mScrolling ) // are we interrupting a current scroll?
1165 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
1167 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1168 mScrollCompletedSignal.Emit( currentScrollPosition );
1171 if( mPanning ) // are we interrupting a current pan?
1173 DALI_LOG_SCROLL_STATE("[0x%X] Interrupting Pan, set to false", this );
1175 mGestureStackDepth = 0;
1176 self.SetProperty( Toolkit::ScrollView::Property::PANNING, false );
1178 if( mScrollMainInternalPrePositionConstraint )
1180 mScrollMainInternalPrePositionConstraint.Remove();
1184 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, true);
1187 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1188 mScrollStartedSignal.Emit( currentScrollPosition );
1189 bool animating = AnimateTo(-position,
1190 Vector2::ONE * duration,
1199 // if not animating, then this pan has completed right now.
1200 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
1203 // If we have no duration, then in the next update frame, we will be at the position specified as we just set.
1204 // In this scenario, we cannot return the currentScrollPosition as this is out-of-date and should instead return the requested final position
1205 Vector2 completedPosition( currentScrollPosition );
1206 if( duration <= Math::MACHINE_EPSILON_10 )
1208 completedPosition = position;
1211 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 2 [%.2f, %.2f]", this, completedPosition.x, completedPosition.y);
1212 SetScrollUpdateNotification(false);
1213 mScrollCompletedSignal.Emit( completedPosition );
1217 void ScrollView::ScrollTo(const Vector2& position)
1219 ScrollTo(position, mSnapDuration );
1222 void ScrollView::ScrollTo(const Vector2& position, float duration)
1224 ScrollTo(position, duration, DirectionBiasNone, DirectionBiasNone);
1227 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha)
1229 ScrollTo(position, duration, alpha, DirectionBiasNone, DirectionBiasNone);
1232 void ScrollView::ScrollTo(const Vector2& position, float duration,
1233 DirectionBias horizontalBias, DirectionBias verticalBias)
1235 ScrollTo(position, duration, mSnapAlphaFunction, horizontalBias, verticalBias);
1238 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha,
1239 DirectionBias horizontalBias, DirectionBias verticalBias)
1241 DALI_LOG_SCROLL_STATE("[0x%X] position[%.2f, %.2f] duration[%.2f], bias[%d, %d]", this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1242 TransformTo(position, duration, alpha, horizontalBias, verticalBias);
1245 void ScrollView::ScrollTo(unsigned int page)
1247 ScrollTo(page, mSnapDuration);
1250 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
1253 unsigned int volume;
1254 unsigned int libraries;
1256 // The position to scroll to is continuous and linear
1257 // unless a domain has been enabled on the X axis.
1258 // or if WrapMode has been enabled.
1259 bool carryX = mRulerX->GetDomain().enabled | mWrapMode;
1260 bool carryY = mRulerY->GetDomain().enabled | mWrapMode;
1262 position.x = mRulerX->GetPositionFromPage(page, volume, carryX);
1263 position.y = mRulerY->GetPositionFromPage(volume, libraries, carryY);
1265 ScrollTo(position, duration, bias, bias);
1268 void ScrollView::ScrollTo(Actor &actor)
1270 ScrollTo(actor, mSnapDuration);
1273 void ScrollView::ScrollTo(Actor &actor, float duration)
1275 DALI_ASSERT_ALWAYS(actor.GetParent() == Self());
1277 Actor self = Self();
1278 Vector3 size = self.GetCurrentSize();
1279 Vector3 position = actor.GetCurrentPosition();
1280 Vector2 prePosition = GetPropertyPrePosition();
1281 position.GetVectorXY() -= prePosition;
1283 ScrollTo(Vector2(position.x - size.width * 0.5f, position.y - size.height * 0.5f), duration);
1286 Actor ScrollView::FindClosestActor()
1288 Actor self = Self();
1289 Vector3 size = self.GetCurrentSize();
1291 return FindClosestActorToPosition(Vector3(size.width * 0.5f,size.height * 0.5f,0.0f));
1294 Actor ScrollView::FindClosestActorToPosition(const Vector3& position, FindDirection dirX, FindDirection dirY, FindDirection dirZ)
1297 float closestDistance2 = 0.0f;
1298 Vector3 actualPosition = position;
1300 unsigned int numChildren = Self().GetChildCount();
1302 for(unsigned int i = 0; i < numChildren; ++i)
1304 Actor child = Self().GetChildAt(i);
1306 if(mInternalActor == child) // ignore internal actor.
1311 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1313 Vector3 delta = childPosition - actualPosition;
1315 // X-axis checking (only find Actors to the [dirX] of actualPosition)
1316 if(dirX > All) // != All,None
1318 FindDirection deltaH = delta.x > 0 ? Right : Left;
1325 // Y-axis checking (only find Actors to the [dirY] of actualPosition)
1326 if(dirY > All) // != All,None
1328 FindDirection deltaV = delta.y > 0 ? Down : Up;
1335 // Z-axis checking (only find Actors to the [dirZ] of actualPosition)
1336 if(dirZ > All) // != All,None
1338 FindDirection deltaV = delta.y > 0 ? In : Out;
1345 // compare child to closest child in terms of distance.
1346 float distance2 = 0.0f;
1348 // distance2 = the Square of the relevant dimensions of delta
1351 distance2 += delta.x * delta.x;
1356 distance2 += delta.y * delta.y;
1361 distance2 += delta.z * delta.z;
1364 if(closestChild) // Next time.
1366 if(distance2 < closestDistance2)
1368 closestChild = child;
1369 closestDistance2 = distance2;
1374 closestChild = child;
1375 closestDistance2 = distance2;
1379 return closestChild;
1382 bool ScrollView::ScrollToSnapPoint()
1384 DALI_LOG_SCROLL_STATE("[0x%X]", this );
1385 Vector2 stationaryVelocity = Vector2(0.0f, 0.0f);
1386 return SnapWithVelocity( stationaryVelocity );
1389 // TODO: In situations where axes are different (X snap, Y free)
1390 // Each axis should really have their own independent animation (time and equation)
1391 // Consider, X axis snapping to nearest grid point (EaseOut over fixed time)
1392 // Consider, Y axis simulating physics to arrive at a point (Physics equation over variable time)
1393 // Currently, the axes have been split however, they both use the same EaseOut equation.
1394 bool ScrollView::SnapWithVelocity(Vector2 velocity)
1396 // Animator takes over now, touches are assumed not to interfere.
1397 // And if touches do interfere, then we'll stop animation, update PrePosition
1398 // to current mScroll's properties, and then resume.
1399 // Note: For Flicking this may work a bit different...
1401 float angle = atan2(velocity.y, velocity.x);
1402 float speed2 = velocity.LengthSquared();
1403 AlphaFunction alphaFunction = mSnapAlphaFunction;
1404 Vector2 positionDuration = Vector2::ONE * mSnapDuration;
1407 FindDirection horizontal = None;
1408 FindDirection vertical = None;
1410 // orthoAngleRange = Angle tolerance within the Exact N,E,S,W direction
1411 // that will be accepted as a general N,E,S,W flick direction.
1413 const float orthoAngleRange = FLICK_ORTHO_ANGLE_RANGE * M_PI / 180.0f;
1414 const float flickSpeedThreshold2 = mFlickSpeedThreshold * mFlickSpeedThreshold;
1416 Vector2 positionSnap = mScrollPrePosition;
1418 // Flick logic X Axis
1420 if(mRulerX->IsEnabled() && mLockAxis != LockHorizontal)
1424 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1425 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1427 if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East
1429 biasX = 0.0f, horizontal = Left;
1431 // This guards against an error where no movement occurs, due to the flick finishing
1432 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1433 positionSnap.x += 1.0f;
1435 else if((angle >= M_PI-orthoAngleRange) || (angle < -M_PI+orthoAngleRange)) // Swiping West
1437 biasX = 1.0f, horizontal = Right;
1439 // This guards against an error where no movement occurs, due to the flick finishing
1440 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1441 positionSnap.x -= 1.0f;
1446 // Flick logic Y Axis
1448 if(mRulerY->IsEnabled() && mLockAxis != LockVertical)
1452 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1453 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1455 if((angle >= M_PI_2-orthoAngleRange) && (angle < M_PI_2+orthoAngleRange)) // Swiping South
1457 biasY = 0.0f, vertical = Up;
1459 else if((angle >= -M_PI_2-orthoAngleRange) && (angle < -M_PI_2+orthoAngleRange)) // Swiping North
1461 biasY = 1.0f, vertical = Down;
1466 // isFlick: Whether this gesture is a flick or not.
1467 bool isFlick = (horizontal != All || vertical != All);
1468 // isFreeFlick: Whether this gesture is a flick under free panning criteria.
1469 bool isFreeFlick = velocity.LengthSquared() > (FREE_FLICK_SPEED_THRESHOLD*FREE_FLICK_SPEED_THRESHOLD);
1471 if(isFlick || isFreeFlick)
1473 positionDuration = Vector2::ONE * mFlickDuration;
1474 alphaFunction = mFlickAlphaFunction;
1477 // Calculate next positionSnap ////////////////////////////////////////////////////////////
1479 if(mActorAutoSnapEnabled)
1481 Vector3 size = Self().GetCurrentSize();
1483 Actor child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f), horizontal, vertical );
1485 if(!child && isFlick )
1487 // If we conducted a direction limited search and found no actor, then just snap to the closest actor.
1488 child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f) );
1493 Vector2 position = Self().GetProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_POSITION);
1495 // Get center-point of the Actor.
1496 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1498 if(mRulerX->IsEnabled())
1500 positionSnap.x = position.x - childPosition.x + size.width * 0.5f;
1502 if(mRulerY->IsEnabled())
1504 positionSnap.y = position.y - childPosition.y + size.height * 0.5f;
1509 Vector2 startPosition = positionSnap;
1510 positionSnap.x = -mRulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system.
1511 positionSnap.y = -mRulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
1513 Vector2 clampDelta(Vector2::ZERO);
1514 ClampPosition(positionSnap);
1516 if( (mRulerX->GetType() == Ruler::Free || mRulerY->GetType() == Ruler::Free)
1517 && isFreeFlick && !mActorAutoSnapEnabled)
1519 // Calculate target position based on velocity of flick.
1521 // a = Deceleration (Set to diagonal stage length * friction coefficient)
1522 // u = Initial Velocity (Flick velocity)
1523 // v = 0 (Final Velocity)
1524 // t = Time (Velocity / Deceleration)
1525 Vector2 stageSize = Stage::GetCurrent().GetSize();
1526 float stageLength = Vector3(stageSize.x, stageSize.y, 0.0f).Length();
1527 float a = (stageLength * mFrictionCoefficient);
1528 Vector3 u = Vector3(velocity.x, velocity.y, 0.0f) * mFlickSpeedCoefficient;
1529 float speed = u.Length();
1532 // TODO: Change this to a decay function. (faster you flick, the slower it should be)
1533 speed = std::min(speed, stageLength * mMaxFlickSpeed );
1535 alphaFunction = ConstantDecelerationAlphaFunction;
1537 float t = speed / a;
1539 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1541 positionSnap.x += t*u.x*0.5f;
1544 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1546 positionSnap.y += t*u.y*0.5f;
1549 clampDelta = positionSnap;
1550 ClampPosition(positionSnap);
1551 if((positionSnap - startPosition).LengthSquared() > Math::MACHINE_EPSILON_0)
1553 clampDelta -= positionSnap;
1554 clampDelta.x = clampDelta.x > 0.0f ? std::min(clampDelta.x, mMaxOvershoot.x) : std::max(clampDelta.x, -mMaxOvershoot.x);
1555 clampDelta.y = clampDelta.y > 0.0f ? std::min(clampDelta.y, mMaxOvershoot.y) : std::max(clampDelta.y, -mMaxOvershoot.y);
1559 clampDelta = Vector2::ZERO;
1562 // If Axis is Free and has velocity, then calculate time taken
1563 // to reach target based on velocity in axis.
1564 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1566 float deltaX = fabsf(startPosition.x - positionSnap.x);
1568 if(fabsf(u.x) > Math::MACHINE_EPSILON_1)
1570 positionDuration.x = fabsf(deltaX / u.x);
1574 positionDuration.x = 0;
1578 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1580 float deltaY = fabsf(startPosition.y - positionSnap.y);
1582 if(fabsf(u.y) > Math::MACHINE_EPSILON_1)
1584 positionDuration.y = fabsf(deltaY / u.y);
1588 positionDuration.y = 0;
1593 if(IsOvershootEnabled())
1595 // Scroll to the end of the overshoot only when overshoot is enabled.
1596 positionSnap += clampDelta;
1599 bool animating = AnimateTo(positionSnap, positionDuration,
1600 alphaFunction, false,
1601 DirectionBiasNone, DirectionBiasNone,
1602 isFlick || isFreeFlick ? Flick : Snap);
1607 void ScrollView::StopAnimation(void)
1609 // Clear Snap animation if exists.
1610 StopAnimation(mInternalXAnimation);
1611 StopAnimation(mInternalYAnimation);
1612 mScrollStateFlags = 0;
1613 // remove scroll animation flags
1614 HandleStoppedAnimation();
1617 void ScrollView::StopAnimation(Animation& animation)
1626 bool ScrollView::AnimateTo(const Vector2& position, const Vector2& positionDuration,
1627 AlphaFunction alpha, bool findShortcuts,
1628 DirectionBias horizontalBias, DirectionBias verticalBias,
1631 // Here we perform an animation on a number of properties (depending on which have changed)
1632 // The animation is applied to all ScrollBases
1633 Actor self = Self();
1634 mScrollTargetPosition = position;
1635 float totalDuration = 0.0f;
1637 bool positionChanged = (mScrollTargetPosition != mScrollPostPosition);
1641 totalDuration = std::max(totalDuration, positionDuration.x);
1642 totalDuration = std::max(totalDuration, positionDuration.y);
1646 // try to animate for a frame, on some occasions update will be changing scroll value while event side thinks it hasnt changed
1647 totalDuration = 0.01f;
1648 positionChanged = true;
1653 // Position Delta ///////////////////////////////////////////////////////
1656 if(mWrapMode && findShortcuts)
1658 // In Wrap Mode, the shortest distance is a little less intuitive...
1659 const RulerDomain rulerDomainX = mRulerX->GetDomain();
1660 const RulerDomain rulerDomainY = mRulerY->GetDomain();
1662 if(mRulerX->IsEnabled())
1664 float dir = VectorInDomain(-mScrollPrePosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
1665 mScrollTargetPosition.x = mScrollPrePosition.x + -dir;
1668 if(mRulerY->IsEnabled())
1670 float dir = VectorInDomain(-mScrollPrePosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
1671 mScrollTargetPosition.y = mScrollPrePosition.y + -dir;
1675 // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting
1676 // a horizonal/vertical wall.delay
1677 AnimateInternalXTo(mScrollTargetPosition.x, positionDuration.x, alpha);
1678 AnimateInternalYTo(mScrollTargetPosition.y, positionDuration.y, alpha);
1680 if( !(mScrollStateFlags & SCROLL_ANIMATION_FLAGS) )
1682 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollTargetPosition.x, mScrollTargetPosition.y );
1683 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollTargetPosition);
1684 mScrollPrePosition = mScrollTargetPosition;
1685 mScrollPostPosition = mScrollTargetPosition;
1686 WrapPosition(mScrollPostPosition);
1689 DALI_LOG_SCROLL_STATE("[0x%X] position-changed, mScrollTargetPosition[%.2f, %.2f], mScrollPrePosition[%.2f, %.2f], mScrollPostPosition[%.2f, %.2f]", this, mScrollTargetPosition.x, mScrollTargetPosition.y, mScrollPrePosition.x, mScrollPrePosition.y, mScrollPostPosition.x, mScrollPostPosition.y );
1690 DALI_LOG_SCROLL_STATE("[0x%X] SCROLL_PRE_POSITION[%.2f, %.2f], SCROLL_POSITION[%.2f, %.2f]", this, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ).Get<Vector2>().x, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ).Get<Vector2>().y, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_POSITION ).Get<Vector2>().x, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_POSITION ).Get<Vector2>().y );
1693 SetScrollUpdateNotification(true);
1695 // Always send a snap event when AnimateTo is called.
1696 Toolkit::ScrollView::SnapEvent snapEvent;
1697 snapEvent.type = snapType;
1698 snapEvent.position = -mScrollTargetPosition;
1699 snapEvent.duration = totalDuration;
1701 DALI_LOG_SCROLL_STATE("[0x%X] mSnapStartedSignal [%.2f, %.2f]", this, snapEvent.position.x, snapEvent.position.y);
1702 mSnapStartedSignal.Emit( snapEvent );
1704 return (mScrollStateFlags & SCROLL_ANIMATION_FLAGS) != 0;
1707 void ScrollView::EnableScrollOvershoot(bool enable)
1709 if(enable && !mOvershootIndicator)
1711 mOvershootIndicator = ScrollOvershootIndicator::New();
1715 mOvershootIndicator->AttachToScrollable(*this);
1719 mMaxOvershoot = mUserMaxOvershoot;
1720 mOvershootIndicator->DetachFromScrollable(*this);
1722 UpdateMainInternalConstraint();
1725 void ScrollView::AddOverlay(Actor actor)
1727 mInternalActor.Add( actor );
1730 void ScrollView::RemoveOverlay(Actor actor)
1732 mInternalActor.Remove( actor );
1735 void ScrollView::SetOvershootEffectColor( const Vector4& color )
1737 mOvershootEffectColor = color;
1738 if( mOvershootIndicator )
1740 mOvershootIndicator->SetOvershootEffectColor( color );
1744 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
1746 PanGestureDetector panGesture( GetPanGestureDetector() );
1748 // First remove just in case we have some set, then add.
1749 panGesture.RemoveDirection( direction );
1750 panGesture.AddDirection( direction, threshold );
1753 void ScrollView::RemoveScrollingDirection( Radian direction )
1755 PanGestureDetector panGesture( GetPanGestureDetector() );
1756 panGesture.RemoveDirection( direction );
1759 Toolkit::ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
1761 return mSnapStartedSignal;
1764 void ScrollView::FindAndUnbindActor(Actor child)
1769 Vector2 ScrollView::GetPropertyPrePosition() const
1771 Vector2 position = Self().GetProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION);
1772 WrapPosition(position);
1776 Vector2 ScrollView::GetPropertyPosition() const
1778 Vector2 position = Self().GetProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_POSITION);
1779 WrapPosition(position);
1784 void ScrollView::HandleStoppedAnimation()
1786 SetScrollUpdateNotification(false);
1789 void ScrollView::HandleSnapAnimationFinished()
1791 // Emit Signal that scrolling has completed.
1793 Actor self = Self();
1794 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
1796 Vector2 deltaPosition(mScrollPrePosition);
1798 UpdateLocalScrollProperties();
1799 WrapPosition(mScrollPrePosition);
1800 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
1801 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
1803 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1804 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 3 current[%.2f, %.2f], mScrollTargetPosition[%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y, -mScrollTargetPosition.x, -mScrollTargetPosition.y );
1805 mScrollCompletedSignal.Emit( currentScrollPosition );
1807 mDomainOffset += deltaPosition - mScrollPostPosition;
1808 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset);
1809 HandleStoppedAnimation();
1812 void ScrollView::SetScrollUpdateNotification( bool enabled )
1814 Actor self = Self();
1815 if( mScrollXUpdateNotification )
1817 // disconnect now to avoid a notification before removed from update thread
1818 mScrollXUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1819 self.RemovePropertyNotification(mScrollXUpdateNotification);
1820 mScrollXUpdateNotification.Reset();
1822 if( enabled && !mScrollUpdatedSignal.Empty())
1824 // Only set up the notification when the application has connected to the updated signal
1825 mScrollXUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 0, StepCondition(mScrollUpdateDistance, 0.0f));
1826 mScrollXUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1828 if( mScrollYUpdateNotification )
1830 // disconnect now to avoid a notification before removed from update thread
1831 mScrollYUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1832 self.RemovePropertyNotification(mScrollYUpdateNotification);
1833 mScrollYUpdateNotification.Reset();
1835 if( enabled && !mScrollUpdatedSignal.Empty())
1837 // Only set up the notification when the application has connected to the updated signal
1838 mScrollYUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 1, StepCondition(mScrollUpdateDistance, 0.0f));
1839 mScrollYUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1843 void ScrollView::OnScrollUpdateNotification(Dali::PropertyNotification& source)
1845 // Guard against destruction during signal emission
1846 Toolkit::ScrollView handle( GetOwner() );
1848 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1849 mScrollUpdatedSignal.Emit( currentScrollPosition );
1852 bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1854 Dali::BaseHandle handle( object );
1856 bool connected( true );
1857 Toolkit::ScrollView view = Toolkit::ScrollView::DownCast( handle );
1859 if( 0 == strcmp( signalName.c_str(), SIGNAL_SNAP_STARTED ) )
1861 view.SnapStartedSignal().Connect( tracker, functor );
1865 // signalName does not match any signal
1872 void ScrollView::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1874 // need to update domain properties for new size
1875 UpdatePropertyDomain(targetSize);
1878 void ScrollView::OnControlSizeSet( const Vector3& size )
1880 // need to update domain properties for new size
1881 if( mDefaultMaxOvershoot )
1883 mUserMaxOvershoot.x = size.x * 0.5f;
1884 mUserMaxOvershoot.y = size.y * 0.5f;
1885 if( !IsOvershootEnabled() )
1887 mMaxOvershoot = mUserMaxOvershoot;
1890 UpdatePropertyDomain(size);
1891 UpdateMainInternalConstraint();
1892 if( IsOvershootEnabled() )
1894 mOvershootIndicator->Reset();
1898 void ScrollView::OnChildAdd(Actor& child)
1900 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
1903 mInternalActor.Add(scrollBar);
1904 if(scrollBar.GetScrollDirection() == Toolkit::ScrollBar::Horizontal)
1906 scrollBar.SetScrollPropertySource(Self(),
1907 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_X,
1908 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_X,
1909 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_X,
1910 Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_X);
1914 scrollBar.SetScrollPropertySource(Self(),
1915 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_Y,
1916 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
1917 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_Y,
1918 Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_Y);
1921 else if(mAlterChild)
1927 void ScrollView::OnChildRemove(Actor& child)
1929 // TODO: Actor needs a RemoveConstraint method to take out an individual constraint.
1933 void ScrollView::StartTouchDownTimer()
1935 if ( !mTouchDownTimer )
1937 mTouchDownTimer = Timer::New( TOUCH_DOWN_TIMER_INTERVAL );
1938 mTouchDownTimer.TickSignal().Connect( this, &ScrollView::OnTouchDownTimeout );
1941 mTouchDownTimer.Start();
1944 void ScrollView::StopTouchDownTimer()
1946 if ( mTouchDownTimer )
1948 mTouchDownTimer.Stop();
1952 bool ScrollView::OnTouchDownTimeout()
1954 DALI_LOG_SCROLL_STATE("[0x%X]", this);
1956 mTouchDownTimeoutReached = true;
1958 unsigned int currentScrollStateFlags( mScrollStateFlags ); // Cleared in StopAnimation so keep local copy for comparison
1959 if( currentScrollStateFlags & (SCROLL_ANIMATION_FLAGS | SNAP_ANIMATION_FLAGS) )
1961 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling Or snapping flags set, stopping animation", this);
1964 if( currentScrollStateFlags & SCROLL_ANIMATION_FLAGS )
1966 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling flags set, emitting signal", this);
1968 mScrollInterrupted = true;
1969 // reset domain offset as scrolling from original plane.
1970 mDomainOffset = Vector2::ZERO;
1971 Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, Vector2::ZERO);
1973 UpdateLocalScrollProperties();
1974 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1975 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 4 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1976 mScrollCompletedSignal.Emit( currentScrollPosition );
1983 bool ScrollView::OnTouchEvent(const TouchEvent& event)
1987 DALI_LOG_SCROLL_STATE("[0x%X], Not Sensitive, ignoring", this);
1989 // Ignore this touch event, if scrollview is insensitive.
1993 // Ignore events with multiple-touch points
1994 if (event.GetPointCount() != 1)
1996 DALI_LOG_SCROLL_STATE("[0x%X], multiple touch, ignoring", this);
2001 const TouchPoint::State pointState = event.GetPoint(0).state;
2002 if( pointState == TouchPoint::Down )
2004 DALI_LOG_SCROLL_STATE("[0x%X] Down", this);
2006 if(mGestureStackDepth==0)
2008 mTouchDownTime = event.time;
2010 // This allows time for a pan-gesture to start, to avoid breaking snap-animation behavior with fast flicks.
2011 // If touch-down does not become a pan (after timeout interval), then snap-animation can be interrupted.
2012 mTouchDownTimeoutReached = false;
2013 mScrollInterrupted = false;
2014 StartTouchDownTimer();
2017 else if( ( pointState == TouchPoint::Up ) ||
2018 ( ( pointState == TouchPoint::Interrupted ) && ( event.GetPoint(0).hitActor == Self() ) ) )
2020 DALI_LOG_SCROLL_STATE("[0x%X] %s", this, ( ( pointState == TouchPoint::Up ) ? "Up" : "Interrupted" ) );
2022 StopTouchDownTimer();
2024 // if the user touches and releases without enough movement to go
2025 // into a gesture state, then we should snap to nearest point.
2026 // otherwise our scroll could be stopped (interrupted) half way through an animation.
2027 if(mGestureStackDepth==0 && mTouchDownTimeoutReached)
2029 if( ( event.GetPoint(0).state == TouchPoint::Interrupted ) ||
2030 ( ( event.time - mTouchDownTime ) >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET ) )
2032 // Reset the velocity only if down was received a while ago
2033 mLastVelocity = Vector2( 0.0f, 0.0f );
2036 UpdateLocalScrollProperties();
2037 // Only finish the transform if scrolling was interrupted on down or if we are scrolling
2038 if ( mScrollInterrupted || mScrolling )
2040 DALI_LOG_SCROLL_STATE("[0x%X] Calling FinishTransform", this);
2045 mTouchDownTimeoutReached = false;
2046 mScrollInterrupted = false;
2052 bool ScrollView::OnMouseWheelEvent(const MouseWheelEvent& event)
2056 // Ignore this mouse wheel event, if scrollview is insensitive.
2060 Vector2 targetScrollPosition = GetPropertyPosition();
2062 if(mRulerX->IsEnabled() && !mRulerY->IsEnabled())
2064 // If only the ruler in the X axis is enabled, scroll in the X axis.
2065 if(mRulerX->GetType() == Ruler::Free)
2067 // Free panning mode
2068 targetScrollPosition.x += event.z * mMouseWheelScrollDistanceStep.x;
2069 ClampPosition(targetScrollPosition);
2070 ScrollTo(-targetScrollPosition);
2072 else if(!mScrolling)
2074 // Snap mode, only respond to the event when the previous snap animation is finished.
2075 ScrollTo(GetCurrentPage() - event.z);
2080 // If the ruler in the Y axis is enabled, scroll in the Y axis.
2081 if(mRulerY->GetType() == Ruler::Free)
2083 // Free panning mode
2084 targetScrollPosition.y += event.z * mMouseWheelScrollDistanceStep.y;
2085 ClampPosition(targetScrollPosition);
2086 ScrollTo(-targetScrollPosition);
2088 else if(!mScrolling)
2090 // Snap mode, only respond to the event when the previous snap animation is finished.
2091 ScrollTo(GetCurrentPage() - event.z * mRulerX->GetTotalPages());
2098 void ScrollView::ResetScrolling()
2100 Actor self = Self();
2101 self.GetProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get(mScrollPostPosition);
2102 mScrollPrePosition = mScrollPostPosition;
2103 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPostPosition.x, mScrollPostPosition.y );
2104 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPostPosition);
2107 void ScrollView::UpdateLocalScrollProperties()
2109 Actor self = Self();
2110 self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get(mScrollPrePosition);
2111 self.GetProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get(mScrollPostPosition);
2114 // private functions
2116 void ScrollView::PreAnimatedScrollSetup()
2118 // SCROLL_PRE_POSITION is our unclamped property with wrapping
2119 // SCROLL_POSITION is our final scroll position after clamping
2121 Actor self = Self();
2123 Vector2 deltaPosition(mScrollPostPosition);
2124 WrapPosition(mScrollPostPosition);
2125 mDomainOffset += deltaPosition - mScrollPostPosition;
2126 Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset);
2128 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2130 // already performing animation on internal x position
2131 StopAnimation(mInternalXAnimation);
2134 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2136 // already performing animation on internal y position
2137 StopAnimation(mInternalYAnimation);
2140 mScrollStateFlags = 0;
2142 // Update Actor position with this wrapped value.
2145 void ScrollView::FinaliseAnimatedScroll()
2147 // TODO - common animation finishing code in here
2150 void ScrollView::AnimateInternalXTo( float position, float duration, AlphaFunction alpha )
2152 StopAnimation(mInternalXAnimation);
2154 if( duration > Math::MACHINE_EPSILON_10 )
2156 Actor self = Self();
2157 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get<Vector2>().x, position );
2158 mInternalXAnimation = Animation::New(duration);
2159 DALI_LOG_SCROLL_STATE("[0x%X], mInternalXAnimation[0x%X]", this, mInternalXAnimation.GetObjectPtr() );
2160 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2161 mInternalXAnimation.AnimateTo( Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position, alpha, TimePeriod(duration));
2162 mInternalXAnimation.Play();
2164 // erase current state flags
2165 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2166 // add internal animation state flag
2167 mScrollStateFlags |= AnimatingInternalX;
2171 void ScrollView::AnimateInternalYTo( float position, float duration, AlphaFunction alpha )
2173 StopAnimation(mInternalYAnimation);
2175 if( duration > Math::MACHINE_EPSILON_10 )
2177 Actor self = Self();
2178 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get<Vector2>().y, position );
2179 mInternalYAnimation = Animation::New(duration);
2180 DALI_LOG_SCROLL_STATE("[0x%X], mInternalYAnimation[0x%X]", this, mInternalYAnimation.GetObjectPtr() );
2181 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2182 mInternalYAnimation.AnimateTo( Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position, alpha, TimePeriod(duration));
2183 mInternalYAnimation.Play();
2185 // erase current state flags
2186 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2187 // add internal animation state flag
2188 mScrollStateFlags |= AnimatingInternalY;
2192 void ScrollView::OnScrollAnimationFinished( Animation& source )
2194 // Guard against destruction during signal emission
2195 // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished()
2196 Toolkit::ScrollView handle( GetOwner() );
2198 bool scrollingFinished = false;
2200 // update our local scroll positions
2201 UpdateLocalScrollProperties();
2203 if( source == mInternalXAnimation )
2205 DALI_LOG_SCROLL_STATE("[0x%X] mInternalXAnimation[0x%X], expected[%.2f], actual[%.2f], post[%.2f]", this, mInternalXAnimation.GetObjectPtr(), mScrollTargetPosition.x, Self().GetProperty(SCROLL_PRE_POSITION).Get<Vector2>().x, mScrollPostPosition.x );
2207 if( !(mScrollStateFlags & AnimatingInternalY) )
2209 scrollingFinished = true;
2211 mInternalXAnimation.Reset();
2212 // wrap pre scroll x position and set it
2215 const RulerDomain rulerDomain = mRulerX->GetDomain();
2216 mScrollPrePosition.x = -WrapInDomain(-mScrollPrePosition.x, rulerDomain.min, rulerDomain.max);
2217 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
2218 handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
2220 SnapInternalXTo(mScrollPostPosition.x);
2223 if( source == mInternalYAnimation )
2225 DALI_LOG_SCROLL_STATE("[0x%X] mInternalYAnimation[0x%X], expected[%.2f], actual[%.2f], post[%.2f]", this, mInternalYAnimation.GetObjectPtr(), mScrollTargetPosition.y, Self().GetProperty(SCROLL_PRE_POSITION).Get<Vector2>().y, mScrollPostPosition.y );
2227 if( !(mScrollStateFlags & AnimatingInternalX) )
2229 scrollingFinished = true;
2231 mInternalYAnimation.Reset();
2234 // wrap pre scroll y position and set it
2235 const RulerDomain rulerDomain = mRulerY->GetDomain();
2236 mScrollPrePosition.y = -WrapInDomain(-mScrollPrePosition.y, rulerDomain.min, rulerDomain.max);
2237 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
2238 handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
2240 SnapInternalYTo(mScrollPostPosition.y);
2243 DALI_LOG_SCROLL_STATE("[0x%X] scrollingFinished[%d] Animation[0x%X]", this, scrollingFinished, source.GetObjectPtr());
2245 if(scrollingFinished)
2247 HandleSnapAnimationFinished();
2251 void ScrollView::OnSnapInternalPositionFinished( Animation& source )
2253 Actor self = Self();
2254 UpdateLocalScrollProperties();
2255 if( source == mInternalXAnimation )
2257 DALI_LOG_SCROLL_STATE("[0x%X] Finished X PostPosition Animation", this );
2259 // clear internal x animation flags
2260 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2261 mInternalXAnimation.Reset();
2262 WrapPosition(mScrollPrePosition);
2264 if( source == mInternalYAnimation )
2266 DALI_LOG_SCROLL_STATE("[0x%X] Finished Y PostPosition Animation", this );
2268 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2269 mInternalYAnimation.Reset();
2270 WrapPosition(mScrollPrePosition);
2274 void ScrollView::SnapInternalXTo(float position)
2276 Actor self = Self();
2278 StopAnimation(mInternalXAnimation);
2280 // erase current state flags
2281 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2283 // if internal x not equal to inputed parameter, animate it
2284 float duration = std::min(fabsf((position - mScrollPrePosition.x) / mMaxOvershoot.x) * mSnapOvershootDuration, mSnapOvershootDuration);
2285 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration );
2286 if( duration > Math::MACHINE_EPSILON_1 )
2288 DALI_LOG_SCROLL_STATE("[0x%X] Starting X Snap Animation to[%.2f]", this, position );
2290 mInternalXAnimation = Animation::New(duration);
2291 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2292 mInternalXAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position);
2293 mInternalXAnimation.Play();
2295 // add internal animation state flag
2296 mScrollStateFlags |= SnappingInternalX;
2300 void ScrollView::SnapInternalYTo(float position)
2302 Actor self = Self();
2304 StopAnimation(mInternalYAnimation);
2306 // erase current state flags
2307 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2309 // if internal y not equal to inputed parameter, animate it
2310 float duration = std::min(fabsf((position - mScrollPrePosition.y) / mMaxOvershoot.y) * mSnapOvershootDuration, mSnapOvershootDuration);
2311 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration );
2312 if( duration > Math::MACHINE_EPSILON_1 )
2314 DALI_LOG_SCROLL_STATE("[0x%X] Starting Y Snap Animation to[%.2f]", this, position );
2316 mInternalYAnimation = Animation::New(duration);
2317 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2318 mInternalYAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position);
2319 mInternalYAnimation.Play();
2321 // add internal animation state flag
2322 mScrollStateFlags |= SnappingInternalY;
2326 void ScrollView::GestureStarted()
2328 // we handle the first gesture.
2329 // if we're currently doing a gesture and receive another
2330 // we continue and combine the effects of the gesture instead of reseting.
2331 if(mGestureStackDepth++==0)
2333 Actor self = Self();
2334 StopTouchDownTimer();
2336 mPanDelta = Vector2::ZERO;
2337 mLastVelocity = Vector2::ZERO;
2340 mLockAxis = LockPossible;
2343 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2345 StopAnimation(mInternalXAnimation);
2347 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2349 StopAnimation(mInternalYAnimation);
2351 mScrollStateFlags = 0;
2353 if(mScrolling) // are we interrupting a current scroll?
2355 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
2357 // send negative scroll position since scroll internal scroll position works as an offset for actors,
2358 // give applications the position within the domain from the scroll view's anchor position
2359 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 5 [%.2f, %.2f]", this, -mScrollPostPosition.x, -mScrollPostPosition.y);
2360 mScrollCompletedSignal.Emit( -mScrollPostPosition );
2365 void ScrollView::GestureContinuing(const Vector2& panDelta)
2367 mPanDelta.x+= panDelta.x;
2368 mPanDelta.y+= panDelta.y;
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, 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 // BUG: Gesture::Finished doesn't always return velocity on release (due to
2385 // timeDelta between last two events being 0 sometimes, or posiiton being the same)
2386 void ScrollView::OnPan( const PanGesture& gesture )
2388 // Guard against destruction during signal emission
2389 // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx()
2390 Actor self( Self() );
2394 DALI_LOG_SCROLL_STATE("[0x%X] Pan Ignored, Insensitive", this);
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 DALI_LOG_SCROLL_STATE("[0x%X] Pan Started", this);
2407 mPanStartPosition = gesture.position - gesture.displacement;
2408 UpdateLocalScrollProperties();
2411 self.SetProperty( Toolkit::ScrollView::Property::PANNING, true );
2412 self.SetProperty( Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(gesture.position.x, gesture.position.y, 0.0f) );
2414 UpdateMainInternalConstraint();
2418 case Gesture::Continuing:
2422 DALI_LOG_SCROLL_STATE("[0x%X] Pan Continuing", this);
2423 GestureContinuing(gesture.screenDisplacement);
2427 // If we do not think we are panning, then we should not do anything here
2433 case Gesture::Finished:
2434 case Gesture::Cancelled:
2438 DALI_LOG_SCROLL_STATE("[0x%X] Pan %s", this, ( ( gesture.state == Gesture::Finished ) ? "Finished" : "Cancelled" ) );
2440 UpdateLocalScrollProperties();
2441 mLastVelocity = gesture.velocity;
2443 self.SetProperty( Toolkit::ScrollView::Property::PANNING, false );
2445 if( mScrollMainInternalPrePositionConstraint )
2447 mScrollMainInternalPrePositionConstraint.Remove();
2450 if( mOvershootIndicator )
2452 mOvershootIndicator->ClearOvershoot();
2457 // If we do not think we are panning, then we should not do anything here
2463 case Gesture::Possible:
2464 case Gesture::Clear:
2466 // Nothing to do, not needed.
2470 } // end switch(gesture.state)
2472 OnGestureEx(gesture.state);
2475 void ScrollView::OnGestureEx(Gesture::State state)
2477 // call necessary signals for application developer
2479 if(state == Gesture::Started)
2481 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2482 Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, true);
2484 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 2 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2485 mScrollStartedSignal.Emit( currentScrollPosition );
2487 else if( (state == Gesture::Finished) ||
2488 (state == Gesture::Cancelled) ) // Finished/default
2490 // when all the gestures have finished, we finish the transform.
2491 // so if a user decides to pan (1 gesture), and then pan+zoom (2 gestures)
2492 // then stop panning (back to 1 gesture), and then stop zooming (0 gestures).
2493 // this is the point we end, and perform necessary snapping.
2494 mGestureStackDepth--;
2495 if(mGestureStackDepth==0)
2497 // no flick if we have not exceeded min flick distance
2498 if( (fabsf(mPanDelta.x) < mMinFlickDistance.x)
2499 && (fabsf(mPanDelta.y) < mMinFlickDistance.y) )
2501 // reset flick velocity
2502 mLastVelocity = Vector2::ZERO;
2508 DALI_LOG_SCROLL_STATE("[0x%X] mGestureStackDepth[%d]", this, mGestureStackDepth);
2513 void ScrollView::FinishTransform()
2515 // at this stage internal x and x scroll position should have followed prescroll position exactly
2516 Actor self = Self();
2518 PreAnimatedScrollSetup();
2520 // convert pixels/millisecond to pixels per second
2521 bool animating = SnapWithVelocity(mLastVelocity * 1000.0f);
2525 // if not animating, then this pan has completed right now.
2526 SetScrollUpdateNotification(false);
2528 Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
2530 if( fabs(mScrollPrePosition.x - mScrollTargetPosition.x) > Math::MACHINE_EPSILON_10 )
2532 SnapInternalXTo(mScrollTargetPosition.x);
2534 if( fabs(mScrollPrePosition.y - mScrollTargetPosition.y) > Math::MACHINE_EPSILON_10 )
2536 SnapInternalYTo(mScrollTargetPosition.y);
2538 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2539 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 6 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2540 mScrollCompletedSignal.Emit( currentScrollPosition );
2544 Vector2 ScrollView::GetOvershoot(Vector2& position) const
2546 Vector3 size = Self().GetCurrentSize();
2549 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2550 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2552 if(mRulerX->IsEnabled() && rulerDomainX.enabled)
2554 const float left = rulerDomainX.min - position.x;
2555 const float right = size.width - rulerDomainX.max - position.x;
2562 overshoot.x = right;
2566 if(mRulerY->IsEnabled() && rulerDomainY.enabled)
2568 const float top = rulerDomainY.min - position.y;
2569 const float bottom = size.height - rulerDomainY.max - position.y;
2576 overshoot.y = bottom;
2583 bool ScrollView::OnAccessibilityPan(PanGesture gesture)
2585 // Keep track of whether this is an AccessibilityPan
2586 mInAccessibilityPan = true;
2588 mInAccessibilityPan = false;
2593 void ScrollView::ClampPosition(Vector2& position) const
2595 ClampState2D clamped;
2596 ClampPosition(position, clamped);
2599 void ScrollView::ClampPosition(Vector2& position, ClampState2D &clamped) const
2601 Vector3 size = Self().GetCurrentSize();
2603 position.x = -mRulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system.
2604 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.
2607 void ScrollView::WrapPosition(Vector2& position) const
2611 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2612 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2614 if(mRulerX->IsEnabled())
2616 position.x = -WrapInDomain(-position.x, rulerDomainX.min, rulerDomainX.max);
2619 if(mRulerY->IsEnabled())
2621 position.y = -WrapInDomain(-position.y, rulerDomainY.min, rulerDomainY.max);
2626 void ScrollView::UpdateMainInternalConstraint()
2628 // TODO: Only update the constraints which have changed, rather than remove all and add all again.
2629 // Requires a dali-core ApplyConstraintAt, or a ReplaceConstraint. The former is probably more flexible.
2630 Actor self = Self();
2631 PanGestureDetector detector( GetPanGestureDetector() );
2633 if(mScrollMainInternalPositionConstraint)
2635 mScrollMainInternalPositionConstraint.Remove();
2636 mScrollMainInternalDeltaConstraint.Remove();
2637 mScrollMainInternalFinalConstraint.Remove();
2638 mScrollMainInternalRelativeConstraint.Remove();
2639 mScrollMainInternalDomainConstraint.Remove();
2640 mScrollMainInternalPrePositionMaxConstraint.Remove();
2642 if( mScrollMainInternalPrePositionConstraint )
2644 mScrollMainInternalPrePositionConstraint.Remove();
2647 // TODO: It's probably better to use a local displacement value as this will give a displacement when scrolling just commences
2648 // but we need to make sure than the gesture system gives displacement since last frame (60Hz), not displacement since last touch event (90Hz).
2650 // 1. First calculate the pre-position (this is the scroll position if no clamping has taken place)
2651 Vector2 initialPanMask = Vector2(mRulerX->IsEnabled() ? 1.0f : 0.0f, mRulerY->IsEnabled() ? 1.0f : 0.0f);
2653 if( mLockAxis == LockVertical )
2655 initialPanMask.y = 0.0f;
2657 else if( mLockAxis == LockHorizontal )
2659 initialPanMask.x = 0.0f;
2664 mScrollMainInternalPrePositionConstraint = Constraint::New<Vector2>( self,
2665 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION,
2666 InternalPrePositionConstraint( mPanStartPosition,
2669 mAxisAutoLockGradient,
2672 mRulerX->GetDomain(),
2673 mRulerY->GetDomain() ) );
2674 mScrollMainInternalPrePositionConstraint.AddSource( Source( detector, PanGestureDetector::Property::LOCAL_POSITION ) );
2675 mScrollMainInternalPrePositionConstraint.AddSource( Source( self, Actor::Property::SIZE ) );
2676 mScrollMainInternalPrePositionConstraint.Apply();
2679 // 2. Second calculate the clamped position (actual position)
2680 mScrollMainInternalPositionConstraint = Constraint::New<Vector2>( self,
2681 Toolkit::ScrollView::Property::SCROLL_POSITION,
2682 InternalPositionConstraint( mRulerX->GetDomain(),
2683 mRulerY->GetDomain(),
2685 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2686 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2687 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2688 mScrollMainInternalPositionConstraint.AddSource( Source( self, Actor::Property::SIZE ) );
2689 mScrollMainInternalPositionConstraint.Apply();
2691 mScrollMainInternalDeltaConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_POSITION_DELTA, InternalPositionDeltaConstraint );
2692 mScrollMainInternalDeltaConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2693 mScrollMainInternalDeltaConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET ) );
2694 mScrollMainInternalDeltaConstraint.Apply();
2696 mScrollMainInternalFinalConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_FINAL,
2697 InternalFinalConstraint( FinalDefaultAlphaFunction,
2698 FinalDefaultAlphaFunction ) );
2699 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2700 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::OVERSHOOT_X ) );
2701 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::OVERSHOOT_Y ) );
2702 mScrollMainInternalFinalConstraint.Apply();
2704 mScrollMainInternalRelativeConstraint = Constraint::New<Vector2>( self, Toolkit::Scrollable::Property::SCROLL_RELATIVE_POSITION, InternalRelativePositionConstraint );
2705 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2706 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2707 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2708 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2709 mScrollMainInternalRelativeConstraint.Apply();
2711 mScrollMainInternalDomainConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE, InternalScrollDomainConstraint );
2712 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2713 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2714 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2715 mScrollMainInternalDomainConstraint.Apply();
2717 mScrollMainInternalPrePositionMaxConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX, InternalPrePositionMaxConstraint );
2718 mScrollMainInternalPrePositionMaxConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2719 mScrollMainInternalPrePositionMaxConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2720 mScrollMainInternalPrePositionMaxConstraint.Apply();
2722 // When panning we want to make sure overshoot values are affected by pre position and post position
2723 SetOvershootConstraintsEnabled(!mWrapMode);
2726 void ScrollView::SetOvershootConstraintsEnabled(bool enabled)
2728 Actor self( Self() );
2729 // remove and reset, it may now be in wrong order with the main internal constraints
2730 if( mScrollMainInternalOvershootXConstraint )
2732 mScrollMainInternalOvershootXConstraint.Remove();
2733 mScrollMainInternalOvershootXConstraint.Reset();
2734 mScrollMainInternalOvershootYConstraint.Remove();
2735 mScrollMainInternalOvershootYConstraint.Reset();
2739 mScrollMainInternalOvershootXConstraint= Constraint::New<float>( self, Toolkit::ScrollView::Property::OVERSHOOT_X, OvershootXConstraint(mMaxOvershoot.x) );
2740 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2741 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2742 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL ) );
2743 mScrollMainInternalOvershootXConstraint.Apply();
2745 mScrollMainInternalOvershootYConstraint = Constraint::New<float>( self, Toolkit::ScrollView::Property::OVERSHOOT_Y, OvershootYConstraint(mMaxOvershoot.y) );
2746 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2747 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2748 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
2749 mScrollMainInternalOvershootYConstraint.Apply();
2753 self.SetProperty(Toolkit::ScrollView::Property::OVERSHOOT_X, 0.0f);
2754 self.SetProperty(Toolkit::ScrollView::Property::OVERSHOOT_Y, 0.0f);
2758 void ScrollView::SetInternalConstraints()
2760 // Internal constraints (applied to target ScrollBase Actor itself) /////////
2761 UpdateMainInternalConstraint();
2763 // User definable constraints to apply to all child actors //////////////////
2764 Actor self = Self();
2766 // Apply some default constraints to ScrollView & its bound actors
2767 // Movement + Wrap function
2769 Constraint constraint;
2771 // MoveActor (scrolling)
2772 constraint = Constraint::New<Vector3>( self, Actor::Property::POSITION, MoveActorConstraint );
2773 constraint.AddSource( Source( self, Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2774 constraint.SetRemoveAction(Constraint::Discard);
2775 ApplyConstraintToBoundActors(constraint);
2777 // WrapActor (wrap functionality)
2778 constraint = Constraint::New<Vector3>( self, Actor::Property::POSITION, WrapActorConstraint );
2779 constraint.AddSource( LocalSource( Actor::Property::SCALE ) );
2780 constraint.AddSource( LocalSource( Actor::Property::ANCHOR_POINT ) );
2781 constraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2782 constraint.AddSource( Source( self, Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2783 constraint.AddSource( Source( self, Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2784 constraint.AddSource( Source( self, Toolkit::ScrollView::Property::WRAP ) );
2785 constraint.SetRemoveAction(Constraint::Discard);
2786 ApplyConstraintToBoundActors(constraint);
2789 } // namespace Internal
2791 } // namespace Toolkit