2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/public-api/controls/scrollable/scrollable.h>
26 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.h>
36 ///////////////////////////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////////////////////////
40 RulerDomain::RulerDomain(float min, float max, bool enabled)
47 float RulerDomain::Clamp(float x, float length, float scale) const
51 return Clamp(x, length, scale, clamped);
54 float RulerDomain::Clamp(float x, float length, float scale, ClampState &clamped) const
62 const float minExtent = min * scale;
63 const float maxExtent = max * scale - length;
66 clamped = ClampedToMin;
69 else if(x > maxExtent)
71 clamped = ClampedToMax;
79 float RulerDomain::GetSize() const
84 ///////////////////////////////////////////////////////////////////////////////////////////////////
86 ///////////////////////////////////////////////////////////////////////////////////////////////////
91 mDomain(RulerDomain(0.0f,1.0f,false))
99 Ruler::RulerType Ruler::GetType() const
104 bool Ruler::IsEnabled() const
114 void Ruler::Disable()
119 void Ruler::SetDomain(RulerDomain domain)
124 const RulerDomain &Ruler::GetDomain() const
129 void Ruler::DisableDomain()
131 mDomain = RulerDomain(0.0f,1.0f,false);
134 float Ruler::Clamp(float x, float length, float scale) const
136 return mDomain.Clamp(x, length, scale);
139 float Ruler::Clamp(float x, float length, float scale, ClampState &clamped) const
141 return mDomain.Clamp(x, length, scale, clamped);
144 float Ruler::SnapAndClamp(float x, float bias, float length, float scale) const
146 return Clamp(Snap(x, bias), length, scale);
149 float Ruler::SnapAndClamp(float x, float bias, float length, float scale, ClampState &clamped) const
151 return Clamp(Snap(x, bias), length, scale, clamped);
154 ///////////////////////////////////////////////////////////////////////////////////////////////////
156 ///////////////////////////////////////////////////////////////////////////////////////////////////
158 DefaultRuler::DefaultRuler()
163 float DefaultRuler::Snap(float x, float bias) const
168 float DefaultRuler::GetPositionFromPage(unsigned int page, unsigned int &volume, bool wrap) const
174 unsigned int DefaultRuler::GetPageFromPosition(float position, bool wrap) const
179 unsigned int DefaultRuler::GetTotalPages() const
184 ///////////////////////////////////////////////////////////////////////////////////////////////////
186 ///////////////////////////////////////////////////////////////////////////////////////////////////
188 FixedRuler::FixedRuler(float spacing)
191 if(fabsf(mSpacing) <= Math::MACHINE_EPSILON_1)
193 DALI_LOG_ERROR( "Page spacing too small (%f).", double(spacing) );
194 mSpacing = spacing >= 0.0f ? Math::MACHINE_EPSILON_1 : -Math::MACHINE_EPSILON_1;
199 float FixedRuler::Snap(float x, float bias) const
201 return floor(x / mSpacing + bias) * mSpacing;
204 float FixedRuler::GetPositionFromPage(unsigned int page, unsigned int &volume, bool wrap) const
206 float position = mDomain.min;
210 // spacing must be present.
213 unsigned int column = page;
215 // In carry mode, a volume (carry) is produced when page exceeds limit within domain
218 unsigned int pagesPerVolume = mDomain.GetSize() / mSpacing;
221 column += pagesPerVolume;
222 column %= pagesPerVolume;
223 volume = page/pagesPerVolume;
227 position = mDomain.min + column * mSpacing;
229 else // Domain (or Spacing) is not present, carry page to volume.
240 unsigned int FixedRuler::GetPageFromPosition(float position, bool wrap) const
242 unsigned int page = 0;
244 // spacing must be present.
249 position = WrapInDomain(position, mDomain.min, mDomain.max);
251 page = floor((position - mDomain.min) / mSpacing + 0.5f);
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)
260 DALI_LOG_ERROR("Ruler domain(%f) is smaller than its spacing(%f).", mDomain.GetSize() * 1.0, mSpacing * 1.0 );
262 page %= pagesPerVolume;
269 unsigned int FixedRuler::GetTotalPages() const
271 unsigned int pagesPerVolume = 1;
273 // spacing must be present.
276 pagesPerVolume = mDomain.GetSize() / mSpacing;
279 return pagesPerVolume;
282 ///////////////////////////////////////////////////////////////////////////////////////////////////
284 ///////////////////////////////////////////////////////////////////////////////////////////////////
286 const float ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f);
287 const float ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f);
288 const float ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION(0.5f);
289 const float ScrollView::DEFAULT_MAX_OVERSHOOT(100.0f); // 100 pixels
291 const float ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f);
292 const float ScrollView::DEFAULT_FRICTION_COEFFICIENT(1.0f);
293 const float ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f);
294 const float ScrollView::DEFAULT_MAX_FLICK_SPEED(3.0f);
296 ScrollView::ScrollView()
300 ScrollView::ScrollView(Internal::ScrollView& implementation)
301 : Scrollable(implementation)
305 ScrollView::ScrollView( Dali::Internal::CustomActor* internal )
306 : Scrollable( internal )
308 VerifyCustomActorPointer<Internal::ScrollView>(internal);
311 ScrollView::ScrollView( const ScrollView& handle )
312 : Scrollable( handle )
316 ScrollView& ScrollView::operator=( const ScrollView& handle )
318 if( &handle != this )
320 Control::operator=( handle );
325 ScrollView ScrollView::New()
327 return Internal::ScrollView::New();
330 ScrollView::~ScrollView()
334 ScrollView ScrollView::DownCast( BaseHandle handle )
336 return Control::DownCast<ScrollView, Internal::ScrollView>(handle);
339 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
341 return GetImpl(*this).GetScrollSnapAlphaFunction();
344 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
346 GetImpl(*this).SetScrollSnapAlphaFunction(alpha);
349 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
351 return GetImpl(*this).GetScrollFlickAlphaFunction();
354 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
356 GetImpl(*this).SetScrollFlickAlphaFunction(alpha);
359 float ScrollView::GetScrollSnapDuration() const
361 return GetImpl(*this).GetScrollSnapDuration();
364 void ScrollView::SetScrollSnapDuration(float time)
366 GetImpl(*this).SetScrollSnapDuration(time);
369 float ScrollView::GetScrollFlickDuration() const
371 return GetImpl(*this).GetScrollFlickDuration();
374 void ScrollView::SetScrollFlickDuration(float time)
376 GetImpl(*this).SetScrollFlickDuration(time);
379 void ScrollView::SetRulerX(RulerPtr ruler)
381 GetImpl(*this).SetRulerX(ruler);
384 void ScrollView::SetRulerY(RulerPtr ruler)
386 GetImpl(*this).SetRulerY(ruler);
389 void ScrollView::SetScrollSensitive(bool sensitive)
391 GetImpl(*this).SetScrollSensitive(sensitive);
394 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
396 GetImpl(*this).SetMaxOvershoot(overshootX, overshootY);
399 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
401 GetImpl(*this).SetSnapOvershootAlphaFunction(alpha);
404 void ScrollView::SetSnapOvershootDuration(float duration)
406 GetImpl(*this).SetSnapOvershootDuration(duration);
409 void ScrollView::SetActorAutoSnap(bool enable)
411 GetImpl(*this).SetActorAutoSnap(enable);
414 void ScrollView::SetWrapMode(bool enable)
416 GetImpl(*this).SetWrapMode(enable);
419 int ScrollView::GetScrollUpdateDistance() const
421 return GetImpl(*this).GetScrollUpdateDistance();
424 void ScrollView::SetScrollUpdateDistance(int distance)
426 GetImpl(*this).SetScrollUpdateDistance(distance);
429 bool ScrollView::GetAxisAutoLock() const
431 return GetImpl(*this).GetAxisAutoLock();
434 void ScrollView::SetAxisAutoLock(bool enable)
436 GetImpl(*this).SetAxisAutoLock(enable);
439 float ScrollView::GetAxisAutoLockGradient() const
441 return GetImpl(*this).GetAxisAutoLockGradient();
444 void ScrollView::SetAxisAutoLockGradient(float gradient)
446 GetImpl(*this).SetAxisAutoLockGradient(gradient);
449 float ScrollView::GetFrictionCoefficient() const
451 return GetImpl(*this).GetFrictionCoefficient();
454 void ScrollView::SetFrictionCoefficient(float friction)
456 GetImpl(*this).SetFrictionCoefficient(friction);
459 float ScrollView::GetFlickSpeedCoefficient() const
461 return GetImpl(*this).GetFlickSpeedCoefficient();
464 void ScrollView::SetFlickSpeedCoefficient(float speed)
466 GetImpl(*this).SetFlickSpeedCoefficient(speed);
469 Vector2 ScrollView::GetMinimumDistanceForFlick() const
471 return GetImpl(*this).GetMinimumDistanceForFlick();
474 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
476 GetImpl(*this).SetMinimumDistanceForFlick(distance);
479 float ScrollView::GetMinimumSpeedForFlick() const
481 return GetImpl(*this).GetMinimumSpeedForFlick();
484 void ScrollView::SetMinimumSpeedForFlick( float speed )
486 GetImpl(*this).SetMinimumSpeedForFlick(speed);
489 float ScrollView::GetMaxFlickSpeed() const
491 return GetImpl(*this).GetMaxFlickSpeed();
494 void ScrollView::SetMaxFlickSpeed(float speed)
496 GetImpl(*this).SetMaxFlickSpeed(speed);
499 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
501 return GetImpl(*this).GetMouseWheelScrollDistanceStep();
504 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
506 GetImpl(*this).SetMouseWheelScrollDistanceStep(step);
509 Vector3 ScrollView::GetCurrentScrollPosition() const
511 return GetImpl(*this).GetCurrentScrollPosition();
514 void ScrollView::SetScrollPosition(const Vector3& position)
516 GetImpl(*this).SetScrollPosition(position);
519 unsigned int ScrollView::GetCurrentPage() const
521 return GetImpl(*this).GetCurrentPage();
524 void ScrollView::ScrollTo(const Vector3 &position)
526 GetImpl(*this).ScrollTo(position);
529 void ScrollView::ScrollTo(const Vector3 &position, float duration)
531 GetImpl(*this).ScrollTo(position, duration);
534 void ScrollView::ScrollTo(const Vector3 &position, float duration, AlphaFunction alpha)
536 GetImpl(*this).ScrollTo(position, duration, alpha);
539 void ScrollView::ScrollTo(const Vector3 &position, float duration,
540 DirectionBias horizontalBias, DirectionBias verticalBias)
542 GetImpl(*this).ScrollTo(position, duration, horizontalBias, verticalBias);
545 void ScrollView::ScrollTo(const Vector3 &position, float duration, AlphaFunction alpha,
546 DirectionBias horizontalBias, DirectionBias verticalBias)
548 GetImpl(*this).ScrollTo(position, duration, alpha, horizontalBias, verticalBias);
551 void ScrollView::ScrollTo(unsigned int page)
553 GetImpl(*this).ScrollTo(page);
556 void ScrollView::ScrollTo(unsigned int page, float duration)
558 GetImpl(*this).ScrollTo(page, duration);
561 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
563 GetImpl(*this).ScrollTo(page, duration, bias);
566 void ScrollView::ScrollTo(Actor &actor)
568 GetImpl(*this).ScrollTo(actor);
571 void ScrollView::ScrollTo(Actor &actor, float duration)
573 GetImpl(*this).ScrollTo(actor, duration);
576 bool ScrollView::ScrollToSnapPoint()
578 return GetImpl(*this).ScrollToSnapPoint();
581 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
583 GetImpl(*this).ApplyConstraintToChildren(constraint);
586 void ScrollView::RemoveConstraintsFromChildren()
588 GetImpl(*this).RemoveConstraintsFromChildren();
591 void ScrollView::ApplyEffect(ScrollViewEffect effect)
593 GetImpl(*this).ApplyEffect(effect);
596 void ScrollView::RemoveEffect(ScrollViewEffect effect)
598 GetImpl(*this).RemoveEffect(effect);
601 void ScrollView::RemoveAllEffects()
603 GetImpl(*this).RemoveAllEffects();
606 void ScrollView::BindActor(Actor child)
608 GetImpl(*this).BindActor(child);
611 void ScrollView::UnbindActor(Actor child)
613 GetImpl(*this).UnbindActor(child);
616 ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
618 return GetImpl(*this).SnapStartedSignal();
621 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
623 GetImpl(*this).SetScrollingDirection( direction, threshold );
626 void ScrollView::RemoveScrollingDirection( Radian direction )
628 GetImpl(*this).RemoveScrollingDirection( direction );
631 } // namespace Toolkit