Remove dead slide effect code and incorrect ref-objects from scroll effects
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / scrollable / scroll-view / scroll-view-page-carousel-effect-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-view-page-carousel-effect-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <boost/bind.hpp>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-helper-functions.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace Internal
34 {
35
36 namespace // unnamed namespace
37 {
38
39 const float PAGE_SIZE_MULTIPLIER( 1.15f );
40
41 using namespace ScrollViewHelperFunctions;
42
43 /**
44  * ScrollPageCarouselEffectInfo
45  *
46  * Color constraint: adjusts the alpha of the page based on their parent page's position relative
47  * to the middle of the screen.
48  * When at middle of screen Alpha is 100% opacity.
49  * When outside the viewable area, the opacity is 0%.
50  *
51  * Position constraint: adjusts the position of the page based on their parent page's position
52  * relative to the middle of the screen.
53  * When at middle of the screen the position is not altered.
54  * When one screen away from middle the position is rotated as per expected in a 3D carousel.
55  */
56 class ScrollPageCarouselEffectInfo
57 {
58 public:
59
60   ScrollPageCarouselEffectInfo( const Vector2& positionToPageSizeRatio )
61   : mPositionToPageSizeRatio( positionToPageSizeRatio )
62   {
63   }
64
65   /**
66    * @param[in] current The current color of this Actor
67    * @param[in] pagePositionProperty The page's position.
68    * @param[in] scrollPositionProperty The scroll-view's position property (SCROLL_POSITION_PROPERTY_NAME)
69    * @param[in] scrollPositionMin The minimum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
70    * @param[in] scrollPositionMax The maximum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
71    * @param[in] pageSizeProperty The size of the page. (scrollView SIZE)
72    * @param[in] scrollWrap Whether scroll wrap has been enabled or not (SCROLL_WRAP_PROPERTY_NAME)
73    * @return The new color of this Actor.
74    */
75   Vector4 ColorConstraint(const Vector4& current,
76                           const PropertyInput& pagePositionProperty,
77                           const PropertyInput& scrollPositionProperty,
78                           const PropertyInput& scrollPositionMin,
79                           const PropertyInput& scrollPositionMax,
80                           const PropertyInput& pageSizeProperty,
81                           const PropertyInput& scrollWrap)
82   {
83     const Vector3& pagePosition = pagePositionProperty.GetVector3();
84     const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
85
86     // Get position of page.
87     Vector3 position = pagePosition + scrollPosition;
88
89     // short circuit: if we're looking straight on at the page.
90     if( IsStraightOnView( position ) )
91     {
92       return current;
93     }
94
95     const Vector3& pageSize = pageSizeProperty.GetVector3();
96
97     if( scrollWrap.GetBoolean() )
98     {
99       WrapPositionWithinDomain( position, pageSize, scrollPositionMin.GetVector3(), scrollPositionMax.GetVector3() );
100     }
101
102     // short circuit: for pages outside of view.
103     if( IsOutsideView( position, pageSize ) )
104     {
105       // note preserve color channels incase there is a shader/further constraint
106       // that wishes to do something with that information.
107       return Vector4(current.r, current.g, current.b, 0.0f);
108     }
109
110     Vector4 color( current );
111     Vector2 distance( position / pageSize * PAGE_SIZE_MULTIPLIER );
112     color.a = Clamp( 1.0f - distance.Length(), 0.0f, 1.0f );
113
114     return color;
115   }
116
117   /**
118    * @param[in] current The current position
119    * @param[in] pagePositionProperty The page's position.
120    * @param[in] scrollPositionProperty The scroll-view's position property (SCROLL_POSITION_PROPERTY_NAME)
121    * @param[in] scrollPositionMin The minimum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
122    * @param[in] scrollPositionMax The maximum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
123    * @param[in] pageSizeProperty The size of the page. (scrollView SIZE)
124    * @param[in] scrollWrap Whether scroll wrap has been enabled or not (SCROLL_WRAP_PROPERTY_NAME)
125    * @return The new position of this Actor.
126    */
127   Vector3 PositionConstraint(const Vector3& current,
128                              const PropertyInput& pagePositionProperty,
129                              const PropertyInput& scrollPositionProperty,
130                              const PropertyInput& scrollPositionMin,
131                              const PropertyInput& scrollPositionMax,
132                              const PropertyInput& pageSizeProperty,
133                              const PropertyInput& scrollWrap)
134   {
135     const Vector3& pagePosition = pagePositionProperty.GetVector3();
136     const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
137
138     // Get position of page.
139     Vector3 position = pagePosition + scrollPosition;
140
141     // short circuit: if we're looking straight on at the page.
142     if( IsStraightOnView( position ) )
143     {
144       return current + scrollPosition;
145     }
146
147     const Vector3& pageSize = pageSizeProperty.GetVector3();
148
149     if( scrollWrap.GetBoolean() )
150     {
151       WrapPositionWithinDomain( position, pageSize, scrollPositionMin.GetVector3(), scrollPositionMax.GetVector3() );
152     }
153
154     // short circuit: for pages outside of view.
155     if( IsOutsideView( position, pageSize ) )
156     {
157       // position actors at: scrollposition (Property) + pagePosition (Parent) + current (this)
158       // they will be invisible so doesn't have to be precise, just away from stage.
159       return current + scrollPosition;
160     }
161
162     Vector3 angle( position / pageSize * PAGE_SIZE_MULTIPLIER );
163
164     position.x = pageSize.x * sinf( angle.x );
165     position.y = pageSize.y * sinf( angle.y );
166
167     Vector2 zMovement( pageSize );
168     zMovement *= mPositionToPageSizeRatio;
169     position.z = - ( ( zMovement.x - ( zMovement.x * cos( angle.x ) ) ) + ( zMovement.y - ( zMovement.y * cos( angle.y ) ) ) );
170
171     return position;
172   }
173
174   const Vector2 mPositionToPageSizeRatio; ///< The page will move its position according to this ratio.
175 };
176
177 /**
178  * Helper: Applies the 3D scroll cube constraints to the child actor
179  *
180  * @param[in] scrollView The ScrollView containing the pages.
181  * @param[in] child The child to be affected with the 3D Effect.
182  * @param[in] info The effect info for the constraints
183  */
184 void ApplyScrollCubeConstraints(Toolkit::ScrollView scrollView,
185                                 Actor child,
186                                 ScrollPageCarouselEffectInfo& info)
187 {
188   // Apply constraints to this actor //
189   Constraint constraint;
190   constraint = Constraint::New<Vector4>( Actor::COLOR,
191                                          LocalSource(Actor::POSITION),
192                                          Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_FINAL_PROPERTY_NAME ) ),
193                                          Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
194                                          Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
195                                          Source(scrollView, Actor::SIZE ),
196                                          Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_WRAP_PROPERTY_NAME ) ),
197                                          boost::bind( &ScrollPageCarouselEffectInfo::ColorConstraint, info, _1, _2, _3, _4, _5, _6, _7) );
198
199   constraint.SetRemoveAction( Constraint::Discard );
200   child.ApplyConstraint( constraint );
201
202   constraint = Constraint::New<Vector3>( Actor::POSITION,
203                                          LocalSource(Actor::POSITION),
204                                          Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_FINAL_PROPERTY_NAME ) ),
205                                          Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
206                                          Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
207                                          Source(scrollView, Actor::SIZE ),
208                                          Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_WRAP_PROPERTY_NAME ) ),
209                                          boost::bind( &ScrollPageCarouselEffectInfo::PositionConstraint, info, _1, _2, _3, _4, _5, _6, _7) );
210
211   constraint.SetRemoveAction( Constraint::Discard );
212   child.ApplyConstraint( constraint );
213 }
214
215 } // unnamed namespace
216
217 ScrollViewPageCarouselEffect::ScrollViewPageCarouselEffect()
218 {
219
220 }
221
222 ScrollViewPageCarouselEffect::~ScrollViewPageCarouselEffect()
223 {
224 }
225
226 void ScrollViewPageCarouselEffect::ApplyToPage( Actor page, const Vector2& positionToPageSizeRatio )
227 {
228   ScrollPageCarouselEffectInfo info( positionToPageSizeRatio );
229
230   ApplyScrollCubeConstraints( GetScrollView(), page, info );
231 }
232
233 void ScrollViewPageCarouselEffect::OnAttach(Toolkit::ScrollView& scrollView)
234 {
235 }
236
237 void ScrollViewPageCarouselEffect::OnDetach(Toolkit::ScrollView& scrollView)
238 {
239 }
240
241 } // namespace Internal
242
243 } // namespace Toolkit
244
245 } // namespace Dali