From 5c1ea2face8c199199a9e536d9f1b2d0cdd61e2d Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Fri, 5 Jan 2024 22:47:00 -0500 Subject: [PATCH] Render cold icon next to outside thermometer --- py/hexagram/cluster.py | 49 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/py/hexagram/cluster.py b/py/hexagram/cluster.py index 6a5948e..e4efdbf 100644 --- a/py/hexagram/cluster.py +++ b/py/hexagram/cluster.py @@ -11,7 +11,7 @@ from hexagram.speedo import Speedo from hexagram.tacho import Tacho from hexagram.fuel import FuelGauge from hexagram.thermo import Thermometer -from hexagram.status import StatusIconBox +from hexagram.status import VehicleStatus, StatusIcon, StatusIconBox class ShiftIndicator(Gauge): __slots__ = 'x', 'y', 'rpm_min', 'rpm_redline', 'rpm_max', @@ -95,14 +95,61 @@ class Odometer(TextGauge): return "%.1f mi" % self.value class AmbientTemp(TextGauge): + __slots__ = 'icon', + + ICON_WIDTH = 40 + ICON_HEIGHT = 40 + ICON_PADDING = 16 + ICON_COLOR = '#fff' + + COLD_THRESHOLD = 4.0 # °C + def __init__(self, x: float, y: float, align: Align): super().__init__(x, y, -100, 100, align) self.value = 0 + self.icon = StatusIcon(VehicleStatus.COLD, + 'cold', + self.ICON_WIDTH, + self.ICON_HEIGHT, + self.ICON_COLOR, + 'path {stroke: %(s)s;stroke-width: 8}') def format_text(self): return "%.1f°C" % self.value + def draw_fg(self, cr: cairo.Context): + cr.select_font_face(self.FONT_FACE, + self.FONT_SLANT, + self.FONT_WEIGHT) + + text = self.format_text() + + cr.set_font_size(self.FONT_SIZE) + + extents = cr.text_extents(text) + width = extents[2] - extents[0] + + if self.value <= self.COLD_THRESHOLD: + width += self.icon.width + self.ICON_PADDING + + if self.align is Align.LEFT: + text_x_offset = 0.0 + elif self.align is Align.MIDDLE: + text_x_offset = self.x - width / 2.0 + elif self.align is Align.RIGHT: + text_x_offset = self.x - width + + cr.set_source_rgb(1, 1, 1) + cr.move_to(text_x_offset, self.y) + cr.show_text(text) + + icon_y_offset = abs(self.icon.height - self.FONT_SIZE) * 2 + + self.icon.draw(cr, + text_x_offset + width - self.icon.width, + self.y - icon_y_offset) + class Clock(TextGauge): def __init__(self, x: float, y: float, align: Align): super().__init__(x, y, 0, 9999, align)