Skip to content

Use vectorio instead of bitmap #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 73 additions & 30 deletions adafruit_progressbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@

# imports
import displayio
import vectorio

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/brentru/Adafruit_CircuitPython_ProgressBar.git"

# pylint: disable=too-many-arguments, too-few-public-methods
class ProgressBar(displayio.TileGrid):
class ProgressBar(displayio.Group):
"""A dynamic progress bar widget.

:param int x: The x-position of the top left corner.
Expand All @@ -61,7 +62,7 @@ class ProgressBar(displayio.TileGrid):

"""

# pylint: disable=invalid-name
# pylint: disable=invalid-name,too-many-instance-attributes
def __init__(
self,
x,
Expand All @@ -74,7 +75,7 @@ def __init__(
stroke=1,
):
assert isinstance(progress, float), "Progress must be a floating point value."
self._bitmap = displayio.Bitmap(width, height, 3)

self._palette = displayio.Palette(3)
self._palette[0] = 0x0
self._palette[1] = outline_color
Expand All @@ -84,18 +85,68 @@ def __init__(
self._height = height

self._progress_val = progress
self.progress = self._progress_val

self._stroke = stroke
# draw outline rectangle
for _w in range(width):
for line in range(stroke):
self._bitmap[_w, line] = 1
self._bitmap[_w, height - 1 - line] = 1
for _h in range(height):
for line in range(stroke):
self._bitmap[line, _h] = 1
self._bitmap[width - 1 - line, _h] = 1
super().__init__(self._bitmap, pixel_shader=self._palette, x=x, y=y)
_outline_palette = displayio.Palette(2)
_outline_palette.make_transparent(0)
_outline_palette[1] = self._palette[1]

self._outline_rect = vectorio.Polygon(
points=[
(0, 0),
(self._width, 0),
(self._width, self._height),
(0, self._height),
]
)
self._outline_rect_shape = vectorio.VectorShape(
shape=self._outline_rect, x=0, y=0, pixel_shader=_outline_palette
)
# draw inner outline rectangle
_inner_outline_palette = displayio.Palette(2)
_inner_outline_palette[1] = self._palette[0]
_inner_outline_palette.make_transparent(0)
self._inner_outline_rect = vectorio.Polygon(
points=[
(0, 0),
(self._width - 1 - stroke, 0),
(self._width - 1 - stroke, self._height - 1 - stroke),
(0, self._height - 1 - stroke),
]
)
self._inner_outline_rect_shape = vectorio.VectorShape(
shape=self._inner_outline_rect,
x=stroke // 2 + 1,
y=stroke // 2 + 1,
pixel_shader=_inner_outline_palette,
)
# draw fill bar
self._fill_bar_max = self._width - 3 - stroke
_fill_bar_palette = displayio.Palette(2)
_fill_bar_palette[1] = self._palette[2]
_fill_bar_palette.make_transparent(0)
self._fill_bar_rect = vectorio.Polygon(
points=[
(0, 0),
(int(self._fill_bar_max * self._progress_val), 0),
(
int(self._fill_bar_max * self._progress_val),
self._height - 3 - stroke,
),
(0, self._height - 3 - stroke),
]
)
self._fill_bar_rect_shape = vectorio.VectorShape(
shape=self._fill_bar_rect,
x=1 + (stroke // 2 + 1),
y=1 + (stroke // 2 + 1),
pixel_shader=_fill_bar_palette,
)

super().__init__(max_size=3, scale=1, x=x, y=y)
self.append(self._outline_rect_shape)
self.append(self._inner_outline_rect_shape)
self.append(self._fill_bar_rect_shape)

@property
def progress(self):
Expand All @@ -115,22 +166,14 @@ def progress(self, value):
assert isinstance(
value, float
), "Progress value must be a floating point value."
if self._progress_val > value:
# uncolorize range from width*value+margin to width-margin
# from right to left
_prev_pixel = max(2, int(self._width * self._progress_val - 2))
_new_pixel = max(int(self._width * value - 2), 2)
for _w in range(_prev_pixel, _new_pixel - 1, -1):
for _h in range(2, self._height - 2):
self._bitmap[_w, _h] = 0
else:
# fill from the previous x pixel to the new x pixel
_prev_pixel = max(2, int(self._width * self._progress_val - 3))
_new_pixel = min(int(self._width * value - 2), int(self._width * 1.0 - 3))
for _w in range(_prev_pixel, _new_pixel + 1):
for _h in range(2, self._height - 2):
self._bitmap[_w, _h] = 2
self._progress_val = value

_new_points = [
(0, 0),
(int(self._fill_bar_max * value), 0),
(int(self._fill_bar_max * value), self._height - 3 - self._stroke),
(0, self._height - 3 - self._stroke),
]
self._fill_bar_rect.points = _new_points

@property
def fill(self):
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# Uncomment the below if you use native CircuitPython modules such as
# digitalio, micropython and busio. List the modules you use. Without it, the
# autodoc module docs will fail to generate with a warning.
autodoc_mock_imports = ["displayio"]
autodoc_mock_imports = ["displayio", "vectorio"]

intersphinx_mapping = {
"python": ("https://docs.python.org/3.4", None),
Expand Down