2 * Copyright (c) 2015 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/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_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = Vector2(0.17f, 0.1f); ///< The step of horizontal scroll distance in the proportion of stage size for each 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( "internalMaxPosition" );
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, "scrollPosition", VECTOR2, SCROLL_POSITION)
248 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPrePosition", VECTOR2, SCROLL_PRE_POSITION)
249 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionX", SCROLL_PRE_POSITION_X, SCROLL_PRE_POSITION, 0)
250 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionY", SCROLL_PRE_POSITION_Y, SCROLL_PRE_POSITION, 1)
251 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionMax", VECTOR2, SCROLL_PRE_POSITION_MAX)
252 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionMaxX", SCROLL_PRE_POSITION_MAX_X, SCROLL_PRE_POSITION_MAX, 0)
253 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionMaxY", SCROLL_PRE_POSITION_MAX_Y, SCROLL_PRE_POSITION_MAX, 1)
254 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "overshootX", FLOAT, OVERSHOOT_X)
255 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "overshootY", FLOAT, OVERSHOOT_Y)
256 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollFinal", VECTOR2, SCROLL_FINAL)
257 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollFinalX", SCROLL_FINAL_X, SCROLL_FINAL,0)
258 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollFinalY", 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, "scrollDomainSize", VECTOR2, SCROLL_DOMAIN_SIZE)
263 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollDomainSizeX", SCROLL_DOMAIN_SIZE_X, SCROLL_DOMAIN_SIZE, 0)
264 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollDomainSizeY", SCROLL_DOMAIN_SIZE_Y, SCROLL_DOMAIN_SIZE, 1)
265 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollDomainOffset", VECTOR2, SCROLL_DOMAIN_OFFSET)
266 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPositionDelta", VECTOR2, SCROLL_POSITION_DELTA)
267 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "startPagePosition", VECTOR3, START_PAGE_POSITION)
269 DALI_SIGNAL_REGISTRATION( Toolkit, ScrollView, "valueChanged", 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 RulerPtr& rulerX, const RulerPtr& rulerY )
323 : mLocalStart( initialPanPosition ),
324 mInitialPanMask( initialPanMask ),
325 mMaxOvershoot( maxOvershoot ),
326 mAxisAutoLockGradient( axisAutoLockGradient ),
327 mLockAxis( initialLockAxis ),
328 mAxisAutoLock( axisAutoLock ),
331 const RulerDomain& rulerDomainX = rulerX->GetDomain();
332 const RulerDomain& rulerDomainY = rulerY->GetDomain();
333 mDomainMin = Vector2( rulerDomainX.min, -rulerDomainY.min );
334 mDomainMax = Vector2( -rulerDomainX.max, -rulerDomainY.max );
335 mClampX = rulerDomainX.enabled;
336 mClampY = rulerDomainY.enabled;
337 mFixedRulerX = rulerX->GetType() == Ruler::Fixed;
338 mFixedRulerY = rulerY->GetType() == Ruler::Fixed;
341 void operator()( Vector2& scrollPostPosition, const PropertyInputContainer& inputs )
343 const Vector2& panPosition = inputs[0]->GetVector2();
344 const bool& inGesture = inputs[1]->GetBoolean();
346 // First check if we are within a gesture.
347 // The ScrollView may have received a start gesture from ::OnPan()
348 // while the finish gesture is received now in this constraint.
349 // This gesture must then be rejected as the value will be "old".
350 // Typically the last value from the end of the last gesture.
351 // If we are rejecting the gesture, we simply don't modify the constraint target.
356 mPrePosition = scrollPostPosition;
357 mStartPosition = mPrePosition;
358 mCurrentPanMask = mInitialPanMask;
362 // Calculate Deltas...
363 const Vector2& currentPosition = panPosition;
364 Vector2 panDelta( currentPosition - mLocalStart );
366 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
367 // appears mostly horizontal or mostly vertical respectively...
370 mLockAxis = GetLockAxis( panDelta, mLockAxis, mAxisAutoLockGradient );
371 if( mLockAxis == ScrollView::LockVertical )
373 mCurrentPanMask.y = 0.0f;
375 else if( mLockAxis == ScrollView::LockHorizontal )
377 mCurrentPanMask.x = 0.0f;
381 // Restrict deltas based on ruler enable/disable and axis-lock state...
382 panDelta *= mCurrentPanMask;
384 // Perform Position transform based on input deltas...
385 scrollPostPosition = mPrePosition;
386 scrollPostPosition += panDelta;
388 // if no wrapping then clamp preposition to maximum overshoot amount
389 const Vector3& size = inputs[2]->GetVector3();
392 float newXPosition = Clamp( scrollPostPosition.x, ( mDomainMax.x + size.x ) - mMaxOvershoot.x, mDomainMin.x + mMaxOvershoot.x );
393 if( (newXPosition < scrollPostPosition.x - Math::MACHINE_EPSILON_1)
394 || (newXPosition > scrollPostPosition.x + Math::MACHINE_EPSILON_1) )
396 mPrePosition.x = newXPosition;
397 mLocalStart.x = panPosition.x;
399 scrollPostPosition.x = newXPosition;
403 float newYPosition = Clamp( scrollPostPosition.y, ( mDomainMax.y + size.y ) - mMaxOvershoot.y, mDomainMin.y + mMaxOvershoot.y );
404 if( ( newYPosition < scrollPostPosition.y - Math::MACHINE_EPSILON_1 )
405 || ( newYPosition > scrollPostPosition.y + Math::MACHINE_EPSILON_1 ) )
407 mPrePosition.y = newYPosition;
408 mLocalStart.y = panPosition.y;
410 scrollPostPosition.y = newYPosition;
413 // If we are using a fixed ruler in a particular axis, limit the maximum pages scrolled on that axis.
414 if( mFixedRulerX || mFixedRulerY )
416 // Here we limit the maximum amount that can be moved from the starting position of the gesture to one page.
417 // We do this only if we have a fixed ruler (on that axis) and the mode is enabled.
418 // Note: 1.0f is subtracted to keep the value within one page size (otherwise we stray on to the page after).
419 // Note: A further 1.0f is subtracted to handle a compensation that happens later within the flick handling code in SnapWithVelocity().
420 // When a flick is completed, an adjustment of 1.0f is sometimes made to allow for the scenario where:
421 // A flick finishes before the update thread has advanced the scroll position past the previous snap point.
422 Vector2 pageSizeLimit( size.x - ( 1.0f + 1.0f ), size.y - ( 1.0f - 1.0f ) );
423 Vector2 minPosition( mStartPosition.x - pageSizeLimit.x, mStartPosition.y - pageSizeLimit.y );
424 Vector2 maxPosition( mStartPosition.x + pageSizeLimit.x, mStartPosition.y + pageSizeLimit.y );
428 scrollPostPosition.x = Clamp( scrollPostPosition.x, minPosition.x, maxPosition.x );
432 scrollPostPosition.y = Clamp( scrollPostPosition.y, minPosition.y, maxPosition.y );
438 Vector2 mPrePosition;
440 Vector2 mStartPosition; ///< The start position of the gesture - used to limit scroll amount (not modified by clamping).
441 Vector2 mInitialPanMask; ///< Initial pan mask (based on ruler settings).
442 Vector2 mCurrentPanMask; ///< Current pan mask that can be altered by axis lock mode.
445 Vector2 mMaxOvershoot;
447 float mAxisAutoLockGradient; ///< Set by ScrollView
448 ScrollView::LockAxis mLockAxis;
450 bool mAxisAutoLock:1; ///< Set by ScrollView
459 * Internal Position Property Constraint.
461 * Generates position property based on pre-position
462 * Note: This is the position after clamping.
463 * (uses result of InternalPrePositionConstraint)
465 struct InternalPositionConstraint
467 InternalPositionConstraint(const RulerDomain& domainX, const RulerDomain& domainY, bool wrap)
468 : mDomainMin( -domainX.min, -domainY.min ),
469 mDomainMax( -domainX.max, -domainY.max ),
470 mClampX( domainX.enabled ),
471 mClampY( domainY.enabled ),
476 void operator()( Vector2& position, const PropertyInputContainer& inputs )
478 position = inputs[0]->GetVector2();
479 const Vector2& size = inputs[3]->GetVector3().GetVectorXY();
480 const Vector2& min = inputs[1]->GetVector2();
481 const Vector2& max = inputs[2]->GetVector2();
485 position.x = -WrapInDomain(-position.x, min.x, max.x);
486 position.y = -WrapInDomain(-position.y, min.y, max.y);
490 // clamp post position to domain
491 position.x = mClampX ? Clamp(position.x, mDomainMax.x + size.x, mDomainMin.x ) : position.x;
492 position.y = mClampY ? Clamp(position.y, mDomainMax.y + size.y, mDomainMin.y ) : position.y;
505 * This constraint updates the X overshoot property using the difference
506 * SCROLL_PRE_POSITION.x and SCROLL_POSITION.x, returning a relative value between 0.0f and 1.0f
508 struct OvershootXConstraint
510 OvershootXConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
512 void operator()( float& current, const PropertyInputContainer& inputs )
514 if( inputs[2]->GetBoolean() )
516 const Vector2& scrollPrePosition = inputs[0]->GetVector2();
517 const Vector2& scrollPostPosition = inputs[1]->GetVector2();
518 float newOvershoot = scrollPrePosition.x - scrollPostPosition.x;
519 current = (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
531 * This constraint updates the Y overshoot property using the difference
532 * SCROLL_PRE_POSITION.y and SCROLL_POSITION.y, returning a relative value between 0.0f and 1.0f
534 struct OvershootYConstraint
536 OvershootYConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
538 void operator()( float& current, const PropertyInputContainer& inputs )
540 if( inputs[2]->GetBoolean() )
542 const Vector2& scrollPrePosition = inputs[0]->GetVector2();
543 const Vector2& scrollPostPosition = inputs[1]->GetVector2();
544 float newOvershoot = scrollPrePosition.y - scrollPostPosition.y;
545 current = (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
557 * Internal Position-Delta Property Constraint.
559 * Generates position-delta property based on scroll-position + scroll-offset properties.
561 void InternalPositionDeltaConstraint( Vector2& current, const PropertyInputContainer& inputs )
563 const Vector2& scrollPosition = inputs[0]->GetVector2();
564 const Vector2& scrollOffset = inputs[1]->GetVector2();
566 current = scrollPosition + scrollOffset;
570 * Internal Final Position Constraint
571 * The position of content is:
572 * of scroll-position + f(scroll-overshoot)
573 * where f(...) function defines how overshoot
574 * should affect final-position.
576 struct InternalFinalConstraint
578 InternalFinalConstraint(AlphaFunctionPrototype functionX,
579 AlphaFunctionPrototype functionY)
580 : mFunctionX(functionX),
581 mFunctionY(functionY)
585 void operator()( Vector2& current, const PropertyInputContainer& inputs )
587 const float& overshootx = inputs[1]->GetFloat();
588 const float& overshooty = inputs[2]->GetFloat();
589 Vector2 offset( mFunctionX(overshootx),
590 mFunctionY(overshooty) );
592 current = inputs[0]->GetVector2() - offset;
595 AlphaFunctionPrototype mFunctionX;
596 AlphaFunctionPrototype mFunctionY;
602 ///////////////////////////////////////////////////////////////////////////////////////////////////
604 ///////////////////////////////////////////////////////////////////////////////////////////////////
606 Dali::Toolkit::ScrollView ScrollView::New()
608 // Create the implementation
609 ScrollViewPtr scrollView(new ScrollView());
611 // Pass ownership to CustomActor via derived handle
612 Dali::Toolkit::ScrollView handle(*scrollView);
614 // Second-phase init of the implementation
615 // This can only be done after the CustomActor connection has been made...
616 scrollView->Initialize();
621 ScrollView::ScrollView()
622 : ScrollBase( ControlBehaviour( REQUIRES_WHEEL_EVENTS ) ), // Enable size negotiation
624 mGestureStackDepth(0),
625 mScrollStateFlags(0),
626 mLockAxis(LockPossible),
627 mScrollUpdateDistance(DEFAULT_SCROLL_UPDATE_DISTANCE),
628 mMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT),
629 mUserMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT),
630 mSnapOvershootDuration(DEFAULT_SNAP_OVERSHOOT_DURATION),
631 mSnapOvershootAlphaFunction(AlphaFunction::EASE_OUT),
632 mSnapDuration(DEFAULT_SLOW_SNAP_ANIMATION_DURATION),
633 mSnapAlphaFunction(AlphaFunction::EASE_OUT),
634 mMinFlickDistance(DEFAULT_MIN_FLICK_DISTANCE),
635 mFlickSpeedThreshold(DEFAULT_MIN_FLICK_SPEED_THRESHOLD),
636 mFlickDuration(DEFAULT_FAST_SNAP_ANIMATION_DURATION),
637 mFlickAlphaFunction(AlphaFunction::EASE_OUT),
638 mAxisAutoLockGradient(DEFAULT_AXIS_AUTO_LOCK_GRADIENT),
639 mFrictionCoefficient(DEFAULT_FRICTION_COEFFICIENT),
640 mFlickSpeedCoefficient(DEFAULT_FLICK_SPEED_COEFFICIENT),
641 mMaxFlickSpeed(DEFAULT_MAX_FLICK_SPEED),
642 mWheelScrollDistanceStep(Vector2::ZERO),
643 mInAccessibilityPan(false),
645 mScrollInterrupted(false),
648 mTouchDownTimeoutReached(false),
649 mActorAutoSnapEnabled(false),
650 mAutoResizeContainerEnabled(false),
652 mAxisAutoLock(false),
654 mDefaultMaxOvershoot(true),
655 mCanScrollHorizontal(true),
656 mCanScrollVertical(true)
660 void ScrollView::OnInitialize()
664 // Internal Actor, used to hide actors from enumerations.
665 // Also actors added to Internal actor appear as overlays e.g. ScrollBar components.
666 mInternalActor = Actor::New();
667 self.Add(mInternalActor);
669 mInternalActor.SetParentOrigin(ParentOrigin::CENTER);
670 mInternalActor.SetAnchorPoint(AnchorPoint::CENTER);
671 mInternalActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
675 mScrollPostPosition = mScrollPrePosition = Vector2::ZERO;
677 mWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
679 mGestureStackDepth = 0;
681 EnableGestureDetection( Gesture::Type( Gesture::Pan ) );
683 // By default we'll allow the user to freely drag the scroll view,
684 // while disabling the other rulers.
685 RulerPtr ruler = new DefaultRuler();
689 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, mCanScrollVertical);
690 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, mCanScrollHorizontal);
692 UpdatePropertyDomain();
693 SetInternalConstraints();
696 void ScrollView::OnStageConnection( int depth )
698 ScrollBase::OnStageConnection( depth );
700 DALI_LOG_SCROLL_STATE("[0x%X]", this);
704 SetScrollSensitive( false );
705 SetScrollSensitive( true );
707 if(IsOvershootEnabled())
709 // try and make sure property notifications are set
710 EnableScrollOvershoot(true);
714 void ScrollView::OnStageDisconnection()
716 DALI_LOG_SCROLL_STATE("[0x%X]", this);
720 ScrollBase::OnStageDisconnection();
723 ScrollView::~ScrollView()
725 DALI_LOG_SCROLL_STATE("[0x%X]", this);
728 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
730 return mSnapAlphaFunction;
733 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
735 mSnapAlphaFunction = alpha;
738 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
740 return mFlickAlphaFunction;
743 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
745 mFlickAlphaFunction = alpha;
748 float ScrollView::GetScrollSnapDuration() const
750 return mSnapDuration;
753 void ScrollView::SetScrollSnapDuration(float time)
755 mSnapDuration = time;
758 float ScrollView::GetScrollFlickDuration() const
760 return mFlickDuration;
763 void ScrollView::SetScrollFlickDuration(float time)
765 mFlickDuration = time;
768 void ScrollView::ApplyEffect(Toolkit::ScrollViewEffect effect)
770 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
772 // Assertion check to ensure effect doesn't already exist in this scrollview
773 bool effectAlreadyExistsInScrollView(false);
774 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
778 effectAlreadyExistsInScrollView = true;
783 DALI_ASSERT_ALWAYS(!effectAlreadyExistsInScrollView);
785 // add effect to effects list
786 mEffects.push_back(effect);
788 // invoke Attachment request to ScrollView first
789 GetImpl(effect).Attach(self);
792 void ScrollView::RemoveEffect(Toolkit::ScrollViewEffect effect)
794 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
796 // remove effect from effects list
797 bool effectExistedInScrollView(false);
798 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
802 mEffects.erase(iter);
803 effectExistedInScrollView = true;
808 // Assertion check to ensure effect existed.
809 DALI_ASSERT_ALWAYS(effectExistedInScrollView);
811 // invoke Detachment request to ScrollView last
812 GetImpl(effect).Detach(self);
815 void ScrollView::RemoveAllEffects()
817 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
819 for (ScrollViewEffectIter effectIter = mEffects.begin(); effectIter != mEffects.end(); ++effectIter)
821 Toolkit::ScrollViewEffect effect = *effectIter;
823 // invoke Detachment request to ScrollView last
824 GetImpl(effect).Detach(self);
830 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
832 ApplyConstraintToBoundActors(constraint);
835 void ScrollView::RemoveConstraintsFromChildren()
837 RemoveConstraintsFromBoundActors();
840 const RulerPtr ScrollView::GetRulerX() const
845 const RulerPtr ScrollView::GetRulerY() const
850 void ScrollView::SetRulerX(RulerPtr ruler)
854 UpdatePropertyDomain();
855 UpdateMainInternalConstraint();
858 void ScrollView::SetRulerY(RulerPtr ruler)
862 UpdatePropertyDomain();
863 UpdateMainInternalConstraint();
866 void ScrollView::UpdatePropertyDomain()
869 Vector3 size = self.GetTargetSize();
870 Vector2 min = mMinScroll;
871 Vector2 max = mMaxScroll;
872 bool scrollPositionChanged = false;
873 bool domainChanged = false;
875 bool canScrollVertical = false;
876 bool canScrollHorizontal = false;
877 UpdateLocalScrollProperties();
878 if(mRulerX->IsEnabled())
880 const Toolkit::RulerDomain& rulerDomain = mRulerX->GetDomain();
881 if( fabsf(min.x - rulerDomain.min) > Math::MACHINE_EPSILON_100
882 || fabsf(max.x - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
884 domainChanged = true;
885 min.x = rulerDomain.min;
886 max.x = rulerDomain.max;
888 // make sure new scroll value is within new domain
889 if( mScrollPrePosition.x < min.x
890 || mScrollPrePosition.x > max.x )
892 scrollPositionChanged = true;
893 mScrollPrePosition.x = Clamp(mScrollPrePosition.x, -(max.x - size.x), -min.x);
896 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.x) > Math::MACHINE_EPSILON_100 )
898 canScrollHorizontal = true;
901 else if( fabs(min.x) > Math::MACHINE_EPSILON_100
902 || fabs(max.x) > Math::MACHINE_EPSILON_100 )
904 // need to reset to 0
905 domainChanged = true;
908 canScrollHorizontal = false;
911 if(mRulerY->IsEnabled())
913 const Toolkit::RulerDomain& rulerDomain = mRulerY->GetDomain();
914 if( fabsf(min.y - rulerDomain.min) > Math::MACHINE_EPSILON_100
915 || fabsf(max.y - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
917 domainChanged = true;
918 min.y = rulerDomain.min;
919 max.y = rulerDomain.max;
921 // make sure new scroll value is within new domain
922 if( mScrollPrePosition.y < min.y
923 || mScrollPrePosition.y > max.y )
925 scrollPositionChanged = true;
926 mScrollPrePosition.y = Clamp(mScrollPrePosition.y, -(max.y - size.y), -min.y);
929 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.y) > Math::MACHINE_EPSILON_100 )
931 canScrollVertical = true;
934 else if( fabs(min.y) > Math::MACHINE_EPSILON_100
935 || fabs(max.y) > Math::MACHINE_EPSILON_100 )
937 // need to reset to 0
938 domainChanged = true;
941 canScrollVertical = false;
944 // avoid setting properties if possible, otherwise this will cause an entire update as well as triggering constraints using each property we update
945 if( mCanScrollVertical != canScrollVertical )
947 mCanScrollVertical = canScrollVertical;
948 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, canScrollVertical);
950 if( mCanScrollHorizontal != canScrollHorizontal )
952 mCanScrollHorizontal = canScrollHorizontal;
953 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, canScrollHorizontal);
955 if( scrollPositionChanged )
957 DALI_LOG_SCROLL_STATE("[0x%X] Domain Changed, setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
958 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
964 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, mMinScroll );
965 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, mMaxScroll );
969 void ScrollView::SetScrollSensitive(bool sensitive)
972 PanGestureDetector panGesture( GetPanGestureDetector() );
974 DALI_LOG_SCROLL_STATE("[0x%X] sensitive: before:[%d] setting[%d]", this, int(mSensitive), int(sensitive));
976 if((!mSensitive) && (sensitive))
978 mSensitive = sensitive;
979 panGesture.Attach(self);
981 else if((mSensitive) && (!sensitive))
983 DALI_LOG_SCROLL_STATE("[0x%X] BEFORE: panning:[%d]", this, int(mPanning));
985 // while the scroll view is panning, the state needs to be reset.
988 PanGesture cancelGesture( Gesture::Cancelled );
989 OnPan( cancelGesture );
992 panGesture.Detach(self);
993 mSensitive = sensitive;
995 mGestureStackDepth = 0;
996 DALI_LOG_SCROLL_STATE("[0x%X] AFTER: panning:[%d]", this, int(mPanning));
1000 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
1002 mMaxOvershoot.x = overshootX;
1003 mMaxOvershoot.y = overshootY;
1004 mUserMaxOvershoot = mMaxOvershoot;
1005 mDefaultMaxOvershoot = false;
1006 UpdateMainInternalConstraint();
1009 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
1011 mSnapOvershootAlphaFunction = alpha;
1014 void ScrollView::SetSnapOvershootDuration(float duration)
1016 mSnapOvershootDuration = duration;
1019 void ScrollView::SetActorAutoSnap(bool enable)
1021 mActorAutoSnapEnabled = enable;
1024 void ScrollView::SetAutoResize(bool enable)
1026 mAutoResizeContainerEnabled = enable;
1027 // TODO: This needs a lot of issues to be addressed before working.
1030 bool ScrollView::GetWrapMode() const
1035 void ScrollView::SetWrapMode(bool enable)
1038 Self().SetProperty(Toolkit::ScrollView::Property::WRAP, enable);
1041 int ScrollView::GetScrollUpdateDistance() const
1043 return mScrollUpdateDistance;
1046 void ScrollView::SetScrollUpdateDistance(int distance)
1048 mScrollUpdateDistance = distance;
1051 bool ScrollView::GetAxisAutoLock() const
1053 return mAxisAutoLock;
1056 void ScrollView::SetAxisAutoLock(bool enable)
1058 mAxisAutoLock = enable;
1059 UpdateMainInternalConstraint();
1062 float ScrollView::GetAxisAutoLockGradient() const
1064 return mAxisAutoLockGradient;
1067 void ScrollView::SetAxisAutoLockGradient(float gradient)
1069 DALI_ASSERT_DEBUG( gradient >= 0.0f && gradient <= 1.0f );
1070 mAxisAutoLockGradient = gradient;
1071 UpdateMainInternalConstraint();
1074 float ScrollView::GetFrictionCoefficient() const
1076 return mFrictionCoefficient;
1079 void ScrollView::SetFrictionCoefficient(float friction)
1081 DALI_ASSERT_DEBUG( friction > 0.0f );
1082 mFrictionCoefficient = friction;
1085 float ScrollView::GetFlickSpeedCoefficient() const
1087 return mFlickSpeedCoefficient;
1090 void ScrollView::SetFlickSpeedCoefficient(float speed)
1092 mFlickSpeedCoefficient = speed;
1095 Vector2 ScrollView::GetMinimumDistanceForFlick() const
1097 return mMinFlickDistance;
1100 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
1102 mMinFlickDistance = distance;
1105 float ScrollView::GetMinimumSpeedForFlick() const
1107 return mFlickSpeedThreshold;
1110 void ScrollView::SetMinimumSpeedForFlick( float speed )
1112 mFlickSpeedThreshold = speed;
1115 float ScrollView::GetMaxFlickSpeed() const
1117 return mMaxFlickSpeed;
1120 void ScrollView::SetMaxFlickSpeed(float speed)
1122 mMaxFlickSpeed = speed;
1125 void ScrollView::SetWheelScrollDistanceStep(Vector2 step)
1127 mWheelScrollDistanceStep = step;
1130 Vector2 ScrollView::GetWheelScrollDistanceStep() const
1132 return mWheelScrollDistanceStep;
1135 unsigned int ScrollView::GetCurrentPage() const
1137 // in case animation is currently taking place.
1138 Vector2 position = GetPropertyPosition();
1140 Actor self = Self();
1141 unsigned int page = 0;
1142 unsigned int pagesPerVolume = 1;
1143 unsigned int volume = 0;
1145 // if rulerX is enabled, then get page count (columns)
1146 page = mRulerX->GetPageFromPosition(-position.x, mWrapMode);
1147 volume = mRulerY->GetPageFromPosition(-position.y, mWrapMode);
1148 pagesPerVolume = mRulerX->GetTotalPages();
1150 return volume * pagesPerVolume + page;
1153 Vector2 ScrollView::GetCurrentScrollPosition() const
1155 return -GetPropertyPosition();
1158 Vector2 ScrollView::GetDomainSize() const
1160 Vector3 size = Self().GetCurrentSize();
1162 const RulerDomain& xDomain = GetRulerX()->GetDomain();
1163 const RulerDomain& yDomain = GetRulerY()->GetDomain();
1166 domainSize.x = xDomain.max - xDomain.min - size.x;
1167 domainSize.y = yDomain.max - yDomain.min - size.y;
1171 void ScrollView::TransformTo(const Vector2& position,
1172 DirectionBias horizontalBias, DirectionBias verticalBias)
1174 TransformTo(position, mSnapDuration, mSnapAlphaFunction, horizontalBias, verticalBias);
1177 void ScrollView::TransformTo(const Vector2& position, float duration, AlphaFunction alpha,
1178 DirectionBias horizontalBias, DirectionBias verticalBias)
1180 // If this is called while the timer is running, then cancel it
1181 StopTouchDownTimer();
1183 Actor self( Self() );
1185 // Guard against destruction during signal emission
1186 // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo()
1187 Toolkit::ScrollView handle( GetOwner() );
1189 DALI_LOG_SCROLL_STATE("[0x%X] pos[%.2f,%.2f], duration[%.2f] bias[%d, %d]",
1190 this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1192 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1193 self.SetProperty( Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(currentScrollPosition) );
1195 if( mScrolling ) // are we interrupting a current scroll?
1197 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
1199 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1200 mScrollCompletedSignal.Emit( currentScrollPosition );
1203 if( mPanning ) // are we interrupting a current pan?
1205 DALI_LOG_SCROLL_STATE("[0x%X] Interrupting Pan, set to false", this );
1207 mGestureStackDepth = 0;
1208 self.SetProperty( Toolkit::ScrollView::Property::PANNING, false );
1210 if( mScrollMainInternalPrePositionConstraint )
1212 mScrollMainInternalPrePositionConstraint.Remove();
1216 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, true);
1219 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1220 mScrollStartedSignal.Emit( currentScrollPosition );
1221 bool animating = AnimateTo(-position,
1222 Vector2::ONE * duration,
1231 // if not animating, then this pan has completed right now.
1232 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
1235 // If we have no duration, then in the next update frame, we will be at the position specified as we just set.
1236 // In this scenario, we cannot return the currentScrollPosition as this is out-of-date and should instead return the requested final position
1237 Vector2 completedPosition( currentScrollPosition );
1238 if( duration <= Math::MACHINE_EPSILON_10 )
1240 completedPosition = position;
1243 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 2 [%.2f, %.2f]", this, completedPosition.x, completedPosition.y);
1244 SetScrollUpdateNotification(false);
1245 mScrollCompletedSignal.Emit( completedPosition );
1249 void ScrollView::ScrollTo(const Vector2& position)
1251 ScrollTo(position, mSnapDuration );
1254 void ScrollView::ScrollTo(const Vector2& position, float duration)
1256 ScrollTo(position, duration, DirectionBiasNone, DirectionBiasNone);
1259 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha)
1261 ScrollTo(position, duration, alpha, DirectionBiasNone, DirectionBiasNone);
1264 void ScrollView::ScrollTo(const Vector2& position, float duration,
1265 DirectionBias horizontalBias, DirectionBias verticalBias)
1267 ScrollTo(position, duration, mSnapAlphaFunction, horizontalBias, verticalBias);
1270 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha,
1271 DirectionBias horizontalBias, DirectionBias verticalBias)
1273 DALI_LOG_SCROLL_STATE("[0x%X] position[%.2f, %.2f] duration[%.2f], bias[%d, %d]", this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1274 TransformTo(position, duration, alpha, horizontalBias, verticalBias);
1277 void ScrollView::ScrollTo(unsigned int page)
1279 ScrollTo(page, mSnapDuration);
1282 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
1285 unsigned int volume;
1286 unsigned int libraries;
1288 // The position to scroll to is continuous and linear
1289 // unless a domain has been enabled on the X axis.
1290 // or if WrapMode has been enabled.
1291 bool carryX = mRulerX->GetDomain().enabled | mWrapMode;
1292 bool carryY = mRulerY->GetDomain().enabled | mWrapMode;
1294 position.x = mRulerX->GetPositionFromPage(page, volume, carryX);
1295 position.y = mRulerY->GetPositionFromPage(volume, libraries, carryY);
1297 ScrollTo(position, duration, bias, bias);
1300 void ScrollView::ScrollTo(Actor &actor)
1302 ScrollTo(actor, mSnapDuration);
1305 void ScrollView::ScrollTo(Actor &actor, float duration)
1307 DALI_ASSERT_ALWAYS(actor.GetParent() == Self());
1309 Actor self = Self();
1310 Vector3 size = self.GetCurrentSize();
1311 Vector3 position = actor.GetCurrentPosition();
1312 Vector2 prePosition = GetPropertyPrePosition();
1313 position.GetVectorXY() -= prePosition;
1315 ScrollTo(Vector2(position.x - size.width * 0.5f, position.y - size.height * 0.5f), duration);
1318 Actor ScrollView::FindClosestActor()
1320 Actor self = Self();
1321 Vector3 size = self.GetCurrentSize();
1323 return FindClosestActorToPosition(Vector3(size.width * 0.5f,size.height * 0.5f,0.0f));
1326 Actor ScrollView::FindClosestActorToPosition(const Vector3& position, FindDirection dirX, FindDirection dirY, FindDirection dirZ)
1329 float closestDistance2 = 0.0f;
1330 Vector3 actualPosition = position;
1332 unsigned int numChildren = Self().GetChildCount();
1334 for(unsigned int i = 0; i < numChildren; ++i)
1336 Actor child = Self().GetChildAt(i);
1338 if(mInternalActor == child) // ignore internal actor.
1343 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1345 Vector3 delta = childPosition - actualPosition;
1347 // X-axis checking (only find Actors to the [dirX] of actualPosition)
1348 if(dirX > All) // != All,None
1350 FindDirection deltaH = delta.x > 0 ? Right : Left;
1357 // Y-axis checking (only find Actors to the [dirY] of actualPosition)
1358 if(dirY > All) // != All,None
1360 FindDirection deltaV = delta.y > 0 ? Down : Up;
1367 // Z-axis checking (only find Actors to the [dirZ] of actualPosition)
1368 if(dirZ > All) // != All,None
1370 FindDirection deltaV = delta.y > 0 ? In : Out;
1377 // compare child to closest child in terms of distance.
1378 float distance2 = 0.0f;
1380 // distance2 = the Square of the relevant dimensions of delta
1383 distance2 += delta.x * delta.x;
1388 distance2 += delta.y * delta.y;
1393 distance2 += delta.z * delta.z;
1396 if(closestChild) // Next time.
1398 if(distance2 < closestDistance2)
1400 closestChild = child;
1401 closestDistance2 = distance2;
1406 closestChild = child;
1407 closestDistance2 = distance2;
1411 return closestChild;
1414 bool ScrollView::ScrollToSnapPoint()
1416 DALI_LOG_SCROLL_STATE("[0x%X]", this );
1417 Vector2 stationaryVelocity = Vector2(0.0f, 0.0f);
1418 return SnapWithVelocity( stationaryVelocity );
1421 // TODO: In situations where axes are different (X snap, Y free)
1422 // Each axis should really have their own independent animation (time and equation)
1423 // Consider, X axis snapping to nearest grid point (EaseOut over fixed time)
1424 // Consider, Y axis simulating physics to arrive at a point (Physics equation over variable time)
1425 // Currently, the axes have been split however, they both use the same EaseOut equation.
1426 bool ScrollView::SnapWithVelocity(Vector2 velocity)
1428 // Animator takes over now, touches are assumed not to interfere.
1429 // And if touches do interfere, then we'll stop animation, update PrePosition
1430 // to current mScroll's properties, and then resume.
1431 // Note: For Flicking this may work a bit different...
1433 float angle = atan2(velocity.y, velocity.x);
1434 float speed2 = velocity.LengthSquared();
1435 AlphaFunction alphaFunction = mSnapAlphaFunction;
1436 Vector2 positionDuration = Vector2::ONE * mSnapDuration;
1439 FindDirection horizontal = None;
1440 FindDirection vertical = None;
1442 // orthoAngleRange = Angle tolerance within the Exact N,E,S,W direction
1443 // that will be accepted as a general N,E,S,W flick direction.
1445 const float orthoAngleRange = FLICK_ORTHO_ANGLE_RANGE * M_PI / 180.0f;
1446 const float flickSpeedThreshold2 = mFlickSpeedThreshold * mFlickSpeedThreshold;
1448 Vector2 positionSnap = mScrollPrePosition;
1450 // Flick logic X Axis
1452 if(mRulerX->IsEnabled() && mLockAxis != LockHorizontal)
1456 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1457 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1459 if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East
1461 biasX = 0.0f, horizontal = Left;
1463 // This guards against an error where no movement occurs, due to the flick finishing
1464 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1465 positionSnap.x += 1.0f;
1467 else if((angle >= M_PI-orthoAngleRange) || (angle < -M_PI+orthoAngleRange)) // Swiping West
1469 biasX = 1.0f, horizontal = Right;
1471 // This guards against an error where no movement occurs, due to the flick finishing
1472 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1473 positionSnap.x -= 1.0f;
1478 // Flick logic Y Axis
1480 if(mRulerY->IsEnabled() && mLockAxis != LockVertical)
1484 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1485 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1487 if((angle >= M_PI_2-orthoAngleRange) && (angle < M_PI_2+orthoAngleRange)) // Swiping South
1489 biasY = 0.0f, vertical = Up;
1491 else if((angle >= -M_PI_2-orthoAngleRange) && (angle < -M_PI_2+orthoAngleRange)) // Swiping North
1493 biasY = 1.0f, vertical = Down;
1498 // isFlick: Whether this gesture is a flick or not.
1499 bool isFlick = (horizontal != All || vertical != All);
1500 // isFreeFlick: Whether this gesture is a flick under free panning criteria.
1501 bool isFreeFlick = velocity.LengthSquared() > (FREE_FLICK_SPEED_THRESHOLD*FREE_FLICK_SPEED_THRESHOLD);
1503 if(isFlick || isFreeFlick)
1505 positionDuration = Vector2::ONE * mFlickDuration;
1506 alphaFunction = mFlickAlphaFunction;
1509 // Calculate next positionSnap ////////////////////////////////////////////////////////////
1511 if(mActorAutoSnapEnabled)
1513 Vector3 size = Self().GetCurrentSize();
1515 Actor child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f), horizontal, vertical );
1517 if(!child && isFlick )
1519 // If we conducted a direction limited search and found no actor, then just snap to the closest actor.
1520 child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f) );
1525 Vector2 position = Self().GetProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_POSITION);
1527 // Get center-point of the Actor.
1528 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1530 if(mRulerX->IsEnabled())
1532 positionSnap.x = position.x - childPosition.x + size.width * 0.5f;
1534 if(mRulerY->IsEnabled())
1536 positionSnap.y = position.y - childPosition.y + size.height * 0.5f;
1541 Vector2 startPosition = positionSnap;
1542 positionSnap.x = -mRulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system.
1543 positionSnap.y = -mRulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
1545 Vector2 clampDelta(Vector2::ZERO);
1546 ClampPosition(positionSnap);
1548 if( (mRulerX->GetType() == Ruler::Free || mRulerY->GetType() == Ruler::Free)
1549 && isFreeFlick && !mActorAutoSnapEnabled)
1551 // Calculate target position based on velocity of flick.
1553 // a = Deceleration (Set to diagonal stage length * friction coefficient)
1554 // u = Initial Velocity (Flick velocity)
1555 // v = 0 (Final Velocity)
1556 // t = Time (Velocity / Deceleration)
1557 Vector2 stageSize = Stage::GetCurrent().GetSize();
1558 float stageLength = Vector3(stageSize.x, stageSize.y, 0.0f).Length();
1559 float a = (stageLength * mFrictionCoefficient);
1560 Vector3 u = Vector3(velocity.x, velocity.y, 0.0f) * mFlickSpeedCoefficient;
1561 float speed = u.Length();
1564 // TODO: Change this to a decay function. (faster you flick, the slower it should be)
1565 speed = std::min(speed, stageLength * mMaxFlickSpeed );
1567 alphaFunction = ConstantDecelerationAlphaFunction;
1569 float t = speed / a;
1571 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1573 positionSnap.x += t*u.x*0.5f;
1576 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1578 positionSnap.y += t*u.y*0.5f;
1581 clampDelta = positionSnap;
1582 ClampPosition(positionSnap);
1583 if((positionSnap - startPosition).LengthSquared() > Math::MACHINE_EPSILON_0)
1585 clampDelta -= positionSnap;
1586 clampDelta.x = clampDelta.x > 0.0f ? std::min(clampDelta.x, mMaxOvershoot.x) : std::max(clampDelta.x, -mMaxOvershoot.x);
1587 clampDelta.y = clampDelta.y > 0.0f ? std::min(clampDelta.y, mMaxOvershoot.y) : std::max(clampDelta.y, -mMaxOvershoot.y);
1591 clampDelta = Vector2::ZERO;
1594 // If Axis is Free and has velocity, then calculate time taken
1595 // to reach target based on velocity in axis.
1596 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1598 float deltaX = fabsf(startPosition.x - positionSnap.x);
1600 if(fabsf(u.x) > Math::MACHINE_EPSILON_1)
1602 positionDuration.x = fabsf(deltaX / u.x);
1606 positionDuration.x = 0;
1610 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1612 float deltaY = fabsf(startPosition.y - positionSnap.y);
1614 if(fabsf(u.y) > Math::MACHINE_EPSILON_1)
1616 positionDuration.y = fabsf(deltaY / u.y);
1620 positionDuration.y = 0;
1625 if(IsOvershootEnabled())
1627 // Scroll to the end of the overshoot only when overshoot is enabled.
1628 positionSnap += clampDelta;
1631 bool animating = AnimateTo(positionSnap, positionDuration,
1632 alphaFunction, false,
1633 DirectionBiasNone, DirectionBiasNone,
1634 isFlick || isFreeFlick ? Flick : Snap);
1639 void ScrollView::StopAnimation(void)
1641 // Clear Snap animation if exists.
1642 StopAnimation(mInternalXAnimation);
1643 StopAnimation(mInternalYAnimation);
1644 mScrollStateFlags = 0;
1645 // remove scroll animation flags
1646 HandleStoppedAnimation();
1649 void ScrollView::StopAnimation(Animation& animation)
1658 bool ScrollView::AnimateTo(const Vector2& position, const Vector2& positionDuration,
1659 AlphaFunction alpha, bool findShortcuts,
1660 DirectionBias horizontalBias, DirectionBias verticalBias,
1663 // Here we perform an animation on a number of properties (depending on which have changed)
1664 // The animation is applied to all ScrollBases
1665 Actor self = Self();
1666 mScrollTargetPosition = position;
1667 float totalDuration = 0.0f;
1669 bool positionChanged = (mScrollTargetPosition != mScrollPostPosition);
1673 totalDuration = std::max(totalDuration, positionDuration.x);
1674 totalDuration = std::max(totalDuration, positionDuration.y);
1678 // try to animate for a frame, on some occasions update will be changing scroll value while event side thinks it hasnt changed
1679 totalDuration = 0.01f;
1680 positionChanged = true;
1685 // Position Delta ///////////////////////////////////////////////////////
1688 if(mWrapMode && findShortcuts)
1690 // In Wrap Mode, the shortest distance is a little less intuitive...
1691 const RulerDomain rulerDomainX = mRulerX->GetDomain();
1692 const RulerDomain rulerDomainY = mRulerY->GetDomain();
1694 if(mRulerX->IsEnabled())
1696 float dir = VectorInDomain(-mScrollPrePosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
1697 mScrollTargetPosition.x = mScrollPrePosition.x + -dir;
1700 if(mRulerY->IsEnabled())
1702 float dir = VectorInDomain(-mScrollPrePosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
1703 mScrollTargetPosition.y = mScrollPrePosition.y + -dir;
1707 // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting
1708 // a horizonal/vertical wall.delay
1709 AnimateInternalXTo(mScrollTargetPosition.x, positionDuration.x, alpha);
1710 AnimateInternalYTo(mScrollTargetPosition.y, positionDuration.y, alpha);
1712 if( !(mScrollStateFlags & SCROLL_ANIMATION_FLAGS) )
1714 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollTargetPosition.x, mScrollTargetPosition.y );
1715 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollTargetPosition);
1716 mScrollPrePosition = mScrollTargetPosition;
1717 mScrollPostPosition = mScrollTargetPosition;
1718 WrapPosition(mScrollPostPosition);
1721 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 );
1722 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 );
1725 SetScrollUpdateNotification(true);
1727 // Always send a snap event when AnimateTo is called.
1728 Toolkit::ScrollView::SnapEvent snapEvent;
1729 snapEvent.type = snapType;
1730 snapEvent.position = -mScrollTargetPosition;
1731 snapEvent.duration = totalDuration;
1733 DALI_LOG_SCROLL_STATE("[0x%X] mSnapStartedSignal [%.2f, %.2f]", this, snapEvent.position.x, snapEvent.position.y);
1734 mSnapStartedSignal.Emit( snapEvent );
1736 return (mScrollStateFlags & SCROLL_ANIMATION_FLAGS) != 0;
1739 void ScrollView::EnableScrollOvershoot(bool enable)
1743 if (!mOvershootIndicator)
1745 mOvershootIndicator = ScrollOvershootIndicator::New();
1748 mOvershootIndicator->AttachToScrollable(*this);
1752 mMaxOvershoot = mUserMaxOvershoot;
1754 if (mOvershootIndicator)
1756 mOvershootIndicator->DetachFromScrollable(*this);
1760 UpdateMainInternalConstraint();
1763 void ScrollView::AddOverlay(Actor actor)
1765 actor.SetDrawMode( DrawMode::OVERLAY_2D );
1766 mInternalActor.Add( actor );
1769 void ScrollView::RemoveOverlay(Actor actor)
1771 mInternalActor.Remove( actor );
1774 void ScrollView::SetOvershootEffectColor( const Vector4& color )
1776 mOvershootEffectColor = color;
1777 if( mOvershootIndicator )
1779 mOvershootIndicator->SetOvershootEffectColor( color );
1783 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
1785 PanGestureDetector panGesture( GetPanGestureDetector() );
1787 // First remove just in case we have some set, then add.
1788 panGesture.RemoveDirection( direction );
1789 panGesture.AddDirection( direction, threshold );
1792 void ScrollView::RemoveScrollingDirection( Radian direction )
1794 PanGestureDetector panGesture( GetPanGestureDetector() );
1795 panGesture.RemoveDirection( direction );
1798 Toolkit::ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
1800 return mSnapStartedSignal;
1803 void ScrollView::FindAndUnbindActor(Actor child)
1808 Vector2 ScrollView::GetPropertyPrePosition() const
1810 Vector2 position = Self().GetProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION);
1811 WrapPosition(position);
1815 Vector2 ScrollView::GetPropertyPosition() const
1817 Vector2 position = Self().GetProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_POSITION);
1818 WrapPosition(position);
1823 void ScrollView::HandleStoppedAnimation()
1825 SetScrollUpdateNotification(false);
1828 void ScrollView::HandleSnapAnimationFinished()
1830 // Emit Signal that scrolling has completed.
1832 Actor self = Self();
1833 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
1835 Vector2 deltaPosition(mScrollPrePosition);
1837 UpdateLocalScrollProperties();
1838 WrapPosition(mScrollPrePosition);
1839 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
1840 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
1842 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1843 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 3 current[%.2f, %.2f], mScrollTargetPosition[%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y, -mScrollTargetPosition.x, -mScrollTargetPosition.y );
1844 mScrollCompletedSignal.Emit( currentScrollPosition );
1846 mDomainOffset += deltaPosition - mScrollPostPosition;
1847 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset);
1848 HandleStoppedAnimation();
1851 void ScrollView::SetScrollUpdateNotification( bool enabled )
1853 Actor self = Self();
1854 if( mScrollXUpdateNotification )
1856 // disconnect now to avoid a notification before removed from update thread
1857 mScrollXUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1858 self.RemovePropertyNotification(mScrollXUpdateNotification);
1859 mScrollXUpdateNotification.Reset();
1861 if( enabled && !mScrollUpdatedSignal.Empty())
1863 // Only set up the notification when the application has connected to the updated signal
1864 mScrollXUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 0, StepCondition(mScrollUpdateDistance, 0.0f));
1865 mScrollXUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1867 if( mScrollYUpdateNotification )
1869 // disconnect now to avoid a notification before removed from update thread
1870 mScrollYUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1871 self.RemovePropertyNotification(mScrollYUpdateNotification);
1872 mScrollYUpdateNotification.Reset();
1874 if( enabled && !mScrollUpdatedSignal.Empty())
1876 // Only set up the notification when the application has connected to the updated signal
1877 mScrollYUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 1, StepCondition(mScrollUpdateDistance, 0.0f));
1878 mScrollYUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1882 void ScrollView::OnScrollUpdateNotification(Dali::PropertyNotification& source)
1884 // Guard against destruction during signal emission
1885 Toolkit::ScrollView handle( GetOwner() );
1887 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1888 mScrollUpdatedSignal.Emit( currentScrollPosition );
1891 bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1893 Dali::BaseHandle handle( object );
1895 bool connected( true );
1896 Toolkit::ScrollView view = Toolkit::ScrollView::DownCast( handle );
1898 if( 0 == strcmp( signalName.c_str(), SIGNAL_SNAP_STARTED ) )
1900 view.SnapStartedSignal().Connect( tracker, functor );
1904 // signalName does not match any signal
1911 void ScrollView::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1913 // need to update domain properties for new size
1914 UpdatePropertyDomain();
1917 void ScrollView::OnSizeSet( const Vector3& size )
1919 // need to update domain properties for new size
1920 if( mDefaultMaxOvershoot )
1922 mUserMaxOvershoot.x = size.x * 0.5f;
1923 mUserMaxOvershoot.y = size.y * 0.5f;
1924 if( !IsOvershootEnabled() )
1926 mMaxOvershoot = mUserMaxOvershoot;
1929 UpdatePropertyDomain();
1930 UpdateMainInternalConstraint();
1931 if( IsOvershootEnabled() )
1933 mOvershootIndicator->Reset();
1937 void ScrollView::OnChildAdd(Actor& child)
1939 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
1942 mInternalActor.Add(scrollBar);
1943 if(scrollBar.GetScrollDirection() == Toolkit::ScrollBar::Horizontal)
1945 scrollBar.SetScrollPropertySource(Self(),
1946 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_X,
1947 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_X,
1948 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_X,
1949 Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_X);
1953 scrollBar.SetScrollPropertySource(Self(),
1954 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_Y,
1955 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
1956 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_Y,
1957 Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_Y);
1960 else if(mAlterChild)
1966 void ScrollView::OnChildRemove(Actor& child)
1968 // TODO: Actor needs a RemoveConstraint method to take out an individual constraint.
1972 void ScrollView::StartTouchDownTimer()
1974 if ( !mTouchDownTimer )
1976 mTouchDownTimer = Timer::New( TOUCH_DOWN_TIMER_INTERVAL );
1977 mTouchDownTimer.TickSignal().Connect( this, &ScrollView::OnTouchDownTimeout );
1980 mTouchDownTimer.Start();
1983 void ScrollView::StopTouchDownTimer()
1985 if ( mTouchDownTimer )
1987 mTouchDownTimer.Stop();
1991 bool ScrollView::OnTouchDownTimeout()
1993 DALI_LOG_SCROLL_STATE("[0x%X]", this);
1995 mTouchDownTimeoutReached = true;
1997 unsigned int currentScrollStateFlags( mScrollStateFlags ); // Cleared in StopAnimation so keep local copy for comparison
1998 if( currentScrollStateFlags & (SCROLL_ANIMATION_FLAGS | SNAP_ANIMATION_FLAGS) )
2000 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling Or snapping flags set, stopping animation", this);
2003 if( currentScrollStateFlags & SCROLL_ANIMATION_FLAGS )
2005 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling flags set, emitting signal", this);
2007 mScrollInterrupted = true;
2008 // reset domain offset as scrolling from original plane.
2009 mDomainOffset = Vector2::ZERO;
2010 Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, Vector2::ZERO);
2012 UpdateLocalScrollProperties();
2013 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2014 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 4 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2015 mScrollCompletedSignal.Emit( currentScrollPosition );
2022 bool ScrollView::OnTouchEvent(const TouchEvent& event)
2026 DALI_LOG_SCROLL_STATE("[0x%X], Not Sensitive, ignoring", this);
2028 // Ignore this touch event, if scrollview is insensitive.
2032 // Ignore events with multiple-touch points
2033 if (event.GetPointCount() != 1)
2035 DALI_LOG_SCROLL_STATE("[0x%X], multiple touch, ignoring", this);
2040 const TouchPoint::State pointState = event.GetPoint(0).state;
2041 if( pointState == TouchPoint::Down )
2043 DALI_LOG_SCROLL_STATE("[0x%X] Down", this);
2045 if(mGestureStackDepth==0)
2047 mTouchDownTime = event.time;
2049 // This allows time for a pan-gesture to start, to avoid breaking snap-animation behavior with fast flicks.
2050 // If touch-down does not become a pan (after timeout interval), then snap-animation can be interrupted.
2051 mTouchDownTimeoutReached = false;
2052 mScrollInterrupted = false;
2053 StartTouchDownTimer();
2056 else if( ( pointState == TouchPoint::Up ) ||
2057 ( ( pointState == TouchPoint::Interrupted ) && ( event.GetPoint(0).hitActor == Self() ) ) )
2059 DALI_LOG_SCROLL_STATE("[0x%X] %s", this, ( ( pointState == TouchPoint::Up ) ? "Up" : "Interrupted" ) );
2061 StopTouchDownTimer();
2063 // if the user touches and releases without enough movement to go
2064 // into a gesture state, then we should snap to nearest point.
2065 // otherwise our scroll could be stopped (interrupted) half way through an animation.
2066 if(mGestureStackDepth==0 && mTouchDownTimeoutReached)
2068 if( ( event.GetPoint(0).state == TouchPoint::Interrupted ) ||
2069 ( ( event.time - mTouchDownTime ) >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET ) )
2071 // Reset the velocity only if down was received a while ago
2072 mLastVelocity = Vector2( 0.0f, 0.0f );
2075 UpdateLocalScrollProperties();
2076 // Only finish the transform if scrolling was interrupted on down or if we are scrolling
2077 if ( mScrollInterrupted || mScrolling )
2079 DALI_LOG_SCROLL_STATE("[0x%X] Calling FinishTransform", this);
2084 mTouchDownTimeoutReached = false;
2085 mScrollInterrupted = false;
2091 bool ScrollView::OnWheelEvent(const WheelEvent& event)
2095 // Ignore this wheel event, if scrollview is insensitive.
2099 Vector2 targetScrollPosition = GetPropertyPosition();
2101 if(mRulerX->IsEnabled() && !mRulerY->IsEnabled())
2103 // If only the ruler in the X axis is enabled, scroll in the X axis.
2104 if(mRulerX->GetType() == Ruler::Free)
2106 // Free panning mode
2107 targetScrollPosition.x += event.z * mWheelScrollDistanceStep.x;
2108 ClampPosition(targetScrollPosition);
2109 ScrollTo(-targetScrollPosition);
2111 else if(!mScrolling)
2113 // Snap mode, only respond to the event when the previous snap animation is finished.
2114 ScrollTo(GetCurrentPage() - event.z);
2119 // If the ruler in the Y axis is enabled, scroll in the Y axis.
2120 if(mRulerY->GetType() == Ruler::Free)
2122 // Free panning mode
2123 targetScrollPosition.y += event.z * mWheelScrollDistanceStep.y;
2124 ClampPosition(targetScrollPosition);
2125 ScrollTo(-targetScrollPosition);
2127 else if(!mScrolling)
2129 // Snap mode, only respond to the event when the previous snap animation is finished.
2130 ScrollTo(GetCurrentPage() - event.z * mRulerX->GetTotalPages());
2137 void ScrollView::ResetScrolling()
2139 Actor self = Self();
2140 self.GetProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get(mScrollPostPosition);
2141 mScrollPrePosition = mScrollPostPosition;
2142 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPostPosition.x, mScrollPostPosition.y );
2143 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPostPosition);
2146 void ScrollView::UpdateLocalScrollProperties()
2148 Actor self = Self();
2149 self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get(mScrollPrePosition);
2150 self.GetProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get(mScrollPostPosition);
2153 // private functions
2155 void ScrollView::PreAnimatedScrollSetup()
2157 // SCROLL_PRE_POSITION is our unclamped property with wrapping
2158 // SCROLL_POSITION is our final scroll position after clamping
2160 Actor self = Self();
2162 Vector2 deltaPosition(mScrollPostPosition);
2163 WrapPosition(mScrollPostPosition);
2164 mDomainOffset += deltaPosition - mScrollPostPosition;
2165 Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset);
2167 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2169 // already performing animation on internal x position
2170 StopAnimation(mInternalXAnimation);
2173 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2175 // already performing animation on internal y position
2176 StopAnimation(mInternalYAnimation);
2179 mScrollStateFlags = 0;
2181 // Update Actor position with this wrapped value.
2184 void ScrollView::FinaliseAnimatedScroll()
2186 // TODO - common animation finishing code in here
2189 void ScrollView::AnimateInternalXTo( float position, float duration, AlphaFunction alpha )
2191 StopAnimation(mInternalXAnimation);
2193 if( duration > Math::MACHINE_EPSILON_10 )
2195 Actor self = Self();
2196 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get<Vector2>().x, position );
2197 mInternalXAnimation = Animation::New(duration);
2198 DALI_LOG_SCROLL_STATE("[0x%X], mInternalXAnimation[0x%X]", this, mInternalXAnimation.GetObjectPtr() );
2199 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2200 mInternalXAnimation.AnimateTo( Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position, alpha, TimePeriod(duration));
2201 mInternalXAnimation.Play();
2203 // erase current state flags
2204 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2205 // add internal animation state flag
2206 mScrollStateFlags |= AnimatingInternalX;
2210 void ScrollView::AnimateInternalYTo( float position, float duration, AlphaFunction alpha )
2212 StopAnimation(mInternalYAnimation);
2214 if( duration > Math::MACHINE_EPSILON_10 )
2216 Actor self = Self();
2217 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get<Vector2>().y, position );
2218 mInternalYAnimation = Animation::New(duration);
2219 DALI_LOG_SCROLL_STATE("[0x%X], mInternalYAnimation[0x%X]", this, mInternalYAnimation.GetObjectPtr() );
2220 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2221 mInternalYAnimation.AnimateTo( Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position, alpha, TimePeriod(duration));
2222 mInternalYAnimation.Play();
2224 // erase current state flags
2225 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2226 // add internal animation state flag
2227 mScrollStateFlags |= AnimatingInternalY;
2231 void ScrollView::OnScrollAnimationFinished( Animation& source )
2233 // Guard against destruction during signal emission
2234 // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished()
2235 Toolkit::ScrollView handle( GetOwner() );
2237 bool scrollingFinished = false;
2239 // update our local scroll positions
2240 UpdateLocalScrollProperties();
2242 if( source == mInternalXAnimation )
2244 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 );
2246 if( !(mScrollStateFlags & AnimatingInternalY) )
2248 scrollingFinished = true;
2250 mInternalXAnimation.Reset();
2251 // wrap pre scroll x position and set it
2254 const RulerDomain rulerDomain = mRulerX->GetDomain();
2255 mScrollPrePosition.x = -WrapInDomain(-mScrollPrePosition.x, rulerDomain.min, rulerDomain.max);
2256 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
2257 handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
2259 SnapInternalXTo(mScrollPostPosition.x);
2262 if( source == mInternalYAnimation )
2264 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 );
2266 if( !(mScrollStateFlags & AnimatingInternalX) )
2268 scrollingFinished = true;
2270 mInternalYAnimation.Reset();
2273 // wrap pre scroll y position and set it
2274 const RulerDomain rulerDomain = mRulerY->GetDomain();
2275 mScrollPrePosition.y = -WrapInDomain(-mScrollPrePosition.y, rulerDomain.min, rulerDomain.max);
2276 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
2277 handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
2279 SnapInternalYTo(mScrollPostPosition.y);
2282 DALI_LOG_SCROLL_STATE("[0x%X] scrollingFinished[%d] Animation[0x%X]", this, scrollingFinished, source.GetObjectPtr());
2284 if(scrollingFinished)
2286 HandleSnapAnimationFinished();
2290 void ScrollView::OnSnapInternalPositionFinished( Animation& source )
2292 Actor self = Self();
2293 UpdateLocalScrollProperties();
2294 if( source == mInternalXAnimation )
2296 DALI_LOG_SCROLL_STATE("[0x%X] Finished X PostPosition Animation", this );
2298 // clear internal x animation flags
2299 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2300 mInternalXAnimation.Reset();
2301 WrapPosition(mScrollPrePosition);
2303 if( source == mInternalYAnimation )
2305 DALI_LOG_SCROLL_STATE("[0x%X] Finished Y PostPosition Animation", this );
2307 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2308 mInternalYAnimation.Reset();
2309 WrapPosition(mScrollPrePosition);
2313 void ScrollView::SnapInternalXTo(float position)
2315 Actor self = Self();
2317 StopAnimation(mInternalXAnimation);
2319 // erase current state flags
2320 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2322 // if internal x not equal to inputed parameter, animate it
2323 float duration = std::min(fabsf((position - mScrollPrePosition.x) / mMaxOvershoot.x) * mSnapOvershootDuration, mSnapOvershootDuration);
2324 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration );
2325 if( duration > Math::MACHINE_EPSILON_1 )
2327 DALI_LOG_SCROLL_STATE("[0x%X] Starting X Snap Animation to[%.2f]", this, position );
2329 mInternalXAnimation = Animation::New(duration);
2330 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2331 mInternalXAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position);
2332 mInternalXAnimation.Play();
2334 // add internal animation state flag
2335 mScrollStateFlags |= SnappingInternalX;
2339 void ScrollView::SnapInternalYTo(float position)
2341 Actor self = Self();
2343 StopAnimation(mInternalYAnimation);
2345 // erase current state flags
2346 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2348 // if internal y not equal to inputed parameter, animate it
2349 float duration = std::min(fabsf((position - mScrollPrePosition.y) / mMaxOvershoot.y) * mSnapOvershootDuration, mSnapOvershootDuration);
2350 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration );
2351 if( duration > Math::MACHINE_EPSILON_1 )
2353 DALI_LOG_SCROLL_STATE("[0x%X] Starting Y Snap Animation to[%.2f]", this, position );
2355 mInternalYAnimation = Animation::New(duration);
2356 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2357 mInternalYAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position);
2358 mInternalYAnimation.Play();
2360 // add internal animation state flag
2361 mScrollStateFlags |= SnappingInternalY;
2365 void ScrollView::GestureStarted()
2367 // we handle the first gesture.
2368 // if we're currently doing a gesture and receive another
2369 // we continue and combine the effects of the gesture instead of reseting.
2370 if(mGestureStackDepth++==0)
2372 Actor self = Self();
2373 StopTouchDownTimer();
2375 mPanDelta = Vector2::ZERO;
2376 mLastVelocity = Vector2::ZERO;
2379 mLockAxis = LockPossible;
2382 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2384 StopAnimation(mInternalXAnimation);
2386 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2388 StopAnimation(mInternalYAnimation);
2390 mScrollStateFlags = 0;
2392 if(mScrolling) // are we interrupting a current scroll?
2394 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
2396 // send negative scroll position since scroll internal scroll position works as an offset for actors,
2397 // give applications the position within the domain from the scroll view's anchor position
2398 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 5 [%.2f, %.2f]", this, -mScrollPostPosition.x, -mScrollPostPosition.y);
2399 mScrollCompletedSignal.Emit( -mScrollPostPosition );
2404 void ScrollView::GestureContinuing(const Vector2& panDelta)
2406 mPanDelta.x+= panDelta.x;
2407 mPanDelta.y+= panDelta.y;
2409 // Save the velocity, there is a bug in PanGesture
2410 // Whereby the Gesture::Finished's velocity is either:
2411 // NaN (due to time delta of zero between the last two events)
2412 // or 0 (due to position being the same between the last two events)
2414 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
2415 // appears mostly horizontal or mostly vertical respectively.
2418 mLockAxis = GetLockAxis(mPanDelta, mLockAxis, mAxisAutoLockGradient);
2419 } // end if mAxisAutoLock
2422 // TODO: Upgrade to use a more powerful gesture detector (one that supports multiple touches on pan - so works as pan and flick gesture)
2423 // BUG: Gesture::Finished doesn't always return velocity on release (due to
2424 // timeDelta between last two events being 0 sometimes, or posiiton being the same)
2425 void ScrollView::OnPan( const PanGesture& gesture )
2427 // Guard against destruction during signal emission
2428 // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx()
2429 Actor self( Self() );
2433 DALI_LOG_SCROLL_STATE("[0x%X] Pan Ignored, Insensitive", this);
2435 // If another callback on the same original signal disables sensitivity,
2436 // this callback will still be called, so we must suppress it.
2440 // translate Gesture input to get useful data...
2441 switch(gesture.state)
2443 case Gesture::Started:
2445 DALI_LOG_SCROLL_STATE("[0x%X] Pan Started", this);
2446 mPanStartPosition = gesture.position - gesture.displacement;
2447 UpdateLocalScrollProperties();
2450 self.SetProperty( Toolkit::ScrollView::Property::PANNING, true );
2451 self.SetProperty( Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(gesture.position.x, gesture.position.y, 0.0f) );
2453 UpdateMainInternalConstraint();
2457 case Gesture::Continuing:
2461 DALI_LOG_SCROLL_STATE("[0x%X] Pan Continuing", this);
2462 GestureContinuing(gesture.screenDisplacement);
2466 // If we do not think we are panning, then we should not do anything here
2472 case Gesture::Finished:
2473 case Gesture::Cancelled:
2477 DALI_LOG_SCROLL_STATE("[0x%X] Pan %s", this, ( ( gesture.state == Gesture::Finished ) ? "Finished" : "Cancelled" ) );
2479 UpdateLocalScrollProperties();
2480 mLastVelocity = gesture.velocity;
2482 self.SetProperty( Toolkit::ScrollView::Property::PANNING, false );
2484 if( mScrollMainInternalPrePositionConstraint )
2486 mScrollMainInternalPrePositionConstraint.Remove();
2491 // If we do not think we are panning, then we should not do anything here
2497 case Gesture::Possible:
2498 case Gesture::Clear:
2500 // Nothing to do, not needed.
2504 } // end switch(gesture.state)
2506 OnGestureEx(gesture.state);
2509 void ScrollView::OnGestureEx(Gesture::State state)
2511 // call necessary signals for application developer
2513 if(state == Gesture::Started)
2515 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2516 Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, true);
2518 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 2 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2519 mScrollStartedSignal.Emit( currentScrollPosition );
2521 else if( (state == Gesture::Finished) ||
2522 (state == Gesture::Cancelled) ) // Finished/default
2524 // when all the gestures have finished, we finish the transform.
2525 // so if a user decides to pan (1 gesture), and then pan+zoom (2 gestures)
2526 // then stop panning (back to 1 gesture), and then stop zooming (0 gestures).
2527 // this is the point we end, and perform necessary snapping.
2528 mGestureStackDepth--;
2529 if(mGestureStackDepth==0)
2531 // no flick if we have not exceeded min flick distance
2532 if( (fabsf(mPanDelta.x) < mMinFlickDistance.x)
2533 && (fabsf(mPanDelta.y) < mMinFlickDistance.y) )
2535 // reset flick velocity
2536 mLastVelocity = Vector2::ZERO;
2542 DALI_LOG_SCROLL_STATE("[0x%X] mGestureStackDepth[%d]", this, mGestureStackDepth);
2547 void ScrollView::FinishTransform()
2549 // at this stage internal x and x scroll position should have followed prescroll position exactly
2550 Actor self = Self();
2552 PreAnimatedScrollSetup();
2554 // convert pixels/millisecond to pixels per second
2555 bool animating = SnapWithVelocity(mLastVelocity * 1000.0f);
2559 // if not animating, then this pan has completed right now.
2560 SetScrollUpdateNotification(false);
2562 Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
2564 if( fabs(mScrollPrePosition.x - mScrollTargetPosition.x) > Math::MACHINE_EPSILON_10 )
2566 SnapInternalXTo(mScrollTargetPosition.x);
2568 if( fabs(mScrollPrePosition.y - mScrollTargetPosition.y) > Math::MACHINE_EPSILON_10 )
2570 SnapInternalYTo(mScrollTargetPosition.y);
2572 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2573 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 6 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2574 mScrollCompletedSignal.Emit( currentScrollPosition );
2578 Vector2 ScrollView::GetOvershoot(Vector2& position) const
2580 Vector3 size = Self().GetCurrentSize();
2583 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2584 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2586 if(mRulerX->IsEnabled() && rulerDomainX.enabled)
2588 const float left = rulerDomainX.min - position.x;
2589 const float right = size.width - rulerDomainX.max - position.x;
2596 overshoot.x = right;
2600 if(mRulerY->IsEnabled() && rulerDomainY.enabled)
2602 const float top = rulerDomainY.min - position.y;
2603 const float bottom = size.height - rulerDomainY.max - position.y;
2610 overshoot.y = bottom;
2617 bool ScrollView::OnAccessibilityPan(PanGesture gesture)
2619 // Keep track of whether this is an AccessibilityPan
2620 mInAccessibilityPan = true;
2622 mInAccessibilityPan = false;
2627 void ScrollView::ClampPosition(Vector2& position) const
2629 ClampState2D clamped;
2630 ClampPosition(position, clamped);
2633 void ScrollView::ClampPosition(Vector2& position, ClampState2D &clamped) const
2635 Vector3 size = Self().GetCurrentSize();
2637 position.x = -mRulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system.
2638 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.
2641 void ScrollView::WrapPosition(Vector2& position) const
2645 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2646 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2648 if(mRulerX->IsEnabled())
2650 position.x = -WrapInDomain(-position.x, rulerDomainX.min, rulerDomainX.max);
2653 if(mRulerY->IsEnabled())
2655 position.y = -WrapInDomain(-position.y, rulerDomainY.min, rulerDomainY.max);
2660 void ScrollView::UpdateMainInternalConstraint()
2662 // TODO: Only update the constraints which have changed, rather than remove all and add all again.
2663 // Requires a dali-core ApplyConstraintAt, or a ReplaceConstraint. The former is probably more flexible.
2664 Actor self = Self();
2665 PanGestureDetector detector( GetPanGestureDetector() );
2667 if(mScrollMainInternalPositionConstraint)
2669 mScrollMainInternalPositionConstraint.Remove();
2670 mScrollMainInternalDeltaConstraint.Remove();
2671 mScrollMainInternalFinalConstraint.Remove();
2672 mScrollMainInternalRelativeConstraint.Remove();
2673 mScrollMainInternalDomainConstraint.Remove();
2674 mScrollMainInternalPrePositionMaxConstraint.Remove();
2676 if( mScrollMainInternalPrePositionConstraint )
2678 mScrollMainInternalPrePositionConstraint.Remove();
2681 // TODO: It's probably better to use a local displacement value as this will give a displacement when scrolling just commences
2682 // but we need to make sure than the gesture system gives displacement since last frame (60Hz), not displacement since last touch event (90Hz).
2684 // 1. First calculate the pre-position (this is the scroll position if no clamping has taken place)
2685 Vector2 initialPanMask = Vector2(mRulerX->IsEnabled() ? 1.0f : 0.0f, mRulerY->IsEnabled() ? 1.0f : 0.0f);
2687 if( mLockAxis == LockVertical )
2689 initialPanMask.y = 0.0f;
2691 else if( mLockAxis == LockHorizontal )
2693 initialPanMask.x = 0.0f;
2698 mScrollMainInternalPrePositionConstraint = Constraint::New<Vector2>( self,
2699 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION,
2700 InternalPrePositionConstraint( mPanStartPosition,
2703 mAxisAutoLockGradient,
2708 mScrollMainInternalPrePositionConstraint.AddSource( Source( detector, PanGestureDetector::Property::LOCAL_POSITION ) );
2709 mScrollMainInternalPrePositionConstraint.AddSource( Source( detector, PanGestureDetector::Property::PANNING ) );
2710 mScrollMainInternalPrePositionConstraint.AddSource( Source( self, Actor::Property::SIZE ) );
2711 mScrollMainInternalPrePositionConstraint.Apply();
2714 // 2. Second calculate the clamped position (actual position)
2715 mScrollMainInternalPositionConstraint = Constraint::New<Vector2>( self,
2716 Toolkit::ScrollView::Property::SCROLL_POSITION,
2717 InternalPositionConstraint( mRulerX->GetDomain(),
2718 mRulerY->GetDomain(),
2720 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2721 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2722 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2723 mScrollMainInternalPositionConstraint.AddSource( Source( self, Actor::Property::SIZE ) );
2724 mScrollMainInternalPositionConstraint.Apply();
2726 mScrollMainInternalDeltaConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_POSITION_DELTA, InternalPositionDeltaConstraint );
2727 mScrollMainInternalDeltaConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2728 mScrollMainInternalDeltaConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET ) );
2729 mScrollMainInternalDeltaConstraint.Apply();
2731 mScrollMainInternalFinalConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_FINAL,
2732 InternalFinalConstraint( FinalDefaultAlphaFunction,
2733 FinalDefaultAlphaFunction ) );
2734 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2735 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::OVERSHOOT_X ) );
2736 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::OVERSHOOT_Y ) );
2737 mScrollMainInternalFinalConstraint.Apply();
2739 mScrollMainInternalRelativeConstraint = Constraint::New<Vector2>( self, Toolkit::Scrollable::Property::SCROLL_RELATIVE_POSITION, InternalRelativePositionConstraint );
2740 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2741 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2742 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2743 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2744 mScrollMainInternalRelativeConstraint.Apply();
2746 mScrollMainInternalDomainConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE, InternalScrollDomainConstraint );
2747 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2748 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2749 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2750 mScrollMainInternalDomainConstraint.Apply();
2752 mScrollMainInternalPrePositionMaxConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX, InternalPrePositionMaxConstraint );
2753 mScrollMainInternalPrePositionMaxConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2754 mScrollMainInternalPrePositionMaxConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2755 mScrollMainInternalPrePositionMaxConstraint.Apply();
2757 // When panning we want to make sure overshoot values are affected by pre position and post position
2758 SetOvershootConstraintsEnabled(!mWrapMode);
2761 void ScrollView::SetOvershootConstraintsEnabled(bool enabled)
2763 Actor self( Self() );
2764 // remove and reset, it may now be in wrong order with the main internal constraints
2765 if( mScrollMainInternalOvershootXConstraint )
2767 mScrollMainInternalOvershootXConstraint.Remove();
2768 mScrollMainInternalOvershootXConstraint.Reset();
2769 mScrollMainInternalOvershootYConstraint.Remove();
2770 mScrollMainInternalOvershootYConstraint.Reset();
2774 mScrollMainInternalOvershootXConstraint= Constraint::New<float>( self, Toolkit::ScrollView::Property::OVERSHOOT_X, OvershootXConstraint(mMaxOvershoot.x) );
2775 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2776 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2777 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL ) );
2778 mScrollMainInternalOvershootXConstraint.Apply();
2780 mScrollMainInternalOvershootYConstraint = Constraint::New<float>( self, Toolkit::ScrollView::Property::OVERSHOOT_Y, OvershootYConstraint(mMaxOvershoot.y) );
2781 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2782 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2783 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
2784 mScrollMainInternalOvershootYConstraint.Apply();
2788 self.SetProperty(Toolkit::ScrollView::Property::OVERSHOOT_X, 0.0f);
2789 self.SetProperty(Toolkit::ScrollView::Property::OVERSHOOT_Y, 0.0f);
2793 void ScrollView::SetInternalConstraints()
2795 // Internal constraints (applied to target ScrollBase Actor itself) /////////
2796 UpdateMainInternalConstraint();
2798 // User definable constraints to apply to all child actors //////////////////
2799 Actor self = Self();
2801 // Apply some default constraints to ScrollView & its bound actors
2802 // Movement + Wrap function
2804 Constraint constraint;
2806 // MoveActor (scrolling)
2807 constraint = Constraint::New<Vector3>( self, Actor::Property::POSITION, MoveActorConstraint );
2808 constraint.AddSource( Source( self, Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2809 constraint.SetRemoveAction(Constraint::Discard);
2810 ApplyConstraintToBoundActors(constraint);
2812 // WrapActor (wrap functionality)
2813 constraint = Constraint::New<Vector3>( self, Actor::Property::POSITION, WrapActorConstraint );
2814 constraint.AddSource( LocalSource( Actor::Property::SCALE ) );
2815 constraint.AddSource( LocalSource( Actor::Property::ANCHOR_POINT ) );
2816 constraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2817 constraint.AddSource( Source( self, Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2818 constraint.AddSource( Source( self, Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2819 constraint.AddSource( Source( self, Toolkit::ScrollView::Property::WRAP ) );
2820 constraint.SetRemoveAction(Constraint::Discard);
2821 ApplyConstraintToBoundActors(constraint);
2824 } // namespace Internal
2826 } // namespace Toolkit