X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka_Displayio.git/blobdiff_plain/8dc304ff4f82d37e493b9500e17fb13799dc200f..e46cbbe709e52c0a1ce5bd8107440ec78919bc6c:/displayio/_tilegrid.py diff --git a/displayio/_tilegrid.py b/displayio/_tilegrid.py index 72e57c0..91b3232 100644 --- a/displayio/_tilegrid.py +++ b/displayio/_tilegrid.py @@ -19,12 +19,17 @@ displayio for Blinka import struct from typing import Union, Optional, Tuple +from circuitpython_typing import WriteableBuffer from ._bitmap import Bitmap from ._colorconverter import ColorConverter from ._ondiskbitmap import OnDiskBitmap from ._shape import Shape from ._palette import Palette -from ._structs import TransformStruct, InputPixelStruct, OutputPixelStruct +from ._structs import ( + InputPixelStruct, + OutputPixelStruct, + null_transform, +) from ._colorspace import Colorspace from ._area import Area @@ -33,7 +38,7 @@ __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git" class TileGrid: - # pylint: disable=too-many-instance-attributes + # pylint: disable=too-many-instance-attributes, too-many-statements """Position a grid of tiles sourced from a bitmap and pixel_shader combination. Multiple grids can share bitmaps and pixel shaders. @@ -75,6 +80,7 @@ class TileGrid: self._hidden_tilegrid = False self._hidden_by_parent = False self._rendered_hidden = False + self._name = "Tilegrid" self._x = x self._y = y self._width_in_tiles = width @@ -104,7 +110,7 @@ class TileGrid: (self._width_in_tiles * self._height_in_tiles) * [default_tile] ) self._in_group = False - self._absolute_transform = TransformStruct(0, 0, 1, 1, 1, False, False, False) + self._absolute_transform = None self._current_area = Area(0, 0, self._pixel_width, self._pixel_height) self._dirty_area = Area(0, 0, 0, 0) self._previous_area = Area(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF) @@ -118,8 +124,10 @@ class TileGrid: def _update_transform(self, absolute_transform): """Update the parent transform and child transforms""" + self._in_group = absolute_transform is not None self._absolute_transform = absolute_transform if self._absolute_transform is not None: + self._moved = True self._update_current_x() self._update_current_y() @@ -129,13 +137,18 @@ class TileGrid: else: width = self._pixel_width - if self._absolute_transform.transpose_xy: + absolute_transform = ( + null_transform + if self._absolute_transform is None + else self._absolute_transform + ) + + if absolute_transform.transpose_xy: self._current_area.y1 = ( - self._absolute_transform.y + self._absolute_transform.dy * self._x + absolute_transform.y + absolute_transform.dy * self._x ) - self._current_area.y2 = ( - self._absolute_transform.y - + self._absolute_transform.dy * (self._x + width) + self._current_area.y2 = absolute_transform.y + absolute_transform.dy * ( + self._x + width ) if self._current_area.y2 < self._current_area.y1: self._current_area.y1, self._current_area.y2 = ( @@ -144,11 +157,10 @@ class TileGrid: ) else: self._current_area.x1 = ( - self._absolute_transform.x + self._absolute_transform.dx * self._x + absolute_transform.x + absolute_transform.dx * self._x ) - self._current_area.x2 = ( - self._absolute_transform.x - + self._absolute_transform.dx * (self._x + width) + self._current_area.x2 = absolute_transform.x + absolute_transform.dx * ( + self._x + width ) if self._current_area.x2 < self._current_area.x1: self._current_area.x1, self._current_area.x2 = ( @@ -162,13 +174,18 @@ class TileGrid: else: height = self._pixel_height - if self._absolute_transform.transpose_xy: + absolute_transform = ( + null_transform + if self._absolute_transform is None + else self._absolute_transform + ) + + if absolute_transform.transpose_xy: self._current_area.x1 = ( - self._absolute_transform.x + self._absolute_transform.dx * self._y + absolute_transform.x + absolute_transform.dx * self._y ) - self._current_area.x2 = ( - self._absolute_transform.x - + self._absolute_transform.dx * (self._y + height) + self._current_area.x2 = absolute_transform.x + absolute_transform.dx * ( + self._y + height ) if self._current_area.x2 < self._current_area.x1: self._current_area.x1, self._current_area.x2 = ( @@ -177,11 +194,10 @@ class TileGrid: ) else: self._current_area.y1 = ( - self._absolute_transform.y + self._absolute_transform.dy * self._y + absolute_transform.y + absolute_transform.dy * self._y ) - self._current_area.y2 = ( - self._absolute_transform.y - + self._absolute_transform.dy * (self._y + height) + self._current_area.y2 = absolute_transform.y + absolute_transform.dy * ( + self._y + height ) if self._current_area.y2 < self._current_area.y1: self._current_area.y1, self._current_area.y2 = ( @@ -211,7 +227,11 @@ class TileGrid: image.putalpha(alpha.convert("L")) def _fill_area( - self, colorspace: Colorspace, area: Area, mask: bytearray, buffer: bytearray + self, + colorspace: Colorspace, + area: Area, + mask: WriteableBuffer, + buffer: WriteableBuffer, ) -> bool: """Draw onto the image""" # pylint: disable=too-many-locals,too-many-branches,too-many-statements @@ -219,11 +239,14 @@ class TileGrid: # If no tiles are present we have no impact tiles = self._tiles + if tiles is None or len(tiles) == 0: + return False + if self._hidden_tilegrid or self._hidden_by_parent: return False - overlap = Area() - if not self._current_area.compute_overlap(area, overlap): + overlap = Area() # area, current_area, overlap + if not area.compute_overlap(self._current_area, overlap): return False if self._bitmap.width <= 0 or self._bitmap.height <= 0: @@ -245,6 +268,8 @@ class TileGrid: start += (area.y2 - area.y1 - 1) * y_stride y_stride *= -1 + # Track if this layer finishes filling in the given area. We can ignore any remaining + # layers at that point. full_coverage = area == overlap transformed = Area() @@ -271,6 +296,7 @@ class TileGrid: else: y_shift = overlap.y1 - area.y1 + # This untransposes x and y so it aligns with bitmap rows if self._transpose_xy != self._absolute_transform.transpose_xy: x_stride, y_stride = y_stride, x_stride x_shift, y_shift = y_shift, x_shift @@ -285,6 +311,7 @@ class TileGrid: ) # In Pixels local_y = input_pixel.y // self._absolute_transform.scale for input_pixel.x in range(start_x, end_x): + # Compute the destination pixel in the buffer and mask based on the transformations offset = ( row_start + (input_pixel.x - start_x + x_shift) * x_stride ) # In Pixels @@ -307,13 +334,19 @@ class TileGrid: input_pixel.tile // self._bitmap_width_in_tiles ) * self._tile_height + local_y % self._tile_height - input_pixel.pixel = ( - self._bitmap._get_pixel( # pylint: disable=protected-access - input_pixel.tile_x, input_pixel.tile_y + output_pixel.pixel = 0 + input_pixel.pixel = 0 + + # We always want to read bitmap pixels by row first and then transpose into + # the destination buffer because most bitmaps are row associated. + if isinstance(self._bitmap, (Bitmap, Shape, OnDiskBitmap)): + input_pixel.pixel = ( + self._bitmap._get_pixel( # pylint: disable=protected-access + input_pixel.tile_x, input_pixel.tile_y + ) ) - ) - output_pixel.opaque = True + output_pixel.opaque = True if self._pixel_shader is None: output_pixel.pixel = input_pixel.pixel elif isinstance(self._pixel_shader, Palette): @@ -330,16 +363,18 @@ class TileGrid: else: mask[offset // 32] |= 1 << (offset % 32) if colorspace.depth == 16: - buffer = ( - buffer[:offset] - + struct.pack("H", output_pixel.pixel) - + buffer[offset + 2 :] + struct.pack_into( + "H", + buffer, + offset * 2, + output_pixel.pixel, ) elif colorspace.depth == 32: - buffer = ( - buffer[:offset] - + struct.pack("I", output_pixel.pixel) - + buffer[offset + 4 :] + struct.pack_into( + "I", + buffer, + offset * 4, + output_pixel.pixel, ) elif colorspace.depth == 8: buffer[offset] = output_pixel.pixel & 0xFF @@ -400,22 +435,24 @@ class TileGrid: areas.append(self._previous_area) return - tail = areas[-1] + tail = areas[-1] if areas else None # If we have an in-memory bitmap, then check it for modifications if isinstance(self._bitmap, Bitmap): self._bitmap._get_refresh_areas(areas) # pylint: disable=protected-access - if tail != areas[-1]: + refresh_area = areas[-1] if areas else None + if tail != refresh_area: # Special case a TileGrid that shows a full bitmap and use its # dirty area. Copy it to ours so we can transform it. if self._tiles_in_bitmap == 1: - areas[-1].copy_into(self._dirty_area) + refresh_area.copy_into(self._dirty_area) self._partial_change = True else: self._full_change = True elif isinstance(self._bitmap, Shape): self._bitmap._get_refresh_areas(areas) # pylint: disable=protected-access - if areas[-1] != tail: - areas[-1].copy_into(self._dirty_area) + refresh_area = areas[-1] if areas else None + if refresh_area != tail: + refresh_area.copy_into(self._dirty_area) self._partial_change = True self._full_change = self._full_change or ( @@ -468,6 +505,35 @@ class TileGrid: ) areas.append(self._dirty_area) + def _set_hidden(self, hidden: bool) -> None: + self._hidden_tilegrid = hidden + self._rendered_hidden = False + if not hidden: + self._full_change = True + + def _set_hidden_by_parent(self, hidden: bool) -> None: + self._hidden_by_parent = hidden + self._rendered_hidden = False + if not hidden: + self._full_change = True + + def _get_rendered_hidden(self) -> bool: + return self._rendered_hidden + + def _set_all_tiles(self, tile_index: int) -> None: + """Set all tiles to the given tile index""" + if tile_index >= self._tiles_in_bitmap: + raise ValueError("Tile index out of bounds") + self._tiles = bytearray( + (self._width_in_tiles * self._height_in_tiles) * [tile_index] + ) + self._full_change = True + + def _set_top_left(self, x: int, y: int) -> None: + self._top_left_x = x + self._top_left_y = y + self._full_change = True + @property def hidden(self) -> bool: """True when the TileGrid is hidden. This may be False even @@ -478,7 +544,8 @@ class TileGrid: def hidden(self, value: bool): if not isinstance(value, (bool, int)): raise ValueError("Expecting a boolean or integer value") - self._hidden_tilegrid = bool(value) + value = bool(value) + self._set_hidden(value) @property def x(self) -> int: @@ -490,8 +557,10 @@ class TileGrid: if not isinstance(value, int): raise TypeError("X should be a integer type") if self._x != value: + self._moved = True self._x = value - self._update_current_x() + if self._absolute_transform is not None: + self._update_current_x() @property def y(self) -> int: @@ -503,8 +572,10 @@ class TileGrid: if not isinstance(value, int): raise TypeError("Y should be a integer type") if self._y != value: + self._moved = True self._y = value - self._update_current_y() + if self._absolute_transform is not None: + self._update_current_y() @property def flip_x(self) -> bool: @@ -517,6 +588,7 @@ class TileGrid: raise TypeError("Flip X should be a boolean type") if self._flip_x != value: self._flip_x = value + self._full_change = True @property def flip_y(self) -> bool: @@ -529,6 +601,7 @@ class TileGrid: raise TypeError("Flip Y should be a boolean type") if self._flip_y != value: self._flip_y = value + self._full_change = True @property def transpose_xy(self) -> bool: @@ -538,13 +611,17 @@ class TileGrid: return self._transpose_xy @transpose_xy.setter - def transpose_xy(self, value: bool): + def transpose_xy(self, value: bool) -> None: if not isinstance(value, bool): raise TypeError("Transpose XY should be a boolean type") if self._transpose_xy != value: self._transpose_xy = value + if self._pixel_width == self._pixel_height: + self._full_change = True + return self._update_current_x() self._update_current_y() + self._moved = True @property def pixel_shader(self) -> Union[ColorConverter, Palette]: @@ -561,6 +638,7 @@ class TileGrid: ) self._pixel_shader = new_pixel_shader + self._full_change = True @property def bitmap(self) -> Union[Bitmap, OnDiskBitmap, Shape]: @@ -585,6 +663,7 @@ class TileGrid: raise ValueError("New bitmap must be same size as old bitmap") self._bitmap = new_bitmap + self._full_change = True def _extract_and_check_index(self, index): if isinstance(index, (tuple, list)):