Skip to content

Commit 5a79400

Browse files
committed
move divider lines inside height and width of GridLayout. fix rounding error. change argument API
1 parent 16cd97d commit 5a79400

File tree

2 files changed

+45
-46
lines changed

2 files changed

+45
-46
lines changed

adafruit_displayio_layout/layouts/grid_layout.py

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
https://github.com/adafruit/circuitpython/releases
2323
2424
"""
25-
25+
import math
2626
import displayio
2727

2828
__version__ = "0.0.0-auto.0"
@@ -39,11 +39,11 @@ class GridLayout(displayio.Group):
3939
:param int height: Height of the layout in pixels.
4040
:param tuple grid_size: Size in cells as two ints in a tuple e.g. (2, 2)
4141
: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.
4747
4848
"""
4949

@@ -55,7 +55,7 @@ def __init__(
5555
width,
5656
height,
5757
grid_size,
58-
cell_padding,
58+
cell_padding=0,
5959
divider_lines=False,
6060
h_divider_line_rows=None,
6161
v_divider_line_cols=None,
@@ -68,11 +68,38 @@ def __init__(
6868
self.grid_size = grid_size
6969
self.cell_padding = cell_padding
7070
self._cell_content_list = []
71-
self._divider_lines_enabled = divider_lines
71+
7272
self._divider_lines = []
7373
self.h_divider_line_rows = h_divider_line_rows
7474
self.v_divider_line_cols = v_divider_line_cols
7575

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+
76103
def _layout_cells(self):
77104
# pylint: disable=too-many-locals, too-many-branches, too-many-statements
78105
for cell in self._cell_content_list:
@@ -87,12 +114,12 @@ def _layout_cells(self):
87114
button_size_y = cell["cell_size"][1]
88115

89116
_measured_width = (
90-
int(button_size_x * self._width / grid_size_x)
117+
math.ceil(button_size_x * self._width / grid_size_x)
91118
- 2 * self.cell_padding
92119
)
93120

94121
_measured_height = (
95-
int(button_size_y * self._height / grid_size_y)
122+
math.ceil(button_size_y * self._height / grid_size_y)
96123
- 2 * self.cell_padding
97124
)
98125
if hasattr(cell["content"], "resize"):
@@ -125,29 +152,13 @@ def _layout_cells(self):
125152
+ self.cell_padding
126153
)
127154
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-
142155
cell["content"].anchor_point = (0, 0)
143156
cell["content"].anchored_position = (
144157
int(grid_position_x * self._width / grid_size_x)
145158
+ self.cell_padding,
146159
int(grid_position_y * self._height / grid_size_y)
147160
+ self.cell_padding,
148161
)
149-
print(cell["content"].anchored_position)
150-
print("---")
151162

152163
self.append(cell["content"])
153164

@@ -159,7 +170,7 @@ def _layout_cells(self):
159170
if not hasattr(cell["content"], "anchor_point"):
160171
_bottom_line_loc_y = (
161172
cell["content"].y + _measured_height + self.cell_padding
162-
)
173+
) - 1
163174
_bottom_line_loc_x = cell["content"].x - self.cell_padding
164175

165176
_top_line_loc_y = cell["content"].y - self.cell_padding
@@ -168,13 +179,13 @@ def _layout_cells(self):
168179
_right_line_loc_y = cell["content"].y - self.cell_padding
169180
_right_line_loc_x = (
170181
cell["content"].x + _measured_width + self.cell_padding
171-
)
182+
) - 1
172183
else:
173184
_bottom_line_loc_y = (
174185
cell["content"].anchored_position[1]
175186
+ _measured_height
176187
+ self.cell_padding
177-
)
188+
) - 1
178189
_bottom_line_loc_x = (
179190
cell["content"].anchored_position[0] - self.cell_padding
180191
)
@@ -193,7 +204,7 @@ def _layout_cells(self):
193204
cell["content"].anchored_position[0]
194205
+ _measured_width
195206
+ self.cell_padding
196-
)
207+
) - 1
197208

198209
_horizontal_divider_line = displayio.Shape(
199210
_measured_width + (2 * self.cell_padding),
@@ -240,41 +251,31 @@ def _layout_cells(self):
240251
for line_obj in self._divider_lines:
241252
self.remove(line_obj["tilegrid"])
242253

243-
print("pos_y: {} - size_y: {}".format(grid_position_y, grid_size_y))
244-
print(grid_position_y == grid_size_y)
245254
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
248256
):
249257
self._divider_lines.append(
250258
{
251259
"shape": _horizontal_divider_line,
252260
"tilegrid": _bottom_divider_tilegrid,
253261
}
254262
)
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:
259264
self._divider_lines.append(
260265
{
261266
"shape": _horizontal_divider_line,
262267
"tilegrid": _top_divider_tilegrid,
263268
}
264269
)
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:
269271
self._divider_lines.append(
270272
{
271273
"shape": _horizontal_divider_line,
272274
"tilegrid": _left_divider_tilegrid,
273275
}
274276
)
275277
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
278279
):
279280
self._divider_lines.append(
280281
{

examples/displayio_layout_gridlayout_dividers.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@
5656
height=80,
5757
grid_size=(2, 3),
5858
cell_padding=4,
59-
divider_lines=True,
6059
h_divider_line_rows=(1, 2), # Lines before rows 1 and 2
61-
v_divider_line_cols=(), # No vertical divider lines
6260
)
6361

6462
other_layout.add_content(

0 commit comments

Comments
 (0)