a9f27696a15ef0c09c37cf642798d89e171fe76b
[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 #include <dali-toolkit/devel-api/controls/scrollable/item-view/default-item-layout-property.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 struct ItemLayout::Impl
37 {
38   Vector3 mItemSize;                              ///< The size of an item in the layout
39   ControlOrientation::Type mOrientation;          ///< the orientation of the layout.
40   Property::Map mProperties;
41   bool mHasLayoutChanged;
42 };
43
44 ItemLayout::ItemLayout()
45 : mImpl( new Impl )
46 {
47   mImpl->mOrientation = ControlOrientation::Up;
48 }
49
50 ItemLayout::~ItemLayout()
51 {
52   delete mImpl;
53 }
54
55 void ItemLayout::SetOrientation(ControlOrientation::Type orientation)
56 {
57   mImpl->mOrientation = orientation;
58 }
59
60 ControlOrientation::Type ItemLayout::GetOrientation() const
61 {
62   return mImpl->mOrientation;
63 }
64
65 void ItemLayout::GetItemSize( unsigned int itemId, const Vector3& layoutSize, Vector3& itemSize ) const
66 {
67   // If item-size has not been set then get the default size
68   if ( mImpl->mItemSize == Vector3::ZERO )
69   {
70     GetDefaultItemSize( itemId, layoutSize, itemSize );
71   }
72   else
73   {
74     itemSize = mImpl->mItemSize;
75   }
76 }
77
78 void ItemLayout::SetItemSize( const Vector3& itemSize )
79 {
80   mImpl->mItemSize = itemSize;
81 }
82
83 float ItemLayout::GetClosestOnScreenLayoutPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize)
84 {
85   Vector3 itemPosition = GetItemPosition( itemID, currentLayoutPosition, layoutSize );
86   Vector3 itemSize;
87   GetItemSize(itemID, layoutSize, itemSize);
88   Vector3 onScreenArea = (layoutSize - itemSize) * 0.5f;
89   if (itemPosition.x < -onScreenArea.x
90       || itemPosition.x > onScreenArea.x
91       || itemPosition.y < -onScreenArea.y
92       || itemPosition.y > onScreenArea.y)
93   {
94     // item not within viewable area
95     // safest thing to do here since we have no idea how the implementation will work is to return the scroll to position
96     return GetItemScrollToPosition(itemID);
97   }
98   return currentLayoutPosition;
99 }
100
101 int ItemLayout::GetNextFocusItemID(int itemID, int maxItems, Dali::Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
102 {
103   switch( direction )
104   {
105     case Control::KeyboardFocus::LEFT:
106     case Control::KeyboardFocus::UP:
107     {
108       itemID--;
109       if( itemID < 0 )
110       {
111         itemID = loopEnabled ? maxItems - 1 : 0;
112       }
113       break;
114     }
115     case Control::KeyboardFocus::RIGHT:
116     case Control::KeyboardFocus::DOWN:
117     {
118       itemID++;
119       if( itemID >= maxItems )
120       {
121         itemID = loopEnabled ? 0 : maxItems - 1;
122       }
123       break;
124     }
125     default:
126     {
127       break;
128     }
129   }
130   return itemID;
131 }
132
133 float ItemLayout::GetFlickSpeedFactor() const
134 {
135   // By default, the speed factor while dragging and swiping is the same.
136   return GetScrollSpeedFactor();
137 }
138
139 void ItemLayout::SetLayoutProperties(const Property::Map& properties)
140 {
141   for( unsigned int idx = 0, mapCount = properties.Count(); idx < mapCount; ++idx )
142   {
143     KeyValuePair propertyPair( properties.GetKeyValue( idx ) );
144
145     if(propertyPair.first == DefaultItemLayoutProperty::ITEM_SIZE)
146     {
147       SetItemSize(propertyPair.second.Get<Vector3>());
148     }
149     else if(propertyPair.first == DefaultItemLayoutProperty::ORIENTATION)
150     {
151       //Up, Left, Down, Right
152       int orientationType = propertyPair.second.Get<int>();
153       if(orientationType <= ControlOrientation::Right && orientationType >= ControlOrientation::Up)
154       {
155         SetOrientation(ControlOrientation::Type(orientationType));
156       }
157     }
158   }
159   mImpl->mHasLayoutChanged = true;
160   mImpl->mProperties = properties;
161 }
162
163 Property::Map ItemLayout::GetLayoutProperties()
164 {
165   return mImpl->mProperties;
166 }
167
168 bool ItemLayout::HasLayoutChanged()
169 {
170   return mImpl->mHasLayoutChanged;
171 }
172
173 void ItemLayout::ResetLayoutChangedFlag()
174 {
175   mImpl->mHasLayoutChanged = false;
176 }
177
178 } // namespace Toolkit
179
180 } // namespace Dali