Merge branch 'CodeUpdate_WriteToConsole' into UI_doc

This commit is contained in:
Michał Czekański 2020-05-15 01:17:43 +02:00
commit 7bf924e2c9

View File

@ -1,10 +1,26 @@
import pygame
from typing import Tuple, List
from pygame.font import FontType
from src.ui.UiElement import UiElement
class UiConsole(UiElement):
def __init__(self, rect: pygame.Rect, bgColor=(125, 125, 125), textColor=(255, 255, 255), font=None, antialias=True):
linesCount: int
antialias: bool
bgColor: Tuple[int, int, int]
font: pygame.font.Font
maxLines: int
lineHeight: int
linesImages: List[pygame.Surface]
topWrittenLineInd: int
consoleLines: List[str]
linesImagesCount: int
consoleWidth: float
image: pygame.Surface
def __init__(self, rect: pygame.Rect, bgColor: Tuple[int, int, int] = (125, 125, 125),
textColor: Tuple[int, int, int] = (255, 255, 255), font: pygame.font.Font = None, antialias: bool = True):
super().__init__(rect)
self.textColor = textColor
@ -32,7 +48,19 @@ class UiConsole(UiElement):
self.addLinesToConsole(["Hello from console!"])
self.writeConsoleLines()
def writeConsoleLines(self, startingLineInd=0):
# TODO: Should this method be static? We need to instantiate some console before we can write anything to it.
def writeToConsole(self, inp: str):
"""
Writes given string to console and scrolls (console) down to display it.
:param inp: String to be written to console.
"""
self.addLinesToConsoleAndScrollToDisplayThem([inp])
def writeConsoleLines(self, startingLineInd: int = 0):
"""
Displays lines stored in console's list of lines, starting from line with given index.
:param startingLineInd: Line index, which will be written on top of the console.
"""
self.image.fill(self.bgColor)
if startingLineInd < 0:
startingLineInd = 0
@ -44,7 +72,14 @@ class UiConsole(UiElement):
self.image.blit(self.linesImages[i], (0, writtenLines * self.lineHeight))
writtenLines += 1
def addLinesToConsole(self, linesToAdd):
def addLinesToConsole(self, linesToAdd: List[str]):
"""
Adds lines to console's list of lines. If one line is too long to display, then it is being cut to pieces,
so that it is appropriate size.
Warning: this method doesn't display given lines, just adds to list of lines.
:param linesToAdd:
"""
for line in linesToAdd:
self.consoleLines.append(line)
self.linesCount += 1
@ -72,7 +107,11 @@ class UiConsole(UiElement):
self.linesImages.append(row)
self.linesImagesCount += 1
def addLinesToConsoleAndScrollToDisplayThem(self, linesToAdd):
def addLinesToConsoleAndScrollToDisplayThem(self, linesToAdd: List[str]):
"""
Adds given lines to console's list of lines, writes them and scrolls console down to display them.
:param linesToAdd: Lines to add to console's list of lines and to display.
"""
self.addLinesToConsole(linesToAdd)
ind = 0
if self.linesImagesCount > self.maxLines: