Add 'ExclusiveArch: armv7l' limit build to arm architecture
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / public-api / controls / scrollable / item-view / item-layout.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
18
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24
25 ItemLayout::ItemLayout()
26 : mOrientation(ControlOrientation::Up)
27 {
28 }
29
30 ItemLayout::~ItemLayout()
31 {
32 }
33
34 void ItemLayout::SetOrientation(ControlOrientation::Type orientation)
35 {
36   mOrientation = orientation;
37 }
38
39 ControlOrientation::Type ItemLayout::GetOrientation() const
40 {
41   return mOrientation;
42 }
43
44 float ItemLayout::GetClosestOnScreenLayoutPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize)
45 {
46   ItemLayout::Vector3Function positionConstraint;
47   Vector3 itemPosition = Vector3::ZERO;
48   if (GetPositionConstraint(itemID, positionConstraint))
49   {
50     itemPosition = positionConstraint(Vector3::ZERO, currentLayoutPosition + itemID, 0.0f, layoutSize);
51   }
52   Vector3 itemSize;
53   GetItemSize(itemID, layoutSize, itemSize);
54   Vector3 onScreenArea = (layoutSize - itemSize) * 0.5f;
55   if (itemPosition.x < -onScreenArea.x
56       || itemPosition.x > onScreenArea.x
57       || itemPosition.y < -onScreenArea.y
58       || itemPosition.y > onScreenArea.y)
59   {
60     // item not within viewable area
61     // safest thing to do here since we have no idea how the implementation will work is to return the scroll to position
62     return GetItemScrollToPosition(itemID);
63   }
64   return currentLayoutPosition;
65 }
66
67 void ItemLayout::GetXAxisScrollHint(Vector2& scrollHint) const
68 {
69   scrollHint = Vector2::ZERO;
70   Radian scrollAngle(GetScrollDirection());
71   Vector2 scrollDirection(sinf(scrollAngle), cosf(scrollAngle));
72   switch(mOrientation)
73   {
74     case ControlOrientation::Up:
75     {
76       if(fabsf(scrollDirection.y) < Math::MACHINE_EPSILON_1)
77       {
78         // we probably want x scrolling
79         if(scrollDirection.x > 0.0f)
80         {
81           // normal positive scrolling
82           scrollHint = Vector2::XAXIS;
83         }
84         else
85         {
86           scrollHint = -Vector2::XAXIS;
87         }
88       }
89       break;
90     }
91     case ControlOrientation::Down:
92     {
93       if(fabsf(scrollDirection.y) < Math::MACHINE_EPSILON_1)
94       {
95         // we probably want x scrolling
96         if(scrollDirection.x > 0.0f)
97         {
98           // normal positive scrolling
99           scrollHint = -Vector2::XAXIS;
100         }
101         else
102         {
103           scrollHint = Vector2::XAXIS;
104         }
105       }
106       break;
107     }
108     case ControlOrientation::Left:
109     {
110       // we probably want x scrolling
111       if(scrollDirection.x > 0.0f)
112       {
113         // normal positive scrolling
114         scrollHint = Vector2::XAXIS;
115       }
116       else
117       {
118         scrollHint = -Vector2::XAXIS;
119       }
120       break;
121     }
122     case ControlOrientation::Right:
123     {
124       // we probably want x scrolling
125       if(scrollDirection.x > 0.0f)
126       {
127         // normal positive scrolling
128         scrollHint = -Vector2::XAXIS;
129       }
130       else
131       {
132         scrollHint = Vector2::XAXIS;
133       }
134       break;
135     }
136   }
137 }
138
139 void ItemLayout::GetYAxisScrollHint(Vector2& scrollHint) const
140 {
141   scrollHint = Vector2::ZERO;
142   Radian scrollAngle(GetScrollDirection());
143   Vector2 scrollDirection(sinf(scrollAngle), cosf(scrollAngle));
144   switch(mOrientation)
145   {
146     case ControlOrientation::Up:
147     {
148       // we probably want x scrolling
149       if(scrollDirection.y > 0.0f)
150       {
151         // normal positive scrolling
152         scrollHint = Vector2::YAXIS;
153       }
154       else
155       {
156         scrollHint = -Vector2::YAXIS;
157       }
158       break;
159     }
160     case ControlOrientation::Down:
161     {
162       // we probably want x scrolling
163       if(scrollDirection.y > 0.0f)
164       {
165         // normal positive scrolling
166         scrollHint = -Vector2::YAXIS;
167       }
168       else
169       {
170         scrollHint = Vector2::YAXIS;
171       }
172       break;
173     }
174     case ControlOrientation::Left:
175     {
176       if(fabsf(scrollDirection.x) < Math::MACHINE_EPSILON_1)
177       {
178         // we probably want x scrolling
179         if(scrollDirection.y > 0.0f)
180         {
181           // normal positive scrolling
182           scrollHint = -Vector2::YAXIS;
183         }
184         else
185         {
186           scrollHint = Vector2::YAXIS;
187         }
188       }
189       break;
190     }
191     case ControlOrientation::Right:
192     {
193       if(fabsf(scrollDirection.x) < Math::MACHINE_EPSILON_1)
194       {
195         // we probably want x scrolling
196         if(scrollDirection.y > 0.0f)
197         {
198           // normal positive scrolling
199           scrollHint = Vector2::YAXIS;
200         }
201         else
202         {
203           scrollHint = -Vector2::YAXIS;
204         }
205       }
206       break;
207     }
208   }
209 }
210
211 int ItemLayout::GetNextFocusItemID(int itemID, int maxItems, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
212 {
213   switch( direction )
214   {
215     case Control::Left:
216     case Control::Up:
217     {
218       itemID--;
219       if( itemID < 0 )
220       {
221         itemID = loopEnabled ? maxItems - 1 : 0;
222       }
223       break;
224     }
225     case Control::Right:
226     case Control::Down:
227     {
228       itemID++;
229       if( itemID >= maxItems )
230       {
231         itemID = loopEnabled ? 0 : maxItems - 1;
232       }
233       break;
234     }
235   }
236   return itemID;
237 }
238
239 } // namespace Toolkit
240
241 } // namespace Dali