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/public-api/controls/scrollable/item-view/item-layout.h>
26 ItemLayout::ItemLayout()
27 : mOrientation(ControlOrientation::Up)
31 ItemLayout::~ItemLayout()
35 void ItemLayout::SetOrientation(ControlOrientation::Type orientation)
37 mOrientation = orientation;
40 ControlOrientation::Type ItemLayout::GetOrientation() const
45 float ItemLayout::GetClosestOnScreenLayoutPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize)
47 ItemLayout::Vector3Function positionConstraint;
48 Vector3 itemPosition = Vector3::ZERO;
49 if (GetPositionConstraint(itemID, positionConstraint))
51 itemPosition = positionConstraint(Vector3::ZERO, currentLayoutPosition + itemID, 0.0f, layoutSize);
54 GetItemSize(itemID, layoutSize, itemSize);
55 Vector3 onScreenArea = (layoutSize - itemSize) * 0.5f;
56 if (itemPosition.x < -onScreenArea.x
57 || itemPosition.x > onScreenArea.x
58 || itemPosition.y < -onScreenArea.y
59 || itemPosition.y > onScreenArea.y)
61 // item not within viewable area
62 // safest thing to do here since we have no idea how the implementation will work is to return the scroll to position
63 return GetItemScrollToPosition(itemID);
65 return currentLayoutPosition;
68 void ItemLayout::GetXAxisScrollHint(Vector2& scrollHint) const
70 scrollHint = Vector2::ZERO;
71 Radian scrollAngle(GetScrollDirection());
72 Vector2 scrollDirection(sinf(scrollAngle), cosf(scrollAngle));
75 case ControlOrientation::Up:
77 if(fabsf(scrollDirection.y) < Math::MACHINE_EPSILON_1)
79 // we probably want x scrolling
80 if(scrollDirection.x > 0.0f)
82 // normal positive scrolling
83 scrollHint = Vector2::XAXIS;
87 scrollHint = -Vector2::XAXIS;
92 case ControlOrientation::Down:
94 if(fabsf(scrollDirection.y) < Math::MACHINE_EPSILON_1)
96 // we probably want x scrolling
97 if(scrollDirection.x > 0.0f)
99 // normal positive scrolling
100 scrollHint = -Vector2::XAXIS;
104 scrollHint = Vector2::XAXIS;
109 case ControlOrientation::Left:
111 // we probably want x scrolling
112 if(scrollDirection.x > 0.0f)
114 // normal positive scrolling
115 scrollHint = Vector2::XAXIS;
119 scrollHint = -Vector2::XAXIS;
123 case ControlOrientation::Right:
125 // we probably want x scrolling
126 if(scrollDirection.x > 0.0f)
128 // normal positive scrolling
129 scrollHint = -Vector2::XAXIS;
133 scrollHint = Vector2::XAXIS;
140 void ItemLayout::GetYAxisScrollHint(Vector2& scrollHint) const
142 scrollHint = Vector2::ZERO;
143 Radian scrollAngle(GetScrollDirection());
144 Vector2 scrollDirection(sinf(scrollAngle), cosf(scrollAngle));
147 case ControlOrientation::Up:
149 // we probably want x scrolling
150 if(scrollDirection.y > 0.0f)
152 // normal positive scrolling
153 scrollHint = Vector2::YAXIS;
157 scrollHint = -Vector2::YAXIS;
161 case ControlOrientation::Down:
163 // we probably want x scrolling
164 if(scrollDirection.y > 0.0f)
166 // normal positive scrolling
167 scrollHint = -Vector2::YAXIS;
171 scrollHint = Vector2::YAXIS;
175 case ControlOrientation::Left:
177 if(fabsf(scrollDirection.x) < Math::MACHINE_EPSILON_1)
179 // we probably want x scrolling
180 if(scrollDirection.y > 0.0f)
182 // normal positive scrolling
183 scrollHint = -Vector2::YAXIS;
187 scrollHint = Vector2::YAXIS;
192 case ControlOrientation::Right:
194 if(fabsf(scrollDirection.x) < Math::MACHINE_EPSILON_1)
196 // we probably want x scrolling
197 if(scrollDirection.y > 0.0f)
199 // normal positive scrolling
200 scrollHint = Vector2::YAXIS;
204 scrollHint = -Vector2::YAXIS;
212 int ItemLayout::GetNextFocusItemID(int itemID, int maxItems, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
222 itemID = loopEnabled ? maxItems - 1 : 0;
230 if( itemID >= maxItems )
232 itemID = loopEnabled ? 0 : maxItems - 1;
240 float ItemLayout::GetFlickSpeedFactor() const
242 // By default, the speed factor while dragging and swiping is the same.
243 return GetScrollSpeedFactor();
246 } // namespace Toolkit