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.
18 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
21 #include <boost/bind.hpp>
23 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
24 #include <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
25 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view.h>
31 const float DEFAULT_MAX_OVERSHOOT_HEIGHT = 36.0f; // 36 pixels
32 const Vector2 OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE( 720.0f, 42.0f );
33 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
35 // local helper function to resize the height of the bounce actor
36 float GetBounceActorHeight( float width )
38 return (width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD) ? OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height : OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height * 0.5f;
41 const float DEFAULT_OVERSHOOT_ANIMATION_DURATION = 0.35f; // time in seconds
42 const float MAX_OVERSHOOT_NOTIFY_AMOUNT = 0.9f; // 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_1; // 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.1f; // 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 void ScrollOvershootIndicator::ClearOvershoot()
122 mEffectX->SetOvershoot(0.0f);
126 mEffectY->SetOvershoot(0.0f);
130 ScrollOvershootEffect::ScrollOvershootEffect( bool vertical ) :
136 bool ScrollOvershootEffect::IsVertical() const
141 ScrollOvershootEffectRipple::ScrollOvershootEffectRipple( bool vertical, Scrollable& scrollable ) :
142 ScrollOvershootEffect( vertical ),
143 mAttachedScrollView(scrollable),
144 mCanScrollPropertyIndex(Property::INVALID_INDEX),
145 mOvershootProperty(Property::INVALID_INDEX),
146 mEffectOvershootProperty(Property::INVALID_INDEX),
147 mMaxOvershootImageSize(DEFAULT_MAX_OVERSHOOT_HEIGHT),
148 mOvershootAnimationDuration(DEFAULT_OVERSHOOT_ANIMATION_DURATION),
150 mAnimationStateFlags(0)
152 mOvershootOverlay = CreateBouncingEffectActor(mEffectOvershootProperty);
153 mOvershootOverlay.SetColor(mAttachedScrollView.GetOvershootEffectColor());
154 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
155 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
156 mOvershootOverlay.SetDrawMode(DrawMode::OVERLAY);
157 mOvershootOverlay.SetVisible(false);
161 void ScrollOvershootEffectRipple::Apply()
163 Actor self = mAttachedScrollView.Self();
164 mOvershootProperty = self.GetPropertyIndex(IsVertical() ? Toolkit::ScrollView::SCROLL_OVERSHOOT_Y_PROPERTY_NAME : Toolkit::ScrollView::SCROLL_OVERSHOOT_X_PROPERTY_NAME);
165 mCanScrollPropertyIndex = self.GetPropertyIndex(IsVertical() ? Scrollable::SCROLLABLE_CAN_SCROLL_VERTICAL : Scrollable::SCROLLABLE_CAN_SCROLL_HORIZONTAL);
167 // make sure height is set, since we only create a constraint for image width
168 mOvershootOverlay.SetSize(OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.width, OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height);
170 mAttachedScrollView.AddOverlay(mOvershootOverlay);
172 UpdatePropertyNotifications();
175 void ScrollOvershootEffectRipple::Remove( Scrollable& scrollable )
177 if(mOvershootOverlay)
179 if(mOvershootIncreaseNotification)
181 scrollable.Self().RemovePropertyNotification(mOvershootIncreaseNotification);
182 mOvershootIncreaseNotification.Reset();
184 if(mOvershootDecreaseNotification)
186 scrollable.Self().RemovePropertyNotification(mOvershootDecreaseNotification);
187 mOvershootDecreaseNotification.Reset();
189 scrollable.RemoveOverlay(mOvershootOverlay);
193 void ScrollOvershootEffectRipple::Reset()
195 mOvershootOverlay.SetVisible(false);
196 mOvershootOverlay.SetProperty( mEffectOvershootProperty, 0.f);
199 void ScrollOvershootEffectRipple::UpdatePropertyNotifications()
201 float absOvershoot = fabsf(mOvershoot);
203 Actor self = mAttachedScrollView.Self();
204 // update overshoot increase notify
205 if( mOvershootIncreaseNotification )
207 self.RemovePropertyNotification( mOvershootIncreaseNotification );
208 mOvershootIncreaseNotification.Reset();
210 if( absOvershoot < MAX_OVERSHOOT_NOTIFY_AMOUNT )
212 float increaseStep = absOvershoot + OVERSHOOT_NOTIFY_STEP;
213 if( increaseStep > MAX_OVERSHOOT_NOTIFY_AMOUNT )
215 increaseStep = MAX_OVERSHOOT_NOTIFY_AMOUNT;
217 mOvershootIncreaseNotification = self.AddPropertyNotification( mOvershootProperty, OutsideCondition(-increaseStep, increaseStep) );
218 mOvershootIncreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
219 mOvershootIncreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
222 // update overshoot decrease notify
223 if( mOvershootDecreaseNotification )
225 self.RemovePropertyNotification( mOvershootDecreaseNotification );
226 mOvershootDecreaseNotification.Reset();
228 if( absOvershoot > MIN_OVERSHOOT_NOTIFY_AMOUNT )
230 float reduceStep = absOvershoot - OVERSHOOT_NOTIFY_STEP;
231 if( reduceStep < MIN_OVERSHOOT_NOTIFY_AMOUNT )
233 reduceStep = MIN_OVERSHOOT_NOTIFY_AMOUNT;
235 mOvershootDecreaseNotification = self.AddPropertyNotification( mOvershootProperty, InsideCondition(-reduceStep, reduceStep) );
236 mOvershootDecreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
237 mOvershootDecreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
241 void ScrollOvershootEffectRipple::SetOvershootEffectColor( const Vector4& color )
243 if(mOvershootOverlay)
245 mOvershootOverlay.SetColor(color);
249 void ScrollOvershootEffectRipple::UpdateVisibility( bool visible )
251 mOvershootOverlay.SetVisible(visible);
252 // make sure overshoot image is correctly placed
255 Actor self = mAttachedScrollView.Self();
256 if(mOvershoot > 0.0f)
258 // positive overshoot
259 const Vector3 size = mOvershootOverlay.GetCurrentSize();
260 Vector3 relativeOffset;
261 const Vector3 parentSize = self.GetCurrentSize();
264 mOvershootOverlay.SetRotation(Quaternion(0.0f, Vector3::ZAXIS));
265 mOvershootOverlay.SetSize(parentSize.width, GetBounceActorHeight(parentSize.width), size.depth);
269 mOvershootOverlay.SetRotation(Quaternion(1.5f * Math::PI, Vector3::ZAXIS));
270 mOvershootOverlay.SetSize(parentSize.height, GetBounceActorHeight(parentSize.height), size.depth);
271 relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
273 mOvershootOverlay.SetPosition(relativeOffset * parentSize);
277 // negative overshoot
278 const Vector3 size = mOvershootOverlay.GetCurrentSize();
279 Vector3 relativeOffset;
280 const Vector3 parentSize = self.GetCurrentSize();
283 mOvershootOverlay.SetRotation(Quaternion(Math::PI, Vector3::ZAXIS));
284 mOvershootOverlay.SetSize(parentSize.width, GetBounceActorHeight(parentSize.width), size.depth);
285 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
289 mOvershootOverlay.SetRotation(Quaternion(0.5f * Math::PI, Vector3::ZAXIS));
290 mOvershootOverlay.SetSize(parentSize.height, GetBounceActorHeight(parentSize.height), size.depth);
291 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
293 mOvershootOverlay.SetPosition(relativeOffset * parentSize);
298 void ScrollOvershootEffectRipple::OnOvershootNotification(PropertyNotification& source)
300 Actor self = mAttachedScrollView.Self();
301 mOvershoot = self.GetProperty<float>(mOvershootProperty);
302 if( source == mOvershootIncreaseNotification )
304 if( mOvershoot > Math::MACHINE_EPSILON_0 )
308 else if ( mOvershoot < -Math::MACHINE_EPSILON_0 )
313 else if( source == mOvershootDecreaseNotification )
316 // overshoot reducing
318 UpdatePropertyNotifications();
321 void ScrollOvershootEffectRipple::SetOvershoot(float amount, bool animate)
323 float absAmount = fabsf(amount);
324 bool animatingOn = absAmount > Math::MACHINE_EPSILON_0;
325 if( (animatingOn && (mAnimationStateFlags & AnimatingIn)) )
327 // trying to do what we are already doing
328 if( mAnimationStateFlags & AnimateBack )
330 mAnimationStateFlags &= ~AnimateBack;
334 if( (!animatingOn && (mAnimationStateFlags & AnimatingOut)) )
336 // trying to do what we are already doing
339 if( !animatingOn && (mAnimationStateFlags & AnimatingIn) )
341 // dont interrupt while animating on
342 mAnimationStateFlags |= AnimateBack;
345 // When we need to animate overshoot to 0
346 if( mOvershootAnimationDuration > Math::MACHINE_EPSILON_1 )
348 // setup the new overshoot to 0 animation
349 float currentOvershoot = fabsf( mOvershootOverlay.GetProperty( mEffectOvershootProperty ).Get<float>() );
350 float duration = mOvershootAnimationDuration * (animatingOn ? (1.0f - currentOvershoot) : currentOvershoot);
352 if( duration > Math::MACHINE_EPSILON_0 )
354 if(mScrollOvershootAnimation)
356 mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
357 mScrollOvershootAnimation.Stop();
358 mScrollOvershootAnimation.Clear();
359 mScrollOvershootAnimation = NULL;
361 mScrollOvershootAnimation = Animation::New(duration);
362 mScrollOvershootAnimation.FinishedSignal().Connect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
363 mScrollOvershootAnimation.AnimateTo( Property(mOvershootOverlay, mEffectOvershootProperty), amount, TimePeriod(duration) );
364 mScrollOvershootAnimation.Play();
365 mAnimationStateFlags = animatingOn ? AnimatingIn : AnimatingOut;
370 mOvershootOverlay.SetProperty( mEffectOvershootProperty, amount);
372 if( absAmount > Math::MACHINE_EPSILON_1 )
374 UpdateVisibility(true);
378 void ScrollOvershootEffectRipple::OnOvershootAnimFinished(Animation& animation)
380 bool animateOff = false;
381 if( mAnimationStateFlags & AnimatingOut )
383 // should now be offscreen
384 mOvershootOverlay.SetVisible(false);
386 if( (mAnimationStateFlags & AnimateBack) )
390 mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
391 mScrollOvershootAnimation.Stop();
392 mScrollOvershootAnimation.Clear();
393 mScrollOvershootAnimation = NULL;
394 mAnimationStateFlags = 0;
397 SetOvershoot(0.0f, true);
401 ScrollOvershootEffectRipplePtr ScrollOvershootEffectRipple::New( bool vertical, Scrollable& scrollable )
403 return new ScrollOvershootEffectRipple(vertical, scrollable);
406 } // namespace Internal
408 } // namespace Toolkit