22
22
https://github.com/adafruit/circuitpython/releases
23
23
24
24
"""
25
-
25
+ import math
26
26
import displayio
27
27
28
28
__version__ = "0.0.0-auto.0"
@@ -39,11 +39,11 @@ class GridLayout(displayio.Group):
39
39
:param int height: Height of the layout in pixels.
40
40
:param tuple grid_size: Size in cells as two ints in a tuple e.g. (2, 2)
41
41
:param int cell_padding: Extra padding space inside each cell. In pixels.
42
- :param bool divider_lines: Whether or not to draw lines between the cells. Defaults to False.
43
- :param tuple h_divider_line_rows: Row indexes to draw divider lines above.
44
- Row indexes are 0 based.
45
- :param tuple v_divider_line_cols: Column indexes to draw divider lines before.
46
- Column indexes are 0 based.
42
+ :param bool divider_lines: Whether or not to draw lines between the cells.
43
+ :param Union[ tuple, list] h_divider_line_rows: Row indexes to draw divider
44
+ lines above. Row indexes are 0 based.
45
+ :param Union[ tuple, list] v_divider_line_cols: Column indexes to draw divider
46
+ lines before. Column indexes are 0 based.
47
47
48
48
"""
49
49
@@ -55,7 +55,7 @@ def __init__(
55
55
width ,
56
56
height ,
57
57
grid_size ,
58
- cell_padding ,
58
+ cell_padding = 0 ,
59
59
divider_lines = False ,
60
60
h_divider_line_rows = None ,
61
61
v_divider_line_cols = None ,
@@ -68,11 +68,38 @@ def __init__(
68
68
self .grid_size = grid_size
69
69
self .cell_padding = cell_padding
70
70
self ._cell_content_list = []
71
- self . _divider_lines_enabled = divider_lines
71
+
72
72
self ._divider_lines = []
73
73
self .h_divider_line_rows = h_divider_line_rows
74
74
self .v_divider_line_cols = v_divider_line_cols
75
75
76
+ self ._divider_lines_enabled = (
77
+ (divider_lines is True )
78
+ or (h_divider_line_rows is not None )
79
+ or (v_divider_line_cols is not None )
80
+ )
81
+
82
+ if divider_lines :
83
+ if self .h_divider_line_rows is None :
84
+ self .h_divider_line_rows = []
85
+ for _y in range (self .grid_size [1 ] + 1 ):
86
+ self .h_divider_line_rows .append (_y )
87
+ if self .v_divider_line_cols is None :
88
+ self .v_divider_line_cols = []
89
+ for _x in range (self .grid_size [0 ] + 1 ):
90
+ self .v_divider_line_cols .append (_x )
91
+ else :
92
+ if not h_divider_line_rows :
93
+ self .h_divider_line_rows = tuple ()
94
+ if not v_divider_line_cols :
95
+ self .v_divider_line_cols = tuple ()
96
+
97
+ # use at least 1 padding so that content is inside the divider lines
98
+ if cell_padding == 0 and (
99
+ divider_lines or h_divider_line_rows or v_divider_line_cols
100
+ ):
101
+ self .cell_padding = 1
102
+
76
103
def _layout_cells (self ):
77
104
# pylint: disable=too-many-locals, too-many-branches, too-many-statements
78
105
for cell in self ._cell_content_list :
@@ -87,12 +114,12 @@ def _layout_cells(self):
87
114
button_size_y = cell ["cell_size" ][1 ]
88
115
89
116
_measured_width = (
90
- int (button_size_x * self ._width / grid_size_x )
117
+ math . ceil (button_size_x * self ._width / grid_size_x )
91
118
- 2 * self .cell_padding
92
119
)
93
120
94
121
_measured_height = (
95
- int (button_size_y * self ._height / grid_size_y )
122
+ math . ceil (button_size_y * self ._height / grid_size_y )
96
123
- 2 * self .cell_padding
97
124
)
98
125
if hasattr (cell ["content" ], "resize" ):
@@ -125,29 +152,13 @@ def _layout_cells(self):
125
152
+ self .cell_padding
126
153
)
127
154
else :
128
- print (
129
- "int({} * {} / {}) + {}" .format (
130
- grid_position_x , self ._width , grid_size_x , self .cell_padding
131
- )
132
- )
133
- print (
134
- "int({} * {} / {}) + {}" .format (
135
- grid_position_y ,
136
- self ._height ,
137
- grid_size_y ,
138
- self .cell_padding ,
139
- )
140
- )
141
-
142
155
cell ["content" ].anchor_point = (0 , 0 )
143
156
cell ["content" ].anchored_position = (
144
157
int (grid_position_x * self ._width / grid_size_x )
145
158
+ self .cell_padding ,
146
159
int (grid_position_y * self ._height / grid_size_y )
147
160
+ self .cell_padding ,
148
161
)
149
- print (cell ["content" ].anchored_position )
150
- print ("---" )
151
162
152
163
self .append (cell ["content" ])
153
164
@@ -159,7 +170,7 @@ def _layout_cells(self):
159
170
if not hasattr (cell ["content" ], "anchor_point" ):
160
171
_bottom_line_loc_y = (
161
172
cell ["content" ].y + _measured_height + self .cell_padding
162
- )
173
+ ) - 1
163
174
_bottom_line_loc_x = cell ["content" ].x - self .cell_padding
164
175
165
176
_top_line_loc_y = cell ["content" ].y - self .cell_padding
@@ -168,13 +179,13 @@ def _layout_cells(self):
168
179
_right_line_loc_y = cell ["content" ].y - self .cell_padding
169
180
_right_line_loc_x = (
170
181
cell ["content" ].x + _measured_width + self .cell_padding
171
- )
182
+ ) - 1
172
183
else :
173
184
_bottom_line_loc_y = (
174
185
cell ["content" ].anchored_position [1 ]
175
186
+ _measured_height
176
187
+ self .cell_padding
177
- )
188
+ ) - 1
178
189
_bottom_line_loc_x = (
179
190
cell ["content" ].anchored_position [0 ] - self .cell_padding
180
191
)
@@ -193,7 +204,7 @@ def _layout_cells(self):
193
204
cell ["content" ].anchored_position [0 ]
194
205
+ _measured_width
195
206
+ self .cell_padding
196
- )
207
+ ) - 1
197
208
198
209
_horizontal_divider_line = displayio .Shape (
199
210
_measured_width + (2 * self .cell_padding ),
@@ -240,41 +251,31 @@ def _layout_cells(self):
240
251
for line_obj in self ._divider_lines :
241
252
self .remove (line_obj ["tilegrid" ])
242
253
243
- print ("pos_y: {} - size_y: {}" .format (grid_position_y , grid_size_y ))
244
- print (grid_position_y == grid_size_y )
245
254
if grid_position_y == grid_size_y - 1 and (
246
- self .h_divider_line_rows is None
247
- or grid_position_y + 1 in self .h_divider_line_rows
255
+ grid_position_y + 1 in self .h_divider_line_rows
248
256
):
249
257
self ._divider_lines .append (
250
258
{
251
259
"shape" : _horizontal_divider_line ,
252
260
"tilegrid" : _bottom_divider_tilegrid ,
253
261
}
254
262
)
255
- if (
256
- self .h_divider_line_rows is None
257
- or grid_position_y in self .h_divider_line_rows
258
- ):
263
+ if grid_position_y in self .h_divider_line_rows :
259
264
self ._divider_lines .append (
260
265
{
261
266
"shape" : _horizontal_divider_line ,
262
267
"tilegrid" : _top_divider_tilegrid ,
263
268
}
264
269
)
265
- if (
266
- self .v_divider_line_cols is None
267
- or grid_position_x in self .v_divider_line_cols
268
- ):
270
+ if grid_position_x in self .v_divider_line_cols :
269
271
self ._divider_lines .append (
270
272
{
271
273
"shape" : _horizontal_divider_line ,
272
274
"tilegrid" : _left_divider_tilegrid ,
273
275
}
274
276
)
275
277
if grid_position_x == grid_size_x - 1 and (
276
- self .v_divider_line_cols is None
277
- or grid_position_x + 1 in self .v_divider_line_cols
278
+ grid_position_x + 1 in self .v_divider_line_cols
278
279
):
279
280
self ._divider_lines .append (
280
281
{
0 commit comments