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(0.0f, 12.0f, 14.0f, 14.0f);
27 const float DEFAULT_SLIDER_DEPTH(1.0f);
28 const float INDICATOR_SHOW_TIME(0.5f);
29 const float INDICATOR_HIDE_TIME(0.5f);
30 const float DEFAULT_PAN_GESTURE_PROCESS_TIME(16.7f); // 16.7 milliseconds, i.e. one frame
33 * Indicator size constraint
34 * Indicator size depends on both indicator's parent size and the scroll content size
36 struct IndicatorSizeConstraint
39 * @param[in] contentSize The size of scrollable content
41 IndicatorSizeConstraint(float contentSize)
42 : mContentSize(contentSize)
48 * @param[in] current The current indicator size
49 * @param[in] parentSizeProperty The parent size of scroll indicator.
50 * @return The new scroll indicator size.
52 Vector3 operator()(const Vector3& current,
53 const PropertyInput& parentSizeProperty)
55 const Vector3& parentSize = parentSizeProperty.GetVector3();
56 float height = mContentSize > parentSize.height ? parentSize.height * ( parentSize.height / mContentSize ) : parentSize.height * ( (parentSize.height - mContentSize * 0.5f) / parentSize.height);
57 return Vector3( parentSize.width, height, parentSize.depth );
60 float mContentSize; ///< The size of scrollable content
64 * Indicator position constraint
65 * Positions the indicator to reflect the current scroll position within the scroll domain.
67 struct IndicatorPositionConstraint
70 * @param[in] minPosition The minimum limit of scroll position
71 * @param[in] maxPosition the maximum limit of scroll position
73 IndicatorPositionConstraint(float minPosition, float maxPosition)
74 : mMinPosition(minPosition),
75 mMaxPosition(maxPosition)
81 * @param[in] current The current indicator position
82 * @param[in] indicatorSizeProperty The size of indicator.
83 * @param[in] parentSizeProperty The parent size of indicator.
84 * @param[in] scrollPositionProperty The scroll position of the scrollable container // (from 0.0 -> 1.0 in each axis)
85 * @return The new indicator position is returned.
87 Vector3 operator()(const Vector3& current,
88 const PropertyInput& indicatorSizeProperty,
89 const PropertyInput& parentSizeProperty,
90 const PropertyInput& scrollPositionProperty)
92 Vector3 indicatorSize = indicatorSizeProperty.GetVector3();
93 Vector3 parentSize = parentSizeProperty.GetVector3();
94 float scrollPosition = scrollPositionProperty.GetFloat();
96 const float domainSize = fabs(mMaxPosition - mMinPosition);
97 float relativePosition = (mMaxPosition - scrollPosition) / domainSize;
98 return Vector3(current.x, relativePosition * (parentSize.height - indicatorSize.height), DEFAULT_SLIDER_DEPTH);
101 float mMinPosition; ///< The minimum scroll position
102 float mMaxPosition; ///< The maximum scroll position
105 } // unnamed namespace
119 using namespace Dali;
126 TypeRegistration mType( typeid(Toolkit::ScrollBar), typeid(Toolkit::Control), Create );
130 ScrollBar::ScrollBar()
131 : mScrollStart(0.0f),
133 mCurrentScrollPosition(0.0f)
137 ScrollBar::~ScrollBar()
141 void ScrollBar::OnInitialize()
145 Image indicatorImage = Image::New( DEFAULT_INDICATOR_IMAGE_PATH );
146 mIndicator = ImageActor::New( indicatorImage );
147 mIndicator.SetNinePatchBorder( DEFAULT_INDICATOR_NINE_PATCH_BORDER );
148 mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
149 mIndicator.SetParentOrigin( ParentOrigin::TOP_LEFT );
150 mIndicator.SetAnchorPoint( AnchorPoint::TOP_LEFT );
151 self.Add(mIndicator);
153 self.SetDrawMode(DrawMode::OVERLAY);
155 // Enable the pan gesture which is attached to the control
156 EnableGestureDetection(Gesture::Type(Gesture::Pan));
159 void ScrollBar::OnScrollConnectorSet( Toolkit::ScrollConnector oldConnector )
163 oldConnector.DomainChangedSignal().Disconnect(this, &ScrollBar::OnScrollDomainChanged);
165 mScrollPositionObject.Reset();
168 if( mScrollConnector )
170 mScrollPositionObject = mScrollConnector.GetScrollPositionObject();
173 mScrollConnector.DomainChangedSignal().Connect(this, &ScrollBar::OnScrollDomainChanged);
177 void ScrollBar::SetBackgroundImage( Image image, const Vector4& border )
181 mBackground = ImageActor::New( image );
182 mBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
183 mBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
184 Self().Add(mBackground);
188 mBackground.SetImage(image);
191 mBackground.SetNinePatchBorder( border );
192 mBackground.SetStyle( ImageActor::STYLE_NINE_PATCH );
195 void ScrollBar::SetIndicatorImage( Image image, const Vector4& border )
197 mIndicator.SetImage(image);
198 mIndicator.SetNinePatchBorder( border );
199 mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
202 Actor ScrollBar::GetScrollIndicator()
207 void ScrollBar::ApplyConstraints()
209 mIndicator.RemoveConstraints();
211 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
212 ParentSource( Actor::SIZE ),
213 IndicatorSizeConstraint( mScrollConnector.GetContentLength() ) );
214 mIndicator.ApplyConstraint( constraint );
216 constraint = Constraint::New<Vector3>( Actor::POSITION,
217 LocalSource( Actor::SIZE ),
218 ParentSource( Actor::SIZE ),
219 Source( mScrollPositionObject, Toolkit::ScrollConnector::SCROLL_POSITION ),
220 IndicatorPositionConstraint( mScrollConnector.GetMinLimit(), mScrollConnector.GetMaxLimit() ) );
221 mIndicator.ApplyConstraint( constraint );
225 mBackground.RemoveConstraints();
227 constraint = Constraint::New<Vector3>(Actor::SIZE,
228 ParentSource(Actor::SIZE),
229 EqualToConstraint());
230 mBackground.ApplyConstraint(constraint);
234 void ScrollBar::SetPositionNotifications( const std::vector<float>& positions )
236 if(mScrollPositionObject)
238 if(mPositionNotification)
240 mScrollPositionObject.RemovePropertyNotification(mPositionNotification);
242 mPositionNotification = mScrollPositionObject.AddPropertyNotification( Toolkit::ScrollConnector::SCROLL_POSITION, VariableStepCondition(positions) );
243 mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionNotified );
247 void ScrollBar::OnScrollPositionNotified(PropertyNotification& source)
249 // Emit the signal to notify the scroll position crossing
250 mScrollPositionNotifiedSignal.Emit(mScrollPositionObject.GetProperty<float>( Toolkit::ScrollConnector::SCROLL_POSITION ));
253 void ScrollBar::Show()
255 // Cancel any animation
262 mAnimation = Animation::New( INDICATOR_SHOW_TIME );
263 mAnimation.OpacityTo( Self(), 1.0f, AlphaFunctions::EaseIn );
267 void ScrollBar::Hide()
269 // Cancel any animation
276 mAnimation = Animation::New( INDICATOR_HIDE_TIME );
277 mAnimation.OpacityTo( Self(), 0.0f, AlphaFunctions::EaseIn );
281 bool ScrollBar::OnPanGestureProcessTick()
283 // Update the scroll position property.
284 mScrollPositionObject.SetProperty( Toolkit::ScrollConnector::SCROLL_POSITION, mCurrentScrollPosition );
286 Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
289 // Refresh ItemView immediately when the scroll position is changed.
290 GetImpl(itemView).DoRefresh(mCurrentScrollPosition, false); // No need to cache extra items.
296 void ScrollBar::OnPan( PanGesture gesture )
298 if(mScrollPositionObject)
300 switch(gesture.state)
302 case Gesture::Started:
306 // Make sure the pan gesture is only being processed once per frame.
307 mTimer = Timer::New( DEFAULT_PAN_GESTURE_PROCESS_TIME );
308 mTimer.TickSignal().Connect( this, &ScrollBar::OnPanGestureProcessTick );
313 mScrollStart = mScrollPositionObject.GetProperty<float>( Toolkit::ScrollConnector::SCROLL_POSITION );
314 mGestureDisplacement = Vector3::ZERO;
319 case Gesture::Continuing:
321 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
322 mGestureDisplacement+=delta;
324 Vector3 span = Self().GetCurrentSize() - mIndicator.GetCurrentSize();
325 const float domainSize = fabs(mScrollConnector.GetMaxLimit() - mScrollConnector.GetMinLimit());
326 mCurrentScrollPosition = mScrollStart - mGestureDisplacement.y * domainSize / span.y;
327 mCurrentScrollPosition = std::min(mScrollConnector.GetMaxLimit(), std::max(mCurrentScrollPosition, mScrollConnector.GetMinLimit()));
337 // Destroy the timer when pan gesture is finished.
339 mTimer.TickSignal().Disconnect( this, &ScrollBar::OnPanGestureProcessTick );
347 Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
350 // Disable automatic refresh in ItemView during fast scrolling
351 GetImpl(itemView).SetRefreshEnabled(!mIsPanning);
356 void ScrollBar::OnScrollDomainChanged(float minPosition, float maxPosition, float contentSize)
358 // Reapply constraints when the scroll domain is changed
362 Toolkit::ScrollBar ScrollBar::New()
364 // Create the implementation, temporarily owned by this handle on stack
365 IntrusivePtr< ScrollBar > impl = new ScrollBar();
367 // Pass ownership to CustomActor handle
368 Toolkit::ScrollBar handle( *impl );
370 // Second-phase init of the implementation
371 // This can only be done after the CustomActor connection has been made...
377 } // namespace Internal
379 } // namespace Toolkit