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