2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.h>
22 #include <cstring> // for strcmp
23 #include <dali/public-api/animation/constraints.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/events/wheel-event.h>
26 #include <dali/public-api/events/touch-data.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/public-api/object/type-registry-helper.h>
29 #include <dali/integration-api/debug.h>
32 #include <dali-toolkit/public-api/controls/scroll-bar/scroll-bar.h>
33 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view.h>
34 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view-constraints.h>
35 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
36 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-effect-impl.h>
38 //#define ENABLED_SCROLL_STATE_LOGGING
40 #ifdef ENABLED_SCROLL_STATE_LOGGING
41 #define DALI_LOG_SCROLL_STATE(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d " format "\n", __PRETTY_FUNCTION__, __LINE__, ## args)
43 #define DALI_LOG_SCROLL_STATE(format, args...)
46 // TODO: Change to two class system:
47 // 1. DraggableActor (is an actor which can be dragged anywhere, can be set to range using the ruler)
48 // 2. ScrollView (contains a draggable actor that can a) be dragged in the negative X, and Y domain, b) has a hitArea for touches)
49 // TODO: external components (page and status overlays).
51 // TODO: upgrade Vector2/3 to support returning Unit vectors, normals, & cross product (dot product is already provided)
57 const float DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f); ///< Default Drag-Release animation time.
58 const float DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f); ///< Default Drag-Flick animation time.
59 const float DEFAULT_SNAP_OVERSHOOT_DURATION(0.5f); ///< Default Overshoot snapping animation time.
60 const float DEFAULT_MAX_OVERSHOOT(100.0f); ///< Default maximum allowed overshoot in pixels
62 const float DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f); ///< Default Axis-AutoLock gradient threshold. default is 0.36:1 (20 degrees)
63 const float DEFAULT_FRICTION_COEFFICIENT(1.0f); ///< Default Friction Co-efficient. (in stage diagonals per second)
64 const float DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f); ///< Default Flick speed coefficient (multiples input touch velocity)
65 const float DEFAULT_MAX_FLICK_SPEED(3.0f); ///< Default Maximum flick speed. (in stage diagonals per second)
67 const Vector2 DEFAULT_MIN_FLICK_DISTANCE(30.0f, 30.0f); ///< minimum distance for pan before flick allowed
68 const float DEFAULT_MIN_FLICK_SPEED_THRESHOLD(500.0f); ///< Minimum pan speed required for flick in pixels/s
69 const float FREE_FLICK_SPEED_THRESHOLD = 200.0f; ///< Free-Flick threshold in pixels/ms
70 const float AUTOLOCK_AXIS_MINIMUM_DISTANCE2 = 100.0f; ///< Auto-lock axis after minimum distance squared.
71 const float FLICK_ORTHO_ANGLE_RANGE = 75.0f; ///< degrees. (if >45, then supports diagonal flicking)
72 const Vector2 DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = Vector2(0.17f, 0.1f); ///< The step of horizontal scroll distance in the proportion of stage size for each wheel event received.
73 const unsigned long MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET( 150u );
74 const float TOUCH_DOWN_TIMER_INTERVAL = 100.0f;
75 const float DEFAULT_SCROLL_UPDATE_DISTANCE( 30.0f ); ///< Default distance to travel in pixels for scroll update signal
77 const std::string INTERNAL_MAX_POSITION_PROPERTY_NAME( "internalMaxPosition" );
79 // Helpers ////////////////////////////////////////////////////////////////////////////////////////
82 * Find the vector (distance) from (a) to (b)
83 * in domain (start) to (end)
84 * (\ / start) (\ / end)
87 * @note assumes both (a) and (b) are already with the domain
90 * @param[in] a the current point
91 * @param[in] b the target point
92 * @param[in] start the start of the domain
93 * @param[in] end the end of the domain
94 * @param[in] bias whether to only take the right direction or the left direction,
95 * or the shortest direction.
96 * @return the shortest direction and distance
98 float VectorInDomain(float a, float b, float start, float end, Dali::Toolkit::DirectionBias bias)
100 if(bias == Dali::Toolkit::DirectionBiasNone)
102 return ShortestDistanceInDomain( a, b, start, end );
105 float size = end-start;
111 if(bias == Dali::Toolkit::DirectionBiasRight) // going right, take the vector.
117 float aRight = a+size;
124 if(bias == Dali::Toolkit::DirectionBiasLeft) // going left, take the vector.
130 float aLeft = a-size;
137 * Returns the position of the anchor within actor
139 * @param actor The Actor
140 * @param anchor The Anchor point of interest.
141 * @return The position of the Anchor
143 Vector3 GetPositionOfAnchor(Actor &actor, const Vector3 &anchor)
145 Vector3 childPosition = actor.GetCurrentPosition();
146 Vector3 childAnchor = - actor.GetCurrentAnchorPoint() + anchor;
147 Vector3 childSize = actor.GetCurrentSize();
149 return childPosition + childAnchor * childSize;
152 // AlphaFunctions /////////////////////////////////////////////////////////////////////////////////
154 float FinalDefaultAlphaFunction(float offset)
156 return offset * 0.5f;
160 * ConstantDecelerationAlphaFunction
161 * Newtoninan distance for constant deceleration
162 * v = 1 - t, s = t - 1/2 t^2
163 * when t = 0, s = 0.0 (min distance)
164 * when t = 1, s = 0.5 (max distance)
165 * progress = s / (max-min) = 2t - t^2
167 * @param[in] offset The input progress
168 * @return The output progress
170 float ConstantDecelerationAlphaFunction(float progress)
172 return progress * 2.0f - progress * progress;
175 // Internal Constraints ///////////////////////////////////////////////////////////////////////////
178 * Internal Relative position Constraint
179 * Generates the relative position value of the scroll view
180 * based on the absolute position, and it's relation to the
181 * scroll domain. This is a value from 0.0f to 1.0f in each
182 * scroll position axis.
184 void InternalRelativePositionConstraint( Vector2& relativePosition, const PropertyInputContainer& inputs)
186 Vector2 position = -inputs[0]->GetVector2();
187 const Vector2& min = inputs[1]->GetVector2();
188 const Vector2& max = inputs[2]->GetVector2();
189 const Vector3& size = inputs[3]->GetVector3();
191 position.x = WrapInDomain(position.x, min.x, max.x);
192 position.y = WrapInDomain(position.y, min.y, max.y);
194 Vector2 domainSize = (max - min) - size.GetVectorXY();
196 relativePosition.x = domainSize.x > Math::MACHINE_EPSILON_1 ? fabsf((position.x - min.x) / domainSize.x) : 0.0f;
197 relativePosition.y = domainSize.y > Math::MACHINE_EPSILON_1 ? fabsf((position.y - min.y) / domainSize.y) : 0.0f;
201 * Internal scroll domain Constraint
202 * Generates the scroll domain of the scroll view.
204 void InternalScrollDomainConstraint( Vector2& scrollDomain, const PropertyInputContainer& inputs)
206 const Vector2& min = inputs[0]->GetVector2();
207 const Vector2& max = inputs[1]->GetVector2();
208 const Vector3& size = inputs[2]->GetVector3();
210 scrollDomain = (max - min) - size.GetVectorXY();
214 * Internal maximum scroll position Constraint
215 * Generates the maximum scroll position of the scroll view.
217 void InternalPrePositionMaxConstraint( Vector2& scrollMax, const PropertyInputContainer& inputs)
219 const Vector2& max = inputs[0]->GetVector2();
220 const Vector3& size = inputs[1]->GetVector3();
222 scrollMax = max - size.GetVectorXY();
225 } // unnamed namespace
241 return Toolkit::ScrollView::New();
244 // Setup properties, signals and actions using the type-registry.
245 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ScrollView, Toolkit::Scrollable, Create )
247 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollView, "wrapEnabled", BOOLEAN, WRAP_ENABLED )
248 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollView, "panningEnabled", BOOLEAN, PANNING_ENABLED )
249 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollView, "axisAutoLockEnabled", BOOLEAN, AXIS_AUTO_LOCK_ENABLED )
250 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollView, "wheelScrollDistanceStep", VECTOR2, WHEEL_SCROLL_DISTANCE_STEP )
252 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPosition", VECTOR2, SCROLL_POSITION)
253 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPrePosition", VECTOR2, SCROLL_PRE_POSITION)
254 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionX", SCROLL_PRE_POSITION_X, SCROLL_PRE_POSITION, 0)
255 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionY", SCROLL_PRE_POSITION_Y, SCROLL_PRE_POSITION, 1)
256 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionMax", VECTOR2, SCROLL_PRE_POSITION_MAX)
257 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionMaxX", SCROLL_PRE_POSITION_MAX_X, SCROLL_PRE_POSITION_MAX, 0)
258 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollPrePositionMaxY", SCROLL_PRE_POSITION_MAX_Y, SCROLL_PRE_POSITION_MAX, 1)
259 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "overshootX", FLOAT, OVERSHOOT_X)
260 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "overshootY", FLOAT, OVERSHOOT_Y)
261 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollFinal", VECTOR2, SCROLL_FINAL)
262 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollFinalX", SCROLL_FINAL_X, SCROLL_FINAL,0)
263 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollFinalY", SCROLL_FINAL_Y, SCROLL_FINAL,1)
264 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "wrap", BOOLEAN, WRAP)
265 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "panning", BOOLEAN, PANNING)
266 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrolling", BOOLEAN, SCROLLING)
267 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollDomainSize", VECTOR2, SCROLL_DOMAIN_SIZE)
268 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollDomainSizeX", SCROLL_DOMAIN_SIZE_X, SCROLL_DOMAIN_SIZE, 0)
269 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scrollDomainSizeY", SCROLL_DOMAIN_SIZE_Y, SCROLL_DOMAIN_SIZE, 1)
270 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollDomainOffset", VECTOR2, SCROLL_DOMAIN_OFFSET)
271 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrollPositionDelta", VECTOR2, SCROLL_POSITION_DELTA)
272 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "startPagePosition", VECTOR3, START_PAGE_POSITION)
274 DALI_SIGNAL_REGISTRATION( Toolkit, ScrollView, "valueChanged", SIGNAL_SNAP_STARTED )
276 DALI_TYPE_REGISTRATION_END()
279 * Returns whether to lock scrolling to a particular axis
281 * @param[in] panDelta Distance panned since gesture started
282 * @param[in] currentLockAxis The current lock axis value
283 * @param[in] lockGradient How quickly to lock to a particular axis
285 * @return The new axis lock state
287 ScrollView::LockAxis GetLockAxis(const Vector2& panDelta, ScrollView::LockAxis currentLockAxis, float lockGradient)
289 if(panDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 &&
290 currentLockAxis == ScrollView::LockPossible)
292 float dx = fabsf(panDelta.x);
293 float dy = fabsf(panDelta.y);
294 if(dx * lockGradient >= dy)
296 // 0.36:1 gradient to the horizontal (deviate < 20 degrees)
297 currentLockAxis = ScrollView::LockVertical;
299 else if(dy * lockGradient > dx)
301 // 0.36:1 gradient to the vertical (deviate < 20 degrees)
302 currentLockAxis = ScrollView::LockHorizontal;
306 currentLockAxis = ScrollView::LockNone;
309 return currentLockAxis;
313 * Internal Pre-Position Property Constraint.
315 * Generates position property based on current position + gesture displacement.
316 * Or generates position property based on positionX/Y.
317 * Note: This is the position prior to any clamping at scroll boundaries.
319 struct InternalPrePositionConstraint
321 InternalPrePositionConstraint( const Vector2& initialPanPosition,
322 const Vector2& initialPanMask,
324 float axisAutoLockGradient,
325 ScrollView::LockAxis initialLockAxis,
326 const Vector2& maxOvershoot,
327 const RulerPtr& rulerX, const RulerPtr& rulerY )
328 : mLocalStart( initialPanPosition ),
329 mInitialPanMask( initialPanMask ),
330 mMaxOvershoot( maxOvershoot ),
331 mAxisAutoLockGradient( axisAutoLockGradient ),
332 mLockAxis( initialLockAxis ),
333 mAxisAutoLock( axisAutoLock ),
336 const RulerDomain& rulerDomainX = rulerX->GetDomain();
337 const RulerDomain& rulerDomainY = rulerY->GetDomain();
338 mDomainMin = Vector2( rulerDomainX.min, -rulerDomainY.min );
339 mDomainMax = Vector2( -rulerDomainX.max, -rulerDomainY.max );
340 mClampX = rulerDomainX.enabled;
341 mClampY = rulerDomainY.enabled;
342 mFixedRulerX = rulerX->GetType() == Ruler::Fixed;
343 mFixedRulerY = rulerY->GetType() == Ruler::Fixed;
346 void operator()( Vector2& scrollPostPosition, const PropertyInputContainer& inputs )
348 const Vector2& panPosition = inputs[0]->GetVector2();
349 const bool& inGesture = inputs[1]->GetBoolean();
351 // First check if we are within a gesture.
352 // The ScrollView may have received a start gesture from ::OnPan()
353 // while the finish gesture is received now in this constraint.
354 // This gesture must then be rejected as the value will be "old".
355 // Typically the last value from the end of the last gesture.
356 // If we are rejecting the gesture, we simply don't modify the constraint target.
361 mPrePosition = scrollPostPosition;
362 mStartPosition = mPrePosition;
363 mCurrentPanMask = mInitialPanMask;
367 // Calculate Deltas...
368 const Vector2& currentPosition = panPosition;
369 Vector2 panDelta( currentPosition - mLocalStart );
371 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
372 // appears mostly horizontal or mostly vertical respectively...
375 mLockAxis = GetLockAxis( panDelta, mLockAxis, mAxisAutoLockGradient );
376 if( mLockAxis == ScrollView::LockVertical )
378 mCurrentPanMask.y = 0.0f;
380 else if( mLockAxis == ScrollView::LockHorizontal )
382 mCurrentPanMask.x = 0.0f;
386 // Restrict deltas based on ruler enable/disable and axis-lock state...
387 panDelta *= mCurrentPanMask;
389 // Perform Position transform based on input deltas...
390 scrollPostPosition = mPrePosition;
391 scrollPostPosition += panDelta;
393 // if no wrapping then clamp preposition to maximum overshoot amount
394 const Vector3& size = inputs[2]->GetVector3();
397 float newXPosition = Clamp( scrollPostPosition.x, ( mDomainMax.x + size.x ) - mMaxOvershoot.x, mDomainMin.x + mMaxOvershoot.x );
398 if( (newXPosition < scrollPostPosition.x - Math::MACHINE_EPSILON_1)
399 || (newXPosition > scrollPostPosition.x + Math::MACHINE_EPSILON_1) )
401 mPrePosition.x = newXPosition;
402 mLocalStart.x = panPosition.x;
404 scrollPostPosition.x = newXPosition;
408 float newYPosition = Clamp( scrollPostPosition.y, ( mDomainMax.y + size.y ) - mMaxOvershoot.y, mDomainMin.y + mMaxOvershoot.y );
409 if( ( newYPosition < scrollPostPosition.y - Math::MACHINE_EPSILON_1 )
410 || ( newYPosition > scrollPostPosition.y + Math::MACHINE_EPSILON_1 ) )
412 mPrePosition.y = newYPosition;
413 mLocalStart.y = panPosition.y;
415 scrollPostPosition.y = newYPosition;
418 // If we are using a fixed ruler in a particular axis, limit the maximum pages scrolled on that axis.
419 if( mFixedRulerX || mFixedRulerY )
421 // Here we limit the maximum amount that can be moved from the starting position of the gesture to one page.
422 // We do this only if we have a fixed ruler (on that axis) and the mode is enabled.
423 // Note: 1.0f is subtracted to keep the value within one page size (otherwise we stray on to the page after).
424 // Note: A further 1.0f is subtracted to handle a compensation that happens later within the flick handling code in SnapWithVelocity().
425 // When a flick is completed, an adjustment of 1.0f is sometimes made to allow for the scenario where:
426 // A flick finishes before the update thread has advanced the scroll position past the previous snap point.
427 Vector2 pageSizeLimit( size.x - ( 1.0f + 1.0f ), size.y - ( 1.0f - 1.0f ) );
428 Vector2 minPosition( mStartPosition.x - pageSizeLimit.x, mStartPosition.y - pageSizeLimit.y );
429 Vector2 maxPosition( mStartPosition.x + pageSizeLimit.x, mStartPosition.y + pageSizeLimit.y );
433 scrollPostPosition.x = Clamp( scrollPostPosition.x, minPosition.x, maxPosition.x );
437 scrollPostPosition.y = Clamp( scrollPostPosition.y, minPosition.y, maxPosition.y );
443 Vector2 mPrePosition;
445 Vector2 mStartPosition; ///< The start position of the gesture - used to limit scroll amount (not modified by clamping).
446 Vector2 mInitialPanMask; ///< Initial pan mask (based on ruler settings).
447 Vector2 mCurrentPanMask; ///< Current pan mask that can be altered by axis lock mode.
450 Vector2 mMaxOvershoot;
452 float mAxisAutoLockGradient; ///< Set by ScrollView
453 ScrollView::LockAxis mLockAxis;
455 bool mAxisAutoLock:1; ///< Set by ScrollView
464 * Internal Position Property Constraint.
466 * Generates position property based on pre-position
467 * Note: This is the position after clamping.
468 * (uses result of InternalPrePositionConstraint)
470 struct InternalPositionConstraint
472 InternalPositionConstraint(const RulerDomain& domainX, const RulerDomain& domainY, bool wrap)
473 : mDomainMin( -domainX.min, -domainY.min ),
474 mDomainMax( -domainX.max, -domainY.max ),
475 mClampX( domainX.enabled ),
476 mClampY( domainY.enabled ),
481 void operator()( Vector2& position, const PropertyInputContainer& inputs )
483 position = inputs[0]->GetVector2();
484 const Vector2& size = inputs[3]->GetVector3().GetVectorXY();
485 const Vector2& min = inputs[1]->GetVector2();
486 const Vector2& max = inputs[2]->GetVector2();
490 position.x = -WrapInDomain(-position.x, min.x, max.x);
491 position.y = -WrapInDomain(-position.y, min.y, max.y);
495 // clamp post position to domain
496 position.x = mClampX ? Clamp(position.x, mDomainMax.x + size.x, mDomainMin.x ) : position.x;
497 position.y = mClampY ? Clamp(position.y, mDomainMax.y + size.y, mDomainMin.y ) : position.y;
510 * This constraint updates the X overshoot property using the difference
511 * SCROLL_PRE_POSITION.x and SCROLL_POSITION.x, returning a relative value between 0.0f and 1.0f
513 struct OvershootXConstraint
515 OvershootXConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
517 void operator()( float& current, const PropertyInputContainer& inputs )
519 if( inputs[2]->GetBoolean() )
521 const Vector2& scrollPrePosition = inputs[0]->GetVector2();
522 const Vector2& scrollPostPosition = inputs[1]->GetVector2();
523 float newOvershoot = scrollPrePosition.x - scrollPostPosition.x;
524 current = (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
536 * This constraint updates the Y overshoot property using the difference
537 * SCROLL_PRE_POSITION.y and SCROLL_POSITION.y, returning a relative value between 0.0f and 1.0f
539 struct OvershootYConstraint
541 OvershootYConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {}
543 void operator()( float& current, const PropertyInputContainer& inputs )
545 if( inputs[2]->GetBoolean() )
547 const Vector2& scrollPrePosition = inputs[0]->GetVector2();
548 const Vector2& scrollPostPosition = inputs[1]->GetVector2();
549 float newOvershoot = scrollPrePosition.y - scrollPostPosition.y;
550 current = (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot;
562 * Internal Position-Delta Property Constraint.
564 * Generates position-delta property based on scroll-position + scroll-offset properties.
566 void InternalPositionDeltaConstraint( Vector2& current, const PropertyInputContainer& inputs )
568 const Vector2& scrollPosition = inputs[0]->GetVector2();
569 const Vector2& scrollOffset = inputs[1]->GetVector2();
571 current = scrollPosition + scrollOffset;
575 * Internal Final Position Constraint
576 * The position of content is:
577 * of scroll-position + f(scroll-overshoot)
578 * where f(...) function defines how overshoot
579 * should affect final-position.
581 struct InternalFinalConstraint
583 InternalFinalConstraint(AlphaFunctionPrototype functionX,
584 AlphaFunctionPrototype functionY)
585 : mFunctionX(functionX),
586 mFunctionY(functionY)
590 void operator()( Vector2& current, const PropertyInputContainer& inputs )
592 const float& overshootx = inputs[1]->GetFloat();
593 const float& overshooty = inputs[2]->GetFloat();
594 Vector2 offset( mFunctionX(overshootx),
595 mFunctionY(overshooty) );
597 current = inputs[0]->GetVector2() - offset;
600 AlphaFunctionPrototype mFunctionX;
601 AlphaFunctionPrototype mFunctionY;
607 ///////////////////////////////////////////////////////////////////////////////////////////////////
609 ///////////////////////////////////////////////////////////////////////////////////////////////////
611 Dali::Toolkit::ScrollView ScrollView::New()
613 // Create the implementation
614 ScrollViewPtr scrollView(new ScrollView());
616 // Pass ownership to CustomActor via derived handle
617 Dali::Toolkit::ScrollView handle(*scrollView);
619 // Second-phase init of the implementation
620 // This can only be done after the CustomActor connection has been made...
621 scrollView->Initialize();
626 ScrollView::ScrollView()
627 : ScrollBase( ControlBehaviour( REQUIRES_WHEEL_EVENTS ) ), // Enable size negotiation
629 mGestureStackDepth(0),
630 mScrollStateFlags(0),
631 mLockAxis(LockPossible),
632 mScrollUpdateDistance(DEFAULT_SCROLL_UPDATE_DISTANCE),
633 mMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT),
634 mUserMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT),
635 mSnapOvershootDuration(DEFAULT_SNAP_OVERSHOOT_DURATION),
636 mSnapOvershootAlphaFunction(AlphaFunction::EASE_OUT),
637 mSnapDuration(DEFAULT_SLOW_SNAP_ANIMATION_DURATION),
638 mSnapAlphaFunction(AlphaFunction::EASE_OUT),
639 mMinFlickDistance(DEFAULT_MIN_FLICK_DISTANCE),
640 mFlickSpeedThreshold(DEFAULT_MIN_FLICK_SPEED_THRESHOLD),
641 mFlickDuration(DEFAULT_FAST_SNAP_ANIMATION_DURATION),
642 mFlickAlphaFunction(AlphaFunction::EASE_OUT),
643 mAxisAutoLockGradient(DEFAULT_AXIS_AUTO_LOCK_GRADIENT),
644 mFrictionCoefficient(DEFAULT_FRICTION_COEFFICIENT),
645 mFlickSpeedCoefficient(DEFAULT_FLICK_SPEED_COEFFICIENT),
646 mMaxFlickSpeed(DEFAULT_MAX_FLICK_SPEED),
647 mWheelScrollDistanceStep(Vector2::ZERO),
648 mInAccessibilityPan(false),
650 mScrollInterrupted(false),
653 mTouchDownTimeoutReached(false),
654 mActorAutoSnapEnabled(false),
655 mAutoResizeContainerEnabled(false),
657 mAxisAutoLock(false),
659 mDefaultMaxOvershoot(true),
660 mCanScrollHorizontal(true),
661 mCanScrollVertical(true)
665 void ScrollView::OnInitialize()
669 // Internal Actor, used to hide actors from enumerations.
670 // Also actors added to Internal actor appear as overlays e.g. ScrollBar components.
671 mInternalActor = Actor::New();
672 self.Add(mInternalActor);
674 mInternalActor.SetParentOrigin(ParentOrigin::CENTER);
675 mInternalActor.SetAnchorPoint(AnchorPoint::CENTER);
676 mInternalActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
680 mScrollPostPosition = mScrollPrePosition = Vector2::ZERO;
682 mWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
684 mGestureStackDepth = 0;
686 self.TouchSignal().Connect( this, &ScrollView::OnTouch );
687 EnableGestureDetection( Gesture::Type( Gesture::Pan ) );
689 // By default we'll allow the user to freely drag the scroll view,
690 // while disabling the other rulers.
691 RulerPtr ruler = new DefaultRuler();
695 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, mCanScrollVertical);
696 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, mCanScrollHorizontal);
698 UpdatePropertyDomain();
699 SetInternalConstraints();
702 void ScrollView::OnStageConnection( int depth )
704 ScrollBase::OnStageConnection( depth );
706 DALI_LOG_SCROLL_STATE("[0x%X]", this);
710 SetScrollSensitive( false );
711 SetScrollSensitive( true );
713 if(IsOvershootEnabled())
715 // try and make sure property notifications are set
716 EnableScrollOvershoot(true);
720 void ScrollView::OnStageDisconnection()
722 DALI_LOG_SCROLL_STATE("[0x%X]", this);
726 ScrollBase::OnStageDisconnection();
729 ScrollView::~ScrollView()
731 DALI_LOG_SCROLL_STATE("[0x%X]", this);
734 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
736 return mSnapAlphaFunction;
739 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
741 mSnapAlphaFunction = alpha;
744 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
746 return mFlickAlphaFunction;
749 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
751 mFlickAlphaFunction = alpha;
754 float ScrollView::GetScrollSnapDuration() const
756 return mSnapDuration;
759 void ScrollView::SetScrollSnapDuration(float time)
761 mSnapDuration = time;
764 float ScrollView::GetScrollFlickDuration() const
766 return mFlickDuration;
769 void ScrollView::SetScrollFlickDuration(float time)
771 mFlickDuration = time;
774 void ScrollView::ApplyEffect(Toolkit::ScrollViewEffect effect)
776 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
778 // Assertion check to ensure effect doesn't already exist in this scrollview
779 bool effectAlreadyExistsInScrollView(false);
780 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
784 effectAlreadyExistsInScrollView = true;
789 DALI_ASSERT_ALWAYS(!effectAlreadyExistsInScrollView);
791 // add effect to effects list
792 mEffects.push_back(effect);
794 // invoke Attachment request to ScrollView first
795 GetImpl(effect).Attach(self);
798 void ScrollView::RemoveEffect(Toolkit::ScrollViewEffect effect)
800 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
802 // remove effect from effects list
803 bool effectExistedInScrollView(false);
804 for (ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
808 mEffects.erase(iter);
809 effectExistedInScrollView = true;
814 // Assertion check to ensure effect existed.
815 DALI_ASSERT_ALWAYS(effectExistedInScrollView);
817 // invoke Detachment request to ScrollView last
818 GetImpl(effect).Detach(self);
821 void ScrollView::RemoveAllEffects()
823 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
825 for (ScrollViewEffectIter effectIter = mEffects.begin(); effectIter != mEffects.end(); ++effectIter)
827 Toolkit::ScrollViewEffect effect = *effectIter;
829 // invoke Detachment request to ScrollView last
830 GetImpl(effect).Detach(self);
836 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
838 ApplyConstraintToBoundActors(constraint);
841 void ScrollView::RemoveConstraintsFromChildren()
843 RemoveConstraintsFromBoundActors();
846 const RulerPtr ScrollView::GetRulerX() const
851 const RulerPtr ScrollView::GetRulerY() const
856 void ScrollView::SetRulerX(RulerPtr ruler)
860 UpdatePropertyDomain();
861 UpdateMainInternalConstraint();
864 void ScrollView::SetRulerY(RulerPtr ruler)
868 UpdatePropertyDomain();
869 UpdateMainInternalConstraint();
872 void ScrollView::UpdatePropertyDomain()
875 Vector3 size = self.GetTargetSize();
876 Vector2 min = mMinScroll;
877 Vector2 max = mMaxScroll;
878 bool scrollPositionChanged = false;
879 bool domainChanged = false;
881 bool canScrollVertical = false;
882 bool canScrollHorizontal = false;
883 UpdateLocalScrollProperties();
884 if(mRulerX->IsEnabled())
886 const Toolkit::RulerDomain& rulerDomain = mRulerX->GetDomain();
887 if( fabsf(min.x - rulerDomain.min) > Math::MACHINE_EPSILON_100
888 || fabsf(max.x - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
890 domainChanged = true;
891 min.x = rulerDomain.min;
892 max.x = rulerDomain.max;
894 // make sure new scroll value is within new domain
895 if( mScrollPrePosition.x < min.x
896 || mScrollPrePosition.x > max.x )
898 scrollPositionChanged = true;
899 mScrollPrePosition.x = Clamp(mScrollPrePosition.x, -(max.x - size.x), -min.x);
902 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.x) > Math::MACHINE_EPSILON_100 )
904 canScrollHorizontal = true;
907 else if( fabs(min.x) > Math::MACHINE_EPSILON_100
908 || fabs(max.x) > Math::MACHINE_EPSILON_100 )
910 // need to reset to 0
911 domainChanged = true;
914 canScrollHorizontal = false;
917 if(mRulerY->IsEnabled())
919 const Toolkit::RulerDomain& rulerDomain = mRulerY->GetDomain();
920 if( fabsf(min.y - rulerDomain.min) > Math::MACHINE_EPSILON_100
921 || fabsf(max.y - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
923 domainChanged = true;
924 min.y = rulerDomain.min;
925 max.y = rulerDomain.max;
927 // make sure new scroll value is within new domain
928 if( mScrollPrePosition.y < min.y
929 || mScrollPrePosition.y > max.y )
931 scrollPositionChanged = true;
932 mScrollPrePosition.y = Clamp(mScrollPrePosition.y, -(max.y - size.y), -min.y);
935 if( (fabsf(rulerDomain.max - rulerDomain.min) - size.y) > Math::MACHINE_EPSILON_100 )
937 canScrollVertical = true;
940 else if( fabs(min.y) > Math::MACHINE_EPSILON_100
941 || fabs(max.y) > Math::MACHINE_EPSILON_100 )
943 // need to reset to 0
944 domainChanged = true;
947 canScrollVertical = false;
950 // avoid setting properties if possible, otherwise this will cause an entire update as well as triggering constraints using each property we update
951 if( mCanScrollVertical != canScrollVertical )
953 mCanScrollVertical = canScrollVertical;
954 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, canScrollVertical);
956 if( mCanScrollHorizontal != canScrollHorizontal )
958 mCanScrollHorizontal = canScrollHorizontal;
959 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, canScrollHorizontal);
961 if( scrollPositionChanged )
963 DALI_LOG_SCROLL_STATE("[0x%X] Domain Changed, setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
964 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
970 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, mMinScroll );
971 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, mMaxScroll );
975 bool ScrollView::GetScrollSensitive()
980 void ScrollView::SetScrollSensitive(bool sensitive)
983 PanGestureDetector panGesture( GetPanGestureDetector() );
985 DALI_LOG_SCROLL_STATE("[0x%X] sensitive: before:[%d] setting[%d]", this, int(mSensitive), int(sensitive));
987 if((!mSensitive) && (sensitive))
989 mSensitive = sensitive;
990 panGesture.Attach(self);
992 else if((mSensitive) && (!sensitive))
994 DALI_LOG_SCROLL_STATE("[0x%X] BEFORE: panning:[%d]", this, int(mPanning));
996 // while the scroll view is panning, the state needs to be reset.
999 PanGesture cancelGesture( Gesture::Cancelled );
1000 OnPan( cancelGesture );
1003 panGesture.Detach(self);
1004 mSensitive = sensitive;
1006 mGestureStackDepth = 0;
1007 DALI_LOG_SCROLL_STATE("[0x%X] AFTER: panning:[%d]", this, int(mPanning));
1011 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
1013 mMaxOvershoot.x = overshootX;
1014 mMaxOvershoot.y = overshootY;
1015 mUserMaxOvershoot = mMaxOvershoot;
1016 mDefaultMaxOvershoot = false;
1017 UpdateMainInternalConstraint();
1020 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
1022 mSnapOvershootAlphaFunction = alpha;
1025 float ScrollView::GetSnapOvershootDuration()
1027 return mSnapOvershootDuration;
1030 void ScrollView::SetSnapOvershootDuration(float duration)
1032 mSnapOvershootDuration = duration;
1035 bool ScrollView::GetActorAutoSnap()
1037 return mActorAutoSnapEnabled;
1040 void ScrollView::SetActorAutoSnap(bool enable)
1042 mActorAutoSnapEnabled = enable;
1045 void ScrollView::SetAutoResize(bool enable)
1047 mAutoResizeContainerEnabled = enable;
1048 // TODO: This needs a lot of issues to be addressed before working.
1051 bool ScrollView::GetWrapMode() const
1056 void ScrollView::SetWrapMode(bool enable)
1059 Self().SetProperty(Toolkit::ScrollView::Property::WRAP, enable);
1062 int ScrollView::GetScrollUpdateDistance() const
1064 return mScrollUpdateDistance;
1067 void ScrollView::SetScrollUpdateDistance(int distance)
1069 mScrollUpdateDistance = distance;
1072 bool ScrollView::GetAxisAutoLock() const
1074 return mAxisAutoLock;
1077 void ScrollView::SetAxisAutoLock(bool enable)
1079 mAxisAutoLock = enable;
1080 UpdateMainInternalConstraint();
1083 float ScrollView::GetAxisAutoLockGradient() const
1085 return mAxisAutoLockGradient;
1088 void ScrollView::SetAxisAutoLockGradient(float gradient)
1090 DALI_ASSERT_DEBUG( gradient >= 0.0f && gradient <= 1.0f );
1091 mAxisAutoLockGradient = gradient;
1092 UpdateMainInternalConstraint();
1095 float ScrollView::GetFrictionCoefficient() const
1097 return mFrictionCoefficient;
1100 void ScrollView::SetFrictionCoefficient(float friction)
1102 DALI_ASSERT_DEBUG( friction > 0.0f );
1103 mFrictionCoefficient = friction;
1106 float ScrollView::GetFlickSpeedCoefficient() const
1108 return mFlickSpeedCoefficient;
1111 void ScrollView::SetFlickSpeedCoefficient(float speed)
1113 mFlickSpeedCoefficient = speed;
1116 Vector2 ScrollView::GetMinimumDistanceForFlick() const
1118 return mMinFlickDistance;
1121 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
1123 mMinFlickDistance = distance;
1126 float ScrollView::GetMinimumSpeedForFlick() const
1128 return mFlickSpeedThreshold;
1131 void ScrollView::SetMinimumSpeedForFlick( float speed )
1133 mFlickSpeedThreshold = speed;
1136 float ScrollView::GetMaxFlickSpeed() const
1138 return mMaxFlickSpeed;
1141 void ScrollView::SetMaxFlickSpeed(float speed)
1143 mMaxFlickSpeed = speed;
1146 void ScrollView::SetWheelScrollDistanceStep(Vector2 step)
1148 mWheelScrollDistanceStep = step;
1151 Vector2 ScrollView::GetWheelScrollDistanceStep() const
1153 return mWheelScrollDistanceStep;
1156 unsigned int ScrollView::GetCurrentPage() const
1158 // in case animation is currently taking place.
1159 Vector2 position = GetPropertyPosition();
1161 Actor self = Self();
1162 unsigned int page = 0;
1163 unsigned int pagesPerVolume = 1;
1164 unsigned int volume = 0;
1166 // if rulerX is enabled, then get page count (columns)
1167 page = mRulerX->GetPageFromPosition(-position.x, mWrapMode);
1168 volume = mRulerY->GetPageFromPosition(-position.y, mWrapMode);
1169 pagesPerVolume = mRulerX->GetTotalPages();
1171 return volume * pagesPerVolume + page;
1174 Vector2 ScrollView::GetCurrentScrollPosition() const
1176 return -GetPropertyPosition();
1179 Vector2 ScrollView::GetDomainSize() const
1181 Vector3 size = Self().GetCurrentSize();
1183 const RulerDomain& xDomain = GetRulerX()->GetDomain();
1184 const RulerDomain& yDomain = GetRulerY()->GetDomain();
1187 domainSize.x = xDomain.max - xDomain.min - size.x;
1188 domainSize.y = yDomain.max - yDomain.min - size.y;
1192 void ScrollView::TransformTo(const Vector2& position,
1193 DirectionBias horizontalBias, DirectionBias verticalBias)
1195 TransformTo(position, mSnapDuration, mSnapAlphaFunction, horizontalBias, verticalBias);
1198 void ScrollView::TransformTo(const Vector2& position, float duration, AlphaFunction alpha,
1199 DirectionBias horizontalBias, DirectionBias verticalBias)
1201 // If this is called while the timer is running, then cancel it
1202 StopTouchDownTimer();
1204 Actor self( Self() );
1206 // Guard against destruction during signal emission
1207 // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo()
1208 Toolkit::ScrollView handle( GetOwner() );
1210 DALI_LOG_SCROLL_STATE("[0x%X] pos[%.2f,%.2f], duration[%.2f] bias[%d, %d]",
1211 this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1213 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1214 self.SetProperty( Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(currentScrollPosition) );
1216 if( mScrolling ) // are we interrupting a current scroll?
1218 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
1220 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1221 mScrollCompletedSignal.Emit( currentScrollPosition );
1224 if( mPanning ) // are we interrupting a current pan?
1226 DALI_LOG_SCROLL_STATE("[0x%X] Interrupting Pan, set to false", this );
1228 mGestureStackDepth = 0;
1229 self.SetProperty( Toolkit::ScrollView::Property::PANNING, false );
1231 if( mScrollMainInternalPrePositionConstraint )
1233 mScrollMainInternalPrePositionConstraint.Remove();
1237 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, true);
1240 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1241 mScrollStartedSignal.Emit( currentScrollPosition );
1242 bool animating = AnimateTo(-position,
1243 Vector2::ONE * duration,
1252 // if not animating, then this pan has completed right now.
1253 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
1256 // If we have no duration, then in the next update frame, we will be at the position specified as we just set.
1257 // In this scenario, we cannot return the currentScrollPosition as this is out-of-date and should instead return the requested final position
1258 Vector2 completedPosition( currentScrollPosition );
1259 if( duration <= Math::MACHINE_EPSILON_10 )
1261 completedPosition = position;
1264 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 2 [%.2f, %.2f]", this, completedPosition.x, completedPosition.y);
1265 SetScrollUpdateNotification(false);
1266 mScrollCompletedSignal.Emit( completedPosition );
1270 void ScrollView::ScrollTo(const Vector2& position)
1272 ScrollTo(position, mSnapDuration );
1275 void ScrollView::ScrollTo(const Vector2& position, float duration)
1277 ScrollTo(position, duration, DirectionBiasNone, DirectionBiasNone);
1280 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha)
1282 ScrollTo(position, duration, alpha, DirectionBiasNone, DirectionBiasNone);
1285 void ScrollView::ScrollTo(const Vector2& position, float duration,
1286 DirectionBias horizontalBias, DirectionBias verticalBias)
1288 ScrollTo(position, duration, mSnapAlphaFunction, horizontalBias, verticalBias);
1291 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha,
1292 DirectionBias horizontalBias, DirectionBias verticalBias)
1294 DALI_LOG_SCROLL_STATE("[0x%X] position[%.2f, %.2f] duration[%.2f], bias[%d, %d]", this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1295 TransformTo(position, duration, alpha, horizontalBias, verticalBias);
1298 void ScrollView::ScrollTo(unsigned int page)
1300 ScrollTo(page, mSnapDuration);
1303 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
1306 unsigned int volume;
1307 unsigned int libraries;
1309 // The position to scroll to is continuous and linear
1310 // unless a domain has been enabled on the X axis.
1311 // or if WrapMode has been enabled.
1312 bool carryX = mRulerX->GetDomain().enabled | mWrapMode;
1313 bool carryY = mRulerY->GetDomain().enabled | mWrapMode;
1315 position.x = mRulerX->GetPositionFromPage(page, volume, carryX);
1316 position.y = mRulerY->GetPositionFromPage(volume, libraries, carryY);
1318 ScrollTo(position, duration, bias, bias);
1321 void ScrollView::ScrollTo(Actor &actor)
1323 ScrollTo(actor, mSnapDuration);
1326 void ScrollView::ScrollTo(Actor &actor, float duration)
1328 DALI_ASSERT_ALWAYS(actor.GetParent() == Self());
1330 Actor self = Self();
1331 Vector3 size = self.GetCurrentSize();
1332 Vector3 position = actor.GetCurrentPosition();
1333 Vector2 prePosition = GetPropertyPrePosition();
1334 position.GetVectorXY() -= prePosition;
1336 ScrollTo(Vector2(position.x - size.width * 0.5f, position.y - size.height * 0.5f), duration);
1339 Actor ScrollView::FindClosestActor()
1341 Actor self = Self();
1342 Vector3 size = self.GetCurrentSize();
1344 return FindClosestActorToPosition(Vector3(size.width * 0.5f,size.height * 0.5f,0.0f));
1347 Actor ScrollView::FindClosestActorToPosition(const Vector3& position, FindDirection dirX, FindDirection dirY, FindDirection dirZ)
1350 float closestDistance2 = 0.0f;
1351 Vector3 actualPosition = position;
1353 unsigned int numChildren = Self().GetChildCount();
1355 for(unsigned int i = 0; i < numChildren; ++i)
1357 Actor child = Self().GetChildAt(i);
1359 if(mInternalActor == child) // ignore internal actor.
1364 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1366 Vector3 delta = childPosition - actualPosition;
1368 // X-axis checking (only find Actors to the [dirX] of actualPosition)
1369 if(dirX > All) // != All,None
1371 FindDirection deltaH = delta.x > 0 ? Right : Left;
1378 // Y-axis checking (only find Actors to the [dirY] of actualPosition)
1379 if(dirY > All) // != All,None
1381 FindDirection deltaV = delta.y > 0 ? Down : Up;
1388 // Z-axis checking (only find Actors to the [dirZ] of actualPosition)
1389 if(dirZ > All) // != All,None
1391 FindDirection deltaV = delta.y > 0 ? In : Out;
1398 // compare child to closest child in terms of distance.
1399 float distance2 = 0.0f;
1401 // distance2 = the Square of the relevant dimensions of delta
1404 distance2 += delta.x * delta.x;
1409 distance2 += delta.y * delta.y;
1414 distance2 += delta.z * delta.z;
1417 if(closestChild) // Next time.
1419 if(distance2 < closestDistance2)
1421 closestChild = child;
1422 closestDistance2 = distance2;
1427 closestChild = child;
1428 closestDistance2 = distance2;
1432 return closestChild;
1435 bool ScrollView::ScrollToSnapPoint()
1437 DALI_LOG_SCROLL_STATE("[0x%X]", this );
1438 Vector2 stationaryVelocity = Vector2(0.0f, 0.0f);
1439 return SnapWithVelocity( stationaryVelocity );
1442 // TODO: In situations where axes are different (X snap, Y free)
1443 // Each axis should really have their own independent animation (time and equation)
1444 // Consider, X axis snapping to nearest grid point (EaseOut over fixed time)
1445 // Consider, Y axis simulating physics to arrive at a point (Physics equation over variable time)
1446 // Currently, the axes have been split however, they both use the same EaseOut equation.
1447 bool ScrollView::SnapWithVelocity(Vector2 velocity)
1449 // Animator takes over now, touches are assumed not to interfere.
1450 // And if touches do interfere, then we'll stop animation, update PrePosition
1451 // to current mScroll's properties, and then resume.
1452 // Note: For Flicking this may work a bit different...
1454 float angle = atan2(velocity.y, velocity.x);
1455 float speed2 = velocity.LengthSquared();
1456 AlphaFunction alphaFunction = mSnapAlphaFunction;
1457 Vector2 positionDuration = Vector2::ONE * mSnapDuration;
1460 FindDirection horizontal = None;
1461 FindDirection vertical = None;
1463 // orthoAngleRange = Angle tolerance within the Exact N,E,S,W direction
1464 // that will be accepted as a general N,E,S,W flick direction.
1466 const float orthoAngleRange = FLICK_ORTHO_ANGLE_RANGE * M_PI / 180.0f;
1467 const float flickSpeedThreshold2 = mFlickSpeedThreshold * mFlickSpeedThreshold;
1469 Vector2 positionSnap = mScrollPrePosition;
1471 // Flick logic X Axis
1473 if(mRulerX->IsEnabled() && mLockAxis != LockHorizontal)
1477 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1478 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1480 if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East
1482 biasX = 0.0f, horizontal = Left;
1484 // This guards against an error where no movement occurs, due to the flick finishing
1485 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1486 positionSnap.x += 1.0f;
1488 else if((angle >= M_PI-orthoAngleRange) || (angle < -M_PI+orthoAngleRange)) // Swiping West
1490 biasX = 1.0f, horizontal = Right;
1492 // This guards against an error where no movement occurs, due to the flick finishing
1493 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
1494 positionSnap.x -= 1.0f;
1499 // Flick logic Y Axis
1501 if(mRulerY->IsEnabled() && mLockAxis != LockVertical)
1505 if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold
1506 mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions
1508 if((angle >= M_PI_2-orthoAngleRange) && (angle < M_PI_2+orthoAngleRange)) // Swiping South
1510 biasY = 0.0f, vertical = Up;
1512 else if((angle >= -M_PI_2-orthoAngleRange) && (angle < -M_PI_2+orthoAngleRange)) // Swiping North
1514 biasY = 1.0f, vertical = Down;
1519 // isFlick: Whether this gesture is a flick or not.
1520 bool isFlick = (horizontal != All || vertical != All);
1521 // isFreeFlick: Whether this gesture is a flick under free panning criteria.
1522 bool isFreeFlick = velocity.LengthSquared() > (FREE_FLICK_SPEED_THRESHOLD*FREE_FLICK_SPEED_THRESHOLD);
1524 if(isFlick || isFreeFlick)
1526 positionDuration = Vector2::ONE * mFlickDuration;
1527 alphaFunction = mFlickAlphaFunction;
1530 // Calculate next positionSnap ////////////////////////////////////////////////////////////
1532 if(mActorAutoSnapEnabled)
1534 Vector3 size = Self().GetCurrentSize();
1536 Actor child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f), horizontal, vertical );
1538 if(!child && isFlick )
1540 // If we conducted a direction limited search and found no actor, then just snap to the closest actor.
1541 child = FindClosestActorToPosition( Vector3(size.width * 0.5f,size.height * 0.5f,0.0f) );
1546 Vector2 position = Self().GetProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_POSITION);
1548 // Get center-point of the Actor.
1549 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
1551 if(mRulerX->IsEnabled())
1553 positionSnap.x = position.x - childPosition.x + size.width * 0.5f;
1555 if(mRulerY->IsEnabled())
1557 positionSnap.y = position.y - childPosition.y + size.height * 0.5f;
1562 Vector2 startPosition = positionSnap;
1563 positionSnap.x = -mRulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system.
1564 positionSnap.y = -mRulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
1566 Vector2 clampDelta(Vector2::ZERO);
1567 ClampPosition(positionSnap);
1569 if( (mRulerX->GetType() == Ruler::Free || mRulerY->GetType() == Ruler::Free)
1570 && isFreeFlick && !mActorAutoSnapEnabled)
1572 // Calculate target position based on velocity of flick.
1574 // a = Deceleration (Set to diagonal stage length * friction coefficient)
1575 // u = Initial Velocity (Flick velocity)
1576 // v = 0 (Final Velocity)
1577 // t = Time (Velocity / Deceleration)
1578 Vector2 stageSize = Stage::GetCurrent().GetSize();
1579 float stageLength = Vector3(stageSize.x, stageSize.y, 0.0f).Length();
1580 float a = (stageLength * mFrictionCoefficient);
1581 Vector3 u = Vector3(velocity.x, velocity.y, 0.0f) * mFlickSpeedCoefficient;
1582 float speed = u.Length();
1585 // TODO: Change this to a decay function. (faster you flick, the slower it should be)
1586 speed = std::min(speed, stageLength * mMaxFlickSpeed );
1588 alphaFunction = ConstantDecelerationAlphaFunction;
1590 float t = speed / a;
1592 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1594 positionSnap.x += t*u.x*0.5f;
1597 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1599 positionSnap.y += t*u.y*0.5f;
1602 clampDelta = positionSnap;
1603 ClampPosition(positionSnap);
1604 if((positionSnap - startPosition).LengthSquared() > Math::MACHINE_EPSILON_0)
1606 clampDelta -= positionSnap;
1607 clampDelta.x = clampDelta.x > 0.0f ? std::min(clampDelta.x, mMaxOvershoot.x) : std::max(clampDelta.x, -mMaxOvershoot.x);
1608 clampDelta.y = clampDelta.y > 0.0f ? std::min(clampDelta.y, mMaxOvershoot.y) : std::max(clampDelta.y, -mMaxOvershoot.y);
1612 clampDelta = Vector2::ZERO;
1615 // If Axis is Free and has velocity, then calculate time taken
1616 // to reach target based on velocity in axis.
1617 if(mRulerX->IsEnabled() && mRulerX->GetType() == Ruler::Free)
1619 float deltaX = fabsf(startPosition.x - positionSnap.x);
1621 if(fabsf(u.x) > Math::MACHINE_EPSILON_1)
1623 positionDuration.x = fabsf(deltaX / u.x);
1627 positionDuration.x = 0;
1631 if(mRulerY->IsEnabled() && mRulerY->GetType() == Ruler::Free)
1633 float deltaY = fabsf(startPosition.y - positionSnap.y);
1635 if(fabsf(u.y) > Math::MACHINE_EPSILON_1)
1637 positionDuration.y = fabsf(deltaY / u.y);
1641 positionDuration.y = 0;
1646 if(IsOvershootEnabled())
1648 // Scroll to the end of the overshoot only when overshoot is enabled.
1649 positionSnap += clampDelta;
1652 bool animating = AnimateTo(positionSnap, positionDuration,
1653 alphaFunction, false,
1654 DirectionBiasNone, DirectionBiasNone,
1655 isFlick || isFreeFlick ? Flick : Snap);
1660 void ScrollView::StopAnimation(void)
1662 // Clear Snap animation if exists.
1663 StopAnimation(mInternalXAnimation);
1664 StopAnimation(mInternalYAnimation);
1665 mScrollStateFlags = 0;
1666 // remove scroll animation flags
1667 HandleStoppedAnimation();
1670 void ScrollView::StopAnimation(Animation& animation)
1679 bool ScrollView::AnimateTo(const Vector2& position, const Vector2& positionDuration,
1680 AlphaFunction alpha, bool findShortcuts,
1681 DirectionBias horizontalBias, DirectionBias verticalBias,
1684 // Here we perform an animation on a number of properties (depending on which have changed)
1685 // The animation is applied to all ScrollBases
1686 Actor self = Self();
1687 mScrollTargetPosition = position;
1688 float totalDuration = 0.0f;
1690 bool positionChanged = (mScrollTargetPosition != mScrollPostPosition);
1694 totalDuration = std::max(totalDuration, positionDuration.x);
1695 totalDuration = std::max(totalDuration, positionDuration.y);
1699 // try to animate for a frame, on some occasions update will be changing scroll value while event side thinks it hasnt changed
1700 totalDuration = 0.01f;
1701 positionChanged = true;
1706 // Position Delta ///////////////////////////////////////////////////////
1709 if(mWrapMode && findShortcuts)
1711 // In Wrap Mode, the shortest distance is a little less intuitive...
1712 const RulerDomain rulerDomainX = mRulerX->GetDomain();
1713 const RulerDomain rulerDomainY = mRulerY->GetDomain();
1715 if(mRulerX->IsEnabled())
1717 float dir = VectorInDomain(-mScrollPrePosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
1718 mScrollTargetPosition.x = mScrollPrePosition.x + -dir;
1721 if(mRulerY->IsEnabled())
1723 float dir = VectorInDomain(-mScrollPrePosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
1724 mScrollTargetPosition.y = mScrollPrePosition.y + -dir;
1728 // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting
1729 // a horizonal/vertical wall.delay
1730 AnimateInternalXTo(mScrollTargetPosition.x, positionDuration.x, alpha);
1731 AnimateInternalYTo(mScrollTargetPosition.y, positionDuration.y, alpha);
1733 if( !(mScrollStateFlags & SCROLL_ANIMATION_FLAGS) )
1735 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollTargetPosition.x, mScrollTargetPosition.y );
1736 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollTargetPosition);
1737 mScrollPrePosition = mScrollTargetPosition;
1738 mScrollPostPosition = mScrollTargetPosition;
1739 WrapPosition(mScrollPostPosition);
1742 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 );
1743 DALI_LOG_SCROLL_STATE("[0x%X] SCROLL_PRE_POSITION[%.2f, %.2f], SCROLL_POSITION[%.2f, %.2f]", this, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ).Get<Vector2>().x, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ).Get<Vector2>().y, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_POSITION ).Get<Vector2>().x, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_POSITION ).Get<Vector2>().y );
1746 SetScrollUpdateNotification(true);
1748 // Always send a snap event when AnimateTo is called.
1749 Toolkit::ScrollView::SnapEvent snapEvent;
1750 snapEvent.type = snapType;
1751 snapEvent.position = -mScrollTargetPosition;
1752 snapEvent.duration = totalDuration;
1754 DALI_LOG_SCROLL_STATE("[0x%X] mSnapStartedSignal [%.2f, %.2f]", this, snapEvent.position.x, snapEvent.position.y);
1755 mSnapStartedSignal.Emit( snapEvent );
1757 return (mScrollStateFlags & SCROLL_ANIMATION_FLAGS) != 0;
1760 void ScrollView::EnableScrollOvershoot(bool enable)
1764 if (!mOvershootIndicator)
1766 mOvershootIndicator = ScrollOvershootIndicator::New();
1769 mOvershootIndicator->AttachToScrollable(*this);
1773 mMaxOvershoot = mUserMaxOvershoot;
1775 if (mOvershootIndicator)
1777 mOvershootIndicator->DetachFromScrollable(*this);
1781 UpdateMainInternalConstraint();
1784 void ScrollView::AddOverlay(Actor actor)
1786 actor.SetDrawMode( DrawMode::OVERLAY_2D );
1787 mInternalActor.Add( actor );
1790 void ScrollView::RemoveOverlay(Actor actor)
1792 mInternalActor.Remove( actor );
1795 void ScrollView::SetOvershootEffectColor( const Vector4& color )
1797 mOvershootEffectColor = color;
1798 if( mOvershootIndicator )
1800 mOvershootIndicator->SetOvershootEffectColor( color );
1804 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
1806 PanGestureDetector panGesture( GetPanGestureDetector() );
1808 // First remove just in case we have some set, then add.
1809 panGesture.RemoveDirection( direction );
1810 panGesture.AddDirection( direction, threshold );
1813 void ScrollView::RemoveScrollingDirection( Radian direction )
1815 PanGestureDetector panGesture( GetPanGestureDetector() );
1816 panGesture.RemoveDirection( direction );
1819 Toolkit::ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
1821 return mSnapStartedSignal;
1824 void ScrollView::FindAndUnbindActor(Actor child)
1829 Vector2 ScrollView::GetPropertyPrePosition() const
1831 Vector2 position = Self().GetProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION);
1832 WrapPosition(position);
1836 Vector2 ScrollView::GetPropertyPosition() const
1838 Vector2 position = Self().GetProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_POSITION);
1839 WrapPosition(position);
1844 void ScrollView::HandleStoppedAnimation()
1846 SetScrollUpdateNotification(false);
1849 void ScrollView::HandleSnapAnimationFinished()
1851 // Emit Signal that scrolling has completed.
1853 Actor self = Self();
1854 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
1856 Vector2 deltaPosition(mScrollPrePosition);
1858 UpdateLocalScrollProperties();
1859 WrapPosition(mScrollPrePosition);
1860 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
1861 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
1863 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1864 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 3 current[%.2f, %.2f], mScrollTargetPosition[%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y, -mScrollTargetPosition.x, -mScrollTargetPosition.y );
1865 mScrollCompletedSignal.Emit( currentScrollPosition );
1867 mDomainOffset += deltaPosition - mScrollPostPosition;
1868 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset);
1869 HandleStoppedAnimation();
1872 void ScrollView::SetScrollUpdateNotification( bool enabled )
1874 Actor self = Self();
1875 if( mScrollXUpdateNotification )
1877 // disconnect now to avoid a notification before removed from update thread
1878 mScrollXUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1879 self.RemovePropertyNotification(mScrollXUpdateNotification);
1880 mScrollXUpdateNotification.Reset();
1882 if( enabled && !mScrollUpdatedSignal.Empty())
1884 // Only set up the notification when the application has connected to the updated signal
1885 mScrollXUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 0, StepCondition(mScrollUpdateDistance, 0.0f));
1886 mScrollXUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1888 if( mScrollYUpdateNotification )
1890 // disconnect now to avoid a notification before removed from update thread
1891 mScrollYUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1892 self.RemovePropertyNotification(mScrollYUpdateNotification);
1893 mScrollYUpdateNotification.Reset();
1895 if( enabled && !mScrollUpdatedSignal.Empty())
1897 // Only set up the notification when the application has connected to the updated signal
1898 mScrollYUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 1, StepCondition(mScrollUpdateDistance, 0.0f));
1899 mScrollYUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification );
1903 void ScrollView::OnScrollUpdateNotification(Dali::PropertyNotification& source)
1905 // Guard against destruction during signal emission
1906 Toolkit::ScrollView handle( GetOwner() );
1908 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1909 mScrollUpdatedSignal.Emit( currentScrollPosition );
1912 bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1914 Dali::BaseHandle handle( object );
1916 bool connected( true );
1917 Toolkit::ScrollView view = Toolkit::ScrollView::DownCast( handle );
1919 if( 0 == strcmp( signalName.c_str(), SIGNAL_SNAP_STARTED ) )
1921 view.SnapStartedSignal().Connect( tracker, functor );
1925 // signalName does not match any signal
1932 void ScrollView::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1934 // need to update domain properties for new size
1935 UpdatePropertyDomain();
1938 void ScrollView::OnSizeSet( const Vector3& size )
1940 // need to update domain properties for new size
1941 if( mDefaultMaxOvershoot )
1943 mUserMaxOvershoot.x = size.x * 0.5f;
1944 mUserMaxOvershoot.y = size.y * 0.5f;
1945 if( !IsOvershootEnabled() )
1947 mMaxOvershoot = mUserMaxOvershoot;
1950 UpdatePropertyDomain();
1951 UpdateMainInternalConstraint();
1952 if( IsOvershootEnabled() )
1954 mOvershootIndicator->Reset();
1958 void ScrollView::OnChildAdd(Actor& child)
1960 ScrollBase::OnChildAdd( child );
1962 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
1965 mInternalActor.Add(scrollBar);
1966 if(scrollBar.GetScrollDirection() == Toolkit::ScrollBar::Horizontal)
1968 scrollBar.SetScrollPropertySource(Self(),
1969 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_X,
1970 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_X,
1971 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_X,
1972 Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_X);
1976 scrollBar.SetScrollPropertySource(Self(),
1977 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_Y,
1978 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
1979 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_Y,
1980 Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_Y);
1983 else if(mAlterChild)
1989 void ScrollView::OnChildRemove(Actor& child)
1991 // TODO: Actor needs a RemoveConstraint method to take out an individual constraint.
1994 ScrollBase::OnChildRemove( child );
1997 void ScrollView::StartTouchDownTimer()
1999 if ( !mTouchDownTimer )
2001 mTouchDownTimer = Timer::New( TOUCH_DOWN_TIMER_INTERVAL );
2002 mTouchDownTimer.TickSignal().Connect( this, &ScrollView::OnTouchDownTimeout );
2005 mTouchDownTimer.Start();
2008 void ScrollView::StopTouchDownTimer()
2010 if ( mTouchDownTimer )
2012 mTouchDownTimer.Stop();
2016 bool ScrollView::OnTouchDownTimeout()
2018 DALI_LOG_SCROLL_STATE("[0x%X]", this);
2020 mTouchDownTimeoutReached = true;
2022 unsigned int currentScrollStateFlags( mScrollStateFlags ); // Cleared in StopAnimation so keep local copy for comparison
2023 if( currentScrollStateFlags & (SCROLL_ANIMATION_FLAGS | SNAP_ANIMATION_FLAGS) )
2025 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling Or snapping flags set, stopping animation", this);
2028 if( currentScrollStateFlags & SCROLL_ANIMATION_FLAGS )
2030 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling flags set, emitting signal", this);
2032 mScrollInterrupted = true;
2033 // reset domain offset as scrolling from original plane.
2034 mDomainOffset = Vector2::ZERO;
2035 Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, Vector2::ZERO);
2037 UpdateLocalScrollProperties();
2038 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2039 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 4 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2040 mScrollCompletedSignal.Emit( currentScrollPosition );
2047 bool ScrollView::OnTouch( Actor actor, const TouchData& touch )
2051 DALI_LOG_SCROLL_STATE("[0x%X], Not Sensitive, ignoring", this);
2053 // Ignore this touch event, if scrollview is insensitive.
2057 // Ignore events with multiple-touch points
2058 if (touch.GetPointCount() != 1)
2060 DALI_LOG_SCROLL_STATE("[0x%X], multiple touch, ignoring", this);
2065 const PointState::Type pointState = touch.GetState( 0 );
2066 if( pointState == PointState::DOWN )
2068 DALI_LOG_SCROLL_STATE("[0x%X] Down", this);
2070 if(mGestureStackDepth==0)
2072 mTouchDownTime = touch.GetTime();
2074 // This allows time for a pan-gesture to start, to avoid breaking snap-animation behavior with fast flicks.
2075 // If touch-down does not become a pan (after timeout interval), then snap-animation can be interrupted.
2076 mTouchDownTimeoutReached = false;
2077 mScrollInterrupted = false;
2078 StartTouchDownTimer();
2081 else if( ( pointState == PointState::UP ) ||
2082 ( ( pointState == PointState::INTERRUPTED ) && ( touch.GetHitActor( 0 )== Self() ) ) )
2084 DALI_LOG_SCROLL_STATE("[0x%X] %s", this, ( ( pointState == TouchPoint::Up ) ? "Up" : "Interrupted" ) );
2086 StopTouchDownTimer();
2088 // if the user touches and releases without enough movement to go
2089 // into a gesture state, then we should snap to nearest point.
2090 // otherwise our scroll could be stopped (interrupted) half way through an animation.
2091 if(mGestureStackDepth==0 && mTouchDownTimeoutReached)
2093 if( ( pointState == PointState::INTERRUPTED ) ||
2094 ( ( touch.GetTime() - mTouchDownTime ) >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET ) )
2096 // Reset the velocity only if down was received a while ago
2097 mLastVelocity = Vector2( 0.0f, 0.0f );
2100 UpdateLocalScrollProperties();
2101 // Only finish the transform if scrolling was interrupted on down or if we are scrolling
2102 if ( mScrollInterrupted || mScrolling )
2104 DALI_LOG_SCROLL_STATE("[0x%X] Calling FinishTransform", this);
2109 mTouchDownTimeoutReached = false;
2110 mScrollInterrupted = false;
2116 bool ScrollView::OnWheelEvent(const WheelEvent& event)
2120 // Ignore this wheel event, if scrollview is insensitive.
2124 Vector2 targetScrollPosition = GetPropertyPosition();
2126 if(mRulerX->IsEnabled() && !mRulerY->IsEnabled())
2128 // If only the ruler in the X axis is enabled, scroll in the X axis.
2129 if(mRulerX->GetType() == Ruler::Free)
2131 // Free panning mode
2132 targetScrollPosition.x += event.z * mWheelScrollDistanceStep.x;
2133 ClampPosition(targetScrollPosition);
2134 ScrollTo(-targetScrollPosition);
2136 else if(!mScrolling)
2138 // Snap mode, only respond to the event when the previous snap animation is finished.
2139 ScrollTo(GetCurrentPage() - event.z);
2144 // If the ruler in the Y axis is enabled, scroll in the Y axis.
2145 if(mRulerY->GetType() == Ruler::Free)
2147 // Free panning mode
2148 targetScrollPosition.y += event.z * mWheelScrollDistanceStep.y;
2149 ClampPosition(targetScrollPosition);
2150 ScrollTo(-targetScrollPosition);
2152 else if(!mScrolling)
2154 // Snap mode, only respond to the event when the previous snap animation is finished.
2155 ScrollTo(GetCurrentPage() - event.z * mRulerX->GetTotalPages());
2162 void ScrollView::ResetScrolling()
2164 Actor self = Self();
2165 self.GetProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get(mScrollPostPosition);
2166 mScrollPrePosition = mScrollPostPosition;
2167 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPostPosition.x, mScrollPostPosition.y );
2168 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPostPosition);
2171 void ScrollView::UpdateLocalScrollProperties()
2173 Actor self = Self();
2174 self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get(mScrollPrePosition);
2175 self.GetProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get(mScrollPostPosition);
2178 // private functions
2180 void ScrollView::PreAnimatedScrollSetup()
2182 // SCROLL_PRE_POSITION is our unclamped property with wrapping
2183 // SCROLL_POSITION is our final scroll position after clamping
2185 Actor self = Self();
2187 Vector2 deltaPosition(mScrollPostPosition);
2188 WrapPosition(mScrollPostPosition);
2189 mDomainOffset += deltaPosition - mScrollPostPosition;
2190 Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset);
2192 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2194 // already performing animation on internal x position
2195 StopAnimation(mInternalXAnimation);
2198 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2200 // already performing animation on internal y position
2201 StopAnimation(mInternalYAnimation);
2204 mScrollStateFlags = 0;
2206 // Update Actor position with this wrapped value.
2209 void ScrollView::FinaliseAnimatedScroll()
2211 // TODO - common animation finishing code in here
2214 void ScrollView::AnimateInternalXTo( float position, float duration, AlphaFunction alpha )
2216 StopAnimation(mInternalXAnimation);
2218 if( duration > Math::MACHINE_EPSILON_10 )
2220 Actor self = Self();
2221 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get<Vector2>().x, position );
2222 mInternalXAnimation = Animation::New(duration);
2223 DALI_LOG_SCROLL_STATE("[0x%X], mInternalXAnimation[0x%X]", this, mInternalXAnimation.GetObjectPtr() );
2224 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2225 mInternalXAnimation.AnimateTo( Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position, alpha, TimePeriod(duration));
2226 mInternalXAnimation.Play();
2228 // erase current state flags
2229 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2230 // add internal animation state flag
2231 mScrollStateFlags |= AnimatingInternalX;
2235 void ScrollView::AnimateInternalYTo( float position, float duration, AlphaFunction alpha )
2237 StopAnimation(mInternalYAnimation);
2239 if( duration > Math::MACHINE_EPSILON_10 )
2241 Actor self = Self();
2242 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get<Vector2>().y, position );
2243 mInternalYAnimation = Animation::New(duration);
2244 DALI_LOG_SCROLL_STATE("[0x%X], mInternalYAnimation[0x%X]", this, mInternalYAnimation.GetObjectPtr() );
2245 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
2246 mInternalYAnimation.AnimateTo( Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position, alpha, TimePeriod(duration));
2247 mInternalYAnimation.Play();
2249 // erase current state flags
2250 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2251 // add internal animation state flag
2252 mScrollStateFlags |= AnimatingInternalY;
2256 void ScrollView::OnScrollAnimationFinished( Animation& source )
2258 // Guard against destruction during signal emission
2259 // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished()
2260 Toolkit::ScrollView handle( GetOwner() );
2262 bool scrollingFinished = false;
2264 // update our local scroll positions
2265 UpdateLocalScrollProperties();
2267 if( source == mInternalXAnimation )
2269 DALI_LOG_SCROLL_STATE("[0x%X] mInternalXAnimation[0x%X], expected[%.2f], actual[%.2f], post[%.2f]", this, mInternalXAnimation.GetObjectPtr(), mScrollTargetPosition.x, Self().GetProperty(SCROLL_PRE_POSITION).Get<Vector2>().x, mScrollPostPosition.x );
2271 if( !(mScrollStateFlags & AnimatingInternalY) )
2273 scrollingFinished = true;
2275 mInternalXAnimation.Reset();
2276 // wrap pre scroll x position and set it
2279 const RulerDomain rulerDomain = mRulerX->GetDomain();
2280 mScrollPrePosition.x = -WrapInDomain(-mScrollPrePosition.x, rulerDomain.min, rulerDomain.max);
2281 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
2282 handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
2284 SnapInternalXTo(mScrollPostPosition.x);
2287 if( source == mInternalYAnimation )
2289 DALI_LOG_SCROLL_STATE("[0x%X] mInternalYAnimation[0x%X], expected[%.2f], actual[%.2f], post[%.2f]", this, mInternalYAnimation.GetObjectPtr(), mScrollTargetPosition.y, Self().GetProperty(SCROLL_PRE_POSITION).Get<Vector2>().y, mScrollPostPosition.y );
2291 if( !(mScrollStateFlags & AnimatingInternalX) )
2293 scrollingFinished = true;
2295 mInternalYAnimation.Reset();
2298 // wrap pre scroll y position and set it
2299 const RulerDomain rulerDomain = mRulerY->GetDomain();
2300 mScrollPrePosition.y = -WrapInDomain(-mScrollPrePosition.y, rulerDomain.min, rulerDomain.max);
2301 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
2302 handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
2304 SnapInternalYTo(mScrollPostPosition.y);
2307 DALI_LOG_SCROLL_STATE("[0x%X] scrollingFinished[%d] Animation[0x%X]", this, scrollingFinished, source.GetObjectPtr());
2309 if(scrollingFinished)
2311 HandleSnapAnimationFinished();
2315 void ScrollView::OnSnapInternalPositionFinished( Animation& source )
2317 Actor self = Self();
2318 UpdateLocalScrollProperties();
2319 if( source == mInternalXAnimation )
2321 DALI_LOG_SCROLL_STATE("[0x%X] Finished X PostPosition Animation", this );
2323 // clear internal x animation flags
2324 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2325 mInternalXAnimation.Reset();
2326 WrapPosition(mScrollPrePosition);
2328 if( source == mInternalYAnimation )
2330 DALI_LOG_SCROLL_STATE("[0x%X] Finished Y PostPosition Animation", this );
2332 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2333 mInternalYAnimation.Reset();
2334 WrapPosition(mScrollPrePosition);
2338 void ScrollView::SnapInternalXTo(float position)
2340 Actor self = Self();
2342 StopAnimation(mInternalXAnimation);
2344 // erase current state flags
2345 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
2347 // if internal x not equal to inputed parameter, animate it
2348 float duration = std::min(fabsf((position - mScrollPrePosition.x) / mMaxOvershoot.x) * mSnapOvershootDuration, mSnapOvershootDuration);
2349 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration );
2350 if( duration > Math::MACHINE_EPSILON_1 )
2352 DALI_LOG_SCROLL_STATE("[0x%X] Starting X Snap Animation to[%.2f]", this, position );
2354 mInternalXAnimation = Animation::New(duration);
2355 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2356 mInternalXAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position);
2357 mInternalXAnimation.Play();
2359 // add internal animation state flag
2360 mScrollStateFlags |= SnappingInternalX;
2364 void ScrollView::SnapInternalYTo(float position)
2366 Actor self = Self();
2368 StopAnimation(mInternalYAnimation);
2370 // erase current state flags
2371 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
2373 // if internal y not equal to inputed parameter, animate it
2374 float duration = std::min(fabsf((position - mScrollPrePosition.y) / mMaxOvershoot.y) * mSnapOvershootDuration, mSnapOvershootDuration);
2375 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration );
2376 if( duration > Math::MACHINE_EPSILON_1 )
2378 DALI_LOG_SCROLL_STATE("[0x%X] Starting Y Snap Animation to[%.2f]", this, position );
2380 mInternalYAnimation = Animation::New(duration);
2381 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
2382 mInternalYAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position);
2383 mInternalYAnimation.Play();
2385 // add internal animation state flag
2386 mScrollStateFlags |= SnappingInternalY;
2390 void ScrollView::GestureStarted()
2392 // we handle the first gesture.
2393 // if we're currently doing a gesture and receive another
2394 // we continue and combine the effects of the gesture instead of reseting.
2395 if(mGestureStackDepth++==0)
2397 Actor self = Self();
2398 StopTouchDownTimer();
2400 mPanDelta = Vector2::ZERO;
2401 mLastVelocity = Vector2::ZERO;
2404 mLockAxis = LockPossible;
2407 if( mScrollStateFlags & SCROLL_X_STATE_MASK )
2409 StopAnimation(mInternalXAnimation);
2411 if( mScrollStateFlags & SCROLL_Y_STATE_MASK )
2413 StopAnimation(mInternalYAnimation);
2415 mScrollStateFlags = 0;
2417 if(mScrolling) // are we interrupting a current scroll?
2419 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
2421 // send negative scroll position since scroll internal scroll position works as an offset for actors,
2422 // give applications the position within the domain from the scroll view's anchor position
2423 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 5 [%.2f, %.2f]", this, -mScrollPostPosition.x, -mScrollPostPosition.y);
2424 mScrollCompletedSignal.Emit( -mScrollPostPosition );
2429 void ScrollView::GestureContinuing(const Vector2& panDelta)
2431 mPanDelta.x+= panDelta.x;
2432 mPanDelta.y+= panDelta.y;
2434 // Save the velocity, there is a bug in PanGesture
2435 // Whereby the Gesture::Finished's velocity is either:
2436 // NaN (due to time delta of zero between the last two events)
2437 // or 0 (due to position being the same between the last two events)
2439 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
2440 // appears mostly horizontal or mostly vertical respectively.
2443 mLockAxis = GetLockAxis(mPanDelta, mLockAxis, mAxisAutoLockGradient);
2444 } // end if mAxisAutoLock
2447 // TODO: Upgrade to use a more powerful gesture detector (one that supports multiple touches on pan - so works as pan and flick gesture)
2448 // BUG: Gesture::Finished doesn't always return velocity on release (due to
2449 // timeDelta between last two events being 0 sometimes, or posiiton being the same)
2450 void ScrollView::OnPan( const PanGesture& gesture )
2452 // Guard against destruction during signal emission
2453 // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx()
2454 Actor self( Self() );
2458 DALI_LOG_SCROLL_STATE("[0x%X] Pan Ignored, Insensitive", this);
2460 // If another callback on the same original signal disables sensitivity,
2461 // this callback will still be called, so we must suppress it.
2465 // translate Gesture input to get useful data...
2466 switch(gesture.state)
2468 case Gesture::Started:
2470 DALI_LOG_SCROLL_STATE("[0x%X] Pan Started", this);
2471 mPanStartPosition = gesture.position - gesture.displacement;
2472 UpdateLocalScrollProperties();
2475 self.SetProperty( Toolkit::ScrollView::Property::PANNING, true );
2476 self.SetProperty( Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(gesture.position.x, gesture.position.y, 0.0f) );
2478 UpdateMainInternalConstraint();
2482 case Gesture::Continuing:
2486 DALI_LOG_SCROLL_STATE("[0x%X] Pan Continuing", this);
2487 GestureContinuing(gesture.screenDisplacement);
2491 // If we do not think we are panning, then we should not do anything here
2497 case Gesture::Finished:
2498 case Gesture::Cancelled:
2502 DALI_LOG_SCROLL_STATE("[0x%X] Pan %s", this, ( ( gesture.state == Gesture::Finished ) ? "Finished" : "Cancelled" ) );
2504 UpdateLocalScrollProperties();
2505 mLastVelocity = gesture.velocity;
2507 self.SetProperty( Toolkit::ScrollView::Property::PANNING, false );
2509 if( mScrollMainInternalPrePositionConstraint )
2511 mScrollMainInternalPrePositionConstraint.Remove();
2516 // If we do not think we are panning, then we should not do anything here
2522 case Gesture::Possible:
2523 case Gesture::Clear:
2525 // Nothing to do, not needed.
2529 } // end switch(gesture.state)
2531 OnGestureEx(gesture.state);
2534 void ScrollView::OnGestureEx(Gesture::State state)
2536 // call necessary signals for application developer
2538 if(state == Gesture::Started)
2540 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2541 Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, true);
2543 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 2 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2544 mScrollStartedSignal.Emit( currentScrollPosition );
2546 else if( (state == Gesture::Finished) ||
2547 (state == Gesture::Cancelled) ) // Finished/default
2549 // when all the gestures have finished, we finish the transform.
2550 // so if a user decides to pan (1 gesture), and then pan+zoom (2 gestures)
2551 // then stop panning (back to 1 gesture), and then stop zooming (0 gestures).
2552 // this is the point we end, and perform necessary snapping.
2553 mGestureStackDepth--;
2554 if(mGestureStackDepth==0)
2556 // no flick if we have not exceeded min flick distance
2557 if( (fabsf(mPanDelta.x) < mMinFlickDistance.x)
2558 && (fabsf(mPanDelta.y) < mMinFlickDistance.y) )
2560 // reset flick velocity
2561 mLastVelocity = Vector2::ZERO;
2567 DALI_LOG_SCROLL_STATE("[0x%X] mGestureStackDepth[%d]", this, mGestureStackDepth);
2572 void ScrollView::FinishTransform()
2574 // at this stage internal x and x scroll position should have followed prescroll position exactly
2575 Actor self = Self();
2577 PreAnimatedScrollSetup();
2579 // convert pixels/millisecond to pixels per second
2580 bool animating = SnapWithVelocity(mLastVelocity * 1000.0f);
2584 // if not animating, then this pan has completed right now.
2585 SetScrollUpdateNotification(false);
2587 Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
2589 if( fabs(mScrollPrePosition.x - mScrollTargetPosition.x) > Math::MACHINE_EPSILON_10 )
2591 SnapInternalXTo(mScrollTargetPosition.x);
2593 if( fabs(mScrollPrePosition.y - mScrollTargetPosition.y) > Math::MACHINE_EPSILON_10 )
2595 SnapInternalYTo(mScrollTargetPosition.y);
2597 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2598 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 6 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2599 mScrollCompletedSignal.Emit( currentScrollPosition );
2603 Vector2 ScrollView::GetOvershoot(Vector2& position) const
2605 Vector3 size = Self().GetCurrentSize();
2608 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2609 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2611 if(mRulerX->IsEnabled() && rulerDomainX.enabled)
2613 const float left = rulerDomainX.min - position.x;
2614 const float right = size.width - rulerDomainX.max - position.x;
2621 overshoot.x = right;
2625 if(mRulerY->IsEnabled() && rulerDomainY.enabled)
2627 const float top = rulerDomainY.min - position.y;
2628 const float bottom = size.height - rulerDomainY.max - position.y;
2635 overshoot.y = bottom;
2642 bool ScrollView::OnAccessibilityPan(PanGesture gesture)
2644 // Keep track of whether this is an AccessibilityPan
2645 mInAccessibilityPan = true;
2647 mInAccessibilityPan = false;
2652 void ScrollView::ClampPosition(Vector2& position) const
2654 ClampState2D clamped;
2655 ClampPosition(position, clamped);
2658 void ScrollView::ClampPosition(Vector2& position, ClampState2D &clamped) const
2660 Vector3 size = Self().GetCurrentSize();
2662 position.x = -mRulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system.
2663 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.
2666 void ScrollView::WrapPosition(Vector2& position) const
2670 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2671 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2673 if(mRulerX->IsEnabled())
2675 position.x = -WrapInDomain(-position.x, rulerDomainX.min, rulerDomainX.max);
2678 if(mRulerY->IsEnabled())
2680 position.y = -WrapInDomain(-position.y, rulerDomainY.min, rulerDomainY.max);
2685 void ScrollView::UpdateMainInternalConstraint()
2687 // TODO: Only update the constraints which have changed, rather than remove all and add all again.
2688 // Requires a dali-core ApplyConstraintAt, or a ReplaceConstraint. The former is probably more flexible.
2689 Actor self = Self();
2690 PanGestureDetector detector( GetPanGestureDetector() );
2692 if(mScrollMainInternalPositionConstraint)
2694 mScrollMainInternalPositionConstraint.Remove();
2695 mScrollMainInternalDeltaConstraint.Remove();
2696 mScrollMainInternalFinalConstraint.Remove();
2697 mScrollMainInternalRelativeConstraint.Remove();
2698 mScrollMainInternalDomainConstraint.Remove();
2699 mScrollMainInternalPrePositionMaxConstraint.Remove();
2701 if( mScrollMainInternalPrePositionConstraint )
2703 mScrollMainInternalPrePositionConstraint.Remove();
2706 // TODO: It's probably better to use a local displacement value as this will give a displacement when scrolling just commences
2707 // but we need to make sure than the gesture system gives displacement since last frame (60Hz), not displacement since last touch event (90Hz).
2709 // 1. First calculate the pre-position (this is the scroll position if no clamping has taken place)
2710 Vector2 initialPanMask = Vector2(mRulerX->IsEnabled() ? 1.0f : 0.0f, mRulerY->IsEnabled() ? 1.0f : 0.0f);
2712 if( mLockAxis == LockVertical )
2714 initialPanMask.y = 0.0f;
2716 else if( mLockAxis == LockHorizontal )
2718 initialPanMask.x = 0.0f;
2723 mScrollMainInternalPrePositionConstraint = Constraint::New<Vector2>( self,
2724 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION,
2725 InternalPrePositionConstraint( mPanStartPosition,
2728 mAxisAutoLockGradient,
2733 mScrollMainInternalPrePositionConstraint.AddSource( Source( detector, PanGestureDetector::Property::LOCAL_POSITION ) );
2734 mScrollMainInternalPrePositionConstraint.AddSource( Source( detector, PanGestureDetector::Property::PANNING ) );
2735 mScrollMainInternalPrePositionConstraint.AddSource( Source( self, Actor::Property::SIZE ) );
2736 mScrollMainInternalPrePositionConstraint.Apply();
2739 // 2. Second calculate the clamped position (actual position)
2740 mScrollMainInternalPositionConstraint = Constraint::New<Vector2>( self,
2741 Toolkit::ScrollView::Property::SCROLL_POSITION,
2742 InternalPositionConstraint( mRulerX->GetDomain(),
2743 mRulerY->GetDomain(),
2745 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2746 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2747 mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2748 mScrollMainInternalPositionConstraint.AddSource( Source( self, Actor::Property::SIZE ) );
2749 mScrollMainInternalPositionConstraint.Apply();
2751 mScrollMainInternalDeltaConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_POSITION_DELTA, InternalPositionDeltaConstraint );
2752 mScrollMainInternalDeltaConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2753 mScrollMainInternalDeltaConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET ) );
2754 mScrollMainInternalDeltaConstraint.Apply();
2756 mScrollMainInternalFinalConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_FINAL,
2757 InternalFinalConstraint( FinalDefaultAlphaFunction,
2758 FinalDefaultAlphaFunction ) );
2759 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2760 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::OVERSHOOT_X ) );
2761 mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::OVERSHOOT_Y ) );
2762 mScrollMainInternalFinalConstraint.Apply();
2764 mScrollMainInternalRelativeConstraint = Constraint::New<Vector2>( self, Toolkit::Scrollable::Property::SCROLL_RELATIVE_POSITION, InternalRelativePositionConstraint );
2765 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2766 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2767 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2768 mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2769 mScrollMainInternalRelativeConstraint.Apply();
2771 mScrollMainInternalDomainConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE, InternalScrollDomainConstraint );
2772 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2773 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2774 mScrollMainInternalDomainConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2775 mScrollMainInternalDomainConstraint.Apply();
2777 mScrollMainInternalPrePositionMaxConstraint = Constraint::New<Vector2>( self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX, InternalPrePositionMaxConstraint );
2778 mScrollMainInternalPrePositionMaxConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2779 mScrollMainInternalPrePositionMaxConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2780 mScrollMainInternalPrePositionMaxConstraint.Apply();
2782 // When panning we want to make sure overshoot values are affected by pre position and post position
2783 SetOvershootConstraintsEnabled(!mWrapMode);
2786 void ScrollView::SetOvershootConstraintsEnabled(bool enabled)
2788 Actor self( Self() );
2789 // remove and reset, it may now be in wrong order with the main internal constraints
2790 if( mScrollMainInternalOvershootXConstraint )
2792 mScrollMainInternalOvershootXConstraint.Remove();
2793 mScrollMainInternalOvershootXConstraint.Reset();
2794 mScrollMainInternalOvershootYConstraint.Remove();
2795 mScrollMainInternalOvershootYConstraint.Reset();
2799 mScrollMainInternalOvershootXConstraint= Constraint::New<float>( self, Toolkit::ScrollView::Property::OVERSHOOT_X, OvershootXConstraint(mMaxOvershoot.x) );
2800 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2801 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2802 mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL ) );
2803 mScrollMainInternalOvershootXConstraint.Apply();
2805 mScrollMainInternalOvershootYConstraint = Constraint::New<float>( self, Toolkit::ScrollView::Property::OVERSHOOT_Y, OvershootYConstraint(mMaxOvershoot.y) );
2806 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) );
2807 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2808 mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
2809 mScrollMainInternalOvershootYConstraint.Apply();
2813 self.SetProperty(Toolkit::ScrollView::Property::OVERSHOOT_X, 0.0f);
2814 self.SetProperty(Toolkit::ScrollView::Property::OVERSHOOT_Y, 0.0f);
2818 void ScrollView::SetInternalConstraints()
2820 // Internal constraints (applied to target ScrollBase Actor itself) /////////
2821 UpdateMainInternalConstraint();
2823 // User definable constraints to apply to all child actors //////////////////
2824 Actor self = Self();
2826 // Apply some default constraints to ScrollView & its bound actors
2827 // Movement + Wrap function
2829 Constraint constraint;
2831 // MoveActor (scrolling)
2832 constraint = Constraint::New<Vector3>( self, Actor::Property::POSITION, MoveActorConstraint );
2833 constraint.AddSource( Source( self, Toolkit::ScrollView::Property::SCROLL_POSITION ) );
2834 constraint.SetRemoveAction(Constraint::Discard);
2835 ApplyConstraintToBoundActors(constraint);
2837 // WrapActor (wrap functionality)
2838 constraint = Constraint::New<Vector3>( self, Actor::Property::POSITION, WrapActorConstraint );
2839 constraint.AddSource( LocalSource( Actor::Property::SCALE ) );
2840 constraint.AddSource( LocalSource( Actor::Property::ANCHOR_POINT ) );
2841 constraint.AddSource( LocalSource( Actor::Property::SIZE ) );
2842 constraint.AddSource( Source( self, Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
2843 constraint.AddSource( Source( self, Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
2844 constraint.AddSource( Source( self, Toolkit::ScrollView::Property::WRAP ) );
2845 constraint.SetRemoveAction(Constraint::Discard);
2846 ApplyConstraintToBoundActors(constraint);
2849 void ScrollView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
2851 Toolkit::ScrollView scrollView = Toolkit::ScrollView::DownCast( Dali::BaseHandle( object ) );
2855 ScrollView& scrollViewImpl( GetImpl( scrollView ) );
2858 case Toolkit::ScrollView::Property::WRAP_ENABLED:
2860 scrollViewImpl.SetWrapMode( value.Get<bool>() );
2863 case Toolkit::ScrollView::Property::PANNING_ENABLED:
2865 scrollViewImpl.SetScrollSensitive( value.Get<bool>() );
2868 case Toolkit::ScrollView::Property::AXIS_AUTO_LOCK_ENABLED:
2870 scrollViewImpl.SetAxisAutoLock( value.Get<bool>() );
2873 case Toolkit::ScrollView::Property::WHEEL_SCROLL_DISTANCE_STEP:
2875 scrollViewImpl.SetWheelScrollDistanceStep( value.Get<Vector2>() );
2882 Property::Value ScrollView::GetProperty( BaseObject* object, Property::Index index )
2884 Property::Value value;
2886 Toolkit::ScrollView scrollView = Toolkit::ScrollView::DownCast( Dali::BaseHandle( object ) );
2890 ScrollView& scrollViewImpl( GetImpl( scrollView ) );
2893 case Toolkit::ScrollView::Property::WRAP_ENABLED:
2895 value = scrollViewImpl.GetWrapMode();
2898 case Toolkit::ScrollView::Property::PANNING_ENABLED:
2900 value = scrollViewImpl.GetScrollSensitive();
2903 case Toolkit::ScrollView::Property::AXIS_AUTO_LOCK_ENABLED:
2905 value = scrollViewImpl.GetAxisAutoLock();
2908 case Toolkit::ScrollView::Property::WHEEL_SCROLL_DISTANCE_STEP:
2910 value = scrollViewImpl.GetWheelScrollDistanceStep();
2919 } // namespace Internal
2921 } // namespace Toolkit