2 * Copyright (c) 2017 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/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
22 #include <dali/devel-api/object/handle-devel.h>
25 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
26 #include <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
27 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view.h>
34 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
36 // local helper function to resize the height of the bounce actor
37 float GetBounceActorHeight( float width, float defaultHeight )
39 return (width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD) ? defaultHeight : defaultHeight * 0.5f;
42 const float MAX_OVERSHOOT_NOTIFY_AMOUNT = 0.99f; // maximum amount to set notification for increased overshoot, beyond this we just wait for it to reduce again
43 const float MIN_OVERSHOOT_NOTIFY_AMOUNT = Math::MACHINE_EPSILON_0; // minimum amount to set notification for reduced overshoot, beyond this we just wait for it to increase again
44 const float OVERSHOOT_NOTIFY_STEP = 0.01f; // amount to set notifications beyond current overshoot value
57 ScrollOvershootIndicator::ScrollOvershootIndicator() :
63 ScrollOvershootIndicator::~ScrollOvershootIndicator()
68 ScrollOvershootIndicator* ScrollOvershootIndicator::New()
70 ScrollOvershootIndicator* scrollOvershootPtr = new ScrollOvershootIndicator();
71 return scrollOvershootPtr;
74 void ScrollOvershootIndicator::AttachToScrollable(Scrollable& scrollable)
78 mEffectX = ScrollOvershootEffectRipple::New(false, scrollable);
83 mEffectY = ScrollOvershootEffectRipple::New(true, scrollable);
88 void ScrollOvershootIndicator::DetachFromScrollable(Scrollable& scrollable)
92 mEffectX->Remove(scrollable);
96 mEffectY->Remove(scrollable);
100 void ScrollOvershootIndicator::Reset()
106 void ScrollOvershootIndicator::SetOvershootEffectColor( const Vector4& color )
110 mEffectX->SetOvershootEffectColor(color);
114 mEffectY->SetOvershootEffectColor(color);
118 ScrollOvershootEffect::ScrollOvershootEffect( bool vertical ) :
124 bool ScrollOvershootEffect::IsVertical() const
129 ScrollOvershootEffectRipple::ScrollOvershootEffectRipple( bool vertical, Scrollable& scrollable ) :
130 ScrollOvershootEffect( vertical ),
131 mAttachedScrollView(scrollable),
132 mOvershootProperty(Property::INVALID_INDEX),
133 mEffectOvershootProperty(Property::INVALID_INDEX),
135 mOvershootSize( scrollable.GetOvershootSize() ),
136 mAnimationStateFlags(0)
138 mOvershootOverlay = CreateBouncingEffectActor(mEffectOvershootProperty);
139 mOvershootOverlay.SetColor(mAttachedScrollView.GetOvershootEffectColor());
140 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
141 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
142 mOvershootOverlay.SetVisible(false);
146 void ScrollOvershootEffectRipple::Apply()
148 Actor self = mAttachedScrollView.Self();
149 mOvershootProperty = IsVertical() ? Toolkit::ScrollView::Property::OVERSHOOT_Y : Toolkit::ScrollView::Property::OVERSHOOT_X;
151 // make sure height is set, since we only create a constraint for image width
152 mOvershootSize = mAttachedScrollView.GetOvershootSize();
153 mOvershootOverlay.SetSize( mOvershootSize );
155 mAttachedScrollView.AddOverlay(mOvershootOverlay);
157 UpdatePropertyNotifications();
160 void ScrollOvershootEffectRipple::Remove( Scrollable& scrollable )
162 if(mOvershootOverlay)
164 if(mOvershootIncreaseNotification)
166 scrollable.Self().RemovePropertyNotification(mOvershootIncreaseNotification);
167 mOvershootIncreaseNotification.Reset();
169 if(mOvershootDecreaseNotification)
171 scrollable.Self().RemovePropertyNotification(mOvershootDecreaseNotification);
172 mOvershootDecreaseNotification.Reset();
174 scrollable.RemoveOverlay(mOvershootOverlay);
178 void ScrollOvershootEffectRipple::Reset()
180 mOvershootOverlay.SetVisible(false);
181 mOvershootOverlay.SetProperty( mEffectOvershootProperty, 0.f);
184 void ScrollOvershootEffectRipple::UpdatePropertyNotifications()
186 float absOvershoot = fabsf(mOvershoot);
188 Actor self = mAttachedScrollView.Self();
189 // update overshoot increase notify
190 if( mOvershootIncreaseNotification )
192 self.RemovePropertyNotification( mOvershootIncreaseNotification );
193 mOvershootIncreaseNotification.Reset();
195 if( absOvershoot < MAX_OVERSHOOT_NOTIFY_AMOUNT )
197 float increaseStep = absOvershoot + OVERSHOOT_NOTIFY_STEP;
198 if( increaseStep > MAX_OVERSHOOT_NOTIFY_AMOUNT )
200 increaseStep = MAX_OVERSHOOT_NOTIFY_AMOUNT;
202 mOvershootIncreaseNotification = self.AddPropertyNotification( mOvershootProperty, OutsideCondition(-increaseStep, increaseStep) );
203 mOvershootIncreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
204 mOvershootIncreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
207 // update overshoot decrease notify
208 if( mOvershootDecreaseNotification )
210 self.RemovePropertyNotification( mOvershootDecreaseNotification );
211 mOvershootDecreaseNotification.Reset();
213 if( absOvershoot > MIN_OVERSHOOT_NOTIFY_AMOUNT )
215 float reduceStep = absOvershoot - OVERSHOOT_NOTIFY_STEP;
216 if( reduceStep < MIN_OVERSHOOT_NOTIFY_AMOUNT )
218 reduceStep = MIN_OVERSHOOT_NOTIFY_AMOUNT;
220 mOvershootDecreaseNotification = self.AddPropertyNotification( mOvershootProperty, InsideCondition(-reduceStep, reduceStep) );
221 mOvershootDecreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
222 mOvershootDecreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
226 void ScrollOvershootEffectRipple::SetOvershootEffectColor( const Vector4& color )
228 if(mOvershootOverlay)
230 mOvershootOverlay.SetColor(color);
234 void ScrollOvershootEffectRipple::UpdateVisibility( bool visible )
236 mOvershootOverlay.SetVisible(visible);
237 // make sure overshoot image is correctly placed
240 Actor self = mAttachedScrollView.Self();
241 if(mOvershoot > 0.0f)
243 // positive overshoot
244 const Vector3 size = mOvershootOverlay.GetCurrentSize();
245 Vector3 relativeOffset;
246 const Vector3 parentSize = self.GetCurrentSize();
249 mOvershootOverlay.SetOrientation( Quaternion( Radian( 0.0f ), Vector3::ZAXIS ) );
250 mOvershootOverlay.SetSize(parentSize.width, GetBounceActorHeight(parentSize.width, mOvershootSize.height), size.depth);
254 mOvershootOverlay.SetOrientation( Quaternion( Radian( 1.5f * Math::PI ), Vector3::ZAXIS ) );
255 mOvershootOverlay.SetSize(parentSize.height, GetBounceActorHeight(parentSize.height, mOvershootSize.height), size.depth);
256 relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
258 mOvershootOverlay.SetPosition(relativeOffset * parentSize);
262 // negative overshoot
263 const Vector3 size = mOvershootOverlay.GetCurrentSize();
264 Vector3 relativeOffset;
265 const Vector3 parentSize = self.GetCurrentSize();
268 mOvershootOverlay.SetOrientation( Quaternion( Radian( Math::PI ), Vector3::ZAXIS ) );
269 mOvershootOverlay.SetSize(parentSize.width, GetBounceActorHeight(parentSize.width, mOvershootSize.height), size.depth);
270 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
274 mOvershootOverlay.SetOrientation( Quaternion( Radian( 0.5f * Math::PI ), Vector3::ZAXIS ) );
275 mOvershootOverlay.SetSize(parentSize.height, GetBounceActorHeight(parentSize.height, mOvershootSize.height), size.depth);
276 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
278 mOvershootOverlay.SetPosition(relativeOffset * parentSize);
283 void ScrollOvershootEffectRipple::OnOvershootNotification(PropertyNotification& source)
285 Actor self = mAttachedScrollView.Self();
286 mOvershoot = DevelHandle::GetCurrentProperty< float >( self, mOvershootProperty );
287 SetOvershoot(mOvershoot, false);
288 UpdatePropertyNotifications();
291 void ScrollOvershootEffectRipple::SetOvershoot(float amount, bool animate)
293 float absAmount = fabsf(amount);
294 bool animatingOn = absAmount > Math::MACHINE_EPSILON_0;
295 if( (animatingOn && (mAnimationStateFlags & AnimatingIn)) )
297 // trying to do what we are already doing
298 if( mAnimationStateFlags & AnimateBack )
300 mAnimationStateFlags &= ~AnimateBack;
304 if( (!animatingOn && (mAnimationStateFlags & AnimatingOut)) )
306 // trying to do what we are already doing
309 if( !animatingOn && (mAnimationStateFlags & AnimatingIn) )
311 // dont interrupt while animating on
312 mAnimationStateFlags |= AnimateBack;
316 if( absAmount > Math::MACHINE_EPSILON_1 )
318 UpdateVisibility(true);
321 float overshootAnimationSpeed = mAttachedScrollView.Self().GetProperty<float>(Toolkit::Scrollable::Property::OVERSHOOT_ANIMATION_SPEED);
323 if( animate && overshootAnimationSpeed > Math::MACHINE_EPSILON_0 )
325 float currentOvershoot = fabsf( mOvershootOverlay.GetProperty( mEffectOvershootProperty ).Get<float>() );
326 float duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - currentOvershoot) : currentOvershoot) / overshootAnimationSpeed;
328 if( duration > Math::MACHINE_EPSILON_0 )
330 if(mScrollOvershootAnimation)
332 mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
333 mScrollOvershootAnimation.Stop();
334 mScrollOvershootAnimation.Reset();
336 mScrollOvershootAnimation = Animation::New(duration);
337 mScrollOvershootAnimation.FinishedSignal().Connect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
338 mScrollOvershootAnimation.AnimateTo( Property(mOvershootOverlay, mEffectOvershootProperty), amount, TimePeriod(duration) );
339 mScrollOvershootAnimation.Play();
340 mAnimationStateFlags = animatingOn ? AnimatingIn : AnimatingOut;
345 mOvershootOverlay.SetProperty( mEffectOvershootProperty, amount);
349 void ScrollOvershootEffectRipple::OnOvershootAnimFinished(Animation& animation)
351 bool animateOff = false;
352 if( mAnimationStateFlags & AnimatingOut )
354 // should now be offscreen
355 mOvershootOverlay.SetVisible(false);
357 if( (mAnimationStateFlags & AnimateBack) )
361 mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
362 mScrollOvershootAnimation.Stop();
363 mScrollOvershootAnimation.Reset();
364 mAnimationStateFlags = 0;
367 SetOvershoot(0.0f, true);
371 ScrollOvershootEffectRipplePtr ScrollOvershootEffectRipple::New( bool vertical, Scrollable& scrollable )
373 return new ScrollOvershootEffectRipple(vertical, scrollable);
376 } // namespace Internal
378 } // namespace Toolkit