Remove Deprecated APIs
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / scroll-bar / scroll-bar-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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
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>
20
21 using namespace Dali;
22
23 namespace
24 {
25
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);
34
35 /**
36  * Indicator size constraint
37  * Indicator size depends on both indicator's parent size and the scroll content size
38  */
39 struct IndicatorSizeConstraint
40 {
41   /**
42    * @param[in] contentSize The size of scrollable content
43    */
44   IndicatorSizeConstraint(float contentSize)
45   : mContentSize(contentSize)
46   {
47   }
48
49   /**
50    * Constraint operator
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.
54    */
55   Vector3 operator()(const Vector3& current,
56                      const PropertyInput& parentSizeProperty)
57   {
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 );
61   }
62
63   float mContentSize;  ///< The size of scrollable content
64 };
65
66 /**
67  * Indicator position constraint
68  * Positions the indicator to reflect the current scroll position within the scroll domain.
69  */
70 struct IndicatorPositionConstraint
71 {
72   /**
73    * @param[in] minPosition The minimum limit of scroll position
74    * @param[in] maxPosition the maximum limit of scroll position
75    */
76   IndicatorPositionConstraint(float minPosition, float maxPosition)
77   : mMinPosition(minPosition),
78     mMaxPosition(maxPosition)
79   {
80   }
81
82   /**
83    * Constraint operator
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.
89    */
90   Vector3 operator()(const Vector3& current,
91                      const PropertyInput& indicatorSizeProperty,
92                      const PropertyInput& parentSizeProperty,
93                      const PropertyInput& scrollPositionProperty)
94   {
95     Vector3 indicatorSize = indicatorSizeProperty.GetVector3();
96     Vector3 parentSize = parentSizeProperty.GetVector3();
97     float scrollPosition = scrollPositionProperty.GetFloat();
98
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);
102   }
103
104   float mMinPosition;  ///< The minimum scroll position
105   float mMaxPosition;  ///< The maximum scroll position
106 };
107
108 } // unnamed namespace
109
110 namespace Dali
111 {
112
113 namespace Toolkit
114 {
115
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 );
118
119 namespace Internal
120 {
121
122 namespace
123 {
124
125 using namespace Dali;
126
127 const char* INDICATOR_HEIGHT_POLICY_NAME[] = {"Variable", "Fixed"};
128
129 BaseHandle Create()
130 {
131   return Toolkit::ScrollBar::New();
132 }
133
134 TypeRegistration typeRegistration( typeid(Toolkit::ScrollBar), typeid(Toolkit::ScrollComponent), Create );
135
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 );
138 }
139
140 ScrollBar::ScrollBar()
141 : mScrollStart(0.0f),
142   mIsPanning(false),
143   mCurrentScrollPosition(0.0f),
144   mIndicatorHeightPolicy(Toolkit::ScrollBar::Variable),
145   mIndicatorFixedHeight(DEFAULT_INDICATOR_FIXED_HEIGHT)
146 {
147 }
148
149 ScrollBar::~ScrollBar()
150 {
151 }
152
153 void ScrollBar::OnInitialize()
154 {
155   Actor self = Self();
156
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);
164
165   self.SetDrawMode(DrawMode::OVERLAY);
166
167   // Enable the pan gesture which is attached to the control
168   EnableGestureDetection(Gesture::Type(Gesture::Pan));
169 }
170
171 void ScrollBar::OnScrollConnectorSet( Toolkit::ScrollConnector oldConnector )
172 {
173   if( oldConnector )
174   {
175     oldConnector.DomainChangedSignal().Disconnect(this, &ScrollBar::OnScrollDomainChanged);
176
177     mScrollPositionObject.Reset();
178   }
179
180   if( mScrollConnector )
181   {
182     mScrollPositionObject = mScrollConnector.GetScrollPositionObject();
183
184     ApplyConstraints();
185     mScrollConnector.DomainChangedSignal().Connect(this, &ScrollBar::OnScrollDomainChanged);
186   }
187 }
188
189 void ScrollBar::SetIndicatorImage( Image image )
190 {
191   mIndicator.SetImage(image);
192 }
193
194 Actor ScrollBar::GetScrollIndicator()
195 {
196   return mIndicator;
197 }
198
199 void ScrollBar::ApplyConstraints()
200 {
201   if( mScrollConnector )
202   {
203     Constraint constraint;
204
205     if(mIndicatorSizeConstraint)
206     {
207       mIndicator.RemoveConstraint(mIndicatorSizeConstraint);
208     }
209
210     // Set indicator height according to the indicator's height policy
211     if(mIndicatorHeightPolicy == Toolkit::ScrollBar::Fixed)
212     {
213       mIndicator.SetSize(Self().GetCurrentSize().width, mIndicatorFixedHeight);
214     }
215     else
216     {
217       constraint = Constraint::New<Vector3>( Actor::SIZE,
218                                              ParentSource( Actor::SIZE ),
219                                              IndicatorSizeConstraint( mScrollConnector.GetContentLength() ) );
220       mIndicatorSizeConstraint = mIndicator.ApplyConstraint( constraint );
221     }
222
223     if(mIndicatorPositionConstraint)
224     {
225       mIndicator.RemoveConstraint(mIndicatorPositionConstraint);
226     }
227
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 );
234
235     if( mBackground )
236     {
237       mBackground.RemoveConstraints();
238
239       constraint = Constraint::New<Vector3>(Actor::SIZE,
240                                             ParentSource(Actor::SIZE),
241                                             EqualToConstraint());
242       mBackground.ApplyConstraint(constraint);
243     }
244   }
245 }
246
247 void ScrollBar::SetPositionNotifications( const std::vector<float>& positions )
248 {
249   if(mScrollPositionObject)
250   {
251     if(mPositionNotification)
252     {
253       mScrollPositionObject.RemovePropertyNotification(mPositionNotification);
254     }
255     mPositionNotification = mScrollPositionObject.AddPropertyNotification( Toolkit::ScrollConnector::SCROLL_POSITION, VariableStepCondition(positions) );
256     mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionNotified );
257   }
258 }
259
260 void ScrollBar::OnScrollPositionNotified(PropertyNotification& source)
261 {
262   // Emit the signal to notify the scroll position crossing
263   mScrollPositionNotifiedSignal.Emit(mScrollConnector.GetScrollPosition());
264 }
265
266 void ScrollBar::Show()
267 {
268   // Cancel any animation
269   if(mAnimation)
270   {
271     mAnimation.Clear();
272     mAnimation.Reset();
273   }
274
275   mAnimation = Animation::New( INDICATOR_SHOW_TIME );
276   mAnimation.OpacityTo( Self(), 1.0f, AlphaFunctions::EaseIn );
277   mAnimation.Play();
278 }
279
280 void ScrollBar::Hide()
281 {
282   // Cancel any animation
283   if(mAnimation)
284   {
285     mAnimation.Clear();
286     mAnimation.Reset();
287   }
288
289   mAnimation = Animation::New( INDICATOR_HIDE_TIME );
290   mAnimation.OpacityTo( Self(), 0.0f, AlphaFunctions::EaseIn );
291   mAnimation.Play();
292 }
293
294 bool ScrollBar::OnPanGestureProcessTick()
295 {
296   // Update the scroll position property.
297   if( mScrollConnector )
298   {
299     mScrollConnector.SetScrollPosition(mCurrentScrollPosition);
300   }
301
302   return true;
303 }
304
305 void ScrollBar::OnPan( PanGesture gesture )
306 {
307   if(mScrollConnector)
308   {
309     Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
310
311     switch(gesture.state)
312     {
313       case Gesture::Started:
314       {
315         if( !mTimer )
316         {
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 );
320           mTimer.Start();
321         }
322
323         Show();
324         mScrollStart = mScrollConnector.GetScrollPosition();
325         mGestureDisplacement = Vector3::ZERO;
326         mIsPanning = true;
327
328         break;
329       }
330       case Gesture::Continuing:
331       {
332         Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
333         mGestureDisplacement+=delta;
334
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()));
339
340         break;
341       }
342       default:
343       {
344         mIsPanning = false;
345
346         if( mTimer )
347         {
348           // Destroy the timer when pan gesture is finished.
349           mTimer.Stop();
350           mTimer.TickSignal().Disconnect( this, &ScrollBar::OnPanGestureProcessTick );
351           mTimer.Reset();
352         }
353
354         if(itemView)
355         {
356           // Refresh the ItemView cache with extra items
357           GetImpl(itemView).DoRefresh(mCurrentScrollPosition, true);
358         }
359
360         break;
361       }
362     }
363
364     if(itemView)
365     {
366       // Disable automatic refresh in ItemView during fast scrolling
367       GetImpl(itemView).SetRefreshEnabled(!mIsPanning);
368     }
369   }
370 }
371
372 void ScrollBar::OnScrollDomainChanged(float minPosition, float maxPosition, float contentSize)
373 {
374   // Reapply constraints when the scroll domain is changed
375   ApplyConstraints();
376 }
377
378 void ScrollBar::SetIndicatorHeightPolicy( Toolkit::ScrollBar::IndicatorHeightPolicy policy )
379 {
380   mIndicatorHeightPolicy = policy;
381   ApplyConstraints();
382 }
383
384 Toolkit::ScrollBar::IndicatorHeightPolicy ScrollBar::GetIndicatorHeightPolicy()
385 {
386   return mIndicatorHeightPolicy;
387 }
388
389 void ScrollBar::SetIndicatorFixedHeight( float height )
390 {
391   mIndicatorFixedHeight = height;
392   ApplyConstraints();
393 }
394
395 float ScrollBar::GetIndicatorFixedHeight()
396 {
397   return mIndicatorFixedHeight;
398 }
399
400 void ScrollBar::OnIndicatorHeightPolicyPropertySet( Property::Value propertyValue )
401 {
402   std::string policyName( propertyValue.Get<std::string>() );
403   if(policyName == "Variable")
404   {
405     SetIndicatorHeightPolicy(Toolkit::ScrollBar::Variable);
406   }
407   else if(policyName == "Fixed")
408   {
409     SetIndicatorHeightPolicy(Toolkit::ScrollBar::Fixed);
410   }
411   else
412   {
413     DALI_ASSERT_ALWAYS( !"ScrollBar::OnIndicatorHeightPolicyPropertySet(). Invalid Property value." );
414   }
415 }
416
417 void ScrollBar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
418 {
419   Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
420
421   if( scrollBar )
422   {
423     ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
424     switch( index )
425     {
426       case Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY:
427       {
428         scrollBarImpl.OnIndicatorHeightPolicyPropertySet( value );
429         break;
430       }
431       case Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT:
432       {
433         scrollBarImpl.SetIndicatorFixedHeight(value.Get<float>());
434         break;
435       }
436     }
437   }
438 }
439
440 Property::Value ScrollBar::GetProperty( BaseObject* object, Property::Index index )
441 {
442   Property::Value value;
443
444   Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
445
446   if( scrollBar )
447   {
448     ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
449     switch( index )
450     {
451       case Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY:
452       {
453         value = INDICATOR_HEIGHT_POLICY_NAME[ scrollBarImpl.GetIndicatorHeightPolicy() ];
454         break;
455       }
456       case Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT:
457       {
458         value = scrollBarImpl.GetIndicatorFixedHeight();
459         break;
460       }
461     }
462   }
463   return value;
464 }
465
466 Toolkit::ScrollBar ScrollBar::New()
467 {
468   // Create the implementation, temporarily owned by this handle on stack
469   IntrusivePtr< ScrollBar > impl = new ScrollBar();
470
471   // Pass ownership to CustomActor handle
472   Toolkit::ScrollBar handle( *impl );
473
474   // Second-phase init of the implementation
475   // This can only be done after the CustomActor connection has been made...
476   impl->Initialize();
477
478   return handle;
479 }
480
481 } // namespace Internal
482
483 } // namespace Toolkit
484
485 } // namespace Dali