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>
24 const char* DEFAULT_INDICATOR_IMAGE_PATH = DALI_IMAGE_DIR "popup_scroll.png";
25 const Vector4 DEFAULT_INDICATOR_NINE_PATCH_BORDER(0.0f, 12.0f, 14.0f, 14.0f);
26 const float DEFAULT_SLIDER_DEPTH(1.0f);
27 const float INDICATOR_SHOW_TIME(0.5f);
28 const float INDICATOR_HIDE_TIME(0.5f);
31 * Indicator size constraint
32 * Indicator size depends on both indicator's parent size and the scroll content size
34 struct IndicatorSizeConstraint
37 * @param[in] contentSize The size of scrollable content
39 IndicatorSizeConstraint(float contentSize)
40 : mContentSize(contentSize)
46 * @param[in] current The current indicator size
47 * @param[in] parentSizeProperty The parent size of scroll indicator.
48 * @return The new scroll indicator size.
50 Vector3 operator()(const Vector3& current,
51 const PropertyInput& parentSizeProperty)
53 const Vector3& parentSize = parentSizeProperty.GetVector3();
54 float height = mContentSize > parentSize.height ? parentSize.height * ( parentSize.height / mContentSize ) : parentSize.height * ( (parentSize.height - mContentSize * 0.5f) / parentSize.height);
55 return Vector3( parentSize.width, height, parentSize.depth );
58 float mContentSize; ///< The size of scrollable content
62 * Indicator position constraint
63 * Positions the indicator to reflect the current scroll position within the scroll domain.
65 struct IndicatorPositionConstraint
68 * @param[in] minPosition The minimum limit of scroll position
69 * @param[in] maxPosition the maximum limit of scroll position
71 IndicatorPositionConstraint(float minPosition, float maxPosition)
72 : mMinPosition(minPosition),
73 mMaxPosition(maxPosition)
79 * @param[in] current The current indicator position
80 * @param[in] indicatorSizeProperty The size of indicator.
81 * @param[in] parentSizeProperty The parent size of indicator.
82 * @param[in] scrollPositionProperty The scroll position of the scrollable container // (from 0.0 -> 1.0 in each axis)
83 * @return The new indicator position is returned.
85 Vector3 operator()(const Vector3& current,
86 const PropertyInput& indicatorSizeProperty,
87 const PropertyInput& parentSizeProperty,
88 const PropertyInput& scrollPositionProperty)
90 Vector3 indicatorSize = indicatorSizeProperty.GetVector3();
91 Vector3 parentSize = parentSizeProperty.GetVector3();
92 float scrollPosition = scrollPositionProperty.GetFloat();
94 const float domainSize = fabs(mMaxPosition - mMinPosition);
95 float relativePosition = (mMaxPosition - scrollPosition) / domainSize;
96 return Vector3(current.x, relativePosition * (parentSize.height - indicatorSize.height), DEFAULT_SLIDER_DEPTH);
99 float mMinPosition; ///< The minimum scroll position
100 float mMaxPosition; ///< The maximum scroll position
103 } // unnamed namespace
117 using namespace Dali;
124 TypeRegistration mType( typeid(Toolkit::ScrollBar), typeid(Toolkit::Control), Create );
128 ScrollBar::ScrollBar()
129 : ControlImpl(true/*requires touch*/),
134 ScrollBar::~ScrollBar()
138 void ScrollBar::OnInitialize()
142 Image indicatorImage = Image::New( DEFAULT_INDICATOR_IMAGE_PATH );
143 mIndicator = ImageActor::New( indicatorImage );
144 mIndicator.SetNinePatchBorder( DEFAULT_INDICATOR_NINE_PATCH_BORDER );
145 mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
146 mIndicator.SetParentOrigin( ParentOrigin::TOP_LEFT );
147 mIndicator.SetAnchorPoint( AnchorPoint::TOP_LEFT );
148 self.Add(mIndicator);
150 self.SetDrawMode(DrawMode::OVERLAY);
152 // Enable the pan gesture which is attached to the control
153 EnableGestureDetection(Gesture::Type(Gesture::Pan));
156 void ScrollBar::SetScrollConnector( Toolkit::ScrollConnector connector )
160 mScrollConnector.DomainChangedSignal().Disconnect(this, &ScrollBar::OnScrollDomainChanged);
163 mScrollConnector = connector;
164 mScrollPositionObject = mScrollConnector.GetScrollPositionObject();
167 mScrollConnector.DomainChangedSignal().Connect(this, &ScrollBar::OnScrollDomainChanged);
170 void ScrollBar::SetBackgroundImage( Image image, const Vector4& border )
174 mBackground = ImageActor::New( image );
175 mBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
176 mBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
177 Self().Add(mBackground);
181 mBackground.SetImage(image);
184 mBackground.SetNinePatchBorder( border );
185 mBackground.SetStyle( ImageActor::STYLE_NINE_PATCH );
188 void ScrollBar::SetIndicatorImage( Image image, const Vector4& border )
190 mIndicator.SetImage(image);
191 mIndicator.SetNinePatchBorder( border );
192 mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
195 Actor ScrollBar::GetScrollIndicator()
200 void ScrollBar::ApplyConstraints()
202 mIndicator.RemoveConstraints();
204 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
205 ParentSource( Actor::SIZE ),
206 IndicatorSizeConstraint( mScrollConnector.GetContentLength() ) );
207 mIndicator.ApplyConstraint( constraint );
209 constraint = Constraint::New<Vector3>( Actor::POSITION,
210 LocalSource( Actor::SIZE ),
211 ParentSource( Actor::SIZE ),
212 Source( mScrollPositionObject, Toolkit::ScrollConnector::SCROLL_POSITION ),
213 IndicatorPositionConstraint( mScrollConnector.GetMinLimit(), mScrollConnector.GetMaxLimit() ) );
214 mIndicator.ApplyConstraint( constraint );
218 mBackground.RemoveConstraints();
220 constraint = Constraint::New<Vector3>(Actor::SIZE,
221 ParentSource(Actor::SIZE),
222 EqualToConstraint());
223 mBackground.ApplyConstraint(constraint);
227 void ScrollBar::SetPositionNotifications( const std::vector<float>& positions )
229 if(mScrollPositionObject)
231 if(mPositionNotification)
233 mScrollPositionObject.RemovePropertyNotification(mPositionNotification);
235 mPositionNotification = mScrollPositionObject.AddPropertyNotification( Toolkit::ScrollConnector::SCROLL_POSITION, VariableStepCondition(positions) );
236 mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionNotified );
240 void ScrollBar::OnScrollPositionNotified(PropertyNotification& source)
242 // Emit the signal to notify the scroll position crossing
243 mScrollPositionNotifiedSignal.Emit(mScrollPositionObject.GetProperty<float>( Toolkit::ScrollConnector::SCROLL_POSITION ));
246 void ScrollBar::Show()
248 // Cancel any animation
255 mAnimation = Animation::New( INDICATOR_SHOW_TIME );
256 mAnimation.OpacityTo( Self(), 1.0f, AlphaFunctions::EaseIn );
260 void ScrollBar::Hide()
262 // Cancel any animation
269 mAnimation = Animation::New( INDICATOR_HIDE_TIME );
270 mAnimation.OpacityTo( Self(), 0.0f, AlphaFunctions::EaseIn );
274 void ScrollBar::OnPan( PanGesture gesture )
276 if(mScrollPositionObject)
278 switch(gesture.state)
280 case Gesture::Started:
283 mScrollStart = mScrollPositionObject.GetProperty<float>( Toolkit::ScrollConnector::SCROLL_POSITION );
284 mGestureDisplacement = Vector3::ZERO;
287 case Gesture::Continuing:
289 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
290 mGestureDisplacement+=delta;
292 Vector3 span = Self().GetCurrentSize() - mIndicator.GetCurrentSize();
293 const float domainSize = fabs(mScrollConnector.GetMaxLimit() - mScrollConnector.GetMinLimit());
294 float position = mScrollStart - mGestureDisplacement.y * domainSize / span.y;
295 position = std::min(mScrollConnector.GetMaxLimit(), std::max(position, mScrollConnector.GetMinLimit()));
296 mScrollPositionObject.SetProperty( Toolkit::ScrollConnector::SCROLL_POSITION, position );
307 void ScrollBar::OnScrollDomainChanged(float minPosition, float maxPosition, float contentSize)
309 // Reapply constraints when the scroll domain is changed
313 Toolkit::ScrollBar ScrollBar::New()
315 // Create the implementation, temporarily owned by this handle on stack
316 IntrusivePtr< ScrollBar > impl = new ScrollBar();
318 // Pass ownership to CustomActor handle
319 Toolkit::ScrollBar handle( *impl );
321 // Second-phase init of the implementation
322 // This can only be done after the CustomActor connection has been made...
328 } // namespace Internal
330 } // namespace Toolkit