Merge "Use new animatable registered properties in ScrollView" into tizen
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scrollable / scrollable-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 // EXTERNAL INCLUDES
19 #include <dali/public-api/object/type-registry.h>
20 #include <dali/public-api/object/type-registry-helper.h>
21
22 // INTERNAL INCLUDES
23 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
24 #include <dali-toolkit/internal/controls/scroll-component/scroll-bar-internal-impl.h>
25
26 using namespace Dali;
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39
40 BaseHandle Create()
41 {
42   // empty handle as we cannot create Scrollable (but type registered for scroll signal)
43   return BaseHandle();
44 }
45
46 // Setup properties, signals and actions using the type-registry.
47 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Scrollable, Toolkit::Control, Create );
48
49 DALI_PROPERTY_REGISTRATION( Scrollable, "overshoot-effect-color",    VECTOR4, OVERSHOOT_EFFECT_COLOR    )
50 DALI_PROPERTY_REGISTRATION( Scrollable, "overshoot-animation-speed", FLOAT,   OVERSHOOT_ANIMATION_SPEED )
51
52 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Scrollable, "scroll-relative-position", VECTOR3, SCROLL_RELATIVE_POSITION)
53 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Scrollable, "scroll-position-min",      VECTOR3, SCROLL_POSITION_MIN)
54 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Scrollable, "scroll-position-max",      VECTOR3, SCROLL_POSITION_MAX)
55 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Scrollable, "scroll-direction",         VECTOR3, SCROLL_DIRECTION)
56 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Scrollable, "can-scroll-vertical",      BOOLEAN, CAN_SCROLL_VERTICAL)
57 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Scrollable, "can-scroll-horizontal",    BOOLEAN, CAN_SCROLL_HORIZONTAL)
58
59 DALI_SIGNAL_REGISTRATION(   Scrollable, "scroll-started",                     SIGNAL_SCROLL_STARTED     )
60 DALI_SIGNAL_REGISTRATION(   Scrollable, "scroll-completed",                   SIGNAL_SCROLL_COMPLETED   )
61 DALI_SIGNAL_REGISTRATION(   Scrollable, "scroll-updated",                     SIGNAL_SCROLL_UPDATED     )
62
63 DALI_TYPE_REGISTRATION_END()
64
65 const Vector4 DEFAULT_OVERSHOOT_COLOUR(0.0f, 0.64f, 0.85f, 0.25f);
66 const float DEFAULT_OVERSHOOT_ANIMATION_SPEED(120.0f); // 120 pixels per second
67
68 }
69
70 ///////////////////////////////////////////////////////////////////////////////////////////////////
71 // Scrollable
72 ///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 // Scrollable controls are not layout containers so they dont need size negotiation..
75 // we dont want size negotiation while scrolling if we can avoid it
76 Scrollable::Scrollable()
77 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS | NO_SIZE_NEGOTIATION ) ),
78   mOvershootEffectColor(  DEFAULT_OVERSHOOT_COLOUR ),
79   mOvershootAnimationSpeed ( DEFAULT_OVERSHOOT_ANIMATION_SPEED ),
80   mOvershootEnabled(false)
81 {
82 }
83
84 Scrollable::~Scrollable()
85 {
86   // Clear scroll components, forces their destruction before Scrollable is destroyed.
87   mComponents.clear();
88 }
89
90 bool Scrollable::IsScrollComponentEnabled(Toolkit::Scrollable::ScrollComponentType type) const
91 {
92   if(type == Toolkit::Scrollable::OvershootIndicator)
93   {
94     return mOvershootEnabled;
95   }
96   return (mComponents.find(type) != mComponents.end());
97 }
98
99 void Scrollable::EnableScrollComponent(Toolkit::Scrollable::ScrollComponentType type)
100 {
101   if(type == Toolkit::Scrollable::OvershootIndicator)
102   {
103     if( !mOvershootEnabled )
104     {
105       SetOvershootEnabled(true);
106       mOvershootEnabled = true;
107     }
108     return;
109   }
110   if( mComponents.find(type) == mComponents.end() )
111   {
112     // Create ScrollComponent
113     Toolkit::Scrollable scrollable = Toolkit::Scrollable::DownCast(Self());
114     Toolkit::ScrollComponent scrollComponent = NewScrollComponent(scrollable, type);
115     Toolkit::ScrollComponentImpl& component = static_cast<Toolkit::ScrollComponentImpl&>(scrollComponent.GetImplementation());
116     ScrollComponentPtr componentPtr(&component);
117
118     mComponents[type] = componentPtr;
119   }
120 }
121
122 void Scrollable::DisableScrollComponent(Toolkit::Scrollable::ScrollComponentType type)
123 {
124   if(type == Toolkit::Scrollable::OvershootIndicator)
125   {
126     if( mOvershootEnabled )
127     {
128       SetOvershootEnabled(false);
129       mOvershootEnabled = false;
130     }
131     return;
132   }
133   ComponentIter pair = mComponents.find( type );
134
135   if( mComponents.end() != pair )
136   {
137     ScrollComponentPtr component = pair->second;
138
139     // Disconnect the scroll component first.
140     component->OnDisconnect();
141
142     // Destroy ScrollComponent.
143     mComponents.erase( type );
144   }
145 }
146
147 Vector4 Scrollable::GetOvershootEffectColor() const
148 {
149   return mOvershootEffectColor;
150 };
151
152 void Scrollable::SetOvershootAnimationSpeed( float pixelsPerSecond )
153 {
154   mOvershootAnimationSpeed = pixelsPerSecond;
155 }
156
157 float Scrollable::GetOvershootAnimationSpeed() const
158 {
159   return mOvershootAnimationSpeed;
160 };
161
162 Toolkit::Scrollable::ScrollStartedSignalType& Scrollable::ScrollStartedSignal()
163 {
164   return mScrollStartedSignal;
165 }
166
167 Toolkit::Scrollable::ScrollUpdatedSignalType& Scrollable::ScrollUpdatedSignal()
168 {
169   return mScrollUpdatedSignal;
170 }
171
172 Toolkit::Scrollable::ScrollCompletedSignalType& Scrollable::ScrollCompletedSignal()
173 {
174   return mScrollCompletedSignal;
175 }
176
177 bool Scrollable::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
178 {
179   Dali::BaseHandle handle( object );
180
181   bool connected( true );
182   Toolkit::Scrollable scrollable = Toolkit::Scrollable::DownCast( handle );
183
184   if( 0 == strcmp( signalName.c_str(), SIGNAL_SCROLL_STARTED ) )
185   {
186     scrollable.ScrollStartedSignal().Connect( tracker, functor );
187   }
188   else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCROLL_UPDATED ) )
189   {
190     scrollable.ScrollUpdatedSignal().Connect( tracker, functor );
191   }
192   else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCROLL_COMPLETED ) )
193   {
194     scrollable.ScrollCompletedSignal().Connect( tracker, functor );
195   }
196   else
197   {
198     // signalName does not match any signal
199     connected = false;
200   }
201
202   return connected;
203 }
204
205 void Scrollable::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
206 {
207   Toolkit::Scrollable scrollable = Toolkit::Scrollable::DownCast( Dali::BaseHandle( object ) );
208
209   if( scrollable )
210   {
211     Scrollable& scrollableImpl( GetImpl( scrollable ) );
212     switch( index )
213     {
214       case Toolkit::Scrollable::Property::OVERSHOOT_EFFECT_COLOR:
215       {
216         scrollableImpl.SetOvershootEffectColor( value.Get<Vector4>() );
217         break;
218       }
219       case Toolkit::Scrollable::Property::OVERSHOOT_ANIMATION_SPEED:
220       {
221         scrollableImpl.SetOvershootAnimationSpeed( value.Get<float>() );
222         break;
223       }
224     }
225   }
226 }
227
228 Property::Value Scrollable::GetProperty( BaseObject* object, Property::Index index )
229 {
230   Property::Value value;
231
232   Toolkit::Scrollable scrollable = Toolkit::Scrollable::DownCast( Dali::BaseHandle( object ) );
233
234   if( scrollable )
235   {
236     Scrollable& scrollableImpl( GetImpl( scrollable ) );
237     switch( index )
238     {
239       case Toolkit::Scrollable::Property::OVERSHOOT_EFFECT_COLOR:
240       {
241         value = scrollableImpl.GetOvershootEffectColor();
242         break;
243       }
244       case Toolkit::Scrollable::Property::OVERSHOOT_ANIMATION_SPEED:
245       {
246         value = scrollableImpl.GetOvershootAnimationSpeed();
247         break;
248       }
249     }
250   }
251
252   return value;
253 }
254
255 Toolkit::ScrollComponent Scrollable::NewScrollComponent(Toolkit::Scrollable& scrollable, Toolkit::Scrollable::ScrollComponentType type)
256 {
257   Toolkit::ScrollComponent instance;
258
259   switch(type)
260   {
261     case Toolkit::Scrollable::VerticalScrollBar:
262     {
263       instance = static_cast<Toolkit::ScrollComponent>(Toolkit::ScrollBarInternal::New(scrollable, true));
264       break;
265     }
266     case Toolkit::Scrollable::HorizontalScrollBar:
267     {
268       instance = static_cast<Toolkit::ScrollComponent>(Toolkit::ScrollBarInternal::New(scrollable, false));
269       break;
270     }
271     case Toolkit::Scrollable::OvershootIndicator:
272     {
273       DALI_ASSERT_ALWAYS(!"Unrecognized component type");
274       break;
275     }
276   }
277
278   return instance;
279 }
280
281 } // namespace Internal
282
283 } // namespace Toolkit
284
285 } // namespace Dali