From af993077ed1a4f1045f217d54c4ec32adcac78ca Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 31 Dec 2023 14:16:19 -0500 Subject: [PATCH] Factor out more classes from cluster.py --- py/hexagram/cluster.py | 50 ++---------------------------------------- py/hexagram/dial.py | 48 ++++++++++++++++++++++++++++++++++++++++ py/hexagram/gauge.py | 5 +++++ 3 files changed, 55 insertions(+), 48 deletions(-) create mode 100644 py/hexagram/dial.py create mode 100644 py/hexagram/gauge.py diff --git a/py/hexagram/cluster.py b/py/hexagram/cluster.py index 1ce69a2..98a1d4d 100644 --- a/py/hexagram/cluster.py +++ b/py/hexagram/cluster.py @@ -3,56 +3,10 @@ import cairo from typing import Iterable, List +from hexagram.gauge import Gauge +from hexagram.dial import Dial from hexagram.pattern import HexagonPattern -class Gauge(): - def draw_bg(self, cr: cairo.Context): - raise NotImplementedError - -class Dial(Gauge): - __slots = 'x', 'y', 'radius', - - BEZEL_WIDTH = 16 - - BEZEL_GRADIENT_STOPS = ( - (0, 1.0, 0.4, 1.0, 1.0), - (1, 0.4, 0.0, 0.4, 0.0) - ) - - def __init__(self, x: float, y: float, radius: float): - self.x = x - self.y = y - self.radius = radius - - def _gradient(self): - gradient = cairo.LinearGradient(self.x - self.radius, - self.y - self.radius, - self.x + self.radius, - self.y + self.radius) - - for stop in self.BEZEL_GRADIENT_STOPS: - gradient.add_color_stop_rgba(*stop) - - return gradient - - def draw_bg(self, cr: cairo.Context): - arc = (self.x, - self.y, - self.radius - self.BEZEL_WIDTH, - 0, - 2.0 * math.pi) - - # Gauge face - cr.set_source_rgba(0, 0, 0, 1) - cr.arc(*arc) - cr.fill() - - # Gauge bezel - cr.set_source(self._gradient()) - cr.set_line_width(self.BEZEL_WIDTH) - cr.arc(*arc) - cr.stroke() - class ShiftIndicator(Gauge): LIGHT_WIDTH = 48 LIGHT_HEIGHT = 12 diff --git a/py/hexagram/dial.py b/py/hexagram/dial.py new file mode 100644 index 0000000..a2f29cf --- /dev/null +++ b/py/hexagram/dial.py @@ -0,0 +1,48 @@ +import math +import cairo + +from hexagram.gauge import Gauge + +class Dial(Gauge): + __slots = 'x', 'y', 'radius', + + BEZEL_WIDTH = 16 + + BEZEL_GRADIENT_STOPS = ( + (0, 1.0, 0.4, 1.0, 1.0), + (1, 0.4, 0.0, 0.4, 0.0) + ) + + def __init__(self, x: float, y: float, radius: float): + self.x = x + self.y = y + self.radius = radius + + def _gradient(self): + gradient = cairo.LinearGradient(self.x - self.radius, + self.y - self.radius, + self.x + self.radius, + self.y + self.radius) + + for stop in self.BEZEL_GRADIENT_STOPS: + gradient.add_color_stop_rgba(*stop) + + return gradient + + def draw_bg(self, cr: cairo.Context): + arc = (self.x, + self.y, + self.radius - self.BEZEL_WIDTH, + 0, + 2.0 * math.pi) + + # Gauge face + cr.set_source_rgba(0, 0, 0, 1) + cr.arc(*arc) + cr.fill() + + # Gauge bezel + cr.set_source(self._gradient()) + cr.set_line_width(self.BEZEL_WIDTH) + cr.arc(*arc) + cr.stroke() diff --git a/py/hexagram/gauge.py b/py/hexagram/gauge.py new file mode 100644 index 0000000..b443e3a --- /dev/null +++ b/py/hexagram/gauge.py @@ -0,0 +1,5 @@ +import cairo + +class Gauge(): + def draw_bg(self, cr: cairo.Context): + raise NotImplementedError