Implemented scroll bar control
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scroll-bar / scroll-bar-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 #include <dali-toolkit/internal/controls/scroll-bar/scroll-bar-impl.h>
18
19 using namespace Dali;
20
21 namespace
22 {
23
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);
29
30 /**
31  * Indicator size constraint
32  * Indicator size depends on both indicator's parent size and the scroll content size
33  */
34 struct IndicatorSizeConstraint
35 {
36   /**
37    * @param[in] contentSize The size of scrollable content
38    */
39   IndicatorSizeConstraint(float contentSize)
40   : mContentSize(contentSize)
41   {
42   }
43
44   /**
45    * Constraint operator
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.
49    */
50   Vector3 operator()(const Vector3& current,
51                      const PropertyInput& parentSizeProperty)
52   {
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 );
56   }
57
58   float mContentSize;  ///< The size of scrollable content
59 };
60
61 /**
62  * Indicator position constraint
63  * Positions the indicator to reflect the current scroll position within the scroll domain.
64  */
65 struct IndicatorPositionConstraint
66 {
67   /**
68    * @param[in] minPosition The minimum limit of scroll position
69    * @param[in] maxPosition the maximum limit of scroll position
70    */
71   IndicatorPositionConstraint(float minPosition, float maxPosition)
72   : mMinPosition(minPosition),
73     mMaxPosition(maxPosition)
74   {
75   }
76
77   /**
78    * Constraint operator
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.
84    */
85   Vector3 operator()(const Vector3& current,
86                      const PropertyInput& indicatorSizeProperty,
87                      const PropertyInput& parentSizeProperty,
88                      const PropertyInput& scrollPositionProperty)
89   {
90     Vector3 indicatorSize = indicatorSizeProperty.GetVector3();
91     Vector3 parentSize = parentSizeProperty.GetVector3();
92     float scrollPosition = scrollPositionProperty.GetFloat();
93
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);
97   }
98
99   float mMinPosition;  ///< The minimum scroll position
100   float mMaxPosition;  ///< The maximum scroll position
101 };
102
103 } // unnamed namespace
104
105 namespace Dali
106 {
107
108 namespace Toolkit
109 {
110
111 namespace Internal
112 {
113
114 namespace
115 {
116
117 using namespace Dali;
118
119 BaseHandle Create()
120 {
121   return BaseHandle();
122 }
123
124 TypeRegistration mType( typeid(Toolkit::ScrollBar), typeid(Toolkit::Control), Create );
125
126 }
127
128 ScrollBar::ScrollBar()
129 : ControlImpl(true/*requires touch*/),
130   mScrollStart(0.0f)
131 {
132 }
133
134 ScrollBar::~ScrollBar()
135 {
136 }
137
138 void ScrollBar::OnInitialize()
139 {
140   Actor self = Self();
141
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);
149
150   self.SetDrawMode(DrawMode::OVERLAY);
151
152   // Enable the pan gesture which is attached to the control
153   EnableGestureDetection(Gesture::Type(Gesture::Pan));
154 }
155
156 void ScrollBar::SetScrollConnector( Toolkit::ScrollConnector connector )
157 {
158   if(mScrollConnector)
159   {
160     mScrollConnector.DomainChangedSignal().Disconnect(this, &ScrollBar::OnScrollDomainChanged);
161   }
162
163   mScrollConnector = connector;
164   mScrollPositionObject = mScrollConnector.GetScrollPositionObject();
165
166   ApplyConstraints();
167   mScrollConnector.DomainChangedSignal().Connect(this, &ScrollBar::OnScrollDomainChanged);
168 }
169
170 void ScrollBar::SetBackgroundImage( Image image, const Vector4& border )
171 {
172   if (!mBackground )
173   {
174     mBackground = ImageActor::New( image );
175     mBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
176     mBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
177     Self().Add(mBackground);
178   }
179   else
180   {
181     mBackground.SetImage(image);
182   }
183
184   mBackground.SetNinePatchBorder( border );
185   mBackground.SetStyle( ImageActor::STYLE_NINE_PATCH );
186 }
187
188 void ScrollBar::SetIndicatorImage( Image image, const Vector4& border )
189 {
190   mIndicator.SetImage(image);
191   mIndicator.SetNinePatchBorder( border );
192   mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
193 }
194
195 Actor ScrollBar::GetScrollIndicator()
196 {
197   return mIndicator;
198 }
199
200 void ScrollBar::ApplyConstraints()
201 {
202   mIndicator.RemoveConstraints();
203
204   Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
205                                                     ParentSource( Actor::SIZE ),
206                                                     IndicatorSizeConstraint( mScrollConnector.GetContentLength() ) );
207   mIndicator.ApplyConstraint( constraint );
208
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 );
215
216   if( mBackground )
217   {
218     mBackground.RemoveConstraints();
219
220     constraint = Constraint::New<Vector3>(Actor::SIZE,
221                                           ParentSource(Actor::SIZE),
222                                           EqualToConstraint());
223     mBackground.ApplyConstraint(constraint);
224   }
225 }
226
227 void ScrollBar::SetPositionNotifications( const std::vector<float>& positions )
228 {
229   if(mScrollPositionObject)
230   {
231     if(mPositionNotification)
232     {
233       mScrollPositionObject.RemovePropertyNotification(mPositionNotification);
234     }
235     mPositionNotification = mScrollPositionObject.AddPropertyNotification( Toolkit::ScrollConnector::SCROLL_POSITION, VariableStepCondition(positions) );
236     mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionNotified );
237   }
238 }
239
240 void ScrollBar::OnScrollPositionNotified(PropertyNotification& source)
241 {
242   // Emit the signal to notify the scroll position crossing
243   mScrollPositionNotifiedSignal.Emit(mScrollPositionObject.GetProperty<float>( Toolkit::ScrollConnector::SCROLL_POSITION ));
244 }
245
246 void ScrollBar::Show()
247 {
248   // Cancel any animation
249   if(mAnimation)
250   {
251     mAnimation.Clear();
252     mAnimation.Reset();
253   }
254
255   mAnimation = Animation::New( INDICATOR_SHOW_TIME );
256   mAnimation.OpacityTo( Self(), 1.0f, AlphaFunctions::EaseIn );
257   mAnimation.Play();
258 }
259
260 void ScrollBar::Hide()
261 {
262   // Cancel any animation
263   if(mAnimation)
264   {
265     mAnimation.Clear();
266     mAnimation.Reset();
267   }
268
269   mAnimation = Animation::New( INDICATOR_HIDE_TIME );
270   mAnimation.OpacityTo( Self(), 0.0f, AlphaFunctions::EaseIn );
271   mAnimation.Play();
272 }
273
274 void ScrollBar::OnPan( PanGesture gesture )
275 {
276   if(mScrollPositionObject)
277   {
278     switch(gesture.state)
279     {
280       case Gesture::Started:
281       {
282         Show();
283         mScrollStart = mScrollPositionObject.GetProperty<float>( Toolkit::ScrollConnector::SCROLL_POSITION );
284         mGestureDisplacement = Vector3::ZERO;
285         break;
286       }
287       case Gesture::Continuing:
288       {
289         Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
290         mGestureDisplacement+=delta;
291
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 );
297         break;
298       }
299       default:
300       {
301         break;
302       }
303     }
304   }
305 }
306
307 void ScrollBar::OnScrollDomainChanged(float minPosition, float maxPosition, float contentSize)
308 {
309   // Reapply constraints when the scroll domain is changed
310   ApplyConstraints();
311 }
312
313 Toolkit::ScrollBar ScrollBar::New()
314 {
315   // Create the implementation, temporarily owned by this handle on stack
316   IntrusivePtr< ScrollBar > impl = new ScrollBar();
317
318   // Pass ownership to CustomActor handle
319   Toolkit::ScrollBar handle( *impl );
320
321   // Second-phase init of the implementation
322   // This can only be done after the CustomActor connection has been made...
323   impl->Initialize();
324
325   return handle;
326 }
327
328 } // namespace Internal
329
330 } // namespace Toolkit
331
332 } // namespace Dali