Merge changes I26f3edb0,Iba714851 into new_text
[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 const Vector2 OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE( 720.0f, 42.0f );
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 )
35 {
36   return (width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD) ? OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height : OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height * 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 void ScrollOvershootIndicator::ClearOvershoot()
116 {
117   if(mEffectX)
118   {
119     mEffectX->SetOvershoot(0.0f);
120   }
121   if(mEffectY)
122   {
123     mEffectY->SetOvershoot(0.0f);
124   }
125 }
126
127 ScrollOvershootEffect::ScrollOvershootEffect( bool vertical ) :
128     mVertical(vertical)
129 {
130
131 }
132
133 bool ScrollOvershootEffect::IsVertical() const
134 {
135   return mVertical;
136 }
137
138 ScrollOvershootEffectRipple::ScrollOvershootEffectRipple( bool vertical, Scrollable& scrollable ) :
139     ScrollOvershootEffect( vertical ),
140     mAttachedScrollView(scrollable),
141     mOvershootProperty(Property::INVALID_INDEX),
142     mEffectOvershootProperty(Property::INVALID_INDEX),
143     mOvershoot(0.0f),
144     mAnimationStateFlags(0)
145 {
146   mOvershootOverlay = CreateBouncingEffectActor(mEffectOvershootProperty);
147   mOvershootOverlay.SetColor(mAttachedScrollView.GetOvershootEffectColor());
148   mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
149   mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
150   mOvershootOverlay.SetDrawMode(DrawMode::OVERLAY);
151   mOvershootOverlay.SetVisible(false);
152
153 }
154
155 void ScrollOvershootEffectRipple::Apply()
156 {
157   Actor self = mAttachedScrollView.Self();
158   mOvershootProperty = IsVertical() ? Toolkit::ScrollView::Property::OVERSHOOT_Y : Toolkit::ScrollView::Property::OVERSHOOT_X;
159
160   // make sure height is set, since we only create a constraint for image width
161   mOvershootOverlay.SetSize(OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.width, OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height);
162
163   mAttachedScrollView.AddOverlay(mOvershootOverlay);
164
165   UpdatePropertyNotifications();
166 }
167
168 void ScrollOvershootEffectRipple::Remove( Scrollable& scrollable )
169 {
170   if(mOvershootOverlay)
171   {
172     if(mOvershootIncreaseNotification)
173     {
174       scrollable.Self().RemovePropertyNotification(mOvershootIncreaseNotification);
175       mOvershootIncreaseNotification.Reset();
176     }
177     if(mOvershootDecreaseNotification)
178     {
179       scrollable.Self().RemovePropertyNotification(mOvershootDecreaseNotification);
180       mOvershootDecreaseNotification.Reset();
181     }
182     scrollable.RemoveOverlay(mOvershootOverlay);
183   }
184 }
185
186 void ScrollOvershootEffectRipple::Reset()
187 {
188   mOvershootOverlay.SetVisible(false);
189   mOvershootOverlay.SetProperty( mEffectOvershootProperty, 0.f);
190 }
191
192 void ScrollOvershootEffectRipple::UpdatePropertyNotifications()
193 {
194   float absOvershoot = fabsf(mOvershoot);
195
196   Actor self = mAttachedScrollView.Self();
197   // update overshoot increase notify
198   if( mOvershootIncreaseNotification )
199   {
200     self.RemovePropertyNotification( mOvershootIncreaseNotification );
201     mOvershootIncreaseNotification.Reset();
202   }
203   if( absOvershoot < MAX_OVERSHOOT_NOTIFY_AMOUNT )
204   {
205     float increaseStep = absOvershoot + OVERSHOOT_NOTIFY_STEP;
206     if( increaseStep > MAX_OVERSHOOT_NOTIFY_AMOUNT )
207     {
208       increaseStep = MAX_OVERSHOOT_NOTIFY_AMOUNT;
209     }
210     mOvershootIncreaseNotification = self.AddPropertyNotification( mOvershootProperty, OutsideCondition(-increaseStep, increaseStep) );
211     mOvershootIncreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
212     mOvershootIncreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
213   }
214
215   // update overshoot decrease notify
216   if( mOvershootDecreaseNotification )
217   {
218     self.RemovePropertyNotification( mOvershootDecreaseNotification );
219     mOvershootDecreaseNotification.Reset();
220   }
221   if( absOvershoot > MIN_OVERSHOOT_NOTIFY_AMOUNT )
222   {
223     float reduceStep = absOvershoot - OVERSHOOT_NOTIFY_STEP;
224     if( reduceStep < MIN_OVERSHOOT_NOTIFY_AMOUNT )
225     {
226       reduceStep = MIN_OVERSHOOT_NOTIFY_AMOUNT;
227     }
228     mOvershootDecreaseNotification = self.AddPropertyNotification( mOvershootProperty, InsideCondition(-reduceStep, reduceStep) );
229     mOvershootDecreaseNotification.SetNotifyMode(PropertyNotification::NotifyOnTrue);
230     mOvershootDecreaseNotification.NotifySignal().Connect(this, &ScrollOvershootEffectRipple::OnOvershootNotification);
231   }
232 }
233
234 void ScrollOvershootEffectRipple::SetOvershootEffectColor( const Vector4& color )
235 {
236   if(mOvershootOverlay)
237   {
238     mOvershootOverlay.SetColor(color);
239   }
240 }
241
242 void ScrollOvershootEffectRipple::UpdateVisibility( bool visible )
243 {
244   mOvershootOverlay.SetVisible(visible);
245   // make sure overshoot image is correctly placed
246   if( visible )
247   {
248     Actor self = mAttachedScrollView.Self();
249     if(mOvershoot > 0.0f)
250     {
251       // positive overshoot
252       const Vector3 size = mOvershootOverlay.GetCurrentSize();
253       Vector3 relativeOffset;
254       const Vector3 parentSize = self.GetCurrentSize();
255       if(IsVertical())
256       {
257         mOvershootOverlay.SetOrientation(Quaternion(0.0f, Vector3::ZAXIS));
258         mOvershootOverlay.SetSize(parentSize.width, GetBounceActorHeight(parentSize.width), size.depth);
259       }
260       else
261       {
262         mOvershootOverlay.SetOrientation(Quaternion(1.5f * Math::PI, Vector3::ZAXIS));
263         mOvershootOverlay.SetSize(parentSize.height, GetBounceActorHeight(parentSize.height), size.depth);
264         relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
265       }
266       mOvershootOverlay.SetPosition(relativeOffset * parentSize);
267     }
268     else
269     {
270       // negative overshoot
271       const Vector3 size = mOvershootOverlay.GetCurrentSize();
272       Vector3 relativeOffset;
273       const Vector3 parentSize = self.GetCurrentSize();
274       if(IsVertical())
275       {
276         mOvershootOverlay.SetOrientation(Quaternion(Math::PI, Vector3::ZAXIS));
277         mOvershootOverlay.SetSize(parentSize.width, GetBounceActorHeight(parentSize.width), size.depth);
278         relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
279       }
280       else
281       {
282         mOvershootOverlay.SetOrientation(Quaternion(0.5f * Math::PI, Vector3::ZAXIS));
283         mOvershootOverlay.SetSize(parentSize.height, GetBounceActorHeight(parentSize.height), size.depth);
284         relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
285       }
286       mOvershootOverlay.SetPosition(relativeOffset * parentSize);
287     }
288   }
289 }
290
291 void ScrollOvershootEffectRipple::OnOvershootNotification(PropertyNotification& source)
292 {
293   Actor self = mAttachedScrollView.Self();
294   mOvershoot = self.GetProperty<float>(mOvershootProperty);
295   SetOvershoot(mOvershoot, false);
296   UpdatePropertyNotifications();
297 }
298
299 void ScrollOvershootEffectRipple::SetOvershoot(float amount, bool animate)
300 {
301   float absAmount = fabsf(amount);
302   bool animatingOn = absAmount > Math::MACHINE_EPSILON_0;
303   if( (animatingOn && (mAnimationStateFlags & AnimatingIn)) )
304   {
305     // trying to do what we are already doing
306     if( mAnimationStateFlags & AnimateBack )
307     {
308       mAnimationStateFlags &= ~AnimateBack;
309     }
310     return;
311   }
312   if( (!animatingOn && (mAnimationStateFlags & AnimatingOut)) )
313   {
314     // trying to do what we are already doing
315     return;
316   }
317   if( !animatingOn && (mAnimationStateFlags & AnimatingIn) )
318   {
319     // dont interrupt while animating on
320     mAnimationStateFlags |= AnimateBack;
321     return;
322   }
323
324   if( absAmount > Math::MACHINE_EPSILON_1 )
325   {
326     UpdateVisibility(true);
327   }
328
329   float overshootAnimationSpeed = mAttachedScrollView.Self().GetProperty<float>(Toolkit::Scrollable::Property::OVERSHOOT_ANIMATION_SPEED);
330
331   if( animate && overshootAnimationSpeed > Math::MACHINE_EPSILON_0 )
332   {
333     float currentOvershoot = fabsf( mOvershootOverlay.GetProperty( mEffectOvershootProperty ).Get<float>() );
334     float duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - currentOvershoot) : currentOvershoot) / overshootAnimationSpeed;
335
336     if( duration > Math::MACHINE_EPSILON_0 )
337     {
338       if(mScrollOvershootAnimation)
339       {
340         mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
341         mScrollOvershootAnimation.Stop();
342         mScrollOvershootAnimation.Reset();
343       }
344       mScrollOvershootAnimation = Animation::New(duration);
345       mScrollOvershootAnimation.FinishedSignal().Connect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
346       mScrollOvershootAnimation.AnimateTo( Property(mOvershootOverlay, mEffectOvershootProperty), amount, TimePeriod(duration) );
347       mScrollOvershootAnimation.Play();
348       mAnimationStateFlags = animatingOn ? AnimatingIn : AnimatingOut;
349     }
350   }
351   else
352   {
353     mOvershootOverlay.SetProperty( mEffectOvershootProperty, amount);
354   }
355 }
356
357 void ScrollOvershootEffectRipple::OnOvershootAnimFinished(Animation& animation)
358 {
359   bool animateOff = false;
360   if( mAnimationStateFlags & AnimatingOut )
361   {
362     // should now be offscreen
363     mOvershootOverlay.SetVisible(false);
364   }
365   if( (mAnimationStateFlags & AnimateBack) )
366   {
367     animateOff = true;
368   }
369   mScrollOvershootAnimation.FinishedSignal().Disconnect( this, &ScrollOvershootEffectRipple::OnOvershootAnimFinished );
370   mScrollOvershootAnimation.Stop();
371   mScrollOvershootAnimation.Reset();
372   mAnimationStateFlags = 0;
373   if( animateOff )
374   {
375     SetOvershoot(0.0f, true);
376   }
377 }
378
379 ScrollOvershootEffectRipplePtr ScrollOvershootEffectRipple::New( bool vertical, Scrollable& scrollable )
380 {
381   return new ScrollOvershootEffectRipple(vertical, scrollable);
382 }
383
384 } // namespace Internal
385
386 } // namespace Toolkit
387
388 } // namespace Dali