def __init__(self, label_text, icon, on_disk=False, **kwargs):
        super().__init__(**kwargs)

        self._icon = icon

        if on_disk:
            self._file = open(self._icon, "rb")
            image = OnDiskBitmap(self._file)
            tile_grid = TileGrid(image, pixel_shader=ColorConverter())
        else:
            image, palette = adafruit_imageload.load(icon)
            tile_grid = TileGrid(image, pixel_shader=palette)
        self.append(tile_grid)
        _label = bitmap_label.Label(
            terminalio.FONT,
            scale=1,
            text=label_text,
            anchor_point=(0.5, 0),
            anchored_position=(image.width // 2, image.height),
        )
        self.append(_label)
        self.touch_boundary = (
            0,
            0,
            image.width,
            image.height + _label.bounding_box[3],
        )
    def __init__(self, map_json_file):
        super().__init__()
        f = open("good_map_2.json", "r")
        self._map_obj = json.loads(f.read())
        f.close()

        self.player_loc = {"x": 1, "y": 1}

        self._tile_properties = {0: {"can_walk": True}}

        if "tiles" in self.map_obj['tilesets'][0]:
            print("has tiles")
            for _tile in self.map_obj['tilesets'][0]['tiles']:
                _tile_id = _tile["id"]
                if _tile_id not in self.tile_properties:
                    self.tile_properties[_tile_id] = {}
                for _property in _tile["properties"]:
                    self.tile_properties[_tile_id][_property["name"]] = _property["value"]

        _sprite_sheet_image = self.map_obj["tilesets"][0]["image"]
        # Load the sprite sheet (bitmap)
        self._sprite_sheet, self._palette = adafruit_imageload.load(_sprite_sheet_image,
                                                                    bitmap=Bitmap,
                                                                    palette=Palette)

        self._palette.make_transparent(0)
        # Create the background TileGrid
        self._background_tilegrid = TileGrid(self._sprite_sheet, pixel_shader=self._palette,
                                             width=10,
                                             height=8,
                                             tile_width=16,
                                             tile_height=16,
                                             default_tile=15)

        # Create the castle TileGrid
        self._entity_tilegrid = TileGrid(self._sprite_sheet, pixel_shader=self._palette,
                                         width=10,
                                         height=8,
                                         tile_width=16,
                                         tile_height=16,
                                         default_tile=17)

        # Create the sprite TileGrid
        self._sprite_tilegrid = TileGrid(self._sprite_sheet, pixel_shader=self._palette,
                                         width=1,
                                         height=1,
                                         tile_width=16,
                                         tile_height=16,
                                         default_tile=0)

        self.append(self._background_tilegrid)
        self.append(self._entity_tilegrid)
        self.append(self._sprite_tilegrid)

        self._load_tilegrids()

        # put the sprite somewhere in the castle
        self._sprite_tilegrid.x = 16 * self.player_loc["x"]
        self._sprite_tilegrid.y = 16 * self.player_loc["y"]
Exemplo n.º 3
0
    def _create_background_box(self, lines: int, y_offset: int) -> TileGrid:
        """Private Class function to create a background_box
        :param lines: int number of lines
        :param y_offset: int y pixel bottom coordinate for the background_box"""

        left = self._bounding_box[0]
        if self._background_tight:  # draw a tight bounding box
            box_width = self._bounding_box[2]
            box_height = self._bounding_box[3]
            x_box_offset = 0
            y_box_offset = self._bounding_box[1]

        else:  # draw a "loose" bounding box to include any ascenders/descenders.
            ascent, descent = self._ascent, self._descent

            if self._label_direction in ("UPR", "DWR", "TTB"):
                box_height = (self._bounding_box[3] + self._padding_top +
                              self._padding_bottom)
                x_box_offset = -self._padding_bottom
                box_width = ((ascent + descent) + int(
                    (lines - 1) * self._width * self._line_spacing) +
                             self._padding_left + self._padding_right)
            else:
                box_width = (self._bounding_box[2] + self._padding_left +
                             self._padding_right)
                x_box_offset = -self._padding_left
                box_height = ((ascent + descent) + int(
                    (lines - 1) * self._height * self._line_spacing) +
                              self._padding_top + self._padding_bottom)

            if self._base_alignment:
                y_box_offset = -ascent - self._padding_top
            else:
                y_box_offset = -ascent + y_offset - self._padding_top

        box_width = max(0, box_width)  # remove any negative values
        box_height = max(0, box_height)  # remove any negative values

        if self._label_direction == "UPR":
            movx = left + x_box_offset
            movy = -box_height - x_box_offset
        elif self._label_direction == "DWR":
            movx = left + x_box_offset
            movy = x_box_offset
        elif self._label_direction == "TTB":
            movx = left + x_box_offset
            movy = x_box_offset
        else:
            movx = left + x_box_offset
            movy = y_box_offset

        background_bitmap = Bitmap(box_width, box_height, 1)
        tile_grid = TileGrid(
            background_bitmap,
            pixel_shader=self._background_palette,
            x=movx,
            y=movy,
        )

        return tile_grid
Exemplo n.º 4
0
def show_image(path):
    with open(path, 'rb') as f:
        bitmap = OnDiskBitmap(f)
        pixel_shader = ColorConverter()
        sprite = TileGrid(bitmap, pixel_shader=pixel_shader)
        group = Group()
        group.append(sprite)
        board.DISPLAY.show(group)
        board.DISPLAY.wait_for_frame()
Exemplo n.º 5
0
    def __init__(self, bitmap, palette):
        super().__init__(scale=1)
        self.tilegrid = TileGrid(bitmap,
                                 pixel_shader=palette,
                                 width=1,
                                 height=1,
                                 tile_width=16,
                                 tile_height=16)

        self.append(self.tilegrid)
        self._source_index = 0
        self.animating = False
    def _update_text(self, new_text: str) -> None:
        # pylint: disable=too-many-branches,too-many-statements

        x = 0
        y = 0
        if self._added_background_tilegrid:
            i = 1
        else:
            i = 0
        tilegrid_count = i
        if self._base_alignment:
            self._y_offset = 0
        else:
            self._y_offset = self._ascent // 2

        if self._label_direction == "RTL":
            left = top = bottom = 0
            right = None
        elif self._label_direction == "LTR":
            right = top = bottom = 0
            left = None
        else:
            top = right = left = 0
            bottom = 0

        for character in new_text:
            if character == "\n":
                y += int(self._height * self._line_spacing)
                x = 0
                continue
            glyph = self._font.get_glyph(ord(character))
            if not glyph:
                continue

            position_x, position_y = 0, 0

            if self._label_direction in ("LTR", "RTL"):
                bottom = max(bottom, y - glyph.dy + self._y_offset)
                if y == 0:  # first line, find the Ascender height
                    top = min(top, -glyph.height - glyph.dy + self._y_offset)
                position_y = y - glyph.height - glyph.dy + self._y_offset

                if self._label_direction == "LTR":
                    right = max(right, x + glyph.shift_x, x + glyph.width + glyph.dx)
                    if x == 0:
                        if left is None:
                            left = glyph.dx
                        else:
                            left = min(left, glyph.dx)
                    position_x = x + glyph.dx
                else:
                    left = max(
                        left, abs(x) + glyph.shift_x, abs(x) + glyph.width + glyph.dx
                    )
                    if x == 0:
                        if right is None:
                            right = glyph.dx
                        else:
                            right = max(right, glyph.dx)
                    position_x = x - glyph.width

            elif self._label_direction == "TTB":
                if x == 0:
                    if left is None:
                        left = glyph.dx
                    else:
                        left = min(left, glyph.dx)
                if y == 0:
                    top = min(top, -glyph.dy)

                bottom = max(bottom, y + glyph.height, y + glyph.height + glyph.dy)
                right = max(
                    right, x + glyph.width + glyph.dx, x + glyph.shift_x + glyph.dx
                )
                position_y = y + glyph.dy
                position_x = x - glyph.width // 2 + self._y_offset

            elif self._label_direction == "UPR":
                if x == 0:
                    if bottom is None:
                        bottom = -glyph.dx

                if y == 0:  # first line, find the Ascender height
                    bottom = min(bottom, -glyph.dy)
                left = min(left, x - glyph.height + self._y_offset)
                top = min(top, y - glyph.width - glyph.dx, y - glyph.shift_x)
                right = max(right, x + glyph.height, x + glyph.height - glyph.dy)
                position_y = y - glyph.width - glyph.dx
                position_x = x - glyph.height - glyph.dy + self._y_offset

            elif self._label_direction == "DWR":
                if y == 0:
                    if top is None:
                        top = -glyph.dx
                top = min(top, -glyph.dx)
                if x == 0:
                    left = min(left, -glyph.dy)
                left = min(left, x, x - glyph.dy - self._y_offset)
                bottom = max(bottom, y + glyph.width + glyph.dx, y + glyph.shift_x)
                right = max(right, x + glyph.height)
                position_y = y + glyph.dx
                position_x = x + glyph.dy - self._y_offset

            if glyph.width > 0 and glyph.height > 0:
                face = TileGrid(
                    glyph.bitmap,
                    pixel_shader=self._palette,
                    default_tile=glyph.tile_index,
                    tile_width=glyph.width,
                    tile_height=glyph.height,
                    x=position_x,
                    y=position_y,
                )

                if self._label_direction == "UPR":
                    face.transpose_xy = True
                    face.flip_x = True
                if self._label_direction == "DWR":
                    face.transpose_xy = True
                    face.flip_y = True

                if tilegrid_count < len(self._local_group):
                    self._local_group[tilegrid_count] = face
                else:
                    self._local_group.append(face)
                tilegrid_count += 1

            if self._label_direction == "RTL":
                x = x - glyph.shift_x
            if self._label_direction == "TTB":
                if glyph.height < 2:
                    y = y + glyph.shift_x
                else:
                    y = y + glyph.height + 1
            if self._label_direction == "UPR":
                y = y - glyph.shift_x
            if self._label_direction == "DWR":
                y = y + glyph.shift_x
            if self._label_direction == "LTR":
                x = x + glyph.shift_x

            i += 1

        if self._label_direction == "LTR" and left is None:
            left = 0
        if self._label_direction == "RTL" and right is None:
            right = 0
        if self._label_direction == "TTB" and top is None:
            top = 0

        while len(self._local_group) > tilegrid_count:  # i:
            self._local_group.pop()

        if self._label_direction == "RTL":
            # pylint: disable=invalid-unary-operand-type
            # type-checkers think left can be None
            self._bounding_box = (-left, top, left - right, bottom - top)
        if self._label_direction == "TTB":
            self._bounding_box = (left, top, right - left, bottom - top)
        if self._label_direction == "UPR":
            self._bounding_box = (left, top, right, bottom - top)
        if self._label_direction == "DWR":
            self._bounding_box = (left, top, right, bottom - top)
        if self._label_direction == "LTR":
            self._bounding_box = (left, top, right - left, bottom - top)

        self._text = new_text

        if self._background_color is not None:
            self._set_background_color(self._background_color)
Exemplo n.º 7
0
    def zoom_animation(self, touch_point):
        """Performs zoom animation when icon is pressed.

        :param touch_point: x,y location of the screen.
        :type touch_point: Tuple[x,y]
        :return: None
        """

        if self._animation_time > 0:
            try:
                _image, _palette = adafruit_imageload.load(self._icon)

                if len(self.__class__.palette_buffer) < len(_palette) + 1:
                    self._animation_time = 0  # skip any animation
                    print(
                        "Warning: IconAnimated - icon bitmap exceeds IconAnimated.max_color_depth;"
                        " defaulting to no animation")

            except NotImplementedError:
                self._animation_time = 0  # skip any animation
                print(
                    "Warning: IconAnimated - True color BMP unsupported for animation;"
                    " defaulting to no animation")

        if self._animation_time > 0:

            animation_bitmap = self.__class__.bitmap_buffer
            animation_palette = self.__class__.palette_buffer

            # store the current display refresh setting
            refresh_status = self.__class__.display.auto_refresh

            ###
            ## Update the zoom palette and bitmap buffers and append the tilegrid
            ###

            # copy the image palette, add a transparent color at the end
            for i, color in enumerate(_palette):
                animation_palette[i] = color
            animation_palette[len(animation_palette) - 1] = 0x000000
            animation_palette.make_transparent(len(animation_palette) - 1)

            # create the zoom bitmap larger than the original image to allow for zooming
            animation_bitmap.fill(len(animation_palette) -
                                  1)  # transparent fill
            animation_bitmap.blit(
                (animation_bitmap.width - _image.width) // 2,
                (animation_bitmap.height - _image.height) // 2,
                _image,
            )  # blit the image into the center of the zoom_bitmap

            # place zoom_bitmap at same location as image
            animation_tilegrid = TileGrid(animation_bitmap,
                                          pixel_shader=animation_palette)
            animation_tilegrid.x = -(animation_bitmap.width -
                                     _image.width) // 2
            animation_tilegrid.y = -(animation_bitmap.height -
                                     _image.height) // 2

            self.__class__.display.auto_refresh = False  # set auto_refresh off
            self[0].hidden = True  # hide the original icon
            self.append(animation_tilegrid)  # add to the self group.

            # Animation: zoom larger
            start_time = time.monotonic()

            while True:
                elapsed_time = time.monotonic() - start_time
                position = min(
                    1.0, easein(elapsed_time /
                                self._animation_time))  # fractional position
                animation_bitmap.fill(len(animation_palette) - 1)
                bitmaptools.rotozoom(
                    dest_bitmap=animation_bitmap,
                    ox=animation_bitmap.width // 2,
                    oy=animation_bitmap.height // 2,
                    source_bitmap=_image,
                    px=_image.width // 2,
                    py=_image.height // 2,
                    scale=1.0 + position *
                    (self._scale - 1.0),  # start scaling at 1.0
                    angle=position * self._angle,
                )
                self.__class__.display.refresh()
                if elapsed_time > self._animation_time:
                    break

            # set display.auto_refresh back to original value
            self.__class__.display.auto_refresh = refresh_status

            del _image
            del _palette
            gc.collect()

            self._zoomed = True
Exemplo n.º 8
0
    if cam.width > width:
        continue
    if cam.height > height:
        continue
    try:
        bitmap = Bitmap(cam.width, cam.height, 65535)
        break
    except MemoryError:
        continue

print(width, height, cam.width, cam.height)
if bitmap is None:
    raise SystemExit("Could not allocate a bitmap")

g = Group(scale=1, x=(width - cam.width) // 2, y=(height - cam.height) // 2)
tg = TileGrid(
    bitmap,
    pixel_shader=ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED))
g.append(tg)
display.show(g)

t0 = time.monotonic_ns()
display.auto_refresh = False
while True:
    cam.capture(bitmap)
    bitmap.dirty()
    display.refresh(minimum_frames_per_second=0)
    t1 = time.monotonic_ns()
    print("fps", 1e9 / (t1 - t0))
    t0 = t1