2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-page-carousel-effect-impl.h>
22 #include <boost/bind.hpp>
25 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-helper-functions.h>
36 namespace // unnamed namespace
39 const float PAGE_SIZE_MULTIPLIER( 1.15f );
41 using namespace ScrollViewHelperFunctions;
44 * ScrollPageCarouselEffectInfo
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%.
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.
56 class ScrollPageCarouselEffectInfo : public Dali::RefObject
60 ScrollPageCarouselEffectInfo( const Vector2& positionToPageSizeRatio )
61 : mPositionToPageSizeRatio( positionToPageSizeRatio )
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.
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)
83 const Vector3& pagePosition = pagePositionProperty.GetVector3();
84 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
86 // Get position of page.
87 Vector3 position = pagePosition + scrollPosition;
89 // short circuit: if we're looking straight on at the page.
90 if( IsStraightOnView( position ) )
95 const Vector3& pageSize = pageSizeProperty.GetVector3();
97 if( scrollWrap.GetBoolean() )
99 WrapPositionWithinDomain( position, pageSize, scrollPositionMin.GetVector3(), scrollPositionMax.GetVector3() );
102 // short circuit: for pages outside of view.
103 if( IsOutsideView( position, pageSize ) )
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);
110 Vector4 color( current );
111 Vector2 distance( position / pageSize * PAGE_SIZE_MULTIPLIER );
112 color.a = Clamp( 1.0f - distance.Length(), 0.0f, 1.0f );
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.
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)
135 const Vector3& pagePosition = pagePositionProperty.GetVector3();
136 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
138 // Get position of page.
139 Vector3 position = pagePosition + scrollPosition;
141 // short circuit: if we're looking straight on at the page.
142 if( IsStraightOnView( position ) )
144 return current + scrollPosition;
147 const Vector3& pageSize = pageSizeProperty.GetVector3();
149 if( scrollWrap.GetBoolean() )
151 WrapPositionWithinDomain( position, pageSize, scrollPositionMin.GetVector3(), scrollPositionMax.GetVector3() );
154 // short circuit: for pages outside of view.
155 if( IsOutsideView( position, pageSize ) )
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;
162 Vector3 angle( position / pageSize * PAGE_SIZE_MULTIPLIER );
164 position.x = pageSize.x * sinf( angle.x );
165 position.y = pageSize.y * sinf( angle.y );
167 Vector2 zMovement( pageSize );
168 zMovement *= mPositionToPageSizeRatio;
169 position.z = - ( ( zMovement.x - ( zMovement.x * cos( angle.x ) ) ) + ( zMovement.y - ( zMovement.y * cos( angle.y ) ) ) );
174 const Vector2 mPositionToPageSizeRatio; ///< The page will move its position according to this ratio.
177 typedef IntrusivePtr<ScrollPageCarouselEffectInfo> ScrollPageCarouselEffectInfoPtr;
180 * Helper: Applies the 3D scroll cube constraints to the child actor
182 * @param[in] scrollView The ScrollView containing the pages.
183 * @param[in] child The child to be affected with the 3D Effect.
184 * @param[in] info The effect info for the constraints
186 void ApplyScrollCubeConstraints(Toolkit::ScrollView scrollView,
188 ScrollPageCarouselEffectInfoPtr info)
190 // Apply constraints to this actor //
191 Constraint constraint;
192 constraint = Constraint::New<Vector4>( Actor::COLOR,
193 LocalSource(Actor::POSITION),
194 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_FINAL_PROPERTY_NAME ) ),
195 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
196 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
197 Source(scrollView, Actor::SIZE ),
198 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_WRAP_PROPERTY_NAME ) ),
199 boost::bind( &ScrollPageCarouselEffectInfo::ColorConstraint, info, _1, _2, _3, _4, _5, _6, _7) );
201 constraint.SetRemoveAction( Constraint::Discard );
202 child.ApplyConstraint( constraint );
204 constraint = Constraint::New<Vector3>( Actor::POSITION,
205 LocalSource(Actor::POSITION),
206 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_FINAL_PROPERTY_NAME ) ),
207 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
208 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
209 Source(scrollView, Actor::SIZE ),
210 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_WRAP_PROPERTY_NAME ) ),
211 boost::bind( &ScrollPageCarouselEffectInfo::PositionConstraint, info, _1, _2, _3, _4, _5, _6, _7) );
213 constraint.SetRemoveAction( Constraint::Discard );
214 child.ApplyConstraint( constraint );
217 } // unnamed namespace
219 ScrollViewPageCarouselEffect::ScrollViewPageCarouselEffect()
224 ScrollViewPageCarouselEffect::~ScrollViewPageCarouselEffect()
228 void ScrollViewPageCarouselEffect::ApplyToPage( Actor page, const Vector2& positionToPageSizeRatio )
230 ScrollPageCarouselEffectInfoPtr info(new ScrollPageCarouselEffectInfo( positionToPageSizeRatio ) );
232 ApplyScrollCubeConstraints( GetScrollView(), page, info );
235 void ScrollViewPageCarouselEffect::OnAttach(Toolkit::ScrollView& scrollView)
239 void ScrollViewPageCarouselEffect::OnDetach(Toolkit::ScrollView& scrollView)
243 } // namespace Internal
245 } // namespace Toolkit