Skip to content

Commit 53b190e

Browse files
committed
GridLayout cell contains
1 parent f5a8f6a commit 53b190e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

adafruit_displayio_layout/layouts/grid_layout.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,21 @@ def get_cell(self, cell_coordinates: Tuple[int, int]) -> displayio.Group:
406406
:return: the displayio content object at those coordinates
407407
"""
408408
for index, cell in enumerate(self._cell_content_list):
409+
# exact location 1x1 cell
409410
if cell["grid_position"] == cell_coordinates:
410411
return self._cell_content_list[index]["content"]
411412

413+
# multi-spanning cell, any size bigger than 1x1
414+
if (
415+
cell["grid_position"][0]
416+
<= cell_coordinates[0]
417+
< cell["grid_position"][0] + cell["cell_size"][0]
418+
and cell["grid_position"][1]
419+
<= cell_coordinates[1]
420+
< cell["grid_position"][1] + cell["cell_size"][1]
421+
):
422+
return self._cell_content_list[index]["content"]
423+
412424
raise KeyError(
413425
"GridLayout does not contain cell at coordinates {}".format(
414426
cell_coordinates
@@ -425,3 +437,40 @@ def cell_size_pixels(self) -> Tuple[int, int]:
425437
pixels of a 1x1 cell in the GridLayout
426438
"""
427439
return (self._width // self.grid_size[0], self._height // self.grid_size[1])
440+
441+
@property
442+
def width(self) -> int:
443+
"""
444+
The width in pixels of the GridLayout.
445+
"""
446+
return self._width
447+
448+
@property
449+
def height(self) -> int:
450+
"""
451+
The width in pixels of the GridLayout.
452+
"""
453+
return self._height
454+
455+
def which_cell_contains(
456+
self, pixel_location: Union[Tuple[int, int], List[int]]
457+
) -> Optional[tuple]:
458+
"""
459+
Given a pixel x,y coordinate returns the location of the cell
460+
that contains the coordinate.
461+
462+
:param pixel_location: x,y pixel coordinate as a tuple or list
463+
:returns: cell coordinates x,y tuple or None if the pixel coordinates are
464+
outside the bounds of the GridLayout
465+
"""
466+
cell_size = self.cell_size_pixels
467+
if (
468+
not self.x <= pixel_location[0] < self.x + self.width
469+
or not self.y <= pixel_location[1] < self.y + self.height
470+
):
471+
return None
472+
473+
cell_x_coord = (pixel_location[0] - self.x) // cell_size[0]
474+
cell_y_coord = (pixel_location[1] - self.y) // cell_size[1]
475+
476+
return cell_x_coord, cell_y_coord

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# autodoc module docs will fail to generate with a warning.
3333
autodoc_mock_imports = [
3434
"vectorio",
35+
"terminalio",
3536
"bitmaptools",
3637
]
3738

0 commit comments

Comments
 (0)