Add size animation in layout measure phase.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / layouting / grid-locations.cpp
1 /*
2  * Copyright (c) 2018 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 //CLASS HEADER
18 #include <dali-toolkit/internal/layouting/grid-locations.h>
19
20 // EXTERNAL HEADERS
21 #include <dali/integration-api/debug.h>
22
23 // INTERNAL HEADERS
24 #include <dali-toolkit/devel-api/layouting/layout-item.h>
25
26 namespace
27 {
28
29 #if defined(DEBUG_ENABLED)
30 static Debug::Filter* gLogFilter = Debug::Filter::New( Debug::Concise, false, "LOG_AXIS" );
31 #endif
32
33 }
34
35 namespace Dali
36 {
37 namespace Toolkit
38 {
39 namespace Internal
40 {
41
42 GridLocationsPtr GridLocations::New()
43 {
44   GridLocationsPtr gridAxis( new GridLocations() );
45   return gridAxis;
46 }
47
48 GridLocations::GridLocations()
49 : mLocations()
50 {
51 }
52
53 GridLocations::~GridLocations(){}
54
55 void GridLocations::CalculateLocations( int numberOfColumns,
56                                         unsigned int availableWidth,
57                                         unsigned int availableHeight,
58                                         unsigned int numberOfCells,
59                                         unsigned int columnWidth,
60                                         unsigned int rowHeight )
61 {
62   DALI_ASSERT_DEBUG( numberOfColumns > 0 && "number of columns should be greater than 0" );
63   numberOfColumns = std::max ( numberOfColumns, 1 );
64   mLocations.clear();
65
66   // Calculate width and height of columns and rows.
67
68   // Calculate numbers of rows, round down result as later check for remainder.
69   unsigned int numberOfRows = numberOfCells/numberOfColumns;
70   // If number of cells not cleanly dividable by colums, add another row to house remainder cells.
71   numberOfRows += (numberOfCells%numberOfColumns)?1:0;
72
73   unsigned int maxColumnWidth = availableWidth / numberOfColumns;
74
75   if( columnWidth > 0 )
76   {
77     // Column width supplied so use this unless exceeds available width.
78     columnWidth = std::min( columnWidth, maxColumnWidth );
79   }
80   else
81   {
82     // Column width not supplied so use calculated value
83     columnWidth = maxColumnWidth;
84   }
85
86   unsigned int maxRowHeight = availableHeight / numberOfRows;
87
88   if( rowHeight > 0 )
89   {
90     // Column height supplied so use this unless exceeds available height.
91     rowHeight = std::min( rowHeight, maxRowHeight );
92   }
93   else
94   {
95     // Column height not supplied so use calculated value
96     rowHeight = maxRowHeight;
97   }
98
99   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ColumWidth[%d] RowHeight[%d] NumberOfRows[%d] NumberOfColumns[%d]\n",
100                                               columnWidth, rowHeight, numberOfRows, numberOfColumns );
101   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Remainder[%d]\n", numberOfCells%numberOfColumns );
102
103   int  y1 = 0;
104   int  y2 = y1 + rowHeight;
105
106   // Calculate start, end, top and bottom coordinate of each cell.
107
108   // Iterate rows
109   for( auto i = 0u; i < numberOfRows; i++ )
110   {
111     int x1 = 0;
112     int x2 = x1 + columnWidth;
113
114     // Iterate columns
115     for( auto j = 0; j < numberOfColumns; j++ )
116     {
117       GridLocations::Cell cell( x1, x2, y1, y2 );
118       mLocations.push_back( cell );
119       // Calculate starting x and ending x position of each column
120       x1 = x2;
121       x2 = x2 + columnWidth;
122     }
123
124     // Calculate top y and bottom y position of each row.
125     y1 = y2;
126     y2 = y2 + rowHeight;
127   }
128
129 #if defined(DEBUG_ENABLED)
130   std::ostringstream oss;
131   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GridLocations::CalculateLocations (%d)\n", numberOfCells );
132   for( auto i = 0u; i < numberOfCells; i++ )
133   {
134     DALI_LOG_STREAM( gLogFilter, Debug::Verbose,"x1:"<<mLocations[i].xStart<<" x2:"<<mLocations[i].xEnd<<" y1:"<<mLocations[i].yTop<<" y2:"<<mLocations[i].yBottom);
135   }
136 #endif
137 }
138
139 GridLocations::LocationVector GridLocations::GetLocations()
140 {
141 #if defined(DEBUG_ENABLED)
142   std::ostringstream oss;
143   auto itemCount = mLocations.size(); // mVerticalLocations mirrors this so same size.
144   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GridLocations::GetLocations for %u cells\n", itemCount );
145   for( auto i = 0u; i < itemCount; i++ )
146   {
147     DALI_LOG_STREAM( gLogFilter, Debug::Verbose,"x1:"<<mLocations[i].xStart<<" x2:"<<mLocations[i].xEnd<<" y1:"<<mLocations[i].yTop<<" y2:"<<mLocations[i].yBottom);
148   }
149 #endif
150
151   return mLocations;
152 }
153
154 } // namespace Internal
155
156 } // namespace Toolkit
157 } // namespace Dali