2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include <dali-toolkit/internal/controls/scroll-bar/scroll-bar-impl.h>
18 #include <dali-toolkit/internal/controls/scrollable/item-view/item-view-impl.h>
25 const char* DEFAULT_INDICATOR_IMAGE_PATH = DALI_IMAGE_DIR "popup_scroll.png";
26 const Vector4 DEFAULT_INDICATOR_NINE_PATCH_BORDER(4.0f, 9.0f, 7.0f, 11.0f);
27 const float MINIMUM_INDICATOR_HEIGHT(20.0f); // The minimum indicator height for the nine patch border
28 const float DEFAULT_SLIDER_DEPTH(1.0f);
29 const float INDICATOR_SHOW_TIME(0.5f);
30 const float INDICATOR_HIDE_TIME(0.5f);
31 const float DEFAULT_PAN_GESTURE_PROCESS_TIME(16.7f); // 16.7 milliseconds, i.e. one frame
32 const float DEFAULT_INDICATOR_FIXED_HEIGHT(80.0f);
35 * Indicator size constraint
36 * Indicator size depends on both indicator's parent size and the scroll content size
38 struct IndicatorSizeConstraint
41 * @param[in] contentSize The size of scrollable content
43 IndicatorSizeConstraint(float contentSize)
44 : mContentSize(contentSize)
50 * @param[in] current The current indicator size
51 * @param[in] parentSizeProperty The parent size of scroll indicator.
52 * @return The new scroll indicator size.
54 Vector3 operator()(const Vector3& current,
55 const PropertyInput& parentSizeProperty)
57 const Vector3& parentSize = parentSizeProperty.GetVector3();
58 float height = mContentSize > parentSize.height ? parentSize.height * ( parentSize.height / mContentSize ) : parentSize.height * ( (parentSize.height - mContentSize * 0.5f) / parentSize.height);
59 return Vector3( parentSize.width, std::max(MINIMUM_INDICATOR_HEIGHT, height), parentSize.depth );
62 float mContentSize; ///< The size of scrollable content
66 * Indicator position constraint
67 * Positions the indicator to reflect the current scroll position within the scroll domain.
69 struct IndicatorPositionConstraint
72 * @param[in] minPosition The minimum limit of scroll position
73 * @param[in] maxPosition the maximum limit of scroll position
75 IndicatorPositionConstraint(float minPosition, float maxPosition)
76 : mMinPosition(minPosition),
77 mMaxPosition(maxPosition)
83 * @param[in] current The current indicator position
84 * @param[in] indicatorSizeProperty The size of indicator.
85 * @param[in] parentSizeProperty The parent size of indicator.
86 * @param[in] scrollPositionProperty The scroll position of the scrollable container // (from 0.0 -> 1.0 in each axis)
87 * @return The new indicator position is returned.
89 Vector3 operator()(const Vector3& current,
90 const PropertyInput& indicatorSizeProperty,
91 const PropertyInput& parentSizeProperty,
92 const PropertyInput& scrollPositionProperty)
94 Vector3 indicatorSize = indicatorSizeProperty.GetVector3();
95 Vector3 parentSize = parentSizeProperty.GetVector3();
96 float scrollPosition = scrollPositionProperty.GetFloat();
98 const float domainSize = fabs(mMaxPosition - mMinPosition);
99 float relativePosition = (mMaxPosition - scrollPosition) / domainSize;
100 return Vector3(current.x, relativePosition * (parentSize.height - indicatorSize.height), DEFAULT_SLIDER_DEPTH);
103 float mMinPosition; ///< The minimum scroll position
104 float mMaxPosition; ///< The maximum scroll position
107 } // unnamed namespace
115 const Property::Index ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY( Internal::ScrollBar::SCROLLBAR_PROPERTY_START_INDEX );
116 const Property::Index ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT( Internal::ScrollBar::SCROLLBAR_PROPERTY_START_INDEX + 1 );
124 using namespace Dali;
126 const char* INDICATOR_HEIGHT_POLICY_NAME[] = {"Variable", "Fixed"};
130 return Toolkit::ScrollBar::New();
133 TypeRegistration typeRegistration( typeid(Toolkit::ScrollBar), typeid(Toolkit::ScrollComponent), Create );
135 PropertyRegistration property1( typeRegistration, "indicator-height-policy", Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY, Property::STRING, &ScrollBar::SetProperty, &ScrollBar::GetProperty );
136 PropertyRegistration property2( typeRegistration, "indicator-fixed-height", Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT, Property::FLOAT, &ScrollBar::SetProperty, &ScrollBar::GetProperty );
139 ScrollBar::ScrollBar()
140 : mScrollStart(0.0f),
142 mCurrentScrollPosition(0.0f),
143 mIndicatorHeightPolicy(Toolkit::ScrollBar::Variable),
144 mIndicatorFixedHeight(DEFAULT_INDICATOR_FIXED_HEIGHT)
148 ScrollBar::~ScrollBar()
152 void ScrollBar::OnInitialize()
156 Image indicatorImage = Image::New( DEFAULT_INDICATOR_IMAGE_PATH );
157 mIndicator = ImageActor::New( indicatorImage );
158 mIndicator.SetNinePatchBorder( DEFAULT_INDICATOR_NINE_PATCH_BORDER );
159 mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
160 mIndicator.SetParentOrigin( ParentOrigin::TOP_LEFT );
161 mIndicator.SetAnchorPoint( AnchorPoint::TOP_LEFT );
162 self.Add(mIndicator);
164 self.SetDrawMode(DrawMode::OVERLAY);
166 // Enable the pan gesture which is attached to the control
167 EnableGestureDetection(Gesture::Type(Gesture::Pan));
170 void ScrollBar::OnScrollConnectorSet( Toolkit::ScrollConnector oldConnector )
174 oldConnector.DomainChangedSignal().Disconnect(this, &ScrollBar::OnScrollDomainChanged);
176 mScrollPositionObject.Reset();
179 if( mScrollConnector )
181 mScrollPositionObject = mScrollConnector.GetScrollPositionObject();
184 mScrollConnector.DomainChangedSignal().Connect(this, &ScrollBar::OnScrollDomainChanged);
188 void ScrollBar::SetBackgroundImage( Image image, const Vector4& border )
192 mBackground = ImageActor::New( image );
193 mBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
194 mBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
195 Self().Add(mBackground);
199 mBackground.SetImage(image);
202 mBackground.SetNinePatchBorder( border );
203 mBackground.SetStyle( ImageActor::STYLE_NINE_PATCH );
206 void ScrollBar::SetIndicatorImage( Image image, const Vector4& border )
208 mIndicator.SetImage(image);
209 mIndicator.SetNinePatchBorder( border );
210 mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
213 Actor ScrollBar::GetScrollIndicator()
218 void ScrollBar::ApplyConstraints()
220 if( mScrollConnector )
222 Constraint constraint;
224 if(mIndicatorSizeConstraint)
226 mIndicator.RemoveConstraint(mIndicatorSizeConstraint);
229 // Set indicator height according to the indicator's height policy
230 if(mIndicatorHeightPolicy == Toolkit::ScrollBar::Fixed)
232 mIndicator.SetSize(Self().GetCurrentSize().width, mIndicatorFixedHeight);
236 constraint = Constraint::New<Vector3>( Actor::SIZE,
237 ParentSource( Actor::SIZE ),
238 IndicatorSizeConstraint( mScrollConnector.GetContentLength() ) );
239 mIndicatorSizeConstraint = mIndicator.ApplyConstraint( constraint );
242 if(mIndicatorPositionConstraint)
244 mIndicator.RemoveConstraint(mIndicatorPositionConstraint);
247 constraint = Constraint::New<Vector3>( Actor::POSITION,
248 LocalSource( Actor::SIZE ),
249 ParentSource( Actor::SIZE ),
250 Source( mScrollPositionObject, Toolkit::ScrollConnector::SCROLL_POSITION ),
251 IndicatorPositionConstraint( mScrollConnector.GetMinLimit(), mScrollConnector.GetMaxLimit() ) );
252 mIndicatorPositionConstraint = mIndicator.ApplyConstraint( constraint );
256 mBackground.RemoveConstraints();
258 constraint = Constraint::New<Vector3>(Actor::SIZE,
259 ParentSource(Actor::SIZE),
260 EqualToConstraint());
261 mBackground.ApplyConstraint(constraint);
266 void ScrollBar::SetPositionNotifications( const std::vector<float>& positions )
268 if(mScrollPositionObject)
270 if(mPositionNotification)
272 mScrollPositionObject.RemovePropertyNotification(mPositionNotification);
274 mPositionNotification = mScrollPositionObject.AddPropertyNotification( Toolkit::ScrollConnector::SCROLL_POSITION, VariableStepCondition(positions) );
275 mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionNotified );
279 void ScrollBar::OnScrollPositionNotified(PropertyNotification& source)
281 // Emit the signal to notify the scroll position crossing
282 mScrollPositionNotifiedSignal.Emit(mScrollPositionObject.GetProperty<float>( Toolkit::ScrollConnector::SCROLL_POSITION ));
285 void ScrollBar::Show()
287 // Cancel any animation
294 mAnimation = Animation::New( INDICATOR_SHOW_TIME );
295 mAnimation.OpacityTo( Self(), 1.0f, AlphaFunctions::EaseIn );
299 void ScrollBar::Hide()
301 // Cancel any animation
308 mAnimation = Animation::New( INDICATOR_HIDE_TIME );
309 mAnimation.OpacityTo( Self(), 0.0f, AlphaFunctions::EaseIn );
313 bool ScrollBar::OnPanGestureProcessTick()
315 // Update the scroll position property.
316 mScrollPositionObject.SetProperty( Toolkit::ScrollConnector::SCROLL_POSITION, mCurrentScrollPosition );
318 Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
321 // Refresh ItemView immediately when the scroll position is changed.
322 GetImpl(itemView).DoRefresh(mCurrentScrollPosition, false); // No need to cache extra items.
328 void ScrollBar::OnPan( PanGesture gesture )
330 if(mScrollPositionObject)
332 switch(gesture.state)
334 case Gesture::Started:
338 // Make sure the pan gesture is only being processed once per frame.
339 mTimer = Timer::New( DEFAULT_PAN_GESTURE_PROCESS_TIME );
340 mTimer.TickSignal().Connect( this, &ScrollBar::OnPanGestureProcessTick );
345 mScrollStart = mScrollPositionObject.GetProperty<float>( Toolkit::ScrollConnector::SCROLL_POSITION );
346 mGestureDisplacement = Vector3::ZERO;
351 case Gesture::Continuing:
353 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
354 mGestureDisplacement+=delta;
356 Vector3 span = Self().GetCurrentSize() - mIndicator.GetCurrentSize();
357 const float domainSize = fabs(mScrollConnector.GetMaxLimit() - mScrollConnector.GetMinLimit());
358 mCurrentScrollPosition = mScrollStart - mGestureDisplacement.y * domainSize / span.y;
359 mCurrentScrollPosition = std::min(mScrollConnector.GetMaxLimit(), std::max(mCurrentScrollPosition, mScrollConnector.GetMinLimit()));
369 // Destroy the timer when pan gesture is finished.
371 mTimer.TickSignal().Disconnect( this, &ScrollBar::OnPanGestureProcessTick );
379 Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
382 // Disable automatic refresh in ItemView during fast scrolling
383 GetImpl(itemView).SetRefreshEnabled(!mIsPanning);
388 void ScrollBar::OnScrollDomainChanged(float minPosition, float maxPosition, float contentSize)
390 // Reapply constraints when the scroll domain is changed
394 void ScrollBar::SetIndicatorHeightPolicy( Toolkit::ScrollBar::IndicatorHeightPolicy policy )
396 mIndicatorHeightPolicy = policy;
400 Toolkit::ScrollBar::IndicatorHeightPolicy ScrollBar::GetIndicatorHeightPolicy()
402 return mIndicatorHeightPolicy;
405 void ScrollBar::SetIndicatorFixedHeight( float height )
407 mIndicatorFixedHeight = height;
411 float ScrollBar::GetIndicatorFixedHeight()
413 return mIndicatorFixedHeight;
416 void ScrollBar::OnIndicatorHeightPolicyPropertySet( Property::Value propertyValue )
418 std::string policyName( propertyValue.Get<std::string>() );
419 if(policyName == "Variable")
421 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Variable);
423 else if(policyName == "Fixed")
425 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Fixed);
429 DALI_ASSERT_ALWAYS( !"ScrollBar::OnIndicatorHeightPolicyPropertySet(). Invalid Property value." );
433 void ScrollBar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
435 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
439 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
442 case Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY:
444 scrollBarImpl.OnIndicatorHeightPolicyPropertySet( value );
447 case Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT:
449 scrollBarImpl.SetIndicatorFixedHeight(value.Get<float>());
456 Property::Value ScrollBar::GetProperty( BaseObject* object, Property::Index index )
458 Property::Value value;
460 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
464 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
467 case Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY:
469 value = INDICATOR_HEIGHT_POLICY_NAME[ scrollBarImpl.GetIndicatorHeightPolicy() ];
472 case Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT:
474 value = scrollBarImpl.GetIndicatorFixedHeight();
482 Toolkit::ScrollBar ScrollBar::New()
484 // Create the implementation, temporarily owned by this handle on stack
485 IntrusivePtr< ScrollBar > impl = new ScrollBar();
487 // Pass ownership to CustomActor handle
488 Toolkit::ScrollBar handle( *impl );
490 // Second-phase init of the implementation
491 // This can only be done after the CustomActor connection has been made...
497 } // namespace Internal
499 } // namespace Toolkit