@@ -406,9 +406,21 @@ def get_cell(self, cell_coordinates: Tuple[int, int]) -> displayio.Group:
406
406
:return: the displayio content object at those coordinates
407
407
"""
408
408
for index , cell in enumerate (self ._cell_content_list ):
409
+ # exact location 1x1 cell
409
410
if cell ["grid_position" ] == cell_coordinates :
410
411
return self ._cell_content_list [index ]["content" ]
411
412
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
+
412
424
raise KeyError (
413
425
"GridLayout does not contain cell at coordinates {}" .format (
414
426
cell_coordinates
@@ -425,3 +437,40 @@ def cell_size_pixels(self) -> Tuple[int, int]:
425
437
pixels of a 1x1 cell in the GridLayout
426
438
"""
427
439
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
0 commit comments