+/**
+ * Clamp a position
+ * @param[in] size The size to clamp to
+ * @param[in] rulerX The horizontal ruler
+ * @param[in] rulerY The vertical ruler
+ * @param[in,out] position The position to clamp
+ * @param[out] clamped the clamped state
+ */
+void ClampPosition(const Vector3& size, Dali::Toolkit::RulerPtr rulerX, Dali::Toolkit::RulerPtr rulerY, Vector2& position, Dali::Toolkit::ClampState2D& clamped)
+{
+ position.x = -rulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system.
+ 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.
+}
+
+/**
+ * TODO: In situations where axes are different (X snap, Y free)
+ * Each axis should really have their own independent animation (time and equation)
+ * Consider, X axis snapping to nearest grid point (EaseOut over fixed time)
+ * Consider, Y axis simulating physics to arrive at a point (Physics equation over variable time)
+ * Currently, the axes have been split however, they both use the same EaseOut equation.
+ *
+ * @param[in] scrollView The main scrollview
+ * @param[in] rulerX The X ruler
+ * @param[in] rulerY The Y ruler
+ * @param[in] lockAxis Which axis (if any) is locked.
+ * @param[in] velocity Current pan velocity
+ * @param[in] maxOvershoot Maximum overshoot
+ * @param[in] inAcessibilityPan True if we are currently panning with accessibility
+ * @param[out] positionSnap The target position of snap animation
+ * @param[out] positionDuration The duration of the snap animation
+ * @param[out] alphaFunction The snap animation alpha function
+ * @param[out] isFlick if we are flicking or not
+ * @param[out] isFreeFlick if we are free flicking or not
+ */
+void SnapWithVelocity(
+ Dali::Toolkit::Internal::ScrollView& scrollView,
+ Dali::Toolkit::RulerPtr rulerX,
+ Dali::Toolkit::RulerPtr rulerY,
+ Dali::Toolkit::Internal::ScrollView::LockAxis lockAxis,
+ Vector2 velocity,
+ Vector2 maxOvershoot,
+ Vector2& positionSnap,
+ Vector2& positionDuration,
+ AlphaFunction& alphaFunction,
+ bool inAccessibilityPan,
+ bool& isFlick,
+ bool& isFreeFlick)
+{
+ // Animator takes over now, touches are assumed not to interfere.
+ // And if touches do interfere, then we'll stop animation, update PrePosition
+ // to current mScroll's properties, and then resume.
+ // Note: For Flicking this may work a bit different...
+
+ float angle = atan2(velocity.y, velocity.x);
+ float speed2 = velocity.LengthSquared();
+ float biasX = 0.5f;
+ float biasY = 0.5f;
+ FindDirection horizontal = FindDirection::None;
+ FindDirection vertical = FindDirection::None;
+
+ using LockAxis = Dali::Toolkit::Internal::ScrollView::LockAxis;
+
+ // orthoAngleRange = Angle tolerance within the Exact N,E,S,W direction
+ // that will be accepted as a general N,E,S,W flick direction.
+
+ const float orthoAngleRange = FLICK_ORTHO_ANGLE_RANGE * M_PI / 180.0f;
+ const float flickSpeedThreshold2 = scrollView.GetMinimumSpeedForFlick() * scrollView.GetMinimumSpeedForFlick();
+
+ // Flick logic X Axis
+
+ if(rulerX->IsEnabled() && lockAxis != LockAxis::LockHorizontal)
+ {
+ horizontal = FindDirection::All;
+
+ if(speed2 > flickSpeedThreshold2 || // exceeds flick threshold
+ inAccessibilityPan) // With AccessibilityPan its easier to move between snap positions
+ {
+ if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East
+ {
+ biasX = 0.0f, horizontal = FindDirection::Left;
+
+ // This guards against an error where no movement occurs, due to the flick finishing
+ // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
+ positionSnap.x += 1.0f;
+ }
+ else if((angle >= M_PI - orthoAngleRange) || (angle < -M_PI + orthoAngleRange)) // Swiping West
+ {
+ biasX = 1.0f, horizontal = FindDirection::Right;
+
+ // This guards against an error where no movement occurs, due to the flick finishing
+ // before the update-thread has advanced mScrollPostPosition past the the previous snap point.
+ positionSnap.x -= 1.0f;
+ }
+ }
+ }
+
+ // Flick logic Y Axis
+
+ if(rulerY->IsEnabled() && lockAxis != LockAxis::LockVertical)
+ {
+ vertical = FindDirection::All;
+
+ if(speed2 > flickSpeedThreshold2 || // exceeds flick threshold
+ inAccessibilityPan) // With AccessibilityPan its easier to move between snap positions
+ {
+ if((angle >= M_PI_2 - orthoAngleRange) && (angle < M_PI_2 + orthoAngleRange)) // Swiping South
+ {
+ biasY = 0.0f, vertical = FindDirection::Up;
+ }
+ else if((angle >= -M_PI_2 - orthoAngleRange) && (angle < -M_PI_2 + orthoAngleRange)) // Swiping North
+ {
+ biasY = 1.0f, vertical = FindDirection::Down;
+ }
+ }
+ }
+
+ // isFlick: Whether this gesture is a flick or not.
+ isFlick = (horizontal != FindDirection::All || vertical != FindDirection::All);
+ // isFreeFlick: Whether this gesture is a flick under free panning criteria.
+ isFreeFlick = velocity.LengthSquared() > (FREE_FLICK_SPEED_THRESHOLD * FREE_FLICK_SPEED_THRESHOLD);
+
+ if(isFlick || isFreeFlick)
+ {
+ positionDuration = Vector2::ONE * scrollView.GetScrollFlickDuration();
+ alphaFunction = scrollView.GetScrollFlickAlphaFunction();
+ }
+
+ // Calculate next positionSnap ////////////////////////////////////////////////////////////
+
+ if(scrollView.GetActorAutoSnap())
+ {
+ Vector3 size = scrollView.Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE);
+
+ Actor child = scrollView.FindClosestActorToPosition(Vector3(size.width * 0.5f, size.height * 0.5f, 0.0f), horizontal, vertical);
+
+ if(!child && isFlick)
+ {
+ // If we conducted a direction limited search and found no actor, then just snap to the closest actor.
+ child = scrollView.FindClosestActorToPosition(Vector3(size.width * 0.5f, size.height * 0.5f, 0.0f));
+ }
+
+ if(child)
+ {
+ Vector2 position = scrollView.Self().GetCurrentProperty<Vector2>(Toolkit::ScrollView::Property::SCROLL_POSITION);
+
+ // Get center-point of the Actor.
+ Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER);
+
+ if(rulerX->IsEnabled())
+ {
+ positionSnap.x = position.x - childPosition.x + size.width * 0.5f;
+ }
+ if(rulerY->IsEnabled())
+ {
+ positionSnap.y = position.y - childPosition.y + size.height * 0.5f;
+ }
+ }
+ }
+
+ Vector2 startPosition = positionSnap;
+ positionSnap.x = -rulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system.
+ positionSnap.y = -rulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT.
+
+ Dali::Toolkit::ClampState2D clamped;
+ Vector3 size = scrollView.Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE);
+ Vector2 clampDelta(Vector2::ZERO);
+ ClampPosition(size, rulerX, rulerY, positionSnap, clamped);
+
+ if((rulerX->GetType() == Dali::Toolkit::Ruler::FREE || rulerY->GetType() == Dali::Toolkit::Ruler::FREE) &&
+ isFreeFlick && !scrollView.GetActorAutoSnap())
+ {
+ // Calculate target position based on velocity of flick.
+
+ // a = Deceleration (Set to diagonal stage length * friction coefficient)
+ // u = Initial Velocity (Flick velocity)
+ // v = 0 (Final Velocity)
+ // t = Time (Velocity / Deceleration)
+ Vector2 stageSize = Stage::GetCurrent().GetSize();
+ float stageLength = Vector3(stageSize.x, stageSize.y, 0.0f).Length();
+ float a = (stageLength * scrollView.GetFrictionCoefficient());
+ Vector3 u = Vector3(velocity.x, velocity.y, 0.0f) * scrollView.GetFlickSpeedCoefficient();
+ float speed = u.Length();
+ u /= speed;
+
+ // TODO: Change this to a decay function. (faster you flick, the slower it should be)
+ speed = std::min(speed, stageLength * scrollView.GetMaxFlickSpeed());
+ u *= speed;
+ alphaFunction = ConstantDecelerationAlphaFunction;
+
+ float t = speed / a;
+
+ if(rulerX->IsEnabled() && rulerX->GetType() == Dali::Toolkit::Ruler::FREE)
+ {
+ positionSnap.x += t * u.x * 0.5f;
+ }
+
+ if(rulerY->IsEnabled() && rulerY->GetType() == Dali::Toolkit::Ruler::FREE)
+ {
+ positionSnap.y += t * u.y * 0.5f;
+ }
+
+ clampDelta = positionSnap;
+ ClampPosition(size, rulerX, rulerY, positionSnap, clamped);
+
+ if((positionSnap - startPosition).LengthSquared() > Math::MACHINE_EPSILON_0)
+ {
+ clampDelta -= positionSnap;
+ clampDelta.x = clampDelta.x > 0.0f ? std::min(clampDelta.x, maxOvershoot.x) : std::max(clampDelta.x, -maxOvershoot.x);
+ clampDelta.y = clampDelta.y > 0.0f ? std::min(clampDelta.y, maxOvershoot.y) : std::max(clampDelta.y, -maxOvershoot.y);
+ }
+ else
+ {
+ clampDelta = Vector2::ZERO;
+ }
+
+ // If Axis is Free and has velocity, then calculate time taken
+ // to reach target based on velocity in axis.
+ if(rulerX->IsEnabled() && rulerX->GetType() == Dali::Toolkit::Ruler::FREE)
+ {
+ float deltaX = fabsf(startPosition.x - positionSnap.x);
+
+ if(fabsf(u.x) > Math::MACHINE_EPSILON_1)
+ {
+ positionDuration.x = fabsf(deltaX / u.x);
+ }
+ else
+ {
+ positionDuration.x = 0;
+ }
+ }
+
+ if(rulerY->IsEnabled() && rulerY->GetType() == Dali::Toolkit::Ruler::FREE)
+ {
+ float deltaY = fabsf(startPosition.y - positionSnap.y);
+
+ if(fabsf(u.y) > Math::MACHINE_EPSILON_1)
+ {
+ positionDuration.y = fabsf(deltaY / u.y);
+ }
+ else
+ {
+ positionDuration.y = 0;
+ }
+ }
+ }
+
+ if(scrollView.IsOvershootEnabled())
+ {
+ // Scroll to the end of the overshoot only when overshoot is enabled.
+ positionSnap += clampDelta;
+ }
+}
+