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::SetIndicatorImage( Image image )
191 mIndicator.SetImage(image);
194 Actor ScrollBar::GetScrollIndicator()
199 void ScrollBar::ApplyConstraints()
201 if( mScrollConnector )
203 Constraint constraint;
205 if(mIndicatorSizeConstraint)
207 mIndicator.RemoveConstraint(mIndicatorSizeConstraint);
210 // Set indicator height according to the indicator's height policy
211 if(mIndicatorHeightPolicy == Toolkit::ScrollBar::Fixed)
213 mIndicator.SetSize(Self().GetCurrentSize().width, mIndicatorFixedHeight);
217 constraint = Constraint::New<Vector3>( Actor::SIZE,
218 ParentSource( Actor::SIZE ),
219 IndicatorSizeConstraint( mScrollConnector.GetContentLength() ) );
220 mIndicatorSizeConstraint = mIndicator.ApplyConstraint( constraint );
223 if(mIndicatorPositionConstraint)
225 mIndicator.RemoveConstraint(mIndicatorPositionConstraint);
228 constraint = Constraint::New<Vector3>( Actor::POSITION,
229 LocalSource( Actor::SIZE ),
230 ParentSource( Actor::SIZE ),
231 Source( mScrollPositionObject, Toolkit::ScrollConnector::SCROLL_POSITION ),
232 IndicatorPositionConstraint( mScrollConnector.GetMinLimit(), mScrollConnector.GetMaxLimit() ) );
233 mIndicatorPositionConstraint = mIndicator.ApplyConstraint( constraint );
237 mBackground.RemoveConstraints();
239 constraint = Constraint::New<Vector3>(Actor::SIZE,
240 ParentSource(Actor::SIZE),
241 EqualToConstraint());
242 mBackground.ApplyConstraint(constraint);
247 void ScrollBar::SetPositionNotifications( const std::vector<float>& positions )
249 if(mScrollPositionObject)
251 if(mPositionNotification)
253 mScrollPositionObject.RemovePropertyNotification(mPositionNotification);
255 mPositionNotification = mScrollPositionObject.AddPropertyNotification( Toolkit::ScrollConnector::SCROLL_POSITION, VariableStepCondition(positions) );
256 mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionNotified );
260 void ScrollBar::OnScrollPositionNotified(PropertyNotification& source)
262 // Emit the signal to notify the scroll position crossing
263 mScrollPositionNotifiedSignal.Emit(mScrollConnector.GetScrollPosition());
266 void ScrollBar::Show()
268 // Cancel any animation
275 mAnimation = Animation::New( INDICATOR_SHOW_TIME );
276 mAnimation.OpacityTo( Self(), 1.0f, AlphaFunctions::EaseIn );
280 void ScrollBar::Hide()
282 // Cancel any animation
289 mAnimation = Animation::New( INDICATOR_HIDE_TIME );
290 mAnimation.OpacityTo( Self(), 0.0f, AlphaFunctions::EaseIn );
294 bool ScrollBar::OnPanGestureProcessTick()
296 // Update the scroll position property.
297 if( mScrollConnector )
299 mScrollConnector.SetScrollPosition(mCurrentScrollPosition);
305 void ScrollBar::OnPan( PanGesture gesture )
309 Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
311 switch(gesture.state)
313 case Gesture::Started:
317 // Make sure the pan gesture is only being processed once per frame.
318 mTimer = Timer::New( DEFAULT_PAN_GESTURE_PROCESS_TIME );
319 mTimer.TickSignal().Connect( this, &ScrollBar::OnPanGestureProcessTick );
324 mScrollStart = mScrollConnector.GetScrollPosition();
325 mGestureDisplacement = Vector3::ZERO;
330 case Gesture::Continuing:
332 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
333 mGestureDisplacement+=delta;
335 Vector3 span = Self().GetCurrentSize() - mIndicator.GetCurrentSize();
336 const float domainSize = fabs(mScrollConnector.GetMaxLimit() - mScrollConnector.GetMinLimit());
337 mCurrentScrollPosition = mScrollStart - mGestureDisplacement.y * domainSize / span.y;
338 mCurrentScrollPosition = std::min(mScrollConnector.GetMaxLimit(), std::max(mCurrentScrollPosition, mScrollConnector.GetMinLimit()));
348 // Destroy the timer when pan gesture is finished.
350 mTimer.TickSignal().Disconnect( this, &ScrollBar::OnPanGestureProcessTick );
356 // Refresh the ItemView cache with extra items
357 GetImpl(itemView).DoRefresh(mCurrentScrollPosition, true);
366 // Disable automatic refresh in ItemView during fast scrolling
367 GetImpl(itemView).SetRefreshEnabled(!mIsPanning);
372 void ScrollBar::OnScrollDomainChanged(float minPosition, float maxPosition, float contentSize)
374 // Reapply constraints when the scroll domain is changed
378 void ScrollBar::SetIndicatorHeightPolicy( Toolkit::ScrollBar::IndicatorHeightPolicy policy )
380 mIndicatorHeightPolicy = policy;
384 Toolkit::ScrollBar::IndicatorHeightPolicy ScrollBar::GetIndicatorHeightPolicy()
386 return mIndicatorHeightPolicy;
389 void ScrollBar::SetIndicatorFixedHeight( float height )
391 mIndicatorFixedHeight = height;
395 float ScrollBar::GetIndicatorFixedHeight()
397 return mIndicatorFixedHeight;
400 void ScrollBar::OnIndicatorHeightPolicyPropertySet( Property::Value propertyValue )
402 std::string policyName( propertyValue.Get<std::string>() );
403 if(policyName == "Variable")
405 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Variable);
407 else if(policyName == "Fixed")
409 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Fixed);
413 DALI_ASSERT_ALWAYS( !"ScrollBar::OnIndicatorHeightPolicyPropertySet(). Invalid Property value." );
417 void ScrollBar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
419 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
423 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
426 case Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY:
428 scrollBarImpl.OnIndicatorHeightPolicyPropertySet( value );
431 case Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT:
433 scrollBarImpl.SetIndicatorFixedHeight(value.Get<float>());
440 Property::Value ScrollBar::GetProperty( BaseObject* object, Property::Index index )
442 Property::Value value;
444 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
448 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
451 case Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY:
453 value = INDICATOR_HEIGHT_POLICY_NAME[ scrollBarImpl.GetIndicatorHeightPolicy() ];
456 case Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT:
458 value = scrollBarImpl.GetIndicatorFixedHeight();
466 Toolkit::ScrollBar ScrollBar::New()
468 // Create the implementation, temporarily owned by this handle on stack
469 IntrusivePtr< ScrollBar > impl = new ScrollBar();
471 // Pass ownership to CustomActor handle
472 Toolkit::ScrollBar handle( *impl );
474 // Second-phase init of the implementation
475 // This can only be done after the CustomActor connection has been made...
481 } // namespace Internal
483 } // namespace Toolkit