|
| 1 | +# SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import bitmaptools |
| 6 | +import displayio |
| 7 | +import adafruit_imageload |
| 8 | +from .doublebuffer import DoubleBuffer |
| 9 | +from .message import Message |
| 10 | + |
| 11 | + |
| 12 | +class MessageBoard: |
| 13 | + def __init__(self, matrix): |
| 14 | + self.fonts = {} |
| 15 | + self.display = matrix.display |
| 16 | + self._buffer_width = self.display.width * 2 |
| 17 | + self._buffer_height = self.display.height * 2 |
| 18 | + self._dbl_buf = DoubleBuffer( |
| 19 | + self.display, self._buffer_width, self._buffer_height |
| 20 | + ) |
| 21 | + self._background = None |
| 22 | + self.set_background() # Set to black |
| 23 | + self._position = (0, 0) |
| 24 | + |
| 25 | + def set_background(self, file_or_color=0x000000): |
| 26 | + """The background image to a bitmap file.""" |
| 27 | + if isinstance(file_or_color, str): # its a filenme: |
| 28 | + background, bg_shader = adafruit_imageload.load(file_or_color) |
| 29 | + self._dbl_buf.shader = bg_shader |
| 30 | + self._background = background |
| 31 | + elif isinstance(file_or_color, int): |
| 32 | + # Make a background color fill |
| 33 | + bg_shader = displayio.ColorConverter( |
| 34 | + input_colorspace=displayio.Colorspace.RGB565 |
| 35 | + ) |
| 36 | + background = displayio.Bitmap( |
| 37 | + self.display.width, self.display.height, 65535 |
| 38 | + ) |
| 39 | + background.fill(displayio.ColorConverter().convert(file_or_color)) |
| 40 | + self._dbl_buf.shader = bg_shader |
| 41 | + self._background = background |
| 42 | + else: |
| 43 | + raise RuntimeError("Unknown type of background") |
| 44 | + |
| 45 | + def animate(self, message, animation_class, animation_function, **kwargs): |
| 46 | + anim_class = __import__( |
| 47 | + f"{self.__module__}.animations.{animation_class.lower()}" |
| 48 | + ) |
| 49 | + anim_class = getattr(anim_class, "animations") |
| 50 | + anim_class = getattr(anim_class, animation_class.lower()) |
| 51 | + anim_class = getattr(anim_class, animation_class) |
| 52 | + animation = anim_class( |
| 53 | + self.display, self._draw, self._position |
| 54 | + ) # Instantiate the class |
| 55 | + # Call the animation function and pass kwargs along with the message (positional) |
| 56 | + anim_func = getattr(animation, animation_function) |
| 57 | + anim_func(message, **kwargs) |
| 58 | + |
| 59 | + def _draw( |
| 60 | + self, |
| 61 | + image, |
| 62 | + x, |
| 63 | + y, |
| 64 | + opacity=None, |
| 65 | + mask_color=0xFF00FF, |
| 66 | + blendmode=bitmaptools.BlendMode.Normal, |
| 67 | + post_draw_position=None, |
| 68 | + ): |
| 69 | + """Draws a message to the buffer taking its current settings into account. |
| 70 | + It also sets the current position and performs a swap. |
| 71 | + """ |
| 72 | + self._position = (x, y) |
| 73 | + buffer_x_offset = self._buffer_width - self.display.width |
| 74 | + buffer_y_offset = self._buffer_height - self.display.height |
| 75 | + |
| 76 | + # Image can be a message in which case its properties will be used |
| 77 | + if isinstance(image, Message): |
| 78 | + if opacity is None: |
| 79 | + opacity = image.opacity |
| 80 | + mask_color = image.mask_color |
| 81 | + blendmode = image.blendmode |
| 82 | + image = image.buffer |
| 83 | + if opacity is None: |
| 84 | + opacity = 1.0 |
| 85 | + |
| 86 | + if mask_color > 65535: |
| 87 | + mask_color = displayio.ColorConverter().convert(mask_color) |
| 88 | + |
| 89 | + # Blit the background |
| 90 | + bitmaptools.blit( |
| 91 | + self._dbl_buf.active_buffer, |
| 92 | + self._background, |
| 93 | + buffer_x_offset, |
| 94 | + buffer_y_offset, |
| 95 | + ) |
| 96 | + |
| 97 | + # If the image is wider than the display buffer, we need to shrink it |
| 98 | + if x + buffer_x_offset < 0: |
| 99 | + new_image = displayio.Bitmap( |
| 100 | + image.width - self.display.width, image.height, 65535 |
| 101 | + ) |
| 102 | + bitmaptools.blit( |
| 103 | + new_image, |
| 104 | + image, |
| 105 | + 0, |
| 106 | + 0, |
| 107 | + x1=self.display.width, |
| 108 | + y1=0, |
| 109 | + x2=image.width, |
| 110 | + y2=image.height, |
| 111 | + ) |
| 112 | + x += self.display.width |
| 113 | + image = new_image |
| 114 | + |
| 115 | + # If the image is taller than the display buffer, we need to shrink it |
| 116 | + if y + buffer_y_offset < 0: |
| 117 | + new_image = displayio.Bitmap( |
| 118 | + image.width, image.height - self.display.height, 65535 |
| 119 | + ) |
| 120 | + bitmaptools.blit( |
| 121 | + new_image, |
| 122 | + image, |
| 123 | + 0, |
| 124 | + 0, |
| 125 | + x1=0, |
| 126 | + y1=self.display.height, |
| 127 | + x2=image.width, |
| 128 | + y2=image.height, |
| 129 | + ) |
| 130 | + y += self.display.height |
| 131 | + image = new_image |
| 132 | + |
| 133 | + # Clear the foreground buffer |
| 134 | + foreground_buffer = displayio.Bitmap( |
| 135 | + self._buffer_width, self._buffer_height, 65535 |
| 136 | + ) |
| 137 | + foreground_buffer.fill(mask_color) |
| 138 | + |
| 139 | + bitmaptools.blit( |
| 140 | + foreground_buffer, image, x + buffer_x_offset, y + buffer_y_offset |
| 141 | + ) |
| 142 | + |
| 143 | + # Blend the foreground buffer into the main buffer |
| 144 | + bitmaptools.alphablend( |
| 145 | + self._dbl_buf.active_buffer, |
| 146 | + self._dbl_buf.active_buffer, |
| 147 | + foreground_buffer, |
| 148 | + displayio.Colorspace.RGB565, |
| 149 | + 1.0, |
| 150 | + opacity, |
| 151 | + blendmode=blendmode, |
| 152 | + skip_source2_index=mask_color, |
| 153 | + ) |
| 154 | + self._dbl_buf.show() |
| 155 | + |
| 156 | + # Allow for an override of the position after drawing (needed for split effects) |
| 157 | + if post_draw_position is not None and isinstance(post_draw_position, tuple): |
| 158 | + self._position = post_draw_position |
0 commit comments