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-cube-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 using namespace ScrollViewHelperFunctions;
42 * ScrollPageCubeEffectInfo
44 * Rotate constraint: adjusts the angle of the page based on its position relative to the middle of
46 * When at middle of screen Angles on X and Y Axes is 0.
47 * When one screen away from the middle Angle is 90 degrees (pi/2)
49 * Color constraint: adjusts the alpha of the page based on their parent page's position relative
50 * to the middle of the screen.
51 * When at middle of screen Alpha is 100% opacity.
52 * When outside the viewable area, the opacity is 0%.
54 * Position constraint: adjusts the position of the page based on their parent page's position
55 * relative to the middle of the screen.
56 * When at middle of the screen the position is not altered.
57 * When one screen away from middle the position is rotated as per expected in a 3D inner cube.
59 class ScrollPageCubeEffectInfo : public Dali::RefObject
63 ScrollPageCubeEffectInfo( const Vector2& angleSwing )
64 : mAngleSwing(angleSwing)
69 * @param[in] current The current orientation of this Actor
70 * @param[in] pagePositionProperty The page's position.
71 * @param[in] scrollPositionProperty The scroll-view's position property (SCROLL_POSITION_PROPERTY_NAME)
72 * @param[in] scrollPositionMin The minimum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
73 * @param[in] scrollPositionMax The maximum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
74 * @param[in] pageSizeProperty The size of the page. (scrollView SIZE)
75 * @param[in] scrollWrap Whether scroll wrap has been enabled or not (SCROLL_WRAP_PROPERTY_NAME)
76 * @return The new orientation of this Actor.
78 Quaternion RotationConstraint(const Quaternion& current,
79 const PropertyInput& pagePositionProperty,
80 const PropertyInput& scrollPositionProperty,
81 const PropertyInput& scrollPositionMin,
82 const PropertyInput& scrollPositionMax,
83 const PropertyInput& pageSizeProperty,
84 const PropertyInput& scrollWrap)
86 const Vector3& pagePosition = pagePositionProperty.GetVector3();
87 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
89 // Get position of page.
90 Vector3 position = pagePosition + scrollPosition;
92 // short circuit: if we're looking straight on at the page.
93 if( IsStraightOnView( position ) )
98 const Vector3& pageSize = pageSizeProperty.GetVector3();
100 if( scrollWrap.GetBoolean() )
102 WrapPositionWithinDomain( position, pageSize, scrollPositionMin.GetVector3(), scrollPositionMax.GetVector3() );
105 // short circuit: for pages outside of view.
106 if( IsOutsideView( position, pageSize ) )
111 // Our target is a 90 degree (PI/2) rotation per page, so calculate the angle we should be rotate
112 // our page by calculating the amount we've moved as a fraction of the total size of the page.
113 Vector2 angle( position / pageSize * Dali::Math::PI_2 );
115 Quaternion rotation = Quaternion( -angle.x * mAngleSwing.x, Vector3::YAXIS ) *
116 Quaternion( angle.y * mAngleSwing.y, Vector3::XAXIS ) *
123 * @param[in] current The current color of this Actor
124 * @param[in] pagePositionProperty The page's position.
125 * @param[in] scrollPositionProperty The scroll-view's position property (SCROLL_POSITION_PROPERTY_NAME)
126 * @param[in] scrollPositionMin The minimum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
127 * @param[in] scrollPositionMax The maximum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
128 * @param[in] pageSizeProperty The size of the page. (scrollView SIZE)
129 * @param[in] scrollWrap Whether scroll wrap has been enabled or not (SCROLL_WRAP_PROPERTY_NAME)
130 * @return The new color of this Actor.
132 Vector4 ColorConstraint(const Vector4& current,
133 const PropertyInput& pagePositionProperty,
134 const PropertyInput& scrollPositionProperty,
135 const PropertyInput& scrollPositionMin,
136 const PropertyInput& scrollPositionMax,
137 const PropertyInput& pageSizeProperty,
138 const PropertyInput& scrollWrap)
140 const Vector3& pagePosition = pagePositionProperty.GetVector3();
141 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
143 // Get position of page.
144 Vector3 position = pagePosition + scrollPosition;
146 // short circuit: if we're looking straight on at the page.
147 if( IsStraightOnView( position ) )
152 const Vector3& pageSize = pageSizeProperty.GetVector3();
154 if( scrollWrap.GetBoolean() )
156 WrapPositionWithinDomain( position, pageSize, scrollPositionMin.GetVector3(), scrollPositionMax.GetVector3() );
159 // short circuit: for pages outside of view.
160 if( IsOutsideView( position, pageSize ) )
162 // note preserve color channels incase there is a shader/further constraint
163 // that wishes to do something with that information.
164 return Vector4(current.r, current.g, current.b, 0.0f);
167 // Calculate the distance of this page from our view and ensure it falls within the appropriate
169 // If it does not, then the opacity is set to 0.0f.
170 position.x /= pageSize.width;
171 position.y /= pageSize.height;
172 float distanceFactor = sqrt( position.x * position.x + position.y * position.y );
174 if ( distanceFactor > 1.0f )
176 return Vector4(current.r, current.g, current.b, 0.0f);
183 * @param[in] current The current position
184 * @param[in] pagePositionProperty The page's position.
185 * @param[in] scrollPositionProperty The scroll-view's position property (SCROLL_POSITION_PROPERTY_NAME)
186 * @param[in] scrollPositionMin The minimum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
187 * @param[in] scrollPositionMax The maximum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
188 * @param[in] pageSizeProperty The size of the page. (scrollView SIZE)
189 * @param[in] scrollWrap Whether scroll wrap has been enabled or not (SCROLL_WRAP_PROPERTY_NAME)
190 * @return The new position of this Actor.
192 Vector3 PositionConstraint(const Vector3& current,
193 const PropertyInput& pagePositionProperty,
194 const PropertyInput& scrollPositionProperty,
195 const PropertyInput& scrollPositionMin,
196 const PropertyInput& scrollPositionMax,
197 const PropertyInput& pageSizeProperty,
198 const PropertyInput& scrollWrap)
200 const Vector3& pagePosition = pagePositionProperty.GetVector3();
201 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
203 // Get position of page.
204 Vector3 position = pagePosition + scrollPosition;
206 // short circuit: if we're looking straight on at the page.
207 if( IsStraightOnView( position ) )
209 return current + scrollPosition;
212 const Vector3& pageSize = pageSizeProperty.GetVector3();
214 if( scrollWrap.GetBoolean() )
216 WrapPositionWithinDomain( position, pageSize, scrollPositionMin.GetVector3(), scrollPositionMax.GetVector3() );
219 // short circuit: for pages outside of view.
220 if( IsOutsideView( position, pageSize ) )
222 // position actors at: scrollposition (Property) + pagePosition (Parent) + current (this)
223 // they will be invisible so doesn't have to be precise, just away from stage.
224 return current + scrollPosition;
227 // Our target when scrolling is moving from the origin to the following points around a curve:
228 // Right To Left: (-pageWidth, 0, pageWidth)
229 // Left To Right: ( pageWidth, 0, pageWidth)
230 // Down To Up: ( 0, -pageHeight, pageWidth)
231 // Up To Down: ( 0, pageHeight, pageWidth)
233 Vector2 angle( position / pageSize * Dali::Math::PI_2 );
234 Vector2 radius( pageSize * 0.5 );
236 position.x = radius.x * sin( angle.x );
237 position.y = radius.y * sin( angle.y );
238 position.z = ( radius.x - ( radius.x * cos( angle.x ) ) ) + ( radius.y - ( radius.y * cos( angle.y ) ) );
243 Vector2 mAngleSwing; ///< Maximum amount in X and Y axes to rotate.
246 typedef IntrusivePtr<ScrollPageCubeEffectInfo> ScrollPageCubeEffectInfoPtr;
249 * Helper: Applies the 3D scroll cube constraints to the child actor
251 * @param[in] scrollView The ScrollView containing the pages.
252 * @param[in] child The child to be affected with the 3D Effect.
253 * @param[in] info The effect info for the constraints
255 void ApplyScrollCubeConstraints(Toolkit::ScrollView scrollView,
257 ScrollPageCubeEffectInfoPtr info)
259 // Apply constraints to this actor //
260 Constraint constraint;
261 constraint = Constraint::New<Quaternion>( Actor::ROTATION,
262 LocalSource(Actor::POSITION),
263 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_FINAL_PROPERTY_NAME ) ),
264 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
265 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
266 Source(scrollView, Actor::SIZE ),
267 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_WRAP_PROPERTY_NAME ) ),
268 boost::bind( &ScrollPageCubeEffectInfo::RotationConstraint, info, _1, _2, _3, _4, _5, _6, _7) );
270 constraint.SetRemoveAction( Constraint::Discard );
271 child.ApplyConstraint( constraint );
273 constraint = Constraint::New<Vector4>( Actor::COLOR,
274 LocalSource(Actor::POSITION),
275 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_FINAL_PROPERTY_NAME ) ),
276 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
277 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
278 Source(scrollView, Actor::SIZE ),
279 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_WRAP_PROPERTY_NAME ) ),
280 boost::bind( &ScrollPageCubeEffectInfo::ColorConstraint, info, _1, _2, _3, _4, _5, _6, _7) );
282 constraint.SetRemoveAction( Constraint::Discard );
283 child.ApplyConstraint( constraint );
285 constraint = Constraint::New<Vector3>( Actor::POSITION,
286 LocalSource(Actor::POSITION),
287 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_FINAL_PROPERTY_NAME ) ),
288 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
289 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
290 Source(scrollView, Actor::SIZE ),
291 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_WRAP_PROPERTY_NAME ) ),
292 boost::bind( &ScrollPageCubeEffectInfo::PositionConstraint, info, _1, _2, _3, _4, _5, _6, _7) );
294 constraint.SetRemoveAction( Constraint::Discard );
295 child.ApplyConstraint( constraint );
298 } // unnamed namespace
300 ScrollViewPageCubeEffect::ScrollViewPageCubeEffect()
305 ScrollViewPageCubeEffect::~ScrollViewPageCubeEffect()
309 void ScrollViewPageCubeEffect::ApplyToPage( Actor page, const Vector2& angleSwing )
311 ScrollPageCubeEffectInfoPtr info(new ScrollPageCubeEffectInfo( angleSwing ));
313 ApplyScrollCubeConstraints( GetScrollView(), page, info );
316 void ScrollViewPageCubeEffect::OnAttach(Toolkit::ScrollView& scrollView)
320 void ScrollViewPageCubeEffect::OnDetach(Toolkit::ScrollView& scrollView)
324 } // namespace Internal
326 } // namespace Toolkit