@@ -39,11 +39,27 @@ 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.
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.
47
+
43
48
"""
44
49
45
50
# pylint: disable=too-many-arguments
46
- def __init__ (self , x , y , width , height , grid_size , cell_padding , divider_lines ):
51
+ def __init__ (
52
+ self ,
53
+ x ,
54
+ y ,
55
+ width ,
56
+ height ,
57
+ grid_size ,
58
+ cell_padding ,
59
+ divider_lines = False ,
60
+ h_divider_line_rows = None ,
61
+ v_divider_line_cols = None ,
62
+ ):
47
63
super ().__init__ (x = x , y = y )
48
64
self .x = x
49
65
self .y = y
@@ -54,9 +70,11 @@ def __init__(self, x, y, width, height, grid_size, cell_padding, divider_lines):
54
70
self ._cell_content_list = []
55
71
self ._divider_lines_enabled = divider_lines
56
72
self ._divider_lines = []
73
+ self .h_divider_line_rows = h_divider_line_rows
74
+ self .v_divider_line_cols = v_divider_line_cols
57
75
58
76
def _layout_cells (self ):
59
-
77
+ # pylint: disable=too-many-locals, too-many-branches, too-many-statements
60
78
for cell in self ._cell_content_list :
61
79
if cell ["content" ] not in self :
62
80
grid_size_x = self .grid_size [0 ]
@@ -69,13 +87,13 @@ def _layout_cells(self):
69
87
button_size_y = cell ["cell_size" ][1 ]
70
88
71
89
_measured_width = (
72
- int (button_size_x * self ._width / grid_size_x )
73
- - 2 * self .cell_padding
90
+ int (button_size_x * self ._width / grid_size_x )
91
+ - 2 * self .cell_padding
74
92
)
75
93
76
94
_measured_height = (
77
- int (button_size_y * self ._height / grid_size_y )
78
- - 2 * self .cell_padding
95
+ int (button_size_y * self ._height / grid_size_y )
96
+ - 2 * self .cell_padding
79
97
)
80
98
if hasattr (cell ["content" ], "resize" ):
81
99
# if it has resize function
@@ -99,20 +117,35 @@ def _layout_cells(self):
99
117
if not hasattr (cell ["content" ], "anchor_point" ):
100
118
101
119
cell ["content" ].x = (
102
- int (grid_position_x * self ._width / grid_size_x ) + self .cell_padding
120
+ int (grid_position_x * self ._width / grid_size_x )
121
+ + self .cell_padding
103
122
)
104
123
cell ["content" ].y = (
105
- int (grid_position_y * self ._height / grid_size_y ) + self .cell_padding
124
+ int (grid_position_y * self ._height / grid_size_y )
125
+ + self .cell_padding
106
126
)
107
127
else :
108
- print ("int({} * {} / {}) + {}" .format (grid_position_x , self ._width , grid_size_x , self .cell_padding ))
109
128
print (
110
- "int({} * {} / {}) + {}" .format (grid_position_y , self ._height , grid_size_y , self .cell_padding ))
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
+ )
111
141
112
142
cell ["content" ].anchor_point = (0 , 0 )
113
143
cell ["content" ].anchored_position = (
114
- int (grid_position_x * self ._width / grid_size_x ) + self .cell_padding ,
115
- int (grid_position_y * self ._height / grid_size_y ) + self .cell_padding )
144
+ int (grid_position_x * self ._width / grid_size_x )
145
+ + self .cell_padding ,
146
+ int (grid_position_y * self ._height / grid_size_y )
147
+ + self .cell_padding ,
148
+ )
116
149
print (cell ["content" ].anchored_position )
117
150
print ("---" )
118
151
@@ -124,73 +157,131 @@ def _layout_cells(self):
124
157
palette [1 ] = 0xFFFFFF
125
158
126
159
if not hasattr (cell ["content" ], "anchor_point" ):
127
- _bottom_line_loc_y = cell ["content" ].y + _measured_height + self .cell_padding
160
+ _bottom_line_loc_y = (
161
+ cell ["content" ].y + _measured_height + self .cell_padding
162
+ )
128
163
_bottom_line_loc_x = cell ["content" ].x - self .cell_padding
129
164
130
165
_top_line_loc_y = cell ["content" ].y - self .cell_padding
131
166
_top_line_loc_x = cell ["content" ].x - self .cell_padding
132
167
133
168
_right_line_loc_y = cell ["content" ].y - self .cell_padding
134
- _right_line_loc_x = cell ["content" ].x + _measured_width + self .cell_padding
169
+ _right_line_loc_x = (
170
+ cell ["content" ].x + _measured_width + self .cell_padding
171
+ )
135
172
else :
136
- _bottom_line_loc_y = cell ["content" ].anchored_position [1 ] + _measured_height + self .cell_padding
137
- _bottom_line_loc_x = cell ["content" ].anchored_position [0 ] - self .cell_padding
138
-
139
- _top_line_loc_y = cell ["content" ].anchored_position [1 ] - self .cell_padding
140
- _top_line_loc_x = cell ["content" ].anchored_position [0 ] - self .cell_padding
141
-
142
- _right_line_loc_y = cell ["content" ].anchored_position [1 ] - self .cell_padding
143
- _right_line_loc_x = cell ["content" ].anchored_position [0 ] + _measured_width + self .cell_padding
173
+ _bottom_line_loc_y = (
174
+ cell ["content" ].anchored_position [1 ]
175
+ + _measured_height
176
+ + self .cell_padding
177
+ )
178
+ _bottom_line_loc_x = (
179
+ cell ["content" ].anchored_position [0 ] - self .cell_padding
180
+ )
181
+
182
+ _top_line_loc_y = (
183
+ cell ["content" ].anchored_position [1 ] - self .cell_padding
184
+ )
185
+ _top_line_loc_x = (
186
+ cell ["content" ].anchored_position [0 ] - self .cell_padding
187
+ )
188
+
189
+ _right_line_loc_y = (
190
+ cell ["content" ].anchored_position [1 ] - self .cell_padding
191
+ )
192
+ _right_line_loc_x = (
193
+ cell ["content" ].anchored_position [0 ]
194
+ + _measured_width
195
+ + self .cell_padding
196
+ )
144
197
145
198
_horizontal_divider_line = displayio .Shape (
146
199
_measured_width + (2 * self .cell_padding ),
147
200
1 ,
148
- mirror_x = False , mirror_y = False )
201
+ mirror_x = False ,
202
+ mirror_y = False ,
203
+ )
149
204
150
205
_bottom_divider_tilegrid = displayio .TileGrid (
151
- _horizontal_divider_line , pixel_shader = palette ,
206
+ _horizontal_divider_line ,
207
+ pixel_shader = palette ,
152
208
y = _bottom_line_loc_y ,
153
- x = _bottom_line_loc_x )
209
+ x = _bottom_line_loc_x ,
210
+ )
154
211
155
212
_top_divider_tilegrid = displayio .TileGrid (
156
- _horizontal_divider_line , pixel_shader = palette ,
213
+ _horizontal_divider_line ,
214
+ pixel_shader = palette ,
157
215
y = _top_line_loc_y ,
158
- x = _top_line_loc_x )
216
+ x = _top_line_loc_x ,
217
+ )
159
218
160
219
_vertical_divider_line = displayio .Shape (
161
220
1 ,
162
221
_measured_height + (2 * self .cell_padding ),
163
- mirror_x = False , mirror_y = False )
222
+ mirror_x = False ,
223
+ mirror_y = False ,
224
+ )
164
225
165
226
_left_divider_tilegrid = displayio .TileGrid (
166
- _vertical_divider_line , pixel_shader = palette ,
227
+ _vertical_divider_line ,
228
+ pixel_shader = palette ,
167
229
y = _top_line_loc_y ,
168
- x = _top_line_loc_x )
230
+ x = _top_line_loc_x ,
231
+ )
169
232
170
233
_right_divider_tilegrid = displayio .TileGrid (
171
- _vertical_divider_line , pixel_shader = palette ,
234
+ _vertical_divider_line ,
235
+ pixel_shader = palette ,
172
236
y = _right_line_loc_y ,
173
- x = _right_line_loc_x )
237
+ x = _right_line_loc_x ,
238
+ )
174
239
175
240
for line_obj in self ._divider_lines :
176
241
self .remove (line_obj ["tilegrid" ])
177
242
178
- self ._divider_lines .append ({
179
- "shape" : _horizontal_divider_line ,
180
- "tilegrid" : _bottom_divider_tilegrid
181
- })
182
- self ._divider_lines .append ({
183
- "shape" : _horizontal_divider_line ,
184
- "tilegrid" : _top_divider_tilegrid
185
- })
186
- self ._divider_lines .append ({
187
- "shape" : _horizontal_divider_line ,
188
- "tilegrid" : _left_divider_tilegrid
189
- })
190
- self ._divider_lines .append ({
191
- "shape" : _vertical_divider_line ,
192
- "tilegrid" : _right_divider_tilegrid
193
- })
243
+ print ("pos_y: {} - size_y: {}" .format (grid_position_y , grid_size_y ))
244
+ print (grid_position_y == grid_size_y )
245
+ 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
248
+ ):
249
+ self ._divider_lines .append (
250
+ {
251
+ "shape" : _horizontal_divider_line ,
252
+ "tilegrid" : _bottom_divider_tilegrid ,
253
+ }
254
+ )
255
+ if (
256
+ self .h_divider_line_rows is None
257
+ or grid_position_y in self .h_divider_line_rows
258
+ ):
259
+ self ._divider_lines .append (
260
+ {
261
+ "shape" : _horizontal_divider_line ,
262
+ "tilegrid" : _top_divider_tilegrid ,
263
+ }
264
+ )
265
+ if (
266
+ self .v_divider_line_cols is None
267
+ or grid_position_x in self .v_divider_line_cols
268
+ ):
269
+ self ._divider_lines .append (
270
+ {
271
+ "shape" : _horizontal_divider_line ,
272
+ "tilegrid" : _left_divider_tilegrid ,
273
+ }
274
+ )
275
+ 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
+ ):
279
+ self ._divider_lines .append (
280
+ {
281
+ "shape" : _vertical_divider_line ,
282
+ "tilegrid" : _right_divider_tilegrid ,
283
+ }
284
+ )
194
285
195
286
for line_obj in self ._divider_lines :
196
287
self .append (line_obj ["tilegrid" ])
0 commit comments