2 * Copyright (c) 2020 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/devel-api/actors/actor-devel.h>
24 #include <dali/public-api/animation/constraints.h>
25 #include <dali/devel-api/common/stage.h>
26 #include <dali/public-api/events/wheel-event.h>
27 #include <dali/public-api/events/touch-event.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/public-api/object/type-registry-helper.h>
30 #include <dali/public-api/object/property-map.h>
31 #include <dali/devel-api/object/property-helper-devel.h>
32 #include <dali/devel-api/events/pan-gesture-devel.h>
33 #include <dali/integration-api/debug.h>
36 #include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
37 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view.h>
38 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view-constraints.h>
39 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-mode.h>
40 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
41 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-effect-impl.h>
43 //#define ENABLED_SCROLL_STATE_LOGGING
45 #ifdef ENABLED_SCROLL_STATE_LOGGING
46 #define DALI_LOG_SCROLL_STATE(format, ...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d " format "\n", __PRETTY_FUNCTION__, __LINE__, ## __VA_ARGS__)
48 #define DALI_LOG_SCROLL_STATE(format, ...)
51 // TODO: Change to two class system:
52 // 1. DraggableActor (is an actor which can be dragged anywhere, can be set to range using the ruler)
53 // 2. ScrollView (contains a draggable actor that can a) be dragged in the negative X, and Y domain, b) has a hitArea for touches)
54 // TODO: external components (page and status overlays).
56 // TODO: upgrade Vector2/3 to support returning Unit vectors, normals, & cross product (dot product is already provided)
62 const float DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f); ///< Default Drag-Release animation time.
63 const float DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f); ///< Default Drag-Flick animation time.
64 const float DEFAULT_SNAP_OVERSHOOT_DURATION(0.5f); ///< Default Overshoot snapping animation time.
65 const float DEFAULT_MAX_OVERSHOOT(100.0f); ///< Default maximum allowed overshoot in pixels
67 const float DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f); ///< Default Axis-AutoLock gradient threshold. default is 0.36:1 (20 degrees)
68 const float DEFAULT_FRICTION_COEFFICIENT(1.0f); ///< Default Friction Co-efficient. (in stage diagonals per second)
69 const float DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f); ///< Default Flick speed coefficient (multiples input touch velocity)
70 const float DEFAULT_MAX_FLICK_SPEED(3.0f); ///< Default Maximum flick speed. (in stage diagonals per second)
72 const Vector2 DEFAULT_MIN_FLICK_DISTANCE(30.0f, 30.0f); ///< minimum distance for pan before flick allowed
73 const float DEFAULT_MIN_FLICK_SPEED_THRESHOLD(500.0f); ///< Minimum pan speed required for flick in pixels/s
74 const float FREE_FLICK_SPEED_THRESHOLD = 200.0f; ///< Free-Flick threshold in pixels/ms
75 const float AUTOLOCK_AXIS_MINIMUM_DISTANCE2 = 100.0f; ///< Auto-lock axis after minimum distance squared.
76 const float FLICK_ORTHO_ANGLE_RANGE = 75.0f; ///< degrees. (if >45, then supports diagonal flicking)
77 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.
78 const unsigned long MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET( 150u );
79 const float TOUCH_DOWN_TIMER_INTERVAL = 100.0f;
80 const float DEFAULT_SCROLL_UPDATE_DISTANCE( 30.0f ); ///< Default distance to travel in pixels for scroll update signal
82 const std::string INTERNAL_MAX_POSITION_PROPERTY_NAME( "internalMaxPosition" );
84 // Helpers ////////////////////////////////////////////////////////////////////////////////////////
87 * Find the vector (distance) from (a) to (b)
88 * in domain (start) to (end)
89 * (\ / start) (\ / end)
92 * @note assumes both (a) and (b) are already with the domain
95 * @param[in] a the current point
96 * @param[in] b the target point
97 * @param[in] start the start of the domain
98 * @param[in] end the end of the domain
99 * @param[in] bias whether to only take the right direction or the left direction,
100 * or the shortest direction.
101 * @return the shortest direction and distance
103 float VectorInDomain(float a, float b, float start, float end, Dali::Toolkit::DirectionBias bias)
105 if(bias == Dali::Toolkit::DIRECTION_BIAS_NONE)
107 return ShortestDistanceInDomain( a, b, start, end );
110 float size = end-start;
116 if(bias == Dali::Toolkit::DIRECTION_BIAS_RIGHT) // going right, take the vector.
122 float aRight = a+size;
129 if(bias == Dali::Toolkit::DIRECTION_BIAS_LEFT) // going left, take the vector.
135 float aLeft = a-size;
142 * Returns the position of the anchor within actor
144 * @param actor The Actor
145 * @param anchor The Anchor point of interest.
146 * @return The position of the Anchor
148 Vector3 GetPositionOfAnchor(Actor &actor, const Vector3 &anchor)
150 Vector3 childPosition = actor.GetCurrentProperty< Vector3 >( Actor::Property::POSITION );
151 Vector3 childAnchor = - actor.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) + anchor;
152 Vector3 childSize = actor.GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
154 return childPosition + childAnchor * childSize;
157 // AlphaFunctions /////////////////////////////////////////////////////////////////////////////////
159 float FinalDefaultAlphaFunction(float offset)
161 return offset * 0.5f;
165 * ConstantDecelerationAlphaFunction
166 * Newtoninan distance for constant deceleration
167 * v = 1 - t, s = t - 1/2 t^2
168 * when t = 0, s = 0.0 (min distance)
169 * when t = 1, s = 0.5 (max distance)
170 * progress = s / (max-min) = 2t - t^2
172 * @param[in] offset The input progress
173 * @return The output progress
175 float ConstantDecelerationAlphaFunction(float progress)
177 return progress * 2.0f - progress * progress;
180 // Internal Constraints ///////////////////////////////////////////////////////////////////////////
183 * Internal Relative position Constraint
184 * Generates the relative position value of the scroll view
185 * based on the absolute position, and it's relation to the
186 * scroll domain. This is a value from 0.0f to 1.0f in each
187 * scroll position axis.
189 void InternalRelativePositionConstraint( Vector2& relativePosition, const PropertyInputContainer& inputs)
191 Vector2 position = -inputs[0]->GetVector2();
192 const Vector2& min = inputs[1]->GetVector2();
193 const Vector2& max = inputs[2]->GetVector2();
194 const Vector3& size = inputs[3]->GetVector3();
196 position.x = WrapInDomain(position.x, min.x, max.x);
197 position.y = WrapInDomain(position.y, min.y, max.y);
199 Vector2 domainSize = (max - min) - size.GetVectorXY();
201 relativePosition.x = domainSize.x > Math::MACHINE_EPSILON_1 ? fabsf((position.x - min.x) / domainSize.x) : 0.0f;
202 relativePosition.y = domainSize.y > Math::MACHINE_EPSILON_1 ? fabsf((position.y - min.y) / domainSize.y) : 0.0f;
206 * Internal scroll domain Constraint
207 * Generates the scroll domain of the scroll view.
209 void InternalScrollDomainConstraint( Vector2& scrollDomain, const PropertyInputContainer& inputs)
211 const Vector2& min = inputs[0]->GetVector2();
212 const Vector2& max = inputs[1]->GetVector2();
213 const Vector3& size = inputs[2]->GetVector3();
215 scrollDomain = (max - min) - size.GetVectorXY();
219 * Internal maximum scroll position Constraint
220 * Generates the maximum scroll position of the scroll view.
222 void InternalPrePositionMaxConstraint( Vector2& scrollMax, const PropertyInputContainer& inputs)
224 const Vector2& max = inputs[0]->GetVector2();
225 const Vector3& size = inputs[1]->GetVector3();
227 scrollMax = max - size.GetVectorXY();
230 } // unnamed namespace
246 return Toolkit::ScrollView::New();
249 // Setup properties, signals and actions using the type-registry.
250 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ScrollView, Toolkit::Scrollable, Create )
252 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollView, "wrapEnabled", BOOLEAN, WRAP_ENABLED )
253 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollView, "panningEnabled", BOOLEAN, PANNING_ENABLED )
254 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollView, "axisAutoLockEnabled", BOOLEAN, AXIS_AUTO_LOCK_ENABLED )
255 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollView, "wheelScrollDistanceStep", VECTOR2, WHEEL_SCROLL_DISTANCE_STEP )
256 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollMode", MAP, SCROLL_MODE )
258 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPosition", VECTOR2, SCROLL_POSITION)
259 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPrePosition", VECTOR2, SCROLL_PRE_POSITION)
260 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionX", SCROLL_PRE_POSITION_X, SCROLL_PRE_POSITION, 0)
261 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionY", SCROLL_PRE_POSITION_Y, SCROLL_PRE_POSITION, 1)
262 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionMax", VECTOR2, SCROLL_PRE_POSITION_MAX)
263 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionMaxX", SCROLL_PRE_POSITION_MAX_X, SCROLL_PRE_POSITION_MAX, 0)
264 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionMaxY", SCROLL_PRE_POSITION_MAX_Y, SCROLL_PRE_POSITION_MAX, 1)
265 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "overshootX", FLOAT, OVERSHOOT_X)
266 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "overshootY", FLOAT, OVERSHOOT_Y)
267 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollFinal", VECTOR2, SCROLL_FINAL)
268 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollFinalX", SCROLL_FINAL_X, SCROLL_FINAL,0)
269 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollFinalY", SCROLL_FINAL_Y, SCROLL_FINAL,1)
270 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "wrap", BOOLEAN, WRAP)
271 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "panning", BOOLEAN, PANNING)
272 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrolling", BOOLEAN, SCROLLING)
273 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollDomainSize", VECTOR2, SCROLL_DOMAIN_SIZE)
274 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollDomainSizeX", SCROLL_DOMAIN_SIZE_X, SCROLL_DOMAIN_SIZE, 0)
275 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollDomainSizeY", SCROLL_DOMAIN_SIZE_Y, SCROLL_DOMAIN_SIZE, 1)
276 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollDomainOffset", VECTOR2, SCROLL_DOMAIN_OFFSET)
277 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPositionDelta", VECTOR2, SCROLL_POSITION_DELTA)
278 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "startPagePosition", VECTOR3, START_PAGE_POSITION)
280 DALI_SIGNAL_REGISTRATION( Toolkit, ScrollView, "valueChanged", SIGNAL_SNAP_STARTED )
282 DALI_TYPE_REGISTRATION_END()
285 * Returns whether to lock scrolling to a particular axis
287 * @param[in] panDelta Distance panned since gesture started
288 * @param[in] currentLockAxis The current lock axis value
289 * @param[in] lockGradient How quickly to lock to a particular axis
291 * @return The new axis lock state
293 ScrollView::LockAxis GetLockAxis(const Vector2& panDelta, ScrollView::LockAxis currentLockAxis, float lockGradient)
295 if(panDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 &&
296 currentLockAxis == ScrollView::LockPossible)
298 float dx = fabsf(panDelta.x);
299 float dy = fabsf(panDelta.y);
300 if(dx * lockGradient >= dy)
302 // 0.36:1 gradient to the horizontal (deviate < 20 degrees)
303 currentLockAxis = ScrollView::LockVertical;
305 else if(dy * lockGradient > dx)
307 // 0.36:1 gradient to the vertical (deviate < 20 degrees)
308 currentLockAxis = ScrollView::LockHorizontal;
312 currentLockAxis = ScrollView::LockNone;
315 return currentLockAxis;
319 * Internal Pre-Position Property Constraint.
321 * Generates position property based on current position + gesture displacement.
322 * Or generates position property based on positionX/Y.
323 * Note: This is the position prior to any clamping at scroll boundaries.
325 struct InternalPrePositionConstraint
327 InternalPrePositionConstraint( const Vector2& initialPanPosition,
328 const Vector2& initialPanMask,
330 float axisAutoLockGradient,
331 ScrollView::LockAxis initialLockAxis,
332 const Vector2& maxOvershoot,
333 const RulerPtr& rulerX, const RulerPtr& rulerY )
334 : mLocalStart( initialPanPosition ),
335 mInitialPanMask( initialPanMask ),
336 mMaxOvershoot( maxOvershoot ),
337 mAxisAutoLockGradient( axisAutoLockGradient ),
338 mLockAxis( initialLockAxis ),
339 mAxisAutoLock( axisAutoLock ),
342 const RulerDomain& rulerDomainX = rulerX->GetDomain();
343 const RulerDomain& rulerDomainY = rulerY->GetDomain();
344 mDomainMin = Vector2( rulerDomainX.min, -rulerDomainY.min );
345 mDomainMax = Vector2( -rulerDomainX.max, -rulerDomainY.max );
346 mClampX = rulerDomainX.enabled;
347 mClampY = rulerDomainY.enabled;
348 mFixedRulerX = rulerX->GetType() == Ruler::FIXED;
349 mFixedRulerY = rulerY->GetType() == Ruler::FIXED;
352 void operator()( Vector2& scrollPostPosition, const PropertyInputContainer& inputs )
354 const Vector2& panPosition = inputs[0]->GetVector2();
355 const bool& inGesture = inputs[1]->GetBoolean();
357 // First check if we are within a gesture.
358 // The ScrollView may have received a start gesture from ::OnPan()
359 // while the finish gesture is received now in this constraint.
360 // This gesture must then be rejected as the value will be "old".
361 // Typically the last value from the end of the last gesture.
362 // If we are rejecting the gesture, we simply don't modify the constraint target.
367 mPrePosition = scrollPostPosition;
368 mStartPosition = mPrePosition;
369 mCurrentPanMask = mInitialPanMask;
373 // Calculate Deltas...
374 const Vector2& currentPosition = panPosition;
375 Vector2 panDelta( currentPosition - mLocalStart );
377 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
378 // appears mostly horizontal or mostly vertical respectively...
381 mLockAxis = GetLockAxis( panDelta, mLockAxis, mAxisAutoLockGradient );
382 if( mLockAxis == ScrollView::LockVertical )
384 mCurrentPanMask.y = 0.0f;
386 else if( mLockAxis == ScrollView::LockHorizontal )
388 mCurrentPanMask.x = 0.0f;
392 // Restrict deltas based on ruler enable/disable and axis-lock state...
393 panDelta *= mCurrentPanMask;
395 // Perform Position transform based on input deltas...
396 scrollPostPosition = mPrePosition;
397 scrollPostPosition += panDelta;
399 // if no wrapping then clamp preposition to maximum overshoot amount
400 const Vector3& size = inputs[2]->GetVector3();
403 float newXPosition = Clamp( scrollPostPosition.x, ( mDomainMax.x + size.x ) - mMaxOvershoot.x, mDomainMin.x + mMaxOvershoot.x );
404 if( (newXPosition < scrollPostPosition.x - Math::MACHINE_EPSILON_1)
405 || (newXPosition > scrollPostPosition.x + Math::MACHINE_EPSILON_1) )
407 mPrePosition.x = newXPosition;
408 mLocalStart.x = panPosition.x;
410 scrollPostPosition.x = newXPosition;
414 float newYPosition = Clamp( scrollPostPosition.y, ( mDomainMax.y + size.y ) - mMaxOvershoot.y, mDomainMin.y + mMaxOvershoot.y );
415 if( ( newYPosition < scrollPostPosition.y - Math::MACHINE_EPSILON_1 )
416 || ( newYPosition > scrollPostPosition.y + Math::MACHINE_EPSILON_1 ) )
418 mPrePosition.y = newYPosition;
419 mLocalStart.y = panPosition.y;
421 scrollPostPosition.y = newYPosition;
424 // If we are using a fixed ruler in a particular axis, limit the maximum pages scrolled on that axis.
425 if( mFixedRulerX || mFixedRulerY )
427 // Here we limit the maximum amount that can be moved from the starting position of the gesture to one page.
428 // We do this only if we have a fixed ruler (on that axis) and the mode is enabled.
429 // Note: 1.0f is subtracted to keep the value within one page size (otherwise we stray on to the page after).
430 // Note: A further 1.0f is subtracted to handle a compensation that happens later within the flick handling code in SnapWithVelocity().
431 // When a flick is completed, an adjustment of 1.0f is sometimes made to allow for the scenario where:
432 // A flick finishes before the update thread has advanced the scroll position past the previous snap point.
433 Vector2 viewPageSizeLimit( size.x - ( 1.0f + 1.0f ), size.y - ( 1.0f - 1.0f ) );
434 Vector2 minPosition( mStartPosition.x - viewPageSizeLimit.x, mStartPosition.y - viewPageSizeLimit.y );
435 Vector2 maxPosition( mStartPosition.x + viewPageSizeLimit.x, mStartPosition.y + viewPageSizeLimit.y );
439 scrollPostPosition.x = Clamp( scrollPostPosition.x, minPosition.x, maxPosition.x );
443 scrollPostPosition.y = Clamp( scrollPostPosition.y, minPosition.y, maxPosition.y );
449 Vector2 mPrePosition;
451 Vector2 mStartPosition; ///< The start position of the gesture - used to limit scroll amount (not modified by clamping).
452 Vector2 mInitialPanMask; ///< Initial pan mask (based on ruler settings).
453 Vector2 mCurrentPanMask; ///< Current pan mask that can be altered by axis lock mode.
456 Vector2 mMaxOvershoot;
458 float mAxisAutoLockGradient; ///< Set by ScrollView
459 ScrollView::LockAxis mLockAxis;
461 bool mAxisAutoLock:1; ///< Set by ScrollView
470 * Internal Position Property Constraint.
472 * Generates position property based on pre-position
473 * Note: This is the position after clamping.
474 * (uses result of InternalPrePositionConstraint)
476 struct InternalPositionConstraint
478 InternalPositionConstraint(const RulerDomain& domainX, const RulerDomain& domainY, bool wrap)
479 : mDomainMin( -domainX.min, -domainY.min ),
480 mDomainMax( -domainX.max, -domainY.max ),
481 mClampX( domainX.enabled ),
482 mClampY( domainY.enabled ),
487 void operator()( Vector2& position, const PropertyInputContainer& inputs )
489 position = inputs[0]->GetVector2();
490 const Vector2& size = inputs[3]->GetVector3().GetVectorXY();
491 const Vector2& min = inputs[1]->GetVector2();
492 const Vector2& max = inputs[2]->GetVector2();
496 position.x = -WrapInDomain(-position.x, min.x, max.x);
497 position.y = -WrapInDomain(-position.y, min.y, max.y);
501 // clamp post position to domain
502 position.x = mClampX ? Clamp(position.x, mDomainMax.x + size.x, mDomainMin.x ) : position.x;
503 position.y = mClampY ? Clamp(position.y, mDomainMax.y + size.y, mDomainMin.y ) : position.y;
516 * This constraint updates the X overshoot property using the difference
517 * SCROLL_PRE_POSITION.x and SCROLL_POSITION.x, returning a relative value between 0.0f and 1.0f
519 struct OvershootXConstraint
521 OvershootXConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
523 void operator()( float& current, const PropertyInputContainer& inputs )
525 if( inputs[2]->GetBoolean() )
527 const Vector2& scrollPrePosition = inputs[0]->GetVector2();
528 const Vector2& scrollPostPosition = inputs[1]->GetVector2();
529 float newOvershoot = scrollPrePosition.x - scrollPostPosition.x;
530 current = (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
542 * This constraint updates the Y overshoot property using the difference
543 * SCROLL_PRE_POSITION.y and SCROLL_POSITION.y, returning a relative value between 0.0f and 1.0f
545 struct OvershootYConstraint
547 OvershootYConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
549 void operator()( float& current, const PropertyInputContainer& inputs )
551 if( inputs[2]->GetBoolean() )
553 const Vector2& scrollPrePosition = inputs[0]->GetVector2();
554 const Vector2& scrollPostPosition = inputs[1]->GetVector2();
555 float newOvershoot = scrollPrePosition.y - scrollPostPosition.y;
556 current = (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
568 * Internal Position-Delta Property Constraint.
570 * Generates position-delta property based on scroll-position + scroll-offset properties.
572 void InternalPositionDeltaConstraint( Vector2& current, const PropertyInputContainer& inputs )
574 const Vector2& scrollPosition = inputs[0]->GetVector2();
575 const Vector2& scrollOffset = inputs[1]->GetVector2();
577 current = scrollPosition + scrollOffset;
581 * Internal Final Position Constraint
582 * The position of content is:
583 * of scroll-position + f(scroll-overshoot)
584 * where f(...) function defines how overshoot
585 * should affect final-position.
587 struct InternalFinalConstraint
589 InternalFinalConstraint(AlphaFunctionPrototype functionX,
590 AlphaFunctionPrototype functionY)
591 : mFunctionX(functionX),
592 mFunctionY(functionY)
596 void operator()( Vector2& current, const PropertyInputContainer& inputs )
598 const float& overshootx = inputs[1]->GetFloat();
599 const float& overshooty = inputs[2]->GetFloat();
600 Vector2 offset( mFunctionX(overshootx),
601 mFunctionY(overshooty) );
603 current = inputs[0]->GetVector2() - offset;
606 AlphaFunctionPrototype mFunctionX;
607 AlphaFunctionPrototype mFunctionY;
613 ///////////////////////////////////////////////////////////////////////////////////////////////////
615 ///////////////////////////////////////////////////////////////////////////////////////////////////
617 Dali::Toolkit::ScrollView ScrollView::New()
619 // Create the implementation
620 ScrollViewPtr scrollView(new ScrollView());
622 // Pass ownership to CustomActor via derived handle
623 Dali::Toolkit::ScrollView handle(*scrollView);
625 // Second-phase init of the implementation
626 // This can only be done after the CustomActor connection has been made...
627 scrollView->Initialize();
632 ScrollView::ScrollView()
633 : ScrollBase( ControlBehaviour( DISABLE_STYLE_CHANGE_SIGNALS ) ), // Enable size negotiation
635 mGestureStackDepth(0),
636 mScrollStateFlags(0),
637 mLockAxis(LockPossible),
638 mScrollUpdateDistance(DEFAULT_SCROLL_UPDATE_DISTANCE),
639 mMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT),
640 mUserMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT),
641 mSnapOvershootDuration(DEFAULT_SNAP_OVERSHOOT_DURATION),
642 mSnapOvershootAlphaFunction(AlphaFunction::EASE_OUT),
643 mSnapDuration(DEFAULT_SLOW_SNAP_ANIMATION_DURATION),
644 mSnapAlphaFunction(AlphaFunction::EASE_OUT),
645 mMinFlickDistance(DEFAULT_MIN_FLICK_DISTANCE),
646 mFlickSpeedThreshold(DEFAULT_MIN_FLICK_SPEED_THRESHOLD),
647 mFlickDuration(DEFAULT_FAST_SNAP_ANIMATION_DURATION),
648 mFlickAlphaFunction(AlphaFunction::EASE_OUT),
649 mAxisAutoLockGradient(DEFAULT_AXIS_AUTO_LOCK_GRADIENT),
650 mFrictionCoefficient(DEFAULT_FRICTION_COEFFICIENT),
651 mFlickSpeedCoefficient(DEFAULT_FLICK_SPEED_COEFFICIENT),
652 mMaxFlickSpeed(DEFAULT_MAX_FLICK_SPEED),
653 mWheelScrollDistanceStep(Vector2::ZERO),
654 mInAccessibilityPan(false),
656 mScrollInterrupted(false),
659 mTouchDownTimeoutReached(false),
660 mActorAutoSnapEnabled(false),
661 mAutoResizeContainerEnabled(false),
663 mAxisAutoLock(false),
665 mDefaultMaxOvershoot(true),
666 mCanScrollHorizontal(true),
667 mCanScrollVertical(true),
668 mTransientScrollBar(true)
672 void ScrollView::OnInitialize()
676 // Internal Actor, used to hide actors from enumerations.
677 // Also actors added to Internal actor appear as overlays e.g. ScrollBar components.
678 mInternalActor = Actor::New();
679 self.Add(mInternalActor);
681 mInternalActor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER );
682 mInternalActor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
683 mInternalActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
687 mScrollPostPosition = mScrollPrePosition = Vector2::ZERO;
689 mWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
691 mGestureStackDepth = 0;
693 self.TouchedSignal().Connect( this, &ScrollView::OnTouch );
694 EnableGestureDetection( GestureType::Value( GestureType::PAN ) );
696 // By default we'll allow the user to freely drag the scroll view,
697 // while disabling the other rulers.
698 RulerPtr ruler = new DefaultRuler();
702 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, mCanScrollVertical);
703 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, mCanScrollHorizontal);
705 UpdatePropertyDomain();
706 SetInternalConstraints();
708 // Connect wheel event
709 self.WheelEventSignal().Connect( this, &ScrollView::OnWheelEvent );
711 DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor ) {
712 return std::unique_ptr< Dali::Accessibility::Accessible >(
713 new AccessibleImpl( actor, Dali::Accessibility::Role::SCROLL_PANE ) );
717 void ScrollView::OnSceneConnection( int depth )
719 DALI_LOG_SCROLL_STATE("[0x%X]", this);
723 SetScrollSensitive( false );
724 SetScrollSensitive( true );
727 if(IsOvershootEnabled())
729 // try and make sure property notifications are set
730 EnableScrollOvershoot(true);
733 ScrollBase::OnSceneConnection( depth );
736 void ScrollView::OnSceneDisconnection()
738 DALI_LOG_SCROLL_STATE("[0x%X]", this);
742 ScrollBase::OnSceneDisconnection();
745 ScrollView::~ScrollView()
747 DALI_LOG_SCROLL_STATE("[0x%X]", this);
750 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
752 return mSnapAlphaFunction;
755 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
757 mSnapAlphaFunction = alpha;
760 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
762 return mFlickAlphaFunction;
765 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
767 mFlickAlphaFunction = alpha;
770 float ScrollView::GetScrollSnapDuration() const
772 return mSnapDuration;
775 void ScrollView::SetScrollSnapDuration(float time)
777 mSnapDuration = time;
780 float ScrollView::GetScrollFlickDuration() const
782 return mFlickDuration;
785 void ScrollView::SetScrollFlickDuration(float time)
787 mFlickDuration = time;
790 void ScrollView::ApplyEffect(Toolkit::ScrollViewEffect effect)
792 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
794 // Assertion check to ensure effect doesn't already exist in this scrollview
795 bool effectAlreadyExistsInScrollView(false);
796 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
800 effectAlreadyExistsInScrollView = true;
805 DALI_ASSERT_ALWAYS(!effectAlreadyExistsInScrollView);
807 // add effect to effects list
808 mEffects.push_back(effect);
810 // invoke Attachment request to ScrollView first
811 GetImpl(effect).Attach(self);
814 void ScrollView::RemoveEffect(Toolkit::ScrollViewEffect effect)
816 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
818 // remove effect from effects list
819 bool effectExistedInScrollView(false);
820 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
824 mEffects.erase(iter);
825 effectExistedInScrollView = true;
830 // Assertion check to ensure effect existed.
831 DALI_ASSERT_ALWAYS(effectExistedInScrollView);
833 // invoke Detachment request to ScrollView last
834 GetImpl(effect).Detach(self);
837 void ScrollView::RemoveAllEffects()
839 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
841 for (ScrollViewEffectIter effectIter = mEffects.begin(); effectIter != mEffects.end(); ++effectIter)
843 Toolkit::ScrollViewEffect effect = *effectIter;
845 // invoke Detachment request to ScrollView last
846 GetImpl(effect).Detach(self);
852 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
854 ApplyConstraintToBoundActors(constraint);
857 void ScrollView::RemoveConstraintsFromChildren()
859 RemoveConstraintsFromBoundActors();
862 const RulerPtr ScrollView::GetRulerX() const
867 const RulerPtr ScrollView::GetRulerY() const
872 void ScrollView::SetRulerX(RulerPtr ruler)
876 UpdatePropertyDomain();
877 UpdateMainInternalConstraint();
880 void ScrollView::SetRulerY(RulerPtr ruler)
884 UpdatePropertyDomain();
885 UpdateMainInternalConstraint();
888 void ScrollView::UpdatePropertyDomain()
891 Vector3 size = self.GetTargetSize();
892 Vector2 min = mMinScroll;
893 Vector2 max = mMaxScroll;
894 bool scrollPositionChanged = false;
895 bool domainChanged = false;
897 bool canScrollVertical = false;
898 bool canScrollHorizontal = false;
899 UpdateLocalScrollProperties();
900 if(mRulerX->IsEnabled())
902 const Toolkit::RulerDomain& rulerDomain = mRulerX->GetDomain();
903 if( fabsf(min.x - rulerDomain.min) > Math::MACHINE_EPSILON_100
904 || fabsf(max.x - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
906 domainChanged = true;
907 min.x = rulerDomain.min;
908 max.x = rulerDomain.max;
910 // make sure new scroll value is within new domain
911 if( mScrollPrePosition.x < min.x
912 || mScrollPrePosition.x > max.x )
914 scrollPositionChanged = true;
915 mScrollPrePosition.x = Clamp(mScrollPrePosition.x, -(max.x - size.x), -min.x);
918 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.x) > Math::MACHINE_EPSILON_100 )
920 canScrollHorizontal = true;
923 else if( fabs(min.x) > Math::MACHINE_EPSILON_100
924 || fabs(max.x) > Math::MACHINE_EPSILON_100 )
926 // need to reset to 0
927 domainChanged = true;
930 canScrollHorizontal = false;
933 if(mRulerY->IsEnabled())
935 const Toolkit::RulerDomain& rulerDomain = mRulerY->GetDomain();
936 if( fabsf(min.y - rulerDomain.min) > Math::MACHINE_EPSILON_100
937 || fabsf(max.y - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
939 domainChanged = true;
940 min.y = rulerDomain.min;
941 max.y = rulerDomain.max;
943 // make sure new scroll value is within new domain
944 if( mScrollPrePosition.y < min.y
945 || mScrollPrePosition.y > max.y )
947 scrollPositionChanged = true;
948 mScrollPrePosition.y = Clamp(mScrollPrePosition.y, -(max.y - size.y), -min.y);
951 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.y) > Math::MACHINE_EPSILON_100 )
953 canScrollVertical = true;
956 else if( fabs(min.y) > Math::MACHINE_EPSILON_100
957 || fabs(max.y) > Math::MACHINE_EPSILON_100 )
959 // need to reset to 0
960 domainChanged = true;
963 canScrollVertical = false;
966 // avoid setting properties if possible, otherwise this will cause an entire update as well as triggering constraints using each property we update
967 if( mCanScrollVertical != canScrollVertical )
969 mCanScrollVertical = canScrollVertical;
970 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, canScrollVertical);
972 if( mCanScrollHorizontal != canScrollHorizontal )
974 mCanScrollHorizontal = canScrollHorizontal;
975 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, canScrollHorizontal);
977 if( scrollPositionChanged )
979 DALI_LOG_SCROLL_STATE("[0x%X] Domain Changed, setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
980 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
986 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, mMinScroll );
987 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, mMaxScroll );
991 bool ScrollView::GetScrollSensitive()
996 void ScrollView::SetScrollSensitive(bool sensitive)
999 PanGestureDetector panGesture( GetPanGestureDetector() );
1001 DALI_LOG_SCROLL_STATE("[0x%X] sensitive: before:[%d] setting[%d]", this, int(mSensitive), int(sensitive));
1003 if((!mSensitive) && (sensitive))
1005 mSensitive = sensitive;
1006 panGesture.Attach(self);
1008 else if((mSensitive) && (!sensitive))
1010 DALI_LOG_SCROLL_STATE("[0x%X] BEFORE: panning:[%d]", this, int(mPanning));
1012 // while the scroll view is panning, the state needs to be reset.
1015 PanGesture cancelGesture = DevelPanGesture::New( GestureState::CANCELLED );
1016 OnPan( cancelGesture );
1019 panGesture.Detach(self);
1020 mSensitive = sensitive;
1022 mGestureStackDepth = 0;
1023 DALI_LOG_SCROLL_STATE("[0x%X] AFTER: panning:[%d]", this, int(mPanning));
1027 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
1029 mMaxOvershoot.x = overshootX;
1030 mMaxOvershoot.y = overshootY;
1031 mUserMaxOvershoot = mMaxOvershoot;
1032 mDefaultMaxOvershoot = false;
1033 UpdateMainInternalConstraint();
1036 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
1038 mSnapOvershootAlphaFunction = alpha;
1041 float ScrollView::GetSnapOvershootDuration()
1043 return mSnapOvershootDuration;
1046 void ScrollView::SetSnapOvershootDuration(float duration)
1048 mSnapOvershootDuration = duration;
1051 bool ScrollView::GetActorAutoSnap()
1053 return mActorAutoSnapEnabled;
1056 void ScrollView::SetActorAutoSnap(bool enable)
1058 mActorAutoSnapEnabled = enable;
1061 void ScrollView::SetAutoResize(bool enable)
1063 mAutoResizeContainerEnabled = enable;
1064 // TODO: This needs a lot of issues to be addressed before working.
1067 bool ScrollView::GetWrapMode() const
1072 void ScrollView::SetWrapMode(bool enable)
1075 Self().SetProperty(Toolkit::ScrollView::Property::WRAP, enable);
1078 int ScrollView::GetScrollUpdateDistance() const
1080 return mScrollUpdateDistance;
1083 void ScrollView::SetScrollUpdateDistance(int distance)
1085 mScrollUpdateDistance = distance;
1088 bool ScrollView::GetAxisAutoLock() const
1090 return mAxisAutoLock;
1093 void ScrollView::SetAxisAutoLock(bool enable)
1095 mAxisAutoLock = enable;
1096 UpdateMainInternalConstraint();
1099 float ScrollView::GetAxisAutoLockGradient() const
1101 return mAxisAutoLockGradient;
1104 void ScrollView::SetAxisAutoLockGradient(float gradient)
1106 DALI_ASSERT_DEBUG( gradient >= 0.0f && gradient <= 1.0f );
1107 mAxisAutoLockGradient = gradient;
1108 UpdateMainInternalConstraint();
1111 float ScrollView::GetFrictionCoefficient() const
1113 return mFrictionCoefficient;
1116 void ScrollView::SetFrictionCoefficient(float friction)
1118 DALI_ASSERT_DEBUG( friction > 0.0f );
1119 mFrictionCoefficient = friction;
1122 float ScrollView::GetFlickSpeedCoefficient() const
1124 return mFlickSpeedCoefficient;
1127 void ScrollView::SetFlickSpeedCoefficient(float speed)
1129 mFlickSpeedCoefficient = speed;
1132 Vector2 ScrollView::GetMinimumDistanceForFlick() const
1134 return mMinFlickDistance;
1137 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
1139 mMinFlickDistance = distance;
1142 float ScrollView::GetMinimumSpeedForFlick() const
1144 return mFlickSpeedThreshold;
1147 void ScrollView::SetMinimumSpeedForFlick( float speed )
1149 mFlickSpeedThreshold = speed;
1152 float ScrollView::GetMaxFlickSpeed() const
1154 return mMaxFlickSpeed;
1157 void ScrollView::SetMaxFlickSpeed(float speed)
1159 mMaxFlickSpeed = speed;
1162 void ScrollView::SetWheelScrollDistanceStep(Vector2 step)
1164 mWheelScrollDistanceStep = step;
1167 Vector2 ScrollView::GetWheelScrollDistanceStep() const
1169 return mWheelScrollDistanceStep;
1172 unsigned int ScrollView::GetCurrentPage() const
1174 // in case animation is currently taking place.
1175 Vector2 position = GetPropertyPosition();
1177 Actor self = Self();
1178 unsigned int page = 0;
1179 unsigned int pagesPerVolume = 1;
1180 unsigned int volume = 0;
1182 // if rulerX is enabled, then get page count (columns)
1183 page = mRulerX->GetPageFromPosition(-position.x, mWrapMode);
1184 volume = mRulerY->GetPageFromPosition(-position.y, mWrapMode);
1185 pagesPerVolume = mRulerX->GetTotalPages();
1187 return volume * pagesPerVolume + page;
1190 Vector2 ScrollView::GetCurrentScrollPosition() const
1192 return -GetPropertyPosition();
1195 void ScrollView::TransformTo(const Vector2& position,
1196 DirectionBias horizontalBias, DirectionBias verticalBias)
1198 TransformTo(position, mSnapDuration, mSnapAlphaFunction, horizontalBias, verticalBias);
1201 void ScrollView::TransformTo(const Vector2& position, float duration, AlphaFunction alpha,
1202 DirectionBias horizontalBias, DirectionBias verticalBias)
1204 // If this is called while the timer is running, then cancel it
1205 StopTouchDownTimer();
1207 Actor self( Self() );
1209 // Guard against destruction during signal emission
1210 // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo()
1211 Toolkit::ScrollView handle( GetOwner() );
1213 DALI_LOG_SCROLL_STATE("[0x%X] pos[%.2f,%.2f], duration[%.2f] bias[%d, %d]",
1214 this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1216 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1217 self.SetProperty( Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(currentScrollPosition) );
1219 if( mScrolling ) // are we interrupting a current scroll?
1221 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
1223 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1224 mScrollCompletedSignal.Emit( currentScrollPosition );
1227 if( mPanning ) // are we interrupting a current pan?
1229 DALI_LOG_SCROLL_STATE("[0x%X] Interrupting Pan, set to false", this );
1231 mGestureStackDepth = 0;
1232 self.SetProperty( Toolkit::ScrollView::Property::PANNING, false );
1234 if( mScrollMainInternalPrePositionConstraint )
1236 mScrollMainInternalPrePositionConstraint.Remove();
1240 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, true);
1243 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1244 mScrollStartedSignal.Emit( currentScrollPosition );
1245 bool animating = AnimateTo(-position,
1246 Vector2::ONE * duration,
1255 // if not animating, then this pan has completed right now.
1256 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
1259 // If we have no duration, then in the next update frame, we will be at the position specified as we just set.
1260 // In this scenario, we cannot return the currentScrollPosition as this is out-of-date and should instead return the requested final position
1261 Vector2 completedPosition( currentScrollPosition );
1262 if( duration <= Math::MACHINE_EPSILON_10 )
1264 completedPosition = position;
1267 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 2 [%.2f, %.2f]", this, completedPosition.x, completedPosition.y);
1268 SetScrollUpdateNotification(false);
1269 mScrollCompletedSignal.Emit( completedPosition );
1273 void ScrollView::ScrollTo(const Vector2& position)
1275 ScrollTo(position, mSnapDuration );
1278 void ScrollView::ScrollTo(const Vector2& position, float duration)
1280 ScrollTo(position, duration, DIRECTION_BIAS_NONE, DIRECTION_BIAS_NONE);
1283 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha)
1285 ScrollTo(position, duration, alpha, DIRECTION_BIAS_NONE, DIRECTION_BIAS_NONE);
1288 void ScrollView::ScrollTo(const Vector2& position, float duration,
1289 DirectionBias horizontalBias, DirectionBias verticalBias)
1291 ScrollTo(position, duration, mSnapAlphaFunction, horizontalBias, verticalBias);
1294 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha,
1295 DirectionBias horizontalBias, DirectionBias verticalBias)
1297 DALI_LOG_SCROLL_STATE("[0x%X] position[%.2f, %.2f] duration[%.2f], bias[%d, %d]", this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1298 TransformTo(position, duration, alpha, horizontalBias, verticalBias);
1301 void ScrollView::ScrollTo(unsigned int page)
1303 ScrollTo(page, mSnapDuration);
1306 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
1309 unsigned int volume;
1310 unsigned int libraries;
1312 // The position to scroll to is continuous and linear
1313 // unless a domain has been enabled on the X axis.
1314 // or if WrapMode has been enabled.
1315 bool carryX = mRulerX->GetDomain().enabled | mWrapMode;
1316 bool carryY = mRulerY->GetDomain().enabled | mWrapMode;
1318 position.x = mRulerX->GetPositionFromPage(page, volume, carryX);
1319 position.y = mRulerY->GetPositionFromPage(volume, libraries, carryY);
1321 ScrollTo(position, duration, bias, bias);
1324 void ScrollView::ScrollTo(Actor &actor)
1326 ScrollTo(actor, mSnapDuration);
1329 void ScrollView::ScrollTo(Actor &actor, float duration)
1331 DALI_ASSERT_ALWAYS(actor.GetParent() == Self());
1333 Actor self = Self();
1334 Vector3 size = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
1335 Vector3 position = actor.GetCurrentProperty< Vector3 >( Actor::Property::POSITION );
1336 Vector2 prePosition = GetPropertyPrePosition();
1337 position.GetVectorXY() -= prePosition;
1339 ScrollTo(Vector2(position.x - size.width * 0.5f, position.y - size.height * 0.5f), duration);
1342 Actor ScrollView::FindClosestActor()
1344 Actor self = Self();
1345 Vector3 size = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
1347 return FindClosestActorToPosition(Vector3(size.width * 0.5f,size.height * 0.5f,0.0f));
1350 Actor ScrollView::FindClosestActorToPosition(const Vector3& position, FindDirection dirX, FindDirection dirY, FindDirection dirZ)
1353 float closestDistance2 = 0.0f;
1354 Vector3 actualPosition = position;
1356 unsigned int numChildren = Self().GetChildCount();
1358 for(unsigned int i = 0; i < numChildren; ++i)
1360 Actor child = Self().GetChildAt(i);
1362 if(mInternalActor == child) // ignore internal actor.
1367 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1369 Vector3 delta = childPosition - actualPosition;
1371 // X-axis checking (only find Actors to the [dirX] of actualPosition)
1372 if(dirX > All) // != All,None
1374 FindDirection deltaH = delta.x > 0 ? Right : Left;
1381 // Y-axis checking (only find Actors to the [dirY] of actualPosition)
1382 if(dirY > All) // != All,None
1384 FindDirection deltaV = delta.y > 0 ? Down : Up;
1391 // Z-axis checking (only find Actors to the [dirZ] of actualPosition)
1392 if(dirZ > All) // != All,None
1394 FindDirection deltaV = delta.y > 0 ? In : Out;
1401 // compare child to closest child in terms of distance.
1402 float distance2 = 0.0f;
1404 // distance2 = the Square of the relevant dimensions of delta
1407 distance2 += delta.x * delta.x;
1412 distance2 += delta.y * delta.y;
1417 distance2 += delta.z * delta.z;
1420 if(closestChild) // Next time.
1422 if(distance2 < closestDistance2)
1424 closestChild = child;
1425 closestDistance2 = distance2;
1430 closestChild = child;
1431 closestDistance2 = distance2;
1435 return closestChild;
1438 bool ScrollView::ScrollToSnapPoint()
1440 DALI_LOG_SCROLL_STATE("[0x%X]", this );
1441 Vector2 stationaryVelocity = Vector2(0.0f, 0.0f);
1442 return SnapWithVelocity( stationaryVelocity );
1445 // TODO: In situations where axes are different (X snap, Y free)
1446 // Each axis should really have their own independent animation (time and equation)
1447 // Consider, X axis snapping to nearest grid point (EaseOut over fixed time)
1448 // Consider, Y axis simulating physics to arrive at a point (Physics equation over variable time)
1449 // Currently, the axes have been split however, they both use the same EaseOut equation.
1450 bool ScrollView::SnapWithVelocity(Vector2 velocity)
1452 // Animator takes over now, touches are assumed not to interfere.
1453 // And if touches do interfere, then we'll stop animation, update PrePosition
1454 // to current mScroll's properties, and then resume.
1455 // Note: For Flicking this may work a bit different...
1457 float angle = atan2(velocity.y, velocity.x);
1458 float speed2 = velocity.LengthSquared();
1459 AlphaFunction alphaFunction = mSnapAlphaFunction;
1460 Vector2 positionDuration = Vector2::ONE * mSnapDuration;
1463 FindDirection horizontal = None;
1464 FindDirection vertical = None;
1466 // orthoAngleRange = Angle tolerance within the Exact N,E,S,W direction
1467 // that will be accepted as a general N,E,S,W flick direction.
1469 const float orthoAngleRange = FLICK_ORTHO_ANGLE_RANGE * M_PI / 180.0f;
1470 const float flickSpeedThreshold2 = mFlickSpeedThreshold * mFlickSpeedThreshold;
1472 Vector2 positionSnap = mScrollPrePosition;
1474 // Flick logic X Axis
1476 if(mRulerX->IsEnabled() && mLockAxis != LockHorizontal)
1480 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1481 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1483 if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East
1485 biasX = 0.0f, horizontal = Left;
1487 // This guards against an error where no movement occurs, due to the flick finishing
1488 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1489 positionSnap.x += 1.0f;
1491 else if((angle >= M_PI-orthoAngleRange) || (angle < -M_PI+orthoAngleRange)) // Swiping West
1493 biasX = 1.0f, horizontal = Right;
1495 // This guards against an error where no movement occurs, due to the flick finishing
1496 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1497 positionSnap.x -= 1.0f;
1502 // Flick logic Y Axis
1504 if(mRulerY->IsEnabled() && mLockAxis != LockVertical)
1508 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1509 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1511 if((angle >= M_PI_2-orthoAngleRange) && (angle < M_PI_2+orthoAngleRange)) // Swiping South
1513 biasY = 0.0f, vertical = Up;
1515 else if((angle >= -M_PI_2-orthoAngleRange) && (angle < -M_PI_2+orthoAngleRange)) // Swiping North
1517 biasY = 1.0f, vertical = Down;
1522 // isFlick: Whether this gesture is a flick or not.
1523 bool isFlick = (horizontal != All || vertical != All);
1524 // isFreeFlick: Whether this gesture is a flick under free panning criteria.
1525 bool isFreeFlick = velocity.LengthSquared() > (FREE_FLICK_SPEED_THRESHOLD*FREE_FLICK_SPEED_THRESHOLD);
1527 if(isFlick || isFreeFlick)
1529 positionDuration = Vector2::ONE * mFlickDuration;
1530 alphaFunction = mFlickAlphaFunction;
1533 // Calculate next positionSnap ////////////////////////////////////////////////////////////
1535 if(mActorAutoSnapEnabled)
1537 Vector3 size = Self().GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
1539 Actor child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f), horizontal, vertical );
1541 if(!child && isFlick )
1543 // If we conducted a direction limited search and found no actor, then just snap to the closest actor.
1544 child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f) );
1549 Vector2 position = Self().GetCurrentProperty<Vector2>( Toolkit::ScrollView::Property::SCROLL_POSITION );
1551 // Get center-point of the Actor.
1552 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1554 if(mRulerX->IsEnabled())
1556 positionSnap.x = position.x - childPosition.x + size.width * 0.5f;
1558 if(mRulerY->IsEnabled())
1560 positionSnap.y = position.y - childPosition.y + size.height * 0.5f;
1565 Vector2 startPosition = positionSnap;
1566 positionSnap.x = -mRulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system.
1567 positionSnap.y = -mRulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
1569 Vector2 clampDelta(Vector2::ZERO);
1570 ClampPosition(positionSnap);
1572 if( (mRulerX->GetType() == Ruler::FREE || mRulerY->GetType() == Ruler::FREE)
1573 && isFreeFlick && !mActorAutoSnapEnabled)
1575 // Calculate target position based on velocity of flick.
1577 // a = Deceleration (Set to diagonal stage length * friction coefficient)
1578 // u = Initial Velocity (Flick velocity)
1579 // v = 0 (Final Velocity)
1580 // t = Time (Velocity / Deceleration)
1581 Vector2 stageSize = Stage::GetCurrent().GetSize();
1582 float stageLength = Vector3(stageSize.x, stageSize.y, 0.0f).Length();
1583 float a = (stageLength * mFrictionCoefficient);
1584 Vector3 u = Vector3(velocity.x, velocity.y, 0.0f) * mFlickSpeedCoefficient;
1585 float speed = u.Length();
1588 // TODO: Change this to a decay function. (faster you flick, the slower it should be)
1589 speed = std::min(speed, stageLength * mMaxFlickSpeed );
1591 alphaFunction = ConstantDecelerationAlphaFunction;
1593 float t = speed / a;
1595 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::FREE)
1597 positionSnap.x += t*u.x*0.5f;
1600 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::FREE)
1602 positionSnap.y += t*u.y*0.5f;
1605 clampDelta = positionSnap;
1606 ClampPosition(positionSnap);
1607 if((positionSnap - startPosition).LengthSquared() > Math::MACHINE_EPSILON_0)
1609 clampDelta -= positionSnap;
1610 clampDelta.x = clampDelta.x > 0.0f ? std::min(clampDelta.x, mMaxOvershoot.x) : std::max(clampDelta.x, -mMaxOvershoot.x);
1611 clampDelta.y = clampDelta.y > 0.0f ? std::min(clampDelta.y, mMaxOvershoot.y) : std::max(clampDelta.y, -mMaxOvershoot.y);
1615 clampDelta = Vector2::ZERO;
1618 // If Axis is Free and has velocity, then calculate time taken
1619 // to reach target based on velocity in axis.
1620 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::FREE)
1622 float deltaX = fabsf(startPosition.x - positionSnap.x);
1624 if(fabsf(u.x) > Math::MACHINE_EPSILON_1)
1626 positionDuration.x = fabsf(deltaX / u.x);
1630 positionDuration.x = 0;
1634 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::FREE)
1636 float deltaY = fabsf(startPosition.y - positionSnap.y);
1638 if(fabsf(u.y) > Math::MACHINE_EPSILON_1)
1640 positionDuration.y = fabsf(deltaY / u.y);
1644 positionDuration.y = 0;
1649 if(IsOvershootEnabled())
1651 // Scroll to the end of the overshoot only when overshoot is enabled.
1652 positionSnap += clampDelta;
1655 bool animating = AnimateTo(positionSnap, positionDuration,
1656 alphaFunction, false,
1657 DIRECTION_BIAS_NONE, DIRECTION_BIAS_NONE,
1658 isFlick || isFreeFlick ? FLICK : SNAP);
1663 void ScrollView::StopAnimation(void)
1665 // Clear Snap animation if exists.
1666 StopAnimation(mInternalXAnimation);
1667 StopAnimation(mInternalYAnimation);
1668 mScrollStateFlags = 0;
1669 // remove scroll animation flags
1670 HandleStoppedAnimation();
1673 void ScrollView::StopAnimation(Animation& animation)
1682 bool ScrollView::AnimateTo(const Vector2& position, const Vector2& positionDuration,
1683 AlphaFunction alpha, bool findShortcuts,
1684 DirectionBias horizontalBias, DirectionBias verticalBias,
1687 // Here we perform an animation on a number of properties (depending on which have changed)
1688 // The animation is applied to all ScrollBases
1689 Actor self = Self();
1690 mScrollTargetPosition = position;
1691 float totalDuration = 0.0f;
1693 bool positionChanged = (mScrollTargetPosition != mScrollPostPosition);
1697 totalDuration = std::max(totalDuration, positionDuration.x);
1698 totalDuration = std::max(totalDuration, positionDuration.y);
1702 // try to animate for a frame, on some occasions update will be changing scroll value while event side thinks it hasnt changed
1703 totalDuration = 0.01f;
1704 positionChanged = true;
1709 // Position Delta ///////////////////////////////////////////////////////
1712 UpdateMainInternalConstraint();
1713 if(mWrapMode && findShortcuts)
1715 // In Wrap Mode, the shortest distance is a little less intuitive...
1716 const RulerDomain rulerDomainX = mRulerX->GetDomain();
1717 const RulerDomain rulerDomainY = mRulerY->GetDomain();
1719 if(mRulerX->IsEnabled())
1721 float dir = VectorInDomain(-mScrollPrePosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
1722 mScrollTargetPosition.x = mScrollPrePosition.x + -dir;
1725 if(mRulerY->IsEnabled())
1727 float dir = VectorInDomain(-mScrollPrePosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
1728 mScrollTargetPosition.y = mScrollPrePosition.y + -dir;
1732 // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting
1733 // a horizonal/vertical wall.delay
1734 AnimateInternalXTo(mScrollTargetPosition.x, positionDuration.x, alpha);
1735 AnimateInternalYTo(mScrollTargetPosition.y, positionDuration.y, alpha);
1737 if( !(mScrollStateFlags & SCROLL_ANIMATION_FLAGS) )
1739 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollTargetPosition.x, mScrollTargetPosition.y );
1740 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollTargetPosition);
1741 mScrollPrePosition = mScrollTargetPosition;
1742 mScrollPostPosition = mScrollTargetPosition;
1743 WrapPosition(mScrollPostPosition);
1746 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 );
1747 DALI_LOG_SCROLL_STATE("[0x%X] SCROLL_PRE_POSITION[%.2f, %.2f], SCROLL_POSITION[%.2f, %.2f]", this, self.GetCurrentProperty( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ).Get<Vector2>().x, self.GetCurrentProperty( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ).Get<Vector2>().y, self.GetCurrentProperty( Toolkit::ScrollView::Property::SCROLL_POSITION ).Get<Vector2>().x, self.GetCurrentProperty( Toolkit::ScrollView::Property::SCROLL_POSITION ).Get<Vector2>().y );
1750 SetScrollUpdateNotification(true);
1752 // Always send a snap event when AnimateTo is called.
1753 Toolkit::ScrollView::SnapEvent snapEvent;
1754 snapEvent.type = snapType;
1755 snapEvent.position = -mScrollTargetPosition;
1756 snapEvent.duration = totalDuration;
1758 DALI_LOG_SCROLL_STATE("[0x%X] mSnapStartedSignal [%.2f, %.2f]", this, snapEvent.position.x, snapEvent.position.y);
1759 mSnapStartedSignal.Emit( snapEvent );
1761 return (mScrollStateFlags & SCROLL_ANIMATION_FLAGS) != 0;
1764 void ScrollView::EnableScrollOvershoot(bool enable)
1768 if (!mOvershootIndicator)
1770 mOvershootIndicator = ScrollOvershootIndicator::New();
1773 mOvershootIndicator->AttachToScrollable(*this);
1777 mMaxOvershoot = mUserMaxOvershoot;
1779 if (mOvershootIndicator)
1781 mOvershootIndicator->DetachFromScrollable(*this);
1785 UpdateMainInternalConstraint();
1788 void ScrollView::AddOverlay(Actor actor)
1790 actor.SetProperty( Actor::Property::DRAW_MODE, DrawMode::OVERLAY_2D );
1791 mInternalActor.Add( actor );
1794 void ScrollView::RemoveOverlay(Actor actor)
1796 mInternalActor.Remove( actor );
1799 void ScrollView::SetOvershootSize( const Vector2& size )
1801 mOvershootSize = size;
1802 if( IsOvershootEnabled() && mOvershootIndicator )
1804 mOvershootIndicator->AttachToScrollable(*this);
1808 void ScrollView::SetOvershootEffectColor( const Vector4& color )
1810 mOvershootEffectColor = color;
1811 if( mOvershootIndicator )
1813 mOvershootIndicator->SetOvershootEffectColor( color );
1817 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
1819 PanGestureDetector panGesture( GetPanGestureDetector() );
1821 // First remove just in case we have some set, then add.
1822 panGesture.RemoveDirection( direction );
1823 panGesture.AddDirection( direction, threshold );
1826 void ScrollView::RemoveScrollingDirection( Radian direction )
1828 PanGestureDetector panGesture( GetPanGestureDetector() );
1829 panGesture.RemoveDirection( direction );
1832 Toolkit::ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
1834 return mSnapStartedSignal;
1837 void ScrollView::AccessibleImpl::EnsureChildVisible(Actor child)
1839 auto scrollView = Dali::Toolkit::ScrollView::DownCast(self);
1840 scrollView.ScrollTo(child);
1843 void ScrollView::FindAndUnbindActor(Actor child)
1848 Vector2 ScrollView::GetPropertyPrePosition() const
1850 Vector2 position = Self().GetCurrentProperty< Vector2 >( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION );
1851 WrapPosition(position);
1855 Vector2 ScrollView::GetPropertyPosition() const
1857 Vector2 position = Self().GetCurrentProperty< Vector2 >( Toolkit::ScrollView::Property::SCROLL_POSITION );
1858 WrapPosition(position);
1863 void ScrollView::HandleStoppedAnimation()
1865 SetScrollUpdateNotification(false);
1868 void ScrollView::HandleSnapAnimationFinished()
1870 // Emit Signal that scrolling has completed.
1872 Actor self = Self();
1873 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
1875 Vector2 deltaPosition(mScrollPrePosition);
1877 UpdateLocalScrollProperties();
1878 WrapPosition(mScrollPrePosition);
1879 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
1880 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
1882 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1883 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 3 current[%.2f, %.2f], mScrollTargetPosition[%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y, -mScrollTargetPosition.x, -mScrollTargetPosition.y );
1884 mScrollCompletedSignal.Emit( currentScrollPosition );
1886 mDomainOffset += deltaPosition - mScrollPostPosition;
1887 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset);
1888 HandleStoppedAnimation();
1891 void ScrollView::SetScrollUpdateNotification( bool enabled )
1893 Actor self = Self();
1894 if( mScrollXUpdateNotification )
1896 // disconnect now to avoid a notification before removed from update thread
1897 mScrollXUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1898 self.RemovePropertyNotification(mScrollXUpdateNotification);
1899 mScrollXUpdateNotification.Reset();
1901 if( enabled && !mScrollUpdatedSignal.Empty())
1903 // Only set up the notification when the application has connected to the updated signal
1904 mScrollXUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 0, StepCondition(mScrollUpdateDistance, 0.0f));
1905 mScrollXUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1907 if( mScrollYUpdateNotification )
1909 // disconnect now to avoid a notification before removed from update thread
1910 mScrollYUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1911 self.RemovePropertyNotification(mScrollYUpdateNotification);
1912 mScrollYUpdateNotification.Reset();
1914 if( enabled && !mScrollUpdatedSignal.Empty())
1916 // Only set up the notification when the application has connected to the updated signal
1917 mScrollYUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 1, StepCondition(mScrollUpdateDistance, 0.0f));
1918 mScrollYUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1922 void ScrollView::OnScrollUpdateNotification(Dali::PropertyNotification& source)
1924 // Guard against destruction during signal emission
1925 Toolkit::ScrollView handle( GetOwner() );
1927 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1928 mScrollUpdatedSignal.Emit( currentScrollPosition );
1931 bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1933 Dali::BaseHandle handle( object );
1935 bool connected( true );
1936 Toolkit::ScrollView view = Toolkit::ScrollView::DownCast( handle );
1938 if( 0 == strcmp( signalName.c_str(), SIGNAL_SNAP_STARTED ) )
1940 view.SnapStartedSignal().Connect( tracker, functor );
1944 // signalName does not match any signal
1951 void ScrollView::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1953 // need to update domain properties for new size
1954 UpdatePropertyDomain();
1957 void ScrollView::OnSizeSet( const Vector3& size )
1959 // need to update domain properties for new size
1960 if( mDefaultMaxOvershoot )
1962 mUserMaxOvershoot.x = size.x * 0.5f;
1963 mUserMaxOvershoot.y = size.y * 0.5f;
1964 if( !IsOvershootEnabled() )
1966 mMaxOvershoot = mUserMaxOvershoot;
1969 UpdatePropertyDomain();
1970 UpdateMainInternalConstraint();
1971 if( IsOvershootEnabled() )
1973 mOvershootIndicator->Reset();
1976 ScrollBase::OnSizeSet( size );
1979 void ScrollView::OnChildAdd(Actor& child)
1981 ScrollBase::OnChildAdd( child );
1983 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
1986 mScrollBar = scrollBar;
1987 scrollBar.SetProperty( Dali::Actor::Property::NAME,"ScrollBar");
1989 mInternalActor.Add( scrollBar );
1990 if( scrollBar.GetScrollDirection() == Toolkit::ScrollBar::HORIZONTAL )
1992 scrollBar.SetScrollPropertySource( Self(),
1993 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_X,
1994 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_X,
1995 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_X,
1996 Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_X );
2000 scrollBar.SetScrollPropertySource( Self(),
2001 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_Y,
2002 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
2003 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_Y,
2004 Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_Y );
2007 if( mTransientScrollBar )
2009 // Show the scroll-indicator for a brief period
2010 Property::Map emptyMap;
2011 scrollBar.DoAction( "ShowTransientIndicator", emptyMap );
2014 else if(mAlterChild)
2020 void ScrollView::OnChildRemove(Actor& child)
2022 // TODO: Actor needs a RemoveConstraint method to take out an individual constraint.
2025 ScrollBase::OnChildRemove( child );
2028 void ScrollView::StartTouchDownTimer()
2030 if ( !mTouchDownTimer )
2032 mTouchDownTimer = Timer::New( TOUCH_DOWN_TIMER_INTERVAL );
2033 mTouchDownTimer.TickSignal().Connect( this, &ScrollView::OnTouchDownTimeout );
2036 mTouchDownTimer.Start();
2039 void ScrollView::StopTouchDownTimer()
2041 if ( mTouchDownTimer )
2043 mTouchDownTimer.Stop();
2047 bool ScrollView::OnTouchDownTimeout()
2049 DALI_LOG_SCROLL_STATE("[0x%X]", this);
2051 mTouchDownTimeoutReached = true;
2053 unsigned int currentScrollStateFlags( mScrollStateFlags ); // Cleared in StopAnimation so keep local copy for comparison
2054 if( currentScrollStateFlags & (SCROLL_ANIMATION_FLAGS | SNAP_ANIMATION_FLAGS) )
2056 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling Or snapping flags set, stopping animation", this);
2059 if( currentScrollStateFlags & SCROLL_ANIMATION_FLAGS )
2061 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling flags set, emitting signal", this);
2063 mScrollInterrupted = true;
2064 // reset domain offset as scrolling from original plane.
2065 mDomainOffset = Vector2::ZERO;
2066 Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, Vector2::ZERO);
2068 UpdateLocalScrollProperties();
2069 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2070 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 4 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2071 mScrollCompletedSignal.Emit( currentScrollPosition );
2078 bool ScrollView::OnTouch( Actor actor, const TouchEvent& touch )
2082 DALI_LOG_SCROLL_STATE("[0x%X], Not Sensitive, ignoring", this);
2084 // Ignore this touch event, if scrollview is insensitive.
2088 // Ignore events with multiple-touch points
2089 if (touch.GetPointCount() != 1)
2091 DALI_LOG_SCROLL_STATE("[0x%X], multiple touch, ignoring", this);
2096 const PointState::Type pointState = touch.GetState( 0 );
2097 if( pointState == PointState::DOWN )
2099 DALI_LOG_SCROLL_STATE("[0x%X] Down", this);
2101 if(mGestureStackDepth==0)
2103 mTouchDownTime = touch.GetTime();
2105 // This allows time for a pan-gesture to start, to avoid breaking snap-animation behavior with fast flicks.
2106 // If touch-down does not become a pan (after timeout interval), then snap-animation can be interrupted.
2107 mTouchDownTimeoutReached = false;
2108 mScrollInterrupted = false;
2109 StartTouchDownTimer();
2112 else if( ( pointState == PointState::UP ) ||
2113 ( ( pointState == PointState::INTERRUPTED ) && ( touch.GetHitActor( 0 )== Self() ) ) )
2115 DALI_LOG_SCROLL_STATE("[0x%X] %s", this, ( ( pointState == PointState::UP ) ? "Up" : "Interrupted" ) );
2117 StopTouchDownTimer();
2119 // if the user touches and releases without enough movement to go
2120 // into a gesture state, then we should snap to nearest point.
2121 // otherwise our scroll could be stopped (interrupted) half way through an animation.
2122 if(mGestureStackDepth==0 && mTouchDownTimeoutReached)
2124 if( ( pointState == PointState::INTERRUPTED ) ||
2125 ( ( touch.GetTime() - mTouchDownTime ) >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET ) )
2127 // Reset the velocity only if down was received a while ago
2128 mLastVelocity = Vector2( 0.0f, 0.0f );
2131 UpdateLocalScrollProperties();
2132 // Only finish the transform if scrolling was interrupted on down or if we are scrolling
2133 if ( mScrollInterrupted || mScrolling )
2135 DALI_LOG_SCROLL_STATE("[0x%X] Calling FinishTransform", this);
2140 mTouchDownTimeoutReached = false;
2141 mScrollInterrupted = false;
2147 bool ScrollView::OnWheelEvent( Actor actor, const WheelEvent& event)
2151 // Ignore this wheel event, if scrollview is insensitive.
2155 Vector2 targetScrollPosition = GetPropertyPosition();
2157 if(mRulerX->IsEnabled() && !mRulerY->IsEnabled())
2159 // If only the ruler in the X axis is enabled, scroll in the X axis.
2160 if(mRulerX->GetType() == Ruler::FREE)
2162 // Free panning mode
2163 targetScrollPosition.x += event.GetDelta() * mWheelScrollDistanceStep.x;
2164 ClampPosition(targetScrollPosition);
2165 ScrollTo(-targetScrollPosition);
2167 else if(!mScrolling)
2169 // Snap mode, only respond to the event when the previous snap animation is finished.
2170 ScrollTo(GetCurrentPage() - event.GetDelta());
2175 // If the ruler in the Y axis is enabled, scroll in the Y axis.
2176 if(mRulerY->GetType() == Ruler::FREE)
2178 // Free panning mode
2179 targetScrollPosition.y += event.GetDelta() * mWheelScrollDistanceStep.y;
2180 ClampPosition(targetScrollPosition);
2181 ScrollTo(-targetScrollPosition);
2183 else if(!mScrolling)
2185 // Snap mode, only respond to the event when the previous snap animation is finished.
2186 ScrollTo(GetCurrentPage() - event.GetDelta() * mRulerX->GetTotalPages());
2193 void ScrollView::ResetScrolling()
2195 Actor self = Self();
2196 self.GetCurrentProperty( Toolkit::ScrollView::Property::SCROLL_POSITION ).Get( mScrollPostPosition );
2197 mScrollPrePosition = mScrollPostPosition;
2198 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPostPosition.x, mScrollPostPosition.y );
2199 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPostPosition);
2202 void ScrollView::UpdateLocalScrollProperties()
2204 Actor self = Self();
2205 self.GetCurrentProperty( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ).Get( mScrollPrePosition );
2206 self.GetCurrentProperty( Toolkit::ScrollView::Property::SCROLL_POSITION ).Get( mScrollPostPosition );
2209 // private functions
2211 void ScrollView::PreAnimatedScrollSetup()
2213 // SCROLL_PRE_POSITION is our unclamped property with wrapping
2214 // SCROLL_POSITION is our final scroll position after clamping
2216 Actor self = Self();
2218 Vector2 deltaPosition(mScrollPostPosition);
2219 WrapPosition(mScrollPostPosition);
2220 mDomainOffset += deltaPosition - mScrollPostPosition;
2221 Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset);
2223 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2225 // already performing animation on internal x position
2226 StopAnimation(mInternalXAnimation);
2229 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2231 // already performing animation on internal y position
2232 StopAnimation(mInternalYAnimation);
2235 mScrollStateFlags = 0;
2237 // Update Actor position with this wrapped value.
2240 void ScrollView::FinaliseAnimatedScroll()
2242 // TODO - common animation finishing code in here
2245 void ScrollView::AnimateInternalXTo( float position, float duration, AlphaFunction alpha )
2247 StopAnimation(mInternalXAnimation);
2249 if( duration > Math::MACHINE_EPSILON_10 )
2251 Actor self = Self();
2252 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetCurrentProperty( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ).Get< Vector2 >().x, position );
2253 mInternalXAnimation = Animation::New(duration);
2254 DALI_LOG_SCROLL_STATE("[0x%X], mInternalXAnimation[0x%X]", this, mInternalXAnimation.GetObjectPtr() );
2255 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2256 mInternalXAnimation.AnimateTo( Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position, alpha, TimePeriod(duration));
2257 mInternalXAnimation.Play();
2259 // erase current state flags
2260 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2261 // add internal animation state flag
2262 mScrollStateFlags |= AnimatingInternalX;
2266 void ScrollView::AnimateInternalYTo( float position, float duration, AlphaFunction alpha )
2268 StopAnimation(mInternalYAnimation);
2270 if( duration > Math::MACHINE_EPSILON_10 )
2272 Actor self = Self();
2273 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetCurrentProperty( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ).Get< Vector2 >().y, position );
2274 mInternalYAnimation = Animation::New(duration);
2275 DALI_LOG_SCROLL_STATE("[0x%X], mInternalYAnimation[0x%X]", this, mInternalYAnimation.GetObjectPtr() );
2276 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2277 mInternalYAnimation.AnimateTo( Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position, alpha, TimePeriod(duration));
2278 mInternalYAnimation.Play();
2280 // erase current state flags
2281 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2282 // add internal animation state flag
2283 mScrollStateFlags |= AnimatingInternalY;
2287 void ScrollView::OnScrollAnimationFinished( Animation& source )
2289 // Guard against destruction during signal emission
2290 // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished()
2291 Toolkit::ScrollView handle( GetOwner() );
2293 bool scrollingFinished = false;
2295 // update our local scroll positions
2296 UpdateLocalScrollProperties();
2298 if( source == mInternalXAnimation )
2300 DALI_LOG_SCROLL_STATE("[0x%X] mInternalXAnimation[0x%X], expected[%.2f], actual[%.2f], post[%.2f]", this, mInternalXAnimation.GetObjectPtr(), mScrollTargetPosition.x, Self().GetCurrentProperty( SCROLL_PRE_POSITION ).Get< Vector2 >().x, mScrollPostPosition.x );
2302 if( !(mScrollStateFlags & AnimatingInternalY) )
2304 scrollingFinished = true;
2306 mInternalXAnimation.Reset();
2307 // wrap pre scroll x position and set it
2310 const RulerDomain rulerDomain = mRulerX->GetDomain();
2311 mScrollPrePosition.x = -WrapInDomain(-mScrollPrePosition.x, rulerDomain.min, rulerDomain.max);
2312 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
2313 handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
2315 SnapInternalXTo(mScrollPostPosition.x);
2318 if( source == mInternalYAnimation )
2320 DALI_LOG_SCROLL_STATE("[0x%X] mInternalYAnimation[0x%X], expected[%.2f], actual[%.2f], post[%.2f]", this, mInternalYAnimation.GetObjectPtr(), mScrollTargetPosition.y, DevelHandle::GetProperty( Self(), SCROLL_PRE_POSITION ).Get< Vector2 >().y, mScrollPostPosition.y );
2322 if( !(mScrollStateFlags & AnimatingInternalX) )
2324 scrollingFinished = true;
2326 mInternalYAnimation.Reset();
2329 // wrap pre scroll y position and set it
2330 const RulerDomain rulerDomain = mRulerY->GetDomain();
2331 mScrollPrePosition.y = -WrapInDomain(-mScrollPrePosition.y, rulerDomain.min, rulerDomain.max);
2332 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
2333 handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
2335 SnapInternalYTo(mScrollPostPosition.y);
2338 DALI_LOG_SCROLL_STATE("[0x%X] scrollingFinished[%d] Animation[0x%X]", this, scrollingFinished, source.GetObjectPtr());
2340 if(scrollingFinished)
2342 HandleSnapAnimationFinished();
2346 void ScrollView::OnSnapInternalPositionFinished( Animation& source )
2348 Actor self = Self();
2349 UpdateLocalScrollProperties();
2350 if( source == mInternalXAnimation )
2352 DALI_LOG_SCROLL_STATE("[0x%X] Finished X PostPosition Animation", this );
2354 // clear internal x animation flags
2355 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2356 mInternalXAnimation.Reset();
2357 WrapPosition(mScrollPrePosition);
2359 if( source == mInternalYAnimation )
2361 DALI_LOG_SCROLL_STATE("[0x%X] Finished Y PostPosition Animation", this );
2363 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2364 mInternalYAnimation.Reset();
2365 WrapPosition(mScrollPrePosition);
2369 void ScrollView::SnapInternalXTo(float position)
2371 Actor self = Self();
2373 StopAnimation(mInternalXAnimation);
2375 // erase current state flags
2376 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2378 // if internal x not equal to inputed parameter, animate it
2379 float duration = std::min(fabsf((position - mScrollPrePosition.x) / mMaxOvershoot.x) * mSnapOvershootDuration, mSnapOvershootDuration);
2380 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration );
2381 if( duration > Math::MACHINE_EPSILON_1 )
2383 DALI_LOG_SCROLL_STATE("[0x%X] Starting X Snap Animation to[%.2f]", this, position );
2385 mInternalXAnimation = Animation::New(duration);
2386 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2387 mInternalXAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position);
2388 mInternalXAnimation.Play();
2390 // add internal animation state flag
2391 mScrollStateFlags |= SnappingInternalX;
2395 void ScrollView::SnapInternalYTo(float position)
2397 Actor self = Self();
2399 StopAnimation(mInternalYAnimation);
2401 // erase current state flags
2402 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2404 // if internal y not equal to inputed parameter, animate it
2405 float duration = std::min(fabsf((position - mScrollPrePosition.y) / mMaxOvershoot.y) * mSnapOvershootDuration, mSnapOvershootDuration);
2406 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration );
2407 if( duration > Math::MACHINE_EPSILON_1 )
2409 DALI_LOG_SCROLL_STATE("[0x%X] Starting Y Snap Animation to[%.2f]", this, position );
2411 mInternalYAnimation = Animation::New(duration);
2412 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2413 mInternalYAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position);
2414 mInternalYAnimation.Play();
2416 // add internal animation state flag
2417 mScrollStateFlags |= SnappingInternalY;
2421 void ScrollView::GestureStarted()
2423 // we handle the first gesture.
2424 // if we're currently doing a gesture and receive another
2425 // we continue and combine the effects of the gesture instead of reseting.
2426 if(mGestureStackDepth++==0)
2428 Actor self = Self();
2429 StopTouchDownTimer();
2431 mPanDelta = Vector2::ZERO;
2432 mLastVelocity = Vector2::ZERO;
2435 mLockAxis = LockPossible;
2438 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2440 StopAnimation(mInternalXAnimation);
2442 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2444 StopAnimation(mInternalYAnimation);
2446 mScrollStateFlags = 0;
2448 if(mScrolling) // are we interrupting a current scroll?
2450 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
2452 // send negative scroll position since scroll internal scroll position works as an offset for actors,
2453 // give applications the position within the domain from the scroll view's anchor position
2454 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 5 [%.2f, %.2f]", this, -mScrollPostPosition.x, -mScrollPostPosition.y);
2455 mScrollCompletedSignal.Emit( -mScrollPostPosition );
2460 void ScrollView::GestureContinuing(const Vector2& panDelta)
2462 mPanDelta.x+= panDelta.x;
2463 mPanDelta.y+= panDelta.y;
2465 // Save the velocity, there is a bug in PanGesture
2466 // Whereby the GestureState::FINISHED's velocity is either:
2467 // NaN (due to time delta of zero between the last two events)
2468 // or 0 (due to position being the same between the last two events)
2470 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
2471 // appears mostly horizontal or mostly vertical respectively.
2474 mLockAxis = GetLockAxis(mPanDelta, mLockAxis, mAxisAutoLockGradient);
2475 } // end if mAxisAutoLock
2478 // TODO: Upgrade to use a more powerful gesture detector (one that supports multiple touches on pan - so works as pan and flick gesture)
2479 // BUG: GestureState::FINISHED doesn't always return velocity on release (due to
2480 // timeDelta between last two events being 0 sometimes, or posiiton being the same)
2481 void ScrollView::OnPan( const PanGesture& gesture )
2483 // Guard against destruction during signal emission
2484 // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx()
2485 Actor self( Self() );
2489 DALI_LOG_SCROLL_STATE("[0x%X] Pan Ignored, Insensitive", this);
2491 // If another callback on the same original signal disables sensitivity,
2492 // this callback will still be called, so we must suppress it.
2496 // translate Gesture input to get useful data...
2497 switch(gesture.GetState())
2499 case GestureState::STARTED:
2501 DALI_LOG_SCROLL_STATE("[0x%X] Pan Started", this);
2502 const Vector2& position = gesture.GetPosition();
2503 mPanStartPosition = position - gesture.GetDisplacement();
2504 UpdateLocalScrollProperties();
2507 self.SetProperty( Toolkit::ScrollView::Property::PANNING, true );
2508 self.SetProperty( Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(position.x, position.y, 0.0f) );
2510 UpdateMainInternalConstraint();
2511 Toolkit::ScrollBar scrollBar = mScrollBar.GetHandle();
2512 if( scrollBar && mTransientScrollBar )
2514 Vector3 size = Self().GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
2515 const Toolkit::RulerDomain& rulerDomainX = mRulerX->GetDomain();
2516 const Toolkit::RulerDomain& rulerDomainY = mRulerY->GetDomain();
2518 if( ( rulerDomainX.max > size.width ) || ( rulerDomainY.max > size.height ) )
2520 scrollBar.ShowIndicator();
2526 case GestureState::CONTINUING:
2530 DALI_LOG_SCROLL_STATE("[0x%X] Pan Continuing", this);
2531 GestureContinuing(gesture.GetScreenDisplacement());
2535 // If we do not think we are panning, then we should not do anything here
2541 case GestureState::FINISHED:
2542 case GestureState::CANCELLED:
2546 DALI_LOG_SCROLL_STATE("[0x%X] Pan %s", this, ( ( gesture.GetState() == GestureState::FINISHED ) ? "Finished" : "Cancelled" ) );
2548 UpdateLocalScrollProperties();
2549 mLastVelocity = gesture.GetVelocity();
2551 self.SetProperty( Toolkit::ScrollView::Property::PANNING, false );
2553 if( mScrollMainInternalPrePositionConstraint )
2555 mScrollMainInternalPrePositionConstraint.Remove();
2558 Toolkit::ScrollBar scrollBar = mScrollBar.GetHandle();
2559 if( scrollBar && mTransientScrollBar )
2561 scrollBar.HideIndicator();
2566 // If we do not think we are panning, then we should not do anything here
2572 case GestureState::POSSIBLE:
2573 case GestureState::CLEAR:
2575 // Nothing to do, not needed.
2579 } // end switch(gesture.state)
2581 OnGestureEx(gesture.GetState());
2584 void ScrollView::OnGestureEx(GestureState state)
2586 // call necessary signals for application developer
2588 if(state == GestureState::STARTED)
2590 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2591 Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, true);
2593 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 2 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2594 mScrollStartedSignal.Emit( currentScrollPosition );
2596 else if( (state == GestureState::FINISHED) ||
2597 (state == GestureState::CANCELLED) ) // Finished/default
2599 // when all the gestures have finished, we finish the transform.
2600 // so if a user decides to pan (1 gesture), and then pan+zoom (2 gestures)
2601 // then stop panning (back to 1 gesture), and then stop zooming (0 gestures).
2602 // this is the point we end, and perform necessary snapping.
2603 mGestureStackDepth--;
2604 if(mGestureStackDepth==0)
2606 // no flick if we have not exceeded min flick distance
2607 if( (fabsf(mPanDelta.x) < mMinFlickDistance.x)
2608 && (fabsf(mPanDelta.y) < mMinFlickDistance.y) )
2610 // reset flick velocity
2611 mLastVelocity = Vector2::ZERO;
2617 DALI_LOG_SCROLL_STATE("[0x%X] mGestureStackDepth[%d]", this, mGestureStackDepth);
2622 void ScrollView::FinishTransform()
2624 // at this stage internal x and x scroll position should have followed prescroll position exactly
2625 Actor self = Self();
2627 PreAnimatedScrollSetup();
2629 // convert pixels/millisecond to pixels per second
2630 bool animating = SnapWithVelocity(mLastVelocity * 1000.0f);
2634 // if not animating, then this pan has completed right now.
2635 SetScrollUpdateNotification(false);
2637 Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
2639 if( fabs(mScrollPrePosition.x - mScrollTargetPosition.x) > Math::MACHINE_EPSILON_10 )
2641 SnapInternalXTo(mScrollTargetPosition.x);
2643 if( fabs(mScrollPrePosition.y - mScrollTargetPosition.y) > Math::MACHINE_EPSILON_10 )
2645 SnapInternalYTo(mScrollTargetPosition.y);
2647 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2648 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 6 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2649 mScrollCompletedSignal.Emit( currentScrollPosition );
2653 Vector2 ScrollView::GetOvershoot(Vector2& position) const
2655 Vector3 size = Self().GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
2658 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2659 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2661 if(mRulerX->IsEnabled() && rulerDomainX.enabled)
2663 const float left = rulerDomainX.min - position.x;
2664 const float right = size.width - rulerDomainX.max - position.x;
2671 overshoot.x = right;
2675 if(mRulerY->IsEnabled() && rulerDomainY.enabled)
2677 const float top = rulerDomainY.min - position.y;
2678 const float bottom = size.height - rulerDomainY.max - position.y;
2685 overshoot.y = bottom;
2692 bool ScrollView::OnAccessibilityPan(PanGesture gesture)
2694 // Keep track of whether this is an AccessibilityPan
2695 mInAccessibilityPan = true;
2697 mInAccessibilityPan = false;
2702 void ScrollView::ClampPosition(Vector2& position) const
2704 ClampState2D clamped;
2705 ClampPosition(position, clamped);
2708 void ScrollView::ClampPosition(Vector2& position, ClampState2D &clamped) const
2710 Vector3 size = Self().GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
2712 position.x = -mRulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system.
2713 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.
2716 void ScrollView::WrapPosition(Vector2& position) const
2720 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2721 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2723 if(mRulerX->IsEnabled())
2725 position.x = -WrapInDomain(-position.x, rulerDomainX.min, rulerDomainX.max);
2728 if(mRulerY->IsEnabled())
2730 position.y = -WrapInDomain(-position.y, rulerDomainY.min, rulerDomainY.max);
2735 void ScrollView::UpdateMainInternalConstraint()
2737 // TODO: Only update the constraints which have changed, rather than remove all and add all again.
2738 // Requires a dali-core ApplyConstraintAt, or a ReplaceConstraint. The former is probably more flexible.
2739 Actor self = Self();
2740 PanGestureDetector detector( GetPanGestureDetector() );
2742 if(mScrollMainInternalPositionConstraint)
2744 mScrollMainInternalPositionConstraint.Remove();
2745 mScrollMainInternalDeltaConstraint.Remove();
2746 mScrollMainInternalFinalConstraint.Remove();
2747 mScrollMainInternalRelativeConstraint.Remove();
2748 mScrollMainInternalDomainConstraint.Remove();
2749 mScrollMainInternalPrePositionMaxConstraint.Remove();
2751 if( mScrollMainInternalPrePositionConstraint )
2753 mScrollMainInternalPrePositionConstraint.Remove();
2756 // TODO: It's probably better to use a local displacement value as this will give a displacement when scrolling just commences
2757 // but we need to make sure than the gesture system gives displacement since last frame (60Hz), not displacement since last touch event (90Hz).
2759 // 1. First calculate the pre-position (this is the scroll position if no clamping has taken place)
2760 Vector2 initialPanMask = Vector2(mRulerX->IsEnabled() ? 1.0f : 0.0f, mRulerY->IsEnabled() ? 1.0f : 0.0f);
2762 if( mLockAxis == LockVertical )
2764 initialPanMask.y = 0.0f;
2766 else if( mLockAxis == LockHorizontal )
2768 initialPanMask.x = 0.0f;
2773 mScrollMainInternalPrePositionConstraint = Constraint::New<Vector2>( self,
2774 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION,
2775 InternalPrePositionConstraint( mPanStartPosition,
2778 mAxisAutoLockGradient,
2783 mScrollMainInternalPrePositionConstraint.AddSource( Source( detector, PanGestureDetector::Property::LOCAL_POSITION ) );
2784 mScrollMainInternalPrePositionConstraint.AddSource( Source( detector, PanGestureDetector::Property::PANNING ) );
2785 mScrollMainInternalPrePositionConstraint.AddSource( Source( self, Actor::Property::SIZE ) );
2786 mScrollMainInternalPrePositionConstraint.Apply();
2789 // 2. Second calculate the clamped position (actual position)
2790 mScrollMainInternalPositionConstraint = Constraint::New<Vector2>( self,
2791 Toolkit::ScrollView::Property::SCROLL_POSITION,
2792 InternalPositionConstraint( mRulerX->GetDomain(),
2793 mRulerY->GetDomain(),
2795 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2796 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2797 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2798 mScrollMainInternalPositionConstraint.AddSource( Source( self, Actor::Property::SIZE ) );
2799 mScrollMainInternalPositionConstraint.Apply();
2801 mScrollMainInternalDeltaConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_POSITION_DELTA, InternalPositionDeltaConstraint );
2802 mScrollMainInternalDeltaConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2803 mScrollMainInternalDeltaConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET ) );
2804 mScrollMainInternalDeltaConstraint.Apply();
2806 mScrollMainInternalFinalConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_FINAL,
2807 InternalFinalConstraint( FinalDefaultAlphaFunction,
2808 FinalDefaultAlphaFunction ) );
2809 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2810 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::OVERSHOOT_X ) );
2811 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::OVERSHOOT_Y ) );
2812 mScrollMainInternalFinalConstraint.Apply();
2814 mScrollMainInternalRelativeConstraint = Constraint::New<Vector2>( self, Toolkit::Scrollable::Property::SCROLL_RELATIVE_POSITION, InternalRelativePositionConstraint );
2815 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2816 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2817 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2818 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2819 mScrollMainInternalRelativeConstraint.Apply();
2821 mScrollMainInternalDomainConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE, InternalScrollDomainConstraint );
2822 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2823 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2824 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2825 mScrollMainInternalDomainConstraint.Apply();
2827 mScrollMainInternalPrePositionMaxConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX, InternalPrePositionMaxConstraint );
2828 mScrollMainInternalPrePositionMaxConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2829 mScrollMainInternalPrePositionMaxConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2830 mScrollMainInternalPrePositionMaxConstraint.Apply();
2832 // When panning we want to make sure overshoot values are affected by pre position and post position
2833 SetOvershootConstraintsEnabled(!mWrapMode);
2836 void ScrollView::SetOvershootConstraintsEnabled(bool enabled)
2838 Actor self( Self() );
2839 // remove and reset, it may now be in wrong order with the main internal constraints
2840 if( mScrollMainInternalOvershootXConstraint )
2842 mScrollMainInternalOvershootXConstraint.Remove();
2843 mScrollMainInternalOvershootXConstraint.Reset();
2844 mScrollMainInternalOvershootYConstraint.Remove();
2845 mScrollMainInternalOvershootYConstraint.Reset();
2849 mScrollMainInternalOvershootXConstraint= Constraint::New<float>( self, Toolkit::ScrollView::Property::OVERSHOOT_X, OvershootXConstraint(mMaxOvershoot.x) );
2850 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2851 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2852 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL ) );
2853 mScrollMainInternalOvershootXConstraint.Apply();
2855 mScrollMainInternalOvershootYConstraint = Constraint::New<float>( self, Toolkit::ScrollView::Property::OVERSHOOT_Y, OvershootYConstraint(mMaxOvershoot.y) );
2856 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2857 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2858 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
2859 mScrollMainInternalOvershootYConstraint.Apply();
2863 self.SetProperty(Toolkit::ScrollView::Property::OVERSHOOT_X, 0.0f);
2864 self.SetProperty(Toolkit::ScrollView::Property::OVERSHOOT_Y, 0.0f);
2868 void ScrollView::SetInternalConstraints()
2870 // Internal constraints (applied to target ScrollBase Actor itself) /////////
2871 UpdateMainInternalConstraint();
2873 // User definable constraints to apply to all child actors //////////////////
2874 Actor self = Self();
2876 // Apply some default constraints to ScrollView & its bound actors
2877 // Movement + Wrap function
2879 Constraint constraint;
2881 // MoveActor (scrolling)
2882 constraint = Constraint::New<Vector3>( self, Actor::Property::POSITION, MoveActorConstraint );
2883 constraint.AddSource( Source( self, Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2884 constraint.SetRemoveAction(Constraint::DISCARD);
2885 ApplyConstraintToBoundActors(constraint);
2887 // WrapActor (wrap functionality)
2888 constraint = Constraint::New<Vector3>( self, Actor::Property::POSITION, WrapActorConstraint );
2889 constraint.AddSource( LocalSource( Actor::Property::SCALE ) );
2890 constraint.AddSource( LocalSource( Actor::Property::ANCHOR_POINT ) );
2891 constraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2892 constraint.AddSource( Source( self, Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2893 constraint.AddSource( Source( self, Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2894 constraint.AddSource( Source( self, Toolkit::ScrollView::Property::WRAP ) );
2895 constraint.SetRemoveAction(Constraint::DISCARD);
2896 ApplyConstraintToBoundActors(constraint);
2899 void ScrollView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
2901 Toolkit::ScrollView scrollView = Toolkit::ScrollView::DownCast( Dali::BaseHandle( object ) );
2905 ScrollView& scrollViewImpl( GetImpl( scrollView ) );
2908 case Toolkit::ScrollView::Property::WRAP_ENABLED:
2910 scrollViewImpl.SetWrapMode( value.Get<bool>() );
2913 case Toolkit::ScrollView::Property::PANNING_ENABLED:
2915 scrollViewImpl.SetScrollSensitive( value.Get<bool>() );
2918 case Toolkit::ScrollView::Property::AXIS_AUTO_LOCK_ENABLED:
2920 scrollViewImpl.SetAxisAutoLock( value.Get<bool>() );
2923 case Toolkit::ScrollView::Property::WHEEL_SCROLL_DISTANCE_STEP:
2925 scrollViewImpl.SetWheelScrollDistanceStep( value.Get<Vector2>() );
2928 case Toolkit::ScrollView::Property::SCROLL_MODE:
2930 const Property::Map* map = value.GetMap();
2933 scrollViewImpl.SetScrollMode( *map );
2940 Property::Value ScrollView::GetProperty( BaseObject* object, Property::Index index )
2942 Property::Value value;
2944 Toolkit::ScrollView scrollView = Toolkit::ScrollView::DownCast( Dali::BaseHandle( object ) );
2948 ScrollView& scrollViewImpl( GetImpl( scrollView ) );
2951 case Toolkit::ScrollView::Property::WRAP_ENABLED:
2953 value = scrollViewImpl.GetWrapMode();
2956 case Toolkit::ScrollView::Property::PANNING_ENABLED:
2958 value = scrollViewImpl.GetScrollSensitive();
2961 case Toolkit::ScrollView::Property::AXIS_AUTO_LOCK_ENABLED:
2963 value = scrollViewImpl.GetAxisAutoLock();
2966 case Toolkit::ScrollView::Property::WHEEL_SCROLL_DISTANCE_STEP:
2968 value = scrollViewImpl.GetWheelScrollDistanceStep();
2977 void ScrollView::SetScrollMode( const Property::Map& scrollModeMap )
2979 Toolkit::RulerPtr rulerX, rulerY;
2981 // Check the scroll mode in the X axis
2982 bool xAxisScrollEnabled = true;
2983 Property::Value* valuePtr = scrollModeMap.Find( Toolkit::ScrollMode::X_AXIS_SCROLL_ENABLED, "xAxisScrollEnabled" );
2984 if( valuePtr && valuePtr->GetType() == Property::BOOLEAN )
2986 valuePtr->Get( xAxisScrollEnabled );
2989 if( !xAxisScrollEnabled )
2991 // Default ruler and disabled
2992 rulerX = new Toolkit::DefaultRuler();
2997 valuePtr = scrollModeMap.Find( Toolkit::ScrollMode::X_AXIS_SNAP_TO_INTERVAL, "xAxisSnapToInterval" );
2998 float xAxisSnapToInterval = 0.0f;
2999 if( valuePtr && valuePtr->Get( xAxisSnapToInterval ) )
3001 // Fixed ruler and enabled
3002 rulerX = new Toolkit::FixedRuler( xAxisSnapToInterval );
3006 // Default ruler and enabled
3007 rulerX = new Toolkit::DefaultRuler();
3010 valuePtr = scrollModeMap.Find( Toolkit::ScrollMode::X_AXIS_SCROLL_BOUNDARY, "xAxisScrollBoundary" );
3011 float xAxisScrollBoundary = 0.0f;
3012 if( valuePtr && valuePtr->Get( xAxisScrollBoundary ) )
3014 // By default ruler domain is disabled unless set
3015 rulerX->SetDomain( Toolkit::RulerDomain( 0, xAxisScrollBoundary, true ) );
3019 // Check the scroll mode in the Y axis
3020 bool yAxisScrollEnabled = true;
3021 valuePtr = scrollModeMap.Find( Toolkit::ScrollMode::Y_AXIS_SCROLL_ENABLED, "yAxisScrollEnabled" );
3022 if( valuePtr && valuePtr->GetType() == Property::BOOLEAN )
3024 valuePtr->Get( yAxisScrollEnabled );
3027 if( !yAxisScrollEnabled )
3029 // Default ruler and disabled
3030 rulerY = new Toolkit::DefaultRuler();
3035 valuePtr = scrollModeMap.Find( Toolkit::ScrollMode::Y_AXIS_SNAP_TO_INTERVAL, "yAxisSnapToInterval" );
3036 float yAxisSnapToInterval = 0.0f;
3037 if( valuePtr && valuePtr->Get( yAxisSnapToInterval ) )
3039 // Fixed ruler and enabled
3040 rulerY = new Toolkit::FixedRuler(yAxisSnapToInterval);
3044 // Default ruler and enabled
3045 rulerY = new Toolkit::DefaultRuler();
3048 valuePtr = scrollModeMap.Find( Toolkit::ScrollMode::Y_AXIS_SCROLL_BOUNDARY, "yAxisScrollBoundary" );
3049 float yAxisScrollBoundary = 0.0f;
3050 if( valuePtr && valuePtr->Get( yAxisScrollBoundary ) )
3052 // By default ruler domain is disabled unless set
3053 rulerY->SetDomain( Toolkit::RulerDomain( 0, yAxisScrollBoundary, true ) );
3061 } // namespace Internal
3063 } // namespace Toolkit