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.
19 #include <dali-toolkit/internal/controls/scroll-bar/scroll-bar-impl.h>
22 #include <dali/public-api/animation/constraint.h>
23 #include <dali/public-api/animation/constraints.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/object/type-registry-helper.h>
26 #include <dali/public-api/images/resource-image.h>
29 #include <dali-toolkit/internal/controls/scrollable/item-view/item-view-impl.h>
36 const char* DEFAULT_INDICATOR_IMAGE_PATH = DALI_IMAGE_DIR "popup_scroll.png";
37 const Vector4 DEFAULT_INDICATOR_NINE_PATCH_BORDER(4.0f, 9.0f, 7.0f, 11.0f);
38 const float MINIMUM_INDICATOR_HEIGHT(20.0f); // The minimum indicator height for the nine patch border
39 const float DEFAULT_SLIDER_DEPTH(1.0f);
40 const float DEFAULT_INDICATOR_SHOW_DURATION(0.5f);
41 const float DEFAULT_INDICATOR_HIDE_DURATION(0.5f);
42 const float DEFAULT_PAN_GESTURE_PROCESS_TIME(16.7f); // 16.7 milliseconds, i.e. one frame
43 const float DEFAULT_INDICATOR_FIXED_HEIGHT(80.0f);
46 * Indicator size depends on both indicator's parent size and the scroll content size
48 Vector3 IndicatorSize( const Vector3& parentSize, float contentSize)
50 float height = contentSize > parentSize.height ?
51 parentSize.height * ( parentSize.height / contentSize ) :
52 parentSize.height * ( (parentSize.height - contentSize * 0.5f) / parentSize.height);
53 return Vector3( parentSize.width, std::max(MINIMUM_INDICATOR_HEIGHT, height), parentSize.depth );
57 * Indicator position constraint
58 * Positions the indicator to reflect the current scroll position within the scroll domain.
60 struct IndicatorPositionConstraint
63 * @param[in] minPosition The minimum limit of scroll position
64 * @param[in] maxPosition the maximum limit of scroll position
66 IndicatorPositionConstraint(float minPosition, float maxPosition)
67 : mMinPosition(minPosition),
68 mMaxPosition(maxPosition)
74 * @param[in,out] current The current indicator position
75 * @param[in] inputs Contains the size of indicator, the size of indicator's parent, and the scroll position of the scrollable container (from 0.0 -> 1.0 in each axis)
76 * @return The new indicator position is returned.
78 void operator()( Vector3& current, const PropertyInputContainer& inputs )
80 const Vector3& indicatorSize = inputs[0]->GetVector3();
81 const Vector3& parentSize = inputs[1]->GetVector3();
82 float scrollPosition = inputs[2]->GetFloat();
84 const float domainSize = fabs(mMaxPosition - mMinPosition);
85 float relativePosition = (mMaxPosition - scrollPosition) / domainSize;
87 current.y = relativePosition * ( parentSize.height - indicatorSize.height );
88 current.z = DEFAULT_SLIDER_DEPTH;
91 float mMinPosition; ///< The minimum scroll position
92 float mMaxPosition; ///< The maximum scroll position
95 } // unnamed namespace
109 using namespace Dali;
113 return Toolkit::ScrollBar::New();
116 // Setup properties, signals and actions using the type-registry.
117 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ScrollBar, Toolkit::ScrollComponent, Create );
119 DALI_PROPERTY_REGISTRATION( ScrollBar, "indicator-height-policy", STRING, INDICATOR_HEIGHT_POLICY )
120 DALI_PROPERTY_REGISTRATION( ScrollBar, "indicator-fixed-height", FLOAT, INDICATOR_FIXED_HEIGHT )
121 DALI_PROPERTY_REGISTRATION( ScrollBar, "indicator-show-duration", FLOAT, INDICATOR_SHOW_DURATION )
122 DALI_PROPERTY_REGISTRATION( ScrollBar, "indicator-hide-duration", FLOAT, INDICATOR_HIDE_DURATION )
124 DALI_SIGNAL_REGISTRATION( ScrollBar, "scroll-position-notified", SCROLL_POSITION_NOTIFIED_SIGNAL )
126 DALI_TYPE_REGISTRATION_END()
128 const char* INDICATOR_HEIGHT_POLICY_NAME[] = {"Variable", "Fixed"};
132 ScrollBar::ScrollBar()
133 : mIndicatorShowDuration(DEFAULT_INDICATOR_SHOW_DURATION),
134 mIndicatorHideDuration(DEFAULT_INDICATOR_HIDE_DURATION),
137 mCurrentScrollPosition(0.0f),
138 mIndicatorHeightPolicy(Toolkit::ScrollBar::Variable),
139 mIndicatorFixedHeight(DEFAULT_INDICATOR_FIXED_HEIGHT),
140 mPropertyIndicatorPosition(Property::INVALID_INDEX)
144 ScrollBar::~ScrollBar()
148 void ScrollBar::OnInitialize()
152 Image indicatorImage = ResourceImage::New( DEFAULT_INDICATOR_IMAGE_PATH );
153 mIndicator = ImageActor::New( indicatorImage );
154 mIndicator.SetNinePatchBorder( DEFAULT_INDICATOR_NINE_PATCH_BORDER );
155 mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
156 mIndicator.SetParentOrigin( ParentOrigin::TOP_LEFT );
157 mIndicator.SetAnchorPoint( AnchorPoint::TOP_LEFT );
158 self.Add(mIndicator);
160 self.SetDrawMode(DrawMode::OVERLAY);
162 // Enable the pan gesture which is attached to the control
163 EnableGestureDetection(Gesture::Type(Gesture::Pan));
166 void ScrollBar::OnScrollConnectorSet( Toolkit::ScrollConnector oldConnector )
170 oldConnector.DomainChangedSignal().Disconnect(this, &ScrollBar::OnScrollDomainChanged);
172 mScrollPositionObject.Reset();
175 if( mScrollConnector )
177 mScrollPositionObject = mScrollConnector.GetScrollPositionObject();
180 mScrollConnector.DomainChangedSignal().Connect(this, &ScrollBar::OnScrollDomainChanged);
184 void ScrollBar::SetIndicatorImage( Image image )
186 mIndicator.SetImage(image);
189 Actor ScrollBar::GetScrollIndicator()
194 void ScrollBar::ApplyConstraints()
196 if( mScrollConnector )
198 // Set indicator height according to the indicator's height policy
199 if(mIndicatorHeightPolicy == Toolkit::ScrollBar::Fixed)
201 mIndicator.SetSize(Self().GetCurrentSize().width, mIndicatorFixedHeight);
205 mIndicator.SetSize( IndicatorSize( Self().GetCurrentSize(), mScrollConnector.GetContentLength() ) );
208 if(mIndicatorPositionConstraint)
210 mIndicatorPositionConstraint.Remove();
213 mIndicatorPositionConstraint = Constraint::New<Vector3>( mIndicator, Actor::Property::POSITION, IndicatorPositionConstraint( mScrollConnector.GetMinLimit(), mScrollConnector.GetMaxLimit() ) );
214 mIndicatorPositionConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
215 mIndicatorPositionConstraint.AddSource( ParentSource( Actor::Property::SIZE ) );
216 mIndicatorPositionConstraint.AddSource( Source( mScrollPositionObject, Toolkit::ScrollConnector::SCROLL_POSITION ) );
217 mIndicatorPositionConstraint.Apply();
221 void ScrollBar::SetPositionNotifications( const std::vector<float>& positions )
223 if(mScrollPositionObject)
225 if(mPositionNotification)
227 mScrollPositionObject.RemovePropertyNotification(mPositionNotification);
229 mPositionNotification = mScrollPositionObject.AddPropertyNotification( Toolkit::ScrollConnector::SCROLL_POSITION, VariableStepCondition(positions) );
230 mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionNotified );
234 void ScrollBar::OnScrollPositionNotified(PropertyNotification& source)
236 // Emit the signal to notify the scroll position crossing
237 mScrollPositionNotifiedSignal.Emit(mScrollConnector.GetScrollPosition());
240 void ScrollBar::Show()
244 // Cancel any animation
251 if(mIndicatorShowDuration > 0.0f)
253 mAnimation = Animation::New( mIndicatorShowDuration );
254 mAnimation.AnimateTo( Property( self, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunctions::EaseIn );
259 self.SetOpacity(1.0f);
263 void ScrollBar::Hide()
267 // Cancel any animation
274 if(mIndicatorHideDuration > 0.0f)
276 mAnimation = Animation::New( mIndicatorHideDuration );
277 mAnimation.AnimateTo( Property( self, Actor::Property::COLOR_ALPHA ), 0.0f, AlphaFunctions::EaseIn );
282 self.SetOpacity(0.0f);
286 bool ScrollBar::OnPanGestureProcessTick()
288 // Update the scroll position property.
289 if( mScrollConnector )
291 mScrollConnector.SetScrollPosition(mCurrentScrollPosition);
297 void ScrollBar::OnPan( PanGesture gesture )
301 Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
303 switch(gesture.state)
305 case Gesture::Started:
309 // Make sure the pan gesture is only being processed once per frame.
310 mTimer = Timer::New( DEFAULT_PAN_GESTURE_PROCESS_TIME );
311 mTimer.TickSignal().Connect( this, &ScrollBar::OnPanGestureProcessTick );
316 mScrollStart = mScrollConnector.GetScrollPosition();
317 mGestureDisplacement = Vector3::ZERO;
322 case Gesture::Continuing:
324 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
325 mGestureDisplacement+=delta;
327 Vector3 span = Self().GetCurrentSize() - mIndicator.GetCurrentSize();
328 const float domainSize = fabs(mScrollConnector.GetMaxLimit() - mScrollConnector.GetMinLimit());
329 mCurrentScrollPosition = mScrollStart - mGestureDisplacement.y * domainSize / span.y;
330 mCurrentScrollPosition = std::min(mScrollConnector.GetMaxLimit(), std::max(mCurrentScrollPosition, mScrollConnector.GetMinLimit()));
340 // Destroy the timer when pan gesture is finished.
342 mTimer.TickSignal().Disconnect( this, &ScrollBar::OnPanGestureProcessTick );
348 // Refresh the ItemView cache with extra items
349 GetImpl(itemView).DoRefresh(mCurrentScrollPosition, true);
358 // Disable automatic refresh in ItemView during fast scrolling
359 GetImpl(itemView).SetRefreshEnabled(!mIsPanning);
364 void ScrollBar::OnControlSizeSet( const Vector3& size )
366 if(mIndicatorHeightPolicy != Toolkit::ScrollBar::Fixed && mScrollConnector)
368 mIndicator.SetSize( IndicatorSize( size, mScrollConnector.GetContentLength() ) );
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::SetIndicatorShowDuration( float durationSeconds )
402 mIndicatorShowDuration = durationSeconds;
405 float ScrollBar::GetIndicatorShowDuration()
407 return mIndicatorShowDuration;
410 void ScrollBar::SetIndicatorHideDuration( float durationSeconds )
412 mIndicatorHideDuration = durationSeconds;
415 float ScrollBar::GetIndicatorHideDuration()
417 return mIndicatorHideDuration;
420 void ScrollBar::OnIndicatorHeightPolicyPropertySet( Property::Value propertyValue )
422 std::string policyName( propertyValue.Get<std::string>() );
423 if(policyName == "Variable")
425 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Variable);
427 else if(policyName == "Fixed")
429 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Fixed);
433 DALI_ASSERT_ALWAYS( !"ScrollBar::OnIndicatorHeightPolicyPropertySet(). Invalid Property value." );
437 bool ScrollBar::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
439 Dali::BaseHandle handle( object );
441 bool connected( true );
442 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( handle );
444 if( 0 == strcmp( signalName.c_str(), SCROLL_POSITION_NOTIFIED_SIGNAL ) )
446 scrollBar.ScrollPositionNotifiedSignal().Connect( tracker, functor );
450 // signalName does not match any signal
457 void ScrollBar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
459 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
463 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
466 case Toolkit::ScrollBar::Property::INDICATOR_HEIGHT_POLICY:
468 scrollBarImpl.OnIndicatorHeightPolicyPropertySet( value );
471 case Toolkit::ScrollBar::Property::INDICATOR_FIXED_HEIGHT:
473 scrollBarImpl.SetIndicatorFixedHeight(value.Get<float>());
476 case Toolkit::ScrollBar::Property::INDICATOR_SHOW_DURATION:
478 scrollBarImpl.SetIndicatorShowDuration(value.Get<float>());
481 case Toolkit::ScrollBar::Property::INDICATOR_HIDE_DURATION:
483 scrollBarImpl.SetIndicatorHideDuration(value.Get<float>());
490 Property::Value ScrollBar::GetProperty( BaseObject* object, Property::Index index )
492 Property::Value value;
494 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
498 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
501 case Toolkit::ScrollBar::Property::INDICATOR_HEIGHT_POLICY:
503 value = INDICATOR_HEIGHT_POLICY_NAME[ scrollBarImpl.GetIndicatorHeightPolicy() ];
506 case Toolkit::ScrollBar::Property::INDICATOR_FIXED_HEIGHT:
508 value = scrollBarImpl.GetIndicatorFixedHeight();
511 case Toolkit::ScrollBar::Property::INDICATOR_SHOW_DURATION:
513 value = scrollBarImpl.GetIndicatorShowDuration();
516 case Toolkit::ScrollBar::Property::INDICATOR_HIDE_DURATION:
518 value = scrollBarImpl.GetIndicatorHideDuration();
526 Toolkit::ScrollBar ScrollBar::New()
528 // Create the implementation, temporarily owned by this handle on stack
529 IntrusivePtr< ScrollBar > impl = new ScrollBar();
531 // Pass ownership to CustomActor handle
532 Toolkit::ScrollBar handle( *impl );
534 // Second-phase init of the implementation
535 // This can only be done after the CustomActor connection has been made...
541 } // namespace Internal
543 } // namespace Toolkit