2 * Copyright (c) 2015 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 = std::max(static_cast<double>(0.0f), 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 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 )
302 : Scrollable( handle )
306 ScrollView& ScrollView::operator=( const ScrollView& handle )
308 if( &handle != this )
310 Control::operator=( handle );
315 ScrollView ScrollView::New()
317 return Internal::ScrollView::New();
320 ScrollView::~ScrollView()
324 ScrollView ScrollView::DownCast( BaseHandle handle )
326 return Control::DownCast<ScrollView, Internal::ScrollView>(handle);
329 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
331 return GetImpl(*this).GetScrollSnapAlphaFunction();
334 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
336 GetImpl(*this).SetScrollSnapAlphaFunction(alpha);
339 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
341 return GetImpl(*this).GetScrollFlickAlphaFunction();
344 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
346 GetImpl(*this).SetScrollFlickAlphaFunction(alpha);
349 float ScrollView::GetScrollSnapDuration() const
351 return GetImpl(*this).GetScrollSnapDuration();
354 void ScrollView::SetScrollSnapDuration(float time)
356 GetImpl(*this).SetScrollSnapDuration(time);
359 float ScrollView::GetScrollFlickDuration() const
361 return GetImpl(*this).GetScrollFlickDuration();
364 void ScrollView::SetScrollFlickDuration(float time)
366 GetImpl(*this).SetScrollFlickDuration(time);
369 void ScrollView::SetRulerX(RulerPtr ruler)
371 GetImpl(*this).SetRulerX(ruler);
374 void ScrollView::SetRulerY(RulerPtr ruler)
376 GetImpl(*this).SetRulerY(ruler);
379 void ScrollView::SetScrollSensitive(bool sensitive)
381 GetImpl(*this).SetScrollSensitive(sensitive);
384 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
386 GetImpl(*this).SetMaxOvershoot(overshootX, overshootY);
389 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
391 GetImpl(*this).SetSnapOvershootAlphaFunction(alpha);
394 void ScrollView::SetSnapOvershootDuration(float duration)
396 GetImpl(*this).SetSnapOvershootDuration(duration);
399 void ScrollView::SetActorAutoSnap(bool enable)
401 GetImpl(*this).SetActorAutoSnap(enable);
404 void ScrollView::SetWrapMode(bool enable)
406 GetImpl(*this).SetWrapMode(enable);
409 int ScrollView::GetScrollUpdateDistance() const
411 return GetImpl(*this).GetScrollUpdateDistance();
414 void ScrollView::SetScrollUpdateDistance(int distance)
416 GetImpl(*this).SetScrollUpdateDistance(distance);
419 bool ScrollView::GetAxisAutoLock() const
421 return GetImpl(*this).GetAxisAutoLock();
424 void ScrollView::SetAxisAutoLock(bool enable)
426 GetImpl(*this).SetAxisAutoLock(enable);
429 float ScrollView::GetAxisAutoLockGradient() const
431 return GetImpl(*this).GetAxisAutoLockGradient();
434 void ScrollView::SetAxisAutoLockGradient(float gradient)
436 GetImpl(*this).SetAxisAutoLockGradient(gradient);
439 float ScrollView::GetFrictionCoefficient() const
441 return GetImpl(*this).GetFrictionCoefficient();
444 void ScrollView::SetFrictionCoefficient(float friction)
446 GetImpl(*this).SetFrictionCoefficient(friction);
449 float ScrollView::GetFlickSpeedCoefficient() const
451 return GetImpl(*this).GetFlickSpeedCoefficient();
454 void ScrollView::SetFlickSpeedCoefficient(float speed)
456 GetImpl(*this).SetFlickSpeedCoefficient(speed);
459 Vector2 ScrollView::GetMinimumDistanceForFlick() const
461 return GetImpl(*this).GetMinimumDistanceForFlick();
464 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
466 GetImpl(*this).SetMinimumDistanceForFlick(distance);
469 float ScrollView::GetMinimumSpeedForFlick() const
471 return GetImpl(*this).GetMinimumSpeedForFlick();
474 void ScrollView::SetMinimumSpeedForFlick( float speed )
476 GetImpl(*this).SetMinimumSpeedForFlick(speed);
479 float ScrollView::GetMaxFlickSpeed() const
481 return GetImpl(*this).GetMaxFlickSpeed();
484 void ScrollView::SetMaxFlickSpeed(float speed)
486 GetImpl(*this).SetMaxFlickSpeed(speed);
489 Vector2 ScrollView::GetWheelScrollDistanceStep() const
491 return GetImpl(*this).GetWheelScrollDistanceStep();
494 void ScrollView::SetWheelScrollDistanceStep(Vector2 step)
496 GetImpl(*this).SetWheelScrollDistanceStep(step);
499 Vector2 ScrollView::GetCurrentScrollPosition() const
501 return GetImpl(*this).GetCurrentScrollPosition();
504 unsigned int ScrollView::GetCurrentPage() const
506 return GetImpl(*this).GetCurrentPage();
509 void ScrollView::ScrollTo(const Vector2 &position)
511 GetImpl(*this).ScrollTo(position);
514 void ScrollView::ScrollTo(const Vector2 &position, float duration)
516 GetImpl(*this).ScrollTo(position, duration);
519 void ScrollView::ScrollTo(const Vector2 &position, float duration, AlphaFunction alpha)
521 GetImpl(*this).ScrollTo(position, duration, alpha);
524 void ScrollView::ScrollTo(const Vector2 &position, float duration,
525 DirectionBias horizontalBias, DirectionBias verticalBias)
527 GetImpl(*this).ScrollTo(position, duration, horizontalBias, verticalBias);
530 void ScrollView::ScrollTo(const Vector2 &position, float duration, AlphaFunction alpha,
531 DirectionBias horizontalBias, DirectionBias verticalBias)
533 GetImpl(*this).ScrollTo(position, duration, alpha, horizontalBias, verticalBias);
536 void ScrollView::ScrollTo(unsigned int page)
538 GetImpl(*this).ScrollTo(page);
541 void ScrollView::ScrollTo(unsigned int page, float duration)
543 GetImpl(*this).ScrollTo(page, duration);
546 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
548 GetImpl(*this).ScrollTo(page, duration, bias);
551 void ScrollView::ScrollTo(Actor &actor)
553 GetImpl(*this).ScrollTo(actor);
556 void ScrollView::ScrollTo(Actor &actor, float duration)
558 GetImpl(*this).ScrollTo(actor, duration);
561 bool ScrollView::ScrollToSnapPoint()
563 return GetImpl(*this).ScrollToSnapPoint();
566 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
568 GetImpl(*this).ApplyConstraintToChildren(constraint);
571 void ScrollView::RemoveConstraintsFromChildren()
573 GetImpl(*this).RemoveConstraintsFromChildren();
576 void ScrollView::ApplyEffect(ScrollViewEffect effect)
578 GetImpl(*this).ApplyEffect(effect);
581 void ScrollView::RemoveEffect(ScrollViewEffect effect)
583 GetImpl(*this).RemoveEffect(effect);
586 void ScrollView::RemoveAllEffects()
588 GetImpl(*this).RemoveAllEffects();
591 void ScrollView::BindActor(Actor child)
593 GetImpl(*this).BindActor(child);
596 void ScrollView::UnbindActor(Actor child)
598 GetImpl(*this).UnbindActor(child);
601 ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
603 return GetImpl(*this).SnapStartedSignal();
606 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
608 GetImpl(*this).SetScrollingDirection( direction, threshold );
611 void ScrollView::RemoveScrollingDirection( Radian direction )
613 GetImpl(*this).RemoveScrollingDirection( direction );
616 } // namespace Toolkit