Skip to content

Gridlayout cell contains #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions adafruit_displayio_layout/layouts/grid_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,14 @@ def __init__(
):
self.cell_padding = 1

def layout_cells(self):
"""render the grid with all cell content and dividers"""
self._layout_cells()

def _layout_cells(self) -> None:
# pylint: disable=too-many-locals, too-many-branches, too-many-statements
for line_obj in self._divider_lines:
self.remove(line_obj["rect"])
for cell in self._cell_content_list:
if cell["content"] not in self:
grid_size_x = self.grid_size[0]
Expand Down Expand Up @@ -297,9 +303,6 @@ def _layout_cells(self) -> None:
x=_right_line_loc_x,
)

for line_obj in self._divider_lines:
self.remove(line_obj["rect"])

"""
Only use bottom divider lines on the bottom row. All
other rows rely on top divder lines of the row beneath them.
Expand Down Expand Up @@ -359,15 +362,16 @@ def _layout_cells(self) -> None:
}
)

for line_obj in self._divider_lines:
self.append(line_obj["rect"])
for line_obj in self._divider_lines:
self.append(line_obj["rect"])

def add_content(
self,
cell_content: displayio.Group,
grid_position: Tuple[int, int],
cell_size: Tuple[int, int],
cell_anchor_point: Optional[Tuple[float, ...]] = None,
layout_cells=True,
) -> None:
"""Add a child to the grid.

Expand Down Expand Up @@ -395,7 +399,8 @@ def add_content(
"cell_size": cell_size,
}
self._cell_content_list.append(sub_view_obj)
self._layout_cells()
if layout_cells:
self._layout_cells()

def get_cell(self, cell_coordinates: Tuple[int, int]) -> displayio.Group:
"""
Expand All @@ -406,9 +411,21 @@ def get_cell(self, cell_coordinates: Tuple[int, int]) -> displayio.Group:
:return: the displayio content object at those coordinates
"""
for index, cell in enumerate(self._cell_content_list):
# exact location 1x1 cell
if cell["grid_position"] == cell_coordinates:
return self._cell_content_list[index]["content"]

# multi-spanning cell, any size bigger than 1x1
if (
cell["grid_position"][0]
<= cell_coordinates[0]
< cell["grid_position"][0] + cell["cell_size"][0]
and cell["grid_position"][1]
<= cell_coordinates[1]
< cell["grid_position"][1] + cell["cell_size"][1]
):
return self._cell_content_list[index]["content"]

raise KeyError(
"GridLayout does not contain cell at coordinates {}".format(
cell_coordinates
Expand All @@ -425,3 +442,40 @@ def cell_size_pixels(self) -> Tuple[int, int]:
pixels of a 1x1 cell in the GridLayout
"""
return (self._width // self.grid_size[0], self._height // self.grid_size[1])

@property
def width(self) -> int:
"""
The width in pixels of the GridLayout.
"""
return self._width

@property
def height(self) -> int:
"""
The height in pixels of the GridLayout.
"""
return self._height

def which_cell_contains(
self, pixel_location: Union[Tuple[int, int], List[int]]
) -> Optional[tuple]:
"""
Given a pixel x,y coordinate returns the location of the cell
that contains the coordinate.

:param pixel_location: x,y pixel coordinate as a tuple or list
:returns: cell coordinates x,y tuple or None if the pixel coordinates are
outside the bounds of the GridLayout
"""
cell_size = self.cell_size_pixels
if (
not self.x <= pixel_location[0] < self.x + self.width
or not self.y <= pixel_location[1] < self.y + self.height
):
return None

cell_x_coord = (pixel_location[0] - self.x) // cell_size[0]
cell_y_coord = (pixel_location[1] - self.y) // cell_size[1]

return cell_x_coord, cell_y_coord
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
# autodoc module docs will fail to generate with a warning.
autodoc_mock_imports = [
"vectorio",
"terminalio",
"bitmaptools",
]

Expand Down