hexagram/py/hexagram/cluster.py

102 lines
2.7 KiB
Python
Raw Normal View History

import math
import cairo
2023-12-29 23:45:46 -05:00
from typing import Iterable, List
2023-12-31 19:12:19 -05:00
from hexagram.pattern import HexagonPattern
from hexagram.gauge import Gauge
from hexagram.dial import Dial
2023-12-31 19:12:19 -05:00
from hexagram.speedo import Speedo
2023-12-31 22:17:02 -05:00
from hexagram.tacho import Tacho
2023-12-29 18:43:06 -05:00
class ShiftIndicator(Gauge):
2023-12-31 22:42:25 -05:00
__slots__ = 'x', 'y', 'rpm_redline', 'rpm_max',
2023-12-29 18:43:06 -05:00
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,),
)
2023-12-29 18:54:20 -05:00
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
2023-12-29 18:43:06 -05:00
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()
2023-12-29 17:35:17 -05:00
class Cluster():
WIDTH = 1280
HEIGHT = 480
2023-12-29 18:43:06 -05:00
SHIFT_INDICATOR_X = 392
SHIFT_INDICATOR_Y = 24
2023-12-31 19:12:19 -05:00
__slots__ = 'gauges', 'speedo', 'tacho',
2023-12-29 17:35:17 -05:00
2023-12-29 18:54:20 -05:00
def __init__(self, rpm_redline: float, rpm_max: float):
2023-12-29 23:45:46 -05:00
self.gauges: List[Gauge] = list()
2023-12-29 17:35:17 -05:00
2023-12-31 19:12:19 -05:00
self.speedo = Speedo(self.HEIGHT / 2,
self.HEIGHT / 2,
self.HEIGHT / 2,
2023-12-31 20:31:12 -05:00
180.0)
2023-12-31 19:12:19 -05:00
2023-12-31 22:17:02 -05:00
self.tacho = Tacho(self.WIDTH - self.HEIGHT / 2,
self.HEIGHT / 2,
self.HEIGHT / 2,
8000)
2023-12-31 19:12:19 -05:00
self.gauges.append(self.speedo)
self.gauges.append(self.tacho)
2023-12-29 18:43:06 -05:00
self.gauges.append(ShiftIndicator(self.SHIFT_INDICATOR_X,
self.SHIFT_INDICATOR_Y,
2023-12-29 18:54:20 -05:00
rpm_redline,
rpm_max))
2023-12-29 17:35:17 -05:00
def draw_bg(self, cr: cairo.Context):
2023-12-31 18:39:54 -05:00
cr.set_source_rgb(0.1, 0.1, 0.1)
2023-12-30 01:23:50 -05:00
cr.rectangle(0, 0, self.WIDTH, self.HEIGHT)
cr.fill()
2023-12-30 01:22:49 -05:00
cr.rectangle(0, 0, self.WIDTH, self.HEIGHT)
2023-12-29 21:37:10 -05:00
2023-12-29 23:45:46 -05:00
pattern = HexagonPattern()
2023-12-30 01:22:49 -05:00
pattern.fill(cr)
2023-12-29 21:37:10 -05:00
2023-12-29 17:35:17 -05:00
for gauge in self.gauges:
gauge.draw_bg(cr)