X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka_Displayio.git/blobdiff_plain/e46cbbe709e52c0a1ce5bd8107440ec78919bc6c..b39699fa4b1ed4838b72d068f0c13fdf62ef95de:/displayio/_tilegrid.py diff --git a/displayio/_tilegrid.py b/displayio/_tilegrid.py index 91b3232..9287e98 100644 --- a/displayio/_tilegrid.py +++ b/displayio/_tilegrid.py @@ -244,10 +244,12 @@ class TileGrid: if self._hidden_tilegrid or self._hidden_by_parent: return False - overlap = Area() # area, current_area, overlap if not area.compute_overlap(self._current_area, overlap): return False + # else: + # print("Checking", area.x1, area.y1, area.x2, area.y2) + # print("Overlap", overlap.x1, overlap.y1, overlap.x2, overlap.y2) if self._bitmap.width <= 0 or self._bitmap.height <= 0: return False @@ -362,22 +364,23 @@ class TileGrid: full_coverage = False else: mask[offset // 32] |= 1 << (offset % 32) + # print("Mask", mask) if colorspace.depth == 16: struct.pack_into( "H", - buffer, - offset * 2, + buffer.cast("H"), + offset, output_pixel.pixel, ) elif colorspace.depth == 32: struct.pack_into( "I", buffer, - offset * 4, + offset, output_pixel.pixel, ) elif colorspace.depth == 8: - buffer[offset] = output_pixel.pixel & 0xFF + buffer.cast("B")[offset] = output_pixel.pixel & 0xFF elif colorspace.depth < 8: # Reorder the offsets to pack multiple rows into # a byte (meaning they share a column). @@ -389,14 +392,17 @@ class TileGrid: # even if we multiply it back out offset = ( col * pixels_per_byte - + (row // pixels_per_byte) * width + + (row // pixels_per_byte) * pixels_per_byte * width + (row % pixels_per_byte) ) shift = (offset % pixels_per_byte) * colorspace.depth if colorspace.reverse_pixels_in_byte: # Reverse the shift by subtracting it from the leftmost shift shift = (pixels_per_byte - 1) * colorspace.depth - shift - buffer[offset // pixels_per_byte] |= output_pixel.pixel << shift + buffer.cast("B")[offset // pixels_per_byte] |= ( + output_pixel.pixel << shift + ) + return full_coverage def _finish_refresh(self): @@ -440,7 +446,7 @@ class TileGrid: if isinstance(self._bitmap, Bitmap): self._bitmap._get_refresh_areas(areas) # pylint: disable=protected-access refresh_area = areas[-1] if areas else None - if tail != refresh_area: + if refresh_area != tail: # 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: @@ -529,6 +535,29 @@ class TileGrid: ) self._full_change = True + def _set_tile(self, x: int, y: int, tile_index: int) -> None: + self._tiles[y * self._width_in_tiles + x] = tile_index + temp_area = Area() + if not self._partial_change: + tile_area = self._dirty_area + else: + tile_area = temp_area + top_x = (x - self._top_left_x) % self._width_in_tiles + if top_x < 0: + top_x += self._width_in_tiles + tile_area.x1 = top_x * self._tile_width + tile_area.x2 = tile_area.x1 + self._tile_width + top_y = (y - self._top_left_y) % self._height_in_tiles + if top_y < 0: + top_y += self._height_in_tiles + tile_area.y1 = top_y * self._tile_height + tile_area.y2 = tile_area.y1 + self._tile_height + + if self._partial_change: + self._dirty_area.union(temp_area, self._dirty_area) + + self._partial_change = True + def _set_top_left(self, x: int, y: int) -> None: self._top_left_x = x self._top_left_y = y @@ -679,23 +708,23 @@ class TileGrid: or index >= len(self._tiles) ): raise ValueError("Tile index out of bounds") - return index + return x, y def __getitem__(self, index: Union[Tuple[int, int], int]) -> int: """Returns the tile index at the given index. The index can either be an x,y tuple or an int equal to ``y * width + x``'. """ - index = self._extract_and_check_index(index) - return self._tiles[index] + x, y = self._extract_and_check_index(index) + return self._tiles[y * self._width_in_tiles + x] def __setitem__(self, index: Union[Tuple[int, int], int], value: int) -> None: """Sets the tile index at the given index. The index can either be an x,y tuple or an int equal to ``y * width + x``. """ - index = self._extract_and_check_index(index) + x, y = self._extract_and_check_index(index) if not 0 <= value <= 255: raise ValueError("Tile value out of bounds") - self._tiles[index] = value + self._set_tile(x, y, value) @property def width(self) -> int: