ScrollView - Fix wrapping issue in FixedRuler::GetPageFromPosition
[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   mType = Fixed;
192 }
193
194 float FixedRuler::Snap(float x, float bias) const
195 {
196   return floor(x / mSpacing + bias) * mSpacing;
197 }
198
199 float FixedRuler::GetPositionFromPage(unsigned int page, unsigned int &volume, bool wrap) const
200 {
201   float position = mDomain.min;
202
203   volume = 0;
204
205   // spacing must be present.
206   if(mEnabled && fabsf(mSpacing) > Math::MACHINE_EPSILON_1)
207   {
208     unsigned int column = page;
209
210     // In carry mode, a volume (carry) is produced when page exceeds limit within domain
211     if(wrap)
212     {
213       unsigned int pagesPerVolume = mDomain.GetSize() / mSpacing;
214       if(pagesPerVolume>0)
215       {
216         column += pagesPerVolume;
217         column %= pagesPerVolume;
218         volume = page/pagesPerVolume;
219       }
220     }
221
222     position = mDomain.min + column * mSpacing;
223   }
224   else  // Domain (or Spacing) is not present, carry page to volume.
225   {
226     if(wrap)
227     {
228       volume = page;
229     }
230   }
231
232   return position;
233 }
234
235 unsigned int FixedRuler::GetPageFromPosition(float position, bool wrap) const
236 {
237   unsigned int page = 0;
238
239   // spacing must be present.
240   if(mEnabled && fabsf(mSpacing) > Math::MACHINE_EPSILON_1)
241   {
242     if( wrap )
243     {
244       position = WrapInDomain(position, mDomain.min, mDomain.max);
245     }
246     page = floor((position - mDomain.min) / mSpacing + 0.5f);
247
248     if(wrap)
249     {
250       unsigned int pagesPerVolume = mDomain.GetSize() / mSpacing;
251       page %= pagesPerVolume;
252     }
253   }
254
255   return page;
256 }
257
258 unsigned int FixedRuler::GetTotalPages() const
259 {
260   unsigned int pagesPerVolume = 1;
261
262   // spacing must be present.
263   if(mEnabled && fabsf(mSpacing) > Math::MACHINE_EPSILON_1)
264   {
265     pagesPerVolume = mDomain.GetSize() / mSpacing;
266   }
267
268   return pagesPerVolume;
269 }
270
271 ///////////////////////////////////////////////////////////////////////////////////////////////////
272 // ScrollView
273 ///////////////////////////////////////////////////////////////////////////////////////////////////
274
275 const std::string ScrollView::SCROLL_PAGE_CURRENT( "scroll-page-current" );
276 const std::string ScrollView::SCROLL_TIME_PROPERTY_NAME( "scroll-time" );
277 const std::string ScrollView::SCROLL_POSITION_PROPERTY_NAME( "scroll-position" );
278 const std::string ScrollView::SCROLL_PRE_POSITION_PROPERTY_NAME( "scroll-pre-position" );
279 const std::string ScrollView::SCROLL_OVERSHOOT_X_PROPERTY_NAME( "scroll-overshoot-x" );
280 const std::string ScrollView::SCROLL_OVERSHOOT_Y_PROPERTY_NAME( "scroll-overshoot-y" );
281 const std::string ScrollView::SCROLL_FINAL_PROPERTY_NAME( "scroll-final" );
282 const std::string ScrollView::SCROLL_X_PROPERTY_NAME( "scroll-x" );
283 const std::string ScrollView::SCROLL_Y_PROPERTY_NAME( "scroll-y" );
284 const std::string ScrollView::SCROLL_SCALE_PROPERTY_NAME( "scroll-scale" );
285 const std::string ScrollView::SCROLL_WRAP_PROPERTY_NAME( "scroll-wrap" );
286 const std::string ScrollView::SCROLL_PANNING_PROPERTY_NAME( "scroll-panning" );
287 const std::string ScrollView::SCROLL_SCROLLING_PROPERTY_NAME( "scroll-scrolling" );
288 const std::string ScrollView::SCROLL_POSITION_DELTA_PROPERTY_NAME( "scroll-position-delta" );
289 const std::string ScrollView::SCROLL_START_PAGE_POSITION_PROPERTY_NAME( "scroll-start-page-position" );
290
291 const float ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f);
292 const float ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f);
293 const float ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION(0.5f);
294 const float ScrollView::DEFAULT_MAX_OVERSHOOT(100.0f);  // 100 pixels
295
296 const float ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f);
297 const float ScrollView::DEFAULT_FRICTION_COEFFICIENT(1.0f);
298 const float ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f);
299 const float ScrollView::DEFAULT_MAX_FLICK_SPEED(3.0f);
300
301 const char* const ScrollView::SIGNAL_SNAP_STARTED = "snap-started";
302
303 ScrollView::ScrollView()
304 {
305 }
306
307 ScrollView::ScrollView(Internal::ScrollView& implementation)
308 : Scrollable(implementation)
309 {
310 }
311
312 ScrollView::ScrollView( Dali::Internal::CustomActor* internal )
313 : Scrollable( internal )
314 {
315   VerifyCustomActorPointer<Internal::ScrollView>(internal);
316 }
317
318 ScrollView::ScrollView( const ScrollView& handle )
319 : Scrollable( handle )
320 {
321 }
322
323 ScrollView& ScrollView::operator=( const ScrollView& handle )
324 {
325   if( &handle != this )
326   {
327     Control::operator=( handle );
328   }
329   return *this;
330 }
331
332 ScrollView ScrollView::New()
333 {
334   return Internal::ScrollView::New();
335 }
336
337 ScrollView::~ScrollView()
338 {
339 }
340
341 ScrollView ScrollView::DownCast( BaseHandle handle )
342 {
343   return Control::DownCast<ScrollView, Internal::ScrollView>(handle);
344 }
345
346 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
347 {
348   return GetImpl(*this).GetScrollSnapAlphaFunction();
349 }
350
351 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
352 {
353   GetImpl(*this).SetScrollSnapAlphaFunction(alpha);
354 }
355
356 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
357 {
358   return GetImpl(*this).GetScrollFlickAlphaFunction();
359 }
360
361 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
362 {
363   GetImpl(*this).SetScrollFlickAlphaFunction(alpha);
364 }
365
366 float ScrollView::GetScrollSnapDuration() const
367 {
368   return GetImpl(*this).GetScrollSnapDuration();
369 }
370
371 void ScrollView::SetScrollSnapDuration(float time)
372 {
373   GetImpl(*this).SetScrollSnapDuration(time);
374 }
375
376 float ScrollView::GetScrollFlickDuration() const
377 {
378   return GetImpl(*this).GetScrollFlickDuration();
379 }
380
381 void ScrollView::SetScrollFlickDuration(float time)
382 {
383   GetImpl(*this).SetScrollFlickDuration(time);
384 }
385
386 void ScrollView::SetRulerX(RulerPtr ruler)
387 {
388   GetImpl(*this).SetRulerX(ruler);
389 }
390
391 void ScrollView::SetRulerY(RulerPtr ruler)
392 {
393   GetImpl(*this).SetRulerY(ruler);
394 }
395
396 void ScrollView::SetRulerScaleX(RulerPtr ruler)
397 {
398   DALI_LOG_ERROR( "Deprecated" );
399 }
400
401 void ScrollView::SetRulerScaleY(RulerPtr ruler)
402 {
403   DALI_LOG_ERROR( "Deprecated" );
404 }
405
406 void ScrollView::SetScrollSensitive(bool sensitive)
407 {
408   GetImpl(*this).SetScrollSensitive(sensitive);
409 }
410
411 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
412 {
413   GetImpl(*this).SetMaxOvershoot(overshootX, overshootY);
414 }
415
416 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
417 {
418   GetImpl(*this).SetSnapOvershootAlphaFunction(alpha);
419 }
420
421 void ScrollView::SetSnapOvershootDuration(float duration)
422 {
423   GetImpl(*this).SetSnapOvershootDuration(duration);
424 }
425
426 void ScrollView::SetTouchesRequiredForPanning(unsigned int minTouches, unsigned int maxTouches, bool endOutside)
427 {
428   GetImpl(*this).SetTouchesRequiredForPanning(minTouches, maxTouches, endOutside);
429 }
430
431 void ScrollView::SetActorAutoSnap(bool enable)
432 {
433   GetImpl(*this).SetActorAutoSnap(enable);
434 }
435
436 void ScrollView::SetWrapMode(bool enable)
437 {
438   GetImpl(*this).SetWrapMode(enable);
439 }
440
441 int ScrollView::GetRefreshInterval() const
442 {
443   return GetImpl(*this).GetRefreshInterval();
444 }
445
446 void ScrollView::SetRefreshInterval(int milliseconds)
447 {
448   GetImpl(*this).SetRefreshInterval(milliseconds);
449 }
450
451 int ScrollView::GetScrollUpdateDistance() const
452 {
453   return GetImpl(*this).GetScrollUpdateDistance();
454 }
455
456 void ScrollView::SetScrollUpdateDistance(int distance)
457 {
458   GetImpl(*this).SetScrollUpdateDistance(distance);
459 }
460
461 bool ScrollView::GetAxisAutoLock() const
462 {
463   return GetImpl(*this).GetAxisAutoLock();
464 }
465
466 void ScrollView::SetAxisAutoLock(bool enable)
467 {
468   GetImpl(*this).SetAxisAutoLock(enable);
469 }
470
471 float ScrollView::GetAxisAutoLockGradient() const
472 {
473   return GetImpl(*this).GetAxisAutoLockGradient();
474 }
475
476 void ScrollView::SetAxisAutoLockGradient(float gradient)
477 {
478   GetImpl(*this).SetAxisAutoLockGradient(gradient);
479 }
480
481 float ScrollView::GetFrictionCoefficient() const
482 {
483   return GetImpl(*this).GetFrictionCoefficient();
484 }
485
486 void ScrollView::SetFrictionCoefficient(float friction)
487 {
488   GetImpl(*this).SetFrictionCoefficient(friction);
489 }
490
491 float ScrollView::GetFlickSpeedCoefficient() const
492 {
493   return GetImpl(*this).GetFlickSpeedCoefficient();
494 }
495
496 void ScrollView::SetFlickSpeedCoefficient(float speed)
497 {
498   GetImpl(*this).SetFlickSpeedCoefficient(speed);
499 }
500
501 Vector2 ScrollView::GetMinimumDistanceForFlick() const
502 {
503   return GetImpl(*this).GetMinimumDistanceForFlick();
504 }
505
506 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
507 {
508   GetImpl(*this).SetMinimumDistanceForFlick(distance);
509 }
510
511 float ScrollView::GetMinimumSpeedForFlick() const
512 {
513   return GetImpl(*this).GetMinimumSpeedForFlick();
514 }
515
516 void ScrollView::SetMinimumSpeedForFlick( float speed )
517 {
518   GetImpl(*this).SetMinimumSpeedForFlick(speed);
519 }
520
521 float ScrollView::GetMaxFlickSpeed() const
522 {
523   return GetImpl(*this).GetMaxFlickSpeed();
524 }
525
526 void ScrollView::SetMaxFlickSpeed(float speed)
527 {
528   GetImpl(*this).SetMaxFlickSpeed(speed);
529 }
530
531 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
532 {
533   return GetImpl(*this).GetMouseWheelScrollDistanceStep();
534 }
535
536 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
537 {
538   GetImpl(*this).SetMouseWheelScrollDistanceStep(step);
539 }
540
541 Vector3 ScrollView::GetCurrentScrollPosition() const
542 {
543   return GetImpl(*this).GetCurrentScrollPosition();
544 }
545
546 void ScrollView::SetScrollPosition(const Vector3& position)
547 {
548   GetImpl(*this).SetScrollPosition(position);
549 }
550
551 Vector3 ScrollView::GetCurrentScrollScale() const
552 {
553   DALI_LOG_ERROR( "Deprecated" );
554   return Vector3::ONE;
555 }
556
557 unsigned int ScrollView::GetCurrentPage() const
558 {
559   return GetImpl(*this).GetCurrentPage();
560 }
561
562 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation)
563 {
564   DALI_LOG_ERROR( "Deprecated" );
565
566   GetImpl(*this).TransformTo(position);
567 }
568
569 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation, float duration)
570 {
571   DALI_LOG_ERROR( "Deprecated" );
572
573   GetImpl(*this).TransformTo(position, duration);
574 }
575
576 void ScrollView::ScrollTo(const Vector3 &position)
577 {
578   GetImpl(*this).ScrollTo(position);
579 }
580
581 void ScrollView::ScrollTo(const Vector3 &position, float duration)
582 {
583   GetImpl(*this).ScrollTo(position, duration);
584 }
585
586 void ScrollView::ScrollTo(const Vector3 &position, float duration,
587                           DirectionBias horizontalBias, DirectionBias verticalBias)
588 {
589   GetImpl(*this).ScrollTo(position, duration, horizontalBias, verticalBias);
590 }
591
592 void ScrollView::ScrollTo(unsigned int page)
593 {
594   GetImpl(*this).ScrollTo(page);
595 }
596
597 void ScrollView::ScrollTo(unsigned int page, float duration)
598 {
599   GetImpl(*this).ScrollTo(page, duration);
600 }
601
602 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
603 {
604   GetImpl(*this).ScrollTo(page, duration, bias);
605 }
606
607 void ScrollView::ScrollTo(Actor &actor)
608 {
609   GetImpl(*this).ScrollTo(actor);
610 }
611
612 void ScrollView::ScrollTo(Actor &actor, float duration)
613 {
614   GetImpl(*this).ScrollTo(actor, duration);
615 }
616
617 bool ScrollView::ScrollToSnapPoint()
618 {
619   return GetImpl(*this).ScrollToSnapPoint();
620 }
621
622 void ScrollView::ScaleTo(const Vector3 &scale)
623 {
624   DALI_LOG_ERROR( "Deprecated" );
625 }
626
627 void ScrollView::ScaleTo(const Vector3 &scale, float duration)
628 {
629   DALI_LOG_ERROR( "Deprecated" );
630 }
631
632 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
633 {
634   GetImpl(*this).ApplyConstraintToChildren(constraint);
635 }
636
637 void ScrollView::RemoveConstraintsFromChildren()
638 {
639   GetImpl(*this).RemoveConstraintsFromChildren();
640 }
641
642 void ScrollView::ApplyEffect(ScrollViewEffect effect)
643 {
644   GetImpl(*this).ApplyEffect(effect);
645 }
646
647 ScrollViewEffect ScrollView::ApplyEffect(ScrollView::PageEffect effect)
648 {
649   return GetImpl(*this).ApplyEffect(effect);
650 }
651
652 void ScrollView::RemoveEffect(ScrollViewEffect effect)
653 {
654   GetImpl(*this).RemoveEffect(effect);
655 }
656
657 void ScrollView::RemoveAllEffects()
658 {
659   GetImpl(*this).RemoveAllEffects();
660 }
661
662 void ScrollView::BindActor(Actor child)
663 {
664   GetImpl(*this).BindActor(child);
665 }
666
667 void ScrollView::UnbindActor(Actor child)
668 {
669   GetImpl(*this).UnbindActor(child);
670 }
671
672 ScrollView::SnapStartedSignalV2& ScrollView::SnapStartedSignal()
673 {
674   return GetImpl(*this).SnapStartedSignal();
675 }
676
677 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
678 {
679   GetImpl(*this).SetScrollingDirection( direction, threshold );
680 }
681
682 void ScrollView::RemoveScrollingDirection( Radian direction )
683 {
684   GetImpl(*this).RemoveScrollingDirection( direction );
685 }
686
687 } // namespace Toolkit
688
689 } // namespace Dali