From 11aee1232054cacdeef4e1baabc31670d7b42fed Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Mon, 16 May 2022 16:40:04 -0700 Subject: [PATCH] Add colors attribute to Bar class This list lets you specify a set of colors that the bar should be painted with depending on the percentage the bar is filled --- erynrl/interface/bar.py | 43 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/erynrl/interface/bar.py b/erynrl/interface/bar.py index 3bc12df..10b1ed4 100644 --- a/erynrl/interface/bar.py +++ b/erynrl/interface/bar.py @@ -1,12 +1,37 @@ # Eryn Wells +from typing import List, Optional, Tuple + from . import color from ..geometry import Point class Bar: - def __init__(self, *, position: Point, width: int): + '''A bar that expresses a percentage.''' + + def __init__(self, *, position: Point, width: int, colors: Optional[List[Tuple[float, color.Color]]] = None): + ''' + Instantiate a new Bar + + Arguments + --------- + position : Point + The position within a console to render this bar + width : int + The length of the bar in tiles + colors : List[Tuple[float, color.Color]] + A list of two-tuples specifying a percentage and color to draw the bar. If the bar is less than or equal to + the specified percentage, that color will be chosen. For example, if the bar is 45% filled, and this colors + array is specified: + + ``` + [(0.25, RED), (0.5, ORANGE), (0.75, YELLOW), (1.0, GREEN)] + ``` + + The bar will be painted `ORANGE` because 45% is greater than 25% and less than 50%. + ''' self.position = position self.width = width + self.colors = sorted(colors, key=lambda c: c[0]) if colors is not None else [] self._percent_filled = 1.0 @@ -20,8 +45,18 @@ class Bar: self._percent_filled = min(1, max(0, value)) def render_to_console(self, console): - console.draw_rect(x=self.position.x, y=self.position.y, width=self.width, height=1, ch=1, bg=color.GREY10) + '''Draw this bar to the console''' + # Draw the background first + console.draw_rect(x=self.position.x, y=self.position.y, width=self.width, height=1, ch=1, bg=color.GREY12) + + percent_filled = self._percent_filled + if percent_filled > 0: + for color_spec in self.colors: + if percent_filled <= color_spec[0]: + bar_color = color_spec[1] + break + else: + bar_color = color.GREY50 - if self._percent_filled > 0: filled_width = round(self._percent_filled * self.width) - console.draw_rect(x=self.position.x, y=self.position.y, width=filled_width, height=1, ch=1, bg=color.GREY50) + console.draw_rect(x=self.position.x, y=self.position.y, width=filled_width, height=1, ch=1, bg=bar_color)