[NUI] Sync dalihub - Add C# layout (#816)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Layouting / GridLocations.cs
1 /*
2  * Copyright (c) 2019 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 using System;
19 using System.Collections.Generic;
20
21 namespace Tizen.NUI
22 {
23     /// <summary>
24     /// [Draft] This internal class houses the algorithm for computing the locations and size of cells.
25     /// </summary>
26     internal class GridLocations
27     {
28         /// <summary>
29         /// A struct holding the 4 points which make up a cell.
30         /// </summary>
31         public struct Cell
32         {
33             public int Start;
34             public int End;
35             public int Top;
36             public int Bottom;
37
38             /// <summary>
39             /// Initialize a cell with the given points.
40             /// </summary>
41             /// <param name="start">The start x coordinate.</param>
42             /// <param name="end">The end x coordinate.</param>
43             /// <param name="top">The top y coordinate.</param>
44             /// <param name="bottom">The bottom y coordinate.</param>
45
46             public Cell( int start, int end, int top, int bottom)
47             {
48                 Start = start;
49                 End = end;
50                 Top = top;
51                 Bottom = bottom;
52             }
53         };
54
55         private List<Cell> _locationsVector;
56
57         /// <summary>
58         /// [Draft]Constructor
59         /// </summary>
60         public GridLocations()
61         {
62             _locationsVector = new List<Cell>();
63         }
64
65         /// <summary>
66         /// Get locations vector with position of each cell and cell size.
67         /// </summary>
68         public List<Cell> GetLocations()
69         {
70             return _locationsVector;
71         }
72
73         /// <summary>
74         /// [Draft] Uses the given parameters to calculate the x,y coordinates of each cell and cell size.
75         /// </summary>
76         public void CalculateLocations( int numberOfColumns, int availableWidth,
77                                         int availableHeight, int numberOfCells)
78         {
79             numberOfColumns = Math.Max( numberOfColumns, 1 );
80             _locationsVector.Clear();
81
82             // Calculate width and height of columns and rows.
83
84             // Calculate numbers of rows, round down result as later check for remainder.
85             int remainder = 0;
86             int numberOfRows = Math.DivRem(numberOfCells,numberOfColumns, out remainder);
87             // If number of cells not cleanly dividable by columns, add another row to house remainder cells.
88             numberOfRows += (remainder > 0) ? 1:0;
89
90             // Rounding on column widths performed here,
91             // if visually noticeable then can divide the space explicitly between columns.
92             int columnWidth = availableWidth / numberOfColumns;
93
94             int rowHeight = availableHeight;
95
96             if( numberOfRows > 0 )
97             {
98                 // Column height supplied so use this unless exceeds available height.
99                 rowHeight = (availableHeight / numberOfRows);
100             }
101
102             Log.Info("NUI", "ColumnWidth[" + columnWidth + "] RowHeight[" +rowHeight + "] NumberOfRows["
103                             + numberOfRows + "] NumberOfColumns[" + numberOfColumns +"]\n");
104
105             int  y1 = 0;
106             int  y2 = y1 + rowHeight;
107
108             // Calculate start, end, top and bottom coordinate of each cell.
109
110             // Iterate rows
111             for( var i = 0u; i < numberOfRows; i++ )
112             {
113                 int x1 = 0;
114                 int x2 = x1 + columnWidth;
115
116                 // Iterate columns
117                 for( var j = 0; j < numberOfColumns; j++ )
118                 {
119                     Cell cell = new Cell( x1, x2, y1, y2 );
120                     _locationsVector.Add( cell );
121                     // Calculate starting x and ending x position of each column
122                     x1 = x2;
123                     x2 = x2 + columnWidth;
124                 }
125
126                 // Calculate top y and bottom y position of each row.
127                 y1 = y2;
128                 y2 = y2 + rowHeight;
129             }
130         }
131     }
132 }