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/internal/controls/scrollable/scroll-view/scroll-view-impl.h>
26 #include <dali-toolkit/public-api/controls/scrollable/scrollable.h>
34 ///////////////////////////////////////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////////////////////////////
38 RulerDomain::RulerDomain(float min, float max, bool enabled)
45 float RulerDomain::Clamp(float x, float length, float scale) const
49 return Clamp(x, length, scale, clamped);
52 float RulerDomain::Clamp(float x, float length, float scale, ClampState& clamped) const
56 clamped = NOT_CLAMPED;
60 const float minExtent = min * scale;
61 const float maxExtent = max * scale - length;
64 clamped = CLAMPED_TO_MIN;
67 else if(x > maxExtent)
69 clamped = CLAMPED_TO_MAX;
73 clamped = NOT_CLAMPED;
77 float RulerDomain::GetSize() const
82 ///////////////////////////////////////////////////////////////////////////////////////////////////
84 ///////////////////////////////////////////////////////////////////////////////////////////////////
89 mDomain(RulerDomain(0.0f, 1.0f, false))
97 Ruler::RulerType Ruler::GetType() const
102 bool Ruler::IsEnabled() const
112 void Ruler::Disable()
117 void Ruler::SetDomain(RulerDomain domain)
122 const RulerDomain& Ruler::GetDomain() const
127 void Ruler::DisableDomain()
129 mDomain = RulerDomain(0.0f, 1.0f, false);
132 float Ruler::Clamp(float x, float length, float scale) const
134 return mDomain.Clamp(x, length, scale);
137 float Ruler::Clamp(float x, float length, float scale, ClampState& clamped) const
139 return mDomain.Clamp(x, length, scale, clamped);
142 float Ruler::SnapAndClamp(float x, float bias, float length, float scale) const
144 return Clamp(Snap(x, bias), length, scale);
147 float Ruler::SnapAndClamp(float x, float bias, float length, float scale, ClampState& clamped) const
149 return Clamp(Snap(x, bias), length, scale, clamped);
152 ///////////////////////////////////////////////////////////////////////////////////////////////////
154 ///////////////////////////////////////////////////////////////////////////////////////////////////
156 DefaultRuler::DefaultRuler()
161 float DefaultRuler::Snap(float x, float bias) const
166 float DefaultRuler::GetPositionFromPage(unsigned int page, unsigned int& volume, bool wrap) const
172 unsigned int DefaultRuler::GetPageFromPosition(float position, bool wrap) const
177 unsigned int DefaultRuler::GetTotalPages() const
182 ///////////////////////////////////////////////////////////////////////////////////////////////////
184 ///////////////////////////////////////////////////////////////////////////////////////////////////
186 FixedRuler::FixedRuler(float spacing)
189 if(fabsf(mSpacing) <= Math::MACHINE_EPSILON_1)
191 DALI_LOG_ERROR("Page spacing too small (%f).\n", double(spacing));
192 mSpacing = spacing >= 0.0f ? Math::MACHINE_EPSILON_1 : -Math::MACHINE_EPSILON_1;
197 float FixedRuler::Snap(float x, float bias) const
199 return floor(x / mSpacing + bias) * mSpacing;
202 float FixedRuler::GetPositionFromPage(unsigned int page, unsigned int& volume, bool wrap) const
204 float position = mDomain.min;
208 // spacing must be present.
211 unsigned int column = page;
213 // In carry mode, a volume (carry) is produced when page exceeds limit within domain
216 unsigned int pagesPerVolume = mDomain.GetSize() / mSpacing;
217 if(pagesPerVolume > 0)
219 column += pagesPerVolume;
220 column %= pagesPerVolume;
221 volume = page / pagesPerVolume;
225 position = mDomain.min + column * mSpacing;
227 else // Domain (or Spacing) is not present, carry page to volume.
238 unsigned int FixedRuler::GetPageFromPosition(float position, bool wrap) const
240 unsigned int page = 0;
242 // spacing must be present.
247 position = WrapInDomain(position, mDomain.min, mDomain.max);
249 page = std::max(static_cast<double>(0.0f), static_cast<double>(floor((position - mDomain.min) / mSpacing + 0.5f)));
253 unsigned int pagesPerVolume = mDomain.GetSize() / mSpacing;
254 // 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):
255 if(pagesPerVolume < 1u)
258 DALI_LOG_ERROR("Ruler domain(%f) is smaller than its spacing(%f).\n", mDomain.GetSize() * 1.0, mSpacing * 1.0);
260 page %= pagesPerVolume;
267 unsigned int FixedRuler::GetTotalPages() const
269 unsigned int pagesPerVolume = 1;
271 // spacing must be present.
274 pagesPerVolume = mDomain.GetSize() / mSpacing;
277 return pagesPerVolume;
280 ///////////////////////////////////////////////////////////////////////////////////////////////////
282 ///////////////////////////////////////////////////////////////////////////////////////////////////
284 ScrollView::ScrollView()
288 ScrollView::ScrollView(Internal::ScrollView& implementation)
289 : Scrollable(implementation)
293 ScrollView::ScrollView(Dali::Internal::CustomActor* internal)
294 : Scrollable(internal)
296 VerifyCustomActorPointer<Internal::ScrollView>(internal);
299 ScrollView::ScrollView(const ScrollView& handle) = default;
301 ScrollView::ScrollView(ScrollView&& rhs) = default;
303 ScrollView& ScrollView::operator=(const ScrollView& handle) = default;
305 ScrollView& ScrollView::operator=(ScrollView&& rhs) = default;
307 ScrollView ScrollView::New()
309 return Internal::ScrollView::New();
312 ScrollView::~ScrollView()
316 ScrollView ScrollView::DownCast(BaseHandle handle)
318 return Control::DownCast<ScrollView, Internal::ScrollView>(handle);
321 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
323 return GetImpl(*this).GetScrollSnapAlphaFunction();
326 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
328 GetImpl(*this).SetScrollSnapAlphaFunction(alpha);
331 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
333 return GetImpl(*this).GetScrollFlickAlphaFunction();
336 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
338 GetImpl(*this).SetScrollFlickAlphaFunction(alpha);
341 float ScrollView::GetScrollSnapDuration() const
343 return GetImpl(*this).GetScrollSnapDuration();
346 void ScrollView::SetScrollSnapDuration(float time)
348 GetImpl(*this).SetScrollSnapDuration(time);
351 float ScrollView::GetScrollFlickDuration() const
353 return GetImpl(*this).GetScrollFlickDuration();
356 void ScrollView::SetScrollFlickDuration(float time)
358 GetImpl(*this).SetScrollFlickDuration(time);
361 void ScrollView::SetRulerX(RulerPtr ruler)
363 GetImpl(*this).SetRulerX(ruler);
366 void ScrollView::SetRulerY(RulerPtr ruler)
368 GetImpl(*this).SetRulerY(ruler);
371 void ScrollView::SetScrollSensitive(bool sensitive)
373 GetImpl(*this).SetScrollSensitive(sensitive);
376 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
378 GetImpl(*this).SetMaxOvershoot(overshootX, overshootY);
381 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
383 GetImpl(*this).SetSnapOvershootAlphaFunction(alpha);
386 void ScrollView::SetSnapOvershootDuration(float duration)
388 GetImpl(*this).SetSnapOvershootDuration(duration);
391 void ScrollView::SetActorAutoSnap(bool enable)
393 GetImpl(*this).SetActorAutoSnap(enable);
396 void ScrollView::SetWrapMode(bool enable)
398 GetImpl(*this).SetWrapMode(enable);
401 int ScrollView::GetScrollUpdateDistance() const
403 return GetImpl(*this).GetScrollUpdateDistance();
406 void ScrollView::SetScrollUpdateDistance(int distance)
408 GetImpl(*this).SetScrollUpdateDistance(distance);
411 bool ScrollView::GetAxisAutoLock() const
413 return GetImpl(*this).GetAxisAutoLock();
416 void ScrollView::SetAxisAutoLock(bool enable)
418 GetImpl(*this).SetAxisAutoLock(enable);
421 float ScrollView::GetAxisAutoLockGradient() const
423 return GetImpl(*this).GetAxisAutoLockGradient();
426 void ScrollView::SetAxisAutoLockGradient(float gradient)
428 GetImpl(*this).SetAxisAutoLockGradient(gradient);
431 float ScrollView::GetFrictionCoefficient() const
433 return GetImpl(*this).GetFrictionCoefficient();
436 void ScrollView::SetFrictionCoefficient(float friction)
438 GetImpl(*this).SetFrictionCoefficient(friction);
441 float ScrollView::GetFlickSpeedCoefficient() const
443 return GetImpl(*this).GetFlickSpeedCoefficient();
446 void ScrollView::SetFlickSpeedCoefficient(float speed)
448 GetImpl(*this).SetFlickSpeedCoefficient(speed);
451 Vector2 ScrollView::GetMinimumDistanceForFlick() const
453 return GetImpl(*this).GetMinimumDistanceForFlick();
456 void ScrollView::SetMinimumDistanceForFlick(const Vector2& distance)
458 GetImpl(*this).SetMinimumDistanceForFlick(distance);
461 float ScrollView::GetMinimumSpeedForFlick() const
463 return GetImpl(*this).GetMinimumSpeedForFlick();
466 void ScrollView::SetMinimumSpeedForFlick(float speed)
468 GetImpl(*this).SetMinimumSpeedForFlick(speed);
471 float ScrollView::GetMaxFlickSpeed() const
473 return GetImpl(*this).GetMaxFlickSpeed();
476 void ScrollView::SetMaxFlickSpeed(float speed)
478 GetImpl(*this).SetMaxFlickSpeed(speed);
481 Vector2 ScrollView::GetWheelScrollDistanceStep() const
483 return GetImpl(*this).GetWheelScrollDistanceStep();
486 void ScrollView::SetWheelScrollDistanceStep(Vector2 step)
488 GetImpl(*this).SetWheelScrollDistanceStep(step);
491 Vector2 ScrollView::GetCurrentScrollPosition() const
493 return GetImpl(*this).GetCurrentScrollPosition();
496 unsigned int ScrollView::GetCurrentPage() const
498 return GetImpl(*this).GetCurrentPage();
501 void ScrollView::ScrollTo(const Vector2& position)
503 GetImpl(*this).ScrollTo(position);
506 void ScrollView::ScrollTo(const Vector2& position, float duration)
508 GetImpl(*this).ScrollTo(position, duration);
511 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha)
513 GetImpl(*this).ScrollTo(position, duration, alpha);
516 void ScrollView::ScrollTo(const Vector2& position, float duration, DirectionBias horizontalBias, DirectionBias verticalBias)
518 GetImpl(*this).ScrollTo(position, duration, horizontalBias, verticalBias);
521 void ScrollView::ScrollTo(const Vector2& position, float duration, AlphaFunction alpha, DirectionBias horizontalBias, DirectionBias verticalBias)
523 GetImpl(*this).ScrollTo(position, duration, alpha, horizontalBias, verticalBias);
526 void ScrollView::ScrollTo(unsigned int page)
528 GetImpl(*this).ScrollTo(page);
531 void ScrollView::ScrollTo(unsigned int page, float duration)
533 GetImpl(*this).ScrollTo(page, duration);
536 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
538 GetImpl(*this).ScrollTo(page, duration, bias);
541 void ScrollView::ScrollTo(Actor& actor)
543 GetImpl(*this).ScrollTo(actor);
546 void ScrollView::ScrollTo(Actor& actor, float duration)
548 GetImpl(*this).ScrollTo(actor, duration);
551 bool ScrollView::ScrollToSnapPoint()
553 return GetImpl(*this).ScrollToSnapPoint();
556 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
558 GetImpl(*this).ApplyConstraintToChildren(constraint);
561 void ScrollView::RemoveConstraintsFromChildren()
563 GetImpl(*this).RemoveConstraintsFromChildren();
566 void ScrollView::ApplyEffect(ScrollViewEffect effect)
568 GetImpl(*this).ApplyEffect(effect);
571 void ScrollView::RemoveEffect(ScrollViewEffect effect)
573 GetImpl(*this).RemoveEffect(effect);
576 void ScrollView::RemoveAllEffects()
578 GetImpl(*this).RemoveAllEffects();
581 void ScrollView::BindActor(Actor child)
583 GetImpl(*this).BindActor(child);
586 void ScrollView::UnbindActor(Actor child)
588 GetImpl(*this).UnbindActor(child);
591 ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
593 return GetImpl(*this).SnapStartedSignal();
596 void ScrollView::SetScrollingDirection(Radian direction, Radian threshold)
598 GetImpl(*this).SetScrollingDirection(direction, threshold);
601 void ScrollView::RemoveScrollingDirection(Radian direction)
603 GetImpl(*this).RemoveScrollingDirection(direction);
606 } // namespace Toolkit