hexagram/py/hexagram/cluster.py
2023-12-31 20:31:12 -05:00

102 lines
2.8 KiB
Python

import math
import cairo
from typing import Iterable, List
from hexagram.pattern import HexagonPattern
from hexagram.gauge import Gauge
from hexagram.dial import Dial
from hexagram.speedo import Speedo
class ShiftIndicator(Gauge):
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,),
)
__slots__ = 'x', 'y', 'rpm_redline', 'rpm_max',
def __init__(self, x: float, y: float, rpm_redline: float, rpm_max: float):
self.x = x
self.y = y
self.rpm_redline = rpm_redline
self.rpm_max = rpm_max
def draw_bg(self, cr: cairo.Context):
for level in range(0, len(self.LIGHT_LEVELS)):
r = self.LIGHT_LOW * self.LIGHT_COLORS[level][0]
g = self.LIGHT_LOW * self.LIGHT_COLORS[level][1]
b = self.LIGHT_LOW * self.LIGHT_COLORS[level][2]
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()
class Cluster():
WIDTH = 1280
HEIGHT = 480
SHIFT_INDICATOR_X = 392
SHIFT_INDICATOR_Y = 24
__slots__ = 'gauges', 'speedo', 'tacho',
def __init__(self, rpm_redline: float, rpm_max: float):
self.gauges: List[Gauge] = list()
self.speedo = Speedo(self.HEIGHT / 2,
self.HEIGHT / 2,
self.HEIGHT / 2,
180.0)
self.tacho = Dial(self.WIDTH - self.HEIGHT / 2,
self.HEIGHT / 2,
self.HEIGHT / 2,
232.0 * (math.pi / 180.0),
488.0 * (math.pi / 180.0),
8000)
self.gauges.append(self.speedo)
self.gauges.append(self.tacho)
self.gauges.append(ShiftIndicator(self.SHIFT_INDICATOR_X,
self.SHIFT_INDICATOR_Y,
rpm_redline,
rpm_max))
def draw_bg(self, cr: cairo.Context):
cr.set_source_rgb(0.1, 0.1, 0.1)
cr.rectangle(0, 0, self.WIDTH, self.HEIGHT)
cr.fill()
cr.rectangle(0, 0, self.WIDTH, self.HEIGHT)
pattern = HexagonPattern()
pattern.fill(cr)
for gauge in self.gauges:
gauge.draw_bg(cr)