85aec941965b35817930332fc32ad9b851e6a6d9
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / public-api / controls / scrollable / scroll-view / scroll-view.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/public-api/controls/scrollable/scroll-view/scroll-view.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/scrollable/scrollable.h>
26 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.h>
27
28 using namespace Dali;
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 ///////////////////////////////////////////////////////////////////////////////////////////////////
37 // RulerDomain
38 ///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 RulerDomain::RulerDomain(float min, float max, bool enabled)
41 : min(min),
42   max(max),
43   enabled(enabled)
44 {
45 }
46
47 float RulerDomain::Clamp(float x, float length, float scale) const
48 {
49   ClampState clamped;
50
51   return Clamp(x, length, scale, clamped);
52 }
53
54 float RulerDomain::Clamp(float x, float length, float scale, ClampState &clamped) const
55 {
56   if(!enabled)
57   {
58     clamped = NotClamped;
59     return x;
60   }
61
62   const float minExtent = min * scale;
63   const float maxExtent = max * scale - length;
64   if(x < minExtent)
65   {
66     clamped = ClampedToMin;
67     return minExtent;
68   }
69   else if(x > maxExtent)
70   {
71     clamped = ClampedToMax;
72     return maxExtent;
73   }
74
75   clamped = NotClamped;
76   return x;
77 }
78
79 float RulerDomain::GetSize() const
80 {
81   return max-min;
82 }
83
84 ///////////////////////////////////////////////////////////////////////////////////////////////////
85 // Ruler
86 ///////////////////////////////////////////////////////////////////////////////////////////////////
87
88 Ruler::Ruler()
89 : mType(Free),
90   mEnabled(true),
91   mDomain(RulerDomain(0.0f,1.0f,false))
92 {
93 }
94
95 Ruler::~Ruler()
96 {
97 }
98
99 Ruler::RulerType Ruler::GetType() const
100 {
101   return mType;
102 }
103
104 bool Ruler::IsEnabled() const
105 {
106   return mEnabled;
107 }
108
109 void Ruler::Enable()
110 {
111   mEnabled = true;
112 }
113
114 void Ruler::Disable()
115 {
116   mEnabled = false;
117 }
118
119 void Ruler::SetDomain(RulerDomain domain)
120 {
121   mDomain = domain;
122 }
123
124 const RulerDomain &Ruler::GetDomain() const
125 {
126   return mDomain;
127 }
128
129 void Ruler::DisableDomain()
130 {
131   mDomain = RulerDomain(0.0f,1.0f,false);
132 }
133
134 float Ruler::Clamp(float x, float length, float scale) const
135 {
136   return mDomain.Clamp(x, length, scale);
137 }
138
139 float Ruler::Clamp(float x, float length, float scale, ClampState &clamped) const
140 {
141   return mDomain.Clamp(x, length, scale, clamped);
142 }
143
144 float Ruler::SnapAndClamp(float x, float bias, float length, float scale) const
145 {
146   return Clamp(Snap(x, bias), length, scale);
147 }
148
149 float Ruler::SnapAndClamp(float x, float bias, float length, float scale, ClampState &clamped) const
150 {
151   return Clamp(Snap(x, bias), length, scale, clamped);
152 }
153
154 ///////////////////////////////////////////////////////////////////////////////////////////////////
155 // DefaultRuler
156 ///////////////////////////////////////////////////////////////////////////////////////////////////
157
158 DefaultRuler::DefaultRuler()
159 {
160   mType = Free;
161 }
162
163 float DefaultRuler::Snap(float x, float bias) const
164 {
165   return x;
166 }
167
168 float DefaultRuler::GetPositionFromPage(unsigned int page, unsigned int &volume, bool wrap) const
169 {
170   volume = 0;
171   return 0.0f;
172 }
173
174 unsigned int DefaultRuler::GetPageFromPosition(float position, bool wrap) const
175 {
176   return 0;
177 }
178
179 unsigned int DefaultRuler::GetTotalPages() const
180 {
181   return 1;
182 }
183
184 ///////////////////////////////////////////////////////////////////////////////////////////////////
185 // FixedRuler
186 ///////////////////////////////////////////////////////////////////////////////////////////////////
187
188 FixedRuler::FixedRuler(float spacing)
189 : mSpacing(spacing)
190 {
191   if(fabsf(mSpacing) <= Math::MACHINE_EPSILON_1)
192   {
193     DALI_LOG_ERROR( "Page spacing too small (%f).", double(spacing) );
194     mSpacing = spacing >= 0.0f ? Math::MACHINE_EPSILON_1 : -Math::MACHINE_EPSILON_1;
195   }
196   mType = Fixed;
197 }
198
199 float FixedRuler::Snap(float x, float bias) const
200 {
201   return floor(x / mSpacing + bias) * mSpacing;
202 }
203
204 float FixedRuler::GetPositionFromPage(unsigned int page, unsigned int &volume, bool wrap) const
205 {
206   float position = mDomain.min;
207
208   volume = 0;
209
210   // spacing must be present.
211   if( mEnabled )
212   {
213     unsigned int column = page;
214
215     // In carry mode, a volume (carry) is produced when page exceeds limit within domain
216     if(wrap)
217     {
218       unsigned int pagesPerVolume = mDomain.GetSize() / mSpacing;
219       if(pagesPerVolume>0)
220       {
221         column += pagesPerVolume;
222         column %= pagesPerVolume;
223         volume = page/pagesPerVolume;
224       }
225     }
226
227     position = mDomain.min + column * mSpacing;
228   }
229   else  // Domain (or Spacing) is not present, carry page to volume.
230   {
231     if(wrap)
232     {
233       volume = page;
234     }
235   }
236
237   return position;
238 }
239
240 unsigned int FixedRuler::GetPageFromPosition(float position, bool wrap) const
241 {
242   unsigned int page = 0;
243
244   // spacing must be present.
245   if( mEnabled )
246   {
247     if( wrap )
248     {
249       position = WrapInDomain(position, mDomain.min, mDomain.max);
250     }
251     page = floor((position - mDomain.min) / mSpacing + 0.5f);
252
253     if(wrap)
254     {
255       unsigned int pagesPerVolume = mDomain.GetSize() / mSpacing;
256       // Defensive check to avoid a divide by zero below when the ruler is in an invalid state (entire domain smaller than spacing between pages of it):
257       if(pagesPerVolume < 1u)
258       {
259         pagesPerVolume = 1u;
260         DALI_LOG_ERROR("Ruler domain(%f) is smaller than its spacing(%f).", mDomain.GetSize() * 1.0, mSpacing * 1.0 );
261       }
262       page %= pagesPerVolume;
263     }
264   }
265
266   return page;
267 }
268
269 unsigned int FixedRuler::GetTotalPages() const
270 {
271   unsigned int pagesPerVolume = 1;
272
273   // spacing must be present.
274   if( mEnabled )
275   {
276     pagesPerVolume = mDomain.GetSize() / mSpacing;
277   }
278
279   return pagesPerVolume;
280 }
281
282 ///////////////////////////////////////////////////////////////////////////////////////////////////
283 // ScrollView
284 ///////////////////////////////////////////////////////////////////////////////////////////////////
285
286 const std::string ScrollView::SCROLL_PAGE_CURRENT( "scroll-page-current" );
287 const std::string ScrollView::SCROLL_TIME_PROPERTY_NAME( "scroll-time" );
288 const std::string ScrollView::SCROLL_POSITION_PROPERTY_NAME( "scroll-position" );
289 const std::string ScrollView::SCROLL_PRE_POSITION_PROPERTY_NAME( "scroll-pre-position" );
290 const std::string ScrollView::SCROLL_OVERSHOOT_X_PROPERTY_NAME( "scroll-overshoot-x" );
291 const std::string ScrollView::SCROLL_OVERSHOOT_Y_PROPERTY_NAME( "scroll-overshoot-y" );
292 const std::string ScrollView::SCROLL_FINAL_PROPERTY_NAME( "scroll-final" );
293 const std::string ScrollView::SCROLL_SCALE_PROPERTY_NAME( "scroll-scale" );
294 const std::string ScrollView::SCROLL_WRAP_PROPERTY_NAME( "scroll-wrap" );
295 const std::string ScrollView::SCROLL_PANNING_PROPERTY_NAME( "scroll-panning" );
296 const std::string ScrollView::SCROLL_SCROLLING_PROPERTY_NAME( "scroll-scrolling" );
297 const std::string ScrollView::SCROLL_POSITION_DELTA_PROPERTY_NAME( "scroll-position-delta" );
298 const std::string ScrollView::SCROLL_START_PAGE_POSITION_PROPERTY_NAME( "scroll-start-page-position" );
299
300 const float ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f);
301 const float ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f);
302 const float ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION(0.5f);
303 const float ScrollView::DEFAULT_MAX_OVERSHOOT(100.0f);  // 100 pixels
304
305 const float ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f);
306 const float ScrollView::DEFAULT_FRICTION_COEFFICIENT(1.0f);
307 const float ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f);
308 const float ScrollView::DEFAULT_MAX_FLICK_SPEED(3.0f);
309
310 const char* const ScrollView::SIGNAL_SNAP_STARTED = "snap-started";
311
312 ScrollView::ScrollView()
313 {
314 }
315
316 ScrollView::ScrollView(Internal::ScrollView& implementation)
317 : Scrollable(implementation)
318 {
319 }
320
321 ScrollView::ScrollView( Dali::Internal::CustomActor* internal )
322 : Scrollable( internal )
323 {
324   VerifyCustomActorPointer<Internal::ScrollView>(internal);
325 }
326
327 ScrollView::ScrollView( const ScrollView& handle )
328 : Scrollable( handle )
329 {
330 }
331
332 ScrollView& ScrollView::operator=( const ScrollView& handle )
333 {
334   if( &handle != this )
335   {
336     Control::operator=( handle );
337   }
338   return *this;
339 }
340
341 ScrollView ScrollView::New()
342 {
343   return Internal::ScrollView::New();
344 }
345
346 ScrollView::~ScrollView()
347 {
348 }
349
350 ScrollView ScrollView::DownCast( BaseHandle handle )
351 {
352   return Control::DownCast<ScrollView, Internal::ScrollView>(handle);
353 }
354
355 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
356 {
357   return GetImpl(*this).GetScrollSnapAlphaFunction();
358 }
359
360 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
361 {
362   GetImpl(*this).SetScrollSnapAlphaFunction(alpha);
363 }
364
365 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
366 {
367   return GetImpl(*this).GetScrollFlickAlphaFunction();
368 }
369
370 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
371 {
372   GetImpl(*this).SetScrollFlickAlphaFunction(alpha);
373 }
374
375 float ScrollView::GetScrollSnapDuration() const
376 {
377   return GetImpl(*this).GetScrollSnapDuration();
378 }
379
380 void ScrollView::SetScrollSnapDuration(float time)
381 {
382   GetImpl(*this).SetScrollSnapDuration(time);
383 }
384
385 float ScrollView::GetScrollFlickDuration() const
386 {
387   return GetImpl(*this).GetScrollFlickDuration();
388 }
389
390 void ScrollView::SetScrollFlickDuration(float time)
391 {
392   GetImpl(*this).SetScrollFlickDuration(time);
393 }
394
395 void ScrollView::SetRulerX(RulerPtr ruler)
396 {
397   GetImpl(*this).SetRulerX(ruler);
398 }
399
400 void ScrollView::SetRulerY(RulerPtr ruler)
401 {
402   GetImpl(*this).SetRulerY(ruler);
403 }
404
405 void ScrollView::SetScrollSensitive(bool sensitive)
406 {
407   GetImpl(*this).SetScrollSensitive(sensitive);
408 }
409
410 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
411 {
412   GetImpl(*this).SetMaxOvershoot(overshootX, overshootY);
413 }
414
415 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
416 {
417   GetImpl(*this).SetSnapOvershootAlphaFunction(alpha);
418 }
419
420 void ScrollView::SetSnapOvershootDuration(float duration)
421 {
422   GetImpl(*this).SetSnapOvershootDuration(duration);
423 }
424
425 void ScrollView::SetTouchesRequiredForPanning(unsigned int minTouches, unsigned int maxTouches, bool endOutside)
426 {
427   GetImpl(*this).SetTouchesRequiredForPanning(minTouches, maxTouches, endOutside);
428 }
429
430 void ScrollView::SetActorAutoSnap(bool enable)
431 {
432   GetImpl(*this).SetActorAutoSnap(enable);
433 }
434
435 void ScrollView::SetWrapMode(bool enable)
436 {
437   GetImpl(*this).SetWrapMode(enable);
438 }
439
440 int ScrollView::GetScrollUpdateDistance() const
441 {
442   return GetImpl(*this).GetScrollUpdateDistance();
443 }
444
445 void ScrollView::SetScrollUpdateDistance(int distance)
446 {
447   GetImpl(*this).SetScrollUpdateDistance(distance);
448 }
449
450 bool ScrollView::GetAxisAutoLock() const
451 {
452   return GetImpl(*this).GetAxisAutoLock();
453 }
454
455 void ScrollView::SetAxisAutoLock(bool enable)
456 {
457   GetImpl(*this).SetAxisAutoLock(enable);
458 }
459
460 float ScrollView::GetAxisAutoLockGradient() const
461 {
462   return GetImpl(*this).GetAxisAutoLockGradient();
463 }
464
465 void ScrollView::SetAxisAutoLockGradient(float gradient)
466 {
467   GetImpl(*this).SetAxisAutoLockGradient(gradient);
468 }
469
470 float ScrollView::GetFrictionCoefficient() const
471 {
472   return GetImpl(*this).GetFrictionCoefficient();
473 }
474
475 void ScrollView::SetFrictionCoefficient(float friction)
476 {
477   GetImpl(*this).SetFrictionCoefficient(friction);
478 }
479
480 float ScrollView::GetFlickSpeedCoefficient() const
481 {
482   return GetImpl(*this).GetFlickSpeedCoefficient();
483 }
484
485 void ScrollView::SetFlickSpeedCoefficient(float speed)
486 {
487   GetImpl(*this).SetFlickSpeedCoefficient(speed);
488 }
489
490 Vector2 ScrollView::GetMinimumDistanceForFlick() const
491 {
492   return GetImpl(*this).GetMinimumDistanceForFlick();
493 }
494
495 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
496 {
497   GetImpl(*this).SetMinimumDistanceForFlick(distance);
498 }
499
500 float ScrollView::GetMinimumSpeedForFlick() const
501 {
502   return GetImpl(*this).GetMinimumSpeedForFlick();
503 }
504
505 void ScrollView::SetMinimumSpeedForFlick( float speed )
506 {
507   GetImpl(*this).SetMinimumSpeedForFlick(speed);
508 }
509
510 float ScrollView::GetMaxFlickSpeed() const
511 {
512   return GetImpl(*this).GetMaxFlickSpeed();
513 }
514
515 void ScrollView::SetMaxFlickSpeed(float speed)
516 {
517   GetImpl(*this).SetMaxFlickSpeed(speed);
518 }
519
520 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
521 {
522   return GetImpl(*this).GetMouseWheelScrollDistanceStep();
523 }
524
525 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
526 {
527   GetImpl(*this).SetMouseWheelScrollDistanceStep(step);
528 }
529
530 Vector3 ScrollView::GetCurrentScrollPosition() const
531 {
532   return GetImpl(*this).GetCurrentScrollPosition();
533 }
534
535 void ScrollView::SetScrollPosition(const Vector3& position)
536 {
537   GetImpl(*this).SetScrollPosition(position);
538 }
539
540 unsigned int ScrollView::GetCurrentPage() const
541 {
542   return GetImpl(*this).GetCurrentPage();
543 }
544
545 void ScrollView::ScrollTo(const Vector3 &position)
546 {
547   GetImpl(*this).ScrollTo(position);
548 }
549
550 void ScrollView::ScrollTo(const Vector3 &position, float duration)
551 {
552   GetImpl(*this).ScrollTo(position, duration);
553 }
554
555 void ScrollView::ScrollTo(const Vector3 &position, float duration, AlphaFunction alpha)
556 {
557   GetImpl(*this).ScrollTo(position, duration, alpha);
558 }
559
560 void ScrollView::ScrollTo(const Vector3 &position, float duration,
561                           DirectionBias horizontalBias, DirectionBias verticalBias)
562 {
563   GetImpl(*this).ScrollTo(position, duration, horizontalBias, verticalBias);
564 }
565
566 void ScrollView::ScrollTo(const Vector3 &position, float duration, AlphaFunction alpha,
567                           DirectionBias horizontalBias, DirectionBias verticalBias)
568 {
569   GetImpl(*this).ScrollTo(position, duration, alpha, horizontalBias, verticalBias);
570 }
571
572 void ScrollView::ScrollTo(unsigned int page)
573 {
574   GetImpl(*this).ScrollTo(page);
575 }
576
577 void ScrollView::ScrollTo(unsigned int page, float duration)
578 {
579   GetImpl(*this).ScrollTo(page, duration);
580 }
581
582 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
583 {
584   GetImpl(*this).ScrollTo(page, duration, bias);
585 }
586
587 void ScrollView::ScrollTo(Actor &actor)
588 {
589   GetImpl(*this).ScrollTo(actor);
590 }
591
592 void ScrollView::ScrollTo(Actor &actor, float duration)
593 {
594   GetImpl(*this).ScrollTo(actor, duration);
595 }
596
597 bool ScrollView::ScrollToSnapPoint()
598 {
599   return GetImpl(*this).ScrollToSnapPoint();
600 }
601
602 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
603 {
604   GetImpl(*this).ApplyConstraintToChildren(constraint);
605 }
606
607 void ScrollView::RemoveConstraintsFromChildren()
608 {
609   GetImpl(*this).RemoveConstraintsFromChildren();
610 }
611
612 void ScrollView::ApplyEffect(ScrollViewEffect effect)
613 {
614   GetImpl(*this).ApplyEffect(effect);
615 }
616
617 void ScrollView::RemoveEffect(ScrollViewEffect effect)
618 {
619   GetImpl(*this).RemoveEffect(effect);
620 }
621
622 void ScrollView::RemoveAllEffects()
623 {
624   GetImpl(*this).RemoveAllEffects();
625 }
626
627 void ScrollView::BindActor(Actor child)
628 {
629   GetImpl(*this).BindActor(child);
630 }
631
632 void ScrollView::UnbindActor(Actor child)
633 {
634   GetImpl(*this).UnbindActor(child);
635 }
636
637 ScrollView::SnapStartedSignalV2& ScrollView::SnapStartedSignal()
638 {
639   return GetImpl(*this).SnapStartedSignal();
640 }
641
642 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
643 {
644   GetImpl(*this).SetScrollingDirection( direction, threshold );
645 }
646
647 void ScrollView::RemoveScrollingDirection( Radian direction )
648 {
649   GetImpl(*this).RemoveScrollingDirection( direction );
650 }
651
652 } // namespace Toolkit
653
654 } // namespace Dali