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/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>
30 const Vector2 OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE( 720.0f, 42.0f );
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 )
36 return (width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD) ? OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height : OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height * 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 void ScrollOvershootIndicator::ClearOvershoot()
119 mEffectX->SetOvershoot(0.0f);
123 mEffectY->SetOvershoot(0.0f);
127 ScrollOvershootEffect::ScrollOvershootEffect( bool vertical ) :
133 bool ScrollOvershootEffect::IsVertical() const
138 ScrollOvershootEffectRipple::ScrollOvershootEffectRipple( bool vertical, Scrollable& scrollable ) :
139 ScrollOvershootEffect( vertical ),
140 mAttachedScrollView(scrollable),
141 mOvershootProperty(Property::INVALID_INDEX),
142 mEffectOvershootProperty(Property::INVALID_INDEX),
144 mAnimationStateFlags(0)
147 mOvershootOverlay = CreateBouncingEffectActor(mEffectOvershootProperty);
148 mOvershootOverlay.SetColor(mAttachedScrollView.GetOvershootEffectColor());
149 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
150 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
151 mOvershootOverlay.SetDrawMode(DrawMode::OVERLAY);
152 mOvershootOverlay.SetVisible(false);
156 void ScrollOvershootEffectRipple::Apply()
158 Actor self = mAttachedScrollView.Self();
159 mOvershootProperty = IsVertical() ? Toolkit::ScrollView::Property::OVERSHOOT_Y : Toolkit::ScrollView::Property::OVERSHOOT_X;
161 // make sure height is set, since we only create a constraint for image width
162 //mOvershootOverlay.SetSize(OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.width, OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height);
164 //mAttachedScrollView.AddOverlay(mOvershootOverlay);
166 UpdatePropertyNotifications();
169 void ScrollOvershootEffectRipple::Remove( Scrollable& scrollable )
171 // if(mOvershootOverlay)
173 if(mOvershootIncreaseNotification)
175 scrollable.Self().RemovePropertyNotification(mOvershootIncreaseNotification);
176 mOvershootIncreaseNotification.Reset();
178 if(mOvershootDecreaseNotification)
180 scrollable.Self().RemovePropertyNotification(mOvershootDecreaseNotification);
181 mOvershootDecreaseNotification.Reset();
183 //scrollable.RemoveOverlay(mOvershootOverlay);
187 void ScrollOvershootEffectRipple::Reset()
189 // mOvershootOverlay.SetVisible(false);
190 // mOvershootOverlay.SetProperty( mEffectOvershootProperty, 0.f);
193 void ScrollOvershootEffectRipple::UpdatePropertyNotifications()
195 float absOvershoot = fabsf(mOvershoot);
197 Actor self = mAttachedScrollView.Self();
198 // update overshoot increase notify
199 if( mOvershootIncreaseNotification )
201 self.RemovePropertyNotification( mOvershootIncreaseNotification );
202 mOvershootIncreaseNotification.Reset();
204 if( absOvershoot < MAX_OVERSHOOT_NOTIFY_AMOUNT )
206 float increaseStep = absOvershoot + OVERSHOOT_NOTIFY_STEP;
207 if( increaseStep > MAX_OVERSHOOT_NOTIFY_AMOUNT )
209 increaseStep = MAX_OVERSHOOT_NOTIFY_AMOUNT;
211 mOvershootIncreaseNotification = self.AddPropertyNotification( mOvershootProperty, OutsideCondition(-increaseStep, increaseStep) );
212 mOvershootIncreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
213 mOvershootIncreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
216 // update overshoot decrease notify
217 if( mOvershootDecreaseNotification )
219 self.RemovePropertyNotification( mOvershootDecreaseNotification );
220 mOvershootDecreaseNotification.Reset();
222 if( absOvershoot > MIN_OVERSHOOT_NOTIFY_AMOUNT )
224 float reduceStep = absOvershoot - OVERSHOOT_NOTIFY_STEP;
225 if( reduceStep < MIN_OVERSHOOT_NOTIFY_AMOUNT )
227 reduceStep = MIN_OVERSHOOT_NOTIFY_AMOUNT;
229 mOvershootDecreaseNotification = self.AddPropertyNotification( mOvershootProperty, InsideCondition(-reduceStep, reduceStep) );
230 mOvershootDecreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
231 mOvershootDecreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
235 void ScrollOvershootEffectRipple::SetOvershootEffectColor( const Vector4& color )
239 void ScrollOvershootEffectRipple::UpdateVisibility( bool visible )
243 void ScrollOvershootEffectRipple::OnOvershootNotification(PropertyNotification& source)
245 Actor self = mAttachedScrollView.Self();
246 mOvershoot = self.GetProperty<float>(mOvershootProperty);
247 SetOvershoot(mOvershoot, false);
248 UpdatePropertyNotifications();
251 void ScrollOvershootEffectRipple::SetOvershoot(float amount, bool animate)
253 float absAmount = fabsf(amount);
254 bool animatingOn = absAmount > Math::MACHINE_EPSILON_0;
255 if( (animatingOn && (mAnimationStateFlags & AnimatingIn)) )
257 // trying to do what we are already doing
258 if( mAnimationStateFlags & AnimateBack )
260 mAnimationStateFlags &= ~AnimateBack;
264 if( (!animatingOn && (mAnimationStateFlags & AnimatingOut)) )
266 // trying to do what we are already doing
269 if( !animatingOn && (mAnimationStateFlags & AnimatingIn) )
271 // dont interrupt while animating on
272 mAnimationStateFlags |= AnimateBack;
276 if( absAmount > Math::MACHINE_EPSILON_1 )
278 UpdateVisibility(true);
281 float overshootAnimationSpeed = mAttachedScrollView.Self().GetProperty<float>(Toolkit::Scrollable::Property::OVERSHOOT_ANIMATION_SPEED);
283 if( animate && overshootAnimationSpeed > Math::MACHINE_EPSILON_0 )
285 //float currentOvershoot = 0.0f;//fabsf( mOvershootOverlay.GetProperty( mEffectOvershootProperty ).Get<float>() );
286 float duration = 0.05f;//mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - currentOvershoot) : currentOvershoot) / overshootAnimationSpeed;
288 if( duration > Math::MACHINE_EPSILON_0 )
290 if(mScrollOvershootAnimation)
292 mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
293 mScrollOvershootAnimation.Stop();
294 mScrollOvershootAnimation.Reset();
296 mScrollOvershootAnimation = Animation::New(duration);
297 mScrollOvershootAnimation.FinishedSignal().Connect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
298 //mScrollOvershootAnimation.AnimateTo( Property(mOvershootOverlay, mEffectOvershootProperty), amount, TimePeriod(duration) );
299 mScrollOvershootAnimation.Play();
300 mAnimationStateFlags = animatingOn ? AnimatingIn : AnimatingOut;
305 //mOvershootOverlay.SetProperty( mEffectOvershootProperty, amount);
309 void ScrollOvershootEffectRipple::OnOvershootAnimFinished(Animation& animation)
311 bool animateOff = false;
312 if( mAnimationStateFlags & AnimatingOut )
314 // should now be offscreen
315 //mOvershootOverlay.SetVisible(false);
317 if( (mAnimationStateFlags & AnimateBack) )
321 mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
322 mScrollOvershootAnimation.Stop();
323 mScrollOvershootAnimation.Reset();
324 mAnimationStateFlags = 0;
327 SetOvershoot(0.0f, true);
331 ScrollOvershootEffectRipplePtr ScrollOvershootEffectRipple::New( bool vertical, Scrollable& scrollable )
333 return new ScrollOvershootEffectRipple(vertical, scrollable);
336 } // namespace Internal
338 } // namespace Toolkit