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.
18 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-effect-impl.h>
19 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-depth-effect-impl.h>
23 namespace // unnamed namespace
26 // constraints ////////////////////////////////////////////////////////////////
29 * Ramp equation is a variable easing equation
30 * of the form f(x) = |x|^y * x / |x|
32 * An exponent (y) of 1 will result in a fast (linear graph)
33 * Increasing the exponent will increase the Ease In
35 * @param[in] x mantissa (value from -1.0f to 1.0f)
36 * @param[in] y exponent (+ve value)
37 * @return The signed progress value from -1.0f to 1.0f
39 inline float RampFunction(float x, float y)
43 return -powf(fabsf(x), y);
46 return powf(fabsf(x), y);
50 * ScrollDepthScaleConstraint
52 * Scale constraint adjusts the scale of the Actors
53 * based on their parent page's position relative to the middle of the screen.
54 * When at middle of the screen the scale is not altered.
55 * As the page is moved away from the middle, Actors shrink in scale but at
56 * different rates defined by the RampFunction(x, f).
57 * All Actors eventually shrink to the same amount once at their destination.
59 struct ScrollDepthScaleConstraint
62 * The scaling constraint uses the amount the actors
63 * have moved in position to determine scaling extent.
64 * So essentially very similar calculations are used here.
66 * @param[in] positionExtent Controls how much Actor's X and Y
67 * position affects their alpha function's exponent value
68 * @param[in] offsetExtent exponent offset for X and Y scrolling
70 * @param[in] positionScale Changes the amount the page as a whole
72 * @param[in] scaleExtent Scale factor to reach when page is one whole
73 * page away from screen.
75 ScrollDepthScaleConstraint( Vector2 positionExtent = Vector2(0.5f, 1.0f),
76 Vector2 offsetExtent = Vector2(1.0f, 1.0f),
77 float positionScale = 1.5f,
78 float scaleExtent = 0.5f)
79 : mPositionExtent(positionExtent),
80 mOffsetExtent(offsetExtent),
81 mMaxExtent(positionExtent.x + positionExtent.y),
82 mPositionScale(positionScale),
83 mScaleExtent(scaleExtent)
88 * @param[in] current The current scale
89 * @param[in] pagePositionProperty The page's position.
90 * @param[in] scrollPositionProperty The scroll-view's position property (SCROLL_POSITION_PROPERTY_NAME)
91 * @param[in] scrollPositionMin The minimum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
92 * @param[in] scrollPositionMax The maximum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
93 * @param[in] pageSizeProperty The size of the page. (scrollView SIZE)
94 * @param[in] scrollWrap Whether scroll wrap has been enabled or not (SCROLL_WRAP_PROPERTY_NAME)
95 * @return The new scale of this Actor.
97 Vector3 operator()(const Vector3& currentScale,
98 const PropertyInput& currentPositionProperty,
99 const PropertyInput& pagePositionProperty,
100 const PropertyInput& scrollPositionProperty,
101 const PropertyInput& scrollPositionMin,
102 const PropertyInput& scrollPositionMax,
103 const PropertyInput& pageSizeProperty)
105 const Vector3& currentPosition = currentPositionProperty.GetVector3();
106 const Vector3& pagePosition = pagePositionProperty.GetVector3();
107 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
109 // Get position of page.
110 Vector3 position = pagePosition + scrollPosition;
112 // short circuit: for orthognal view.
113 if( (fabsf(position.x) < Math::MACHINE_EPSILON_1) && (fabsf(position.y) < Math::MACHINE_EPSILON_1) )
118 const Vector3& pageSize = pageSizeProperty.GetVector3();
120 // Don't have enough parameters, to provide Wrap mode (need a way of having 'uniforms' instead of scrollWrap.GetBoolean())
122 const Vector3& min = scrollPositionMin.GetVector3();
123 const Vector3& max = scrollPositionMax.GetVector3();
125 if(fabsf(min.x - max.x) > Math::MACHINE_EPSILON_1)
127 // WRAP X (based on the position of the right side)
128 position.x = WrapInDomain(position.x + pageSize.x, min.x, max.x) - pageSize.x;
131 if(fabsf(min.y - max.y) > Math::MACHINE_EPSILON_1)
133 // WRAP Y (based on the position of the bottom side)
134 position.y = WrapInDomain(position.y + pageSize.y, min.y, max.y) - pageSize.y;
137 // short circuit: for pages outside of view.
138 if( (fabsf(position.x) >= pageSize.x) || (fabsf(position.y) >= pageSize.y) )
143 // Calculate scale ////////////////////////////////////////////////////////
145 position.x /= pageSize.x;
146 position.y /= pageSize.y;
148 position *= mPositionScale;
150 Vector3 relCurrentPosition = currentPosition;
151 relCurrentPosition.x = relCurrentPosition.x / pageSize.x + 0.5f;
152 relCurrentPosition.y = relCurrentPosition.y / pageSize.y + 0.5f;
154 Vector3 extent( (relCurrentPosition.x * mPositionExtent.x + relCurrentPosition.y * mPositionExtent.y) * 1.0f,
155 (relCurrentPosition.x * mPositionExtent.y + relCurrentPosition.y * mPositionExtent.x) * 1.0f,
160 extent.x = mMaxExtent - extent.x; // Flip for right.
164 extent.y = mMaxExtent - extent.y; // Flip for bottom.
167 position.x = RampFunction(position.x, mOffsetExtent.x + extent.x);
168 position.y = RampFunction(position.y, mOffsetExtent.y + extent.y);
170 float f = mScaleExtent + cos(position.x * Math::PI_2) * cos(position.y * Math::PI_2) * (1.0f - mScaleExtent);
172 return currentScale * f;
175 const Vector2 mPositionExtent; ///< Determines how much of the Actor's X and Y position affects exponent value.
176 const Vector2 mOffsetExtent; ///< Offset for exponent value.
177 const float mMaxExtent; ///< Maximum possible extent (mOffsetExtent.x + mOffsetExtent.y)
178 const float mPositionScale; ///< Position scaling factor (spreads out pages, to avoid overlap)
179 const float mScaleExtent; ///< Scale factor when page is at furthest from
183 * ScrollDepthPositionConstraint
185 * Position constraint adjusts the position of the Actors
186 * based on their parent page's position relative to the middle of the screen.
187 * When at middle of the screen the position is not altered.
188 * As the page is moved away from the middle, Actors move away but at
189 * different rates defined by the RampFunction(x, f).
190 * All Actors eventually arrive at their destination at the same time.
192 struct ScrollDepthPositionConstraint
195 * @param [in] positionExtent Controls how much Actor's X and Y
196 * position affects their alpha function's exponent value
197 * @param [in] offsetExtent exponent offset for X and Y scrolling
199 * @param [in] positionScale Changes the amount the page as a whole
202 ScrollDepthPositionConstraint( Vector2 positionExtent = Vector2(0.5f, 1.0f),
203 Vector2 offsetExtent = Vector2(1.0f, 1.0f),
204 float positionScale = 1.5f )
205 : mPositionExtent(positionExtent),
206 mOffsetExtent(offsetExtent),
207 mMaxExtent(positionExtent.x + positionExtent.y),
208 mPositionScale(positionScale)
213 * @param[in] current The current position
214 * @param[in] pagePositionProperty The page's position.
215 * @param[in] scrollPositionProperty The scroll-view's position property (SCROLL_POSITION_PROPERTY_NAME)
216 * @param[in] scrollPositionMin The minimum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
217 * @param[in] scrollPositionMax The maximum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
218 * @param[in] pageSizeProperty The size of the page. (scrollView SIZE)
219 * @param[in] scrollWrap Whether scroll wrap has been enabled or not (SCROLL_WRAP_PROPERTY_NAME)
220 * @return The new position of this Actor.
222 Vector3 operator()(const Vector3& currentPosition,
223 const PropertyInput& pagePositionProperty,
224 const PropertyInput& scrollPositionProperty,
225 const PropertyInput& scrollPositionMin,
226 const PropertyInput& scrollPositionMax,
227 const PropertyInput& pageSizeProperty,
228 const PropertyInput& scrollWrap)
230 const Vector3& pagePosition = pagePositionProperty.GetVector3();
231 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
233 // Get position of page.
234 Vector3 position = pagePosition + scrollPosition;
236 // short circuit: for orthognal view.
237 if( (fabsf(position.x) < Math::MACHINE_EPSILON_1) && (fabsf(position.y) < Math::MACHINE_EPSILON_1) )
239 return currentPosition + scrollPosition;
242 const Vector3& pageSize = pageSizeProperty.GetVector3();
243 bool wrap = scrollWrap.GetBoolean();
247 const Vector3& min = scrollPositionMin.GetVector3();
248 const Vector3& max = scrollPositionMax.GetVector3();
250 if(fabsf(min.x - max.x) > Math::MACHINE_EPSILON_1)
252 // WRAP X (based on the position of the right side)
253 position.x = WrapInDomain(position.x + pageSize.x, min.x, max.x) - pageSize.x;
256 if(fabsf(min.y - max.y) > Math::MACHINE_EPSILON_1)
258 // WRAP Y (based on the position of the bottom side)
259 position.y = WrapInDomain(position.y + pageSize.y, min.y, max.y) - pageSize.y;
263 // short circuit: for pages outside of view.
264 if( (fabsf(position.x) >= pageSize.x) || (fabsf(position.y) >= pageSize.y) )
266 // position actors at: scrollposition (Property) + pagePosition (Parent) + current (this)
267 // they will be invisible so doesn't have to be precise, just away from stage.
268 return currentPosition + scrollPosition;
271 // Calculate position /////////////////////////////////////////////////////
273 position.x /= pageSize.x;
274 position.y /= pageSize.y;
276 position *= mPositionScale;
278 Vector3 finalPosition(currentPosition - pagePosition);
280 Vector3 relCurrentPosition = currentPosition;
281 relCurrentPosition.x = relCurrentPosition.x / pageSize.x + 0.5f;
282 relCurrentPosition.y = relCurrentPosition.y / pageSize.y + 0.5f;
284 Vector3 extent( (relCurrentPosition.x * mPositionExtent.x + relCurrentPosition.y * mPositionExtent.y) * 1.0f,
285 (relCurrentPosition.x * mPositionExtent.y + relCurrentPosition.y * mPositionExtent.x) * 1.0f,
290 extent.x = mMaxExtent - extent.x; // Flip for right.
294 extent.y = mMaxExtent - extent.y; // Flip for bottom.
297 position.x = RampFunction(position.x, mOffsetExtent.x + extent.x);
298 position.y = RampFunction(position.y, mOffsetExtent.y + extent.y);
300 finalPosition += pageSize * position;
302 return finalPosition;
305 const Vector2 mPositionExtent; ///< Determines how much of the Actor's X and Y position affects exponent value.
306 const Vector2 mOffsetExtent; ///< Offset for exponent value.
307 const float mMaxExtent; ///< Maximum possible extent (mOffsetExtent.x + mOffsetExtent.y)
308 const float mPositionScale; ///< Position scaling factor (spreads out pages, to avoid overlap)
312 * Applies the scroll depth constraints to the child actor
314 * @param[in] scrollView The ScrollView containing the pages.
315 * @param[in] child The child to be affected with the 3D Effect.
316 * @param[in] positionExtent Controls how much Actor's X and Y
317 * position affects their alpha function's exponent value
318 * @param[in] offsetExtent exponent offset for X and Y scrolling
320 * @param[in] positionScale Changes the amount the page as a whole
322 * @param[in] scaleExtent Scale factor to reach when page is one whole
323 * page away from screen.
325 void ApplyScrollDepthConstraints(Toolkit::ScrollView scrollView,
327 const Vector2& positionExtent,
328 const Vector2& offsetExtent,
333 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
334 LocalSource(Actor::POSITION),
335 ParentSource(Actor::POSITION),
336 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
337 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
338 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
339 Source(scrollView, Actor::SIZE ),
340 ScrollDepthScaleConstraint( positionExtent, offsetExtent, positionScale, scaleExtent ) );
341 constraint.SetRemoveAction( Constraint::Discard );
342 child.ApplyConstraint( constraint );
344 // Position Constraint (apply last as other constraints use Actor::POSITION as a function input)
345 constraint = Constraint::New<Vector3>( Actor::POSITION,
346 ParentSource(Actor::POSITION),
347 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
348 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
349 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
350 Source(scrollView, Actor::SIZE ),
351 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_WRAP_PROPERTY_NAME ) ),
352 ScrollDepthPositionConstraint( positionExtent, offsetExtent, positionScale ) );
354 constraint.SetRemoveAction( Constraint::Discard );
355 child.ApplyConstraint( constraint );
358 } // unnamed namespace
369 ScrollViewDepthEffect::ScrollViewDepthEffect()
374 ScrollViewDepthEffect::~ScrollViewDepthEffect()
378 void ScrollViewDepthEffect::ApplyToActor(Actor child,
379 const Vector2& positionExtent,
380 const Vector2& offsetExtent,
384 ApplyScrollDepthConstraints( GetScrollView(), child, positionExtent, offsetExtent, positionScale, scaleExtent );
387 void ScrollViewDepthEffect::OnAttach(Toolkit::ScrollView& scrollView)
391 void ScrollViewDepthEffect::OnDetach(Toolkit::ScrollView& scrollView)
395 } // namespace Internal
397 } // namespace Toolkit