2 * Copyright (c) 2020 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).\n", 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 = std::max(static_cast<double>(0.0f), static_cast<double>(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).\n", 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 ScrollView::ScrollView()
290 ScrollView::ScrollView(Internal::ScrollView& implementation)
291 : Scrollable(implementation)
295 ScrollView::ScrollView( Dali::Internal::CustomActor* internal )
296 : Scrollable( internal )
298 VerifyCustomActorPointer<Internal::ScrollView>(internal);
301 ScrollView::ScrollView( const ScrollView& handle ) = default;
303 ScrollView::ScrollView( ScrollView&& rhs ) = default;
305 ScrollView& ScrollView::operator=( const ScrollView& handle ) = default;
307 ScrollView& ScrollView::operator=( ScrollView&& rhs ) = default;
309 ScrollView ScrollView::New()
311 return Internal::ScrollView::New();
314 ScrollView::~ScrollView()
318 ScrollView ScrollView::DownCast( BaseHandle handle )
320 return Control::DownCast<ScrollView, Internal::ScrollView>(handle);
323 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
325 return GetImpl(*this).GetScrollSnapAlphaFunction();
328 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
330 GetImpl(*this).SetScrollSnapAlphaFunction(alpha);
333 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
335 return GetImpl(*this).GetScrollFlickAlphaFunction();
338 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
340 GetImpl(*this).SetScrollFlickAlphaFunction(alpha);
343 float ScrollView::GetScrollSnapDuration() const
345 return GetImpl(*this).GetScrollSnapDuration();
348 void ScrollView::SetScrollSnapDuration(float time)
350 GetImpl(*this).SetScrollSnapDuration(time);
353 float ScrollView::GetScrollFlickDuration() const
355 return GetImpl(*this).GetScrollFlickDuration();
358 void ScrollView::SetScrollFlickDuration(float time)
360 GetImpl(*this).SetScrollFlickDuration(time);
363 void ScrollView::SetRulerX(RulerPtr ruler)
365 GetImpl(*this).SetRulerX(ruler);
368 void ScrollView::SetRulerY(RulerPtr ruler)
370 GetImpl(*this).SetRulerY(ruler);
373 void ScrollView::SetScrollSensitive(bool sensitive)
375 GetImpl(*this).SetScrollSensitive(sensitive);
378 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
380 GetImpl(*this).SetMaxOvershoot(overshootX, overshootY);
383 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
385 GetImpl(*this).SetSnapOvershootAlphaFunction(alpha);
388 void ScrollView::SetSnapOvershootDuration(float duration)
390 GetImpl(*this).SetSnapOvershootDuration(duration);
393 void ScrollView::SetActorAutoSnap(bool enable)
395 GetImpl(*this).SetActorAutoSnap(enable);
398 void ScrollView::SetWrapMode(bool enable)
400 GetImpl(*this).SetWrapMode(enable);
403 int ScrollView::GetScrollUpdateDistance() const
405 return GetImpl(*this).GetScrollUpdateDistance();
408 void ScrollView::SetScrollUpdateDistance(int distance)
410 GetImpl(*this).SetScrollUpdateDistance(distance);
413 bool ScrollView::GetAxisAutoLock() const
415 return GetImpl(*this).GetAxisAutoLock();
418 void ScrollView::SetAxisAutoLock(bool enable)
420 GetImpl(*this).SetAxisAutoLock(enable);
423 float ScrollView::GetAxisAutoLockGradient() const
425 return GetImpl(*this).GetAxisAutoLockGradient();
428 void ScrollView::SetAxisAutoLockGradient(float gradient)
430 GetImpl(*this).SetAxisAutoLockGradient(gradient);
433 float ScrollView::GetFrictionCoefficient() const
435 return GetImpl(*this).GetFrictionCoefficient();
438 void ScrollView::SetFrictionCoefficient(float friction)
440 GetImpl(*this).SetFrictionCoefficient(friction);
443 float ScrollView::GetFlickSpeedCoefficient() const
445 return GetImpl(*this).GetFlickSpeedCoefficient();
448 void ScrollView::SetFlickSpeedCoefficient(float speed)
450 GetImpl(*this).SetFlickSpeedCoefficient(speed);
453 Vector2 ScrollView::GetMinimumDistanceForFlick() const
455 return GetImpl(*this).GetMinimumDistanceForFlick();
458 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
460 GetImpl(*this).SetMinimumDistanceForFlick(distance);
463 float ScrollView::GetMinimumSpeedForFlick() const
465 return GetImpl(*this).GetMinimumSpeedForFlick();
468 void ScrollView::SetMinimumSpeedForFlick( float speed )
470 GetImpl(*this).SetMinimumSpeedForFlick(speed);
473 float ScrollView::GetMaxFlickSpeed() const
475 return GetImpl(*this).GetMaxFlickSpeed();
478 void ScrollView::SetMaxFlickSpeed(float speed)
480 GetImpl(*this).SetMaxFlickSpeed(speed);
483 Vector2 ScrollView::GetWheelScrollDistanceStep() const
485 return GetImpl(*this).GetWheelScrollDistanceStep();
488 void ScrollView::SetWheelScrollDistanceStep(Vector2 step)
490 GetImpl(*this).SetWheelScrollDistanceStep(step);
493 Vector2 ScrollView::GetCurrentScrollPosition() const
495 return GetImpl(*this).GetCurrentScrollPosition();
498 unsigned int ScrollView::GetCurrentPage() const
500 return GetImpl(*this).GetCurrentPage();
503 void ScrollView::ScrollTo(const Vector2 &position)
505 GetImpl(*this).ScrollTo(position);
508 void ScrollView::ScrollTo(const Vector2 &position, float duration)
510 GetImpl(*this).ScrollTo(position, duration);
513 void ScrollView::ScrollTo(const Vector2 &position, float duration, AlphaFunction alpha)
515 GetImpl(*this).ScrollTo(position, duration, alpha);
518 void ScrollView::ScrollTo(const Vector2 &position, float duration,
519 DirectionBias horizontalBias, DirectionBias verticalBias)
521 GetImpl(*this).ScrollTo(position, duration, horizontalBias, verticalBias);
524 void ScrollView::ScrollTo(const Vector2 &position, float duration, AlphaFunction alpha,
525 DirectionBias horizontalBias, DirectionBias verticalBias)
527 GetImpl(*this).ScrollTo(position, duration, alpha, horizontalBias, verticalBias);
530 void ScrollView::ScrollTo(unsigned int page)
532 GetImpl(*this).ScrollTo(page);
535 void ScrollView::ScrollTo(unsigned int page, float duration)
537 GetImpl(*this).ScrollTo(page, duration);
540 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
542 GetImpl(*this).ScrollTo(page, duration, bias);
545 void ScrollView::ScrollTo(Actor &actor)
547 GetImpl(*this).ScrollTo(actor);
550 void ScrollView::ScrollTo(Actor &actor, float duration)
552 GetImpl(*this).ScrollTo(actor, duration);
555 bool ScrollView::ScrollToSnapPoint()
557 return GetImpl(*this).ScrollToSnapPoint();
560 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
562 GetImpl(*this).ApplyConstraintToChildren(constraint);
565 void ScrollView::RemoveConstraintsFromChildren()
567 GetImpl(*this).RemoveConstraintsFromChildren();
570 void ScrollView::ApplyEffect(ScrollViewEffect effect)
572 GetImpl(*this).ApplyEffect(effect);
575 void ScrollView::RemoveEffect(ScrollViewEffect effect)
577 GetImpl(*this).RemoveEffect(effect);
580 void ScrollView::RemoveAllEffects()
582 GetImpl(*this).RemoveAllEffects();
585 void ScrollView::BindActor(Actor child)
587 GetImpl(*this).BindActor(child);
590 void ScrollView::UnbindActor(Actor child)
592 GetImpl(*this).UnbindActor(child);
595 ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
597 return GetImpl(*this).SnapStartedSignal();
600 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
602 GetImpl(*this).SetScrollingDirection( direction, threshold );
605 void ScrollView::RemoveScrollingDirection( Radian direction )
607 GetImpl(*this).RemoveScrollingDirection( direction );
610 } // namespace Toolkit