Size negotiation patch 3: Scope size negotiation enums
[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 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 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/scroll-bar/scroll-bar-impl.h>
20
21 // EXTERNAL INCLUDES
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>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/internal/controls/scrollable/item-view/item-view-impl.h>
30
31 using namespace Dali;
32
33 namespace
34 {
35
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);
44
45 /**
46  * Indicator size depends on both indicator's parent size and the scroll content size
47  */
48 Vector3 IndicatorSize( const Vector3& parentSize, float contentSize)
49 {
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 );
54 }
55
56 /**
57  * Indicator position constraint
58  * Positions the indicator to reflect the current scroll position within the scroll domain.
59  */
60 struct IndicatorPositionConstraint
61 {
62   /**
63    * @param[in] minPosition The minimum limit of scroll position
64    * @param[in] maxPosition the maximum limit of scroll position
65    */
66   IndicatorPositionConstraint(float minPosition, float maxPosition)
67   : mMinPosition(minPosition),
68     mMaxPosition(maxPosition)
69   {
70   }
71
72   /**
73    * Constraint operator
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.
77    */
78   void operator()( Vector3& current, const PropertyInputContainer& inputs )
79   {
80     const Vector3& indicatorSize = inputs[0]->GetVector3();
81     const Vector3& parentSize = inputs[1]->GetVector3();
82     float scrollPosition = inputs[2]->GetFloat();
83
84     const float domainSize = fabs(mMaxPosition - mMinPosition);
85     float relativePosition = (mMaxPosition - scrollPosition) / domainSize;
86
87     current.y = relativePosition * ( parentSize.height - indicatorSize.height );
88     current.z = DEFAULT_SLIDER_DEPTH;
89   }
90
91   float mMinPosition;  ///< The minimum scroll position
92   float mMaxPosition;  ///< The maximum scroll position
93 };
94
95 } // unnamed namespace
96
97 namespace Dali
98 {
99
100 namespace Toolkit
101 {
102
103 namespace Internal
104 {
105
106 namespace
107 {
108
109 using namespace Dali;
110
111 BaseHandle Create()
112 {
113   return Toolkit::ScrollBar::New();
114 }
115
116 // Setup properties, signals and actions using the type-registry.
117 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ScrollBar, Toolkit::ScrollComponent, Create );
118
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         )
123
124 DALI_SIGNAL_REGISTRATION(   ScrollBar, "scroll-position-notified",        SCROLL_POSITION_NOTIFIED_SIGNAL )
125
126 DALI_TYPE_REGISTRATION_END()
127
128 const char* INDICATOR_HEIGHT_POLICY_NAME[] = {"Variable", "Fixed"};
129
130 }
131
132 ScrollBar::ScrollBar()
133 : mIndicatorShowDuration(DEFAULT_INDICATOR_SHOW_DURATION),
134   mIndicatorHideDuration(DEFAULT_INDICATOR_HIDE_DURATION),
135   mScrollStart(0.0f),
136   mIsPanning(false),
137   mCurrentScrollPosition(0.0f),
138   mIndicatorHeightPolicy(Toolkit::ScrollBar::Variable),
139   mIndicatorFixedHeight(DEFAULT_INDICATOR_FIXED_HEIGHT),
140   mPropertyIndicatorPosition(Property::INVALID_INDEX)
141 {
142 }
143
144 ScrollBar::~ScrollBar()
145 {
146 }
147
148 void ScrollBar::OnInitialize()
149 {
150   Actor self = Self();
151
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);
159
160   self.SetDrawMode(DrawMode::OVERLAY);
161
162   // Enable the pan gesture which is attached to the control
163   EnableGestureDetection(Gesture::Type(Gesture::Pan));
164 }
165
166 void ScrollBar::OnScrollConnectorSet( Toolkit::ScrollConnector oldConnector )
167 {
168   if( oldConnector )
169   {
170     oldConnector.DomainChangedSignal().Disconnect(this, &ScrollBar::OnScrollDomainChanged);
171
172     mScrollPositionObject.Reset();
173   }
174
175   if( mScrollConnector )
176   {
177     mScrollPositionObject = mScrollConnector.GetScrollPositionObject();
178
179     ApplyConstraints();
180     mScrollConnector.DomainChangedSignal().Connect(this, &ScrollBar::OnScrollDomainChanged);
181   }
182 }
183
184 void ScrollBar::SetIndicatorImage( Image image )
185 {
186   mIndicator.SetImage(image);
187 }
188
189 Actor ScrollBar::GetScrollIndicator()
190 {
191   return mIndicator;
192 }
193
194 void ScrollBar::ApplyConstraints()
195 {
196   if( mScrollConnector )
197   {
198     // Set indicator height according to the indicator's height policy
199     if(mIndicatorHeightPolicy == Toolkit::ScrollBar::Fixed)
200     {
201       mIndicator.SetSize(Self().GetCurrentSize().width, mIndicatorFixedHeight);
202     }
203     else
204     {
205       mIndicator.SetSize( IndicatorSize( Self().GetCurrentSize(), mScrollConnector.GetContentLength() ) );
206     }
207
208     if(mIndicatorPositionConstraint)
209     {
210       mIndicatorPositionConstraint.Remove();
211     }
212
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();
218   }
219 }
220
221 void ScrollBar::SetPositionNotifications( const std::vector<float>& positions )
222 {
223   if(mScrollPositionObject)
224   {
225     if(mPositionNotification)
226     {
227       mScrollPositionObject.RemovePropertyNotification(mPositionNotification);
228     }
229     mPositionNotification = mScrollPositionObject.AddPropertyNotification( Toolkit::ScrollConnector::SCROLL_POSITION, VariableStepCondition(positions) );
230     mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionNotified );
231   }
232 }
233
234 void ScrollBar::OnScrollPositionNotified(PropertyNotification& source)
235 {
236   // Emit the signal to notify the scroll position crossing
237   mScrollPositionNotifiedSignal.Emit(mScrollConnector.GetScrollPosition());
238 }
239
240 void ScrollBar::Show()
241 {
242   Actor self = Self();
243
244   // Cancel any animation
245   if(mAnimation)
246   {
247     mAnimation.Clear();
248     mAnimation.Reset();
249   }
250
251   if(mIndicatorShowDuration > 0.0f)
252   {
253     mAnimation = Animation::New( mIndicatorShowDuration );
254     mAnimation.AnimateTo( Property( self, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunctions::EaseIn );
255     mAnimation.Play();
256   }
257   else
258   {
259     self.SetOpacity(1.0f);
260   }
261 }
262
263 void ScrollBar::Hide()
264 {
265   Actor self = Self();
266
267   // Cancel any animation
268   if(mAnimation)
269   {
270     mAnimation.Clear();
271     mAnimation.Reset();
272   }
273
274   if(mIndicatorHideDuration > 0.0f)
275   {
276     mAnimation = Animation::New( mIndicatorHideDuration );
277     mAnimation.AnimateTo( Property( self, Actor::Property::COLOR_ALPHA ), 0.0f, AlphaFunctions::EaseIn );
278     mAnimation.Play();
279   }
280   else
281   {
282     self.SetOpacity(0.0f);
283   }
284 }
285
286 bool ScrollBar::OnPanGestureProcessTick()
287 {
288   // Update the scroll position property.
289   if( mScrollConnector )
290   {
291     mScrollConnector.SetScrollPosition(mCurrentScrollPosition);
292   }
293
294   return true;
295 }
296
297 void ScrollBar::OnPan( PanGesture gesture )
298 {
299   if(mScrollConnector)
300   {
301     Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
302
303     switch(gesture.state)
304     {
305       case Gesture::Started:
306       {
307         if( !mTimer )
308         {
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 );
312           mTimer.Start();
313         }
314
315         Show();
316         mScrollStart = mScrollConnector.GetScrollPosition();
317         mGestureDisplacement = Vector3::ZERO;
318         mIsPanning = true;
319
320         break;
321       }
322       case Gesture::Continuing:
323       {
324         Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
325         mGestureDisplacement+=delta;
326
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()));
331
332         break;
333       }
334       default:
335       {
336         mIsPanning = false;
337
338         if( mTimer )
339         {
340           // Destroy the timer when pan gesture is finished.
341           mTimer.Stop();
342           mTimer.TickSignal().Disconnect( this, &ScrollBar::OnPanGestureProcessTick );
343           mTimer.Reset();
344         }
345
346         if(itemView)
347         {
348           // Refresh the ItemView cache with extra items
349           GetImpl(itemView).DoRefresh(mCurrentScrollPosition, true);
350         }
351
352         break;
353       }
354     }
355
356     if(itemView)
357     {
358       // Disable automatic refresh in ItemView during fast scrolling
359       GetImpl(itemView).SetRefreshEnabled(!mIsPanning);
360     }
361   }
362 }
363
364 void ScrollBar::OnControlSizeSet( const Vector3& size )
365 {
366   if(mIndicatorHeightPolicy != Toolkit::ScrollBar::Fixed && mScrollConnector)
367   {
368     mIndicator.SetSize( IndicatorSize( size, mScrollConnector.GetContentLength() ) );
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::SetIndicatorShowDuration( float durationSeconds )
401 {
402   mIndicatorShowDuration = durationSeconds;
403 }
404
405 float ScrollBar::GetIndicatorShowDuration()
406 {
407   return mIndicatorShowDuration;
408 }
409
410 void ScrollBar::SetIndicatorHideDuration( float durationSeconds )
411 {
412   mIndicatorHideDuration = durationSeconds;
413 }
414
415 float ScrollBar::GetIndicatorHideDuration()
416 {
417   return mIndicatorHideDuration;
418 }
419
420 void ScrollBar::OnIndicatorHeightPolicyPropertySet( Property::Value propertyValue )
421 {
422   std::string policyName( propertyValue.Get<std::string>() );
423   if(policyName == "Variable")
424   {
425     SetIndicatorHeightPolicy(Toolkit::ScrollBar::Variable);
426   }
427   else if(policyName == "Fixed")
428   {
429     SetIndicatorHeightPolicy(Toolkit::ScrollBar::Fixed);
430   }
431   else
432   {
433     DALI_ASSERT_ALWAYS( !"ScrollBar::OnIndicatorHeightPolicyPropertySet(). Invalid Property value." );
434   }
435 }
436
437 bool ScrollBar::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
438 {
439   Dali::BaseHandle handle( object );
440
441   bool connected( true );
442   Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( handle );
443
444   if( 0 == strcmp( signalName.c_str(), SCROLL_POSITION_NOTIFIED_SIGNAL ) )
445   {
446     scrollBar.ScrollPositionNotifiedSignal().Connect( tracker, functor );
447   }
448   else
449   {
450     // signalName does not match any signal
451     connected = false;
452   }
453
454   return connected;
455 }
456
457 void ScrollBar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
458 {
459   Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
460
461   if( scrollBar )
462   {
463     ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
464     switch( index )
465     {
466       case Toolkit::ScrollBar::Property::INDICATOR_HEIGHT_POLICY:
467       {
468         scrollBarImpl.OnIndicatorHeightPolicyPropertySet( value );
469         break;
470       }
471       case Toolkit::ScrollBar::Property::INDICATOR_FIXED_HEIGHT:
472       {
473         scrollBarImpl.SetIndicatorFixedHeight(value.Get<float>());
474         break;
475       }
476       case Toolkit::ScrollBar::Property::INDICATOR_SHOW_DURATION:
477       {
478         scrollBarImpl.SetIndicatorShowDuration(value.Get<float>());
479         break;
480       }
481       case Toolkit::ScrollBar::Property::INDICATOR_HIDE_DURATION:
482       {
483         scrollBarImpl.SetIndicatorHideDuration(value.Get<float>());
484         break;
485       }
486     }
487   }
488 }
489
490 Property::Value ScrollBar::GetProperty( BaseObject* object, Property::Index index )
491 {
492   Property::Value value;
493
494   Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
495
496   if( scrollBar )
497   {
498     ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
499     switch( index )
500     {
501       case Toolkit::ScrollBar::Property::INDICATOR_HEIGHT_POLICY:
502       {
503         value = INDICATOR_HEIGHT_POLICY_NAME[ scrollBarImpl.GetIndicatorHeightPolicy() ];
504         break;
505       }
506       case Toolkit::ScrollBar::Property::INDICATOR_FIXED_HEIGHT:
507       {
508         value = scrollBarImpl.GetIndicatorFixedHeight();
509         break;
510       }
511       case Toolkit::ScrollBar::Property::INDICATOR_SHOW_DURATION:
512       {
513         value = scrollBarImpl.GetIndicatorShowDuration();
514         break;
515       }
516       case Toolkit::ScrollBar::Property::INDICATOR_HIDE_DURATION:
517       {
518         value = scrollBarImpl.GetIndicatorHideDuration();
519         break;
520       }
521     }
522   }
523   return value;
524 }
525
526 Toolkit::ScrollBar ScrollBar::New()
527 {
528   // Create the implementation, temporarily owned by this handle on stack
529   IntrusivePtr< ScrollBar > impl = new ScrollBar();
530
531   // Pass ownership to CustomActor handle
532   Toolkit::ScrollBar handle( *impl );
533
534   // Second-phase init of the implementation
535   // This can only be done after the CustomActor connection has been made...
536   impl->Initialize();
537
538   return handle;
539 }
540
541 } // namespace Internal
542
543 } // namespace Toolkit
544
545 } // namespace Dali