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