Skip to content

Commit

Permalink
Merge pull request #500 from caerffili/advanced_radials
Browse files Browse the repository at this point in the history
  • Loading branch information
mathoudebine authored Dec 22, 2024
2 parents 164bb2a + a2575ad commit d90560d
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 4 deletions.
77 changes: 74 additions & 3 deletions library/lcd/lcd_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,29 @@ def DisplayLineGraph(self, x: int, y: int, width: int, height: int,

self.DisplayPILImage(graph_image, x, y)

def DrawRadialDecoration(self, draw: ImageDraw, angle: float, radius: float, width: float, color: Tuple[int, int, int] = (0, 0, 0)):
i_cos = math.cos(angle*math.pi/180)
i_sin = math.sin(angle*math.pi/180)
x_f = (i_cos * (radius - width/2)) + radius
if math.modf(x_f) == 0.5:
if i_cos > 0:
x_f = math.floor(x_f)
else:
x_f = math.ceil(x_f)
else:
x_f = math.floor(x_f + 0.5)

y_f = (i_sin * (radius - width/2)) + radius
if math.modf(y_f) == 0.5:
if i_sin > 0:
y_f = math.floor(y_f)
else:
y_f = math.ceil(y_f)
else:
y_f = math.floor(y_f + 0.5)
draw.ellipse([x_f - width/2, y_f - width/2, x_f + width/2, y_f - 1 + width/2 - 1], outline=color, fill=color, width=1)


def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int,
min_value: int = 0,
max_value: int = 100,
Expand All @@ -461,7 +484,12 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
font_color: Tuple[int, int, int] = (0, 0, 0),
bar_color: Tuple[int, int, int] = (0, 0, 0),
background_color: Tuple[int, int, int] = (255, 255, 255),
background_image: str = None):
background_image: str = None,
custom_bbox: Tuple[int, int, int, int] = (0, 0, 0, 0),
text_offset: Tuple[int, int] = (0,0),
bar_background_color: Tuple[int, int, int] = (0, 0, 0),
draw_bar_background: bool = False,
bar_decoration: str = ""):
# Generate a radial progress bar and display it
# Provide the background image path to display progress bar with transparent background

Expand All @@ -474,6 +502,9 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
if isinstance(font_color, str):
font_color = tuple(map(int, font_color.split(', ')))

if isinstance(bar_background_color, str):
bar_background_color = tuple(map(int, bar_background_color.split(', ')))

if angle_start % 361 == angle_end % 361:
if clockwise:
angle_start += 0.1
Expand Down Expand Up @@ -525,6 +556,23 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
ecart = 360 - angle_start + angle_end
else:
ecart = angle_end - angle_start

# draw bar background
if draw_bar_background:
if angle_end < angle_start:
angleE = angle_start + ecart
angleS = angle_start
else:
angleS = angle_start
angleE = angle_start + ecart
draw.arc([0, 0, diameter - 1, diameter - 1], angleS, angleE, fill=bar_background_color, width=bar_width)

# draw bar decoration
if bar_decoration == "Ellipse":
self.DrawRadialDecoration(draw = draw, angle = angle_end, radius = radius, width = bar_width, color = bar_background_color)
self.DrawRadialDecoration(draw = draw, angle = angle_start, radius = radius, width = bar_width, color = bar_color)
self.DrawRadialDecoration(draw = draw, angle = angle_start + pct * ecart, radius = radius, width = bar_width, color = bar_color)

#
# solid bar case
if angle_sep == 0:
Expand Down Expand Up @@ -558,6 +606,25 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
ecart = angle_start - angle_end
else:
ecart = 360 - angle_end + angle_start

# draw bar background
if draw_bar_background:
if angle_end < angle_start:
angleE = angle_start
angleS = angle_start - ecart
else:
angleS = angle_start - ecart
angleE = angle_start
draw.arc([0, 0, diameter - 1, diameter - 1], angleS, angleE, fill=bar_background_color, width=bar_width)


# draw bar decoration
if bar_decoration == "Ellipse":
self.DrawRadialDecoration(draw = draw, angle = angle_end, radius = radius, width = bar_width, color = bar_background_color)
self.DrawRadialDecoration(draw = draw, angle = angle_start, radius = radius, width = bar_width, color = bar_color)
self.DrawRadialDecoration(draw = draw, angle = angle_start - pct * ecart, radius = radius, width = bar_width, color = bar_color)

#
# solid bar case
if angle_sep == 0:
if angle_end < angle_start:
Expand Down Expand Up @@ -593,10 +660,14 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
font = ImageFont.truetype("./res/fonts/" + font, font_size)
left, top, right, bottom = font.getbbox(text)
w, h = right - left, bottom - top
draw.text((radius - w / 2, radius - top - h / 2), text,
draw.text((radius - w / 2 + text_offset[0], radius - top - h / 2 + text_offset[1]), text,
font=font, fill=font_color)

self.DisplayPILImage(bar_image, xc - radius, yc - radius)
if custom_bbox[0] != 0 or custom_bbox[1] != 0 or custom_bbox[2] != 0 or custom_bbox[3] != 0:
bar_image = bar_image.crop(box=custom_bbox)

self.DisplayPILImage(bar_image, xc - radius + custom_bbox[0], yc - radius + custom_bbox[1])
# self.DisplayPILImage(bar_image, xc - radius, yc - radius)

# Load image from the filesystem, or get from the cache if it has already been loaded previously
def open_image(self, bitmap_path: str) -> Image:
Expand Down
7 changes: 6 additions & 1 deletion library/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ def display_themed_radial_bar(theme_data, value, min_size=0, unit='', custom_tex
font_size=theme_data.get("FONT_SIZE", 10),
font_color=theme_data.get("FONT_COLOR", (0, 0, 0)),
background_color=theme_data.get("BACKGROUND_COLOR", (0, 0, 0)),
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None))
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None)),
custom_bbox=theme_data.get("CUSTOM_BBOX", (0, 0, 0, 0)),
text_offset=theme_data.get("TEXT_OFFSET", (0, 0)),
bar_background_color = theme_data.get("BAR_BACKGROUND_COLOR", (0, 0, 0)),
draw_bar_background = theme_data.get("DRAW_BAR_BACKGROUND", False),
bar_decoration = theme_data.get("BAR_DECORATION", "")
)


Expand Down
4 changes: 4 additions & 0 deletions res/themes/Advanced Radials Test/Credits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Credits for graphical resources used from freepik

Acknowledgements for resources :

Binary file added res/themes/Advanced Radials Test/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/themes/Advanced Radials Test/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d90560d

Please sign in to comment.