Update scroll end effect GUI
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scrollable / scroll-view / scroll-overshoot-indicator-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
20
21 // INTERNAL INCLUDES
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>
25
26 using namespace Dali;
27
28 namespace
29 {
30
31 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
32
33 // local helper function to resize the height of the bounce actor
34 float GetBounceActorHeight( float width, float defaultHeight )
35 {
36   return (width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD) ? defaultHeight : defaultHeight * 0.5f;
37 }
38
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
42
43 }
44
45 namespace Dali
46 {
47
48 namespace Toolkit
49 {
50
51 namespace Internal
52 {
53
54 ScrollOvershootIndicator::ScrollOvershootIndicator() :
55   mEffectX(NULL),
56   mEffectY(NULL)
57 {
58 }
59
60 ScrollOvershootIndicator::~ScrollOvershootIndicator()
61 {
62
63 }
64
65 ScrollOvershootIndicator* ScrollOvershootIndicator::New()
66 {
67   ScrollOvershootIndicator* scrollOvershootPtr = new ScrollOvershootIndicator();
68   return scrollOvershootPtr;
69 }
70
71 void ScrollOvershootIndicator::AttachToScrollable(Scrollable& scrollable)
72 {
73   if(!mEffectX)
74   {
75     mEffectX = ScrollOvershootEffectRipple::New(false, scrollable);
76   }
77   mEffectX->Apply();
78   if(!mEffectY)
79   {
80     mEffectY = ScrollOvershootEffectRipple::New(true, scrollable);
81   }
82   mEffectY->Apply();
83 }
84
85 void ScrollOvershootIndicator::DetachFromScrollable(Scrollable& scrollable)
86 {
87   if(mEffectX)
88   {
89     mEffectX->Remove(scrollable);
90   }
91   if(mEffectY)
92   {
93     mEffectY->Remove(scrollable);
94   }
95 }
96
97 void ScrollOvershootIndicator::Reset()
98 {
99   mEffectX->Reset();
100   mEffectY->Reset();
101 }
102
103 void ScrollOvershootIndicator::SetOvershootEffectColor( const Vector4& color )
104 {
105   if(mEffectX)
106   {
107     mEffectX->SetOvershootEffectColor(color);
108   }
109   if(mEffectY)
110   {
111     mEffectY->SetOvershootEffectColor(color);
112   }
113 }
114
115 ScrollOvershootEffect::ScrollOvershootEffect( bool vertical ) :
116     mVertical(vertical)
117 {
118
119 }
120
121 bool ScrollOvershootEffect::IsVertical() const
122 {
123   return mVertical;
124 }
125
126 ScrollOvershootEffectRipple::ScrollOvershootEffectRipple( bool vertical, Scrollable& scrollable ) :
127     ScrollOvershootEffect( vertical ),
128     mAttachedScrollView(scrollable),
129     mOvershootProperty(Property::INVALID_INDEX),
130     mEffectOvershootProperty(Property::INVALID_INDEX),
131     mOvershoot(0.0f),
132     mOvershootSize( scrollable.GetOvershootSize() ),
133     mAnimationStateFlags(0)
134 {
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);
140
141 }
142
143 void ScrollOvershootEffectRipple::Apply()
144 {
145   Actor self = mAttachedScrollView.Self();
146   mOvershootProperty = IsVertical() ? Toolkit::ScrollView::Property::OVERSHOOT_Y : Toolkit::ScrollView::Property::OVERSHOOT_X;
147
148   // make sure height is set, since we only create a constraint for image width
149   mOvershootOverlay.SetSize( mOvershootSize );
150
151   mAttachedScrollView.AddOverlay(mOvershootOverlay);
152
153   UpdatePropertyNotifications();
154 }
155
156 void ScrollOvershootEffectRipple::Remove( Scrollable& scrollable )
157 {
158   if(mOvershootOverlay)
159   {
160     if(mOvershootIncreaseNotification)
161     {
162       scrollable.Self().RemovePropertyNotification(mOvershootIncreaseNotification);
163       mOvershootIncreaseNotification.Reset();
164     }
165     if(mOvershootDecreaseNotification)
166     {
167       scrollable.Self().RemovePropertyNotification(mOvershootDecreaseNotification);
168       mOvershootDecreaseNotification.Reset();
169     }
170     scrollable.RemoveOverlay(mOvershootOverlay);
171   }
172 }
173
174 void ScrollOvershootEffectRipple::Reset()
175 {
176   mOvershootOverlay.SetVisible(false);
177   mOvershootOverlay.SetProperty( mEffectOvershootProperty, 0.f);
178 }
179
180 void ScrollOvershootEffectRipple::UpdatePropertyNotifications()
181 {
182   float absOvershoot = fabsf(mOvershoot);
183
184   Actor self = mAttachedScrollView.Self();
185   // update overshoot increase notify
186   if( mOvershootIncreaseNotification )
187   {
188     self.RemovePropertyNotification( mOvershootIncreaseNotification );
189     mOvershootIncreaseNotification.Reset();
190   }
191   if( absOvershoot < MAX_OVERSHOOT_NOTIFY_AMOUNT )
192   {
193     float increaseStep = absOvershoot + OVERSHOOT_NOTIFY_STEP;
194     if( increaseStep > MAX_OVERSHOOT_NOTIFY_AMOUNT )
195     {
196       increaseStep = MAX_OVERSHOOT_NOTIFY_AMOUNT;
197     }
198     mOvershootIncreaseNotification = self.AddPropertyNotification( mOvershootProperty, OutsideCondition(-increaseStep, increaseStep) );
199     mOvershootIncreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
200     mOvershootIncreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
201   }
202
203   // update overshoot decrease notify
204   if( mOvershootDecreaseNotification )
205   {
206     self.RemovePropertyNotification( mOvershootDecreaseNotification );
207     mOvershootDecreaseNotification.Reset();
208   }
209   if( absOvershoot > MIN_OVERSHOOT_NOTIFY_AMOUNT )
210   {
211     float reduceStep = absOvershoot - OVERSHOOT_NOTIFY_STEP;
212     if( reduceStep < MIN_OVERSHOOT_NOTIFY_AMOUNT )
213     {
214       reduceStep = MIN_OVERSHOOT_NOTIFY_AMOUNT;
215     }
216     mOvershootDecreaseNotification = self.AddPropertyNotification( mOvershootProperty, InsideCondition(-reduceStep, reduceStep) );
217     mOvershootDecreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
218     mOvershootDecreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
219   }
220 }
221
222 void ScrollOvershootEffectRipple::SetOvershootEffectColor( const Vector4& color )
223 {
224   if(mOvershootOverlay)
225   {
226     mOvershootOverlay.SetColor(color);
227   }
228 }
229
230 void ScrollOvershootEffectRipple::UpdateVisibility( bool visible )
231 {
232   mOvershootOverlay.SetVisible(visible);
233   // make sure overshoot image is correctly placed
234   if( visible )
235   {
236     Actor self = mAttachedScrollView.Self();
237     if(mOvershoot > 0.0f)
238     {
239       // positive overshoot
240       const Vector3 size = mOvershootOverlay.GetCurrentSize();
241       Vector3 relativeOffset;
242       const Vector3 parentSize = self.GetCurrentSize();
243       if(IsVertical())
244       {
245         mOvershootOverlay.SetOrientation( Quaternion( Radian( 0.0f ), Vector3::ZAXIS ) );
246         mOvershootOverlay.SetSize(parentSize.width, GetBounceActorHeight(parentSize.width, mOvershootSize.height), size.depth);
247       }
248       else
249       {
250         mOvershootOverlay.SetOrientation( Quaternion( Radian( 1.5f * Math::PI ), Vector3::ZAXIS ) );
251         mOvershootOverlay.SetSize(parentSize.height, GetBounceActorHeight(parentSize.height, mOvershootSize.height), size.depth);
252         relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
253       }
254       mOvershootOverlay.SetPosition(relativeOffset * parentSize);
255     }
256     else
257     {
258       // negative overshoot
259       const Vector3 size = mOvershootOverlay.GetCurrentSize();
260       Vector3 relativeOffset;
261       const Vector3 parentSize = self.GetCurrentSize();
262       if(IsVertical())
263       {
264         mOvershootOverlay.SetOrientation( Quaternion( Radian( Math::PI ), Vector3::ZAXIS ) );
265         mOvershootOverlay.SetSize(parentSize.width, GetBounceActorHeight(parentSize.width, mOvershootSize.height), size.depth);
266         relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
267       }
268       else
269       {
270         mOvershootOverlay.SetOrientation( Quaternion( Radian( 0.5f * Math::PI ), Vector3::ZAXIS ) );
271         mOvershootOverlay.SetSize(parentSize.height, GetBounceActorHeight(parentSize.height, mOvershootSize.height), size.depth);
272         relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
273       }
274       mOvershootOverlay.SetPosition(relativeOffset * parentSize);
275     }
276   }
277 }
278
279 void ScrollOvershootEffectRipple::OnOvershootNotification(PropertyNotification& source)
280 {
281   Actor self = mAttachedScrollView.Self();
282   mOvershoot = self.GetProperty<float>(mOvershootProperty);
283   SetOvershoot(mOvershoot, false);
284   UpdatePropertyNotifications();
285 }
286
287 void ScrollOvershootEffectRipple::SetOvershoot(float amount, bool animate)
288 {
289   float absAmount = fabsf(amount);
290   bool animatingOn = absAmount > Math::MACHINE_EPSILON_0;
291   if( (animatingOn && (mAnimationStateFlags & AnimatingIn)) )
292   {
293     // trying to do what we are already doing
294     if( mAnimationStateFlags & AnimateBack )
295     {
296       mAnimationStateFlags &= ~AnimateBack;
297     }
298     return;
299   }
300   if( (!animatingOn && (mAnimationStateFlags & AnimatingOut)) )
301   {
302     // trying to do what we are already doing
303     return;
304   }
305   if( !animatingOn && (mAnimationStateFlags & AnimatingIn) )
306   {
307     // dont interrupt while animating on
308     mAnimationStateFlags |= AnimateBack;
309     return;
310   }
311
312   if( absAmount > Math::MACHINE_EPSILON_1 )
313   {
314     UpdateVisibility(true);
315   }
316
317   float overshootAnimationSpeed = mAttachedScrollView.Self().GetProperty<float>(Toolkit::Scrollable::Property::OVERSHOOT_ANIMATION_SPEED);
318
319   if( animate && overshootAnimationSpeed > Math::MACHINE_EPSILON_0 )
320   {
321     float currentOvershoot = fabsf( mOvershootOverlay.GetProperty( mEffectOvershootProperty ).Get<float>() );
322     float duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - currentOvershoot) : currentOvershoot) / overshootAnimationSpeed;
323
324     if( duration > Math::MACHINE_EPSILON_0 )
325     {
326       if(mScrollOvershootAnimation)
327       {
328         mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
329         mScrollOvershootAnimation.Stop();
330         mScrollOvershootAnimation.Reset();
331       }
332       mScrollOvershootAnimation = Animation::New(duration);
333       mScrollOvershootAnimation.FinishedSignal().Connect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
334       mScrollOvershootAnimation.AnimateTo( Property(mOvershootOverlay, mEffectOvershootProperty), amount, TimePeriod(duration) );
335       mScrollOvershootAnimation.Play();
336       mAnimationStateFlags = animatingOn ? AnimatingIn : AnimatingOut;
337     }
338   }
339   else
340   {
341     mOvershootOverlay.SetProperty( mEffectOvershootProperty, amount);
342   }
343 }
344
345 void ScrollOvershootEffectRipple::OnOvershootAnimFinished(Animation& animation)
346 {
347   bool animateOff = false;
348   if( mAnimationStateFlags & AnimatingOut )
349   {
350     // should now be offscreen
351     mOvershootOverlay.SetVisible(false);
352   }
353   if( (mAnimationStateFlags & AnimateBack) )
354   {
355     animateOff = true;
356   }
357   mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
358   mScrollOvershootAnimation.Stop();
359   mScrollOvershootAnimation.Reset();
360   mAnimationStateFlags = 0;
361   if( animateOff )
362   {
363     SetOvershoot(0.0f, true);
364   }
365 }
366
367 ScrollOvershootEffectRipplePtr ScrollOvershootEffectRipple::New( bool vertical, Scrollable& scrollable )
368 {
369   return new ScrollOvershootEffectRipple(vertical, scrollable);
370 }
371
372 } // namespace Internal
373
374 } // namespace Toolkit
375
376 } // namespace Dali