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 std::string ScrollView::SCROLL_TIME_PROPERTY_NAME( "scroll-time" );
287 const std::string ScrollView::SCROLL_POSITION_PROPERTY_NAME( "scroll-position" );
288 const std::string ScrollView::SCROLL_PRE_POSITION_PROPERTY_NAME( "scroll-pre-position" );
289 const std::string ScrollView::SCROLL_OVERSHOOT_X_PROPERTY_NAME( "scroll-overshoot-x" );
290 const std::string ScrollView::SCROLL_OVERSHOOT_Y_PROPERTY_NAME( "scroll-overshoot-y" );
291 const std::string ScrollView::SCROLL_FINAL_PROPERTY_NAME( "scroll-final" );
292 const std::string ScrollView::SCROLL_WRAP_PROPERTY_NAME( "scroll-wrap" );
293 const std::string ScrollView::SCROLL_PANNING_PROPERTY_NAME( "scroll-panning" );
294 const std::string ScrollView::SCROLL_SCROLLING_PROPERTY_NAME( "scroll-scrolling" );
295 const std::string ScrollView::SCROLL_POSITION_DELTA_PROPERTY_NAME( "scroll-position-delta" );
296 const std::string ScrollView::SCROLL_START_PAGE_POSITION_PROPERTY_NAME( "scroll-start-page-position" );
298 const float ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f);
299 const float ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f);
300 const float ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION(0.5f);
301 const float ScrollView::DEFAULT_MAX_OVERSHOOT(100.0f); // 100 pixels
303 const float ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f);
304 const float ScrollView::DEFAULT_FRICTION_COEFFICIENT(1.0f);
305 const float ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f);
306 const float ScrollView::DEFAULT_MAX_FLICK_SPEED(3.0f);
308 ScrollView::ScrollView()
312 ScrollView::ScrollView(Internal::ScrollView& implementation)
313 : Scrollable(implementation)
317 ScrollView::ScrollView( Dali::Internal::CustomActor* internal )
318 : Scrollable( internal )
320 VerifyCustomActorPointer<Internal::ScrollView>(internal);
323 ScrollView::ScrollView( const ScrollView& handle )
324 : Scrollable( handle )
328 ScrollView& ScrollView::operator=( const ScrollView& handle )
330 if( &handle != this )
332 Control::operator=( handle );
337 ScrollView ScrollView::New()
339 return Internal::ScrollView::New();
342 ScrollView::~ScrollView()
346 ScrollView ScrollView::DownCast( BaseHandle handle )
348 return Control::DownCast<ScrollView, Internal::ScrollView>(handle);
351 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
353 return GetImpl(*this).GetScrollSnapAlphaFunction();
356 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
358 GetImpl(*this).SetScrollSnapAlphaFunction(alpha);
361 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
363 return GetImpl(*this).GetScrollFlickAlphaFunction();
366 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
368 GetImpl(*this).SetScrollFlickAlphaFunction(alpha);
371 float ScrollView::GetScrollSnapDuration() const
373 return GetImpl(*this).GetScrollSnapDuration();
376 void ScrollView::SetScrollSnapDuration(float time)
378 GetImpl(*this).SetScrollSnapDuration(time);
381 float ScrollView::GetScrollFlickDuration() const
383 return GetImpl(*this).GetScrollFlickDuration();
386 void ScrollView::SetScrollFlickDuration(float time)
388 GetImpl(*this).SetScrollFlickDuration(time);
391 void ScrollView::SetRulerX(RulerPtr ruler)
393 GetImpl(*this).SetRulerX(ruler);
396 void ScrollView::SetRulerY(RulerPtr ruler)
398 GetImpl(*this).SetRulerY(ruler);
401 void ScrollView::SetScrollSensitive(bool sensitive)
403 GetImpl(*this).SetScrollSensitive(sensitive);
406 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
408 GetImpl(*this).SetMaxOvershoot(overshootX, overshootY);
411 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
413 GetImpl(*this).SetSnapOvershootAlphaFunction(alpha);
416 void ScrollView::SetSnapOvershootDuration(float duration)
418 GetImpl(*this).SetSnapOvershootDuration(duration);
421 void ScrollView::SetActorAutoSnap(bool enable)
423 GetImpl(*this).SetActorAutoSnap(enable);
426 void ScrollView::SetWrapMode(bool enable)
428 GetImpl(*this).SetWrapMode(enable);
431 int ScrollView::GetScrollUpdateDistance() const
433 return GetImpl(*this).GetScrollUpdateDistance();
436 void ScrollView::SetScrollUpdateDistance(int distance)
438 GetImpl(*this).SetScrollUpdateDistance(distance);
441 bool ScrollView::GetAxisAutoLock() const
443 return GetImpl(*this).GetAxisAutoLock();
446 void ScrollView::SetAxisAutoLock(bool enable)
448 GetImpl(*this).SetAxisAutoLock(enable);
451 float ScrollView::GetAxisAutoLockGradient() const
453 return GetImpl(*this).GetAxisAutoLockGradient();
456 void ScrollView::SetAxisAutoLockGradient(float gradient)
458 GetImpl(*this).SetAxisAutoLockGradient(gradient);
461 float ScrollView::GetFrictionCoefficient() const
463 return GetImpl(*this).GetFrictionCoefficient();
466 void ScrollView::SetFrictionCoefficient(float friction)
468 GetImpl(*this).SetFrictionCoefficient(friction);
471 float ScrollView::GetFlickSpeedCoefficient() const
473 return GetImpl(*this).GetFlickSpeedCoefficient();
476 void ScrollView::SetFlickSpeedCoefficient(float speed)
478 GetImpl(*this).SetFlickSpeedCoefficient(speed);
481 Vector2 ScrollView::GetMinimumDistanceForFlick() const
483 return GetImpl(*this).GetMinimumDistanceForFlick();
486 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
488 GetImpl(*this).SetMinimumDistanceForFlick(distance);
491 float ScrollView::GetMinimumSpeedForFlick() const
493 return GetImpl(*this).GetMinimumSpeedForFlick();
496 void ScrollView::SetMinimumSpeedForFlick( float speed )
498 GetImpl(*this).SetMinimumSpeedForFlick(speed);
501 float ScrollView::GetMaxFlickSpeed() const
503 return GetImpl(*this).GetMaxFlickSpeed();
506 void ScrollView::SetMaxFlickSpeed(float speed)
508 GetImpl(*this).SetMaxFlickSpeed(speed);
511 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
513 return GetImpl(*this).GetMouseWheelScrollDistanceStep();
516 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
518 GetImpl(*this).SetMouseWheelScrollDistanceStep(step);
521 Vector3 ScrollView::GetCurrentScrollPosition() const
523 return GetImpl(*this).GetCurrentScrollPosition();
526 void ScrollView::SetScrollPosition(const Vector3& position)
528 GetImpl(*this).SetScrollPosition(position);
531 unsigned int ScrollView::GetCurrentPage() const
533 return GetImpl(*this).GetCurrentPage();
536 void ScrollView::ScrollTo(const Vector3 &position)
538 GetImpl(*this).ScrollTo(position);
541 void ScrollView::ScrollTo(const Vector3 &position, float duration)
543 GetImpl(*this).ScrollTo(position, duration);
546 void ScrollView::ScrollTo(const Vector3 &position, float duration, AlphaFunction alpha)
548 GetImpl(*this).ScrollTo(position, duration, alpha);
551 void ScrollView::ScrollTo(const Vector3 &position, float duration,
552 DirectionBias horizontalBias, DirectionBias verticalBias)
554 GetImpl(*this).ScrollTo(position, duration, horizontalBias, verticalBias);
557 void ScrollView::ScrollTo(const Vector3 &position, float duration, AlphaFunction alpha,
558 DirectionBias horizontalBias, DirectionBias verticalBias)
560 GetImpl(*this).ScrollTo(position, duration, alpha, horizontalBias, verticalBias);
563 void ScrollView::ScrollTo(unsigned int page)
565 GetImpl(*this).ScrollTo(page);
568 void ScrollView::ScrollTo(unsigned int page, float duration)
570 GetImpl(*this).ScrollTo(page, duration);
573 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
575 GetImpl(*this).ScrollTo(page, duration, bias);
578 void ScrollView::ScrollTo(Actor &actor)
580 GetImpl(*this).ScrollTo(actor);
583 void ScrollView::ScrollTo(Actor &actor, float duration)
585 GetImpl(*this).ScrollTo(actor, duration);
588 bool ScrollView::ScrollToSnapPoint()
590 return GetImpl(*this).ScrollToSnapPoint();
593 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
595 GetImpl(*this).ApplyConstraintToChildren(constraint);
598 void ScrollView::RemoveConstraintsFromChildren()
600 GetImpl(*this).RemoveConstraintsFromChildren();
603 void ScrollView::ApplyEffect(ScrollViewEffect effect)
605 GetImpl(*this).ApplyEffect(effect);
608 void ScrollView::RemoveEffect(ScrollViewEffect effect)
610 GetImpl(*this).RemoveEffect(effect);
613 void ScrollView::RemoveAllEffects()
615 GetImpl(*this).RemoveAllEffects();
618 void ScrollView::BindActor(Actor child)
620 GetImpl(*this).BindActor(child);
623 void ScrollView::UnbindActor(Actor child)
625 GetImpl(*this).UnbindActor(child);
628 ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
630 return GetImpl(*this).SnapStartedSignal();
633 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
635 GetImpl(*this).SetScrollingDirection( direction, threshold );
638 void ScrollView::RemoveScrollingDirection( Radian direction )
640 GetImpl(*this).RemoveScrollingDirection( direction );
643 } // namespace Toolkit