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