Add comments in UiBar.py

This commit is contained in:
Michał Czekański 2020-05-14 21:10:51 +02:00 committed by Marcin Kostrzewski
parent bb46f56ace
commit 9d1ecf5d90

View File

@ -1,11 +1,30 @@
from typing import Tuple
import pygame import pygame
from src.ui.UiElement import UiElement from src.ui.UiElement import UiElement
class UiBar(UiElement): class UiBar(UiElement):
def __init__(self, rect: pygame.Rect, initialFilledPercent=100, filledBarColor=(255, 0, 0), emptyBarColor=(0, 0, 0), value: int
outlineColor=(75, 75, 75), outlineThickness=10): filledBarColor: Tuple[int, int, int]
outlineThickness: int
outlineColor: Tuple[int, int, int]
emptyBarColor: Tuple[int, int, int]
filledPercent: float
def __init__(self, rect: pygame.Rect, initialFilledPercent: int = 100,
filledBarColor: Tuple[int, int, int] = (255, 0, 0), emptyBarColor: Tuple[int, int, int] = (0, 0, 0),
outlineColor: Tuple[int, int, int] = (75, 75, 75), outlineThickness: int = 10):
"""
Creates UiBar object
:param rect:
:param initialFilledPercent: How much bar is filled at the beginning. Number between 0 and 100
:param filledBarColor: Color of the filled part of the bar.
:param emptyBarColor: Color of the empty part of the bar.
:param outlineColor: Color of the bar outline.
:param outlineThickness:
"""
super().__init__(rect) super().__init__(rect)
self.filledPercent = initialFilledPercent / 100 self.filledPercent = initialFilledPercent / 100
self.emptyBarColor = emptyBarColor self.emptyBarColor = emptyBarColor
@ -18,6 +37,9 @@ class UiBar(UiElement):
self.__genBar__() self.__genBar__()
def __genBar__(self): def __genBar__(self):
"""
Generates bar image.
"""
self.image = pygame.Surface((self.rect.width, self.rect.height)) self.image = pygame.Surface((self.rect.width, self.rect.height))
filledPartRect = pygame.rect.Rect(self.outlineThickness / 2, self.outlineThickness / 2, filledPartRect = pygame.rect.Rect(self.outlineThickness / 2, self.outlineThickness / 2,
(self.rect.width - self.outlineThickness) * self.filledPercent, (self.rect.width - self.outlineThickness) * self.filledPercent,
@ -26,7 +48,11 @@ class UiBar(UiElement):
pygame.draw.rect(self.image, self.outlineColor, pygame.rect.Rect(0, 0, self.rect.width, self.rect.height), pygame.draw.rect(self.image, self.outlineColor, pygame.rect.Rect(0, 0, self.rect.width, self.rect.height),
self.outlineThickness) self.outlineThickness)
def updateFill(self, filledPercent): def updateFill(self, filledPercent: int):
"""
Updates how much bar is filled
:param filledPercent: Value between 0 and 100
"""
self.filledPercent = filledPercent / 100 self.filledPercent = filledPercent / 100
self.value = filledPercent self.value = filledPercent
self.__genBar__() self.__genBar__()