import enum import math import cairo from hexagram.dial import Dial class SpeedUnits(enum.Enum): KPH = 0 MPH = 1 class Speedo(Dial): __slots__ = 'units', MIN_ANGLE = 232.0 * (math.pi / 180.0) MAX_ANGLE = 488.0 * (math.pi / 180.0) FONT_FACE = "Muli" def __init__(self, x: float, y: float, radius: float, max_value: float, units: SpeedUnits=SpeedUnits.KPH): super().__init__(x, y, radius, self.MIN_ANGLE, self.MAX_ANGLE, 0, max_value) self.value = 0 self.units = units def draw_bg(self, cr: cairo.Context): super().draw_bg(cr) cr.select_font_face(self.FONT_FACE, cairo.FontSlant.ITALIC, cairo.FontWeight.BOLD) cr.set_font_size(self.radius * 0.1) cr.set_source_rgb(1, 1, 1) for speed in range(0, int(self.max_value)+1, 5): min_radius = 0.81 if speed % 10 == 0 else 0.82 cr.set_line_width(6.0 if speed % 10 == 0 else 2.0) self.draw_mark(cr, min_radius, 0.87, speed) for speed in range(0, int(self.max_value)+1, 20): self.draw_number(cr, 0.68, speed, "%d" % int(speed)) def _draw_text(self, cr: cairo.Context, x: float, y: float, text: str, size: float): cr.select_font_face("Muli", cairo.FontSlant.ITALIC, cairo.FontWeight.BOLD) cr.set_font_size(self.radius * size) cr.set_source_rgb(1, 0.4, 1) extents = cr.text_extents(text) width = extents[2] - extents[0] height = extents[3] - extents[1] cr.move_to(x - width / 2, y + height / 4) cr.show_text(text) def draw_fg(self, cr: cairo.Context): super().draw_fg(cr) self._draw_text(cr, self.x, self.y, "%d" % self.value, 0.32) self._draw_text(cr, self.x, self.y + self.radius * 0.2, 'mph', 0.1)