Skip to content

Commit e1f87ae

Browse files
committed
Start importing and refactoring vertical bat
Still displays horizontally (for testing). Will flip to vertical rendering as the next change.
1 parent d91cc96 commit e1f87ae

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

adafruit_progressbar/verticalprogressbar.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,37 @@ def __init__(
130130
self._x = anchor_position[0]
131131
self._y = anchor_position[1]
132132

133+
self._stroke = stroke
134+
135+
super().__init__(
136+
anchor_position,
137+
size,
138+
progress,
139+
bar_color,
140+
outline_color,
141+
0x444444,
142+
border_thickness=stroke,
143+
show_margin=True,
144+
value_range=(0.0, 1.0),
145+
)
146+
147+
self._draw_outline()
148+
149+
def _draw_outline(self):
150+
"""
151+
Draws the outline (border) of the widget.
152+
"""
153+
133154
# draw outline rectangle
134155
for _w in range(self._width):
135-
for line in range(stroke):
156+
for line in range(self._stroke):
136157
self._bitmap[_w, line] = 1
137158
self._bitmap[_w, self._height - 1 - line] = 1
138159
for _h in range(self._height):
139-
for line in range(stroke):
160+
for line in range(self._stroke):
140161
self._bitmap[line, _h] = 1
141162
self._bitmap[self._width - 1 - line, _h] = 1
142163

143-
# pylint: disable=unexpected-keyword-arg, no-value-for-parameter
144-
super().__init__(self._bitmap, pixel_shader=self._palette, x=self._x, y=self._y)
145-
146164
@property
147165
def progress(self):
148166
"""The percentage of the progress bar expressed as a
@@ -162,9 +180,30 @@ def progress(self, value):
162180
assert value <= self._max, "Progress value may not be > maximum value"
163181
assert value >= self._min, "Progress value may not be < minimum value"
164182

183+
_old_value = self._progress_val
165184
_new_value = round(value / self._max, 2)
185+
self._progress_val = _new_value
186+
self.render(_old_value, _new_value, value)
187+
188+
def render(self, old_value, new_value, progress):
189+
"""
190+
Does the work of actually creating the graphical representation of
191+
the value (percentage, aka "progress") to be displayed.
192+
193+
:param old_value: The previously displayed value
194+
:type old_value: float
195+
:param new_value: The new value to display
196+
:type new_value: float
197+
:param progress: The value to display, as a percentage, represented
198+
by a float from 0.0 to 1.0 (0% to 100%)
199+
:type progress: float
200+
:return: None
201+
:rtype: None
202+
"""
166203
_padding = 1
167204

205+
print(f"Drawing a visual of progress value {progress}")
206+
168207
if self._margin:
169208
_padding = 1
170209

@@ -181,18 +220,18 @@ def progress(self, value):
181220
self.height - (2 * _padding) - _border_size
182221
) # Count padding on the top and bottom
183222

184-
_prev_value_size = int(self._progress_val * _fill_width)
185-
_new_value_size = int(_new_value * _fill_width)
223+
_prev_value_size = int(old_value * _fill_width)
224+
_new_value_size = int(new_value * _fill_width)
186225

187226
# If we have *ANY* value other than "zero" (minimum), we should
188227
# have at least one element showing
189-
if _new_value_size == 0 and value > self._min:
228+
if _new_value_size == 0 and new_value > self._min:
190229
_new_value_size = 1
191230

192231
# Conversely, if we have *ANY* value other than 100% (maximum),
193232
# we should NOT show a full bar.
194233

195-
if _new_value_size == _fill_width and value < self._max:
234+
if _new_value_size == _fill_width and new_value < self._max:
196235
_new_value_size -= 1
197236

198237
# Default values for increasing value
@@ -214,22 +253,14 @@ def progress(self, value):
214253
else:
215254
pass
216255

217-
# DEBUG
218-
print(
219-
f"Start: {_start} End: {_end} Incr: {_incr} Size: {_new_value_size} Color: {_color}"
220-
)
221-
222256
# Because range() is ( from-include, to-exclude )...
223-
224257
_vert_start = _border_thickness + _padding
225258
_vert_end = _vert_start + _fill_height
226259

227260
for h in range(_vert_start, _vert_end):
228261
for w in range(_start, _end, _incr):
229262
self._bitmap[w, h] = _color
230263

231-
self._progress_val = _new_value
232-
233264
@property
234265
def fill(self):
235266
"""The fill of the progress bar. Can be a hex value for a color or ``None`` for

0 commit comments

Comments
 (0)