2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali-toolkit/internal/controls/scroll-bar/scroll-bar-impl.h>
19 #include <dali-toolkit/internal/controls/scrollable/item-view/item-view-impl.h>
26 const char* DEFAULT_INDICATOR_IMAGE_PATH = DALI_IMAGE_DIR "popup_scroll.png";
27 const Vector4 DEFAULT_INDICATOR_NINE_PATCH_BORDER(4.0f, 9.0f, 7.0f, 11.0f);
28 const float MINIMUM_INDICATOR_HEIGHT(20.0f); // The minimum indicator height for the nine patch border
29 const float DEFAULT_SLIDER_DEPTH(1.0f);
30 const float INDICATOR_SHOW_TIME(0.5f);
31 const float INDICATOR_HIDE_TIME(0.5f);
32 const float DEFAULT_PAN_GESTURE_PROCESS_TIME(16.7f); // 16.7 milliseconds, i.e. one frame
33 const float DEFAULT_INDICATOR_FIXED_HEIGHT(80.0f);
36 * Indicator size constraint
37 * Indicator size depends on both indicator's parent size and the scroll content size
39 struct IndicatorSizeConstraint
42 * @param[in] contentSize The size of scrollable content
44 IndicatorSizeConstraint(float contentSize)
45 : mContentSize(contentSize)
51 * @param[in] current The current indicator size
52 * @param[in] parentSizeProperty The parent size of scroll indicator.
53 * @return The new scroll indicator size.
55 Vector3 operator()(const Vector3& current,
56 const PropertyInput& parentSizeProperty)
58 const Vector3& parentSize = parentSizeProperty.GetVector3();
59 float height = mContentSize > parentSize.height ? parentSize.height * ( parentSize.height / mContentSize ) : parentSize.height * ( (parentSize.height - mContentSize * 0.5f) / parentSize.height);
60 return Vector3( parentSize.width, std::max(MINIMUM_INDICATOR_HEIGHT, height), parentSize.depth );
63 float mContentSize; ///< The size of scrollable content
67 * Indicator position constraint
68 * Positions the indicator to reflect the current scroll position within the scroll domain.
70 struct IndicatorPositionConstraint
73 * @param[in] minPosition The minimum limit of scroll position
74 * @param[in] maxPosition the maximum limit of scroll position
76 IndicatorPositionConstraint(float minPosition, float maxPosition)
77 : mMinPosition(minPosition),
78 mMaxPosition(maxPosition)
84 * @param[in] current The current indicator position
85 * @param[in] indicatorSizeProperty The size of indicator.
86 * @param[in] parentSizeProperty The parent size of indicator.
87 * @param[in] scrollPositionProperty The scroll position of the scrollable container // (from 0.0 -> 1.0 in each axis)
88 * @return The new indicator position is returned.
90 Vector3 operator()(const Vector3& current,
91 const PropertyInput& indicatorSizeProperty,
92 const PropertyInput& parentSizeProperty,
93 const PropertyInput& scrollPositionProperty)
95 Vector3 indicatorSize = indicatorSizeProperty.GetVector3();
96 Vector3 parentSize = parentSizeProperty.GetVector3();
97 float scrollPosition = scrollPositionProperty.GetFloat();
99 const float domainSize = fabs(mMaxPosition - mMinPosition);
100 float relativePosition = (mMaxPosition - scrollPosition) / domainSize;
101 return Vector3(current.x, relativePosition * (parentSize.height - indicatorSize.height), DEFAULT_SLIDER_DEPTH);
104 float mMinPosition; ///< The minimum scroll position
105 float mMaxPosition; ///< The maximum scroll position
108 } // unnamed namespace
116 const Property::Index ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY( Internal::ScrollBar::SCROLLBAR_PROPERTY_START_INDEX );
117 const Property::Index ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT( Internal::ScrollBar::SCROLLBAR_PROPERTY_START_INDEX + 1 );
125 using namespace Dali;
127 const char* INDICATOR_HEIGHT_POLICY_NAME[] = {"Variable", "Fixed"};
131 return Toolkit::ScrollBar::New();
134 TypeRegistration typeRegistration( typeid(Toolkit::ScrollBar), typeid(Toolkit::ScrollComponent), Create );
136 PropertyRegistration property1( typeRegistration, "indicator-height-policy", Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY, Property::STRING, &ScrollBar::SetProperty, &ScrollBar::GetProperty );
137 PropertyRegistration property2( typeRegistration, "indicator-fixed-height", Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT, Property::FLOAT, &ScrollBar::SetProperty, &ScrollBar::GetProperty );
140 ScrollBar::ScrollBar()
141 : mScrollStart(0.0f),
143 mCurrentScrollPosition(0.0f),
144 mIndicatorHeightPolicy(Toolkit::ScrollBar::Variable),
145 mIndicatorFixedHeight(DEFAULT_INDICATOR_FIXED_HEIGHT)
149 ScrollBar::~ScrollBar()
153 void ScrollBar::OnInitialize()
157 Image indicatorImage = Image::New( DEFAULT_INDICATOR_IMAGE_PATH );
158 mIndicator = ImageActor::New( indicatorImage );
159 mIndicator.SetNinePatchBorder( DEFAULT_INDICATOR_NINE_PATCH_BORDER );
160 mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
161 mIndicator.SetParentOrigin( ParentOrigin::TOP_LEFT );
162 mIndicator.SetAnchorPoint( AnchorPoint::TOP_LEFT );
163 self.Add(mIndicator);
165 self.SetDrawMode(DrawMode::OVERLAY);
167 // Enable the pan gesture which is attached to the control
168 EnableGestureDetection(Gesture::Type(Gesture::Pan));
171 void ScrollBar::OnScrollConnectorSet( Toolkit::ScrollConnector oldConnector )
175 oldConnector.DomainChangedSignal().Disconnect(this, &ScrollBar::OnScrollDomainChanged);
177 mScrollPositionObject.Reset();
180 if( mScrollConnector )
182 mScrollPositionObject = mScrollConnector.GetScrollPositionObject();
185 mScrollConnector.DomainChangedSignal().Connect(this, &ScrollBar::OnScrollDomainChanged);
189 void ScrollBar::SetBackgroundImage( Image image, const Vector4& border )
193 mBackground = ImageActor::New( image );
194 mBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
195 mBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
196 Self().Add(mBackground);
200 mBackground.SetImage(image);
202 mBackground.SetNinePatchBorder( border );
203 mBackground.SetStyle( ImageActor::STYLE_NINE_PATCH );
206 void ScrollBar::SetIndicatorImage( Image image )
208 mIndicator.SetImage(image);
211 void ScrollBar::SetIndicatorImage( Image image, const Vector4& border )
213 mIndicator.SetImage(image);
214 mIndicator.SetNinePatchBorder( border );
215 mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
218 Actor ScrollBar::GetScrollIndicator()
223 void ScrollBar::ApplyConstraints()
225 if( mScrollConnector )
227 Constraint constraint;
229 if(mIndicatorSizeConstraint)
231 mIndicator.RemoveConstraint(mIndicatorSizeConstraint);
234 // Set indicator height according to the indicator's height policy
235 if(mIndicatorHeightPolicy == Toolkit::ScrollBar::Fixed)
237 mIndicator.SetSize(Self().GetCurrentSize().width, mIndicatorFixedHeight);
241 constraint = Constraint::New<Vector3>( Actor::SIZE,
242 ParentSource( Actor::SIZE ),
243 IndicatorSizeConstraint( mScrollConnector.GetContentLength() ) );
244 mIndicatorSizeConstraint = mIndicator.ApplyConstraint( constraint );
247 if(mIndicatorPositionConstraint)
249 mIndicator.RemoveConstraint(mIndicatorPositionConstraint);
252 constraint = Constraint::New<Vector3>( Actor::POSITION,
253 LocalSource( Actor::SIZE ),
254 ParentSource( Actor::SIZE ),
255 Source( mScrollPositionObject, Toolkit::ScrollConnector::SCROLL_POSITION ),
256 IndicatorPositionConstraint( mScrollConnector.GetMinLimit(), mScrollConnector.GetMaxLimit() ) );
257 mIndicatorPositionConstraint = mIndicator.ApplyConstraint( constraint );
261 mBackground.RemoveConstraints();
263 constraint = Constraint::New<Vector3>(Actor::SIZE,
264 ParentSource(Actor::SIZE),
265 EqualToConstraint());
266 mBackground.ApplyConstraint(constraint);
271 void ScrollBar::SetPositionNotifications( const std::vector<float>& positions )
273 if(mScrollPositionObject)
275 if(mPositionNotification)
277 mScrollPositionObject.RemovePropertyNotification(mPositionNotification);
279 mPositionNotification = mScrollPositionObject.AddPropertyNotification( Toolkit::ScrollConnector::SCROLL_POSITION, VariableStepCondition(positions) );
280 mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionNotified );
284 void ScrollBar::OnScrollPositionNotified(PropertyNotification& source)
286 // Emit the signal to notify the scroll position crossing
287 mScrollPositionNotifiedSignal.Emit(mScrollPositionObject.GetProperty<float>( Toolkit::ScrollConnector::SCROLL_POSITION ));
290 void ScrollBar::Show()
292 // Cancel any animation
299 mAnimation = Animation::New( INDICATOR_SHOW_TIME );
300 mAnimation.OpacityTo( Self(), 1.0f, AlphaFunctions::EaseIn );
304 void ScrollBar::Hide()
306 // Cancel any animation
313 mAnimation = Animation::New( INDICATOR_HIDE_TIME );
314 mAnimation.OpacityTo( Self(), 0.0f, AlphaFunctions::EaseIn );
318 bool ScrollBar::OnPanGestureProcessTick()
320 // Update the scroll position property.
321 mScrollPositionObject.SetProperty( Toolkit::ScrollConnector::SCROLL_POSITION, mCurrentScrollPosition );
323 Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
326 // Refresh ItemView immediately when the scroll position is changed.
327 GetImpl(itemView).DoRefresh(mCurrentScrollPosition, false); // No need to cache extra items.
333 void ScrollBar::OnPan( PanGesture gesture )
335 if(mScrollPositionObject)
337 switch(gesture.state)
339 case Gesture::Started:
343 // Make sure the pan gesture is only being processed once per frame.
344 mTimer = Timer::New( DEFAULT_PAN_GESTURE_PROCESS_TIME );
345 mTimer.TickSignal().Connect( this, &ScrollBar::OnPanGestureProcessTick );
350 mScrollStart = mScrollPositionObject.GetProperty<float>( Toolkit::ScrollConnector::SCROLL_POSITION );
351 mGestureDisplacement = Vector3::ZERO;
356 case Gesture::Continuing:
358 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
359 mGestureDisplacement+=delta;
361 Vector3 span = Self().GetCurrentSize() - mIndicator.GetCurrentSize();
362 const float domainSize = fabs(mScrollConnector.GetMaxLimit() - mScrollConnector.GetMinLimit());
363 mCurrentScrollPosition = mScrollStart - mGestureDisplacement.y * domainSize / span.y;
364 mCurrentScrollPosition = std::min(mScrollConnector.GetMaxLimit(), std::max(mCurrentScrollPosition, mScrollConnector.GetMinLimit()));
374 // Destroy the timer when pan gesture is finished.
376 mTimer.TickSignal().Disconnect( this, &ScrollBar::OnPanGestureProcessTick );
384 Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
387 // Disable automatic refresh in ItemView during fast scrolling
388 GetImpl(itemView).SetRefreshEnabled(!mIsPanning);
393 void ScrollBar::OnScrollDomainChanged(float minPosition, float maxPosition, float contentSize)
395 // Reapply constraints when the scroll domain is changed
399 void ScrollBar::SetIndicatorHeightPolicy( Toolkit::ScrollBar::IndicatorHeightPolicy policy )
401 mIndicatorHeightPolicy = policy;
405 Toolkit::ScrollBar::IndicatorHeightPolicy ScrollBar::GetIndicatorHeightPolicy()
407 return mIndicatorHeightPolicy;
410 void ScrollBar::SetIndicatorFixedHeight( float height )
412 mIndicatorFixedHeight = height;
416 float ScrollBar::GetIndicatorFixedHeight()
418 return mIndicatorFixedHeight;
421 void ScrollBar::OnIndicatorHeightPolicyPropertySet( Property::Value propertyValue )
423 std::string policyName( propertyValue.Get<std::string>() );
424 if(policyName == "Variable")
426 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Variable);
428 else if(policyName == "Fixed")
430 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Fixed);
434 DALI_ASSERT_ALWAYS( !"ScrollBar::OnIndicatorHeightPolicyPropertySet(). Invalid Property value." );
438 void ScrollBar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
440 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
444 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
447 case Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY:
449 scrollBarImpl.OnIndicatorHeightPolicyPropertySet( value );
452 case Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT:
454 scrollBarImpl.SetIndicatorFixedHeight(value.Get<float>());
461 Property::Value ScrollBar::GetProperty( BaseObject* object, Property::Index index )
463 Property::Value value;
465 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
469 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
472 case Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY:
474 value = INDICATOR_HEIGHT_POLICY_NAME[ scrollBarImpl.GetIndicatorHeightPolicy() ];
477 case Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT:
479 value = scrollBarImpl.GetIndicatorFixedHeight();
487 Toolkit::ScrollBar ScrollBar::New()
489 // Create the implementation, temporarily owned by this handle on stack
490 IntrusivePtr< ScrollBar > impl = new ScrollBar();
492 // Pass ownership to CustomActor handle
493 Toolkit::ScrollBar handle( *impl );
495 // Second-phase init of the implementation
496 // This can only be done after the CustomActor connection has been made...
502 } // namespace Internal
504 } // namespace Toolkit