diff --git a/testing/sprites/lib/tile_grid.py b/testing/sprites/lib/tile_grid.py index f7c09ab..40bbb32 100644 --- a/testing/sprites/lib/tile_grid.py +++ b/testing/sprites/lib/tile_grid.py @@ -16,16 +16,10 @@ logger.setLevel(logging.DEBUG) -TILE_GRID_CELL_WIDTH = 64 -TILE_GRID_CELL_HEIGHT = 12 - -TILE_GRID_CELL_ICON_WIDTH = 14 -TILE_GRID_CELL_ICON_HEIGHT = 12 - # STYLE CLASSES +""" TILE_GRID_STYLE_DEFAULT = { - "cell_alpha": 255, "cell_color_background": pygame.Color(16, 16, 16, 255), "cell_color_border": pygame.Color(0, 0, 0, 0), "label_color_text": pygame.Color(255, 255, 255, 255), @@ -42,7 +36,14 @@ "icon_color_foreground": pygame.Color(255, 255, 255, 255), "icon_codepoint": None, } +""" +# CONSTANTS + +TILE_GRID_CELL_WIDTH = 64 +TILE_GRID_CELL_HEIGHT = 12 +TILE_GRID_CELL_ICON_WIDTH = 14 +TILE_GRID_CELL_ICON_HEIGHT = 12 # TILE GRID @@ -57,10 +58,30 @@ class BaseSprite(pygame.sprite.Sprite): pass +# Mixins + + +class StyleMixin: + # Cell + cell_color_background: pygame.Color = pygame.Color(16, 16, 16, 255) + # Label + label_antialias: bool = True + label_outline: bool = True + label_color_foreground: pygame.Color = pygame.Color(255, 255, 255, 255) + label_color_outline: pygame.Color = pygame.Color(0, 0, 0, 255) + # Icon + icon_visible: bool = True + icon_codepoint: int = FontAwesomeIcons.ICON_FA_BOLT + icon_width: int = TILE_GRID_CELL_ICON_WIDTH + icon_height: int = TILE_GRID_CELL_ICON_HEIGHT + icon_color_background: pygame.Color = pygame.Color(32, 32, 32, 255) + icon_color_foreground: pygame.Color = pygame.Color(255, 255, 255, 255) + + # Tile Grid Cell Sprites -class TileGridCell(BaseSprite): +class TileGridCell(BaseSprite, StyleMixin): style: Dict width: int = TILE_GRID_CELL_WIDTH height: int = TILE_GRID_CELL_HEIGHT @@ -70,9 +91,8 @@ class TileGridCell(BaseSprite): def __init__(self, state): super().__init__(state) self.state = state - self.style = TILE_GRID_STYLE_DEFAULT.copy() self.image = pygame.Surface((self.width, self.height)) - self.image.fill(self.style["cell_color_background"]) + self.image.fill(self.cell_color_background) self.rect = self.image.get_rect() def update(self): @@ -80,27 +100,25 @@ def update(self): def render(self): self.image = pygame.Surface((self.rect.width, self.rect.height)) - self.image.fill(self.style["cell_color_background"]) + self.image.fill(self.cell_color_background) cx, cy = 0, 0 - if self.style["icon_visible"]: + if self.icon_visible: icon_surface = render_icon( - self.style["icon_width"], - self.style["icon_height"], - self.style["icon_codepoint"], - self.style["icon_color_background"], - self.style["icon_color_foreground"], + self.icon_width, + self.icon_height, + self.icon_codepoint, + self.icon_color_background, + self.icon_color_foreground, ) self.image.blit(icon_surface, (0, 0)) cx += icon_surface.get_width() label_surface = render_text( text=self.label, - font_filename=self.style["label_font"], - font_size=self.style["label_size"], - antialias=self.style["label_antialias"], - color_foreground=self.style["label_color_text"], - color_outline=self.style["label_color_outline"], - padding=self.style["label_padding"], - outline=self.style["label_outline"], + antialias=self.label_antialias, + color_foreground=self.label_color_foreground, + color_outline=self.label_color_outline, + padding=(2, 1), + outline=self.label_outline, ) self.image.blit(label_surface, (cx, 0)) self.rect = self.image.get_rect() diff --git a/testing/sprites/tiles.py b/testing/sprites/tiles.py index ba46869..ce3db5d 100644 --- a/testing/sprites/tiles.py +++ b/testing/sprites/tiles.py @@ -45,27 +45,31 @@ class GridCell(VerticalCollapseTileGridCell): class CellElectricityDemand(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_BOLT + def update(self): super().update() v = int(self.state.get("sensor.octopus_energy_electricity_current_demand", 0)) self.label = f"{v}" open = v > 500 self.height_animator.set(open) - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_BOLT - self.style["icon_color_background"] = ( + self.icon_color_background = ( CustomColor.RED_DARK.value if v > 1000 else CustomColor.TRANSPARENT.value ) class CellElectricityRate(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_HOURGLASS + def update(self): super().update() v = float(self.state.get("sensor.octopus_energy_electricity_current_rate", 0)) self.label = f"{v}" - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_HOURGLASS class CellElectricityAccumulativeCost(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_CLOCK + def update(self): super().update() v = float( @@ -74,36 +78,39 @@ def update(self): ) ) self.label = f"{v}" - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_CLOCK # Battery Tiles class CellBatteryLevel(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_BATTERY_FULL + def update(self): super().update() v = int(self.state.get("sensor.delta_2_max_downstairs_battery_level", 0)) self.label = f"{v}" - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_BATTERY_FULL class CellBatteryACInput(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_PLUG_CIRCLE_PLUS + def update(self): super().update() v = int(self.state.get("sensor.delta_2_max_downstairs_ac_in_power", 0)) self.label = f"{v}" - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_PLUG_CIRCLE_PLUS + open = v > 100 self.height_animator.set(open) class CellBatteryACOutput(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_PLUG_CIRCLE_MINUS + def update(self): super().update() v = int(self.state.get("sensor.delta_2_max_downstairs_ac_out_power", 0)) self.label = f"{v}" - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_PLUG_CIRCLE_MINUS open = v > 100 self.height_animator.set(open) @@ -112,41 +119,47 @@ def update(self): class CellSpeedTestDownload(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_CIRCLE_ARROW_DOWN + def update(self): super().update() v = int(self.state.get("sensor.speedtest_download_average", 0)) self.label = f"{v}Mb" - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_CIRCLE_ARROW_DOWN open = v > 500 self.height_animator.set(open) class CellSpeedTestUpload(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_CIRCLE_ARROW_UP + def update(self): super().update() v = int(self.state.get("sensor.speedtest_upload_average", 0)) self.label = f"{v}Mb" - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_CIRCLE_ARROW_UP + open = v > 500 self.height_animator.set(open) class CellSpeedTestPing(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_HEART_PULSE + def update(self): super().update() v = int(self.state.get("sensor.speedtest_ping_average", 0)) self.label = f"{v}ms" - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_HEART_PULSE open = v > 25 self.height_animator.set(open) class CellDS920VolumeUsage(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_HARD_DRIVE + def update(self): super().update() v = int(self.state.get("sensor.ds920plus_volume_used", 0)) self.label = f"{v}" - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_HARD_DRIVE + open = v > 25 self.height_animator.set(open) @@ -155,11 +168,13 @@ def update(self): class CellTestRandom(GridCell): + icon_codepoint = FontAwesomeIcons.ICON_FA_DICE_THREE + def update(self): super().update() v = datetime.datetime.now().second self.label = f"{v}" - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_DICE_THREE + open = 0 <= v % 5 <= 2 self.height_animator.set(open) @@ -169,11 +184,12 @@ def update(self): class CellSwitchLoungeFan(GridCell): label = "Fan" + icon_codepoint = FontAwesomeIcons.ICON_FA_FAN def update(self): super().update() v = int(self.state.get("fan", 0)) - self.style["icon_codepoint"] = FontAwesomeIcons.ICON_FA_FAN + open = v > 90 self.height_animator.set(open)