X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fscrollable%2Fscroll-view%2Fscroll-view-impl.cpp;h=86e9173baa8602108479ceff4e9c87db36e76c99;hp=1acf2a63c58ff6aca2f2e7ca486179a0ba45bbf6;hb=906b5eed2f9ddd8c03ac4b7b4b6aee7547bab42d;hpb=e2eda444afbe82e9591fe198eef339227f90a616 diff --git a/dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.cpp b/dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.cpp index 1acf2a6..86e9173 100644 --- a/dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.cpp +++ b/dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.cpp @@ -1,33 +1,51 @@ -// -// Copyright (c) 2014 Samsung Electronics Co., Ltd. -// -// Licensed under the Flora License, Version 1.0 (the License); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://floralicense.org/license/ -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an AS IS BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -// INTERNAL INCLUDES -#include +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +// CLASS HEADER #include + +// EXTERNAL INCLUDES +#include // for strcmp +#include +#include +#include +#include +#include +#include +#include + +// INTERNAL INCLUDES +#include +#include #include -#include -#include #include +#include + +//#define ENABLED_SCROLL_STATE_LOGGING + +#ifdef ENABLED_SCROLL_STATE_LOGGING +#define DALI_LOG_SCROLL_STATE(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d " format "\n", __PRETTY_FUNCTION__, __LINE__, ## args) +#else +#define DALI_LOG_SCROLL_STATE(format, args...) +#endif // TODO: Change to two class system: -// 1. DraggableActor (is an actor which can be dragged anywhere/scaled/rotated, can be set to range using the ruler) +// 1. DraggableActor (is an actor which can be dragged anywhere, can be set to range using the ruler) // 2. ScrollView (contains a draggable actor that can a) be dragged in the negative X, and Y domain, b) has a hitArea for touches) -// TODO: Rotation -// TODO: Asymetrical scaling // TODO: external components (page and status overlays). // TODO: Orientation. // TODO: upgrade Vector2/3 to support returning Unit vectors, normals, & cross product (dot product is already provided) @@ -36,47 +54,30 @@ using namespace Dali; namespace { - -const int DEFAULT_REFRESH_INTERVAL_MILLISECONDS = 50; ///< Refresh rate TODO: Animation should have an update signal (and see item-view-impl) -const float FLICK_SPEED_THRESHOLD = 500.0f; ///< Flick threshold in pixels/ms -const float FREE_FLICK_SPEED_THRESHOLD = 200.0f; ///< Free-Flick threshold in pixels/ms -const float AUTOLOCK_AXIS_MINIMUM_DISTANCE2 = 100.0f; ///< Auto-lock axis after minimum distance squared. -const float FLICK_ORTHO_ANGLE_RANGE = 60.0f; ///< degrees. (if >45, then supports diagonal flicking) -const unsigned int MAXIMUM_NUMBER_OF_VALUES = 5; ///< Number of values to use for weighted pan calculation. -const Vector2 DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = Vector2(0.17f, 0.1f); ///< The step of horizontal scroll distance in the proportion of stage size for each mouse wheel event received. +const float DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f); ///< Default Drag-Release animation time. +const float DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f); ///< Default Drag-Flick animation time. +const float DEFAULT_SNAP_OVERSHOOT_DURATION(0.5f); ///< Default Overshoot snapping animation time. +const float DEFAULT_MAX_OVERSHOOT(100.0f); ///< Default maximum allowed overshoot in pixels + +const float DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f); ///< Default Axis-AutoLock gradient threshold. default is 0.36:1 (20 degrees) +const float DEFAULT_FRICTION_COEFFICIENT(1.0f); ///< Default Friction Co-efficient. (in stage diagonals per second) +const float DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f); ///< Default Flick speed coefficient (multiples input touch velocity) +const float DEFAULT_MAX_FLICK_SPEED(3.0f); ///< Default Maximum flick speed. (in stage diagonals per second) + +const Vector2 DEFAULT_MIN_FLICK_DISTANCE(30.0f, 30.0f); ///< minimum distance for pan before flick allowed +const float DEFAULT_MIN_FLICK_SPEED_THRESHOLD(500.0f); ///< Minimum pan speed required for flick in pixels/s +const float FREE_FLICK_SPEED_THRESHOLD = 200.0f; ///< Free-Flick threshold in pixels/ms +const float AUTOLOCK_AXIS_MINIMUM_DISTANCE2 = 100.0f; ///< Auto-lock axis after minimum distance squared. +const float FLICK_ORTHO_ANGLE_RANGE = 75.0f; ///< degrees. (if >45, then supports diagonal flicking) +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. const unsigned long MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET( 150u ); +const float TOUCH_DOWN_TIMER_INTERVAL = 100.0f; +const float DEFAULT_SCROLL_UPDATE_DISTANCE( 30.0f ); ///< Default distance to travel in pixels for scroll update signal -// predefined effect values -const Vector3 ANGLE_CAROUSEL_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f); -const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.2f, Math::PI * 0.2f, 0.0f); ///< Cube page rotates as if it has ten sides with the camera positioned inside -const Vector2 ANGLE_CUSTOM_CUBE_SWING(-Math::PI * 0.45f, -Math::PI * 0.45f); ///< outer cube pages swing 90 degrees as they pan offscreen -const Vector2 ANGLE_SPIRAL_SWING_IN(Math::PI * 0.5f, Math::PI * 0.5f); -const Vector2 ANGLE_SPIRAL_SWING_OUT(Math::PI * 0.35f, Math::PI * 0.35f); -const Vector2 ANGLE_OUTER_CUBE_SWING(Math::PI * 0.5f, Math::PI * 0.5f); ///< outer cube pages swing 90 degrees as they pan offscreen +const std::string INTERNAL_MAX_POSITION_PROPERTY_NAME( "internal-max-position" ); // Helpers //////////////////////////////////////////////////////////////////////////////////////// -// TODO: GetAngle for Vector2 can be moved. -// GetAngle for Vector3 needs to be measured against a normal/plane. - -/** - * @param[in] vector The 3D vector to be measured - * @return angle in radians from 0 to 2PI - */ -float GetAngle(const Vector3& vector) -{ - return atan2(vector.y, vector.x) + Math::PI; -} - -/** - * @param[in] vector The 2D vector to be measured - * @return angle in radians from 0 to 2PI - */ -float GetAngle(const Vector2& vector) -{ - return atan2(vector.y, vector.x) + Math::PI; -} - /** * Find the vector (distance) from (a) to (b) * in domain (start) to (end) @@ -180,24 +181,45 @@ float ConstantDecelerationAlphaFunction(float progress) * scroll domain. This is a value from 0.0f to 1.0f in each * scroll position axis. */ -Vector3 InternalRelativePositionConstraint(const Vector3& current, - const PropertyInput& scrollPositionProperty, - const PropertyInput& scrollMinProperty, - const PropertyInput& scrollMaxProperty, - const PropertyInput& scrollSizeProperty) +void InternalRelativePositionConstraint( Vector2& relativePosition, const PropertyInputContainer& inputs) { - const Vector3& position = -scrollPositionProperty.GetVector3(); - const Vector3& min = scrollMinProperty.GetVector3(); - const Vector3& max = scrollMaxProperty.GetVector3(); - const Vector3& size = scrollSizeProperty.GetVector3(); + Vector2 position = -inputs[0]->GetVector2(); + const Vector2& min = inputs[1]->GetVector2(); + const Vector2& max = inputs[2]->GetVector2(); + const Vector3& size = inputs[3]->GetVector3(); - Vector3 relativePosition; - Vector3 domainSize = (max - min) - size; + position.x = WrapInDomain(position.x, min.x, max.x); + position.y = WrapInDomain(position.y, min.y, max.y); + + Vector2 domainSize = (max - min) - size.GetVectorXY(); relativePosition.x = domainSize.x > Math::MACHINE_EPSILON_1 ? fabsf((position.x - min.x) / domainSize.x) : 0.0f; relativePosition.y = domainSize.y > Math::MACHINE_EPSILON_1 ? fabsf((position.y - min.y) / domainSize.y) : 0.0f; +} + +/** + * Internal scroll domain Constraint + * Generates the scroll domain of the scroll view. + */ +void InternalScrollDomainConstraint( Vector2& scrollDomain, const PropertyInputContainer& inputs) +{ + const Vector2& min = inputs[0]->GetVector2(); + const Vector2& max = inputs[1]->GetVector2(); + const Vector3& size = inputs[2]->GetVector3(); - return relativePosition; + scrollDomain = (max - min) - size.GetVectorXY(); +} + +/** + * Internal maximum scroll position Constraint + * Generates the maximum scroll position of the scroll view. + */ +void InternalPrePositionMaxConstraint( Vector2& scrollMax, const PropertyInputContainer& inputs) +{ + const Vector2& max = inputs[0]->GetVector2(); + const Vector3& size = inputs[1]->GetVector3(); + + scrollMax = max - size.GetVectorXY(); } } // unnamed namespace @@ -214,113 +236,182 @@ namespace Internal namespace { +BaseHandle Create() +{ + return Toolkit::ScrollView::New(); +} + +// Setup properties, signals and actions using the type-registry. +DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ScrollView, Toolkit::Scrollable, Create ) + +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-position", VECTOR2, SCROLL_POSITION) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position", VECTOR2, SCROLL_PRE_POSITION) +DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position-x", SCROLL_PRE_POSITION_X, SCROLL_PRE_POSITION, 0) +DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position-y", SCROLL_PRE_POSITION_Y, SCROLL_PRE_POSITION, 1) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position-max", VECTOR2, SCROLL_PRE_POSITION_MAX) +DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position-max-x", SCROLL_PRE_POSITION_MAX_X, SCROLL_PRE_POSITION_MAX, 0) +DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-pre-position-max-y", SCROLL_PRE_POSITION_MAX_Y, SCROLL_PRE_POSITION_MAX, 1) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "overshoot-x", FLOAT, OVERSHOOT_X) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "overshoot-y", FLOAT, OVERSHOOT_Y) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-final", VECTOR2, SCROLL_FINAL) +DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-final-x", SCROLL_FINAL_X, SCROLL_FINAL,0) +DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-final-y", SCROLL_FINAL_Y, SCROLL_FINAL,1) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "wrap", BOOLEAN, WRAP) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "panning", BOOLEAN, PANNING) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scrolling", BOOLEAN, SCROLLING) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-domain-size", VECTOR2, SCROLL_DOMAIN_SIZE) +DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-domain-size-x", SCROLL_DOMAIN_SIZE_X, SCROLL_DOMAIN_SIZE, 0) +DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, ScrollView, "scroll-domain-size-y", SCROLL_DOMAIN_SIZE_Y, SCROLL_DOMAIN_SIZE, 1) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-domain-offset", VECTOR2, SCROLL_DOMAIN_OFFSET) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "scroll-position-delta", VECTOR2, SCROLL_POSITION_DELTA) +DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ScrollView, "start-page-position", VECTOR3, START_PAGE_POSITION) + +DALI_SIGNAL_REGISTRATION( Toolkit, ScrollView, "value-changed", SIGNAL_SNAP_STARTED ) + +DALI_TYPE_REGISTRATION_END() + +/** + * Returns whether to lock scrolling to a particular axis + * + * @param[in] panDelta Distance panned since gesture started + * @param[in] currentLockAxis The current lock axis value + * @param[in] lockGradient How quickly to lock to a particular axis + * + * @return The new axis lock state + */ +ScrollView::LockAxis GetLockAxis(const Vector2& panDelta, ScrollView::LockAxis currentLockAxis, float lockGradient) +{ + if(panDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 && + currentLockAxis == ScrollView::LockPossible) + { + float dx = fabsf(panDelta.x); + float dy = fabsf(panDelta.y); + if(dx * lockGradient >= dy) + { + // 0.36:1 gradient to the horizontal (deviate < 20 degrees) + currentLockAxis = ScrollView::LockVertical; + } + else if(dy * lockGradient > dx) + { + // 0.36:1 gradient to the vertical (deviate < 20 degrees) + currentLockAxis = ScrollView::LockHorizontal; + } + else + { + currentLockAxis = ScrollView::LockNone; + } + } + return currentLockAxis; +} + /** * Internal Pre-Position Property Constraint. * * Generates position property based on current position + gesture displacement. * Or generates position property based on positionX/Y. * Note: This is the position prior to any clamping at scroll boundaries. - * TODO: Scale & Rotation Transforms. */ struct InternalPrePositionConstraint { - InternalPrePositionConstraint(const Vector2& initialPanMask, + InternalPrePositionConstraint(const Vector2& initialPanPosition, + const Vector2& initialPanMask, bool axisAutoLock, - float axisAutoLockGradient) - : mInitialPanMask(initialPanMask), - mAxisAutoLock(axisAutoLock), - mLockAxis(ScrollView::LockPossible), + float axisAutoLockGradient, + ScrollView::LockAxis initialLockAxis, + const Vector2& maxOvershoot, + const RulerDomain& domainX, const RulerDomain& domainY) + : mLocalStart(initialPanPosition), + mInitialPanMask(initialPanMask), + mDomainMin( -domainX.min, -domainY.min ), + mDomainMax( -domainX.max, -domainY.max ), + mMaxOvershoot(maxOvershoot), mAxisAutoLockGradient(axisAutoLockGradient), - mPrePosition(Vector3::ZERO), - mWasPanning(false) + mLockAxis(initialLockAxis), + mAxisAutoLock(axisAutoLock), + mWasPanning(false), + mClampX( domainX.enabled ), + mClampY( domainY.enabled ) { } - Vector3 operator()(const Vector3& current, - const PropertyInput& gesturePositionProperty, - const PropertyInput& gestureDisplacementProperty, - const PropertyInput& scrollPositionXProperty, - const PropertyInput& scrollPositionYProperty, - const PropertyInput& panningProperty) + void operator()( Vector2& scrollPostPosition, const PropertyInputContainer& inputs ) { - const bool panning = panningProperty.GetBoolean(); - Vector3 scrollPostPosition; + const Vector2& panPosition = inputs[0]->GetVector2(); - if(panning) + if(!mWasPanning) { - // Check if panning has just started... - if(!mWasPanning) - { - mLocalStart = gesturePositionProperty.GetVector2() - gestureDisplacementProperty.GetVector2(); - mPrePosition = current; - mLockAxis = ScrollView::LockPossible; + mPrePosition = scrollPostPosition; + mCurrentPanMask = mInitialPanMask; + mWasPanning = true; + } - mCurrentPanMask = mInitialPanMask; - } + // Calculate Deltas... + const Vector2& currentPosition = panPosition; + Vector2 panDelta( currentPosition - mLocalStart ); - // Calculate Deltas... - Vector2 currentPosition = gesturePositionProperty.GetVector2(); - Vector2 panDelta( currentPosition - mLocalStart ); + // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan + // appears mostly horizontal or mostly vertical respectively... + if( mAxisAutoLock ) + { + mLockAxis = GetLockAxis(panDelta, mLockAxis, mAxisAutoLockGradient); + if( mLockAxis == ScrollView::LockVertical ) + { + mCurrentPanMask.y = 0.0f; + } + else if( mLockAxis == ScrollView::LockHorizontal ) + { + mCurrentPanMask.x = 0.0f; + } + } - // Axis Auto Lock - locks the panning to the horizontal or vertical axis if the pan - // appears mostly horizontal or mostly vertical respectively... - AxisAutoLock(panDelta); + // Restrict deltas based on ruler enable/disable and axis-lock state... + panDelta *= mCurrentPanMask; - // Restrict deltas based on ruler enable/disable and axis-lock state... - panDelta *= mCurrentPanMask; + // Perform Position transform based on input deltas... + scrollPostPosition = mPrePosition; + scrollPostPosition += panDelta; - // Perform Position transform based on input deltas... - scrollPostPosition = mPrePosition; - scrollPostPosition.GetVectorXY() += panDelta; - } - else + // if no wrapping then clamp preposition to maximum overshoot amount + const Vector3& size = inputs[1]->GetVector3(); + if( mClampX ) { - scrollPostPosition.x = scrollPositionXProperty.GetFloat(); - scrollPostPosition.y = scrollPositionYProperty.GetFloat(); + float newXPosition = Clamp(scrollPostPosition.x, (mDomainMax.x + size.x) - mMaxOvershoot.x, mDomainMin.x + mMaxOvershoot.x ); + if( (newXPosition < scrollPostPosition.x - Math::MACHINE_EPSILON_1) + || (newXPosition > scrollPostPosition.x + Math::MACHINE_EPSILON_1) ) + { + mPrePosition.x = newXPosition; + mLocalStart.x = panPosition.x; + } + scrollPostPosition.x = newXPosition; } - - mWasPanning = panning; - return scrollPostPosition; - } - - void AxisAutoLock(Vector2& panDelta) - { - if(mAxisAutoLock) + if( mClampY ) { - if(panDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 && - mLockAxis == ScrollView::LockPossible) + float newYPosition = Clamp(scrollPostPosition.y, (mDomainMax.y + size.y) - mMaxOvershoot.y, mDomainMin.y + mMaxOvershoot.y ); + if( (newYPosition < scrollPostPosition.y - Math::MACHINE_EPSILON_1) + || (newYPosition > scrollPostPosition.y + Math::MACHINE_EPSILON_1) ) { - float dx = fabsf(panDelta.x); - float dy = fabsf(panDelta.y); - if(dx * mAxisAutoLockGradient >= dy) - { - // 0.36:1 gradient to the horizontal (deviate < 20 degrees) - mLockAxis = ScrollView::LockVertical; - mCurrentPanMask.y = 0.0f; - } - else if(dy * mAxisAutoLockGradient > dx) - { - // 0.36:1 gradient to the vertical (deviate < 20 degrees) - mLockAxis = ScrollView::LockHorizontal; - mCurrentPanMask.x = 0.0f; - } - else - { - mLockAxis = ScrollView::LockNone; - } + mPrePosition.y = newYPosition; + mLocalStart.y = panPosition.y; } - } // end if mAxisAutoLock + scrollPostPosition.y = newYPosition; + } } + Vector2 mPrePosition; Vector2 mLocalStart; Vector2 mInitialPanMask; ///< Initial pan mask (based on ruler settings) Vector2 mCurrentPanMask; ///< Current pan mask that can be altered by axis lock mode. + Vector2 mDomainMin; + Vector2 mDomainMax; + Vector2 mMaxOvershoot; - bool mAxisAutoLock; ///< Set by ScrollView - ScrollView::LockAxis mLockAxis; float mAxisAutoLockGradient; ///< Set by ScrollView - Vector3 mPrePosition; - bool mWasPanning; + ScrollView::LockAxis mLockAxis; + + bool mAxisAutoLock:1; ///< Set by ScrollView + bool mWasPanning:1; + bool mClampX:1; + bool mClampY:1; }; /** @@ -332,113 +423,106 @@ struct InternalPrePositionConstraint */ struct InternalPositionConstraint { - InternalPositionConstraint(const RulerDomain& domainX, const RulerDomain& domainY) + InternalPositionConstraint(const RulerDomain& domainX, const RulerDomain& domainY, bool wrap) : mDomainMin( -domainX.min, -domainY.min ), mDomainMax( -domainX.max, -domainY.max ), mClampX( domainX.enabled ), - mClampY( domainY.enabled ) + mClampY( domainY.enabled ), + mWrap( wrap ) { } - Vector3 operator()(const Vector3& current, - const PropertyInput& scrollPositionProperty, - const PropertyInput& scrollSizeProperty) + void operator()( Vector2& position, const PropertyInputContainer& inputs ) { - Vector3 position = scrollPositionProperty.GetVector3(); - const Vector2& size = scrollSizeProperty.GetVector3().GetVectorXY(); - - position.x = mClampX ? Clamp(position.x, mDomainMax.x + size.x, mDomainMin.x ) : position.x; - position.y = mClampY ? Clamp(position.y, mDomainMax.y + size.y, mDomainMin.y ) : position.y; + position = inputs[0]->GetVector2(); + const Vector2& size = inputs[3]->GetVector3().GetVectorXY(); + const Vector2& min = inputs[1]->GetVector2(); + const Vector2& max = inputs[2]->GetVector2(); - return position; + if( mWrap ) + { + position.x = -WrapInDomain(-position.x, min.x, max.x); + position.y = -WrapInDomain(-position.y, min.y, max.y); + } + else + { + // clamp post position to domain + position.x = mClampX ? Clamp(position.x, mDomainMax.x + size.x, mDomainMin.x ) : position.x; + position.y = mClampY ? Clamp(position.y, mDomainMax.y + size.y, mDomainMin.y ) : position.y; + } } Vector2 mDomainMin; Vector2 mDomainMax; bool mClampX; bool mClampY; + bool mWrap; }; /** * This constraint updates the X overshoot property using the difference - * mPropertyPrePosition.x and mPropertyPosition.x, returning a relative value between 0.0f and 1.0f + * SCROLL_PRE_POSITION.x and SCROLL_POSITION.x, returning a relative value between 0.0f and 1.0f */ struct OvershootXConstraint { - OvershootXConstraint(float maxOvershoot) : mLastOvershoot(0.0f), mMaxOvershoot(maxOvershoot) {} + OvershootXConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {} - float operator()(const float& current, - const PropertyInput& scrollPrePositionProperty, - const PropertyInput& scrollPostPositionProperty) + void operator()( float& current, const PropertyInputContainer& inputs ) { - Vector3 scrollPrePosition = scrollPrePositionProperty.GetVector3(); - Vector3 scrollPostPosition = scrollPostPositionProperty.GetVector3(); - float newOvershoot = scrollPrePosition.x - scrollPostPosition.x; - return (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot; + if( inputs[2]->GetBoolean() ) + { + const Vector2& scrollPrePosition = inputs[0]->GetVector2(); + const Vector2& scrollPostPosition = inputs[1]->GetVector2(); + float newOvershoot = scrollPrePosition.x - scrollPostPosition.x; + current = (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot; + } + else + { + current = 0.0f; + } } - float mLastOvershoot; float mMaxOvershoot; }; /** * This constraint updates the Y overshoot property using the difference - * mPropertyPrePosition.y and mPropertyPosition.y, returning a relative value between 0.0f and 1.0f + * SCROLL_PRE_POSITION.y and SCROLL_POSITION.y, returning a relative value between 0.0f and 1.0f */ struct OvershootYConstraint { - OvershootYConstraint(float maxOvershoot) : mLastOvershoot(0.0f), mMaxOvershoot(maxOvershoot) {} + OvershootYConstraint(float maxOvershoot) : mMaxOvershoot(maxOvershoot) {} - float operator()(const float& current, - const PropertyInput& scrollPrePositionProperty, - const PropertyInput& scrollPostPositionProperty) + void operator()( float& current, const PropertyInputContainer& inputs ) { - Vector3 scrollPrePosition = scrollPrePositionProperty.GetVector3(); - Vector3 scrollPostPosition = scrollPostPositionProperty.GetVector3(); - float newOvershoot = scrollPrePosition.y - scrollPostPosition.y; - return (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot; + if( inputs[2]->GetBoolean() ) + { + const Vector2& scrollPrePosition = inputs[0]->GetVector2(); + const Vector2& scrollPostPosition = inputs[1]->GetVector2(); + float newOvershoot = scrollPrePosition.y - scrollPostPosition.y; + current = (newOvershoot > 0.0f ? std::min(newOvershoot, mMaxOvershoot) : std::max(newOvershoot, -mMaxOvershoot)) / mMaxOvershoot; + } + else + { + current = 0.0f; + } } - float mLastOvershoot; float mMaxOvershoot; }; /** - * When panning, this constraint updates the X property, otherwise - * it has no effect on the X property. - */ -float InternalXConstraint(const float& current, - const PropertyInput& scrollPosition, - const PropertyInput& panningProperty) -{ - return scrollPosition.GetVector3().x; -} - -/** - * When panning, this constraint updates the Y property, otherwise - * it has no effect on the Y property. - */ -float InternalYConstraint(const float& current, - const PropertyInput& scrollPosition, - const PropertyInput& panningProperty) -{ - return scrollPosition.GetVector3().y; -} - -/** * Internal Position-Delta Property Constraint. * * Generates position-delta property based on scroll-position + scroll-offset properties. */ -Vector3 InternalPositionDeltaConstraint(const Vector3& current, - const PropertyInput& scrollPositionProperty, - const PropertyInput& scrollOffsetProperty) +void InternalPositionDeltaConstraint( Vector2& current, const PropertyInputContainer& inputs ) { - const Vector3& scrollPosition = scrollPositionProperty.GetVector3(); - const Vector3& scrollOffset = scrollOffsetProperty.GetVector3(); + const Vector2& scrollPosition = inputs[0]->GetVector2(); + const Vector2& scrollOffset = inputs[1]->GetVector2(); - return scrollPosition + scrollOffset; + current = scrollPosition + scrollOffset; } /** @@ -450,41 +534,27 @@ Vector3 InternalPositionDeltaConstraint(const Vector3& current, */ struct InternalFinalConstraint { - InternalFinalConstraint(AlphaFunction functionX, - AlphaFunction functionY) + InternalFinalConstraint(AlphaFunctionPrototype functionX, + AlphaFunctionPrototype functionY) : mFunctionX(functionX), mFunctionY(functionY) { } - Vector3 operator()(const Vector3& current, - const PropertyInput& scrollPositionProperty, - const PropertyInput& scrollOvershootXProperty, - const PropertyInput& scrollOvershootYProperty) + void operator()( Vector2& current, const PropertyInputContainer& inputs ) { - const float& overshootx = scrollOvershootXProperty.GetFloat(); - const float& overshooty = scrollOvershootYProperty.GetFloat(); - Vector3 offset( mFunctionX(overshootx), - mFunctionY(overshooty), - 0.0f); + const float& overshootx = inputs[1]->GetFloat(); + const float& overshooty = inputs[2]->GetFloat(); + Vector2 offset( mFunctionX(overshootx), + mFunctionY(overshooty) ); - return scrollPositionProperty.GetVector3() - offset; + current = inputs[0]->GetVector2() - offset; } - AlphaFunction mFunctionX; - AlphaFunction mFunctionY; + AlphaFunctionPrototype mFunctionX; + AlphaFunctionPrototype mFunctionY; }; - -BaseHandle Create() -{ - return Toolkit::ScrollView::New(); -} - -TypeRegistration typeRegistration( typeid(Toolkit::ScrollView), typeid(Toolkit::Scrollable), Create ); - -SignalConnectorType signalConnector1( typeRegistration, Toolkit::ScrollView::SIGNAL_SNAP_STARTED, &ScrollView::DoConnectSignal ); - } @@ -508,121 +578,111 @@ Dali::Toolkit::ScrollView ScrollView::New() } ScrollView::ScrollView() -: ScrollBase(), +: ScrollBase( ControlBehaviour( REQUIRES_WHEEL_EVENTS ) ), // Enable size negotiation + mTouchDownTime(0u), + mGestureStackDepth(0), + mScrollStateFlags(0), + mLockAxis(LockPossible), + mScrollUpdateDistance(DEFAULT_SCROLL_UPDATE_DISTANCE), + mMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT), + mUserMaxOvershoot(DEFAULT_MAX_OVERSHOOT, DEFAULT_MAX_OVERSHOOT), + mSnapOvershootDuration(DEFAULT_SNAP_OVERSHOOT_DURATION), + mSnapOvershootAlphaFunction(AlphaFunction::EASE_OUT), + mSnapDuration(DEFAULT_SLOW_SNAP_ANIMATION_DURATION), + mSnapAlphaFunction(AlphaFunction::EASE_OUT), + mMinFlickDistance(DEFAULT_MIN_FLICK_DISTANCE), + mFlickSpeedThreshold(DEFAULT_MIN_FLICK_SPEED_THRESHOLD), + mFlickDuration(DEFAULT_FAST_SNAP_ANIMATION_DURATION), + mFlickAlphaFunction(AlphaFunction::EASE_OUT), + mAxisAutoLockGradient(DEFAULT_AXIS_AUTO_LOCK_GRADIENT), + mFrictionCoefficient(DEFAULT_FRICTION_COEFFICIENT), + mFlickSpeedCoefficient(DEFAULT_FLICK_SPEED_COEFFICIENT), + mMaxFlickSpeed(DEFAULT_MAX_FLICK_SPEED), + mWheelScrollDistanceStep(Vector2::ZERO), + mInAccessibilityPan(false), mInitialized(false), mScrolling(false), mScrollInterrupted(false), - mTouchDownTime(0u), + mPanning(false), mSensitive(true), - mGestureStackDepth(0), - mRotationDelta(0.0f), - mScrollPreRotation(0.0f), - mScrollPostRotation(0.0f), - mTouchDownReceived(false), + mTouchDownTimeoutReached(false), mActorAutoSnapEnabled(false), mAutoResizeContainerEnabled(false), mWrapMode(false), mAxisAutoLock(false), - mMinTouchesForPanning(1), - mMaxTouchesForPanning(1), - mLockAxis(LockPossible), - mRefreshIntervalMilliseconds(DEFAULT_REFRESH_INTERVAL_MILLISECONDS), mAlterChild(false), - mOvershootDelay(1.0f), - mMaxOvershoot(Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT, Toolkit::ScrollView::DEFAULT_MAX_OVERSHOOT), mDefaultMaxOvershoot(true), - mSnapOvershootDuration(Toolkit::ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION), - mSnapOvershootAlphaFunction(AlphaFunctions::EaseOut), - mSnapDuration(Toolkit::ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION), - mSnapAlphaFunction(AlphaFunctions::EaseOut), - mFlickDuration(Toolkit::ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION), - mFlickAlphaFunction(AlphaFunctions::EaseOut), - mAxisAutoLockGradient(Toolkit::ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT), - mFrictionCoefficient(Toolkit::ScrollView::DEFAULT_FRICTION_COEFFICIENT), - mFlickSpeedCoefficient(Toolkit::ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT), - mMaxFlickSpeed(Toolkit::ScrollView::DEFAULT_MAX_FLICK_SPEED) + mCanScrollHorizontal(true), + mCanScrollVertical(true) { - SetRequiresMouseWheelEvents(true); } void ScrollView::OnInitialize() { Actor self = Self(); - self.SetLeaveRequired(true); // Internal Actor, used to hide actors from enumerations. // Also actors added to Internal actor appear as overlays e.g. ScrollBar components. mInternalActor = Actor::New(); - mInternalActor.SetDrawMode(DrawMode::OVERLAY); self.Add(mInternalActor); - mInternalActor.ApplyConstraint( Constraint::New( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) ); + mInternalActor.SetParentOrigin(ParentOrigin::CENTER); mInternalActor.SetAnchorPoint(AnchorPoint::CENTER); + mInternalActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); mAlterChild = true; - // Register Scroll Properties. - RegisterProperties(); + mScrollPostPosition = mScrollPrePosition = Vector2::ZERO; - mScrollPostPosition = mScrollPrePosition = Vector3::ZERO; - mScrollPostScale = mScrollPreScale = Vector3::ONE; - mScrollPostRotation = mScrollPreRotation = 0.0f; - - mMouseWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION; + mWheelScrollDistanceStep = Stage::GetCurrent().GetSize() * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION; mInitialized = true; mGestureStackDepth = 0; - EnableGestureDetection( Gesture::Type( Gesture::Pan | Gesture::Pinch ) ); - - // For pan, default to only 1 touch required, ignoring touches outside this range. - SetTouchesRequiredForPanning(1, 1, false); + EnableGestureDetection( Gesture::Type( Gesture::Pan ) ); // By default we'll allow the user to freely drag the scroll view, // while disabling the other rulers. RulerPtr ruler = new DefaultRuler(); - RulerPtr rulerDisabled = new DefaultRuler(); - rulerDisabled->Disable(); mRulerX = ruler; mRulerY = ruler; - mRulerScaleX = rulerDisabled; - mRulerScaleY = rulerDisabled; - mRulerRotation = rulerDisabled; - EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator); + SetOvershootEnabled(true); - Vector3 size = GetControlSize(); - UpdatePropertyDomain(size); + self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, mCanScrollVertical); + self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, mCanScrollHorizontal); + + UpdatePropertyDomain(); SetInternalConstraints(); } -void ScrollView::OnControlStageConnection() +void ScrollView::OnStageConnection( int depth ) { + DALI_LOG_SCROLL_STATE("[0x%X]", this); + if ( mSensitive ) { SetScrollSensitive( false ); SetScrollSensitive( true ); } - if(IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator)) + if(IsOvershootEnabled()) { // try and make sure property notifications are set - EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator); + EnableScrollOvershoot(true); } } -void ScrollView::OnControlStageDisconnection() +void ScrollView::OnStageDisconnection() { - if ( mSnapOvershootAnimation ) - { - SetOvershootToOrigin(); - } + DALI_LOG_SCROLL_STATE("[0x%X]", this); StopAnimation(); } ScrollView::~ScrollView() { + DALI_LOG_SCROLL_STATE("[0x%X]", this); } AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const @@ -689,84 +749,6 @@ void ScrollView::ApplyEffect(Toolkit::ScrollViewEffect effect) GetImpl(effect).Attach(self); } -Toolkit::ScrollViewEffect ScrollView::ApplyEffect(Toolkit::ScrollView::PageEffect effect) -{ - Toolkit::ScrollViewEffect scrollEffect; - switch(effect) - { - case Toolkit::ScrollView::PageEffectNone: - { - break; - } - case Toolkit::ScrollView::PageEffectOuterCube: - { - Toolkit::ScrollViewCustomEffect customEffect; - scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New(); - Vector2 pageSize = Stage::GetCurrent().GetSize(); - // set the page translation to the slide off distance, also add an extra value to space the pages, having a smaller spacing on translationOut will allow the spacing to reduce over time - // the page moving onto screen will start 50.0f further out (1.0f * 50.0f) and the spacing will reduce as its position reaches the centre (0.0f * 50.0f) - // the page moving off screen will slowly build a spacing from 0.0f to 20.0f - // the spacing from each page is added together for the final spacing between the two pages. - customEffect.SetPageTranslation(Vector3(pageSize.x, pageSize.y, 0) + Vector3(50.0f, 50.0f, 0.0f), Vector3(pageSize.x, pageSize.y, 0) + Vector3(20.0f, 20.0f, 0.0f)); - customEffect.SetSwingAngleOut(ANGLE_CUSTOM_CUBE_SWING.x, Vector3(0.0f, -1.0f, 0.0f)); - customEffect.SetSwingAnchor(AnchorPoint::CENTER, AnchorPoint::CENTER_LEFT); - customEffect.SetOpacityThreshold(0.7f); - break; - } - case Toolkit::ScrollView::PageEffectDepth: - { - Toolkit::ScrollViewCustomEffect customEffect; - scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New(); - break; - } - case Toolkit::ScrollView::PageEffectInnerCube: - { - Toolkit::ScrollViewCustomEffect customEffect; - scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New(); - customEffect.SetPageSpacing(Vector2(30.0f, 30.0f)); - customEffect.SetAngledOriginPageRotation(ANGLE_CUBE_PAGE_ROTATE); - customEffect.SetSwingAngle(ANGLE_CUBE_PAGE_ROTATE.x, Vector3(0,-1,0)); - customEffect.SetOpacityThreshold(0.5f); - break; - } - case Toolkit::ScrollView::PageEffectCarousel: - { - Toolkit::ScrollViewCustomEffect customEffect; - scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New(); - customEffect.SetPageTranslation(Vector3(0,0,0), Vector3(-30, 0, 0)); - customEffect.SetPageSpacing(Vector2(60.0f, 60.0f)); - customEffect.SetAngledOriginPageRotation(-ANGLE_CUBE_PAGE_ROTATE); - customEffect.SetOpacityThreshold(0.2f, 0.6f); - break; - } - case Toolkit::ScrollView::PageEffectSpiral: - { - Toolkit::ScrollViewCustomEffect customEffect; - scrollEffect = customEffect = Toolkit::ScrollViewCustomEffect::New(); - - Vector2 pageSize = Stage::GetCurrent().GetSize(); - customEffect.SetSwingAngle(-ANGLE_SPIRAL_SWING_IN.x, Vector3(0.0f, -1.0f, 0.0f), ANGLE_SPIRAL_SWING_OUT.x, Vector3(0.0f, -1.0f, 0.0f)); - //customEffect.SetSwingAngleAlphaFunctionOut(AlphaFunctions::EaseOut); - customEffect.SetSwingAnchor(AnchorPoint::CENTER_RIGHT); - customEffect.SetPageTranslation(Vector3(pageSize.x, pageSize.y, 0) + Vector3(100.0f, 100.0f, 0.0f), Vector3(pageSize.x, pageSize.y, -pageSize.y * 2.0f) * 0.33f); - //customEffect.SetPageTranslateAlphaFunctionOut(AlphaFunctions::EaseOut); - customEffect.SetOpacityThreshold(0.75f, 0.6f); - customEffect.SetOpacityAlphaFunctionIn(AlphaFunctions::EaseInOut); - break; - } - default: - { - DALI_ASSERT_DEBUG(0 && "unknown scroll view effect"); - } - } - RemoveConstraintsFromChildren(); - if(scrollEffect) - { - ApplyEffect(scrollEffect); - } - return scrollEffect; -} - void ScrollView::RemoveEffect(Toolkit::ScrollViewEffect effect) { Dali::Toolkit::ScrollView self = Dali::Toolkit::ScrollView::DownCast(Self()); @@ -829,8 +811,7 @@ void ScrollView::SetRulerX(RulerPtr ruler) { mRulerX = ruler; - Vector3 size = GetControlSize(); - UpdatePropertyDomain(size); + UpdatePropertyDomain(); UpdateMainInternalConstraint(); } @@ -838,103 +819,141 @@ void ScrollView::SetRulerY(RulerPtr ruler) { mRulerY = ruler; - Vector3 size = GetControlSize(); - UpdatePropertyDomain(size); + UpdatePropertyDomain(); UpdateMainInternalConstraint(); } -void ScrollView::UpdatePropertyDomain(const Vector3& size) +void ScrollView::UpdatePropertyDomain() { - Vector3 min; - Vector3 max; + Actor self = Self(); + Vector3 size = self.GetTargetSize(); + Vector2 min = mMinScroll; + Vector2 max = mMaxScroll; + bool scrollPositionChanged = false; + bool domainChanged = false; bool canScrollVertical = false; bool canScrollHorizontal = false; - Actor self = Self(); + UpdateLocalScrollProperties(); if(mRulerX->IsEnabled()) { const Toolkit::RulerDomain& rulerDomain = mRulerX->GetDomain(); - min.x = rulerDomain.min; - max.x = rulerDomain.max; + if( fabsf(min.x - rulerDomain.min) > Math::MACHINE_EPSILON_100 + || fabsf(max.x - rulerDomain.max) > Math::MACHINE_EPSILON_100 ) + { + domainChanged = true; + min.x = rulerDomain.min; + max.x = rulerDomain.max; - // make sure new scroll value is within new domain - float newScroll = min.x; - int scrollXPropertyIndex = self.GetPropertyIndex(Toolkit::ScrollView::SCROLL_X_PROPERTY_NAME); - if((fabsf(max.x - min.x) - size.x) > Math::MACHINE_EPSILON_1) + // make sure new scroll value is within new domain + if( mScrollPrePosition.x < min.x + || mScrollPrePosition.x > max.x ) + { + scrollPositionChanged = true; + mScrollPrePosition.x = Clamp(mScrollPrePosition.x, -(max.x - size.x), -min.x); + } + } + if( (fabsf(rulerDomain.max - rulerDomain.min) - size.x) > Math::MACHINE_EPSILON_100 ) { canScrollHorizontal = true; - float currentScroll = self.GetProperty(scrollXPropertyIndex); - newScroll = Clamp(currentScroll, -(max.x - size.x), -min.x); } - self.SetProperty(scrollXPropertyIndex, newScroll); + } + else if( fabs(min.x) > Math::MACHINE_EPSILON_100 + || fabs(max.x) > Math::MACHINE_EPSILON_100 ) + { + // need to reset to 0 + domainChanged = true; + min.x = 0.0f; + max.x = 0.0f; + canScrollHorizontal = false; } if(mRulerY->IsEnabled()) { const Toolkit::RulerDomain& rulerDomain = mRulerY->GetDomain(); - min.y = rulerDomain.min; - max.y = rulerDomain.max; + if( fabsf(min.y - rulerDomain.min) > Math::MACHINE_EPSILON_100 + || fabsf(max.y - rulerDomain.max) > Math::MACHINE_EPSILON_100 ) + { + domainChanged = true; + min.y = rulerDomain.min; + max.y = rulerDomain.max; - // make sure new scroll value is within new domain - float newScroll = min.y; - int scrollYPropertyIndex = self.GetPropertyIndex(Toolkit::ScrollView::SCROLL_Y_PROPERTY_NAME); - if((fabsf(max.y - min.y) - size.y) > Math::MACHINE_EPSILON_1) + // make sure new scroll value is within new domain + if( mScrollPrePosition.y < min.y + || mScrollPrePosition.y > max.y ) + { + scrollPositionChanged = true; + mScrollPrePosition.y = Clamp(mScrollPrePosition.y, -(max.y - size.y), -min.y); + } + } + if( (fabsf(rulerDomain.max - rulerDomain.min) - size.y) > Math::MACHINE_EPSILON_100 ) { canScrollVertical = true; - float currentScroll = self.GetProperty(scrollYPropertyIndex); - newScroll = Clamp(currentScroll, -(max.y - size.y), -min.y); } - self.SetProperty(scrollYPropertyIndex, newScroll); } - self.SetProperty(mPropertyCanScrollVertical, canScrollVertical); - self.SetProperty(mPropertyCanScrollHorizontal, canScrollHorizontal); - - self.SetProperty(mPropertyPositionMin, min ); - self.SetProperty(mPropertyPositionMax, max ); -} - -void ScrollView::SetRulerScaleX(RulerPtr ruler) -{ - mRulerScaleX = ruler; - UpdateMainInternalConstraint(); -} - -void ScrollView::SetRulerScaleY(RulerPtr ruler) -{ - mRulerScaleY = ruler; - UpdateMainInternalConstraint(); -} + else if( fabs(min.y) > Math::MACHINE_EPSILON_100 + || fabs(max.y) > Math::MACHINE_EPSILON_100 ) + { + // need to reset to 0 + domainChanged = true; + min.y = 0.0f; + max.y = 0.0f; + canScrollVertical = false; + } -void ScrollView::SetRulerRotation(RulerPtr ruler) -{ - mRulerRotation = ruler; - UpdateMainInternalConstraint(); + // avoid setting properties if possible, otherwise this will cause an entire update as well as triggering constraints using each property we update + if( mCanScrollVertical != canScrollVertical ) + { + mCanScrollVertical = canScrollVertical; + self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, canScrollVertical); + } + if( mCanScrollHorizontal != canScrollHorizontal ) + { + mCanScrollHorizontal = canScrollHorizontal; + self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, canScrollHorizontal); + } + if( scrollPositionChanged ) + { + DALI_LOG_SCROLL_STATE("[0x%X] Domain Changed, setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y ); + self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition); + } + if( domainChanged ) + { + mMinScroll = min; + mMaxScroll = max; + self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, mMinScroll ); + self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, mMaxScroll ); + } } void ScrollView::SetScrollSensitive(bool sensitive) { Actor self = Self(); PanGestureDetector panGesture( GetPanGestureDetector() ); - PinchGestureDetector pinchGesture( GetPinchGestureDetector() ); + + DALI_LOG_SCROLL_STATE("[0x%X] sensitive: before:[%d] setting[%d]", this, int(mSensitive), int(sensitive)); if((!mSensitive) && (sensitive)) { mSensitive = sensitive; panGesture.Attach(self); - pinchGesture.Attach(self); } else if((mSensitive) && (!sensitive)) { - mSensitive = sensitive; + DALI_LOG_SCROLL_STATE("[0x%X] BEFORE: panning:[%d]", this, int(mPanning)); + + // while the scroll view is panning, the state needs to be reset. + if ( mPanning ) + { + PanGesture cancelGesture( Gesture::Cancelled ); + OnPan( cancelGesture ); + } + panGesture.Detach(self); - pinchGesture.Detach(self); + mSensitive = sensitive; mGestureStackDepth = 0; - self.SetProperty(mPropertyPanning, false); - - // Remove X & Y position constraints as they are not required when we are not panning. - self.RemoveConstraint(mScrollMainInternalXConstraint); - self.RemoveConstraint(mScrollMainInternalYConstraint); + DALI_LOG_SCROLL_STATE("[0x%X] AFTER: panning:[%d]", this, int(mPanning)); } } @@ -942,6 +961,7 @@ void ScrollView::SetMaxOvershoot(float overshootX, float overshootY) { mMaxOvershoot.x = overshootX; mMaxOvershoot.y = overshootY; + mUserMaxOvershoot = mMaxOvershoot; mDefaultMaxOvershoot = false; UpdateMainInternalConstraint(); } @@ -956,25 +976,6 @@ void ScrollView::SetSnapOvershootDuration(float duration) mSnapOvershootDuration = duration; } -void ScrollView::SetTouchesRequiredForPanning(unsigned int minTouches, unsigned int maxTouches, bool endOutside) -{ - PanGestureDetector panGesture( GetPanGestureDetector() ); - - mMinTouchesForPanning = minTouches; - mMaxTouchesForPanning = maxTouches; - - if(endOutside) - { - panGesture.SetMinimumTouchesRequired(minTouches); - panGesture.SetMaximumTouchesRequired(maxTouches); - } - else - { - panGesture.SetMinimumTouchesRequired(1); - panGesture.SetMaximumTouchesRequired(UINT_MAX); - } -} - void ScrollView::SetActorAutoSnap(bool enable) { mActorAutoSnapEnabled = enable; @@ -994,17 +995,17 @@ bool ScrollView::GetWrapMode() const void ScrollView::SetWrapMode(bool enable) { mWrapMode = enable; - Self().SetProperty(mPropertyWrap, enable); + Self().SetProperty(Toolkit::ScrollView::Property::WRAP, enable); } -int ScrollView::GetRefreshInterval() const +int ScrollView::GetScrollUpdateDistance() const { - return mRefreshIntervalMilliseconds; + return mScrollUpdateDistance; } -void ScrollView::SetRefreshInterval(int milliseconds) +void ScrollView::SetScrollUpdateDistance(int distance) { - mRefreshIntervalMilliseconds = milliseconds; + mScrollUpdateDistance = distance; } bool ScrollView::GetAxisAutoLock() const @@ -1051,6 +1052,26 @@ void ScrollView::SetFlickSpeedCoefficient(float speed) mFlickSpeedCoefficient = speed; } +Vector2 ScrollView::GetMinimumDistanceForFlick() const +{ + return mMinFlickDistance; +} + +void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance ) +{ + mMinFlickDistance = distance; +} + +float ScrollView::GetMinimumSpeedForFlick() const +{ + return mFlickSpeedThreshold; +} + +void ScrollView::SetMinimumSpeedForFlick( float speed ) +{ + mFlickSpeedThreshold = speed; +} + float ScrollView::GetMaxFlickSpeed() const { return mMaxFlickSpeed; @@ -1061,20 +1082,20 @@ void ScrollView::SetMaxFlickSpeed(float speed) mMaxFlickSpeed = speed; } -void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step) +void ScrollView::SetWheelScrollDistanceStep(Vector2 step) { - mMouseWheelScrollDistanceStep = step; + mWheelScrollDistanceStep = step; } -Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const +Vector2 ScrollView::GetWheelScrollDistanceStep() const { - return mMouseWheelScrollDistanceStep; + return mWheelScrollDistanceStep; } unsigned int ScrollView::GetCurrentPage() const { // in case animation is currently taking place. - Vector3 position = GetPropertyPrePosition(); + Vector2 position = GetPropertyPosition(); Actor self = Self(); unsigned int page = 0; @@ -1089,62 +1110,77 @@ unsigned int ScrollView::GetCurrentPage() const return volume * pagesPerVolume + page; } -Vector3 ScrollView::GetCurrentScrollPosition() const -{ - // in case animation is currently taking place. - return -GetPropertyPrePosition(); -} - -Vector3 ScrollView::GetCurrentScrollScale() const +Vector2 ScrollView::GetCurrentScrollPosition() const { - // in case animation is currently taking place. - return GetPropertyScale(); + return -GetPropertyPosition(); } -Vector3 ScrollView::GetDomainSize() const +Vector2 ScrollView::GetDomainSize() const { Vector3 size = Self().GetCurrentSize(); const RulerDomain& xDomain = GetRulerX()->GetDomain(); const RulerDomain& yDomain = GetRulerY()->GetDomain(); - Vector3 domainSize = Vector3( xDomain.max - xDomain.min, yDomain.max - yDomain.min, 0.0f ) - size; + Vector2 domainSize; + domainSize.x = xDomain.max - xDomain.min - size.x; + domainSize.y = yDomain.max - yDomain.min - size.y; return domainSize; } -void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation, +void ScrollView::TransformTo(const Vector2& position, DirectionBias horizontalBias, DirectionBias verticalBias) { - TransformTo(position, scale, rotation, mSnapDuration, horizontalBias, verticalBias); + TransformTo(position, mSnapDuration, mSnapAlphaFunction, horizontalBias, verticalBias); } -void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation, float duration, +void ScrollView::TransformTo(const Vector2& position, float duration, AlphaFunction alpha, DirectionBias horizontalBias, DirectionBias verticalBias) { + // If this is called while the timer is running, then cancel it + StopTouchDownTimer(); + + Actor self( Self() ); + // Guard against destruction during signal emission // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo() Toolkit::ScrollView handle( GetOwner() ); - Vector3 currentScrollPosition = GetCurrentScrollPosition(); - Self().SetProperty( mPropertyScrollStartPagePosition, currentScrollPosition ); + DALI_LOG_SCROLL_STATE("[0x%X] pos[%.2f,%.2f], duration[%.2f] bias[%d, %d]", + this, position.x, position.y, duration, int(horizontalBias), int(verticalBias)); - if(mScrolling) // are we interrupting a current scroll? + Vector2 currentScrollPosition = GetCurrentScrollPosition(); + self.SetProperty( Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(currentScrollPosition) ); + + if( mScrolling ) // are we interrupting a current scroll? { // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete. mScrolling = false; - mScrollCompletedSignalV2.Emit( currentScrollPosition ); + DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y); + mScrollCompletedSignal.Emit( currentScrollPosition ); + } + + if( mPanning ) // are we interrupting a current pan? + { + DALI_LOG_SCROLL_STATE("[0x%X] Interrupting Pan, set to false", this ); + mPanning = false; + mGestureStackDepth = 0; + self.SetProperty( Toolkit::ScrollView::Property::PANNING, false ); + + if( mScrollMainInternalPrePositionConstraint ) + { + mScrollMainInternalPrePositionConstraint.Remove(); + } } - Self().SetProperty(mPropertyScrolling, true); + self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, true); mScrolling = true; - mScrollStartedSignalV2.Emit( currentScrollPosition ); + + DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y); + mScrollStartedSignal.Emit( currentScrollPosition ); bool animating = AnimateTo(-position, - Vector3::ONE * duration, - scale, - Vector3::ONE * duration, - rotation, - duration, - mSnapAlphaFunction, + Vector2::ONE * duration, + alpha, true, horizontalBias, verticalBias, @@ -1153,26 +1189,49 @@ void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, floa if(!animating) { // if not animating, then this pan has completed right now. - Self().SetProperty(mPropertyScrolling, false); + self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false); mScrolling = false; - mScrollCompletedSignalV2.Emit( currentScrollPosition ); + + // If we have no duration, then in the next update frame, we will be at the position specified as we just set. + // In this scenario, we cannot return the currentScrollPosition as this is out-of-date and should instead return the requested final position + Vector2 completedPosition( currentScrollPosition ); + if( duration <= Math::MACHINE_EPSILON_10 ) + { + completedPosition = position; + } + + DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 2 [%.2f, %.2f]", this, completedPosition.x, completedPosition.y); + SetScrollUpdateNotification(false); + mScrollCompletedSignal.Emit( completedPosition ); } } -void ScrollView::ScrollTo(const Vector3& position) +void ScrollView::ScrollTo(const Vector2& position) { ScrollTo(position, mSnapDuration ); } -void ScrollView::ScrollTo(const Vector3& position, float duration) +void ScrollView::ScrollTo(const Vector2& position, float duration) { ScrollTo(position, duration, DirectionBiasNone, DirectionBiasNone); } -void ScrollView::ScrollTo(const Vector3& position, float duration, - DirectionBias horizontalBias, DirectionBias verticalBias) +void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha) { - TransformTo(position, mScrollPostScale, mScrollPostRotation, duration, horizontalBias, verticalBias); + ScrollTo(position, duration, alpha, DirectionBiasNone, DirectionBiasNone); +} + +void ScrollView::ScrollTo(const Vector2& position, float duration, + DirectionBias horizontalBias, DirectionBias verticalBias) +{ + ScrollTo(position, duration, mSnapAlphaFunction, horizontalBias, verticalBias); +} + +void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha, + DirectionBias horizontalBias, DirectionBias verticalBias) +{ + DALI_LOG_SCROLL_STATE("[0x%X] position[%.2f, %.2f] duration[%.2f], bias[%d, %d]", this, position.x, position.y, duration, int(horizontalBias), int(verticalBias)); + TransformTo(position, duration, alpha, horizontalBias, verticalBias); } void ScrollView::ScrollTo(unsigned int page) @@ -1182,7 +1241,7 @@ void ScrollView::ScrollTo(unsigned int page) void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias) { - Vector3 position; + Vector2 position; unsigned int volume; unsigned int libraries; @@ -1210,9 +1269,10 @@ void ScrollView::ScrollTo(Actor &actor, float duration) Actor self = Self(); Vector3 size = self.GetCurrentSize(); Vector3 position = actor.GetCurrentPosition(); - position -= GetPropertyPrePosition(); + Vector2 prePosition = GetPropertyPrePosition(); + position.GetVectorXY() -= prePosition; - ScrollTo(Vector3(position.x - size.width * 0.5f, position.y - size.height * 0.5f, 0.0f), duration); + ScrollTo(Vector2(position.x - size.width * 0.5f, position.y - size.height * 0.5f), duration); } Actor ScrollView::FindClosestActor() @@ -1313,21 +1373,11 @@ Actor ScrollView::FindClosestActorToPosition(const Vector3& position, FindDirect bool ScrollView::ScrollToSnapPoint() { + DALI_LOG_SCROLL_STATE("[0x%X]", this ); Vector2 stationaryVelocity = Vector2(0.0f, 0.0f); return SnapWithVelocity( stationaryVelocity ); } -void ScrollView::ScaleTo(const Vector3& scale) -{ - ScaleTo(scale, mSnapDuration); -} - -void ScrollView::ScaleTo(const Vector3& scale, float duration) -{ - TransformTo(mScrollPostPosition, scale, mScrollPostRotation, duration); -} - - // 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) @@ -1343,9 +1393,7 @@ bool ScrollView::SnapWithVelocity(Vector2 velocity) float angle = atan2(velocity.y, velocity.x); float speed2 = velocity.LengthSquared(); AlphaFunction alphaFunction = mSnapAlphaFunction; - Vector3 positionDuration = Vector3::ONE * mSnapDuration; - Vector3 scaleDuration = Vector3::ONE * mSnapDuration; - float rotationDuration = mSnapDuration; + Vector2 positionDuration = Vector2::ONE * mSnapDuration; float biasX = 0.5f; float biasY = 0.5f; FindDirection horizontal = None; @@ -1355,34 +1403,46 @@ bool ScrollView::SnapWithVelocity(Vector2 velocity) // 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 = FLICK_SPEED_THRESHOLD*FLICK_SPEED_THRESHOLD; + const float flickSpeedThreshold2 = mFlickSpeedThreshold * mFlickSpeedThreshold; + + Vector2 positionSnap = mScrollPrePosition; // Flick logic X Axis - if(mRulerX->IsEnabled()) + if(mRulerX->IsEnabled() && mLockAxis != LockHorizontal) { horizontal = All; - if(speed2 > flickSpeedThreshold2) // exceeds flick threshold + if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold + mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions { if((angle >= -orthoAngleRange) && (angle < orthoAngleRange)) // Swiping East { biasX = 0.0f, horizontal = 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 = 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(mRulerY->IsEnabled()) + if(mRulerY->IsEnabled() && mLockAxis != LockVertical) { vertical = All; - if(speed2 > flickSpeedThreshold2) // exceeds flick threshold + if( speed2 > flickSpeedThreshold2 || // exceeds flick threshold + mInAccessibilityPan ) // With AccessibilityPan its easier to move between snap positions { if((angle >= M_PI_2-orthoAngleRange) && (angle < M_PI_2+orthoAngleRange)) // Swiping South { @@ -1402,12 +1462,11 @@ bool ScrollView::SnapWithVelocity(Vector2 velocity) if(isFlick || isFreeFlick) { - positionDuration = Vector3::ONE * mFlickDuration; + positionDuration = Vector2::ONE * mFlickDuration; alphaFunction = mFlickAlphaFunction; } - // Position Snap //////////////////////////////////////////////////////////// - Vector3 positionSnap = mScrollPostPosition; + // Calculate next positionSnap //////////////////////////////////////////////////////////// if(mActorAutoSnapEnabled) { @@ -1423,7 +1482,7 @@ bool ScrollView::SnapWithVelocity(Vector2 velocity) if(child) { - Vector3 position = Self().GetProperty(mPropertyPosition); + Vector2 position = Self().GetProperty(Toolkit::ScrollView::Property::SCROLL_POSITION); // Get center-point of the Actor. Vector3 childPosition = GetPositionOfAnchor(child, AnchorPoint::CENTER); @@ -1439,11 +1498,11 @@ bool ScrollView::SnapWithVelocity(Vector2 velocity) } } - Vector3 startPosition = positionSnap; + Vector2 startPosition = positionSnap; positionSnap.x = -mRulerX->Snap(-positionSnap.x, biasX); // NOTE: X & Y rulers think in -ve coordinate system. positionSnap.y = -mRulerY->Snap(-positionSnap.y, biasY); // That is scrolling RIGHT (e.g. 100.0, 0.0) means moving LEFT. - Vector3 clampDelta(Vector3::ZERO); + Vector2 clampDelta(Vector2::ZERO); ClampPosition(positionSnap); if( (mRulerX->GetType() == Ruler::Free || mRulerY->GetType() == Ruler::Free) @@ -1489,7 +1548,7 @@ bool ScrollView::SnapWithVelocity(Vector2 velocity) } else { - clampDelta = Vector3::ZERO; + clampDelta = Vector2::ZERO; } // If Axis is Free and has velocity, then calculate time taken @@ -1522,72 +1581,41 @@ bool ScrollView::SnapWithVelocity(Vector2 velocity) } } } - positionSnap += clampDelta; - // Scale Snap /////////////////////////////////////////////////////////////// - Vector3 scaleSnap = mScrollPostScale; - - scaleSnap.x = mRulerScaleX->Snap(scaleSnap.x); - scaleSnap.y = mRulerScaleY->Snap(scaleSnap.y); - - ClampScale(scaleSnap); - - // Rotation Snap //////////////////////////////////////////////////////////// - float rotationSnap = mScrollPostRotation; - // TODO: implement rotation snap + if(IsOvershootEnabled()) + { + // Scroll to the end of the overshoot only when overshoot is enabled. + positionSnap += clampDelta; + } bool animating = AnimateTo(positionSnap, positionDuration, - scaleSnap, scaleDuration, - rotationSnap, rotationDuration, alphaFunction, false, DirectionBiasNone, DirectionBiasNone, isFlick || isFreeFlick ? Flick : Snap); - if(animating) - { - AnimateOvershootToOrigin(positionDuration.x, positionDuration.y); - } - return animating; } void ScrollView::StopAnimation(void) { // Clear Snap animation if exists. - if(mSnapAnimation) - { - mSnapAnimation.Stop(); - mSnapAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapAnimationFinished); - mSnapAnimation.Clear(); - mSnapAnimation = NULL; - } - if(mSnapXAnimation) - { - mSnapXAnimation.Stop(); - mSnapXAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapXAnimationFinished); - mSnapXAnimation.Clear(); - mSnapXAnimation = NULL; - } - if(mSnapYAnimation) - { - mSnapYAnimation.Stop(); - mSnapYAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapYAnimationFinished); - mSnapYAnimation.Clear(); - mSnapYAnimation = NULL; - } - if(mSnapOvershootAnimation) + StopAnimation(mInternalXAnimation); + StopAnimation(mInternalYAnimation); + mScrollStateFlags = 0; + // remove scroll animation flags + HandleStoppedAnimation(); +} + +void ScrollView::StopAnimation(Animation& animation) +{ + if(animation) { - mSnapOvershootAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapOvershootAnimationFinished); - mSnapOvershootAnimation.Stop(); - mSnapOvershootAnimation.Clear(); - mSnapOvershootAnimation = NULL; + animation.Stop(); + animation.Reset(); } - HandleStoppedAnimation(); } -bool ScrollView::AnimateTo(const Vector3& position, const Vector3& positionDuration, - const Vector3& scale, const Vector3& scaleDuration, - float rotation, float rotationDuration, +bool ScrollView::AnimateTo(const Vector2& position, const Vector2& positionDuration, AlphaFunction alpha, bool findShortcuts, DirectionBias horizontalBias, DirectionBias verticalBias, SnapType snapType) @@ -1595,128 +1623,100 @@ bool ScrollView::AnimateTo(const Vector3& position, const Vector3& positionDurat // Here we perform an animation on a number of properties (depending on which have changed) // The animation is applied to all ScrollBases Actor self = Self(); - bool startAnimation = false; - Vector3 positionTransformed = position; + mScrollTargetPosition = position; float totalDuration = 0.0f; - bool positionChanged = (positionTransformed != mScrollPostPosition); - bool scaleChanged = (scale != mScrollPostScale); - bool rotationChanged = fabsf(rotation - mScrollPostRotation) > Math::MACHINE_EPSILON_0; + bool positionChanged = (mScrollTargetPosition != mScrollPostPosition); if(positionChanged) { totalDuration = std::max(totalDuration, positionDuration.x); totalDuration = std::max(totalDuration, positionDuration.y); } - - if(scaleChanged) + else { - totalDuration = std::max(totalDuration, scaleDuration.x); - totalDuration = std::max(totalDuration, scaleDuration.y); + // try to animate for a frame, on some occasions update will be changing scroll value while event side thinks it hasnt changed + totalDuration = 0.01f; + positionChanged = true; } - if(rotationChanged) - { - totalDuration = std::max(totalDuration, rotationDuration); - } + StopAnimation(); - if(totalDuration > Math::MACHINE_EPSILON_1) + // Position Delta /////////////////////////////////////////////////////// + if(positionChanged) { - StopAnimation(); - mSnapAnimation = Animation::New(totalDuration); - mSnapAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapAnimationFinished); - mSnapXAnimation = Animation::New(positionDuration.x); - mSnapXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapXAnimationFinished); - mSnapYAnimation = Animation::New(positionDuration.y); - mSnapYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapYAnimationFinished); - startAnimation = true; - - // Position Delta /////////////////////////////////////////////////////// - if(positionChanged) - { - if(mWrapMode && findShortcuts) - { - // In Wrap Mode, the shortest distance is a little less intuitive... - const RulerDomain rulerDomainX = mRulerX->GetDomain(); - const RulerDomain rulerDomainY = mRulerY->GetDomain(); - - if(mRulerX->IsEnabled()) - { - float dir = VectorInDomain(-mScrollPostPosition.x, -positionTransformed.x, rulerDomainX.min, rulerDomainX.max, horizontalBias); - positionTransformed.x = mScrollPostPosition.x + -dir; - } + if(mWrapMode && findShortcuts) + { + // In Wrap Mode, the shortest distance is a little less intuitive... + const RulerDomain rulerDomainX = mRulerX->GetDomain(); + const RulerDomain rulerDomainY = mRulerY->GetDomain(); - if(mRulerY->IsEnabled()) - { - float dir = VectorInDomain(-mScrollPostPosition.y, -positionTransformed.y, rulerDomainY.min, rulerDomainY.max, verticalBias); - positionTransformed.y = mScrollPostPosition.y + -dir; - } + if(mRulerX->IsEnabled()) + { + float dir = VectorInDomain(-mScrollPrePosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias); + mScrollTargetPosition.x = mScrollPrePosition.x + -dir; } - // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting - // a horizonal/vertical wall.delay - mSnapXAnimation.AnimateTo( Property(self, mPropertyX), positionTransformed.x, alpha, TimePeriod(0.0f, positionDuration.x)); - mSnapYAnimation.AnimateTo( Property(self, mPropertyY), positionTransformed.y, alpha, TimePeriod(0.0f, positionDuration.y)); - } - - // Scale Delta /////////////////////////////////////////////////////// - if(scaleChanged) - { - // TODO: for non-uniform scaling to different bounds e.g. scaling a square to a 4:3 aspect ratio screen with a velocity - // the height will hit first, and then the width, so that would require two different animation times just like position. - mSnapAnimation.AnimateTo( Property(self, mPropertyScale), scale, alpha, TimePeriod(0.0f, scaleDuration.x)); + if(mRulerY->IsEnabled()) + { + float dir = VectorInDomain(-mScrollPrePosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias); + mScrollTargetPosition.y = mScrollPrePosition.y + -dir; + } } - mSnapAnimation.AnimateTo( Property(self, mPropertyTime), totalDuration, AlphaFunctions::Linear ); + // note we have two separate animations for X & Y, this deals with sliding diagonally and hitting + // a horizonal/vertical wall.delay + AnimateInternalXTo(mScrollTargetPosition.x, positionDuration.x, alpha); + AnimateInternalYTo(mScrollTargetPosition.y, positionDuration.y, alpha); - mSnapAnimation.Play(); - mSnapXAnimation.Play(); - mSnapYAnimation.Play(); - StartRefreshTimer(); - } // end if(totalDuration > Math::MACHINE_EPSILON_1) - else // totalDuration == 0 - { - // instantly set transform. - if(positionChanged) + if( !(mScrollStateFlags & SCROLL_ANIMATION_FLAGS) ) { - self.SetProperty(mPropertyX, positionTransformed.x); - self.SetProperty(mPropertyY, positionTransformed.y); - - mScrollPrePosition = mScrollPostPosition = positionTransformed; + DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollTargetPosition.x, mScrollTargetPosition.y ); + self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollTargetPosition); + mScrollPrePosition = mScrollTargetPosition; + mScrollPostPosition = mScrollTargetPosition; + WrapPosition(mScrollPostPosition); } - if(scaleChanged) - { - self.SetProperty(mPropertyScale, scale); - - mScrollPreScale = mScrollPostScale = scale; - } + 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 ); + 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().x, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ).Get().y, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_POSITION ).Get().x, self.GetProperty( Toolkit::ScrollView::Property::SCROLL_POSITION ).Get().y ); } + SetScrollUpdateNotification(true); + // Always send a snap event when AnimateTo is called. Toolkit::ScrollView::SnapEvent snapEvent; snapEvent.type = snapType; - snapEvent.position = positionTransformed; - snapEvent.scale = scale; - snapEvent.rotation = rotation; + snapEvent.position = -mScrollTargetPosition; snapEvent.duration = totalDuration; - mSnapStartedSignalV2.Emit( snapEvent ); + DALI_LOG_SCROLL_STATE("[0x%X] mSnapStartedSignal [%.2f, %.2f]", this, snapEvent.position.x, snapEvent.position.y); + mSnapStartedSignal.Emit( snapEvent ); - return startAnimation; + return (mScrollStateFlags & SCROLL_ANIMATION_FLAGS) != 0; } -void ScrollView::SetOvershootEnabled(bool enabled) +void ScrollView::EnableScrollOvershoot(bool enable) { - if(enabled && !mOvershootIndicator) + if(enable && !mOvershootIndicator) + { + mOvershootIndicator = ScrollOvershootIndicator::New(); + } + if( enable ) + { + mOvershootIndicator->AttachToScrollable(*this); + } + else { - mOvershootIndicator = ScrollOvershootIndicator::New(*this); + mMaxOvershoot = mUserMaxOvershoot; + mOvershootIndicator->DetachFromScrollable(*this); } - mOvershootIndicator->Enable(enabled); + UpdateMainInternalConstraint(); } void ScrollView::AddOverlay(Actor actor) { + actor.SetDrawMode( DrawMode::OVERLAY_2D ); mInternalActor.Add( actor ); } @@ -1725,6 +1725,15 @@ void ScrollView::RemoveOverlay(Actor actor) mInternalActor.Remove( actor ); } +void ScrollView::SetOvershootEffectColor( const Vector4& color ) +{ + mOvershootEffectColor = color; + if( mOvershootIndicator ) + { + mOvershootIndicator->SetOvershootEffectColor( color ); + } +} + void ScrollView::SetScrollingDirection( Radian direction, Radian threshold ) { PanGestureDetector panGesture( GetPanGestureDetector() ); @@ -1740,9 +1749,9 @@ void ScrollView::RemoveScrollingDirection( Radian direction ) panGesture.RemoveDirection( direction ); } -Toolkit::ScrollView::SnapStartedSignalV2& ScrollView::SnapStartedSignal() +Toolkit::ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal() { - return mSnapStartedSignalV2; + return mSnapStartedSignal; } void ScrollView::FindAndUnbindActor(Actor child) @@ -1750,66 +1759,89 @@ void ScrollView::FindAndUnbindActor(Actor child) UnbindActor(child); } -Vector3 ScrollView::GetPropertyPrePosition() const +Vector2 ScrollView::GetPropertyPrePosition() const { - Vector3 position(Self().GetProperty(mPropertyX), Self().GetProperty(mPropertyY), 0.0f); + Vector2 position = Self().GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION); WrapPosition(position); - return position; } -Vector3 ScrollView::GetPropertyPosition() const +Vector2 ScrollView::GetPropertyPosition() const { - Vector3 position = Self().GetProperty(mPropertyPosition); + Vector2 position = Self().GetProperty(Toolkit::ScrollView::Property::SCROLL_POSITION); WrapPosition(position); return position; } -Vector3 ScrollView::GetPropertyScale() const -{ - return Self().GetProperty(mPropertyScale); -} - void ScrollView::HandleStoppedAnimation() { - // Animation has stopped, so stop sending the scroll-update signal. - CancelRefreshTimer(); - - // cement transform now, and allow interactivity to resume. - mScrollPostPosition = GetPropertyPosition(); - - mScrollPostScale = GetPropertyScale(); - - // Update Actor position with this wrapped value. - - Self().SetProperty(mPropertyX, mScrollPostPosition.x); - Self().SetProperty(mPropertyY, mScrollPostPosition.y); - // TODO Rotation - - mScrollPrePosition = mScrollPostPosition; - mScrollPreScale = mScrollPostScale; - mScrollPreRotation = mScrollPostRotation; + SetScrollUpdateNotification(false); } void ScrollView::HandleSnapAnimationFinished() { // Emit Signal that scrolling has completed. mScrolling = false; - Self().SetProperty(mPropertyScrolling, false); + Actor self = Self(); + self.SetProperty(Toolkit::ScrollView::Property::SCROLLING, false); + + Vector2 deltaPosition(mScrollPrePosition); - Vector3 deltaPosition(Self().GetProperty(mPropertyX), - Self().GetProperty(mPropertyY), - 0.0f); + UpdateLocalScrollProperties(); + WrapPosition(mScrollPrePosition); + DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y ); + self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition); - Vector3 currentScrollPosition = GetCurrentScrollPosition(); - mScrollCompletedSignalV2.Emit( currentScrollPosition ); + Vector2 currentScrollPosition = GetCurrentScrollPosition(); + DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 3 current[%.2f, %.2f], mScrollTargetPosition[%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y, -mScrollTargetPosition.x, -mScrollTargetPosition.y ); + mScrollCompletedSignal.Emit( currentScrollPosition ); mDomainOffset += deltaPosition - mScrollPostPosition; - Self().SetProperty(mPropertyDomainOffset, mDomainOffset); + self.SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset); HandleStoppedAnimation(); } +void ScrollView::SetScrollUpdateNotification( bool enabled ) +{ + Actor self = Self(); + if( mScrollXUpdateNotification ) + { + // disconnect now to avoid a notification before removed from update thread + mScrollXUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification); + self.RemovePropertyNotification(mScrollXUpdateNotification); + mScrollXUpdateNotification.Reset(); + } + if( enabled && !mScrollUpdatedSignal.Empty()) + { + // Only set up the notification when the application has connected to the updated signal + mScrollXUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 0, StepCondition(mScrollUpdateDistance, 0.0f)); + mScrollXUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification ); + } + if( mScrollYUpdateNotification ) + { + // disconnect now to avoid a notification before removed from update thread + mScrollYUpdateNotification.NotifySignal().Disconnect(this, &ScrollView::OnScrollUpdateNotification); + self.RemovePropertyNotification(mScrollYUpdateNotification); + mScrollYUpdateNotification.Reset(); + } + if( enabled && !mScrollUpdatedSignal.Empty()) + { + // Only set up the notification when the application has connected to the updated signal + mScrollYUpdateNotification = self.AddPropertyNotification(Toolkit::ScrollView::Property::SCROLL_POSITION, 1, StepCondition(mScrollUpdateDistance, 0.0f)); + mScrollYUpdateNotification.NotifySignal().Connect( this, &ScrollView::OnScrollUpdateNotification ); + } +} + +void ScrollView::OnScrollUpdateNotification(Dali::PropertyNotification& source) +{ + // Guard against destruction during signal emission + Toolkit::ScrollView handle( GetOwner() ); + + Vector2 currentScrollPosition = GetCurrentScrollPosition(); + mScrollUpdatedSignal.Emit( currentScrollPosition ); +} + bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor ) { Dali::BaseHandle handle( object ); @@ -1817,7 +1849,7 @@ bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface bool connected( true ); Toolkit::ScrollView view = Toolkit::ScrollView::DownCast( handle ); - if( Toolkit::ScrollView::SIGNAL_SNAP_STARTED == signalName ) + if( 0 == strcmp( signalName.c_str(), SIGNAL_SNAP_STARTED ) ) { view.SnapStartedSignal().Connect( tracker, functor ); } @@ -1833,20 +1865,24 @@ bool ScrollView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface void ScrollView::OnSizeAnimation(Animation& animation, const Vector3& targetSize) { // need to update domain properties for new size - UpdatePropertyDomain(targetSize); + UpdatePropertyDomain(); } -void ScrollView::OnControlSizeSet( const Vector3& size ) +void ScrollView::OnSizeSet( const Vector3& size ) { // need to update domain properties for new size if( mDefaultMaxOvershoot ) { - mMaxOvershoot.x = size.x * 0.5f; - mMaxOvershoot.y = size.y * 0.5f; + mUserMaxOvershoot.x = size.x * 0.5f; + mUserMaxOvershoot.y = size.y * 0.5f; + if( !IsOvershootEnabled() ) + { + mMaxOvershoot = mUserMaxOvershoot; + } } - UpdatePropertyDomain(size); + UpdatePropertyDomain(); UpdateMainInternalConstraint(); - if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) ) + if( IsOvershootEnabled() ) { mOvershootIndicator->Reset(); } @@ -1854,7 +1890,28 @@ void ScrollView::OnControlSizeSet( const Vector3& size ) void ScrollView::OnChildAdd(Actor& child) { - if(mAlterChild) + Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child); + if(scrollBar) + { + mInternalActor.Add(scrollBar); + if(scrollBar.GetScrollDirection() == Toolkit::ScrollBar::Horizontal) + { + scrollBar.SetScrollPropertySource(Self(), + Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_X, + Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_X, + Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_X, + Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_X); + } + else + { + scrollBar.SetScrollPropertySource(Self(), + Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_Y, + Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y, + Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX_Y, + Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE_Y); + } + } + else if(mAlterChild) { BindActor(child); } @@ -1866,10 +1923,62 @@ void ScrollView::OnChildRemove(Actor& child) UnbindActor(child); } +void ScrollView::StartTouchDownTimer() +{ + if ( !mTouchDownTimer ) + { + mTouchDownTimer = Timer::New( TOUCH_DOWN_TIMER_INTERVAL ); + mTouchDownTimer.TickSignal().Connect( this, &ScrollView::OnTouchDownTimeout ); + } + + mTouchDownTimer.Start(); +} + +void ScrollView::StopTouchDownTimer() +{ + if ( mTouchDownTimer ) + { + mTouchDownTimer.Stop(); + } +} + +bool ScrollView::OnTouchDownTimeout() +{ + DALI_LOG_SCROLL_STATE("[0x%X]", this); + + mTouchDownTimeoutReached = true; + + unsigned int currentScrollStateFlags( mScrollStateFlags ); // Cleared in StopAnimation so keep local copy for comparison + if( currentScrollStateFlags & (SCROLL_ANIMATION_FLAGS | SNAP_ANIMATION_FLAGS) ) + { + DALI_LOG_SCROLL_STATE("[0x%X] Scrolling Or snapping flags set, stopping animation", this); + + StopAnimation(); + if( currentScrollStateFlags & SCROLL_ANIMATION_FLAGS ) + { + DALI_LOG_SCROLL_STATE("[0x%X] Scrolling flags set, emitting signal", this); + + mScrollInterrupted = true; + // reset domain offset as scrolling from original plane. + mDomainOffset = Vector2::ZERO; + Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, Vector2::ZERO); + + UpdateLocalScrollProperties(); + Vector2 currentScrollPosition = GetCurrentScrollPosition(); + DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 4 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y); + mScrollCompletedSignal.Emit( currentScrollPosition ); + } + } + + return false; +} + bool ScrollView::OnTouchEvent(const TouchEvent& event) { if(!mSensitive) { + DALI_LOG_SCROLL_STATE("[0x%X], Not Sensitive, ignoring", this); + // Ignore this touch event, if scrollview is insensitive. return false; } @@ -1877,73 +1986,71 @@ bool ScrollView::OnTouchEvent(const TouchEvent& event) // Ignore events with multiple-touch points if (event.GetPointCount() != 1) { + DALI_LOG_SCROLL_STATE("[0x%X], multiple touch, ignoring", this); + return false; } - if (event.GetPoint(0).state == TouchPoint::Down) + const TouchPoint::State pointState = event.GetPoint(0).state; + if( pointState == TouchPoint::Down ) { - mTouchDownTime = event.time; - mTouchDownReceived = true; - mTouchDownPosition = event.GetPoint(0).local; - - if( mSnapAnimation || mSnapXAnimation || mSnapYAnimation || mSnapOvershootAnimation ) - { - mScrollInterrupted = true; - StopAnimation(); - } + DALI_LOG_SCROLL_STATE("[0x%X] Down", this); - if(mScrolling) // are we interrupting a current scroll? + if(mGestureStackDepth==0) { - // reset domain offset as scrolling from original plane. - mDomainOffset = Vector3::ZERO; - Self().SetProperty(mPropertyDomainOffset, Vector3::ZERO); + mTouchDownTime = event.time; - mScrolling = false; - Vector3 currentScrollPosition = GetCurrentScrollPosition(); - mScrollCompletedSignalV2.Emit( currentScrollPosition ); + // This allows time for a pan-gesture to start, to avoid breaking snap-animation behavior with fast flicks. + // If touch-down does not become a pan (after timeout interval), then snap-animation can be interrupted. + mTouchDownTimeoutReached = false; + mScrollInterrupted = false; + StartTouchDownTimer(); } } - else if(event.GetPoint(0).state == TouchPoint::Up) + else if( ( pointState == TouchPoint::Up ) || + ( ( pointState == TouchPoint::Interrupted ) && ( event.GetPoint(0).hitActor == Self() ) ) ) { + DALI_LOG_SCROLL_STATE("[0x%X] %s", this, ( ( pointState == TouchPoint::Up ) ? "Up" : "Interrupted" ) ); + + StopTouchDownTimer(); + // if the user touches and releases without enough movement to go // into a gesture state, then we should snap to nearest point. // otherwise our scroll could be stopped (interrupted) half way through an animation. - if(mGestureStackDepth==0 && mTouchDownReceived) + if(mGestureStackDepth==0 && mTouchDownTimeoutReached) { - unsigned timeDelta( event.time - mTouchDownTime ); - if ( timeDelta >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET ) + if( ( event.GetPoint(0).state == TouchPoint::Interrupted ) || + ( ( event.time - mTouchDownTime ) >= MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET ) ) { // Reset the velocity only if down was received a while ago mLastVelocity = Vector2( 0.0f, 0.0f ); } - else - { - Vector2 positionDelta( mTouchDownPosition - event.GetPoint(0).local ); - mLastVelocity = positionDelta / timeDelta; - } + UpdateLocalScrollProperties(); // Only finish the transform if scrolling was interrupted on down or if we are scrolling - if ( mSnapAnimation || mSnapXAnimation || mSnapYAnimation || mSnapOvershootAnimation || mScrollInterrupted || mScrolling ) + if ( mScrollInterrupted || mScrolling ) { + DALI_LOG_SCROLL_STATE("[0x%X] Calling FinishTransform", this); + FinishTransform(); } } - mTouchDownReceived = false; + mTouchDownTimeoutReached = false; mScrollInterrupted = false; } - return true; // consume since we're potentially scrolling + return true; } -bool ScrollView::OnMouseWheelEvent(const MouseWheelEvent& event) +bool ScrollView::OnWheelEvent(const WheelEvent& event) { if(!mSensitive) { - // Ignore this mouse wheel event, if scrollview is insensitive. + // Ignore this wheel event, if scrollview is insensitive. return false; } - Vector3 targetScrollPosition = GetPropertyPosition(); + Vector2 targetScrollPosition = GetPropertyPosition(); if(mRulerX->IsEnabled() && !mRulerY->IsEnabled()) { @@ -1951,14 +2058,14 @@ bool ScrollView::OnMouseWheelEvent(const MouseWheelEvent& event) if(mRulerX->GetType() == Ruler::Free) { // Free panning mode - targetScrollPosition.x -= event.z * mMouseWheelScrollDistanceStep.x; + targetScrollPosition.x += event.z * mWheelScrollDistanceStep.x; ClampPosition(targetScrollPosition); ScrollTo(-targetScrollPosition); } else if(!mScrolling) { // Snap mode, only respond to the event when the previous snap animation is finished. - ScrollTo(GetCurrentPage() + event.z); + ScrollTo(GetCurrentPage() - event.z); } } else @@ -1967,75 +2074,245 @@ bool ScrollView::OnMouseWheelEvent(const MouseWheelEvent& event) if(mRulerY->GetType() == Ruler::Free) { // Free panning mode - targetScrollPosition.y -= event.z * mMouseWheelScrollDistanceStep.y; + targetScrollPosition.y += event.z * mWheelScrollDistanceStep.y; ClampPosition(targetScrollPosition); ScrollTo(-targetScrollPosition); } else if(!mScrolling) { // Snap mode, only respond to the event when the previous snap animation is finished. - ScrollTo(GetCurrentPage() + event.z * mRulerX->GetTotalPages()); + ScrollTo(GetCurrentPage() - event.z * mRulerX->GetTotalPages()); } } return true; } -void ScrollView::OnSnapAnimationFinished( Animation& source ) +void ScrollView::ResetScrolling() { - mSnapAnimation.FinishedSignal().Disconnect( this, &ScrollView::OnSnapAnimationFinished ); - mSnapAnimation = NULL; + Actor self = Self(); + self.GetProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get(mScrollPostPosition); + mScrollPrePosition = mScrollPostPosition; + DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPostPosition.x, mScrollPostPosition.y ); + self.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPostPosition); } -void ScrollView::OnSnapXAnimationFinished( Animation& source ) +void ScrollView::UpdateLocalScrollProperties() { - // Guard against destruction during signal emission - // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished() - Toolkit::ScrollView handle( GetOwner() ); + Actor self = Self(); + self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get(mScrollPrePosition); + self.GetProperty(Toolkit::ScrollView::Property::SCROLL_POSITION).Get(mScrollPostPosition); +} + +// private functions + +void ScrollView::PreAnimatedScrollSetup() +{ + // SCROLL_PRE_POSITION is our unclamped property with wrapping + // SCROLL_POSITION is our final scroll position after clamping + + Actor self = Self(); - if(!mSnapYAnimation) + Vector2 deltaPosition(mScrollPostPosition); + WrapPosition(mScrollPostPosition); + mDomainOffset += deltaPosition - mScrollPostPosition; + Self().SetProperty(Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET, mDomainOffset); + + if( mScrollStateFlags & SCROLL_X_STATE_MASK ) { - HandleSnapAnimationFinished(); + // already performing animation on internal x position + StopAnimation(mInternalXAnimation); } - if(mScrollMainInternalOvershootXConstraint) + + if( mScrollStateFlags & SCROLL_Y_STATE_MASK ) { - Self().RemoveConstraint(mScrollMainInternalOvershootXConstraint); - mScrollMainInternalOvershootXConstraint.Reset(); - mScrollMainInternalOvershootXConstraint = 0; + // already performing animation on internal y position + StopAnimation(mInternalYAnimation); + } + + mScrollStateFlags = 0; + + // Update Actor position with this wrapped value. +} + +void ScrollView::FinaliseAnimatedScroll() +{ + // TODO - common animation finishing code in here +} + +void ScrollView::AnimateInternalXTo( float position, float duration, AlphaFunction alpha ) +{ + StopAnimation(mInternalXAnimation); + + if( duration > Math::MACHINE_EPSILON_10 ) + { + Actor self = Self(); + DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get().x, position ); + mInternalXAnimation = Animation::New(duration); + DALI_LOG_SCROLL_STATE("[0x%X], mInternalXAnimation[0x%X]", this, mInternalXAnimation.GetObjectPtr() ); + mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished); + mInternalXAnimation.AnimateTo( Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position, alpha, TimePeriod(duration)); + mInternalXAnimation.Play(); + + // erase current state flags + mScrollStateFlags &= ~SCROLL_X_STATE_MASK; + // add internal animation state flag + mScrollStateFlags |= AnimatingInternalX; } - mSnapXAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapXAnimationFinished); - mSnapXAnimation.Reset(); - mSnapXAnimation = NULL; - if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) ) +} + +void ScrollView::AnimateInternalYTo( float position, float duration, AlphaFunction alpha ) +{ + StopAnimation(mInternalYAnimation); + + if( duration > Math::MACHINE_EPSILON_10 ) { - // kick start animation to 0 - Self().SetProperty(mPropertyOvershootX, 0.0f); + Actor self = Self(); + DALI_LOG_SCROLL_STATE("[0x%X], Animating from[%.2f] to[%.2f]", this, self.GetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION).Get().y, position ); + mInternalYAnimation = Animation::New(duration); + DALI_LOG_SCROLL_STATE("[0x%X], mInternalYAnimation[0x%X]", this, mInternalYAnimation.GetObjectPtr() ); + mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnScrollAnimationFinished); + mInternalYAnimation.AnimateTo( Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position, alpha, TimePeriod(duration)); + mInternalYAnimation.Play(); + + // erase current state flags + mScrollStateFlags &= ~SCROLL_Y_STATE_MASK; + // add internal animation state flag + mScrollStateFlags |= AnimatingInternalY; } } -void ScrollView::OnSnapYAnimationFinished( Animation& source ) +void ScrollView::OnScrollAnimationFinished( Animation& source ) { // Guard against destruction during signal emission // Note that ScrollCompletedSignal is emitted from HandleSnapAnimationFinished() Toolkit::ScrollView handle( GetOwner() ); - if(!mSnapXAnimation) + bool scrollingFinished = false; + + // update our local scroll positions + UpdateLocalScrollProperties(); + + if( source == mInternalXAnimation ) + { + 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().x, mScrollPostPosition.x ); + + if( !(mScrollStateFlags & AnimatingInternalY) ) + { + scrollingFinished = true; + } + mInternalXAnimation.Reset(); + // wrap pre scroll x position and set it + if( mWrapMode ) + { + const RulerDomain rulerDomain = mRulerX->GetDomain(); + mScrollPrePosition.x = -WrapInDomain(-mScrollPrePosition.x, rulerDomain.min, rulerDomain.max); + DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y ); + handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition); + } + SnapInternalXTo(mScrollPostPosition.x); + } + + if( source == mInternalYAnimation ) + { + 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().y, mScrollPostPosition.y ); + + if( !(mScrollStateFlags & AnimatingInternalX) ) + { + scrollingFinished = true; + } + mInternalYAnimation.Reset(); + if( mWrapMode ) + { + // wrap pre scroll y position and set it + const RulerDomain rulerDomain = mRulerY->GetDomain(); + mScrollPrePosition.y = -WrapInDomain(-mScrollPrePosition.y, rulerDomain.min, rulerDomain.max); + DALI_LOG_SCROLL_STATE("[0x%X] Setting SCROLL_PRE_POSITION To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y ); + handle.SetProperty(Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, mScrollPrePosition); + } + SnapInternalYTo(mScrollPostPosition.y); + } + + DALI_LOG_SCROLL_STATE("[0x%X] scrollingFinished[%d] Animation[0x%X]", this, scrollingFinished, source.GetObjectPtr()); + + if(scrollingFinished) { HandleSnapAnimationFinished(); } - if(mScrollMainInternalOvershootYConstraint) +} + +void ScrollView::OnSnapInternalPositionFinished( Animation& source ) +{ + Actor self = Self(); + UpdateLocalScrollProperties(); + if( source == mInternalXAnimation ) + { + DALI_LOG_SCROLL_STATE("[0x%X] Finished X PostPosition Animation", this ); + + // clear internal x animation flags + mScrollStateFlags &= ~SCROLL_X_STATE_MASK; + mInternalXAnimation.Reset(); + WrapPosition(mScrollPrePosition); + } + if( source == mInternalYAnimation ) { - Self().RemoveConstraint(mScrollMainInternalOvershootYConstraint); - mScrollMainInternalOvershootYConstraint.Reset(); - mScrollMainInternalOvershootYConstraint = 0; + DALI_LOG_SCROLL_STATE("[0x%X] Finished Y PostPosition Animation", this ); + + mScrollStateFlags &= ~SCROLL_Y_STATE_MASK; + mInternalYAnimation.Reset(); + WrapPosition(mScrollPrePosition); } - mSnapYAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapYAnimationFinished); - mSnapYAnimation.Reset(); - mSnapYAnimation = NULL; - if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) ) +} + +void ScrollView::SnapInternalXTo(float position) +{ + Actor self = Self(); + + StopAnimation(mInternalXAnimation); + + // erase current state flags + mScrollStateFlags &= ~SCROLL_X_STATE_MASK; + + // if internal x not equal to inputed parameter, animate it + float duration = std::min(fabsf((position - mScrollPrePosition.x) / mMaxOvershoot.x) * mSnapOvershootDuration, mSnapOvershootDuration); + DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration ); + if( duration > Math::MACHINE_EPSILON_1 ) { - // kick start animation to 0 - Self().SetProperty(mPropertyOvershootY, 0.0f); + DALI_LOG_SCROLL_STATE("[0x%X] Starting X Snap Animation to[%.2f]", this, position ); + + mInternalXAnimation = Animation::New(duration); + mInternalXAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished); + mInternalXAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 0), position); + mInternalXAnimation.Play(); + + // add internal animation state flag + mScrollStateFlags |= SnappingInternalX; + } +} + +void ScrollView::SnapInternalYTo(float position) +{ + Actor self = Self(); + + StopAnimation(mInternalYAnimation); + + // erase current state flags + mScrollStateFlags &= ~SCROLL_Y_STATE_MASK; + + // if internal y not equal to inputed parameter, animate it + float duration = std::min(fabsf((position - mScrollPrePosition.y) / mMaxOvershoot.y) * mSnapOvershootDuration, mSnapOvershootDuration); + DALI_LOG_SCROLL_STATE("[0x%X] duration[%.2f]", this, duration ); + if( duration > Math::MACHINE_EPSILON_1 ) + { + DALI_LOG_SCROLL_STATE("[0x%X] Starting Y Snap Animation to[%.2f]", this, position ); + + mInternalYAnimation = Animation::New(duration); + mInternalYAnimation.FinishedSignal().Connect(this, &ScrollView::OnSnapInternalPositionFinished); + mInternalYAnimation.AnimateTo(Property(self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, 1), position); + mInternalYAnimation.Play(); + + // add internal animation state flag + mScrollStateFlags |= SnappingInternalY; } } @@ -2046,30 +2323,42 @@ void ScrollView::GestureStarted() // we continue and combine the effects of the gesture instead of reseting. if(mGestureStackDepth++==0) { + Actor self = Self(); + StopTouchDownTimer(); StopAnimation(); - mPanDelta = Vector3::ZERO; - mScaleDelta = Vector3::ONE; - mRotationDelta = 0.0f; - mLastVelocity = Vector2(0.0f, 0.0f); - mLockAxis = LockPossible; + mPanDelta = Vector2::ZERO; + mLastVelocity = Vector2::ZERO; + if( !mScrolling ) + { + mLockAxis = LockPossible; + } + + if( mScrollStateFlags & SCROLL_X_STATE_MASK ) + { + StopAnimation(mInternalXAnimation); + } + if( mScrollStateFlags & SCROLL_Y_STATE_MASK ) + { + StopAnimation(mInternalYAnimation); + } + mScrollStateFlags = 0; if(mScrolling) // are we interrupting a current scroll? { // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete. mScrolling = false; - Vector3 currentScrollPosition = GetCurrentScrollPosition(); - mScrollCompletedSignalV2.Emit( currentScrollPosition ); + // send negative scroll position since scroll internal scroll position works as an offset for actors, + // give applications the position within the domain from the scroll view's anchor position + DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 5 [%.2f, %.2f]", this, -mScrollPostPosition.x, -mScrollPostPosition.y); + mScrollCompletedSignal.Emit( -mScrollPostPosition ); } } } -void ScrollView::GestureContinuing(Vector2 panDelta, Vector2 scaleDelta, float rotationDelta) +void ScrollView::GestureContinuing(const Vector2& panDelta) { mPanDelta.x+= panDelta.x; mPanDelta.y+= panDelta.y; - mScaleDelta.x*= scaleDelta.x; - mScaleDelta.y*= scaleDelta.y; - mRotationDelta+= rotationDelta; // Save the velocity, there is a bug in PanGesture // Whereby the Gesture::Finished's velocity is either: @@ -2080,35 +2369,14 @@ void ScrollView::GestureContinuing(Vector2 panDelta, Vector2 scaleDelta, float r // appears mostly horizontal or mostly vertical respectively. if(mAxisAutoLock) { - if(mPanDelta.LengthSquared() > AUTOLOCK_AXIS_MINIMUM_DISTANCE2 && - mLockAxis == LockPossible) - { - float dx = fabsf(mPanDelta.x); - float dy = fabsf(mPanDelta.y); - if(dx * mAxisAutoLockGradient >= dy) - { - // 0.36:1 gradient to the horizontal (deviate < 20 degrees) - mLockAxis = LockVertical; - } - else if(dy * mAxisAutoLockGradient > dx) - { - // 0.36:1 gradient to the vertical (deviate < 20 degrees) - mLockAxis = LockHorizontal; - } - else - { - mLockAxis = LockNone; - } - } + mLockAxis = GetLockAxis(mPanDelta, mLockAxis, mAxisAutoLockGradient); } // end if mAxisAutoLock } // TODO: Upgrade to use a more powerful gesture detector (one that supports multiple touches on pan - so works as pan and flick gesture) -// TODO: Reimplement Scaling (pinching 2+ points) -// TODO: Reimplment Rotation (pinching 2+ points) // BUG: Gesture::Finished doesn't always return velocity on release (due to // timeDelta between last two events being 0 sometimes, or posiiton being the same) -void ScrollView::OnPan(PanGesture gesture) +void ScrollView::OnPan( const PanGesture& gesture ) { // Guard against destruction during signal emission // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx() @@ -2116,6 +2384,8 @@ void ScrollView::OnPan(PanGesture gesture) if(!mSensitive) { + DALI_LOG_SCROLL_STATE("[0x%X] Pan Ignored, Insensitive", this); + // If another callback on the same original signal disables sensitivity, // this callback will still be called, so we must suppress it. return; @@ -2126,42 +2396,60 @@ void ScrollView::OnPan(PanGesture gesture) { case Gesture::Started: { + DALI_LOG_SCROLL_STATE("[0x%X] Pan Started", this); + mPanStartPosition = gesture.position - gesture.displacement; + UpdateLocalScrollProperties(); GestureStarted(); - self.SetProperty( mPropertyPanning, true ); - self.SetProperty( mPropertyScrollStartPagePosition, GetCurrentScrollPosition() ); - - // Update property: X & Y = Position (only when in panning mode - in snapping mode, X & Y are animated). - Constraint constraint = Constraint::New( mPropertyX, - LocalSource( mPropertyPosition ), - Source( self, mPropertyPanning ), - InternalXConstraint ); - mScrollMainInternalXConstraint = self.ApplyConstraint(constraint); - - constraint = Constraint::New( mPropertyY, - LocalSource( mPropertyPosition ), - Source( self, mPropertyPanning ), - InternalYConstraint ); - mScrollMainInternalYConstraint = self.ApplyConstraint(constraint); - // When panning we want to make sure overshoot values are affected by pre position and post position - SetOvershootConstraintsEnabled(true); + mPanning = true; + self.SetProperty( Toolkit::ScrollView::Property::PANNING, true ); + self.SetProperty( Toolkit::ScrollView::Property::START_PAGE_POSITION, Vector3(gesture.position.x, gesture.position.y, 0.0f) ); + + UpdateMainInternalConstraint(); break; } case Gesture::Continuing: { - // Nothing to do, handled in constraint. + if ( mPanning ) + { + DALI_LOG_SCROLL_STATE("[0x%X] Pan Continuing", this); + GestureContinuing(gesture.screenDisplacement); + } + else + { + // If we do not think we are panning, then we should not do anything here + return; + } break; } case Gesture::Finished: case Gesture::Cancelled: { - mLastVelocity = gesture.velocity; - self.SetProperty( mPropertyPanning, false ); + if ( mPanning ) + { + DALI_LOG_SCROLL_STATE("[0x%X] Pan %s", this, ( ( gesture.state == Gesture::Finished ) ? "Finished" : "Cancelled" ) ); - // Remove X & Y position constraints as they are not required when we are not panning. - self.RemoveConstraint(mScrollMainInternalXConstraint); - self.RemoveConstraint(mScrollMainInternalYConstraint); + UpdateLocalScrollProperties(); + mLastVelocity = gesture.velocity; + mPanning = false; + self.SetProperty( Toolkit::ScrollView::Property::PANNING, false ); + + if( mScrollMainInternalPrePositionConstraint ) + { + mScrollMainInternalPrePositionConstraint.Remove(); + } + + if( mOvershootIndicator ) + { + mOvershootIndicator->ClearOvershoot(); + } + } + else + { + // If we do not think we are panning, then we should not do anything here + return; + } break; } @@ -2177,21 +2465,17 @@ void ScrollView::OnPan(PanGesture gesture) OnGestureEx(gesture.state); } -void ScrollView::OnPinch(PinchGesture gesture) -{ - // TODO: Reintroduce the pinch functionality for scaling. -} - void ScrollView::OnGestureEx(Gesture::State state) { // call necessary signals for application developer if(state == Gesture::Started) { - Vector3 currentScrollPosition = GetCurrentScrollPosition(); - Self().SetProperty(mPropertyScrolling, true); + Vector2 currentScrollPosition = GetCurrentScrollPosition(); + Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, true); mScrolling = true; - mScrollStartedSignalV2.Emit( currentScrollPosition ); + DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignal 2 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y); + mScrollStartedSignal.Emit( currentScrollPosition ); } else if( (state == Gesture::Finished) || (state == Gesture::Cancelled) ) // Finished/default @@ -2203,47 +2487,57 @@ void ScrollView::OnGestureEx(Gesture::State state) mGestureStackDepth--; if(mGestureStackDepth==0) { + // no flick if we have not exceeded min flick distance + if( (fabsf(mPanDelta.x) < mMinFlickDistance.x) + && (fabsf(mPanDelta.y) < mMinFlickDistance.y) ) + { + // reset flick velocity + mLastVelocity = Vector2::ZERO; + } FinishTransform(); } + else + { + DALI_LOG_SCROLL_STATE("[0x%X] mGestureStackDepth[%d]", this, mGestureStackDepth); + } } } -void ScrollView::UpdateTransform() -{ -// TODO: notify clamps using property notifications (or see if we need this, can deprecate it) -} - void ScrollView::FinishTransform() { - const Vector3& scrollPosition = Self().GetProperty(mPropertyPosition); - - mScrollPostPosition.x = scrollPosition.x; - mScrollPostPosition.y = scrollPosition.y; - - Vector3 deltaPosition(mScrollPostPosition); - // Cement PRE transform (PRE = POST), and Begin Snap Animation if necessary. - WrapPosition(mScrollPostPosition); + // at this stage internal x and x scroll position should have followed prescroll position exactly + Actor self = Self(); - mDomainOffset += deltaPosition - mScrollPostPosition; - Self().SetProperty(mPropertyDomainOffset, mDomainOffset); + PreAnimatedScrollSetup(); + // convert pixels/millisecond to pixels per second bool animating = SnapWithVelocity(mLastVelocity * 1000.0f); if(!animating) { - AnimateOvershootToOrigin(0.0f, 0.0f); // if not animating, then this pan has completed right now. + SetScrollUpdateNotification(false); mScrolling = false; - Self().SetProperty(mPropertyScrolling, false); - Vector3 currentScrollPosition = GetCurrentScrollPosition(); - mScrollCompletedSignalV2.Emit( currentScrollPosition ); + Self().SetProperty(Toolkit::ScrollView::Property::SCROLLING, false); + + if( fabs(mScrollPrePosition.x - mScrollTargetPosition.x) > Math::MACHINE_EPSILON_10 ) + { + SnapInternalXTo(mScrollTargetPosition.x); + } + if( fabs(mScrollPrePosition.y - mScrollTargetPosition.y) > Math::MACHINE_EPSILON_10 ) + { + SnapInternalYTo(mScrollTargetPosition.y); + } + Vector2 currentScrollPosition = GetCurrentScrollPosition(); + DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignal 6 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y); + mScrollCompletedSignal.Emit( currentScrollPosition ); } } -Vector3 ScrollView::GetOvershoot(Vector3& position) const +Vector2 ScrollView::GetOvershoot(Vector2& position) const { Vector3 size = Self().GetCurrentSize(); - Vector3 overshoot; + Vector2 overshoot; const RulerDomain rulerDomainX = mRulerX->GetDomain(); const RulerDomain rulerDomainY = mRulerY->GetDomain(); @@ -2281,39 +2575,29 @@ Vector3 ScrollView::GetOvershoot(Vector3& position) const bool ScrollView::OnAccessibilityPan(PanGesture gesture) { + // Keep track of whether this is an AccessibilityPan + mInAccessibilityPan = true; OnPan(gesture); + mInAccessibilityPan = false; + return true; } -void ScrollView::ClampPosition(Vector3& position) const +void ScrollView::ClampPosition(Vector2& position) const { - ClampState3 clamped; + ClampState2D clamped; ClampPosition(position, clamped); } -void ScrollView::ClampPosition(Vector3& position, ClampState3 &clamped) const +void ScrollView::ClampPosition(Vector2& position, ClampState2D &clamped) const { Vector3 size = Self().GetCurrentSize(); - // determine size of viewport relative to current scaled size. - // e.g. if you're zoomed in 200%, then each pixel on screen is only 0.5 pixels on subject. - if(fabsf(mScrollPostScale.x) > Math::MACHINE_EPSILON_0) - { - size.x /= mScrollPostScale.x; - } - - if(fabsf(mScrollPostScale.y) > Math::MACHINE_EPSILON_0) - { - size.y /= mScrollPostScale.y; - } - position.x = -mRulerX->Clamp(-position.x, size.width, 1.0f, clamped.x); // NOTE: X & Y rulers think in -ve coordinate system. 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. - - clamped.z = NotClamped; } -void ScrollView::WrapPosition(Vector3& position) const +void ScrollView::WrapPosition(Vector2& position) const { if(mWrapMode) { @@ -2332,19 +2616,6 @@ void ScrollView::WrapPosition(Vector3& position) const } } -void ScrollView::ClampScale(Vector3& scale) const -{ - ClampState3 clamped; - ClampScale(scale, clamped); -} - -void ScrollView::ClampScale(Vector3& scale, ClampState3 &clamped) const -{ - scale.x = mRulerScaleX->Clamp(scale.x, 0.0f, 1.0f, clamped.x); - scale.y = mRulerScaleY->Clamp(scale.y, 0.0f, 1.0f, clamped.y); - clamped.z = NotClamped; -} - void ScrollView::UpdateMainInternalConstraint() { // TODO: Only update the constraints which have changed, rather than remove all and add all again. @@ -2352,13 +2623,18 @@ void ScrollView::UpdateMainInternalConstraint() Actor self = Self(); PanGestureDetector detector( GetPanGestureDetector() ); - if(mScrollMainInternalPrePositionConstraint) + if(mScrollMainInternalPositionConstraint) + { + mScrollMainInternalPositionConstraint.Remove(); + mScrollMainInternalDeltaConstraint.Remove(); + mScrollMainInternalFinalConstraint.Remove(); + mScrollMainInternalRelativeConstraint.Remove(); + mScrollMainInternalDomainConstraint.Remove(); + mScrollMainInternalPrePositionMaxConstraint.Remove(); + } + if( mScrollMainInternalPrePositionConstraint ) { - self.RemoveConstraint(mScrollMainInternalPrePositionConstraint); - self.RemoveConstraint(mScrollMainInternalPositionConstraint); - self.RemoveConstraint(mScrollMainInternalDeltaConstraint); - self.RemoveConstraint(mScrollMainInternalFinalConstraint); - self.RemoveConstraint(mScrollMainInternalRelativeConstraint); + mScrollMainInternalPrePositionConstraint.Remove(); } // TODO: It's probably better to use a local displacement value as this will give a displacement when scrolling just commences @@ -2367,98 +2643,108 @@ void ScrollView::UpdateMainInternalConstraint() // 1. First calculate the pre-position (this is the scroll position if no clamping has taken place) Vector2 initialPanMask = Vector2(mRulerX->IsEnabled() ? 1.0f : 0.0f, mRulerY->IsEnabled() ? 1.0f : 0.0f); - Constraint constraint = Constraint::New( mPropertyPrePosition, - Source( detector, PanGestureDetector::LOCAL_POSITION ), - Source( detector, PanGestureDetector::LOCAL_DISPLACEMENT ), - LocalSource( mPropertyX ), - LocalSource( mPropertyY ), - Source( self, mPropertyPanning ), - InternalPrePositionConstraint( initialPanMask, mAxisAutoLock, mAxisAutoLockGradient ) ); - mScrollMainInternalPrePositionConstraint = self.ApplyConstraint(constraint); - - // 2. Second calculate the clamped position (actual position) - constraint = Constraint::New( mPropertyPosition, - LocalSource( mPropertyPrePosition ), - Source( self, Actor::SIZE ), - InternalPositionConstraint( mRulerX->GetDomain(), - mRulerY->GetDomain()) ); - mScrollMainInternalPositionConstraint = self.ApplyConstraint(constraint); - - constraint = Constraint::New( mPropertyPositionDelta, - LocalSource( mPropertyPosition ), - LocalSource( mPropertyDomainOffset ), - InternalPositionDeltaConstraint ); - mScrollMainInternalDeltaConstraint = self.ApplyConstraint(constraint); - - constraint = Constraint::New( mPropertyFinal, - LocalSource( mPropertyPosition ), - LocalSource( mPropertyOvershootX ), - LocalSource( mPropertyOvershootY ), - InternalFinalConstraint( FinalDefaultAlphaFunction, - FinalDefaultAlphaFunction ) ); - mScrollMainInternalFinalConstraint = self.ApplyConstraint(constraint); - - constraint = Constraint::New( mPropertyRelativePosition, - LocalSource( mPropertyPosition ), - LocalSource( mPropertyPositionMin ), - LocalSource( mPropertyPositionMax ), - LocalSource( Actor::SIZE ), - InternalRelativePositionConstraint ); - mScrollMainInternalRelativeConstraint = self.ApplyConstraint(constraint); - - if(mScrollMainInternalOvershootXConstraint) - { - // reset these constraints in correct order - self.RemoveConstraint(mScrollMainInternalOvershootXConstraint); - mScrollMainInternalOvershootXConstraint.Reset(); - - Constraint constraint = Constraint::New( mPropertyOvershootX, - LocalSource( mPropertyPrePosition ), - LocalSource( mPropertyPosition ), - OvershootXConstraint(mMaxOvershoot.x) ); - mScrollMainInternalOvershootXConstraint = self.ApplyConstraint(constraint); + if( mLockAxis == LockVertical ) + { + initialPanMask.y = 0.0f; } - - if(mScrollMainInternalOvershootYConstraint) + else if( mLockAxis == LockHorizontal ) { - // reset these constraints in correct order - self.RemoveConstraint(mScrollMainInternalOvershootYConstraint); - mScrollMainInternalOvershootYConstraint.Reset(); + initialPanMask.x = 0.0f; + } - Constraint constraint = Constraint::New( mPropertyOvershootY, - LocalSource( mPropertyPrePosition ), - LocalSource( mPropertyPosition ), - OvershootXConstraint(mMaxOvershoot.y) ); - mScrollMainInternalOvershootYConstraint = self.ApplyConstraint(constraint); + if( mPanning ) + { + mScrollMainInternalPrePositionConstraint = Constraint::New( self, + Toolkit::ScrollView::Property::SCROLL_PRE_POSITION, + InternalPrePositionConstraint( mPanStartPosition, + initialPanMask, + mAxisAutoLock, + mAxisAutoLockGradient, + mLockAxis, + mMaxOvershoot, + mRulerX->GetDomain(), + mRulerY->GetDomain() ) ); + mScrollMainInternalPrePositionConstraint.AddSource( Source( detector, PanGestureDetector::Property::LOCAL_POSITION ) ); + mScrollMainInternalPrePositionConstraint.AddSource( Source( self, Actor::Property::SIZE ) ); + mScrollMainInternalPrePositionConstraint.Apply(); } + + // 2. Second calculate the clamped position (actual position) + mScrollMainInternalPositionConstraint = Constraint::New( self, + Toolkit::ScrollView::Property::SCROLL_POSITION, + InternalPositionConstraint( mRulerX->GetDomain(), + mRulerY->GetDomain(), + mWrapMode ) ); + mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) ); + mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) ); + mScrollMainInternalPositionConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) ); + mScrollMainInternalPositionConstraint.AddSource( Source( self, Actor::Property::SIZE ) ); + mScrollMainInternalPositionConstraint.Apply(); + + mScrollMainInternalDeltaConstraint = Constraint::New( self, Toolkit::ScrollView::Property::SCROLL_POSITION_DELTA, InternalPositionDeltaConstraint ); + mScrollMainInternalDeltaConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) ); + mScrollMainInternalDeltaConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_DOMAIN_OFFSET ) ); + mScrollMainInternalDeltaConstraint.Apply(); + + mScrollMainInternalFinalConstraint = Constraint::New( self, Toolkit::ScrollView::Property::SCROLL_FINAL, + InternalFinalConstraint( FinalDefaultAlphaFunction, + FinalDefaultAlphaFunction ) ); + mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) ); + mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::OVERSHOOT_X ) ); + mScrollMainInternalFinalConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::OVERSHOOT_Y ) ); + mScrollMainInternalFinalConstraint.Apply(); + + mScrollMainInternalRelativeConstraint = Constraint::New( self, Toolkit::Scrollable::Property::SCROLL_RELATIVE_POSITION, InternalRelativePositionConstraint ); + mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) ); + mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) ); + mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) ); + mScrollMainInternalRelativeConstraint.AddSource( LocalSource( Actor::Property::SIZE ) ); + mScrollMainInternalRelativeConstraint.Apply(); + + mScrollMainInternalDomainConstraint = Constraint::New( self, Toolkit::ScrollView::Property::SCROLL_DOMAIN_SIZE, InternalScrollDomainConstraint ); + mScrollMainInternalDomainConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) ); + mScrollMainInternalDomainConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) ); + mScrollMainInternalDomainConstraint.AddSource( LocalSource( Actor::Property::SIZE ) ); + mScrollMainInternalDomainConstraint.Apply(); + + mScrollMainInternalPrePositionMaxConstraint = Constraint::New( self, Toolkit::ScrollView::Property::SCROLL_PRE_POSITION_MAX, InternalPrePositionMaxConstraint ); + mScrollMainInternalPrePositionMaxConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) ); + mScrollMainInternalPrePositionMaxConstraint.AddSource( LocalSource( Actor::Property::SIZE ) ); + mScrollMainInternalPrePositionMaxConstraint.Apply(); + + // When panning we want to make sure overshoot values are affected by pre position and post position + SetOvershootConstraintsEnabled(!mWrapMode); } void ScrollView::SetOvershootConstraintsEnabled(bool enabled) { Actor self( Self() ); // remove and reset, it may now be in wrong order with the main internal constraints - if(mScrollMainInternalOvershootXConstraint) + if( mScrollMainInternalOvershootXConstraint ) { - self.RemoveConstraint(mScrollMainInternalOvershootXConstraint); + mScrollMainInternalOvershootXConstraint.Remove(); mScrollMainInternalOvershootXConstraint.Reset(); + mScrollMainInternalOvershootYConstraint.Remove(); + mScrollMainInternalOvershootYConstraint.Reset(); } - if(mScrollMainInternalOvershootYConstraint) + if( enabled ) { - self.RemoveConstraint(mScrollMainInternalOvershootYConstraint); - mScrollMainInternalOvershootYConstraint.Reset(); + mScrollMainInternalOvershootXConstraint= Constraint::New( self, Toolkit::ScrollView::Property::OVERSHOOT_X, OvershootXConstraint(mMaxOvershoot.x) ); + mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) ); + mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) ); + mScrollMainInternalOvershootXConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL ) ); + mScrollMainInternalOvershootXConstraint.Apply(); + + mScrollMainInternalOvershootYConstraint = Constraint::New( self, Toolkit::ScrollView::Property::OVERSHOOT_Y, OvershootYConstraint(mMaxOvershoot.y) ); + mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_PRE_POSITION ) ); + mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::ScrollView::Property::SCROLL_POSITION ) ); + mScrollMainInternalOvershootYConstraint.AddSource( LocalSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) ); + mScrollMainInternalOvershootYConstraint.Apply(); } - if(enabled) + else { - Constraint constraint = Constraint::New( mPropertyOvershootX, - LocalSource( mPropertyPrePosition ), - LocalSource( mPropertyPosition ), - OvershootXConstraint(mMaxOvershoot.x) ); - mScrollMainInternalOvershootXConstraint = self.ApplyConstraint(constraint); - constraint = Constraint::New( mPropertyOvershootY, - LocalSource( mPropertyPrePosition ), - LocalSource( mPropertyPosition ), - OvershootYConstraint(mMaxOvershoot.y) ); - mScrollMainInternalOvershootYConstraint = self.ApplyConstraint(constraint); + self.SetProperty(Toolkit::ScrollView::Property::OVERSHOOT_X, 0.0f); + self.SetProperty(Toolkit::ScrollView::Property::OVERSHOOT_Y, 0.0f); } } @@ -2470,157 +2756,29 @@ void ScrollView::SetInternalConstraints() // User definable constraints to apply to all child actors ////////////////// Actor self = Self(); - // LocalSource - The Actors to be moved. - // self - The ScrollView - - // Apply some default constraints to ScrollView. - // Movement + Scaling + Wrap function + // Apply some default constraints to ScrollView & its bound actors + // Movement + Wrap function Constraint constraint; - // MoveScaledActor (scrolling/zooming) - constraint = Constraint::New( Actor::POSITION, - Source( self, mPropertyPosition ), - Source( self, mPropertyScale ), - MoveScaledActorConstraint ); - constraint.SetRemoveAction(Constraint::Discard); - ApplyConstraintToBoundActors(constraint); - - // ScaleActor (scrolling/zooming) - constraint = Constraint::New( Actor::SCALE, - Source( self, mPropertyScale ), - ScaleActorConstraint ); + // MoveActor (scrolling) + constraint = Constraint::New( self, Actor::Property::POSITION, MoveActorConstraint ); + constraint.AddSource( Source( self, Toolkit::ScrollView::Property::SCROLL_POSITION ) ); constraint.SetRemoveAction(Constraint::Discard); ApplyConstraintToBoundActors(constraint); // WrapActor (wrap functionality) - constraint = Constraint::New( Actor::POSITION, - LocalSource( Actor::SCALE ), - LocalSource( Actor::ANCHOR_POINT ), - LocalSource( Actor::SIZE ), - Source( self, mPropertyPositionMin ), - Source( self, mPropertyPositionMax ), - Source( self, mPropertyWrap ), - WrapActorConstraint ); + constraint = Constraint::New( self, Actor::Property::POSITION, WrapActorConstraint ); + constraint.AddSource( LocalSource( Actor::Property::SCALE ) ); + constraint.AddSource( LocalSource( Actor::Property::ANCHOR_POINT ) ); + constraint.AddSource( LocalSource( Actor::Property::SIZE ) ); + constraint.AddSource( Source( self, Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) ); + constraint.AddSource( Source( self, Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) ); + constraint.AddSource( Source( self, Toolkit::ScrollView::Property::WRAP ) ); constraint.SetRemoveAction(Constraint::Discard); ApplyConstraintToBoundActors(constraint); } -void ScrollView::SetOvershootToOrigin() -{ - // Clear Snap animation if exists. - if(mSnapOvershootAnimation) - { - mSnapOvershootAnimation.FinishedSignal().Disconnect(this, &ScrollView::OnSnapOvershootAnimationFinished); - mSnapOvershootAnimation.Stop(); - mSnapOvershootAnimation.Clear(); - mSnapOvershootAnimation = NULL; - } - SetOvershootConstraintsEnabled(false); - Self().SetProperty(mPropertyOvershootX, 0.0f); - Self().SetProperty(mPropertyOvershootY, 0.0f); -} - -void ScrollView::AnimateOvershootToOrigin(float xDelay, float yDelay) -{ - if( IsScrollComponentEnabled(Toolkit::Scrollable::OvershootIndicator) ) - { - if(xDelay < Math::MACHINE_EPSILON_1) - { - // kick start animation to 0 - Self().SetProperty(mPropertyOvershootX, 0.0f); - } - if(yDelay < Math::MACHINE_EPSILON_1) - { - // kick start animation to 0 - Self().SetProperty(mPropertyOvershootY, 0.0f); - } - return; - } - // When we need to animate overshoot to 0 - if(mSnapOvershootDuration > Math::MACHINE_EPSILON_1) - { - Actor self = Self(); - // Clear Snap animation if exists. - if(mSnapOvershootAnimation) - { - mSnapOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollView::OnSnapOvershootAnimationFinished ); - mSnapOvershootAnimation.Stop(); - mSnapOvershootAnimation.Clear(); - mSnapOvershootAnimation = NULL; - } - if(!mSnapXAnimation && mScrollMainInternalOvershootXConstraint) - { - // need to remove the x overshoot constraint now or it will override animation to 0 - Self().RemoveConstraint(mScrollMainInternalOvershootXConstraint); - mScrollMainInternalOvershootXConstraint.Reset(); - mScrollMainInternalOvershootXConstraint = 0; - } - if(!mSnapYAnimation && mScrollMainInternalOvershootYConstraint) - { - // need to remove the y overshoot constraint now or it will override animation to 0 - Self().RemoveConstraint(mScrollMainInternalOvershootYConstraint); - mScrollMainInternalOvershootYConstraint.Reset(); - mScrollMainInternalOvershootYConstraint = 0; - } - // setup the new overshoot to 0 animation - float totalDuration = (xDelay > yDelay ? xDelay : yDelay) + mSnapOvershootDuration; - mSnapOvershootAnimation = Animation::New(totalDuration); - mSnapOvershootAnimation.FinishedSignal().Connect( this, &ScrollView::OnSnapOvershootAnimationFinished ); - - mSnapOvershootAnimation.AnimateTo( Property(self, mPropertyOvershootX), 0.0f, mSnapOvershootAlphaFunction, TimePeriod(xDelay, mSnapOvershootDuration) ); - mSnapOvershootAnimation.AnimateTo( Property(self, mPropertyOvershootY), 0.0f, mSnapOvershootAlphaFunction, TimePeriod(yDelay, mSnapOvershootDuration) ); - - mSnapOvershootAnimation.SetDuration(totalDuration); - mSnapOvershootAnimation.Play(); - } - else - { - SetOvershootToOrigin(); - } -} - -void ScrollView::OnSnapOvershootAnimationFinished( Animation& source ) -{ - mSnapOvershootAnimation = NULL; -} - -void ScrollView::StartRefreshTimer() -{ - if(mRefreshIntervalMilliseconds > 0) - { - if (!mRefreshTimer) - { - mRefreshTimer = Timer::New( mRefreshIntervalMilliseconds ); - mRefreshTimer.TickSignal().Connect( this, &ScrollView::OnRefreshTick ); - } - - if (!mRefreshTimer.IsRunning()) - { - mRefreshTimer.Start(); - } - } -} - -void ScrollView::CancelRefreshTimer() -{ - if (mRefreshTimer) - { - mRefreshTimer.Stop(); - } -} - -bool ScrollView::OnRefreshTick() -{ - // Guard against destruction during signal emission - Toolkit::ScrollView handle( GetOwner() ); - - Vector3 currentScrollPosition = GetCurrentScrollPosition(); - mScrollUpdatedSignalV2.Emit( currentScrollPosition ); - - return true; -} - } // namespace Internal } // namespace Toolkit