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-toolkit/internal/controls/scrollable/scrollable-impl.h>
23 #include <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
24 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view.h>
31 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
33 // local helper function to resize the height of the bounce actor
34 float GetBounceActorHeight( float width, float defaultHeight )
36 return (width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD) ? defaultHeight : defaultHeight * 0.5f;
39 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
40 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
41 const float OVERSHOOT_NOTIFY_STEP = 0.01f; // amount to set notifications beyond current overshoot value
54 ScrollOvershootIndicator::ScrollOvershootIndicator() :
60 ScrollOvershootIndicator::~ScrollOvershootIndicator()
65 ScrollOvershootIndicator* ScrollOvershootIndicator::New()
67 ScrollOvershootIndicator* scrollOvershootPtr = new ScrollOvershootIndicator();
68 return scrollOvershootPtr;
71 void ScrollOvershootIndicator::AttachToScrollable(Scrollable& scrollable)
75 mEffectX = ScrollOvershootEffectRipple::New(false, scrollable);
80 mEffectY = ScrollOvershootEffectRipple::New(true, scrollable);
85 void ScrollOvershootIndicator::DetachFromScrollable(Scrollable& scrollable)
89 mEffectX->Remove(scrollable);
93 mEffectY->Remove(scrollable);
97 void ScrollOvershootIndicator::Reset()
103 void ScrollOvershootIndicator::SetOvershootEffectColor( const Vector4& color )
107 mEffectX->SetOvershootEffectColor(color);
111 mEffectY->SetOvershootEffectColor(color);
115 ScrollOvershootEffect::ScrollOvershootEffect( bool vertical ) :
121 bool ScrollOvershootEffect::IsVertical() const
126 ScrollOvershootEffectRipple::ScrollOvershootEffectRipple( bool vertical, Scrollable& scrollable ) :
127 ScrollOvershootEffect( vertical ),
128 mAttachedScrollView(scrollable),
129 mOvershootProperty(Property::INVALID_INDEX),
130 mEffectOvershootProperty(Property::INVALID_INDEX),
132 mOvershootSize( scrollable.GetOvershootSize() ),
133 mAnimationStateFlags(0)
135 mOvershootOverlay = CreateBouncingEffectActor(mEffectOvershootProperty);
136 mOvershootOverlay.SetColor(mAttachedScrollView.GetOvershootEffectColor());
137 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
138 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
139 mOvershootOverlay.SetVisible(false);
143 void ScrollOvershootEffectRipple::Apply()
145 Actor self = mAttachedScrollView.Self();
146 mOvershootProperty = IsVertical() ? Toolkit::ScrollView::Property::OVERSHOOT_Y : Toolkit::ScrollView::Property::OVERSHOOT_X;
148 // make sure height is set, since we only create a constraint for image width
149 mOvershootSize = mAttachedScrollView.GetOvershootSize();
150 mOvershootOverlay.SetSize( mOvershootSize );
152 mAttachedScrollView.AddOverlay(mOvershootOverlay);
154 UpdatePropertyNotifications();
157 void ScrollOvershootEffectRipple::Remove( Scrollable& scrollable )
159 if(mOvershootOverlay)
161 if(mOvershootIncreaseNotification)
163 scrollable.Self().RemovePropertyNotification(mOvershootIncreaseNotification);
164 mOvershootIncreaseNotification.Reset();
166 if(mOvershootDecreaseNotification)
168 scrollable.Self().RemovePropertyNotification(mOvershootDecreaseNotification);
169 mOvershootDecreaseNotification.Reset();
171 scrollable.RemoveOverlay(mOvershootOverlay);
175 void ScrollOvershootEffectRipple::Reset()
177 mOvershootOverlay.SetVisible(false);
178 mOvershootOverlay.SetProperty( mEffectOvershootProperty, 0.f);
181 void ScrollOvershootEffectRipple::UpdatePropertyNotifications()
183 float absOvershoot = fabsf(mOvershoot);
185 Actor self = mAttachedScrollView.Self();
186 // update overshoot increase notify
187 if( mOvershootIncreaseNotification )
189 self.RemovePropertyNotification( mOvershootIncreaseNotification );
190 mOvershootIncreaseNotification.Reset();
192 if( absOvershoot < MAX_OVERSHOOT_NOTIFY_AMOUNT )
194 float increaseStep = absOvershoot + OVERSHOOT_NOTIFY_STEP;
195 if( increaseStep > MAX_OVERSHOOT_NOTIFY_AMOUNT )
197 increaseStep = MAX_OVERSHOOT_NOTIFY_AMOUNT;
199 mOvershootIncreaseNotification = self.AddPropertyNotification( mOvershootProperty, OutsideCondition(-increaseStep, increaseStep) );
200 mOvershootIncreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
201 mOvershootIncreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
204 // update overshoot decrease notify
205 if( mOvershootDecreaseNotification )
207 self.RemovePropertyNotification( mOvershootDecreaseNotification );
208 mOvershootDecreaseNotification.Reset();
210 if( absOvershoot > MIN_OVERSHOOT_NOTIFY_AMOUNT )
212 float reduceStep = absOvershoot - OVERSHOOT_NOTIFY_STEP;
213 if( reduceStep < MIN_OVERSHOOT_NOTIFY_AMOUNT )
215 reduceStep = MIN_OVERSHOOT_NOTIFY_AMOUNT;
217 mOvershootDecreaseNotification = self.AddPropertyNotification( mOvershootProperty, InsideCondition(-reduceStep, reduceStep) );
218 mOvershootDecreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
219 mOvershootDecreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
223 void ScrollOvershootEffectRipple::SetOvershootEffectColor( const Vector4& color )
225 if(mOvershootOverlay)
227 mOvershootOverlay.SetColor(color);
231 void ScrollOvershootEffectRipple::UpdateVisibility( bool visible )
233 mOvershootOverlay.SetVisible(visible);
234 // make sure overshoot image is correctly placed
237 Actor self = mAttachedScrollView.Self();
238 if(mOvershoot > 0.0f)
240 // positive overshoot
241 const Vector3 size = mOvershootOverlay.GetCurrentSize();
242 Vector3 relativeOffset;
243 const Vector3 parentSize = self.GetCurrentSize();
246 mOvershootOverlay.SetOrientation( Quaternion( Radian( 0.0f ), Vector3::ZAXIS ) );
247 mOvershootOverlay.SetSize(parentSize.width, GetBounceActorHeight(parentSize.width, mOvershootSize.height), size.depth);
251 mOvershootOverlay.SetOrientation( Quaternion( Radian( 1.5f * Math::PI ), Vector3::ZAXIS ) );
252 mOvershootOverlay.SetSize(parentSize.height, GetBounceActorHeight(parentSize.height, mOvershootSize.height), size.depth);
253 relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
255 mOvershootOverlay.SetPosition(relativeOffset * parentSize);
259 // negative overshoot
260 const Vector3 size = mOvershootOverlay.GetCurrentSize();
261 Vector3 relativeOffset;
262 const Vector3 parentSize = self.GetCurrentSize();
265 mOvershootOverlay.SetOrientation( Quaternion( Radian( Math::PI ), Vector3::ZAXIS ) );
266 mOvershootOverlay.SetSize(parentSize.width, GetBounceActorHeight(parentSize.width, mOvershootSize.height), size.depth);
267 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
271 mOvershootOverlay.SetOrientation( Quaternion( Radian( 0.5f * Math::PI ), Vector3::ZAXIS ) );
272 mOvershootOverlay.SetSize(parentSize.height, GetBounceActorHeight(parentSize.height, mOvershootSize.height), size.depth);
273 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
275 mOvershootOverlay.SetPosition(relativeOffset * parentSize);
280 void ScrollOvershootEffectRipple::OnOvershootNotification(PropertyNotification& source)
282 Actor self = mAttachedScrollView.Self();
283 mOvershoot = self.GetCurrentProperty< float >( mOvershootProperty );
284 SetOvershoot(mOvershoot, false);
285 UpdatePropertyNotifications();
288 void ScrollOvershootEffectRipple::SetOvershoot(float amount, bool animate)
290 float absAmount = fabsf(amount);
291 bool animatingOn = absAmount > Math::MACHINE_EPSILON_0;
292 if( (animatingOn && (mAnimationStateFlags & AnimatingIn)) )
294 // trying to do what we are already doing
295 if( mAnimationStateFlags & AnimateBack )
297 mAnimationStateFlags &= ~AnimateBack;
301 if( (!animatingOn && (mAnimationStateFlags & AnimatingOut)) )
303 // trying to do what we are already doing
306 if( !animatingOn && (mAnimationStateFlags & AnimatingIn) )
308 // dont interrupt while animating on
309 mAnimationStateFlags |= AnimateBack;
313 if( absAmount > Math::MACHINE_EPSILON_1 )
315 UpdateVisibility(true);
318 float overshootAnimationSpeed = mAttachedScrollView.Self().GetProperty<float>(Toolkit::Scrollable::Property::OVERSHOOT_ANIMATION_SPEED);
320 if( animate && overshootAnimationSpeed > Math::MACHINE_EPSILON_0 )
322 float currentOvershoot = fabsf( mOvershootOverlay.GetProperty( mEffectOvershootProperty ).Get<float>() );
323 float duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - currentOvershoot) : currentOvershoot) / overshootAnimationSpeed;
325 if( duration > Math::MACHINE_EPSILON_0 )
327 if(mScrollOvershootAnimation)
329 mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
330 mScrollOvershootAnimation.Stop();
331 mScrollOvershootAnimation.Reset();
333 mScrollOvershootAnimation = Animation::New(duration);
334 mScrollOvershootAnimation.FinishedSignal().Connect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
335 mScrollOvershootAnimation.AnimateTo( Property(mOvershootOverlay, mEffectOvershootProperty), amount, TimePeriod(duration) );
336 mScrollOvershootAnimation.Play();
337 mAnimationStateFlags = animatingOn ? AnimatingIn : AnimatingOut;
342 mOvershootOverlay.SetProperty( mEffectOvershootProperty, amount);
346 void ScrollOvershootEffectRipple::OnOvershootAnimFinished(Animation& animation)
348 bool animateOff = false;
349 if( mAnimationStateFlags & AnimatingOut )
351 // should now be offscreen
352 mOvershootOverlay.SetVisible(false);
354 if( (mAnimationStateFlags & AnimateBack) )
358 mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
359 mScrollOvershootAnimation.Stop();
360 mScrollOvershootAnimation.Reset();
361 mAnimationStateFlags = 0;
364 SetOvershoot(0.0f, true);
368 ScrollOvershootEffectRipplePtr ScrollOvershootEffectRipple::New( bool vertical, Scrollable& scrollable )
370 return new ScrollOvershootEffectRipple(vertical, scrollable);
373 } // namespace Internal
375 } // namespace Toolkit