From 53124802d908dc8776cecf680fd17f2888083786 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 6 Aug 2021 12:46:15 -0500 Subject: [PATCH 1/4] starting divider lines --- .../layouts/grid_layout.py | 61 ++++++++++++------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/adafruit_displayio_layout/layouts/grid_layout.py b/adafruit_displayio_layout/layouts/grid_layout.py index 807774c..4746543 100644 --- a/adafruit_displayio_layout/layouts/grid_layout.py +++ b/adafruit_displayio_layout/layouts/grid_layout.py @@ -30,7 +30,6 @@ class GridLayout(displayio.Group): - """ A layout that organizes children into a grid table structure. @@ -40,10 +39,11 @@ class GridLayout(displayio.Group): :param int height: Height of the layout in pixels. :param tuple grid_size: Size in cells as two ints in a tuple e.g. (2, 2) :param int cell_padding: Extra padding space inside each cell. In pixels. + :param bool divider_lines: Whether or not to draw lines between the cells. """ # pylint: disable=too-many-arguments - def __init__(self, x, y, width, height, grid_size, cell_padding): + def __init__(self, x, y, width, height, grid_size, cell_padding, divider_lines): super().__init__(x=x, y=y) self.x = x self.y = y @@ -52,6 +52,8 @@ def __init__(self, x, y, width, height, grid_size, cell_padding): self.grid_size = grid_size self.cell_padding = cell_padding self._cell_content_list = [] + self._divider_lines_enabled = divider_lines + self._divider_lines = [] def _layout_cells(self): @@ -66,43 +68,60 @@ def _layout_cells(self): button_size_x = cell["cell_size"][0] button_size_y = cell["cell_size"][1] + _measured_width = ( + int(button_size_x * self._width / grid_size_x) + - 2 * self.cell_padding + ) + + _measured_height = ( + int(button_size_y * self._height / grid_size_y) + - 2 * self.cell_padding + ) if hasattr(cell["content"], "resize"): # if it has resize function cell["content"].resize( - ( - int(button_size_x * self._width / grid_size_x) - - 2 * self.cell_padding - ), - ( - int(button_size_y * self._height / grid_size_y) - - 2 * self.cell_padding - ), + _measured_width, + _measured_height, ) else: try: # try width and height properties. - cell["content"].width = ( - int(button_size_x * self._width / grid_size_x) - - 2 * self.cell_padding - ) - cell["content"].height = ( - int(button_size_y * self._height / grid_size_y) - - 2 * self.cell_padding - ) + cell["content"].width = _measured_width + cell["content"].height = _measured_height except AttributeError: # This element does not allow setting width and height. # No problem, we'll use whatever size it already is. + _measured_width = cell["content"].width + _measured_height = cell["content"].height + pass cell["content"].x = ( - int(grid_position_x * self._width / grid_size_x) + self.cell_padding + int(grid_position_x * self._width / grid_size_x) + self.cell_padding ) cell["content"].y = ( - int(grid_position_y * self._height / grid_size_y) - + self.cell_padding + int(grid_position_y * self._height / grid_size_y) + + self.cell_padding ) + palette = displayio.Palette(2) + palette[0] = 0xFFFFFF + palette[1] = 0xFFFFFF + + _bottom_divider_line = displayio.Shape(_measured_width, _measured_height, mirror_x=False, + mirror_y=False) + + _bottom_divider_tilegrid = displayio.TileGrid( + _bottom_divider_line, pixel_shader=palette, + y=cell["content"].y + _measured_height) + + self._divider_lines.append({ + "shape": _bottom_divider_line, + "tilegrid": _bottom_divider_tilegrid + }) + self.append(cell["content"]) + self.append(_bottom_divider_tilegrid) def add_content(self, cell_content, grid_position, cell_size): """Add a child to the grid. From 813b843a90e37f91df283c5b68801e380e7e4478 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 7 Aug 2021 11:27:11 -0500 Subject: [PATCH 2/4] basic divider_lines argument implementation --- .../layouts/grid_layout.py | 120 ++++++++++++++---- 1 file changed, 96 insertions(+), 24 deletions(-) diff --git a/adafruit_displayio_layout/layouts/grid_layout.py b/adafruit_displayio_layout/layouts/grid_layout.py index 4746543..0b14f4f 100644 --- a/adafruit_displayio_layout/layouts/grid_layout.py +++ b/adafruit_displayio_layout/layouts/grid_layout.py @@ -91,37 +91,109 @@ def _layout_cells(self): except AttributeError: # This element does not allow setting width and height. # No problem, we'll use whatever size it already is. - _measured_width = cell["content"].width - _measured_height = cell["content"].height + # _measured_width = cell["content"].width + # _measured_height = cell["content"].height pass - cell["content"].x = ( - int(grid_position_x * self._width / grid_size_x) + self.cell_padding - ) - cell["content"].y = ( - int(grid_position_y * self._height / grid_size_y) - + self.cell_padding - ) - - palette = displayio.Palette(2) - palette[0] = 0xFFFFFF - palette[1] = 0xFFFFFF - - _bottom_divider_line = displayio.Shape(_measured_width, _measured_height, mirror_x=False, - mirror_y=False) + if not hasattr(cell["content"], "anchor_point"): - _bottom_divider_tilegrid = displayio.TileGrid( - _bottom_divider_line, pixel_shader=palette, - y=cell["content"].y + _measured_height) + cell["content"].x = ( + int(grid_position_x * self._width / grid_size_x) + self.cell_padding + ) + cell["content"].y = ( + int(grid_position_y * self._height / grid_size_y) + self.cell_padding + ) + else: + print("int({} * {} / {}) + {}".format(grid_position_x, self._width, grid_size_x, self.cell_padding)) + print( + "int({} * {} / {}) + {}".format(grid_position_y, self._height, grid_size_y, self.cell_padding)) - self._divider_lines.append({ - "shape": _bottom_divider_line, - "tilegrid": _bottom_divider_tilegrid - }) + cell["content"].anchor_point = (0, 0) + cell["content"].anchored_position = ( + int(grid_position_x * self._width / grid_size_x) + self.cell_padding, + int(grid_position_y * self._height / grid_size_y) + self.cell_padding) + print(cell["content"].anchored_position) + print("---") self.append(cell["content"]) - self.append(_bottom_divider_tilegrid) + + if self._divider_lines_enabled: + palette = displayio.Palette(2) + palette[0] = 0xFFFFFF + palette[1] = 0xFFFFFF + + if not hasattr(cell["content"], "anchor_point"): + _bottom_line_loc_y = cell["content"].y + _measured_height + self.cell_padding + _bottom_line_loc_x = cell["content"].x - self.cell_padding + + _top_line_loc_y = cell["content"].y - self.cell_padding + _top_line_loc_x = cell["content"].x - self.cell_padding + + _right_line_loc_y = cell["content"].y - self.cell_padding + _right_line_loc_x = cell["content"].x + _measured_width + self.cell_padding + else: + _bottom_line_loc_y = cell["content"].anchored_position[1] + _measured_height + self.cell_padding + _bottom_line_loc_x = cell["content"].anchored_position[0] - self.cell_padding + + _top_line_loc_y = cell["content"].anchored_position[1] - self.cell_padding + _top_line_loc_x = cell["content"].anchored_position[0] - self.cell_padding + + _right_line_loc_y = cell["content"].anchored_position[1] - self.cell_padding + _right_line_loc_x = cell["content"].anchored_position[0] + _measured_width + self.cell_padding + + _horizontal_divider_line = displayio.Shape( + _measured_width + (2 * self.cell_padding), + 1, + mirror_x=False, mirror_y=False) + + _bottom_divider_tilegrid = displayio.TileGrid( + _horizontal_divider_line, pixel_shader=palette, + y=_bottom_line_loc_y, + x=_bottom_line_loc_x) + + _top_divider_tilegrid = displayio.TileGrid( + _horizontal_divider_line, pixel_shader=palette, + y=_top_line_loc_y, + x=_top_line_loc_x) + + _vertical_divider_line = displayio.Shape( + 1, + _measured_height + (2 * self.cell_padding), + mirror_x=False, mirror_y=False) + + _left_divider_tilegrid = displayio.TileGrid( + _vertical_divider_line, pixel_shader=palette, + y=_top_line_loc_y, + x=_top_line_loc_x) + + _right_divider_tilegrid = displayio.TileGrid( + _vertical_divider_line, pixel_shader=palette, + y=_right_line_loc_y, + x=_right_line_loc_x) + + for line_obj in self._divider_lines: + self.remove(line_obj["tilegrid"]) + + self._divider_lines.append({ + "shape": _horizontal_divider_line, + "tilegrid": _bottom_divider_tilegrid + }) + self._divider_lines.append({ + "shape": _horizontal_divider_line, + "tilegrid": _top_divider_tilegrid + }) + self._divider_lines.append({ + "shape": _horizontal_divider_line, + "tilegrid": _left_divider_tilegrid + }) + self._divider_lines.append({ + "shape": _vertical_divider_line, + "tilegrid": _right_divider_tilegrid + }) + + for line_obj in self._divider_lines: + self.append(line_obj["tilegrid"]) def add_content(self, cell_content, grid_position, cell_size): """Add a child to the grid. From 16cd97dcda371baaf6997a8af62584f364aca830 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sun, 29 Aug 2021 15:55:29 -0500 Subject: [PATCH 3/4] configurable divider lines on GridLayout --- .../layouts/grid_layout.py | 189 +++++++++++++----- docs/examples.rst | 9 + .../displayio_layout_gridlayout_dividers.py | 87 ++++++++ 3 files changed, 236 insertions(+), 49 deletions(-) create mode 100644 examples/displayio_layout_gridlayout_dividers.py diff --git a/adafruit_displayio_layout/layouts/grid_layout.py b/adafruit_displayio_layout/layouts/grid_layout.py index 0b14f4f..f377060 100644 --- a/adafruit_displayio_layout/layouts/grid_layout.py +++ b/adafruit_displayio_layout/layouts/grid_layout.py @@ -39,11 +39,27 @@ class GridLayout(displayio.Group): :param int height: Height of the layout in pixels. :param tuple grid_size: Size in cells as two ints in a tuple e.g. (2, 2) :param int cell_padding: Extra padding space inside each cell. In pixels. - :param bool divider_lines: Whether or not to draw lines between the cells. + :param bool divider_lines: Whether or not to draw lines between the cells. Defaults to False. + :param tuple h_divider_line_rows: Row indexes to draw divider lines above. + Row indexes are 0 based. + :param tuple v_divider_line_cols: Column indexes to draw divider lines before. + Column indexes are 0 based. + """ # pylint: disable=too-many-arguments - def __init__(self, x, y, width, height, grid_size, cell_padding, divider_lines): + def __init__( + self, + x, + y, + width, + height, + grid_size, + cell_padding, + divider_lines=False, + h_divider_line_rows=None, + v_divider_line_cols=None, + ): super().__init__(x=x, y=y) self.x = x self.y = y @@ -54,9 +70,11 @@ def __init__(self, x, y, width, height, grid_size, cell_padding, divider_lines): self._cell_content_list = [] self._divider_lines_enabled = divider_lines self._divider_lines = [] + self.h_divider_line_rows = h_divider_line_rows + self.v_divider_line_cols = v_divider_line_cols def _layout_cells(self): - + # pylint: disable=too-many-locals, too-many-branches, too-many-statements for cell in self._cell_content_list: if cell["content"] not in self: grid_size_x = self.grid_size[0] @@ -69,13 +87,13 @@ def _layout_cells(self): button_size_y = cell["cell_size"][1] _measured_width = ( - int(button_size_x * self._width / grid_size_x) - - 2 * self.cell_padding + int(button_size_x * self._width / grid_size_x) + - 2 * self.cell_padding ) _measured_height = ( - int(button_size_y * self._height / grid_size_y) - - 2 * self.cell_padding + int(button_size_y * self._height / grid_size_y) + - 2 * self.cell_padding ) if hasattr(cell["content"], "resize"): # if it has resize function @@ -99,20 +117,35 @@ def _layout_cells(self): if not hasattr(cell["content"], "anchor_point"): cell["content"].x = ( - int(grid_position_x * self._width / grid_size_x) + self.cell_padding + int(grid_position_x * self._width / grid_size_x) + + self.cell_padding ) cell["content"].y = ( - int(grid_position_y * self._height / grid_size_y) + self.cell_padding + int(grid_position_y * self._height / grid_size_y) + + self.cell_padding ) else: - print("int({} * {} / {}) + {}".format(grid_position_x, self._width, grid_size_x, self.cell_padding)) print( - "int({} * {} / {}) + {}".format(grid_position_y, self._height, grid_size_y, self.cell_padding)) + "int({} * {} / {}) + {}".format( + grid_position_x, self._width, grid_size_x, self.cell_padding + ) + ) + print( + "int({} * {} / {}) + {}".format( + grid_position_y, + self._height, + grid_size_y, + self.cell_padding, + ) + ) cell["content"].anchor_point = (0, 0) cell["content"].anchored_position = ( - int(grid_position_x * self._width / grid_size_x) + self.cell_padding, - int(grid_position_y * self._height / grid_size_y) + self.cell_padding) + int(grid_position_x * self._width / grid_size_x) + + self.cell_padding, + int(grid_position_y * self._height / grid_size_y) + + self.cell_padding, + ) print(cell["content"].anchored_position) print("---") @@ -124,73 +157,131 @@ def _layout_cells(self): palette[1] = 0xFFFFFF if not hasattr(cell["content"], "anchor_point"): - _bottom_line_loc_y = cell["content"].y + _measured_height + self.cell_padding + _bottom_line_loc_y = ( + cell["content"].y + _measured_height + self.cell_padding + ) _bottom_line_loc_x = cell["content"].x - self.cell_padding _top_line_loc_y = cell["content"].y - self.cell_padding _top_line_loc_x = cell["content"].x - self.cell_padding _right_line_loc_y = cell["content"].y - self.cell_padding - _right_line_loc_x = cell["content"].x + _measured_width + self.cell_padding + _right_line_loc_x = ( + cell["content"].x + _measured_width + self.cell_padding + ) else: - _bottom_line_loc_y = cell["content"].anchored_position[1] + _measured_height + self.cell_padding - _bottom_line_loc_x = cell["content"].anchored_position[0] - self.cell_padding - - _top_line_loc_y = cell["content"].anchored_position[1] - self.cell_padding - _top_line_loc_x = cell["content"].anchored_position[0] - self.cell_padding - - _right_line_loc_y = cell["content"].anchored_position[1] - self.cell_padding - _right_line_loc_x = cell["content"].anchored_position[0] + _measured_width + self.cell_padding + _bottom_line_loc_y = ( + cell["content"].anchored_position[1] + + _measured_height + + self.cell_padding + ) + _bottom_line_loc_x = ( + cell["content"].anchored_position[0] - self.cell_padding + ) + + _top_line_loc_y = ( + cell["content"].anchored_position[1] - self.cell_padding + ) + _top_line_loc_x = ( + cell["content"].anchored_position[0] - self.cell_padding + ) + + _right_line_loc_y = ( + cell["content"].anchored_position[1] - self.cell_padding + ) + _right_line_loc_x = ( + cell["content"].anchored_position[0] + + _measured_width + + self.cell_padding + ) _horizontal_divider_line = displayio.Shape( _measured_width + (2 * self.cell_padding), 1, - mirror_x=False, mirror_y=False) + mirror_x=False, + mirror_y=False, + ) _bottom_divider_tilegrid = displayio.TileGrid( - _horizontal_divider_line, pixel_shader=palette, + _horizontal_divider_line, + pixel_shader=palette, y=_bottom_line_loc_y, - x=_bottom_line_loc_x) + x=_bottom_line_loc_x, + ) _top_divider_tilegrid = displayio.TileGrid( - _horizontal_divider_line, pixel_shader=palette, + _horizontal_divider_line, + pixel_shader=palette, y=_top_line_loc_y, - x=_top_line_loc_x) + x=_top_line_loc_x, + ) _vertical_divider_line = displayio.Shape( 1, _measured_height + (2 * self.cell_padding), - mirror_x=False, mirror_y=False) + mirror_x=False, + mirror_y=False, + ) _left_divider_tilegrid = displayio.TileGrid( - _vertical_divider_line, pixel_shader=palette, + _vertical_divider_line, + pixel_shader=palette, y=_top_line_loc_y, - x=_top_line_loc_x) + x=_top_line_loc_x, + ) _right_divider_tilegrid = displayio.TileGrid( - _vertical_divider_line, pixel_shader=palette, + _vertical_divider_line, + pixel_shader=palette, y=_right_line_loc_y, - x=_right_line_loc_x) + x=_right_line_loc_x, + ) for line_obj in self._divider_lines: self.remove(line_obj["tilegrid"]) - self._divider_lines.append({ - "shape": _horizontal_divider_line, - "tilegrid": _bottom_divider_tilegrid - }) - self._divider_lines.append({ - "shape": _horizontal_divider_line, - "tilegrid": _top_divider_tilegrid - }) - self._divider_lines.append({ - "shape": _horizontal_divider_line, - "tilegrid": _left_divider_tilegrid - }) - self._divider_lines.append({ - "shape": _vertical_divider_line, - "tilegrid": _right_divider_tilegrid - }) + print("pos_y: {} - size_y: {}".format(grid_position_y, grid_size_y)) + print(grid_position_y == grid_size_y) + if grid_position_y == grid_size_y - 1 and ( + self.h_divider_line_rows is None + or grid_position_y + 1 in self.h_divider_line_rows + ): + self._divider_lines.append( + { + "shape": _horizontal_divider_line, + "tilegrid": _bottom_divider_tilegrid, + } + ) + if ( + self.h_divider_line_rows is None + or grid_position_y in self.h_divider_line_rows + ): + self._divider_lines.append( + { + "shape": _horizontal_divider_line, + "tilegrid": _top_divider_tilegrid, + } + ) + if ( + self.v_divider_line_cols is None + or grid_position_x in self.v_divider_line_cols + ): + self._divider_lines.append( + { + "shape": _horizontal_divider_line, + "tilegrid": _left_divider_tilegrid, + } + ) + if grid_position_x == grid_size_x - 1 and ( + self.v_divider_line_cols is None + or grid_position_x + 1 in self.v_divider_line_cols + ): + self._divider_lines.append( + { + "shape": _vertical_divider_line, + "tilegrid": _right_divider_tilegrid, + } + ) for line_obj in self._divider_lines: self.append(line_obj["tilegrid"]) diff --git a/docs/examples.rst b/docs/examples.rst index 2c46c7f..b8d6bbc 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -7,6 +7,15 @@ Ensure your device works with this simple test. :caption: examples/displayio_layout_simpletest.py :linenos: +GridLayout divider lines example +-------------------------------- + +Create GridLayouts with divider lines. + +.. literalinclude:: ../examples/displayio_layout_gridlayout_dividers.py + :caption: examples/displayio_layout_gridlayout_dividers.py + :linenos: + Pygame simple test ------------------ diff --git a/examples/displayio_layout_gridlayout_dividers.py b/examples/displayio_layout_gridlayout_dividers.py new file mode 100644 index 0000000..3292d72 --- /dev/null +++ b/examples/displayio_layout_gridlayout_dividers.py @@ -0,0 +1,87 @@ +# SPDX-FileCopyrightText: 2021 Tim C, written for Adafruit Industries +# +# SPDX-License-Identifier: MIT +""" +Illustrate how to use divider lines with GridLayout +""" +import board +import displayio +import terminalio +from adafruit_display_text import label +from adafruit_displayio_layout.layouts.grid_layout import GridLayout + +# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.) +# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.) +# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus +display = board.DISPLAY + +# Make the display context +main_group = displayio.Group() +display.show(main_group) + +layout = GridLayout( + x=10, + y=10, + width=200, + height=100, + grid_size=(2, 2), + cell_padding=8, + divider_lines=True, # divider lines around every cell +) +_labels = [] + +_labels.append( + label.Label( + terminalio.FONT, scale=2, x=0, y=0, text="Hello", background_color=0x770077 + ) +) +layout.add_content(_labels[0], grid_position=(0, 0), cell_size=(1, 1)) +_labels.append( + label.Label( + terminalio.FONT, scale=2, x=0, y=0, text="World", background_color=0x007700 + ) +) +layout.add_content(_labels[1], grid_position=(1, 0), cell_size=(1, 1)) +_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Hello")) +layout.add_content(_labels[2], grid_position=(0, 1), cell_size=(1, 1)) +_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Grid")) +layout.add_content(_labels[3], grid_position=(1, 1), cell_size=(1, 1)) + +main_group.append(layout) + +other_layout = GridLayout( + x=10, + y=120, + width=140, + height=80, + grid_size=(2, 3), + cell_padding=4, + divider_lines=True, + h_divider_line_rows=(1, 2), # Lines before rows 1 and 2 + v_divider_line_cols=(), # No vertical divider lines +) + +other_layout.add_content( + label.Label(terminalio.FONT, text="0x0"), grid_position=(0, 0), cell_size=(1, 1) +) +other_layout.add_content( + label.Label(terminalio.FONT, text="0x1"), grid_position=(0, 1), cell_size=(1, 1) +) +other_layout.add_content( + label.Label(terminalio.FONT, text="0x2"), grid_position=(0, 2), cell_size=(1, 1) +) + +other_layout.add_content( + label.Label(terminalio.FONT, text="1x0"), grid_position=(1, 0), cell_size=(1, 1) +) +other_layout.add_content( + label.Label(terminalio.FONT, text="1x1"), grid_position=(1, 1), cell_size=(1, 1) +) +other_layout.add_content( + label.Label(terminalio.FONT, text="1x2"), grid_position=(1, 2), cell_size=(1, 1) +) + +main_group.append(other_layout) + +while True: + pass From 5a7940090ad602229bd845c547b50bd62838dda6 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 31 Aug 2021 20:59:14 -0500 Subject: [PATCH 4/4] move divider lines inside height and width of GridLayout. fix rounding error. change argument API --- .../layouts/grid_layout.py | 89 ++++++++++--------- .../displayio_layout_gridlayout_dividers.py | 2 - 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/adafruit_displayio_layout/layouts/grid_layout.py b/adafruit_displayio_layout/layouts/grid_layout.py index f377060..6c8bb8b 100644 --- a/adafruit_displayio_layout/layouts/grid_layout.py +++ b/adafruit_displayio_layout/layouts/grid_layout.py @@ -22,7 +22,7 @@ https://github.com/adafruit/circuitpython/releases """ - +import math import displayio __version__ = "0.0.0-auto.0" @@ -39,11 +39,11 @@ class GridLayout(displayio.Group): :param int height: Height of the layout in pixels. :param tuple grid_size: Size in cells as two ints in a tuple e.g. (2, 2) :param int cell_padding: Extra padding space inside each cell. In pixels. - :param bool divider_lines: Whether or not to draw lines between the cells. Defaults to False. - :param tuple h_divider_line_rows: Row indexes to draw divider lines above. - Row indexes are 0 based. - :param tuple v_divider_line_cols: Column indexes to draw divider lines before. - Column indexes are 0 based. + :param bool divider_lines: Whether or not to draw lines between the cells. + :param Union[tuple, list] h_divider_line_rows: Row indexes to draw divider + lines above. Row indexes are 0 based. + :param Union[tuple, list] v_divider_line_cols: Column indexes to draw divider + lines before. Column indexes are 0 based. """ @@ -55,7 +55,7 @@ def __init__( width, height, grid_size, - cell_padding, + cell_padding=0, divider_lines=False, h_divider_line_rows=None, v_divider_line_cols=None, @@ -68,11 +68,38 @@ def __init__( self.grid_size = grid_size self.cell_padding = cell_padding self._cell_content_list = [] - self._divider_lines_enabled = divider_lines + self._divider_lines = [] self.h_divider_line_rows = h_divider_line_rows self.v_divider_line_cols = v_divider_line_cols + self._divider_lines_enabled = ( + (divider_lines is True) + or (h_divider_line_rows is not None) + or (v_divider_line_cols is not None) + ) + + if divider_lines: + if self.h_divider_line_rows is None: + self.h_divider_line_rows = [] + for _y in range(self.grid_size[1] + 1): + self.h_divider_line_rows.append(_y) + if self.v_divider_line_cols is None: + self.v_divider_line_cols = [] + for _x in range(self.grid_size[0] + 1): + self.v_divider_line_cols.append(_x) + else: + if not h_divider_line_rows: + self.h_divider_line_rows = tuple() + if not v_divider_line_cols: + self.v_divider_line_cols = tuple() + + # use at least 1 padding so that content is inside the divider lines + if cell_padding == 0 and ( + divider_lines or h_divider_line_rows or v_divider_line_cols + ): + self.cell_padding = 1 + def _layout_cells(self): # pylint: disable=too-many-locals, too-many-branches, too-many-statements for cell in self._cell_content_list: @@ -87,12 +114,12 @@ def _layout_cells(self): button_size_y = cell["cell_size"][1] _measured_width = ( - int(button_size_x * self._width / grid_size_x) + math.ceil(button_size_x * self._width / grid_size_x) - 2 * self.cell_padding ) _measured_height = ( - int(button_size_y * self._height / grid_size_y) + math.ceil(button_size_y * self._height / grid_size_y) - 2 * self.cell_padding ) if hasattr(cell["content"], "resize"): @@ -125,20 +152,6 @@ def _layout_cells(self): + self.cell_padding ) else: - print( - "int({} * {} / {}) + {}".format( - grid_position_x, self._width, grid_size_x, self.cell_padding - ) - ) - print( - "int({} * {} / {}) + {}".format( - grid_position_y, - self._height, - grid_size_y, - self.cell_padding, - ) - ) - cell["content"].anchor_point = (0, 0) cell["content"].anchored_position = ( int(grid_position_x * self._width / grid_size_x) @@ -146,8 +159,6 @@ def _layout_cells(self): int(grid_position_y * self._height / grid_size_y) + self.cell_padding, ) - print(cell["content"].anchored_position) - print("---") self.append(cell["content"]) @@ -159,7 +170,7 @@ def _layout_cells(self): if not hasattr(cell["content"], "anchor_point"): _bottom_line_loc_y = ( cell["content"].y + _measured_height + self.cell_padding - ) + ) - 1 _bottom_line_loc_x = cell["content"].x - self.cell_padding _top_line_loc_y = cell["content"].y - self.cell_padding @@ -168,13 +179,13 @@ def _layout_cells(self): _right_line_loc_y = cell["content"].y - self.cell_padding _right_line_loc_x = ( cell["content"].x + _measured_width + self.cell_padding - ) + ) - 1 else: _bottom_line_loc_y = ( cell["content"].anchored_position[1] + _measured_height + self.cell_padding - ) + ) - 1 _bottom_line_loc_x = ( cell["content"].anchored_position[0] - self.cell_padding ) @@ -193,7 +204,7 @@ def _layout_cells(self): cell["content"].anchored_position[0] + _measured_width + self.cell_padding - ) + ) - 1 _horizontal_divider_line = displayio.Shape( _measured_width + (2 * self.cell_padding), @@ -240,11 +251,8 @@ def _layout_cells(self): for line_obj in self._divider_lines: self.remove(line_obj["tilegrid"]) - print("pos_y: {} - size_y: {}".format(grid_position_y, grid_size_y)) - print(grid_position_y == grid_size_y) if grid_position_y == grid_size_y - 1 and ( - self.h_divider_line_rows is None - or grid_position_y + 1 in self.h_divider_line_rows + grid_position_y + 1 in self.h_divider_line_rows ): self._divider_lines.append( { @@ -252,20 +260,14 @@ def _layout_cells(self): "tilegrid": _bottom_divider_tilegrid, } ) - if ( - self.h_divider_line_rows is None - or grid_position_y in self.h_divider_line_rows - ): + if grid_position_y in self.h_divider_line_rows: self._divider_lines.append( { "shape": _horizontal_divider_line, "tilegrid": _top_divider_tilegrid, } ) - if ( - self.v_divider_line_cols is None - or grid_position_x in self.v_divider_line_cols - ): + if grid_position_x in self.v_divider_line_cols: self._divider_lines.append( { "shape": _horizontal_divider_line, @@ -273,8 +275,7 @@ def _layout_cells(self): } ) if grid_position_x == grid_size_x - 1 and ( - self.v_divider_line_cols is None - or grid_position_x + 1 in self.v_divider_line_cols + grid_position_x + 1 in self.v_divider_line_cols ): self._divider_lines.append( { diff --git a/examples/displayio_layout_gridlayout_dividers.py b/examples/displayio_layout_gridlayout_dividers.py index 3292d72..f649562 100644 --- a/examples/displayio_layout_gridlayout_dividers.py +++ b/examples/displayio_layout_gridlayout_dividers.py @@ -56,9 +56,7 @@ height=80, grid_size=(2, 3), cell_padding=4, - divider_lines=True, h_divider_line_rows=(1, 2), # Lines before rows 1 and 2 - v_divider_line_cols=(), # No vertical divider lines ) other_layout.add_content(