diff --git a/shared-bindings/displayio/TileGrid.c b/shared-bindings/displayio/TileGrid.c index d084d94be2..d84ff5f20f 100644 --- a/shared-bindings/displayio/TileGrid.c +++ b/shared-bindings/displayio/TileGrid.c @@ -322,6 +322,24 @@ MP_PROPERTY_GETSET(displayio_tilegrid_transpose_xy_obj, (mp_obj_t)&displayio_tilegrid_get_transpose_xy_obj, (mp_obj_t)&displayio_tilegrid_set_transpose_xy_obj); +//| def contains(self, touch_tuple: tuple) -> bool: +//| """Returns True if the first two values in ``touch_tuple`` represent an x,y coordinate +//| inside the tilegrid rectangle bounds.""" +//| +STATIC mp_obj_t displayio_tilegrid_obj_contains(mp_obj_t self_in, mp_obj_t touch_tuple) { + displayio_tilegrid_t *self = MP_OBJ_TO_PTR(self_in); + + mp_obj_t *touch_tuple_items; + mp_obj_get_array_fixed_n(touch_tuple, 3, &touch_tuple_items); + uint16_t x = 0; + uint16_t y = 0; + x = mp_obj_get_int(touch_tuple_items[0]); + y = mp_obj_get_int(touch_tuple_items[1]); + + return mp_obj_new_bool(common_hal_displayio_tilegrid_contains(self, x, y)); +} +MP_DEFINE_CONST_FUN_OBJ_2(displayio_tilegrid_contains_obj, displayio_tilegrid_obj_contains); + //| pixel_shader: Union[ColorConverter, Palette] //| """The pixel shader of the tilegrid.""" //| @@ -484,6 +502,7 @@ STATIC const mp_rom_map_elem_t displayio_tilegrid_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_flip_x), MP_ROM_PTR(&displayio_tilegrid_flip_x_obj) }, { MP_ROM_QSTR(MP_QSTR_flip_y), MP_ROM_PTR(&displayio_tilegrid_flip_y_obj) }, { MP_ROM_QSTR(MP_QSTR_transpose_xy), MP_ROM_PTR(&displayio_tilegrid_transpose_xy_obj) }, + { MP_ROM_QSTR(MP_QSTR_contains), MP_ROM_PTR(&displayio_tilegrid_contains_obj) }, { MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&displayio_tilegrid_pixel_shader_obj) }, { MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&displayio_tilegrid_bitmap_obj) }, }; diff --git a/shared-bindings/displayio/TileGrid.h b/shared-bindings/displayio/TileGrid.h index 2c7941222f..2b4e42cb46 100644 --- a/shared-bindings/displayio/TileGrid.h +++ b/shared-bindings/displayio/TileGrid.h @@ -56,6 +56,8 @@ void common_hal_displayio_tilegrid_set_flip_y(displayio_tilegrid_t *self, bool f bool common_hal_displayio_tilegrid_get_transpose_xy(displayio_tilegrid_t *self); void common_hal_displayio_tilegrid_set_transpose_xy(displayio_tilegrid_t *self, bool transpose_xy); +bool common_hal_displayio_tilegrid_contains(displayio_tilegrid_t *self, uint16_t x, uint16_t y); + uint16_t common_hal_displayio_tilegrid_get_width(displayio_tilegrid_t *self); uint16_t common_hal_displayio_tilegrid_get_height(displayio_tilegrid_t *self); diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c index 5c968c30e1..36cbf76619 100644 --- a/shared-module/displayio/TileGrid.c +++ b/shared-module/displayio/TileGrid.c @@ -342,6 +342,13 @@ void common_hal_displayio_tilegrid_set_transpose_xy(displayio_tilegrid_t *self, self->moved = true; } +bool common_hal_displayio_tilegrid_contains(displayio_tilegrid_t *self, uint16_t x, uint16_t y) { + uint16_t right_edge = self->x + (self->width_in_tiles * self->tile_width); + uint16_t bottom_edge = self->y + (self->height_in_tiles * self->tile_height); + return x >= self->x && x < right_edge && + y >= self->y && y < bottom_edge; +} + void common_hal_displayio_tilegrid_set_top_left(displayio_tilegrid_t *self, uint16_t x, uint16_t y) { self->top_left_x = x; self->top_left_y = y;