2e7ddee0337c6be1f4869ad31af346b9adcdd9a2
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / scrollable / item-view / item-layout.cpp
1 /*
2  * Copyright (c) 2015 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 // CLASS HEADER
19 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/animation/animation.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/animation/time-period.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 struct ItemLayout::Impl
36 {
37   Vector3 mItemSize;                              ///< The size of an item in the layout
38   ControlOrientation::Type mOrientation;          ///< the orientation of the layout.
39 };
40
41 ItemLayout::ItemLayout()
42 : mImpl( new Impl )
43 {
44   mImpl->mOrientation = ControlOrientation::Up;
45 }
46
47 ItemLayout::~ItemLayout()
48 {
49   delete mImpl;
50 }
51
52 void ItemLayout::SetOrientation(ControlOrientation::Type orientation)
53 {
54   mImpl->mOrientation = orientation;
55 }
56
57 ControlOrientation::Type ItemLayout::GetOrientation() const
58 {
59   return mImpl->mOrientation;
60 }
61
62 void ItemLayout::GetItemSize( unsigned int itemId, const Vector3& layoutSize, Vector3& itemSize ) const
63 {
64   // If item-size has not been set then get the default size
65   if ( mImpl->mItemSize == Vector3::ZERO )
66   {
67     GetDefaultItemSize( itemId, layoutSize, itemSize );
68   }
69   else
70   {
71     itemSize = mImpl->mItemSize;
72   }
73 }
74
75 void ItemLayout::SetItemSize( const Vector3& itemSize )
76 {
77   mImpl->mItemSize = itemSize;
78 }
79
80 float ItemLayout::GetClosestOnScreenLayoutPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize)
81 {
82   Vector3 itemPosition = GetItemPosition( itemID, currentLayoutPosition, layoutSize );
83   Vector3 itemSize;
84   GetItemSize(itemID, layoutSize, itemSize);
85   Vector3 onScreenArea = (layoutSize - itemSize) * 0.5f;
86   if (itemPosition.x < -onScreenArea.x
87       || itemPosition.x > onScreenArea.x
88       || itemPosition.y < -onScreenArea.y
89       || itemPosition.y > onScreenArea.y)
90   {
91     // item not within viewable area
92     // safest thing to do here since we have no idea how the implementation will work is to return the scroll to position
93     return GetItemScrollToPosition(itemID);
94   }
95   return currentLayoutPosition;
96 }
97
98 int ItemLayout::GetNextFocusItemID(int itemID, int maxItems, Dali::Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
99 {
100   switch( direction )
101   {
102     case Control::KeyboardFocus::LEFT:
103     case Control::KeyboardFocus::UP:
104     {
105       itemID--;
106       if( itemID < 0 )
107       {
108         itemID = loopEnabled ? maxItems - 1 : 0;
109       }
110       break;
111     }
112     case Control::KeyboardFocus::RIGHT:
113     case Control::KeyboardFocus::DOWN:
114     {
115       itemID++;
116       if( itemID >= maxItems )
117       {
118         itemID = loopEnabled ? 0 : maxItems - 1;
119       }
120       break;
121     }
122     default:
123     {
124       break;
125     }
126   }
127   return itemID;
128 }
129
130 float ItemLayout::GetFlickSpeedFactor() const
131 {
132   // By default, the speed factor while dragging and swiping is the same.
133   return GetScrollSpeedFactor();
134 }
135
136 } // namespace Toolkit
137
138 } // namespace Dali