diff --git a/module_defaults.json b/module_defaults.json index 14c9c1c1f6467122c45af82133d43c2386ef6ee7..1e2c43471de2611374e8f1ed14a93e78b7e0ce00 100644 --- a/module_defaults.json +++ b/module_defaults.json @@ -275,9 +275,39 @@ } }, "WeatherWidget": { + "scalable_title_font_size": { + "name": "Title font size", + "description": "The font size of the title.", + "default": 40, + "type": "integer" + }, + "title_font_color": { + "name": "Title font colour", + "description": "The colour of the font.", + "default": "#ffffff", + "type": "color" + }, + "title_font_name": { + "name": "Title font name", + "description": "The name of the font to use.", + "default": null, + "type": "font" + }, + "title_font_bold": { + "name": "Title font bold", + "description": "Whether the font should be bold.", + "default": false, + "type": "boolean" + }, + "title_font_italic": { + "name": "Title font italic", + "description": "Whether the font should be italic.", + "default": false, + "type": "boolean" + }, "scalable_font_size": { "name": "Font size", - "description": "The font size of the clock.", + "description": "The font size of the weather description.", "default": 15, "type": "integer" }, @@ -305,6 +335,12 @@ "default": false, "type": "boolean" }, + "scalable_temp_font_size": { + "name": "Temperature font size", + "description": "The font size of the temperature.", + "default": 30, + "type": "integer" + }, "draw_background": { "name": "Draw background", "description": "Draw the background of the widget.", @@ -321,7 +357,7 @@ "background_color": { "name": "Background colour", "description": "The colour of the background.", - "default": "#000000", + "default": "#ba4a00", "type": "color" }, "background_image": { diff --git a/screenwidgets.py b/screenwidgets.py index a74da57c3cc416336770223304391fb4edd75b00..284074faf591cda9f637ddbc0dc6267673cd7d5a 100644 --- a/screenwidgets.py +++ b/screenwidgets.py @@ -3,7 +3,7 @@ import time from datetime import datetime from config import PROP_WINDOW_SCALE, PROP_SCREEN_WIDTH, PROP_SCREEN_HEIGHT, hw_config -from utils.num import get_screen_centre +from utils.num import get_screen_centre, percentage_width, percentage_height from utils.net import get_ip from utils.fonts import get_font_path_or_default from utils.weather import Weather @@ -249,14 +249,22 @@ class WeatherWidget(IWidget): self.pygame = config['pygame'] # Get the font path + self.title_font_path = get_font_path_or_default(self.pygame, config.get("title_font_name", None), + config.get("title_font_bold", False), config.get("title_font_italic", False)) self.font_path = get_font_path_or_default(self.pygame, config.get("font_name", None), config.get("font_bold", False), config.get("font_italic", False)) # Load the font + self.title_font = self.pygame.font.Font(self.font_path, config.get("title_font_size", 15 * PROP_WINDOW_SCALE)) + self.title_font.bold = config.get("title_font_bold", False) + self.title_font.italic = config.get("title_font_italic", False) + self.font = self.pygame.font.Font(self.font_path, config.get("font_size", 15 * PROP_WINDOW_SCALE)) self.font.bold = config.get("font_bold", False) self.font.italic = config.get("font_italic", False) + self.temp_font = self.pygame.font.Font(self.font_path, config.get("temp_font_size", 30 * PROP_WINDOW_SCALE)) + # Set the colours self.color = config.get("font_color", "white") self.background_color = config.get("background_color", "black") @@ -277,6 +285,9 @@ class WeatherWidget(IWidget): self.weather_icon = "" self.temperature = 0 + self.wicon = None + self.wicon_rect = None + self.update_frequency = hw_config["weather_update_frequency"] def draw(self, screen): @@ -290,11 +301,17 @@ class WeatherWidget(IWidget): self.weather_description = weather["description"] self.weather_icon = weather["icon"] self.temperature = weather["temp"] + + self.wicon = self.pygame.image.load(self.weather_icon) + self.wicon = self.pygame.transform.scale(self.wicon, (65 * PROP_WINDOW_SCALE, 65 * PROP_WINDOW_SCALE)) + self.wicon_rect = self.wicon.get_rect() + self.wicon_rect.center = (percentage_width(0.2), percentage_height(0.5)) else: self.place_name = "No Data" self.weather_description = "" self.weather_icon = "" self.temperature = 0 + self.wicon = None # Fill the background @@ -305,16 +322,19 @@ class WeatherWidget(IWidget): screen.blit(self.bg_image, (0, 0)) # Render the text - if self.weather_icon != "": - wicon = self.pygame.image.load(self.weather_icon) - wicon = self.pygame.transform.scale(wicon, (65 * PROP_WINDOW_SCALE, 65 * PROP_WINDOW_SCALE)) + if self.wicon is not None: + screen.blit(self.wicon, self.wicon_rect) - screen.blit(wicon, (get_screen_centre()[0] - 50 * PROP_WINDOW_SCALE, get_screen_centre()[1] - 50 * PROP_WINDOW_SCALE)) + text_surface = self.title_font.render(f"{self.place_name}", True, self.color) + text_rect = text_surface.get_rect(center=(percentage_width(0.5), percentage_height(0.2))) - text_surface = self.font.render(f"{self.place_name}", True, self.color) text_surface_2 = self.font.render(f"{self.weather_description}", True, self.color) - text_surface_3 = self.font.render(f"{self.temperature}", True, self.color) + text_rect_2 = text_surface_2.get_rect() + text_rect_2.center = (percentage_width(0.65), percentage_height(0.5)) - screen.blit(text_surface, (get_screen_centre()[0] - 50 * PROP_WINDOW_SCALE, get_screen_centre()[1] + 50 * PROP_WINDOW_SCALE)) - screen.blit(text_surface_2, (get_screen_centre()[0] - 50 * PROP_WINDOW_SCALE, get_screen_centre()[1] + 70 * PROP_WINDOW_SCALE)) - screen.blit(text_surface_3, (get_screen_centre()[0] - 50 * PROP_WINDOW_SCALE, get_screen_centre()[1] + 90 * PROP_WINDOW_SCALE)) \ No newline at end of file + text_surface_3 = self.temp_font.render(f"{self.temperature}°C", True, self.color) + text_rect_3 = text_surface_3.get_rect(center=(percentage_width(0.5), percentage_height(0.8))) + + screen.blit(text_surface, text_rect) + screen.blit(text_surface_2, text_rect_2) + screen.blit(text_surface_3, text_rect_3) diff --git a/utils/num.py b/utils/num.py index 8eeacf48328601b48dc86b5ece9e68464356f32c..3a06bef116c0c94c29ef4efb94080f31350f0ea9 100644 --- a/utils/num.py +++ b/utils/num.py @@ -15,3 +15,19 @@ def color_hex_to_tuple(hx: str) -> tuple[int, int, int]: """ hx = hx.removeprefix("#") return tuple(int(hx[i:i + 2], 16) for i in (0, 2, 4)) + +def percentage_width(percentage: float) -> int: + """ + Get the pixel width of a percentage of the screen width + :param percentage: the percentage (as a decimal) + :return: the pixel width + """ + return int(PROP_SCREEN_WIDTH * percentage * PROP_WINDOW_SCALE) + +def percentage_height(percentage: float) -> int: + """ + Get the pixel height of a percentage of the screen height + :param percentage: the percentage (as a decimal) + :return: the pixel height + """ + return int(PROP_SCREEN_HEIGHT * percentage * PROP_WINDOW_SCALE)