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