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();
119 // Don't have enough parameters, to provide Wrap mode (need a way of having 'uniforms' instead of scrollWrap.GetBoolean())
120 const bool wrap = true;
124 const Vector3& min = scrollPositionMin.GetVector3();
125 const Vector3& max = scrollPositionMax.GetVector3();
127 if(fabsf(min.x - max.x) > Math::MACHINE_EPSILON_1)
129 // WRAP X (based on the position of the right side)
130 position.x = WrapInDomain(position.x + pageSize.x, min.x, max.x) - pageSize.x;
133 if(fabsf(min.y - max.y) > Math::MACHINE_EPSILON_1)
135 // WRAP Y (based on the position of the bottom side)
136 position.y = WrapInDomain(position.y + pageSize.y, min.y, max.y) - pageSize.y;
140 // short circuit: for pages outside of view.
141 if( (fabsf(position.x) >= pageSize.x) || (fabsf(position.y) >= pageSize.y) )
146 // Calculate scale ////////////////////////////////////////////////////////
148 position.x /= pageSize.x;
149 position.y /= pageSize.y;
151 position *= mPositionScale;
153 Vector3 relCurrentPosition = currentPosition;
154 relCurrentPosition.x = relCurrentPosition.x / pageSize.x + 0.5f;
155 relCurrentPosition.y = relCurrentPosition.y / pageSize.y + 0.5f;
157 Vector3 extent( (relCurrentPosition.x * mPositionExtent.x + relCurrentPosition.y * mPositionExtent.y) * 1.0f,
158 (relCurrentPosition.x * mPositionExtent.y + relCurrentPosition.y * mPositionExtent.x) * 1.0f,
163 extent.x = mMaxExtent - extent.x; // Flip for right.
167 extent.y = mMaxExtent - extent.y; // Flip for bottom.
170 position.x = RampFunction(position.x, mOffsetExtent.x + extent.x);
171 position.y = RampFunction(position.y, mOffsetExtent.y + extent.y);
173 float f = mScaleExtent + cos(position.x * Math::PI_2) * cos(position.y * Math::PI_2) * (1.0f - mScaleExtent);
175 return currentScale * f;
178 const Vector2 mPositionExtent; ///< Determines how much of the Actor's X and Y position affects exponent value.
179 const Vector2 mOffsetExtent; ///< Offset for exponent value.
180 const float mMaxExtent; ///< Maximum possible extent (mOffsetExtent.x + mOffsetExtent.y)
181 const float mPositionScale; ///< Position scaling factor (spreads out pages, to avoid overlap)
182 const float mScaleExtent; ///< Scale factor when page is at furthest from
186 * ScrollDepthPositionConstraint
188 * Position constraint adjusts the position of the Actors
189 * based on their parent page's position relative to the middle of the screen.
190 * When at middle of the screen the position is not altered.
191 * As the page is moved away from the middle, Actors move away but at
192 * different rates defined by the RampFunction(x, f).
193 * All Actors eventually arrive at their destination at the same time.
195 struct ScrollDepthPositionConstraint
198 * @param [in] positionExtent Controls how much Actor's X and Y
199 * position affects their alpha function's exponent value
200 * @param [in] offsetExtent exponent offset for X and Y scrolling
202 * @param [in] positionScale Changes the amount the page as a whole
205 ScrollDepthPositionConstraint( Vector2 positionExtent = Vector2(0.5f, 1.0f),
206 Vector2 offsetExtent = Vector2(1.0f, 1.0f),
207 float positionScale = 1.5f )
208 : mPositionExtent(positionExtent),
209 mOffsetExtent(offsetExtent),
210 mMaxExtent(positionExtent.x + positionExtent.y),
211 mPositionScale(positionScale)
216 * @param[in] current The current position
217 * @param[in] pagePositionProperty The page's position.
218 * @param[in] scrollPositionProperty The scroll-view's position property (SCROLL_POSITION_PROPERTY_NAME)
219 * @param[in] scrollPositionMin The minimum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
220 * @param[in] scrollPositionMax The maximum extent of this scroll domain. (SCROLL_POSITION_MIN_PROPERTY_NAME)
221 * @param[in] pageSizeProperty The size of the page. (scrollView SIZE)
222 * @param[in] scrollWrap Whether scroll wrap has been enabled or not (SCROLL_WRAP_PROPERTY_NAME)
223 * @return The new position of this Actor.
225 Vector3 operator()(const Vector3& currentPosition,
226 const PropertyInput& pagePositionProperty,
227 const PropertyInput& scrollPositionProperty,
228 const PropertyInput& scrollPositionMin,
229 const PropertyInput& scrollPositionMax,
230 const PropertyInput& pageSizeProperty,
231 const PropertyInput& scrollWrap)
233 const Vector3& pagePosition = pagePositionProperty.GetVector3();
234 const Vector3& scrollPosition = scrollPositionProperty.GetVector3();
236 // Get position of page.
237 Vector3 position = pagePosition + scrollPosition;
239 // short circuit: for orthognal view.
240 if( (fabsf(position.x) < Math::MACHINE_EPSILON_1) && (fabsf(position.y) < Math::MACHINE_EPSILON_1) )
242 return currentPosition + scrollPosition;
245 const Vector3& pageSize = pageSizeProperty.GetVector3();
246 bool wrap = scrollWrap.GetBoolean();
250 const Vector3& min = scrollPositionMin.GetVector3();
251 const Vector3& max = scrollPositionMax.GetVector3();
253 if(fabsf(min.x - max.x) > Math::MACHINE_EPSILON_1)
255 // WRAP X (based on the position of the right side)
256 position.x = WrapInDomain(position.x + pageSize.x, min.x, max.x) - pageSize.x;
259 if(fabsf(min.y - max.y) > Math::MACHINE_EPSILON_1)
261 // WRAP Y (based on the position of the bottom side)
262 position.y = WrapInDomain(position.y + pageSize.y, min.y, max.y) - pageSize.y;
266 // short circuit: for pages outside of view.
267 if( (fabsf(position.x) >= pageSize.x) || (fabsf(position.y) >= pageSize.y) )
269 // position actors at: scrollposition (Property) + pagePosition (Parent) + current (this)
270 // they will be invisible so doesn't have to be precise, just away from stage.
271 return currentPosition + scrollPosition;
274 // Calculate position /////////////////////////////////////////////////////
276 position.x /= pageSize.x;
277 position.y /= pageSize.y;
279 position *= mPositionScale;
281 Vector3 finalPosition(currentPosition - pagePosition);
283 Vector3 relCurrentPosition = currentPosition;
284 relCurrentPosition.x = relCurrentPosition.x / pageSize.x + 0.5f;
285 relCurrentPosition.y = relCurrentPosition.y / pageSize.y + 0.5f;
287 Vector3 extent( (relCurrentPosition.x * mPositionExtent.x + relCurrentPosition.y * mPositionExtent.y) * 1.0f,
288 (relCurrentPosition.x * mPositionExtent.y + relCurrentPosition.y * mPositionExtent.x) * 1.0f,
293 extent.x = mMaxExtent - extent.x; // Flip for right.
297 extent.y = mMaxExtent - extent.y; // Flip for bottom.
300 position.x = RampFunction(position.x, mOffsetExtent.x + extent.x);
301 position.y = RampFunction(position.y, mOffsetExtent.y + extent.y);
303 finalPosition += pageSize * position;
305 return finalPosition;
308 const Vector2 mPositionExtent; ///< Determines how much of the Actor's X and Y position affects exponent value.
309 const Vector2 mOffsetExtent; ///< Offset for exponent value.
310 const float mMaxExtent; ///< Maximum possible extent (mOffsetExtent.x + mOffsetExtent.y)
311 const float mPositionScale; ///< Position scaling factor (spreads out pages, to avoid overlap)
315 * Applies the scroll depth constraints to the child actor
317 * @param[in] scrollView The ScrollView containing the pages.
318 * @param[in] child The child to be affected with the 3D Effect.
319 * @param[in] positionExtent Controls how much Actor's X and Y
320 * position affects their alpha function's exponent value
321 * @param[in] offsetExtent exponent offset for X and Y scrolling
323 * @param[in] positionScale Changes the amount the page as a whole
325 * @param[in] scaleExtent Scale factor to reach when page is one whole
326 * page away from screen.
328 void ApplyScrollDepthConstraints(Toolkit::ScrollView scrollView,
330 const Vector2& positionExtent,
331 const Vector2& offsetExtent,
336 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
337 LocalSource(Actor::POSITION),
338 ParentSource(Actor::POSITION),
339 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
340 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
341 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
342 Source(scrollView, Actor::SIZE ),
343 ScrollDepthScaleConstraint( positionExtent, offsetExtent, positionScale, scaleExtent ) );
344 constraint.SetRemoveAction( Constraint::Discard );
345 child.ApplyConstraint( constraint );
347 // Position Constraint (apply last as other constraints use Actor::POSITION as a function input)
348 constraint = Constraint::New<Vector3>( Actor::POSITION,
349 ParentSource(Actor::POSITION),
350 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
351 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MIN_PROPERTY_NAME ) ),
352 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_POSITION_MAX_PROPERTY_NAME ) ),
353 Source(scrollView, Actor::SIZE ),
354 Source(scrollView, scrollView.GetPropertyIndex( Toolkit::ScrollView::SCROLL_WRAP_PROPERTY_NAME ) ),
355 ScrollDepthPositionConstraint( positionExtent, offsetExtent, positionScale ) );
357 constraint.SetRemoveAction( Constraint::Discard );
358 child.ApplyConstraint( constraint );
361 } // unnamed namespace
372 ScrollViewDepthEffect::ScrollViewDepthEffect()
377 ScrollViewDepthEffect::~ScrollViewDepthEffect()
381 void ScrollViewDepthEffect::ApplyToActor(Actor child,
382 const Vector2& positionExtent,
383 const Vector2& offsetExtent,
387 ApplyScrollDepthConstraints( GetScrollView(), child, positionExtent, offsetExtent, positionScale, scaleExtent );
390 void ScrollViewDepthEffect::OnAttach(Toolkit::ScrollView& scrollView)
394 void ScrollViewDepthEffect::OnDetach(Toolkit::ScrollView& scrollView)
398 } // namespace Internal
400 } // namespace Toolkit