diff --git a/py/hexagram/cluster.py b/py/hexagram/cluster.py index 9f5719b..9bd0685 100644 --- a/py/hexagram/cluster.py +++ b/py/hexagram/cluster.py @@ -8,85 +8,11 @@ from hexagram.pattern import HexagonPattern from hexagram.gauge import Gauge, TextGauge from hexagram.dial import Dial from hexagram.speedo import Speedo -from hexagram.tacho import Tacho +from hexagram.tacho import Tacho, ShiftIndicator from hexagram.fuel import FuelGauge from hexagram.thermo import Thermometer from hexagram.status import StatusIconBox, ColdStatus, ColdIcon -class ShiftIndicator(Gauge): - __slots__ = 'x', 'y', 'rpm_min', 'rpm_redline', 'rpm_max', - - LIGHT_WIDTH = 48 - LIGHT_HEIGHT = 12 - LIGHT_SPACING = 8 - LIGHT_LOW = 0.25 - - LIGHT_COLORS = ( - (0.0, 1.0, 0.0), - (0.6, 1.0, 0.0), - (1.0, 1.0, 0.0), - (1.0, 0.6, 0.0), - (1.0, 0.0, 0.0), - ) - - LIGHT_LEVELS = ( - (0, 8), - (1, 7), - (2, 6), - (3, 5), - (4,), - ) - - def __init__(self, x: float, y: float, rpm_min: float, rpm_redline: float, rpm_max: float): - self.value = 0 - self.x = x - self.y = y - self.rpm_min = rpm_min - self.rpm_redline = rpm_redline - self.rpm_max = rpm_max - - def draw_level(self, cr: cairo.Context, level: int, on: bool=False): - max_level = len(self.LIGHT_LEVELS) - 1 - - if level < 0: - level = 0 - elif level >= max_level: - level = max_level - - r, g, b = self.LIGHT_COLORS[level] - - if not on: - r *= self.LIGHT_LOW - g *= self.LIGHT_LOW - b *= self.LIGHT_LOW - - cr.set_source_rgb(r, g, b) - - for i in self.LIGHT_LEVELS[level]: - x = i * (self.LIGHT_WIDTH + self.LIGHT_SPACING) - - cr.rectangle(self.x + x, - self.y, - self.LIGHT_WIDTH, - self.LIGHT_HEIGHT) - - cr.fill() - - def draw_bg(self, cr: cairo.Context): - for level in range(0, len(self.LIGHT_LEVELS)): - self.draw_level(cr, level) - - def draw_fg(self, cr: cairo.Context): - if self.value < self.rpm_min: - return - - adj = self.value - self.rpm_min - adj_max = self.rpm_max - self.rpm_min - level = int((adj / adj_max) * len(self.LIGHT_LEVELS)) - - for l in range(0, len(self.LIGHT_LEVELS)): - self.draw_level(cr, l, level >= l) - class Odometer(TextGauge): def __init__(self, x: float, y: float, align: Align): super().__init__(x, y, 0, 999999999, align) diff --git a/py/hexagram/tacho.py b/py/hexagram/tacho.py index 0f7850c..504ab20 100644 --- a/py/hexagram/tacho.py +++ b/py/hexagram/tacho.py @@ -2,6 +2,7 @@ import enum import math import cairo +from hexagram.gauge import Gauge from hexagram.dial import Dial from hexagram.trans import Gear, ShiftMode @@ -88,3 +89,77 @@ class Tacho(Dial): self.y + height / 4) cr.show_text(text) + +class ShiftIndicator(Gauge): + __slots__ = 'x', 'y', 'rpm_min', 'rpm_redline', 'rpm_max', + + LIGHT_WIDTH = 48 + LIGHT_HEIGHT = 12 + LIGHT_SPACING = 8 + LIGHT_LOW = 0.25 + + LIGHT_COLORS = ( + (0.0, 1.0, 0.0), + (0.6, 1.0, 0.0), + (1.0, 1.0, 0.0), + (1.0, 0.6, 0.0), + (1.0, 0.0, 0.0), + ) + + LIGHT_LEVELS = ( + (0, 8), + (1, 7), + (2, 6), + (3, 5), + (4,), + ) + + def __init__(self, x: float, y: float, rpm_min: float, rpm_redline: float, rpm_max: float): + self.value = 0 + self.x = x + self.y = y + self.rpm_min = rpm_min + self.rpm_redline = rpm_redline + self.rpm_max = rpm_max + + def draw_level(self, cr: cairo.Context, level: int, on: bool=False): + max_level = len(self.LIGHT_LEVELS) - 1 + + if level < 0: + level = 0 + elif level >= max_level: + level = max_level + + r, g, b = self.LIGHT_COLORS[level] + + if not on: + r *= self.LIGHT_LOW + g *= self.LIGHT_LOW + b *= self.LIGHT_LOW + + cr.set_source_rgb(r, g, b) + + for i in self.LIGHT_LEVELS[level]: + x = i * (self.LIGHT_WIDTH + self.LIGHT_SPACING) + + cr.rectangle(self.x + x, + self.y, + self.LIGHT_WIDTH, + self.LIGHT_HEIGHT) + + cr.fill() + + def draw_bg(self, cr: cairo.Context): + for level in range(0, len(self.LIGHT_LEVELS)): + self.draw_level(cr, level) + + def draw_fg(self, cr: cairo.Context): + if self.value < self.rpm_min: + return + + adj = self.value - self.rpm_min + adj_max = self.rpm_max - self.rpm_min + level = int((adj / adj_max) * len(self.LIGHT_LEVELS)) + + for l in range(0, len(self.LIGHT_LEVELS)): + self.draw_level(cr, l, level >= l)