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_PAGE_CURRENT( "scroll-page-current" );
287 const std::string ScrollView::SCROLL_TIME_PROPERTY_NAME( "scroll-time" );
288 const std::string ScrollView::SCROLL_POSITION_PROPERTY_NAME( "scroll-position" );
289 const std::string ScrollView::SCROLL_PRE_POSITION_PROPERTY_NAME( "scroll-pre-position" );
290 const std::string ScrollView::SCROLL_OVERSHOOT_X_PROPERTY_NAME( "scroll-overshoot-x" );
291 const std::string ScrollView::SCROLL_OVERSHOOT_Y_PROPERTY_NAME( "scroll-overshoot-y" );
292 const std::string ScrollView::SCROLL_FINAL_PROPERTY_NAME( "scroll-final" );
293 const std::string ScrollView::SCROLL_SCALE_PROPERTY_NAME( "scroll-scale" );
294 const std::string ScrollView::SCROLL_WRAP_PROPERTY_NAME( "scroll-wrap" );
295 const std::string ScrollView::SCROLL_PANNING_PROPERTY_NAME( "scroll-panning" );
296 const std::string ScrollView::SCROLL_SCROLLING_PROPERTY_NAME( "scroll-scrolling" );
297 const std::string ScrollView::SCROLL_POSITION_DELTA_PROPERTY_NAME( "scroll-position-delta" );
298 const std::string ScrollView::SCROLL_START_PAGE_POSITION_PROPERTY_NAME( "scroll-start-page-position" );
300 const float ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f);
301 const float ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f);
302 const float ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION(0.5f);
303 const float ScrollView::DEFAULT_MAX_OVERSHOOT(100.0f); // 100 pixels
305 const float ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f);
306 const float ScrollView::DEFAULT_FRICTION_COEFFICIENT(1.0f);
307 const float ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f);
308 const float ScrollView::DEFAULT_MAX_FLICK_SPEED(3.0f);
310 const char* const ScrollView::SIGNAL_SNAP_STARTED = "snap-started";
312 ScrollView::ScrollView()
316 ScrollView::ScrollView(Internal::ScrollView& implementation)
317 : Scrollable(implementation)
321 ScrollView::ScrollView( Dali::Internal::CustomActor* internal )
322 : Scrollable( internal )
324 VerifyCustomActorPointer<Internal::ScrollView>(internal);
327 ScrollView::ScrollView( const ScrollView& handle )
328 : Scrollable( handle )
332 ScrollView& ScrollView::operator=( const ScrollView& handle )
334 if( &handle != this )
336 Control::operator=( handle );
341 ScrollView ScrollView::New()
343 return Internal::ScrollView::New();
346 ScrollView::~ScrollView()
350 ScrollView ScrollView::DownCast( BaseHandle handle )
352 return Control::DownCast<ScrollView, Internal::ScrollView>(handle);
355 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
357 return GetImpl(*this).GetScrollSnapAlphaFunction();
360 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
362 GetImpl(*this).SetScrollSnapAlphaFunction(alpha);
365 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
367 return GetImpl(*this).GetScrollFlickAlphaFunction();
370 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
372 GetImpl(*this).SetScrollFlickAlphaFunction(alpha);
375 float ScrollView::GetScrollSnapDuration() const
377 return GetImpl(*this).GetScrollSnapDuration();
380 void ScrollView::SetScrollSnapDuration(float time)
382 GetImpl(*this).SetScrollSnapDuration(time);
385 float ScrollView::GetScrollFlickDuration() const
387 return GetImpl(*this).GetScrollFlickDuration();
390 void ScrollView::SetScrollFlickDuration(float time)
392 GetImpl(*this).SetScrollFlickDuration(time);
395 void ScrollView::SetRulerX(RulerPtr ruler)
397 GetImpl(*this).SetRulerX(ruler);
400 void ScrollView::SetRulerY(RulerPtr ruler)
402 GetImpl(*this).SetRulerY(ruler);
405 void ScrollView::SetScrollSensitive(bool sensitive)
407 GetImpl(*this).SetScrollSensitive(sensitive);
410 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
412 GetImpl(*this).SetMaxOvershoot(overshootX, overshootY);
415 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
417 GetImpl(*this).SetSnapOvershootAlphaFunction(alpha);
420 void ScrollView::SetSnapOvershootDuration(float duration)
422 GetImpl(*this).SetSnapOvershootDuration(duration);
425 void ScrollView::SetTouchesRequiredForPanning(unsigned int minTouches, unsigned int maxTouches, bool endOutside)
427 GetImpl(*this).SetTouchesRequiredForPanning(minTouches, maxTouches, endOutside);
430 void ScrollView::SetActorAutoSnap(bool enable)
432 GetImpl(*this).SetActorAutoSnap(enable);
435 void ScrollView::SetWrapMode(bool enable)
437 GetImpl(*this).SetWrapMode(enable);
440 int ScrollView::GetScrollUpdateDistance() const
442 return GetImpl(*this).GetScrollUpdateDistance();
445 void ScrollView::SetScrollUpdateDistance(int distance)
447 GetImpl(*this).SetScrollUpdateDistance(distance);
450 bool ScrollView::GetAxisAutoLock() const
452 return GetImpl(*this).GetAxisAutoLock();
455 void ScrollView::SetAxisAutoLock(bool enable)
457 GetImpl(*this).SetAxisAutoLock(enable);
460 float ScrollView::GetAxisAutoLockGradient() const
462 return GetImpl(*this).GetAxisAutoLockGradient();
465 void ScrollView::SetAxisAutoLockGradient(float gradient)
467 GetImpl(*this).SetAxisAutoLockGradient(gradient);
470 float ScrollView::GetFrictionCoefficient() const
472 return GetImpl(*this).GetFrictionCoefficient();
475 void ScrollView::SetFrictionCoefficient(float friction)
477 GetImpl(*this).SetFrictionCoefficient(friction);
480 float ScrollView::GetFlickSpeedCoefficient() const
482 return GetImpl(*this).GetFlickSpeedCoefficient();
485 void ScrollView::SetFlickSpeedCoefficient(float speed)
487 GetImpl(*this).SetFlickSpeedCoefficient(speed);
490 Vector2 ScrollView::GetMinimumDistanceForFlick() const
492 return GetImpl(*this).GetMinimumDistanceForFlick();
495 void ScrollView::SetMinimumDistanceForFlick( const Vector2& distance )
497 GetImpl(*this).SetMinimumDistanceForFlick(distance);
500 float ScrollView::GetMinimumSpeedForFlick() const
502 return GetImpl(*this).GetMinimumSpeedForFlick();
505 void ScrollView::SetMinimumSpeedForFlick( float speed )
507 GetImpl(*this).SetMinimumSpeedForFlick(speed);
510 float ScrollView::GetMaxFlickSpeed() const
512 return GetImpl(*this).GetMaxFlickSpeed();
515 void ScrollView::SetMaxFlickSpeed(float speed)
517 GetImpl(*this).SetMaxFlickSpeed(speed);
520 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
522 return GetImpl(*this).GetMouseWheelScrollDistanceStep();
525 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
527 GetImpl(*this).SetMouseWheelScrollDistanceStep(step);
530 Vector3 ScrollView::GetCurrentScrollPosition() const
532 return GetImpl(*this).GetCurrentScrollPosition();
535 void ScrollView::SetScrollPosition(const Vector3& position)
537 GetImpl(*this).SetScrollPosition(position);
540 unsigned int ScrollView::GetCurrentPage() const
542 return GetImpl(*this).GetCurrentPage();
545 void ScrollView::ScrollTo(const Vector3 &position)
547 GetImpl(*this).ScrollTo(position);
550 void ScrollView::ScrollTo(const Vector3 &position, float duration)
552 GetImpl(*this).ScrollTo(position, duration);
555 void ScrollView::ScrollTo(const Vector3 &position, float duration, AlphaFunction alpha)
557 GetImpl(*this).ScrollTo(position, duration, alpha);
560 void ScrollView::ScrollTo(const Vector3 &position, float duration,
561 DirectionBias horizontalBias, DirectionBias verticalBias)
563 GetImpl(*this).ScrollTo(position, duration, horizontalBias, verticalBias);
566 void ScrollView::ScrollTo(const Vector3 &position, float duration, AlphaFunction alpha,
567 DirectionBias horizontalBias, DirectionBias verticalBias)
569 GetImpl(*this).ScrollTo(position, duration, alpha, horizontalBias, verticalBias);
572 void ScrollView::ScrollTo(unsigned int page)
574 GetImpl(*this).ScrollTo(page);
577 void ScrollView::ScrollTo(unsigned int page, float duration)
579 GetImpl(*this).ScrollTo(page, duration);
582 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
584 GetImpl(*this).ScrollTo(page, duration, bias);
587 void ScrollView::ScrollTo(Actor &actor)
589 GetImpl(*this).ScrollTo(actor);
592 void ScrollView::ScrollTo(Actor &actor, float duration)
594 GetImpl(*this).ScrollTo(actor, duration);
597 bool ScrollView::ScrollToSnapPoint()
599 return GetImpl(*this).ScrollToSnapPoint();
602 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
604 GetImpl(*this).ApplyConstraintToChildren(constraint);
607 void ScrollView::RemoveConstraintsFromChildren()
609 GetImpl(*this).RemoveConstraintsFromChildren();
612 void ScrollView::ApplyEffect(ScrollViewEffect effect)
614 GetImpl(*this).ApplyEffect(effect);
617 void ScrollView::RemoveEffect(ScrollViewEffect effect)
619 GetImpl(*this).RemoveEffect(effect);
622 void ScrollView::RemoveAllEffects()
624 GetImpl(*this).RemoveAllEffects();
627 void ScrollView::BindActor(Actor child)
629 GetImpl(*this).BindActor(child);
632 void ScrollView::UnbindActor(Actor child)
634 GetImpl(*this).UnbindActor(child);
637 ScrollView::SnapStartedSignalType& ScrollView::SnapStartedSignal()
639 return GetImpl(*this).SnapStartedSignal();
642 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
644 GetImpl(*this).SetScrollingDirection( direction, threshold );
647 void ScrollView::RemoveScrollingDirection( Radian direction )
649 GetImpl(*this).RemoveScrollingDirection( direction );
652 } // namespace Toolkit