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