X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka_Displayio.git/blobdiff_plain/e6284da03fe940ace9fe15d092f45124cb7c12d2..a8baabe8c4e39e9c709a0757c6244a0b711c9b99:/displayio/_tilegrid.py diff --git a/displayio/_tilegrid.py b/displayio/_tilegrid.py index f2ccde2..316543d 100644 --- a/displayio/_tilegrid.py +++ b/displayio/_tilegrid.py @@ -18,20 +18,17 @@ displayio for Blinka """ from typing import Union, Optional, Tuple -from recordclass import recordclass from PIL import Image from ._bitmap import Bitmap from ._colorconverter import ColorConverter from ._ondiskbitmap import OnDiskBitmap from ._shape import Shape from ._palette import Palette +from ._structs import RectangleStruct, TransformStruct -__version__ = "0.0.0-auto.0" +__version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git" -Rectangle = recordclass("Rectangle", "x1 y1 x2 y2") -Transform = recordclass("Transform", "x y dx dy scale transpose_xy mirror_x mirror_y") - class TileGrid: # pylint: disable=too-many-instance-attributes @@ -100,9 +97,11 @@ class TileGrid: self._pixel_width = width * tile_width self._pixel_height = height * tile_height self._tiles = (self._width * self._height) * [default_tile] - self.in_group = False - self._absolute_transform = Transform(0, 0, 1, 1, 1, False, False, False) - self._current_area = Rectangle(0, 0, self._pixel_width, self._pixel_height) + self._in_group = False + self._absolute_transform = TransformStruct(0, 0, 1, 1, 1, False, False, False) + self._current_area = RectangleStruct( + 0, 0, self._pixel_width, self._pixel_height + ) self._moved = False def _update_transform(self, absolute_transform): @@ -266,11 +265,11 @@ class TileGrid: ), resample=Image.NEAREST, ) - if absolute_transform.mirror_x: + if absolute_transform.mirror_x != self._flip_x: image = image.transpose(Image.FLIP_LEFT_RIGHT) - if absolute_transform.mirror_y: + if absolute_transform.mirror_y != self._flip_y: image = image.transpose(Image.FLIP_TOP_BOTTOM) - if absolute_transform.transpose_xy: + if absolute_transform.transpose_xy != self._transpose_xy: image = image.transpose(Image.TRANSPOSE) x *= absolute_transform.dx y *= absolute_transform.dy @@ -296,6 +295,9 @@ class TileGrid: ): buffer.alpha_composite(image, (x, y), source=(source_x, source_y)) + def _finish_refresh(self): + pass + @property def hidden(self) -> bool: """True when the TileGrid is hidden. This may be False even @@ -379,6 +381,42 @@ class TileGrid: """The pixel shader of the tilegrid.""" return self._pixel_shader + @pixel_shader.setter + def pixel_shader(self, new_pixel_shader: Union[ColorConverter, Palette]) -> None: + if not isinstance(new_pixel_shader, ColorConverter) and not isinstance( + new_pixel_shader, Palette + ): + raise TypeError( + "Unsupported Type: new_pixel_shader must be ColorConverter or Palette" + ) + + self._pixel_shader = new_pixel_shader + + @property + def bitmap(self) -> Union[Bitmap, OnDiskBitmap, Shape]: + """The Bitmap, OnDiskBitmap, or Shape that is assigned to this TileGrid""" + return self._bitmap + + @bitmap.setter + def bitmap(self, new_bitmap: Union[Bitmap, OnDiskBitmap, Shape]) -> None: + + if ( + not isinstance(new_bitmap, Bitmap) + and not isinstance(new_bitmap, OnDiskBitmap) + and not isinstance(new_bitmap, Shape) + ): + raise TypeError( + "Unsupported Type: new_bitmap must be Bitmap, OnDiskBitmap, or Shape" + ) + + if ( + new_bitmap.width != self.bitmap.width + or new_bitmap.height != self.bitmap.height + ): + raise ValueError("New bitmap must be same size as old bitmap") + + self._bitmap = new_bitmap + def _extract_and_check_index(self, index): if isinstance(index, (tuple, list)): x = index[0] @@ -406,3 +444,23 @@ class TileGrid: if not 0 <= value <= 255: raise ValueError("Tile value out of bounds") self._tiles[index] = value + + @property + def width(self) -> int: + """Width in tiles""" + return self._width + + @property + def height(self) -> int: + """Height in tiles""" + return self._height + + @property + def tile_width(self) -> int: + """Width of each tile in pixels""" + return self._tile_width + + @property + def tile_height(self) -> int: + """Height of each tile in pixels""" + return self._tile_height