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