2 * Copyright (c) 2021 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 <dali/devel-api/actors/actor-devel.h>
23 #include <dali/devel-api/common/stage.h>
24 #include <dali/devel-api/events/pan-gesture-devel.h>
25 #include <dali/devel-api/object/property-helper-devel.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/animation/constraints.h>
28 #include <dali/public-api/events/touch-event.h>
29 #include <dali/public-api/events/wheel-event.h>
30 #include <dali/public-api/object/property-map.h>
31 #include <dali/public-api/object/type-registry-helper.h>
32 #include <dali/public-api/object/type-registry.h>
33 #include <cstring> // for strcmp
36 #include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
37 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
38 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-effect-impl.h>
39 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl-property-handler.h>
40 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-mode.h>
41 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view-constraints.h>
42 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view.h>
44 //#define ENABLED_SCROLL_STATE_LOGGING
46 #ifdef ENABLED_SCROLL_STATE_LOGGING
47 #define DALI_LOG_SCROLL_STATE(format, ...) Dali::Integration::Log::LogMessageWithFunctionLine(Dali::Integration::Log::DebugInfo, "%s:%d " format "\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
49 #define DALI_LOG_SCROLL_STATE(format, ...)
52 // TODO: Change to two class system:
53 // 1. DraggableActor (is an actor which can be dragged anywhere, can be set to range using the ruler)
54 // 2. ScrollView (contains a draggable actor that can a) be dragged in the negative X, and Y domain, b) has a hitArea for touches)
55 // TODO: external components (page and status overlays).
57 // TODO: upgrade Vector2/3 to support returning Unit vectors, normals, & cross product (dot product is already provided)
63 constexpr float DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f); ///< Default Drag-Release animation time.
64 constexpr float DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f); ///< Default Drag-Flick animation time.
65 constexpr float DEFAULT_SNAP_OVERSHOOT_DURATION(0.5f); ///< Default Overshoot snapping animation time.
66 constexpr float DEFAULT_MAX_OVERSHOOT(100.0f); ///< Default maximum allowed overshoot in pixels
68 constexpr float DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f); ///< Default Axis-AutoLock gradient threshold. default is 0.36:1 (20 degrees)
69 constexpr float DEFAULT_FRICTION_COEFFICIENT(1.0f); ///< Default Friction Co-efficient. (in stage diagonals per second)
70 constexpr float DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f); ///< Default Flick speed coefficient (multiples input touch velocity)
71 constexpr float DEFAULT_MAX_FLICK_SPEED(3.0f); ///< Default Maximum flick speed. (in stage diagonals per second)
73 constexpr Dali::Vector2 DEFAULT_MIN_FLICK_DISTANCE(30.0f, 30.0f); ///< minimum distance for pan before flick allowed
74 constexpr float DEFAULT_MIN_FLICK_SPEED_THRESHOLD(500.0f); ///< Minimum pan speed required for flick in pixels/s
76 constexpr float FREE_FLICK_SPEED_THRESHOLD = 200.0f; ///< Free-Flick threshold in pixels/ms
77 constexpr float AUTOLOCK_AXIS_MINIMUM_DISTANCE2 = 100.0f; ///< Auto-lock axis after minimum distance squared.
78 constexpr float FLICK_ORTHO_ANGLE_RANGE = 75.0f; ///< degrees. (if >45, then supports diagonal flicking)
80 constexpr Dali::Vector2 DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION(0.17f, 0.1f); ///< The step of horizontal scroll distance in the proportion of stage size for each wheel event received.
82 constexpr unsigned long MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET(150u);
83 constexpr float TOUCH_DOWN_TIMER_INTERVAL = 100.0f;
84 constexpr float DEFAULT_SCROLL_UPDATE_DISTANCE(30.0f); ///< Default distance to travel in pixels for scroll update signal
86 const std::string INTERNAL_MAX_POSITION_PROPERTY_NAME("internalMaxPosition");
88 // Helpers ////////////////////////////////////////////////////////////////////////////////////////
91 * Find the vector (distance) from (a) to (b)
92 * in domain (start) to (end)
93 * (\ / start) (\ / end)
96 * @note assumes both (a) and (b) are already with the domain
99 * @param[in] a the current point
100 * @param[in] b the target point
101 * @param[in] start the start of the domain
102 * @param[in] end the end of the domain
103 * @param[in] bias whether to only take the right direction or the left direction,
104 * or the shortest direction.
105 * @return the shortest direction and distance
107 float VectorInDomain(float a, float b, float start, float end, Dali::Toolkit::DirectionBias bias)
109 if(bias == Dali::Toolkit::DIRECTION_BIAS_NONE)
111 return Dali::ShortestDistanceInDomain(a, b, start, end);
114 float size = end - start;
120 if(bias == Dali::Toolkit::DIRECTION_BIAS_RIGHT) // going right, take the vector.
126 float aRight = a + size;
133 if(bias == Dali::Toolkit::DIRECTION_BIAS_LEFT) // going left, take the vector.
139 float aLeft = a - size;
146 * Returns the position of the anchor within actor
148 * @param actor The Actor
149 * @param anchor The Anchor point of interest.
150 * @return The position of the Anchor
152 Dali::Vector3 GetPositionOfAnchor(Dali::Actor& actor, const Dali::Vector3& anchor)
154 Dali::Vector3 childPosition = actor.GetCurrentProperty<Dali::Vector3>(Dali::Actor::Property::POSITION);
155 Dali::Vector3 childAnchor = -actor.GetCurrentProperty<Dali::Vector3>(Dali::Actor::Property::ANCHOR_POINT) + anchor;
156 Dali::Vector3 childSize = actor.GetCurrentProperty<Dali::Vector3>(Dali::Actor::Property::SIZE);
158 return childPosition + childAnchor * childSize;
162 * Returns the closest actor to the given position
163 * @param[in] actor The scrollview actor
164 * @param[in] internalActor The internal actor (to ignore)
165 * @param[in] position The given position
166 * @param[in] dirX Direction to search in
167 * @param[in] dirY Direction to search in
168 * @param[in] dirZ Direction to search in
169 * @return the closest child actor
171 using FindDirection = Dali::Toolkit::Internal::ScrollView::FindDirection;
173 Actor FindClosestActorToPosition(
174 CustomActor actor, Actor internalActor, const Vector3& position, FindDirection dirX, FindDirection dirY, FindDirection dirZ)
177 float closestDistance2 = 0.0f;
178 Vector3 actualPosition = position;
180 unsigned int numChildren = actor.GetChildCount();
182 for(unsigned int i = 0; i < numChildren; ++i)
184 Actor child = actor.GetChildAt(i);
186 if(internalActor == child) // ignore internal actor.
191 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
193 Vector3 delta = childPosition - actualPosition;
195 // X-axis checking (only find Actors to the [dirX] of actualPosition)
196 if(dirX > FindDirection::All) // != All,None
198 FindDirection deltaH = delta.x > 0 ? FindDirection::Right : FindDirection::Left;
205 // Y-axis checking (only find Actors to the [dirY] of actualPosition)
206 if(dirY > FindDirection::All) // != All,None
208 FindDirection deltaV = delta.y > 0 ? FindDirection::Down : FindDirection::Up;
215 // Z-axis checking (only find Actors to the [dirZ] of actualPosition)
216 if(dirZ > FindDirection::All) // != All,None
218 FindDirection deltaV = delta.y > 0 ? FindDirection::In : FindDirection::Out;
225 // compare child to closest child in terms of distance.
226 float distance2 = 0.0f;
228 // distance2 = the Square of the relevant dimensions of delta
229 if(dirX != FindDirection::None)
231 distance2 += delta.x * delta.x;
234 if(dirY != FindDirection::None)
236 distance2 += delta.y * delta.y;
239 if(dirZ != FindDirection::None)
241 distance2 += delta.z * delta.z;
244 if(closestChild) // Next time.
246 if(distance2 < closestDistance2)
248 closestChild = child;
249 closestDistance2 = distance2;
254 closestChild = child;
255 closestDistance2 = distance2;
262 // AlphaFunctions /////////////////////////////////////////////////////////////////////////////////
265 * ConstantDecelerationAlphaFunction
266 * Newtoninan distance for constant deceleration
267 * v = 1 - t, s = t - 1/2 t^2
268 * when t = 0, s = 0.0 (min distance)
269 * when t = 1, s = 0.5 (max distance)
270 * progress = s / (max-min) = 2t - t^2
272 * @param[in] offset The input progress
273 * @return The output progress
275 float ConstantDecelerationAlphaFunction(float progress)
277 return progress * 2.0f - progress * progress;
282 * @param[in] size The size to clamp to
283 * @param[in] rulerX The horizontal ruler
284 * @param[in] rulerY The vertical ruler
285 * @param[in,out] position The position to clamp
286 * @param[out] clamped the clamped state
288 void ClampPosition(const Vector3& size, Dali::Toolkit::RulerPtr rulerX, Dali::Toolkit::RulerPtr rulerY, Vector2& position, Dali::Toolkit::ClampState2D& clamped)
290 position.x = -rulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system.
291 position.y = -rulerY->Clamp(-position.y, size.height, 1.0f, clamped.y); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
295 * TODO: In situations where axes are different (X snap, Y free)
296 * Each axis should really have their own independent animation (time and equation)
297 * Consider, X axis snapping to nearest grid point (EaseOut over fixed time)
298 * Consider, Y axis simulating physics to arrive at a point (Physics equation over variable time)
299 * Currently, the axes have been split however, they both use the same EaseOut equation.
301 * @param[in] scrollView The main scrollview
302 * @param[in] rulerX The X ruler
303 * @param[in] rulerY The Y ruler
304 * @param[in] lockAxis Which axis (if any) is locked.
305 * @param[in] velocity Current pan velocity
306 * @param[in] maxOvershoot Maximum overshoot
307 * @param[in] inAcessibilityPan True if we are currently panning with accessibility
308 * @param[out] positionSnap The target position of snap animation
309 * @param[out] positionDuration The duration of the snap animation
310 * @param[out] alphaFunction The snap animation alpha function
311 * @param[out] isFlick if we are flicking or not
312 * @param[out] isFreeFlick if we are free flicking or not
314 void SnapWithVelocity(
315 Dali::Toolkit::Internal::ScrollView& scrollView,
316 Dali::Toolkit::RulerPtr rulerX,
317 Dali::Toolkit::RulerPtr rulerY,
318 Dali::Toolkit::Internal::ScrollView::LockAxis lockAxis,
320 Vector2 maxOvershoot,
321 Vector2& positionSnap,
322 Vector2& positionDuration,
323 AlphaFunction& alphaFunction,
324 bool inAccessibilityPan,
328 // Animator takes over now, touches are assumed not to interfere.
329 // And if touches do interfere, then we'll stop animation, update PrePosition
330 // to current mScroll's properties, and then resume.
331 // Note: For Flicking this may work a bit different...
333 float angle = atan2(velocity.y, velocity.x);
334 float speed2 = velocity.LengthSquared();
337 FindDirection horizontal = FindDirection::None;
338 FindDirection vertical = FindDirection::None;
340 using LockAxis = Dali::Toolkit::Internal::ScrollView::LockAxis;
342 // orthoAngleRange = Angle tolerance within the Exact N,E,S,W direction
343 // that will be accepted as a general N,E,S,W flick direction.
345 const float orthoAngleRange = FLICK_ORTHO_ANGLE_RANGE * M_PI / 180.0f;
346 const float flickSpeedThreshold2 = scrollView.GetMinimumSpeedForFlick() * scrollView.GetMinimumSpeedForFlick();
348 // Flick logic X Axis
350 if(rulerX->IsEnabled() && lockAxis != LockAxis::LockHorizontal)
352 horizontal = FindDirection::All;
354 if(speed2 > flickSpeedThreshold2 || // exceeds flick threshold
355 inAccessibilityPan) // With AccessibilityPan its easier to move between snap positions
357 if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East
359 biasX = 0.0f, horizontal = FindDirection::Left;
361 // This guards against an error where no movement occurs, due to the flick finishing
362 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
363 positionSnap.x += 1.0f;
365 else if((angle >= M_PI - orthoAngleRange) || (angle < -M_PI + orthoAngleRange)) // Swiping West
367 biasX = 1.0f, horizontal = FindDirection::Right;
369 // This guards against an error where no movement occurs, due to the flick finishing
370 // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
371 positionSnap.x -= 1.0f;
376 // Flick logic Y Axis
378 if(rulerY->IsEnabled() && lockAxis != LockAxis::LockVertical)
380 vertical = FindDirection::All;
382 if(speed2 > flickSpeedThreshold2 || // exceeds flick threshold
383 inAccessibilityPan) // With AccessibilityPan its easier to move between snap positions
385 if((angle >= M_PI_2 - orthoAngleRange) && (angle < M_PI_2 + orthoAngleRange)) // Swiping South
387 biasY = 0.0f, vertical = FindDirection::Up;
389 else if((angle >= -M_PI_2 - orthoAngleRange) && (angle < -M_PI_2 + orthoAngleRange)) // Swiping North
391 biasY = 1.0f, vertical = FindDirection::Down;
396 // isFlick: Whether this gesture is a flick or not.
397 isFlick = (horizontal != FindDirection::All || vertical != FindDirection::All);
398 // isFreeFlick: Whether this gesture is a flick under free panning criteria.
399 isFreeFlick = velocity.LengthSquared() > (FREE_FLICK_SPEED_THRESHOLD * FREE_FLICK_SPEED_THRESHOLD);
401 if(isFlick || isFreeFlick)
403 positionDuration = Vector2::ONE * scrollView.GetScrollFlickDuration();
404 alphaFunction = scrollView.GetScrollFlickAlphaFunction();
407 // Calculate next positionSnap ////////////////////////////////////////////////////////////
409 if(scrollView.GetActorAutoSnap())
411 Vector3 size = scrollView.Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE);
413 Actor child = scrollView.FindClosestActorToPosition(Vector3(size.width * 0.5f, size.height * 0.5f, 0.0f), horizontal, vertical);
415 if(!child && isFlick)
417 // If we conducted a direction limited search and found no actor, then just snap to the closest actor.
418 child = scrollView.FindClosestActorToPosition(Vector3(size.width * 0.5f, size.height * 0.5f, 0.0f));
423 Vector2 position = scrollView.Self().GetCurrentProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_POSITION);
425 // Get center-point of the Actor.
426 Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
428 if(rulerX->IsEnabled())
430 positionSnap.x = position.x - childPosition.x + size.width * 0.5f;
432 if(rulerY->IsEnabled())
434 positionSnap.y = position.y - childPosition.y + size.height * 0.5f;
439 Vector2 startPosition = positionSnap;
440 positionSnap.x = -rulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system.
441 positionSnap.y = -rulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
443 Dali::Toolkit::ClampState2D clamped;
444 Vector3 size = scrollView.Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE);
445 Vector2 clampDelta(Vector2::ZERO);
446 ClampPosition(size, rulerX, rulerY, positionSnap, clamped);
448 if((rulerX->GetType() == Dali::Toolkit::Ruler::FREE || rulerY->GetType() == Dali::Toolkit::Ruler::FREE) &&
449 isFreeFlick && !scrollView.GetActorAutoSnap())
451 // Calculate target position based on velocity of flick.
453 // a = Deceleration (Set to diagonal stage length * friction coefficient)
454 // u = Initial Velocity (Flick velocity)
455 // v = 0 (Final Velocity)
456 // t = Time (Velocity / Deceleration)
457 Vector2 stageSize = Stage::GetCurrent().GetSize();
458 float stageLength = Vector3(stageSize.x, stageSize.y, 0.0f).Length();
459 float a = (stageLength * scrollView.GetFrictionCoefficient());
460 Vector3 u = Vector3(velocity.x, velocity.y, 0.0f) * scrollView.GetFlickSpeedCoefficient();
461 float speed = u.Length();
464 // TODO: Change this to a decay function. (faster you flick, the slower it should be)
465 speed = std::min(speed, stageLength * scrollView.GetMaxFlickSpeed());
467 alphaFunction = ConstantDecelerationAlphaFunction;
471 if(rulerX->IsEnabled() && rulerX->GetType() == Dali::Toolkit::Ruler::FREE)
473 positionSnap.x += t * u.x * 0.5f;
476 if(rulerY->IsEnabled() && rulerY->GetType() == Dali::Toolkit::Ruler::FREE)
478 positionSnap.y += t * u.y * 0.5f;
481 clampDelta = positionSnap;
482 ClampPosition(size, rulerX, rulerY, positionSnap, clamped);
484 if((positionSnap - startPosition).LengthSquared() > Math::MACHINE_EPSILON_0)
486 clampDelta -= positionSnap;
487 clampDelta.x = clampDelta.x > 0.0f ? std::min(clampDelta.x, maxOvershoot.x) : std::max(clampDelta.x, -maxOvershoot.x);
488 clampDelta.y = clampDelta.y > 0.0f ? std::min(clampDelta.y, maxOvershoot.y) : std::max(clampDelta.y, -maxOvershoot.y);
492 clampDelta = Vector2::ZERO;
495 // If Axis is Free and has velocity, then calculate time taken
496 // to reach target based on velocity in axis.
497 if(rulerX->IsEnabled() && rulerX->GetType() == Dali::Toolkit::Ruler::FREE)
499 float deltaX = fabsf(startPosition.x - positionSnap.x);
501 if(fabsf(u.x) > Math::MACHINE_EPSILON_1)
503 positionDuration.x = fabsf(deltaX / u.x);
507 positionDuration.x = 0;
511 if(rulerY->IsEnabled() && rulerY->GetType() == Dali::Toolkit::Ruler::FREE)
513 float deltaY = fabsf(startPosition.y - positionSnap.y);
515 if(fabsf(u.y) > Math::MACHINE_EPSILON_1)
517 positionDuration.y = fabsf(deltaY / u.y);
521 positionDuration.y = 0;
526 if(scrollView.IsOvershootEnabled())
528 // Scroll to the end of the overshoot only when overshoot is enabled.
529 positionSnap += clampDelta;
533 } // unnamed namespace
545 return Toolkit::ScrollView::New();
548 // Setup properties, signals and actions using the type-registry.
549 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::ScrollView, Toolkit::Scrollable, Create)
551 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollView, "wrapEnabled", BOOLEAN, WRAP_ENABLED)
552 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollView, "panningEnabled", BOOLEAN, PANNING_ENABLED)
553 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollView, "axisAutoLockEnabled", BOOLEAN, AXIS_AUTO_LOCK_ENABLED)
554 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollView, "wheelScrollDistanceStep", VECTOR2, WHEEL_SCROLL_DISTANCE_STEP)
555 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollView, "scrollMode", MAP, SCROLL_MODE)
557 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "scrollPosition", VECTOR2, SCROLL_POSITION)
558 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "scrollPrePosition", VECTOR2, SCROLL_PRE_POSITION)
559 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, ScrollView, "scrollPrePositionX", SCROLL_PRE_POSITION_X, SCROLL_PRE_POSITION, 0)
560 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, ScrollView, "scrollPrePositionY", SCROLL_PRE_POSITION_Y, SCROLL_PRE_POSITION, 1)
561 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "scrollPrePositionMax", VECTOR2, SCROLL_PRE_POSITION_MAX)
562 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, ScrollView, "scrollPrePositionMaxX", SCROLL_PRE_POSITION_MAX_X, SCROLL_PRE_POSITION_MAX, 0)
563 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, ScrollView, "scrollPrePositionMaxY", SCROLL_PRE_POSITION_MAX_Y, SCROLL_PRE_POSITION_MAX, 1)
564 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "overshootX", FLOAT, OVERSHOOT_X)
565 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "overshootY", FLOAT, OVERSHOOT_Y)
566 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "scrollFinal", VECTOR2, SCROLL_FINAL)
567 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, ScrollView, "scrollFinalX", SCROLL_FINAL_X, SCROLL_FINAL, 0)
568 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, ScrollView, "scrollFinalY", SCROLL_FINAL_Y, SCROLL_FINAL, 1)
569 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "wrap", BOOLEAN, WRAP)
570 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "panning", BOOLEAN, PANNING)
571 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "scrolling", BOOLEAN, SCROLLING)
572 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "scrollDomainSize", VECTOR2, SCROLL_DOMAIN_SIZE)
573 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, ScrollView, "scrollDomainSizeX", SCROLL_DOMAIN_SIZE_X, SCROLL_DOMAIN_SIZE, 0)
574 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, ScrollView, "scrollDomainSizeY", SCROLL_DOMAIN_SIZE_Y, SCROLL_DOMAIN_SIZE, 1)
575 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "scrollDomainOffset", VECTOR2, SCROLL_DOMAIN_OFFSET)
576 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "scrollPositionDelta", VECTOR2, SCROLL_POSITION_DELTA)
577 DALI_ANIMATABLE_PROPERTY_REGISTRATION(Toolkit, ScrollView, "startPagePosition", VECTOR3, START_PAGE_POSITION)
579 DALI_SIGNAL_REGISTRATION(Toolkit, ScrollView, "valueChanged", SIGNAL_SNAP_STARTED)
581 DALI_TYPE_REGISTRATION_END()
585 ///////////////////////////////////////////////////////////////////////////////////////////////////
587 ///////////////////////////////////////////////////////////////////////////////////////////////////
589 Dali::Toolkit::ScrollView ScrollView::New()
591 // Create the implementation
592 ScrollViewPtr scrollView(new ScrollView());
594 // Pass ownership to CustomActor via derived handle
595 Dali::Toolkit::ScrollView handle(*scrollView);
597 // Second-phase init of the implementation
598 // This can only be done after the CustomActor connection has been made...
599 scrollView->Initialize();
604 ScrollView::ScrollView()
605 : ScrollBase(ControlBehaviour(DISABLE_STYLE_CHANGE_SIGNALS)), // Enable size negotiation
607 mGestureStackDepth(0),
608 mScrollStateFlags(0),
609 mLockAxis(LockPossible),
610 mScrollUpdateDistance(DEFAULT_SCROLL_UPDATE_DISTANCE),
611 mMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT),
612 mUserMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT),
613 mSnapOvershootDuration(DEFAULT_SNAP_OVERSHOOT_DURATION),
614 mSnapOvershootAlphaFunction(AlphaFunction::EASE_OUT),
615 mSnapDuration(DEFAULT_SLOW_SNAP_ANIMATION_DURATION),
616 mSnapAlphaFunction(AlphaFunction::EASE_OUT),
617 mMinFlickDistance(DEFAULT_MIN_FLICK_DISTANCE),
618 mFlickSpeedThreshold(DEFAULT_MIN_FLICK_SPEED_THRESHOLD),
619 mFlickDuration(DEFAULT_FAST_SNAP_ANIMATION_DURATION),
620 mFlickAlphaFunction(AlphaFunction::EASE_OUT),
621 mAxisAutoLockGradient(DEFAULT_AXIS_AUTO_LOCK_GRADIENT),
622 mFrictionCoefficient(DEFAULT_FRICTION_COEFFICIENT),
623 mFlickSpeedCoefficient(DEFAULT_FLICK_SPEED_COEFFICIENT),
624 mMaxFlickSpeed(DEFAULT_MAX_FLICK_SPEED),
625 mWheelScrollDistanceStep(Vector2::ZERO),
626 mInAccessibilityPan(false),
628 mScrollInterrupted(false),
631 mTouchDownTimeoutReached(false),
632 mActorAutoSnapEnabled(false),
633 mAutoResizeContainerEnabled(false),
635 mAxisAutoLock(false),
637 mDefaultMaxOvershoot(true),
638 mCanScrollHorizontal(true),
639 mCanScrollVertical(true),
640 mTransientScrollBar(true)
644 void ScrollView::OnInitialize()
648 // Internal Actor, used to hide actors from enumerations.
649 // Also actors added to Internal actor appear as overlays e.g. ScrollBar components.
650 mInternalActor = Actor::New();
651 self.Add(mInternalActor);
653 mInternalActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
654 mInternalActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
655 mInternalActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
659 mScrollPostPosition = mScrollPrePosition = Vector2::ZERO;
661 mWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
663 mGestureStackDepth = 0;
665 self.TouchedSignal().Connect(this, &ScrollView::OnTouch);
666 EnableGestureDetection(GestureType::Value(GestureType::PAN));
668 // By default we'll allow the user to freely drag the scroll view,
669 // while disabling the other rulers.
670 RulerPtr ruler = new DefaultRuler();
674 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, mCanScrollVertical);
675 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, mCanScrollHorizontal);
677 ScrollViewPropertyHandler::UpdatePropertyDomain(*this);
678 mConstraints.SetInternalConstraints(*this);
680 // Connect wheel event
681 self.WheelEventSignal().Connect(this, &ScrollView::OnWheelEvent);
683 DevelControl::SetAccessibilityConstructor(Self(), [](Dali::Actor actor) {
684 return std::unique_ptr<Dali::Accessibility::Accessible>(
685 new AccessibleImpl(actor, Dali::Accessibility::Role::SCROLL_PANE));
689 void ScrollView::OnSceneConnection(int depth)
691 DALI_LOG_SCROLL_STATE("[0x%X]", this);
695 SetScrollSensitive(false);
696 SetScrollSensitive(true);
699 if(IsOvershootEnabled())
701 // try and make sure property notifications are set
702 EnableScrollOvershoot(true);
705 ScrollBase::OnSceneConnection(depth);
708 void ScrollView::OnSceneDisconnection()
710 DALI_LOG_SCROLL_STATE("[0x%X]", this);
714 ScrollBase::OnSceneDisconnection();
717 ScrollView::~ScrollView()
719 DALI_LOG_SCROLL_STATE("[0x%X]", this);
722 void ScrollView::ApplyEffect(Toolkit::ScrollViewEffect effect)
724 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
726 // Assertion check to ensure effect doesn't already exist in this scrollview
727 bool effectAlreadyExistsInScrollView(false);
728 for(ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
732 effectAlreadyExistsInScrollView = true;
737 DALI_ASSERT_ALWAYS(!effectAlreadyExistsInScrollView);
739 // add effect to effects list
740 mEffects.push_back(effect);
742 // invoke Attachment request to ScrollView first
743 GetImpl(effect).Attach(self);
746 void ScrollView::RemoveEffect(Toolkit::ScrollViewEffect effect)
748 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
750 // remove effect from effects list
751 bool effectExistedInScrollView(false);
752 for(ScrollViewEffectIter iter = mEffects.begin(); iter != mEffects.end(); ++iter)
756 mEffects.erase(iter);
757 effectExistedInScrollView = true;
762 // Assertion check to ensure effect existed.
763 DALI_ASSERT_ALWAYS(effectExistedInScrollView);
765 // invoke Detachment request to ScrollView last
766 GetImpl(effect).Detach(self);
769 void ScrollView::RemoveAllEffects()
771 Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self());
773 for(ScrollViewEffectIter effectIter = mEffects.begin(); effectIter != mEffects.end(); ++effectIter)
775 Toolkit::ScrollViewEffect effect = *effectIter;
777 // invoke Detachment request to ScrollView last
778 GetImpl(effect).Detach(self);
784 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
786 ApplyConstraintToBoundActors(constraint);
789 void ScrollView::RemoveConstraintsFromChildren()
791 RemoveConstraintsFromBoundActors();
794 void ScrollView::SetRulerX(RulerPtr ruler)
798 ScrollViewPropertyHandler::UpdatePropertyDomain(*this);
799 mConstraints.UpdateMainInternalConstraint(*this);
802 void ScrollView::SetRulerY(RulerPtr ruler)
806 ScrollViewPropertyHandler::UpdatePropertyDomain(*this);
807 mConstraints.UpdateMainInternalConstraint(*this);
810 void ScrollView::SetScrollSensitive(bool sensitive)
813 PanGestureDetector panGesture(GetPanGestureDetector());
815 DALI_LOG_SCROLL_STATE("[0x%X] sensitive: before:[%d] setting[%d]", this, int(mSensitive), int(sensitive));
817 if((!mSensitive) && (sensitive))
819 mSensitive = sensitive;
820 panGesture.Attach(self);
822 else if((mSensitive) && (!sensitive))
824 DALI_LOG_SCROLL_STATE("[0x%X] BEFORE: panning:[%d]", this, int(mPanning));
826 // while the scroll view is panning, the state needs to be reset.
829 PanGesture cancelGesture = DevelPanGesture::New(GestureState::CANCELLED);
830 OnPan(cancelGesture);
833 panGesture.Detach(self);
834 mSensitive = sensitive;
836 mGestureStackDepth = 0;
837 DALI_LOG_SCROLL_STATE("[0x%X] AFTER: panning:[%d]", this, int(mPanning));
841 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
843 mMaxOvershoot.x = overshootX;
844 mMaxOvershoot.y = overshootY;
845 mUserMaxOvershoot = mMaxOvershoot;
846 mDefaultMaxOvershoot = false;
847 mConstraints.UpdateMainInternalConstraint(*this);
850 bool ScrollView::GetActorAutoSnap()
852 return mActorAutoSnapEnabled;
855 void ScrollView::SetAutoResize(bool enable)
857 mAutoResizeContainerEnabled = enable;
858 // TODO: This needs a lot of issues to be addressed before working.
861 void ScrollView::SetWrapMode(bool enable)
864 Self().SetProperty(Toolkit::ScrollView::Property::WRAP, enable);
867 void ScrollView::SetAxisAutoLock(bool enable)
869 mAxisAutoLock = enable;
870 mConstraints.UpdateMainInternalConstraint(*this);
873 void ScrollView::SetAxisAutoLockGradient(float gradient)
875 DALI_ASSERT_DEBUG(gradient >= 0.0f && gradient <= 1.0f);
876 mAxisAutoLockGradient = gradient;
877 mConstraints.UpdateMainInternalConstraint(*this);
880 void ScrollView::SetFrictionCoefficient(float friction)
882 DALI_ASSERT_DEBUG(friction > 0.0f);
883 mFrictionCoefficient = friction;
886 unsigned int ScrollView::GetCurrentPage() const
888 // in case animation is currently taking place.
889 Vector2 position = GetPropertyPosition();
892 unsigned int page = 0;
893 unsigned int pagesPerVolume = 1;
894 unsigned int volume = 0;
896 // if rulerX is enabled, then get page count (columns)
897 page = mRulerX->GetPageFromPosition(-position.x, mWrapMode);
898 volume = mRulerY->GetPageFromPosition(-position.y, mWrapMode);
899 pagesPerVolume = mRulerX->GetTotalPages();
901 return volume * pagesPerVolume + page;
904 Vector2 ScrollView::GetCurrentScrollPosition() const
906 return -GetPropertyPosition();
909 void ScrollView::TransformTo(const Vector2& position,
910 DirectionBias horizontalBias,
911 DirectionBias verticalBias)
913 TransformTo(position, mSnapDuration, mSnapAlphaFunction, horizontalBias, verticalBias);
916 void ScrollView::TransformTo(const Vector2& position, float duration, AlphaFunction alpha, DirectionBias horizontalBias, DirectionBias verticalBias)
918 // If this is called while the timer is running, then cancel it
919 StopTouchDownTimer();
923 // Guard against destruction during signal emission
924 // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo()
925 Toolkit::ScrollView handle(GetOwner());
927 DALI_LOG_SCROLL_STATE("[0x%X] pos[%.2f,%.2f], duration[%.2f] bias[%d, %d]",
935 Vector2 currentScrollPosition = GetCurrentScrollPosition();
936 self.SetProperty(Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(currentScrollPosition));
938 if(mScrolling) // are we interrupting a current scroll?
940 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
942 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
943 mScrollCompletedSignal.Emit(currentScrollPosition);
946 if(mPanning) // are we interrupting a current pan?
948 DALI_LOG_SCROLL_STATE("[0x%X] Interrupting Pan, set to false", this);
950 mGestureStackDepth = 0;
951 self.SetProperty(Toolkit::ScrollView::Property::PANNING, false);
953 if(mConstraints.mScrollMainInternalPrePositionConstraint)
955 mConstraints.mScrollMainInternalPrePositionConstraint.Remove();
959 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, true);
962 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
963 mScrollStartedSignal.Emit(currentScrollPosition);
964 bool animating = AnimateTo(-position,
965 Vector2::ONE * duration,
974 // if not animating, then this pan has completed right now.
975 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
978 // If we have no duration, then in the next update frame, we will be at the position specified as we just set.
979 // In this scenario, we cannot return the currentScrollPosition as this is out-of-date and should instead return the requested final position
980 Vector2 completedPosition(currentScrollPosition);
981 if(duration <= Math::MACHINE_EPSILON_10)
983 completedPosition = position;
986 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 2 [%.2f, %.2f]", this, completedPosition.x, completedPosition.y);
987 SetScrollUpdateNotification(false);
988 mScrollCompletedSignal.Emit(completedPosition);
992 void ScrollView::ScrollTo(const Vector2& position)
994 ScrollTo(position, mSnapDuration);
997 void ScrollView::ScrollTo(const Vector2& position, float duration)
999 ScrollTo(position, duration, DIRECTION_BIAS_NONE, DIRECTION_BIAS_NONE);
1002 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha)
1004 ScrollTo(position, duration, alpha, DIRECTION_BIAS_NONE, DIRECTION_BIAS_NONE);
1007 void ScrollView::ScrollTo(const Vector2& position, float duration, DirectionBias horizontalBias, DirectionBias verticalBias)
1009 ScrollTo(position, duration, mSnapAlphaFunction, horizontalBias, verticalBias);
1012 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha, DirectionBias horizontalBias, DirectionBias verticalBias)
1014 DALI_LOG_SCROLL_STATE("[0x%X] position[%.2f, %.2f] duration[%.2f], bias[%d, %d]", this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
1015 TransformTo(position, duration, alpha, horizontalBias, verticalBias);
1018 void ScrollView::ScrollTo(unsigned int page)
1020 ScrollTo(page, mSnapDuration);
1023 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
1026 unsigned int volume;
1027 unsigned int libraries;
1029 // The position to scroll to is continuous and linear
1030 // unless a domain has been enabled on the X axis.
1031 // or if WrapMode has been enabled.
1032 bool carryX = mRulerX->GetDomain().enabled | mWrapMode;
1033 bool carryY = mRulerY->GetDomain().enabled | mWrapMode;
1035 position.x = mRulerX->GetPositionFromPage(page, volume, carryX);
1036 position.y = mRulerY->GetPositionFromPage(volume, libraries, carryY);
1038 ScrollTo(position, duration, bias, bias);
1041 void ScrollView::ScrollTo(Actor& actor)
1043 ScrollTo(actor, mSnapDuration);
1046 void ScrollView::ScrollTo(Actor& actor, float duration)
1048 DALI_ASSERT_ALWAYS(actor.GetParent() == Self());
1050 Actor self = Self();
1051 Vector3 size = self.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
1052 Vector3 position = actor.GetCurrentProperty<Vector3>(Actor::Property::POSITION);
1053 Vector2 prePosition = GetPropertyPrePosition();
1054 position.GetVectorXY() -= prePosition;
1056 ScrollTo(Vector2(position.x - size.width * 0.5f, position.y - size.height * 0.5f), duration);
1059 Actor ScrollView::FindClosestActor()
1061 Actor self = Self();
1062 Vector3 size = self.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
1064 return FindClosestActorToPosition(Vector3(size.width * 0.5f, size.height * 0.5f, 0.0f));
1067 Actor ScrollView::FindClosestActorToPosition(const Vector3& position, FindDirection dirX, FindDirection dirY, FindDirection dirZ)
1069 return ::FindClosestActorToPosition(Self(), mInternalActor, position, dirX, dirY, dirZ);
1072 bool ScrollView::ScrollToSnapPoint()
1074 DALI_LOG_SCROLL_STATE("[0x%X]", this);
1075 Vector2 stationaryVelocity = Vector2(0.0f, 0.0f);
1076 return SnapWithVelocity(stationaryVelocity);
1079 bool ScrollView::SnapWithVelocity(Vector2 velocity)
1081 Vector2 positionSnap = mScrollPrePosition;
1082 Vector2 positionDuration = Vector2::ONE * mSnapDuration;
1083 AlphaFunction alphaFunction = mSnapAlphaFunction;
1087 ::SnapWithVelocity(*this, mRulerX, mRulerY, mLockAxis, velocity, mMaxOvershoot, positionSnap, positionDuration, alphaFunction, mInAccessibilityPan, isFlick, isFreeFlick);
1089 bool animating = AnimateTo(positionSnap, positionDuration, alphaFunction, false, DIRECTION_BIAS_NONE, DIRECTION_BIAS_NONE, isFlick || isFreeFlick ? FLICK : SNAP);
1094 void ScrollView::StopAnimation(void)
1096 // Clear Snap animation if exists.
1097 StopAnimation(mInternalXAnimation);
1098 StopAnimation(mInternalYAnimation);
1099 mScrollStateFlags = 0;
1100 // remove scroll animation flags
1101 HandleStoppedAnimation();
1104 void ScrollView::StopAnimation(Animation& animation)
1113 bool ScrollView::AnimateTo(const Vector2& position, const Vector2& positionDuration, AlphaFunction alpha, bool findShortcuts, DirectionBias horizontalBias, DirectionBias verticalBias, SnapType snapType)
1115 // Here we perform an animation on a number of properties (depending on which have changed)
1116 // The animation is applied to all ScrollBases
1117 Actor self = Self();
1118 mScrollTargetPosition = position;
1119 float totalDuration = 0.0f;
1121 bool positionChanged = (mScrollTargetPosition != mScrollPostPosition);
1125 totalDuration = std::max(totalDuration, positionDuration.x);
1126 totalDuration = std::max(totalDuration, positionDuration.y);
1130 // try to animate for a frame, on some occasions update will be changing scroll value while event side thinks it hasnt changed
1131 totalDuration = 0.01f;
1132 positionChanged = true;
1137 // Position Delta ///////////////////////////////////////////////////////
1140 mConstraints.UpdateMainInternalConstraint(*this);
1141 if(mWrapMode && findShortcuts)
1143 // In Wrap Mode, the shortest distance is a little less intuitive...
1144 const RulerDomain rulerDomainX = mRulerX->GetDomain();
1145 const RulerDomain rulerDomainY = mRulerY->GetDomain();
1147 if(mRulerX->IsEnabled())
1149 float dir = VectorInDomain(-mScrollPrePosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
1150 mScrollTargetPosition.x = mScrollPrePosition.x + -dir;
1153 if(mRulerY->IsEnabled())
1155 float dir = VectorInDomain(-mScrollPrePosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
1156 mScrollTargetPosition.y = mScrollPrePosition.y + -dir;
1160 // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting
1161 // a horizonal/vertical wall.delay
1162 AnimateInternalXTo(mScrollTargetPosition.x, positionDuration.x, alpha);
1163 AnimateInternalYTo(mScrollTargetPosition.y, positionDuration.y, alpha);
1165 if(!(mScrollStateFlags & SCROLL_ANIMATION_FLAGS))
1167 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollTargetPosition.x, mScrollTargetPosition.y);
1168 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollTargetPosition);
1169 mScrollPrePosition = mScrollTargetPosition;
1170 mScrollPostPosition = mScrollTargetPosition;
1171 WrapPosition(mScrollPostPosition);
1174 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);
1175 DALI_LOG_SCROLL_STATE("[0x%X] SCROLL_PRE_POSITION[%.2f, %.2f], SCROLL_POSITION[%.2f, %.2f]", this, self.GetCurrentProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get<Vector2>().x, self.GetCurrentProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get<Vector2>().y, self.GetCurrentProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get<Vector2>().x, self.GetCurrentProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get<Vector2>().y);
1178 SetScrollUpdateNotification(true);
1180 // Always send a snap event when AnimateTo is called.
1181 Toolkit::ScrollView::SnapEvent snapEvent;
1182 snapEvent.type = snapType;
1183 snapEvent.position = -mScrollTargetPosition;
1184 snapEvent.duration = totalDuration;
1186 DALI_LOG_SCROLL_STATE("[0x%X] mSnapStartedSignal [%.2f, %.2f]", this, snapEvent.position.x, snapEvent.position.y);
1187 mSnapStartedSignal.Emit(snapEvent);
1189 return (mScrollStateFlags & SCROLL_ANIMATION_FLAGS) != 0;
1192 void ScrollView::EnableScrollOvershoot(bool enable)
1196 if(!mOvershootIndicator)
1198 mOvershootIndicator = ScrollOvershootIndicator::New();
1201 mOvershootIndicator->AttachToScrollable(*this);
1205 mMaxOvershoot = mUserMaxOvershoot;
1207 if(mOvershootIndicator)
1209 mOvershootIndicator->DetachFromScrollable(*this);
1213 mConstraints.UpdateMainInternalConstraint(*this);
1216 void ScrollView::AddOverlay(Actor actor)
1218 actor.SetProperty(Actor::Property::DRAW_MODE, DrawMode::OVERLAY_2D);
1219 mInternalActor.Add(actor);
1222 void ScrollView::RemoveOverlay(Actor actor)
1224 mInternalActor.Remove(actor);
1227 void ScrollView::SetOvershootSize(const Vector2& size)
1229 mOvershootSize = size;
1230 if(IsOvershootEnabled() && mOvershootIndicator)
1232 mOvershootIndicator->AttachToScrollable(*this);
1236 void ScrollView::SetOvershootEffectColor(const Vector4& color)
1238 mOvershootEffectColor = color;
1239 if(mOvershootIndicator)
1241 mOvershootIndicator->SetOvershootEffectColor(color);
1245 void ScrollView::SetScrollingDirection(Radian direction, Radian threshold)
1247 PanGestureDetector panGesture(GetPanGestureDetector());
1249 // First remove just in case we have some set, then add.
1250 panGesture.RemoveDirection(direction);
1251 panGesture.AddDirection(direction, threshold);
1254 void ScrollView::RemoveScrollingDirection(Radian direction)
1256 PanGestureDetector panGesture(GetPanGestureDetector());
1257 panGesture.RemoveDirection(direction);
1260 Toolkit::ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
1262 return mSnapStartedSignal;
1265 bool ScrollView::AccessibleImpl::ScrollToChild(Actor child)
1267 auto scrollView = Dali::Toolkit::ScrollView::DownCast(Self());
1268 if(Toolkit::GetImpl(scrollView).FindClosestActor() == child)
1273 // FIXME: ScrollTo does not work (snaps back to original position)
1274 scrollView.ScrollTo(child, scrollView.GetScrollFlickDuration());
1278 void ScrollView::FindAndUnbindActor(Actor child)
1283 Vector2 ScrollView::GetPropertyPrePosition() const
1285 Vector2 position = Self().GetCurrentProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION);
1286 WrapPosition(position);
1290 Vector2 ScrollView::GetPropertyPosition() const
1292 Vector2 position = Self().GetCurrentProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_POSITION);
1293 WrapPosition(position);
1298 void ScrollView::HandleStoppedAnimation()
1300 SetScrollUpdateNotification(false);
1303 void ScrollView::HandleSnapAnimationFinished()
1305 // Emit Signal that scrolling has completed.
1307 Actor self = Self();
1308 self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
1310 Vector2 deltaPosition(mScrollPrePosition);
1312 UpdateLocalScrollProperties();
1313 WrapPosition(mScrollPrePosition);
1314 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y);
1315 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
1317 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1318 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 3 current[%.2f, %.2f], mScrollTargetPosition[%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y, -mScrollTargetPosition.x, -mScrollTargetPosition.y);
1319 mScrollCompletedSignal.Emit(currentScrollPosition);
1321 mDomainOffset += deltaPosition - mScrollPostPosition;
1322 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset);
1323 HandleStoppedAnimation();
1326 void ScrollView::SetScrollUpdateNotification(bool enabled)
1328 Actor self = Self();
1329 if(mScrollXUpdateNotification)
1331 // disconnect now to avoid a notification before removed from update thread
1332 mScrollXUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1333 self.RemovePropertyNotification(mScrollXUpdateNotification);
1334 mScrollXUpdateNotification.Reset();
1336 if(enabled && !mScrollUpdatedSignal.Empty())
1338 // Only set up the notification when the application has connected to the updated signal
1339 mScrollXUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 0, StepCondition(mScrollUpdateDistance, 0.0f));
1340 mScrollXUpdateNotification.NotifySignal().Connect(this, &ScrollView::OnScrollUpdateNotification);
1342 if(mScrollYUpdateNotification)
1344 // disconnect now to avoid a notification before removed from update thread
1345 mScrollYUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification);
1346 self.RemovePropertyNotification(mScrollYUpdateNotification);
1347 mScrollYUpdateNotification.Reset();
1349 if(enabled && !mScrollUpdatedSignal.Empty())
1351 // Only set up the notification when the application has connected to the updated signal
1352 mScrollYUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 1, StepCondition(mScrollUpdateDistance, 0.0f));
1353 mScrollYUpdateNotification.NotifySignal().Connect(this, &ScrollView::OnScrollUpdateNotification);
1357 void ScrollView::OnScrollUpdateNotification(Dali::PropertyNotification& source)
1359 // Guard against destruction during signal emission
1360 Toolkit::ScrollView handle(GetOwner());
1362 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1363 mScrollUpdatedSignal.Emit(currentScrollPosition);
1366 bool ScrollView::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
1368 Dali::BaseHandle handle(object);
1370 bool connected(true);
1371 Toolkit::ScrollView view = Toolkit::ScrollView::DownCast(handle);
1373 if(0 == strcmp(signalName.c_str(), SIGNAL_SNAP_STARTED))
1375 view.SnapStartedSignal().Connect(tracker, functor);
1379 // signalName does not match any signal
1386 void ScrollView::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1388 // need to update domain properties for new size
1389 ScrollViewPropertyHandler::UpdatePropertyDomain(*this);
1392 void ScrollView::OnSizeSet(const Vector3& size)
1394 // need to update domain properties for new size
1395 if(mDefaultMaxOvershoot)
1397 mUserMaxOvershoot.x = size.x * 0.5f;
1398 mUserMaxOvershoot.y = size.y * 0.5f;
1399 if(!IsOvershootEnabled())
1401 mMaxOvershoot = mUserMaxOvershoot;
1404 ScrollViewPropertyHandler::UpdatePropertyDomain(*this);
1405 mConstraints.UpdateMainInternalConstraint(*this);
1406 if(IsOvershootEnabled())
1408 mOvershootIndicator->Reset();
1411 ScrollBase::OnSizeSet(size);
1414 void ScrollView::OnChildAdd(Actor& child)
1416 ScrollBase::OnChildAdd(child);
1418 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
1421 mScrollBar = scrollBar;
1422 scrollBar.SetProperty(Dali::Actor::Property::NAME, "ScrollBar");
1424 mInternalActor.Add(scrollBar);
1425 if(scrollBar.GetScrollDirection() == Toolkit::ScrollBar::HORIZONTAL)
1427 scrollBar.SetScrollPropertySource(Self(),
1428 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_X,
1429 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_X,
1430 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_X,
1431 Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_X);
1435 scrollBar.SetScrollPropertySource(Self(),
1436 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_Y,
1437 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
1438 Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_Y,
1439 Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_Y);
1442 if(mTransientScrollBar)
1444 // Show the scroll-indicator for a brief period
1445 Property::Map emptyMap;
1446 scrollBar.DoAction("ShowTransientIndicator", emptyMap);
1449 else if(mAlterChild)
1455 void ScrollView::OnChildRemove(Actor& child)
1457 // TODO: Actor needs a RemoveConstraint method to take out an individual constraint.
1460 ScrollBase::OnChildRemove(child);
1463 void ScrollView::StartTouchDownTimer()
1465 if(!mTouchDownTimer)
1467 mTouchDownTimer = Timer::New(TOUCH_DOWN_TIMER_INTERVAL);
1468 mTouchDownTimer.TickSignal().Connect(this, &ScrollView::OnTouchDownTimeout);
1471 mTouchDownTimer.Start();
1474 void ScrollView::StopTouchDownTimer()
1478 mTouchDownTimer.Stop();
1482 bool ScrollView::OnTouchDownTimeout()
1484 DALI_LOG_SCROLL_STATE("[0x%X]", this);
1486 mTouchDownTimeoutReached = true;
1488 unsigned int currentScrollStateFlags(mScrollStateFlags); // Cleared in StopAnimation so keep local copy for comparison
1489 if(currentScrollStateFlags & (SCROLL_ANIMATION_FLAGS | SNAP_ANIMATION_FLAGS))
1491 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling Or snapping flags set, stopping animation", this);
1494 if(currentScrollStateFlags & SCROLL_ANIMATION_FLAGS)
1496 DALI_LOG_SCROLL_STATE("[0x%X] Scrolling flags set, emitting signal", this);
1498 mScrollInterrupted = true;
1499 // reset domain offset as scrolling from original plane.
1500 mDomainOffset = Vector2::ZERO;
1501 Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, Vector2::ZERO);
1503 UpdateLocalScrollProperties();
1504 Vector2 currentScrollPosition = GetCurrentScrollPosition();
1505 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 4 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
1506 mScrollCompletedSignal.Emit(currentScrollPosition);
1513 bool ScrollView::OnTouch(Actor actor, const TouchEvent& touch)
1517 DALI_LOG_SCROLL_STATE("[0x%X], Not Sensitive, ignoring", this);
1519 // Ignore this touch event, if scrollview is insensitive.
1523 // Ignore events with multiple-touch points
1524 if(touch.GetPointCount() != 1)
1526 DALI_LOG_SCROLL_STATE("[0x%X], multiple touch, ignoring", this);
1531 const PointState::Type pointState = touch.GetState(0);
1532 if(pointState == PointState::DOWN)
1534 DALI_LOG_SCROLL_STATE("[0x%X] Down", this);
1536 if(mGestureStackDepth == 0)
1538 mTouchDownTime = touch.GetTime();
1540 // This allows time for a pan-gesture to start, to avoid breaking snap-animation behavior with fast flicks.
1541 // If touch-down does not become a pan (after timeout interval), then snap-animation can be interrupted.
1542 mTouchDownTimeoutReached = false;
1543 mScrollInterrupted = false;
1544 StartTouchDownTimer();
1547 else if((pointState == PointState::UP) ||
1548 ((pointState == PointState::INTERRUPTED) && (touch.GetHitActor(0) == Self())))
1550 DALI_LOG_SCROLL_STATE("[0x%X] %s", this, ((pointState == PointState::UP) ? "Up" : "Interrupted"));
1552 StopTouchDownTimer();
1554 // if the user touches and releases without enough movement to go
1555 // into a gesture state, then we should snap to nearest point.
1556 // otherwise our scroll could be stopped (interrupted) half way through an animation.
1557 if(mGestureStackDepth == 0 && mTouchDownTimeoutReached)
1559 if((pointState == PointState::INTERRUPTED) ||
1560 ((touch.GetTime() - mTouchDownTime) >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET))
1562 // Reset the velocity only if down was received a while ago
1563 mLastVelocity = Vector2(0.0f, 0.0f);
1566 UpdateLocalScrollProperties();
1567 // Only finish the transform if scrolling was interrupted on down or if we are scrolling
1568 if(mScrollInterrupted || mScrolling)
1570 DALI_LOG_SCROLL_STATE("[0x%X] Calling FinishTransform", this);
1575 mTouchDownTimeoutReached = false;
1576 mScrollInterrupted = false;
1582 bool ScrollView::OnWheelEvent(Actor actor, const WheelEvent& event)
1586 // Ignore this wheel event, if scrollview is insensitive.
1590 Vector2 targetScrollPosition = GetPropertyPosition();
1592 if(mRulerX->IsEnabled() && !mRulerY->IsEnabled())
1594 // If only the ruler in the X axis is enabled, scroll in the X axis.
1595 if(mRulerX->GetType() == Ruler::FREE)
1597 // Free panning mode
1598 targetScrollPosition.x += event.GetDelta() * mWheelScrollDistanceStep.x;
1599 ClampPosition(targetScrollPosition);
1600 ScrollTo(-targetScrollPosition);
1602 else if(!mScrolling)
1604 // Snap mode, only respond to the event when the previous snap animation is finished.
1605 ScrollTo(GetCurrentPage() - event.GetDelta());
1610 // If the ruler in the Y axis is enabled, scroll in the Y axis.
1611 if(mRulerY->GetType() == Ruler::FREE)
1613 // Free panning mode
1614 targetScrollPosition.y += event.GetDelta() * mWheelScrollDistanceStep.y;
1615 ClampPosition(targetScrollPosition);
1616 ScrollTo(-targetScrollPosition);
1618 else if(!mScrolling)
1620 // Snap mode, only respond to the event when the previous snap animation is finished.
1621 ScrollTo(GetCurrentPage() - event.GetDelta() * mRulerX->GetTotalPages());
1628 void ScrollView::ResetScrolling()
1630 Actor self = Self();
1631 self.GetCurrentProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get(mScrollPostPosition);
1632 mScrollPrePosition = mScrollPostPosition;
1633 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPostPosition.x, mScrollPostPosition.y);
1634 self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPostPosition);
1637 void ScrollView::UpdateLocalScrollProperties()
1639 Actor self = Self();
1640 self.GetCurrentProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get(mScrollPrePosition);
1641 self.GetCurrentProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get(mScrollPostPosition);
1644 // private functions
1646 void ScrollView::PreAnimatedScrollSetup()
1648 // SCROLL_PRE_POSITION is our unclamped property with wrapping
1649 // SCROLL_POSITION is our final scroll position after clamping
1651 Actor self = Self();
1653 Vector2 deltaPosition(mScrollPostPosition);
1654 WrapPosition(mScrollPostPosition);
1655 mDomainOffset += deltaPosition - mScrollPostPosition;
1656 Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset);
1658 if(mScrollStateFlags & SCROLL_X_STATE_MASK)
1660 // already performing animation on internal x position
1661 StopAnimation(mInternalXAnimation);
1664 if(mScrollStateFlags & SCROLL_Y_STATE_MASK)
1666 // already performing animation on internal y position
1667 StopAnimation(mInternalYAnimation);
1670 mScrollStateFlags = 0;
1672 // Update Actor position with this wrapped value.
1675 void ScrollView::FinaliseAnimatedScroll()
1677 // TODO - common animation finishing code in here
1680 void ScrollView::AnimateInternalXTo(float position, float duration, AlphaFunction alpha)
1682 StopAnimation(mInternalXAnimation);
1684 if(duration > Math::MACHINE_EPSILON_10)
1686 Actor self = Self();
1687 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetCurrentProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get<Vector2>().x, position);
1688 mInternalXAnimation = Animation::New(duration);
1689 DALI_LOG_SCROLL_STATE("[0x%X], mInternalXAnimation[0x%X]", this, mInternalXAnimation.GetObjectPtr());
1690 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
1691 mInternalXAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position, alpha, TimePeriod(duration));
1692 mInternalXAnimation.Play();
1694 // erase current state flags
1695 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
1696 // add internal animation state flag
1697 mScrollStateFlags |= AnimatingInternalX;
1701 void ScrollView::AnimateInternalYTo(float position, float duration, AlphaFunction alpha)
1703 StopAnimation(mInternalYAnimation);
1705 if(duration > Math::MACHINE_EPSILON_10)
1707 Actor self = Self();
1708 DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetCurrentProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get<Vector2>().y, position);
1709 mInternalYAnimation = Animation::New(duration);
1710 DALI_LOG_SCROLL_STATE("[0x%X], mInternalYAnimation[0x%X]", this, mInternalYAnimation.GetObjectPtr());
1711 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished);
1712 mInternalYAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position, alpha, TimePeriod(duration));
1713 mInternalYAnimation.Play();
1715 // erase current state flags
1716 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
1717 // add internal animation state flag
1718 mScrollStateFlags |= AnimatingInternalY;
1722 void ScrollView::OnScrollAnimationFinished(Animation& source)
1724 // Guard against destruction during signal emission
1725 // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished()
1726 Toolkit::ScrollView handle(GetOwner());
1728 bool scrollingFinished = false;
1730 // update our local scroll positions
1731 UpdateLocalScrollProperties();
1733 if(source == mInternalXAnimation)
1735 DALI_LOG_SCROLL_STATE("[0x%X] mInternalXAnimation[0x%X], expected[%.2f], actual[%.2f], post[%.2f]", this, mInternalXAnimation.GetObjectPtr(), mScrollTargetPosition.x, Self().GetCurrentProperty(SCROLL_PRE_POSITION).Get<Vector2>().x, mScrollPostPosition.x);
1737 if(!(mScrollStateFlags & AnimatingInternalY))
1739 scrollingFinished = true;
1741 mInternalXAnimation.Reset();
1742 // wrap pre scroll x position and set it
1745 const RulerDomain rulerDomain = mRulerX->GetDomain();
1746 mScrollPrePosition.x = -WrapInDomain(-mScrollPrePosition.x, rulerDomain.min, rulerDomain.max);
1747 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y);
1748 handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
1750 SnapInternalXTo(mScrollPostPosition.x);
1753 if(source == mInternalYAnimation)
1755 DALI_LOG_SCROLL_STATE("[0x%X] mInternalYAnimation[0x%X], expected[%.2f], actual[%.2f], post[%.2f]", this, mInternalYAnimation.GetObjectPtr(), mScrollTargetPosition.y, DevelHandle::GetProperty(Self(), SCROLL_PRE_POSITION).Get<Vector2>().y, mScrollPostPosition.y);
1757 if(!(mScrollStateFlags & AnimatingInternalX))
1759 scrollingFinished = true;
1761 mInternalYAnimation.Reset();
1764 // wrap pre scroll y position and set it
1765 const RulerDomain rulerDomain = mRulerY->GetDomain();
1766 mScrollPrePosition.y = -WrapInDomain(-mScrollPrePosition.y, rulerDomain.min, rulerDomain.max);
1767 DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y);
1768 handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition);
1770 SnapInternalYTo(mScrollPostPosition.y);
1773 DALI_LOG_SCROLL_STATE("[0x%X] scrollingFinished[%d] Animation[0x%X]", this, scrollingFinished, source.GetObjectPtr());
1775 if(scrollingFinished)
1777 HandleSnapAnimationFinished();
1781 void ScrollView::OnSnapInternalPositionFinished(Animation& source)
1783 Actor self = Self();
1784 UpdateLocalScrollProperties();
1785 if(source == mInternalXAnimation)
1787 DALI_LOG_SCROLL_STATE("[0x%X] Finished X PostPosition Animation", this);
1789 // clear internal x animation flags
1790 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
1791 mInternalXAnimation.Reset();
1792 WrapPosition(mScrollPrePosition);
1794 if(source == mInternalYAnimation)
1796 DALI_LOG_SCROLL_STATE("[0x%X] Finished Y PostPosition Animation", this);
1798 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
1799 mInternalYAnimation.Reset();
1800 WrapPosition(mScrollPrePosition);
1804 void ScrollView::SnapInternalXTo(float position)
1806 Actor self = Self();
1808 StopAnimation(mInternalXAnimation);
1810 // erase current state flags
1811 mScrollStateFlags &= ~SCROLL_X_STATE_MASK;
1813 // if internal x not equal to inputed parameter, animate it
1814 float duration = std::min(fabsf((position - mScrollPrePosition.x) / mMaxOvershoot.x) * mSnapOvershootDuration, mSnapOvershootDuration);
1815 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration);
1816 if(duration > Math::MACHINE_EPSILON_1)
1818 DALI_LOG_SCROLL_STATE("[0x%X] Starting X Snap Animation to[%.2f]", this, position);
1820 mInternalXAnimation = Animation::New(duration);
1821 mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
1822 mInternalXAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position);
1823 mInternalXAnimation.Play();
1825 // add internal animation state flag
1826 mScrollStateFlags |= SnappingInternalX;
1830 void ScrollView::SnapInternalYTo(float position)
1832 Actor self = Self();
1834 StopAnimation(mInternalYAnimation);
1836 // erase current state flags
1837 mScrollStateFlags &= ~SCROLL_Y_STATE_MASK;
1839 // if internal y not equal to inputed parameter, animate it
1840 float duration = std::min(fabsf((position - mScrollPrePosition.y) / mMaxOvershoot.y) * mSnapOvershootDuration, mSnapOvershootDuration);
1841 DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration);
1842 if(duration > Math::MACHINE_EPSILON_1)
1844 DALI_LOG_SCROLL_STATE("[0x%X] Starting Y Snap Animation to[%.2f]", this, position);
1846 mInternalYAnimation = Animation::New(duration);
1847 mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished);
1848 mInternalYAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position);
1849 mInternalYAnimation.Play();
1851 // add internal animation state flag
1852 mScrollStateFlags |= SnappingInternalY;
1856 void ScrollView::GestureStarted()
1858 // we handle the first gesture.
1859 // if we're currently doing a gesture and receive another
1860 // we continue and combine the effects of the gesture instead of reseting.
1861 if(mGestureStackDepth++ == 0)
1863 Actor self = Self();
1864 StopTouchDownTimer();
1866 mPanDelta = Vector2::ZERO;
1867 mLastVelocity = Vector2::ZERO;
1870 mLockAxis = LockPossible;
1873 if(mScrollStateFlags & SCROLL_X_STATE_MASK)
1875 StopAnimation(mInternalXAnimation);
1877 if(mScrollStateFlags & SCROLL_Y_STATE_MASK)
1879 StopAnimation(mInternalYAnimation);
1881 mScrollStateFlags = 0;
1883 if(mScrolling) // are we interrupting a current scroll?
1885 // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
1887 // send negative scroll position since scroll internal scroll position works as an offset for actors,
1888 // give applications the position within the domain from the scroll view's anchor position
1889 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 5 [%.2f, %.2f]", this, -mScrollPostPosition.x, -mScrollPostPosition.y);
1890 mScrollCompletedSignal.Emit(-mScrollPostPosition);
1895 void ScrollView::GestureContinuing(const Vector2& panDelta)
1897 mPanDelta.x += panDelta.x;
1898 mPanDelta.y += panDelta.y;
1900 // Save the velocity, there is a bug in PanGesture
1901 // Whereby the GestureState::FINISHED's velocity is either:
1902 // NaN (due to time delta of zero between the last two events)
1903 // or 0 (due to position being the same between the last two events)
1905 // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan
1906 // appears mostly horizontal or mostly vertical respectively.
1909 mLockAxis = GetLockAxis(mPanDelta, mLockAxis, mAxisAutoLockGradient);
1910 } // end if mAxisAutoLock
1913 // TODO: Upgrade to use a more powerful gesture detector (one that supports multiple touches on pan - so works as pan and flick gesture)
1914 // BUG: GestureState::FINISHED doesn't always return velocity on release (due to
1915 // timeDelta between last two events being 0 sometimes, or posiiton being the same)
1916 void ScrollView::OnPan(const PanGesture& gesture)
1918 // Guard against destruction during signal emission
1919 // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx()
1924 DALI_LOG_SCROLL_STATE("[0x%X] Pan Ignored, Insensitive", this);
1926 // If another callback on the same original signal disables sensitivity,
1927 // this callback will still be called, so we must suppress it.
1931 // translate Gesture input to get useful data...
1932 switch(gesture.GetState())
1934 case GestureState::STARTED:
1936 DALI_LOG_SCROLL_STATE("[0x%X] Pan Started", this);
1937 const Vector2& position = gesture.GetPosition();
1938 mPanStartPosition = position - gesture.GetDisplacement();
1939 UpdateLocalScrollProperties();
1942 self.SetProperty(Toolkit::ScrollView::Property::PANNING, true);
1943 self.SetProperty(Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(position.x, position.y, 0.0f));
1945 mConstraints.UpdateMainInternalConstraint(*this);
1946 Toolkit::ScrollBar scrollBar = mScrollBar.GetHandle();
1947 if(scrollBar && mTransientScrollBar)
1949 Vector3 size = Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE);
1950 const Toolkit::RulerDomain& rulerDomainX = mRulerX->GetDomain();
1951 const Toolkit::RulerDomain& rulerDomainY = mRulerY->GetDomain();
1953 if((rulerDomainX.max > size.width) || (rulerDomainY.max > size.height))
1955 scrollBar.ShowIndicator();
1961 case GestureState::CONTINUING:
1965 DALI_LOG_SCROLL_STATE("[0x%X] Pan Continuing", this);
1966 GestureContinuing(gesture.GetScreenDisplacement());
1970 // If we do not think we are panning, then we should not do anything here
1976 case GestureState::FINISHED:
1977 case GestureState::CANCELLED:
1981 DALI_LOG_SCROLL_STATE("[0x%X] Pan %s", this, ((gesture.GetState() == GestureState::FINISHED) ? "Finished" : "Cancelled"));
1983 UpdateLocalScrollProperties();
1984 mLastVelocity = gesture.GetVelocity();
1986 self.SetProperty(Toolkit::ScrollView::Property::PANNING, false);
1988 if(mConstraints.mScrollMainInternalPrePositionConstraint)
1990 mConstraints.mScrollMainInternalPrePositionConstraint.Remove();
1993 Toolkit::ScrollBar scrollBar = mScrollBar.GetHandle();
1994 if(scrollBar && mTransientScrollBar)
1996 scrollBar.HideIndicator();
2001 // If we do not think we are panning, then we should not do anything here
2007 case GestureState::POSSIBLE:
2008 case GestureState::CLEAR:
2010 // Nothing to do, not needed.
2014 } // end switch(gesture.state)
2016 OnGestureEx(gesture.GetState());
2019 void ScrollView::OnGestureEx(GestureState state)
2021 // call necessary signals for application developer
2023 if(state == GestureState::STARTED)
2025 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2026 Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, true);
2028 DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 2 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2029 mScrollStartedSignal.Emit(currentScrollPosition);
2031 else if((state == GestureState::FINISHED) ||
2032 (state == GestureState::CANCELLED)) // Finished/default
2034 // when all the gestures have finished, we finish the transform.
2035 // so if a user decides to pan (1 gesture), and then pan+zoom (2 gestures)
2036 // then stop panning (back to 1 gesture), and then stop zooming (0 gestures).
2037 // this is the point we end, and perform necessary snapping.
2038 mGestureStackDepth--;
2039 if(mGestureStackDepth == 0)
2041 // no flick if we have not exceeded min flick distance
2042 if((fabsf(mPanDelta.x) < mMinFlickDistance.x) && (fabsf(mPanDelta.y) < mMinFlickDistance.y))
2044 // reset flick velocity
2045 mLastVelocity = Vector2::ZERO;
2051 DALI_LOG_SCROLL_STATE("[0x%X] mGestureStackDepth[%d]", this, mGestureStackDepth);
2056 void ScrollView::FinishTransform()
2058 // at this stage internal x and x scroll position should have followed prescroll position exactly
2059 Actor self = Self();
2061 PreAnimatedScrollSetup();
2063 // convert pixels/millisecond to pixels per second
2064 bool animating = SnapWithVelocity(mLastVelocity * 1000.0f);
2068 // if not animating, then this pan has completed right now.
2069 SetScrollUpdateNotification(false);
2071 Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, false);
2073 if(fabs(mScrollPrePosition.x - mScrollTargetPosition.x) > Math::MACHINE_EPSILON_10)
2075 SnapInternalXTo(mScrollTargetPosition.x);
2077 if(fabs(mScrollPrePosition.y - mScrollTargetPosition.y) > Math::MACHINE_EPSILON_10)
2079 SnapInternalYTo(mScrollTargetPosition.y);
2081 Vector2 currentScrollPosition = GetCurrentScrollPosition();
2082 DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 6 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
2083 mScrollCompletedSignal.Emit(currentScrollPosition);
2087 bool ScrollView::OnAccessibilityPan(PanGesture gesture)
2089 // Keep track of whether this is an AccessibilityPan
2090 mInAccessibilityPan = true;
2092 mInAccessibilityPan = false;
2097 void ScrollView::ClampPosition(Vector2& position) const
2099 ClampState2D clamped;
2100 ClampPosition(position, clamped);
2103 void ScrollView::ClampPosition(Vector2& position, ClampState2D& clamped) const
2105 Vector3 size = Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE);
2107 ::ClampPosition(size, mRulerX, mRulerY, position, clamped);
2110 void ScrollView::WrapPosition(Vector2& position) const
2114 const RulerDomain rulerDomainX = mRulerX->GetDomain();
2115 const RulerDomain rulerDomainY = mRulerY->GetDomain();
2117 if(mRulerX->IsEnabled())
2119 position.x = -WrapInDomain(-position.x, rulerDomainX.min, rulerDomainX.max);
2122 if(mRulerY->IsEnabled())
2124 position.y = -WrapInDomain(-position.y, rulerDomainY.min, rulerDomainY.max);
2129 void ScrollView::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
2131 ScrollViewPropertyHandler::Set(object, index, value);
2134 Property::Value ScrollView::GetProperty(BaseObject* object, Property::Index index)
2136 return ScrollViewPropertyHandler::Get(object, index);
2139 ScrollView::LockAxis GetLockAxis(const Vector2& panDelta, ScrollView::LockAxis currentLockAxis, float lockGradient)
2141 if(panDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 &&
2142 currentLockAxis == ScrollView::LockPossible)
2144 float dx = fabsf(panDelta.x);
2145 float dy = fabsf(panDelta.y);
2146 if(dx * lockGradient >= dy)
2148 // 0.36:1 gradient to the horizontal (deviate < 20 degrees)
2149 currentLockAxis = ScrollView::LockVertical;
2151 else if(dy * lockGradient > dx)
2153 // 0.36:1 gradient to the vertical (deviate < 20 degrees)
2154 currentLockAxis = ScrollView::LockHorizontal;
2158 currentLockAxis = ScrollView::LockNone;
2161 return currentLockAxis;
2164 } // namespace Internal
2166 } // namespace Toolkit